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