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

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

Issue 25846002: Use vertexless shaders when NVpr is available (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix mac build 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
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 "GrDrawEffect.h" 9 #include "GrDrawEffect.h"
10 #include "gl/GrGLEffect.h" 10 #include "gl/GrGLEffect.h"
(...skipping 19 matching lines...) Expand all
30 30
31 /** 31 /**
32 * The key for an individual coord transform is made up of a matrix type and a b it that 32 * The key for an individual coord transform is made up of a matrix type and a b it that
33 * indicates the source of the input coords. 33 * indicates the source of the input coords.
34 */ 34 */
35 enum { 35 enum {
36 kMatrixTypeKeyBits = 2, 36 kMatrixTypeKeyBits = 2,
37 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, 37 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
38 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), 38 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
39 kTransformKeyBits = kMatrixTypeKeyBits + 1, 39 kTransformKeyBits = kMatrixTypeKeyBits + 1,
40 kTransformKeyMask = (1 << kTransformKeyBits) - 1,
41 }; 40 };
42 41
43 namespace { 42 namespace {
44 43
45 /** 44 /**
46 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are 45 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are
47 * present in the texture's config. swizzleComponentMask indicates the channels present in the 46 * present in the texture's config. swizzleComponentMask indicates the channels present in the
48 * shader swizzle. 47 * shader swizzle.
49 */ 48 */
50 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, 49 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
(...skipping 11 matching lines...) Expand all
62 } 61 }
63 if (kRGB_GrColorComponentFlags & swizzleComponentMask) { 62 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
64 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that 63 // 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 rea d. 64 // alpha-only textures smear alpha across all four channels when rea d.
66 return true; 65 return true;
67 } 66 }
68 } 67 }
69 return false; 68 return false;
70 } 69 }
71 70
71 /**
72 * Retrieves the matrix type from transformKey for the transform at transformIdx .
73 */
74 MatrixType get_matrix_type(EffectKey transformKey, int transformIdx) {
75 return static_cast<MatrixType>(
76 (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTyp eKeyMask);
77 }
78
79 /**
80 * Retrieves the source coords from transformKey for the transform at transformI dx. It may not be
81 * the same coordinate set as the original GrCoordTransform if the position and local coords are
82 * identical for this program.
83 */
84 GrCoordSet get_source_coords(EffectKey transformKey, int transformIdx) {
85 return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoord s_Flag ?
86 kPosition_GrCoordSet :
87 kLocal_GrCoordSet;
88 }
89
90 /**
91 * Retrieves the final translation that a transform needs to apply to its source coords (and
92 * verifies that a translation is all it needs).
93 */
94 void get_transform_translation(const GrDrawEffect& drawEffect,
95 int transformIdx,
96 GrGLfloat* tx,
97 GrGLfloat* ty) {
98 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransf orm(transformIdx);
99 SkASSERT(!coordTransform.reverseY());
100 const SkMatrix& matrix = coordTransform.getMatrix();
101 if (kLocal_GrCoordSet == coordTransform.sourceCoords() &&
102 !drawEffect.programHasExplicitLocalCoords()) {
103 const SkMatrix& coordChangeMatrix = drawEffect.getCoordChangeMatrix();
104 SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMat rix.getType()));
105 *tx = SkScalarToFloat(matrix[SkMatrix::kMTransX] + coordChangeMatrix[SkM atrix::kMTransX]);
106 *ty = SkScalarToFloat(matrix[SkMatrix::kMTransY] + coordChangeMatrix[SkM atrix::kMTransY]);
107 } else {
108 SkASSERT(SkMatrix::kTranslate_Mask == matrix.getType());
109 *tx = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
110 *ty = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
111 }
112 }
113
114 /**
115 * Retrieves the final matrix that a transform needs to apply to its source coor ds.
116 */
117 SkMatrix get_transform_matrix(const GrDrawEffect& drawEffect, int transformIdx) {
118 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransf orm(transformIdx);
119 SkMatrix combined;
120 if (kLocal_GrCoordSet == coordTransform.sourceCoords() &&
121 !drawEffect.programHasExplicitLocalCoords()) {
122 combined.setConcat(coordTransform.getMatrix(), drawEffect.getCoordChange Matrix());
123 } else {
124 combined = coordTransform.getMatrix();
125 }
126 if (coordTransform.reverseY()) {
127 // combined.postScale(1,-1);
128 // combined.postTranslate(0,1);
129 combined.set(SkMatrix::kMSkewY,
130 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
131 combined.set(SkMatrix::kMScaleY,
132 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
133 combined.set(SkMatrix::kMTransY,
134 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
135 }
136 return combined;
137 }
138
72 } 139 }
73 140
141 ////////////////////////////////////////////////////////////////////////////////
142
74 EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) { 143 EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) {
75 EffectKey key = 0; 144 EffectKey key = 0;
76 int numAttributes = drawEffect.getVertexAttribIndexCount(); 145 int numAttributes = drawEffect.getVertexAttribIndexCount();
77 SkASSERT(numAttributes <= 2); 146 SkASSERT(numAttributes <= 2);
78 const int* attributeIndices = drawEffect.getVertexAttribIndices(); 147 const int* attributeIndices = drawEffect.getVertexAttribIndices();
79 for (int a = 0; a < numAttributes; ++a) { 148 for (int a = 0; a < numAttributes; ++a) {
80 EffectKey value = attributeIndices[a] << 3 * a; 149 EffectKey value = attributeIndices[a] << 3 * a;
81 SkASSERT(0 == (value & key)); // keys for each attribute ought not to ov erlap 150 SkASSERT(0 == (value & key)); // keys for each attribute ought not to ov erlap
82 key |= value; 151 key |= value;
83 } 152 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 return key; 206 return key;
138 } 207 }
139 208
140 GrGLProgramEffects::~GrGLProgramEffects() { 209 GrGLProgramEffects::~GrGLProgramEffects() {
141 int numEffects = fGLEffects.count(); 210 int numEffects = fGLEffects.count();
142 for (int e = 0; e < numEffects; ++e) { 211 for (int e = 0; e < numEffects; ++e) {
143 SkDELETE(fGLEffects[e]); 212 SkDELETE(fGLEffects[e]);
144 } 213 }
145 } 214 }
146 215
216 void GrGLProgramEffects::emitSamplers(GrGLShaderBuilder* builder,
217 const GrEffectRef& effect,
218 TextureSamplerArray* outSamplers) {
219 SkTArray<Sampler, true>& samplers = fSamplers.push_back();
220 int numTextures = effect->numTextures();
221 samplers.push_back_n(numTextures);
222 SkString name;
223 for (int t = 0; t < numTextures; ++t) {
224 name.printf("Sampler%d", t);
225 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ Visibility,
226 kSampler2D_GrSLType,
227 name.c_str());
228 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler,
229 (samplers[t].fUniform, effect->textureAccess(t))) ;
230 }
231 }
232
147 void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) { 233 void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) {
148 int numEffects = fGLEffects.count(); 234 int numEffects = fGLEffects.count();
149 SkASSERT(numEffects == fSamplers.count()); 235 SkASSERT(numEffects == fSamplers.count());
150 for (int e = 0; e < numEffects; ++e) { 236 for (int e = 0; e < numEffects; ++e) {
151 SkTArray<Sampler, true>& samplers = fSamplers[e]; 237 SkTArray<Sampler, true>& samplers = fSamplers[e];
152 int numSamplers = samplers.count(); 238 int numSamplers = samplers.count();
153 for (int s = 0; s < numSamplers; ++s) { 239 for (int s = 0; s < numSamplers; ++s) {
154 SkASSERT(samplers[s].fUniform.isValid()); 240 SkASSERT(samplers[s].fUniform.isValid());
155 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx); 241 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx);
156 samplers[s].fTextureUnit = (*texUnitIdx)++; 242 samplers[s].fTextureUnit = (*texUnitIdx)++;
157 } 243 }
158 } 244 }
159 } 245 }
160 246
161 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
162 const GrGLUniformManager& uniformManager,
163 const GrEffectStage* effectStages[]) {
164 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
175 void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& unifor mManager,
176 const GrDrawEffect& drawEffect,
177 int effectIdx) {
178 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())->coordTr ansform(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 == transform s[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() | coordC hangeMatrix.getType()));
196 SkASSERT(!coordTransform.reverseY());
197 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[S kMatrix::kMTransX];
198 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[S kMatrix::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::kMSkew Y]);
215 combined.set(SkMatrix::kMScaleY,
216 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScal eY]);
217 combined.set(SkMatrix::kMTransY,
218 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTran sY]);
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
232 void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffectRef& effect, i nt effectIdx) { 247 void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffectRef& effect, i nt effectIdx) {
233 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx]; 248 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
234 int numSamplers = samplers.count(); 249 int numSamplers = samplers.count();
235 SkASSERT(numSamplers == effect->numTextures()); 250 SkASSERT(numSamplers == effect->numTextures());
236 for (int s = 0; s < numSamplers; ++s) { 251 for (int s = 0; s < numSamplers; ++s) {
237 SkASSERT(samplers[s].fTextureUnit >= 0); 252 SkASSERT(samplers[s].fTextureUnit >= 0);
238 const GrTextureAccess& textureAccess = effect->textureAccess(s); 253 const GrTextureAccess& textureAccess = effect->textureAccess(s);
239 gpu->bindTexture(samplers[s].fTextureUnit, 254 gpu->bindTexture(samplers[s].fTextureUnit,
240 textureAccess.getParams(), 255 textureAccess.getParams(),
241 static_cast<GrGLTexture*>(textureAccess.getTexture())); 256 static_cast<GrGLTexture*>(textureAccess.getTexture()));
242 } 257 }
243 } 258 }
244 259
245 //////////////////////////////////////////////////////////////////////////////// 260 ////////////////////////////////////////////////////////////////////////////////
246 261
247 GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderB uilder* builder, 262 void GrGLVertexProgramEffects::emitEffect(GrGLFullShaderBuilder* builder,
248 int reserveCoun t) 263 const GrEffectStage& stage,
249 : fBuilder(builder) 264 EffectKey key,
250 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects, 265 const char* outColor,
251 (reserveCount, fBuilder->hasExplicitLocalCoords ()))) { 266 const char* inColor,
252 } 267 int stageIndex) {
253 268 GrDrawEffect drawEffect(stage, fHasExplicitLocalCoords);
254 void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
255 EffectKey key,
256 const char* outColor,
257 const char* inColor,
258 int stageIndex) {
259 SkASSERT(NULL != fProgramEffects.get());
260
261 GrDrawEffect drawEffect(stage, fProgramEffects->fHasExplicitLocalCoords);
262 const GrEffectRef& effect = *stage.getEffect(); 269 const GrEffectRef& effect = *stage.getEffect();
263 SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); 270 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
264 SkSTArray<4, TextureSampler> samplers(effect->numTextures()); 271 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
265 272
266 this->emitAttributes(stage); 273 this->emitAttributes(builder, stage);
267 this->emitTransforms(effect, key, &coords); 274 this->emitTransforms(builder, effect, key, &coords);
268 INHERITED::emitSamplers(fBuilder, fProgramEffects.get(), effect, &samplers); 275 this->emitSamplers(builder, effect, &samplers);
269 276
270 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); 277 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
271 fProgramEffects->fGLEffects.push_back(glEffect); 278 fGLEffects.push_back(glEffect);
272 279
273 // Enclose custom code in a block to avoid namespace conflicts 280 // Enclose custom code in a block to avoid namespace conflicts
274 SkString openBrace; 281 SkString openBrace;
275 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); 282 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
276 fBuilder->vsCodeAppend(openBrace.c_str()); 283 builder->vsCodeAppend(openBrace.c_str());
277 fBuilder->fsCodeAppend(openBrace.c_str()); 284 builder->fsCodeAppend(openBrace.c_str());
278 285
279 if (glEffect->isVertexEffect()) { 286 if (glEffect->isVertexEffect()) {
280 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect ); 287 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect );
281 vertexEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coo rds, samplers); 288 vertexEffect->emitCode(builder, drawEffect, key, outColor, inColor, coor ds, samplers);
282 } else { 289 } else {
283 glEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords, samplers); 290 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers);
284 } 291 }
285 292
286 fBuilder->vsCodeAppend("\t}\n"); 293 builder->vsCodeAppend("\t}\n");
287 fBuilder->fsCodeAppend("\t}\n"); 294 builder->fsCodeAppend("\t}\n");
288 } 295 }
289 296
290 void GrGLVertexProgramEffectsBuilder::emitAttributes(const GrEffectStage& stage) { 297 void GrGLVertexProgramEffects::emitAttributes(GrGLFullShaderBuilder* builder,
298 const GrEffectStage& stage) {
291 int numAttributes = stage.getVertexAttribIndexCount(); 299 int numAttributes = stage.getVertexAttribIndexCount();
292 const int* attributeIndices = stage.getVertexAttribIndices(); 300 const int* attributeIndices = stage.getVertexAttribIndices();
293 for (int a = 0; a < numAttributes; ++a) { 301 for (int a = 0; a < numAttributes; ++a) {
294 // TODO: Make addAttribute mangle the name. 302 // TODO: Make addAttribute mangle the name.
295 SkString attributeName("aAttr"); 303 SkString attributeName("aAttr");
296 attributeName.appendS32(attributeIndices[a]); 304 attributeName.appendS32(attributeIndices[a]);
297 fBuilder->addEffectAttribute(attributeIndices[a], 305 builder->addEffectAttribute(attributeIndices[a],
298 (*stage.getEffect())->vertexAttribType(a), 306 (*stage.getEffect())->vertexAttribType(a),
299 attributeName); 307 attributeName);
300 } 308 }
301 } 309 }
302 310
303 void GrGLVertexProgramEffectsBuilder::emitTransforms(const GrEffectRef& effect, 311 void GrGLVertexProgramEffects::emitTransforms(GrGLFullShaderBuilder* builder,
304 EffectKey effectKey, 312 const GrEffectRef& effect,
305 TransformedCoordsArray* out Coords) { 313 EffectKey effectKey,
306 typedef GrGLVertexProgramEffects::Transform Transform; 314 TransformedCoordsArray* outCoords) {
307 SkTArray<Transform, true>& transforms = fProgramEffects->fTransforms.push_ba ck(); 315 SkTArray<Transform, true>& transforms = fTransforms.push_back();
308 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey); 316 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
309 int numTransforms = effect->numTransforms(); 317 int numTransforms = effect->numTransforms();
310 transforms.push_back_n(numTransforms); 318 transforms.push_back_n(numTransforms);
311 for (int t = 0; t < numTransforms; t++) { 319 for (int t = 0; t < numTransforms; t++) {
312 EffectKey key = (totalKey >> (kTransformKeyBits * t)) & kTransformKeyMas k;
313 GrSLType varyingType = kVoid_GrSLType; 320 GrSLType varyingType = kVoid_GrSLType;
314 const char* uniName; 321 const char* uniName;
315 switch (key & kMatrixTypeKeyMask) { 322 switch (get_matrix_type(totalKey, t)) {
316 case kIdentity_MatrixType: 323 case kIdentity_MatrixType:
317 transforms[t].fType = kVoid_GrSLType; 324 transforms[t].fType = kVoid_GrSLType;
318 uniName = NULL; 325 uniName = NULL;
319 varyingType = kVec2f_GrSLType; 326 varyingType = kVec2f_GrSLType;
320 break; 327 break;
321 case kTrans_MatrixType: 328 case kTrans_MatrixType:
322 transforms[t].fType = kVec2f_GrSLType; 329 transforms[t].fType = kVec2f_GrSLType;
323 uniName = "StageTranslate"; 330 uniName = "StageTranslate";
324 varyingType = kVec2f_GrSLType; 331 varyingType = kVec2f_GrSLType;
325 break; 332 break;
(...skipping 10 matching lines...) Expand all
336 default: 343 default:
337 GrCrash("Unexpected key."); 344 GrCrash("Unexpected key.");
338 } 345 }
339 SkString suffixedUniName; 346 SkString suffixedUniName;
340 if (kVoid_GrSLType != transforms[t].fType) { 347 if (kVoid_GrSLType != transforms[t].fType) {
341 if (0 != t) { 348 if (0 != t) {
342 suffixedUniName.append(uniName); 349 suffixedUniName.append(uniName);
343 suffixedUniName.appendf("_%i", t); 350 suffixedUniName.appendf("_%i", t);
344 uniName = suffixedUniName.c_str(); 351 uniName = suffixedUniName.c_str();
345 } 352 }
346 transforms[t].fHandle = fBuilder->addUniform(GrGLShaderBuilder::kVer tex_Visibility, 353 transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVert ex_Visibility,
347 transforms[t].fType, 354 transforms[t].fType,
348 uniName, 355 uniName,
349 &uniName); 356 &uniName);
350 } 357 }
351 358
352 const char* varyingName = "MatrixCoord"; 359 const char* varyingName = "MatrixCoord";
353 SkString suffixedVaryingName; 360 SkString suffixedVaryingName;
354 if (0 != t) { 361 if (0 != t) {
355 suffixedVaryingName.append(varyingName); 362 suffixedVaryingName.append(varyingName);
356 suffixedVaryingName.appendf("_%i", t); 363 suffixedVaryingName.appendf("_%i", t);
357 varyingName = suffixedVaryingName.c_str(); 364 varyingName = suffixedVaryingName.c_str();
358 } 365 }
359 const char* vsVaryingName; 366 const char* vsVaryingName;
360 const char* fsVaryingName; 367 const char* fsVaryingName;
361 fBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryin gName); 368 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVarying Name);
362 369
363 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ? 370 const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords( totalKey, t) ?
364 fBuilder->positionAttribute() : 371 builder->positionAttribute() :
365 fBuilder->localCoordsAttribute(); 372 builder->localCoordsAttribute();
366 // varying = matrix * coords (logically) 373 // varying = matrix * coords (logically)
367 switch (transforms[t].fType) { 374 switch (transforms[t].fType) {
368 case kVoid_GrSLType: 375 case kVoid_GrSLType:
369 SkASSERT(kVec2f_GrSLType == varyingType); 376 SkASSERT(kVec2f_GrSLType == varyingType);
370 fBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c_ str()); 377 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c_s tr());
371 break; 378 break;
372 case kVec2f_GrSLType: 379 case kVec2f_GrSLType:
373 SkASSERT(kVec2f_GrSLType == varyingType); 380 SkASSERT(kVec2f_GrSLType == varyingType);
374 fBuilder->vsCodeAppendf("\t%s = %s + %s;\n", 381 builder->vsCodeAppendf("\t%s = %s + %s;\n",
375 vsVaryingName, uniName, coords.c_str()); 382 vsVaryingName, uniName, coords.c_str());
376 break; 383 break;
377 case kMat33f_GrSLType: { 384 case kMat33f_GrSLType: {
378 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == va ryingType); 385 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == va ryingType);
379 if (kVec2f_GrSLType == varyingType) { 386 if (kVec2f_GrSLType == varyingType) {
380 fBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", 387 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
381 vsVaryingName, uniName, coords.c_str ()); 388 vsVaryingName, uniName, coords.c_str( ));
382 } else { 389 } else {
383 fBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", 390 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
384 vsVaryingName, uniName, coords.c_str ()); 391 vsVaryingName, uniName, coords.c_str( ));
385 } 392 }
386 break; 393 break;
387 } 394 }
388 default: 395 default:
389 GrCrash("Unexpected uniform type."); 396 GrCrash("Unexpected uniform type.");
390 } 397 }
391 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (fsVaryingName, var yingType)); 398 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords,
392 } 399 (SkString(fsVaryingName), varyingType));
393 } 400 }
394 401 }
395 void GrGLProgramEffectsBuilder::emitSamplers(GrGLShaderBuilder* builder, 402
396 GrGLProgramEffects* programEffects, 403 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
397 const GrEffectRef& effect, 404 const GrGLUniformManager& uniformManager,
398 TextureSamplerArray* outSamplers) { 405 const GrEffectStage* effectStages[]) {
399 typedef GrGLProgramEffects::Sampler Sampler; 406 int numEffects = fGLEffects.count();
400 SkTArray<Sampler, true>& samplers = programEffects->fSamplers.push_back(); 407 SkASSERT(numEffects == fTransforms.count());
401 int numTextures = effect->numTextures(); 408 SkASSERT(numEffects == fSamplers.count());
402 samplers.push_back_n(numTextures); 409 for (int e = 0; e < numEffects; ++e) {
410 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords);
411 fGLEffects[e]->setData(uniformManager, drawEffect);
412 this->setTransformData(uniformManager, drawEffect, e);
413 this->bindTextures(gpu, *drawEffect.effect(), e);
414 }
415 }
416
417 void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& unifor mManager,
418 const GrDrawEffect& drawEffect,
419 int effectIdx) {
420 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
421 int numTransforms = transforms.count();
422 SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms());
423 for (int t = 0; t < numTransforms; ++t) {
424 SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transform s[t].fType));
425 switch (transforms[t].fType) {
426 case kVoid_GrSLType:
427 SkASSERT(get_transform_matrix(drawEffect, t).isIdentity());
428 return;
429 case kVec2f_GrSLType: {
430 GrGLfloat tx, ty;
431 get_transform_translation(drawEffect, t, &tx, &ty);
432 if (transforms[t].fCurrentValue.get(SkMatrix::kMTransX) != tx ||
433 transforms[t].fCurrentValue.get(SkMatrix::kMTransY) != ty) {
434 uniformManager.set2f(transforms[t].fHandle, tx, ty);
435 transforms[t].fCurrentValue.set(SkMatrix::kMTransX, tx);
436 transforms[t].fCurrentValue.set(SkMatrix::kMTransY, ty);
437 }
438 break;
439 }
440 case kMat33f_GrSLType: {
441 const SkMatrix& matrix = get_transform_matrix(drawEffect, t);
442 if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) {
443 uniformManager.setSkMatrix(transforms[t].fHandle, matrix);
444 transforms[t].fCurrentValue = matrix;
445 }
446 break;
447 }
448 default:
449 GrCrash("Unexpected uniform type.");
450 }
451 }
452 }
453
454 GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderB uilder* builder,
455 int reserveCoun t)
456 : fBuilder(builder)
457 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects,
458 (reserveCount, fBuilder->hasExplicitLocalCoords ()))) {
459 }
460
461 void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
462 GrGLProgramEffects::EffectKey k ey,
463 const char* outColor,
464 const char* inColor,
465 int stageIndex) {
466 SkASSERT(NULL != fProgramEffects.get());
467 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIn dex);
468 }
469
470 ////////////////////////////////////////////////////////////////////////////////
471
472 void GrGLTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder ,
473 const GrEffectStage& stage,
474 EffectKey key,
475 const char* outColor,
476 const char* inColor,
477 int stageIndex) {
478 GrDrawEffect drawEffect(stage, false);
479 const GrEffectRef& effect = *stage.getEffect();
480 SkSTArray<2, TransformedCoords> coords(effect->numTransforms());
481 SkSTArray<4, TextureSampler> samplers(effect->numTextures());
482
483 SkASSERT(0 == stage.getVertexAttribIndexCount());
484 this->setupTexGen(builder, effect, key, &coords);
485 this->emitSamplers(builder, effect, &samplers);
486
487 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect);
488 fGLEffects.push_back(glEffect);
489
490 // Enclose custom code in a block to avoid namespace conflicts
491 SkString openBrace;
492 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name());
493 builder->fsCodeAppend(openBrace.c_str());
494
495 SkASSERT(!glEffect->isVertexEffect());
496 glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samp lers);
497
498 builder->fsCodeAppend("\t}\n");
499 }
500
501 void GrGLTexGenProgramEffects::setupTexGen(GrGLFragmentOnlyShaderBuilder* builde r,
502 const GrEffectRef& effect,
503 EffectKey effectKey,
504 TransformedCoordsArray* outCoords) {
505 int numTransforms = effect->numTransforms();
506 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey);
507 int texCoordIndex = builder->addTexCoordSets(numTransforms);
508 SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex));
403 SkString name; 509 SkString name;
404 for (int t = 0; t < numTextures; ++t) { 510 for (int t = 0; t < numTransforms; ++t) {
405 name.printf("Sampler%d", t); 511 GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ?
406 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ Visibility, 512 kVec3f_GrSLType :
407 kSampler2D_GrSLType, 513 kVec2f_GrSLType;
408 name.c_str()); 514 name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex ++);
409 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler, 515 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type));
410 (samplers[t].fUniform, effect->textureAccess(t))) ; 516 }
411 } 517 }
412 } 518
519 void GrGLTexGenProgramEffects::setData(GrGpuGL* gpu,
520 const GrGLUniformManager& uniformManager,
521 const GrEffectStage* effectStages[]) {
522 int numEffects = fGLEffects.count();
523 SkASSERT(numEffects == fTransforms.count());
524 SkASSERT(numEffects == fSamplers.count());
525 for (int e = 0; e < numEffects; ++e) {
526 GrDrawEffect drawEffect(*effectStages[e], false);
527 fGLEffects[e]->setData(uniformManager, drawEffect);
528 this->setTexGenState(gpu, drawEffect, e);
529 this->bindTextures(gpu, *drawEffect.effect(), e);
530 }
531 }
532
533 void GrGLTexGenProgramEffects::setTexGenState(GrGpuGL* gpu,
534 const GrDrawEffect& drawEffect,
535 int effectIdx) {
536 EffectKey totalKey = fTransforms[effectIdx].fTransformKey;
537 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
538 int numTransforms = (*drawEffect.effect())->numTransforms();
539 for (int t = 0; t < numTransforms; ++t) {
540 switch (get_matrix_type(totalKey, t)) {
541 case kIdentity_MatrixType: {
542 SkASSERT(get_transform_matrix(drawEffect, t).isIdentity());
543 GrGLfloat identity[] = {1, 0, 0,
544 0, 1, 0};
545 gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents , identity);
546 break;
547 }
548 case kTrans_MatrixType: {
549 GrGLfloat tx, ty;
550 get_transform_translation(drawEffect, t, &tx, &ty);
551 GrGLfloat translate[] = {1, 0, tx,
552 0, 1, ty};
553 gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents , translate);
554 break;
555 }
556 case kNoPersp_MatrixType: {
557 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
558 gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents , transform);
559 break;
560 }
561 case kGeneral_MatrixType: {
562 const SkMatrix& transform = get_transform_matrix(drawEffect, t);
563 gpu->enableTexGen(texCoordIndex++, GrGpuGL::kSTR_TexGenComponent s, transform);
564 break;
565 }
566 default:
567 GrCrash("Unexpected matrixs type.");
568 }
569 }
570 }
571
572 GrGLTexGenProgramEffectsBuilder::GrGLTexGenProgramEffectsBuilder(
573 GrGLFragmentOnlyShaderBuilder* builder,
574 int reserveCount)
575 : fBuilder(builder)
576 , fProgramEffects(SkNEW_ARGS(GrGLTexGenProgramEffects, (reserveCount))) {
577 }
578
579 void GrGLTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage,
580 GrGLProgramEffects::EffectKey k ey,
581 const char* outColor,
582 const char* inColor,
583 int stageIndex) {
584 SkASSERT(NULL != fProgramEffects.get());
585 fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIn dex);
586 }
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