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