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.
8 #include "gl/GrGLPathRendering.h"
9 #include "gl/GrGLUniformHandle.h"
10 #include "gl/GrGpuGL.h"
13 #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \
14 SkASSERT(arrayCount <= uni.fArrayCount || \
15 (1 == arrayCount && GrGLShaderVar::kNonArray == uni.fArrayCount))
17 GrGLProgramDataManager::GrGLProgramDataManager(GrGpuGL* gpu, const UniformInfoArray& uniforms)
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);
27 uniform.fArrayCount = builderUniform.fVariable.getArrayCount();
28 uniform.fType = builderUniform.fVariable.getType();
30 // TODO: Move the Xoom uniform array in both FS and VS bug workaround here.
32 if (GrGLProgramBuilder::kVertex_Visibility & builderUniform.fVisibility) {
33 uniform.fVSLocation = builderUniform.fLocation;
35 uniform.fVSLocation = kUnusedUniform;
37 if (GrGLProgramBuilder::kFragment_Visibility & builderUniform.fVisibility) {
38 uniform.fFSLocation = builderUniform.fLocation;
40 uniform.fFSLocation = kUnusedUniform;
45 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 // SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
53 if (kUnusedUniform != uni.fFSLocation) {
54 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fFSLocation, texUnit));
56 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
57 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fVSLocation, texUnit));
61 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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
66 if (kUnusedUniform != uni.fFSLocation) {
67 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fFSLocation, v0));
69 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
70 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fVSLocation, v0));
74 void GrGLProgramDataManager::set1fv(UniformHandle u,
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 //SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
85 if (kUnusedUniform != uni.fFSLocation) {
86 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fFSLocation, arrayCount, v));
88 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
89 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fVSLocation, arrayCount, v));
93 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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
98 if (kUnusedUniform != uni.fFSLocation) {
99 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fFSLocation, v0, v1));
101 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
102 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fVSLocation, v0, v1));
106 void GrGLProgramDataManager::set2fv(UniformHandle u,
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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
114 if (kUnusedUniform != uni.fFSLocation) {
115 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fFSLocation, arrayCount, v));
117 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
118 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fVSLocation, arrayCount, v));
122 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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
127 if (kUnusedUniform != uni.fFSLocation) {
128 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fFSLocation, v0, v1, v2));
130 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
131 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fVSLocation, v0, v1, v2));
135 void GrGLProgramDataManager::set3fv(UniformHandle u,
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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
143 if (kUnusedUniform != uni.fFSLocation) {
144 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fFSLocation, arrayCount, v));
146 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
147 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fVSLocation, arrayCount, v));
151 void GrGLProgramDataManager::set4f(UniformHandle u,
155 GrGLfloat v3) const {
156 const Uniform& uni = fUniforms[u.toProgramDataIndex()];
157 SkASSERT(uni.fType == kVec4f_GrSLType);
158 SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
159 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
160 if (kUnusedUniform != uni.fFSLocation) {
161 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fFSLocation, v0, v1, v2, v3));
163 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
164 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fVSLocation, v0, v1, v2, v3));
168 void GrGLProgramDataManager::set4fv(UniformHandle u,
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 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
176 if (kUnusedUniform != uni.fFSLocation) {
177 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fFSLocation, arrayCount, v));
179 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
180 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fVSLocation, arrayCount, v));
184 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 // TODO: Re-enable this assert once texture matrices aren't forced on all effects
189 // SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
190 if (kUnusedUniform != uni.fFSLocation) {
191 GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fFSLocation, 1, false, matrix));
193 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
194 GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fVSLocation, 1, false, matrix));
198 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const GrGLfloat matrix[]) const {
199 const Uniform& uni = fUniforms[u.toProgramDataIndex()];
200 SkASSERT(uni.fType == kMat44f_GrSLType);
201 SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount);
202 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
203 if (kUnusedUniform != uni.fFSLocation) {
204 GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fFSLocation, 1, false, matrix));
206 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
207 GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fVSLocation, 1, false, matrix));
211 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u,
213 const GrGLfloat matrices[]) const {
214 const Uniform& uni = fUniforms[u.toProgramDataIndex()];
215 SkASSERT(uni.fType == kMat33f_GrSLType);
216 SkASSERT(arrayCount > 0);
217 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
218 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
219 if (kUnusedUniform != uni.fFSLocation) {
220 GR_GL_CALL(fGpu->glInterface(),
221 UniformMatrix3fv(uni.fFSLocation, arrayCount, false, matrices));
223 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
224 GR_GL_CALL(fGpu->glInterface(),
225 UniformMatrix3fv(uni.fVSLocation, arrayCount, false, matrices));
229 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u,
231 const GrGLfloat matrices[]) const {
232 const Uniform& uni = fUniforms[u.toProgramDataIndex()];
233 SkASSERT(uni.fType == kMat44f_GrSLType);
234 SkASSERT(arrayCount > 0);
235 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
236 SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation);
237 if (kUnusedUniform != uni.fFSLocation) {
238 GR_GL_CALL(fGpu->glInterface(),
239 UniformMatrix4fv(uni.fFSLocation, arrayCount, false, matrices));
241 if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) {
242 GR_GL_CALL(fGpu->glInterface(),
243 UniformMatrix4fv(uni.fVSLocation, arrayCount, false, matrices));
247 void GrGLProgramDataManager::setSkMatrix(UniformHandle u, const SkMatrix& matrix) const {
249 matrix.get(SkMatrix::kMScaleX),
250 matrix.get(SkMatrix::kMSkewY),
251 matrix.get(SkMatrix::kMPersp0),
252 matrix.get(SkMatrix::kMSkewX),
253 matrix.get(SkMatrix::kMScaleY),
254 matrix.get(SkMatrix::kMPersp1),
255 matrix.get(SkMatrix::kMTransX),
256 matrix.get(SkMatrix::kMTransY),
257 matrix.get(SkMatrix::kMPersp2),
259 this->setMatrix3f(u, mt);