From 4e9fe4bd54312327e497bf74952e07901c853783 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 20 Dec 2016 15:50:50 +0100 Subject: [PATCH] Vulkan: Fix menu shader parameters for slangp. video_shader_resolve_parameters did not take #includes into account while the Vulkan implementation did. Added a helper function which parses a shader file and figures out the shader parameters in the same way. --- Makefile.common | 2 + gfx/drivers_shader/glslang_util.cpp | 8 +-- gfx/drivers_shader/glslang_util.hpp | 4 ++ gfx/drivers_shader/slang_preprocess.cpp | 87 +++++++++++++++++++++++++ gfx/drivers_shader/slang_preprocess.h | 34 ++++++++++ gfx/video_shader_parse.c | 15 +++++ 6 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 gfx/drivers_shader/slang_preprocess.cpp create mode 100644 gfx/drivers_shader/slang_preprocess.h diff --git a/Makefile.common b/Makefile.common index 2851353841..8bf248dff5 100644 --- a/Makefile.common +++ b/Makefile.common @@ -903,6 +903,7 @@ ifeq ($(HAVE_VULKAN), 1) gfx/drivers_shader/shader_vulkan.o \ gfx/drivers_shader/glslang_util.o \ gfx/drivers_shader/slang_reflection.o \ + gfx/drivers_shader/slang_preprocess.o \ $(GLSLANG_OBJ) \ $(SPIRV_CROSS_OBJ) @@ -913,6 +914,7 @@ ifeq ($(HAVE_VULKAN), 1) OBJ += menu/drivers_display/menu_display_vulkan.o endif LIBS += -lstdc++ + DEFINES += -DHAVE_SLANG endif diff --git a/gfx/drivers_shader/glslang_util.cpp b/gfx/drivers_shader/glslang_util.cpp index 6f0793f103..d49a58b633 100644 --- a/gfx/drivers_shader/glslang_util.cpp +++ b/gfx/drivers_shader/glslang_util.cpp @@ -32,7 +32,7 @@ using namespace std; -static bool read_shader_file(const char *path, vector *output, bool root_file) +bool glslang_read_shader_file(const char *path, vector *output, bool root_file) { vector lines; char include_path[PATH_MAX_LENGTH]; @@ -122,7 +122,7 @@ static bool read_shader_file(const char *path, vector *output, bool root fill_pathname_resolve_relative(include_path, path, c, sizeof(include_path)); - if (!read_shader_file(include_path, output, false)) + if (!glslang_read_shader_file(include_path, output, false)) { free(buf); return false; @@ -268,7 +268,7 @@ static glslang_format glslang_find_format(const char *fmt) return SLANG_FORMAT_UNKNOWN; } -static bool glslang_parse_meta(const vector &lines, glslang_meta *meta) +bool glslang_parse_meta(const vector &lines, glslang_meta *meta) { char id[64] = {}; char desc[64] = {}; @@ -362,7 +362,7 @@ bool glslang_compile_shader(const char *shader_path, glslang_output *output) vector lines; RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path); - if (!read_shader_file(shader_path, &lines, true)) + if (!glslang_read_shader_file(shader_path, &lines, true)) return false; if (!glslang_parse_meta(lines, &output->meta)) diff --git a/gfx/drivers_shader/glslang_util.hpp b/gfx/drivers_shader/glslang_util.hpp index 0ababea8b1..263eefce5f 100644 --- a/gfx/drivers_shader/glslang_util.hpp +++ b/gfx/drivers_shader/glslang_util.hpp @@ -90,5 +90,9 @@ struct glslang_output bool glslang_compile_shader(const char *shader_path, glslang_output *output); const char *glslang_format_to_string(enum glslang_format fmt); +// Helpers for internal use. +bool glslang_read_shader_file(const char *path, std::vector *output, bool root_file); +bool glslang_parse_meta(const std::vector &lines, glslang_meta *meta); + #endif diff --git a/gfx/drivers_shader/slang_preprocess.cpp b/gfx/drivers_shader/slang_preprocess.cpp new file mode 100644 index 0000000000..f596521a50 --- /dev/null +++ b/gfx/drivers_shader/slang_preprocess.cpp @@ -0,0 +1,87 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2016 - Hans-Kristian Arntzen + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#include "slang_preprocess.h" +#include "glslang_util.hpp" +#include +#include +#include +#include "../../verbosity.h" +#include "../../libretro-common/include/compat/strl.h" + +using namespace std; + +bool slang_preprocess_parse_parameters(const char *shader_path, + struct video_shader *shader) +{ + glslang_meta meta; + vector lines; + if (!glslang_read_shader_file(shader_path, &lines, true)) + return false; + + if (!glslang_parse_meta(lines, &meta)) + return false; + + unsigned old_num_parameters = shader->num_parameters; + + // Assumes num_parameters is initialized to something sane. + for (auto ¶m : meta.parameters) + { + bool mismatch_dup = false; + bool dup = false; + + auto itr = find_if(shader->parameters, shader->parameters + shader->num_parameters, + [&](const video_shader_parameter &parsed_param) { + return param.id == parsed_param.id; + }); + + if (itr != shader->parameters + shader->num_parameters) + { + dup = true; + // Allow duplicate #pragma parameter, but only if they are exactly the same. + if (param.desc != itr->desc || + param.initial != itr->initial || + param.minimum != itr->minimum || + param.maximum != itr->maximum || + param.step != itr->step) + { + RARCH_ERR("[Vulkan]: Duplicate parameters found for \"%s\", but arguments do not match.\n", + itr->id); + mismatch_dup = true; + } + } + + if (dup && !mismatch_dup) + continue; + + if (mismatch_dup || shader->num_parameters == GFX_MAX_PARAMETERS) + { + shader->num_parameters = old_num_parameters; + return false; + } + + auto &p = shader->parameters[shader->num_parameters++]; + strlcpy(p.id, param.id.c_str(), sizeof(p.id)); + strlcpy(p.desc, param.desc.c_str(), sizeof(p.desc)); + p.initial = param.initial; + p.minimum = param.minimum; + p.maximum = param.maximum; + p.step = param.step; + p.current = param.initial; + } + + return true; +} + diff --git a/gfx/drivers_shader/slang_preprocess.h b/gfx/drivers_shader/slang_preprocess.h new file mode 100644 index 0000000000..5721e5148a --- /dev/null +++ b/gfx/drivers_shader/slang_preprocess.h @@ -0,0 +1,34 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2016 - Hans-Kristian Arntzen + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#ifndef SLANG_PREPROCESS_H +#define SLANG_PREPROCESS_H + +#include +#include +#include "../video_shader_driver.h" + +RETRO_BEGIN_DECLS + +/* Utility function to implement the same parameter reflection + * which happens in the slang backend. + * This does preprocess over the input file to handle #includes and so on. */ +bool slang_preprocess_parse_parameters(const char *shader_path, + struct video_shader *shader); + +RETRO_END_DECLS + +#endif + diff --git a/gfx/video_shader_parse.c b/gfx/video_shader_parse.c index 07ca73f24c..df50a2d75e 100644 --- a/gfx/video_shader_parse.c +++ b/gfx/video_shader_parse.c @@ -29,6 +29,10 @@ #include "../verbosity.h" #include "video_shader_parse.h" +#ifdef HAVE_SLANG +#include "drivers_shader/slang_preprocess.h" +#endif + #define WRAP_MODE_CLAMP_TO_BORDER 0x3676ed11U #define WRAP_MODE_CLAMP_TO_EDGE 0x9427a608U #define WRAP_MODE_REPEAT 0x192dec66U @@ -483,6 +487,17 @@ bool video_shader_resolve_parameters(config_file_t *conf, for (i = 0; i < shader->passes; i++) { +#ifdef HAVE_SLANG + /* First try to use the more robust slang implementation to support #includes. */ + /* FIXME: The check for slang can be removed if it's sufficiently tested for + * GLSL/Cg as well, it should be the same implementation. */ + if (string_is_equal(path_get_extension(shader->pass[i].source.path), "slang") && + slang_preprocess_parse_parameters(shader->pass[i].source.path, shader)) + continue; + /* If that doesn't work, fallback to the old path. + * Ideally, we'd get rid of this path sooner or later. */ +#endif + char line[4096]; RFILE *file = filestream_open(shader->pass[i].source.path, RFILE_MODE_READ_TEXT, -1);