Add constant coverage to GrDrawState
Review URL: http://codereview.appspot.com/5543052/
git-svn-id: http://skia.googlecode.com/svn/trunk@3054 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index b8417ba..c736f1e 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -70,6 +70,7 @@
// memset exceptions
fColor = 0xffffffff;
+ fCoverage = 0xffffffff;
fFirstCoverageStage = kNumStages;
fColorFilterMode = SkXfermode::kDst_Mode;
fSrcBlend = kOne_BlendCoeff;
@@ -78,9 +79,9 @@
// ensure values that will be memcmp'ed in == but not memset in reset()
// are tightly packed
- GrAssert(kMemsetSize + sizeof(fColor) + sizeof(fFirstCoverageStage) +
- sizeof(fColorFilterMode) + sizeof(fSrcBlend) +
- sizeof(fDstBlend) + sizeof(GrMatrix) ==
+ GrAssert(kMemsetSize + sizeof(fColor) + sizeof(fCoverage) +
+ sizeof(fFirstCoverageStage) + sizeof(fColorFilterMode) +
+ sizeof(fSrcBlend) + sizeof(fDstBlend) + sizeof(GrMatrix) ==
reinterpret_cast<intptr_t>(&fEdgeAANumEdges) -
reinterpret_cast<intptr_t>(this));
@@ -125,6 +126,33 @@
/// @}
///////////////////////////////////////////////////////////////////////////
+ /// @name Coverage
+ ////
+
+ /**
+ * Sets a constant fractional coverage to be applied to the draw. The
+ * initial value (after construction or reset()) is 0xff. The constant
+ * coverage is ignored when per-vertex coverage is provided.
+ */
+ void setCoverage(uint8_t coverage) {
+ fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
+ }
+
+ /**
+ * Version of above that specifies 4 channel per-vertex color. The value
+ * should be premultiplied.
+ */
+ void setCoverage4(GrColor coverage) {
+ fCoverage = coverage;
+ }
+
+ GrColor getCoverage() const {
+ return fCoverage;
+ }
+
+ /// @}
+
+ ///////////////////////////////////////////////////////////////////////////
/// @name Textures
////
@@ -753,6 +781,7 @@
// @{ Initialized to values other than zero
GrColor fColor;
+ GrColor fCoverage;
int fFirstCoverageStage;
SkXfermode::Mode fColorFilterMode;
GrBlendCoeff fSrcBlend;
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index be6bd0a..fa266d1 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -885,8 +885,10 @@
// When coeffs are (0,1) there is no reason to draw at all, unless
// stenciling is enabled. Having color writes disabled is effectively
- // (0,1).
- if ((kZero_BlendCoeff == *srcCoeff && dstCoeffIsOne)) {
+ // (0,1). The same applies when coverage is known to be 0.
+ if ((kZero_BlendCoeff == *srcCoeff && dstCoeffIsOne) ||
+ (!(layout & kCoverage_VertexLayoutBit) &&
+ 0 == drawState.getCoverage())) {
if (drawState.getStencil().doesWrite()) {
return kDisableBlend_BlendOptFlag |
kEmitTransBlack_BlendOptFlag;
@@ -895,8 +897,10 @@
}
}
- // check for coverage due to edge aa or coverage texture stage
+ // check for coverage due to constant coverage, per-vertex coverage,
+ // edge aa or coverage texture stage
bool hasCoverage = forceCoverage ||
+ 0xffffffff != drawState.getCoverage() ||
drawState.getNumAAEdges() > 0 ||
(layout & kCoverage_VertexLayoutBit) ||
(layout & kEdge_VertexLayoutBit);
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 2635ddd..5fc420d 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -239,8 +239,7 @@
enum VertexLayoutBits {
/* vertices have colors (GrColor) */
kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
- /* vertices have coverage (GrColor where all channels should have the
- * same value)
+ /* vertices have coverage (GrColor)
*/
kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
/* Use text vertices. (Pos and tex coords may be a different type for
diff --git a/src/gpu/GrGLProgram.cpp b/src/gpu/GrGLProgram.cpp
index d159f5a..7acdd29 100644
--- a/src/gpu/GrGLProgram.cpp
+++ b/src/gpu/GrGLProgram.cpp
@@ -92,6 +92,7 @@
#define COV_ATTR_NAME "aCoverage"
#define EDGE_ATTR_NAME "aEdge"
#define COL_UNI_NAME "uColor"
+#define COV_UNI_NAME "uCoverage"
#define EDGES_UNI_NAME "uEdges"
#define COL_FILTER_UNI_NAME "uColorFilter"
#define COL_MATRIX_UNI_NAME "uColorMatrix"
@@ -611,21 +612,37 @@
}
}
-void genPerVertexCoverage(ShaderCodeSegments* segments,
- GrStringBuilder* inCoverage) {
- segments->fVSAttrs.push_back().set(GrGLShaderVar::kFloat_Type,
+void genAttributeCoverage(ShaderCodeSegments* segments,
+ GrStringBuilder* inOutCoverage) {
+ segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
GrGLShaderVar::kAttribute_TypeModifier,
COV_ATTR_NAME);
const char *vsName, *fsName;
- append_varying(GrGLShaderVar::kFloat_Type, "Coverage",
+ append_varying(GrGLShaderVar::kVec4f_Type, "Coverage",
segments, &vsName, &fsName);
segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
- if (inCoverage->size()) {
- segments->fFSCode.appendf("\tfloat edgeAndAttrCov = %s * %s;\n",
- fsName, inCoverage->c_str());
- *inCoverage = "edgeAndAttrCov";
+ if (inOutCoverage->size()) {
+ segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
+ fsName, inOutCoverage->c_str());
+ *inOutCoverage = "attrCoverage";
} else {
- *inCoverage = fsName;
+ *inOutCoverage = fsName;
+ }
+}
+
+void genUniformCoverage(ShaderCodeSegments* segments,
+ GrGLProgram::CachedData* programData,
+ GrStringBuilder* inOutCoverage) {
+ segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
+ GrGLShaderVar::kUniform_TypeModifier,
+ COV_UNI_NAME);
+ programData->fUniLocations.fCoverageUni = kUseUniform;
+ if (inOutCoverage->size()) {
+ segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
+ COV_UNI_NAME, inOutCoverage->c_str());
+ *inOutCoverage = "uniCoverage";
+ } else {
+ *inOutCoverage = COV_UNI_NAME;
}
}
@@ -687,6 +704,7 @@
#endif
SkXfermode::Coeff colorCoeff, uniformCoeff;
+ bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
// The rest of transfer mode color filters have not been implemented
if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
GR_DEBUGCODE(bool success =)
@@ -699,6 +717,13 @@
uniformCoeff = SkXfermode::kZero_Coeff;
}
+ // no need to do the color filter / matrix at all if coverage is 0
+ if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
+ colorCoeff = SkXfermode::kZero_Coeff;
+ uniformCoeff = SkXfermode::kZero_Coeff;
+ applyColorMatrix = false;
+ }
+
// If we know the final color is going to be all zeros then we can
// simplify the color filter coeffecients. needComputedColor will then
// come out false below.
@@ -832,7 +857,7 @@
bool wroteFragColorZero = false;
if (SkXfermode::kZero_Coeff == uniformCoeff &&
SkXfermode::kZero_Coeff == colorCoeff &&
- !fProgramDesc.fColorMatrixEnabled) {
+ !applyColorMatrix) {
segments.fFSCode.appendf("\t%s = %s;\n",
fsColorOutput,
all_zeros_vec(4));
@@ -844,7 +869,7 @@
colorCoeff, color);
inColor = "filteredColor";
}
- if (fProgramDesc.fColorMatrixEnabled) {
+ if (applyColorMatrix) {
segments.fFSUnis.push_back().set(GrGLShaderVar::kMat44f_Type,
GrGLShaderVar::kUniform_TypeModifier,
COL_MATRIX_UNI_NAME);
@@ -872,9 +897,20 @@
// get edge AA coverage and use it as inCoverage to first coverage stage
this->genEdgeCoverage(gl, layout, programData, &inCoverage, &segments);
- // include explicit per-vertex coverage if we have it
- if (GrDrawTarget::kCoverage_VertexLayoutBit & layout) {
- genPerVertexCoverage(&segments, &inCoverage);
+ switch (fProgramDesc.fCoverageInput) {
+ case ProgramDesc::kSolidWhite_ColorInput:
+ // empty string implies solid white
+ break;
+ case ProgramDesc::kTransBlack_ColorInput:
+ GrCrash("We should have already written 0 as the frag output.");
+ case ProgramDesc::kAttribute_ColorInput:
+ genAttributeCoverage(&segments, &inCoverage);
+ break;
+ case ProgramDesc::kUniform_ColorInput:
+ genUniformCoverage(&segments, programData, &inCoverage);
+ break;
+ default:
+ GrCrash("Unexpected input coverage.");
}
GrStringBuilder outCoverage;
@@ -1288,6 +1324,11 @@
GR_GL_CALL_RET(gl, programData->fUniLocations.fColorMatrixVecUni,
GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
}
+ if (kUseUniform == programData->fUniLocations.fCoverageUni) {
+ GR_GL_CALL_RET(gl, programData->fUniLocations.fCoverageUni,
+ GetUniformLocation(progID, COV_UNI_NAME));
+ GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
+ }
if (kUseUniform == programData->fUniLocations.fEdgesUni) {
GR_GL_CALL_RET(gl, programData->fUniLocations.fEdgesUni,
diff --git a/src/gpu/GrGLProgram.h b/src/gpu/GrGLProgram.h
index b4ad4af..4d80f50 100644
--- a/src/gpu/GrGLProgram.h
+++ b/src/gpu/GrGLProgram.h
@@ -211,6 +211,7 @@
#endif
uint8_t fColorInput; // casts to enum ColorInput
+ uint8_t fCoverageInput; // casts to enum CoverageInput
uint8_t fOutputPM; // cases to enum OutputPM
uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
int8_t fFirstCoverageStage;
@@ -261,6 +262,7 @@
struct UniLocations {
GrGLint fViewMatrixUni;
GrGLint fColorUni;
+ GrGLint fCoverageUni;
GrGLint fEdgesUni;
GrGLint fColorFilterUni;
GrGLint fColorMatrixUni;
@@ -269,6 +271,7 @@
void reset() {
fViewMatrixUni = kUnusedUniform;
fColorUni = kUnusedUniform;
+ fCoverageUni = kUnusedUniform;
fEdgesUni = kUnusedUniform;
fColorFilterUni = kUnusedUniform;
fColorMatrixUni = kUnusedUniform;
@@ -306,6 +309,7 @@
// these reflect the current values of uniforms
// (GL uniform values travel with program)
GrColor fColor;
+ GrColor fCoverage;
GrColor fColorFilterColor;
GrMatrix fTextureMatrices[GrDrawState::kNumStages];
// width and height used for normalized texel size
diff --git a/src/gpu/GrGpuGLShaders.cpp b/src/gpu/GrGpuGLShaders.cpp
index 251fc41..b8e0999a 100644
--- a/src/gpu/GrGpuGLShaders.cpp
+++ b/src/gpu/GrGpuGLShaders.cpp
@@ -198,6 +198,7 @@
pdesc.fVertexLayout = 0;
pdesc.fEmitsPointSize = random.nextF() > .5f;
pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
+ pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
@@ -653,7 +654,8 @@
switch (desc.fColorInput) {
case ProgramDesc::kAttribute_ColorInput:
if (fHWDrawState.getColor() != color) {
- // OpenGL ES only supports the float varities of glVertexAttrib
+ // OpenGL ES only supports the float varieties of
+ // glVertexAttrib
float c[] = GR_COLOR_TO_VEC4(color);
GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
c));
@@ -662,7 +664,8 @@
break;
case ProgramDesc::kUniform_ColorInput:
if (fProgramData->fColor != color) {
- // OpenGL ES only supports the float varities of glVertexAttrib
+ // OpenGL ES doesn't support unsigned byte varieties of
+ // glUniform
float c[] = GR_COLOR_TO_VEC4(color);
GrAssert(GrGLProgram::kUnusedUniform !=
fProgramData->fUniLocations.fColorUni);
@@ -688,6 +691,47 @@
}
}
+void GrGpuGLShaders::flushCoverage(GrColor coverage) {
+ const ProgramDesc& desc = fCurrentProgram.getDesc();
+ const GrDrawState& drawState = this->getDrawState();
+
+
+ if (this->getGeomSrc().fVertexLayout & kCoverage_VertexLayoutBit) {
+ // coverage will be specified per-vertex as an attribute
+ // invalidate the const vertex attrib coverage
+ fHWDrawState.setCoverage4(GrColor_ILLEGAL);
+ } else {
+ switch (desc.fCoverageInput) {
+ case ProgramDesc::kAttribute_ColorInput:
+ if (fHWDrawState.getCoverage() != coverage) {
+ // OpenGL ES only supports the float varieties of
+ // glVertexAttrib
+ float c[] = GR_COLOR_TO_VEC4(coverage);
+ GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
+ c));
+ fHWDrawState.setCoverage(coverage);
+ }
+ break;
+ case ProgramDesc::kUniform_ColorInput:
+ if (fProgramData->fCoverage != coverage) {
+ // OpenGL ES doesn't support unsigned byte varieties of
+ // glUniform
+ float c[] = GR_COLOR_TO_VEC4(coverage);
+ GrAssert(GrGLProgram::kUnusedUniform !=
+ fProgramData->fUniLocations.fCoverageUni);
+ GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
+ 1, c));
+ fProgramData->fCoverage = coverage;
+ }
+ break;
+ case ProgramDesc::kSolidWhite_ColorInput:
+ case ProgramDesc::kTransBlack_ColorInput:
+ break;
+ default:
+ GrCrash("Unknown coverage type.");
+ }
+ }
+}
bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
if (!flushGLStateCommon(type)) {
@@ -726,14 +770,19 @@
this->flushBlend(type, srcCoeff, dstCoeff);
GrColor color;
+ GrColor coverage;
if (blendOpts & kEmitTransBlack_BlendOptFlag) {
color = 0;
+ coverage = 0;
} else if (blendOpts & kEmitCoverage_BlendOptFlag) {
color = 0xffffffff;
+ coverage = drawState.getCoverage();
} else {
color = drawState.getColor();
+ coverage = drawState.getCoverage();
}
this->flushColor(color);
+ this->flushCoverage(coverage);
this->flushViewMatrix();
@@ -862,15 +911,14 @@
}
if (newCoverageOffset > 0) {
- // bind a single channel, they should all have the same value.
GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
int idx = GrGLProgram::CoverageAttributeIdx();
if (oldCoverageOffset <= 0) {
GL_CALL(EnableVertexAttribArray(idx));
- GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
+ GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
true, newStride, coverageOffset));
} else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
- GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
+ GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
true, newStride, coverageOffset));
}
} else if (oldCoverageOffset > 0) {
@@ -922,10 +970,14 @@
bool requiresAttributeColors =
!skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
- // fColorInput records how colors are specified for the program. Strip
- // the bit from the layout to avoid false negatives when searching for an
- // existing program in the cache.
- desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
+ bool requiresAttributeCoverage =
+ !skipCoverage && SkToBool(desc.fVertexLayout &
+ kCoverage_VertexLayoutBit);
+
+ // fColorInput/fCoverageInput records how colors are specified for the.
+ // program. So we strip the bits from the layout to avoid false negatives
+ // when searching for an existing program in the cache.
+ desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
desc.fColorFilterXfermode = skipColor ?
SkXfermode::kDst_Mode :
@@ -953,6 +1005,19 @@
} else {
desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
}
+
+ bool covIsSolidWhite = !requiresAttributeCoverage &&
+ 0xffffffff == drawState.getCoverage();
+
+ if (skipCoverage) {
+ desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
+ } else if (covIsSolidWhite) {
+ desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
+ } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
+ desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
+ } else {
+ desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
+ }
desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
@@ -1101,7 +1166,7 @@
if (!hasCoverage) {
hasCoverage =
desc.fEdgeAANumEdges ||
- (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) ||
+ requiresAttributeCoverage ||
(desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
}
diff --git a/src/gpu/GrGpuGLShaders.h b/src/gpu/GrGpuGLShaders.h
index 4b972b5..d875fe9 100644
--- a/src/gpu/GrGpuGLShaders.h
+++ b/src/gpu/GrGpuGLShaders.h
@@ -57,9 +57,12 @@
// sets the texture domain uniform for currently bound program
void flushTextureDomain(int stage);
- // sets the color specified by GrDrawTarget::setColor()
+ // sets the color specified by GrDrawState::setColor()
void flushColor(GrColor color);
+ // sets the color specified by GrDrawState::setCoverage()
+ void flushCoverage(GrColor color);
+
// sets the MVP matrix uniform for currently bound program
void flushViewMatrix();