Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 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 | #ifndef GrDrawIndirectCommand_DEFINED |
| 9 | #define GrDrawIndirectCommand_DEFINED |
| 10 | |
| 11 | #include "src/gpu/GrCaps.h" |
| 12 | #include <array> |
| 13 | |
| 14 | // Draw commands on the GPU are simple tuples of uint32_t. The ordering is backend-specific. |
| 15 | using GrDrawIndirectCommand = std::array<uint32_t, 4>; |
| 16 | using GrDrawIndexedIndirectCommand = std::array<uint32_t, 5>; |
| 17 | |
| 18 | // Helper for writing commands to an indirect draw buffer. Usage: |
| 19 | // |
| 20 | // GrDrawIndirectWriter indirectWriter = target->makeDrawIndirectSpace(...); |
| 21 | // indirectWriter.write(...); |
| 22 | // indirectWriter.write(...); |
| 23 | struct GrDrawIndirectWriter { |
| 24 | public: |
| 25 | GrDrawIndirectWriter() = default; |
| 26 | GrDrawIndirectWriter(void* data) : fData(static_cast<GrDrawIndirectCommand*>(data)) {} |
| 27 | GrDrawIndirectWriter(const GrDrawIndirectWriter&) = delete; |
| 28 | GrDrawIndirectWriter(GrDrawIndirectWriter&& that) { *this = std::move(that); } |
| 29 | |
| 30 | GrDrawIndirectWriter& operator=(const GrDrawIndirectWriter&) = delete; |
| 31 | GrDrawIndirectWriter& operator=(GrDrawIndirectWriter&& that) { |
| 32 | fData = that.fData; |
| 33 | that.fData = nullptr; |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | bool isValid() const { return fData != nullptr; } |
| 38 | |
| 39 | inline void write(uint32_t instanceCount, uint32_t baseInstance, uint32_t vertexCount, |
| 40 | uint32_t baseVertex, const GrCaps&) { |
| 41 | *fData++ = {vertexCount, instanceCount, baseVertex, baseInstance}; |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | GrDrawIndirectCommand* fData; |
| 46 | }; |
| 47 | |
| 48 | // Helper for writing commands to an indexed indirect draw buffer. Usage: |
| 49 | // |
| 50 | // GrDrawIndexedIndirectWriter indirectWriter = target->makeDrawIndexedIndirectSpace(...); |
| 51 | // indirectWriter.writeIndexed(...); |
| 52 | // indirectWriter.writeIndexed(...); |
| 53 | struct GrDrawIndexedIndirectWriter { |
| 54 | public: |
| 55 | GrDrawIndexedIndirectWriter() = default; |
| 56 | GrDrawIndexedIndirectWriter(void* data) |
| 57 | : fData(static_cast<GrDrawIndexedIndirectCommand*>(data)) {} |
| 58 | GrDrawIndexedIndirectWriter(const GrDrawIndexedIndirectWriter&) = delete; |
| 59 | GrDrawIndexedIndirectWriter(GrDrawIndexedIndirectWriter&& that) { *this = std::move(that); } |
| 60 | |
| 61 | GrDrawIndexedIndirectWriter& operator=(const GrDrawIndexedIndirectWriter&) = delete; |
| 62 | GrDrawIndexedIndirectWriter& operator=(GrDrawIndexedIndirectWriter&& that) { |
| 63 | fData = that.fData; |
| 64 | that.fData = nullptr; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | bool isValid() const { return fData != nullptr; } |
| 69 | |
| 70 | inline void writeIndexed(uint32_t indexCount, uint32_t baseIndex, uint32_t instanceCount, |
| 71 | uint32_t baseInstance, uint32_t baseVertex, const GrCaps&) { |
| 72 | *fData++ = {indexCount, instanceCount, baseIndex, baseVertex, baseInstance}; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | GrDrawIndexedIndirectCommand* fData; |
| 77 | }; |
| 78 | |
| 79 | #endif |