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 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 18 | GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args, |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 19 | GrPipelineOptimizations* opts) { |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 20 | const GrPipelineBuilder& builder = *args.fPipelineBuilder; |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 21 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 22 | // Create XferProcessor from DS's XPFactory |
| 23 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 24 | builder.getXPFactory()->createXferProcessor(args.fColorPOI, args.fCoveragePOI, |
| 25 | builder.hasMixedSamples(), &args.fDstTexture, |
| 26 | *args.fCaps)); |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame^] | 27 | if (!xferProcessor) { |
| 28 | return nullptr; |
| 29 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 30 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 31 | GrColor overrideColor = GrColor_ILLEGAL; |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 32 | if (args.fColorPOI.firstEffectiveStageIndex() != 0) { |
| 33 | overrideColor = args.fColorPOI.inputColorToEffectiveStage(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 36 | GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 37 | |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame^] | 38 | optFlags = xferProcessor->getOptimizations(args.fColorPOI, |
| 39 | args.fCoveragePOI, |
| 40 | builder.getStencil().doesWrite(), |
| 41 | &overrideColor, |
| 42 | *args.fCaps); |
| 43 | |
| 44 | // When path rendering the stencil settings are not always set on the GrPipelineBuilder |
| 45 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 46 | // null GrPipeline. |
| 47 | if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) { |
| 48 | return nullptr; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 49 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 50 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 51 | // No need to have an override color if it isn't even going to be used. |
| 52 | if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) { |
| 53 | overrideColor = GrColor_ILLEGAL; |
| 54 | } |
| 55 | |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame^] | 56 | GrPipeline* pipeline = SkNEW_PLACEMENT(memory, GrPipeline); |
| 57 | pipeline->fXferProcessor.reset(xferProcessor.get()); |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 58 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 59 | pipeline->fRenderTarget.reset(builder.fRenderTarget.get()); |
| 60 | SkASSERT(pipeline->fRenderTarget); |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 61 | pipeline->fScissorState = *args.fScissor; |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 62 | pipeline->fStencilSettings = builder.getStencil(); |
| 63 | pipeline->fDrawFace = builder.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 64 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 65 | pipeline->fFlags = 0; |
| 66 | if (builder.isHWAntialias()) { |
| 67 | pipeline->fFlags |= kHWAA_Flag; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 68 | } |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 69 | if (builder.isDither()) { |
| 70 | pipeline->fFlags |= kDither_Flag; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 71 | } |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 72 | if (builder.snapVerticesToPixelCenters()) { |
| 73 | pipeline->fFlags |= kSnapVertices_Flag; |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 74 | } |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 75 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 76 | int firstColorStageIdx = args.fColorPOI.firstEffectiveStageIndex(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 77 | |
| 78 | // 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] | 79 | // GrPipelineBuilder's coverageProcInfo (like color above) to set this initial information. |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 80 | int firstCoverageStageIdx = 0; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 81 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 82 | pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fColorPOI, args.fCoveragePOI, |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 83 | &firstColorStageIdx, &firstCoverageStageIdx); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 84 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 85 | bool usesLocalCoords = false; |
| 86 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 87 | // Copy Stages from PipelineBuilder to Pipeline |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 88 | for (int i = firstColorStageIdx; i < builder.numColorFragmentStages(); ++i) { |
| 89 | const GrFragmentStage& fps = builder.fColorStages[i]; |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 90 | const GrFragmentProcessor* fp = fps.processor(); |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 91 | SkNEW_APPEND_TO_TARRAY(&pipeline->fFragmentStages, GrPendingFragmentStage, (fps)); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 92 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 93 | fp->gatherCoordTransforms(&pipeline->fCoordTransforms); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 94 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 95 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 96 | pipeline->fNumColorStages = pipeline->fFragmentStages.count(); |
| 97 | for (int i = firstCoverageStageIdx; i < builder.numCoverageFragmentStages(); ++i) { |
| 98 | const GrFragmentStage& fps = builder.fCoverageStages[i]; |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 99 | const GrFragmentProcessor* fp = fps.processor(); |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 100 | SkNEW_APPEND_TO_TARRAY(&pipeline->fFragmentStages, GrPendingFragmentStage, (fps)); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 101 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 102 | fp->gatherCoordTransforms(&pipeline->fCoordTransforms); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 103 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 104 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 105 | // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline. |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 106 | opts->fFlags = 0; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 107 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 108 | opts->fFlags |= GrPipelineOptimizations::kReadsColor_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 109 | } |
| 110 | if (GrColor_ILLEGAL != overrideColor) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 111 | opts->fFlags |= GrPipelineOptimizations::kUseOverrideColor_Flag; |
| 112 | opts->fOverrideColor = overrideColor; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 113 | } |
| 114 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 115 | opts->fFlags |= GrPipelineOptimizations::kReadsCoverage_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 116 | } |
| 117 | if (usesLocalCoords) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 118 | opts->fFlags |= GrPipelineOptimizations::kReadsLocalCoords_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 119 | } |
| 120 | if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 121 | opts->fFlags |= GrPipelineOptimizations::kCanTweakAlphaForCoverage_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 122 | } |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 123 | return pipeline; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 124 | } |
| 125 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 126 | void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder, |
| 127 | GrXferProcessor::OptFlags flags, |
| 128 | const GrProcOptInfo& colorPOI, |
| 129 | const GrProcOptInfo& coveragePOI, |
| 130 | int* firstColorStageIdx, |
| 131 | int* firstCoverageStageIdx) { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 132 | fReadsFragPosition = fXferProcessor->willReadFragmentPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 133 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 134 | if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) || |
| 135 | (flags & GrXferProcessor::kOverrideColor_OptFlag)) { |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 136 | *firstColorStageIdx = pipelineBuilder.numColorFragmentStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 137 | } else { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 138 | if (coveragePOI.readsFragPosition()) { |
| 139 | fReadsFragPosition = true; |
| 140 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 141 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 142 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 143 | if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) { |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 144 | *firstCoverageStageIdx = pipelineBuilder.numCoverageFragmentStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 145 | } else { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 146 | if (coveragePOI.readsFragPosition()) { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 147 | fReadsFragPosition = true; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 148 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 152 | //////////////////////////////////////////////////////////////////////////////// |
| 153 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 154 | bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b, |
| 155 | bool ignoreCoordTransforms) { |
| 156 | SkASSERT(&a != &b); |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 157 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 158 | if (a.getRenderTarget() != b.getRenderTarget() || |
| 159 | a.fFragmentStages.count() != b.fFragmentStages.count() || |
| 160 | a.fNumColorStages != b.fNumColorStages || |
| 161 | a.fScissorState != b.fScissorState || |
| 162 | a.fFlags != b.fFlags || |
| 163 | a.fStencilSettings != b.fStencilSettings || |
| 164 | a.fDrawFace != b.fDrawFace) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 168 | if (!a.getXferProcessor()->isEqual(*b.getXferProcessor())) { |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 172 | for (int i = 0; i < a.numFragmentStages(); i++) { |
| 173 | if (!a.getFragmentStage(i).processor()->isEqual(*b.getFragmentStage(i).processor(), |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 174 | ignoreCoordTransforms)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |