blob: 0d2a2ddb9c7d27c5bc5185dc0c5db1a08dcab9b5 [file] [log] [blame]
csmartdaltona7f29642016-07-07 08:49:11 -07001/*
2 * Copyright 2016 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#include "GLInstancedRendering.h"
9
10#include "GrResourceProvider.h"
11#include "gl/GrGLGpu.h"
12#include "instanced/InstanceProcessor.h"
13
14#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
15
16namespace gr_instanced {
17
Brian Salomon99ad1642016-12-16 09:50:45 -050018class GLInstancedRendering::GLOp final : public InstancedRendering::Op {
csmartdaltona7f29642016-07-07 08:49:11 -070019public:
Brian Salomon25a88092016-12-01 09:36:50 -050020 DEFINE_OP_CLASS_ID
csmartdaltona7f29642016-07-07 08:49:11 -070021
Brian Salomon99ad1642016-12-16 09:50:45 -050022 GLOp(GLInstancedRendering* instRendering) : INHERITED(ClassID(), instRendering) {}
csmartdaltona7f29642016-07-07 08:49:11 -070023 int numGLCommands() const { return 1 + fNumChangesInGeometry; }
24
25private:
26 int fEmulatedBaseInstance;
27 int fGLDrawCmdsIdx;
28
29 friend class GLInstancedRendering;
30
Brian Salomon99ad1642016-12-16 09:50:45 -050031 typedef Op INHERITED;
csmartdaltona7f29642016-07-07 08:49:11 -070032};
33
csmartdaltone0d36292016-07-29 08:14:20 -070034GrCaps::InstancedSupport GLInstancedRendering::CheckSupport(const GrGLCaps& glCaps) {
35 // This method is only intended to be used for initializing fInstancedSupport in the caps.
36 SkASSERT(GrCaps::InstancedSupport::kNone == glCaps.instancedSupport());
csmartdalton4c18b622016-07-29 12:19:28 -070037 if (!glCaps.vertexArrayObjectSupport() ||
38 (!glCaps.drawIndirectSupport() && !glCaps.drawInstancedSupport())) {
csmartdaltone0d36292016-07-29 08:14:20 -070039 return GrCaps::InstancedSupport::kNone;
csmartdaltona7f29642016-07-07 08:49:11 -070040 }
Brian Salomon1edc5b92016-11-29 13:43:46 -050041 return InstanceProcessor::CheckSupport(*glCaps.shaderCaps(), glCaps);
csmartdaltona7f29642016-07-07 08:49:11 -070042}
43
csmartdaltone0d36292016-07-29 08:14:20 -070044GLInstancedRendering::GLInstancedRendering(GrGLGpu* gpu)
45 : INHERITED(gpu),
csmartdaltona7f29642016-07-07 08:49:11 -070046 fVertexArrayID(0),
47 fGLDrawCmdsInfo(0),
48 fInstanceAttribsBufferUniqueId(SK_InvalidUniqueID) {
csmartdaltone0d36292016-07-29 08:14:20 -070049 SkASSERT(GrCaps::InstancedSupport::kNone != this->gpu()->caps()->instancedSupport());
csmartdaltona7f29642016-07-07 08:49:11 -070050}
51
52GLInstancedRendering::~GLInstancedRendering() {
53 if (fVertexArrayID) {
54 GL_CALL(DeleteVertexArrays(1, &fVertexArrayID));
55 this->glGpu()->notifyVertexArrayDelete(fVertexArrayID);
56 }
57}
58
59inline GrGLGpu* GLInstancedRendering::glGpu() const {
60 return static_cast<GrGLGpu*>(this->gpu());
61}
62
Brian Salomonf8334782017-01-03 09:42:58 -050063std::unique_ptr<InstancedRendering::Op> GLInstancedRendering::makeOp() {
64 return std::unique_ptr<Op>(new GLOp(this));
65}
csmartdaltona7f29642016-07-07 08:49:11 -070066
67void GLInstancedRendering::onBeginFlush(GrResourceProvider* rp) {
68 // Count what there is to draw.
Brian Salomon99ad1642016-12-16 09:50:45 -050069 OpList::Iter iter;
70 iter.init(this->trackedOps(), OpList::Iter::kHead_IterStart);
csmartdaltona7f29642016-07-07 08:49:11 -070071 int numGLInstances = 0;
72 int numGLDrawCmds = 0;
Brian Salomon99ad1642016-12-16 09:50:45 -050073 while (Op* o = iter.get()) {
74 GLOp* op = static_cast<GLOp*>(o);
csmartdaltona7f29642016-07-07 08:49:11 -070075 iter.next();
76
Brian Salomon99ad1642016-12-16 09:50:45 -050077 numGLInstances += op->fNumDraws;
78 numGLDrawCmds += op->numGLCommands();
csmartdaltona7f29642016-07-07 08:49:11 -070079 }
80 if (!numGLDrawCmds) {
81 return;
82 }
83 SkASSERT(numGLInstances);
84
85 // Lazily create a vertex array object.
86 if (!fVertexArrayID) {
87 GL_CALL(GenVertexArrays(1, &fVertexArrayID));
88 if (!fVertexArrayID) {
89 return;
90 }
91 this->glGpu()->bindVertexArray(fVertexArrayID);
92
93 // Attach our index buffer to the vertex array.
csmartdalton485a1202016-07-13 10:16:32 -070094 SkASSERT(!this->indexBuffer()->isCPUBacked());
csmartdaltona7f29642016-07-07 08:49:11 -070095 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
96 static_cast<const GrGLBuffer*>(this->indexBuffer())->bufferID()));
97
98 // Set up the non-instanced attribs.
csmartdalton485a1202016-07-13 10:16:32 -070099 this->glGpu()->bindBuffer(kVertex_GrBufferType, this->vertexBuffer());
csmartdaltona7f29642016-07-07 08:49:11 -0700100 GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeCoords));
101 GL_CALL(VertexAttribPointer((int)Attrib::kShapeCoords, 2, GR_GL_FLOAT, GR_GL_FALSE,
102 sizeof(ShapeVertex), (void*) offsetof(ShapeVertex, fX)));
103 GL_CALL(EnableVertexAttribArray((int)Attrib::kVertexAttrs));
104 GL_CALL(VertexAttribIPointer((int)Attrib::kVertexAttrs, 1, GR_GL_INT, sizeof(ShapeVertex),
105 (void*) offsetof(ShapeVertex, fAttrs)));
106
Robert Phillips294870f2016-11-11 12:38:40 -0500107 SkASSERT(fInstanceAttribsBufferUniqueId.isInvalid());
csmartdaltona7f29642016-07-07 08:49:11 -0700108 }
109
110 // Create and map instance and draw-indirect buffers.
111 SkASSERT(!fInstanceBuffer);
csmartdalton485a1202016-07-13 10:16:32 -0700112 fInstanceBuffer.reset(
csmartdaltona7f29642016-07-07 08:49:11 -0700113 rp->createBuffer(sizeof(Instance) * numGLInstances, kVertex_GrBufferType,
csmartdalton485a1202016-07-13 10:16:32 -0700114 kDynamic_GrAccessPattern,
115 GrResourceProvider::kNoPendingIO_Flag |
116 GrResourceProvider::kRequireGpuMemory_Flag));
csmartdaltona7f29642016-07-07 08:49:11 -0700117 if (!fInstanceBuffer) {
118 return;
119 }
120
121 SkASSERT(!fDrawIndirectBuffer);
csmartdalton4c18b622016-07-29 12:19:28 -0700122 if (this->glGpu()->glCaps().drawIndirectSupport()) {
123 fDrawIndirectBuffer.reset(
124 rp->createBuffer(sizeof(GrGLDrawElementsIndirectCommand) * numGLDrawCmds,
125 kDrawIndirect_GrBufferType, kDynamic_GrAccessPattern,
126 GrResourceProvider::kNoPendingIO_Flag |
127 GrResourceProvider::kRequireGpuMemory_Flag));
128 if (!fDrawIndirectBuffer) {
129 return;
130 }
csmartdaltona7f29642016-07-07 08:49:11 -0700131 }
132
133 Instance* glMappedInstances = static_cast<Instance*>(fInstanceBuffer->map());
csmartdalton4c18b622016-07-29 12:19:28 -0700134 SkASSERT(glMappedInstances);
csmartdaltona7f29642016-07-07 08:49:11 -0700135 int glInstancesIdx = 0;
136
csmartdalton4c18b622016-07-29 12:19:28 -0700137 GrGLDrawElementsIndirectCommand* glMappedCmds = nullptr;
csmartdaltona7f29642016-07-07 08:49:11 -0700138 int glDrawCmdsIdx = 0;
csmartdalton4c18b622016-07-29 12:19:28 -0700139 if (fDrawIndirectBuffer) {
140 glMappedCmds = static_cast<GrGLDrawElementsIndirectCommand*>(fDrawIndirectBuffer->map());
141 SkASSERT(glMappedCmds);
142 }
csmartdaltona7f29642016-07-07 08:49:11 -0700143
144 bool baseInstanceSupport = this->glGpu()->glCaps().baseInstanceSupport();
csmartdalton4c18b622016-07-29 12:19:28 -0700145 SkASSERT(!baseInstanceSupport || fDrawIndirectBuffer);
csmartdaltona7f29642016-07-07 08:49:11 -0700146
csmartdalton4c18b622016-07-29 12:19:28 -0700147 SkASSERT(!fGLDrawCmdsInfo);
Brian Salomon99ad1642016-12-16 09:50:45 -0500148 if (GR_GL_LOG_INSTANCED_OPS || !baseInstanceSupport) {
csmartdaltona7f29642016-07-07 08:49:11 -0700149 fGLDrawCmdsInfo.reset(numGLDrawCmds);
150 }
151
Brian Salomon99ad1642016-12-16 09:50:45 -0500152 // Generate the instance and draw-indirect buffer contents based on the tracked ops.
153 iter.init(this->trackedOps(), OpList::Iter::kHead_IterStart);
154 while (Op* o = iter.get()) {
155 GLOp* op = static_cast<GLOp*>(o);
csmartdaltona7f29642016-07-07 08:49:11 -0700156 iter.next();
157
Brian Salomon99ad1642016-12-16 09:50:45 -0500158 op->fEmulatedBaseInstance = baseInstanceSupport ? 0 : glInstancesIdx;
159 op->fGLDrawCmdsIdx = glDrawCmdsIdx;
csmartdaltona7f29642016-07-07 08:49:11 -0700160
Brian Salomon99ad1642016-12-16 09:50:45 -0500161 const Op::Draw* draw = op->fHeadDraw;
csmartdaltona7f29642016-07-07 08:49:11 -0700162 SkASSERT(draw);
163 do {
164 int instanceCount = 0;
165 IndexRange geometry = draw->fGeometry;
166 SkASSERT(!geometry.isEmpty());
167
168 do {
169 glMappedInstances[glInstancesIdx + instanceCount++] = draw->fInstance;
170 draw = draw->fNext;
171 } while (draw && draw->fGeometry == geometry);
172
csmartdalton4c18b622016-07-29 12:19:28 -0700173 if (fDrawIndirectBuffer) {
174 GrGLDrawElementsIndirectCommand& glCmd = glMappedCmds[glDrawCmdsIdx];
175 glCmd.fCount = geometry.fCount;
176 glCmd.fInstanceCount = instanceCount;
177 glCmd.fFirstIndex = geometry.fStart;
178 glCmd.fBaseVertex = 0;
179 glCmd.fBaseInstance = baseInstanceSupport ? glInstancesIdx : 0;
180 }
csmartdaltona7f29642016-07-07 08:49:11 -0700181
Brian Salomon99ad1642016-12-16 09:50:45 -0500182 if (GR_GL_LOG_INSTANCED_OPS || !baseInstanceSupport) {
csmartdalton4c18b622016-07-29 12:19:28 -0700183 GLDrawCmdInfo& cmdInfo = fGLDrawCmdsInfo[glDrawCmdsIdx];
184 cmdInfo.fGeometry = geometry;
185 cmdInfo.fInstanceCount = instanceCount;
csmartdaltona7f29642016-07-07 08:49:11 -0700186 }
187
188 glInstancesIdx += instanceCount;
189 ++glDrawCmdsIdx;
190 } while (draw);
191 }
192
193 SkASSERT(glDrawCmdsIdx == numGLDrawCmds);
csmartdalton4c18b622016-07-29 12:19:28 -0700194 if (fDrawIndirectBuffer) {
195 fDrawIndirectBuffer->unmap();
196 }
csmartdaltona7f29642016-07-07 08:49:11 -0700197
198 SkASSERT(glInstancesIdx == numGLInstances);
199 fInstanceBuffer->unmap();
200}
201
202void GLInstancedRendering::onDraw(const GrPipeline& pipeline, const InstanceProcessor& instProc,
Brian Salomon99ad1642016-12-16 09:50:45 -0500203 const Op* baseOp) {
csmartdalton4c18b622016-07-29 12:19:28 -0700204 if (!fDrawIndirectBuffer && !fGLDrawCmdsInfo) {
csmartdaltona7f29642016-07-07 08:49:11 -0700205 return; // beginFlush was not successful.
206 }
bsalomon2eda5b32016-09-21 10:53:24 -0700207 if (!this->glGpu()->flushGLState(pipeline, instProc, false)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700208 return;
209 }
210
csmartdalton4c18b622016-07-29 12:19:28 -0700211 if (fDrawIndirectBuffer) {
212 this->glGpu()->bindBuffer(kDrawIndirect_GrBufferType, fDrawIndirectBuffer.get());
213 }
csmartdaltona7f29642016-07-07 08:49:11 -0700214
215 const GrGLCaps& glCaps = this->glGpu()->glCaps();
Brian Salomon99ad1642016-12-16 09:50:45 -0500216 const GLOp* op = static_cast<const GLOp*>(baseOp);
217 int numCommands = op->numGLCommands();
csmartdaltona7f29642016-07-07 08:49:11 -0700218
Brian Salomon99ad1642016-12-16 09:50:45 -0500219#if GR_GL_LOG_INSTANCED_OPS
csmartdaltona7f29642016-07-07 08:49:11 -0700220 SkASSERT(fGLDrawCmdsInfo);
Brian Salomon99ad1642016-12-16 09:50:45 -0500221 SkDebugf("Instanced op: [");
csmartdaltona7f29642016-07-07 08:49:11 -0700222 for (int i = 0; i < numCommands; ++i) {
Brian Salomon99ad1642016-12-16 09:50:45 -0500223 int glCmdIdx = op->fGLDrawCmdsIdx + i;
csmartdaltona7f29642016-07-07 08:49:11 -0700224 SkDebugf("%s%i * %s", (i ? ", " : ""), fGLDrawCmdsInfo[glCmdIdx].fInstanceCount,
225 InstanceProcessor::GetNameOfIndexRange(fGLDrawCmdsInfo[glCmdIdx].fGeometry));
226 }
227 SkDebugf("]\n");
228#else
229 SkASSERT(SkToBool(fGLDrawCmdsInfo) == !glCaps.baseInstanceSupport());
230#endif
231
csmartdalton4c18b622016-07-29 12:19:28 -0700232 if (numCommands > 1 && glCaps.multiDrawIndirectSupport() && glCaps.baseInstanceSupport()) {
233 SkASSERT(fDrawIndirectBuffer);
Brian Salomon99ad1642016-12-16 09:50:45 -0500234 int glCmdsIdx = op->fGLDrawCmdsIdx;
235 this->flushInstanceAttribs(op->fEmulatedBaseInstance);
csmartdaltona7f29642016-07-07 08:49:11 -0700236 GL_CALL(MultiDrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE,
237 (GrGLDrawElementsIndirectCommand*) nullptr + glCmdsIdx,
238 numCommands, 0));
csmartdalton4c18b622016-07-29 12:19:28 -0700239 return;
240 }
241
Brian Salomon99ad1642016-12-16 09:50:45 -0500242 int emulatedBaseInstance = op->fEmulatedBaseInstance;
csmartdalton4c18b622016-07-29 12:19:28 -0700243 for (int i = 0; i < numCommands; ++i) {
Brian Salomon99ad1642016-12-16 09:50:45 -0500244 int glCmdIdx = op->fGLDrawCmdsIdx + i;
csmartdalton4c18b622016-07-29 12:19:28 -0700245 this->flushInstanceAttribs(emulatedBaseInstance);
246 if (fDrawIndirectBuffer) {
247 GL_CALL(DrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE,
248 (GrGLDrawElementsIndirectCommand*) nullptr + glCmdIdx));
249 } else {
mtklein35b26a42016-09-15 09:56:28 -0700250 const GLDrawCmdInfo& cmdInfo = fGLDrawCmdsInfo[glCmdIdx];
csmartdalton4c18b622016-07-29 12:19:28 -0700251 GL_CALL(DrawElementsInstanced(GR_GL_TRIANGLES, cmdInfo.fGeometry.fCount,
252 GR_GL_UNSIGNED_BYTE,
253 (GrGLubyte*) nullptr + cmdInfo.fGeometry.fStart,
254 cmdInfo.fInstanceCount));
255 }
256 if (!glCaps.baseInstanceSupport()) {
mtklein35b26a42016-09-15 09:56:28 -0700257 const GLDrawCmdInfo& cmdInfo = fGLDrawCmdsInfo[glCmdIdx];
csmartdalton4c18b622016-07-29 12:19:28 -0700258 emulatedBaseInstance += cmdInfo.fInstanceCount;
259 }
csmartdaltona7f29642016-07-07 08:49:11 -0700260 }
261}
262
263void GLInstancedRendering::flushInstanceAttribs(int baseInstance) {
264 SkASSERT(fVertexArrayID);
265 this->glGpu()->bindVertexArray(fVertexArrayID);
266
267 SkASSERT(fInstanceBuffer);
robertphillips8abb3702016-08-31 14:04:06 -0700268 if (fInstanceAttribsBufferUniqueId != fInstanceBuffer->uniqueID() ||
csmartdaltona7f29642016-07-07 08:49:11 -0700269 fInstanceAttribsBaseInstance != baseInstance) {
270 Instance* offsetInBuffer = (Instance*) nullptr + baseInstance;
271
272 this->glGpu()->bindBuffer(kVertex_GrBufferType, fInstanceBuffer.get());
273
274 // Info attrib.
275 GL_CALL(EnableVertexAttribArray((int)Attrib::kInstanceInfo));
276 GL_CALL(VertexAttribIPointer((int)Attrib::kInstanceInfo, 1, GR_GL_UNSIGNED_INT,
277 sizeof(Instance), &offsetInBuffer->fInfo));
278 GL_CALL(VertexAttribDivisor((int)Attrib::kInstanceInfo, 1));
279
280 // Shape matrix attrib.
281 GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixX));
282 GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixY));
283 GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixX, 3, GR_GL_FLOAT, GR_GL_FALSE,
284 sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[0]));
285 GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixY, 3, GR_GL_FLOAT, GR_GL_FALSE,
286 sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[3]));
287 GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixX, 1));
288 GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixY, 1));
289
290 // Color attrib.
291 GL_CALL(EnableVertexAttribArray((int)Attrib::kColor));
292 GL_CALL(VertexAttribPointer((int)Attrib::kColor, 4, GR_GL_UNSIGNED_BYTE, GR_GL_TRUE,
293 sizeof(Instance), &offsetInBuffer->fColor));
294 GL_CALL(VertexAttribDivisor((int)Attrib::kColor, 1));
295
296 // Local rect attrib.
297 GL_CALL(EnableVertexAttribArray((int)Attrib::kLocalRect));
298 GL_CALL(VertexAttribPointer((int)Attrib::kLocalRect, 4, GR_GL_FLOAT, GR_GL_FALSE,
299 sizeof(Instance), &offsetInBuffer->fLocalRect));
300 GL_CALL(VertexAttribDivisor((int)Attrib::kLocalRect, 1));
301
robertphillips8abb3702016-08-31 14:04:06 -0700302 fInstanceAttribsBufferUniqueId = fInstanceBuffer->uniqueID();
csmartdaltona7f29642016-07-07 08:49:11 -0700303 fInstanceAttribsBaseInstance = baseInstance;
304 }
305}
306
307void GLInstancedRendering::onEndFlush() {
308 fInstanceBuffer.reset();
309 fDrawIndirectBuffer.reset();
310 fGLDrawCmdsInfo.reset(0);
311}
312
313void GLInstancedRendering::onResetGpuResources(ResetType resetType) {
314 if (fVertexArrayID && ResetType::kDestroy == resetType) {
315 GL_CALL(DeleteVertexArrays(1, &fVertexArrayID));
316 this->glGpu()->notifyVertexArrayDelete(fVertexArrayID);
317 }
318 fVertexArrayID = 0;
319 fInstanceBuffer.reset();
320 fDrawIndirectBuffer.reset();
Robert Phillips294870f2016-11-11 12:38:40 -0500321 fInstanceAttribsBufferUniqueId.makeInvalid();
csmartdaltona7f29642016-07-07 08:49:11 -0700322}
323
324}