SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX12Texture.hpp
Go to the documentation of this file.
1#ifndef DIRECTX12TEXTURE_HPP_
2#define DIRECTX12TEXTURE_HPP_
3
4#include <Core/OSDef.hpp>
5
6#ifdef PLATFORM_WIN
7
8#include <Runtime/Texture.hpp>
9#include <d3d12.h>
10#include <wrl/client.h>
11
12namespace Sleak {
13namespace RenderEngine {
14
15/// D3D12 2D texture: default-heap resource with an upload-heap staging path and shared-SRV-heap support.
16class ENGINE_API DirectX12Texture : public Texture {
17public:
18 DirectX12Texture(ID3D12Device* device,
19 ID3D12CommandQueue* commandQueue,
20 ID3D12GraphicsCommandList* commandList = nullptr);
21 ~DirectX12Texture() override;
22
23 /// Uploads raw pixel data as a new D3D12 texture resource.
24 bool LoadFromMemory(const void* data, uint32_t width, uint32_t height,
25 TextureFormat format) override;
26 /// Decodes an image file and uploads it as a new D3D12 texture resource.
27 bool LoadFromFile(const std::string& filePath) override;
28
29 void Bind(uint32_t slot = 0) const override;
30 void Unbind() const override;
31
32 void SetFilter(TextureFilter filter) override;
33 void SetWrapMode(TextureWrapMode wrapMode) override;
34
35 uint32_t GetWidth() const override { return m_width; }
36 uint32_t GetHeight() const override { return m_height; }
37 TextureFormat GetFormat() const override { return m_format; }
38 TextureType GetType() const override { return TextureType::Texture2D; }
39
40 uint64_t GetImGuiTextureID() const override { return m_srvGpuHandle.ptr; }
41
42 ID3D12Resource* GetResource() const { return m_texture.Get(); }
43
44 // Bind SRV table to a command list for rendering (heap already set)
45 /// Binds this texture's SRV table at the given root parameter for a draw.
46 void BindToCommandList(ID3D12GraphicsCommandList* cmdList,
47 UINT rootParameterIndex) const;
48
49 // Set shared SRV heap slot (called by renderer during creation)
50 void SetSharedSrvGPUHandle(D3D12_GPU_DESCRIPTOR_HANDLE handle) { m_srvGpuHandle = handle; m_usesSharedHeap = true; }
51
52 // Create SRV directly into an externally-provided CPU handle (shared heap)
53 bool CreateSRVIntoHandle(DXGI_FORMAT format, D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle);
54
55private:
56 /// Allocates the default-heap 2D texture resource at the given size/format.
57 bool CreateTextureResource(uint32_t width, uint32_t height,
58 DXGI_FORMAT format);
59 /// Stages pixel data through the upload heap and copies it into the GPU resource.
60 bool UploadTextureData(const void* data, uint32_t width,
61 uint32_t height);
62 /// Creates the shader resource view into either the shared heap or a per-texture fallback heap.
63 bool CreateSRV(DXGI_FORMAT format);
64 /// Maps the engine's TextureFormat to the matching DXGI_FORMAT.
65 DXGI_FORMAT GetDXGIFormat(TextureFormat format) const;
66 /// Blocks until the upload-heap copy to the GPU-resident texture completes.
67 void WaitForUpload();
68
69 ID3D12Device* m_device = nullptr;
70 ID3D12CommandQueue* m_commandQueue = nullptr;
71 ID3D12GraphicsCommandList* m_commandList = nullptr;
72
73 Microsoft::WRL::ComPtr<ID3D12Resource> m_texture;
74 Microsoft::WRL::ComPtr<ID3D12Resource> m_uploadBuffer;
75 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_srvHeap; // fallback per-texture heap
76 D3D12_GPU_DESCRIPTOR_HANDLE m_srvGpuHandle = {};
77 bool m_usesSharedHeap = false;
78
79 uint32_t m_width = 0;
80 uint32_t m_height = 0;
81 TextureFormat m_format = TextureFormat::RGBA8;
82 TextureFilter m_filter = TextureFilter::Linear;
83 TextureWrapMode m_wrapMode = TextureWrapMode::Repeat;
84};
85
86} // namespace RenderEngine
87} // namespace Sleak
88
89#endif // PLATFORM_WIN
90
91#endif // DIRECTX12TEXTURE_HPP_
int width
int height
TextureFormat
Definition Texture.hpp:10
TextureFilter
Definition Texture.hpp:28
TextureType
Definition Texture.hpp:20
TextureWrapMode
Definition Texture.hpp:43
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10