Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 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 GrVertexChunkArray_DEFINED |
| 9 | #define GrVertexChunkArray_DEFINED |
| 10 | |
| 11 | #include "include/private/SkNoncopyable.h" |
| 12 | #include "include/private/SkTArray.h" |
| 13 | #include "src/gpu/GrBuffer.h" |
| 14 | #include "src/gpu/GrVertexWriter.h" |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame^] | 15 | |
| 16 | class GrMeshDrawTarget; |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 17 | |
| 18 | // Represents a chunk of vertex data. Use with GrVertexChunkArray and GrVertexChunkBuilder. We write |
| 19 | // the data out in chunks when we don't start out knowing exactly how many vertices (or instances) |
| 20 | // we will end up writing. |
| 21 | struct GrVertexChunk { |
| 22 | sk_sp<const GrBuffer> fBuffer; |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 23 | int fCount = 0; |
| 24 | int fBase; // baseVertex or baseInstance, depending on the use case. |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | // Represents an array of GrVertexChunks. |
| 28 | // |
| 29 | // We only preallocate 1 chunk because if the array needs to grow, then we're also allocating a |
| 30 | // brand new GPU buffer anyway. |
| 31 | using GrVertexChunkArray = SkSTArray<1, GrVertexChunk>; |
| 32 | |
| 33 | // Builds a GrVertexChunkArray. The provided Target must not be used externally throughout the |
| 34 | // entire lifetime of this object. |
| 35 | class GrVertexChunkBuilder : SkNoncopyable { |
| 36 | public: |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame^] | 37 | GrVertexChunkBuilder(GrMeshDrawTarget* target, GrVertexChunkArray* chunks, size_t stride, |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 38 | int minVerticesPerChunk) |
| 39 | : fTarget(target) |
| 40 | , fChunks(chunks) |
| 41 | , fStride(stride) |
| 42 | , fMinVerticesPerChunk(minVerticesPerChunk) { |
| 43 | SkASSERT(fMinVerticesPerChunk > 0); |
| 44 | } |
| 45 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame^] | 46 | ~GrVertexChunkBuilder(); |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 47 | |
| 48 | // Appends 'count' contiguous vertices. These vertices are not guaranteed to be contiguous with |
| 49 | // previous or future calls to appendVertices. |
| 50 | SK_ALWAYS_INLINE GrVertexWriter appendVertices(int count) { |
| 51 | SkASSERT(count > 0); |
| 52 | if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) { |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 53 | SkDEBUGCODE(fLastAppendAmount = 0;) |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 54 | return {nullptr}; |
| 55 | } |
| 56 | SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity); |
| 57 | fCurrChunkVertexCount += count; |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 58 | SkDEBUGCODE(fLastAppendAmount = count;) |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 59 | return std::exchange(fCurrChunkVertexWriter, |
| 60 | fCurrChunkVertexWriter.makeOffset(fStride * count)); |
| 61 | } |
| 62 | |
| 63 | SK_ALWAYS_INLINE GrVertexWriter appendVertex() { return this->appendVertices(1); } |
| 64 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 65 | // Pops the most recent 'count' contiguous vertices. Since there is no guarantee of contiguity |
| 66 | // between appends, 'count' may be no larger than the most recent call to appendVertices(). |
| 67 | void popVertices(int count) { |
| 68 | SkASSERT(count <= fLastAppendAmount); |
| 69 | SkASSERT(fLastAppendAmount <= fCurrChunkVertexCount); |
| 70 | SkASSERT(count >= 0); |
| 71 | fCurrChunkVertexCount -= count; |
| 72 | fCurrChunkVertexWriter = fCurrChunkVertexWriter.makeOffset(fStride * -count); |
| 73 | SkDEBUGCODE(fLastAppendAmount -= count;) |
| 74 | } |
| 75 | |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 76 | private: |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame^] | 77 | bool allocChunk(int minCount); |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 78 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame^] | 79 | GrMeshDrawTarget* const fTarget; |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 80 | GrVertexChunkArray* const fChunks; |
| 81 | const size_t fStride; |
Chris Dalton | 1c933f8 | 2021-05-20 19:25:53 -0600 | [diff] [blame] | 82 | int fMinVerticesPerChunk; |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 83 | |
| 84 | GrVertexWriter fCurrChunkVertexWriter; |
| 85 | int fCurrChunkVertexCount = 0; |
| 86 | int fCurrChunkVertexCapacity = 0; |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 87 | |
| 88 | SkDEBUGCODE(int fLastAppendAmount = 0;) |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #endif |