4#include <d3d11shader.h>
5#include <d3dcompiler.h>
12#ifdef SLEAK_HOT_RELOAD
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
29std::string StemFromPath(
const std::string& path) {
30 const std::string name = std::filesystem::path(path).filename().string();
31 return name.substr(0, name.find(
'.'));
35std::unordered_set<DirectX11Shader*>& LiveShaders() {
36 static auto* shaders =
new std::unordered_set<DirectX11Shader*>();
44 assert(m_device !=
nullptr);
45 m_device->GetImmediateContext(m_deviceContext.GetAddressOf());
46#ifdef SLEAK_HOT_RELOAD
47 LiveShaders().insert(
this);
52#ifdef SLEAK_HOT_RELOAD
53 LiveShaders().erase(
this);
57#ifdef SLEAK_HOT_RELOAD
58const std::unordered_set<DirectX11Shader*>& DirectX11Shader::GetLiveShaders() {
62bool DirectX11Shader::Reload() {
63 if (m_sources.vertPath.empty() || m_sources.fragPath.empty())
return false;
64 return BuildStages(m_sources);
67void DirectX11Shader::AdoptStages(
const DirectX11Shader& source) {
68 m_vertexShader = source.m_vertexShader;
69 m_pixelShader = source.m_pixelShader;
70 m_vertexShaderBlob = source.m_vertexShaderBlob;
71 m_pixelShaderBlob = source.m_pixelShaderBlob;
72 Layout = source.Layout;
77 return BuildStages({shaderPath, VERTEX_ENTRY, shaderPath, PIXEL_ENTRY});
81 const std::string& fragPath) {
83 {vertPath, SPLIT_FILE_ENTRY, fragPath, SPLIT_FILE_ENTRY});
86bool DirectX11Shader::BuildStages(StageSources sources) {
87 Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
88 Microsoft::WRL::ComPtr<ID3D11PixelShader> pixelShader;
89 Microsoft::WRL::ComPtr<ID3DBlob> vertexBlob;
90 Microsoft::WRL::ComPtr<ID3DBlob> pixelBlob;
91 if (!compileShader(sources.vertPath, sources.vertEntry, VERTEX_PROFILE,
92 vertexShader, vertexBlob)) {
93 SLEAK_ERROR(
"Failed to compile vertex shader {}", sources.vertPath);
96 if (!compileShader(sources.fragPath, sources.fragEntry, PIXEL_PROFILE,
97 pixelShader, pixelBlob)) {
98 SLEAK_ERROR(
"Failed to compile pixel shader {}", sources.fragPath);
103 Microsoft::WRL::ComPtr<ID3D11InputLayout> layout;
104 if (Layout && !CreateLayoutFor(vertexBlob.Get(), layout))
return false;
106 m_vertexShader = std::move(vertexShader);
107 m_pixelShader = std::move(pixelShader);
108 m_vertexShaderBlob = std::move(vertexBlob);
109 m_pixelShaderBlob = std::move(pixelBlob);
110 if (layout) Layout = std::move(layout);
111#ifdef SLEAK_HOT_RELOAD
112 m_vertStem = StemFromPath(sources.vertPath);
113 m_fragStem = StemFromPath(sources.fragPath);
114 m_sources = std::move(sources);
120 if (!m_vertexShader || !m_pixelShader) {
121 throw std::runtime_error(
"Shaders are not compiled!");
124 m_deviceContext->VSSetShader(m_vertexShader.Get(),
nullptr, 0);
125 m_deviceContext->PSSetShader(m_pixelShader.Get(),
nullptr, 0);
127 m_deviceContext->IASetInputLayout(Layout.Get());
131 return m_vertexShaderBlob.Get();
135 Microsoft::WRL::ComPtr<ID3D11InputLayout> layout;
136 if (!m_vertexShaderBlob ||
137 !CreateLayoutFor(m_vertexShaderBlob.Get(), layout))
142 return layout.Detach();
145bool DirectX11Shader::CreateLayoutFor(
146 ID3DBlob* vertexBlob, Microsoft::WRL::ComPtr<ID3D11InputLayout>& layout) {
148 bool isSkinned =
false;
149 Microsoft::WRL::ComPtr<ID3D11ShaderReflection> reflector;
151 D3DReflect(vertexBlob->GetBufferPointer(), vertexBlob->GetBufferSize(),
152 IID_ID3D11ShaderReflection,
153 reinterpret_cast<void**
>(reflector.GetAddressOf()));
155 D3D11_SHADER_DESC shaderDesc;
156 reflector->GetDesc(&shaderDesc);
157 for (UINT p = 0; p < shaderDesc.InputParameters; ++p) {
158 D3D11_SIGNATURE_PARAMETER_DESC paramDesc;
159 reflector->GetInputParameterDesc(p, ¶mDesc);
160 if (strcmp(paramDesc.SemanticName,
"BLENDINDICES") == 0) {
167 D3D11_INPUT_ELEMENT_DESC* layoutDesc = isSkinned ? SkinnedLayout : DefaultLayout;
168 UINT layoutCount = isSkinned ? ARRAYSIZE(SkinnedLayout) : ARRAYSIZE(DefaultLayout);
170 hr = m_device->CreateInputLayout(
171 layoutDesc, layoutCount, vertexBlob->GetBufferPointer(),
172 vertexBlob->GetBufferSize(), layout.ReleaseAndGetAddressOf());
175 SLEAK_ERROR(
"Failed to create input layout: 0x{:08X}",
176 static_cast<unsigned int>(hr));
184bool DirectX11Shader::compileShader(
const std::string& filePath,
185 const std::string& entryPoint,
186 const std::string& profile,
187 Microsoft::WRL::ComPtr<T>& shader,
188 Microsoft::WRL::ComPtr<ID3DBlob>& blob) {
189 Microsoft::WRL::ComPtr<ID3DBlob> errorBlob;
190 HRESULT hr = D3DCompileFromFile(
191 std::wstring(filePath.begin(), filePath.end()).c_str(),
193 D3D_COMPILE_STANDARD_FILE_INCLUDE,
204 SLEAK_ERROR(
"Shader compilation error in {} ({}): {}", filePath,
205 entryPoint, (
char*)errorBlob->GetBufferPointer());
207 SLEAK_ERROR(
"Shader compilation error in {} ({}): 0x{:08X}",
208 filePath, entryPoint,
static_cast<unsigned int>(hr));
214 if constexpr (std::is_same_v<T, ID3D11VertexShader>) {
215 hr = m_device->CreateVertexShader(
216 blob->GetBufferPointer(), blob->GetBufferSize(),
nullptr,
217 reinterpret_cast<ID3D11VertexShader**
>(shader.GetAddressOf()));
218 }
else if constexpr (std::is_same_v<T, ID3D11PixelShader>) {
219 hr = m_device->CreatePixelShader(
220 blob->GetBufferPointer(), blob->GetBufferSize(),
nullptr,
221 reinterpret_cast<ID3D11PixelShader**
>(shader.GetAddressOf()));
225 SLEAK_ERROR(
"Failed to create shader object for {} ({}): 0x{:08X}",
226 filePath, entryPoint,
static_cast<unsigned int>(hr));
228 return SUCCEEDED(hr);
ID3D11InputLayout * createInputLayout()
Builds the input layout matching DefaultLayout or SkinnedLayout from the vertex shader bytecode.
bool compile(const std::string &shaderPath) override
Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
ID3DBlob * getVertexShaderBlob() const
~DirectX11Shader() override
void bind() override
Binds this program as the active shader for subsequent draws.
DirectX11Shader(ID3D11Device *device)
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.