blob: 68d01669a8684784d6c93cef8d4e23fbdcf062e4 [file] [log] [blame]
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +00001/*
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
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00008#include "SkMatrix.h"
joshualitte7afc2d2015-09-11 10:44:13 -07009#include "gl/GrGLProgramDataManager.h"
10#include "gl/GrGLGpu.h"
egdaniel7ea439b2015-12-03 09:20:44 -080011#include "glsl/GrGLSLUniformHandler.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000012
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000013#define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \
cdalton8d988b32016-03-07 15:39:09 -080014 SkASSERT((COUNT) <= (UNI).fArrayCount || \
15 (1 == (COUNT) && GrGLSLShaderVar::kNonArray == (UNI).fArrayCount))
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000016
joshualittd8dd47b2015-09-11 11:45:01 -070017GrGLProgramDataManager::GrGLProgramDataManager(GrGLGpu* gpu, GrGLuint programID,
18 const UniformInfoArray& uniforms,
egdaniel0eafe792015-11-20 14:01:22 -080019 const VaryingInfoArray& pathProcVaryings)
joshualittd8dd47b2015-09-11 11:45:01 -070020 : fGpu(gpu)
21 , fProgramID(programID) {
joshualitt47bb3822014-10-07 16:43:25 -070022 int count = uniforms.count();
kkinnunendddc18a2014-08-03 23:19:46 -070023 fUniforms.push_back_n(count);
24 for (int i = 0; i < count; i++) {
25 Uniform& uniform = fUniforms[i];
joshualitt47bb3822014-10-07 16:43:25 -070026 const UniformInfo& builderUniform = uniforms[i];
egdaniel0d3f0612015-10-21 10:45:48 -070027 SkASSERT(GrGLSLShaderVar::kNonArray == builderUniform.fVariable.getArrayCount() ||
kkinnunendddc18a2014-08-03 23:19:46 -070028 builderUniform.fVariable.getArrayCount() > 0);
29 SkDEBUGCODE(
30 uniform.fArrayCount = builderUniform.fVariable.getArrayCount();
31 uniform.fType = builderUniform.fVariable.getType();
32 );
Brian Salomon101b8442016-11-18 11:58:54 -050033 uniform.fLocation = builderUniform.fLocation;
kkinnunendddc18a2014-08-03 23:19:46 -070034 }
joshualittd8dd47b2015-09-11 11:45:01 -070035
36 // NVPR programs have separable varyings
egdaniel0eafe792015-11-20 14:01:22 -080037 count = pathProcVaryings.count();
38 fPathProcVaryings.push_back_n(count);
joshualittd8dd47b2015-09-11 11:45:01 -070039 for (int i = 0; i < count; i++) {
40 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
egdaniel0eafe792015-11-20 14:01:22 -080041 PathProcVarying& pathProcVarying = fPathProcVaryings[i];
42 const VaryingInfo& builderPathProcVarying = pathProcVaryings[i];
43 SkASSERT(GrGLSLShaderVar::kNonArray == builderPathProcVarying.fVariable.getArrayCount() ||
44 builderPathProcVarying.fVariable.getArrayCount() > 0);
joshualittd8dd47b2015-09-11 11:45:01 -070045 SkDEBUGCODE(
egdaniel0eafe792015-11-20 14:01:22 -080046 pathProcVarying.fArrayCount = builderPathProcVarying.fVariable.getArrayCount();
47 pathProcVarying.fType = builderPathProcVarying.fVariable.getType();
joshualittd8dd47b2015-09-11 11:45:01 -070048 );
egdaniel0eafe792015-11-20 14:01:22 -080049 pathProcVarying.fLocation = builderPathProcVarying.fLocation;
joshualittd8dd47b2015-09-11 11:45:01 -070050 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000051}
52
Brian Salomon101b8442016-11-18 11:58:54 -050053void GrGLProgramDataManager::setSamplers(const UniformInfoArray& samplers) const {
egdaniel09aa1fc2016-04-20 07:09:46 -070054 for (int i = 0; i < samplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -050055 const UniformInfo& sampler = samplers[i];
56 SkASSERT(sampler.fVisibility);
57 if (kUnusedUniform != sampler.fLocation) {
58 GR_GL_CALL(fGpu->glInterface(), Uniform1i(sampler.fLocation, i));
egdaniel09aa1fc2016-04-20 07:09:46 -070059 } else {
Brian Salomon101b8442016-11-18 11:58:54 -050060 // FIXME: We still insert a single sampler uniform for every stage. If the shader does
61 // not reference the sampler then the compiler may have optimized it out. Uncomment this
62 // assert once stages insert their own samplers.
63 // this->printUnused(uni);
egdaniel09aa1fc2016-04-20 07:09:46 -070064 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000065 }
66}
67
fmenozzi497e9e22016-06-21 09:42:12 -070068void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
69 const Uniform& uni = fUniforms[u.toIndex()];
70 SkASSERT(uni.fType == kInt_GrSLType);
71 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
72 SkDEBUGCODE(this->printUnused(uni));
Brian Salomon101b8442016-11-18 11:58:54 -050073 if (kUnusedUniform != uni.fLocation) {
74 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
fmenozzi497e9e22016-06-21 09:42:12 -070075 }
76}
77
fmenozzi35a98c72016-07-20 08:26:12 -070078void GrGLProgramDataManager::set1iv(UniformHandle u,
79 int arrayCount,
80 const int v[]) const {
81 const Uniform& uni = fUniforms[u.toIndex()];
82 SkASSERT(uni.fType == kInt_GrSLType);
83 SkASSERT(arrayCount > 0);
84 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050085 if (kUnusedUniform != uni.fLocation) {
86 GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
fmenozzi35a98c72016-07-20 08:26:12 -070087 }
88}
89
egdaniel018fb622015-10-28 07:26:40 -070090void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
joshualitte7afc2d2015-09-11 10:44:13 -070091 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000092 SkASSERT(uni.fType == kFloat_GrSLType);
egdaniel0d3f0612015-10-21 10:45:48 -070093 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -070094 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -050095 if (kUnusedUniform != uni.fLocation) {
96 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000097 }
98}
99
kkinnunen7510b222014-07-30 00:04:16 -0700100void GrGLProgramDataManager::set1fv(UniformHandle u,
101 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700102 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700103 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000104 SkASSERT(uni.fType == kFloat_GrSLType);
105 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000106 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000107 // This assert fires in some instances of the two-pt gradient for its VSParams.
108 // Once the uniform manager is responsible for inserting the duplicate uniform
109 // arrays in VS and FS driver bug workaround, this can be enabled.
joshualittaf2d56d2015-05-11 06:21:34 -0700110 // this->printUni(uni);
Brian Salomon101b8442016-11-18 11:58:54 -0500111 if (kUnusedUniform != uni.fLocation) {
112 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000113 }
114}
115
egdaniel018fb622015-10-28 07:26:40 -0700116void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700117 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000118 SkASSERT(uni.fType == kVec2f_GrSLType);
egdaniel0d3f0612015-10-21 10:45:48 -0700119 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700120 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500121 if (kUnusedUniform != uni.fLocation) {
122 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000123 }
124}
125
kkinnunen7510b222014-07-30 00:04:16 -0700126void GrGLProgramDataManager::set2fv(UniformHandle u,
127 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700128 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700129 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000130 SkASSERT(uni.fType == kVec2f_GrSLType);
131 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000132 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700133 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500134 if (kUnusedUniform != uni.fLocation) {
135 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000136 }
137}
138
egdaniel018fb622015-10-28 07:26:40 -0700139void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700140 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000141 SkASSERT(uni.fType == kVec3f_GrSLType);
egdaniel0d3f0612015-10-21 10:45:48 -0700142 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700143 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500144 if (kUnusedUniform != uni.fLocation) {
145 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000146 }
147}
148
kkinnunen7510b222014-07-30 00:04:16 -0700149void GrGLProgramDataManager::set3fv(UniformHandle u,
150 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700151 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700152 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(uni.fType == kVec3f_GrSLType);
154 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000155 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700156 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500157 if (kUnusedUniform != uni.fLocation) {
158 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000159 }
160}
161
kkinnunen7510b222014-07-30 00:04:16 -0700162void GrGLProgramDataManager::set4f(UniformHandle u,
egdaniel018fb622015-10-28 07:26:40 -0700163 float v0,
164 float v1,
165 float v2,
166 float v3) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700167 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000168 SkASSERT(uni.fType == kVec4f_GrSLType);
egdaniel0d3f0612015-10-21 10:45:48 -0700169 SkASSERT(GrGLSLShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700170 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500171 if (kUnusedUniform != uni.fLocation) {
172 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000173 }
174}
175
kkinnunen7510b222014-07-30 00:04:16 -0700176void GrGLProgramDataManager::set4fv(UniformHandle u,
177 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700178 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700179 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000180 SkASSERT(uni.fType == kVec4f_GrSLType);
181 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000182 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700183 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500184 if (kUnusedUniform != uni.fLocation) {
185 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000186 }
187}
188
cdalton8d988b32016-03-07 15:39:09 -0800189void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
190 this->setMatrices<2>(u, 1, matrix);
191}
192
egdaniel018fb622015-10-28 07:26:40 -0700193void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800194 this->setMatrices<3>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000195}
196
egdaniel018fb622015-10-28 07:26:40 -0700197void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800198 this->setMatrices<4>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000199}
200
cdalton8d988b32016-03-07 15:39:09 -0800201void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
202 this->setMatrices<2>(u, arrayCount, m);
203}
204
205void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
206 this->setMatrices<3>(u, arrayCount, m);
207}
208
209void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
210 this->setMatrices<4>(u, arrayCount, m);
211}
212
213template<int N> struct set_uniform_matrix;
214
215template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
216 int arrayCount,
217 const float matrices[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700218 const Uniform& uni = fUniforms[u.toIndex()];
cdalton8d988b32016-03-07 15:39:09 -0800219 SkASSERT(uni.fType == kMat22f_GrSLType + (N - 2));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000220 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000221 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700222 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500223 if (kUnusedUniform != uni.fLocation) {
224 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000225 }
226}
227
cdalton8d988b32016-03-07 15:39:09 -0800228template<> struct set_uniform_matrix<2> {
229 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
230 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000231 }
cdalton8d988b32016-03-07 15:39:09 -0800232};
233
234template<> struct set_uniform_matrix<3> {
235 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
236 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000237 }
cdalton8d988b32016-03-07 15:39:09 -0800238};
239
240template<> struct set_uniform_matrix<4> {
241 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
242 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
243 }
244};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000245
egdaniel0eafe792015-11-20 14:01:22 -0800246void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
joshualittd8dd47b2015-09-11 11:45:01 -0700247 int components,
248 const SkMatrix& matrix) const {
249 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
egdaniel0eafe792015-11-20 14:01:22 -0800250 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
joshualittd8dd47b2015-09-11 11:45:01 -0700251
252 SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
253 (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
254
255 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
256 fragmentInput.fLocation,
257 GR_GL_OBJECT_LINEAR,
258 components,
259 matrix);
260}
261
joshualittaf2d56d2015-05-11 06:21:34 -0700262#ifdef SK_DEBUG
263void GrGLProgramDataManager::printUnused(const Uniform& uni) const {
Brian Salomon101b8442016-11-18 11:58:54 -0500264 if (kUnusedUniform == uni.fLocation) {
bsalomon682c2692015-05-22 14:01:46 -0700265 GrCapsDebugf(fGpu->caps(), "Unused uniform in shader\n");
joshualittaf2d56d2015-05-11 06:21:34 -0700266 }
267}
268#endif