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