Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * Encapsulation of backend buffers with batching and sharing. 20 : */ 21 : 22 : #ifndef INCLUDED_VERTEXBUFFER 23 : #define INCLUDED_VERTEXBUFFER 24 : 25 : #include "lib/code_annotation.h" 26 : #include "renderer/backend/IBuffer.h" 27 : 28 : #include <cstddef> 29 : #include <cstdint> 30 : #include <memory> 31 : #include <vector> 32 : 33 : namespace Renderer::Backend { class IDevice; } 34 : namespace Renderer::Backend { class IDeviceCommandContext; } 35 : 36 : /** 37 : * CVertexBuffer: encapsulation of backend buffers, also supplying 38 : * some additional functionality for sharing buffers between multiple objects. 39 : * 40 : * The class can be used in two modes, depending on the usage parameter: 41 : * 42 : * Static buffer: Call Allocate() with backingStore = nullptr. Then call 43 : * UpdateChunkVertices() with any pointer - the data will be immediately copied 44 : * to the buffer. This should be used for vertex data that rarely changes. 45 : * 46 : * Dynamic buffer: Call Allocate() with backingStore pointing 47 : * at some memory that will remain valid for the lifetime of the CVertexBuffer. 48 : * This should be used for vertex data that may change every frame. 49 : * Rendering is expected to occur in two phases: 50 : * - "Prepare" phase: 51 : * If this chunk is going to be used for rendering during the next rendering phase, 52 : * you must call PrepareForRendering(). 53 : * If the vertex data in backingStore has been modified since the last uploading phase, 54 : * you must call UpdateChunkVertices(). 55 : * - "Upload" phase: 56 : * UploadedIfNeeded() can be called (multiple times). The vertex data will be uploaded 57 : * to the GPU if necessary. 58 : * It is okay to have multiple prepare/upload cycles per frame (though slightly less 59 : * efficient), but they must occur sequentially. 60 : */ 61 : class CVertexBuffer 62 : { 63 : NONCOPYABLE(CVertexBuffer); 64 : 65 : public: 66 : 67 : // VBChunk: describes a portion of this vertex buffer 68 : struct VBChunk 69 : { 70 : // Owning (parent) vertex buffer 71 : CVertexBuffer* m_Owner; 72 : // Start index of this chunk in owner 73 : size_t m_Index; 74 : // Number of vertices used by chunk 75 : size_t m_Count; 76 : // If UseStreaming() is true, points at the data for this chunk 77 : void* m_BackingStore; 78 : 79 : // If true, the backend buffer is not consistent with the chunk's 80 : // backing store (and will need to be re-uploaded before rendering with 81 : // this chunk). 82 : bool m_Dirty; 83 : 84 : // If true, we have been told this chunk is going to be used for 85 : // rendering in the next uploading phase and will need to be uploaded 86 : bool m_Needed; 87 : 88 : private: 89 : // Only CVertexBuffer can construct/delete these 90 : // (Other people should use g_Renderer.GetVertexBufferManager().AllocateChunk) 91 : friend class CVertexBuffer; 92 20 : VBChunk() {} 93 20 : ~VBChunk() {} 94 : }; 95 : 96 : public: 97 : CVertexBuffer( 98 : Renderer::Backend::IDevice* device, 99 : const char* name, const size_t vertexSize, 100 : const Renderer::Backend::IBuffer::Type type, const uint32_t usage); 101 : CVertexBuffer( 102 : Renderer::Backend::IDevice* device, 103 : const char* name, const size_t vertexSize, 104 : const Renderer::Backend::IBuffer::Type type, const uint32_t usage, 105 : const size_t maximumBufferSize); 106 : ~CVertexBuffer(); 107 : 108 : void UploadIfNeeded(Renderer::Backend::IDeviceCommandContext* deviceCommandContext); 109 : 110 : /// Make the vertex data available for the next usage. 111 : void PrepareForRendering(VBChunk* chunk); 112 : 113 : /// Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer. 114 : void UpdateChunkVertices(VBChunk* chunk, void* data); 115 : 116 : size_t GetVertexSize() const { return m_VertexSize; } 117 : size_t GetBytesReserved() const; 118 : size_t GetBytesAllocated() const; 119 : 120 : /// Returns true if this vertex buffer is compatible with the specified vertex type and intended usage. 121 : bool CompatibleVertexType( 122 : const size_t vertexSize, const Renderer::Backend::IBuffer::Type type, 123 : const uint32_t usage) const; 124 : 125 : void DumpStatus() const; 126 : 127 : /** 128 : * Given the usage flags of a buffer that has been (or will be) allocated: 129 : * 130 : * If true, we assume the buffer is going to be modified on every frame, 131 : * so we will re-upload the entire buffer every frame using glMapBuffer. 132 : * This requires the buffer's owner to hold onto its backing store. 133 : * 134 : * If false, we assume it will change rarely, and use direct upload to 135 : * update it incrementally. The backing store can be freed to save memory. 136 : */ 137 : static bool UseStreaming(const uint32_t usage); 138 : 139 0 : Renderer::Backend::IBuffer* GetBuffer() { return m_Buffer.get(); } 140 : 141 : private: 142 : friend class CVertexBufferManager; // allow allocate only via CVertexBufferManager 143 : 144 : /// Try to allocate a buffer of given number of vertices (each of given size), 145 : /// and with the given type - return null if no free chunks available 146 : VBChunk* Allocate( 147 : const size_t vertexSize, const size_t numberOfVertices, 148 : const Renderer::Backend::IBuffer::Type type, const uint32_t usage, 149 : void* backingStore); 150 : /// Return given chunk to this buffer 151 : void Release(VBChunk* chunk); 152 : 153 : /// Vertex size of this vertex buffer 154 : size_t m_VertexSize; 155 : /// Number of vertices of above size in this buffer 156 : size_t m_MaxVertices; 157 : /// List of free chunks in this buffer 158 : std::vector<VBChunk*> m_FreeList; 159 : /// List of allocated chunks 160 : std::vector<VBChunk*> m_AllocList; 161 : /// Available free vertices - total of all free vertices in the free list 162 : size_t m_FreeVertices; 163 : 164 : std::unique_ptr<Renderer::Backend::IBuffer> m_Buffer; 165 : 166 : bool m_HasNeededChunks; 167 : }; 168 : 169 : #endif // INCLUDED_VERTEXBUFFER