SleakEngine 1.0.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
13#include "../Common/Shader.hpp"
14
15namespace Sleak {
16namespace RenderEngine {
17
18/// D3D11 vertex+pixel shader pair, compiled from HLSL and paired with an input layout.
19class DirectX11Shader : public Shader {
20 public:
21 DirectX11Shader(ID3D11Device* device);
22 ~DirectX11Shader() override;
23
24 /// Compiles the combined-path convention (vs_main/ps_main entry points) from a single HLSL file.
25 bool compile(const std::string& shaderPath) override;
26 /// Compiles separate vertex and pixel shader HLSL files.
27 bool compile(const std::string& vertPath,
28 const std::string& fragPath) override;
29 void bind() override;
30
31 ID3DBlob* getVertexShaderBlob() const;
32 /// Builds the input layout matching DefaultLayout or SkinnedLayout from the vertex shader bytecode.
33 ID3D11InputLayout* createInputLayout();
34 ID3D11InputLayout* getInputLayout() const { return Layout.Get(); }
35
36 private:
37 /// Compiles one HLSL entry point/profile via D3DCompile into a shader object and bytecode blob.
38 template <typename T>
39 bool compileShader(const std::string& filePath,
40 const std::string& entryPoint,
41 const std::string& profile,
42 Microsoft::WRL::ComPtr<T>& shader,
43 Microsoft::WRL::ComPtr<ID3DBlob>& blob);
44
45 private:
46 Microsoft::WRL::ComPtr<ID3D11Device> m_device;
47 Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_deviceContext;
48
49 Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
50 Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
51 Microsoft::WRL::ComPtr<ID3D11InputLayout> Layout;
52
53 Microsoft::WRL::ComPtr<ID3DBlob>
54 m_vertexShaderBlob; // For input layout creation
55 Microsoft::WRL::ComPtr<ID3DBlob> m_pixelShaderBlob;
56
57 D3D11_INPUT_ELEMENT_DESC DefaultLayout[5] = {
58 // Semantic Index Format Slot Offset Class Step Rate
59 {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
60 {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
61 {"TANGENT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
62 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D11_INPUT_PER_VERTEX_DATA, 0},
63 {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 56, D3D11_INPUT_PER_VERTEX_DATA, 0}
64 };
65
66 D3D11_INPUT_ELEMENT_DESC SkinnedLayout[7] = {
67 {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
68 {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
69 {"TANGENT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
70 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D11_INPUT_PER_VERTEX_DATA, 0},
71 {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 56, D3D11_INPUT_PER_VERTEX_DATA, 0},
72 {"BLENDINDICES", 0, DXGI_FORMAT_R32G32B32A32_SINT, 0, 64, D3D11_INPUT_PER_VERTEX_DATA, 0},
73 {"BLENDWEIGHT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 80, D3D11_INPUT_PER_VERTEX_DATA, 0}
74 };
75};
76
77} // namespace RenderEngine
78} // namespace Sleak
79
80#endif // _DIRECTX11_SHADER_H_
ID3D11InputLayout * createInputLayout()
Builds the input layout matching DefaultLayout or SkinnedLayout from the vertex shader bytecode.
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.
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