csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | namespace gr_instanced { |
| 17 | |
| 18 | class GLInstancedRendering::GLBatch : public InstancedRendering::Batch { |
| 19 | public: |
| 20 | DEFINE_BATCH_CLASS_ID |
| 21 | |
| 22 | GLBatch(GLInstancedRendering* instRendering) : INHERITED(ClassID(), instRendering) {} |
| 23 | int numGLCommands() const { return 1 + fNumChangesInGeometry; } |
| 24 | |
| 25 | private: |
| 26 | int fEmulatedBaseInstance; |
| 27 | int fGLDrawCmdsIdx; |
| 28 | |
| 29 | friend class GLInstancedRendering; |
| 30 | |
| 31 | typedef Batch INHERITED; |
| 32 | }; |
| 33 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 34 | GrCaps::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()); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 37 | if (!glCaps.vertexArrayObjectSupport() || |
| 38 | (!glCaps.drawIndirectSupport() && !glCaps.drawInstancedSupport())) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 39 | return GrCaps::InstancedSupport::kNone; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 40 | } |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 41 | return InstanceProcessor::CheckSupport(*glCaps.glslCaps(), glCaps); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 42 | } |
| 43 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 44 | GLInstancedRendering::GLInstancedRendering(GrGLGpu* gpu) |
| 45 | : INHERITED(gpu), |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 46 | fVertexArrayID(0), |
| 47 | fGLDrawCmdsInfo(0), |
| 48 | fInstanceAttribsBufferUniqueId(SK_InvalidUniqueID) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 49 | SkASSERT(GrCaps::InstancedSupport::kNone != this->gpu()->caps()->instancedSupport()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | GLInstancedRendering::~GLInstancedRendering() { |
| 53 | if (fVertexArrayID) { |
| 54 | GL_CALL(DeleteVertexArrays(1, &fVertexArrayID)); |
| 55 | this->glGpu()->notifyVertexArrayDelete(fVertexArrayID); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | inline GrGLGpu* GLInstancedRendering::glGpu() const { |
| 60 | return static_cast<GrGLGpu*>(this->gpu()); |
| 61 | } |
| 62 | |
| 63 | InstancedRendering::Batch* GLInstancedRendering::createBatch() { |
| 64 | return new GLBatch(this); |
| 65 | } |
| 66 | |
| 67 | void GLInstancedRendering::onBeginFlush(GrResourceProvider* rp) { |
| 68 | // Count what there is to draw. |
| 69 | BatchList::Iter iter; |
| 70 | iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart); |
| 71 | int numGLInstances = 0; |
| 72 | int numGLDrawCmds = 0; |
| 73 | while (Batch* b = iter.get()) { |
| 74 | GLBatch* batch = static_cast<GLBatch*>(b); |
| 75 | iter.next(); |
| 76 | |
| 77 | numGLInstances += batch->fNumDraws; |
| 78 | numGLDrawCmds += batch->numGLCommands(); |
| 79 | } |
| 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. |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 94 | SkASSERT(!this->indexBuffer()->isCPUBacked()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 95 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 96 | static_cast<const GrGLBuffer*>(this->indexBuffer())->bufferID())); |
| 97 | |
| 98 | // Set up the non-instanced attribs. |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 99 | this->glGpu()->bindBuffer(kVertex_GrBufferType, this->vertexBuffer()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 100 | 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 | |
| 107 | SkASSERT(SK_InvalidUniqueID == fInstanceAttribsBufferUniqueId); |
| 108 | } |
| 109 | |
| 110 | // Create and map instance and draw-indirect buffers. |
| 111 | SkASSERT(!fInstanceBuffer); |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 112 | fInstanceBuffer.reset( |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 113 | rp->createBuffer(sizeof(Instance) * numGLInstances, kVertex_GrBufferType, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 114 | kDynamic_GrAccessPattern, |
| 115 | GrResourceProvider::kNoPendingIO_Flag | |
| 116 | GrResourceProvider::kRequireGpuMemory_Flag)); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 117 | if (!fInstanceBuffer) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | SkASSERT(!fDrawIndirectBuffer); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 122 | 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 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | Instance* glMappedInstances = static_cast<Instance*>(fInstanceBuffer->map()); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 134 | SkASSERT(glMappedInstances); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 135 | int glInstancesIdx = 0; |
| 136 | |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 137 | GrGLDrawElementsIndirectCommand* glMappedCmds = nullptr; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 138 | int glDrawCmdsIdx = 0; |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 139 | if (fDrawIndirectBuffer) { |
| 140 | glMappedCmds = static_cast<GrGLDrawElementsIndirectCommand*>(fDrawIndirectBuffer->map()); |
| 141 | SkASSERT(glMappedCmds); |
| 142 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 143 | |
| 144 | bool baseInstanceSupport = this->glGpu()->glCaps().baseInstanceSupport(); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 145 | SkASSERT(!baseInstanceSupport || fDrawIndirectBuffer); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 146 | |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 147 | SkASSERT(!fGLDrawCmdsInfo); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 148 | if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) { |
| 149 | fGLDrawCmdsInfo.reset(numGLDrawCmds); |
| 150 | } |
| 151 | |
| 152 | // Generate the instance and draw-indirect buffer contents based on the tracked batches. |
| 153 | iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart); |
| 154 | while (Batch* b = iter.get()) { |
| 155 | GLBatch* batch = static_cast<GLBatch*>(b); |
| 156 | iter.next(); |
| 157 | |
| 158 | batch->fEmulatedBaseInstance = baseInstanceSupport ? 0 : glInstancesIdx; |
| 159 | batch->fGLDrawCmdsIdx = glDrawCmdsIdx; |
| 160 | |
| 161 | const Batch::Draw* draw = batch->fHeadDraw; |
| 162 | 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 | |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 173 | 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 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 181 | |
| 182 | if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) { |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 183 | GLDrawCmdInfo& cmdInfo = fGLDrawCmdsInfo[glDrawCmdsIdx]; |
| 184 | cmdInfo.fGeometry = geometry; |
| 185 | cmdInfo.fInstanceCount = instanceCount; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | glInstancesIdx += instanceCount; |
| 189 | ++glDrawCmdsIdx; |
| 190 | } while (draw); |
| 191 | } |
| 192 | |
| 193 | SkASSERT(glDrawCmdsIdx == numGLDrawCmds); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 194 | if (fDrawIndirectBuffer) { |
| 195 | fDrawIndirectBuffer->unmap(); |
| 196 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 197 | |
| 198 | SkASSERT(glInstancesIdx == numGLInstances); |
| 199 | fInstanceBuffer->unmap(); |
| 200 | } |
| 201 | |
| 202 | void GLInstancedRendering::onDraw(const GrPipeline& pipeline, const InstanceProcessor& instProc, |
| 203 | const Batch* baseBatch) { |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 204 | if (!fDrawIndirectBuffer && !fGLDrawCmdsInfo) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 205 | return; // beginFlush was not successful. |
| 206 | } |
| 207 | if (!this->glGpu()->flushGLState(pipeline, instProc)) { |
| 208 | return; |
| 209 | } |
| 210 | |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 211 | if (fDrawIndirectBuffer) { |
| 212 | this->glGpu()->bindBuffer(kDrawIndirect_GrBufferType, fDrawIndirectBuffer.get()); |
| 213 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 214 | |
| 215 | const GrGLCaps& glCaps = this->glGpu()->glCaps(); |
| 216 | const GLBatch* batch = static_cast<const GLBatch*>(baseBatch); |
| 217 | int numCommands = batch->numGLCommands(); |
| 218 | |
| 219 | #if GR_GL_LOG_INSTANCED_BATCHES |
| 220 | SkASSERT(fGLDrawCmdsInfo); |
| 221 | SkDebugf("Instanced batch: ["); |
| 222 | for (int i = 0; i < numCommands; ++i) { |
| 223 | int glCmdIdx = batch->fGLDrawCmdsIdx + i; |
| 224 | 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 | |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 232 | if (numCommands > 1 && glCaps.multiDrawIndirectSupport() && glCaps.baseInstanceSupport()) { |
| 233 | SkASSERT(fDrawIndirectBuffer); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 234 | int glCmdsIdx = batch->fGLDrawCmdsIdx; |
| 235 | this->flushInstanceAttribs(batch->fEmulatedBaseInstance); |
| 236 | GL_CALL(MultiDrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE, |
| 237 | (GrGLDrawElementsIndirectCommand*) nullptr + glCmdsIdx, |
| 238 | numCommands, 0)); |
csmartdalton | 4c18b62 | 2016-07-29 12:19:28 -0700 | [diff] [blame^] | 239 | return; |
| 240 | } |
| 241 | |
| 242 | int emulatedBaseInstance = batch->fEmulatedBaseInstance; |
| 243 | for (int i = 0; i < numCommands; ++i) { |
| 244 | int glCmdIdx = batch->fGLDrawCmdsIdx + i; |
| 245 | const GLDrawCmdInfo& cmdInfo = fGLDrawCmdsInfo[glCmdIdx]; |
| 246 | this->flushInstanceAttribs(emulatedBaseInstance); |
| 247 | if (fDrawIndirectBuffer) { |
| 248 | GL_CALL(DrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE, |
| 249 | (GrGLDrawElementsIndirectCommand*) nullptr + glCmdIdx)); |
| 250 | } else { |
| 251 | 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()) { |
| 257 | emulatedBaseInstance += cmdInfo.fInstanceCount; |
| 258 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | void GLInstancedRendering::flushInstanceAttribs(int baseInstance) { |
| 263 | SkASSERT(fVertexArrayID); |
| 264 | this->glGpu()->bindVertexArray(fVertexArrayID); |
| 265 | |
| 266 | SkASSERT(fInstanceBuffer); |
| 267 | if (fInstanceAttribsBufferUniqueId != fInstanceBuffer->getUniqueID() || |
| 268 | fInstanceAttribsBaseInstance != baseInstance) { |
| 269 | Instance* offsetInBuffer = (Instance*) nullptr + baseInstance; |
| 270 | |
| 271 | this->glGpu()->bindBuffer(kVertex_GrBufferType, fInstanceBuffer.get()); |
| 272 | |
| 273 | // Info attrib. |
| 274 | GL_CALL(EnableVertexAttribArray((int)Attrib::kInstanceInfo)); |
| 275 | GL_CALL(VertexAttribIPointer((int)Attrib::kInstanceInfo, 1, GR_GL_UNSIGNED_INT, |
| 276 | sizeof(Instance), &offsetInBuffer->fInfo)); |
| 277 | GL_CALL(VertexAttribDivisor((int)Attrib::kInstanceInfo, 1)); |
| 278 | |
| 279 | // Shape matrix attrib. |
| 280 | GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixX)); |
| 281 | GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixY)); |
| 282 | GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixX, 3, GR_GL_FLOAT, GR_GL_FALSE, |
| 283 | sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[0])); |
| 284 | GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixY, 3, GR_GL_FLOAT, GR_GL_FALSE, |
| 285 | sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[3])); |
| 286 | GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixX, 1)); |
| 287 | GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixY, 1)); |
| 288 | |
| 289 | // Color attrib. |
| 290 | GL_CALL(EnableVertexAttribArray((int)Attrib::kColor)); |
| 291 | GL_CALL(VertexAttribPointer((int)Attrib::kColor, 4, GR_GL_UNSIGNED_BYTE, GR_GL_TRUE, |
| 292 | sizeof(Instance), &offsetInBuffer->fColor)); |
| 293 | GL_CALL(VertexAttribDivisor((int)Attrib::kColor, 1)); |
| 294 | |
| 295 | // Local rect attrib. |
| 296 | GL_CALL(EnableVertexAttribArray((int)Attrib::kLocalRect)); |
| 297 | GL_CALL(VertexAttribPointer((int)Attrib::kLocalRect, 4, GR_GL_FLOAT, GR_GL_FALSE, |
| 298 | sizeof(Instance), &offsetInBuffer->fLocalRect)); |
| 299 | GL_CALL(VertexAttribDivisor((int)Attrib::kLocalRect, 1)); |
| 300 | |
| 301 | fInstanceAttribsBufferUniqueId = fInstanceBuffer->getUniqueID(); |
| 302 | fInstanceAttribsBaseInstance = baseInstance; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void GLInstancedRendering::onEndFlush() { |
| 307 | fInstanceBuffer.reset(); |
| 308 | fDrawIndirectBuffer.reset(); |
| 309 | fGLDrawCmdsInfo.reset(0); |
| 310 | } |
| 311 | |
| 312 | void GLInstancedRendering::onResetGpuResources(ResetType resetType) { |
| 313 | if (fVertexArrayID && ResetType::kDestroy == resetType) { |
| 314 | GL_CALL(DeleteVertexArrays(1, &fVertexArrayID)); |
| 315 | this->glGpu()->notifyVertexArrayDelete(fVertexArrayID); |
| 316 | } |
| 317 | fVertexArrayID = 0; |
| 318 | fInstanceBuffer.reset(); |
| 319 | fDrawIndirectBuffer.reset(); |
| 320 | fInstanceAttribsBufferUniqueId = SK_InvalidUniqueID; |
| 321 | } |
| 322 | |
| 323 | } |