Skip to content

Commit

Permalink
slang shaders: add support for __has_include like C++17 macro. (#17109)
Browse files Browse the repository at this point in the history
* slang shaders: add support for __has_include like C++17 macro.
This adds a new #pragma include_if_exist "filename" directive that acts like #include statements,
but does not return error if the file does not exists.

* removed unuseful define
  • Loading branch information
kokoko3k authored Oct 21, 2024
1 parent 790deeb commit e1b2e29
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
21 changes: 14 additions & 7 deletions gfx/drivers_shader/glslang_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ enum slang_texture_semantic slang_name_to_texture_semantic_array(
}

bool glslang_read_shader_file(const char *path,
struct string_list *output, bool root_file)
struct string_list *output, bool root_file, bool is_optional)
{
size_t i;
char tmp[PATH_MAX_LENGTH];
Expand All @@ -124,7 +124,8 @@ bool glslang_read_shader_file(const char *path,
/* Read file contents */
if (!filestream_read_file(path, (void**)&buf, &buf_len))
{
RARCH_ERR("[slang]: Failed to open shader file: \"%s\".\n", path);
if (!is_optional)
RARCH_ERR("[slang]: Failed to open shader file: \"%s\".\n", path);
return false;
}

Expand Down Expand Up @@ -181,15 +182,17 @@ bool glslang_read_shader_file(const char *path,
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", root_file ? 2 : 1, basename);
if (!string_list_append(output, tmp, attr))
goto error;

/* Loop through lines of file */
for (i = root_file ? 1 : 0; i < lines.size; i++)
{
const char *line = lines.elems[i].data;

/* Check for 'include' statements */
if (!strncmp("#include ", line, STRLEN_CONST("#include ")))
bool include_optional = !strncmp("#pragma include_optional ", line, STRLEN_CONST("#pragma include_optional "));
if ( !strncmp("#include ", line, STRLEN_CONST("#include ")) || include_optional )
{

char include_file[PATH_MAX_LENGTH];
char include_path[PATH_MAX_LENGTH];

Expand All @@ -209,8 +212,12 @@ bool glslang_read_shader_file(const char *path,
include_path, path, include_file, sizeof(include_path));

/* Parse include file */
if (!glslang_read_shader_file(include_path, output, false))
goto error;
if (!glslang_read_shader_file(include_path, output, false, include_optional)) {
if (include_optional)
RARCH_LOG("[slang]: Optional include not found \"%s\".\n", include_path);
else
goto error;
}

/* After including a file, use line directive
* to pull it back to current file. */
Expand Down
2 changes: 1 addition & 1 deletion gfx/drivers_shader/glslang_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ enum glslang_format glslang_find_format(const char *fmt);
Returns a Bool indicating if parsing was successful.
*/
bool glslang_read_shader_file(const char *path,
struct string_list *output, bool root_file);
struct string_list *output, bool root_file, bool is_optional);

bool slang_texture_semantic_is_array(enum slang_texture_semantic sem);

Expand Down
2 changes: 1 addition & 1 deletion gfx/drivers_shader/glslang_util_cxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ bool glslang_compile_shader(const char *shader_path, glslang_output *output)

RARCH_LOG("[slang]: Compiling shader: \"%s\".\n", shader_path);

if (!glslang_read_shader_file(shader_path, &lines, true))
if (!glslang_read_shader_file(shader_path, &lines, true, false))
goto error;
output->meta = glslang_meta{};
if (!glslang_parse_meta(&lines, &output->meta))
Expand Down
2 changes: 1 addition & 1 deletion gfx/drivers_shader/slang_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ bool slang_preprocess_parse_parameters(const char *shader_path,
if (!string_list_initialize(&lines))
goto error;

if (!glslang_read_shader_file(shader_path, &lines, true))
if (!glslang_read_shader_file(shader_path, &lines, true, false))
goto error;
meta = glslang_meta{};
if (!glslang_parse_meta(&lines, &meta))
Expand Down

0 comments on commit e1b2e29

Please sign in to comment.