SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectX11Shader.hpp
Go to the documentation of this file.
1#ifndef _DIRECTX11_SHADER_H_
2#define _DIRECTX11_SHADER_H_
3
4#include <d3d11.h>
5#include <d3d11shader.h>
6#include <d3dcompiler.h>
7#include <wrl/client.h>
8
9#include <stdexcept>
10#include <string>
11#include <vector>
12#ifdef SLEAK_HOT_RELOAD
13#include <unordered_set>
14#endif
15
16#include "../Common/Shader.hpp"
17
18namespace Sleak {
19namespace RenderEngine {
20
21/// D3D11 vertex+pixel shader pair, compiled from HLSL and paired with an input layout.
22class DirectX11Shader : public Shader {
23 public:
24 DirectX11Shader(ID3D11Device* device);
25 ~DirectX11Shader() 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 void bind() override;
35
36 ID3DBlob* getVertexShaderBlob() const;
37 /// Builds the input layout matching DefaultLayout or SkinnedLayout from the vertex shader bytecode.
38 ID3D11InputLayout* createInputLayout();
39 ID3D11InputLayout* getInputLayout() const { return Layout.Get(); }
40
41#ifdef SLEAK_HOT_RELOAD
42 /// Rebuilds stages and input layout, keeping the old ones on failure.
43 bool Reload();
44 /// Shares the stages and layout of a shader built from the same files.
45 void AdoptStages(const DirectX11Shader& source);
46 /// Returns true when both shaders compile the same files and entry points.
47 bool HasSameSources(const DirectX11Shader& other) const {
48 return m_sources == other.m_sources;
49 }
50 /// Returns the stem of the vertex source, e.g. skybox for skybox.hlsl.
51 const std::string& GetStem() const { return m_vertStem; }
52 /// Returns true when the vertex or pixel source has the given stem.
53 bool UsesStem(const std::string& stem) const {
54 return stem == m_vertStem || stem == m_fragStem;
55 }
56 /// Returns every live DirectX11Shader instance.
57 static const std::unordered_set<DirectX11Shader*>& GetLiveShaders();
58#endif
59
60 private:
61 /// Files and entry points the two stages compile from.
62 struct StageSources {
63 std::string vertPath;
64 std::string vertEntry;
65 std::string fragPath;
66 std::string fragEntry;
67
68 bool operator==(const StageSources&) const = default;
69 };
70
71 /// Compiles both stages into new objects and swaps them in on success.
72 bool BuildStages(StageSources sources);
73 /// Creates an input layout matching the inputs of the vertex bytecode.
74 bool CreateLayoutFor(ID3DBlob* vertexBlob,
75 Microsoft::WRL::ComPtr<ID3D11InputLayout>& layout);
76 /// Compiles one HLSL entry point/profile via D3DCompile into a shader object and bytecode blob.
77 template <typename T>
78 bool compileShader(const std::string& filePath,
79 const std::string& entryPoint,
80 const std::string& profile,
81 Microsoft::WRL::ComPtr<T>& shader,
82 Microsoft::WRL::ComPtr<ID3DBlob>& blob);
83
84 private:
85 Microsoft::WRL::ComPtr<ID3D11Device> m_device;
86 Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_deviceContext;
87
88 Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
89 Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
90 Microsoft::WRL::ComPtr<ID3D11InputLayout> Layout;
91
92 Microsoft::WRL::ComPtr<ID3DBlob>
93 m_vertexShaderBlob; // For input layout creation
94 Microsoft::WRL::ComPtr<ID3DBlob> m_pixelShaderBlob;
95
96#ifdef SLEAK_HOT_RELOAD
97 StageSources m_sources;
98 std::string m_vertStem;
99 std::string m_fragStem;
100#endif
101
102 D3D11_INPUT_ELEMENT_DESC DefaultLayout[5] = {
103 // Semantic Index Format Slot Offset Class Step Rate
104 {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
105 {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
106 {"TANGENT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
107 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D11_INPUT_PER_VERTEX_DATA, 0},
108 {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 56, D3D11_INPUT_PER_VERTEX_DATA, 0}
109 };
110
111 D3D11_INPUT_ELEMENT_DESC SkinnedLayout[7] = {
112 {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
113 {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
114 {"TANGENT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
115 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D11_INPUT_PER_VERTEX_DATA, 0},
116 {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 56, D3D11_INPUT_PER_VERTEX_DATA, 0},
117 {"BLENDINDICES", 0, DXGI_FORMAT_R32G32B32A32_SINT, 0, 64, D3D11_INPUT_PER_VERTEX_DATA, 0},
118 {"BLENDWEIGHT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 80, D3D11_INPUT_PER_VERTEX_DATA, 0}
119 };
120};
121
122} // namespace RenderEngine
123} // namespace Sleak
124
125#endif // _DIRECTX11_SHADER_H_
D3D11 vertex+pixel shader pair, compiled from HLSL and paired with an input layout.
ID3D11InputLayout * createInputLayout()
Builds the input layout matching DefaultLayout or SkinnedLayout from the vertex shader bytecode.
DirectX11Shader & operator=(const DirectX11Shader &)=delete
bool compile(const std::string &shaderPath) override
Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
ID3D11InputLayout * getInputLayout() const
void bind() override
Binds this program as the active shader for subsequent draws.
DirectX11Shader(const DirectX11Shader &)=delete
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