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 <stdint.h> 12 #include <utility> 13 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"); 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; GrDrawIndirectWriterGrDrawIndirectWriter42 GrDrawIndirectWriter(void* data) : fData(static_cast<GrDrawIndirectCommand*>(data)) {} 43 GrDrawIndirectWriter(const GrDrawIndirectWriter&) = delete; GrDrawIndirectWriterGrDrawIndirectWriter44 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 operator==(const GrDrawIndirectWriter& that) { return fData == that.fData; } 54 isValidGrDrawIndirectWriter55 bool isValid() const { return fData != nullptr; } 56 makeOffsetGrDrawIndirectWriter57 GrDrawIndirectWriter makeOffset(int drawCount) const { return {fData + drawCount}; } 58 writeGrDrawIndirectWriter59 inline void write(uint32_t instanceCount, uint32_t baseInstance, uint32_t vertexCount, 60 int32_t baseVertex) { 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; GrDrawIndexedIndirectWriterGrDrawIndexedIndirectWriter76 GrDrawIndexedIndirectWriter(void* data) 77 : fData(static_cast<GrDrawIndexedIndirectCommand*>(data)) {} 78 GrDrawIndexedIndirectWriter(const GrDrawIndexedIndirectWriter&) = delete; GrDrawIndexedIndirectWriterGrDrawIndexedIndirectWriter79 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 88 bool operator==(const GrDrawIndexedIndirectWriter& that) { return fData == that.fData; } 89 90 operator bool() const { return fData != nullptr; } 91 makeOffsetGrDrawIndexedIndirectWriter92 GrDrawIndexedIndirectWriter makeOffset(int drawCount) const { return {fData + drawCount}; } 93 writeIndexedGrDrawIndexedIndirectWriter94 inline void writeIndexed(uint32_t indexCount, uint32_t baseIndex, uint32_t instanceCount, 95 uint32_t baseInstance, int32_t baseVertex) { 96 *fData++ = {indexCount, instanceCount, baseIndex, baseVertex, baseInstance}; 97 } 98 99 private: 100 GrDrawIndexedIndirectCommand* fData; 101 }; 102 103 #endif 104