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 "gl/GrGLUniformHandler.h"
9
10 #include "gl/GrGLCaps.h"
11 #include "gl/GrGLGpu.h"
12 #include "gl/builders/GrGLProgramBuilder.h"
13
14 #define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
15 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
16
internalAddUniformArray(uint32_t visibility,GrSLType type,GrSLPrecision precision,const char * name,bool mangleName,int arrayCount,const char ** outName)17 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
18 uint32_t visibility,
19 GrSLType type,
20 GrSLPrecision precision,
21 const char* name,
22 bool mangleName,
23 int arrayCount,
24 const char** outName) {
25 SkASSERT(name && strlen(name));
26 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
27 SkASSERT(0 == (~kVisMask & visibility));
28 SkASSERT(0 != visibility);
29 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsNumeric(type));
30
31 UniformInfo& uni = fUniforms.push_back();
32 uni.fVariable.setType(type);
33 uni.fVariable.setTypeModifier(GrGLSLShaderVar::kUniform_TypeModifier);
34 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
35 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
36 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
37 // the names will mismatch. I think the correct solution is to have all GPs which need the
38 // uniform view matrix, they should upload the view matrix in their setData along with regular
39 // uniforms.
40 char prefix = 'u';
41 if ('u' == name[0]) {
42 prefix = '\0';
43 }
44 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
45 uni.fVariable.setArrayCount(arrayCount);
46 uni.fVisibility = visibility;
47 uni.fVariable.setPrecision(precision);
48
49 if (outName) {
50 *outName = uni.fVariable.c_str();
51 }
52 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
53 }
54
appendUniformDecls(GrShaderFlags visibility,SkString * out) const55 void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
56 for (int i = 0; i < fUniforms.count(); ++i) {
57 if (fUniforms[i].fVisibility & visibility) {
58 fUniforms[i].fVariable.appendDecl(fProgramBuilder->glslCaps(), out);
59 out->append(";\n");
60 }
61 }
62 }
63
bindUniformLocations(GrGLuint programID,const GrGLCaps & caps)64 void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
65 if (caps.bindUniformLocationSupport()) {
66 int count = fUniforms.count();
67 for (int i = 0; i < count; ++i) {
68 GL_CALL(BindUniformLocation(programID, i, fUniforms[i].fVariable.c_str()));
69 fUniforms[i].fLocation = i;
70 }
71 }
72 }
73
getUniformLocations(GrGLuint programID,const GrGLCaps & caps)74 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
75 if (!caps.bindUniformLocationSupport()) {
76 int count = fUniforms.count();
77 for (int i = 0; i < count; ++i) {
78 GrGLint location;
79 GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str()));
80 fUniforms[i].fLocation = location;
81 }
82 }
83 }
84
glGpu() const85 const GrGLGpu* GrGLUniformHandler::glGpu() const {
86 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
87 return glPB->gpu();
88 }
89
90
91