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 | |
| 8 | #ifndef GrVertexBatch_DEFINED |
| 9 | #define GrVertexBatch_DEFINED |
| 10 | |
| 11 | #include "GrDrawBatch.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame^] | 12 | #include "GrMesh.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 13 | #include "GrPrimitiveProcessor.h" |
| 14 | #include "GrPendingProgramElement.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 15 | |
| 16 | #include "SkTLList.h" |
| 17 | |
| 18 | class GrBatchFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Base class for vertex-based GrBatches. |
| 22 | */ |
| 23 | class GrVertexBatch : public GrDrawBatch { |
| 24 | public: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 25 | class Target; |
| 26 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 27 | GrVertexBatch(uint32_t classID); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 28 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 29 | protected: |
| 30 | /** Helper for rendering instances using an instanced index index buffer. This class creates the |
| 31 | space for the vertices and flushes the draws to the batch target. */ |
| 32 | class InstancedHelper { |
| 33 | public: |
| 34 | InstancedHelper() {} |
| 35 | /** Returns the allocated storage for the vertices. The caller should populate the before |
| 36 | vertices before calling issueDraws(). */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 37 | void* init(Target*, GrPrimitiveType, size_t vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 38 | const GrIndexBuffer*, int verticesPerInstance, int indicesPerInstance, |
| 39 | int instancesToDraw); |
| 40 | |
| 41 | /** Call after init() to issue draws to the batch target.*/ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 42 | void recordDraw(Target* target); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 43 | private: |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame^] | 44 | GrMesh fMesh; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static const int kVerticesPerQuad = 4; |
| 48 | static const int kIndicesPerQuad = 6; |
| 49 | |
| 50 | /** A specialization of InstanceHelper for quad rendering. */ |
| 51 | class QuadHelper : private InstancedHelper { |
| 52 | public: |
| 53 | QuadHelper() : INHERITED() {} |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 55 | and on sucess a pointer to the vertex data that the caller should populate before |
| 56 | calling issueDraws(). */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 57 | void* init(Target* batchTarget, size_t vertexStride, int quadsToDraw); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 58 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 59 | using InstancedHelper::recordDraw; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 60 | private: |
| 61 | typedef InstancedHelper INHERITED; |
| 62 | }; |
| 63 | |
| 64 | private: |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 65 | void onPrepare(GrBatchFlushState* state) final; |
| 66 | void onDraw(GrBatchFlushState* state) final; |
| 67 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 68 | virtual void onPrepareDraws(Target*) const = 0; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 69 | |
| 70 | // A set of contiguous draws with no inline uploads between them that all use the same |
| 71 | // primitive processor. All the draws in a DrawArray share a primitive processor and use the |
| 72 | // the batch's GrPipeline. |
| 73 | struct DrawArray { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame^] | 74 | SkSTArray<1, GrMesh, true> fDraws; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 75 | GrPendingProgramElement<const GrPrimitiveProcessor> fPrimitiveProcessor; |
| 76 | }; |
| 77 | |
| 78 | // Array of DrawArray. There may be inline uploads between each DrawArray and each DrawArray |
| 79 | // may use a different primitive processor. |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 80 | typedef SkTLList<DrawArray, 4> DrawArrayList; |
| 81 | DrawArrayList fDrawArrays; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 82 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 83 | typedef GrDrawBatch INHERITED; |
| 84 | }; |
| 85 | |
| 86 | #endif |