SleakEngine 0.1.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 {
15
16/// D3D12 2D texture: default-heap resource with an upload-heap staging path and shared-SRV-heap support.
17class ENGINE_API DirectX12Texture : public Texture {
18public:
19 /// Uploads go through the shared uploader when given, else they block.
20 DirectX12Texture(ID3D12Device* device, ID3D12CommandQueue* commandQueue,
21 ID3D12GraphicsCommandList* commandList = nullptr,
22 DirectX12UploadContext* uploader = nullptr);
23 ~DirectX12Texture() override;
24
25 /// Uploads raw pixel data as a new D3D12 texture resource.
26 bool LoadFromMemory(const void* data, uint32_t width, uint32_t height,
27 TextureFormat format) override;
28 /// Decodes an image file and uploads it as a new D3D12 texture resource.
29 bool LoadFromFile(const std::string& filePath) override;
30
31 void Bind(uint32_t slot = 0) const override;
32 void Unbind() const override;
33
34 void SetFilter(TextureFilter filter) override;
35 void SetWrapMode(TextureWrapMode wrapMode) override;
36
37 uint32_t GetWidth() const override { return m_width; }
38 uint32_t GetHeight() const override { return m_height; }
39 TextureFormat GetFormat() const override { return m_format; }
40 TextureType GetType() const override { return TextureType::Texture2D; }
41
42 uint64_t GetImGuiTextureID() const override { return m_srvGpuHandle.ptr; }
43
44 ID3D12Resource* GetResource() const { return m_texture.Get(); }
45
46 // Bind SRV table to a command list for rendering (heap already set)
47 /// Binds this texture's SRV table at the given root parameter for a draw.
48 void BindToCommandList(ID3D12GraphicsCommandList* cmdList,
49 UINT rootParameterIndex) const;
50
51 // Set shared SRV heap slot (called by renderer during creation)
52 void SetSharedSrvGPUHandle(D3D12_GPU_DESCRIPTOR_HANDLE handle) {
53 m_srvGpuHandle = handle;
54 m_usesSharedHeap = true;
55 }
56
57 // Create SRV directly into an externally-provided CPU handle (shared heap)
58 bool CreateSRVIntoHandle(DXGI_FORMAT format, D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle);
59
60private:
61 /// Allocates the default-heap 2D texture resource at the given size/format.
62 bool CreateTextureResource(uint32_t width, uint32_t height,
63 DXGI_FORMAT format);
64 /// Stages the pixels plus a box filtered mip chain and copies them in.
65 bool UploadTextureData(const void* data, uint32_t width,
66 uint32_t height);
67 /// Creates the shader resource view into either the shared heap or a per-texture fallback heap.
68 bool CreateSRV(DXGI_FORMAT format);
69 /// Maps the engine's TextureFormat to the matching DXGI_FORMAT.
70 DXGI_FORMAT GetDXGIFormat(TextureFormat format) const;
71
72 ID3D12Device* m_device = nullptr;
73 ID3D12CommandQueue* m_commandQueue = nullptr;
74 ID3D12GraphicsCommandList* m_commandList = nullptr;
75 DirectX12UploadContext* m_uploader = nullptr;
76
77 Microsoft::WRL::ComPtr<ID3D12Resource> m_texture;
78 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_srvHeap; // fallback per-texture heap
79 D3D12_GPU_DESCRIPTOR_HANDLE m_srvGpuHandle = {};
80 bool m_usesSharedHeap = false;
81
82 uint32_t m_width = 0;
83 uint32_t m_height = 0;
84 uint32_t m_mipLevels = 1;
85 TextureFormat m_format = TextureFormat::RGBA8;
86 TextureFilter m_filter = TextureFilter::Linear;
87 TextureWrapMode m_wrapMode = TextureWrapMode::Repeat;
88};
89
90} // namespace RenderEngine
91} // namespace Sleak
92
93#endif // PLATFORM_WIN
94
95#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