• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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((COUNT) <= (UNI).fArrayCount || \
15                   (1 == (COUNT) && GrShaderVar::kNonArray == (UNI).fArrayCount))
16 
GrGLProgramDataManager(GrGLGpu * gpu,GrGLuint programID,const UniformInfoArray & uniforms,const VaryingInfoArray & pathProcVaryings)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(GrShaderVar::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         uniform.fLocation = builderUniform.fLocation;
34     }
35 
36     // NVPR programs have separable varyings
37     count = pathProcVaryings.count();
38     fPathProcVaryings.push_back_n(count);
39     for (int i = 0; i < count; i++) {
40         SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
41         PathProcVarying& pathProcVarying = fPathProcVaryings[i];
42         const VaryingInfo& builderPathProcVarying = pathProcVaryings[i];
43         SkASSERT(GrShaderVar::kNonArray == builderPathProcVarying.fVariable.getArrayCount() ||
44                  builderPathProcVarying.fVariable.getArrayCount() > 0);
45         SkDEBUGCODE(
46             pathProcVarying.fArrayCount = builderPathProcVarying.fVariable.getArrayCount();
47             pathProcVarying.fType = builderPathProcVarying.fVariable.getType();
48         );
49         pathProcVarying.fLocation = builderPathProcVarying.fLocation;
50     }
51 }
52 
setSamplerUniforms(const UniformInfoArray & samplers,int startUnit) const53 void GrGLProgramDataManager::setSamplerUniforms(const UniformInfoArray& samplers,
54                                                 int startUnit) const {
55     for (int i = 0; i < samplers.count(); ++i) {
56         const UniformInfo& sampler = samplers[i];
57         SkASSERT(sampler.fVisibility);
58         if (kUnusedUniform != sampler.fLocation) {
59             GR_GL_CALL(fGpu->glInterface(), Uniform1i(sampler.fLocation, i + startUnit));
60         }
61     }
62 }
63 
setImageStorages(const UniformInfoArray & images) const64 void GrGLProgramDataManager::setImageStorages(const UniformInfoArray& images) const {
65     for (int i = 0; i < images.count(); ++i) {
66         const UniformInfo& image = images[i];
67         SkASSERT(image.fVisibility);
68         if (kUnusedUniform != image.fLocation) {
69             GR_GL_CALL(fGpu->glInterface(), Uniform1i(image.fLocation, i));
70         }
71     }
72 }
73 
set1i(UniformHandle u,int32_t i) const74 void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
75     const Uniform& uni = fUniforms[u.toIndex()];
76     SkASSERT(uni.fType == kInt_GrSLType);
77     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
78     if (kUnusedUniform != uni.fLocation) {
79         GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
80     }
81 }
82 
set1iv(UniformHandle u,int arrayCount,const int v[]) const83 void GrGLProgramDataManager::set1iv(UniformHandle u,
84                                     int arrayCount,
85                                     const int v[]) const {
86     const Uniform& uni = fUniforms[u.toIndex()];
87     SkASSERT(uni.fType == kInt_GrSLType);
88     SkASSERT(arrayCount > 0);
89     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
90     if (kUnusedUniform != uni.fLocation) {
91         GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
92     }
93 }
94 
set1f(UniformHandle u,float v0) const95 void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
96     const Uniform& uni = fUniforms[u.toIndex()];
97     SkASSERT(uni.fType == kFloat_GrSLType);
98     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
99     if (kUnusedUniform != uni.fLocation) {
100         GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
101     }
102 }
103 
set1fv(UniformHandle u,int arrayCount,const float v[]) const104 void GrGLProgramDataManager::set1fv(UniformHandle u,
105                                     int arrayCount,
106                                     const float v[]) const {
107     const Uniform& uni = fUniforms[u.toIndex()];
108     SkASSERT(uni.fType == kFloat_GrSLType);
109     SkASSERT(arrayCount > 0);
110     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
111     // This assert fires in some instances of the two-pt gradient for its VSParams.
112     // Once the uniform manager is responsible for inserting the duplicate uniform
113     // arrays in VS and FS driver bug workaround, this can be enabled.
114     // this->printUni(uni);
115     if (kUnusedUniform != uni.fLocation) {
116         GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
117     }
118 }
119 
set2f(UniformHandle u,float v0,float v1) const120 void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
121     const Uniform& uni = fUniforms[u.toIndex()];
122     SkASSERT(uni.fType == kVec2f_GrSLType);
123     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
124     if (kUnusedUniform != uni.fLocation) {
125         GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
126     }
127 }
128 
set2fv(UniformHandle u,int arrayCount,const float v[]) const129 void GrGLProgramDataManager::set2fv(UniformHandle u,
130                                     int arrayCount,
131                                     const float v[]) const {
132     const Uniform& uni = fUniforms[u.toIndex()];
133     SkASSERT(uni.fType == kVec2f_GrSLType);
134     SkASSERT(arrayCount > 0);
135     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
136     if (kUnusedUniform != uni.fLocation) {
137         GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v));
138     }
139 }
140 
set3f(UniformHandle u,float v0,float v1,float v2) const141 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(GrShaderVar::kNonArray == uni.fArrayCount);
145     if (kUnusedUniform != uni.fLocation) {
146         GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
147     }
148 }
149 
set3fv(UniformHandle u,int arrayCount,const float v[]) const150 void GrGLProgramDataManager::set3fv(UniformHandle u,
151                                     int arrayCount,
152                                     const float v[]) const {
153     const Uniform& uni = fUniforms[u.toIndex()];
154     SkASSERT(uni.fType == kVec3f_GrSLType);
155     SkASSERT(arrayCount > 0);
156     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
157     if (kUnusedUniform != uni.fLocation) {
158         GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v));
159     }
160 }
161 
set4f(UniformHandle u,float v0,float v1,float v2,float v3) const162 void GrGLProgramDataManager::set4f(UniformHandle u,
163                                    float v0,
164                                    float v1,
165                                    float v2,
166                                    float v3) const {
167     const Uniform& uni = fUniforms[u.toIndex()];
168     SkASSERT(uni.fType == kVec4f_GrSLType);
169     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
170     if (kUnusedUniform != uni.fLocation) {
171         GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
172     }
173 }
174 
set4fv(UniformHandle u,int arrayCount,const float v[]) const175 void GrGLProgramDataManager::set4fv(UniformHandle u,
176                                     int arrayCount,
177                                     const float v[]) const {
178     const Uniform& uni = fUniforms[u.toIndex()];
179     SkASSERT(uni.fType == kVec4f_GrSLType);
180     SkASSERT(arrayCount > 0);
181     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
182     if (kUnusedUniform != uni.fLocation) {
183         GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
184     }
185 }
186 
setMatrix2f(UniformHandle u,const float matrix[]) const187 void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
188     this->setMatrices<2>(u, 1, matrix);
189 }
190 
setMatrix3f(UniformHandle u,const float matrix[]) const191 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
192     this->setMatrices<3>(u, 1, matrix);
193 }
194 
setMatrix4f(UniformHandle u,const float matrix[]) const195 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
196     this->setMatrices<4>(u, 1, matrix);
197 }
198 
setMatrix2fv(UniformHandle u,int arrayCount,const float m[]) const199 void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
200     this->setMatrices<2>(u, arrayCount, m);
201 }
202 
setMatrix3fv(UniformHandle u,int arrayCount,const float m[]) const203 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
204     this->setMatrices<3>(u, arrayCount, m);
205 }
206 
setMatrix4fv(UniformHandle u,int arrayCount,const float m[]) const207 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
208     this->setMatrices<4>(u, arrayCount, m);
209 }
210 
211 template<int N> struct set_uniform_matrix;
212 
setMatrices(UniformHandle u,int arrayCount,const float matrices[]) const213 template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
214                                                                 int arrayCount,
215                                                                 const float matrices[]) const {
216     const Uniform& uni = fUniforms[u.toIndex()];
217     SkASSERT(uni.fType == kMat22f_GrSLType + (N - 2));
218     SkASSERT(arrayCount > 0);
219     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
220     if (kUnusedUniform != uni.fLocation) {
221         set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
222     }
223 }
224 
225 template<> struct set_uniform_matrix<2> {
setset_uniform_matrix226     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
227         GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
228     }
229 };
230 
231 template<> struct set_uniform_matrix<3> {
setset_uniform_matrix232     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
233         GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
234     }
235 };
236 
237 template<> struct set_uniform_matrix<4> {
setset_uniform_matrix238     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
239         GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
240     }
241 };
242 
setPathFragmentInputTransform(VaryingHandle u,int components,const SkMatrix & matrix) const243 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
244                                                            int components,
245                                                            const SkMatrix& matrix) const {
246     SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
247     const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
248 
249     SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
250               (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
251 
252     fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
253                                                                   fragmentInput.fLocation,
254                                                                   GR_GL_OBJECT_LINEAR,
255                                                                   components,
256                                                                   matrix);
257 }
258