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
19bool 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
27GrGLSLUniformHandler::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 SkString resolvedName;
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 fProgramBuilder->nameVariable(&resolvedName, prefix, name, mangleName);
51 GLUniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::GLUniformInfo{
52 {
53 GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform,
54 arrayCount},
55 visibility, owner, SkString(name)
56 },
57 -1
58 });
59
60 if (outName) {
61 *outName = uni.fVariable.c_str();
62 }
63 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
64}
65
66GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
67 const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle,
68 const char* name, const GrShaderCaps* shaderCaps) {
69 SkASSERT(name && strlen(name));
70
71 SkString mangleName;
72 char prefix = 'u';
73 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
74
75 GrTextureType type = backendFormat.textureType();
76
77 fSamplers.push_back(GrGLProgramDataManager::GLUniformInfo{
78 {
79 GrShaderVar{std::move(mangleName), GrSLCombinedSamplerTypeForTextureType(type),
80 GrShaderVar::TypeModifier::Uniform},
81 kFragment_GrShaderFlag, nullptr, SkString(name)
82 },
83 -1
84 });
85
86 if (shaderCaps->textureSwizzleAppliedInShader()) {
87 fSamplerSwizzles.push_back(swizzle);
88 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
89 }
90 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
91}
92
93void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
94 for (const UniformInfo& uniform : fUniforms.items()) {
95 if (uniform.fVisibility & visibility) {
96 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
97 out->append(";");
98 }
99 }
100 for (const UniformInfo& sampler : fSamplers.items()) {
101 if (sampler.fVisibility & visibility) {
102 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
103 out->append(";\n");
104 }
105 }
106}
107
108void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
109 if (caps.bindUniformLocationSupport()) {
110 int currUniform = 0;
111 for (GLUniformInfo& uniform : fUniforms.items()) {
112 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
113 uniform.fLocation = currUniform;
114 ++currUniform;
115 }
116 for (GLUniformInfo& sampler : fSamplers.items()) {
117 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
118 sampler.fLocation = currUniform;
119 ++currUniform;
120 }
121 }
122}
123
124void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
125 if (!caps.bindUniformLocationSupport() || force) {
126 for (GLUniformInfo& uniform : fUniforms.items()) {
127 GrGLint location;
128 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
129 uniform.fLocation = location;
130 }
131 for (GLUniformInfo& sampler : fSamplers.items()) {
132 GrGLint location;
133 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
134 sampler.fLocation = location;
135 }
136 }
137}
138
139const GrGLGpu* GrGLUniformHandler::glGpu() const {
140 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
141 return glPB->gpu();
142}
143