blob: 689b291a9f41e857490a62737d6743148fcc7a67 [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
Greg Danielbc5d4d72017-05-05 10:28:42 -040053void GrGLProgramDataManager::setSamplerUniforms(const UniformInfoArray& samplers,
54 int startUnit) const {
egdaniel09aa1fc2016-04-20 07:09:46 -070055 for (int i = 0; i < samplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -050056 const UniformInfo& sampler = samplers[i];
57 SkASSERT(sampler.fVisibility);
58 if (kUnusedUniform != sampler.fLocation) {
Greg Danielbc5d4d72017-05-05 10:28:42 -040059 GR_GL_CALL(fGpu->glInterface(), Uniform1i(sampler.fLocation, i + startUnit));
egdaniel09aa1fc2016-04-20 07:09:46 -070060 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000061 }
62}
63
fmenozzi497e9e22016-06-21 09:42:12 -070064void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
65 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040066 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050067 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050068 if (kUnusedUniform != uni.fLocation) {
69 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
fmenozzi497e9e22016-06-21 09:42:12 -070070 }
71}
72
fmenozzi35a98c72016-07-20 08:26:12 -070073void GrGLProgramDataManager::set1iv(UniformHandle u,
74 int arrayCount,
75 const int v[]) const {
76 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040077 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
fmenozzi35a98c72016-07-20 08:26:12 -070078 SkASSERT(arrayCount > 0);
79 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050080 if (kUnusedUniform != uni.fLocation) {
81 GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
fmenozzi35a98c72016-07-20 08:26:12 -070082 }
83}
84
egdaniel018fb622015-10-28 07:26:40 -070085void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
joshualitte7afc2d2015-09-11 10:44:13 -070086 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -040087 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050088 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050089 if (kUnusedUniform != uni.fLocation) {
90 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000091 }
92}
93
kkinnunen7510b222014-07-30 00:04:16 -070094void GrGLProgramDataManager::set1fv(UniformHandle u,
95 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -070096 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -070097 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -040098 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000099 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000100 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000101 // This assert fires in some instances of the two-pt gradient for its VSParams.
102 // Once the uniform manager is responsible for inserting the duplicate uniform
103 // arrays in VS and FS driver bug workaround, this can be enabled.
joshualittaf2d56d2015-05-11 06:21:34 -0700104 // this->printUni(uni);
Brian Salomon101b8442016-11-18 11:58:54 -0500105 if (kUnusedUniform != uni.fLocation) {
106 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000107 }
108}
109
egdaniel018fb622015-10-28 07:26:40 -0700110void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700111 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400112 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500113 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500114 if (kUnusedUniform != uni.fLocation) {
115 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000116 }
117}
118
kkinnunen7510b222014-07-30 00:04:16 -0700119void GrGLProgramDataManager::set2fv(UniformHandle u,
120 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700121 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700122 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400123 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000124 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000125 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500126 if (kUnusedUniform != uni.fLocation) {
127 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000128 }
129}
130
egdaniel018fb622015-10-28 07:26:40 -0700131void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700132 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400133 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500134 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500135 if (kUnusedUniform != uni.fLocation) {
136 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000137 }
138}
139
kkinnunen7510b222014-07-30 00:04:16 -0700140void GrGLProgramDataManager::set3fv(UniformHandle u,
141 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700142 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700143 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400144 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000146 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500147 if (kUnusedUniform != uni.fLocation) {
148 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000149 }
150}
151
kkinnunen7510b222014-07-30 00:04:16 -0700152void GrGLProgramDataManager::set4f(UniformHandle u,
egdaniel018fb622015-10-28 07:26:40 -0700153 float v0,
154 float v1,
155 float v2,
156 float v3) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700157 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400158 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500159 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500160 if (kUnusedUniform != uni.fLocation) {
161 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000162 }
163}
164
kkinnunen7510b222014-07-30 00:04:16 -0700165void GrGLProgramDataManager::set4fv(UniformHandle u,
166 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700167 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700168 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400169 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000170 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000171 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500172 if (kUnusedUniform != uni.fLocation) {
173 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000174 }
175}
176
cdalton8d988b32016-03-07 15:39:09 -0800177void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
178 this->setMatrices<2>(u, 1, matrix);
179}
180
egdaniel018fb622015-10-28 07:26:40 -0700181void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800182 this->setMatrices<3>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000183}
184
egdaniel018fb622015-10-28 07:26:40 -0700185void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800186 this->setMatrices<4>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000187}
188
cdalton8d988b32016-03-07 15:39:09 -0800189void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
190 this->setMatrices<2>(u, arrayCount, m);
191}
192
193void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
194 this->setMatrices<3>(u, arrayCount, m);
195}
196
197void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
198 this->setMatrices<4>(u, arrayCount, m);
199}
200
201template<int N> struct set_uniform_matrix;
202
203template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
204 int arrayCount,
205 const float matrices[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700206 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400207 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400208 uni.fType == kHalf2x2_GrSLType + (N - 2));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000209 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000210 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500211 if (kUnusedUniform != uni.fLocation) {
212 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000213 }
214}
215
cdalton8d988b32016-03-07 15:39:09 -0800216template<> struct set_uniform_matrix<2> {
217 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
218 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000219 }
cdalton8d988b32016-03-07 15:39:09 -0800220};
221
222template<> struct set_uniform_matrix<3> {
223 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
224 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000225 }
cdalton8d988b32016-03-07 15:39:09 -0800226};
227
228template<> struct set_uniform_matrix<4> {
229 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
230 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
231 }
232};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000233
egdaniel0eafe792015-11-20 14:01:22 -0800234void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
joshualittd8dd47b2015-09-11 11:45:01 -0700235 int components,
236 const SkMatrix& matrix) const {
237 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
egdaniel0eafe792015-11-20 14:01:22 -0800238 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
joshualittd8dd47b2015-09-11 11:45:01 -0700239
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400240 SkASSERT((components == 2 && (fragmentInput.fType == kFloat2_GrSLType ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400241 fragmentInput.fType == kHalf2_GrSLType)) ||
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400242 (components == 3 && (fragmentInput.fType == kFloat3_GrSLType ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400243 fragmentInput.fType == kHalf3_GrSLType)));
joshualittd8dd47b2015-09-11 11:45:01 -0700244
245 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
246 fragmentInput.fLocation,
247 GR_GL_OBJECT_LINEAR,
248 components,
249 matrix);
250}