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
12#include <d3d11.h>
13#include <Core/Window.hpp>
14#include <imgui.h>
15#include <backends/imgui_impl_dx11.h>
16#include <chrono>
17#include <Core/Timer.hpp>
19
20namespace Sleak {
21namespace RenderEngine {
22
23/// D3D11 backend: forward pipeline with shadow mapping, MSAA, and ACES tonemapping.
24class ENGINE_API DirectX11Renderer : public Renderer, public RenderContext {
25public:
26 DirectX11Renderer(Window* window);
27 ~DirectX11Renderer() override;
28
29 bool Initialize() override;
30 void BeginRender() override;
31 void EndRender() override;
32 void Cleanup() override;
33
34 /// Allocates the depth/stencil texture and view sized to the swapchain.
35 bool CreateDepthStencilBuffer(uint32_t width, uint32_t height);
36 /// Creates and binds a depth-stencil state matching the requested depth/stencil toggles.
37 void SetDepthStencilState(bool enableDepth, bool enableStencil);
38
39 virtual void Resize(uint32_t width, uint32_t height) override;
40
41 // Feature capability mask
42 virtual uint32_t GetFeatureCaps() const override {
44 }
45
46 virtual void Draw(uint32_t vertexCount) override;
47 virtual void Draw(uint32_t vertexCount, uint32_t firstVertex) override;
48 virtual void DrawIndexed(uint32_t indexCount) override;
49 virtual void DrawIndexed(uint32_t indexCount, uint32_t firstIndex,
50 int32_t baseVertex) override;
51 virtual void DrawInstance(uint32_t instanceCount, uint32_t vertexPerInstance) override;
52 virtual void DrawIndexedInstance(uint32_t instanceCount, uint32_t indexPerInstance) override;
53 virtual void DrawIndexedInstance(uint32_t instanceCount,
54 uint32_t indexPerInstance,
55 uint32_t firstIndex,
56 int32_t baseVertex) override;
57
58 // State management
59 virtual void SetRenderMode(RenderMode mode) override;
60 virtual void SetRenderFace(RenderFace face) override;
61 virtual void SetViewport(float x, float y, float width, float height, float minDepth = 0.0f, float maxDepth = 1.0f) override;
62 virtual void ClearRenderTarget(float r, float g, float b, float a) override;
63 virtual void ClearDepthStencil(bool clearDepth, bool clearStencil,
64 float depth, uint8_t stencil) override;
65
66 // Buffer binding
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;
71 virtual void BindConstantBuffer(RefPtr<BufferBase> buffer,
72 uint32_t slot = 0) override;
73
74 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size, void* data) override;
75 virtual Shader* CreateShader(const std::string& shaderSource) override;
76 virtual Texture* CreateTexture(const std::string& TexturePath) override;
77 virtual Texture* CreateTextureFromData(uint32_t width, uint32_t height,
78 void* data) override;
79
80 /// Loads a cubemap from six face image paths.
81 Texture* CreateCubemapTexture(const std::array<std::string, 6>& facePaths);
82 /// Loads a cubemap by converting a single equirectangular panorama.
83 Texture* CreateCubemapTextureFromPanorama(const std::string& panoramaPath);
84
85 // Skybox state management
86 virtual void SetDepthWrite(bool enabled) override;
87 virtual void SetDepthCompare(DepthCompare compare) override;
88 virtual void SetCullEnabled(bool enabled) override;
89 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0) override;
90 virtual void BeginSkyboxPass() override;
91 virtual void EndSkyboxPass() override;
92 virtual void BeginDebugLinePass() override;
93 virtual void EndDebugLinePass() override;
94
95 virtual bool CreateImGUI();
96
97 void ApplyMSAAChange() override;
98
99 // Shadow mapping support
100 void UpdateShadowLightUBO(const void* data, uint32_t size) override;
101
102 virtual RenderContext* GetContext() override { return this; }
103
104 private:
105 virtual void ConfigureRenderMode() override;
106 virtual void ConfigureRenderFace() override;
107 /// Creates the back-buffer render target view from the swapchain.
108 bool CreateRenderTargetView();
109 /// Creates and binds the rasterizer state matching current fill/cull settings.
110 bool SetRasterState();
111
112 /// Creates the alpha-blend state used for transparent draws.
113 bool CreateBlendState();
114 void SetBlendState(float r, float g, float b, float a, UINT Mask);
115
116 /// Queries the device for supported MSAA sample counts and their quality levels.
117 void CheckMSAASupport();
118 /// Allocates the multisampled color render target at the current sample count.
119 bool CreateMSAARenderTarget();
120 /// Allocates the multisampled depth-stencil buffer at the current sample count.
121 bool CreateMSAADepthStencil();
122 /// Resolves the multisampled render target into the single-sample back buffer.
123 void ResolveMSAA();
124
125
126 bool bIsLayoutCreated = false;
127
128 List<std::string> SupportedMSAA;
129
130 UINT msaaSampleCount = 1;
131 UINT msaaQualityLevel = 0;
132
133 IDXGISwapChain* swapChain; // Swap chain for double buffering
134
135 ID3D11Device* device; // DirectX 11 device
136 ID3D11DeviceContext* deviceContext; // DirectX 11 device context
137 ID3D11RenderTargetView* renderTargetView; // Render target view
138 ID3D11InputLayout* layout;
139
140 ID3D11DepthStencilState* depthStencilState;
141 ID3D11DepthStencilView* depthStencilView;
142 ID3D11Texture2D* depthStencilBuffer;
143
144 ID3D11BlendState* blendState;
145
146 ID3D11Texture2D* msaaRenderTarget = nullptr;
147 ID3D11RenderTargetView* msaaRenderTargetView = nullptr;
148 ID3D11Texture2D* msaaDepthStencilBuffer = nullptr;
149 ID3D11DepthStencilView* msaaDepthStencilView = nullptr;
150
151 ID3D11Query* query;
152
153 D3D11_PRIMITIVE_TOPOLOGY topology;
154 D3D11_FILL_MODE fillMode;
155 D3D11_CULL_MODE cull;
156 Window* window;
157
158 // Skybox state
159 D3D11_PRIMITIVE_TOPOLOGY m_savedTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
160 ID3D11DepthStencilState* m_savedDepthStencilState = nullptr;
161 D3D11_CULL_MODE m_savedCullMode = D3D11_CULL_FRONT;
162 bool m_depthWriteEnabled = true;
163 D3D11_COMPARISON_FUNC m_depthFunc = D3D11_COMPARISON_LESS;
164
165 ImGuiContext* ImCon;
166
167 // Shadow mapping
168 ID3D11Buffer* m_shadowCB = nullptr;
169 bool m_shadowCBCreated = false;
170 /// Allocates the constant buffer backing PCSSShadowGPUData.
171 bool CreateShadowConstantBuffer();
172 void CleanupShadowResources();
173
174 // Shadow depth map
175 ID3D11Texture2D* m_shadowDepthTex = nullptr;
176 ID3D11DepthStencilView* m_shadowDSV = nullptr;
177 ID3D11ShaderResourceView* m_shadowSRV = nullptr;
178 ID3D11SamplerState* m_shadowSampler = nullptr;
179 ID3D11RasterizerState* m_shadowRasterState = nullptr;
180 bool m_shadowMapCreated = false;
181 float m_lightVP[16] = {};
182 float m_pendingLightVP[16] = {};
183 bool m_hasPendingLightVP = false;
184 ID3D11Buffer* m_shadowTransformCB = nullptr; // Dedicated CB for shadow pass transforms
185 bool m_inShadowPass = false;
186 void SetLightVP(const float* mat) override;
187 /// Allocates the shadow-pass depth texture, DSV/SRV, and comparison sampler.
188 bool CreateShadowMapResources();
189 /// Replays cached shadow-caster draws into the depth-only shadow map.
190 void RenderShadowPass();
191
192 // Post-process: Tonemapping
193 ID3D11Buffer* m_postProcessCB = nullptr;
194 ID3D11Texture2D* m_hdrTexture = nullptr;
195 ID3D11RenderTargetView* m_hdrRTV = nullptr;
196 ID3D11ShaderResourceView* m_hdrSRV = nullptr;
197 ID3D11SamplerState* m_pointSampler = nullptr;
198 ID3D11VertexShader* m_tonemapVS = nullptr;
199 ID3D11PixelShader* m_tonemapPS = nullptr;
200 bool m_tonemapResourcesCreated = false;
201 /// Allocates the HDR offscreen target and tonemap shaders/sampler.
202 bool CreateTonemapResources();
203 void CleanupTonemapResources();
204 /// Runs the ACES tonemap full-screen pass from the HDR target to the back buffer.
205 void ExecuteTonemapPass();
206};
207
208} // namespace RenderEngine
209} // namespace Sleak
210
211#endif // _DIRECTX11RENDERER_H
212
213#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:37
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