5#ifndef _DIRECTX12RENDERER_H
6#define _DIRECTX12RENDERER_H
8#include <backends/imgui_impl_dx12.h>
10#include <d3dcompiler.h>
13#include <wrl/client.h>
35 DirectX12Renderer(Window* window);
36 virtual ~DirectX12Renderer();
38 bool Initialize()
override;
39 void BeginRender()
override;
40 void EndRender()
override;
41 void Cleanup()
override;
42 void WaitIdle()
override { WaitForGPU(); }
44 virtual void Resize(uint32_t
width, uint32_t
height)
override;
47 static bool IsSupport();
49 virtual bool CreateImGUI()
override;
51 virtual RenderContext* GetContext()
override {
return this; }
54 virtual uint32_t GetFeatureCaps()
const override {
return CapShadows; }
57 virtual void Draw(uint32_t vertexCount)
override;
58 virtual void Draw(uint32_t vertexCount, uint32_t firstVertex)
override;
59 virtual void DrawIndexed(uint32_t indexCount)
override;
60 virtual void DrawIndexed(uint32_t indexCount, uint32_t firstIndex,
61 int32_t baseVertex)
override;
62 virtual void DrawInstance(uint32_t instanceCount,
63 uint32_t vertexPerInstance)
override;
64 virtual void DrawIndexedInstance(uint32_t instanceCount,
65 uint32_t indexPerInstance)
override;
66 virtual void DrawIndexedInstance(uint32_t instanceCount,
67 uint32_t indexPerInstance,
69 int32_t baseVertex)
override;
71 virtual void SetRenderFace(RenderFace face)
override;
72 virtual void SetRenderMode(RenderMode mode)
override;
74 float minDepth = 0.0f,
75 float maxDepth = 1.0f)
override;
78 virtual void ClearDepthStencil(
bool clearDepth,
bool clearStencil,
79 float depth, uint8_t stencil)
override;
81 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
82 uint32_t slot = 0)
override;
83 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
84 uint32_t slot = 0)
override;
86 uint32_t slot = 0)
override;
88 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size,
90 virtual Shader* CreateShader(
const std::string& shaderSource)
override;
91 virtual Texture* CreateTexture(
const std::string& TexturePath)
override;
96 Texture* CreateCubemapTexture(
const std::array<std::string, 6>& facePaths);
98 Texture* CreateCubemapTextureFromPanorama(
const std::string& panoramaPath);
101 void UpdateShadowLightUBO(
const void* data, uint32_t size)
override;
104 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0)
override;
105 virtual void BindTextureRaw(Sleak::Texture* texture, uint32_t slot = 0)
override;
106 virtual void BeginSkyboxPass()
override;
107 virtual void EndSkyboxPass()
override;
108 virtual void BeginDebugLinePass()
override;
109 virtual void EndDebugLinePass()
override;
112 void BindShaderPipeline(ID3D12PipelineState* pso);
119 uint32_t vertexFormat = 0;
120 D3D12_CULL_MODE cullMode = D3D12_CULL_MODE_FRONT;
121 BOOL depthClip = TRUE;
123 float depthBiasClamp = 0.0f;
124 float slopeScaledDepthBias = 0.0f;
125 D3D12_DEPTH_WRITE_MASK depthWrite = D3D12_DEPTH_WRITE_MASK_ALL;
126 D3D12_COMPARISON_FUNC depthFunc = D3D12_COMPARISON_FUNC_LESS;
127 BOOL blendEnable = FALSE;
128 D3D12_PRIMITIVE_TOPOLOGY_TYPE topology =
129 D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
130 DXGI_FORMAT rtvFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
131 DXGI_FORMAT dsvFormat = DXGI_FORMAT_D32_FLOAT;
133 bool operator==(
const PipelineKey&)
const =
default;
137 ID3D12PipelineState* GetPipeline(
const PipelineKey& key, ID3DBlob* vs,
140 void RestoreShaderPipeline();
142 void BindLightConstants();
146 bool CreateCommandQueue();
148 bool CreateSwapChain();
150 bool CreateCommandAllocatorAndList();
152 bool CreateRenderTargetViews();
153 bool CreateDepthStencilView();
155 bool CreateRootSignature();
157 virtual void ConfigureRenderMode()
override;
158 virtual void ConfigureRenderFace()
override;
163 void EnumerateDevices(Microsoft::WRL::ComPtr<IDXGIFactory4> factory);
166 static constexpr UINT FrameCount = 2;
169 Microsoft::WRL::ComPtr<ID3D12Device> device;
170 Microsoft::WRL::ComPtr<IDXGISwapChain3> swapChain;
171 Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
174 Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue;
175 Microsoft::WRL::ComPtr<ID3D12CommandAllocator> commandAllocators[FrameCount];
176 Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> commandList;
179 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> rtvHeap;
180 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> dsvHeap;
183 Microsoft::WRL::ComPtr<ID3D12Resource> renderTargets[FrameCount];
184 Microsoft::WRL::ComPtr<ID3D12Resource> depthStencilBuffer;
187 Microsoft::WRL::ComPtr<ID3D12RootSignature> rootSignature;
189 std::pair<PipelineKey, Microsoft::WRL::ComPtr<ID3D12PipelineState>>>
191 ID3D12PipelineState* m_defaultPipelineState =
nullptr;
192 ID3D12PipelineState* m_shaderPipeline =
nullptr;
193 ID3D12PipelineState* m_passPipeline =
nullptr;
197 Microsoft::WRL::ComPtr<ID3D12Fence> fence;
198 HANDLE fenceEvent =
nullptr;
199 UINT64 m_fenceValue = 0;
200 UINT64 fenceValues[FrameCount] = {};
201 uint64_t m_frameSerial = 0;
202 bool m_frameActive =
false;
205 UINT rtvDescriptorSize = 0;
212 D3D12_VIEWPORT viewport = {};
213 D3D12_RECT scissorRect = {};
215 bool m_Initialized =
false;
218 DirectX12Texture* m_defaultTexture =
nullptr;
221 ID3D12PipelineState* m_skyboxPipelineState =
nullptr;
222 bool m_skyboxPipelineFailed =
false;
224 bool CreateSkyboxPipelineState();
227 ID3D12PipelineState* m_debugLinePipelineState =
nullptr;
228 bool m_debugLinePipelineFailed =
false;
230 bool CreateDebugLinePipelineState();
233 DirectX12UploadRing m_uploadRing;
235 DirectX12UploadContext m_uploader;
238 std::vector<uint8_t> m_lightData;
241 Microsoft::WRL::ComPtr<ID3D12Resource> m_shadowDepthBuffer;
242 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_shadowDsvHeap;
243 D3D12_CPU_DESCRIPTOR_HANDLE m_shadowDsvHandle = {};
244 UINT m_shadowSrvIndex = 0;
245 bool m_shadowMapCreated =
false;
246 float m_lightVP[16] = {};
247 float m_pendingLightVP[16] = {};
248 bool m_hasPendingLightVP =
false;
249 bool m_inShadowPass =
false;
251 ID3D12PipelineState* m_shadowPassPSO =
nullptr;
252 Microsoft::WRL::ComPtr<ID3DBlob> m_shadowVSBlob;
253 bool m_shadowVSCompiled =
false;
255 Microsoft::WRL::ComPtr<ID3DBlob> m_cachedVSBlob;
256 void SetLightVP(
const float* mat)
override;
258 bool CreateShadowMapResources();
260 bool CreateShadowPassPSO();
262 void RenderShadowPass();
265 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> imguiSrvHeap;
268 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_sharedSrvHeap;
269 UINT m_srvDescriptorSize = 0;
270 UINT m_nextSrvSlot = 1;
271 static constexpr UINT MAX_SRV_DESCRIPTORS = 512;
274 bool CreateSharedSrvHeap();
277 UINT AllocateSRVSlot();
278 D3D12_CPU_DESCRIPTOR_HANDLE GetSharedSrvCPUHandle(UINT slot)
const;
279 D3D12_GPU_DESCRIPTOR_HANDLE GetSharedSrvGPUHandle(UINT slot)
const;
280 ID3D12DescriptorHeap* GetSharedSrvHeap()
const {
return m_sharedSrvHeap.Get(); }
290class 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.
bool operator==(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)