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

Side by Side Diff: src/gpu/GrDrawState.h

Issue 12531015: Adds local coords to GrEffect system. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 9 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/GrContext.cpp ('k') | src/gpu/GrDrawState.cpp » ('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 2011 Google Inc. 2 * Copyright 2011 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 #ifndef GrDrawState_DEFINED 8 #ifndef GrDrawState_DEFINED
9 #define GrDrawState_DEFINED 9 #define GrDrawState_DEFINED
10 10
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 250
251 /// @} 251 /// @}
252 252
253 /////////////////////////////////////////////////////////////////////////// 253 ///////////////////////////////////////////////////////////////////////////
254 /// @name Attribute Bindings 254 /// @name Attribute Bindings
255 //// 255 ////
256 256
257 /** 257 /**
258 * The vertex data used by the current program is represented as a bitfield 258 * The vertex data used by the current program is represented as a bitfield
259 * of flags. Programs always use positions and may also use texture 259 * of flags. Programs always use positions and may also use texture
260 * coordinates, per-vertex colors, per-vertex coverage and edge data. Each 260 * coordinates, per-vertex colors, per-vertex coverage and edge data. The
261 * stage can use the explicit texture coordinates as its input texture 261 * local coords accessible by effects may either come from positions or
262 * coordinates or it may use the positions as texture coordinates. 262 * be specified explicitly.
263 */ 263 */
264 264
265 /** 265 /**
266 * Generates a bit indicating that a texture stage uses texture coordinates
267 *
268 * @param stageIdx the stage that will use texture coordinates.
269 *
270 * @return the bit to add to a GrAttribBindings bitfield.
271 */
272 static int ExplicitTexCoordAttribBindingsBit(int stageIdx) {
273 GrAssert(stageIdx < kNumStages);
274 return (1 << stageIdx);
275 }
276
277 static bool StageBindsExplicitTexCoords(GrAttribBindings bindings, int stage Idx);
278
279 /**
280 * Additional Bits that can be specified in GrAttribBindings. 266 * Additional Bits that can be specified in GrAttribBindings.
281 */ 267 */
282 enum AttribBindingsBits { 268 enum AttribBindingsBits {
269 /** explicit local coords are provided (instead of using pre-view-matrix positions) */
270 kLocalCoords_AttribBindingsBit = 0x1,
283 /* program uses colors (GrColor) */ 271 /* program uses colors (GrColor) */
284 kColor_AttribBindingsBit = 1 << (kNumStages + 0), 272 kColor_AttribBindingsBit = 0x2,
285 /* program uses coverage (GrColor) 273 /* program uses coverage (GrColor)
286 */ 274 */
287 kCoverage_AttribBindingsBit = 1 << (kNumStages + 1), 275 kCoverage_AttribBindingsBit = 0x4,
288 /* program uses edge data. Distance to the edge is used to 276 /* program uses edge data. Distance to the edge is used to
289 * compute a coverage. See GrDrawState::setVertexEdgeType(). 277 * compute a coverage. See GrDrawState::setVertexEdgeType().
290 */ 278 */
291 kEdge_AttribBindingsBit = 1 << (kNumStages + 2), 279 kEdge_AttribBindingsBit = 0x8,
292 // for below assert 280 // for below assert
293 kDummyAttribBindingsBit, 281 kDummyAttribBindingsBit,
294 kHighAttribBindingsBit = kDummyAttribBindingsBit - 1 282 kHighAttribBindingsBit = kDummyAttribBindingsBit - 1
295 }; 283 };
296 // make sure we haven't exceeded the number of bits in GrAttribBindings. 284 // make sure we haven't exceeded the number of bits in GrAttribBindings.
297 GR_STATIC_ASSERT(kHighAttribBindingsBit < ((uint64_t)1 << 8*sizeof(GrAttribB indings))); 285 GR_STATIC_ASSERT(kHighAttribBindingsBit < ((uint64_t)1 << 8*sizeof(GrAttribB indings)));
298 286
299 enum AttribBindings { 287 enum AttribBindings {
300 kDefault_AttribBindings = 0 288 kDefault_AttribBindings = 0
301 }; 289 };
302 290
303 /** 291 /**
304 * Sets attribute bindings for next draw. 292 * Sets attribute bindings for next draw.
305 * 293 *
306 * @param bindings the attribute bindings to set. 294 * @param bindings the attribute bindings to set.
307 */ 295 */
308 void setAttribBindings(GrAttribBindings bindings) { fCommon.fAttribBindings = bindings; } 296 void setAttribBindings(GrAttribBindings bindings) { fCommon.fAttribBindings = bindings; }
309 297
310 GrAttribBindings getAttribBindings() const { return fCommon.fAttribBindings; } 298 GrAttribBindings getAttribBindings() const { return fCommon.fAttribBindings; }
311 299
312 //////////////////////////////////////////////////////////////////////////// 300 ////////////////////////////////////////////////////////////////////////////
313 // Helpers for picking apart attribute bindings 301 // Helpers for picking apart attribute bindings
314 302
315 /** 303 /**
316 * Helper function to determine if program uses explicit texture
317 * coordinates.
318 *
319 * @param bindings attribute bindings to query
320 *
321 * @return true if program uses texture coordinates,
322 * false otherwise.
323 */
324 static bool AttributesBindExplicitTexCoords(GrAttribBindings bindings);
325
326 /**
327 * Determines whether src alpha is guaranteed to be one for all src pixels 304 * Determines whether src alpha is guaranteed to be one for all src pixels
328 */ 305 */
329 bool srcAlphaWillBeOne(GrAttribBindings) const; 306 bool srcAlphaWillBeOne(GrAttribBindings) const;
330 307
331 /** 308 /**
332 * Determines whether the output coverage is guaranteed to be one for all pi xels hit by a draw. 309 * Determines whether the output coverage is guaranteed to be one for all pi xels hit by a draw.
333 */ 310 */
334 bool hasSolidCoverage(GrAttribBindings) const; 311 bool hasSolidCoverage(GrAttribBindings) const;
335 312
336 static void VertexAttributesUnitTest(); 313 static void VertexAttributesUnitTest();
(...skipping 12 matching lines...) Expand all
349 * attribute index if we're binding attributes in GL. 326 * attribute index if we're binding attributes in GL.
350 * 327 *
351 * Indices which do not have active attribute bindings will be ignored. 328 * Indices which do not have active attribute bindings will be ignored.
352 */ 329 */
353 330
354 enum AttribIndex { 331 enum AttribIndex {
355 kPosition_AttribIndex = 0, 332 kPosition_AttribIndex = 0,
356 kColor_AttribIndex, 333 kColor_AttribIndex,
357 kCoverage_AttribIndex, 334 kCoverage_AttribIndex,
358 kEdge_AttribIndex, 335 kEdge_AttribIndex,
359 kTexCoord_AttribIndex, 336 kLocalCoords_AttribIndex,
360 337
361 kLast_AttribIndex = kTexCoord_AttribIndex 338 kLast_AttribIndex = kLocalCoords_AttribIndex
362 }; 339 };
363 static const int kAttribIndexCount = kLast_AttribIndex + 1; 340 static const int kAttribIndexCount = kLast_AttribIndex + 1;
364 341
365 // these are used when vertex color and coverage isn't set 342 // these are used when vertex color and coverage isn't set
366 enum { 343 enum {
367 kColorOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt, 344 kColorOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt,
368 kCoverageOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt+1, 345 kCoverageOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt+1,
369 }; 346 };
370 347
371 //////////////////////////////////////////////////////////////////////////// 348 ////////////////////////////////////////////////////////////////////////////
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 * (r,g,b,a) = (alpha, alpha, alpha, alpha). 380 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
404 * 381 *
405 * @param alpha The alpha value to set as the color. 382 * @param alpha The alpha value to set as the color.
406 */ 383 */
407 void setAlpha(uint8_t a) { 384 void setAlpha(uint8_t a) {
408 this->setColor((a << 24) | (a << 16) | (a << 8) | a); 385 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
409 } 386 }
410 387
411 /** 388 /**
412 * Add a color filter that can be represented by a color and a mode. Applied 389 * Add a color filter that can be represented by a color and a mode. Applied
413 * after color-computing texture stages. 390 * after color-computing effect stages.
414 */ 391 */
415 void setColorFilter(GrColor c, SkXfermode::Mode mode) { 392 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
416 fCommon.fColorFilterColor = c; 393 fCommon.fColorFilterColor = c;
417 fCommon.fColorFilterMode = mode; 394 fCommon.fColorFilterMode = mode;
418 } 395 }
419 396
420 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; } 397 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
421 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMod e; } 398 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMod e; }
422 399
423 /** 400 /**
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 return effect; 467 return effect;
491 } 468 }
492 469
493 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect, 470 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect,
494 int attr0, int attr1 = -1) { 471 int attr0, int attr1 = -1) {
495 fStages[stageIdx].setEffect(effect, attr0, attr1); 472 fStages[stageIdx].setEffect(effect, attr0, attr1);
496 return effect; 473 return effect;
497 } 474 }
498 475
499 /** 476 /**
500 * Creates a GrSimpleTextureEffect. 477 * Creates a GrSimpleTextureEffect that uses local coords as texture coordin ates.
501 */ 478 */
502 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& m atrix) { 479 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& m atrix) {
503 GrAssert(!this->getStage(stageIdx).getEffect()); 480 GrAssert(!this->getStage(stageIdx).getEffect());
504 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); 481 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
505 this->setEffect(stageIdx, effect)->unref(); 482 this->setEffect(stageIdx, effect)->unref();
506 } 483 }
507 void createTextureEffect(int stageIdx, 484 void createTextureEffect(int stageIdx,
508 GrTexture* texture, 485 GrTexture* texture,
509 const SkMatrix& matrix, 486 const SkMatrix& matrix,
510 const GrTextureParams& params) { 487 const GrTextureParams& params) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 526
550 /** 527 /**
551 * Returns the current stage by index. 528 * Returns the current stage by index.
552 */ 529 */
553 const GrEffectStage& getStage(int stageIdx) const { 530 const GrEffectStage& getStage(int stageIdx) const {
554 GrAssert((unsigned)stageIdx < kNumStages); 531 GrAssert((unsigned)stageIdx < kNumStages);
555 return fStages[stageIdx]; 532 return fStages[stageIdx];
556 } 533 }
557 534
558 /** 535 /**
559 * Called when the source coord system is changing. preConcat gives the tran sformation from the 536 * Called when the source coord system is changing. This ensures that effect s will see the
560 * old coord system to the new coord system. 537 * correct local coordinates. oldToNew gives the transformation from the old coord system in
538 * which the geometry was specified to the new coordinate system from which it will be rendered.
561 */ 539 */
562 void preConcatStageMatrices(const SkMatrix& preConcat) { 540 void localCoordChange(const SkMatrix& oldToNew) {
563 this->preConcatStageMatrices(~0U, preConcat);
564 }
565 /**
566 * Version of above that applies the update matrix selectively to stages via a mask.
567 */
568 void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
569 for (int i = 0; i < kNumStages; ++i) { 541 for (int i = 0; i < kNumStages; ++i) {
570 if (((1 << i) & stageMask) && this->isStageEnabled(i)) { 542 if (this->isStageEnabled(i)) {
571 fStages[i].preConcatCoordChange(preConcat); 543 fStages[i].localCoordChange(oldToNew);
572 } 544 }
573 } 545 }
574 } 546 }
575 547
576 /**
577 * Called when the source coord system is changing. preConcatInverse is the inverse of the
578 * transformation from the old coord system to the new coord system. Returns false if the matrix
579 * cannot be inverted.
580 */
581 bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
582 SkMatrix inv;
583 bool computed = false;
584 for (int i = 0; i < kNumStages; ++i) {
585 if (this->isStageEnabled(i)) {
586 if (!computed && !preConcatInverse.invert(&inv)) {
587 return false;
588 } else {
589 computed = true;
590 }
591 fStages[i].preConcatCoordChange(preConcatInverse);
592 }
593 }
594 return true;
595 }
596
597 /// @} 548 /// @}
598 549
599 /////////////////////////////////////////////////////////////////////////// 550 ///////////////////////////////////////////////////////////////////////////
600 /// @name Coverage / Color Stages 551 /// @name Coverage / Color Stages
601 //// 552 ////
602 553
603 /** 554 /**
604 * A common pattern is to compute a color with the initial stages and then 555 * A common pattern is to compute a color with the initial stages and then
605 * modulate that color by a coverage value in later stage(s) (AA, mask- 556 * modulate that color by a coverage value in later stage(s) (AA, mask-
606 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be 557 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 //////////////////////////////////////////////////////////////////////////// 776 ////////////////////////////////////////////////////////////////////////////
826 777
827 /** 778 /**
828 * Preconcats the current view matrix and restores the previous view matrix in the destructor. 779 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
829 * Effect matrices are automatically adjusted to compensate. 780 * Effect matrices are automatically adjusted to compensate.
830 */ 781 */
831 class AutoViewMatrixRestore : public ::GrNoncopyable { 782 class AutoViewMatrixRestore : public ::GrNoncopyable {
832 public: 783 public:
833 AutoViewMatrixRestore() : fDrawState(NULL) {} 784 AutoViewMatrixRestore() : fDrawState(NULL) {}
834 785
835 AutoViewMatrixRestore(GrDrawState* ds, 786 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
836 const SkMatrix& preconcatMatrix,
837 uint32_t explicitCoordStageMask = 0) {
838 fDrawState = NULL; 787 fDrawState = NULL;
839 this->set(ds, preconcatMatrix, explicitCoordStageMask); 788 this->set(ds, preconcatMatrix);
840 } 789 }
841 790
842 ~AutoViewMatrixRestore() { this->restore(); } 791 ~AutoViewMatrixRestore() { this->restore(); }
843 792
844 /** 793 /**
845 * Can be called prior to destructor to restore the original matrix. 794 * Can be called prior to destructor to restore the original matrix.
846 */ 795 */
847 void restore(); 796 void restore();
848 797
849 void set(GrDrawState* drawState, 798 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
850 const SkMatrix& preconcatMatrix,
851 uint32_t explicitCoordStageMask = 0);
852 799
853 bool isSet() const { return NULL != fDrawState; } 800 bool isSet() const { return NULL != fDrawState; }
854 801
855 private: 802 private:
856 GrDrawState* fDrawState; 803 GrDrawState* fDrawState;
857 SkMatrix fViewMatrix; 804 SkMatrix fViewMatrix;
858 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum Stages]; 805 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum Stages];
859 uint32_t fRestoreMask; 806 uint32_t fRestoreMask;
860 }; 807 };
861 808
862 //////////////////////////////////////////////////////////////////////////// 809 ////////////////////////////////////////////////////////////////////////////
863 810
864 /** 811 /**
865 * This sets the view matrix to identity and adjusts stage matrices to compe nsate. The 812 * This sets the view matrix to identity and adjusts stage matrices to compe nsate. The
866 * destructor undoes the changes, restoring the view matrix that was set bef ore the 813 * destructor undoes the changes, restoring the view matrix that was set bef ore the
867 * constructor. It is similar to passing the inverse of the current view mat rix to 814 * constructor. It is similar to passing the inverse of the current view mat rix to
868 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary. 815 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
869 */ 816 */
870 class AutoDeviceCoordDraw : ::GrNoncopyable { 817 class AutoDeviceCoordDraw : ::GrNoncopyable {
871 public: 818 public:
872 AutoDeviceCoordDraw() : fDrawState(NULL) {} 819 AutoDeviceCoordDraw() : fDrawState(NULL) {}
873 /** 820 /**
874 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to 821 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
875 * positions, then we don't want to modify its matrix. The explicitCoord StageMask is used 822 * positions, then we don't want to modify its matrix. The explicitCoord StageMask is used
876 * to specify such stages. 823 * to specify such stages.
877 */ 824 */
878 AutoDeviceCoordDraw(GrDrawState* drawState, 825 AutoDeviceCoordDraw(GrDrawState* drawState) {
879 uint32_t explicitCoordStageMask = 0) {
880 fDrawState = NULL; 826 fDrawState = NULL;
881 this->set(drawState, explicitCoordStageMask); 827 this->set(drawState);
882 } 828 }
883 829
884 ~AutoDeviceCoordDraw() { this->restore(); } 830 ~AutoDeviceCoordDraw() { this->restore(); }
885 831
886 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0); 832 bool set(GrDrawState* drawState);
887 833
888 /** 834 /**
889 * Returns true if this object was successfully initialized on to a GrDr awState. It may 835 * Returns true if this object was successfully initialized on to a GrDr awState. It may
890 * return false because a non-default constructor or set() were never ca lled or because 836 * return false because a non-default constructor or set() were never ca lled or because
891 * the view matrix was not invertible. 837 * the view matrix was not invertible.
892 */ 838 */
893 bool succeeded() const { return NULL != fDrawState; } 839 bool succeeded() const { return NULL != fDrawState; }
894 840
895 /** 841 /**
896 * Returns the matrix that was set previously set on the drawState. This is only valid 842 * Returns the matrix that was set previously set on the drawState. This is only valid
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 } 1136 }
1191 1137
1192 bool operator ==(const GrDrawState& s) const { 1138 bool operator ==(const GrDrawState& s) const {
1193 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon ) { 1139 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon ) {
1194 return false; 1140 return false;
1195 } 1141 }
1196 if (fVertexAttribs != s.fVertexAttribs) { 1142 if (fVertexAttribs != s.fVertexAttribs) {
1197 return false; 1143 return false;
1198 } 1144 }
1199 for (int i = 0; i < kAttribIndexCount; ++i) { 1145 for (int i = 0; i < kAttribIndexCount; ++i) {
1200 if ((i == kPosition_AttribIndex || 1146 if ((i == kPosition_AttribIndex || s.fCommon.fAttribBindings & (1 << i)) &&
1201 s.fCommon.fAttribBindings & kAttribIndexMasks[i]) &&
1202 fAttribIndices[i] != s.fAttribIndices[i]) { 1147 fAttribIndices[i] != s.fAttribIndices[i]) {
1203 return false; 1148 return false;
1204 } 1149 }
1205 } 1150 }
1206 for (int i = 0; i < kNumStages; i++) { 1151 for (int i = 0; i < kNumStages; i++) {
1207 bool enabled = this->isStageEnabled(i); 1152 bool enabled = this->isStageEnabled(i);
1208 if (enabled != s.isStageEnabled(i)) { 1153 if (enabled != s.isStageEnabled(i)) {
1209 return false; 1154 return false;
1210 } 1155 }
1211 if (enabled && this->fStages[i] != s.fStages[i]) { 1156 if (enabled && this->fStages[i] != s.fStages[i]) {
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1365 int fAttribIndices[kAttribIndexCount]; 1310 int fAttribIndices[kAttribIndexCount];
1366 GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs; 1311 GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs;
1367 GrEffectStage fStages[kNumStages]; 1312 GrEffectStage fStages[kNumStages];
1368 1313
1369 typedef GrRefCnt INHERITED; 1314 typedef GrRefCnt INHERITED;
1370 }; 1315 };
1371 1316
1372 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); 1317 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1373 1318
1374 #endif 1319 #endif
OLDNEW
« no previous file with comments | « src/gpu/GrContext.cpp ('k') | src/gpu/GrDrawState.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698