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(); }
43#ifdef SLEAK_HOT_RELOAD
45 bool ReloadShaders(
const std::vector<std::string>& stems)
override;
48 virtual void Resize(uint32_t
width, uint32_t
height)
override;
51 static bool IsSupport();
53 virtual bool CreateImGUI()
override;
55 virtual RenderContext* GetContext()
override {
return this; }
58 virtual uint32_t GetFeatureCaps()
const override {
return CapShadows; }
61 virtual void Draw(uint32_t vertexCount)
override;
62 virtual void Draw(uint32_t vertexCount, uint32_t firstVertex)
override;
63 virtual void DrawIndexed(uint32_t indexCount)
override;
64 virtual void DrawIndexed(uint32_t indexCount, uint32_t firstIndex,
65 int32_t baseVertex)
override;
66 virtual void DrawInstance(uint32_t instanceCount,
67 uint32_t vertexPerInstance)
override;
68 virtual void DrawIndexedInstance(uint32_t instanceCount,
69 uint32_t indexPerInstance)
override;
70 virtual void DrawIndexedInstance(uint32_t instanceCount,
71 uint32_t indexPerInstance,
73 int32_t baseVertex)
override;
75 virtual void SetRenderFace(RenderFace face)
override;
76 virtual void SetRenderMode(RenderMode mode)
override;
78 float minDepth = 0.0f,
79 float maxDepth = 1.0f)
override;
82 virtual void ClearDepthStencil(
bool clearDepth,
bool clearStencil,
83 float depth, uint8_t stencil)
override;
85 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
86 uint32_t slot = 0)
override;
87 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
88 uint32_t slot = 0)
override;
90 uint32_t slot = 0)
override;
92 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size,
94 virtual Shader* CreateShader(
const std::string& shaderSource)
override;
95 virtual Texture* CreateTexture(
const std::string& TexturePath)
override;
100 Texture* CreateCubemapTexture(
const std::array<std::string, 6>& facePaths);
102 Texture* CreateCubemapTextureFromPanorama(
const std::string& panoramaPath);
105 void UpdateShadowLightUBO(
const void* data, uint32_t size)
override;
108 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0)
override;
109 virtual void BindTextureRaw(Sleak::Texture* texture, uint32_t slot = 0)
override;
110 virtual void BeginSkyboxPass()
override;
111 virtual void EndSkyboxPass()
override;
112 virtual void BeginDebugLinePass()
override;
113 virtual void EndDebugLinePass()
override;
116 void BindShaderPipeline(ID3D12PipelineState* pso);
123 uint32_t vertexFormat = 0;
124 D3D12_CULL_MODE cullMode = D3D12_CULL_MODE_FRONT;
125 BOOL depthClip = TRUE;
127 float depthBiasClamp = 0.0f;
128 float slopeScaledDepthBias = 0.0f;
129 D3D12_DEPTH_WRITE_MASK depthWrite = D3D12_DEPTH_WRITE_MASK_ALL;
130 D3D12_COMPARISON_FUNC depthFunc = D3D12_COMPARISON_FUNC_LESS;
131 BOOL blendEnable = FALSE;
132 D3D12_PRIMITIVE_TOPOLOGY_TYPE topology =
133 D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
134 DXGI_FORMAT rtvFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
135 DXGI_FORMAT dsvFormat = DXGI_FORMAT_D32_FLOAT;
137 bool operator==(
const PipelineKey&)
const =
default;
141 ID3D12PipelineState* GetPipeline(
const PipelineKey& key, ID3DBlob* vs,
144 ID3D12PipelineState* GetShaderPipeline(ID3DBlob* vs, ID3DBlob* ps);
145#ifdef SLEAK_HOT_RELOAD
147 void ReloadLiveShaders(
const std::vector<std::string>& stems,
148 std::vector<ID3D12PipelineState*>& replaced);
150 void ReloadPassPipeline(ID3D12PipelineState*& pso,
bool& failed,
151 bool (DirectX12Renderer::*create)(),
153 std::vector<ID3D12PipelineState*>& replaced);
155 void ReloadShadowPassPSO(std::vector<ID3D12PipelineState*>& replaced);
157 void PruneReplacedPipelines(
158 const std::vector<ID3D12PipelineState*>& replaced);
161 void RestoreShaderPipeline();
163 void BindLightConstants();
167 bool CreateCommandQueue();
169 bool CreateSwapChain();
171 bool CreateCommandAllocatorAndList();
173 bool CreateRenderTargetViews();
174 bool CreateDepthStencilView();
176 bool CreateRootSignature();
178 virtual void ConfigureRenderMode()
override;
179 virtual void ConfigureRenderFace()
override;
184 void EnumerateDevices(Microsoft::WRL::ComPtr<IDXGIFactory4> factory);
187 static constexpr UINT FrameCount = 2;
190 Microsoft::WRL::ComPtr<ID3D12Device> device;
191 Microsoft::WRL::ComPtr<IDXGISwapChain3> swapChain;
192 Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
195 Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue;
196 Microsoft::WRL::ComPtr<ID3D12CommandAllocator> commandAllocators[FrameCount];
197 Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> commandList;
200 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> rtvHeap;
201 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> dsvHeap;
204 Microsoft::WRL::ComPtr<ID3D12Resource> renderTargets[FrameCount];
205 Microsoft::WRL::ComPtr<ID3D12Resource> depthStencilBuffer;
208 Microsoft::WRL::ComPtr<ID3D12RootSignature> rootSignature;
210 std::pair<PipelineKey, Microsoft::WRL::ComPtr<ID3D12PipelineState>>>
212 ID3D12PipelineState* m_defaultPipelineState =
nullptr;
213 ID3D12PipelineState* m_shaderPipeline =
nullptr;
214 ID3D12PipelineState* m_passPipeline =
nullptr;
218 Microsoft::WRL::ComPtr<ID3D12Fence> fence;
219 HANDLE fenceEvent =
nullptr;
220 UINT64 m_fenceValue = 0;
221 UINT64 fenceValues[FrameCount] = {};
222 uint64_t m_frameSerial = 0;
223 bool m_frameActive =
false;
226 UINT rtvDescriptorSize = 0;
233 D3D12_VIEWPORT viewport = {};
234 D3D12_RECT scissorRect = {};
236 bool m_Initialized =
false;
239 DirectX12Texture* m_defaultTexture =
nullptr;
242 ID3D12PipelineState* m_skyboxPipelineState =
nullptr;
243 bool m_skyboxPipelineFailed =
false;
245 bool CreateSkyboxPipelineState();
248 ID3D12PipelineState* m_debugLinePipelineState =
nullptr;
249 bool m_debugLinePipelineFailed =
false;
251 bool CreateDebugLinePipelineState();
254 DirectX12UploadRing m_uploadRing;
256 DirectX12UploadContext m_uploader;
259 std::vector<uint8_t> m_lightData;
262 Microsoft::WRL::ComPtr<ID3D12Resource> m_shadowDepthBuffer;
263 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_shadowDsvHeap;
264 D3D12_CPU_DESCRIPTOR_HANDLE m_shadowDsvHandle = {};
265 UINT m_shadowSrvIndex = 0;
266 bool m_shadowMapCreated =
false;
267 float m_lightVP[16] = {};
268 float m_pendingLightVP[16] = {};
269 bool m_hasPendingLightVP =
false;
271 float m_shadowWorldCache[16] = {};
272 D3D12_GPU_VIRTUAL_ADDRESS m_shadowCBAddress = 0;
273 bool m_inShadowPass =
false;
275 ID3D12PipelineState* m_shadowPassPSO =
nullptr;
276 Microsoft::WRL::ComPtr<ID3DBlob> m_shadowVSBlob;
277 bool m_shadowVSCompiled =
false;
279 Microsoft::WRL::ComPtr<ID3DBlob> m_cachedVSBlob;
280 void SetLightVP(
const float* mat)
override;
282 bool CreateShadowMapResources();
284 bool CreateShadowPassPSO();
286 void RenderShadowPass();
289 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> imguiSrvHeap;
292 Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_sharedSrvHeap;
293 UINT m_srvDescriptorSize = 0;
294 UINT m_nextSrvSlot = 1;
295 static constexpr UINT MAX_SRV_DESCRIPTORS = 512;
298 bool CreateSharedSrvHeap();
301 UINT AllocateSRVSlot();
302 D3D12_CPU_DESCRIPTOR_HANDLE GetSharedSrvCPUHandle(UINT slot)
const;
303 D3D12_GPU_DESCRIPTOR_HANDLE GetSharedSrvGPUHandle(UINT slot)
const;
304 ID3D12DescriptorHeap* GetSharedSrvHeap()
const {
return m_sharedSrvHeap.Get(); }
314class 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)