Skip to content

Commit 9b23a81

Browse files
committed
switch to libretro-common functions when possible
1 parent b16165d commit 9b23a81

1 file changed

Lines changed: 42 additions & 50 deletions

File tree

gfx/drivers_shader/slang_cache.cpp

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
#include "glslang_util.h"
33
#include "slang_process.h"
44

5-
#include <stdio.h>
65
#include <stdlib.h>
76
#include <string.h>
87
#include <file/file_path.h>
8+
#include <streams/file_stream.h>
9+
#include <vfs/vfs.h>
910
#include <compat/strl.h>
10-
#include <sys/stat.h>
1111
#include <lrc_hash.h>
1212

13-
#if defined(_WIN32)
14-
#include <direct.h>
15-
#endif
16-
1713
#include "../../configuration.h"
1814
#include "../../verbosity.h"
1915

@@ -53,13 +49,7 @@ static bool spirv_cache_ensure_dir(void)
5349
if (!spirv_cache_get_dir(cache_dir, sizeof(cache_dir)))
5450
return false;
5551

56-
#if defined(_WIN32)
57-
mkdir(cache_dir);
58-
#else
59-
mkdir(cache_dir, 0755);
60-
#endif
61-
62-
return true;
52+
return path_mkdir(cache_dir);
6353
}
6454

6555
/**
@@ -91,14 +81,14 @@ static bool spirv_cache_get_filename(const char *hash,
9181
* @param str String to write (may be NULL or empty)
9282
* @return true on success, false on error
9383
*/
94-
static bool spirv_cache_write_string(FILE *file, const std::string &str)
84+
static bool spirv_cache_write_string(RFILE *file, const std::string &str)
9585
{
9686
uint32_t len = str.length();
9787

98-
if (fwrite(&len, sizeof(uint32_t), 1, file) != 1)
88+
if (filestream_write(file, &len, sizeof(uint32_t)) != sizeof(uint32_t))
9989
return false;
10090

101-
if (len > 0 && fwrite(str.c_str(), 1, len, file) != len)
91+
if (len > 0 && filestream_write(file, str.c_str(), len) != len)
10292
return false;
10393

10494
return true;
@@ -111,11 +101,11 @@ static bool spirv_cache_write_string(FILE *file, const std::string &str)
111101
* @param str_out Output string
112102
* @return true on success, false on error
113103
*/
114-
static bool spirv_cache_read_string(FILE *file, std::string &str_out)
104+
static bool spirv_cache_read_string(RFILE *file, std::string &str_out)
115105
{
116106
uint32_t len;
117107

118-
if (fread(&len, sizeof(uint32_t), 1, file) != 1)
108+
if (filestream_read(file, &len, sizeof(uint32_t)) != sizeof(uint32_t))
119109
return false;
120110

121111
if (len == 0)
@@ -129,7 +119,7 @@ static bool spirv_cache_read_string(FILE *file, std::string &str_out)
129119
if (!buf)
130120
return false;
131121

132-
if (fread(buf, 1, len, file) != len)
122+
if (filestream_read(file, buf, len) != len)
133123
{
134124
delete[] buf;
135125
return false;
@@ -172,7 +162,7 @@ bool spirv_cache_compute_hash(const char *vertex_source, const char *fragment_so
172162

173163
bool spirv_cache_load(const char *hash, struct glslang_output *output)
174164
{
175-
FILE *file;
165+
RFILE *file;
176166
char cache_file[PATH_MAX_LENGTH];
177167
uint8_t version;
178168
uint32_t vertex_size, fragment_size, param_count, i;
@@ -184,41 +174,42 @@ bool spirv_cache_load(const char *hash, struct glslang_output *output)
184174
if (!spirv_cache_get_filename(hash, cache_file, sizeof(cache_file)))
185175
return false;
186176

187-
file = fopen(cache_file, "rb");
177+
file = filestream_open(cache_file, RETRO_VFS_FILE_ACCESS_READ,
178+
RETRO_VFS_FILE_ACCESS_HINT_NONE);
188179
if (!file)
189180
return false; /* Cache file doesn't exist yet */
190181

191182
/* Read version */
192-
if (fread(&version, sizeof(uint8_t), 1, file) != 1)
183+
if (filestream_read(file, &version, sizeof(uint8_t)) != sizeof(uint8_t))
193184
goto error;
194185

195186
if (version != SPIRV_CACHE_VERSION)
196187
goto error; /* Version mismatch */
197188

198189
/* Read vertex SPIR-V */
199-
if (fread(&vertex_size, sizeof(uint32_t), 1, file) != 1)
190+
if (filestream_read(file, &vertex_size, sizeof(uint32_t)) != sizeof(uint32_t))
200191
goto error;
201192

202193
if (vertex_size > 0)
203194
{
204195
output->vertex.resize(vertex_size);
205-
if (fread(output->vertex.data(), sizeof(uint32_t), vertex_size, file) != vertex_size)
196+
if (filestream_read(file, output->vertex.data(), vertex_size * sizeof(uint32_t)) != (int64_t)(vertex_size * sizeof(uint32_t)))
206197
goto error;
207198
}
208199

209200
/* Read fragment SPIR-V */
210-
if (fread(&fragment_size, sizeof(uint32_t), 1, file) != 1)
201+
if (filestream_read(file, &fragment_size, sizeof(uint32_t)) != sizeof(uint32_t))
211202
goto error;
212203

213204
if (fragment_size > 0)
214205
{
215206
output->fragment.resize(fragment_size);
216-
if (fread(output->fragment.data(), sizeof(uint32_t), fragment_size, file) != fragment_size)
207+
if (filestream_read(file, output->fragment.data(), fragment_size * sizeof(uint32_t)) != (int64_t)(fragment_size * sizeof(uint32_t)))
217208
goto error;
218209
}
219210

220211
/* Read parameters count */
221-
if (fread(&param_count, sizeof(uint32_t), 1, file) != 1)
212+
if (filestream_read(file, &param_count, sizeof(uint32_t)) != sizeof(uint32_t))
222213
goto error;
223214

224215
if (param_count > 0)
@@ -235,13 +226,13 @@ bool spirv_cache_load(const char *hash, struct glslang_output *output)
235226
if (!spirv_cache_read_string(file, param.desc))
236227
goto error;
237228

238-
if (fread(&param.initial, sizeof(float), 1, file) != 1)
229+
if (filestream_read(file, &param.initial, sizeof(float)) != sizeof(float))
239230
goto error;
240-
if (fread(&param.minimum, sizeof(float), 1, file) != 1)
231+
if (filestream_read(file, &param.minimum, sizeof(float)) != sizeof(float))
241232
goto error;
242-
if (fread(&param.maximum, sizeof(float), 1, file) != 1)
233+
if (filestream_read(file, &param.maximum, sizeof(float)) != sizeof(float))
243234
goto error;
244-
if (fread(&param.step, sizeof(float), 1, file) != 1)
235+
if (filestream_read(file, &param.step, sizeof(float)) != sizeof(float))
245236
goto error;
246237
}
247238

@@ -250,24 +241,24 @@ bool spirv_cache_load(const char *hash, struct glslang_output *output)
250241
goto error;
251242

252243
/* Read render target format */
253-
if (fread(&rt_format, sizeof(uint16_t), 1, file) != 1)
244+
if (filestream_read(file, &rt_format, sizeof(uint16_t)) != sizeof(uint16_t))
254245
goto error;
255246
output->meta.rt_format = (enum glslang_format)rt_format;
256247

257-
fclose(file);
248+
filestream_close(file);
258249

259250
RARCH_LOG("[Slang Cache] Loaded shader cache for hash: %.16s...\n", hash);
260251

261252
return true;
262253

263254
error:
264-
fclose(file);
255+
filestream_close(file);
265256
return false;
266257
}
267258

268259
bool spirv_cache_save(const char *hash, const struct glslang_output *output)
269260
{
270-
FILE *file;
261+
RFILE *file;
271262
char cache_file[PATH_MAX_LENGTH];
272263
uint8_t version = SPIRV_CACHE_VERSION;
273264
uint32_t vertex_size, fragment_size, param_count, i;
@@ -283,37 +274,38 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
283274
if (!spirv_cache_get_filename(hash, cache_file, sizeof(cache_file)))
284275
return false;
285276

286-
file = fopen(cache_file, "wb");
277+
file = filestream_open(cache_file, RETRO_VFS_FILE_ACCESS_WRITE,
278+
RETRO_VFS_FILE_ACCESS_HINT_NONE);
287279
if (!file)
288280
return false;
289281

290282
/* Write version */
291-
if (fwrite(&version, sizeof(uint8_t), 1, file) != 1)
283+
if (filestream_write(file, &version, sizeof(uint8_t)) != sizeof(uint8_t))
292284
goto error;
293285

294286
/* Write vertex SPIR-V */
295287
vertex_size = output->vertex.size();
296-
if (fwrite(&vertex_size, sizeof(uint32_t), 1, file) != 1)
288+
if (filestream_write(file, &vertex_size, sizeof(uint32_t)) != sizeof(uint32_t))
297289
goto error;
298290
if (vertex_size > 0)
299291
{
300-
if (fwrite(output->vertex.data(), sizeof(uint32_t), vertex_size, file) != vertex_size)
292+
if (filestream_write(file, output->vertex.data(), vertex_size * sizeof(uint32_t)) != (int64_t)(vertex_size * sizeof(uint32_t)))
301293
goto error;
302294
}
303295

304296
/* Write fragment SPIR-V */
305297
fragment_size = output->fragment.size();
306-
if (fwrite(&fragment_size, sizeof(uint32_t), 1, file) != 1)
298+
if (filestream_write(file, &fragment_size, sizeof(uint32_t)) != sizeof(uint32_t))
307299
goto error;
308300
if (fragment_size > 0)
309301
{
310-
if (fwrite(output->fragment.data(), sizeof(uint32_t), fragment_size, file) != fragment_size)
302+
if (filestream_write(file, output->fragment.data(), fragment_size * sizeof(uint32_t)) != (int64_t)(fragment_size * sizeof(uint32_t)))
311303
goto error;
312304
}
313305

314306
/* Write parameters */
315307
param_count = output->meta.parameters.size();
316-
if (fwrite(&param_count, sizeof(uint32_t), 1, file) != 1)
308+
if (filestream_write(file, &param_count, sizeof(uint32_t)) != sizeof(uint32_t))
317309
goto error;
318310

319311
for (i = 0; i < param_count; i++)
@@ -325,13 +317,13 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
325317
if (!spirv_cache_write_string(file, param.desc))
326318
goto error;
327319

328-
if (fwrite(&param.initial, sizeof(float), 1, file) != 1)
320+
if (filestream_write(file, &param.initial, sizeof(float)) != sizeof(float))
329321
goto error;
330-
if (fwrite(&param.minimum, sizeof(float), 1, file) != 1)
322+
if (filestream_write(file, &param.minimum, sizeof(float)) != sizeof(float))
331323
goto error;
332-
if (fwrite(&param.maximum, sizeof(float), 1, file) != 1)
324+
if (filestream_write(file, &param.maximum, sizeof(float)) != sizeof(float))
333325
goto error;
334-
if (fwrite(&param.step, sizeof(float), 1, file) != 1)
326+
if (filestream_write(file, &param.step, sizeof(float)) != sizeof(float))
335327
goto error;
336328
}
337329

@@ -341,18 +333,18 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
341333

342334
/* Write render target format */
343335
rt_format = (uint16_t)output->meta.rt_format;
344-
if (fwrite(&rt_format, sizeof(uint16_t), 1, file) != 1)
336+
if (filestream_write(file, &rt_format, sizeof(uint16_t)) != sizeof(uint16_t))
345337
goto error;
346338

347-
fclose(file);
339+
filestream_close(file);
348340

349341
RARCH_LOG("[Slang Cache] Saved shader cache for hash: %.16s...\n", hash);
350342

351343
return true;
352344

353345
error:
354-
fclose(file);
355-
remove(cache_file); /* Clean up partial file on error */
346+
filestream_close(file);
347+
filestream_delete(cache_file); /* Clean up partial file on error */
356348
return false;
357349
}
358350

0 commit comments

Comments
 (0)