(video_shader_parse) Optimise parsing of (non-slang) shader parameters

This commit is contained in:
jdgleaver 2019-08-07 17:19:18 +01:00
parent 708a87247d
commit bcfc5db7d8

View File

@ -28,7 +28,7 @@
#include <file/file_path.h> #include <file/file_path.h>
#include <rhash.h> #include <rhash.h>
#include <string/stdstring.h> #include <string/stdstring.h>
#include <streams/interface_stream.h> #include <streams/file_stream.h>
#include <lists/string_list.h> #include <lists/string_list.h>
#include "../configuration.h" #include "../configuration.h"
@ -484,13 +484,17 @@ bool video_shader_resolve_parameters(config_file_t *conf,
for (i = 0; i < shader->passes; i++) for (i = 0; i < shader->passes; i++)
{ {
intfstream_t *file = NULL; const char *path = shader->pass[i].source.path;
size_t line_size = 4096 * sizeof(char); uint8_t *buf = NULL;
char *line = NULL; int64_t buf_len = 0;
const char *path = shader->pass[i].source.path; struct string_list *lines = NULL;
size_t line_index = 0;
if (string_is_empty(path)) if (string_is_empty(path))
continue; continue;
if (!path_is_valid(path))
continue;
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS) #if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
/* First try to use the more robust slang /* First try to use the more robust slang
@ -505,24 +509,38 @@ bool video_shader_resolve_parameters(config_file_t *conf,
/* If that doesn't work, fallback to the old path. /* If that doesn't work, fallback to the old path.
* Ideally, we'd get rid of this path sooner or later. */ * Ideally, we'd get rid of this path sooner or later. */
#endif #endif
file = intfstream_open_file(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!file) /* Read file contents */
if (!filestream_read_file(path, (void**)&buf, &buf_len))
continue; continue;
line = (char*)malloc(line_size); /* Split into lines */
line[0] = '\0'; if (buf_len > 0)
lines = string_split((const char*)buf, "\n");
/* Buffer is no longer required - clean up */
if ((void*)buf)
free((void*)buf);
if (!lines)
continue;
/* even though the pass is set in the loop too, not all passes have parameters */ /* even though the pass is set in the loop too, not all passes have parameters */
param->pass = i; param->pass = i;
while (shader->num_parameters < ARRAY_SIZE(shader->parameters) while ((shader->num_parameters < ARRAY_SIZE(shader->parameters)) &&
&& intfstream_gets(file, line, line_size)) (line_index < lines->size))
{ {
int ret = sscanf(line, int ret;
"#pragma parameter %63s \"%63[^\"]\" %f %f %f %f", const char *line = lines->elems[line_index].data;
line_index++;
/* Check if this is a '#pragma parameter' line */
if (strncmp("#pragma parameter", line, STRLEN_CONST("#pragma parameter")))
continue;
/* Parse line */
ret = sscanf(line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
param->id, param->desc, &param->initial, param->id, param->desc, &param->initial,
&param->minimum, &param->maximum, &param->step); &param->minimum, &param->maximum, &param->step);
@ -546,9 +564,7 @@ bool video_shader_resolve_parameters(config_file_t *conf,
param++; param++;
} }
free(line); string_list_free(lines);
intfstream_close(file);
free(file);
} }
if (conf && !video_shader_resolve_current_parameters(conf, shader)) if (conf && !video_shader_resolve_current_parameters(conf, shader))