blob: d5826abe3042e9e1eb9057996d763483161a3044 [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 {
25 kIdentity_MatrixType = 0,
26 kTrans_MatrixType = 1,
27 kNoPersp_MatrixType = 2,
28 kGeneral_MatrixType = 3,
29};
30
31/**
32 * The key for an individual coord transform is made up of a matrix type and a bit that
33 * indicates the source of the input coords.
34 */
35enum {
36 kMatrixTypeKeyBits = 2,
37 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
38 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
39 kTransformKeyBits = kMatrixTypeKeyBits + 1,
40 kTransformKeyMask = (1 << kTransformKeyBits) - 1,
41};
42
43namespace {
44
45/**
46 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
47 * present in the texture's config. swizzleComponentMask indicates the channels present in the
48 * shader swizzle.
49 */
50inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
51 uint32_t configComponentMask,
52 uint32_t swizzleComponentMask) {
53 if (caps.textureSwizzleSupport()) {
54 // Any remapping is handled using texture swizzling not shader modifications.
55 return false;
56 }
57 // check if the texture is alpha-only
58 if (kA_GrColorComponentFlag == configComponentMask) {
59 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
60 // we must map the swizzle 'a's to 'r'.
61 return true;
62 }
63 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
64 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
65 // alpha-only textures smear alpha across all four channels when read.
66 return true;
67 }
68 }
69 return false;
70}
71
72}
73
74EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) {
75 EffectKey key = 0;
76 int numAttributes = drawEffect.getVertexAttribIndexCount();
77 SkASSERT(numAttributes <= 2);
78 const int* attributeIndices = drawEffect.getVertexAttribIndices();
79 for (int a = 0; a < numAttributes; ++a) {
80 EffectKey value = attributeIndices[a] << 3 * a;
81 SkASSERT(0 == (value & key)); // keys for each attribute ought not to overlap
82 key |= value;
83 }
84 return key;
85}
86
87EffectKey GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) {
88 EffectKey totalKey = 0;
89 int numTransforms = (*drawEffect.effect())->numTransforms();
90 for (int t = 0; t < numTransforms; ++t) {
91 EffectKey key = 0;
92 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransform(t);
93 SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType();
94 SkMatrix::TypeMask type1;
95 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
96 type1 = drawEffect.getCoordChangeMatrix().getType();
97 } else {
98 if (drawEffect.programHasExplicitLocalCoords()) {
99 // We only make the key indicate that device coords are referenced when the local coords
100 // are not actually determined by positions. Otherwise the local coords var and position
101 // var are identical.
102 key |= kPositionCoords_Flag;
103 }
104 type1 = SkMatrix::kIdentity_Mask;
105 }
106
107 int combinedTypes = type0 | type1;
108
109 bool reverseY = coordTransform.reverseY();
110
111 if (SkMatrix::kPerspective_Mask & combinedTypes) {
112 key |= kGeneral_MatrixType;
113 } else if (((SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask) & combinedTypes) || reverseY) {
114 key |= kNoPersp_MatrixType;
115 } else if (SkMatrix::kTranslate_Mask & combinedTypes) {
116 key |= kTrans_MatrixType;
117 } else {
118 key |= kIdentity_MatrixType;
119 }
120 key <<= kTransformKeyBits * t;
121 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
122 totalKey |= key;
123 }
124 return totalKey;
125}
126
127EffectKey GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps) {
128 EffectKey key = 0;
129 int numTextures = (*drawEffect.effect())->numTextures();
130 for (int t = 0; t < numTextures; ++t) {
131 const GrTextureAccess& access = (*drawEffect.effect())->textureAccess(t);
132 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
133 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
134 key |= 1 << t;
135 }
136 }
137 return key;
138}
139
140GrGLProgramEffects::~GrGLProgramEffects() {
141 int numEffects = fGLEffects.count();
142 for (int e = 0; e < numEffects; ++e) {
143 SkDELETE(fGLEffects[e]);
144 }
145}
146
147void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) {
148 int numEffects = fGLEffects.count();
149 SkASSERT(numEffects == fSamplers.count());
150 for (int e = 0; e < numEffects; ++e) {
151 SkTArray<Sampler, true>& samplers = fSamplers[e];
152 int numSamplers = samplers.count();
153 for (int s = 0; s < numSamplers; ++s) {
154 SkASSERT(samplers[s].fUniform.isValid());
155 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx);
156 samplers[s].fTextureUnit = (*texUnitIdx)++;
157 }
158 }
159}
160
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000161void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
162 const GrGLUniformManager& uniformManager,
163 const GrEffectStage* effectStages[]) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000164 int numEffects = fGLEffects.count();
165 SkASSERT(numEffects == fTransforms.count());
166 SkASSERT(numEffects == fSamplers.count());
167 for (int e = 0; e < numEffects; ++e) {
168 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
169 fGLEffects[e]->setData(uniformManager, drawEffect);
170 this->setTransformData(uniformManager, drawEffect, e);
171 this->bindTextures(gpu, *drawEffect.effect(), e);
172 }
173}
174
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000175void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& uniformManager,
176 const GrDrawEffect& drawEffect,
177 int effectIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000178 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
179 int numTransforms = transforms.count();
180 SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms());
181 for (int t = 0; t < numTransforms; ++t) {
182 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransform(t);
183 const SkMatrix& matrix = coordTransform.getMatrix();
184 const SkMatrix& coordChangeMatrix = kLocal_GrCoordSet == coordTransform.sourceCoords() ?
185 drawEffect.getCoordChangeMatrix() :
186 SkMatrix::I();
187 SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transforms[t].fType));
188 switch (transforms[t].fType) {
189 case kVoid_GrSLType:
190 SkASSERT(matrix.isIdentity());
191 SkASSERT(coordChangeMatrix.isIdentity());
192 SkASSERT(!coordTransform.reverseY());
193 return;
194 case kVec2f_GrSLType: {
195 SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMatrix.getType()));
196 SkASSERT(!coordTransform.reverseY());
197 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMatrix::kMTransX];
198 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMatrix::kMTransY];
199 if (transforms[t].fCurrentValue.get(SkMatrix::kMTransX) != tx ||
200 transforms[t].fCurrentValue.get(SkMatrix::kMTransY) != ty) {
201 uniformManager.set2f(transforms[t].fHandle, tx, ty);
202 transforms[t].fCurrentValue.set(SkMatrix::kMTransX, tx);
203 transforms[t].fCurrentValue.set(SkMatrix::kMTransY, ty);
204 }
205 break;
206 }
207 case kMat33f_GrSLType: {
208 SkMatrix combined;
209 combined.setConcat(matrix, coordChangeMatrix);
210 if (coordTransform.reverseY()) {
211 // combined.postScale(1,-1);
212 // combined.postTranslate(0,1);
213 combined.set(SkMatrix::kMSkewY,
214 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
215 combined.set(SkMatrix::kMScaleY,
216 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
217 combined.set(SkMatrix::kMTransY,
218 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
219 }
220 if (!transforms[t].fCurrentValue.cheapEqualTo(combined)) {
221 uniformManager.setSkMatrix(transforms[t].fHandle, combined);
222 transforms[t].fCurrentValue = combined;
223 }
224 break;
225 }
226 default:
227 GrCrash("Unexpected uniform type.");
228 }
229 }
230}
231
232void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffectRef& effect, int effectIdx) {
233 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
234 int numSamplers = samplers.count();
235 SkASSERT(numSamplers == effect->numTextures());
236 for (int s = 0; s < numSamplers; ++s) {
237 SkASSERT(samplers[s].fTextureUnit >= 0);
238 const GrTextureAccess& textureAccess = effect->textureAccess(s);
239 gpu->bindTexture(samplers[s].fTextureUnit,
240 textureAccess.getParams(),
241 static_cast<GrGLTexture*>(textureAccess.getTexture()));
242 }
243}
244
245////////////////////////////////////////////////////////////////////////////////
246
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000247GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder,
248 int reserveCount)
249 : fBuilder(builder)
250 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
251 (reserveCount, fBuilder->hasExplicitLocalCoords()))) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000252}
253
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000254void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
255 EffectKey key,
256 const char* outColor,
257 const char* inColor,
258 int stageIndex) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000259 SkASSERT(NULL != fProgramEffects.get());
260
261 GrDrawEffect drawEffect(stage, fProgramEffects->fHasExplicitLocalCoords);
262 const GrEffectRef& effect = *stage.getEffect();
263 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
264 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
265
266 this->emitAttributes(stage);
267 this->emitTransforms(effect, key, &coords);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000268 INHERITED::emitSamplers(fBuilder, fProgramEffects.get(), effect, &samplers);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000269
270 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
271 fProgramEffects->fGLEffects.push_back(glEffect);
272
273 // Enclose custom code in a block to avoid namespace conflicts
274 SkString openBrace;
275 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000276 fBuilder->vsCodeAppend(openBrace.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000277 fBuilder->fsCodeAppend(openBrace.c_str());
278
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000279 if (glEffect->isVertexEffect()) {
280 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect);
281 vertexEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords, samplers);
282 } else {
283 glEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords, samplers);
284 }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000285
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000286 fBuilder->vsCodeAppend("\t}\n");
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000287 fBuilder->fsCodeAppend("\t}\n");
288}
289
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000290void GrGLVertexProgramEffectsBuilder::emitAttributes(const GrEffectStage& stage) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000291 int numAttributes = stage.getVertexAttribIndexCount();
292 const int* attributeIndices = stage.getVertexAttribIndices();
293 for (int a = 0; a < numAttributes; ++a) {
294 // TODO: Make addAttribute mangle the name.
295 SkString attributeName("aAttr");
296 attributeName.appendS32(attributeIndices[a]);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000297 fBuilder->addEffectAttribute(attributeIndices[a],
298 (*stage.getEffect())->vertexAttribType(a),
299 attributeName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000300 }
301}
302
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000303void GrGLVertexProgramEffectsBuilder::emitTransforms(const GrEffectRef& effect,
304 EffectKey effectKey,
305 TransformedCoordsArray* outCoords) {
306 typedef GrGLVertexProgramEffects::Transform Transform;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000307 SkTArray<Transform, true>& transforms = fProgramEffects->fTransforms.push_back();
308 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
309 int numTransforms = effect->numTransforms();
310 transforms.push_back_n(numTransforms);
311 for (int t = 0; t < numTransforms; t++) {
312 EffectKey key = (totalKey >> (kTransformKeyBits * t)) & kTransformKeyMask;
313 GrSLType varyingType = kVoid_GrSLType;
314 const char* uniName;
315 switch (key & kMatrixTypeKeyMask) {
316 case kIdentity_MatrixType:
317 transforms[t].fType = kVoid_GrSLType;
318 uniName = NULL;
319 varyingType = kVec2f_GrSLType;
320 break;
321 case kTrans_MatrixType:
322 transforms[t].fType = kVec2f_GrSLType;
323 uniName = "StageTranslate";
324 varyingType = kVec2f_GrSLType;
325 break;
326 case kNoPersp_MatrixType:
327 transforms[t].fType = kMat33f_GrSLType;
328 uniName = "StageMatrix";
329 varyingType = kVec2f_GrSLType;
330 break;
331 case kGeneral_MatrixType:
332 transforms[t].fType = kMat33f_GrSLType;
333 uniName = "StageMatrix";
334 varyingType = kVec3f_GrSLType;
335 break;
336 default:
337 GrCrash("Unexpected key.");
338 }
339 SkString suffixedUniName;
340 if (kVoid_GrSLType != transforms[t].fType) {
341 if (0 != t) {
342 suffixedUniName.append(uniName);
343 suffixedUniName.appendf("_%i", t);
344 uniName = suffixedUniName.c_str();
345 }
346 transforms[t].fHandle = fBuilder->addUniform(GrGLShaderBuilder::kVertex_Visibility,
347 transforms[t].fType,
348 uniName,
349 &uniName);
350 }
351
352 const char* varyingName = "MatrixCoord";
353 SkString suffixedVaryingName;
354 if (0 != t) {
355 suffixedVaryingName.append(varyingName);
356 suffixedVaryingName.appendf("_%i", t);
357 varyingName = suffixedVaryingName.c_str();
358 }
359 const char* vsVaryingName;
360 const char* fsVaryingName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000361 fBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000362
363 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ?
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000364 fBuilder->positionAttribute() :
365 fBuilder->localCoordsAttribute();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000366 // varying = matrix * coords (logically)
367 switch (transforms[t].fType) {
368 case kVoid_GrSLType:
369 SkASSERT(kVec2f_GrSLType == varyingType);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000370 fBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000371 break;
372 case kVec2f_GrSLType:
373 SkASSERT(kVec2f_GrSLType == varyingType);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000374 fBuilder->vsCodeAppendf("\t%s = %s + %s;\n",
375 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000376 break;
377 case kMat33f_GrSLType: {
378 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
379 if (kVec2f_GrSLType == varyingType) {
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000380 fBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
381 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000382 } else {
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000383 fBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
384 vsVaryingName, uniName, coords.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000385 }
386 break;
387 }
388 default:
389 GrCrash("Unexpected uniform type.");
390 }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000391 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (fsVaryingName, varyingType));
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000392 }
393}
394
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000395void GrGLProgramEffectsBuilder::emitSamplers(GrGLShaderBuilder* builder,
396 GrGLProgramEffects* programEffects,
397 const GrEffectRef& effect,
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000398 TextureSamplerArray* outSamplers) {
399 typedef GrGLProgramEffects::Sampler Sampler;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000400 SkTArray<Sampler, true>& samplers = programEffects->fSamplers.push_back();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000401 int numTextures = effect->numTextures();
402 samplers.push_back_n(numTextures);
403 SkString name;
404 for (int t = 0; t < numTextures; ++t) {
405 name.printf("Sampler%d", t);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000406 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
407 kSampler2D_GrSLType,
408 name.c_str());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000409 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
410 (samplers[t].fUniform, effect->textureAccess(t)));
411 }
412}