blob: ab8dee6306276c6b7f9186ba108e3cf84d4aa100 [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
Brian Salomonf9f45122016-11-29 11:59:17 -050064void GrGLProgramDataManager::setImageStorages(const UniformInfoArray& images) const {
65 for (int i = 0; i < images.count(); ++i) {
66 const UniformInfo& image = images[i];
67 SkASSERT(image.fVisibility);
68 if (kUnusedUniform != image.fLocation) {
69 GR_GL_CALL(fGpu->glInterface(), Uniform1i(image.fLocation, i));
70 }
71 }
72}
73
fmenozzi497e9e22016-06-21 09:42:12 -070074void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
75 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040076 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050077 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050078 if (kUnusedUniform != uni.fLocation) {
79 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
fmenozzi497e9e22016-06-21 09:42:12 -070080 }
81}
82
fmenozzi35a98c72016-07-20 08:26:12 -070083void GrGLProgramDataManager::set1iv(UniformHandle u,
84 int arrayCount,
85 const int v[]) const {
86 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040087 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
fmenozzi35a98c72016-07-20 08:26:12 -070088 SkASSERT(arrayCount > 0);
89 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050090 if (kUnusedUniform != uni.fLocation) {
91 GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
fmenozzi35a98c72016-07-20 08:26:12 -070092 }
93}
94
egdaniel018fb622015-10-28 07:26:40 -070095void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
joshualitte7afc2d2015-09-11 10:44:13 -070096 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -040097 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050098 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -050099 if (kUnusedUniform != uni.fLocation) {
100 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000101 }
102}
103
kkinnunen7510b222014-07-30 00:04:16 -0700104void GrGLProgramDataManager::set1fv(UniformHandle u,
105 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700106 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700107 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400108 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000109 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000110 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000111 // This assert fires in some instances of the two-pt gradient for its VSParams.
112 // Once the uniform manager is responsible for inserting the duplicate uniform
113 // arrays in VS and FS driver bug workaround, this can be enabled.
joshualittaf2d56d2015-05-11 06:21:34 -0700114 // this->printUni(uni);
Brian Salomon101b8442016-11-18 11:58:54 -0500115 if (kUnusedUniform != uni.fLocation) {
116 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000117 }
118}
119
egdaniel018fb622015-10-28 07:26:40 -0700120void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700121 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400122 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500123 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500124 if (kUnusedUniform != uni.fLocation) {
125 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000126 }
127}
128
kkinnunen7510b222014-07-30 00:04:16 -0700129void GrGLProgramDataManager::set2fv(UniformHandle u,
130 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700131 const float v[]) 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 == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000134 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000135 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500136 if (kUnusedUniform != uni.fLocation) {
137 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000138 }
139}
140
egdaniel018fb622015-10-28 07:26:40 -0700141void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700142 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400143 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500144 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500145 if (kUnusedUniform != uni.fLocation) {
146 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000147 }
148}
149
kkinnunen7510b222014-07-30 00:04:16 -0700150void GrGLProgramDataManager::set3fv(UniformHandle u,
151 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700152 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700153 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400154 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000155 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000156 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
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()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400168 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500169 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500170 if (kUnusedUniform != uni.fLocation) {
171 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000172 }
173}
174
kkinnunen7510b222014-07-30 00:04:16 -0700175void GrGLProgramDataManager::set4fv(UniformHandle u,
176 int arrayCount,
egdaniel018fb622015-10-28 07:26:40 -0700177 const float v[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700178 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400179 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000180 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000181 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500182 if (kUnusedUniform != uni.fLocation) {
183 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000184 }
185}
186
cdalton8d988b32016-03-07 15:39:09 -0800187void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
188 this->setMatrices<2>(u, 1, matrix);
189}
190
egdaniel018fb622015-10-28 07:26:40 -0700191void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800192 this->setMatrices<3>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000193}
194
egdaniel018fb622015-10-28 07:26:40 -0700195void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800196 this->setMatrices<4>(u, 1, matrix);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000197}
198
cdalton8d988b32016-03-07 15:39:09 -0800199void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
200 this->setMatrices<2>(u, arrayCount, m);
201}
202
203void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
204 this->setMatrices<3>(u, arrayCount, m);
205}
206
207void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
208 this->setMatrices<4>(u, arrayCount, m);
209}
210
211template<int N> struct set_uniform_matrix;
212
213template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
214 int arrayCount,
215 const float matrices[]) const {
joshualitte7afc2d2015-09-11 10:44:13 -0700216 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400217 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400218 uni.fType == kHalf2x2_GrSLType + (N - 2));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000219 SkASSERT(arrayCount > 0);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000220 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
Brian Salomon101b8442016-11-18 11:58:54 -0500221 if (kUnusedUniform != uni.fLocation) {
222 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000223 }
224}
225
cdalton8d988b32016-03-07 15:39:09 -0800226template<> struct set_uniform_matrix<2> {
227 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
228 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000229 }
cdalton8d988b32016-03-07 15:39:09 -0800230};
231
232template<> struct set_uniform_matrix<3> {
233 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
234 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000235 }
cdalton8d988b32016-03-07 15:39:09 -0800236};
237
238template<> struct set_uniform_matrix<4> {
239 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
240 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
241 }
242};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000243
egdaniel0eafe792015-11-20 14:01:22 -0800244void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
joshualittd8dd47b2015-09-11 11:45:01 -0700245 int components,
246 const SkMatrix& matrix) const {
247 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
egdaniel0eafe792015-11-20 14:01:22 -0800248 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
joshualittd8dd47b2015-09-11 11:45:01 -0700249
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400250 SkASSERT((components == 2 && (fragmentInput.fType == kFloat2_GrSLType ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400251 fragmentInput.fType == kHalf2_GrSLType)) ||
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400252 (components == 3 && (fragmentInput.fType == kFloat3_GrSLType ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400253 fragmentInput.fType == kHalf3_GrSLType)));
joshualittd8dd47b2015-09-11 11:45:01 -0700254
255 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
256 fragmentInput.fLocation,
257 GR_GL_OBJECT_LINEAR,
258 components,
259 matrix);
260}