• 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/ganesh/gl/GrGLUniformHandler.h"
9 
10 #include "src/gpu/ganesh/GrTexture.h"
11 #include "src/gpu/ganesh/GrUtil.h"
12 #include "src/gpu/ganesh/gl/GrGLCaps.h"
13 #include "src/gpu/ganesh/gl/GrGLGpu.h"
14 #include "src/gpu/ganesh/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 GrProcessor * owner,uint32_t visibility,SkSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)28 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
29                                                                    const GrProcessor* 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,
73         GrSamplerState,
74         const skgpu::Swizzle& swizzle,
75         const char* name,
76         const GrShaderCaps* shaderCaps) {
77     SkASSERT(name && strlen(name));
78 
79     constexpr char prefix = 'u';
80     SkString mangleName = fProgramBuilder->nameVariable(prefix, name, true);
81 
82     GrTextureType type = backendFormat.textureType();
83 
84     GLUniformInfo tempInfo;
85     tempInfo.fVariable = GrShaderVar{std::move(mangleName),
86                                      SkSLCombinedSamplerTypeForTextureType(type)};
87 
88     tempInfo.fVisibility = kFragment_GrShaderFlag;
89     tempInfo.fOwner      = nullptr;
90     tempInfo.fRawName    = SkString(name);
91     tempInfo.fLocation   = -1;
92 
93     fSamplers.push_back(tempInfo);
94     fSamplerSwizzles.push_back(swizzle);
95     SkASSERT(fSamplers.count() == fSamplerSwizzles.size());
96 
97     return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
98 }
99 
appendUniformDecls(GrShaderFlags visibility,SkString * out) const100 void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
101     for (const UniformInfo& uniform : fUniforms.items()) {
102         if (uniform.fVisibility & visibility) {
103             uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
104             out->append(";");
105         }
106     }
107     for (const UniformInfo& sampler : fSamplers.items()) {
108         if (sampler.fVisibility & visibility) {
109             sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
110             out->append(";\n");
111         }
112     }
113 }
114 
bindUniformLocations(GrGLuint programID,const GrGLCaps & caps)115 void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
116     if (caps.bindUniformLocationSupport()) {
117         int currUniform = 0;
118         for (GLUniformInfo& uniform : fUniforms.items()) {
119             GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
120             uniform.fLocation = currUniform;
121             ++currUniform;
122         }
123         for (GLUniformInfo& sampler : fSamplers.items()) {
124             GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
125             sampler.fLocation = currUniform;
126             ++currUniform;
127         }
128     }
129 }
130 
getUniformLocations(GrGLuint programID,const GrGLCaps & caps,bool force)131 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
132     if (!caps.bindUniformLocationSupport() || force) {
133         for (GLUniformInfo& uniform : fUniforms.items()) {
134             GrGLint location;
135             GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
136             uniform.fLocation = location;
137         }
138         for (GLUniformInfo& sampler : fSamplers.items()) {
139             GrGLint location;
140             GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
141             sampler.fLocation = location;
142         }
143     }
144 }
145 
glGpu() const146 const GrGLGpu* GrGLUniformHandler::glGpu() const {
147     GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
148     return glPB->gpu();
149 }
150