blob: 863eab7abf13e9d8961f6a4d0cecbf48835735fe [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 || \
Brian Salomon99938a82016-11-21 13:41:08 -050015 (1 == (COUNT) && GrShaderVar::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];
Brian Salomon99938a82016-11-21 13:41:08 -050027 SkASSERT(GrShaderVar::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];
Brian Salomon99938a82016-11-21 13:41:08 -050043 SkASSERT(GrShaderVar::kNonArray == builderPathProcVarying.fVariable.getArrayCount() ||
egdaniel0eafe792015-11-20 14:01:22 -080044 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
Brian Salomonbe348822016-11-22 15:56:30 -050068void GrGLProgramDataManager::setImageStorages(const UniformInfoArray& images) const {
69 for (int i = 0; i < images.count(); ++i) {
70 const UniformInfo& image = images[i];
71 SkASSERT(image.fVisibility);
72 if (kUnusedUniform != image.fLocation) {
73 GR_GL_CALL(fGpu->glInterface(), Uniform1i(image.fLocation, i));
74 }
75 }
76}
77
fmenozzi497e9e22016-06-21 09:42:12 -070078void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
79 const Uniform& uni = fUniforms[u.toIndex()];
80 SkASSERT(uni.fType == kInt_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050081 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
fmenozzi497e9e22016-06-21 09:42:12 -070082 SkDEBUGCODE(this->printUnused(uni));
Brian Salomon101b8442016-11-18 11:58:54 -050083 if (kUnusedUniform != uni.fLocation) {
84 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
fmenozzi497e9e22016-06-21 09:42:12 -070085 }
86}
87
fmenozzi35a98c72016-07-20 08:26:12 -070088void GrGLProgramDataManager::set1iv(UniformHandle u,
89 int arrayCount,
90 const int v[]) const {
91 const Uniform& uni = fUniforms[u.toIndex()];
92 SkASSERT(uni.fType == kInt_GrSLType);
93 SkASSERT(arrayCount > 0);
94 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050095 if (kUnusedUniform != uni.fLocation) {
96 GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
fmenozzi35a98c72016-07-20 08:26:12 -070097 }
98}
99
egdaniel018fb622015-10-28 07:26:40 -0700100void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700101 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(uni.fType == kFloat_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500103 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700104 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500105 if (kUnusedUniform != uni.fLocation) {
106 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000107 }
108}
109
kkinnunen7510b222014-07-30 00:04:16 -0700110void GrGLProgramDataManager::set1fv(UniformHandle u,
111 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700112 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700113 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(uni.fType == kFloat_GrSLType);
115 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000116 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000117 // This assert fires in some instances of the two-pt gradient for its VSParams.
118 // Once the uniform manager is responsible for inserting the duplicate uniform
119 // arrays in VS and FS driver bug workaround, this can be enabled.
joshualittaf2d56d2015-05-11 06:21:34 -0700120 // this->printUni(uni);
Brian Salomon101b8442016-11-18 11:58:54 -0500121 if (kUnusedUniform != uni.fLocation) {
122 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000123 }
124}
125
egdaniel018fb622015-10-28 07:26:40 -0700126void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700127 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000128 SkASSERT(uni.fType == kVec2f_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500129 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700130 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500131 if (kUnusedUniform != uni.fLocation) {
132 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000133 }
134}
135
kkinnunen7510b222014-07-30 00:04:16 -0700136void GrGLProgramDataManager::set2fv(UniformHandle u,
137 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700138 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700139 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000140 SkASSERT(uni.fType == kVec2f_GrSLType);
141 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000142 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
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(), Uniform2fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000146 }
147}
148
egdaniel018fb622015-10-28 07:26:40 -0700149void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700150 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000151 SkASSERT(uni.fType == kVec3f_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500152 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700153 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500154 if (kUnusedUniform != uni.fLocation) {
155 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000156 }
157}
158
kkinnunen7510b222014-07-30 00:04:16 -0700159void GrGLProgramDataManager::set3fv(UniformHandle u,
160 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700161 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700162 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(uni.fType == kVec3f_GrSLType);
164 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000165 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700166 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500167 if (kUnusedUniform != uni.fLocation) {
168 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000169 }
170}
171
kkinnunen7510b222014-07-30 00:04:16 -0700172void GrGLProgramDataManager::set4f(UniformHandle u,
egdaniel018fb622015-10-28 07:26:40 -0700173 float v0,
174 float v1,
175 float v2,
176 float v3) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700177 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000178 SkASSERT(uni.fType == kVec4f_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500179 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700180 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500181 if (kUnusedUniform != uni.fLocation) {
182 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000183 }
184}
185
kkinnunen7510b222014-07-30 00:04:16 -0700186void GrGLProgramDataManager::set4fv(UniformHandle u,
187 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700188 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700189 const Uniform& uni = fUniforms[u.toIndex()];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000190 SkASSERT(uni.fType == kVec4f_GrSLType);
191 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000192 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700193 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500194 if (kUnusedUniform != uni.fLocation) {
195 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000196 }
197}
198
cdalton8d988b32016-03-07 15:39:09 -0800199void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
200 this->setMatrices<2>(u, 1, matrix);
201}
202
egdaniel018fb622015-10-28 07:26:40 -0700203void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800204 this->setMatrices<3>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000205}
206
egdaniel018fb622015-10-28 07:26:40 -0700207void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800208 this->setMatrices<4>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000209}
210
cdalton8d988b32016-03-07 15:39:09 -0800211void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
212 this->setMatrices<2>(u, arrayCount, m);
213}
214
215void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
216 this->setMatrices<3>(u, arrayCount, m);
217}
218
219void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
220 this->setMatrices<4>(u, arrayCount, m);
221}
222
223template<int N> struct set_uniform_matrix;
224
225template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
226 int arrayCount,
227 const float matrices[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700228 const Uniform& uni = fUniforms[u.toIndex()];
cdalton8d988b32016-03-07 15:39:09 -0800229 SkASSERT(uni.fType == kMat22f_GrSLType + (N - 2));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000230 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000231 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
joshualitt7d022d62015-05-12 12:03:50 -0700232 SkDEBUGCODE(this->printUnused(uni);)
Brian Salomon101b8442016-11-18 11:58:54 -0500233 if (kUnusedUniform != uni.fLocation) {
234 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000235 }
236}
237
cdalton8d988b32016-03-07 15:39:09 -0800238template<> struct set_uniform_matrix<2> {
239 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
240 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000241 }
cdalton8d988b32016-03-07 15:39:09 -0800242};
243
244template<> struct set_uniform_matrix<3> {
245 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
246 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000247 }
cdalton8d988b32016-03-07 15:39:09 -0800248};
249
250template<> struct set_uniform_matrix<4> {
251 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
252 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
253 }
254};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000255
egdaniel0eafe792015-11-20 14:01:22 -0800256void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
joshualittd8dd47b2015-09-11 11:45:01 -0700257 int components,
258 const SkMatrix& matrix) const {
259 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
egdaniel0eafe792015-11-20 14:01:22 -0800260 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
joshualittd8dd47b2015-09-11 11:45:01 -0700261
262 SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
263 (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
264
265 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
266 fragmentInput.fLocation,
267 GR_GL_OBJECT_LINEAR,
268 components,
269 matrix);
270}
271
joshualittaf2d56d2015-05-11 06:21:34 -0700272#ifdef SK_DEBUG
273void GrGLProgramDataManager::printUnused(const Uniform& uni) const {
Brian Salomon101b8442016-11-18 11:58:54 -0500274 if (kUnusedUniform == uni.fLocation) {
bsalomon682c2692015-05-22 14:01:46 -0700275 GrCapsDebugf(fGpu->caps(), "Unused uniform in shader\n");
joshualittaf2d56d2015-05-11 06:21:34 -0700276 }
277}
278#endif