blob: 79eef0c02499153e642c084e62ea706db0ebcacb [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
joshualitt4973d9d2014-11-08 09:24:25 -080010#include "GrDefaultGeoProcFactory.h"
egdaniel3658f382014-09-15 07:01:59 -070011#include "GrDrawState.h"
egdanielc0648242014-09-22 13:17:02 -070012#include "GrDrawTargetCaps.h"
joshualitt4973d9d2014-11-08 09:24:25 -080013#include "GrGpu.h"
egdaniel3658f382014-09-15 07:01:59 -070014
egdaniel170f90b2014-09-16 12:54:40 -070015GrOptDrawState::GrOptDrawState(const GrDrawState& drawState,
16 BlendOptFlags blendOptFlags,
17 GrBlendCoeff optSrcCoeff,
egdanielc0648242014-09-22 13:17:02 -070018 GrBlendCoeff optDstCoeff,
joshualitt79f8fae2014-10-28 17:59:26 -070019 GrGpu* gpu,
20 const GrDeviceCoordTexture* dstCopy,
21 GrGpu::DrawType drawType) {
bsalomonbcf0a522014-10-08 08:40:09 -070022 fRenderTarget.set(SkSafeRef(drawState.getRenderTarget()), kWrite_GrIOType);
egdaniel3658f382014-09-15 07:01:59 -070023 fColor = drawState.getColor();
24 fCoverage = drawState.getCoverage();
25 fViewMatrix = drawState.getViewMatrix();
26 fBlendConstant = drawState.getBlendConstant();
27 fFlagBits = drawState.getFlagBits();
28 fVAPtr = drawState.getVertexAttribs();
29 fVACount = drawState.getVertexAttribCount();
30 fVAStride = drawState.getVertexStride();
31 fStencilSettings = drawState.getStencil();
egdaniel89af44a2014-09-26 06:15:04 -070032 fDrawFace = (DrawFace)drawState.getDrawFace();
egdaniel170f90b2014-09-16 12:54:40 -070033 fBlendOptFlags = blendOptFlags;
34 fSrcBlend = optSrcCoeff;
35 fDstBlend = optDstCoeff;
joshualitt79f8fae2014-10-28 17:59:26 -070036 GrProgramDesc::DescInfo descInfo;
egdaniel3658f382014-09-15 07:01:59 -070037
joshualitt79f8fae2014-10-28 17:59:26 -070038 memcpy(descInfo.fFixedFunctionVertexAttribIndices,
egdaniel89af44a2014-09-26 06:15:04 -070039 drawState.getFixedFunctionVertexAttribIndices(),
joshualitt79f8fae2014-10-28 17:59:26 -070040 sizeof(descInfo.fFixedFunctionVertexAttribIndices));
egdaniel3658f382014-09-15 07:01:59 -070041
joshualitt79f8fae2014-10-28 17:59:26 -070042 descInfo.fInputColorIsUsed = true;
43 descInfo.fInputCoverageIsUsed = true;
egdaniel3658f382014-09-15 07:01:59 -070044
egdaniel9cf45bf2014-10-08 06:49:10 -070045 int firstColorStageIdx = 0;
46 int firstCoverageStageIdx = 0;
egdaniel9cf45bf2014-10-08 06:49:10 -070047
48 uint8_t fixedFunctionVAToRemove = 0;
49
joshualitt79f8fae2014-10-28 17:59:26 -070050 this->computeEffectiveColorStages(drawState, &descInfo, &firstColorStageIdx,
51 &fixedFunctionVAToRemove);
52 this->computeEffectiveCoverageStages(drawState, &descInfo, &firstCoverageStageIdx);
53 this->adjustFromBlendOpts(drawState, &descInfo, &firstColorStageIdx, &firstCoverageStageIdx,
egdaniel9cf45bf2014-10-08 06:49:10 -070054 &fixedFunctionVAToRemove);
55 // Should not be setting any more FFVA to be removed at this point
egdanielc0651c12014-10-21 07:47:10 -070056 if (0 != fixedFunctionVAToRemove) {
joshualitt79f8fae2014-10-28 17:59:26 -070057 this->removeFixedFunctionVertexAttribs(fixedFunctionVAToRemove, &descInfo);
egdanielc0651c12014-10-21 07:47:10 -070058 }
joshualitt79f8fae2014-10-28 17:59:26 -070059 this->getStageStats(drawState, firstColorStageIdx, firstCoverageStageIdx, &descInfo);
egdaniel9cf45bf2014-10-08 06:49:10 -070060
61 // Copy GeometryProcesssor from DS or ODS
egdaniel3658f382014-09-15 07:01:59 -070062 if (drawState.hasGeometryProcessor()) {
joshualitta5305a12014-10-10 17:47:00 -070063 fGeometryProcessor.initAndRef(drawState.fGeometryProcessor);
joshualitt4973d9d2014-11-08 09:24:25 -080064 } else if (!GrGpu::IsPathRenderingDrawType(drawType)) {
65 // Install default GP, this will be ignored if we are rendering with fragment shader only
66 // TODO(joshualitt) rendering code should do this
67 fGeometryProcessor.reset(GrDefaultGeoProcFactory::Create());
egdaniel3658f382014-09-15 07:01:59 -070068 } else {
69 fGeometryProcessor.reset(NULL);
70 }
71
egdaniel9cf45bf2014-10-08 06:49:10 -070072 // Copy Color Stages from DS to ODS
73 if (firstColorStageIdx < drawState.numColorStages()) {
egdanield9aa2182014-10-09 13:47:05 -070074 fFragmentStages.reset(&drawState.getColorStage(firstColorStageIdx),
75 drawState.numColorStages() - firstColorStageIdx);
egdaniel9cf45bf2014-10-08 06:49:10 -070076 } else {
egdanield9aa2182014-10-09 13:47:05 -070077 fFragmentStages.reset();
egdaniel9cf45bf2014-10-08 06:49:10 -070078 }
79
egdanield9aa2182014-10-09 13:47:05 -070080 fNumColorStages = fFragmentStages.count();
81
egdaniel9cf45bf2014-10-08 06:49:10 -070082 // Copy Coverage Stages from DS to ODS
egdanield9aa2182014-10-09 13:47:05 -070083 if (firstCoverageStageIdx < drawState.numCoverageStages()) {
84 fFragmentStages.push_back_n(drawState.numCoverageStages() - firstCoverageStageIdx,
85 &drawState.getCoverageStage(firstCoverageStageIdx));
egdaniel9cf45bf2014-10-08 06:49:10 -070086 }
joshualitt79f8fae2014-10-28 17:59:26 -070087
joshualitt4dd99882014-11-11 08:51:30 -080088 this->setOutputStateInfo(drawState, *gpu->caps(), &descInfo);
89
joshualitt79f8fae2014-10-28 17:59:26 -070090 // now create a key
91 gpu->buildProgramDesc(*this, descInfo, drawType, dstCopy, &fDesc);
egdaniel3658f382014-09-15 07:01:59 -070092};
93
joshualitt79f8fae2014-10-28 17:59:26 -070094GrOptDrawState* GrOptDrawState::Create(const GrDrawState& drawState,
95 GrGpu* gpu,
96 const GrDeviceCoordTexture* dstCopy,
egdanielb109ac22014-10-07 06:45:44 -070097 GrGpu::DrawType drawType) {
egdaniel69bb90c2014-11-11 07:32:45 -080098 GrBlendCoeff srcCoeff;
99 GrBlendCoeff dstCoeff;
100 BlendOptFlags blendFlags = (BlendOptFlags) drawState.getBlendOpts(false,
101 &srcCoeff,
102 &dstCoeff);
egdanielb109ac22014-10-07 06:45:44 -0700103
egdaniel69bb90c2014-11-11 07:32:45 -0800104 // If our blend coeffs are set to 0,1 we know we will not end up drawing unless we are
105 // stenciling. When path rendering the stencil settings are not always set on the draw state
106 // so we must check the draw type. In cases where we will skip drawing we simply return a
107 // null GrOptDrawState.
108 if (kZero_GrBlendCoeff == srcCoeff && kOne_GrBlendCoeff == dstCoeff &&
109 !drawState.getStencil().doesWrite() && GrGpu::kStencilPath_DrawType != drawType) {
110 return NULL;
egdanielb109ac22014-10-07 06:45:44 -0700111 }
egdaniel69bb90c2014-11-11 07:32:45 -0800112
113 return SkNEW_ARGS(GrOptDrawState, (drawState, blendFlags, srcCoeff,
114 dstCoeff, gpu, dstCopy, drawType));
egdanielb109ac22014-10-07 06:45:44 -0700115}
116
egdaniel9cf45bf2014-10-08 06:49:10 -0700117void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds,
118 const GrDrawTargetCaps& caps,
joshualitt4dd99882014-11-11 08:51:30 -0800119 GrProgramDesc::DescInfo* descInfo) {
egdanielc0648242014-09-22 13:17:02 -0700120 // Set this default and then possibly change our mind if there is coverage.
joshualitt79f8fae2014-10-28 17:59:26 -0700121 descInfo->fPrimaryOutputType = GrProgramDesc::kModulate_PrimaryOutputType;
122 descInfo->fSecondaryOutputType = GrProgramDesc::kNone_SecondaryOutputType;
egdanielc0648242014-09-22 13:17:02 -0700123
joshualitt4dd99882014-11-11 08:51:30 -0800124 // If we do have coverage determine whether it matters. Dual source blending is expensive so
125 // we don't do it if we are doing coverage drawing. If we aren't then We always do dual source
126 // blending if we have any effective coverage stages OR the geometry processor doesn't emits
127 // solid coverage.
128 // TODO move the gp logic into the GP base class
129 if (!this->isCoverageDrawing() && !ds.hasSolidCoverage()) {
egdanielc0648242014-09-22 13:17:02 -0700130 if (caps.dualSourceBlendingSupport()) {
131 if (kZero_GrBlendCoeff == fDstBlend) {
132 // write the coverage value to second color
joshualitt79f8fae2014-10-28 17:59:26 -0700133 descInfo->fSecondaryOutputType = GrProgramDesc::kCoverage_SecondaryOutputType;
egdanielc0648242014-09-22 13:17:02 -0700134 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
135 } else if (kSA_GrBlendCoeff == fDstBlend) {
136 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
joshualitt79f8fae2014-10-28 17:59:26 -0700137 descInfo->fSecondaryOutputType = GrProgramDesc::kCoverageISA_SecondaryOutputType;
egdanielc0648242014-09-22 13:17:02 -0700138 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
139 } else if (kSC_GrBlendCoeff == fDstBlend) {
140 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
joshualitt79f8fae2014-10-28 17:59:26 -0700141 descInfo->fSecondaryOutputType = GrProgramDesc::kCoverageISC_SecondaryOutputType;
egdanielc0648242014-09-22 13:17:02 -0700142 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
143 }
joshualitt79f8fae2014-10-28 17:59:26 -0700144 } else if (descInfo->fReadsDst &&
egdanielc0648242014-09-22 13:17:02 -0700145 kOne_GrBlendCoeff == fSrcBlend &&
146 kZero_GrBlendCoeff == fDstBlend) {
joshualitt79f8fae2014-10-28 17:59:26 -0700147 descInfo->fPrimaryOutputType = GrProgramDesc::kCombineWithDst_PrimaryOutputType;
egdanielc0648242014-09-22 13:17:02 -0700148 }
149 }
egdanielc0648242014-09-22 13:17:02 -0700150}
151
egdaniel9cf45bf2014-10-08 06:49:10 -0700152void GrOptDrawState::adjustFromBlendOpts(const GrDrawState& ds,
joshualitt79f8fae2014-10-28 17:59:26 -0700153 GrProgramDesc::DescInfo* descInfo,
egdaniel9cf45bf2014-10-08 06:49:10 -0700154 int* firstColorStageIdx,
155 int* firstCoverageStageIdx,
156 uint8_t* fixedFunctionVAToRemove) {
egdaniel170f90b2014-09-16 12:54:40 -0700157 switch (fBlendOptFlags) {
158 case kNone_BlendOpt:
159 case kSkipDraw_BlendOptFlag:
160 break;
161 case kCoverageAsAlpha_BlendOptFlag:
162 fFlagBits |= kCoverageDrawing_StateBit;
163 break;
164 case kEmitCoverage_BlendOptFlag:
165 fColor = 0xffffffff;
joshualitt79f8fae2014-10-28 17:59:26 -0700166 descInfo->fInputColorIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700167 *firstColorStageIdx = ds.numColorStages();
168 *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding;
egdaniel170f90b2014-09-16 12:54:40 -0700169 break;
170 case kEmitTransBlack_BlendOptFlag:
171 fColor = 0;
172 fCoverage = 0xff;
joshualitt79f8fae2014-10-28 17:59:26 -0700173 descInfo->fInputColorIsUsed = true;
174 descInfo->fInputCoverageIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700175 *firstColorStageIdx = ds.numColorStages();
176 *firstCoverageStageIdx = ds.numCoverageStages();
177 *fixedFunctionVAToRemove |= (0x1 << kColor_GrVertexAttribBinding |
178 0x1 << kCoverage_GrVertexAttribBinding);
egdaniel170f90b2014-09-16 12:54:40 -0700179 break;
180 default:
181 SkFAIL("Unknown BlendOptFlag");
egdaniel170f90b2014-09-16 12:54:40 -0700182 }
183}
184
joshualitt79f8fae2014-10-28 17:59:26 -0700185void GrOptDrawState::removeFixedFunctionVertexAttribs(uint8_t removeVAFlag,
186 GrProgramDesc::DescInfo* descInfo) {
egdaniel3658f382014-09-15 07:01:59 -0700187 int numToRemove = 0;
188 uint8_t maskCheck = 0x1;
189 // Count the number of vertex attributes that we will actually remove
190 for (int i = 0; i < kGrFixedFunctionVertexAttribBindingCnt; ++i) {
joshualitt79f8fae2014-10-28 17:59:26 -0700191 if ((maskCheck & removeVAFlag) && -1 != descInfo->fFixedFunctionVertexAttribIndices[i]) {
egdaniel3658f382014-09-15 07:01:59 -0700192 ++numToRemove;
193 }
194 maskCheck <<= 1;
195 }
egdaniel170f90b2014-09-16 12:54:40 -0700196
egdaniel3658f382014-09-15 07:01:59 -0700197 fOptVA.reset(fVACount - numToRemove);
198
199 GrVertexAttrib* dst = fOptVA.get();
200 const GrVertexAttrib* src = fVAPtr;
201
202 for (int i = 0, newIdx = 0; i < fVACount; ++i, ++src) {
203 const GrVertexAttrib& currAttrib = *src;
204 if (currAttrib.fBinding < kGrFixedFunctionVertexAttribBindingCnt) {
205 uint8_t maskCheck = 0x1 << currAttrib.fBinding;
206 if (maskCheck & removeVAFlag) {
joshualitt79f8fae2014-10-28 17:59:26 -0700207 SkASSERT(-1 != descInfo->fFixedFunctionVertexAttribIndices[currAttrib.fBinding]);
208 descInfo->fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = -1;
egdaniel3658f382014-09-15 07:01:59 -0700209 continue;
210 }
joshualitt79f8fae2014-10-28 17:59:26 -0700211 descInfo->fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = newIdx;
egdaniel3658f382014-09-15 07:01:59 -0700212 }
213 memcpy(dst, src, sizeof(GrVertexAttrib));
egdaniel3658f382014-09-15 07:01:59 -0700214 ++newIdx;
215 ++dst;
216 }
217 fVACount -= numToRemove;
218 fVAPtr = fOptVA.get();
219}
220
joshualitt79f8fae2014-10-28 17:59:26 -0700221void GrOptDrawState::computeEffectiveColorStages(const GrDrawState& ds,
222 GrProgramDesc::DescInfo* descInfo,
223 int* firstColorStageIdx,
egdaniel9cf45bf2014-10-08 06:49:10 -0700224 uint8_t* fixedFunctionVAToRemove) {
egdaniel3658f382014-09-15 07:01:59 -0700225 // Set up color and flags for ConstantColorComponent checks
egdaniel1a8ecdf2014-10-03 06:24:12 -0700226 GrProcessor::InvariantOutput inout;
227 inout.fIsSingleComponent = false;
joshualitt79f8fae2014-10-28 17:59:26 -0700228 if (!descInfo->hasColorVertexAttribute()) {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700229 inout.fColor = ds.getColor();
230 inout.fValidFlags = kRGBA_GrColorComponentFlags;
egdaniel3658f382014-09-15 07:01:59 -0700231 } else {
232 if (ds.vertexColorsAreOpaque()) {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700233 inout.fColor = 0xFF << GrColor_SHIFT_A;
234 inout.fValidFlags = kA_GrColorComponentFlag;
egdaniel3658f382014-09-15 07:01:59 -0700235 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700236 inout.fValidFlags = 0;
237 // not strictly necessary but we get false alarms from tools about uninit.
238 inout.fColor = 0;
egdaniel3658f382014-09-15 07:01:59 -0700239 }
240 }
241
242 for (int i = 0; i < ds.numColorStages(); ++i) {
joshualitt47bb3822014-10-07 16:43:25 -0700243 const GrFragmentProcessor* fp = ds.getColorStage(i).getProcessor();
egdaniel9e4d6d12014-10-15 13:49:02 -0700244 fp->computeInvariantOutput(&inout);
245 if (!inout.fWillUseInputColor) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700246 *firstColorStageIdx = i;
joshualitt79f8fae2014-10-28 17:59:26 -0700247 descInfo->fInputColorIsUsed = false;
egdaniel3658f382014-09-15 07:01:59 -0700248 }
egdaniel1a8ecdf2014-10-03 06:24:12 -0700249 if (kRGBA_GrColorComponentFlags == inout.fValidFlags) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700250 *firstColorStageIdx = i + 1;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700251 fColor = inout.fColor;
joshualitt79f8fae2014-10-28 17:59:26 -0700252 descInfo->fInputColorIsUsed = true;
egdaniel9cf45bf2014-10-08 06:49:10 -0700253 *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding;
egdaniel9e4d6d12014-10-15 13:49:02 -0700254 // Since we are clearing all previous color stages we are in a state where we have found
255 // zero stages that don't multiply the inputColor.
256 inout.fNonMulStageFound = false;
egdaniel3658f382014-09-15 07:01:59 -0700257 }
258 }
egdaniel3658f382014-09-15 07:01:59 -0700259}
260
egdaniel9cf45bf2014-10-08 06:49:10 -0700261void GrOptDrawState::computeEffectiveCoverageStages(const GrDrawState& ds,
joshualitt79f8fae2014-10-28 17:59:26 -0700262 GrProgramDesc::DescInfo* descInfo,
egdaniel9cf45bf2014-10-08 06:49:10 -0700263 int* firstCoverageStageIdx) {
egdaniel3658f382014-09-15 07:01:59 -0700264 // We do not try to optimize out constantColor coverage effects here. It is extremely rare
265 // to have a coverage effect that returns a constant value for all four channels. Thus we
266 // save having to make extra virtual calls by not checking for it.
267
268 // Don't do any optimizations on coverage stages. It should not be the case where we do not use
269 // input coverage in an effect
270#ifdef OptCoverageStages
egdaniel9e4d6d12014-10-15 13:49:02 -0700271 GrProcessor::InvariantOutput inout;
egdaniel3658f382014-09-15 07:01:59 -0700272 for (int i = 0; i < ds.numCoverageStages(); ++i) {
egdaniel9e4d6d12014-10-15 13:49:02 -0700273 const GrFragmentProcessor* fp = ds.getCoverageStage(i).getProcessor();
274 fp->computeInvariantOutput(&inout);
275 if (!inout.fWillUseInputColor) {
egdaniel9cf45bf2014-10-08 06:49:10 -0700276 *firstCoverageStageIdx = i;
joshualitt79f8fae2014-10-28 17:59:26 -0700277 descInfo->fInputCoverageIsUsed = false;
egdaniel3658f382014-09-15 07:01:59 -0700278 }
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,
joshualitt79f8fae2014-10-28 17:59:26 -0700293 int firstCoverageStageIdx, GrProgramDesc::DescInfo* descInfo) {
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
joshualitt79f8fae2014-10-28 17:59:26 -0700296 descInfo->fRequiresLocalCoordAttrib = descInfo->hasLocalCoordAttribute() &&
egdaniel9cf45bf2014-10-08 06:49:10 -0700297 ds.numTotalStages() - firstColorStageIdx - firstCoverageStageIdx > 0;
egdaniela7dc0a82014-09-17 08:25:05 -0700298
joshualitt79f8fae2014-10-28 17:59:26 -0700299 descInfo->fReadsDst = false;
300 descInfo->fReadsFragPosition = false;
egdaniela7dc0a82014-09-17 08:25:05 -0700301
egdaniel9cf45bf2014-10-08 06:49:10 -0700302 for (int s = firstColorStageIdx; s < ds.numColorStages(); ++s) {
303 const GrFragmentStage& stage = ds.getColorStage(s);
joshualitt79f8fae2014-10-28 17:59:26 -0700304 get_stage_stats(stage, &descInfo->fReadsDst, &descInfo->fReadsFragPosition);
egdaniela7dc0a82014-09-17 08:25:05 -0700305 }
egdaniel9cf45bf2014-10-08 06:49:10 -0700306 for (int s = firstCoverageStageIdx; s < ds.numCoverageStages(); ++s) {
307 const GrFragmentStage& stage = ds.getCoverageStage(s);
joshualitt79f8fae2014-10-28 17:59:26 -0700308 get_stage_stats(stage, &descInfo->fReadsDst, &descInfo->fReadsFragPosition);
egdaniela7dc0a82014-09-17 08:25:05 -0700309 }
egdaniel9cf45bf2014-10-08 06:49:10 -0700310 if (ds.hasGeometryProcessor()) {
joshualitta5305a12014-10-10 17:47:00 -0700311 const GrGeometryProcessor& gp = *ds.getGeometryProcessor();
joshualitt79f8fae2014-10-28 17:59:26 -0700312 descInfo->fReadsFragPosition = descInfo->fReadsFragPosition || gp.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 {
joshualitt79f8fae2014-10-28 17:59:26 -0700323 if (this->fDesc != that.fDesc) {
324 return false;
325 }
326 bool usingVertexColors = that.fDesc.header().fColorAttributeIndex != -1;
egdaniel89af44a2014-09-26 06:15:04 -0700327 if (!usingVertexColors && this->fColor != that.fColor) {
328 return false;
329 }
330
331 if (this->getRenderTarget() != that.getRenderTarget() ||
egdaniel89af44a2014-09-26 06:15:04 -0700332 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) ||
333 this->fSrcBlend != that.fSrcBlend ||
334 this->fDstBlend != that.fDstBlend ||
335 this->fBlendConstant != that.fBlendConstant ||
336 this->fFlagBits != that.fFlagBits ||
337 this->fVACount != that.fVACount ||
338 this->fVAStride != that.fVAStride ||
339 memcmp(this->fVAPtr, that.fVAPtr, this->fVACount * sizeof(GrVertexAttrib)) ||
340 this->fStencilSettings != that.fStencilSettings ||
joshualitt79f8fae2014-10-28 17:59:26 -0700341 this->fDrawFace != that.fDrawFace) {
egdaniel89af44a2014-09-26 06:15:04 -0700342 return false;
343 }
344
joshualitt79f8fae2014-10-28 17:59:26 -0700345 bool usingVertexCoverage = this->fDesc.header().fCoverageAttributeIndex != -1;
egdaniel89af44a2014-09-26 06:15:04 -0700346 if (!usingVertexCoverage && this->fCoverage != that.fCoverage) {
347 return false;
348 }
349
egdaniel89af44a2014-09-26 06:15:04 -0700350 if (this->hasGeometryProcessor()) {
351 if (!that.hasGeometryProcessor()) {
352 return false;
joshualitta5305a12014-10-10 17:47:00 -0700353 } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) {
egdaniel89af44a2014-09-26 06:15:04 -0700354 return false;
355 }
356 } else if (that.hasGeometryProcessor()) {
357 return false;
358 }
359
joshualitt79f8fae2014-10-28 17:59:26 -0700360 bool explicitLocalCoords = this->fDesc.header().fLocalCoordAttributeIndex != -1;
egdanield9aa2182014-10-09 13:47:05 -0700361 for (int i = 0; i < this->numFragmentStages(); i++) {
joshualitta5305a12014-10-10 17:47:00 -0700362 if (!GrFragmentStage::AreCompatible(this->getFragmentStage(i), that.getFragmentStage(i),
363 explicitLocalCoords)) {
egdaniel89af44a2014-09-26 06:15:04 -0700364 return false;
365 }
366 }
egdaniel89af44a2014-09-26 06:15:04 -0700367 return true;
368}
369