OLD | NEW |
(Empty) | |
| 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 GrVertices_DEFINED |
| 9 #define GrVertices_DEFINED |
| 10 |
| 11 #include "GrIndexBuffer.h" |
| 12 #include "GrVertexBuffer.h" |
| 13 |
| 14 /** |
| 15 * Used to communicate index and vertex buffers, counts, and offsets for a draw
from GrBatch to |
| 16 * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving o
wnership of this |
| 17 * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vert
ex info lives there |
| 18 * already (stride, attribute mappings). |
| 19 */ |
| 20 class GrVertices { |
| 21 public: |
| 22 GrVertices() {} |
| 23 GrVertices(const GrVertices& di) { (*this) = di; } |
| 24 GrVertices& operator =(const GrVertices& di); |
| 25 |
| 26 void init(GrPrimitiveType primType, const GrVertexBuffer* vertexBuffer, int
startVertex, |
| 27 int vertexCount) { |
| 28 SkASSERT(vertexBuffer); |
| 29 SkASSERT(vertexCount); |
| 30 SkASSERT(startVertex >= 0); |
| 31 fPrimitiveType = primType; |
| 32 fVertexBuffer.reset(SkRef(vertexBuffer)); |
| 33 fIndexBuffer.reset(NULL); |
| 34 fStartVertex = startVertex; |
| 35 fStartIndex = 0; |
| 36 fVertexCount = vertexCount; |
| 37 fIndexCount = 0; |
| 38 fInstanceCount = 0; |
| 39 fVerticesPerInstance = 0; |
| 40 fIndicesPerInstance = 0; |
| 41 } |
| 42 |
| 43 void initIndexed(GrPrimitiveType primType, |
| 44 const GrVertexBuffer* vertexBuffer, |
| 45 const GrIndexBuffer* indexBuffer, |
| 46 int startVertex, |
| 47 int startIndex, |
| 48 int vertexCount, |
| 49 int indexCount) { |
| 50 SkASSERT(indexBuffer); |
| 51 SkASSERT(vertexBuffer); |
| 52 SkASSERT(indexCount); |
| 53 SkASSERT(vertexCount); |
| 54 SkASSERT(startIndex >= 0); |
| 55 SkASSERT(startVertex >= 0); |
| 56 fPrimitiveType = primType; |
| 57 fVertexBuffer.reset(SkRef(vertexBuffer)); |
| 58 fIndexBuffer.reset(SkRef(indexBuffer)); |
| 59 fStartVertex = startVertex; |
| 60 fStartIndex = startIndex; |
| 61 fVertexCount = vertexCount; |
| 62 fIndexCount = indexCount; |
| 63 fInstanceCount = 0; |
| 64 fVerticesPerInstance = 0; |
| 65 fIndicesPerInstance = 0; |
| 66 } |
| 67 |
| 68 void initInstanced(GrPrimitiveType primType, |
| 69 const GrVertexBuffer* vertexBuffer, |
| 70 const GrIndexBuffer* indexBuffer, |
| 71 int startVertex, |
| 72 int verticesPerInstance, |
| 73 int indicesPerInstance, |
| 74 int instanceCount) { |
| 75 SkASSERT(vertexBuffer); |
| 76 SkASSERT(indexBuffer); |
| 77 SkASSERT(instanceCount); |
| 78 SkASSERT(verticesPerInstance); |
| 79 SkASSERT(indicesPerInstance); |
| 80 SkASSERT(startVertex >= 0); |
| 81 fPrimitiveType = primType; |
| 82 fVertexBuffer.reset(SkRef(vertexBuffer)); |
| 83 fIndexBuffer.reset(SkRef(indexBuffer)); |
| 84 fStartVertex = startVertex; |
| 85 fStartIndex = 0; |
| 86 fVerticesPerInstance = verticesPerInstance; |
| 87 fIndicesPerInstance = indicesPerInstance; |
| 88 fInstanceCount = instanceCount; |
| 89 fVertexCount = instanceCount * fVerticesPerInstance; |
| 90 fIndexCount = instanceCount * fIndicesPerInstance; |
| 91 } |
| 92 |
| 93 /** Variation of the above that may be used when the total number of instanc
es may exceed |
| 94 the number of instances supported by the index buffer. To be used with |
| 95 nextInstances() to draw in max-sized batches.*/ |
| 96 void initInstanced(GrPrimitiveType primType, |
| 97 const GrVertexBuffer* vertexBuffer, |
| 98 const GrIndexBuffer* indexBuffer, |
| 99 int startVertex, |
| 100 int verticesPerInstance, |
| 101 int indicesPerInstance, |
| 102 int* instancesRemaining, |
| 103 int maxInstancesPerDraw) { |
| 104 int instanceCount = SkTMin(*instancesRemaining, maxInstancesPerDraw); |
| 105 *instancesRemaining -= instanceCount; |
| 106 this->initInstanced(primType, vertexBuffer, indexBuffer, startVertex, |
| 107 verticesPerInstance, indicesPerInstance, instanceCou
nt); |
| 108 } |
| 109 |
| 110 GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
| 111 int startVertex() const { return fStartVertex; } |
| 112 int startIndex() const { return fStartIndex; } |
| 113 int vertexCount() const { return fVertexCount; } |
| 114 int indexCount() const { return fIndexCount; } |
| 115 |
| 116 /** These return 0 if initInstanced was not used to initialize the GrVertice
s. */ |
| 117 int verticesPerInstance() const { return fVerticesPerInstance; } |
| 118 int indicesPerInstance() const { return fIndicesPerInstance; } |
| 119 int instanceCount() const { return fInstanceCount; } |
| 120 |
| 121 bool isIndexed() const { return fIndexCount > 0; } |
| 122 bool isInstanced() const { return fInstanceCount > 0; } |
| 123 |
| 124 /** Called after using this draw info to draw the next set of instances. |
| 125 The vertex offset is advanced while the index buffer is reused at the sa
me |
| 126 position. instancesRemaining is number of instances that remain, maxInst
ances is |
| 127 the most number of instances that can be used with the index buffer. If
there |
| 128 are no instances remaining, the GrVertices is unmodified and false is re
turned.*/ |
| 129 bool nextInstances(int* instancesRemaining, int maxInstances) { |
| 130 SkASSERT(this->isInstanced()); |
| 131 if (!*instancesRemaining) { |
| 132 return false; |
| 133 } |
| 134 fStartVertex += fVertexCount; |
| 135 fInstanceCount = SkTMin(*instancesRemaining, maxInstances); |
| 136 fVertexCount = fInstanceCount * fVerticesPerInstance; |
| 137 fIndexCount = fInstanceCount * fIndicesPerInstance; |
| 138 *instancesRemaining -= fInstanceCount; |
| 139 return true; |
| 140 } |
| 141 |
| 142 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); } |
| 143 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); } |
| 144 |
| 145 private: |
| 146 GrPrimitiveType fPrimitiveType; |
| 147 |
| 148 int fStartVertex; |
| 149 int fStartIndex; |
| 150 int fVertexCount; |
| 151 int fIndexCount; |
| 152 |
| 153 int fInstanceCount; |
| 154 int fVerticesPerInstance; |
| 155 int fIndicesPerInstance; |
| 156 |
| 157 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer; |
| 158 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer; |
| 159 }; |
| 160 |
| 161 #endif |
OLD | NEW |