Create GLSLUniformHandler class for gpu backend
[platform/upstream/libSkiaSharp.git] / src / gpu / gl / GrGLProgramDataManager.cpp
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 "SkMatrix.h"
9 #include "gl/GrGLProgramDataManager.h"
10 #include "gl/GrGLGpu.h"
11 #include "glsl/GrGLSLUniformHandler.h"
12
13 #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \
14          SkASSERT(arrayCount <= uni.fArrayCount || \
15                   (1 == arrayCount && GrGLSLShaderVar::kNonArray == uni.fArrayCount))
16
17 GrGLProgramDataManager::GrGLProgramDataManager(GrGLGpu* gpu, GrGLuint programID,
18                                                const UniformInfoArray& uniforms,
19                                                const VaryingInfoArray& pathProcVaryings)
20     : fGpu(gpu)
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);
29         SkDEBUGCODE(
30             uniform.fArrayCount = builderUniform.fVariable.getArrayCount();
31             uniform.fType = builderUniform.fVariable.getType();
32         );
33         // TODO: Move the Xoom uniform array in both FS and VS bug workaround here.
34
35         if (GrGLSLUniformHandler::kVertex_Visibility & builderUniform.fVisibility) {
36             uniform.fVSLocation = builderUniform.fLocation;
37         } else {
38             uniform.fVSLocation = kUnusedUniform;
39         }
40         if (GrGLSLUniformHandler::kFragment_Visibility & builderUniform.fVisibility) {
41             uniform.fFSLocation = builderUniform.fLocation;
42         } else {
43             uniform.fFSLocation = kUnusedUniform;
44         }
45     }
46
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);
56         SkDEBUGCODE(
57             pathProcVarying.fArrayCount = builderPathProcVarying.fVariable.getArrayCount();
58             pathProcVarying.fType = builderPathProcVarying.fVariable.getType();
59         );
60         pathProcVarying.fLocation = builderPathProcVarying.fLocation;
61     }
62 }
63
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));
74     }
75     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
76         GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fVSLocation, texUnit));
77     }
78 }
79
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));
87     }
88     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
89         GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fVSLocation, v0));
90     }
91 }
92
93 void GrGLProgramDataManager::set1fv(UniformHandle u,
94                                     int arrayCount,
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));
106     }
107     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
108         GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fVSLocation, arrayCount, v));
109     }
110 }
111
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));
119     }
120     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
121         GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fVSLocation, v0, v1));
122     }
123 }
124
125 void GrGLProgramDataManager::set2fv(UniformHandle u,
126                                     int arrayCount,
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));
135     }
136     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
137         GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fVSLocation, arrayCount, v));
138     }
139 }
140
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));
148     }
149     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
150         GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fVSLocation, v0, v1, v2));
151     }
152 }
153
154 void GrGLProgramDataManager::set3fv(UniformHandle u,
155                                     int arrayCount,
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));
164     }
165     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
166         GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fVSLocation, arrayCount, v));
167     }
168 }
169
170 void GrGLProgramDataManager::set4f(UniformHandle u,
171                                    float v0,
172                                    float v1,
173                                    float v2,
174                                    float v3) const {
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));
181     }
182     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
183         GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fVSLocation, v0, v1, v2, v3));
184     }
185 }
186
187 void GrGLProgramDataManager::set4fv(UniformHandle u,
188                                     int arrayCount,
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));
197     }
198     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
199         GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fVSLocation, arrayCount, v));
200     }
201 }
202
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));
210     }
211     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
212         GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fVSLocation, 1, false, matrix));
213     }
214 }
215
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));
223     }
224     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
225         GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fVSLocation, 1, false, matrix));
226     }
227 }
228
229 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u,
230                                           int arrayCount,
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));
240     }
241     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
242         GR_GL_CALL(fGpu->glInterface(),
243                    UniformMatrix3fv(uni.fVSLocation, arrayCount, false, matrices));
244     }
245 }
246
247 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u,
248                                           int arrayCount,
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));
258     }
259     if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
260         GR_GL_CALL(fGpu->glInterface(),
261                    UniformMatrix4fv(uni.fVSLocation, arrayCount, false, matrices));
262     }
263 }
264
265 void GrGLProgramDataManager::setSkMatrix(UniformHandle u, const SkMatrix& matrix) const {
266     float mt[] = {
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),
276     };
277     this->setMatrix3f(u, mt);
278 }
279
280 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
281                                                            int components,
282                                                            const SkMatrix& matrix) const {
283     SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
284     const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
285
286     SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
287               (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
288
289     fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
290                                                                   fragmentInput.fLocation,
291                                                                   GR_GL_OBJECT_LINEAR,
292                                                                   components,
293                                                                   matrix);
294 }
295
296 #ifdef SK_DEBUG
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");
300     }
301 }
302 #endif