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 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 11 | #include "instanced/InstancedOp.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 12 | #include "instanced/InstancedRenderingTypes.h" |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 13 | |
| 14 | #include "SkTInternalLList.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 15 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 16 | class GrGpu; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 17 | class GrResourceProvider; |
| 18 | |
| 19 | namespace gr_instanced { |
| 20 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 21 | class InstancedOp; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 22 | class InstanceProcessor; |
| 23 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 24 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 25 | /** |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 26 | * Instanced Rendering occurs through the interaction of four class: |
| 27 | * InstancedOp |
| 28 | * OpAllocator |
| 29 | * InstancedRendering |
| 30 | * InstanceProcessor |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 31 | * |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 32 | * The InstancedOp is a GrDrawOp but is more of a proxy than normal operations. It accumulates a |
| 33 | * LL of Draw objects that are allocated all together by the OpAllocator. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 34 | * |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 35 | * There is only one OpAllocator which encapsulates the creation of InstancedOps and the pool |
| 36 | * of memory used for their Draw objects. |
| 37 | * |
| 38 | * The InstancedRendering class tracks a list of InstancedOps that will all be drawn during |
| 39 | * the same flush. There is currently one per opList. The nature of instanced |
| 40 | * rendering allows these ops to combine well and render efficiently. |
| 41 | * During a flush, it assembles the accumulated draw data into a single vertex and texel |
| 42 | * buffer per opList, and its subclasses draw the ops using backend-specific instanced |
| 43 | * rendering APIs. |
| 44 | * |
| 45 | * InstanceProcessors implement the shaders required for instance rendering. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 46 | */ |
| 47 | class InstancedRendering : public SkNoncopyable { |
| 48 | public: |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 49 | virtual ~InstancedRendering(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 50 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 51 | GrGpu* gpu() const { return fGpu.get(); } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 54 | * 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] | 55 | * ops created by this class. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 56 | */ |
| 57 | void beginFlush(GrResourceProvider*); |
| 58 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 59 | void draw(const GrPipeline& pipeline, OpInfo info, const InstancedOp* baseOp); |
| 60 | |
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 | * 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] | 63 | * client to begin recording draws again. |
| 64 | */ |
| 65 | void endFlush(); |
| 66 | |
| 67 | enum class ResetType : bool { |
| 68 | kDestroy, |
| 69 | kAbandon |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * Resets all GPU resources, including those that are held long term. They will be lazily |
| 74 | * reinitialized if the class begins to be used again. |
| 75 | */ |
| 76 | void resetGpuResources(ResetType); |
| 77 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 78 | void addOp(InstancedOp* op) { fTrackedOps.addToTail(op); } |
| 79 | void removeOp(InstancedOp* op) { fTrackedOps.remove(op); } |
| 80 | int addOpParams(InstancedOp* op); |
| 81 | |
| 82 | #ifdef SK_DEBUG |
| 83 | bool isFlushing() const { return InstancedRendering::State::kFlushing == fState; } |
| 84 | #endif |
| 85 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 86 | protected: |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 87 | typedef SkTInternalLList<InstancedOp> OpList; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 88 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 89 | InstancedRendering(GrGpu* gpu); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 90 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 91 | const OpList& trackedOps() const { return fTrackedOps; } |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 92 | const GrBuffer* vertexBuffer() const { SkASSERT(fVertexBuffer); return fVertexBuffer.get(); } |
| 93 | const GrBuffer* indexBuffer() const { SkASSERT(fIndexBuffer); return fIndexBuffer.get(); } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 94 | |
| 95 | virtual void onBeginFlush(GrResourceProvider*) = 0; |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 96 | virtual void onDraw(const GrPipeline&, const InstanceProcessor&, const InstancedOp*) = 0; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 97 | virtual void onEndFlush() = 0; |
| 98 | virtual void onResetGpuResources(ResetType) = 0; |
| 99 | |
| 100 | private: |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 101 | #ifdef SK_DEBUG |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 102 | enum class State : bool { |
| 103 | kRecordingDraws, |
| 104 | kFlushing |
| 105 | }; |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 106 | #endif |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 107 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 108 | const sk_sp<GrGpu> fGpu; |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 109 | SkDEBUGCODE(State fState;) |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 110 | SkSTArray<1024, ParamsTexel, true> fParams; |
| 111 | OpList fTrackedOps; |
| 112 | sk_sp<const GrBuffer> fVertexBuffer; |
| 113 | sk_sp<const GrBuffer> fIndexBuffer; |
| 114 | sk_sp<GrBuffer> fParamsBuffer; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | #endif |