-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrecompileShader.py
252 lines (209 loc) · 11.1 KB
/
PrecompileShader.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import subprocess;
import os;
import sys;
import glob;
# Metallicafan212: Go into the subdir
os.chdir("Shaders");
class Macro:
def __init__(self, mac, val):
self.define = mac;
self.value = val;
# Metallicafan212: TODO! Make this automatically synced to the C++ code! Maybe a shared header?
global_macros = [
Macro("EXTRA_VERT_INFO", "1"),
Macro("COMPLEX_SURF_MANUAL_UVs", "1"),
Macro("GM_XOpenGL", "0"),
Macro("GM_DX9", "1"),
Macro("FIRST_USER_CONSTBUFF", "b4"),
]
# Metallicafan212: Defined language version
class LanguageTarget:
def __init__(self, targetName, inMacros = []):
# Metallicafan212: Use a local list so the globals appear first
localList = [];
localList.extend(global_macros);
localList.extend(inMacros);
self.target = targetName;
self.macros = localList;
#print(self.macros);
# Metallicafan212: TODO! Don't hard-code the indexes
global_langages = {
"vs_5_0" : LanguageTarget('vs_5_0', [Macro("USE_GEO_SHADER", "1")]),
"vs_4_1" : LanguageTarget('vs_4_1', [Macro("USE_GEO_SHADER", "1")]),
"vs_4_0" : LanguageTarget('vs_4_0', [Macro("USE_GEO_SHADER", "1")]),
#"vs_4_0_level_9_3" : LanguageTarget('vs_4_0_level_9_3', [Macro("USE_GEO_SHADER", "0")]),
"ps_5_0" : LanguageTarget('ps_5_0', [Macro("USE_GEO_SHADER", "1")]),
"ps_4_1" : LanguageTarget('ps_4_1', [Macro("USE_GEO_SHADER", "1")]),
"ps_4_0" : LanguageTarget('ps_4_0', [Macro("USE_GEO_SHADER", "1")]),
#"ps_4_0_level_9_3" : LanguageTarget('ps_4_0_level_9_3', [Macro("USE_GEO_SHADER", "0")]),
"gs_5_0" : LanguageTarget('gs_5_0', [Macro("USE_GEO_SHADER", "1")]),
"gs_4_1" : LanguageTarget('gs_4_1', [Macro("USE_GEO_SHADER", "1")]),
"gs_4_0" : LanguageTarget('gs_4_0', [Macro("USE_GEO_SHADER", "1")]),
}
class EntryPoint:
def __init__(self, ent, languages):
self.entrypoint = ent;
self.langs = languages;
# Metallicafan212: Define a shader
class Shader:
def __init__(self, key, file, entrypoints):
self.key = key;
self.file = file;
self.entrypoints = entrypoints;
# Metallicafan212: Process all shaders for all specific targets
# TODO! I need specific macros defined in each target
ShaderArray = [
Shader(
"ComplexSurfShader",
"ComplexSurfShader.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]),#global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]),#global_langages["ps_4_0_level_9_3"]]),
]
),
Shader(
"GeneralShader",
"GeneralShader.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]),#global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]),#global_langages["ps_4_0_level_9_3"]]),
]
),
Shader(
"LineShader",
"LineShader.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]), #global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]), #global_langages["ps_4_0_level_9_3"]]),
]
),
Shader(
"Line_GeoShader",
"Line_GeoShader.hlsl",
[
EntryPoint("GeoShader", [global_langages["gs_5_0"], global_langages["gs_4_1"], global_langages["gs_4_0"]]),
]
),
Shader(
"MeshShader",
"MeshShader.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]), #global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]), #global_langages["ps_4_0_level_9_3"]]),
]
),
Shader(
"ResScaling",
"ResScaling.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]), #global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]), #global_langages["ps_4_0_level_9_3"]]),
]
),
Shader(
"TileShader",
"TileShader.hlsl",
[
EntryPoint("VertShader", [global_langages["vs_5_0"], global_langages["vs_4_1"], global_langages["vs_4_0"]]), #global_langages["vs_4_0_level_9_3"]]),
EntryPoint("PxShader", [global_langages["ps_5_0"], global_langages["ps_4_1"], global_langages["ps_4_0"]]), #global_langages["ps_4_0_level_9_3"]]),
]
),
]
# Metallicafan212: We're going to be writing a function to automatically load the hard-coded shader when needed
hardcoded_cpp = "..\\Src\\UnHardcodedShaders.cpp"
newline = "\n";
tabbedLine = newline + "\t";
includes = "// Metallicafan212:\tThis file contains auto generated code to load hardcoded shaders (compiled from fxc.exe)" + newline + "#include \"ICBINDx11Drv.h\"" + newline + newline;
# Metallicafan212: Function code
functionCode = "void FShaderManager::LoadHardcodedShaders()" + newline + "{" + tabbedLine + "guard(FShaderManager::LoadHardcodedShaders);" + tabbedLine + tabbedLine + "TArray<BYTE>* Shad = nullptr;" + tabbedLine + tabbedLine;
endOfFunction = tabbedLine + "unguard;" + newline + "}";
# Metallicafan212: Shader directory
# This'll automatically use the macro in the C++ code, or can be redefined here
shaderFolder = "SHADER_FOLDER ";
'''
void FShaderManager::LoadHardcodedShaders()
{
guard(FShaderManager::LoadHardcodedShaders);
// Metallicafan212: Set the specific slots
TArray<BYTE>* Shad = nullptr;
Shad = &Bytecode.Set(TEXT("Blah"), TArray<BYTE>());
// Metallicafan212: Now copy in
Shad->Add(ARRAY_COUNT(Bytes));
appMemcpy(Shad->GetData(), Bytes, ARRAY_COUNT(Bytes));
unguard;
}
'''
# Metallicafan212: Now that everything is defined, loop through and compile each shader
# Note that the FXC path is hard-coded here because it's not on the path variable
fxc_path = 'C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.22621.0\\x86\\fxc.exe';
# Metallicafan212: Default to fxc_path, and run a detection to see if we're currently running within a vs instance
fxc = fxc_path;
# Metallicafan212: Per AnthraX, check first if we're running from within a VS command prompt, as that'll have the current windows kit on the path variable
#if True:
try:
check_fxc = subprocess.run("fxc /?", text=True, check=False, capture_output=True);
#print("Output was " + check_fxc.stdout);
# Metallicafan212: TODO! Check if it was actually FXC? This would conflict with "future" replacements if they don't output the same info, for now just assume if we got output that it works
if (check_fxc.stdout != ""):
# Metallicafan212: TODO! Reevaluate this, this fucking blows since the env variable adds the \\ onto the end that the folder has... We just want to print the version number.
# Perhaps find a different env var?
fxc_eval = subprocess.run("where fxc", text=True, check=False, capture_output=True).stdout;#.split('\n')[0];
# Metallicafan212: Check for a newline
if('\n' in fxc_eval):
fxc_eval = fxc_eval.split('\n')[0];
# Metallicafan212: Lookup the current windows SDK version (in case it doesn't match the FXC path)
windows_kit = os.environ["WINDOWSSDKVERSION"];
# Metallicafan212: It has a slash for me, so remove it for clarity sake
if('\\' in windows_kit):
windows_kit = windows_kit.split('\\')[0];
print("Found FXC (" + fxc_eval + ") in the current path/path env var, the current windows kit is " + windows_kit);
fxc = 'fxc.exe';
#else:
except:
# Metallicafan212: Wasn't found, so don't override it.
print("FXC not found on the path variable, defaulting to " + fxc_path);
fxc = fxc_path;
for Shad in ShaderArray :
# Metallicafan212: Process this shader
for Entry in Shad.entrypoints:
# Metallicafan212: Now assemble the command
for Lang in Entry.langs:
#print(Lang.target);
args = "";
# Metallicafan212: Save these so we can assemble a namespace for the CPP file
generatedNamespace = Shad.key + "_" + Entry.entrypoint + "_" + Lang.target;
generatedShader = generatedNamespace + ".h";
# Metallicafan212: TODO! Should probably make it so that the code in the driver doesn't expect a real path, and instead relies just on the filename
generatedKey = shaderFolder + "TEXT(\"" + Shad.file + ":" + Entry.entrypoint + ":" + Lang.target + "\")";
generatedBytes = generatedNamespace + "::g_" + Entry.entrypoint;
compileCommand = [
fxc,
"/T" + Lang.target,
"/E" + Entry.entrypoint,
"/Fh..\\Inc\\CompiledShaders\\" + generatedShader
];
# Metallicafan212: Now add all the macros
for Mac in Lang.macros:
# Metallicafan212: The online documentation says that you do blah=dah, but it's wrong for some reason
compileCommand.append('/D' + Mac.define + "=" + Mac.value + '');
compileCommand.append(Shad.file);
#print(compileCommand);
process = subprocess.run(compileCommand);
if(process.returncode == 0):
# Metallicafan212: Now add onto the includes
includes += "namespace " + generatedNamespace + newline + "{" + tabbedLine + "#include \"CompiledShaders\\" + generatedShader + "\"" + newline + "}" + newline + newline;
# Metallicafan212: Add the code to set the data
functionCode += "// Metallicafan212: Load " + generatedNamespace + tabbedLine;
functionCode += "Shad = &Bytecode.Set(" + generatedKey + ", TArray<BYTE>());" + tabbedLine;
functionCode += "Shad->Add(ARRAY_COUNT(" + generatedBytes + "));" + tabbedLine + tabbedLine;
functionCode += "appMemcpy(Shad->GetData(), " + generatedBytes + ", ARRAY_COUNT(" + generatedBytes + "));"
functionCode += tabbedLine + tabbedLine;
print(includes);
print(functionCode);
print(endOfFunction);
# Metallicafan212: Write it to a file
with open(hardcoded_cpp, "w+") as file:
file.write(includes);
file.write(functionCode);
file.write(endOfFunction);
file.close();