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 "InstancedRendering.h" |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 9 | |
| 10 | #include "InstancedOp.h" |
Brian Salomon | 13071c5 | 2017-03-29 21:28:20 -0400 | [diff] [blame] | 11 | #include "GrAppliedClip.h" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame^] | 13 | #include "GrGpu.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 14 | #include "GrPipeline.h" |
| 15 | #include "GrResourceProvider.h" |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 16 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 17 | #include "instanced/InstanceProcessor.h" |
| 18 | |
| 19 | namespace gr_instanced { |
| 20 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 21 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 22 | InstancedRendering::InstancedRendering(GrGpu* gpu) |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 23 | : fGpu(SkRef(gpu)) |
| 24 | SkDEBUGCODE(, fState(State::kRecordingDraws)) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 25 | } |
| 26 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 27 | |
| 28 | void InstancedRendering::beginFlush(GrResourceProvider* rp) { |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 29 | #ifdef SK_DEBUG |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 30 | SkASSERT(State::kRecordingDraws == fState); |
| 31 | fState = State::kFlushing; |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 32 | #endif |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 33 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 34 | if (fTrackedOps.isEmpty()) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (!fVertexBuffer) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 39 | fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get())); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 40 | if (!fVertexBuffer) { |
| 41 | return; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (!fIndexBuffer) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 46 | fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get())); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 47 | if (!fIndexBuffer) { |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (!fParams.empty()) { |
| 53 | fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel), |
| 54 | kTexel_GrBufferType, kDynamic_GrAccessPattern, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 55 | GrResourceProvider::kNoPendingIO_Flag | |
| 56 | GrResourceProvider::kRequireGpuMemory_Flag, |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 57 | fParams.begin())); |
| 58 | if (!fParamsBuffer) { |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | this->onBeginFlush(rp); |
| 64 | } |
| 65 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 66 | void InstancedRendering::draw(const GrPipeline& pipeline, |
| 67 | OpInfo info, |
| 68 | const InstancedOp* baseOp) { |
| 69 | InstanceProcessor instProc(info, fParamsBuffer.get()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 70 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 71 | this->onDraw(pipeline, instProc, baseOp); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void InstancedRendering::endFlush() { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 75 | // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 76 | // method has been called) before ending the flush. |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame] | 77 | SkASSERT(fTrackedOps.isEmpty()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 78 | fParams.reset(); |
| 79 | fParamsBuffer.reset(); |
| 80 | this->onEndFlush(); |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 81 | SkDEBUGCODE(fState = State::kRecordingDraws;) |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 82 | // Hold on to the shape coords and index buffers. |
| 83 | } |
| 84 | |
| 85 | void InstancedRendering::resetGpuResources(ResetType resetType) { |
| 86 | fVertexBuffer.reset(); |
| 87 | fIndexBuffer.reset(); |
| 88 | fParamsBuffer.reset(); |
| 89 | this->onResetGpuResources(resetType); |
| 90 | } |
| 91 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 92 | int InstancedRendering::addOpParams(InstancedOp* op) { |
| 93 | if (op->fParams.empty()) { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | SkASSERT(fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner. |
| 98 | int count = fParams.count(); |
| 99 | fParams.push_back_n(op->fParams.count(), op->fParams.begin()); |
| 100 | return count; |
| 101 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 102 | } |