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" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 12 | #include "GrPrimitiveProcessor.h" |
| 13 | #include "GrPendingProgramElement.h" |
| 14 | #include "GrVertices.h" |
| 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 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 27 | GrVertexBatch(); |
| 28 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 29 | void prepareDraws(GrBatchFlushState* state); |
| 30 | void issueDraws(GrBatchFlushState* state); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 31 | |
| 32 | protected: |
| 33 | /** Helper for rendering instances using an instanced index index buffer. This class creates the |
| 34 | space for the vertices and flushes the draws to the batch target. */ |
| 35 | class InstancedHelper { |
| 36 | public: |
| 37 | InstancedHelper() {} |
| 38 | /** Returns the allocated storage for the vertices. The caller should populate the before |
| 39 | vertices before calling issueDraws(). */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 40 | void* init(Target*, GrPrimitiveType, size_t vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 41 | const GrIndexBuffer*, int verticesPerInstance, int indicesPerInstance, |
| 42 | int instancesToDraw); |
| 43 | |
| 44 | /** Call after init() to issue draws to the batch target.*/ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 45 | void recordDraw(Target* target); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 46 | private: |
| 47 | GrVertices fVertices; |
| 48 | }; |
| 49 | |
| 50 | static const int kVerticesPerQuad = 4; |
| 51 | static const int kIndicesPerQuad = 6; |
| 52 | |
| 53 | /** A specialization of InstanceHelper for quad rendering. */ |
| 54 | class QuadHelper : private InstancedHelper { |
| 55 | public: |
| 56 | QuadHelper() : INHERITED() {} |
| 57 | /** Finds the cached quad index buffer and reserves vertex space. Returns NULL on failure |
| 58 | and on sucess a pointer to the vertex data that the caller should populate before |
| 59 | calling issueDraws(). */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 60 | void* init(Target* batchTarget, size_t vertexStride, int quadsToDraw); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 61 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 62 | using InstancedHelper::recordDraw; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 63 | private: |
| 64 | typedef InstancedHelper INHERITED; |
| 65 | }; |
| 66 | |
| 67 | private: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 68 | virtual void onPrepareDraws(Target*) = 0; |
| 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 { |
| 74 | SkSTArray<1, GrVertices, true> fDraws; |
| 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. |
| 80 | SkTLList<DrawArray> fDrawArrays; |
| 81 | |
| 82 | // What is this? |
| 83 | GrBatchTracker fBatchTracker; |
| 84 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 85 | typedef GrDrawBatch INHERITED; |
| 86 | }; |
| 87 | |
| 88 | #endif |