SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX12Buffer.hpp
Go to the documentation of this file.
1#ifndef DIRECTX12BUFFER_HPP_
2#define DIRECTX12BUFFER_HPP_
4#include <d3d12.h>
5#include <wrl/client.h>
6#include <memory>
7
8namespace Sleak {
9namespace RenderEngine {
10/// D3D12 buffer wrapper: default-heap resource plus an upload heap for CPU-to-GPU copies.
12public:
13 // Constructor for common buffer types
14 DirectX12Buffer(ID3D12Device* device, ID3D12CommandQueue* queue, size_t size, BufferType type);
15
16 // Constructor for custom buffer configuration
17 DirectX12Buffer(ID3D12Device* device, size_t size, D3D12_HEAP_TYPE heapType,
18 D3D12_RESOURCE_STATES resourceState);
19
20 // Prevent copying
23
24 // Allow moving
26 DirectX12Buffer& operator=(DirectX12Buffer&&) noexcept;
27
28 ~DirectX12Buffer() override;
29 /// Creates the default-heap resource and uploads initial data via a staging buffer, if any.
30 bool Initialize(void* Data) override;
31 void Update() override;
32 void Cleanup() override;
33
34 /// Maps the buffer for CPU writes (upload-heap buffers only).
35 bool Map() override;
36 void Unmap() override;
37 /// Overwrites buffer contents via a fresh upload-heap copy.
38 void Update(void* data, size_t size) override;
39 /// Creates the buffer sized and pre-populated from a raw payload.
40 bool Initialize(const void* data, size_t size);
41
42 void* GetData() override;
43
44 // Getter for the underlying D3D buffer
45 ID3D12Resource* GetD3DBuffer() const { return m_buffer.Get(); }
46
47 // Get command list for execution
48 ID3D12GraphicsCommandList* GetCommandList() const { return m_commandList.Get(); }
49
50 // Check if command list is ready for execution
51 bool HasPendingCommands() const { return m_commandList != nullptr; }
52
53 // Additional utility methods
54 bool IsValid() const { return m_buffer != nullptr; }
55 D3D12_HEAP_TYPE GetHeapType() const { return m_heapType; }
56 D3D12_RESOURCE_STATES GetResourceState() const { return m_resourceState; }
57
58private:
59 // Helper method to set up buffer description
60 D3D12_RESOURCE_DESC CreateBufferDesc() const;
61
62 // Convert BufferType to DirectX configuration
63 void ConfigureFromBufferType(BufferType type);
64
65 // Create an upload buffer and copy data to the default buffer
66 bool CreateUploadBuffer(const void* data, size_t dataSize);
67
68 void SetAsVertexBuffer (ID3D12GraphicsCommandList* commandList, UINT slot, UINT stride, UINT offset = 0);
69 void SetAsIndexBuffer (ID3D12GraphicsCommandList* commandList, DXGI_FORMAT format, UINT offset = 0);
70 void SetAsConstantBuffer(ID3D12GraphicsCommandList* commandList, UINT rootParameterIndex);
71
72 // Wait for pending GPU upload to complete (for command list/allocator reuse)
73 void WaitForUploadComplete();
74
75public:
76 // Release staging upload resources (call after GPU copy is guaranteed complete)
78
79 // Deferred cleanup: queue buffers for deletion after GPU is done
80 static void DeferCleanup(Microsoft::WRL::ComPtr<ID3D12Resource> resource);
81 static void ProcessDeferredCleanup();
82
83private:
84
85 // Smart pointers for proper resource management
86 Microsoft::WRL::ComPtr<ID3D12Device> m_device;
87 ID3D12CommandQueue* m_commandQueue = nullptr; // non-owning, for upload execution
88 Microsoft::WRL::ComPtr<ID3D12Resource> m_buffer;
89 Microsoft::WRL::ComPtr<ID3D12Resource> m_uploadBuffer;
90 Microsoft::WRL::ComPtr<ID3D12CommandAllocator> m_commandAllocator;
91 Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> m_commandList;
92
93 D3D12_HEAP_TYPE m_heapType = D3D12_HEAP_TYPE_DEFAULT;
94 D3D12_RESOURCE_STATES m_resourceState = D3D12_RESOURCE_STATE_COMMON;
95 // Actual current GPU resource state (tracked for correct barriers on re-upload)
96 D3D12_RESOURCE_STATES m_currentState = D3D12_RESOURCE_STATE_COMMON;
97 void* m_mappedData = nullptr;
98};
99} // namespace RenderEngine
100} // namespace Sleak
101#endif // DIRECTX12BUFFER_HPP_
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
ID3D12GraphicsCommandList * GetCommandList() const
bool Initialize(void *Data) override
Creates the default-heap resource and uploads initial data via a staging buffer, if any.
DirectX12Buffer(ID3D12Device *device, ID3D12CommandQueue *queue, size_t size, BufferType type)
D3D12_RESOURCE_STATES GetResourceState() const
static void DeferCleanup(Microsoft::WRL::ComPtr< ID3D12Resource > resource)
bool Map() override
Maps the buffer for CPU writes (upload-heap buffers only).
DirectX12Buffer & operator=(const DirectX12Buffer &)=delete
ID3D12Resource * GetD3DBuffer() const
DirectX12Buffer(const DirectX12Buffer &)=delete
Backend-facing rendering layer shared by the four graphics backends.
BufferType
GPU buffer usage kind, drives backend binding flags and layout.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10