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