SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX12Shader.cpp
Go to the documentation of this file.
2#include <d3d12.h>
3#include <d3dcompiler.h>
4#include <wrl/client.h>
5#include <Core/Logger.hpp>
6
7namespace Sleak {
8namespace RenderEngine {
9
10DirectX12Shader::DirectX12Shader(ID3D12Device* device) : m_device(device) {
11 assert(m_device != nullptr);
12}
13
15
16bool DirectX12Shader::compile(const std::string& shaderPath) {
17 // Strip .hlsl extension if present, then use _dx12.hlsl
18 std::string dx12Path = shaderPath;
19 auto hlslPos = dx12Path.rfind(".hlsl");
20 if (hlslPos != std::string::npos) {
21 dx12Path = dx12Path.substr(0, hlslPos);
22 }
23 dx12Path += "_dx12.hlsl";
24
25 // Compile shaders
26 if (!compileShader(dx12Path, "VS_Main", "vs_5_0", m_vertexShaderBlob)) {
27 SLEAK_ERROR("Failed to compile vertex shader!");
28 return false;
29 }
30
31 if (!compileShader(dx12Path, "PS_Main", "ps_5_0", m_pixelShaderBlob)) {
32 SLEAK_ERROR("Failed to compile pixel shader!");
33 return false;
34 }
35
36 return true;
37}
38
39bool DirectX12Shader::compile(const std::string& vertPath,
40 const std::string& fragPath) {
41 // Compile shaders
42 if (!compileShader(vertPath, "Main", "vs_5_0", m_vertexShaderBlob)) {
43 SLEAK_ERROR("Failed to compile vertex shader!");
44 return false;
45 }
46
47 if (!compileShader(fragPath, "Main", "ps_5_0", m_pixelShaderBlob)) {
48 SLEAK_ERROR("Failed to compile pixel shader!");
49 return false;
50 }
51
52 return true;
53}
54
56 if (m_pipelineState && m_commandList) {
57 m_commandList->SetPipelineState(m_pipelineState.Get());
58 }
59}
60
62 return m_vertexShaderBlob.Get();
63}
64
66 return m_pixelShaderBlob.Get();
67}
68
69void DirectX12Shader::SetPipelineState(Microsoft::WRL::ComPtr<ID3D12PipelineState> pso) {
70 m_pipelineState = pso;
71}
72
73ID3D12PipelineState* DirectX12Shader::GetPipelineState() const {
74 return m_pipelineState.Get();
75}
76
77void DirectX12Shader::SetCommandList(ID3D12GraphicsCommandList* cmdList) {
78 m_commandList = cmdList;
79}
80
81bool DirectX12Shader::compileShader(const std::string& filePath,
82 const std::string& entryPoint,
83 const std::string& profile,
84 Microsoft::WRL::ComPtr<ID3DBlob>& blob) {
85 Microsoft::WRL::ComPtr<ID3DBlob> errorBlob;
86 HRESULT hr = D3DCompileFromFile(
87 std::wstring(filePath.begin(), filePath.end()).c_str(),
88 nullptr,
89 D3D_COMPILE_STANDARD_FILE_INCLUDE,
90 entryPoint.c_str(),
91 profile.c_str(),
92 D3DCOMPILE_ENABLE_STRICTNESS,
93 0,
94 &blob,
95 &errorBlob);
96
97 if (FAILED(hr)) {
98 if (errorBlob) {
99 SLEAK_ERROR("Shader error: {}", (char*)errorBlob->GetBufferPointer());
100 }
101 return false;
102 }
103 return true;
104}
105
106} // namespace RenderEngine
107} // 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.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10