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
10
11namespace Sleak {
12namespace RenderEngine {
13
14DirectX12Shader::DirectX12Shader(ID3D12Device* device) : m_device(device) {
15 assert(m_device != nullptr);
16}
17
19
20bool DirectX12Shader::compile(const std::string& shaderPath) {
21 // Strip .hlsl extension if present, then use _dx12.hlsl
22 std::string dx12Path = shaderPath;
23 auto hlslPos = dx12Path.rfind(".hlsl");
24 if (hlslPos != std::string::npos) {
25 dx12Path = dx12Path.substr(0, hlslPos);
26 }
27 dx12Path += "_dx12.hlsl";
28
29 // Compile shaders
30 if (!compileShader(dx12Path, "VS_Main", "vs_5_0", m_vertexShaderBlob)) {
31 SLEAK_ERROR("Failed to compile vertex shader!");
32 return false;
33 }
34
35 if (!compileShader(dx12Path, "PS_Main", "ps_5_0", m_pixelShaderBlob)) {
36 SLEAK_ERROR("Failed to compile pixel shader!");
37 return false;
38 }
39
40 return true;
41}
42
43bool DirectX12Shader::compile(const std::string& vertPath,
44 const std::string& fragPath) {
45 // Compile shaders
46 if (!compileShader(vertPath, "Main", "vs_5_0", m_vertexShaderBlob)) {
47 SLEAK_ERROR("Failed to compile vertex shader!");
48 return false;
49 }
50
51 if (!compileShader(fragPath, "Main", "ps_5_0", m_pixelShaderBlob)) {
52 SLEAK_ERROR("Failed to compile pixel shader!");
53 return false;
54 }
55
56 return true;
57}
58
60 if (!m_pipelineState) return;
61 if (m_renderer) {
62 m_renderer->BindShaderPipeline(m_pipelineState.Get());
63 } else if (m_commandList) {
64 m_commandList->SetPipelineState(m_pipelineState.Get());
65 }
66}
67
69 return m_vertexShaderBlob.Get();
70}
71
73 return m_pixelShaderBlob.Get();
74}
75
76void DirectX12Shader::SetPipelineState(Microsoft::WRL::ComPtr<ID3D12PipelineState> pso) {
77 m_pipelineState = pso;
78}
79
80ID3D12PipelineState* DirectX12Shader::GetPipelineState() const {
81 return m_pipelineState.Get();
82}
83
84void DirectX12Shader::SetCommandList(ID3D12GraphicsCommandList* cmdList) {
85 m_commandList = cmdList;
86}
87
88void DirectX12Shader::SetRenderer(DirectX12Renderer* renderer) {
89 m_renderer = renderer;
90}
91
92bool DirectX12Shader::compileShader(const std::string& filePath,
93 const std::string& entryPoint,
94 const std::string& profile,
95 Microsoft::WRL::ComPtr<ID3DBlob>& blob) {
96 Microsoft::WRL::ComPtr<ID3DBlob> errorBlob;
97 HRESULT hr = D3DCompileFromFile(
98 std::wstring(filePath.begin(), filePath.end()).c_str(),
99 nullptr,
100 D3D_COMPILE_STANDARD_FILE_INCLUDE,
101 entryPoint.c_str(),
102 profile.c_str(),
103 D3DCOMPILE_ENABLE_STRICTNESS,
104 0,
105 &blob,
106 &errorBlob);
107
108 if (FAILED(hr)) {
109 if (errorBlob) {
110 SLEAK_ERROR("Shader error: {}", (char*)errorBlob->GetBufferPointer());
111 }
112 return false;
113 }
114 return true;
115}
116
117} // namespace RenderEngine
118} // 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