Skip to content

Commit e1b2e29

Browse files
authored
slang shaders: add support for __has_include like C++17 macro. (libretro#17109)
* 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
1 parent 790deeb commit e1b2e29

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

gfx/drivers_shader/glslang_util.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ enum slang_texture_semantic slang_name_to_texture_semantic_array(
9898
}
9999

100100
bool glslang_read_shader_file(const char *path,
101-
struct string_list *output, bool root_file)
101+
struct string_list *output, bool root_file, bool is_optional)
102102
{
103103
size_t i;
104104
char tmp[PATH_MAX_LENGTH];
@@ -124,7 +124,8 @@ bool glslang_read_shader_file(const char *path,
124124
/* Read file contents */
125125
if (!filestream_read_file(path, (void**)&buf, &buf_len))
126126
{
127-
RARCH_ERR("[slang]: Failed to open shader file: \"%s\".\n", path);
127+
if (!is_optional)
128+
RARCH_ERR("[slang]: Failed to open shader file: \"%s\".\n", path);
128129
return false;
129130
}
130131

@@ -181,15 +182,17 @@ bool glslang_read_shader_file(const char *path,
181182
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", root_file ? 2 : 1, basename);
182183
if (!string_list_append(output, tmp, attr))
183184
goto error;
184-
185+
185186
/* Loop through lines of file */
186187
for (i = root_file ? 1 : 0; i < lines.size; i++)
187188
{
188189
const char *line = lines.elems[i].data;
189-
190+
190191
/* Check for 'include' statements */
191-
if (!strncmp("#include ", line, STRLEN_CONST("#include ")))
192+
bool include_optional = !strncmp("#pragma include_optional ", line, STRLEN_CONST("#pragma include_optional "));
193+
if ( !strncmp("#include ", line, STRLEN_CONST("#include ")) || include_optional )
192194
{
195+
193196
char include_file[PATH_MAX_LENGTH];
194197
char include_path[PATH_MAX_LENGTH];
195198

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

211214
/* Parse include file */
212-
if (!glslang_read_shader_file(include_path, output, false))
213-
goto error;
215+
if (!glslang_read_shader_file(include_path, output, false, include_optional)) {
216+
if (include_optional)
217+
RARCH_LOG("[slang]: Optional include not found \"%s\".\n", include_path);
218+
else
219+
goto error;
220+
}
214221

215222
/* After including a file, use line directive
216223
* to pull it back to current file. */

gfx/drivers_shader/glslang_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ enum glslang_format glslang_find_format(const char *fmt);
129129
Returns a Bool indicating if parsing was successful.
130130
*/
131131
bool glslang_read_shader_file(const char *path,
132-
struct string_list *output, bool root_file);
132+
struct string_list *output, bool root_file, bool is_optional);
133133

134134
bool slang_texture_semantic_is_array(enum slang_texture_semantic sem);
135135

gfx/drivers_shader/glslang_util_cxx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ bool glslang_compile_shader(const char *shader_path, glslang_output *output)
220220

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

223-
if (!glslang_read_shader_file(shader_path, &lines, true))
223+
if (!glslang_read_shader_file(shader_path, &lines, true, false))
224224
goto error;
225225
output->meta = glslang_meta{};
226226
if (!glslang_parse_meta(&lines, &output->meta))

gfx/drivers_shader/slang_process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ bool slang_preprocess_parse_parameters(const char *shader_path,
397397
if (!string_list_initialize(&lines))
398398
goto error;
399399

400-
if (!glslang_read_shader_file(shader_path, &lines, true))
400+
if (!glslang_read_shader_file(shader_path, &lines, true, false))
401401
goto error;
402402
meta = glslang_meta{};
403403
if (!glslang_parse_meta(&lines, &meta))

0 commit comments

Comments
 (0)