• 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/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"
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(uint32_t visibility,GrSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)27 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
28                                                                             uint32_t visibility,
29                                                                             GrSLType type,
30                                                                             const char* name,
31                                                                             bool mangleName,
32                                                                             int arrayCount,
33                                                                             const char** outName) {
34     SkASSERT(name && strlen(name));
35     SkASSERT(valid_name(name));
36     SkASSERT(0 != visibility);
37 
38     UniformInfo& uni = fUniforms.push_back();
39     uni.fVariable.setType(type);
40     uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
41     // TODO this is a bit hacky, lets think of a better way.  Basically we need to be able to use
42     // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
43     // exactly what name it wants to use for the uniform view matrix.  If we prefix anythings, then
44     // the names will mismatch.  I think the correct solution is to have all GPs which need the
45     // uniform view matrix, they should upload the view matrix in their setData along with regular
46     // uniforms.
47     char prefix = 'u';
48     if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
49         prefix = '\0';
50     }
51     fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
52     uni.fVariable.setArrayCount(arrayCount);
53     uni.fVisibility = visibility;
54     uni.fLocation = -1;
55 
56     if (outName) {
57         *outName = uni.fVariable.c_str();
58     }
59     return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
60 }
61 
addSampler(const GrSurfaceProxy * texture,GrSamplerState,const GrSwizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)62 GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(const GrSurfaceProxy* texture,
63                                                                    GrSamplerState,
64                                                                    const GrSwizzle& swizzle,
65                                                                    const char* name,
66                                                                    const GrShaderCaps* shaderCaps) {
67     SkASSERT(name && strlen(name));
68     SkASSERT(texture->asTextureProxy());
69 
70     SkString mangleName;
71     char prefix = 'u';
72     fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
73 
74     GrTextureType type = texture->backendFormat().textureType();
75 
76     UniformInfo& sampler = fSamplers.push_back();
77     sampler.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
78     sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
79     sampler.fVariable.setName(mangleName);
80     sampler.fLocation = -1;
81     sampler.fVisibility = kFragment_GrShaderFlag;
82     if (shaderCaps->textureSwizzleAppliedInShader()) {
83         fSamplerSwizzles.push_back(swizzle);
84         SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
85     }
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 (int i = 0; i < fUniforms.count(); ++i) {
91         if (fUniforms[i].fVisibility & visibility) {
92             fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
93             out->append(";");
94         }
95     }
96     for (int i = 0; i < fSamplers.count(); ++i) {
97         if (fSamplers[i].fVisibility & visibility) {
98             fSamplers[i].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 (int i = 0; i < fUniforms.count(); ++i, ++currUniform) {
108             GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str()));
109             fUniforms[i].fLocation = currUniform;
110         }
111         for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) {
112             GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str()));
113             fSamplers[i].fLocation = currUniform;
114         }
115     }
116 }
117 
getUniformLocations(GrGLuint programID,const GrGLCaps & caps,bool force)118 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
119     if (!caps.bindUniformLocationSupport() || force) {
120         int count = fUniforms.count();
121         for (int i = 0; i < count; ++i) {
122             GrGLint location;
123             GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str()));
124             fUniforms[i].fLocation = location;
125         }
126         for (int i = 0; i < fSamplers.count(); ++i) {
127             GrGLint location;
128             GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str()));
129             fSamplers[i].fLocation = location;
130         }
131     }
132 }
133 
glGpu() const134 const GrGLGpu* GrGLUniformHandler::glGpu() const {
135     GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
136     return glPB->gpu();
137 }
138