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 |
egdaniel | c0651c1 | 2014-10-21 07:47:10 -0700 | [diff] [blame^] | 51 | if (0 != fixedFunctionVAToRemove) { |
| 52 | this->removeFixedFunctionVertexAttribs(fixedFunctionVAToRemove); |
| 53 | } |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 54 | this->getStageStats(drawState, firstColorStageIdx, firstCoverageStageIdx); |
| 55 | this->setOutputStateInfo(drawState, caps, firstCoverageStageIdx, &separateCoverageFromColor); |
| 56 | |
| 57 | // Copy GeometryProcesssor from DS or ODS |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 58 | if (drawState.hasGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 59 | fGeometryProcessor.initAndRef(drawState.fGeometryProcessor); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 60 | } else { |
| 61 | fGeometryProcessor.reset(NULL); |
| 62 | } |
| 63 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 64 | // Copy Color Stages from DS to ODS |
| 65 | if (firstColorStageIdx < drawState.numColorStages()) { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 66 | fFragmentStages.reset(&drawState.getColorStage(firstColorStageIdx), |
| 67 | drawState.numColorStages() - firstColorStageIdx); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 68 | } else { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 69 | fFragmentStages.reset(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 72 | fNumColorStages = fFragmentStages.count(); |
| 73 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 74 | // Copy Coverage Stages from DS to ODS |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 75 | if (firstCoverageStageIdx < drawState.numCoverageStages()) { |
| 76 | fFragmentStages.push_back_n(drawState.numCoverageStages() - firstCoverageStageIdx, |
| 77 | &drawState.getCoverageStage(firstCoverageStageIdx)); |
| 78 | if (!separateCoverageFromColor) { |
| 79 | fNumColorStages = fFragmentStages.count(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 84 | GrOptDrawState* GrOptDrawState::Create(const GrDrawState& drawState, const GrDrawTargetCaps& caps, |
| 85 | GrGpu::DrawType drawType) { |
| 86 | if (NULL == drawState.fCachedOptState || caps.getUniqueID() != drawState.fCachedCapsID) { |
| 87 | GrBlendCoeff srcCoeff; |
| 88 | GrBlendCoeff dstCoeff; |
| 89 | BlendOptFlags blendFlags = (BlendOptFlags) drawState.getBlendOpts(false, |
| 90 | &srcCoeff, |
| 91 | &dstCoeff); |
| 92 | |
| 93 | // If our blend coeffs are set to 0,1 we know we will not end up drawing unless we are |
| 94 | // stenciling. When path rendering the stencil settings are not always set on the draw state |
| 95 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 96 | // null GrOptDrawState. |
| 97 | if (kZero_GrBlendCoeff == srcCoeff && kOne_GrBlendCoeff == dstCoeff && |
| 98 | !drawState.getStencil().doesWrite() && GrGpu::kStencilPath_DrawType != drawType) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | drawState.fCachedOptState = SkNEW_ARGS(GrOptDrawState, (drawState, blendFlags, srcCoeff, |
| 103 | dstCoeff, caps)); |
| 104 | drawState.fCachedCapsID = caps.getUniqueID(); |
| 105 | } else { |
| 106 | #ifdef SK_DEBUG |
| 107 | GrBlendCoeff srcCoeff; |
| 108 | GrBlendCoeff dstCoeff; |
| 109 | BlendOptFlags blendFlags = (BlendOptFlags) drawState.getBlendOpts(false, |
| 110 | &srcCoeff, |
| 111 | &dstCoeff); |
| 112 | SkASSERT(GrOptDrawState(drawState, blendFlags, srcCoeff, dstCoeff, caps) == |
| 113 | *drawState.fCachedOptState); |
| 114 | #endif |
| 115 | } |
| 116 | drawState.fCachedOptState->ref(); |
| 117 | return drawState.fCachedOptState; |
| 118 | } |
| 119 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 120 | void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds, |
| 121 | const GrDrawTargetCaps& caps, |
| 122 | int firstCoverageStageIdx, |
| 123 | bool* separateCoverageFromColor) { |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 124 | // Set this default and then possibly change our mind if there is coverage. |
| 125 | fPrimaryOutputType = kModulate_PrimaryOutputType; |
| 126 | fSecondaryOutputType = kNone_SecondaryOutputType; |
| 127 | |
| 128 | // If we do have coverage determine whether it matters. |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 129 | *separateCoverageFromColor = this->hasGeometryProcessor(); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 130 | if (!this->isCoverageDrawing() && |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 131 | (ds.numCoverageStages() - firstCoverageStageIdx > 0 || |
| 132 | ds.hasGeometryProcessor() || |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 133 | this->hasCoverageVertexAttribute())) { |
| 134 | |
| 135 | if (caps.dualSourceBlendingSupport()) { |
| 136 | if (kZero_GrBlendCoeff == fDstBlend) { |
| 137 | // write the coverage value to second color |
| 138 | fSecondaryOutputType = kCoverage_SecondaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 139 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 140 | fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 141 | } else if (kSA_GrBlendCoeff == fDstBlend) { |
| 142 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 143 | fSecondaryOutputType = kCoverageISA_SecondaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 144 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 145 | fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 146 | } else if (kSC_GrBlendCoeff == fDstBlend) { |
| 147 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 148 | fSecondaryOutputType = kCoverageISC_SecondaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 149 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 150 | fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 151 | } |
| 152 | } else if (fReadsDst && |
| 153 | kOne_GrBlendCoeff == fSrcBlend && |
| 154 | kZero_GrBlendCoeff == fDstBlend) { |
| 155 | fPrimaryOutputType = kCombineWithDst_PrimaryOutputType; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 156 | *separateCoverageFromColor = true; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 159 | } |
| 160 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 161 | void GrOptDrawState::adjustFromBlendOpts(const GrDrawState& ds, |
| 162 | int* firstColorStageIdx, |
| 163 | int* firstCoverageStageIdx, |
| 164 | uint8_t* fixedFunctionVAToRemove) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 165 | switch (fBlendOptFlags) { |
| 166 | case kNone_BlendOpt: |
| 167 | case kSkipDraw_BlendOptFlag: |
| 168 | break; |
| 169 | case kCoverageAsAlpha_BlendOptFlag: |
| 170 | fFlagBits |= kCoverageDrawing_StateBit; |
| 171 | break; |
| 172 | case kEmitCoverage_BlendOptFlag: |
| 173 | fColor = 0xffffffff; |
| 174 | fInputColorIsUsed = true; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 175 | *firstColorStageIdx = ds.numColorStages(); |
| 176 | *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 177 | break; |
| 178 | case kEmitTransBlack_BlendOptFlag: |
| 179 | fColor = 0; |
| 180 | fCoverage = 0xff; |
| 181 | fInputColorIsUsed = true; |
| 182 | fInputCoverageIsUsed = true; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 183 | *firstColorStageIdx = ds.numColorStages(); |
| 184 | *firstCoverageStageIdx = ds.numCoverageStages(); |
| 185 | *fixedFunctionVAToRemove |= (0x1 << kColor_GrVertexAttribBinding | |
| 186 | 0x1 << kCoverage_GrVertexAttribBinding); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | SkFAIL("Unknown BlendOptFlag"); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 193 | void GrOptDrawState::removeFixedFunctionVertexAttribs(uint8_t removeVAFlag) { |
| 194 | int numToRemove = 0; |
| 195 | uint8_t maskCheck = 0x1; |
| 196 | // Count the number of vertex attributes that we will actually remove |
| 197 | for (int i = 0; i < kGrFixedFunctionVertexAttribBindingCnt; ++i) { |
| 198 | if ((maskCheck & removeVAFlag) && -1 != fFixedFunctionVertexAttribIndices[i]) { |
| 199 | ++numToRemove; |
| 200 | } |
| 201 | maskCheck <<= 1; |
| 202 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 203 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 204 | fOptVA.reset(fVACount - numToRemove); |
| 205 | |
| 206 | GrVertexAttrib* dst = fOptVA.get(); |
| 207 | const GrVertexAttrib* src = fVAPtr; |
| 208 | |
| 209 | for (int i = 0, newIdx = 0; i < fVACount; ++i, ++src) { |
| 210 | const GrVertexAttrib& currAttrib = *src; |
| 211 | if (currAttrib.fBinding < kGrFixedFunctionVertexAttribBindingCnt) { |
| 212 | uint8_t maskCheck = 0x1 << currAttrib.fBinding; |
| 213 | if (maskCheck & removeVAFlag) { |
| 214 | SkASSERT(-1 != fFixedFunctionVertexAttribIndices[currAttrib.fBinding]); |
| 215 | fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = -1; |
| 216 | continue; |
| 217 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 218 | fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = newIdx; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 219 | } |
| 220 | memcpy(dst, src, sizeof(GrVertexAttrib)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 221 | ++newIdx; |
| 222 | ++dst; |
| 223 | } |
| 224 | fVACount -= numToRemove; |
| 225 | fVAPtr = fOptVA.get(); |
| 226 | } |
| 227 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 228 | void GrOptDrawState::computeEffectiveColorStages(const GrDrawState& ds, int* firstColorStageIdx, |
| 229 | uint8_t* fixedFunctionVAToRemove) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 230 | // Set up color and flags for ConstantColorComponent checks |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 231 | GrProcessor::InvariantOutput inout; |
| 232 | inout.fIsSingleComponent = false; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 233 | if (!this->hasColorVertexAttribute()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 234 | inout.fColor = ds.getColor(); |
| 235 | inout.fValidFlags = kRGBA_GrColorComponentFlags; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 236 | } else { |
| 237 | if (ds.vertexColorsAreOpaque()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 238 | inout.fColor = 0xFF << GrColor_SHIFT_A; |
| 239 | inout.fValidFlags = kA_GrColorComponentFlag; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 240 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 241 | inout.fValidFlags = 0; |
| 242 | // not strictly necessary but we get false alarms from tools about uninit. |
| 243 | inout.fColor = 0; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | for (int i = 0; i < ds.numColorStages(); ++i) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 248 | const GrFragmentProcessor* fp = ds.getColorStage(i).getProcessor(); |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 249 | fp->computeInvariantOutput(&inout); |
| 250 | if (!inout.fWillUseInputColor) { |
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 | if (kRGBA_GrColorComponentFlags == inout.fValidFlags) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 255 | *firstColorStageIdx = i + 1; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 256 | fColor = inout.fColor; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 257 | fInputColorIsUsed = true; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 258 | *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding; |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 259 | // Since we are clearing all previous color stages we are in a state where we have found |
| 260 | // zero stages that don't multiply the inputColor. |
| 261 | inout.fNonMulStageFound = false; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 264 | } |
| 265 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 266 | void GrOptDrawState::computeEffectiveCoverageStages(const GrDrawState& ds, |
| 267 | int* firstCoverageStageIdx) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 268 | // We do not try to optimize out constantColor coverage effects here. It is extremely rare |
| 269 | // to have a coverage effect that returns a constant value for all four channels. Thus we |
| 270 | // save having to make extra virtual calls by not checking for it. |
| 271 | |
| 272 | // Don't do any optimizations on coverage stages. It should not be the case where we do not use |
| 273 | // input coverage in an effect |
| 274 | #ifdef OptCoverageStages |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 275 | GrProcessor::InvariantOutput inout; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 276 | for (int i = 0; i < ds.numCoverageStages(); ++i) { |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 277 | const GrFragmentProcessor* fp = ds.getCoverageStage(i).getProcessor(); |
| 278 | fp->computeInvariantOutput(&inout); |
| 279 | if (!inout.fWillUseInputColor) { |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 280 | *firstCoverageStageIdx = i; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 281 | fInputCoverageIsUsed = false; |
| 282 | } |
| 283 | } |
| 284 | #endif |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 285 | } |
| 286 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 287 | static void get_stage_stats(const GrFragmentStage& stage, bool* readsDst, bool* readsFragPosition) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 288 | if (stage.getProcessor()->willReadDstColor()) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 289 | *readsDst = true; |
| 290 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 291 | if (stage.getProcessor()->willReadFragmentPosition()) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 292 | *readsFragPosition = true; |
| 293 | } |
| 294 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 295 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 296 | void GrOptDrawState::getStageStats(const GrDrawState& ds, int firstColorStageIdx, |
| 297 | int firstCoverageStageIdx) { |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 298 | // We will need a local coord attrib if there is one currently set on the optState and we are |
| 299 | // actually generating some effect code |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 300 | fRequiresLocalCoordAttrib = this->hasLocalCoordAttribute() && |
| 301 | ds.numTotalStages() - firstColorStageIdx - firstCoverageStageIdx > 0; |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 302 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 303 | fReadsDst = false; |
| 304 | fReadsFragPosition = false; |
| 305 | |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 306 | for (int s = firstColorStageIdx; s < ds.numColorStages(); ++s) { |
| 307 | const GrFragmentStage& stage = ds.getColorStage(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 | for (int s = firstCoverageStageIdx; s < ds.numCoverageStages(); ++s) { |
| 311 | const GrFragmentStage& stage = ds.getCoverageStage(s); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 312 | get_stage_stats(stage, &fReadsDst, &fReadsFragPosition); |
| 313 | } |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 314 | if (ds.hasGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 315 | const GrGeometryProcessor& gp = *ds.getGeometryProcessor(); |
| 316 | fReadsFragPosition = fReadsFragPosition || gp.willReadFragmentPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 320 | //////////////////////////////////////////////////////////////////////////////// |
| 321 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 322 | bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
| 323 | return this->isEqual(that); |
| 324 | } |
| 325 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 326 | bool GrOptDrawState::isEqual(const GrOptDrawState& that) const { |
| 327 | bool usingVertexColors = this->hasColorVertexAttribute(); |
| 328 | if (!usingVertexColors && this->fColor != that.fColor) { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | if (this->getRenderTarget() != that.getRenderTarget() || |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 333 | this->fFragmentStages.count() != that.fFragmentStages.count() || |
| 334 | this->fNumColorStages != that.fNumColorStages || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 335 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
| 336 | this->fSrcBlend != that.fSrcBlend || |
| 337 | this->fDstBlend != that.fDstBlend || |
| 338 | this->fBlendConstant != that.fBlendConstant || |
| 339 | this->fFlagBits != that.fFlagBits || |
| 340 | this->fVACount != that.fVACount || |
| 341 | this->fVAStride != that.fVAStride || |
| 342 | memcmp(this->fVAPtr, that.fVAPtr, this->fVACount * sizeof(GrVertexAttrib)) || |
| 343 | this->fStencilSettings != that.fStencilSettings || |
| 344 | this->fDrawFace != that.fDrawFace || |
| 345 | this->fInputColorIsUsed != that.fInputColorIsUsed || |
| 346 | this->fInputCoverageIsUsed != that.fInputCoverageIsUsed || |
| 347 | this->fReadsDst != that.fReadsDst || |
| 348 | this->fReadsFragPosition != that.fReadsFragPosition || |
| 349 | this->fRequiresLocalCoordAttrib != that.fRequiresLocalCoordAttrib || |
| 350 | this->fPrimaryOutputType != that.fPrimaryOutputType || |
| 351 | this->fSecondaryOutputType != that.fSecondaryOutputType) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | bool usingVertexCoverage = this->hasCoverageVertexAttribute(); |
| 356 | if (!usingVertexCoverage && this->fCoverage != that.fCoverage) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | bool explicitLocalCoords = this->hasLocalCoordAttribute(); |
| 361 | if (this->hasGeometryProcessor()) { |
| 362 | if (!that.hasGeometryProcessor()) { |
| 363 | return false; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 364 | } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | } else if (that.hasGeometryProcessor()) { |
| 368 | return false; |
| 369 | } |
| 370 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 371 | for (int i = 0; i < this->numFragmentStages(); i++) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 372 | if (!GrFragmentStage::AreCompatible(this->getFragmentStage(i), that.getFragmentStage(i), |
| 373 | explicitLocalCoords)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | SkASSERT(0 == memcmp(this->fFixedFunctionVertexAttribIndices, |
| 379 | that.fFixedFunctionVertexAttribIndices, |
| 380 | sizeof(this->fFixedFunctionVertexAttribIndices))); |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |