SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX12Shader.cpp
Go to the documentation of this file.
2
3#include <d3d12.h>
4#include <d3dcompiler.h>
5#include <wrl/client.h>
6
7#include <Core/Logger.hpp>
8#include <utility>
9#ifdef SLEAK_HOT_RELOAD
10#include <filesystem>
11#include <string_view>
12#endif
13
15
16namespace Sleak {
17namespace RenderEngine {
18
19namespace {
20
21constexpr const char* VERTEX_ENTRY = "VS_Main";
22constexpr const char* PIXEL_ENTRY = "PS_Main";
23constexpr const char* SPLIT_FILE_ENTRY = "Main";
24constexpr const char* VERTEX_PROFILE = "vs_5_0";
25constexpr const char* PIXEL_PROFILE = "ps_5_0";
26
27#ifdef SLEAK_HOT_RELOAD
28constexpr std::string_view DX12_STEM_SUFFIX = "_dx12";
29
30/// Returns the file name up to its first '.', minus a trailing _dx12.
31std::string StemFromPath(const std::string& path) {
32 std::string stem = std::filesystem::path(path).filename().string();
33 stem = stem.substr(0, stem.find('.'));
34 if (stem.ends_with(DX12_STEM_SUFFIX)) {
35 stem.resize(stem.size() - DX12_STEM_SUFFIX.size());
36 }
37 return stem;
38}
39
40/// Returns the live shader registry, leaked to survive static teardown.
41std::unordered_set<DirectX12Shader*>& LiveShaders() {
42 static auto* shaders = new std::unordered_set<DirectX12Shader*>();
43 return *shaders;
44}
45#endif
46
47} // namespace
48
49DirectX12Shader::DirectX12Shader(ID3D12Device* device) : m_device(device) {
50 assert(m_device != nullptr);
51#ifdef SLEAK_HOT_RELOAD
52 LiveShaders().insert(this);
53#endif
54}
55
57#ifdef SLEAK_HOT_RELOAD
58 LiveShaders().erase(this);
59#endif
60}
61
62#ifdef SLEAK_HOT_RELOAD
63const std::unordered_set<DirectX12Shader*>& DirectX12Shader::GetLiveShaders() {
64 return LiveShaders();
65}
66
67bool DirectX12Shader::RecompileStages(
68 Microsoft::WRL::ComPtr<ID3DBlob>& vs,
69 Microsoft::WRL::ComPtr<ID3DBlob>& ps) const {
70 if (m_sources.vertPath.empty() || m_sources.fragPath.empty()) return false;
71 return CompileSources(m_sources, vs, ps);
72}
73
74void DirectX12Shader::ReplaceStages(Microsoft::WRL::ComPtr<ID3DBlob> vs,
75 Microsoft::WRL::ComPtr<ID3DBlob> ps,
76 ID3D12PipelineState* pso) {
77 m_vertexShaderBlob = std::move(vs);
78 m_pixelShaderBlob = std::move(ps);
79 m_pipelineState = pso;
80}
81#endif
82
83bool DirectX12Shader::compile(const std::string& shaderPath) {
84 // Strip .hlsl extension if present, then use _dx12.hlsl
85 std::string dx12Path = shaderPath;
86 auto hlslPos = dx12Path.rfind(".hlsl");
87 if (hlslPos != std::string::npos) {
88 dx12Path = dx12Path.substr(0, hlslPos);
89 }
90 dx12Path += "_dx12.hlsl";
91
92 return BuildStages({dx12Path, VERTEX_ENTRY, dx12Path, PIXEL_ENTRY});
93}
94
95bool DirectX12Shader::compile(const std::string& vertPath,
96 const std::string& fragPath) {
97 return BuildStages(
98 {vertPath, SPLIT_FILE_ENTRY, fragPath, SPLIT_FILE_ENTRY});
99}
100
101bool DirectX12Shader::BuildStages(StageSources sources) {
102 Microsoft::WRL::ComPtr<ID3DBlob> vs;
103 Microsoft::WRL::ComPtr<ID3DBlob> ps;
104 if (!CompileSources(sources, vs, ps)) return false;
105
106 m_vertexShaderBlob = std::move(vs);
107 m_pixelShaderBlob = std::move(ps);
108#ifdef SLEAK_HOT_RELOAD
109 m_vertStem = StemFromPath(sources.vertPath);
110 m_fragStem = StemFromPath(sources.fragPath);
111 m_sources = std::move(sources);
112#endif
113 return true;
114}
115
116bool DirectX12Shader::CompileSources(
117 const StageSources& sources, Microsoft::WRL::ComPtr<ID3DBlob>& vs,
118 Microsoft::WRL::ComPtr<ID3DBlob>& ps) const {
119 if (!compileShader(sources.vertPath, sources.vertEntry, VERTEX_PROFILE,
120 vs)) {
121 SLEAK_ERROR("Failed to compile vertex shader {}", sources.vertPath);
122 return false;
123 }
124 if (!compileShader(sources.fragPath, sources.fragEntry, PIXEL_PROFILE,
125 ps)) {
126 SLEAK_ERROR("Failed to compile pixel shader {}", sources.fragPath);
127 return false;
128 }
129 return true;
130}
131
133 if (!m_pipelineState) return;
134 if (m_renderer) {
135 m_renderer->BindShaderPipeline(m_pipelineState.Get());
136 } else if (m_commandList) {
137 m_commandList->SetPipelineState(m_pipelineState.Get());
138 }
139}
140
142 return m_vertexShaderBlob.Get();
143}
144
146 return m_pixelShaderBlob.Get();
147}
148
149void DirectX12Shader::SetPipelineState(Microsoft::WRL::ComPtr<ID3D12PipelineState> pso) {
150 m_pipelineState = pso;
151}
152
153ID3D12PipelineState* DirectX12Shader::GetPipelineState() const {
154 return m_pipelineState.Get();
155}
156
157void DirectX12Shader::SetCommandList(ID3D12GraphicsCommandList* cmdList) {
158 m_commandList = cmdList;
159}
160
161void DirectX12Shader::SetRenderer(DirectX12Renderer* renderer) {
162 m_renderer = renderer;
163}
164
165bool DirectX12Shader::compileShader(
166 const std::string& filePath, const std::string& entryPoint,
167 const std::string& profile, Microsoft::WRL::ComPtr<ID3DBlob>& blob) const {
168 Microsoft::WRL::ComPtr<ID3DBlob> errorBlob;
169 HRESULT hr = D3DCompileFromFile(
170 std::wstring(filePath.begin(), filePath.end()).c_str(),
171 nullptr,
172 D3D_COMPILE_STANDARD_FILE_INCLUDE,
173 entryPoint.c_str(),
174 profile.c_str(),
175 D3DCOMPILE_ENABLE_STRICTNESS,
176 0,
177 &blob,
178 &errorBlob);
179
180 if (FAILED(hr)) {
181 if (errorBlob) {
182 SLEAK_ERROR("Shader error in {} ({}): {}", filePath, entryPoint,
183 (char*)errorBlob->GetBufferPointer());
184 } else {
185 SLEAK_ERROR("Shader error in {} ({}): 0x{:08X}", filePath,
186 entryPoint, static_cast<unsigned int>(hr));
187 }
188 return false;
189 }
190 return true;
191}
192
193} // namespace RenderEngine
194} // namespace Sleak
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
void bind() override
Binds the shader's cached PSO to the current command list.
void SetCommandList(ID3D12GraphicsCommandList *cmdList)
void SetPipelineState(Microsoft::WRL::ComPtr< ID3D12PipelineState > pso)
ID3D12PipelineState * GetPipelineState() const
bool compile(const std::string &shaderPath) override
Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
void SetRenderer(DirectX12Renderer *renderer)
Routes bind() through the renderer so pass-specific PSOs are respected.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10