1 /*
2  * Copyright 2012 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/GrGLPathRendering.h"
9 #include "gl/GrGLUniformHandle.h"
10 #include "gl/GrGLGpu.h"
11 #include "SkMatrix.h"
12 
13 #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \
14          SkASSERT(arrayCount <= uni.fArrayCount || \
15                   (1 == arrayCount && GrGLShaderVar::kNonArray == uni.fArrayCount))
16 
GrGLProgramDataManager(GrGLGpu * gpu,const UniformInfoArray & uniforms)17 GrGLProgramDataManager::GrGLProgramDataManager(GrGLGpu* gpu, const UniformInfoArray& uniforms)
18     : fGpu(gpu) {
19     int count = uniforms.count();
20     fUniforms.push_back_n(count);
21     for (int i = 0; i < count; i++) {
22         Uniform& uniform = fUniforms[i];
23         const UniformInfo& builderUniform = uniforms[i];
24         SkASSERT(GrGLShaderVar::kNonArray == builderUniform.fVariable.getArrayCount() ||
25                  builderUniform.fVariable.getArrayCount() > 0);
26         SkDEBUGCODE(
27             uniform.fArrayCount = builderUniform.fVariable.getArrayCount();
28             uniform.fType = builderUniform.fVariable.getType();
29         );
30         // TODO: Move the Xoom uniform array in both FS and VS bug workaround here.
31 
32         if (GrGLProgramBuilder::kVertex_Visibility & builderUniform.fVisibility) {
33             uniform.fVSLocation = builderUniform.fLocation;
34         } else {
35             uniform.fVSLocation = kUnusedUniform;
36         }
37         if (GrGLProgramBuilder::kFragment_Visibility & builderUniform.fVisibility) {
38             uniform.fFSLocation = builderUniform.fLocation;
39         } else {
40             uniform.fFSLocation = kUnusedUniform;
41         }
42     }
43 }
44 
setSampler(UniformHandle u,GrGLint texUnit) const45 void GrGLProgramDataManager::setSampler(UniformHandle u, GrGLint texUnit) const {
46     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
47     SkASSERT(uni.fType == kSampler2D_GrSLType);
48     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
49     // FIXME: We still insert a single sampler uniform for every stage. If the shader does not
50     // reference the sampler then the compiler may have optimized it out. Uncomment this assert
51     // once stages insert their own samplers.
52     // this->printUnused(uni);
53     if (kUnusedUniform != uni.fFSLocation) {
54         GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fFSLocation, texUnit));
55     }
56     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
57         GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fVSLocation, texUnit));
58     }
59 }
60 
set1f(UniformHandle u,GrGLfloat v0) const61 void GrGLProgramDataManager::set1f(UniformHandle u, GrGLfloat v0) const {
62     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
63     SkASSERT(uni.fType == kFloat_GrSLType);
64     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
65     SkDEBUGCODE(this->printUnused(uni);)
66     if (kUnusedUniform != uni.fFSLocation) {
67         GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fFSLocation, v0));
68     }
69     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
70         GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fVSLocation, v0));
71     }
72 }
73 
set1fv(UniformHandle u,int arrayCount,const GrGLfloat v[]) const74 void GrGLProgramDataManager::set1fv(UniformHandle u,
75                                     int arrayCount,
76                                     const GrGLfloat v[]) const {
77     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
78     SkASSERT(uni.fType == kFloat_GrSLType);
79     SkASSERT(arrayCount > 0);
80     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
81     // This assert fires in some instances of the two-pt gradient for its VSParams.
82     // Once the uniform manager is responsible for inserting the duplicate uniform
83     // arrays in VS and FS driver bug workaround, this can be enabled.
84     // this->printUni(uni);
85     if (kUnusedUniform != uni.fFSLocation) {
86         GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fFSLocation, arrayCount, v));
87     }
88     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
89         GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fVSLocation, arrayCount, v));
90     }
91 }
92 
set2f(UniformHandle u,GrGLfloat v0,GrGLfloat v1) const93 void GrGLProgramDataManager::set2f(UniformHandle u, GrGLfloat v0, GrGLfloat v1) const {
94     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
95     SkASSERT(uni.fType == kVec2f_GrSLType);
96     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
97     SkDEBUGCODE(this->printUnused(uni);)
98     if (kUnusedUniform != uni.fFSLocation) {
99         GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fFSLocation, v0, v1));
100     }
101     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
102         GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fVSLocation, v0, v1));
103     }
104 }
105 
set2fv(UniformHandle u,int arrayCount,const GrGLfloat v[]) const106 void GrGLProgramDataManager::set2fv(UniformHandle u,
107                                     int arrayCount,
108                                     const GrGLfloat v[]) const {
109     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
110     SkASSERT(uni.fType == kVec2f_GrSLType);
111     SkASSERT(arrayCount > 0);
112     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
113     SkDEBUGCODE(this->printUnused(uni);)
114     if (kUnusedUniform != uni.fFSLocation) {
115         GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fFSLocation, arrayCount, v));
116     }
117     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
118         GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fVSLocation, arrayCount, v));
119     }
120 }
121 
set3f(UniformHandle u,GrGLfloat v0,GrGLfloat v1,GrGLfloat v2) const122 void GrGLProgramDataManager::set3f(UniformHandle u, GrGLfloat v0, GrGLfloat v1, GrGLfloat v2) const {
123     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
124     SkASSERT(uni.fType == kVec3f_GrSLType);
125     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
126     SkDEBUGCODE(this->printUnused(uni);)
127     if (kUnusedUniform != uni.fFSLocation) {
128         GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fFSLocation, v0, v1, v2));
129     }
130     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
131         GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fVSLocation, v0, v1, v2));
132     }
133 }
134 
set3fv(UniformHandle u,int arrayCount,const GrGLfloat v[]) const135 void GrGLProgramDataManager::set3fv(UniformHandle u,
136                                     int arrayCount,
137                                     const GrGLfloat v[]) const {
138     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
139     SkASSERT(uni.fType == kVec3f_GrSLType);
140     SkASSERT(arrayCount > 0);
141     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
142     SkDEBUGCODE(this->printUnused(uni);)
143     if (kUnusedUniform != uni.fFSLocation) {
144         GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fFSLocation, arrayCount, v));
145     }
146     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
147         GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fVSLocation, arrayCount, v));
148     }
149 }
150 
set4f(UniformHandle u,GrGLfloat v0,GrGLfloat v1,GrGLfloat v2,GrGLfloat v3) const151 void GrGLProgramDataManager::set4f(UniformHandle u,
152                                    GrGLfloat v0,
153                                    GrGLfloat v1,
154                                    GrGLfloat v2,
155                                    GrGLfloat v3) const {
156     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
157     SkASSERT(uni.fType == kVec4f_GrSLType);
158     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
159     SkDEBUGCODE(this->printUnused(uni);)
160     if (kUnusedUniform != uni.fFSLocation) {
161         GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fFSLocation, v0, v1, v2, v3));
162     }
163     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
164         GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fVSLocation, v0, v1, v2, v3));
165     }
166 }
167 
set4fv(UniformHandle u,int arrayCount,const GrGLfloat v[]) const168 void GrGLProgramDataManager::set4fv(UniformHandle u,
169                                     int arrayCount,
170                                     const GrGLfloat v[]) const {
171     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
172     SkASSERT(uni.fType == kVec4f_GrSLType);
173     SkASSERT(arrayCount > 0);
174     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
175     SkDEBUGCODE(this->printUnused(uni);)
176     if (kUnusedUniform != uni.fFSLocation) {
177         GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fFSLocation, arrayCount, v));
178     }
179     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
180         GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fVSLocation, arrayCount, v));
181     }
182 }
183 
setMatrix3f(UniformHandle u,const GrGLfloat matrix[]) const184 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const GrGLfloat matrix[]) const {
185     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
186     SkASSERT(uni.fType == kMat33f_GrSLType);
187     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
188     SkDEBUGCODE(this->printUnused(uni);)
189     if (kUnusedUniform != uni.fFSLocation) {
190         GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fFSLocation, 1, false, matrix));
191     }
192     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
193         GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fVSLocation, 1, false, matrix));
194     }
195 }
196 
setMatrix4f(UniformHandle u,const GrGLfloat matrix[]) const197 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const GrGLfloat matrix[]) const {
198     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
199     SkASSERT(uni.fType == kMat44f_GrSLType);
200     SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
201     SkDEBUGCODE(this->printUnused(uni);)
202     if (kUnusedUniform != uni.fFSLocation) {
203         GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fFSLocation, 1, false, matrix));
204     }
205     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
206         GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fVSLocation, 1, false, matrix));
207     }
208 }
209 
setMatrix3fv(UniformHandle u,int arrayCount,const GrGLfloat matrices[]) const210 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u,
211                                           int arrayCount,
212                                           const GrGLfloat matrices[]) const {
213     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
214     SkASSERT(uni.fType == kMat33f_GrSLType);
215     SkASSERT(arrayCount > 0);
216     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
217     SkDEBUGCODE(this->printUnused(uni);)
218     if (kUnusedUniform != uni.fFSLocation) {
219         GR_GL_CALL(fGpu->glInterface(),
220                    UniformMatrix3fv(uni.fFSLocation, arrayCount, false, matrices));
221     }
222     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
223         GR_GL_CALL(fGpu->glInterface(),
224                    UniformMatrix3fv(uni.fVSLocation, arrayCount, false, matrices));
225     }
226 }
227 
setMatrix4fv(UniformHandle u,int arrayCount,const GrGLfloat matrices[]) const228 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u,
229                                           int arrayCount,
230                                           const GrGLfloat matrices[]) const {
231     const Uniform& uni = fUniforms[u.toProgramDataIndex()];
232     SkASSERT(uni.fType == kMat44f_GrSLType);
233     SkASSERT(arrayCount > 0);
234     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
235     SkDEBUGCODE(this->printUnused(uni);)
236     if (kUnusedUniform != uni.fFSLocation) {
237         GR_GL_CALL(fGpu->glInterface(),
238                    UniformMatrix4fv(uni.fFSLocation, arrayCount, false, matrices));
239     }
240     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
241         GR_GL_CALL(fGpu->glInterface(),
242                    UniformMatrix4fv(uni.fVSLocation, arrayCount, false, matrices));
243     }
244 }
245 
setSkMatrix(UniformHandle u,const SkMatrix & matrix) const246 void GrGLProgramDataManager::setSkMatrix(UniformHandle u, const SkMatrix& matrix) const {
247     GrGLfloat mt[] = {
248         matrix.get(SkMatrix::kMScaleX),
249         matrix.get(SkMatrix::kMSkewY),
250         matrix.get(SkMatrix::kMPersp0),
251         matrix.get(SkMatrix::kMSkewX),
252         matrix.get(SkMatrix::kMScaleY),
253         matrix.get(SkMatrix::kMPersp1),
254         matrix.get(SkMatrix::kMTransX),
255         matrix.get(SkMatrix::kMTransY),
256         matrix.get(SkMatrix::kMPersp2),
257     };
258     this->setMatrix3f(u, mt);
259 }
260 
261 #ifdef SK_DEBUG
printUnused(const Uniform & uni) const262 void GrGLProgramDataManager::printUnused(const Uniform& uni) const {
263     if (kUnusedUniform == uni.fFSLocation && kUnusedUniform == uni.fVSLocation) {
264         GrContextDebugf(fGpu->getContext(), "Unused uniform in shader\n");
265     }
266 }
267 #endif
268