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