-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathShaderCompilation.h
180 lines (142 loc) · 4.99 KB
/
ShaderCompilation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//=================================================================================================
//
// MJP's DX12 Sample Framework
// http://mynameismjp.wordpress.com/
//
// All code licensed under the MIT license
//
//=================================================================================================
#pragma once
#include "..\\PCH.h"
#include "..\\InterfacePointers.h"
#include "..\\Assert.h"
#include "..\\MurmurHash.h"
namespace SampleFramework12
{
class CompileOptions
{
public:
// constants
static const uint32 MaxDefines = 16;
static const uint32 BufferSize = 1024;
CompileOptions();
void Add(const std::string& name, uint32 value);
void Reset();
void MakeDefines(D3D_SHADER_MACRO defines[MaxDefines + 1]) const;
private:
uint32 nameOffsets[MaxDefines];
uint32 defineOffsets[MaxDefines];
char buffer[BufferSize];
uint32 numDefines;
uint32 bufferIdx;
};
enum class ShaderType
{
Vertex = 0,
Hull,
Domain,
Geometry,
Pixel,
Compute,
NumTypes
};
enum class ShaderProfile
{
SM50 = 0,
SM51,
NumProfiles
};
class CompiledShader
{
public:
std::wstring FilePath;
std::string FunctionName;
CompileOptions CompileOpts;
bool ForceOptimization;
ID3DBlobPtr ByteCode;
ShaderType Type;
Hash ByteCodeHash;
CompiledShader(const wchar* filePath, const char* functionName,
const CompileOptions& compileOptions,
bool forceOptimization, ShaderType type) : FilePath(filePath),
FunctionName(functionName),
CompileOpts(compileOptions),
ForceOptimization(forceOptimization),
Type(type)
{
}
};
class CompiledShaderPtr
{
public:
CompiledShaderPtr() : ptr(nullptr)
{
}
CompiledShaderPtr(const CompiledShader* ptr_) : ptr(ptr_)
{
}
const CompiledShader* operator->() const
{
Assert_(ptr != nullptr);
return ptr;
}
const CompiledShader& operator*() const
{
Assert_(ptr != nullptr);
return *ptr;
}
bool Valid() const
{
return ptr != nullptr;
}
D3D12_SHADER_BYTECODE ByteCode() const
{
Assert_(ptr != nullptr);
D3D12_SHADER_BYTECODE byteCode;
byteCode.pShaderBytecode = ptr->ByteCode->GetBufferPointer();
byteCode.BytecodeLength = ptr->ByteCode->GetBufferSize();
return byteCode;
}
private:
const CompiledShader* ptr;
};
typedef CompiledShaderPtr VertexShaderPtr;
typedef CompiledShaderPtr HullShaderPtr;
typedef CompiledShaderPtr DomainShaderPtr;
typedef CompiledShaderPtr GeometryShaderPtr;
typedef CompiledShaderPtr PixelShaderPtr;
typedef CompiledShaderPtr ComputeShaderPtr;
typedef ComputeShaderPtr ShaderPtr;
// Compiles a shader from file and creates the appropriate shader instance
CompiledShaderPtr CompileFromFile(const wchar* path,
const char* functionName,
ShaderType type,
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
VertexShaderPtr CompileVSFromFile(const wchar* path,
const char* functionName = "VS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
PixelShaderPtr CompilePSFromFile(const wchar* path,
const char* functionName = "PS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
GeometryShaderPtr CompileGSFromFile(const wchar* path,
const char* functionName = "GS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
HullShaderPtr CompileHSFromFile(const wchar* path,
const char* functionName = "HS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
DomainShaderPtr CompileDSFromFile(const wchar* path,
const char* functionName = "DS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
ComputeShaderPtr CompileCSFromFile(const wchar* path,
const char* functionName = "CS",
const CompileOptions& compileOpts = CompileOptions(),
bool forceOptimization = false);
bool UpdateShaders();
void ShutdownShaders();
}