blob: fd91f584c42558678f4b037288f6eb4b10dde666 [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);
242 this->emitTransforms(builder, effect, key, &coords);
243 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,
bsalomonf99f8842014-07-07 11:54:23 -0700280 const GrEffect* effect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000281 EffectKey effectKey,
282 TransformedCoordsArray* outCoords) {
283 SkTArray<Transform, true>& transforms = fTransforms.push_back();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000284 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
285 int numTransforms = effect->numTransforms();
286 transforms.push_back_n(numTransforms);
287 for (int t = 0; t < numTransforms; t++) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000288 GrSLType varyingType = kVoid_GrSLType;
289 const char* uniName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000290 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000291 case kNoPersp_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000292 uniName = "StageMatrix";
293 varyingType = kVec2f_GrSLType;
294 break;
295 case kGeneral_MatrixType:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000296 uniName = "StageMatrix";
297 varyingType = kVec3f_GrSLType;
298 break;
299 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000300 SkFAIL("Unexpected key.");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000301 }
302 SkString suffixedUniName;
egdaniel4c6443e2014-06-24 13:43:12 -0700303 if (0 != t) {
304 suffixedUniName.append(uniName);
305 suffixedUniName.appendf("_%i", t);
306 uniName = suffixedUniName.c_str();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000307 }
egdaniel4c6443e2014-06-24 13:43:12 -0700308 transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility,
309 kMat33f_GrSLType,
310 uniName,
311 &uniName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000312
313 const char* varyingName = "MatrixCoord";
314 SkString suffixedVaryingName;
315 if (0 != t) {
316 suffixedVaryingName.append(varyingName);
317 suffixedVaryingName.appendf("_%i", t);
318 varyingName = suffixedVaryingName.c_str();
319 }
320 const char* vsVaryingName;
321 const char* fsVaryingName;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000322 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000323
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000324 const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords(totalKey, t) ?
325 builder->positionAttribute() :
326 builder->localCoordsAttribute();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000327 // varying = matrix * coords (logically)
egdaniel4c6443e2014-06-24 13:43:12 -0700328 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
329 if (kVec2f_GrSLType == varyingType) {
330 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
331 vsVaryingName, uniName, coords.c_str());
332 } else {
333 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
334 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000335 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000336 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
337 (SkString(fsVaryingName), varyingType));
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000338 }
339}
340
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000341void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
342 const GrGLUniformManager& uniformManager,
343 const GrEffectStage* effectStages[]) {
344 int numEffects = fGLEffects.count();
345 SkASSERT(numEffects == fTransforms.count());
346 SkASSERT(numEffects == fSamplers.count());
347 for (int e = 0; e < numEffects; ++e) {
348 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
349 fGLEffects[e]->setData(uniformManager, drawEffect);
350 this->setTransformData(uniformManager, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700351 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000352 }
353}
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000354
355void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& uniformManager,
356 const GrDrawEffect& drawEffect,
357 int effectIdx) {
358 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
359 int numTransforms = transforms.count();
bsalomonf99f8842014-07-07 11:54:23 -0700360 SkASSERT(numTransforms == drawEffect.effect()->numTransforms());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000361 for (int t = 0; t < numTransforms; ++t) {
egdaniel4c6443e2014-06-24 13:43:12 -0700362 SkASSERT(transforms[t].fHandle.isValid());
363 const SkMatrix& matrix = get_transform_matrix(drawEffect, t);
364 if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) {
365 uniformManager.setSkMatrix(transforms[t].fHandle, matrix);
366 transforms[t].fCurrentValue = matrix;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000367 }
368 }
369}
370
371GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder,
372 int reserveCount)
373 : fBuilder(builder)
374 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
375 (reserveCount, fBuilder->hasExplicitLocalCoords()))) {
376}
377
378void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
379 GrGLProgramEffects::EffectKey key,
380 const char* outColor,
381 const char* inColor,
382 int stageIndex) {
383 SkASSERT(NULL != fProgramEffects.get());
384 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
385}
386
387////////////////////////////////////////////////////////////////////////////////
388
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000389void GrGLPathTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000390 const GrEffectStage& stage,
391 EffectKey key,
392 const char* outColor,
393 const char* inColor,
394 int stageIndex) {
395 GrDrawEffect drawEffect(stage, false);
bsalomonf99f8842014-07-07 11:54:23 -0700396 const GrEffect* effect = stage.getEffect();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000397 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
398 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
399
400 SkASSERT(0 == stage.getVertexAttribIndexCount());
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000401 this->setupPathTexGen(builder, effect, key, &coords);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000402 this->emitSamplers(builder, effect, &samplers);
403
404 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
405 fGLEffects.push_back(glEffect);
406
407 // Enclose custom code in a block to avoid namespace conflicts
408 SkString openBrace;
409 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
410 builder->fsCodeAppend(openBrace.c_str());
411
412 SkASSERT(!glEffect->isVertexEffect());
413 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
414
415 builder->fsCodeAppend("\t}\n");
416}
417
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000418void GrGLPathTexGenProgramEffects::setupPathTexGen(GrGLFragmentOnlyShaderBuilder* builder,
bsalomonf99f8842014-07-07 11:54:23 -0700419 const GrEffect* effect,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000420 EffectKey effectKey,
421 TransformedCoordsArray* outCoords) {
422 int numTransforms = effect->numTransforms();
423 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
424 int texCoordIndex = builder->addTexCoordSets(numTransforms);
425 SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex));
426 SkString name;
427 for (int t = 0; t < numTransforms; ++t) {
428 GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ?
429 kVec3f_GrSLType :
430 kVec2f_GrSLType;
431 name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex++);
432 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type));
433 }
434}
435
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000436void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000437 const GrGLUniformManager& uniformManager,
438 const GrEffectStage* effectStages[]) {
439 int numEffects = fGLEffects.count();
440 SkASSERT(numEffects == fTransforms.count());
441 SkASSERT(numEffects == fSamplers.count());
442 for (int e = 0; e < numEffects; ++e) {
443 GrDrawEffect drawEffect(*effectStages[e], false);
444 fGLEffects[e]->setData(uniformManager, drawEffect);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000445 this->setPathTexGenState(gpu, drawEffect, e);
bsalomonf99f8842014-07-07 11:54:23 -0700446 this->bindTextures(gpu, drawEffect.effect(), e);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000447 }
448}
449
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000450void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000451 const GrDrawEffect& drawEffect,
452 int effectIdx) {
453 EffectKey totalKey = fTransforms[effectIdx].fTransformKey;
454 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
bsalomonf99f8842014-07-07 11:54:23 -0700455 int numTransforms = drawEffect.effect()->numTransforms();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000456 for (int t = 0; t < numTransforms; ++t) {
457 switch (get_matrix_type(totalKey, t)) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000458 case kNoPersp_MatrixType: {
459 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000460 gpu->enablePathTexGen(texCoordIndex++,
461 GrGpuGL::kST_PathTexGenComponents,
462 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000463 break;
464 }
465 case kGeneral_MatrixType: {
466 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000467 gpu->enablePathTexGen(texCoordIndex++,
468 GrGpuGL::kSTR_PathTexGenComponents,
469 transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000470 break;
471 }
472 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000473 SkFAIL("Unexpected matrixs type.");
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000474 }
475 }
476}
477
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000478GrGLPathTexGenProgramEffectsBuilder::GrGLPathTexGenProgramEffectsBuilder(
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000479 GrGLFragmentOnlyShaderBuilder* builder,
480 int reserveCount)
481 : fBuilder(builder)
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000482 , fProgramEffects(SkNEW_ARGS(GrGLPathTexGenProgramEffects, (reserveCount))) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000483}
484
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000485void GrGLPathTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
486 GrGLProgramEffects::EffectKey key,
487 const char* outColor,
488 const char* inColor,
489 int stageIndex) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000490 SkASSERT(NULL != fProgramEffects.get());
491 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex);
492}