SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
DirectX12Shader.cpp
Go to the documentation of this file.
1
#include "
../../include/private/Graphics/DirectX12/DirectX12Shader.hpp
"
2
3
#include <d3d12.h>
4
#include <d3dcompiler.h>
5
#include <wrl/client.h>
6
7
#include <
Core/Logger.hpp
>
8
9
#include "
../../include/private/Graphics/DirectX12/DirectX12Renderer.hpp
"
10
11
namespace
Sleak
{
12
namespace
RenderEngine
{
13
14
DirectX12Shader::DirectX12Shader
(ID3D12Device* device) : m_device(device) {
15
assert(m_device !=
nullptr
);
16
}
17
18
DirectX12Shader::~DirectX12Shader
() =
default
;
19
20
bool
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
43
bool
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
59
void
DirectX12Shader::bind
() {
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
68
ID3DBlob*
DirectX12Shader::getVertexShaderBlob
()
const
{
69
return
m_vertexShaderBlob.Get();
70
}
71
72
ID3DBlob*
DirectX12Shader::getPixelShaderBlob
()
const
{
73
return
m_pixelShaderBlob.Get();
74
}
75
76
void
DirectX12Shader::SetPipelineState
(Microsoft::WRL::ComPtr<ID3D12PipelineState> pso) {
77
m_pipelineState = pso;
78
}
79
80
ID3D12PipelineState*
DirectX12Shader::GetPipelineState
()
const
{
81
return
m_pipelineState.Get();
82
}
83
84
void
DirectX12Shader::SetCommandList
(ID3D12GraphicsCommandList* cmdList) {
85
m_commandList = cmdList;
86
}
87
88
void
DirectX12Shader::SetRenderer
(DirectX12Renderer* renderer) {
89
m_renderer = renderer;
90
}
91
92
bool
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
DirectX12Renderer.hpp
DirectX12Shader.hpp
Logger.hpp
SLEAK_ERROR
#define SLEAK_ERROR(...)
Definition
Logger.hpp:22
Sleak::RenderEngine::DirectX12Shader::bind
void bind() override
Binds the shader's cached PSO to the current command list.
Definition
DirectX12Shader.cpp:59
Sleak::RenderEngine::DirectX12Shader::SetCommandList
void SetCommandList(ID3D12GraphicsCommandList *cmdList)
Definition
DirectX12Shader.cpp:84
Sleak::RenderEngine::DirectX12Shader::getVertexShaderBlob
ID3DBlob * getVertexShaderBlob() const
Definition
DirectX12Shader.cpp:68
Sleak::RenderEngine::DirectX12Shader::getPixelShaderBlob
ID3DBlob * getPixelShaderBlob() const
Definition
DirectX12Shader.cpp:72
Sleak::RenderEngine::DirectX12Shader::SetPipelineState
void SetPipelineState(Microsoft::WRL::ComPtr< ID3D12PipelineState > pso)
Definition
DirectX12Shader.cpp:76
Sleak::RenderEngine::DirectX12Shader::~DirectX12Shader
~DirectX12Shader() override
Sleak::RenderEngine::DirectX12Shader::GetPipelineState
ID3D12PipelineState * GetPipelineState() const
Definition
DirectX12Shader.cpp:80
Sleak::RenderEngine::DirectX12Shader::DirectX12Shader
DirectX12Shader(ID3D12Device *device)
Definition
DirectX12Shader.cpp:14
Sleak::RenderEngine::DirectX12Shader::compile
bool compile(const std::string &shaderPath) override
Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
Definition
DirectX12Shader.cpp:20
Sleak::RenderEngine::DirectX12Shader::SetRenderer
void SetRenderer(DirectX12Renderer *renderer)
Routes bind() through the renderer so pass-specific PSOs are respected.
Definition
DirectX12Shader.cpp:88
Sleak::RenderEngine
Backend-facing rendering layer shared by the four graphics backends.
Definition
Application.hpp:60
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
src
Graphics
DirectX12
DirectX12Shader.cpp
Generated on
for SleakEngine by
1.18.0