2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "gl/GrGLProgramDataManager.h"
10 #include "gl/GrGLGpu.h"
11 #include "glsl/GrGLSLUniformHandler.h"
13 #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \
14 SkASSERT(arrayCount <= uni.fArrayCount || \
15 (1 == arrayCount && GrGLSLShaderVar::kNonArray == uni.fArrayCount))
17 GrGLProgramDataManager::GrGLProgramDataManager(GrGLGpu* gpu, GrGLuint programID,
18 const UniformInfoArray& uniforms,
19 const VaryingInfoArray& pathProcVaryings)
21 , fProgramID(programID) {
22 int count = uniforms.count();
23 fUniforms.push_back_n(count);
24 for (int i = 0; i < count; i++) {
25 Uniform& uniform = fUniforms[i];
26 const UniformInfo& builderUniform = uniforms[i];
27 SkASSERT(GrGLSLShaderVar::kNonArray == builderUniform.fVariable.getArrayCount() ||
28 builderUniform.fVariable.getArrayCount() > 0);
30 uniform.fArrayCount = builderUniform.fVariable.getArrayCount();
31 uniform.fType = builderUniform.fVariable.getType();
33 // TODO: Move the Xoom uniform array in both FS and VS bug workaround here.
35 if (GrGLSLUniformHandler::kVertex_Visibility & builderUniform.fVisibility) {
36 uniform.fVSLocation = builderUniform.fLocation;
38 uniform.fVSLocation = kUnusedUniform;
40 if (GrGLSLUniformHandler::kFragment_Visibility & builderUniform.fVisibility) {
41 uniform.fFSLocation = builderUniform.fLocation;
43 uniform.fFSLocation = kUnusedUniform;
47 // NVPR programs have separable varyings
48 count = pathProcVaryings.count();
49 fPathProcVaryings.push_back_n(count);
50 for (int i = 0; i < count; i++) {
51 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
52 PathProcVarying& pathProcVarying = fPathProcVaryings[i];
53 const VaryingInfo& builderPathProcVarying = pathProcVaryings[i];
54 SkASSERT(GrGLSLShaderVar::kNonArray == builderPathProcVarying.fVariable.getArrayCount() ||
55 builderPathProcVarying.fVariable.getArrayCount() > 0);
57 pathProcVarying.fArrayCount = builderPathProcVarying.fVariable.getArrayCount();
58 pathProcVarying.fType = builderPathProcVarying.fVariable.getType();
60 pathProcVarying.fLocation = builderPathProcVarying.fLocation;
64 void GrGLProgramDataManager::setSampler(UniformHandle u, int texUnit) const {
65 const Uniform& uni = fUniforms[u.toIndex()];
66 SkASSERT(uni.fType == kSampler2D_GrSLType || uni.fType == kSamplerExternal_GrSLType);
67 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
68 // FIXME: We still insert a single sampler uniform for every stage. If the shader does not
69 // reference the sampler then the compiler may have optimized it out. Uncomment this assert
70 // once stages insert their own samplers.
71 // this->printUnused(uni);
72 if (kUnusedUniform != uni.fFSLocation) {
73 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fFSLocation, texUnit));
75 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
76 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fVSLocation, texUnit));
80 void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
81 const Uniform& uni = fUniforms[u.toIndex()];
82 SkASSERT(uni.fType == kFloat_GrSLType);
83 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
84 SkDEBUGCODE(this->printUnused(uni);)
85 if (kUnusedUniform != uni.fFSLocation) {
86 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fFSLocation, v0));
88 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
89 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fVSLocation, v0));
93 void GrGLProgramDataManager::set1fv(UniformHandle u,
95 const float v[]) const {
96 const Uniform& uni = fUniforms[u.toIndex()];
97 SkASSERT(uni.fType == kFloat_GrSLType);
98 SkASSERT(arrayCount > 0);
99 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
100 // This assert fires in some instances of the two-pt gradient for its VSParams.
101 // Once the uniform manager is responsible for inserting the duplicate uniform
102 // arrays in VS and FS driver bug workaround, this can be enabled.
103 // this->printUni(uni);
104 if (kUnusedUniform != uni.fFSLocation) {
105 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fFSLocation, arrayCount, v));
107 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
108 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fVSLocation, arrayCount, v));
112 void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
113 const Uniform& uni = fUniforms[u.toIndex()];
114 SkASSERT(uni.fType == kVec2f_GrSLType);
115 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
116 SkDEBUGCODE(this->printUnused(uni);)
117 if (kUnusedUniform != uni.fFSLocation) {
118 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fFSLocation, v0, v1));
120 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
121 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fVSLocation, v0, v1));
125 void GrGLProgramDataManager::set2fv(UniformHandle u,
127 const float v[]) const {
128 const Uniform& uni = fUniforms[u.toIndex()];
129 SkASSERT(uni.fType == kVec2f_GrSLType);
130 SkASSERT(arrayCount > 0);
131 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
132 SkDEBUGCODE(this->printUnused(uni);)
133 if (kUnusedUniform != uni.fFSLocation) {
134 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fFSLocation, arrayCount, v));
136 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
137 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fVSLocation, arrayCount, v));
141 void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
142 const Uniform& uni = fUniforms[u.toIndex()];
143 SkASSERT(uni.fType == kVec3f_GrSLType);
144 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
145 SkDEBUGCODE(this->printUnused(uni);)
146 if (kUnusedUniform != uni.fFSLocation) {
147 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fFSLocation, v0, v1, v2));
149 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
150 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fVSLocation, v0, v1, v2));
154 void GrGLProgramDataManager::set3fv(UniformHandle u,
156 const float v[]) const {
157 const Uniform& uni = fUniforms[u.toIndex()];
158 SkASSERT(uni.fType == kVec3f_GrSLType);
159 SkASSERT(arrayCount > 0);
160 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
161 SkDEBUGCODE(this->printUnused(uni);)
162 if (kUnusedUniform != uni.fFSLocation) {
163 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fFSLocation, arrayCount, v));
165 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
166 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fVSLocation, arrayCount, v));
170 void GrGLProgramDataManager::set4f(UniformHandle u,
175 const Uniform& uni = fUniforms[u.toIndex()];
176 SkASSERT(uni.fType == kVec4f_GrSLType);
177 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
178 SkDEBUGCODE(this->printUnused(uni);)
179 if (kUnusedUniform != uni.fFSLocation) {
180 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fFSLocation, v0, v1, v2, v3));
182 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
183 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fVSLocation, v0, v1, v2, v3));
187 void GrGLProgramDataManager::set4fv(UniformHandle u,
189 const float v[]) const {
190 const Uniform& uni = fUniforms[u.toIndex()];
191 SkASSERT(uni.fType == kVec4f_GrSLType);
192 SkASSERT(arrayCount > 0);
193 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
194 SkDEBUGCODE(this->printUnused(uni);)
195 if (kUnusedUniform != uni.fFSLocation) {
196 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fFSLocation, arrayCount, v));
198 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
199 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fVSLocation, arrayCount, v));
203 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
204 const Uniform& uni = fUniforms[u.toIndex()];
205 SkASSERT(uni.fType == kMat33f_GrSLType);
206 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
207 SkDEBUGCODE(this->printUnused(uni);)
208 if (kUnusedUniform != uni.fFSLocation) {
209 GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fFSLocation, 1, false, matrix));
211 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
212 GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fVSLocation, 1, false, matrix));
216 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
217 const Uniform& uni = fUniforms[u.toIndex()];
218 SkASSERT(uni.fType == kMat44f_GrSLType);
219 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
220 SkDEBUGCODE(this->printUnused(uni);)
221 if (kUnusedUniform != uni.fFSLocation) {
222 GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fFSLocation, 1, false, matrix));
224 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
225 GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fVSLocation, 1, false, matrix));
229 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u,
231 const float matrices[]) const {
232 const Uniform& uni = fUniforms[u.toIndex()];
233 SkASSERT(uni.fType == kMat33f_GrSLType);
234 SkASSERT(arrayCount > 0);
235 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
236 SkDEBUGCODE(this->printUnused(uni);)
237 if (kUnusedUniform != uni.fFSLocation) {
238 GR_GL_CALL(fGpu->glInterface(),
239 UniformMatrix3fv(uni.fFSLocation, arrayCount, false, matrices));
241 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
242 GR_GL_CALL(fGpu->glInterface(),
243 UniformMatrix3fv(uni.fVSLocation, arrayCount, false, matrices));
247 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u,
249 const float matrices[]) const {
250 const Uniform& uni = fUniforms[u.toIndex()];
251 SkASSERT(uni.fType == kMat44f_GrSLType);
252 SkASSERT(arrayCount > 0);
253 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
254 SkDEBUGCODE(this->printUnused(uni);)
255 if (kUnusedUniform != uni.fFSLocation) {
256 GR_GL_CALL(fGpu->glInterface(),
257 UniformMatrix4fv(uni.fFSLocation, arrayCount, false, matrices));
259 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
260 GR_GL_CALL(fGpu->glInterface(),
261 UniformMatrix4fv(uni.fVSLocation, arrayCount, false, matrices));
265 void GrGLProgramDataManager::setSkMatrix(UniformHandle u, const SkMatrix& matrix) const {
267 matrix.get(SkMatrix::kMScaleX),
268 matrix.get(SkMatrix::kMSkewY),
269 matrix.get(SkMatrix::kMPersp0),
270 matrix.get(SkMatrix::kMSkewX),
271 matrix.get(SkMatrix::kMScaleY),
272 matrix.get(SkMatrix::kMPersp1),
273 matrix.get(SkMatrix::kMTransX),
274 matrix.get(SkMatrix::kMTransY),
275 matrix.get(SkMatrix::kMPersp2),
277 this->setMatrix3f(u, mt);
280 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
282 const SkMatrix& matrix) const {
283 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
284 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
286 SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
287 (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
289 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
290 fragmentInput.fLocation,
297 void GrGLProgramDataManager::printUnused(const Uniform& uni) const {
298 if (kUnusedUniform == uni.fFSLocation && kUnusedUniform == uni.fVSLocation) {
299 GrCapsDebugf(fGpu->caps(), "Unused uniform in shader\n");