Flutter Engine
The Flutter Engine
GrGLUniformHandler.cpp
Go to the documentation of this file.
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
9
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
20bool 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
28GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
29 const GrProcessor* owner,
30 uint32_t visibility,
32 const char* name,
33 bool mangleName,
34 int arrayCount,
35 const char** outName) {
36 SkASSERT(name && strlen(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,
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
71GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
72 const GrBackendFormat& backendFormat,
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),
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
100void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
101 for (const UniformInfo& uniform : fUniforms.items()) {
102 if (uniform.fVisibility & visibility) {
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
115void 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
131void 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
146const GrGLGpu* GrGLUniformHandler::glGpu() const {
148 return glPB->gpu();
149}
#define GR_NO_MANGLE_PREFIX
unsigned int GrGLuint
Definition: GrGLTypes.h:113
int GrGLint
Definition: GrGLTypes.h:108
#define GL_CALL(X)
#define GL_CALL_RET(R, X)
bool valid_name(const char *name)
GrShaderFlags
Definition: GrTypesPriv.h:284
@ kFragment_GrShaderFlag
Definition: GrTypesPriv.h:287
GrTextureType
Definition: GrTypesPriv.h:268
static SkSLType SkSLCombinedSamplerTypeForTextureType(GrTextureType type)
Definition: GrUtil.h:50
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkSLType
GLenum type
GrTextureType textureType() const
bool bindUniformLocationSupport() const
Definition: GrGLCaps.h:381
GrGLGpu * gpu() const
SkString nameVariable(char prefix, const char *name, bool mangle=true)
const GrShaderCaps * shaderCaps() const
GrGLSLProgramBuilder * fProgramBuilder
GrGLSLProgramDataManager::UniformHandle UniformHandle
UniformInfo & uniform(int idx) override
const char * c_str() const
Definition: GrShaderVar.h:94
void appendDecl(const GrShaderCaps *, SkString *out) const
Definition: GrShaderVar.cpp:23
static constexpr const char RTADJUST_NAME[]
Definition: SkSLCompiler.h:72
T & push_back()
Definition: SkTBlockList.h:90
int count() const
Definition: SkTBlockList.h:167
int size() const
Definition: SkTArray.h:421
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32