SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX12Shader.hpp
Go to the documentation of this file.
1#ifndef _DIRECTX12_SHADER_H_
2#define _DIRECTX12_SHADER_H_
3
4#include <d3d12.h>
5#include <d3dcompiler.h>
6#include <wrl/client.h>
7
8#include <stdexcept>
9#include <string>
10#include <vector>
11#ifdef SLEAK_HOT_RELOAD
12#include <unordered_set>
13#endif
14
15#include "../Common/Shader.hpp"
16
17namespace Sleak {
18namespace RenderEngine {
19class DirectX12Renderer;
20
21/// D3D12 vertex+pixel shader pair; the pipeline state object itself is owned per-shader.
22class DirectX12Shader : public Shader {
23public:
24 DirectX12Shader(ID3D12Device* device);
25 ~DirectX12Shader() override;
28
29 /// Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
30 bool compile(const std::string& shaderPath) override;
31 /// Compiles separate vertex and pixel shader HLSL files.
32 bool compile(const std::string& vertPath,
33 const std::string& fragPath) override;
34 /// Binds the shader's cached PSO to the current command list.
35 void bind() override;
36
37 ID3DBlob* getVertexShaderBlob() const;
38 ID3DBlob* getPixelShaderBlob() const;
39
40 // Per-shader PSO
41 void SetPipelineState(Microsoft::WRL::ComPtr<ID3D12PipelineState> pso);
42 ID3D12PipelineState* GetPipelineState() const;
43 void SetCommandList(ID3D12GraphicsCommandList* cmdList);
44 /// Routes bind() through the renderer so pass-specific PSOs are respected.
45 void SetRenderer(DirectX12Renderer* renderer);
46
47#ifdef SLEAK_HOT_RELOAD
48 /// Recompiles both stages from their files without touching the live ones.
49 bool RecompileStages(Microsoft::WRL::ComPtr<ID3DBlob>& vs,
50 Microsoft::WRL::ComPtr<ID3DBlob>& ps) const;
51 /// Swaps in new bytecode together with the PSO built from it.
52 void ReplaceStages(Microsoft::WRL::ComPtr<ID3DBlob> vs,
53 Microsoft::WRL::ComPtr<ID3DBlob> ps,
54 ID3D12PipelineState* pso);
55 /// Returns true when both shaders compile the same files and entry points.
56 bool HasSameSources(const DirectX12Shader& other) const {
57 return m_sources == other.m_sources;
58 }
59 /// Returns the stem of the vertex source, e.g. skybox for skybox_dx12.hlsl.
60 const std::string& GetStem() const { return m_vertStem; }
61 /// Returns true when the vertex or pixel source has the given stem.
62 bool UsesStem(const std::string& stem) const {
63 return stem == m_vertStem || stem == m_fragStem;
64 }
65 /// Returns every live DirectX12Shader instance.
66 static const std::unordered_set<DirectX12Shader*>& GetLiveShaders();
67#endif
68
69 private:
70 /// Files and entry points the two stages compile from.
71 struct StageSources {
72 std::string vertPath;
73 std::string vertEntry;
74 std::string fragPath;
75 std::string fragEntry;
76
77 bool operator==(const StageSources&) const = default;
78 };
79
80 /// Compiles both stages and swaps them in only when both succeed.
81 bool BuildStages(StageSources sources);
82 /// Compiles both stages of the given sources into new blobs.
83 bool CompileSources(const StageSources& sources,
84 Microsoft::WRL::ComPtr<ID3DBlob>& vs,
85 Microsoft::WRL::ComPtr<ID3DBlob>& ps) const;
86 /// Compiles one HLSL entry point/profile via D3DCompileFromFile into bytecode.
87 bool compileShader(const std::string& filePath,
88 const std::string& entryPoint,
89 const std::string& profile,
90 Microsoft::WRL::ComPtr<ID3DBlob>& blob) const;
91
92 Microsoft::WRL::ComPtr<ID3D12Device> m_device;
93
94 Microsoft::WRL::ComPtr<ID3DBlob> m_vertexShaderBlob;
95 Microsoft::WRL::ComPtr<ID3DBlob> m_pixelShaderBlob;
96 Microsoft::WRL::ComPtr<ID3D12PipelineState> m_pipelineState;
97 ID3D12GraphicsCommandList* m_commandList = nullptr;
98 DirectX12Renderer* m_renderer = nullptr;
99#ifdef SLEAK_HOT_RELOAD
100 StageSources m_sources;
101 std::string m_vertStem;
102 std::string m_fragStem;
103#endif
104};
105
106} // namespace RenderEngine
107} // namespace Sleak
108#endif // _DIRECTX12_SHADER_H_
void bind() override
Binds the shader's cached PSO to the current command list.
void SetCommandList(ID3D12GraphicsCommandList *cmdList)
DirectX12Shader(const DirectX12Shader &)=delete
DirectX12Shader & operator=(const DirectX12Shader &)=delete
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-agnostic compiled shader program.
Definition Shader.hpp:11
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10