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" |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame^] | 11 | #include "GrDrawTarget.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 12 | #include "GrGpu.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 13 | #include "GrPipelineBuilder.h" |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 14 | #include "GrProcOptInfo.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 16 | |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 17 | #include "batches/GrBatch.h" |
| 18 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 19 | GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args, |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 20 | GrPipelineOptimizations* opts) { |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 21 | const GrPipelineBuilder& builder = *args.fPipelineBuilder; |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 22 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 23 | // Create XferProcessor from DS's XPFactory |
| 24 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 25 | builder.getXPFactory()->createXferProcessor(args.fColorPOI, args.fCoveragePOI, |
| 26 | builder.hasMixedSamples(), &args.fDstTexture, |
| 27 | *args.fCaps)); |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame] | 28 | if (!xferProcessor) { |
| 29 | return nullptr; |
| 30 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 31 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 32 | GrColor overrideColor = GrColor_ILLEGAL; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 33 | if (args.fColorPOI.firstEffectiveProcessorIndex() != 0) { |
| 34 | overrideColor = args.fColorPOI.inputColorToFirstEffectiveProccesor(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 37 | GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 38 | |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame] | 39 | optFlags = xferProcessor->getOptimizations(args.fColorPOI, |
| 40 | args.fCoveragePOI, |
| 41 | builder.getStencil().doesWrite(), |
| 42 | &overrideColor, |
| 43 | *args.fCaps); |
| 44 | |
| 45 | // When path rendering the stencil settings are not always set on the GrPipelineBuilder |
| 46 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 47 | // null GrPipeline. |
| 48 | if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) { |
| 49 | return nullptr; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 50 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 51 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 52 | // No need to have an override color if it isn't even going to be used. |
| 53 | if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) { |
| 54 | overrideColor = GrColor_ILLEGAL; |
| 55 | } |
| 56 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 57 | GrPipeline* pipeline = new (memory) GrPipeline; |
bsalomon | c8d3f57 | 2015-08-13 06:44:04 -0700 | [diff] [blame] | 58 | pipeline->fXferProcessor.reset(xferProcessor.get()); |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 59 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 60 | pipeline->fRenderTarget.reset(builder.fRenderTarget.get()); |
| 61 | SkASSERT(pipeline->fRenderTarget); |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 62 | pipeline->fScissorState = *args.fScissor; |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 63 | pipeline->fStencilSettings = builder.getStencil(); |
| 64 | pipeline->fDrawFace = builder.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 65 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 66 | pipeline->fFlags = 0; |
| 67 | if (builder.isHWAntialias()) { |
| 68 | pipeline->fFlags |= kHWAA_Flag; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 69 | } |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 70 | if (builder.snapVerticesToPixelCenters()) { |
| 71 | pipeline->fFlags |= kSnapVertices_Flag; |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 72 | } |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 73 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 74 | int firstColorProcessorIdx = args.fColorPOI.firstEffectiveProcessorIndex(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 75 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 76 | // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors |
| 77 | // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial |
| 78 | // information. |
| 79 | int firstCoverageProcessorIdx = 0; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 80 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 81 | pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fColorPOI, args.fCoveragePOI, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 82 | &firstColorProcessorIdx, &firstCoverageProcessorIdx); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 83 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 84 | bool usesLocalCoords = false; |
| 85 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 86 | // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline |
| 87 | pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx; |
| 88 | int numTotalProcessors = pipeline->fNumColorProcessors + |
| 89 | builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx; |
| 90 | pipeline->fFragmentProcessors.reset(numTotalProcessors); |
| 91 | int currFPIdx = 0; |
| 92 | for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors(); |
| 93 | ++i, ++currFPIdx) { |
| 94 | const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i); |
| 95 | pipeline->fFragmentProcessors[currFPIdx].reset(fp); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 96 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 97 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 98 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 99 | for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors(); |
| 100 | ++i, ++currFPIdx) { |
| 101 | const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i); |
| 102 | pipeline->fFragmentProcessors[currFPIdx].reset(fp); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 103 | usesLocalCoords = usesLocalCoords || fp->usesLocalCoords(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 104 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 105 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 106 | // 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] | 107 | opts->fFlags = 0; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 108 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 109 | opts->fFlags |= GrPipelineOptimizations::kReadsColor_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 110 | } |
| 111 | if (GrColor_ILLEGAL != overrideColor) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 112 | opts->fFlags |= GrPipelineOptimizations::kUseOverrideColor_Flag; |
| 113 | opts->fOverrideColor = overrideColor; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 114 | } |
| 115 | if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 116 | opts->fFlags |= GrPipelineOptimizations::kReadsCoverage_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 117 | } |
| 118 | if (usesLocalCoords) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 119 | opts->fFlags |= GrPipelineOptimizations::kReadsLocalCoords_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 120 | } |
| 121 | if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 122 | opts->fFlags |= GrPipelineOptimizations::kCanTweakAlphaForCoverage_Flag; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 123 | } |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 124 | |
| 125 | GrXPFactory::InvariantBlendedColor blendedColor; |
| 126 | builder.fXPFactory->getInvariantBlendedColor(args.fColorPOI, &blendedColor); |
| 127 | if (blendedColor.fWillBlendWithDst) { |
| 128 | opts->fFlags |= GrPipelineOptimizations::kWillColorBlendWithDst_Flag; |
| 129 | } |
| 130 | |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 131 | return pipeline; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame^] | 134 | static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) { |
| 135 | for (int i = 0; i < proc->numChildProcessors(); ++i) { |
| 136 | // need to recurse |
| 137 | add_dependencies_for_processor(&proc->childProcessor(i), rt); |
| 138 | } |
| 139 | |
| 140 | for (int i = 0; i < proc->numTextures(); ++i) { |
| 141 | GrTexture* texture = proc->textureAccess(i).getTexture(); |
| 142 | SkASSERT(rt->getLastDrawTarget()); |
| 143 | rt->getLastDrawTarget()->addDependency(texture); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void GrPipeline::addDependenciesTo(GrRenderTarget* rt) const { |
| 148 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 149 | add_dependencies_for_processor(fFragmentProcessors[i].get(), rt); |
| 150 | } |
| 151 | |
| 152 | if (fXferProcessor.get()) { |
| 153 | const GrXferProcessor* xfer = fXferProcessor.get(); |
| 154 | |
| 155 | for (int i = 0; i < xfer->numTextures(); ++i) { |
| 156 | GrTexture* texture = xfer->textureAccess(i).getTexture(); |
| 157 | SkASSERT(rt->getLastDrawTarget()); |
| 158 | rt->getLastDrawTarget()->addDependency(texture); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 163 | void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder, |
| 164 | GrXferProcessor::OptFlags flags, |
| 165 | const GrProcOptInfo& colorPOI, |
| 166 | const GrProcOptInfo& coveragePOI, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 167 | int* firstColorProcessorIdx, |
| 168 | int* firstCoverageProcessorIdx) { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 169 | fReadsFragPosition = fXferProcessor->willReadFragmentPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 170 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 171 | if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) || |
| 172 | (flags & GrXferProcessor::kOverrideColor_OptFlag)) { |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 173 | *firstColorProcessorIdx = pipelineBuilder.numColorFragmentProcessors(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 174 | } else { |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 175 | if (coveragePOI.readsFragPosition()) { |
| 176 | fReadsFragPosition = true; |
| 177 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 178 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 179 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 180 | if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) { |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 181 | *firstCoverageProcessorIdx = pipelineBuilder.numCoverageFragmentProcessors(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 182 | } else { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 183 | if (coveragePOI.readsFragPosition()) { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 184 | fReadsFragPosition = true; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 185 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 189 | //////////////////////////////////////////////////////////////////////////////// |
| 190 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 191 | bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b, |
| 192 | bool ignoreCoordTransforms) { |
| 193 | SkASSERT(&a != &b); |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 194 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 195 | if (a.getRenderTarget() != b.getRenderTarget() || |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 196 | a.fFragmentProcessors.count() != b.fFragmentProcessors.count() || |
| 197 | a.fNumColorProcessors != b.fNumColorProcessors || |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 198 | a.fScissorState != b.fScissorState || |
| 199 | a.fFlags != b.fFlags || |
| 200 | a.fStencilSettings != b.fStencilSettings || |
| 201 | a.fDrawFace != b.fDrawFace) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 205 | if (!a.getXferProcessor()->isEqual(*b.getXferProcessor())) { |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 209 | for (int i = 0; i < a.numFragmentProcessors(); i++) { |
| 210 | if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i), ignoreCoordTransforms)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |