• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gl/GrGLCaps.h"
12 #include "src/gpu/gl/GrGLGpu.h"
13 #include "src/gpu/gl/builders/GrGLProgramBuilder.h"
14 #include "src/sksl/SkSLCompiler.h"
15 
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 
valid_name(const char * name)19 bool 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 
internalAddUniformArray(const GrFragmentProcessor * owner,uint32_t visibility,GrSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)27 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
28                                                                    const GrFragmentProcessor* owner,
29                                                                    uint32_t visibility,
30                                                                    GrSLType type,
31                                                                    const char* name,
32                                                                    bool mangleName,
33                                                                    int arrayCount,
34                                                                    const char** outName) {
35     SkASSERT(name && strlen(name));
36     SkASSERT(valid_name(name));
37     SkASSERT(0 != visibility);
38 
39     // 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.
45     char prefix = 'u';
46     if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
47         prefix = '\0';
48     }
49     SkString resolvedName = fProgramBuilder->nameVariable(prefix, name, mangleName);
50     GLUniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::GLUniformInfo{
51         {
52             GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform,
53                         arrayCount},
54             visibility, owner, SkString(name)
55         },
56         -1
57     });
58 
59     if (outName) {
60         *outName = uni.fVariable.c_str();
61     }
62     return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
63 }
64 
addSampler(const GrBackendFormat & backendFormat,GrSamplerState,const GrSwizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)65 GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
66         const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle,
67         const char* name, const GrShaderCaps* shaderCaps) {
68     SkASSERT(name && strlen(name));
69 
70     constexpr char prefix = 'u';
71     SkString mangleName = fProgramBuilder->nameVariable(prefix, name, true);
72 
73     GrTextureType type = backendFormat.textureType();
74 
75     fSamplers.push_back(GrGLProgramDataManager::GLUniformInfo{
76         {
77             GrShaderVar{std::move(mangleName), GrSLCombinedSamplerTypeForTextureType(type),
78                           GrShaderVar::TypeModifier::Uniform},
79             kFragment_GrShaderFlag, nullptr, SkString(name)
80         },
81         -1
82     });
83 
84     fSamplerSwizzles.push_back(swizzle);
85     SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
86     return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
87 }
88 
appendUniformDecls(GrShaderFlags visibility,SkString * out) const89 void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
90     for (const UniformInfo& uniform : fUniforms.items()) {
91         if (uniform.fVisibility & visibility) {
92             uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
93             out->append(";");
94         }
95     }
96     for (const UniformInfo& sampler : fSamplers.items()) {
97         if (sampler.fVisibility & visibility) {
98             sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
99             out->append(";\n");
100         }
101     }
102 }
103 
bindUniformLocations(GrGLuint programID,const GrGLCaps & caps)104 void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
105     if (caps.bindUniformLocationSupport()) {
106         int currUniform = 0;
107         for (GLUniformInfo& uniform : fUniforms.items()) {
108             GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
109             uniform.fLocation = currUniform;
110             ++currUniform;
111         }
112         for (GLUniformInfo& sampler : fSamplers.items()) {
113             GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
114             sampler.fLocation = currUniform;
115             ++currUniform;
116         }
117     }
118 }
119 
getUniformLocations(GrGLuint programID,const GrGLCaps & caps,bool force)120 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
121     if (!caps.bindUniformLocationSupport() || force) {
122         for (GLUniformInfo& uniform : fUniforms.items()) {
123             GrGLint location;
124             GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
125             uniform.fLocation = location;
126         }
127         for (GLUniformInfo& sampler : fSamplers.items()) {
128             GrGLint location;
129             GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
130             sampler.fLocation = location;
131         }
132     }
133 }
134 
glGpu() const135 const GrGLGpu* GrGLUniformHandler::glGpu() const {
136     GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
137     return glPB->gpu();
138 }
139