Skip to content

Commit

Permalink
Simplified Parameter Parsing to have all all shader formats use the s…
Browse files Browse the repository at this point in the history
…ame slang method
  • Loading branch information
HyperspaceMadness committed Jun 22, 2024
1 parent 7830e8a commit 8759a55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 84 deletions.
23 changes: 18 additions & 5 deletions gfx/drivers_shader/glslang_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ enum slang_texture_semantic slang_name_to_texture_semantic_array(
return SLANG_INVALID_TEXTURE_SEMANTIC;
}

/* Reads a shader file and outputs its contents as a string list.
Takes the path of the shader file and appends each line of the file
to the output string list.
If the root_file argument is set to true, it expects the first line of the file
to be a valid '#version' string
Handles '#include' statements by recursively parsing included files and appending their contents.
Returns a Bool indicating if parsing was successful.
*/

bool glslang_read_shader_file(const char *path,
struct string_list *output, bool root_file)
{
Expand Down Expand Up @@ -150,9 +163,9 @@ bool glslang_read_shader_file(const char *path,
if (lines.size < 1)
goto error;

/* If this is the 'parent' shader file, ensure that first
* line is a 'VERSION' string */
if (root_file)
/* If this is the 'parent' shader file and a slang file,
* ensure that first line is a 'VERSION' string */
if (root_file && (strcmp(path_get_extension(path), "slang") == 0))
{
const char *line = lines.elems[0].data;

Expand All @@ -166,9 +179,9 @@ bool glslang_read_shader_file(const char *path,
if (!string_list_append(output, line, attr))
goto error;

/* Allows us to use #line to make dealing with shader
/* Allows us to use #line to make dealing with shader
* errors easier.
* This is supported by glslang, but since we always
* This is supported by glslang, but since we always
* use glslang statically, this is fine. */
if (!string_list_append(output,
"#extension GL_GOOGLE_cpp_style_line_directive : require",
Expand Down
82 changes: 3 additions & 79 deletions gfx/video_shader_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,86 +876,10 @@ void video_shader_resolve_parameters(struct video_shader *shader)
if (!path_is_valid(path))
continue;

/* First try to use the more robust slang implementation
* to support #includes. */
/* Now uses the same slang parsing for parameters since
* it should be the same implementation, but supporting #include directives */
slang_preprocess_parse_parameters(path, shader);

#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
/* FIXME: The check for slang can be removed
* if it's sufficiently tested for GLSL/Cg as well,
* it should be the same implementation.
* The problem with switching currently is that it looks
* for a #version string in the first line of the file
* which glsl doesn't have */

if ( string_is_equal(path_get_extension(path), "slang")
&& slang_preprocess_parse_parameters(path, shader))
continue;
#endif

/* Read file contents */
if (filestream_read_file(path, (void**)&buf, &buf_len))
{
size_t line_index = 0;
struct string_list lines = {0};
bool lines_inited = false;

/* Split into lines */
if (buf_len > 0)
{
string_list_initialize(&lines);
lines_inited = string_split_noalloc(&lines, (const char*)buf, "\n");
}

/* Buffer is no longer required - clean up */
if ((void*)buf)
free((void*)buf);

if (!lines_inited)
continue;

/* Even though the pass is set in the loop too,
* not all passes have parameters */
param->pass = (int)i;

while ((shader->num_parameters < ARRAY_SIZE(shader->parameters))
&& (line_index < lines.size))
{
int ret;
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 */
if ((ret = sscanf(line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
param->id, param->desc, &param->initial,
&param->minimum, &param->maximum, &param->step)) < 5)
continue;

param->id[63] = '\0';
param->desc[63] = '\0';

if (ret == 5)
param->step = 0.1f * (param->maximum - param->minimum);

param->pass = (int)i;

#ifdef DEBUG
RARCH_DBG("[Shaders]: Found #pragma parameter %s (%s) %f %f %f %f in pass %d.\n",
param->desc, param->id, param->initial,
param->minimum, param->maximum, param->step, param->pass);
#endif
param->current = param->initial;

shader->num_parameters++;
param++;
}

string_list_deinitialize(&lines);
}
}
}

Expand Down

0 comments on commit 8759a55

Please sign in to comment.