bsalomon | cb8979d | 2015-05-05 09:51:38 -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 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 8 | #ifndef GrMesh_DEFINED |
| 9 | #define GrMesh_DEFINED |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 10 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 11 | #include "GrBuffer.h" |
Hal Canary | 466c7d6 | 2017-07-03 15:11:49 -0400 | [diff] [blame] | 12 | #include "GrGpuResourceRef.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 13 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 14 | class GrPrimitiveProcessor; |
| 15 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 16 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 17 | * Used to communicate index and vertex buffers, counts, and offsets for a draw from GrOp to |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 18 | * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving ownership of this |
| 19 | * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vertex info lives there |
| 20 | * already (stride, attribute mappings). |
| 21 | */ |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 22 | class GrMesh { |
| 23 | public: |
| 24 | GrMesh(GrPrimitiveType primitiveType) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 25 | : fPrimitiveType(primitiveType) |
| 26 | , fBaseVertex(0) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 27 | SkDEBUGCODE(fNonIndexNonInstanceData.fVertexCount = -1;) |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 28 | } |
| 29 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 30 | GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
| 31 | bool isIndexed() const { return SkToBool(fIndexBuffer.get()); } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 32 | bool isInstanced() const { return SkToBool(fInstanceBuffer.get()); } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 33 | bool hasVertexData() const { return SkToBool(fVertexBuffer.get()); } |
| 34 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 35 | void setNonIndexedNonInstanced(int vertexCount); |
| 36 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 37 | void setIndexed(const GrBuffer* indexBuffer, int indexCount, int baseIndex, |
| 38 | uint16_t minIndexValue, uint16_t maxIndexValue); |
| 39 | void setIndexedPatterned(const GrBuffer* indexBuffer, int indexCount, int vertexCount, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 40 | int patternRepeatCount, int maxPatternRepetitionsInIndexBuffer); |
| 41 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 42 | void setInstanced(const GrBuffer* instanceBuffer, int instanceCount, int baseInstance, |
| 43 | int vertexCount); |
| 44 | void setIndexedInstanced(const GrBuffer* indexBuffer, int indexCount, |
| 45 | const GrBuffer* instanceBuffer, int instanceCount, int baseInstance=0); |
| 46 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 47 | void setVertexData(const GrBuffer* vertexBuffer, int baseVertex = 0); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 48 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 49 | class SendToGpuImpl { |
| 50 | public: |
| 51 | virtual void sendMeshToGpu(const GrPrimitiveProcessor&, GrPrimitiveType, |
| 52 | const GrBuffer* vertexBuffer, int vertexCount, |
| 53 | int baseVertex) = 0; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 54 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 55 | virtual void sendIndexedMeshToGpu(const GrPrimitiveProcessor&, GrPrimitiveType, |
| 56 | const GrBuffer* indexBuffer, int indexCount, |
| 57 | int baseIndex, uint16_t minIndexValue, |
| 58 | uint16_t maxIndexValue, const GrBuffer* vertexBuffer, |
| 59 | int baseVertex) = 0; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 60 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 61 | virtual void sendInstancedMeshToGpu(const GrPrimitiveProcessor&, GrPrimitiveType, |
| 62 | const GrBuffer* vertexBuffer, int vertexCount, |
| 63 | int baseVertex, const GrBuffer* instanceBuffer, |
| 64 | int instanceCount, int baseInstance) = 0; |
| 65 | |
| 66 | virtual void sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor&, GrPrimitiveType, |
| 67 | const GrBuffer* indexBuffer, int indexCount, |
| 68 | int baseIndex, const GrBuffer* vertexBuffer, |
| 69 | int baseVertex, const GrBuffer* instanceBuffer, |
| 70 | int instanceCount, int baseInstance) = 0; |
| 71 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 72 | virtual ~SendToGpuImpl() {} |
| 73 | }; |
| 74 | |
| 75 | void sendToGpu(const GrPrimitiveProcessor&, SendToGpuImpl*) const; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 76 | |
| 77 | struct PatternBatch; |
| 78 | |
| 79 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 80 | using PendingBuffer = GrPendingIOResource<const GrBuffer, kRead_GrIOType>; |
| 81 | |
| 82 | GrPrimitiveType fPrimitiveType; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 83 | PendingBuffer fIndexBuffer; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 84 | PendingBuffer fInstanceBuffer; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 85 | PendingBuffer fVertexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 86 | int fBaseVertex; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 87 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 88 | union { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 89 | struct { // When fIndexBuffer == nullptr and fInstanceBuffer == nullptr. |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 90 | int fVertexCount; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 91 | } fNonIndexNonInstanceData; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 92 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 93 | struct { // When fIndexBuffer != nullptr and fInstanceBuffer == nullptr. |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 94 | struct { |
| 95 | int fIndexCount; |
| 96 | int fPatternRepeatCount; |
| 97 | } fIndexData; |
| 98 | |
| 99 | union { |
| 100 | struct { // When fPatternRepeatCount == 0. |
| 101 | int fBaseIndex; |
| 102 | uint16_t fMinIndexValue; |
| 103 | uint16_t fMaxIndexValue; |
| 104 | } fNonPatternIndexData; |
| 105 | |
| 106 | struct { // When fPatternRepeatCount != 0. |
| 107 | int fVertexCount; |
| 108 | int fMaxPatternRepetitionsInIndexBuffer; |
| 109 | } fPatternData; |
| 110 | }; |
| 111 | }; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 112 | |
| 113 | struct { // When fInstanceBuffer != nullptr. |
| 114 | struct { |
| 115 | int fInstanceCount; |
| 116 | int fBaseInstance; |
| 117 | } fInstanceData; |
| 118 | |
| 119 | union { // When fIndexBuffer == nullptr. |
| 120 | struct { |
| 121 | int fVertexCount; |
| 122 | } fInstanceNonIndexData; |
| 123 | |
| 124 | struct { // When fIndexBuffer != nullptr. |
| 125 | int fIndexCount; |
| 126 | } fInstanceIndexData; |
| 127 | }; |
| 128 | }; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 129 | }; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 130 | }; |
| 131 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 132 | inline void GrMesh::setNonIndexedNonInstanced(int vertexCount) { |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 133 | fIndexBuffer.reset(nullptr); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 134 | fInstanceBuffer.reset(nullptr); |
| 135 | fNonIndexNonInstanceData.fVertexCount = vertexCount; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 136 | } |
| 137 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 138 | inline void GrMesh::setIndexed(const GrBuffer* indexBuffer, int indexCount, int baseIndex, |
| 139 | uint16_t minIndexValue, uint16_t maxIndexValue) { |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 140 | SkASSERT(indexBuffer); |
| 141 | SkASSERT(indexCount >= 1); |
| 142 | SkASSERT(baseIndex >= 0); |
Chris Dalton | a88da48 | 2017-06-06 12:25:28 -0600 | [diff] [blame] | 143 | SkASSERT(maxIndexValue >= minIndexValue); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 144 | fIndexBuffer.reset(indexBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 145 | fInstanceBuffer.reset(nullptr); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 146 | fIndexData.fIndexCount = indexCount; |
| 147 | fIndexData.fPatternRepeatCount = 0; |
| 148 | fNonPatternIndexData.fBaseIndex = baseIndex; |
| 149 | fNonPatternIndexData.fMinIndexValue = minIndexValue; |
| 150 | fNonPatternIndexData.fMaxIndexValue = maxIndexValue; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inline void GrMesh::setIndexedPatterned(const GrBuffer* indexBuffer, int indexCount, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 154 | int vertexCount, int patternRepeatCount, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 155 | int maxPatternRepetitionsInIndexBuffer) { |
| 156 | SkASSERT(indexBuffer); |
| 157 | SkASSERT(indexCount >= 1); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 158 | SkASSERT(vertexCount >= 1); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 159 | SkASSERT(patternRepeatCount >= 1); |
| 160 | SkASSERT(maxPatternRepetitionsInIndexBuffer >= 1); |
| 161 | fIndexBuffer.reset(indexBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 162 | fInstanceBuffer.reset(nullptr); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 163 | fIndexData.fIndexCount = indexCount; |
| 164 | fIndexData.fPatternRepeatCount = patternRepeatCount; |
| 165 | fPatternData.fVertexCount = vertexCount; |
| 166 | fPatternData.fMaxPatternRepetitionsInIndexBuffer = maxPatternRepetitionsInIndexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 169 | inline void GrMesh::setInstanced(const GrBuffer* instanceBuffer, int instanceCount, |
| 170 | int baseInstance, int vertexCount) { |
| 171 | SkASSERT(instanceBuffer); |
| 172 | SkASSERT(instanceCount >= 1); |
| 173 | SkASSERT(baseInstance >= 0); |
| 174 | fIndexBuffer.reset(nullptr); |
| 175 | fInstanceBuffer.reset(instanceBuffer); |
| 176 | fInstanceData.fInstanceCount = instanceCount; |
| 177 | fInstanceData.fBaseInstance = baseInstance; |
| 178 | fInstanceNonIndexData.fVertexCount = vertexCount; |
| 179 | } |
| 180 | |
| 181 | inline void GrMesh::setIndexedInstanced(const GrBuffer* indexBuffer, int indexCount, |
| 182 | const GrBuffer* instanceBuffer, int instanceCount, |
| 183 | int baseInstance) { |
| 184 | SkASSERT(indexBuffer); |
| 185 | SkASSERT(indexCount >= 1); |
| 186 | SkASSERT(instanceBuffer); |
| 187 | SkASSERT(instanceCount >= 1); |
| 188 | SkASSERT(baseInstance >= 0); |
| 189 | fIndexBuffer.reset(indexBuffer); |
| 190 | fInstanceBuffer.reset(instanceBuffer); |
| 191 | fInstanceData.fInstanceCount = instanceCount; |
| 192 | fInstanceData.fBaseInstance = baseInstance; |
| 193 | fInstanceIndexData.fIndexCount = indexCount; |
| 194 | } |
| 195 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 196 | inline void GrMesh::setVertexData(const GrBuffer* vertexBuffer, int baseVertex) { |
| 197 | SkASSERT(baseVertex >= 0); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 198 | fVertexBuffer.reset(vertexBuffer); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 199 | fBaseVertex = baseVertex; |
| 200 | } |
| 201 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 202 | inline void GrMesh::sendToGpu(const GrPrimitiveProcessor& primProc, SendToGpuImpl* impl) const { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 203 | if (this->isInstanced()) { |
| 204 | if (!this->isIndexed()) { |
| 205 | impl->sendInstancedMeshToGpu(primProc, fPrimitiveType, fVertexBuffer.get(), |
| 206 | fInstanceNonIndexData.fVertexCount, fBaseVertex, |
| 207 | fInstanceBuffer.get(), fInstanceData.fInstanceCount, |
| 208 | fInstanceData.fBaseInstance); |
| 209 | } else { |
| 210 | impl->sendIndexedInstancedMeshToGpu(primProc, fPrimitiveType, fIndexBuffer.get(), |
| 211 | fInstanceIndexData.fIndexCount, 0, |
| 212 | fVertexBuffer.get(), fBaseVertex, |
| 213 | fInstanceBuffer.get(), fInstanceData.fInstanceCount, |
| 214 | fInstanceData.fBaseInstance); |
| 215 | } |
| 216 | return; |
| 217 | } |
| 218 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 219 | if (!this->isIndexed()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 220 | SkASSERT(fNonIndexNonInstanceData.fVertexCount > 0); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 221 | impl->sendMeshToGpu(primProc, fPrimitiveType, fVertexBuffer.get(), |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 222 | fNonIndexNonInstanceData.fVertexCount, fBaseVertex); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 223 | return; |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 226 | if (0 == fIndexData.fPatternRepeatCount) { |
| 227 | impl->sendIndexedMeshToGpu(primProc, fPrimitiveType, fIndexBuffer.get(), |
| 228 | fIndexData.fIndexCount, fNonPatternIndexData.fBaseIndex, |
| 229 | fNonPatternIndexData.fMinIndexValue, |
| 230 | fNonPatternIndexData.fMaxIndexValue, fVertexBuffer.get(), |
| 231 | fBaseVertex); |
| 232 | return; |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 235 | SkASSERT(fIndexData.fPatternRepeatCount > 0); |
| 236 | int baseRepetition = 0; |
| 237 | do { |
| 238 | int repeatCount = SkTMin(fPatternData.fMaxPatternRepetitionsInIndexBuffer, |
| 239 | fIndexData.fPatternRepeatCount - baseRepetition); |
| 240 | // A patterned index buffer must contain indices in the range [0..vertexCount]. |
| 241 | int minIndexValue = 0; |
| 242 | int maxIndexValue = fPatternData.fVertexCount * repeatCount - 1; |
| 243 | impl->sendIndexedMeshToGpu(primProc, fPrimitiveType, fIndexBuffer.get(), |
| 244 | fIndexData.fIndexCount * repeatCount, 0, minIndexValue, |
| 245 | maxIndexValue, fVertexBuffer.get(), |
| 246 | fBaseVertex + fPatternData.fVertexCount * baseRepetition); |
| 247 | baseRepetition += repeatCount; |
| 248 | } while (baseRepetition < fIndexData.fPatternRepeatCount); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 249 | } |
| 250 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 251 | #endif |