blob: 396a9ac806e21d1701b96f99534a53ea029e7d18 [file] [log] [blame]
egdaniel7ea439b2015-12-03 09:20:44 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/GrGLUniformHandler.h"
egdaniel7ea439b2015-12-03 09:20:44 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrTexturePriv.h"
11#include "src/gpu/gl/GrGLCaps.h"
12#include "src/gpu/gl/GrGLGpu.h"
13#include "src/gpu/gl/builders/GrGLProgramBuilder.h"
14#include "src/sksl/SkSLCompiler.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015
16#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
17#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
18
Robert Phillipsfe8da172018-01-24 14:52:02 +000019bool valid_name(const char* name) {
20 // disallow unknown names that start with "sk_"
21 if (!strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
22 return !strcmp(name, SkSL::Compiler::RTADJUST_NAME);
23 }
24 return true;
25}
26
egdaniel7ea439b2015-12-03 09:20:44 -080027GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
Ethan Nicholas16464c32020-04-06 13:53:05 -040028 const GrFragmentProcessor* owner,
29 uint32_t visibility,
30 GrSLType type,
31 const char* name,
32 bool mangleName,
33 int arrayCount,
34 const char** outName) {
egdaniel7ea439b2015-12-03 09:20:44 -080035 SkASSERT(name && strlen(name));
Robert Phillipsfe8da172018-01-24 14:52:02 +000036 SkASSERT(valid_name(name));
egdaniel7ea439b2015-12-03 09:20:44 -080037 SkASSERT(0 != visibility);
egdaniel7ea439b2015-12-03 09:20:44 -080038
egdaniel7ea439b2015-12-03 09:20:44 -080039 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
40 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
41 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
42 // the names will mismatch. I think the correct solution is to have all GPs which need the
43 // uniform view matrix, they should upload the view matrix in their setData along with regular
44 // uniforms.
Ben Wagnerdabd98c2020-03-25 15:17:18 -040045 SkString resolvedName;
egdaniel7ea439b2015-12-03 09:20:44 -080046 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +000047 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
egdaniel7ea439b2015-12-03 09:20:44 -080048 prefix = '\0';
49 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -040050 fProgramBuilder->nameVariable(&resolvedName, prefix, name, mangleName);
Ethan Nicholas16464c32020-04-06 13:53:05 -040051 GLUniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::GLUniformInfo{
52 {
53 GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform,
54 arrayCount},
55 visibility, owner, SkString(name)
56 },
57 -1
Ben Wagnerdabd98c2020-03-25 15:17:18 -040058 });
egdaniel7ea439b2015-12-03 09:20:44 -080059
60 if (outName) {
61 *outName = uni.fVariable.c_str();
62 }
63 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
64}
65
Chris Dalton1b1b0d52020-03-03 12:00:59 -070066GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
67 const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle,
68 const char* name, const GrShaderCaps* shaderCaps) {
egdaniel09aa1fc2016-04-20 07:09:46 -070069 SkASSERT(name && strlen(name));
Brian Salomon101b8442016-11-18 11:58:54 -050070
egdaniel09aa1fc2016-04-20 07:09:46 -070071 SkString mangleName;
72 char prefix = 'u';
73 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -050074
Chris Dalton1b1b0d52020-03-03 12:00:59 -070075 GrTextureType type = backendFormat.textureType();
Greg Daniel9a51a862018-11-30 10:18:14 -050076
Ethan Nicholas16464c32020-04-06 13:53:05 -040077 fSamplers.push_back(GrGLProgramDataManager::GLUniformInfo{
78 {
79 GrShaderVar{std::move(mangleName), GrSLCombinedSamplerTypeForTextureType(type),
80 GrShaderVar::TypeModifier::Uniform},
81 kFragment_GrShaderFlag, nullptr, SkString(name)
82 },
83 -1
Ben Wagnerdabd98c2020-03-25 15:17:18 -040084 });
85
Brian Salomon68ba1172019-06-05 11:15:08 -040086 if (shaderCaps->textureSwizzleAppliedInShader()) {
87 fSamplerSwizzles.push_back(swizzle);
88 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
89 }
egdaniel09aa1fc2016-04-20 07:09:46 -070090 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
91}
92
cdalton5e58cee2016-02-11 12:49:47 -080093void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Michael Ludwig45191342020-03-24 12:29:39 -040094 for (const UniformInfo& uniform : fUniforms.items()) {
95 if (uniform.fVisibility & visibility) {
96 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -050097 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -080098 }
99 }
Michael Ludwig45191342020-03-24 12:29:39 -0400100 for (const UniformInfo& sampler : fSamplers.items()) {
101 if (sampler.fVisibility & visibility) {
102 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700103 out->append(";\n");
104 }
105 }
egdaniel7ea439b2015-12-03 09:20:44 -0800106}
107
108void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
109 if (caps.bindUniformLocationSupport()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500110 int currUniform = 0;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400111 for (GLUniformInfo& uniform : fUniforms.items()) {
Michael Ludwig45191342020-03-24 12:29:39 -0400112 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
113 uniform.fLocation = currUniform;
114 ++currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800115 }
Ethan Nicholas16464c32020-04-06 13:53:05 -0400116 for (GLUniformInfo& sampler : fSamplers.items()) {
Michael Ludwig45191342020-03-24 12:29:39 -0400117 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
118 sampler.fLocation = currUniform;
119 ++currUniform;
Brian Salomonf9f45122016-11-29 11:59:17 -0500120 }
egdaniel7ea439b2015-12-03 09:20:44 -0800121 }
122}
123
Brian Osman2c60a382019-06-26 15:19:30 -0400124void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
125 if (!caps.bindUniformLocationSupport() || force) {
Ethan Nicholas16464c32020-04-06 13:53:05 -0400126 for (GLUniformInfo& uniform : fUniforms.items()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800127 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400128 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
129 uniform.fLocation = location;
egdaniel7ea439b2015-12-03 09:20:44 -0800130 }
Ethan Nicholas16464c32020-04-06 13:53:05 -0400131 for (GLUniformInfo& sampler : fSamplers.items()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700132 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400133 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
134 sampler.fLocation = location;
egdaniel09aa1fc2016-04-20 07:09:46 -0700135 }
egdaniel7ea439b2015-12-03 09:20:44 -0800136 }
137}
138
139const GrGLGpu* GrGLUniformHandler::glGpu() const {
140 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
141 return glPB->gpu();
142}