blob: 5d1ce6f3e4f57e7d6a703dc76358f54e3050b18a [file] [log] [blame]
bsalomoncb8979d2015-05-05 09:51:38 -07001/*
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
egdaniel0e1853c2016-03-17 11:35:45 -07008#ifndef GrMesh_DEFINED
9#define GrMesh_DEFINED
bsalomoncb8979d2015-05-05 09:51:38 -070010
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
12#include "GrGpuResourceRef.h"
bsalomoncb8979d2015-05-05 09:51:38 -070013
egdaniel0e1853c2016-03-17 11:35:45 -070014class GrNonInstancedMesh {
bsalomone64eb572015-05-07 11:35:55 -070015public:
16 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
17 int startVertex() const { return fStartVertex; }
18 int startIndex() const { return fStartIndex; }
19 int vertexCount() const { return fVertexCount; }
20 int indexCount() const { return fIndexCount; }
21 bool isIndexed() const { return fIndexCount > 0; }
22
cdalton397536c2016-03-25 12:15:03 -070023 const GrBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
24 const GrBuffer* indexBuffer() const { return fIndexBuffer.get(); }
bsalomone64eb572015-05-07 11:35:55 -070025
26protected:
27 GrPrimitiveType fPrimitiveType;
28 int fStartVertex;
29 int fStartIndex;
30 int fVertexCount;
31 int fIndexCount;
cdalton397536c2016-03-25 12:15:03 -070032 GrPendingIOResource<const GrBuffer, kRead_GrIOType> fVertexBuffer;
33 GrPendingIOResource<const GrBuffer, kRead_GrIOType> fIndexBuffer;
egdaniel0e1853c2016-03-17 11:35:45 -070034 friend class GrMesh;
bsalomone64eb572015-05-07 11:35:55 -070035};
36
bsalomoncb8979d2015-05-05 09:51:38 -070037/**
Brian Salomon25a88092016-12-01 09:36:50 -050038 * Used to communicate index and vertex buffers, counts, and offsets for a draw from GrOp to
bsalomoncb8979d2015-05-05 09:51:38 -070039 * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving ownership of this
40 * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vertex info lives there
41 * already (stride, attribute mappings).
42 */
egdaniel0e1853c2016-03-17 11:35:45 -070043class GrMesh : public GrNonInstancedMesh {
bsalomoncb8979d2015-05-05 09:51:38 -070044public:
egdaniel0e1853c2016-03-17 11:35:45 -070045 GrMesh() {}
46 GrMesh(const GrMesh& di) { (*this) = di; }
47 GrMesh& operator =(const GrMesh& di);
bsalomoncb8979d2015-05-05 09:51:38 -070048
cdalton397536c2016-03-25 12:15:03 -070049 void init(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int startVertex,
bsalomoncb8979d2015-05-05 09:51:38 -070050 int vertexCount) {
51 SkASSERT(vertexBuffer);
52 SkASSERT(vertexCount);
53 SkASSERT(startVertex >= 0);
54 fPrimitiveType = primType;
bsalomon301f9892015-05-05 13:20:09 -070055 fVertexBuffer.reset(vertexBuffer);
halcanary96fcdcc2015-08-27 07:41:13 -070056 fIndexBuffer.reset(nullptr);
bsalomoncb8979d2015-05-05 09:51:38 -070057 fStartVertex = startVertex;
58 fStartIndex = 0;
59 fVertexCount = vertexCount;
60 fIndexCount = 0;
61 fInstanceCount = 0;
62 fVerticesPerInstance = 0;
63 fIndicesPerInstance = 0;
bsalomone64eb572015-05-07 11:35:55 -070064 fMaxInstancesPerDraw = 0;
bsalomoncb8979d2015-05-05 09:51:38 -070065 }
66
67 void initIndexed(GrPrimitiveType primType,
cdalton397536c2016-03-25 12:15:03 -070068 const GrBuffer* vertexBuffer,
69 const GrBuffer* indexBuffer,
bsalomoncb8979d2015-05-05 09:51:38 -070070 int startVertex,
71 int startIndex,
72 int vertexCount,
73 int indexCount) {
74 SkASSERT(indexBuffer);
75 SkASSERT(vertexBuffer);
76 SkASSERT(indexCount);
77 SkASSERT(vertexCount);
78 SkASSERT(startIndex >= 0);
79 SkASSERT(startVertex >= 0);
80 fPrimitiveType = primType;
bsalomon301f9892015-05-05 13:20:09 -070081 fVertexBuffer.reset(vertexBuffer);
82 fIndexBuffer.reset(indexBuffer);
bsalomoncb8979d2015-05-05 09:51:38 -070083 fStartVertex = startVertex;
84 fStartIndex = startIndex;
85 fVertexCount = vertexCount;
86 fIndexCount = indexCount;
87 fInstanceCount = 0;
88 fVerticesPerInstance = 0;
89 fIndicesPerInstance = 0;
bsalomone64eb572015-05-07 11:35:55 -070090 fMaxInstancesPerDraw = 0;
bsalomoncb8979d2015-05-05 09:51:38 -070091 }
92
bsalomone64eb572015-05-07 11:35:55 -070093
94 /** Variation of the above that may be used when the total number of instances may exceed
95 the number of instances supported by the index buffer. To be used with
96 nextInstances() to draw in max-sized batches.*/
bsalomoncb8979d2015-05-05 09:51:38 -070097 void initInstanced(GrPrimitiveType primType,
cdalton397536c2016-03-25 12:15:03 -070098 const GrBuffer* vertexBuffer,
99 const GrBuffer* indexBuffer,
bsalomoncb8979d2015-05-05 09:51:38 -0700100 int startVertex,
101 int verticesPerInstance,
102 int indicesPerInstance,
bsalomone64eb572015-05-07 11:35:55 -0700103 int instanceCount,
104 int maxInstancesPerDraw) {
bsalomoncb8979d2015-05-05 09:51:38 -0700105 SkASSERT(vertexBuffer);
106 SkASSERT(indexBuffer);
107 SkASSERT(instanceCount);
108 SkASSERT(verticesPerInstance);
109 SkASSERT(indicesPerInstance);
110 SkASSERT(startVertex >= 0);
111 fPrimitiveType = primType;
bsalomon301f9892015-05-05 13:20:09 -0700112 fVertexBuffer.reset(vertexBuffer);
113 fIndexBuffer.reset(indexBuffer);
bsalomoncb8979d2015-05-05 09:51:38 -0700114 fStartVertex = startVertex;
115 fStartIndex = 0;
116 fVerticesPerInstance = verticesPerInstance;
117 fIndicesPerInstance = indicesPerInstance;
118 fInstanceCount = instanceCount;
119 fVertexCount = instanceCount * fVerticesPerInstance;
120 fIndexCount = instanceCount * fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -0700121 fMaxInstancesPerDraw = maxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -0700122 }
123
bsalomoncb8979d2015-05-05 09:51:38 -0700124
125 /** These return 0 if initInstanced was not used to initialize the GrVertices. */
126 int verticesPerInstance() const { return fVerticesPerInstance; }
127 int indicesPerInstance() const { return fIndicesPerInstance; }
128 int instanceCount() const { return fInstanceCount; }
129
bsalomoncb8979d2015-05-05 09:51:38 -0700130 bool isInstanced() const { return fInstanceCount > 0; }
131
bsalomone64eb572015-05-07 11:35:55 -0700132 class Iterator {
133 public:
egdaniel0e1853c2016-03-17 11:35:45 -0700134 const GrNonInstancedMesh* init(const GrMesh& mesh) {
135 fMesh = &mesh;
136 if (mesh.fInstanceCount <= mesh.fMaxInstancesPerDraw) {
bsalomone64eb572015-05-07 11:35:55 -0700137 fInstancesRemaining = 0;
138 // Note, this also covers the non-instanced case!
egdaniel0e1853c2016-03-17 11:35:45 -0700139 return &mesh;
bsalomone64eb572015-05-07 11:35:55 -0700140 }
egdaniel0e1853c2016-03-17 11:35:45 -0700141 SkASSERT(mesh.isInstanced());
142 fInstanceBatch.fIndexBuffer.reset(mesh.fIndexBuffer.get());
143 fInstanceBatch.fVertexBuffer.reset(mesh.fVertexBuffer.get());
144 fInstanceBatch.fIndexCount = mesh.fMaxInstancesPerDraw *
145 mesh.fIndicesPerInstance;
146 fInstanceBatch.fVertexCount = mesh.fMaxInstancesPerDraw *
147 mesh.fVerticesPerInstance;
148 fInstanceBatch.fPrimitiveType = mesh.fPrimitiveType;
149 fInstanceBatch.fStartIndex = mesh.fStartIndex;
150 fInstanceBatch.fStartVertex = mesh.fStartVertex;
151 fInstancesRemaining = mesh.fInstanceCount - mesh.fMaxInstancesPerDraw;
bsalomone64eb572015-05-07 11:35:55 -0700152 return &fInstanceBatch;
bsalomoncb8979d2015-05-05 09:51:38 -0700153 }
bsalomoncb8979d2015-05-05 09:51:38 -0700154
egdaniel0e1853c2016-03-17 11:35:45 -0700155 const GrNonInstancedMesh* next() {
bsalomone64eb572015-05-07 11:35:55 -0700156 if (!fInstancesRemaining) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
bsalomone64eb572015-05-07 11:35:55 -0700158 }
159 fInstanceBatch.fStartVertex += fInstanceBatch.fVertexCount;
egdaniel0e1853c2016-03-17 11:35:45 -0700160 int instances = SkTMin(fInstancesRemaining, fMesh->fMaxInstancesPerDraw);
161 fInstanceBatch.fIndexCount = instances * fMesh->fIndicesPerInstance;
162 fInstanceBatch.fVertexCount = instances * fMesh->fVerticesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -0700163 fInstancesRemaining -= instances;
164 return &fInstanceBatch;
165 }
166 private:
egdaniel0e1853c2016-03-17 11:35:45 -0700167 GrNonInstancedMesh fInstanceBatch;
168 const GrMesh* fMesh;
bsalomone64eb572015-05-07 11:35:55 -0700169 int fInstancesRemaining;
170 };
bsalomoncb8979d2015-05-05 09:51:38 -0700171
172private:
bsalomoncb8979d2015-05-05 09:51:38 -0700173 int fInstanceCount;
174 int fVerticesPerInstance;
175 int fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -0700176 int fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -0700177};
178
179#endif