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 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 11 | #include "GrGpu.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
| 13 | #include "SkTInternalLList.h" |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 14 | #include "batches/GrDrawOp.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 15 | #include "instanced/InstancedRenderingTypes.h" |
| 16 | #include "../private/GrInstancedPipelineInfo.h" |
| 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 | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 26 | * instanced draws into one location, and creates special ops that pull from this data. The nature |
| 27 | * 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 | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 48 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor, GrAA, |
| 49 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 50 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 51 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor, |
| 52 | const SkRect& localRect, GrAA, |
| 53 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 54 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 55 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor, |
| 56 | const SkMatrix& localMatrix, GrAA, |
| 57 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 58 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 59 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordOval(const SkRect&, const SkMatrix&, GrColor, GrAA, |
| 60 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 61 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 62 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordRRect(const SkRRect&, const SkMatrix&, GrColor, |
| 63 | GrAA, const GrInstancedPipelineInfo&, |
| 64 | GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 65 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 66 | sk_sp<GrDrawOp> SK_WARN_UNUSED_RESULT recordDRRect(const SkRRect& outer, const SkRRect& inner, |
| 67 | const SkMatrix&, GrColor, GrAA, |
| 68 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * 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^] | 72 | * ops created by this class. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 73 | */ |
| 74 | void beginFlush(GrResourceProvider*); |
| 75 | |
| 76 | /** |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 77 | * 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] | 78 | * client to begin recording draws again. |
| 79 | */ |
| 80 | void endFlush(); |
| 81 | |
| 82 | enum class ResetType : bool { |
| 83 | kDestroy, |
| 84 | kAbandon |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Resets all GPU resources, including those that are held long term. They will be lazily |
| 89 | * reinitialized if the class begins to be used again. |
| 90 | */ |
| 91 | void resetGpuResources(ResetType); |
| 92 | |
| 93 | protected: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 94 | class Op : public GrDrawOp { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 95 | public: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 96 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Op); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 98 | ~Op() override; |
| 99 | const char* name() const override { return "InstancedRendering::Op"; } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 100 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 101 | SkString dumpInfo() const override { |
| 102 | SkString string; |
| 103 | string.printf("AA: %d, ShapeTypes: 0x%02x, IShapeTypes: 0x%02x, Persp %d, " |
| 104 | "NonSquare: %d, PLoad: %0.2f, Tracked: %d, NumDraws: %d, " |
| 105 | "GeomChanges: %d\n", |
| 106 | (int)fInfo.fAntialiasMode, |
| 107 | fInfo.fShapeTypes, |
| 108 | fInfo.fInnerShapeTypes, |
| 109 | fInfo.fHasPerspective, |
| 110 | fInfo.fNonSquare, |
| 111 | fPixelLoad, |
| 112 | fIsTracked, |
| 113 | fNumDraws, |
| 114 | fNumChangesInGeometry); |
| 115 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 116 | string.append(INHERITED::dumpInfo()); |
| 117 | return string; |
| 118 | } |
| 119 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 120 | struct Draw { |
| 121 | Instance fInstance; |
| 122 | IndexRange fGeometry; |
| 123 | Draw* fNext; |
| 124 | }; |
| 125 | |
| 126 | Draw& getSingleDraw() const { SkASSERT(fHeadDraw && !fHeadDraw->fNext); return *fHeadDraw; } |
| 127 | Instance& getSingleInstance() const { return this->getSingleDraw().fInstance; } |
| 128 | |
| 129 | void appendRRectParams(const SkRRect&); |
| 130 | void appendParamsTexel(const SkScalar* vals, int count); |
| 131 | void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w); |
| 132 | void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z); |
| 133 | |
| 134 | protected: |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 135 | Op(uint32_t classID, InstancedRendering* ir); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 136 | |
| 137 | void initBatchTracker(const GrXPOverridesForBatch&) override; |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 138 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 139 | |
| 140 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 141 | GrInitInvariantOutput* coverage, |
| 142 | GrBatchToXPOverrides*) const override; |
| 143 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 144 | void onPrepare(GrOpFlushState*) override {} |
| 145 | void onDraw(GrOpFlushState*, const SkRect& bounds) override; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 146 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 147 | InstancedRendering* const fInstancedRendering; |
| 148 | OpInfo fInfo; |
| 149 | SkScalar fPixelLoad; |
| 150 | SkSTArray<5, ParamsTexel, true> fParams; |
| 151 | bool fIsTracked; |
| 152 | int fNumDraws; |
| 153 | int fNumChangesInGeometry; |
| 154 | Draw* fHeadDraw; |
| 155 | Draw* fTailDraw; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 156 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 157 | typedef GrDrawOp INHERITED; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 158 | |
| 159 | friend class InstancedRendering; |
| 160 | }; |
| 161 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 162 | typedef SkTInternalLList<Op> OpList; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 163 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 164 | InstancedRendering(GrGpu* gpu); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 165 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 166 | const OpList& trackedOps() const { return fTrackedOps; } |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 167 | const GrBuffer* vertexBuffer() const { SkASSERT(fVertexBuffer); return fVertexBuffer.get(); } |
| 168 | const GrBuffer* indexBuffer() const { SkASSERT(fIndexBuffer); return fIndexBuffer.get(); } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 169 | |
| 170 | virtual void onBeginFlush(GrResourceProvider*) = 0; |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 171 | virtual void onDraw(const GrPipeline&, const InstanceProcessor&, const Op*) = 0; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 172 | virtual void onEndFlush() = 0; |
| 173 | virtual void onResetGpuResources(ResetType) = 0; |
| 174 | |
| 175 | private: |
| 176 | enum class State : bool { |
| 177 | kRecordingDraws, |
| 178 | kFlushing |
| 179 | }; |
| 180 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 181 | sk_sp<Op> SK_WARN_UNUSED_RESULT recordShape(ShapeType, const SkRect& bounds, |
| 182 | const SkMatrix& viewMatrix, GrColor, |
| 183 | const SkRect& localRect, GrAA aa, |
| 184 | const GrInstancedPipelineInfo&, GrAAType*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 185 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 186 | bool selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa, const GrInstancedPipelineInfo&, |
| 187 | GrAAType*, AntialiasMode*); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 188 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 189 | virtual sk_sp<Op> makeOp() = 0; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 190 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 191 | const sk_sp<GrGpu> fGpu; |
| 192 | State fState; |
| 193 | GrObjectMemoryPool<Op::Draw> fDrawPool; |
| 194 | SkSTArray<1024, ParamsTexel, true> fParams; |
| 195 | OpList fTrackedOps; |
| 196 | sk_sp<const GrBuffer> fVertexBuffer; |
| 197 | sk_sp<const GrBuffer> fIndexBuffer; |
| 198 | sk_sp<GrBuffer> fParamsBuffer; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | #endif |