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