OLD | NEW |
(Empty) | |
| 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 GrInstancedRendering_DEFINED |
| 9 #define GrInstancedRendering_DEFINED |
| 10 |
| 11 #include "GrAllocator.h" |
| 12 #include "GrInstancedRenderingTypes.h" |
| 13 #include "batches/GrDrawBatch.h" |
| 14 |
| 15 class GrInstanceProcessor; |
| 16 class GrResourceProvider; |
| 17 |
| 18 /** |
| 19 * This class serves as a centralized clearinghouse for instanced rendering. It
accumulates data for |
| 20 * instanced draws into one location, and creates special batches that pull from
this data. The |
| 21 * nature of instanced rendering allows these batches to combine well and render
efficiently. |
| 22 * |
| 23 * During a flush, this class assembles the accumulated draw data into a single
vertex and texel |
| 24 * buffer, and its subclass draws the batches using backend-specific instanced r
endering APIs. |
| 25 * |
| 26 * This class is responsible for the CPU side of instanced rendering. Shaders ar
e implemented by |
| 27 * GrInstanceProcessor. |
| 28 */ |
| 29 class GrInstancedRendering : public SkNoncopyable, protected GrInstancedRenderin
gTypes { |
| 30 public: |
| 31 virtual ~GrInstancedRendering() { SkASSERT(State::kRecordingShapes == fState
); } |
| 32 |
| 33 GrGpu* gpu() const { return fGpu; } |
| 34 |
| 35 enum Flags { |
| 36 kStencilWrite_Flag = (1 << 0), |
| 37 kStencilBufferMSAA_Flag = (1 << 1), |
| 38 kColorWrite_Flag = (1 << 2), |
| 39 kColorBufferMSAA_Flag = (1 << 3), |
| 40 /** |
| 41 * This should not be set if the fragment shader uses derivatives, autom
atic mipmap LOD, or |
| 42 * other features that depend on neighboring pixels. |
| 43 */ |
| 44 kCanDiscardFragments_Flag = (1 << 4) |
| 45 }; |
| 46 |
| 47 /** |
| 48 * These methods record a new instanced draw and return a batch that can ren
der it. The client |
| 49 * must call commitToGpu() before attempting to draw batches returned by thi
s class. After |
| 50 * commitToGpu(), it becomes invalid to record new draws until a subsequent
call to restart(). |
| 51 * |
| 52 * The caller is responsible to enable HW antialias in the GrPipelineBuilder
if (and only if) |
| 53 * the value of "antialias" is true and the draw is fully multisampled (e.g.
stencil-only draw |
| 54 * to a multisampled stencil buffer, stencil + color draw when both buffers
are multisampled), |
| 55 * or if the value of "requireHWAA" gets overwritten with true. requireHWAA
will be left |
| 56 * unchanged unless the batch requires HW antialias under different circumst
ances than those |
| 57 * listed above (e.g. mixed samples). |
| 58 */ |
| 59 GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&
, GrColor, |
| 60 bool antialias, uint32_t flags
, |
| 61 bool* requireHWAA = nullptr); |
| 62 |
| 63 GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&
, GrColor, |
| 64 const SkRect& localRect, bool
antialias, |
| 65 uint32_t flags, bool* requireH
WAA = nullptr); |
| 66 |
| 67 GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&
, GrColor, |
| 68 const SkMatrix& localMatrix, b
ool antialias, |
| 69 uint32_t flags, bool* requireH
WAA = nullptr); |
| 70 |
| 71 GrDrawBatch* SK_WARN_UNUSED_RESULT recordOval(const SkRect&, const SkMatrix&
, GrColor, |
| 72 bool antialias, uint32_t flags
, |
| 73 bool* requireHWAA = nullptr); |
| 74 |
| 75 GrDrawBatch* SK_WARN_UNUSED_RESULT recordRRect(const SkRRect&, const SkMatri
x&, GrColor, |
| 76 bool antialias, uint32_t flag
s, |
| 77 bool* requireHWAA = nullptr); |
| 78 |
| 79 GrDrawBatch* SK_WARN_UNUSED_RESULT recordDRRect(const SkRRect& outer, const
SkRRect& inner, |
| 80 const SkMatrix&, GrColor, bo
ol antialias, |
| 81 uint32_t flags, bool* requir
eHWAA = nullptr); |
| 82 |
| 83 /** |
| 84 * Commits all recorded draws to GPU memory and allows the client to begin d
rawing the batches |
| 85 * created by this class. |
| 86 */ |
| 87 void commitToGpu(GrResourceProvider*); |
| 88 |
| 89 /** |
| 90 * Called once the batches created previously by this class have all been re
leased. Allows the |
| 91 * client to begin recording draws again. |
| 92 */ |
| 93 void restart(); |
| 94 |
| 95 enum class ClearType { |
| 96 kDestroy, |
| 97 kAbandon |
| 98 }; |
| 99 |
| 100 /** |
| 101 * Clears all GPU resources, including those that are held long term. They w
ill be lazily |
| 102 * reinitialized if the class begins to be used again. |
| 103 */ |
| 104 void clearGpuResources(ClearType); |
| 105 |
| 106 protected: |
| 107 class Batch : public GrDrawBatch { |
| 108 public: |
| 109 virtual ~Batch() { fInUse = false; } // fInUse will continue to be acces
sed. |
| 110 |
| 111 const char* name() const override { return "Instanced Batch"; } |
| 112 |
| 113 void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 114 GrInitInvariantOutput* coverage, |
| 115 GrBatchToXPOverrides*) const override; |
| 116 |
| 117 protected: |
| 118 Batch(uint32_t classID, GrInstancedRendering* instRendering, AntialiasMo
de aa, |
| 119 uint32_t flags, int instanceIdx) |
| 120 : INHERITED(classID), |
| 121 fInstancedRendering(instRendering), |
| 122 fAntialiasMode(aa), |
| 123 fFlags(flags), |
| 124 fFirstInstanceIdx(instanceIdx), |
| 125 fInUse(true) { |
| 126 #ifdef SK_DEBUG |
| 127 fIsCombined = false; |
| 128 #endif |
| 129 } |
| 130 |
| 131 void initBatchTracker(const GrXPOverridesForBatch&) override; |
| 132 |
| 133 void onPrepare(GrBatchFlushState*) override {} |
| 134 void onDraw(GrBatchFlushState*) override; |
| 135 void onDelete() const override; |
| 136 |
| 137 GrInstancedRendering* const fInstancedRendering; |
| 138 const AntialiasMode fAntialiasMode; |
| 139 const uint32_t fFlags; |
| 140 const int fFirstInstanceIdx; |
| 141 BatchTracker fTracker; |
| 142 bool fInUse; |
| 143 #ifdef SK_DEBUG |
| 144 bool fIsCombined; |
| 145 #endif |
| 146 |
| 147 typedef GrDrawBatch INHERITED; |
| 148 |
| 149 friend class GrInstancedRendering; |
| 150 }; |
| 151 |
| 152 class BatchAllocator : public GrAllocator { |
| 153 public: |
| 154 BatchAllocator(size_t sizeofBatchClass) |
| 155 : INHERITED(sizeofBatchClass, kBatchesPerBlock, nullptr) { |
| 156 fFirstBlock = sk_malloc_throw(kBatchesPerBlock * sizeofBatchClass); |
| 157 this->setInitialBlock(fFirstBlock); |
| 158 } |
| 159 |
| 160 ~BatchAllocator() { |
| 161 sk_free(fFirstBlock); |
| 162 } |
| 163 |
| 164 private: |
| 165 enum { kBatchesPerBlock = 128 }; |
| 166 |
| 167 void* fFirstBlock; |
| 168 |
| 169 typedef GrAllocator INHERITED; |
| 170 }; |
| 171 |
| 172 GrInstancedRendering(GrGpu* gpu, uint32_t supportedAAModes, size_t sizeofBat
chClass); |
| 173 |
| 174 const GrBuffer* vertexBuffer() const { SkASSERT(fVertexBuffer); return fVert
exBuffer; } |
| 175 const GrBuffer* indexBuffer() const { SkASSERT(fIndexBuffer); return fIndexB
uffer; } |
| 176 const GrBuffer* instanceBuffer() const { SkASSERT(fInstanceBuffer); return f
InstanceBuffer; } |
| 177 const BatchAllocator* batchAllocator() const { return &fBatchAllocator; } |
| 178 |
| 179 virtual void onCommitToGpu(GrResourceProvider*) = 0; |
| 180 virtual void onDraw(const GrPipeline&, const GrInstanceProcessor&, const Bat
ch*) = 0; |
| 181 virtual void onRestart() = 0; |
| 182 virtual void onClearGpuResources(ClearType) = 0; |
| 183 |
| 184 #ifdef SK_DEBUG |
| 185 int fInUseBatchCount; |
| 186 #endif |
| 187 |
| 188 private: |
| 189 enum class State { |
| 190 kRecordingShapes, |
| 191 kDrawingBatches |
| 192 }; |
| 193 |
| 194 Batch* SK_WARN_UNUSED_RESULT recordShape(ShapeType, const SkRect& bounds, |
| 195 const SkMatrix& viewMatrix, GrColor
, |
| 196 const SkRect& localRect, bool antia
lias, |
| 197 uint32_t flags, bool* requireHWAA); |
| 198 |
| 199 bool selectAntialiasMode(const SkMatrix& viewMatrix, bool antialias, uint32_
t flags, |
| 200 AntialiasMode*, bool* requireHWAA); |
| 201 |
| 202 void appendRRectParams(const SkRRect&, BatchTracker*); |
| 203 void appendParamsTexel(const SkScalar* vals, int count); |
| 204 void appendParamsTexel(float x, float y, float z, float w); |
| 205 void appendParamsTexel(float x, float y, float z); |
| 206 |
| 207 virtual Batch* constructBatch(void* storage, AntialiasMode, uint32_t flags, |
| 208 int instanceIdx) = 0; |
| 209 |
| 210 const SkAutoTUnref<GrGpu> fGpu; |
| 211 const uint32_t fSupportedAAModes; |
| 212 State fState; |
| 213 SkSTArray<1024, Instance, true> fInstances; |
| 214 SkSTArray<1024, ParamsTexel, true> fParams; |
| 215 BatchAllocator fBatchAllocator; |
| 216 SkAutoTUnref<const GrBuffer> fVertexBuffer; |
| 217 SkAutoTUnref<const GrBuffer> fIndexBuffer; |
| 218 SkAutoTUnref<const GrBuffer> fInstanceBuffer; |
| 219 SkAutoTUnref<GrBuffer> fParamsBuffer; |
| 220 }; |
| 221 |
| 222 #endif |
OLD | NEW |