Line data Source code
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 :
17 0 : 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 0 : SkASSERT(name && strlen(name));
26 0 : SkASSERT(0 != visibility);
27 0 : SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeAcceptsPrecision(type));
28 :
29 0 : UniformInfo& uni = fUniforms.push_back();
30 0 : uni.fVariable.setType(type);
31 0 : uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
32 : // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
33 : // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
34 : // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
35 : // the names will mismatch. I think the correct solution is to have all GPs which need the
36 : // uniform view matrix, they should upload the view matrix in their setData along with regular
37 : // uniforms.
38 0 : char prefix = 'u';
39 0 : if ('u' == name[0]) {
40 0 : prefix = '\0';
41 : }
42 0 : fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
43 0 : uni.fVariable.setArrayCount(arrayCount);
44 0 : uni.fVisibility = visibility;
45 0 : uni.fVariable.setPrecision(precision);
46 0 : uni.fLocation = -1;
47 :
48 0 : if (outName) {
49 0 : *outName = uni.fVariable.c_str();
50 : }
51 0 : return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
52 : }
53 :
54 0 : GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(uint32_t visibility,
55 : GrSwizzle swizzle,
56 : GrSLType type,
57 : GrSLPrecision precision,
58 : const char* name) {
59 0 : SkASSERT(name && strlen(name));
60 0 : SkASSERT(0 != visibility);
61 :
62 0 : SkString mangleName;
63 0 : char prefix = 'u';
64 0 : fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
65 :
66 0 : UniformInfo& sampler = fSamplers.push_back();
67 0 : SkASSERT(GrSLTypeIsCombinedSamplerType(type));
68 0 : sampler.fVariable.setType(type);
69 0 : sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
70 0 : sampler.fVariable.setPrecision(precision);
71 0 : sampler.fVariable.setName(mangleName);
72 0 : sampler.fLocation = -1;
73 0 : sampler.fVisibility = visibility;
74 0 : fSamplerSwizzles.push_back(swizzle);
75 0 : SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
76 0 : return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
77 : }
78 :
79 0 : GrGLSLUniformHandler::ImageStorageHandle GrGLUniformHandler::addImageStorage(
80 : uint32_t visibility, GrSLType type, GrImageStorageFormat format, GrSLMemoryModel model,
81 : GrSLRestrict restrict, GrIOType ioType, const char* name) {
82 0 : SkASSERT(name && strlen(name));
83 0 : SkASSERT(0 != visibility);
84 0 : SkString mangleName;
85 0 : char prefix = 'u';
86 0 : fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
87 :
88 0 : UniformInfo& imageStorage = fImageStorages.push_back();
89 0 : imageStorage.fVariable.setName(mangleName);
90 :
91 0 : SkASSERT(GrSLTypeIsImageStorage(type));
92 0 : imageStorage.fVariable.setType(type);
93 0 : imageStorage.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
94 0 : imageStorage.fVariable.setImageStorageFormat(format);
95 0 : imageStorage.fVariable.setMemoryModel(model);
96 0 : imageStorage.fVariable.setRestrict(restrict);
97 0 : imageStorage.fVariable.setIOType(ioType);
98 0 : imageStorage.fVariable.setPrecision(kHigh_GrSLPrecision);
99 0 : imageStorage.fLocation = -1;
100 0 : imageStorage.fVisibility = visibility;
101 0 : return GrGLSLUniformHandler::ImageStorageHandle(fImageStorages.count() - 1);
102 : }
103 :
104 0 : void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
105 0 : for (int i = 0; i < fUniforms.count(); ++i) {
106 0 : if (fUniforms[i].fVisibility & visibility) {
107 0 : fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
108 0 : out->append(";");
109 : }
110 : }
111 0 : for (int i = 0; i < fSamplers.count(); ++i) {
112 0 : if (fSamplers[i].fVisibility & visibility) {
113 0 : fSamplers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
114 0 : out->append(";\n");
115 : }
116 : }
117 0 : for (int i = 0; i < fImageStorages.count(); ++i) {
118 0 : if (fImageStorages[i].fVisibility & visibility) {
119 0 : fImageStorages[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
120 0 : out->append(";");
121 : }
122 : }
123 0 : }
124 :
125 0 : void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
126 0 : if (caps.bindUniformLocationSupport()) {
127 0 : int currUniform = 0;
128 0 : for (int i = 0; i < fUniforms.count(); ++i, ++currUniform) {
129 0 : GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str()));
130 0 : fUniforms[i].fLocation = currUniform;
131 : }
132 0 : for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) {
133 0 : GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str()));
134 0 : fSamplers[i].fLocation = currUniform;
135 : }
136 0 : for (int i = 0; i < fImageStorages.count(); ++i) {
137 0 : GL_CALL(BindUniformLocation(programID, currUniform,
138 : fImageStorages[i].fVariable.c_str()));
139 0 : fImageStorages[i].fLocation = currUniform;
140 : }
141 : }
142 0 : }
143 :
144 0 : void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
145 0 : if (!caps.bindUniformLocationSupport()) {
146 0 : int count = fUniforms.count();
147 0 : for (int i = 0; i < count; ++i) {
148 : GrGLint location;
149 0 : GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str()));
150 0 : fUniforms[i].fLocation = location;
151 : }
152 0 : for (int i = 0; i < fSamplers.count(); ++i) {
153 : GrGLint location;
154 0 : GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str()));
155 0 : fSamplers[i].fLocation = location;
156 : }
157 0 : for (int i = 0; i < fImageStorages.count(); ++i) {
158 : GrGLint location;
159 0 : GL_CALL_RET(location, GetUniformLocation(programID,
160 : fImageStorages[i].fVariable.c_str()));
161 0 : fImageStorages[i].fLocation = location;
162 : }
163 : }
164 0 : }
165 :
166 0 : const GrGLGpu* GrGLUniformHandler::glGpu() const {
167 0 : GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
168 0 : return glPB->gpu();
169 : }
|