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 | |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <utility> |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 13 | |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 14 | struct GrDrawIndirectCommand { |
| 15 | uint32_t fVertexCount; |
| 16 | uint32_t fInstanceCount; |
| 17 | int32_t fBaseVertex; |
| 18 | uint32_t fBaseInstance; |
| 19 | }; |
| 20 | |
| 21 | static_assert(sizeof(GrDrawIndirectCommand) == 16, "GrDrawIndirectCommand must be tightly packed"); |
| 22 | |
| 23 | struct GrDrawIndexedIndirectCommand { |
| 24 | uint32_t fIndexCount; |
| 25 | uint32_t fInstanceCount; |
| 26 | uint32_t fBaseIndex; |
| 27 | int32_t fBaseVertex; |
| 28 | uint32_t fBaseInstance; |
| 29 | }; |
| 30 | |
| 31 | static_assert(sizeof(GrDrawIndexedIndirectCommand) == 20, |
| 32 | "GrDrawIndexedIndirectCommand must be tightly packed"); |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 33 | |
| 34 | // Helper for writing commands to an indirect draw buffer. Usage: |
| 35 | // |
| 36 | // GrDrawIndirectWriter indirectWriter = target->makeDrawIndirectSpace(...); |
| 37 | // indirectWriter.write(...); |
| 38 | // indirectWriter.write(...); |
| 39 | struct GrDrawIndirectWriter { |
| 40 | public: |
| 41 | GrDrawIndirectWriter() = default; |
| 42 | GrDrawIndirectWriter(void* data) : fData(static_cast<GrDrawIndirectCommand*>(data)) {} |
| 43 | GrDrawIndirectWriter(const GrDrawIndirectWriter&) = delete; |
| 44 | GrDrawIndirectWriter(GrDrawIndirectWriter&& that) { *this = std::move(that); } |
| 45 | |
| 46 | GrDrawIndirectWriter& operator=(const GrDrawIndirectWriter&) = delete; |
| 47 | GrDrawIndirectWriter& operator=(GrDrawIndirectWriter&& that) { |
| 48 | fData = that.fData; |
| 49 | that.fData = nullptr; |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | bool isValid() const { return fData != nullptr; } |
| 54 | |
| 55 | inline void write(uint32_t instanceCount, uint32_t baseInstance, uint32_t vertexCount, |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 56 | int32_t baseVertex) { |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 57 | *fData++ = {vertexCount, instanceCount, baseVertex, baseInstance}; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | GrDrawIndirectCommand* fData; |
| 62 | }; |
| 63 | |
| 64 | // Helper for writing commands to an indexed indirect draw buffer. Usage: |
| 65 | // |
| 66 | // GrDrawIndexedIndirectWriter indirectWriter = target->makeDrawIndexedIndirectSpace(...); |
| 67 | // indirectWriter.writeIndexed(...); |
| 68 | // indirectWriter.writeIndexed(...); |
| 69 | struct GrDrawIndexedIndirectWriter { |
| 70 | public: |
| 71 | GrDrawIndexedIndirectWriter() = default; |
| 72 | GrDrawIndexedIndirectWriter(void* data) |
| 73 | : fData(static_cast<GrDrawIndexedIndirectCommand*>(data)) {} |
| 74 | GrDrawIndexedIndirectWriter(const GrDrawIndexedIndirectWriter&) = delete; |
| 75 | GrDrawIndexedIndirectWriter(GrDrawIndexedIndirectWriter&& that) { *this = std::move(that); } |
| 76 | |
| 77 | GrDrawIndexedIndirectWriter& operator=(const GrDrawIndexedIndirectWriter&) = delete; |
| 78 | GrDrawIndexedIndirectWriter& operator=(GrDrawIndexedIndirectWriter&& that) { |
| 79 | fData = that.fData; |
| 80 | that.fData = nullptr; |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | bool isValid() const { return fData != nullptr; } |
| 85 | |
| 86 | inline void writeIndexed(uint32_t indexCount, uint32_t baseIndex, uint32_t instanceCount, |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 87 | uint32_t baseInstance, int32_t baseVertex) { |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 88 | *fData++ = {indexCount, instanceCount, baseIndex, baseVertex, baseInstance}; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | GrDrawIndexedIndirectCommand* fData; |
| 93 | }; |
| 94 | |
| 95 | #endif |