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

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

Issue 551253004: Changes to remove program effects builder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 3 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
« no previous file with comments | « src/gpu/gl/GrGLProgramEffects.h ('k') | src/gpu/gl/builders/GrGLFragmentOnlyProgramBuilder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrGLProgramEffects.h" 8 #include "GrGLProgramEffects.h"
9 #include "gl/GrGLEffect.h" 9 #include "gl/GrGLEffect.h"
10 #include "gl/GrGLPathRendering.h" 10 #include "gl/GrGLPathRendering.h"
11 #include "gl/builders/GrGLFullProgramBuilder.h" 11 #include "gl/builders/GrGLFullProgramBuilder.h"
12 #include "gl/builders/GrGLFragmentOnlyProgramBuilder.h" 12 #include "gl/builders/GrGLFragmentOnlyProgramBuilder.h"
13 #include "gl/GrGLGeometryProcessor.h" 13 #include "gl/GrGLGeometryProcessor.h"
14 #include "gl/GrGpuGL.h" 14 #include "gl/GrGpuGL.h"
15 15
16 typedef GrGLProgramEffects::TransformedCoords TransformedCoords; 16 typedef GrGLEffect::TransformedCoords TransformedCoords;
17 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; 17 typedef GrGLEffect::TransformedCoordsArray TransformedCoordsArray;
18 typedef GrGLProgramEffects::TextureSampler TextureSampler; 18 typedef GrGLEffect::TextureSampler TextureSampler;
19 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; 19 typedef GrGLEffect::TextureSamplerArray TextureSamplerArray;
20
21 /**
22 * We specialize the vertex code for each of these matrix types.
23 */
24 enum MatrixType {
25 kNoPersp_MatrixType = 0,
26 kGeneral_MatrixType = 1,
27 };
28
29 /**
30 * The key for an individual coord transform is made up of a matrix type and a b it that
31 * indicates the source of the input coords.
32 */
33 enum {
34 kMatrixTypeKeyBits = 1,
35 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
36 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
37 kTransformKeyBits = kMatrixTypeKeyBits + 1,
38 };
39 20
40 namespace { 21 namespace {
41
42 /**
43 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are
44 * present in the texture's config. swizzleComponentMask indicates the channels present in the
45 * shader swizzle.
46 */
47 inline 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 modificat ions.
52 return false;
53 }
54 // check if the texture is alpha-only
55 if (kA_GrColorComponentFlag == configComponentMask) {
56 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleCompon entMask)) {
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 rea d.
63 return true;
64 }
65 }
66 return false;
67 }
68
69 /**
70 * Retrieves the matrix type from transformKey for the transform at transformIdx .
71 */
72 MatrixType get_matrix_type(uint32_t transformKey, int transformIdx) {
73 return static_cast<MatrixType>(
74 (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTyp eKeyMask);
75 }
76
77 /**
78 * Retrieves the source coords from transformKey for the transform at transformI dx. 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 */
82 GrCoordSet get_source_coords(uint32_t transformKey, int transformIdx) {
83 return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoord s_Flag ?
84 kPosition_GrCoordSet :
85 kLocal_GrCoordSet;
86 }
87
88 /** 22 /**
89 * Retrieves the final matrix that a transform needs to apply to its source coor ds. 23 * Retrieves the final matrix that a transform needs to apply to its source coor ds.
90 */ 24 */
91 SkMatrix get_transform_matrix(const GrEffectStage& effectStage, 25 SkMatrix get_transform_matrix(const GrEffectStage& effectStage,
92 bool useExplicitLocalCoords, 26 bool useExplicitLocalCoords,
93 int transformIdx) { 27 int transformIdx) {
94 const GrCoordTransform& coordTransform = effectStage.getEffect()->coordTrans form(transformIdx); 28 const GrCoordTransform& coordTransform = effectStage.getEffect()->coordTrans form(transformIdx);
95 SkMatrix combined; 29 SkMatrix combined;
96 30
97 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) { 31 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
(...skipping 13 matching lines...) Expand all
111 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); 45 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
112 combined.set(SkMatrix::kMTransY, 46 combined.set(SkMatrix::kMTransY,
113 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); 47 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
114 } 48 }
115 return combined; 49 return combined;
116 } 50 }
117 } 51 }
118 52
119 //////////////////////////////////////////////////////////////////////////////// 53 ////////////////////////////////////////////////////////////////////////////////
120 54
121 bool GrGLProgramEffects::GenEffectMetaKey(const GrEffectStage& effectStage,
122 bool useExplicitLocalCoords,
123 const GrGLCaps& caps,
124 GrEffectKeyBuilder* b) {
125
126 uint32_t textureKey = GrGLProgramEffects::GenTextureKey(effectStage.getEffec t(), caps);
127 uint32_t transformKey = GrGLProgramEffects::GenTransformKey(effectStage,useE xplicitLocalCoords);
128 uint32_t attribKey = GrGLProgramEffects::GenAttribKey(effectStage.getEffect( ));
129 uint32_t classID = effectStage.getEffect()->getFactory().effectClassID();
130
131 // Currently we allow 16 bits for each of the above portions of the meta-key . Fail if they
132 // don't fit.
133 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
134 if ((textureKey | transformKey | attribKey | classID) & kMetaKeyInvalidMask) {
135 return false;
136 }
137
138 uint32_t* key = b->add32n(2);
139 key[0] = (textureKey << 16 | transformKey);
140 key[1] = (classID << 16 | attribKey);
141 return true;
142 }
143
144 uint32_t GrGLProgramEffects::GenAttribKey(const GrEffect* effect) {
145 uint32_t key = 0;
146
147 const GrEffect::VertexAttribArray& vars = effect->getVertexAttribs();
148 int numAttributes = vars.count();
149 SkASSERT(numAttributes <= 2);
150 for (int a = 0; a < numAttributes; ++a) {
151 uint32_t value = 1 << a;
152 key |= value;
153 }
154 return key;
155 }
156
157 uint32_t GrGLProgramEffects::GenTransformKey(const GrEffectStage& effectStage,
158 bool useExplicitLocalCoords) {
159 uint32_t totalKey = 0;
160 int numTransforms = effectStage.getEffect()->numTransforms();
161 for (int t = 0; t < numTransforms; ++t) {
162 uint32_t key = 0;
163 const GrCoordTransform& coordTransform = effectStage.getEffect()->coordT ransform(t);
164 SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType();
165 SkMatrix::TypeMask type1 = SkMatrix::kIdentity_Mask;
166 if (kLocal_GrCoordSet == coordTransform.sourceCoords() && !useExplicitLo calCoords) {
167 type1 = effectStage.getCoordChangeMatrix().getType();
168 } else if (kPosition_GrCoordSet == coordTransform.sourceCoords() && useE xplicitLocalCoords) {
169 // We only make the key indicate that device coords are referenced w hen the local coords
170 // are not actually determined by positions. Otherwise the local coo rds var and position
171 // var are identical.
172 key |= kPositionCoords_Flag;
173 }
174
175 int combinedTypes = type0 | type1;
176
177 if (SkMatrix::kPerspective_Mask & combinedTypes) {
178 key |= kGeneral_MatrixType;
179 } else {
180 key |= kNoPersp_MatrixType;
181 }
182 key <<= kTransformKeyBits * t;
183 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
184 totalKey |= key;
185 }
186 return totalKey;
187 }
188
189 uint32_t GrGLProgramEffects::GenTextureKey(const GrEffect* effect, const GrGLCap s& caps) {
190 uint32_t key = 0;
191 int numTextures = effect->numTextures();
192 for (int t = 0; t < numTextures; ++t) {
193 const GrTextureAccess& access = effect->textureAccess(t);
194 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getText ure()->config());
195 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.s wizzleMask())) {
196 key |= 1 << t;
197 }
198 }
199 return key;
200 }
201
202 GrGLProgramEffects::~GrGLProgramEffects() { 55 GrGLProgramEffects::~GrGLProgramEffects() {
203 int numEffects = fGLEffects.count(); 56 int numEffects = fGLEffects.count();
204 for (int e = 0; e < numEffects; ++e) { 57 for (int e = 0; e < numEffects; ++e) {
205 SkDELETE(fGLEffects[e]); 58 SkDELETE(fGLEffects[e]);
206 } 59 }
207 } 60 }
208 61
209 void GrGLProgramEffects::emitSamplers(GrGLProgramBuilder* builder,
210 const GrEffect& effect,
211 TextureSamplerArray* outSamplers) {
212 SkTArray<Sampler, true>& samplers = fSamplers.push_back();
213 int numTextures = effect.numTextures();
214 samplers.push_back_n(numTextures);
215 SkString name;
216 for (int t = 0; t < numTextures; ++t) {
217 name.printf("Sampler%d", t);
218 samplers[t].fUniform = builder->addUniform(GrGLProgramBuilder::kFragment _Visibility,
219 kSampler2D_GrSLType,
220 name.c_str());
221 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
222 (samplers[t].fUniform, effect.textureAccess(t)));
223 }
224 }
225
226 void GrGLProgramEffects::initSamplers(const GrGLProgramDataManager& programResou rceManager, int* texUnitIdx) { 62 void GrGLProgramEffects::initSamplers(const GrGLProgramDataManager& programResou rceManager, int* texUnitIdx) {
227 int numEffects = fGLEffects.count(); 63 int numEffects = fGLEffects.count();
228 SkASSERT(numEffects == fSamplers.count()); 64 SkASSERT(numEffects == fSamplers.count());
229 for (int e = 0; e < numEffects; ++e) { 65 for (int e = 0; e < numEffects; ++e) {
230 SkTArray<Sampler, true>& samplers = fSamplers[e]; 66 SkTArray<Sampler, true>& samplers = fSamplers[e];
231 int numSamplers = samplers.count(); 67 int numSamplers = samplers.count();
232 for (int s = 0; s < numSamplers; ++s) { 68 for (int s = 0; s < numSamplers; ++s) {
233 SkASSERT(samplers[s].fUniform.isValid()); 69 SkASSERT(samplers[s].fUniform.isValid());
234 programResourceManager.setSampler(samplers[s].fUniform, *texUnitIdx) ; 70 programResourceManager.setSampler(samplers[s].fUniform, *texUnitIdx) ;
235 samplers[s].fTextureUnit = (*texUnitIdx)++; 71 samplers[s].fTextureUnit = (*texUnitIdx)++;
236 } 72 }
237 } 73 }
238 } 74 }
239 75
240 void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect& effect, int effectIdx) { 76 void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect& effect, int effectIdx) {
241 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx]; 77 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
242 int numSamplers = samplers.count(); 78 int numSamplers = samplers.count();
243 SkASSERT(numSamplers == effect.numTextures()); 79 SkASSERT(numSamplers == effect.numTextures());
244 for (int s = 0; s < numSamplers; ++s) { 80 for (int s = 0; s < numSamplers; ++s) {
245 SkASSERT(samplers[s].fTextureUnit >= 0); 81 SkASSERT(samplers[s].fTextureUnit >= 0);
246 const GrTextureAccess& textureAccess = effect.textureAccess(s); 82 const GrTextureAccess& textureAccess = effect.textureAccess(s);
247 gpu->bindTexture(samplers[s].fTextureUnit, 83 gpu->bindTexture(samplers[s].fTextureUnit,
248 textureAccess.getParams(), 84 textureAccess.getParams(),
249 static_cast<GrGLTexture*>(textureAccess.getTexture())); 85 static_cast<GrGLTexture*>(textureAccess.getTexture()));
250 } 86 }
251 } 87 }
252 88
253 //////////////////////////////////////////////////////////////////////////////// 89 ////////////////////////////////////////////////////////////////////////////////
254 90
255 void GrGLVertexProgramEffects::emitEffect(GrGLFullProgramBuilder* builder,
256 const GrEffectStage& stage,
257 const GrEffectKey& key,
258 const char* outColor,
259 const char* inColor,
260 int stageIndex) {
261 const GrEffect& effect = *stage.getEffect();
262 SkSTArray<2, TransformedCoords> coords(effect.numTransforms());
263 SkSTArray<4, TextureSampler> samplers(effect.numTextures());
264
265 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
266 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
267 vsBuilder->emitAttributes(stage);
268 this->emitTransforms(builder, stage, &coords);
269 this->emitSamplers(builder, effect, &samplers);
270
271 GrGLEffect* glEffect = effect.getFactory().createGLInstance(effect);
272 fGLEffects.push_back(glEffect);
273
274 // Enclose custom code in a block to avoid namespace conflicts
275 SkString openBrace;
276 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
277 fsBuilder->codeAppend(openBrace.c_str());
278 vsBuilder->codeAppend(openBrace.c_str());
279
280 if (glEffect->isVertexEffect()) {
281 GrGLGeometryProcessor* vertexEffect = static_cast<GrGLGeometryProcessor* >(glEffect);
282 vertexEffect->emitCode(builder, effect, key, outColor, inColor, coords, samplers);
283 } else {
284 glEffect->emitCode(builder, effect, key, outColor, inColor, coords, samp lers);
285 }
286
287 vsBuilder->codeAppend("\t}\n");
288 fsBuilder->codeAppend("\t}\n");
289 }
290
291 void GrGLVertexProgramEffects::emitTransforms(GrGLFullProgramBuilder* builder,
292 const GrEffectStage& effectStage,
293 TransformedCoordsArray* outCoords) {
294 SkTArray<Transform, true>& transforms = fTransforms.push_back();
295 uint32_t totalKey = GenTransformKey(effectStage, fHasExplicitLocalCoords);
296 int numTransforms = effectStage.getEffect()->numTransforms();
297 transforms.push_back_n(numTransforms);
298
299 SkTArray<PathTransform, true>* pathTransforms = NULL;
300 const GrGLCaps* glCaps = builder->ctxInfo().caps();
301 if (glCaps->pathRenderingSupport() &&
302 builder->gpu()->glPathRendering()->texturingMode() ==
303 GrGLPathRendering::SeparableShaders_TexturingMode) {
304 pathTransforms = &fPathTransforms.push_back();
305 pathTransforms->push_back_n(numTransforms);
306 }
307
308 for (int t = 0; t < numTransforms; t++) {
309 GrSLType varyingType = kVoid_GrSLType;
310 const char* uniName;
311 switch (get_matrix_type(totalKey, t)) {
312 case kNoPersp_MatrixType:
313 uniName = "StageMatrix";
314 varyingType = kVec2f_GrSLType;
315 break;
316 case kGeneral_MatrixType:
317 uniName = "StageMatrix";
318 varyingType = kVec3f_GrSLType;
319 break;
320 default:
321 SkFAIL("Unexpected key.");
322 }
323 SkString suffixedUniName;
324 if (0 != t) {
325 suffixedUniName.append(uniName);
326 suffixedUniName.appendf("_%i", t);
327 uniName = suffixedUniName.c_str();
328 }
329 transforms[t].fHandle = builder->addUniform(GrGLProgramBuilder::kVertex_ Visibility,
330 kMat33f_GrSLType,
331 uniName,
332 &uniName);
333
334 const char* varyingName = "MatrixCoord";
335 SkString suffixedVaryingName;
336 if (0 != t) {
337 suffixedVaryingName.append(varyingName);
338 suffixedVaryingName.appendf("_%i", t);
339 varyingName = suffixedVaryingName.c_str();
340 }
341 const char* vsVaryingName;
342 const char* fsVaryingName;
343 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
344 if (pathTransforms) {
345 (*pathTransforms)[t].fHandle =
346 builder->addSeparableVarying(varyingType, varyingName, &vsVaryin gName, &fsVaryingName);
347 (*pathTransforms)[t].fType = varyingType;
348 } else {
349 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVar yingName);
350 }
351
352 const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords( totalKey, t) ?
353 vsBuilder->positionAttribute() :
354 vsBuilder->localCoordsAttribute();
355 // varying = matrix * coords (logically)
356 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingTyp e);
357 if (kVec2f_GrSLType == varyingType) {
358 vsBuilder->codeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
359 vsVaryingName, uniName, coords.c_str());
360 } else {
361 vsBuilder->codeAppendf("\t%s = %s * vec3(%s, 1);\n",
362 vsVaryingName, uniName, coords.c_str());
363 }
364 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
365 (SkString(fsVaryingName), varyingType));
366 }
367 }
368
369 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu, 91 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
370 GrGpu::DrawType drawType, 92 GrGpu::DrawType drawType,
371 const GrGLProgramDataManager& programData Manager, 93 const GrGLProgramDataManager& programData Manager,
372 const GrEffectStage* effectStages[]) { 94 const GrEffectStage* effectStages[]) {
373 int numEffects = fGLEffects.count(); 95 int numEffects = fGLEffects.count();
374 SkASSERT(numEffects == fTransforms.count()); 96 SkASSERT(numEffects == fTransforms.count());
375 SkASSERT(numEffects == fSamplers.count()); 97 SkASSERT(numEffects == fSamplers.count());
376 for (int e = 0; e < numEffects; ++e) { 98 for (int e = 0; e < numEffects; ++e) {
377 const GrEffectStage& effectStage = *effectStages[e]; 99 const GrEffectStage& effectStage = *effectStages[e];
378 const GrEffect& effect = *effectStage.getEffect(); 100 const GrEffect& effect = *effectStage.getEffect();
379 fGLEffects[e]->setData(programDataManager, effect); 101 fGLEffects[e]->setData(programDataManager, effect);
380 if (GrGpu::IsPathRenderingDrawType(drawType)) { 102 if (GrGpu::IsPathRenderingDrawType(drawType)) {
381 this->setPathTransformData(gpu, programDataManager, effectStage, e); 103 this->setPathTransformData(gpu, programDataManager, effectStage, e);
382 } else { 104 } else {
383 this->setTransformData(gpu, programDataManager, effectStage, e); 105 this->setTransformData(gpu, programDataManager, effectStage, e);
384 } 106 }
385 107
386 this->bindTextures(gpu, effect, e); 108 this->bindTextures(gpu, effect, e);
387 } 109 }
388 } 110 }
389 111
390 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
391 GrGpu::DrawType drawType,
392 const GrGLProgramDataManager& programData Manager,
393 const GrEffectStage* effectStage) {
394 SkASSERT(1 == fTransforms.count());
395 SkASSERT(1 == fSamplers.count());
396 SkASSERT(1 == fGLEffects.count());
397 const GrEffect& effect = *effectStage->getEffect();
398 fGLEffects[0]->setData(programDataManager, effect);
399 if (GrGpu::IsPathRenderingDrawType(drawType)) {
400 this->setPathTransformData(gpu, programDataManager, *effectStage, 0);
401 } else {
402 this->setTransformData(gpu, programDataManager, *effectStage, 0);
403 }
404
405 this->bindTextures(gpu, effect, 0);
406 }
407
408 void GrGLVertexProgramEffects::setTransformData(GrGpuGL* gpu, 112 void GrGLVertexProgramEffects::setTransformData(GrGpuGL* gpu,
409 const GrGLProgramDataManager& pd man, 113 const GrGLProgramDataManager& pd man,
410 const GrEffectStage& effectStage , 114 const GrEffectStage& effectStage ,
411 int effectIdx) { 115 int effectIdx) {
412 SkTArray<Transform, true>& transforms = fTransforms[effectIdx]; 116 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
413 int numTransforms = transforms.count(); 117 int numTransforms = transforms.count();
414 SkASSERT(numTransforms == effectStage.getEffect()->numTransforms()); 118 SkASSERT(numTransforms == effectStage.getEffect()->numTransforms());
415 for (int t = 0; t < numTransforms; ++t) { 119 for (int t = 0; t < numTransforms; ++t) {
416 SkASSERT(transforms[t].fHandle.isValid()); 120 SkASSERT(transforms[t].fHandle.isValid());
417 const SkMatrix& matrix = get_transform_matrix(effectStage, fHasExplicitL ocalCoords, t); 121 const SkMatrix& matrix = get_transform_matrix(effectStage, fHasExplicitL ocalCoords, t);
(...skipping 24 matching lines...) Expand all
442 break; 146 break;
443 case kVec3f_GrSLType: 147 case kVec3f_GrSLType:
444 pdman.setProgramPathFragmentInputTransform(transforms[t].fHandle , 3, transform); 148 pdman.setProgramPathFragmentInputTransform(transforms[t].fHandle , 3, transform);
445 break; 149 break;
446 default: 150 default:
447 SkFAIL("Unexpected matrix type."); 151 SkFAIL("Unexpected matrix type.");
448 } 152 }
449 } 153 }
450 } 154 }
451 155
452 GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullProgram Builder* builder,
453 int reserveCoun t)
454 : fBuilder(builder)
455 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
456 (reserveCount,
457 fBuilder->getVertexShaderBuilder()->hasExplici tLocalCoords()))) {
458 }
459 void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
460 const GrEffectKey& key,
461 const char* outColor,
462 const char* inColor,
463 int stageIndex) {
464 SkASSERT(fProgramEffects.get());
465 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIn dex);
466 }
467
468 //////////////////////////////////////////////////////////////////////////////// 156 ////////////////////////////////////////////////////////////////////////////////
469 157
470 void GrGLPathTexGenProgramEffects::emitEffect(GrGLFragmentOnlyProgramBuilder* bu ilder,
471 const GrEffectStage& stage,
472 const GrEffectKey& key,
473 const char* outColor,
474 const char* inColor,
475 int stageIndex) {
476 const GrEffect& effect = *stage.getEffect();
477 SkSTArray<2, TransformedCoords> coords(effect.numTransforms());
478 SkSTArray<4, TextureSampler> samplers(effect.numTextures());
479
480 SkASSERT(0 == effect.getVertexAttribs().count());
481 this->setupPathTexGen(builder, stage, &coords);
482 this->emitSamplers(builder, effect, &samplers);
483
484 GrGLEffect* glEffect = effect.getFactory().createGLInstance(effect);
485 fGLEffects.push_back(glEffect);
486
487 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
488 // Enclose custom code in a block to avoid namespace conflicts
489 SkString openBrace;
490 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
491 fsBuilder->codeAppend(openBrace.c_str());
492
493 SkASSERT(!glEffect->isVertexEffect());
494 glEffect->emitCode(builder, effect, key, outColor, inColor, coords, samplers );
495
496 fsBuilder->codeAppend("\t}\n");
497 }
498
499 void GrGLPathTexGenProgramEffects::setupPathTexGen(GrGLFragmentOnlyProgramBuilde r* builder,
500 const GrEffectStage& effectStage,
501 TransformedCoordsArray* outCoords) {
502 int numTransforms = effectStage.getEffect()->numTransforms();
503 uint32_t totalKey = GenTransformKey(effectStage, false);
504 int texCoordIndex = builder->addTexCoordSets(numTransforms);
505 SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex));
506 SkString name;
507 for (int t = 0; t < numTransforms; ++t) {
508 GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ?
509 kVec3f_GrSLType :
510 kVec2f_GrSLType;
511 name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex ++);
512 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type));
513 }
514 }
515
516 void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu, 158 void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu,
517 GrGpu::DrawType, 159 GrGpu::DrawType,
518 const GrGLProgramDataManager& pdman, 160 const GrGLProgramDataManager& pdman,
519 const GrEffectStage* effectStages[]) { 161 const GrEffectStage* effectStages[]) {
520 int numEffects = fGLEffects.count(); 162 int numEffects = fGLEffects.count();
521 SkASSERT(numEffects == fTransforms.count()); 163 SkASSERT(numEffects == fTransforms.count());
522 SkASSERT(numEffects == fSamplers.count()); 164 SkASSERT(numEffects == fSamplers.count());
523 for (int e = 0; e < numEffects; ++e) { 165 for (int e = 0; e < numEffects; ++e) {
524 const GrEffectStage& effectStage = *effectStages[e]; 166 const GrEffectStage& effectStage = *effectStages[e];
525 const GrEffect& effect = *effectStage.getEffect(); 167 const GrEffect& effect = *effectStage.getEffect();
526 fGLEffects[e]->setData(pdman, effect); 168 fGLEffects[e]->setData(pdman, effect);
527 this->setPathTexGenState(gpu, effectStage, e); 169 this->setPathTexGenState(gpu, effectStage, e);
528 this->bindTextures(gpu, effect, e); 170 this->bindTextures(gpu, effect, e);
529 } 171 }
530 } 172 }
531 173
532 void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu, 174 void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu,
533 const GrEffectStage& effectStage, 175 const GrEffectStage& effectStage,
534 int effectIdx) { 176 int effectIdx) {
535 uint32_t totalKey = fTransforms[effectIdx].fTransformKey;
536 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex; 177 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
537 int numTransforms = effectStage.getEffect()->numTransforms(); 178 int numTransforms = effectStage.getEffect()->numTransforms();
538 for (int t = 0; t < numTransforms; ++t) { 179 for (int t = 0; t < numTransforms; ++t) {
539 switch (get_matrix_type(totalKey, t)) { 180 const SkMatrix& transform = get_transform_matrix(effectStage, false, t);
540 case kNoPersp_MatrixType: { 181 GrGLPathRendering::PathTexGenComponents components =
541 const SkMatrix& transform = get_transform_matrix(effectStage, fa lse, t); 182 GrGLPathRendering::kST_PathTexGenComponents;
542 gpu->glPathRendering()->enablePathTexGen( 183 if (effectStage.isPerspectiveCoordTransform(t, false)) {
543 texCoordIndex++, 184 components = GrGLPathRendering::kSTR_PathTexGenComponents;
544 GrGLPathRendering::kST_PathTexGenComponents,
545 transform);
546 break;
547 }
548 case kGeneral_MatrixType: {
549 const SkMatrix& transform = get_transform_matrix(effectStage, fa lse, t);
550 gpu->glPathRendering()->enablePathTexGen(
551 texCoordIndex++,
552 GrGLPathRendering::kSTR_PathTexGenComponents,
553 transform);
554 break;
555 }
556 default:
557 SkFAIL("Unexpected matrixs type.");
558 } 185 }
186 gpu->glPathRendering()->enablePathTexGen(texCoordIndex++, components, tr ansform);
559 } 187 }
560 } 188 }
561
562 GrGLPathTexGenProgramEffectsBuilder::GrGLPathTexGenProgramEffectsBuilder(
563 GrGLFragmentOnlyProgramBuilder* builder,
564 int reserveCount)
565 : fBuilder(builder)
566 , fProgramEffects(SkNEW_ARGS(GrGLPathTexGenProgramEffects, (reserveCount))) {
567 }
568 void GrGLPathTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
569 const GrEffectKey& key,
570 const char* outColor,
571 const char* inColor,
572 int stageIndex) {
573 SkASSERT(fProgramEffects.get());
574 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIn dex);
575 }
576
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLProgramEffects.h ('k') | src/gpu/gl/builders/GrGLFragmentOnlyProgramBuilder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698