bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 8 | #ifndef GrMeshDrawOp_DEFINED |
| 9 | #define GrMeshDrawOp_DEFINED |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 10 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 11 | #include "GrDrawOp.h" |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 14 | #include "GrPendingProgramElement.h" |
Brian Salomon | e14bd80 | 2017-04-04 15:13:25 -0400 | [diff] [blame] | 15 | #include "GrPipelineBuilder.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 16 | |
| 17 | #include "SkTLList.h" |
| 18 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 19 | class GrCaps; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 20 | class GrOpFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 23 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 25 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 26 | public: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 27 | class Target; |
| 28 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 29 | protected: |
| 30 | GrMeshDrawOp(uint32_t classID); |
| 31 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 32 | /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the |
| 33 | space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */ |
| 34 | class PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 35 | public: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 36 | PatternHelper() {} |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 37 | /** Returns the allocated storage for the vertices. The caller should populate the vertices |
| 38 | before calling recordDraws(). */ |
| 39 | void* init(Target*, GrPrimitiveType, size_t vertexStride, const GrBuffer*, |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 40 | int verticesPerRepetition, int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 41 | |
| 42 | /** Call after init() to issue draws to the GrMeshDrawOp::Target.*/ |
| 43 | void recordDraw(Target*, const GrGeometryProcessor*, const GrPipeline*); |
| 44 | |
| 45 | private: |
| 46 | GrMesh fMesh; |
| 47 | }; |
| 48 | |
| 49 | static const int kVerticesPerQuad = 4; |
| 50 | static const int kIndicesPerQuad = 6; |
| 51 | |
| 52 | /** A specialization of InstanceHelper for quad rendering. */ |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 53 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 54 | public: |
| 55 | QuadHelper() : INHERITED() {} |
| 56 | /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure |
| 57 | and on success a pointer to the vertex data that the caller should populate before |
| 58 | calling recordDraws(). */ |
| 59 | void* init(Target*, size_t vertexStride, int quadsToDraw); |
| 60 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 61 | using PatternHelper::recordDraw; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 62 | |
| 63 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame^] | 64 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | private: |
| 68 | void onPrepare(GrOpFlushState* state) final; |
| 69 | void onExecute(GrOpFlushState* state) final; |
| 70 | |
| 71 | virtual void onPrepareDraws(Target*) const = 0; |
| 72 | |
| 73 | // A set of contiguous draws that share a draw token and primitive processor. The draws all use |
| 74 | // the op's pipeline. The meshes for the draw are stored in the fMeshes array and each |
| 75 | // Queued draw uses fMeshCnt meshes from the fMeshes array. The reason for coallescing meshes |
| 76 | // that share a primitive processor into a QueuedDraw is that it allows the Gpu object to setup |
| 77 | // the shared state once and then issue draws for each mesh. |
| 78 | struct QueuedDraw { |
| 79 | int fMeshCnt = 0; |
| 80 | GrPendingProgramElement<const GrGeometryProcessor> fGeometryProcessor; |
| 81 | const GrPipeline* fPipeline; |
| 82 | }; |
| 83 | |
| 84 | // All draws in all the GrMeshDrawOps have implicit tokens based on the order they are enqueued |
| 85 | // globally across all ops. This is the offset of the first entry in fQueuedDraws. |
| 86 | // fQueuedDraws[i]'s token is fBaseDrawToken + i. |
| 87 | GrDrawOpUploadToken fBaseDrawToken; |
| 88 | SkSTArray<4, GrMesh> fMeshes; |
| 89 | SkSTArray<4, QueuedDraw, true> fQueuedDraws; |
| 90 | |
| 91 | typedef GrDrawOp INHERITED; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Many of our ops derive from this class which initializes a GrPipeline just before being recorded. |
| 96 | * We are migrating away from use of this class. |
| 97 | */ |
| 98 | class GrLegacyMeshDrawOp : public GrMeshDrawOp { |
| 99 | public: |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Performs analysis of the fragment processors in GrProcessorSet and GrAppliedClip using the |
| 102 | * initial color and coverage from this op's geometry processor. |
| 103 | */ |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 104 | GrProcessorSet::Analysis analyzeUpdateAndRecordProcessors(GrPipelineBuilder* pipelineBuilder, |
| 105 | const GrAppliedClip* appliedClip, |
| 106 | bool isMixedSamples, |
| 107 | const GrCaps& caps, |
| 108 | GrColor* overrideColor) const { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 109 | GrProcessorAnalysisColor inputColor; |
| 110 | GrProcessorAnalysisCoverage inputCoverage; |
| 111 | this->getProcessorAnalysisInputs(&inputColor, &inputCoverage); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 112 | return pipelineBuilder->finalizeProcessors(inputColor, inputCoverage, appliedClip, |
| 113 | isMixedSamples, caps, overrideColor); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 116 | void initPipeline(const GrPipeline::InitArgs& args, const GrProcessorSet::Analysis& analysis, |
| 117 | GrColor overrideColor) { |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 118 | fPipeline.init(args); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 119 | this->applyPipelineOptimizations(PipelineOptimizations(analysis, overrideColor)); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 122 | void addDependenciesTo(GrRenderTargetProxy* rtp) { |
| 123 | fPipeline.addDependenciesTo(rtp); |
| 124 | } |
| 125 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 126 | /** |
| 127 | * Mesh draw ops use a legacy system in GrRenderTargetContext where the pipeline is created when |
| 128 | * the op is recorded. These methods are unnecessary as this information is in the pipeline. |
| 129 | */ |
| 130 | FixedFunctionFlags fixedFunctionFlags() const override { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 131 | SkFAIL("This should never be called for legacy mesh draw ops."); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 132 | return FixedFunctionFlags::kNone; |
| 133 | } |
| 134 | bool xpRequiresDstTexture(const GrCaps&, const GrAppliedClip*) override { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 135 | SkFAIL("Should never be called for legacy mesh draw ops."); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 136 | return false; |
| 137 | } |
Brian Salomon | 2bf4b3a | 2017-03-16 14:19:07 -0400 | [diff] [blame] | 138 | |
Brian Salomon | c48af93 | 2017-03-16 19:51:42 +0000 | [diff] [blame] | 139 | protected: |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 140 | GrLegacyMeshDrawOp(uint32_t classID) : INHERITED(classID) {} |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 141 | /** |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 142 | * This is a legacy class only used by GrLegacyMeshDrawOp and will be removed. It presents some |
| 143 | * aspects of GrProcessorSet::Analysis to GrLegacyMeshDrawOp subclasses. |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 144 | */ |
| 145 | class PipelineOptimizations { |
| 146 | public: |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 147 | PipelineOptimizations(const GrProcessorSet::Analysis& analysis, GrColor overrideColor) { |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 148 | fFlags = 0; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 149 | if (analysis.inputColorIsOverridden()) { |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 150 | fFlags |= kUseOverrideColor_Flag; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 151 | fOverrideColor = overrideColor; |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 152 | } |
| 153 | if (analysis.usesLocalCoords()) { |
| 154 | fFlags |= kReadsLocalCoords_Flag; |
| 155 | } |
| 156 | if (analysis.isCompatibleWithCoverageAsAlpha()) { |
| 157 | fFlags |= kCanTweakAlphaForCoverage_Flag; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 162 | bool readsLocalCoords() const { return SkToBool(kReadsLocalCoords_Flag & fFlags); } |
| 163 | |
| 164 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 165 | color output ? */ |
| 166 | bool canTweakAlphaForCoverage() const { |
| 167 | return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags); |
| 168 | } |
| 169 | |
| 170 | /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if |
| 171 | so get the color)? */ |
| 172 | bool getOverrideColorIfSet(GrColor* overrideColor) const { |
| 173 | if (SkToBool(kUseOverrideColor_Flag & fFlags)) { |
| 174 | if (overrideColor) { |
| 175 | *overrideColor = fOverrideColor; |
| 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | enum { |
| 184 | // If this is not set the primitive processor need not produce local coordinates |
| 185 | kReadsLocalCoords_Flag = 0x1, |
| 186 | // If this flag is set then the primitive processor may produce color*coverage as |
| 187 | // its color output (and not output a separate coverage). |
| 188 | kCanTweakAlphaForCoverage_Flag = 0x2, |
| 189 | // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its |
| 190 | // output color. If not set fOverrideColor is to be ignored. |
| 191 | kUseOverrideColor_Flag = 0x4, |
| 192 | }; |
| 193 | |
| 194 | uint32_t fFlags; |
| 195 | GrColor fOverrideColor; |
| 196 | }; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 197 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 198 | const GrPipeline* pipeline() const { |
| 199 | SkASSERT(fPipeline.isInitialized()); |
| 200 | return &fPipeline; |
| 201 | } |
| 202 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 203 | private: |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 204 | /** |
| 205 | * Provides information about the GrPrimitiveProccesor color and coverage outputs which become |
| 206 | * inputs to the first color and coverage fragment processors. |
| 207 | */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 208 | virtual void getProcessorAnalysisInputs(GrProcessorAnalysisColor*, |
| 209 | GrProcessorAnalysisCoverage*) const = 0; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 210 | |
| 211 | /** |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 212 | * After processor analysis is complete this is called so that the op can use the analysis |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 213 | * results when constructing its GrPrimitiveProcessor. |
| 214 | */ |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 215 | virtual void applyPipelineOptimizations(const PipelineOptimizations&) = 0; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 216 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 217 | GrPipeline fPipeline; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 218 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 219 | typedef GrMeshDrawOp INHERITED; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | #endif |