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 | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 12 | #include "GrGpu.h" |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 13 | #include "GrProcOptInfo.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.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, |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 17 | const GrGeometryProcessor* gp, |
| 18 | const GrPathProcessor* pathProc, |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 19 | const GrDrawTargetCaps& caps, |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 20 | const ScissorState& scissorState, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 21 | const GrDeviceCoordTexture* dstCopy, |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 22 | GrGpu::DrawType drawType) |
| 23 | : fFinalized(false) { |
| 24 | fDrawType = drawType; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 25 | |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 26 | // Copy GeometryProcesssor from DS or ODS |
| 27 | if (gp) { |
| 28 | SkASSERT(!pathProc); |
| 29 | SkASSERT(!(GrGpu::IsPathRenderingDrawType(drawType) || |
| 30 | GrGpu::kStencilPath_DrawType == drawType)); |
| 31 | fGeometryProcessor.reset(gp); |
| 32 | fPrimitiveProcessor.reset(gp); |
| 33 | } else { |
| 34 | SkASSERT(!gp && pathProc && (GrGpu::IsPathRenderingDrawType(drawType) || |
| 35 | GrGpu::kStencilPath_DrawType == drawType)); |
| 36 | fPrimitiveProcessor.reset(pathProc); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | const GrProcOptInfo& colorPOI = drawState.colorProcInfo(fPrimitiveProcessor); |
| 41 | const GrProcOptInfo& coveragePOI = drawState.coverageProcInfo(fPrimitiveProcessor); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 42 | |
| 43 | fColor = colorPOI.inputColorToEffectiveStage(); |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 44 | // TODO fix this when coverage stages work correctly |
| 45 | // fCoverage = coveragePOI.inputColorToEffectiveStage(); |
| 46 | fCoverage = fPrimitiveProcessor->coverage(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 47 | |
| 48 | // Create XferProcessor from DS's XPFactory |
| 49 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
| 50 | drawState.getXPFactory()->createXferProcessor(colorPOI, coveragePOI)); |
| 51 | |
| 52 | GrXferProcessor::OptFlags optFlags; |
| 53 | if (xferProcessor) { |
| 54 | fXferProcessor.reset(xferProcessor.get()); |
| 55 | |
| 56 | optFlags = xferProcessor->getOptimizations(colorPOI, |
| 57 | coveragePOI, |
| 58 | drawState.isCoverageDrawing(), |
| 59 | drawState.isColorWriteDisabled(), |
| 60 | drawState.getStencil().doesWrite(), |
| 61 | &fColor, |
egdaniel | 4dffc94 | 2014-12-10 07:43:49 -0800 | [diff] [blame] | 62 | &fCoverage, |
| 63 | caps); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 64 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 65 | |
| 66 | // When path rendering the stencil settings are not always set on the draw state |
| 67 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 68 | // null GrOptDrawState. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 69 | if (!xferProcessor || ((GrXferProcessor::kSkipDraw_OptFlag & optFlags) && |
| 70 | GrGpu::kStencilPath_DrawType != drawType)) { |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 71 | // Set the fields that don't default init and return. The lack of a render target will |
| 72 | // indicate that this can be skipped. |
| 73 | fFlags = 0; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 74 | fDrawFace = GrDrawState::kInvalid_DrawFace; |
| 75 | fSrcBlend = kZero_GrBlendCoeff; |
| 76 | fDstBlend = kZero_GrBlendCoeff; |
| 77 | fBlendConstant = 0x0; |
| 78 | fViewMatrix.reset(); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | fRenderTarget.reset(drawState.fRenderTarget.get()); |
| 83 | SkASSERT(fRenderTarget); |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 84 | fScissorState = scissorState; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 85 | fViewMatrix = drawState.getViewMatrix(); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 86 | fStencilSettings = drawState.getStencil(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 87 | fDrawFace = drawState.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 88 | // TODO move this out of optDrawState |
| 89 | if (dstCopy) { |
| 90 | fDstCopy = *dstCopy; |
| 91 | } |
| 92 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 93 | fFlags = 0; |
| 94 | if (drawState.isHWAntialias()) { |
| 95 | fFlags |= kHWAA_Flag; |
| 96 | } |
| 97 | if (drawState.isColorWriteDisabled()) { |
| 98 | fFlags |= kDisableColorWrite_Flag; |
| 99 | } |
| 100 | if (drawState.isDither()) { |
| 101 | fFlags |= kDither_Flag; |
| 102 | } |
| 103 | |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 104 | fDescInfo.fHasVertexColor = gp && gp->hasVertexColor(); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 105 | |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 106 | fDescInfo.fHasVertexCoverage = gp && gp->hasVertexCoverage(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 107 | |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 108 | bool hasLocalCoords = gp && gp->hasLocalCoords(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 109 | |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 110 | int firstColorStageIdx = colorPOI.firstEffectiveStageIndex(); |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 111 | fDescInfo.fInputColorIsUsed = colorPOI.inputColorIsUsed(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 112 | if (colorPOI.removeVertexAttrib()) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 113 | fDescInfo.fHasVertexColor = false; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // TODO: Once we can handle single or four channel input into coverage stages then we can use |
| 117 | // drawState's coverageProcInfo (like color above) to set this initial information. |
| 118 | int firstCoverageStageIdx = 0; |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 119 | fDescInfo.fInputCoverageIsUsed = true; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 120 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 121 | GrXferProcessor::BlendInfo blendInfo; |
| 122 | fXferProcessor->getBlendInfo(&blendInfo); |
| 123 | fSrcBlend = blendInfo.fSrcBlend; |
| 124 | fDstBlend = blendInfo.fDstBlend; |
| 125 | fBlendConstant = blendInfo.fBlendConstant; |
| 126 | |
| 127 | this->adjustProgramFromOptimizations(drawState, optFlags, colorPOI, coveragePOI, |
| 128 | &firstColorStageIdx, &firstCoverageStageIdx); |
| 129 | |
| 130 | fDescInfo.fRequiresLocalCoordAttrib = hasLocalCoords; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 131 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 132 | // Copy Stages from DS to ODS |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 133 | for (int i = firstColorStageIdx; i < drawState.numColorStages(); ++i) { |
| 134 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 135 | GrPendingFragmentStage, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 136 | (drawState.fColorStages[i], hasLocalCoords)); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 137 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 138 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 139 | fNumColorStages = fFragmentStages.count(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 140 | for (int i = firstCoverageStageIdx; i < drawState.numCoverageStages(); ++i) { |
| 141 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 142 | GrPendingFragmentStage, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 143 | (drawState.fCoverageStages[i], hasLocalCoords)); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 144 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 145 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 146 | // let the GP init the batch tracker |
joshualitt | c6bc58e | 2014-12-10 13:48:57 -0800 | [diff] [blame^] | 147 | if (gp) { |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 148 | GrGeometryProcessor::InitBT init; |
| 149 | init.fOutputColor = fDescInfo.fInputColorIsUsed; |
| 150 | init.fOutputCoverage = fDescInfo.fInputCoverageIsUsed; |
| 151 | init.fColor = this->getColor(); |
| 152 | init.fCoverage = this->getCoverage(); |
| 153 | fGeometryProcessor->initBatchTracker(&fBatchTracker, init); |
| 154 | } |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 155 | } |
| 156 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 157 | void GrOptDrawState::adjustProgramFromOptimizations(const GrDrawState& ds, |
| 158 | GrXferProcessor::OptFlags flags, |
| 159 | const GrProcOptInfo& colorPOI, |
| 160 | const GrProcOptInfo& coveragePOI, |
| 161 | int* firstColorStageIdx, |
| 162 | int* firstCoverageStageIdx) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 163 | fDescInfo.fReadsDst = false; |
| 164 | fDescInfo.fReadsFragPosition = false; |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 165 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 166 | if (flags & GrXferProcessor::kClearColorStages_OptFlag) { |
| 167 | fDescInfo.fInputColorIsUsed = true; |
| 168 | *firstColorStageIdx = ds.numColorStages(); |
| 169 | fDescInfo.fHasVertexColor = false; |
| 170 | } else { |
| 171 | fDescInfo.fReadsDst = colorPOI.readsDst(); |
| 172 | fDescInfo.fReadsFragPosition = colorPOI.readsFragPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 173 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 174 | |
| 175 | if (flags & GrXferProcessor::kClearCoverageStages_OptFlag) { |
| 176 | fDescInfo.fInputCoverageIsUsed = true; |
| 177 | *firstCoverageStageIdx = ds.numCoverageStages(); |
| 178 | fDescInfo.fHasVertexCoverage = false; |
| 179 | } else { |
| 180 | if (coveragePOI.readsDst()) { |
| 181 | fDescInfo.fReadsDst = true; |
| 182 | } |
| 183 | if (coveragePOI.readsFragPosition()) { |
| 184 | fDescInfo.fReadsFragPosition = true; |
| 185 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 189 | void GrOptDrawState::finalize(GrGpu* gpu) { |
| 190 | gpu->buildProgramDesc(*this, fDescInfo, fDrawType, &fDesc); |
| 191 | fFinalized = true; |
| 192 | } |
| 193 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 194 | //////////////////////////////////////////////////////////////////////////////// |
| 195 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 196 | bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 197 | if (fDescInfo != that.fDescInfo) { |
joshualitt | f78c60c | 2014-12-04 06:01:45 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 200 | |
| 201 | if (!fDescInfo.fHasVertexColor && this->fColor != that.fColor) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (this->getRenderTarget() != that.getRenderTarget() || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 206 | this->fFragmentStages.count() != that.fFragmentStages.count() || |
| 207 | this->fNumColorStages != that.fNumColorStages || |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 208 | this->fScissorState != that.fScissorState || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 209 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
| 210 | this->fSrcBlend != that.fSrcBlend || |
| 211 | this->fDstBlend != that.fDstBlend || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 212 | this->fDrawType != that.fDrawType || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 213 | this->fBlendConstant != that.fBlendConstant || |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 214 | this->fFlags != that.fFlags || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 215 | this->fStencilSettings != that.fStencilSettings || |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 216 | this->fDrawFace != that.fDrawFace || |
| 217 | this->fDstCopy.texture() != that.fDstCopy.texture()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 221 | if (!fDescInfo.fHasVertexCoverage && this->fCoverage != that.fCoverage) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 225 | if (this->hasGeometryProcessor()) { |
| 226 | if (!that.hasGeometryProcessor()) { |
| 227 | return false; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 228 | } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | } else if (that.hasGeometryProcessor()) { |
| 232 | return false; |
| 233 | } |
| 234 | |
egdaniel | 4dffc94 | 2014-12-10 07:43:49 -0800 | [diff] [blame] | 235 | if (!this->getXferProcessor()->isEqual(*that.getXferProcessor())) { |
| 236 | return false; |
| 237 | } |
| 238 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 239 | // The program desc comparison should have already assured that the stage counts match. |
| 240 | SkASSERT(this->numFragmentStages() == that.numFragmentStages()); |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 241 | for (int i = 0; i < this->numFragmentStages(); i++) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 242 | |
| 243 | if (this->getFragmentStage(i) != that.getFragmentStage(i)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |