forked from waskord/UnleashedRecomp_Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_main.py
More file actions
255 lines (196 loc) · 9.48 KB
/
Copy pathupdate_main.py
File metadata and controls
255 lines (196 loc) · 9.48 KB
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
253
254
255
import os
with open('build_overrides/tools/XenosRecomp/XenosRecomp/main.cpp', 'r') as f:
content = f.read()
# Replace RecompiledShader struct
recompiled_shader_old = """struct RecompiledShader
{
uint8_t* data = nullptr;
IDxcBlob* dxil = nullptr;
std::vector<uint8_t> spirv;
uint32_t specConstantsMask = 0;
};"""
recompiled_shader_new = """struct RecompiledShader
{
std::vector<uint8_t> shaderData;
uint8_t* data = nullptr;
IDxcBlob* dxil = nullptr;
std::vector<uint8_t> spirv;
uint32_t specConstantsMask = 0;
};"""
content = content.replace(recompiled_shader_old, recompiled_shader_new)
# Replace loop and add helper
helper_func = """
static void writeByteArray(std::ofstream& out, const std::string& name, const std::vector<uint8_t>& data)
{
fmt::print(out, "const uint8_t {}[] = {{", name);
for (size_t i = 0; i < data.size(); ++i)
{
if (i % 16 == 0) fmt::print(out, "\n\t");
fmt::print(out, "0x{:02X}, ", data[i]);
}
fmt::println(out, "\n}};");
}
"""
# Find the location to insert helper_func (after writeAllBytes)
insertion_point = content.find('static void writeAllBytes')
insertion_point = content.find('}', insertion_point) + 1
content = content[:insertion_point] + helper_func + content[insertion_point:]
# Refactor the loop
old_loop = """ if (std::filesystem::is_directory(input))
{
std::vector<std::unique_ptr<uint8_t[]>> files;
std::map<XXH64_hash_t, RecompiledShader> shaders;
for (auto& file : std::filesystem::recursive_directory_iterator(input))
{
if (std::filesystem::is_directory(file))
{
continue;
}
size_t fileSize = 0;
auto fileData = readAllBytes(file.path().string().c_str(), fileSize);
bool foundAny = false;
for (size_t i = 0; fileSize > sizeof(ShaderContainer) && i < fileSize - sizeof(ShaderContainer) - 1;)
{
auto shaderContainer = reinterpret_cast<const ShaderContainer*>(fileData.get() + i);
size_t dataSize = shaderContainer->virtualSize + shaderContainer->physicalSize;
if ((shaderContainer->flags & 0xFFFFFF00) == 0x102A1100 &&
dataSize <= (fileSize - i) &&
shaderContainer->field1C == 0 &&
shaderContainer->field20 == 0)
{
XXH64_hash_t hash = XXH3_64bits(shaderContainer, dataSize);
auto shader = shaders.try_emplace(hash);
if (shader.second)
{
shader.first->second.data = fileData.get() + i;
foundAny = true;
}
i += dataSize;
}
else
{
i += sizeof(uint32_t);
}
}
if (foundAny)
files.emplace_back(std::move(fileData));
}"""
new_loop = """ if (std::filesystem::is_directory(input))
{
std::map<XXH64_hash_t, RecompiledShader> shaders;
for (auto& file : std::filesystem::recursive_directory_iterator(input))
{
if (std::filesystem::is_directory(file))
{
continue;
}
size_t fileSize = 0;
auto fileData = readAllBytes(file.path().string().c_str(), fileSize);
if (!fileData) continue;
for (size_t i = 0; fileSize > sizeof(ShaderContainer) && i < fileSize - sizeof(ShaderContainer) - 1;)
{
auto shaderContainer = reinterpret_cast<const ShaderContainer*>(fileData.get() + i);
size_t dataSize = shaderContainer->virtualSize + shaderContainer->physicalSize;
if ((shaderContainer->flags & 0xFFFFFF00) == 0x102A1100 &&
dataSize <= (fileSize - i) &&
shaderContainer->field1C == 0 &&
shaderContainer->field20 == 0)
{
XXH64_hash_t hash = XXH3_64bits(shaderContainer, dataSize);
auto shader = shaders.try_emplace(hash);
if (shader.second)
{
shader.first->second.shaderData.assign(fileData.get() + i, fileData.get() + i + dataSize);
shader.first->second.data = shader.first->second.shaderData.data();
}
i += dataSize;
}
else
{
i += sizeof(uint32_t);
}
}
}"""
content = content.replace(old_loop, new_loop)
# Replace StringBuffer usage
old_buffer_logic = """ fmt::println("Creating shader cache...");
StringBuffer f;
f.println("#include \"shader_cache.h\"");
f.println("ShaderCacheEntry g_shaderCacheEntries[] = {{");
std::vector<uint8_t> dxil;
std::vector<uint8_t> spirv;
for (auto& [hash, shader] : shaders)
{
f.println("\t{{ 0x{:X}, {}, {}, {}, {}, {} }},",
hash, dxil.size(), (shader.dxil != nullptr) ? shader.dxil->GetBufferSize() : 0, spirv.size(), shader.spirv.size(), shader.specConstantsMask);
if (shader.dxil != nullptr)
{
dxil.insert(dxil.end(), reinterpret_cast<uint8_t *>(shader.dxil->GetBufferPointer()),
reinterpret_cast<uint8_t *>(shader.dxil->GetBufferPointer()) + shader.dxil->GetBufferSize());
}
spirv.insert(spirv.end(), shader.spirv.begin(), shader.spirv.end());
}
f.println("}};");
fmt::println("Compressing DXIL cache...");
int level = ZSTD_maxCLevel();
#ifdef XENOS_RECOMP_DXIL
std::vector<uint8_t> dxilCompressed(ZSTD_compressBound(dxil.size()));
dxilCompressed.resize(ZSTD_compress(dxilCompressed.data(), dxilCompressed.size(), dxil.data(), dxil.size(), level));
f.print("const uint8_t g_compressedDxilCache[] = {{");
for (auto data : dxilCompressed)
f.print("{},", data);
f.println("}};");
f.println("const size_t g_dxilCacheCompressedSize = {};", dxilCompressed.size());
f.println("const size_t g_dxilCacheDecompressedSize = {};", dxil.size());
#endif
fmt::println("Compressing SPIRV cache...");
std::vector<uint8_t> spirvCompressed(ZSTD_compressBound(spirv.size()));
spirvCompressed.resize(ZSTD_compress(spirvCompressed.data(), spirvCompressed.size(), spirv.data(), spirv.size(), level));
f.print("const uint8_t g_compressedSpirvCache[] = {{");
for (auto data : spirvCompressed)
f.print("{},", data);
f.println("}};");
f.println("const size_t g_spirvCacheCompressedSize = {};", spirvCompressed.size());
f.println("const size_t g_spirvCacheDecompressedSize = {};", spirv.size());
f.println("const size_t g_shaderCacheEntryCount = {};", shaders.size());
writeAllBytes(output, f.out.data(), f.out.size());"""
new_buffer_logic = """ fmt::println("Creating shader cache...");
std::ofstream outFile(output, std::ios::binary);
fmt::println(outFile, "#include \"shader_cache.h\"");
fmt::println(outFile, "ShaderCacheEntry g_shaderCacheEntries[] = {{");
std::vector<uint8_t> dxil;
std::vector<uint8_t> spirv;
for (auto& [hash, shader] : shaders)
{
fmt::println(outFile, "\t{{ 0x{:X}, {}, {}, {}, {}, {} }},",
hash, dxil.size(), (shader.dxil != nullptr) ? shader.dxil->GetBufferSize() : 0, spirv.size(), shader.spirv.size(), shader.specConstantsMask);
if (shader.dxil != nullptr)
{
dxil.insert(dxil.end(), reinterpret_cast<uint8_t *>(shader.dxil->GetBufferPointer()),
reinterpret_cast<uint8_t *>(shader.dxil->GetBufferPointer()) + shader.dxil->GetBufferSize());
}
spirv.insert(spirv.end(), shader.spirv.begin(), shader.spirv.end());
}
fmt::println(outFile, "}};");
fmt::println("Compressing DXIL cache...");
int level = ZSTD_maxCLevel();
#ifdef XENOS_RECOMP_DXIL
std::vector<uint8_t> dxilCompressed(ZSTD_compressBound(dxil.size()));
dxilCompressed.resize(ZSTD_compress(dxilCompressed.data(), dxilCompressed.size(), dxil.data(), dxil.size(), level));
writeByteArray(outFile, "g_compressedDxilCache", dxilCompressed);
fmt::println(outFile, "const size_t g_dxilCacheCompressedSize = {};", dxilCompressed.size());
fmt::println(outFile, "const size_t g_dxilCacheDecompressedSize = {};", dxil.size());
#endif
fmt::println("Compressing SPIRV cache...");
std::vector<uint8_t> spirvCompressed(ZSTD_compressBound(spirv.size()));
spirvCompressed.resize(ZSTD_compress(spirvCompressed.data(), spirvCompressed.size(), spirv.data(), spirv.size(), level));
writeByteArray(outFile, "g_compressedSpirvCache", spirvCompressed);
fmt::println(outFile, "const size_t g_spirvCacheCompressedSize = {};", spirvCompressed.size());
fmt::println(outFile, "const size_t g_spirvCacheDecompressedSize = {};", spirv.size());
fmt::println(outFile, "const size_t g_shaderCacheEntryCount = {};", shaders.size());"""
content = content.replace(old_buffer_logic, new_buffer_logic)
# Add #include <fstream>
if '#include <fstream>' not in content:
content = "#include <fstream>\n" + content
with open('build_overrides/tools/XenosRecomp/XenosRecomp/main.cpp', 'w') as f:
f.write(content)