egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 1 | /* |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2 | * Copyright 2015 Google Inc. |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 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 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 8 | #include "GrPipeline.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 9 | |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 11 | #include "GrGpu.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 12 | #include "GrPipelineBuilder.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 | |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame^] | 16 | #include "batches/GrBatch.h" |
| 17 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 18 | GrPipeline::GrPipeline(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 19 | const GrProcOptInfo& colorPOI, |
| 20 | const GrProcOptInfo& coveragePOI, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 21 | const GrCaps& caps, |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 22 | const GrScissorState& scissorState, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 23 | const GrXferProcessor::DstTexture* dstTexture) { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 24 | // Create XferProcessor from DS's XPFactory |
| 25 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 26 | pipelineBuilder.getXPFactory()->createXferProcessor( |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 27 | colorPOI, coveragePOI, pipelineBuilder.hasMixedSamples(), dstTexture, caps)); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 28 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 29 | GrColor overrideColor = GrColor_ILLEGAL; |
| 30 | if (colorPOI.firstEffectiveStageIndex() != 0) { |
| 31 | overrideColor = colorPOI.inputColorToEffectiveStage(); |
| 32 | } |
| 33 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 34 | GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 35 | if (xferProcessor) { |
| 36 | fXferProcessor.reset(xferProcessor.get()); |
| 37 | |
| 38 | optFlags = xferProcessor->getOptimizations(colorPOI, |
| 39 | coveragePOI, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 40 | pipelineBuilder.getStencil().doesWrite(), |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 41 | &overrideColor, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 42 | caps); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 43 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 44 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 45 | // No need to have an override color if it isn't even going to be used. |
| 46 | if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) { |
| 47 | overrideColor = GrColor_ILLEGAL; |
| 48 | } |
| 49 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 50 | // When path rendering the stencil settings are not always set on the GrPipelineBuilder |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 51 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 52 | // null GrPipeline. |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 53 | if (!xferProcessor || (GrXferProcessor::kSkipDraw_OptFlag & optFlags)) { |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 54 | // Set the fields that don't default init and return. The lack of a render target will |
| 55 | // indicate that this can be skipped. |
| 56 | fFlags = 0; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 57 | fDrawFace = GrPipelineBuilder::kInvalid_DrawFace; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 61 | fRenderTarget.reset(pipelineBuilder.fRenderTarget.get()); |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 62 | SkASSERT(fRenderTarget); |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 63 | fScissorState = scissorState; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 64 | fStencilSettings = pipelineBuilder.getStencil(); |
| 65 | fDrawFace = pipelineBuilder.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 66 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 67 | fFlags = 0; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 68 | if (pipelineBuilder.isHWAntialias()) { |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 69 | fFlags |= kHWAA_Flag; |
| 70 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 71 | if (pipelineBuilder.isDither()) { |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 72 | fFlags |= kDither_Flag; |
| 73 | } |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 74 | if (pipelineBuilder.snapVerticesToPixelCenters()) { |
| 75 | fFlags |= kSnapVertices_Flag; |
| 76 | } |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 77 | |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 78 | int firstColorStageIdx = colorPOI.firstEffectiveStageIndex(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 79 | |
| 80 | // TODO: Once we can handle single or four channel input into coverage stages then we can use |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 81 | // GrPipelineBuilder's coverageProcInfo (like color above) to set this initial information. |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 82 | int firstCoverageStageIdx = 0; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 83 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 84 | this->adjustProgramFromOptimizations(pipelineBuilder, optFlags, colorPOI, coveragePOI, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 85 | &firstColorStageIdx, &firstCoverageStageIdx); |
| 86 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 87 | bool usesLocalCoords = false; |
| 88 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 89 | // Copy Stages from PipelineBuilder to Pipeline |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 90 | for (int i = firstColorStageIdx; i < pipelineBuilder.numColorFragmentStages(); ++i) { |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 91 | const GrFragmentStage& fps = pipelineBuilder.fColorStages[i]; |
| 92 | const GrFragmentProcessor* fp = fps.processor(); |
| 93 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, GrPendingFragmentStage, (fps)); |
| 94 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
| 95 | fp->gatherCoordTransforms(&fCoordTransforms); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 96 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 97 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 98 | fNumColorStages = fFragmentStages.count(); |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 99 | for (int i = firstCoverageStageIdx; i < pipelineBuilder.numCoverageFragmentStages(); ++i) { |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 100 | const GrFragmentStage& fps = pipelineBuilder.fCoverageStages[i]; |
| 101 | const GrFragmentProcessor* fp = fps.processor(); |
| 102 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, GrPendingFragmentStage, (fps)); |
| 103 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
| 104 | fp->gatherCoordTransforms(&fCoordTransforms); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 105 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 106 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 107 | // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline. |
| 108 | fInfoForPrimitiveProcessor.fFlags = 0; |
| 109 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) { |
| 110 | fInfoForPrimitiveProcessor.fFlags |= GrPipelineInfo::kReadsColor_GrPipelineInfoFlag; |
| 111 | } |
| 112 | if (GrColor_ILLEGAL != overrideColor) { |
| 113 | fInfoForPrimitiveProcessor.fFlags |= GrPipelineInfo::kUseOverrideColor_GrPipelineInfoFlag; |
| 114 | fInfoForPrimitiveProcessor.fOverrideColor = overrideColor; |
| 115 | } |
| 116 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) { |
| 117 | fInfoForPrimitiveProcessor.fFlags |= GrPipelineInfo::kReadsCoverage_GrPipelineInfoFlag; |
| 118 | } |
| 119 | if (usesLocalCoords) { |
| 120 | fInfoForPrimitiveProcessor.fFlags |= GrPipelineInfo::kReadsLocalCoords_GrPipelineInfoFlag; |
| 121 | } |
| 122 | if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) { |
| 123 | fInfoForPrimitiveProcessor.fFlags |= |
| 124 | GrPipelineInfo::kCanTweakAlphaForCoverage_GrPipelineInfoFlag; |
| 125 | } |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 128 | void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder, |
| 129 | GrXferProcessor::OptFlags flags, |
| 130 | const GrProcOptInfo& colorPOI, |
| 131 | const GrProcOptInfo& coveragePOI, |
| 132 | int* firstColorStageIdx, |
| 133 | int* firstCoverageStageIdx) { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 134 | fReadsFragPosition = fXferProcessor->willReadFragmentPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 135 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 136 | if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) || |
| 137 | (flags & GrXferProcessor::kOverrideColor_OptFlag)) { |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 138 | *firstColorStageIdx = pipelineBuilder.numColorFragmentStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 139 | } else { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 140 | if (coveragePOI.readsFragPosition()) { |
| 141 | fReadsFragPosition = true; |
| 142 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 143 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 144 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 145 | if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) { |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 146 | *firstCoverageStageIdx = pipelineBuilder.numCoverageFragmentStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 147 | } else { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 148 | if (coveragePOI.readsFragPosition()) { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 149 | fReadsFragPosition = true; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 150 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 154 | //////////////////////////////////////////////////////////////////////////////// |
| 155 | |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 156 | bool GrPipeline::isEqual(const GrPipeline& that, bool ignoreCoordTransforms) const { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 157 | // If we point to the same pipeline, then we are necessarily equal |
| 158 | if (this == &that) { |
| 159 | return true; |
| 160 | } |
| 161 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 162 | if (this->getRenderTarget() != that.getRenderTarget() || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 163 | this->fFragmentStages.count() != that.fFragmentStages.count() || |
| 164 | this->fNumColorStages != that.fNumColorStages || |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 165 | this->fScissorState != that.fScissorState || |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 166 | this->fFlags != that.fFlags || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 167 | this->fStencilSettings != that.fStencilSettings || |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 168 | this->fDrawFace != that.fDrawFace) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 172 | if (!this->getXferProcessor()->isEqual(*that.getXferProcessor())) { |
| 173 | return false; |
| 174 | } |
| 175 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 176 | for (int i = 0; i < this->numFragmentStages(); i++) { |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 177 | if (!this->getFragmentStage(i).processor()->isEqual(*that.getFragmentStage(i).processor(), |
| 178 | ignoreCoordTransforms)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |