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