-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathslang_cache.cpp
More file actions
356 lines (282 loc) · 9.7 KB
/
slang_cache.cpp
File metadata and controls
356 lines (282 loc) · 9.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "slang_cache.h"
#include "glslang_util.h"
#include "slang_process.h"
#include <stdlib.h>
#include <string.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <vfs/vfs.h>
#include <compat/strl.h>
#include <lrc_hash.h>
#include "../../configuration.h"
#include "../../verbosity.h"
#define SPIRV_CACHE_VERSION 1
#define SPIRV_CACHE_SUBDIR "spirv"
/**
* Get the full path to the SPIR-V cache directory
*
* @param cache_dir_out Output buffer for the cache directory path (must be at least PATH_MAX_LENGTH)
* @param cache_dir_out_len Size of the output buffer
* @return true on success, false if cache dir is not configured
*/
static bool spirv_cache_get_dir(char *cache_dir_out, size_t cache_dir_out_len)
{
settings_t *settings = config_get_ptr();
if (!settings || !settings->paths.directory_cache[0])
return false;
/* Build the spirv subdirectory path */
snprintf(cache_dir_out, cache_dir_out_len, "%s/%s",
settings->paths.directory_cache, SPIRV_CACHE_SUBDIR);
return true;
}
/**
* Ensure the SPIR-V cache directory exists
*
* @return true if directory exists or was created, false on error
*/
static bool spirv_cache_ensure_dir(void)
{
char cache_dir[PATH_MAX_LENGTH];
if (!spirv_cache_get_dir(cache_dir, sizeof(cache_dir)))
return false;
return path_mkdir(cache_dir);
}
/**
* Get the full path to a cache file for a given hash
*
* @param hash Hash string (64 characters)
* @param cache_file_out Output buffer for the full cache file path
* @param cache_file_out_len Size of the output buffer
* @return true on success, false on error
*/
static bool spirv_cache_get_filename(const char *hash,
char *cache_file_out, size_t cache_file_out_len)
{
char cache_dir[PATH_MAX_LENGTH];
int ret;
if (!spirv_cache_get_dir(cache_dir, sizeof(cache_dir)))
return false;
ret = snprintf(cache_file_out, cache_file_out_len, "%s/%s.spirv",
cache_dir, hash);
/* Check if snprintf truncated the output */
if (ret < 0 || (size_t)ret >= cache_file_out_len)
return false;
return true;
}
/**
* Write a null-terminated string to a file with length prefix
*
* @param file File pointer (opened in binary mode)
* @param str String to write (may be NULL or empty)
* @return true on success, false on error
*/
static bool spirv_cache_write_string(RFILE *file, const std::string &str)
{
uint32_t len = str.length();
if (filestream_write(file, &len, sizeof(uint32_t)) != sizeof(uint32_t))
return false;
if (len > 0 && filestream_write(file, str.c_str(), len) != len)
return false;
return true;
}
/**
* Read a null-terminated string from a file
*
* @param file File pointer (opened in binary mode)
* @param str_out Output string
* @return true on success, false on error
*/
static bool spirv_cache_read_string(RFILE *file, std::string &str_out)
{
uint32_t len;
if (filestream_read(file, &len, sizeof(uint32_t)) != sizeof(uint32_t))
return false;
if (len == 0)
{
str_out.clear();
return true;
}
/* Allocate and read string */
char *buf = new char[len + 1];
if (!buf)
return false;
if (filestream_read(file, buf, len) != len)
{
delete[] buf;
return false;
}
buf[len] = '\0';
str_out = buf;
delete[] buf;
return true;
}
extern "C" {
bool spirv_cache_compute_hash(const char *vertex_source, const char *fragment_source,
char *hash_out)
{
if (!vertex_source || !fragment_source || !hash_out)
return false;
/* Build combined hash input: vertex + "|" + fragment */
size_t vertex_len = strlen(vertex_source);
size_t fragment_len = strlen(fragment_source);
size_t total_len = vertex_len + 1 + fragment_len; /* 1 for "|" separator */
uint8_t *combined = new uint8_t[total_len];
if (!combined)
return false;
memcpy(combined, vertex_source, vertex_len);
combined[vertex_len] = '|';
memcpy(combined + vertex_len + 1, fragment_source, fragment_len);
/* Compute SHA256 hash using libretro-common */
sha256_hash(hash_out, combined, total_len);
delete[] combined;
return true;
}
bool spirv_cache_load(const char *hash, struct glslang_output *output)
{
RFILE *file;
char cache_file[PATH_MAX_LENGTH];
uint8_t version;
uint32_t vertex_size, fragment_size, param_count, i;
uint16_t rt_format;
if (!hash || !output)
return false;
if (!spirv_cache_get_filename(hash, cache_file, sizeof(cache_file)))
return false;
file = filestream_open(cache_file, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!file)
return false; /* Cache file doesn't exist yet */
/* Read version */
if (filestream_read(file, &version, sizeof(uint8_t)) != sizeof(uint8_t))
goto error;
if (version != SPIRV_CACHE_VERSION)
goto error; /* Version mismatch */
/* Read vertex SPIR-V */
if (filestream_read(file, &vertex_size, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
if (vertex_size > 0)
{
output->vertex.resize(vertex_size);
if (filestream_read(file, output->vertex.data(), vertex_size * sizeof(uint32_t)) != (int64_t)(vertex_size * sizeof(uint32_t)))
goto error;
}
/* Read fragment SPIR-V */
if (filestream_read(file, &fragment_size, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
if (fragment_size > 0)
{
output->fragment.resize(fragment_size);
if (filestream_read(file, output->fragment.data(), fragment_size * sizeof(uint32_t)) != (int64_t)(fragment_size * sizeof(uint32_t)))
goto error;
}
/* Read parameters count */
if (filestream_read(file, ¶m_count, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
if (param_count > 0)
output->meta.parameters.resize(param_count);
/* Read each parameter */
for (i = 0; i < param_count; i++)
{
glslang_parameter ¶m = output->meta.parameters[i];
if (!spirv_cache_read_string(file, param.id))
goto error;
if (!spirv_cache_read_string(file, param.desc))
goto error;
if (filestream_read(file, ¶m.initial, sizeof(float)) != sizeof(float))
goto error;
if (filestream_read(file, ¶m.minimum, sizeof(float)) != sizeof(float))
goto error;
if (filestream_read(file, ¶m.maximum, sizeof(float)) != sizeof(float))
goto error;
if (filestream_read(file, ¶m.step, sizeof(float)) != sizeof(float))
goto error;
}
/* Read shader name */
if (!spirv_cache_read_string(file, output->meta.name))
goto error;
/* Read render target format */
if (filestream_read(file, &rt_format, sizeof(uint16_t)) != sizeof(uint16_t))
goto error;
output->meta.rt_format = (enum glslang_format)rt_format;
filestream_close(file);
RARCH_LOG("[Slang Cache] Loaded shader cache for hash: %.16s...\n", hash);
return true;
error:
filestream_close(file);
return false;
}
bool spirv_cache_save(const char *hash, const struct glslang_output *output)
{
RFILE *file;
char cache_file[PATH_MAX_LENGTH];
uint8_t version = SPIRV_CACHE_VERSION;
uint32_t vertex_size, fragment_size, param_count, i;
uint16_t rt_format;
if (!hash || !output)
return false;
/* Ensure cache directory exists */
if (!spirv_cache_ensure_dir())
return false;
if (!spirv_cache_get_filename(hash, cache_file, sizeof(cache_file)))
return false;
file = filestream_open(cache_file, RETRO_VFS_FILE_ACCESS_WRITE,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!file)
return false;
/* Write version */
if (filestream_write(file, &version, sizeof(uint8_t)) != sizeof(uint8_t))
goto error;
/* Write vertex SPIR-V */
vertex_size = output->vertex.size();
if (filestream_write(file, &vertex_size, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
if (vertex_size > 0)
{
if (filestream_write(file, output->vertex.data(), vertex_size * sizeof(uint32_t)) != (int64_t)(vertex_size * sizeof(uint32_t)))
goto error;
}
/* Write fragment SPIR-V */
fragment_size = output->fragment.size();
if (filestream_write(file, &fragment_size, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
if (fragment_size > 0)
{
if (filestream_write(file, output->fragment.data(), fragment_size * sizeof(uint32_t)) != (int64_t)(fragment_size * sizeof(uint32_t)))
goto error;
}
/* Write parameters */
param_count = output->meta.parameters.size();
if (filestream_write(file, ¶m_count, sizeof(uint32_t)) != sizeof(uint32_t))
goto error;
for (i = 0; i < param_count; i++)
{
const glslang_parameter ¶m = output->meta.parameters[i];
if (!spirv_cache_write_string(file, param.id))
goto error;
if (!spirv_cache_write_string(file, param.desc))
goto error;
if (filestream_write(file, ¶m.initial, sizeof(float)) != sizeof(float))
goto error;
if (filestream_write(file, ¶m.minimum, sizeof(float)) != sizeof(float))
goto error;
if (filestream_write(file, ¶m.maximum, sizeof(float)) != sizeof(float))
goto error;
if (filestream_write(file, ¶m.step, sizeof(float)) != sizeof(float))
goto error;
}
/* Write shader name */
if (!spirv_cache_write_string(file, output->meta.name))
goto error;
/* Write render target format */
rt_format = (uint16_t)output->meta.rt_format;
if (filestream_write(file, &rt_format, sizeof(uint16_t)) != sizeof(uint16_t))
goto error;
filestream_close(file);
RARCH_LOG("[Slang Cache] Saved shader cache for hash: %.16s...\n", hash);
return true;
error:
filestream_close(file);
filestream_delete(cache_file); /* Clean up partial file on error */
return false;
}
} /* extern "C" */