SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX11Renderer.hpp
Go to the documentation of this file.
1#include <Core/OSDef.hpp>
2
3#ifdef PLATFORM_WIN
4
5#ifndef _DIRECTX11RENDERER_H
6#define _DIRECTX11RENDERER_H
7
8#include <backends/imgui_impl_dx11.h>
9#include <d3d11.h>
10#include <imgui.h>
11#include <wrl/client.h>
12
13#include <Core/Timer.hpp>
14#include <Core/Window.hpp>
17#include <chrono>
18#include <utility>
19#include <vector>
20
24
25namespace Sleak {
26namespace RenderEngine {
27
28/// D3D11 backend: forward pipeline with shadow mapping, MSAA, and ACES tonemapping.
29class ENGINE_API DirectX11Renderer : public Renderer, public RenderContext {
30public:
31 DirectX11Renderer(Window* window);
32 ~DirectX11Renderer() override;
33
34 bool Initialize() override;
35 void BeginRender() override;
36 void EndRender() override;
37 void Cleanup() override;
38#ifdef SLEAK_HOT_RELOAD
39 /// Recompiles every live shader and fixed pass built from one of the stems.
40 bool ReloadShaders(const std::vector<std::string>& stems) override;
41#endif
42
43 /// Allocates the depth/stencil texture and view sized to the swapchain.
44 bool CreateDepthStencilBuffer(uint32_t width, uint32_t height);
45 /// Creates and binds a depth-stencil state matching the requested depth/stencil toggles.
46 void SetDepthStencilState(bool enableDepth, bool enableStencil);
47
48 virtual void Resize(uint32_t width, uint32_t height) override;
49
50 // Feature capability mask
51 virtual uint32_t GetFeatureCaps() const override {
53 }
54
55 virtual void Draw(uint32_t vertexCount) override;
56 virtual void Draw(uint32_t vertexCount, uint32_t firstVertex) override;
57 virtual void DrawIndexed(uint32_t indexCount) override;
58 virtual void DrawIndexed(uint32_t indexCount, uint32_t firstIndex,
59 int32_t baseVertex) override;
60 virtual void DrawInstance(uint32_t instanceCount, uint32_t vertexPerInstance) override;
61 virtual void DrawIndexedInstance(uint32_t instanceCount, uint32_t indexPerInstance) override;
62 virtual void DrawIndexedInstance(uint32_t instanceCount,
63 uint32_t indexPerInstance,
64 uint32_t firstIndex,
65 int32_t baseVertex) override;
66
67 // State management
68 virtual void SetRenderMode(RenderMode mode) override;
69 virtual void SetRenderFace(RenderFace face) override;
70 virtual void SetViewport(float x, float y, float width, float height, float minDepth = 0.0f, float maxDepth = 1.0f) override;
71 virtual void ClearRenderTarget(float r, float g, float b, float a) override;
72 virtual void ClearDepthStencil(bool clearDepth, bool clearStencil,
73 float depth, uint8_t stencil) override;
74
75 // Buffer binding
76 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
77 uint32_t slot = 0) override;
78 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
79 uint32_t slot = 0) override;
80 virtual void BindConstantBuffer(RefPtr<BufferBase> buffer,
81 uint32_t slot = 0) override;
82
83 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size, void* data) override;
84 virtual Shader* CreateShader(const std::string& shaderSource) override;
85 virtual Texture* CreateTexture(const std::string& TexturePath) override;
86 virtual Texture* CreateTextureFromData(uint32_t width, uint32_t height,
87 void* data) override;
88
89 /// Loads a cubemap from six face image paths.
90 Texture* CreateCubemapTexture(const std::array<std::string, 6>& facePaths);
91 /// Loads a cubemap by converting a single equirectangular panorama.
92 Texture* CreateCubemapTextureFromPanorama(const std::string& panoramaPath);
93
94 // Skybox state management
95 virtual void SetDepthWrite(bool enabled) override;
96 virtual void SetDepthCompare(DepthCompare compare) override;
97 virtual void SetCullEnabled(bool enabled) override;
98 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0) override;
99 virtual void BeginSkyboxPass() override;
100 virtual void EndSkyboxPass() override;
101 virtual void BeginDebugLinePass() override;
102 virtual void EndDebugLinePass() override;
103
104 virtual bool CreateImGUI();
105
106 void ApplyMSAAChange() override;
107
108 // Shadow mapping support
109 void UpdateShadowLightUBO(const void* data, uint32_t size) override;
110
111 virtual RenderContext* GetContext() override { return this; }
112
113 private:
114 virtual void ConfigureRenderMode() override;
115 virtual void ConfigureRenderFace() override;
116 /// Creates the back-buffer render target view from the swapchain.
117 bool CreateRenderTargetView();
118 /// Creates and binds the rasterizer state matching current fill/cull settings.
119 bool SetRasterState();
120 /// Binds the depth state matching the tracked depth write and compare.
121 void ApplyDepthState();
122 /// Returns the cached rasterizer state for desc, creating it on a miss.
123 ID3D11RasterizerState* GetRasterState(const D3D11_RASTERIZER_DESC& desc);
124 /// Returns the cached depth-stencil state for desc, creating it on a miss.
125 ID3D11DepthStencilState* GetDepthState(
126 const D3D11_DEPTH_STENCIL_DESC& desc);
127
128 /// Creates the alpha-blend state used for transparent draws.
129 bool CreateBlendState();
130 void SetBlendState(float r, float g, float b, float a, UINT Mask);
131
132 /// Queries the device for supported MSAA sample counts and their quality levels.
133 void CheckMSAASupport();
134 /// Allocates the multisampled color render target at the current sample count.
135 bool CreateMSAARenderTarget();
136 /// Allocates the multisampled depth-stencil buffer at the current sample count.
137 bool CreateMSAADepthStencil();
138 /// Resolves the multisampled render target into the single-sample back buffer.
139 void ResolveMSAA();
140
141
142 bool bIsLayoutCreated = false;
143
144 List<std::string> SupportedMSAA;
145
146 UINT msaaSampleCount = 1;
147 UINT msaaQualityLevel = 0;
148
149 IDXGISwapChain* swapChain; // Swap chain for double buffering
150
151 ID3D11Device* device; // DirectX 11 device
152 ID3D11DeviceContext* deviceContext; // DirectX 11 device context
153 ID3D11RenderTargetView* renderTargetView; // Render target view
154 ID3D11InputLayout* layout;
155
156 ID3D11DepthStencilState* depthStencilState = nullptr;
157 ID3D11DepthStencilView* depthStencilView;
158 ID3D11Texture2D* depthStencilBuffer;
159
160 ID3D11BlendState* blendState;
161
162 ID3D11Texture2D* msaaRenderTarget = nullptr;
163 ID3D11RenderTargetView* msaaRenderTargetView = nullptr;
164 ID3D11Texture2D* msaaDepthStencilBuffer = nullptr;
165 ID3D11DepthStencilView* msaaDepthStencilView = nullptr;
166
167 ID3D11Query* query = nullptr;
168
169 D3D11_PRIMITIVE_TOPOLOGY topology;
170 D3D11_FILL_MODE fillMode;
171 D3D11_CULL_MODE cull;
172 Window* window;
173
174 // Skybox state
175 D3D11_PRIMITIVE_TOPOLOGY m_savedTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
176 ID3D11DepthStencilState* m_savedDepthStencilState = nullptr;
177 D3D11_CULL_MODE m_savedCullMode = D3D11_CULL_FRONT;
178 bool m_depthWriteEnabled = true;
179 D3D11_COMPARISON_FUNC m_depthFunc = D3D11_COMPARISON_LESS;
180
181 // Immutable state objects keyed by description, owned until Cleanup
182 std::vector<std::pair<D3D11_RASTERIZER_DESC,
183 Microsoft::WRL::ComPtr<ID3D11RasterizerState>>>
184 m_rasterStates;
185 std::vector<std::pair<D3D11_DEPTH_STENCIL_DESC,
186 Microsoft::WRL::ComPtr<ID3D11DepthStencilState>>>
187 m_depthStates;
188
189 ImGuiContext* ImCon;
190
191 // Shadow mapping
192 ID3D11Buffer* m_shadowCB = nullptr;
193 bool m_shadowCBCreated = false;
194 /// Allocates the constant buffer backing PCSSShadowGPUData.
195 bool CreateShadowConstantBuffer();
196 void CleanupShadowResources();
197
198 // Shadow depth map
199 ID3D11Texture2D* m_shadowDepthTex = nullptr;
200 ID3D11DepthStencilView* m_shadowDSV = nullptr;
201 ID3D11ShaderResourceView* m_shadowSRV = nullptr;
202 ID3D11SamplerState* m_shadowSampler = nullptr;
203 ID3D11RasterizerState* m_shadowRasterState = nullptr;
204 bool m_shadowMapCreated = false;
205 float m_lightVP[16] = {};
206 float m_pendingLightVP[16] = {};
207 bool m_hasPendingLightVP = false;
208 ID3D11Buffer* m_shadowTransformCB = nullptr; // Dedicated CB for shadow pass transforms
209 // World last uploaded to m_shadowTransformCB, valid until lightVP changes
210 float m_shadowWorldCache[16] = {};
211 bool m_shadowCBCacheValid = false;
212 bool m_inShadowPass = false;
213 void SetLightVP(const float* mat) override;
214 /// Allocates the shadow-pass depth texture, DSV/SRV, and comparison sampler.
215 bool CreateShadowMapResources();
216 /// Replays cached shadow-caster draws into the depth-only shadow map.
217 void RenderShadowPass();
218
219 // Post-process: Tonemapping
220 ID3D11Buffer* m_postProcessCB = nullptr;
221 ID3D11Texture2D* m_hdrTexture = nullptr;
222 ID3D11RenderTargetView* m_hdrRTV = nullptr;
223 ID3D11ShaderResourceView* m_hdrSRV = nullptr;
224 ID3D11SamplerState* m_pointSampler = nullptr;
225 ID3D11VertexShader* m_tonemapVS = nullptr;
226 ID3D11PixelShader* m_tonemapPS = nullptr;
227 bool m_tonemapResourcesCreated = false;
228 /// Allocates the HDR offscreen target and tonemap shaders/sampler.
229 bool CreateTonemapResources();
230 /// Compiles tonemap.hlsl, writing the outputs only on success.
231 bool CompileTonemapShaders(ID3D11VertexShader** vs, ID3D11PixelShader** ps);
232#ifdef SLEAK_HOT_RELOAD
233 /// Swaps in recompiled tonemap shaders, keeping the old ones on failure.
234 void ReloadTonemapShaders();
235#endif
236 void CleanupTonemapResources();
237 /// Runs the ACES tonemap full-screen pass from the HDR target to the back buffer.
238 void ExecuteTonemapPass();
239};
240
241} // namespace RenderEngine
242} // namespace Sleak
243
244#endif // _DIRECTX11RENDERER_H
245
246#endif
int width
int height
Abstract interface for high-level graphics command execution and resource management.
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Definition Renderer.hpp:41
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
class ENGINE_API Window