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