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 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 11 | #include "GrDrawOp.h" |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 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 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 21 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 22 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 23 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | public: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 25 | class Target; |
| 26 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 27 | GrMeshDrawOp(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. */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 32 | class InstancedHelper { |
| 33 | public: |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 34 | InstancedHelper() {} |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 35 | /** Returns the allocated storage for the vertices. The caller should populate the vertices |
| 36 | before calling recordDraws(). */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 37 | void* init(Target*, GrPrimitiveType, size_t vertexStride, const GrBuffer*, |
| 38 | int verticesPerInstance, int indicesPerInstance, int instancesToDraw); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 39 | |
| 40 | /** Call after init() to issue draws to the batch target.*/ |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 41 | void recordDraw(Target*, const GrGeometryProcessor*); |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 42 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 43 | private: |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -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 | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 55 | and on success a pointer to the vertex data that the caller should populate before |
| 56 | calling recordDraws(). */ |
| 57 | void* init(Target*, 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; |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 60 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 61 | private: |
| 62 | typedef InstancedHelper INHERITED; |
| 63 | }; |
| 64 | |
| 65 | private: |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 66 | void onPrepare(GrBatchFlushState* state) final; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 67 | void onDraw(GrBatchFlushState* state, const SkRect& bounds) final; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 68 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 69 | virtual void onPrepareDraws(Target*) const = 0; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 70 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 71 | // A set of contiguous draws that share a draw token and primitive processor. The draws all use |
| 72 | // the batch's pipeline. The meshes for the draw are stored in the fMeshes array and each |
| 73 | // Queued draw uses fMeshCnt meshes from the fMeshes array. The reason for coallescing meshes |
| 74 | // that share a primitive processor into a QueuedDraw is that it allows the Gpu object to setup |
| 75 | // the shared state once and then issue draws for each mesh. |
| 76 | struct QueuedDraw { |
| 77 | int fMeshCnt = 0; |
| 78 | GrPendingProgramElement<const GrGeometryProcessor> fGeometryProcessor; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 81 | // All draws in all the vertex batches have implicit tokens based on the order they are |
| 82 | // enqueued globally across all batches. This is the offset of the first entry in fQueuedDraws. |
| 83 | // fQueuedDraws[i]'s token is fBaseDrawToken + i. |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 84 | GrDrawOpUploadToken fBaseDrawToken; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 85 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 86 | SkSTArray<4, GrMesh> fMeshes; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 87 | SkSTArray<4, QueuedDraw, true> fQueuedDraws; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 88 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 89 | typedef GrDrawOp INHERITED; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | #endif |