blob: c8218d7a9020a617d2cc4ffae75dc497d885916f [file] [log] [blame]
egdaniel3658f382014-09-15 07:01:59 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrOptDrawState.h"
9
10#include "GrDrawState.h"
egdanielc0648242014-09-22 13:17:02 -070011#include "GrDrawTargetCaps.h"
egdaniel3658f382014-09-15 07:01:59 -070012
egdaniel170f90b2014-09-16 12:54:40 -070013GrOptDrawState::GrOptDrawState(const GrDrawState& drawState,
14 BlendOptFlags blendOptFlags,
15 GrBlendCoeff optSrcCoeff,
egdanielc0648242014-09-22 13:17:02 -070016 GrBlendCoeff optDstCoeff,
egdaniel89af44a2014-09-26 06:15:04 -070017 const GrDrawTargetCaps& caps) {
bsalomonbcf0a522014-10-08 08:40:09 -070018 fRenderTarget.set(SkSafeRef(drawState.getRenderTarget()), kWrite_GrIOType);
egdaniel3658f382014-09-15 07:01:59 -070019 fColor = drawState.getColor();
20 fCoverage = drawState.getCoverage();
21 fViewMatrix = drawState.getViewMatrix();
22 fBlendConstant = drawState.getBlendConstant();
23 fFlagBits = drawState.getFlagBits();
24 fVAPtr = drawState.getVertexAttribs();
25 fVACount = drawState.getVertexAttribCount();
26 fVAStride = drawState.getVertexStride();
27 fStencilSettings = drawState.getStencil();
egdaniel89af44a2014-09-26 06:15:04 -070028 fDrawFace = (DrawFace)drawState.getDrawFace();
egdaniel170f90b2014-09-16 12:54:40 -070029 fBlendOptFlags = blendOptFlags;
30 fSrcBlend = optSrcCoeff;
31 fDstBlend = optDstCoeff;
egdaniel3658f382014-09-15 07:01:59 -070032
33 memcpy(fFixedFunctionVertexAttribIndices,
egdaniel89af44a2014-09-26 06:15:04 -070034 drawState.getFixedFunctionVertexAttribIndices(),
35 sizeof(fFixedFunctionVertexAttribIndices));
egdaniel3658f382014-09-15 07:01:59 -070036
37 fInputColorIsUsed = true;
38 fInputCoverageIsUsed = true;
39
egdaniel9cf45bf2014-10-08 06:49:10 -070040 int firstColorStageIdx = 0;
41 int firstCoverageStageIdx = 0;
42 bool separateCoverageFromColor;
43
44 uint8_t fixedFunctionVAToRemove = 0;
45
46 this->computeEffectiveColorStages(drawState, &firstColorStageIdx, &fixedFunctionVAToRemove);
47 this->computeEffectiveCoverageStages(drawState, &firstCoverageStageIdx);
48 this->adjustFromBlendOpts(drawState, &firstColorStageIdx, &firstCoverageStageIdx,
49 &fixedFunctionVAToRemove);
50 // Should not be setting any more FFVA to be removed at this point
51 this->removeFixedFunctionVertexAttribs(fixedFunctionVAToRemove);
52 this->getStageStats(drawState, firstColorStageIdx, firstCoverageStageIdx);
53 this->setOutputStateInfo(drawState, caps, firstCoverageStageIdx, &separateCoverageFromColor);
54
55 // Copy GeometryProcesssor from DS or ODS
egdaniel3658f382014-09-15 07:01:59 -070056 if (drawState.hasGeometryProcessor()) {
joshualittb0a8a372014-09-23 09:50:21 -070057 fGeometryProcessor.reset(SkNEW_ARGS(GrGeometryStage, (*drawState.getGeometryProcessor())));
egdaniel3658f382014-09-15 07:01:59 -070058 } else {
59 fGeometryProcessor.reset(NULL);
60 }
61
egdaniel9cf45bf2014-10-08 06:49:10 -070062 // Copy Color Stages from DS to ODS
63 if (firstColorStageIdx < drawState.numColorStages()) {
64 fColorStages.reset(&drawState.getColorStage(firstColorStageIdx),
65 drawState.numColorStages() - firstColorStageIdx);
66 } else {
67 fColorStages.reset();
68 }
69
70 // Copy Coverage Stages from DS to ODS
egdaniel47326ab2014-10-08 07:39:19 -070071 if (firstCoverageStageIdx < drawState.numCoverageStages() && separateCoverageFromColor) {
egdaniel9cf45bf2014-10-08 06:49:10 -070072 fCoverageStages.reset(&drawState.getCoverageStage(firstCoverageStageIdx),
73 drawState.numCoverageStages() - firstCoverageStageIdx);
74 } else {
75 fCoverageStages.reset();
egdaniel47326ab2014-10-08 07:39:19 -070076 if (firstCoverageStageIdx < drawState.numCoverageStages()) {
egdaniel9cf45bf2014-10-08 06:49:10 -070077 // TODO: Once we have flag to know if we only multiply on stages, only push coverage
78 // into color stages if everything is multiply
79 fColorStages.push_back_n(drawState.numCoverageStages() - firstCoverageStageIdx,
80 &drawState.getCoverageStage(firstCoverageStageIdx));
81 }
82 }
egdaniel3658f382014-09-15 07:01:59 -070083};
84
egdanielb109ac22014-10-07 06:45:44 -070085GrOptDrawState* GrOptDrawState::Create(const GrDrawState& drawState, const GrDrawTargetCaps& caps,
86 GrGpu::DrawType drawType) {
87 if (NULL == drawState.fCachedOptState || caps.getUniqueID() != drawState.fCachedCapsID) {
88 GrBlendCoeff srcCoeff;
89 GrBlendCoeff dstCoeff;
90 BlendOptFlags blendFlags = (BlendOptFlags) drawState.getBlendOpts(false,
91 &srcCoeff,
92 &dstCoeff);
93
94 // If our blend coeffs are set to 0,1 we know we will not end up drawing unless we are
95 // stenciling. When path rendering the stencil settings are not always set on the draw state
96 // so we must check the draw type. In cases where we will skip drawing we simply return a
97 // null GrOptDrawState.
98 if (kZero_GrBlendCoeff == srcCoeff && kOne_GrBlendCoeff == dstCoeff &&
99 !drawState.getStencil().doesWrite() && GrGpu::kStencilPath_DrawType != drawType) {
100 return NULL;
101 }
102
103 drawState.fCachedOptState = SkNEW_ARGS(GrOptDrawState, (drawState, blendFlags, srcCoeff,
104 dstCoeff, caps));
105 drawState.fCachedCapsID = caps.getUniqueID();
106 } else {
107#ifdef SK_DEBUG
108 GrBlendCoeff srcCoeff;
109 GrBlendCoeff dstCoeff;
110 BlendOptFlags blendFlags = (BlendOptFlags) drawState.getBlendOpts(false,
111 &srcCoeff,
112 &dstCoeff);
113 SkASSERT(GrOptDrawState(drawState, blendFlags, srcCoeff, dstCoeff, caps) ==
114 *drawState.fCachedOptState);
115#endif
116 }
117 drawState.fCachedOptState->ref();
118 return drawState.fCachedOptState;
119}
120
egdaniel9cf45bf2014-10-08 06:49:10 -0700121void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds,
122 const GrDrawTargetCaps& caps,
123 int firstCoverageStageIdx,
124 bool* separateCoverageFromColor) {
egdanielc0648242014-09-22 13:17:02 -0700125 // Set this default and then possibly change our mind if there is coverage.
126 fPrimaryOutputType = kModulate_PrimaryOutputType;
127 fSecondaryOutputType = kNone_SecondaryOutputType;
128
129 // If we do have coverage determine whether it matters.
egdaniel9cf45bf2014-10-08 06:49:10 -0700130 *separateCoverageFromColor = this->hasGeometryProcessor();
egdanielc0648242014-09-22 13:17:02 -0700131 if (!this->isCoverageDrawing() &&
egdaniel9cf45bf2014-10-08 06:49:10 -0700132 (ds.numCoverageStages() - firstCoverageStageIdx > 0 ||
133 ds.hasGeometryProcessor() ||
egdanielc0648242014-09-22 13:17:02 -0700134 this->hasCoverageVertexAttribute())) {
135
136 if (caps.dualSourceBlendingSupport()) {
137 if (kZero_GrBlendCoeff == fDstBlend) {
138 // write the coverage value to second color
139 fSecondaryOutputType = kCoverage_SecondaryOutputType;
egdaniel9cf45bf2014-10-08 06:49:10 -0700140 *separateCoverageFromColor = true;
egdanielc0648242014-09-22 13:17:02 -0700141 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
142 } else if (kSA_GrBlendCoeff == fDstBlend) {
143 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
144 fSecondaryOutputType = kCoverageISA_SecondaryOutputType;
egdaniel9cf45bf2014-10-08 06:49:10 -0700145 *separateCoverageFromColor = true;
egdanielc0648242014-09-22 13:17:02 -0700146 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
147 } else if (kSC_GrBlendCoeff == fDstBlend) {
148 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
149 fSecondaryOutputType = kCoverageISC_SecondaryOutputType;
egdaniel9cf45bf2014-10-08 06:49:10 -0700150 *separateCoverageFromColor = true;
egdanielc0648242014-09-22 13:17:02 -0700151 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
152 }
153 } else if (fReadsDst &&
154 kOne_GrBlendCoeff == fSrcBlend &&
155 kZero_GrBlendCoeff == fDstBlend) {
156 fPrimaryOutputType = kCombineWithDst_PrimaryOutputType;
egdaniel9cf45bf2014-10-08 06:49:10 -0700157 *separateCoverageFromColor = true;
egdanielc0648242014-09-22 13:17:02 -0700158 }
159 }
egdanielc0648242014-09-22 13:17:02 -0700160}
161
egdaniel9cf45bf2014-10-08 06:49:10 -0700162void GrOptDrawState::adjustFromBlendOpts(const GrDrawState& ds,
163 int* firstColorStageIdx,
164 int* firstCoverageStageIdx,
165 uint8_t* fixedFunctionVAToRemove) {
egdaniel170f90b2014-09-16 12:54:40 -0700166 switch (fBlendOptFlags) {
167 case kNone_BlendOpt:
168 case kSkipDraw_BlendOptFlag:
169 break;
170 case kCoverageAsAlpha_BlendOptFlag:
171 fFlagBits |= kCoverageDrawing_StateBit;
172 break;
173 case kEmitCoverage_BlendOptFlag:
174 fColor = 0xffffffff;
175 fInputColorIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700176 *firstColorStageIdx = ds.numColorStages();
177 *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding;
egdaniel170f90b2014-09-16 12:54:40 -0700178 break;
179 case kEmitTransBlack_BlendOptFlag:
180 fColor = 0;
181 fCoverage = 0xff;
182 fInputColorIsUsed = true;
183 fInputCoverageIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700184 *firstColorStageIdx = ds.numColorStages();
185 *firstCoverageStageIdx = ds.numCoverageStages();
186 *fixedFunctionVAToRemove |= (0x1 << kColor_GrVertexAttribBinding |
187 0x1 << kCoverage_GrVertexAttribBinding);
egdaniel170f90b2014-09-16 12:54:40 -0700188 break;
189 default:
190 SkFAIL("Unknown BlendOptFlag");
egdaniel170f90b2014-09-16 12:54:40 -0700191 }
192}
193
egdaniel3658f382014-09-15 07:01:59 -0700194void GrOptDrawState::removeFixedFunctionVertexAttribs(uint8_t removeVAFlag) {
195 int numToRemove = 0;
196 uint8_t maskCheck = 0x1;
197 // Count the number of vertex attributes that we will actually remove
198 for (int i = 0; i < kGrFixedFunctionVertexAttribBindingCnt; ++i) {
199 if ((maskCheck & removeVAFlag) && -1 != fFixedFunctionVertexAttribIndices[i]) {
200 ++numToRemove;
201 }
202 maskCheck <<= 1;
203 }
egdaniel170f90b2014-09-16 12:54:40 -0700204
egdaniel3658f382014-09-15 07:01:59 -0700205 fOptVA.reset(fVACount - numToRemove);
206
207 GrVertexAttrib* dst = fOptVA.get();
208 const GrVertexAttrib* src = fVAPtr;
209
210 for (int i = 0, newIdx = 0; i < fVACount; ++i, ++src) {
211 const GrVertexAttrib& currAttrib = *src;
212 if (currAttrib.fBinding < kGrFixedFunctionVertexAttribBindingCnt) {
213 uint8_t maskCheck = 0x1 << currAttrib.fBinding;
214 if (maskCheck & removeVAFlag) {
215 SkASSERT(-1 != fFixedFunctionVertexAttribIndices[currAttrib.fBinding]);
216 fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = -1;
217 continue;
218 }
egdaniel170f90b2014-09-16 12:54:40 -0700219 fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = newIdx;
egdaniel3658f382014-09-15 07:01:59 -0700220 }
221 memcpy(dst, src, sizeof(GrVertexAttrib));
egdaniel3658f382014-09-15 07:01:59 -0700222 ++newIdx;
223 ++dst;
224 }
225 fVACount -= numToRemove;
226 fVAPtr = fOptVA.get();
227}
228
egdaniel9cf45bf2014-10-08 06:49:10 -0700229void GrOptDrawState::computeEffectiveColorStages(const GrDrawState& ds, int* firstColorStageIdx,
230 uint8_t* fixedFunctionVAToRemove) {
egdaniel3658f382014-09-15 07:01:59 -0700231 // Set up color and flags for ConstantColorComponent checks
egdaniel1a8ecdf2014-10-03 06:24:12 -0700232 GrProcessor::InvariantOutput inout;
233 inout.fIsSingleComponent = false;
egdaniel3658f382014-09-15 07:01:59 -0700234 if (!this->hasColorVertexAttribute()) {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700235 inout.fColor = ds.getColor();
236 inout.fValidFlags = kRGBA_GrColorComponentFlags;
egdaniel3658f382014-09-15 07:01:59 -0700237 } else {
238 if (ds.vertexColorsAreOpaque()) {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700239 inout.fColor = 0xFF << GrColor_SHIFT_A;
240 inout.fValidFlags = kA_GrColorComponentFlag;
egdaniel3658f382014-09-15 07:01:59 -0700241 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700242 inout.fValidFlags = 0;
243 // not strictly necessary but we get false alarms from tools about uninit.
244 inout.fColor = 0;
egdaniel3658f382014-09-15 07:01:59 -0700245 }
246 }
247
248 for (int i = 0; i < ds.numColorStages(); ++i) {
joshualitt47bb3822014-10-07 16:43:25 -0700249 const GrFragmentProcessor* fp = ds.getColorStage(i).getProcessor();
joshualittb0a8a372014-09-23 09:50:21 -0700250 if (!fp->willUseInputColor()) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700251 *firstColorStageIdx = i;
egdaniel3658f382014-09-15 07:01:59 -0700252 fInputColorIsUsed = false;
253 }
egdaniel1a8ecdf2014-10-03 06:24:12 -0700254 fp->computeInvariantOutput(&inout);
255 if (kRGBA_GrColorComponentFlags == inout.fValidFlags) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700256 *firstColorStageIdx = i + 1;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700257 fColor = inout.fColor;
egdaniel3658f382014-09-15 07:01:59 -0700258 fInputColorIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700259 *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding;
egdaniel3658f382014-09-15 07:01:59 -0700260 }
261 }
egdaniel3658f382014-09-15 07:01:59 -0700262}
263
egdaniel9cf45bf2014-10-08 06:49:10 -0700264void GrOptDrawState::computeEffectiveCoverageStages(const GrDrawState& ds,
265 int* firstCoverageStageIdx) {
egdaniel3658f382014-09-15 07:01:59 -0700266 // We do not try to optimize out constantColor coverage effects here. It is extremely rare
267 // to have a coverage effect that returns a constant value for all four channels. Thus we
268 // save having to make extra virtual calls by not checking for it.
269
270 // Don't do any optimizations on coverage stages. It should not be the case where we do not use
271 // input coverage in an effect
272#ifdef OptCoverageStages
273 for (int i = 0; i < ds.numCoverageStages(); ++i) {
joshualittb0a8a372014-09-23 09:50:21 -0700274 const GrProcessor* processor = ds.getCoverageStage(i).getProcessor();
275 if (!processor->willUseInputColor()) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700276 *firstCoverageStageIdx = i;
egdaniel3658f382014-09-15 07:01:59 -0700277 fInputCoverageIsUsed = false;
278 }
279 }
280#endif
egdaniel3658f382014-09-15 07:01:59 -0700281}
282
joshualittb0a8a372014-09-23 09:50:21 -0700283static void get_stage_stats(const GrFragmentStage& stage, bool* readsDst, bool* readsFragPosition) {
joshualitt47bb3822014-10-07 16:43:25 -0700284 if (stage.getProcessor()->willReadDstColor()) {
egdaniela7dc0a82014-09-17 08:25:05 -0700285 *readsDst = true;
286 }
joshualitt47bb3822014-10-07 16:43:25 -0700287 if (stage.getProcessor()->willReadFragmentPosition()) {
egdaniela7dc0a82014-09-17 08:25:05 -0700288 *readsFragPosition = true;
289 }
290}
joshualittb0a8a372014-09-23 09:50:21 -0700291
egdaniel9cf45bf2014-10-08 06:49:10 -0700292void GrOptDrawState::getStageStats(const GrDrawState& ds, int firstColorStageIdx,
293 int firstCoverageStageIdx) {
egdaniela7dc0a82014-09-17 08:25:05 -0700294 // We will need a local coord attrib if there is one currently set on the optState and we are
295 // actually generating some effect code
egdaniel9cf45bf2014-10-08 06:49:10 -0700296 fRequiresLocalCoordAttrib = this->hasLocalCoordAttribute() &&
297 ds.numTotalStages() - firstColorStageIdx - firstCoverageStageIdx > 0;
egdaniela7dc0a82014-09-17 08:25:05 -0700298
egdaniela7dc0a82014-09-17 08:25:05 -0700299 fReadsDst = false;
300 fReadsFragPosition = false;
301
egdaniel9cf45bf2014-10-08 06:49:10 -0700302 for (int s = firstColorStageIdx; s < ds.numColorStages(); ++s) {
303 const GrFragmentStage& stage = ds.getColorStage(s);
egdaniela7dc0a82014-09-17 08:25:05 -0700304 get_stage_stats(stage, &fReadsDst, &fReadsFragPosition);
305 }
egdaniel9cf45bf2014-10-08 06:49:10 -0700306 for (int s = firstCoverageStageIdx; s < ds.numCoverageStages(); ++s) {
307 const GrFragmentStage& stage = ds.getCoverageStage(s);
egdaniela7dc0a82014-09-17 08:25:05 -0700308 get_stage_stats(stage, &fReadsDst, &fReadsFragPosition);
309 }
egdaniel9cf45bf2014-10-08 06:49:10 -0700310 if (ds.hasGeometryProcessor()) {
311 const GrGeometryStage& stage = *ds.getGeometryProcessor();
joshualittb0a8a372014-09-23 09:50:21 -0700312 fReadsFragPosition = fReadsFragPosition || stage.getProcessor()->willReadFragmentPosition();
egdaniela7dc0a82014-09-17 08:25:05 -0700313 }
314}
315
egdaniel89af44a2014-09-26 06:15:04 -0700316////////////////////////////////////////////////////////////////////////////////
317
egdaniel3658f382014-09-15 07:01:59 -0700318bool GrOptDrawState::operator== (const GrOptDrawState& that) const {
319 return this->isEqual(that);
320}
321
egdaniel89af44a2014-09-26 06:15:04 -0700322bool GrOptDrawState::isEqual(const GrOptDrawState& that) const {
323 bool usingVertexColors = this->hasColorVertexAttribute();
324 if (!usingVertexColors && this->fColor != that.fColor) {
325 return false;
326 }
327
328 if (this->getRenderTarget() != that.getRenderTarget() ||
329 this->fColorStages.count() != that.fColorStages.count() ||
330 this->fCoverageStages.count() != that.fCoverageStages.count() ||
331 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) ||
332 this->fSrcBlend != that.fSrcBlend ||
333 this->fDstBlend != that.fDstBlend ||
334 this->fBlendConstant != that.fBlendConstant ||
335 this->fFlagBits != that.fFlagBits ||
336 this->fVACount != that.fVACount ||
337 this->fVAStride != that.fVAStride ||
338 memcmp(this->fVAPtr, that.fVAPtr, this->fVACount * sizeof(GrVertexAttrib)) ||
339 this->fStencilSettings != that.fStencilSettings ||
340 this->fDrawFace != that.fDrawFace ||
341 this->fInputColorIsUsed != that.fInputColorIsUsed ||
342 this->fInputCoverageIsUsed != that.fInputCoverageIsUsed ||
343 this->fReadsDst != that.fReadsDst ||
344 this->fReadsFragPosition != that.fReadsFragPosition ||
345 this->fRequiresLocalCoordAttrib != that.fRequiresLocalCoordAttrib ||
346 this->fPrimaryOutputType != that.fPrimaryOutputType ||
347 this->fSecondaryOutputType != that.fSecondaryOutputType) {
348 return false;
349 }
350
351 bool usingVertexCoverage = this->hasCoverageVertexAttribute();
352 if (!usingVertexCoverage && this->fCoverage != that.fCoverage) {
353 return false;
354 }
355
356 bool explicitLocalCoords = this->hasLocalCoordAttribute();
357 if (this->hasGeometryProcessor()) {
358 if (!that.hasGeometryProcessor()) {
359 return false;
360 } else if (!GrProcessorStage::AreCompatible(*this->getGeometryProcessor(),
361 *that.getGeometryProcessor(),
362 explicitLocalCoords)) {
363 return false;
364 }
365 } else if (that.hasGeometryProcessor()) {
366 return false;
367 }
368
369 for (int i = 0; i < this->numColorStages(); i++) {
370 if (!GrProcessorStage::AreCompatible(this->getColorStage(i), that.getColorStage(i),
371 explicitLocalCoords)) {
372 return false;
373 }
374 }
375 for (int i = 0; i < this->numCoverageStages(); i++) {
376 if (!GrProcessorStage::AreCompatible(this->getCoverageStage(i), that.getCoverageStage(i),
377 explicitLocalCoords)) {
378 return false;
379 }
380 }
381
382 SkASSERT(0 == memcmp(this->fFixedFunctionVertexAttribIndices,
383 that.fFixedFunctionVertexAttribIndices,
384 sizeof(this->fFixedFunctionVertexAttribIndices)));
385
386 return true;
387}
388