csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #ifndef gr_instanced_InstancedRendering_DEFINED |
| 9 | #define gr_instanced_InstancedRendering_DEFINED |
| 10 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 11 | #include "../private/GrInstancedPipelineInfo.h" |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 13 | #include "GrMemoryPool.h" |
| 14 | #include "SkTInternalLList.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 15 | #include "instanced/InstancedRenderingTypes.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 16 | #include "ops/GrDrawOp.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 17 | |
| 18 | class GrResourceProvider; |
| 19 | |
| 20 | namespace gr_instanced { |
| 21 | |
| 22 | class InstanceProcessor; |
| 23 | |
| 24 | /** |
| 25 | * This class serves as a centralized clearinghouse for instanced rendering. It accumulates data for |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 26 | * instanced draws into one location, and creates special ops that pull from this data. The |
| 27 | * nature of instanced rendering allows these ops to combine well and render efficiently. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 28 | * |
| 29 | * During a flush, this class assembles the accumulated draw data into a single vertex and texel |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 30 | * buffer, and its subclass draws the ops using backend-specific instanced rendering APIs. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 31 | * |
| 32 | * This class is responsible for the CPU side of instanced rendering. Shaders are implemented by |
| 33 | * InstanceProcessor. |
| 34 | */ |
| 35 | class InstancedRendering : public SkNoncopyable { |
| 36 | public: |
| 37 | virtual ~InstancedRendering() { SkASSERT(State::kRecordingDraws == fState); } |
| 38 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 39 | GrGpu* gpu() const { return fGpu.get(); } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 42 | * These methods make a new record internally for an instanced draw, and return an op that is |
| 43 | * effectively just an index to that record. The returned op is not self-contained, but |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 44 | * rather relies on this class to handle the rendering. The client must call beginFlush() on |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 45 | * this class before attempting to flush ops returned by it. It is invalid to record new |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 46 | * draws between beginFlush() and endFlush(). |
| 47 | */ |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 48 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, |
| 49 | GrColor, GrAA, |
| 50 | const GrInstancedPipelineInfo&, |
| 51 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 52 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 53 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, |
| 54 | GrColor, const SkRect& localRect, |
| 55 | GrAA, const GrInstancedPipelineInfo&, |
| 56 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 57 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 58 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, |
| 59 | GrColor, const SkMatrix& localMatrix, |
| 60 | GrAA, const GrInstancedPipelineInfo&, |
| 61 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 62 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 63 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordOval(const SkRect&, const SkMatrix&, |
| 64 | GrColor, GrAA, |
| 65 | const GrInstancedPipelineInfo&, |
| 66 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 67 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 68 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordRRect(const SkRRect&, const SkMatrix&, |
| 69 | GrColor, GrAA, |
| 70 | const GrInstancedPipelineInfo&, |
| 71 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 72 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 73 | std::unique_ptr<GrDrawOp> SK_WARN_UNUSED_RESULT recordDRRect(const SkRRect& outer, |
| 74 | const SkRRect& inner, |
| 75 | const SkMatrix&, GrColor, GrAA, |
| 76 | const GrInstancedPipelineInfo&, |
| 77 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Compiles all recorded draws into GPU buffers and allows the client to begin flushing the |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 81 | * ops created by this class. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 82 | */ |
| 83 | void beginFlush(GrResourceProvider*); |
| 84 | |
| 85 | /** |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 86 | * Called once the ops created previously by this class have all been released. Allows the |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 87 | * client to begin recording draws again. |
| 88 | */ |
| 89 | void endFlush(); |
| 90 | |
| 91 | enum class ResetType : bool { |
| 92 | kDestroy, |
| 93 | kAbandon |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * Resets all GPU resources, including those that are held long term. They will be lazily |
| 98 | * reinitialized if the class begins to be used again. |
| 99 | */ |
| 100 | void resetGpuResources(ResetType); |
| 101 | |
| 102 | protected: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 103 | class Op : public GrDrawOp { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 104 | public: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 105 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Op); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 106 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 107 | ~Op() override; |
| 108 | const char* name() const override { return "InstancedRendering::Op"; } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 109 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 110 | SkString dumpInfo() const override { |
| 111 | SkString string; |
Brian Salomon | af9847e | 2017-03-01 11:28:27 -0500 | [diff] [blame] | 112 | string.printf( |
| 113 | "AA: %d, ShapeTypes: 0x%02x, IShapeTypes: 0x%02x, Persp %d, " |
| 114 | "NonSquare: %d, PLoad: %0.2f, Tracked: %d, NumDraws: %d, " |
| 115 | "GeomChanges: %d\n", |
| 116 | (unsigned)fInfo.fAAType, |
| 117 | fInfo.fShapeTypes, |
| 118 | fInfo.fInnerShapeTypes, |
| 119 | fInfo.fHasPerspective, |
| 120 | fInfo.fNonSquare, |
| 121 | fPixelLoad, |
| 122 | fIsTracked, |
| 123 | fNumDraws, |
| 124 | fNumChangesInGeometry); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 125 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 126 | string.append(INHERITED::dumpInfo()); |
| 127 | return string; |
| 128 | } |
| 129 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 130 | struct Draw { |
| 131 | Instance fInstance; |
| 132 | IndexRange fGeometry; |
| 133 | Draw* fNext; |
| 134 | }; |
| 135 | |
| 136 | Draw& getSingleDraw() const { SkASSERT(fHeadDraw && !fHeadDraw->fNext); return *fHeadDraw; } |
| 137 | Instance& getSingleInstance() const { return this->getSingleDraw().fInstance; } |
| 138 | |
| 139 | void appendRRectParams(const SkRRect&); |
| 140 | void appendParamsTexel(const SkScalar* vals, int count); |
| 141 | void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w); |
| 142 | void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z); |
| 143 | |
Brian Salomon | d543e0a | 2017-03-06 16:36:49 -0500 | [diff] [blame^] | 144 | // Registers the op with the InstancedRendering list of tracked ops. |
| 145 | void wasRecorded() override; |
| 146 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 147 | protected: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 148 | Op(uint32_t classID, InstancedRendering* ir); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 149 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 150 | InstancedRendering* const fInstancedRendering; |
| 151 | OpInfo fInfo; |
| 152 | SkScalar fPixelLoad; |
| 153 | SkSTArray<5, ParamsTexel, true> fParams; |
| 154 | bool fIsTracked; |
| 155 | int fNumDraws; |
| 156 | int fNumChangesInGeometry; |
| 157 | Draw* fHeadDraw; |
| 158 | Draw* fTailDraw; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 159 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 160 | private: |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 161 | void getFragmentProcessorAnalysisInputs( |
| 162 | FragmentProcessorAnalysisInputs* input) const override; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 163 | void applyPipelineOptimizations(const GrPipelineOptimizations&) override; |
| 164 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override; |
| 165 | void onPrepare(GrOpFlushState*) override {} |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 166 | void onExecute(GrOpFlushState*) override; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 167 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 168 | typedef GrDrawOp INHERITED; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 169 | |
| 170 | friend class InstancedRendering; |
| 171 | }; |
| 172 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 173 | typedef SkTInternalLList<Op> OpList; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 174 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 175 | InstancedRendering(GrGpu* gpu); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 176 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 177 | const OpList& trackedOps() const { return fTrackedOps; } |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 178 | const GrBuffer* vertexBuffer() const { SkASSERT(fVertexBuffer); return fVertexBuffer.get(); } |
| 179 | const GrBuffer* indexBuffer() const { SkASSERT(fIndexBuffer); return fIndexBuffer.get(); } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 180 | |
| 181 | virtual void onBeginFlush(GrResourceProvider*) = 0; |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 182 | virtual void onDraw(const GrPipeline&, const InstanceProcessor&, const Op*) = 0; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 183 | virtual void onEndFlush() = 0; |
| 184 | virtual void onResetGpuResources(ResetType) = 0; |
| 185 | |
| 186 | private: |
| 187 | enum class State : bool { |
| 188 | kRecordingDraws, |
| 189 | kFlushing |
| 190 | }; |
| 191 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 192 | std::unique_ptr<Op> SK_WARN_UNUSED_RESULT recordShape(ShapeType, const SkRect& bounds, |
| 193 | const SkMatrix& viewMatrix, GrColor, |
| 194 | const SkRect& localRect, GrAA aa, |
| 195 | const GrInstancedPipelineInfo&, |
| 196 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 197 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 198 | bool selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa, const GrInstancedPipelineInfo&, |
Brian Salomon | af9847e | 2017-03-01 11:28:27 -0500 | [diff] [blame] | 199 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 200 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 201 | virtual std::unique_ptr<Op> makeOp() = 0; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 202 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 203 | const sk_sp<GrGpu> fGpu; |
| 204 | State fState; |
| 205 | GrObjectMemoryPool<Op::Draw> fDrawPool; |
| 206 | SkSTArray<1024, ParamsTexel, true> fParams; |
| 207 | OpList fTrackedOps; |
| 208 | sk_sp<const GrBuffer> fVertexBuffer; |
| 209 | sk_sp<const GrBuffer> fIndexBuffer; |
| 210 | sk_sp<GrBuffer> fParamsBuffer; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | #endif |