5#ifndef _DIRECTX12RENDERER_H
6#define _DIRECTX12RENDERER_H
16#include <d3dcompiler.h>
18#include <wrl/client.h>
20#include <backends/imgui_impl_dx12.h>
28 DirectX12Renderer(Window* window);
29 virtual ~DirectX12Renderer();
31 bool Initialize()
override;
32 void BeginRender()
override;
33 void EndRender()
override;
34 void Cleanup()
override;
35 void WaitIdle()
override { WaitForGPU(); }
37 virtual void Resize(uint32_t
width, uint32_t
height)
override;
40 static bool IsSupport();
42 virtual bool CreateImGUI()
override;
44 virtual RenderContext* GetContext()
override {
return this; }
47 virtual uint32_t GetFeatureCaps()
const override {
return CapShadows; }
50 virtual void Draw(uint32_t vertexCount)
override;
51 virtual void DrawIndexed(uint32_t indexCount)
override;
52 virtual void DrawInstance(uint32_t instanceCount,
53 uint32_t vertexPerInstance)
override;
54 virtual void DrawIndexedInstance(uint32_t instanceCount,
55 uint32_t indexPerInstance)
override;
57 virtual void SetRenderFace(RenderFace face)
override;
58 virtual void SetRenderMode(RenderMode mode)
override;
60 float minDepth = 0.0f,
61 float maxDepth = 1.0f)
override;
64 virtual void ClearDepthStencil(
bool clearDepth,
bool clearStencil,
65 float depth, uint8_t stencil)
override;
67 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
68 uint32_t slot = 0)
override;
69 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
70 uint32_t slot = 0)
override;
72 uint32_t slot = 0)
override;
74 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size,
76 virtual Shader* CreateShader(
const std::string& shaderSource)
override;
77 virtual Texture* CreateTexture(
const std::string& TexturePath)
override;
82 Texture* CreateCubemapTexture(
const std::array<std::string, 6>& facePaths);
84 Texture* CreateCubemapTextureFromPanorama(
const std::string& panoramaPath);
87 void UpdateShadowLightUBO(
const void* data, uint32_t size)
override;
90 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0)
override;
91 virtual void BindTextureRaw(Sleak::Texture* texture, uint32_t slot = 0)
override;
92 virtual void BeginSkyboxPass()
override;
93 virtual void EndSkyboxPass()
override;
94 virtual void BeginDebugLinePass()
override;
95 virtual void EndDebugLinePass()
override;
100 bool CreateCommandQueue();
102 bool CreateSwapChain();
104 bool CreateCommandAllocatorAndList();
106 bool CreateRenderTargetViews();
107 bool CreateDepthStencilView();
109 bool CreateRootSignature();
111 bool CreatePipelineState();
113 bool CreatePipelineStateFromShader(ID3DBlob* vertexShaderBlob,
114 ID3DBlob* pixelShaderBlob);
116 virtual void ConfigureRenderMode()
override;
117 virtual void ConfigureRenderFace()
override;
122 void EnumerateDevices(Microsoft::WRL::ComPtr<IDXGIFactory4> factory);
125 static constexpr UINT FrameCount = 2;
128 Microsoft::WRL::ComPtr<ID3D12Device> device;
129 Microsoft::WRL::ComPtr<IDXGISwapChain3> swapChain;
130 Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
133 Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue;
134 Microsoft::WRL::ComPtr<ID3D12CommandAllocator> commandAllocators[FrameCount];
135 Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> commandList;
138 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> rtvHeap;
139 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> dsvHeap;
142 Microsoft::WRL::ComPtr<ID3D12Resource> renderTargets[FrameCount];
143 Microsoft::WRL::ComPtr<ID3D12Resource> depthStencilBuffer;
146 Microsoft::WRL::ComPtr<ID3D12RootSignature> rootSignature;
147 Microsoft::WRL::ComPtr<ID3D12PipelineState> pipelineState;
150 Microsoft::WRL::ComPtr<ID3D12Fence> fence;
151 HANDLE fenceEvent =
nullptr;
152 UINT64 fenceValues[FrameCount] = {};
155 UINT rtvDescriptorSize = 0;
162 D3D12_VIEWPORT viewport = {};
163 D3D12_RECT scissorRect = {};
165 bool m_Initialized =
false;
168 DirectX12Texture* m_defaultTexture =
nullptr;
171 Microsoft::WRL::ComPtr<ID3D12PipelineState> m_skyboxPipelineState;
173 bool CreateSkyboxPipelineState();
176 Microsoft::WRL::ComPtr<ID3D12PipelineState> m_debugLinePipelineState;
178 bool CreateDebugLinePipelineState();
181 Microsoft::WRL::ComPtr<ID3D12Resource> m_lightUBO;
182 void* m_lightUBOMapped =
nullptr;
183 bool m_lightUBOCreated =
false;
185 bool CreateLightUBO();
188 Microsoft::WRL::ComPtr<ID3D12Resource> m_shadowDepthBuffer;
189 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_shadowDsvHeap;
190 D3D12_CPU_DESCRIPTOR_HANDLE m_shadowDsvHandle = {};
191 UINT m_shadowSrvIndex = 0;
192 bool m_shadowMapCreated =
false;
193 float m_lightVP[16] = {};
194 float m_pendingLightVP[16] = {};
195 bool m_hasPendingLightVP =
false;
196 bool m_inShadowPass =
false;
197 Microsoft::WRL::ComPtr<ID3D12Resource> m_shadowTransformCB;
198 void* m_shadowTransformMapped =
nullptr;
200 Microsoft::WRL::ComPtr<ID3D12PipelineState> m_shadowPassPSO;
201 Microsoft::WRL::ComPtr<ID3DBlob> m_cachedVSBlob;
202 void SetLightVP(
const float* mat)
override;
204 bool CreateShadowMapResources();
206 bool CreateShadowPassPSO();
208 void RenderShadowPass();
211 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> imguiSrvHeap;
214 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_sharedSrvHeap;
215 UINT m_srvDescriptorSize = 0;
216 UINT m_nextSrvSlot = 1;
217 static constexpr UINT MAX_SRV_DESCRIPTORS = 512;
220 bool CreateSharedSrvHeap();
223 UINT AllocateSRVSlot();
224 D3D12_CPU_DESCRIPTOR_HANDLE GetSharedSrvCPUHandle(UINT slot)
const;
225 D3D12_GPU_DESCRIPTOR_HANDLE GetSharedSrvGPUHandle(UINT slot)
const;
226 ID3D12DescriptorHeap* GetSharedSrvHeap()
const {
return m_sharedSrvHeap.Get(); }
236class DirectX12Renderer;
Abstract interface for high-level graphics command execution and resource management.
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.