blob: 65d14fde116dc65f29f20ab426048583aa326ba1 [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
15typedef GrGLProgramEffects::EffectKey EffectKey;
16typedef GrGLProgramEffects::TransformedCoords TransformedCoords;
17typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
18typedef GrGLProgramEffects::TextureSampler TextureSampler;
19typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
20
21/**
22 * We specialize the vertex code for each of these matrix types.
23 */
24enum MatrixType {
egdaniel4c6443e2014-06-24 13:43:12 -070025 kNoPersp_MatrixType = 0,
26 kGeneral_MatrixType = 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000027};
28
29/**
30 * The key for an individual coord transform is made up of a matrix type and a bit that
31 * indicates the source of the input coords.
32 */
33enum {
egdaniel4c6443e2014-06-24 13:43:12 -070034 kMatrixTypeKeyBits = 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000035 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
36 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
37 kTransformKeyBits = kMatrixTypeKeyBits + 1,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000038};
39
40namespace {
41
42/**
43 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
44 * present in the texture's config. swizzleComponentMask indicates the channels present in the
45 * shader swizzle.
46 */
47inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
48 uint32_t configComponentMask,
49 uint32_t swizzleComponentMask) {
50 if (caps.textureSwizzleSupport()) {
51 // Any remapping is handled using texture swizzling not shader modifications.
52 return false;
53 }
54 // check if the texture is alpha-only
55 if (kA_GrColorComponentFlag == configComponentMask) {
56 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
57 // we must map the swizzle 'a's to 'r'.
58 return true;
59 }
60 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
61 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
62 // alpha-only textures smear alpha across all four channels when read.
63 return true;
64 }
65 }
66 return false;
67}
68
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000069/**
70 * Retrieves the matrix type from transformKey for the transform at transformIdx.
71 */
72MatrixType get_matrix_type(EffectKey transformKey, int transformIdx) {
73 return static_cast<MatrixType>(
74 (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTypeKeyMask);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000075}
76
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000077/**
78 * Retrieves the source coords from transformKey for the transform at transformIdx. It may not be
79 * the same coordinate set as the original GrCoordTransform if the position and local coords are
80 * identical for this program.
81 */
82GrCoordSet get_source_coords(EffectKey transformKey, int transformIdx) {
83 return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoords_Flag ?
84 kPosition_GrCoordSet :
85 kLocal_GrCoordSet;
86}
87
88/**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000089 * Retrieves the final matrix that a transform needs to apply to its source coords.
90 */
91SkMatrix get_transform_matrix(const GrDrawEffect& drawEffect, int transformIdx) {
bsalomonf99f8842014-07-07 11:54:23 -070092 const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(transformIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000093 SkMatrix combined;
94 if (kLocal_GrCoordSet == coordTransform.sourceCoords() &&
95 !drawEffect.programHasExplicitLocalCoords()) {
96 combined.setConcat(coordTransform.getMatrix(), drawEffect.getCoordChangeMatrix());
97 } else {
98 combined = coordTransform.getMatrix();
99 }
100 if (coordTransform.reverseY()) {
101 // combined.postScale(1,-1);
102 // combined.postTranslate(0,1);
103 combined.set(SkMatrix::kMSkewY,
104 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
105 combined.set(SkMatrix::kMScaleY,
106 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
107 combined.set(SkMatrix::kMTransY,
108 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
109 }
110 return combined;
111}
112
113}
114
115////////////////////////////////////////////////////////////////////////////////
116
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000117EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) {
118 EffectKey key = 0;
119 int numAttributes = drawEffect.getVertexAttribIndexCount();
120 SkASSERT(numAttributes <= 2);
121 const int* attributeIndices = drawEffect.getVertexAttribIndices();
122 for (int a = 0; a < numAttributes; ++a) {
123 EffectKey value = attributeIndices[a] << 3 * a;
124 SkASSERT(0 == (value & key)); // keys for each attribute ought not to overlap
125 key |= value;
126 }
127 return key;
128}
129
130EffectKey GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) {
131 EffectKey totalKey = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700132 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000133 for (int t = 0; t < numTransforms; ++t) {
134 EffectKey key = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700135 const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(t);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000136 SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType();
137 SkMatrix::TypeMask type1;
138 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
139 type1 = drawEffect.getCoordChangeMatrix().getType();
140 } else {
141 if (drawEffect.programHasExplicitLocalCoords()) {
142 // We only make the key indicate that device coords are referenced when the local coords
143 // are not actually determined by positions. Otherwise the local coords var and position
144 // var are identical.
145 key |= kPositionCoords_Flag;
146 }
147 type1 = SkMatrix::kIdentity_Mask;
148 }
149
150 int combinedTypes = type0 | type1;
151
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000152 if (SkMatrix::kPerspective_Mask & combinedTypes) {
153 key |= kGeneral_MatrixType;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000154 } else {
egdaniel4c6443e2014-06-24 13:43:12 -0700155 key |= kNoPersp_MatrixType;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000156 }
157 key <<= kTransformKeyBits * t;
158 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
159 totalKey |= key;
160 }
161 return totalKey;
162}
163
164EffectKey GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps) {
165 EffectKey key = 0;
bsalomonf99f8842014-07-07 11:54:23 -0700166 int numTextures = drawEffect.effect()->numTextures();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000167 for (int t = 0; t < numTextures; ++t) {
bsalomonf99f8842014-07-07 11:54:23 -0700168 const GrTextureAccess& access = drawEffect.effect()->textureAccess(t);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000169 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
170 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
171 key |= 1 << t;
172 }
173 }
174 return key;
175}
176
177GrGLProgramEffects::~GrGLProgramEffects() {
178 int numEffects = fGLEffects.count();
179 for (int e = 0; e < numEffects; ++e) {
180 SkDELETE(fGLEffects[e]);
181 }
182}
183
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000184void GrGLProgramEffects::emitSamplers(GrGLShaderBuilder* builder,
bsalomonf99f8842014-07-07 11:54:23 -0700185 const GrEffect* effect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000186 TextureSamplerArray* outSamplers) {
187 SkTArray<Sampler, true>& samplers = fSamplers.push_back();
188 int numTextures = effect->numTextures();
189 samplers.push_back_n(numTextures);
190 SkString name;
191 for (int t = 0; t < numTextures; ++t) {
192 name.printf("Sampler%d", t);
193 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
194 kSampler2D_GrSLType,
195 name.c_str());
196 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
197 (samplers[t].fUniform, effect->textureAccess(t)));
198 }
199}
200
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000201void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) {
202 int numEffects = fGLEffects.count();
203 SkASSERT(numEffects == fSamplers.count());
204 for (int e = 0; e < numEffects; ++e) {
205 SkTArray<Sampler, true>& samplers = fSamplers[e];
206 int numSamplers = samplers.count();
207 for (int s = 0; s < numSamplers; ++s) {
208 SkASSERT(samplers[s].fUniform.isValid());
209 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx);
210 samplers[s].fTextureUnit = (*texUnitIdx)++;
211 }
212 }
213}
214
bsalomonf99f8842014-07-07 11:54:23 -0700215void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect* effect, int effectIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000216 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
217 int numSamplers = samplers.count();
218 SkASSERT(numSamplers == effect->numTextures());
219 for (int s = 0; s < numSamplers; ++s) {
220 SkASSERT(samplers[s].fTextureUnit >= 0);
221 const GrTextureAccess& textureAccess = effect->textureAccess(s);
222 gpu->bindTexture(samplers[s].fTextureUnit,
223 textureAccess.getParams(),
224 static_cast<GrGLTexture*>(textureAccess.getTexture()));
225 }
226}
227
228////////////////////////////////////////////////////////////////////////////////
229
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000230void GrGLVertexProgramEffects::emitEffect(GrGLFullShaderBuilder* builder,
231 const GrEffectStage& stage,
232 EffectKey key,
233 const char* outColor,
234 const char* inColor,
235 int stageIndex) {
236 GrDrawEffect drawEffect(stage, fHasExplicitLocalCoords);
bsalomonf99f8842014-07-07 11:54:23 -0700237 const GrEffect* effect = stage.getEffect();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000238 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
239 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
240
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000241 this->emitAttributes(builder, stage);
bsalomon848faf02014-07-11 10:01:02 -0700242 this->emitTransforms(builder, drawEffect, &coords);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000243 this->emitSamplers(builder, effect, &samplers);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000244
245 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000246 fGLEffects.push_back(glEffect);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000247
248 // Enclose custom code in a block to avoid namespace conflicts
249 SkString openBrace;
250 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000251 builder->vsCodeAppend(openBrace.c_str());
252 builder->fsCodeAppend(openBrace.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000253
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000254 if (glEffect->isVertexEffect()) {
255 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000256 vertexEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000257 } else {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000258 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000259 }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000260
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000261 builder->vsCodeAppend("\t}\n");
262 builder->fsCodeAppend("\t}\n");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000263}
264
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000265void GrGLVertexProgramEffects::emitAttributes(GrGLFullShaderBuilder* builder,
266 const GrEffectStage& stage) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000267 int numAttributes = stage.getVertexAttribIndexCount();
268 const int* attributeIndices = stage.getVertexAttribIndices();
269 for (int a = 0; a < numAttributes; ++a) {
270 // TODO: Make addAttribute mangle the name.
271 SkString attributeName("aAttr");
272 attributeName.appendS32(attributeIndices[a]);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000273 builder->addEffectAttribute(attributeIndices[a],
bsalomonf99f8842014-07-07 11:54:23 -0700274 stage.getEffect()->vertexAttribType(a),
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000275 attributeName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000276 }
277}
278
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000279void GrGLVertexProgramEffects::emitTransforms(GrGLFullShaderBuilder* builder,
bsalomon848faf02014-07-11 10:01:02 -0700280 const GrDrawEffect& drawEffect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000281 TransformedCoordsArray* outCoords) {
282 SkTArray<Transform, true>& transforms = fTransforms.push_back();
bsalomon848faf02014-07-11 10:01:02 -0700283 EffectKey totalKey = GenTransformKey(drawEffect);
284 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000285 transforms.push_back_n(numTransforms);
286 for (int t = 0; t < numTransforms; t++) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000287 GrSLType varyingType = kVoid_GrSLType;
288 const char* uniName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000289 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000290 case kNoPersp_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000291 uniName = "StageMatrix";
292 varyingType = kVec2f_GrSLType;
293 break;
294 case kGeneral_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000295 uniName = "StageMatrix";
296 varyingType = kVec3f_GrSLType;
297 break;
298 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000299 SkFAIL("Unexpected key.");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000300 }
301 SkString suffixedUniName;
egdaniel4c6443e2014-06-24 13:43:12 -0700302 if (0 != t) {
303 suffixedUniName.append(uniName);
304 suffixedUniName.appendf("_%i", t);
305 uniName = suffixedUniName.c_str();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000306 }
egdaniel4c6443e2014-06-24 13:43:12 -0700307 transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility,
308 kMat33f_GrSLType,
309 uniName,
310 &uniName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000311
312 const char* varyingName = "MatrixCoord";
313 SkString suffixedVaryingName;
314 if (0 != t) {
315 suffixedVaryingName.append(varyingName);
316 suffixedVaryingName.appendf("_%i", t);
317 varyingName = suffixedVaryingName.c_str();
318 }
319 const char* vsVaryingName;
320 const char* fsVaryingName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000321 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000322
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000323 const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords(totalKey, t) ?
324 builder->positionAttribute() :
325 builder->localCoordsAttribute();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000326 // varying = matrix * coords (logically)
egdaniel4c6443e2014-06-24 13:43:12 -0700327 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
328 if (kVec2f_GrSLType == varyingType) {
329 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
330 vsVaryingName, uniName, coords.c_str());
331 } else {
332 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
333 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000334 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000335 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
336 (SkString(fsVaryingName), varyingType));
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000337 }
338}
339
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000340void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
341 const GrGLUniformManager& uniformManager,
342 const GrEffectStage* effectStages[]) {
343 int numEffects = fGLEffects.count();
344 SkASSERT(numEffects == fTransforms.count());
345 SkASSERT(numEffects == fSamplers.count());
346 for (int e = 0; e < numEffects; ++e) {
347 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
348 fGLEffects[e]->setData(uniformManager, drawEffect);
349 this->setTransformData(uniformManager, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700350 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000351 }
352}
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000353
354void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& uniformManager,
355 const GrDrawEffect& drawEffect,
356 int effectIdx) {
357 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
358 int numTransforms = transforms.count();
bsalomonf99f8842014-07-07 11:54:23 -0700359 SkASSERT(numTransforms == drawEffect.effect()->numTransforms());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000360 for (int t = 0; t < numTransforms; ++t) {
egdaniel4c6443e2014-06-24 13:43:12 -0700361 SkASSERT(transforms[t].fHandle.isValid());
362 const SkMatrix& matrix = get_transform_matrix(drawEffect, t);
363 if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) {
364 uniformManager.setSkMatrix(transforms[t].fHandle, matrix);
365 transforms[t].fCurrentValue = matrix;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000366 }
367 }
368}
369
370GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder,
371 int reserveCount)
372 : fBuilder(builder)
373 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
374 (reserveCount, fBuilder->hasExplicitLocalCoords()))) {
375}
376
377void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
378 GrGLProgramEffects::EffectKey key,
379 const char* outColor,
380 const char* inColor,
381 int stageIndex) {
382 SkASSERT(NULL != fProgramEffects.get());
383 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
384}
385
386////////////////////////////////////////////////////////////////////////////////
387
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000388void GrGLPathTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000389 const GrEffectStage& stage,
390 EffectKey key,
391 const char* outColor,
392 const char* inColor,
393 int stageIndex) {
394 GrDrawEffect drawEffect(stage, false);
bsalomonf99f8842014-07-07 11:54:23 -0700395 const GrEffect* effect = stage.getEffect();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000396 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
397 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
398
399 SkASSERT(0 == stage.getVertexAttribIndexCount());
bsalomon848faf02014-07-11 10:01:02 -0700400 this->setupPathTexGen(builder, drawEffect, &coords);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000401 this->emitSamplers(builder, effect, &samplers);
402
403 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
404 fGLEffects.push_back(glEffect);
405
406 // Enclose custom code in a block to avoid namespace conflicts
407 SkString openBrace;
408 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
409 builder->fsCodeAppend(openBrace.c_str());
410
411 SkASSERT(!glEffect->isVertexEffect());
412 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
413
414 builder->fsCodeAppend("\t}\n");
415}
416
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000417void GrGLPathTexGenProgramEffects::setupPathTexGen(GrGLFragmentOnlyShaderBuilder* builder,
bsalomon848faf02014-07-11 10:01:02 -0700418 const GrDrawEffect& drawEffect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000419 TransformedCoordsArray* outCoords) {
bsalomon848faf02014-07-11 10:01:02 -0700420 int numTransforms = drawEffect.effect()->numTransforms();
421 EffectKey totalKey = GenTransformKey(drawEffect);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000422 int texCoordIndex = builder->addTexCoordSets(numTransforms);
423 SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex));
424 SkString name;
425 for (int t = 0; t < numTransforms; ++t) {
426 GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ?
427 kVec3f_GrSLType :
428 kVec2f_GrSLType;
429 name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex++);
430 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type));
431 }
432}
433
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000434void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000435 const GrGLUniformManager& uniformManager,
436 const GrEffectStage* effectStages[]) {
437 int numEffects = fGLEffects.count();
438 SkASSERT(numEffects == fTransforms.count());
439 SkASSERT(numEffects == fSamplers.count());
440 for (int e = 0; e < numEffects; ++e) {
441 GrDrawEffect drawEffect(*effectStages[e], false);
442 fGLEffects[e]->setData(uniformManager, drawEffect);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000443 this->setPathTexGenState(gpu, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700444 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000445 }
446}
447
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000448void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000449 const GrDrawEffect& drawEffect,
450 int effectIdx) {
451 EffectKey totalKey = fTransforms[effectIdx].fTransformKey;
452 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
bsalomonf99f8842014-07-07 11:54:23 -0700453 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000454 for (int t = 0; t < numTransforms; ++t) {
455 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000456 case kNoPersp_MatrixType: {
457 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000458 gpu->enablePathTexGen(texCoordIndex++,
459 GrGpuGL::kST_PathTexGenComponents,
460 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000461 break;
462 }
463 case kGeneral_MatrixType: {
464 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000465 gpu->enablePathTexGen(texCoordIndex++,
466 GrGpuGL::kSTR_PathTexGenComponents,
467 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000468 break;
469 }
470 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000471 SkFAIL("Unexpected matrixs type.");
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000472 }
473 }
474}
475
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000476GrGLPathTexGenProgramEffectsBuilder::GrGLPathTexGenProgramEffectsBuilder(
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000477 GrGLFragmentOnlyShaderBuilder* builder,
478 int reserveCount)
479 : fBuilder(builder)
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000480 , fProgramEffects(SkNEW_ARGS(GrGLPathTexGenProgramEffects, (reserveCount))) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000481}
482
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000483void GrGLPathTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
484 GrGLProgramEffects::EffectKey key,
485 const char* outColor,
486 const char* inColor,
487 int stageIndex) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000488 SkASSERT(NULL != fProgramEffects.get());
489 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
490}