egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 1 | /* |
| 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" |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 11 | #include "GrDrawTargetCaps.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 12 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 13 | GrOptDrawState::GrOptDrawState(const GrDrawState& drawState, |
| 14 | BlendOptFlags blendOptFlags, |
| 15 | GrBlendCoeff optSrcCoeff, |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 16 | GrBlendCoeff optDstCoeff, |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 17 | const GrDrawTargetCaps& caps) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 18 | fRenderTarget.set(SkSafeRef(drawState.getRenderTarget()), kWrite_GrIOType); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 19 | 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(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 28 | fDrawFace = (DrawFace)drawState.getDrawFace(); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 29 | fBlendOptFlags = blendOptFlags; |
| 30 | fSrcBlend = optSrcCoeff; |
| 31 | fDstBlend = optDstCoeff; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 32 | |
| 33 | memcpy(fFixedFunctionVertexAttribIndices, |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 34 | drawState.getFixedFunctionVertexAttribIndices(), |
| 35 | sizeof(fFixedFunctionVertexAttribIndices)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 36 | |
| 37 | fInputColorIsUsed = true; |
| 38 | fInputCoverageIsUsed = true; |
| 39 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 40 | 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 |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 56 | if (drawState.hasGeometryProcessor()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 57 | fGeometryProcessor.reset(SkNEW_ARGS(GrGeometryStage, (*drawState.getGeometryProcessor()))); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 58 | } else { |
| 59 | fGeometryProcessor.reset(NULL); |
| 60 | } |
| 61 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 62 | // 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 |
egdaniel | 47326ab | 2014-10-08 07:39:19 -0700 | [diff] [blame] | 71 | if (firstCoverageStageIdx < drawState.numCoverageStages() && separateCoverageFromColor) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 72 | fCoverageStages.reset(&drawState.getCoverageStage(firstCoverageStageIdx), |
| 73 | drawState.numCoverageStages() - firstCoverageStageIdx); |
| 74 | } else { |
| 75 | fCoverageStages.reset(); |
egdaniel | 47326ab | 2014-10-08 07:39:19 -0700 | [diff] [blame] | 76 | if (firstCoverageStageIdx < drawState.numCoverageStages()) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 77 | // 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 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 85 | GrOptDrawState* 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 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 121 | void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds, |
| 122 | const GrDrawTargetCaps& caps, |
| 123 | int firstCoverageStageIdx, |
| 124 | bool* separateCoverageFromColor) { |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 125 | // 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. |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 130 | *separateCoverageFromColor = this->hasGeometryProcessor(); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 131 | if (!this->isCoverageDrawing() && |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 132 | (ds.numCoverageStages() - firstCoverageStageIdx > 0 || |
| 133 | ds.hasGeometryProcessor() || |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 134 | this->hasCoverageVertexAttribute())) { |
| 135 | |
| 136 | if (caps.dualSourceBlendingSupport()) { |
| 137 | if (kZero_GrBlendCoeff == fDstBlend) { |
| 138 | // write the coverage value to second color |
| 139 | fSecondaryOutputType = kCoverage_SecondaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 140 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 141 | 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; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 145 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 146 | 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; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 150 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 151 | fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 152 | } |
| 153 | } else if (fReadsDst && |
| 154 | kOne_GrBlendCoeff == fSrcBlend && |
| 155 | kZero_GrBlendCoeff == fDstBlend) { |
| 156 | fPrimaryOutputType = kCombineWithDst_PrimaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 157 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 160 | } |
| 161 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 162 | void GrOptDrawState::adjustFromBlendOpts(const GrDrawState& ds, |
| 163 | int* firstColorStageIdx, |
| 164 | int* firstCoverageStageIdx, |
| 165 | uint8_t* fixedFunctionVAToRemove) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 166 | 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; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 176 | *firstColorStageIdx = ds.numColorStages(); |
| 177 | *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 178 | break; |
| 179 | case kEmitTransBlack_BlendOptFlag: |
| 180 | fColor = 0; |
| 181 | fCoverage = 0xff; |
| 182 | fInputColorIsUsed = true; |
| 183 | fInputCoverageIsUsed = true; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 184 | *firstColorStageIdx = ds.numColorStages(); |
| 185 | *firstCoverageStageIdx = ds.numCoverageStages(); |
| 186 | *fixedFunctionVAToRemove |= (0x1 << kColor_GrVertexAttribBinding | |
| 187 | 0x1 << kCoverage_GrVertexAttribBinding); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 188 | break; |
| 189 | default: |
| 190 | SkFAIL("Unknown BlendOptFlag"); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 194 | void 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 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 204 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 205 | 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 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 219 | fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = newIdx; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 220 | } |
| 221 | memcpy(dst, src, sizeof(GrVertexAttrib)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 222 | ++newIdx; |
| 223 | ++dst; |
| 224 | } |
| 225 | fVACount -= numToRemove; |
| 226 | fVAPtr = fOptVA.get(); |
| 227 | } |
| 228 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 229 | void GrOptDrawState::computeEffectiveColorStages(const GrDrawState& ds, int* firstColorStageIdx, |
| 230 | uint8_t* fixedFunctionVAToRemove) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 231 | // Set up color and flags for ConstantColorComponent checks |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 232 | GrProcessor::InvariantOutput inout; |
| 233 | inout.fIsSingleComponent = false; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 234 | if (!this->hasColorVertexAttribute()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 235 | inout.fColor = ds.getColor(); |
| 236 | inout.fValidFlags = kRGBA_GrColorComponentFlags; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 237 | } else { |
| 238 | if (ds.vertexColorsAreOpaque()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 239 | inout.fColor = 0xFF << GrColor_SHIFT_A; |
| 240 | inout.fValidFlags = kA_GrColorComponentFlag; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 241 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 242 | inout.fValidFlags = 0; |
| 243 | // not strictly necessary but we get false alarms from tools about uninit. |
| 244 | inout.fColor = 0; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | for (int i = 0; i < ds.numColorStages(); ++i) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 249 | const GrFragmentProcessor* fp = ds.getColorStage(i).getProcessor(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 250 | if (!fp->willUseInputColor()) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 251 | *firstColorStageIdx = i; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 252 | fInputColorIsUsed = false; |
| 253 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 254 | fp->computeInvariantOutput(&inout); |
| 255 | if (kRGBA_GrColorComponentFlags == inout.fValidFlags) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 256 | *firstColorStageIdx = i + 1; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 257 | fColor = inout.fColor; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 258 | fInputColorIsUsed = true; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 259 | *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 262 | } |
| 263 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 264 | void GrOptDrawState::computeEffectiveCoverageStages(const GrDrawState& ds, |
| 265 | int* firstCoverageStageIdx) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 266 | // 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) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 274 | const GrProcessor* processor = ds.getCoverageStage(i).getProcessor(); |
| 275 | if (!processor->willUseInputColor()) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 276 | *firstCoverageStageIdx = i; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 277 | fInputCoverageIsUsed = false; |
| 278 | } |
| 279 | } |
| 280 | #endif |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 281 | } |
| 282 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | static void get_stage_stats(const GrFragmentStage& stage, bool* readsDst, bool* readsFragPosition) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 284 | if (stage.getProcessor()->willReadDstColor()) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 285 | *readsDst = true; |
| 286 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 287 | if (stage.getProcessor()->willReadFragmentPosition()) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 288 | *readsFragPosition = true; |
| 289 | } |
| 290 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 291 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 292 | void GrOptDrawState::getStageStats(const GrDrawState& ds, int firstColorStageIdx, |
| 293 | int firstCoverageStageIdx) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 294 | // 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 |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 296 | fRequiresLocalCoordAttrib = this->hasLocalCoordAttribute() && |
| 297 | ds.numTotalStages() - firstColorStageIdx - firstCoverageStageIdx > 0; |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 298 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 299 | fReadsDst = false; |
| 300 | fReadsFragPosition = false; |
| 301 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 302 | for (int s = firstColorStageIdx; s < ds.numColorStages(); ++s) { |
| 303 | const GrFragmentStage& stage = ds.getColorStage(s); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 304 | get_stage_stats(stage, &fReadsDst, &fReadsFragPosition); |
| 305 | } |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 306 | for (int s = firstCoverageStageIdx; s < ds.numCoverageStages(); ++s) { |
| 307 | const GrFragmentStage& stage = ds.getCoverageStage(s); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 308 | get_stage_stats(stage, &fReadsDst, &fReadsFragPosition); |
| 309 | } |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 310 | if (ds.hasGeometryProcessor()) { |
| 311 | const GrGeometryStage& stage = *ds.getGeometryProcessor(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 312 | fReadsFragPosition = fReadsFragPosition || stage.getProcessor()->willReadFragmentPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 316 | //////////////////////////////////////////////////////////////////////////////// |
| 317 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 318 | bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
| 319 | return this->isEqual(that); |
| 320 | } |
| 321 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 322 | bool 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 | |