Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: src/gpu/gl/GrGLProgramEffects.cpp

Issue 25605008: Repurpose GrGLCoordTransform as GrGLEffectArray (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/gpu/gl/GrGLProgramEffects.h ('k') | src/gpu/gl/GrGLShaderBuilder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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"
12 #include "gl/GrGpuGL.h"
13
14 typedef GrGLProgramEffects::EffectKey EffectKey;
15 typedef GrGLProgramEffects::TransformedCoords TransformedCoords;
16 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
17 typedef GrGLProgramEffects::TextureSampler TextureSampler;
18 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
19
20 /**
21 * We specialize the vertex code for each of these matrix types.
22 */
23 enum MatrixType {
24 kIdentity_MatrixType = 0,
25 kTrans_MatrixType = 1,
26 kNoPersp_MatrixType = 2,
27 kGeneral_MatrixType = 3,
28 };
29
30 /**
31 * The key for an individual coord transform is made up of a matrix type and a b it that
32 * indicates the source of the input coords.
33 */
34 enum {
35 kMatrixTypeKeyBits = 2,
36 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
37 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
38 kTransformKeyBits = kMatrixTypeKeyBits + 1,
39 kTransformKeyMask = (1 << kTransformKeyBits) - 1,
40 };
41
42 namespace {
43
44 /**
45 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are
46 * present in the texture's config. swizzleComponentMask indicates the channels present in the
47 * shader swizzle.
48 */
49 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
50 uint32_t configComponentMask,
51 uint32_t swizzleComponentMask) {
52 if (caps.textureSwizzleSupport()) {
53 // Any remapping is handled using texture swizzling not shader modificat ions.
54 return false;
55 }
56 // check if the texture is alpha-only
57 if (kA_GrColorComponentFlag == configComponentMask) {
58 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleCompon entMask)) {
59 // we must map the swizzle 'a's to 'r'.
60 return true;
61 }
62 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
63 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
64 // alpha-only textures smear alpha across all four channels when rea d.
65 return true;
66 }
67 }
68 return false;
69 }
70
71 }
72
73 EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) {
74 EffectKey key = 0;
75 int numAttributes = drawEffect.getVertexAttribIndexCount();
76 SkASSERT(numAttributes <= 2);
77 const int* attributeIndices = drawEffect.getVertexAttribIndices();
78 for (int a = 0; a < numAttributes; ++a) {
79 EffectKey value = attributeIndices[a] << 3 * a;
80 SkASSERT(0 == (value & key)); // keys for each attribute ought not to ov erlap
81 key |= value;
82 }
83 return key;
84 }
85
86 EffectKey GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) {
87 EffectKey totalKey = 0;
88 int numTransforms = (*drawEffect.effect())->numTransforms();
89 for (int t = 0; t < numTransforms; ++t) {
90 EffectKey key = 0;
91 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTr ansform(t);
92 SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType();
93 SkMatrix::TypeMask type1;
94 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
95 type1 = drawEffect.getCoordChangeMatrix().getType();
96 } else {
97 if (drawEffect.programHasExplicitLocalCoords()) {
98 // We only make the key indicate that device coords are referenc ed when the local coords
99 // are not actually determined by positions. Otherwise the local coords var and position
100 // var are identical.
101 key |= kPositionCoords_Flag;
102 }
103 type1 = SkMatrix::kIdentity_Mask;
104 }
105
106 int combinedTypes = type0 | type1;
107
108 bool reverseY = coordTransform.reverseY();
109
110 if (SkMatrix::kPerspective_Mask & combinedTypes) {
111 key |= kGeneral_MatrixType;
112 } else if (((SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask) & combinedT ypes) || reverseY) {
113 key |= kNoPersp_MatrixType;
114 } else if (SkMatrix::kTranslate_Mask & combinedTypes) {
115 key |= kTrans_MatrixType;
116 } else {
117 key |= kIdentity_MatrixType;
118 }
119 key <<= kTransformKeyBits * t;
120 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
121 totalKey |= key;
122 }
123 return totalKey;
124 }
125
126 EffectKey GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, cons t GrGLCaps& caps) {
127 EffectKey key = 0;
128 int numTextures = (*drawEffect.effect())->numTextures();
129 for (int t = 0; t < numTextures; ++t) {
130 const GrTextureAccess& access = (*drawEffect.effect())->textureAccess(t) ;
131 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getText ure()->config());
132 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.s wizzleMask())) {
133 key |= 1 << t;
134 }
135 }
136 return key;
137 }
138
139 GrGLProgramEffects::~GrGLProgramEffects() {
140 int numEffects = fGLEffects.count();
141 for (int e = 0; e < numEffects; ++e) {
142 SkDELETE(fGLEffects[e]);
143 }
144 }
145
146 void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) {
147 int numEffects = fGLEffects.count();
148 SkASSERT(numEffects == fSamplers.count());
149 for (int e = 0; e < numEffects; ++e) {
150 SkTArray<Sampler, true>& samplers = fSamplers[e];
151 int numSamplers = samplers.count();
152 for (int s = 0; s < numSamplers; ++s) {
153 SkASSERT(samplers[s].fUniform.isValid());
154 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx);
155 samplers[s].fTextureUnit = (*texUnitIdx)++;
156 }
157 }
158 }
159
160 void GrGLProgramEffects::setData(GrGpuGL* gpu,
161 const GrGLUniformManager& uniformManager,
162 const GrEffectStage* effectStages[]) {
163 int numEffects = fGLEffects.count();
164 SkASSERT(numEffects == fTransforms.count());
165 SkASSERT(numEffects == fSamplers.count());
166 for (int e = 0; e < numEffects; ++e) {
167 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
168 fGLEffects[e]->setData(uniformManager, drawEffect);
169 this->setTransformData(uniformManager, drawEffect, e);
170 this->bindTextures(gpu, *drawEffect.effect(), e);
171 }
172 }
173
174 void GrGLProgramEffects::setTransformData(const GrGLUniformManager& uniformManag er,
175 const GrDrawEffect& drawEffect,
176 int effectIdx) {
177 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
178 int numTransforms = transforms.count();
179 SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms());
180 for (int t = 0; t < numTransforms; ++t) {
181 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTr ansform(t);
182 const SkMatrix& matrix = coordTransform.getMatrix();
183 const SkMatrix& coordChangeMatrix = kLocal_GrCoordSet == coordTransform. sourceCoords() ?
184 drawEffect.getCoordChangeMatrix( ) :
185 SkMatrix::I();
186 SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transform s[t].fType));
187 switch (transforms[t].fType) {
188 case kVoid_GrSLType:
189 SkASSERT(matrix.isIdentity());
190 SkASSERT(coordChangeMatrix.isIdentity());
191 SkASSERT(!coordTransform.reverseY());
192 return;
193 case kVec2f_GrSLType: {
194 SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordC hangeMatrix.getType()));
195 SkASSERT(!coordTransform.reverseY());
196 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[S kMatrix::kMTransX];
197 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[S kMatrix::kMTransY];
198 if (transforms[t].fCurrentValue.get(SkMatrix::kMTransX) != tx ||
199 transforms[t].fCurrentValue.get(SkMatrix::kMTransY) != ty) {
200 uniformManager.set2f(transforms[t].fHandle, tx, ty);
201 transforms[t].fCurrentValue.set(SkMatrix::kMTransX, tx);
202 transforms[t].fCurrentValue.set(SkMatrix::kMTransY, ty);
203 }
204 break;
205 }
206 case kMat33f_GrSLType: {
207 SkMatrix combined;
208 combined.setConcat(matrix, coordChangeMatrix);
209 if (coordTransform.reverseY()) {
210 // combined.postScale(1,-1);
211 // combined.postTranslate(0,1);
212 combined.set(SkMatrix::kMSkewY,
213 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkew Y]);
214 combined.set(SkMatrix::kMScaleY,
215 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScal eY]);
216 combined.set(SkMatrix::kMTransY,
217 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTran sY]);
218 }
219 if (!transforms[t].fCurrentValue.cheapEqualTo(combined)) {
220 uniformManager.setSkMatrix(transforms[t].fHandle, combined);
221 transforms[t].fCurrentValue = combined;
222 }
223 break;
224 }
225 default:
226 GrCrash("Unexpected uniform type.");
227 }
228 }
229 }
230
231 void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffectRef& effect, i nt effectIdx) {
232 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
233 int numSamplers = samplers.count();
234 SkASSERT(numSamplers == effect->numTextures());
235 for (int s = 0; s < numSamplers; ++s) {
236 SkASSERT(samplers[s].fTextureUnit >= 0);
237 const GrTextureAccess& textureAccess = effect->textureAccess(s);
238 gpu->bindTexture(samplers[s].fTextureUnit,
239 textureAccess.getParams(),
240 static_cast<GrGLTexture*>(textureAccess.getTexture()));
241 }
242 }
243
244 ////////////////////////////////////////////////////////////////////////////////
245
246 GrGLProgramEffectsBuilder::GrGLProgramEffectsBuilder(GrGLShaderBuilder* builder, int reserveCount)
247 : fBuilder(builder) {
248 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder ();
249 SkASSERT(NULL != vertexBuilder);
250 fProgramEffects.reset(SkNEW_ARGS(GrGLProgramEffects,
251 (reserveCount, vertexBuilder->hasExplicitLo calCoords())));
252 }
253
254 void GrGLProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
255 EffectKey key,
256 const char* outColor,
257 const char* inColor,
258 int stageIndex) {
259 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder ();
260 SkASSERT(NULL != vertexBuilder);
261 SkASSERT(NULL != fProgramEffects.get());
262
263 GrDrawEffect drawEffect(stage, fProgramEffects->fHasExplicitLocalCoords);
264 const GrEffectRef& effect = *stage.getEffect();
265 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
266 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
267
268 this->emitAttributes(stage);
269 this->emitTransforms(effect, key, &coords);
270 this->emitSamplers(effect, &samplers);
271
272 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
273 fProgramEffects->fGLEffects.push_back(glEffect);
274
275 // Enclose custom code in a block to avoid namespace conflicts
276 SkString openBrace;
277 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
278 vertexBuilder->vsCodeAppend(openBrace.c_str());
279 fBuilder->fsCodeAppend(openBrace.c_str());
280
281 glEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords, sam plers);
282
283 vertexBuilder->vsCodeAppend("\t}\n");
284 fBuilder->fsCodeAppend("\t}\n");
285 }
286
287 void GrGLProgramEffectsBuilder::emitAttributes(const GrEffectStage& stage) {
288 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder ();
289 SkASSERT(NULL != vertexBuilder);
290
291 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]);
297 vertexBuilder->addEffectAttribute(attributeIndices[a],
298 (*stage.getEffect())->vertexAttribType (a),
299 attributeName);
300 }
301 }
302
303 void GrGLProgramEffectsBuilder::emitTransforms(const GrEffectRef& effect,
304 EffectKey effectKey,
305 TransformedCoordsArray* outCoords ) {
306 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder ();
307 SkASSERT(NULL != vertexBuilder);
308
309 typedef GrGLProgramEffects::Transform Transform;
310 SkTArray<Transform, true>& transforms = fProgramEffects->fTransforms.push_ba ck();
311 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
312 int numTransforms = effect->numTransforms();
313 transforms.push_back_n(numTransforms);
314 for (int t = 0; t < numTransforms; t++) {
315 EffectKey key = (totalKey >> (kTransformKeyBits * t)) & kTransformKeyMas k;
316 GrSLType varyingType = kVoid_GrSLType;
317 const char* uniName;
318 switch (key & kMatrixTypeKeyMask) {
319 case kIdentity_MatrixType:
320 transforms[t].fType = kVoid_GrSLType;
321 uniName = NULL;
322 varyingType = kVec2f_GrSLType;
323 break;
324 case kTrans_MatrixType:
325 transforms[t].fType = kVec2f_GrSLType;
326 uniName = "StageTranslate";
327 varyingType = kVec2f_GrSLType;
328 break;
329 case kNoPersp_MatrixType:
330 transforms[t].fType = kMat33f_GrSLType;
331 uniName = "StageMatrix";
332 varyingType = kVec2f_GrSLType;
333 break;
334 case kGeneral_MatrixType:
335 transforms[t].fType = kMat33f_GrSLType;
336 uniName = "StageMatrix";
337 varyingType = kVec3f_GrSLType;
338 break;
339 default:
340 GrCrash("Unexpected key.");
341 }
342 SkString suffixedUniName;
343 if (kVoid_GrSLType != transforms[t].fType) {
344 if (0 != t) {
345 suffixedUniName.append(uniName);
346 suffixedUniName.appendf("_%i", t);
347 uniName = suffixedUniName.c_str();
348 }
349 transforms[t].fHandle = fBuilder->addUniform(GrGLShaderBuilder::kVer tex_Visibility,
350 transforms[t].fType,
351 uniName,
352 &uniName);
353 }
354
355 const char* varyingName = "MatrixCoord";
356 SkString suffixedVaryingName;
357 if (0 != t) {
358 suffixedVaryingName.append(varyingName);
359 suffixedVaryingName.appendf("_%i", t);
360 varyingName = suffixedVaryingName.c_str();
361 }
362 const char* vsVaryingName;
363 const char* fsVaryingName;
364 vertexBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsV aryingName);
365
366 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ?
367 vertexBuilder->positionAttribute() :
368 vertexBuilder->localCoordsAttribute();
369 // varying = matrix * coords (logically)
370 switch (transforms[t].fType) {
371 case kVoid_GrSLType:
372 SkASSERT(kVec2f_GrSLType == varyingType);
373 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coor ds.c_str());
374 break;
375 case kVec2f_GrSLType:
376 SkASSERT(kVec2f_GrSLType == varyingType);
377 vertexBuilder->vsCodeAppendf("\t%s = %s + %s;\n",
378 vsVaryingName, uniName, coords.c_st r());
379 break;
380 case kMat33f_GrSLType: {
381 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == va ryingType);
382 if (kVec2f_GrSLType == varyingType) {
383 vertexBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\ n",
384 vsVaryingName, uniName, coords. c_str());
385 } else {
386 vertexBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
387 vsVaryingName, uniName, coords. c_str());
388 }
389 break;
390 }
391 default:
392 GrCrash("Unexpected uniform type.");
393 }
394 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
395 (fsVaryingName, varyingType, vsVaryingName));
396 }
397 }
398
399 void GrGLProgramEffectsBuilder::emitSamplers(const GrEffectRef& effect,
400 TextureSamplerArray* outSamplers) {
401 typedef GrGLProgramEffects::Sampler Sampler;
402 SkTArray<Sampler, true>& samplers = fProgramEffects->fSamplers.push_back();
403 int numTextures = effect->numTextures();
404 samplers.push_back_n(numTextures);
405 SkString name;
406 for (int t = 0; t < numTextures; ++t) {
407 name.printf("Sampler%d", t);
408 samplers[t].fUniform = fBuilder->addUniform(GrGLShaderBuilder::kFragment _Visibility,
409 kSampler2D_GrSLType,
410 name.c_str());
411 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
412 (samplers[t].fUniform, effect->textureAccess(t))) ;
413 }
414 }
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLProgramEffects.h ('k') | src/gpu/gl/GrGLShaderBuilder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698