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