4#include <d3dcompiler.h>
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";
27#ifdef SLEAK_HOT_RELOAD
28constexpr std::string_view DX12_STEM_SUFFIX =
"_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());
41std::unordered_set<DirectX12Shader*>& LiveShaders() {
42 static auto* shaders =
new std::unordered_set<DirectX12Shader*>();
50 assert(m_device !=
nullptr);
51#ifdef SLEAK_HOT_RELOAD
52 LiveShaders().insert(
this);
57#ifdef SLEAK_HOT_RELOAD
58 LiveShaders().erase(
this);
62#ifdef SLEAK_HOT_RELOAD
63const std::unordered_set<DirectX12Shader*>& DirectX12Shader::GetLiveShaders() {
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);
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;
85 std::string dx12Path = shaderPath;
86 auto hlslPos = dx12Path.rfind(
".hlsl");
87 if (hlslPos != std::string::npos) {
88 dx12Path = dx12Path.substr(0, hlslPos);
90 dx12Path +=
"_dx12.hlsl";
92 return BuildStages({dx12Path, VERTEX_ENTRY, dx12Path, PIXEL_ENTRY});
96 const std::string& fragPath) {
98 {vertPath, SPLIT_FILE_ENTRY, fragPath, SPLIT_FILE_ENTRY});
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;
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);
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,
121 SLEAK_ERROR(
"Failed to compile vertex shader {}", sources.vertPath);
124 if (!compileShader(sources.fragPath, sources.fragEntry, PIXEL_PROFILE,
126 SLEAK_ERROR(
"Failed to compile pixel shader {}", sources.fragPath);
133 if (!m_pipelineState)
return;
135 m_renderer->BindShaderPipeline(m_pipelineState.Get());
136 }
else if (m_commandList) {
137 m_commandList->SetPipelineState(m_pipelineState.Get());
142 return m_vertexShaderBlob.Get();
146 return m_pixelShaderBlob.Get();
150 m_pipelineState = pso;
154 return m_pipelineState.Get();
158 m_commandList = cmdList;
162 m_renderer = renderer;
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(),
172 D3D_COMPILE_STANDARD_FILE_INCLUDE,
175 D3DCOMPILE_ENABLE_STRICTNESS,
182 SLEAK_ERROR(
"Shader error in {} ({}): {}", filePath, entryPoint,
183 (
char*)errorBlob->GetBufferPointer());
185 SLEAK_ERROR(
"Shader error in {} ({}): 0x{:08X}", filePath,
186 entryPoint,
static_cast<unsigned int>(hr));
void bind() override
Binds the shader's cached PSO to the current command list.
void SetCommandList(ID3D12GraphicsCommandList *cmdList)
ID3DBlob * getVertexShaderBlob() const
ID3DBlob * getPixelShaderBlob() const
void SetPipelineState(Microsoft::WRL::ComPtr< ID3D12PipelineState > pso)
~DirectX12Shader() override
ID3D12PipelineState * GetPipelineState() const
DirectX12Shader(ID3D12Device *device)
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.