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 | |
Chris Dalton | b41676d | 2021-02-11 12:31:53 -0700 | [diff] [blame] | 53 | bool operator==(const GrDrawIndirectWriter& that) { return fData == that.fData; } |
| 54 | |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 55 | bool isValid() const { return fData != nullptr; } |
| 56 | |
Chris Dalton | b41676d | 2021-02-11 12:31:53 -0700 | [diff] [blame] | 57 | GrDrawIndirectWriter makeOffset(int drawCount) const { return {fData + drawCount}; } |
| 58 | |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 59 | inline void write(uint32_t instanceCount, uint32_t baseInstance, uint32_t vertexCount, |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 60 | int32_t baseVertex) { |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 61 | *fData++ = {vertexCount, instanceCount, baseVertex, baseInstance}; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | GrDrawIndirectCommand* fData; |
| 66 | }; |
| 67 | |
| 68 | // Helper for writing commands to an indexed indirect draw buffer. Usage: |
| 69 | // |
| 70 | // GrDrawIndexedIndirectWriter indirectWriter = target->makeDrawIndexedIndirectSpace(...); |
| 71 | // indirectWriter.writeIndexed(...); |
| 72 | // indirectWriter.writeIndexed(...); |
| 73 | struct GrDrawIndexedIndirectWriter { |
| 74 | public: |
| 75 | GrDrawIndexedIndirectWriter() = default; |
| 76 | GrDrawIndexedIndirectWriter(void* data) |
| 77 | : fData(static_cast<GrDrawIndexedIndirectCommand*>(data)) {} |
| 78 | GrDrawIndexedIndirectWriter(const GrDrawIndexedIndirectWriter&) = delete; |
| 79 | GrDrawIndexedIndirectWriter(GrDrawIndexedIndirectWriter&& that) { *this = std::move(that); } |
| 80 | |
| 81 | GrDrawIndexedIndirectWriter& operator=(const GrDrawIndexedIndirectWriter&) = delete; |
| 82 | GrDrawIndexedIndirectWriter& operator=(GrDrawIndexedIndirectWriter&& that) { |
| 83 | fData = that.fData; |
| 84 | that.fData = nullptr; |
| 85 | return *this; |
| 86 | } |
| 87 | |
Chris Dalton | b41676d | 2021-02-11 12:31:53 -0700 | [diff] [blame] | 88 | bool operator==(const GrDrawIndexedIndirectWriter& that) { return fData == that.fData; } |
| 89 | |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame^] | 90 | operator bool() const { return fData != nullptr; } |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 91 | |
Chris Dalton | b41676d | 2021-02-11 12:31:53 -0700 | [diff] [blame] | 92 | GrDrawIndexedIndirectWriter makeOffset(int drawCount) const { return {fData + drawCount}; } |
| 93 | |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 94 | inline void writeIndexed(uint32_t indexCount, uint32_t baseIndex, uint32_t instanceCount, |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 95 | uint32_t baseInstance, int32_t baseVertex) { |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 96 | *fData++ = {indexCount, instanceCount, baseIndex, baseVertex, baseInstance}; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | GrDrawIndexedIndirectCommand* fData; |
| 101 | }; |
| 102 | |
| 103 | #endif |