blob: cff31e29c26e6ebbbea1c3c49425378fe38ed383 [file] [log] [blame]
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +00001/*
2 * Copyright 2013 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 "GrGLProgramEffects.h"
9#include "GrDrawEffect.h"
10#include "gl/GrGLEffect.h"
11#include "gl/GrGLShaderBuilder.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000012#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000013#include "gl/GrGpuGL.h"
14
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000015typedef GrGLProgramEffects::TransformedCoords TransformedCoords;
16typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
17typedef GrGLProgramEffects::TextureSampler TextureSampler;
18typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
19
20/**
21 * We specialize the vertex code for each of these matrix types.
22 */
23enum MatrixType {
egdaniel4c6443e2014-06-24 13:43:12 -070024 kNoPersp_MatrixType = 0,
25 kGeneral_MatrixType = 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000026};
27
28/**
29 * The key for an individual coord transform is made up of a matrix type and a bit that
30 * indicates the source of the input coords.
31 */
32enum {
egdaniel4c6443e2014-06-24 13:43:12 -070033 kMatrixTypeKeyBits = 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000034 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
35 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
36 kTransformKeyBits = kMatrixTypeKeyBits + 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000037};
38
39namespace {
40
41/**
42 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
43 * present in the texture's config. swizzleComponentMask indicates the channels present in the
44 * shader swizzle.
45 */
46inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
47 uint32_t configComponentMask,
48 uint32_t swizzleComponentMask) {
49 if (caps.textureSwizzleSupport()) {
50 // Any remapping is handled using texture swizzling not shader modifications.
51 return false;
52 }
53 // check if the texture is alpha-only
54 if (kA_GrColorComponentFlag == configComponentMask) {
55 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
56 // we must map the swizzle 'a's to 'r'.
57 return true;
58 }
59 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
60 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
61 // alpha-only textures smear alpha across all four channels when read.
62 return true;
63 }
64 }
65 return false;
66}
67
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000068/**
69 * Retrieves the matrix type from transformKey for the transform at transformIdx.
70 */
bsalomon63e99f72014-07-21 08:03:14 -070071MatrixType get_matrix_type(uint32_t transformKey, int transformIdx) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000072 return static_cast<MatrixType>(
73 (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTypeKeyMask);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000074}
75
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000076/**
77 * Retrieves the source coords from transformKey for the transform at transformIdx. It may not be
78 * the same coordinate set as the original GrCoordTransform if the position and local coords are
79 * identical for this program.
80 */
bsalomon63e99f72014-07-21 08:03:14 -070081GrCoordSet get_source_coords(uint32_t transformKey, int transformIdx) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000082 return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoords_Flag ?
83 kPosition_GrCoordSet :
84 kLocal_GrCoordSet;
85}
86
87/**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000088 * Retrieves the final matrix that a transform needs to apply to its source coords.
89 */
90SkMatrix get_transform_matrix(const GrDrawEffect& drawEffect, int transformIdx) {
bsalomonf99f8842014-07-07 11:54:23 -070091 const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(transformIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000092 SkMatrix combined;
93 if (kLocal_GrCoordSet == coordTransform.sourceCoords() &&
94 !drawEffect.programHasExplicitLocalCoords()) {
95 combined.setConcat(coordTransform.getMatrix(), drawEffect.getCoordChangeMatrix());
96 } else {
97 combined = coordTransform.getMatrix();
98 }
99 if (coordTransform.reverseY()) {
100 // combined.postScale(1,-1);
101 // combined.postTranslate(0,1);
102 combined.set(SkMatrix::kMSkewY,
103 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
104 combined.set(SkMatrix::kMScaleY,
105 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
106 combined.set(SkMatrix::kMTransY,
107 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
108 }
109 return combined;
110}
111
112}
113
114////////////////////////////////////////////////////////////////////////////////
115
bsalomon929f29a2014-07-17 07:55:11 -0700116bool GrGLProgramEffects::GenEffectMetaKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps,
117 GrEffectKeyBuilder* b) {
118
bsalomon63e99f72014-07-21 08:03:14 -0700119 uint32_t textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, caps);
120 uint32_t transformKey = GrGLProgramEffects::GenTransformKey(drawEffect);
121 uint32_t attribKey = GrGLProgramEffects::GenAttribKey(drawEffect);
bsalomon929f29a2014-07-17 07:55:11 -0700122 uint32_t classID = drawEffect.effect()->getFactory().effectClassID();
123
124 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
125 // don't fit.
126 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
127 if ((textureKey | transformKey | attribKey | classID) & kMetaKeyInvalidMask) {
128 return false;
129 }
130
131 uint32_t* key = b->add32n(2);
132 key[0] = (textureKey << 16 | transformKey);
133 key[1] = (classID << 16 | attribKey);
134 return true;
135}
136
bsalomon63e99f72014-07-21 08:03:14 -0700137uint32_t GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) {
138 uint32_t key = 0;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000139 int numAttributes = drawEffect.getVertexAttribIndexCount();
140 SkASSERT(numAttributes <= 2);
141 const int* attributeIndices = drawEffect.getVertexAttribIndices();
142 for (int a = 0; a < numAttributes; ++a) {
bsalomon63e99f72014-07-21 08:03:14 -0700143 uint32_t value = attributeIndices[a] << 3 * a;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000144 SkASSERT(0 == (value & key)); // keys for each attribute ought not to overlap
145 key |= value;
146 }
147 return key;
148}
149
bsalomon63e99f72014-07-21 08:03:14 -0700150uint32_t GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) {
151 uint32_t totalKey = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700152 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000153 for (int t = 0; t < numTransforms; ++t) {
bsalomon63e99f72014-07-21 08:03:14 -0700154 uint32_t key = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700155 const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(t);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000156 SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType();
157 SkMatrix::TypeMask type1;
158 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
159 type1 = drawEffect.getCoordChangeMatrix().getType();
160 } else {
161 if (drawEffect.programHasExplicitLocalCoords()) {
162 // We only make the key indicate that device coords are referenced when the local coords
163 // are not actually determined by positions. Otherwise the local coords var and position
164 // var are identical.
165 key |= kPositionCoords_Flag;
166 }
167 type1 = SkMatrix::kIdentity_Mask;
168 }
169
170 int combinedTypes = type0 | type1;
171
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000172 if (SkMatrix::kPerspective_Mask & combinedTypes) {
173 key |= kGeneral_MatrixType;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000174 } else {
egdaniel4c6443e2014-06-24 13:43:12 -0700175 key |= kNoPersp_MatrixType;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000176 }
177 key <<= kTransformKeyBits * t;
178 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
179 totalKey |= key;
180 }
181 return totalKey;
182}
183
bsalomon63e99f72014-07-21 08:03:14 -0700184uint32_t GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps) {
185 uint32_t key = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700186 int numTextures = drawEffect.effect()->numTextures();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000187 for (int t = 0; t < numTextures; ++t) {
bsalomonf99f8842014-07-07 11:54:23 -0700188 const GrTextureAccess& access = drawEffect.effect()->textureAccess(t);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000189 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
190 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
191 key |= 1 << t;
192 }
193 }
194 return key;
195}
196
197GrGLProgramEffects::~GrGLProgramEffects() {
198 int numEffects = fGLEffects.count();
199 for (int e = 0; e < numEffects; ++e) {
200 SkDELETE(fGLEffects[e]);
201 }
202}
203
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000204void GrGLProgramEffects::emitSamplers(GrGLShaderBuilder* builder,
bsalomonf99f8842014-07-07 11:54:23 -0700205 const GrEffect* effect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000206 TextureSamplerArray* outSamplers) {
207 SkTArray<Sampler, true>& samplers = fSamplers.push_back();
208 int numTextures = effect->numTextures();
209 samplers.push_back_n(numTextures);
210 SkString name;
211 for (int t = 0; t < numTextures; ++t) {
212 name.printf("Sampler%d", t);
213 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
214 kSampler2D_GrSLType,
215 name.c_str());
216 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
217 (samplers[t].fUniform, effect->textureAccess(t)));
218 }
219}
220
kkinnunen7510b222014-07-30 00:04:16 -0700221void GrGLProgramEffects::initSamplers(const GrGLProgramDataManager& programResourceManager, int* texUnitIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000222 int numEffects = fGLEffects.count();
223 SkASSERT(numEffects == fSamplers.count());
224 for (int e = 0; e < numEffects; ++e) {
225 SkTArray<Sampler, true>& samplers = fSamplers[e];
226 int numSamplers = samplers.count();
227 for (int s = 0; s < numSamplers; ++s) {
228 SkASSERT(samplers[s].fUniform.isValid());
kkinnunen7510b222014-07-30 00:04:16 -0700229 programResourceManager.setSampler(samplers[s].fUniform, *texUnitIdx);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000230 samplers[s].fTextureUnit = (*texUnitIdx)++;
231 }
232 }
233}
234
bsalomonf99f8842014-07-07 11:54:23 -0700235void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect* effect, int effectIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000236 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
237 int numSamplers = samplers.count();
238 SkASSERT(numSamplers == effect->numTextures());
239 for (int s = 0; s < numSamplers; ++s) {
240 SkASSERT(samplers[s].fTextureUnit >= 0);
241 const GrTextureAccess& textureAccess = effect->textureAccess(s);
242 gpu->bindTexture(samplers[s].fTextureUnit,
243 textureAccess.getParams(),
244 static_cast<GrGLTexture*>(textureAccess.getTexture()));
245 }
246}
247
248////////////////////////////////////////////////////////////////////////////////
249
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000250void GrGLVertexProgramEffects::emitEffect(GrGLFullShaderBuilder* builder,
251 const GrEffectStage& stage,
bsalomon63e99f72014-07-21 08:03:14 -0700252 const GrEffectKey& key,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000253 const char* outColor,
254 const char* inColor,
255 int stageIndex) {
256 GrDrawEffect drawEffect(stage, fHasExplicitLocalCoords);
bsalomonf99f8842014-07-07 11:54:23 -0700257 const GrEffect* effect = stage.getEffect();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000258 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
259 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
260
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000261 this->emitAttributes(builder, stage);
bsalomon848faf02014-07-11 10:01:02 -0700262 this->emitTransforms(builder, drawEffect, &coords);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000263 this->emitSamplers(builder, effect, &samplers);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000264
265 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000266 fGLEffects.push_back(glEffect);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000267
268 // Enclose custom code in a block to avoid namespace conflicts
269 SkString openBrace;
270 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000271 builder->vsCodeAppend(openBrace.c_str());
272 builder->fsCodeAppend(openBrace.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000273
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000274 if (glEffect->isVertexEffect()) {
275 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000276 vertexEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000277 } else {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000278 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000279 }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000280
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000281 builder->vsCodeAppend("\t}\n");
282 builder->fsCodeAppend("\t}\n");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000283}
284
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000285void GrGLVertexProgramEffects::emitAttributes(GrGLFullShaderBuilder* builder,
286 const GrEffectStage& stage) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000287 int numAttributes = stage.getVertexAttribIndexCount();
288 const int* attributeIndices = stage.getVertexAttribIndices();
289 for (int a = 0; a < numAttributes; ++a) {
290 // TODO: Make addAttribute mangle the name.
291 SkString attributeName("aAttr");
292 attributeName.appendS32(attributeIndices[a]);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000293 builder->addEffectAttribute(attributeIndices[a],
bsalomonf99f8842014-07-07 11:54:23 -0700294 stage.getEffect()->vertexAttribType(a),
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000295 attributeName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000296 }
297}
298
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000299void GrGLVertexProgramEffects::emitTransforms(GrGLFullShaderBuilder* builder,
bsalomon848faf02014-07-11 10:01:02 -0700300 const GrDrawEffect& drawEffect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000301 TransformedCoordsArray* outCoords) {
302 SkTArray<Transform, true>& transforms = fTransforms.push_back();
bsalomon63e99f72014-07-21 08:03:14 -0700303 uint32_t totalKey = GenTransformKey(drawEffect);
bsalomon848faf02014-07-11 10:01:02 -0700304 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000305 transforms.push_back_n(numTransforms);
306 for (int t = 0; t < numTransforms; t++) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000307 GrSLType varyingType = kVoid_GrSLType;
308 const char* uniName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000309 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000310 case kNoPersp_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000311 uniName = "StageMatrix";
312 varyingType = kVec2f_GrSLType;
313 break;
314 case kGeneral_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000315 uniName = "StageMatrix";
316 varyingType = kVec3f_GrSLType;
317 break;
318 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000319 SkFAIL("Unexpected key.");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000320 }
321 SkString suffixedUniName;
egdaniel4c6443e2014-06-24 13:43:12 -0700322 if (0 != t) {
323 suffixedUniName.append(uniName);
324 suffixedUniName.appendf("_%i", t);
325 uniName = suffixedUniName.c_str();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000326 }
egdaniel4c6443e2014-06-24 13:43:12 -0700327 transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility,
328 kMat33f_GrSLType,
329 uniName,
330 &uniName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000331
332 const char* varyingName = "MatrixCoord";
333 SkString suffixedVaryingName;
334 if (0 != t) {
335 suffixedVaryingName.append(varyingName);
336 suffixedVaryingName.appendf("_%i", t);
337 varyingName = suffixedVaryingName.c_str();
338 }
339 const char* vsVaryingName;
340 const char* fsVaryingName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000341 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000342
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000343 const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords(totalKey, t) ?
344 builder->positionAttribute() :
345 builder->localCoordsAttribute();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000346 // varying = matrix * coords (logically)
egdaniel4c6443e2014-06-24 13:43:12 -0700347 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
348 if (kVec2f_GrSLType == varyingType) {
349 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
350 vsVaryingName, uniName, coords.c_str());
351 } else {
352 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
353 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000354 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000355 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
356 (SkString(fsVaryingName), varyingType));
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000357 }
358}
359
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000360void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
kkinnunen7510b222014-07-30 00:04:16 -0700361 const GrGLProgramDataManager& programResourceManager,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000362 const GrEffectStage* effectStages[]) {
363 int numEffects = fGLEffects.count();
364 SkASSERT(numEffects == fTransforms.count());
365 SkASSERT(numEffects == fSamplers.count());
366 for (int e = 0; e < numEffects; ++e) {
367 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
kkinnunen7510b222014-07-30 00:04:16 -0700368 fGLEffects[e]->setData(programResourceManager, drawEffect);
369 this->setTransformData(programResourceManager, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700370 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000371 }
372}
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000373
kkinnunen7510b222014-07-30 00:04:16 -0700374void GrGLVertexProgramEffects::setTransformData(const GrGLProgramDataManager& programResourceManager,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000375 const GrDrawEffect& drawEffect,
376 int effectIdx) {
377 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
378 int numTransforms = transforms.count();
bsalomonf99f8842014-07-07 11:54:23 -0700379 SkASSERT(numTransforms == drawEffect.effect()->numTransforms());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000380 for (int t = 0; t < numTransforms; ++t) {
egdaniel4c6443e2014-06-24 13:43:12 -0700381 SkASSERT(transforms[t].fHandle.isValid());
382 const SkMatrix& matrix = get_transform_matrix(drawEffect, t);
383 if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) {
kkinnunen7510b222014-07-30 00:04:16 -0700384 programResourceManager.setSkMatrix(transforms[t].fHandle, matrix);
egdaniel4c6443e2014-06-24 13:43:12 -0700385 transforms[t].fCurrentValue = matrix;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000386 }
387 }
388}
389
390GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder,
391 int reserveCount)
392 : fBuilder(builder)
393 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
394 (reserveCount, fBuilder->hasExplicitLocalCoords()))) {
395}
396
397void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
bsalomon63e99f72014-07-21 08:03:14 -0700398 const GrEffectKey& key,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000399 const char* outColor,
400 const char* inColor,
401 int stageIndex) {
402 SkASSERT(NULL != fProgramEffects.get());
403 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
404}
405
406////////////////////////////////////////////////////////////////////////////////
407
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000408void GrGLPathTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000409 const GrEffectStage& stage,
bsalomon63e99f72014-07-21 08:03:14 -0700410 const GrEffectKey& key,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000411 const char* outColor,
412 const char* inColor,
413 int stageIndex) {
414 GrDrawEffect drawEffect(stage, false);
bsalomonf99f8842014-07-07 11:54:23 -0700415 const GrEffect* effect = stage.getEffect();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000416 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
417 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
418
419 SkASSERT(0 == stage.getVertexAttribIndexCount());
bsalomon848faf02014-07-11 10:01:02 -0700420 this->setupPathTexGen(builder, drawEffect, &coords);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000421 this->emitSamplers(builder, effect, &samplers);
422
423 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
424 fGLEffects.push_back(glEffect);
425
426 // Enclose custom code in a block to avoid namespace conflicts
427 SkString openBrace;
428 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
429 builder->fsCodeAppend(openBrace.c_str());
430
431 SkASSERT(!glEffect->isVertexEffect());
432 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
433
434 builder->fsCodeAppend("\t}\n");
435}
436
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000437void GrGLPathTexGenProgramEffects::setupPathTexGen(GrGLFragmentOnlyShaderBuilder* builder,
bsalomon848faf02014-07-11 10:01:02 -0700438 const GrDrawEffect& drawEffect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000439 TransformedCoordsArray* outCoords) {
bsalomon848faf02014-07-11 10:01:02 -0700440 int numTransforms = drawEffect.effect()->numTransforms();
bsalomon63e99f72014-07-21 08:03:14 -0700441 uint32_t totalKey = GenTransformKey(drawEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000442 int texCoordIndex = builder->addTexCoordSets(numTransforms);
443 SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex));
444 SkString name;
445 for (int t = 0; t < numTransforms; ++t) {
446 GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ?
447 kVec3f_GrSLType :
448 kVec2f_GrSLType;
449 name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex++);
450 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type));
451 }
452}
453
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000454void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu,
kkinnunen7510b222014-07-30 00:04:16 -0700455 const GrGLProgramDataManager& programResourceManager,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000456 const GrEffectStage* effectStages[]) {
457 int numEffects = fGLEffects.count();
458 SkASSERT(numEffects == fTransforms.count());
459 SkASSERT(numEffects == fSamplers.count());
460 for (int e = 0; e < numEffects; ++e) {
461 GrDrawEffect drawEffect(*effectStages[e], false);
kkinnunen7510b222014-07-30 00:04:16 -0700462 fGLEffects[e]->setData(programResourceManager, drawEffect);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000463 this->setPathTexGenState(gpu, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700464 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000465 }
466}
467
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000468void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000469 const GrDrawEffect& drawEffect,
470 int effectIdx) {
bsalomon63e99f72014-07-21 08:03:14 -0700471 uint32_t totalKey = fTransforms[effectIdx].fTransformKey;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000472 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
bsalomonf99f8842014-07-07 11:54:23 -0700473 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000474 for (int t = 0; t < numTransforms; ++t) {
475 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000476 case kNoPersp_MatrixType: {
477 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000478 gpu->enablePathTexGen(texCoordIndex++,
479 GrGpuGL::kST_PathTexGenComponents,
480 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000481 break;
482 }
483 case kGeneral_MatrixType: {
484 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000485 gpu->enablePathTexGen(texCoordIndex++,
486 GrGpuGL::kSTR_PathTexGenComponents,
487 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000488 break;
489 }
490 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000491 SkFAIL("Unexpected matrixs type.");
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000492 }
493 }
494}
495
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000496GrGLPathTexGenProgramEffectsBuilder::GrGLPathTexGenProgramEffectsBuilder(
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000497 GrGLFragmentOnlyShaderBuilder* builder,
498 int reserveCount)
499 : fBuilder(builder)
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000500 , fProgramEffects(SkNEW_ARGS(GrGLPathTexGenProgramEffects, (reserveCount))) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000501}
502
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000503void GrGLPathTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
bsalomon63e99f72014-07-21 08:03:14 -0700504 const GrEffectKey& key,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000505 const char* outColor,
506 const char* inColor,
507 int stageIndex) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000508 SkASSERT(NULL != fProgramEffects.get());
509 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
510}