1 /*
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
8 #include "src/gpu/gl/GrGLUniformHandler.h"
9
10 #include "src/gpu/GrTexture.h"
11 #include "src/gpu/GrUtil.h"
12 #include "src/gpu/gl/GrGLCaps.h"
13 #include "src/gpu/gl/GrGLGpu.h"
14 #include "src/gpu/gl/builders/GrGLProgramBuilder.h"
15 #include "src/sksl/SkSLCompiler.h"
16
17 #define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
18 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
19
valid_name(const char * name)20 bool valid_name(const char* name) {
21 // disallow unknown names that start with "sk_"
22 if (!strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
23 return !strcmp(name, SkSL::Compiler::RTADJUST_NAME);
24 }
25 return true;
26 }
27
internalAddUniformArray(const GrFragmentProcessor * owner,uint32_t visibility,SkSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)28 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
29 const GrFragmentProcessor* owner,
30 uint32_t visibility,
31 SkSLType type,
32 const char* name,
33 bool mangleName,
34 int arrayCount,
35 const char** outName) {
36 SkASSERT(name && strlen(name));
37 SkASSERT(valid_name(name));
38 SkASSERT(0 != visibility);
39
40 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
41 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
42 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
43 // the names will mismatch. I think the correct solution is to have all GPs which need the
44 // uniform view matrix, they should upload the view matrix in their setData along with regular
45 // uniforms.
46 char prefix = 'u';
47 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
48 prefix = '\0';
49 }
50 SkString resolvedName = fProgramBuilder->nameVariable(prefix, name, mangleName);
51
52 GLUniformInfo tempInfo;
53 tempInfo.fVariable = GrShaderVar{std::move(resolvedName),
54 type,
55 GrShaderVar::TypeModifier::Uniform,
56 arrayCount};
57
58 tempInfo.fVisibility = visibility;
59 tempInfo.fOwner = owner;
60 tempInfo.fRawName = SkString(name);
61 tempInfo.fLocation = -1;
62
63 fUniforms.push_back(tempInfo);
64
65 if (outName) {
66 *outName = fUniforms.back().fVariable.c_str();
67 }
68 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
69 }
70
addSampler(const GrBackendFormat & backendFormat,GrSamplerState,const skgpu::Swizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)71 GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
72 const GrBackendFormat& backendFormat, GrSamplerState, const skgpu::Swizzle& swizzle,
73 const char* name, const GrShaderCaps* shaderCaps) {
74 SkASSERT(name && strlen(name));
75
76 constexpr char prefix = 'u';
77 SkString mangleName = fProgramBuilder->nameVariable(prefix, name, true);
78
79 GrTextureType type = backendFormat.textureType();
80
81 GLUniformInfo tempInfo;
82 tempInfo.fVariable = GrShaderVar{std::move(mangleName),
83 SkSLCombinedSamplerTypeForTextureType(type),
84 GrShaderVar::TypeModifier::Uniform};
85
86 tempInfo.fVisibility = kFragment_GrShaderFlag;
87 tempInfo.fOwner = nullptr;
88 tempInfo.fRawName = SkString(name);
89 tempInfo.fLocation = -1;
90
91 fSamplers.push_back(tempInfo);
92 fSamplerSwizzles.push_back(swizzle);
93 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
94
95 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
96 }
97
appendUniformDecls(GrShaderFlags visibility,SkString * out) const98 void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
99 for (const UniformInfo& uniform : fUniforms.items()) {
100 if (uniform.fVisibility & visibility) {
101 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
102 out->append(";");
103 }
104 }
105 for (const UniformInfo& sampler : fSamplers.items()) {
106 if (sampler.fVisibility & visibility) {
107 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
108 out->append(";\n");
109 }
110 }
111 }
112
bindUniformLocations(GrGLuint programID,const GrGLCaps & caps)113 void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
114 if (caps.bindUniformLocationSupport()) {
115 int currUniform = 0;
116 for (GLUniformInfo& uniform : fUniforms.items()) {
117 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
118 uniform.fLocation = currUniform;
119 ++currUniform;
120 }
121 for (GLUniformInfo& sampler : fSamplers.items()) {
122 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
123 sampler.fLocation = currUniform;
124 ++currUniform;
125 }
126 }
127 }
128
getUniformLocations(GrGLuint programID,const GrGLCaps & caps,bool force)129 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
130 if (!caps.bindUniformLocationSupport() || force) {
131 for (GLUniformInfo& uniform : fUniforms.items()) {
132 GrGLint location;
133 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
134 uniform.fLocation = location;
135 }
136 for (GLUniformInfo& sampler : fSamplers.items()) {
137 GrGLint location;
138 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
139 sampler.fLocation = location;
140 }
141 }
142 }
143
glGpu() const144 const GrGLGpu* GrGLUniformHandler::glGpu() const {
145 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
146 return glPB->gpu();
147 }
148