From c4fdb3f3714de26d864a228d69ffce15690c9a23 Mon Sep 17 00:00:00 2001 From: Tibor Klajnscek Date: Fri, 26 May 2023 15:36:28 +0200 Subject: [PATCH 1/3] added get_required_extensions() API to GLSL compiler --- spirv_cross_c.cpp | 37 +++++++++++++++++++++++++++++++++++++ spirv_cross_c.h | 2 ++ spirv_glsl.cpp | 4 ++++ spirv_glsl.hpp | 4 ++++ tests-other/c_api_test.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/spirv_cross_c.cpp b/spirv_cross_c.cpp index f76c14c0..e48bfbfb 100644 --- a/spirv_cross_c.cpp +++ b/spirv_cross_c.cpp @@ -820,6 +820,43 @@ spvc_result spvc_compiler_require_extension(spvc_compiler compiler, const char * #endif } +size_t spvc_compiler_get_num_required_extensions(spvc_compiler compiler) +{ +#if SPIRV_CROSS_C_API_GLSL + if (compiler->backend == SPVC_BACKEND_NONE) + { + compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + return SPVC_ERROR_INVALID_ARGUMENT; + } + + return static_cast(compiler->compiler.get())->get_required_extensions().size(); +#else + compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + return 0; +#endif +} + +const char *spvc_compiler_get_required_extension(spvc_compiler compiler, size_t index) +{ +#if SPIRV_CROSS_C_API_GLSL + if (compiler->backend == SPVC_BACKEND_NONE) + { + compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + return nullptr; + } + + auto& exts = static_cast(compiler->compiler.get())->get_required_extensions(); + if (index < exts.size()) { + return exts[index].c_str(); + } else { + return nullptr; + } +#else + compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + return nullptr; +#endif +} + spvc_result spvc_compiler_flatten_buffer_block(spvc_compiler compiler, spvc_variable_id id) { #if SPIRV_CROSS_C_API_GLSL diff --git a/spirv_cross_c.h b/spirv_cross_c.h index 7f8c6fad..feca0610 100644 --- a/spirv_cross_c.h +++ b/spirv_cross_c.h @@ -784,6 +784,8 @@ SPVC_PUBLIC_API spvc_result spvc_compiler_compile(spvc_compiler compiler, const /* Maps to C++ API. */ SPVC_PUBLIC_API spvc_result spvc_compiler_add_header_line(spvc_compiler compiler, const char *line); SPVC_PUBLIC_API spvc_result spvc_compiler_require_extension(spvc_compiler compiler, const char *ext); +SPVC_PUBLIC_API size_t spvc_compiler_get_num_required_extensions(spvc_compiler compiler); +SPVC_PUBLIC_API const char *spvc_compiler_get_required_extension(spvc_compiler compiler, size_t index); SPVC_PUBLIC_API spvc_result spvc_compiler_flatten_buffer_block(spvc_compiler compiler, spvc_variable_id id); SPVC_PUBLIC_API spvc_bool spvc_compiler_variable_is_depth_or_compare(spvc_compiler compiler, spvc_variable_id id); diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 947cc95e..c01b533f 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -15565,6 +15565,10 @@ void CompilerGLSL::require_extension(const std::string &ext) forced_extensions.push_back(ext); } +const SmallVector &CompilerGLSL::get_required_extensions() const { + return forced_extensions; +} + void CompilerGLSL::require_extension_internal(const string &ext) { if (backend.supports_extensions && !has_extension(ext)) diff --git a/spirv_glsl.hpp b/spirv_glsl.hpp index d4af6924..913526bb 100644 --- a/spirv_glsl.hpp +++ b/spirv_glsl.hpp @@ -258,6 +258,10 @@ public: // require_extension("GL_KHR_my_extension"); void require_extension(const std::string &ext); + // Returns the list of required extensions. After compilation this will contains any other + // extensions that the compiler used automatically, in addition to the user specified ones. + const SmallVector &get_required_extensions() const; + // Legacy GLSL compatibility method. // Takes a uniform or push constant variable and flattens it into a (i|u)vec4 array[N]; array instead. // For this to work, all types in the block must be the same basic type, e.g. mixing vec2 and vec4 is fine, but diff --git a/tests-other/c_api_test.c b/tests-other/c_api_test.c index 413e2044..e3fda2ef 100644 --- a/tests-other/c_api_test.c +++ b/tests-other/c_api_test.c @@ -7,6 +7,7 @@ #include #include #include +#include #define SPVC_CHECKED_CALL(x) do { \ if ((x) != SPVC_SUCCESS) { \ @@ -175,6 +176,18 @@ int main(int argc, char **argv) SPVC_CHECKED_CALL(spvc_compiler_create_compiler_options(compiler_glsl, &options)); SPVC_CHECKED_CALL(spvc_compiler_install_compiler_options(compiler_glsl, options)); + const int NUM_EXTS = 2; + const char* expected_exts[NUM_EXTS] = { + "EXT_first", + "EXT_second" + }; + + int ext_idx = 0; + while(ext_idx < NUM_EXTS) { + SPVC_CHECKED_CALL(spvc_compiler_require_extension(compiler_glsl, expected_exts[ext_idx])); + ext_idx += 1; + } + SPVC_CHECKED_CALL(spvc_compiler_create_shader_resources(compiler_none, &resources)); dump_resources(compiler_none, resources); compile(compiler_glsl, "GLSL"); @@ -183,6 +196,23 @@ int main(int argc, char **argv) compile(compiler_json, "JSON"); compile(compiler_cpp, "CPP"); + int num_exts = spvc_compiler_get_num_required_extensions(compiler_glsl); + if (num_exts != NUM_EXTS) + { + fprintf(stderr, "num_exts mismatch!\n"); + return 1; + } + + ext_idx = 0; + while(ext_idx < num_exts) { + const char* ext = spvc_compiler_get_required_extension(compiler_glsl, ext_idx); + if(strcmp(ext, expected_exts[ext_idx]) != 0) { + fprintf(stderr, "extension mismatch (%s != %s)!\n", ext, expected_exts[ext_idx]); + return 1; + } + ext_idx += 1; + } + spvc_context_destroy(context); free(buffer); return 0; From 2ace2714edc3a4f0b6bacd11d59540c9734e0725 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 6 Jun 2023 12:41:59 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- spirv_cross_c.cpp | 19 +++++++++---------- spirv_glsl.cpp | 3 ++- tests-other/c_api_test.c | 16 ++++++++++------ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/spirv_cross_c.cpp b/spirv_cross_c.cpp index e48bfbfb..6a6dee0c 100644 --- a/spirv_cross_c.cpp +++ b/spirv_cross_c.cpp @@ -823,15 +823,15 @@ spvc_result spvc_compiler_require_extension(spvc_compiler compiler, const char * size_t spvc_compiler_get_num_required_extensions(spvc_compiler compiler) { #if SPIRV_CROSS_C_API_GLSL - if (compiler->backend == SPVC_BACKEND_NONE) + if (compiler->backend != SPVC_BACKEND_GLSL) { - compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + compiler->context->report_error("Enabled extensions can only be queried on GLSL backend."); return SPVC_ERROR_INVALID_ARGUMENT; } return static_cast(compiler->compiler.get())->get_required_extensions().size(); #else - compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + compiler->context->report_error("Enabled extensions can only be queried on GLSL backend."); return 0; #endif } @@ -839,20 +839,19 @@ size_t spvc_compiler_get_num_required_extensions(spvc_compiler compiler) const char *spvc_compiler_get_required_extension(spvc_compiler compiler, size_t index) { #if SPIRV_CROSS_C_API_GLSL - if (compiler->backend == SPVC_BACKEND_NONE) + if (compiler->backend != SPVC_BACKEND_GLSL) { - compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + compiler->context->report_error("Enabled extensions can only be queried on GLSL backend."); return nullptr; } - auto& exts = static_cast(compiler->compiler.get())->get_required_extensions(); - if (index < exts.size()) { + auto &exts = static_cast(compiler->compiler.get())->get_required_extensions(); + if (index < exts.size()) return exts[index].c_str(); - } else { + else return nullptr; - } #else - compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection."); + compiler->context->report_error("Enabled extensions can only be queried on GLSL backend."); return nullptr; #endif } diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index c01b533f..281d4faa 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -15565,7 +15565,8 @@ void CompilerGLSL::require_extension(const std::string &ext) forced_extensions.push_back(ext); } -const SmallVector &CompilerGLSL::get_required_extensions() const { +const SmallVector &CompilerGLSL::get_required_extensions() const +{ return forced_extensions; } diff --git a/tests-other/c_api_test.c b/tests-other/c_api_test.c index e3fda2ef..81853e44 100644 --- a/tests-other/c_api_test.c +++ b/tests-other/c_api_test.c @@ -176,14 +176,16 @@ int main(int argc, char **argv) SPVC_CHECKED_CALL(spvc_compiler_create_compiler_options(compiler_glsl, &options)); SPVC_CHECKED_CALL(spvc_compiler_install_compiler_options(compiler_glsl, options)); - const int NUM_EXTS = 2; - const char* expected_exts[NUM_EXTS] = { + static const int NUM_EXTS = 2; + const char *expected_exts[NUM_EXTS] = + { "EXT_first", "EXT_second" }; int ext_idx = 0; - while(ext_idx < NUM_EXTS) { + while (ext_idx < NUM_EXTS) + { SPVC_CHECKED_CALL(spvc_compiler_require_extension(compiler_glsl, expected_exts[ext_idx])); ext_idx += 1; } @@ -204,9 +206,11 @@ int main(int argc, char **argv) } ext_idx = 0; - while(ext_idx < num_exts) { - const char* ext = spvc_compiler_get_required_extension(compiler_glsl, ext_idx); - if(strcmp(ext, expected_exts[ext_idx]) != 0) { + while (ext_idx < num_exts) + { + const char *ext = spvc_compiler_get_required_extension(compiler_glsl, ext_idx); + if (strcmp(ext, expected_exts[ext_idx]) != 0) + { fprintf(stderr, "extension mismatch (%s != %s)!\n", ext, expected_exts[ext_idx]); return 1; } From bf3f651f6c262e01b3d1349c2ff497bf0e698a60 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 6 Jun 2023 12:58:22 +0200 Subject: [PATCH 3/3] Workaround MSVC jank --- tests-other/c_api_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests-other/c_api_test.c b/tests-other/c_api_test.c index 81853e44..74a1883a 100644 --- a/tests-other/c_api_test.c +++ b/tests-other/c_api_test.c @@ -176,7 +176,7 @@ int main(int argc, char **argv) SPVC_CHECKED_CALL(spvc_compiler_create_compiler_options(compiler_glsl, &options)); SPVC_CHECKED_CALL(spvc_compiler_install_compiler_options(compiler_glsl, options)); - static const int NUM_EXTS = 2; +#define NUM_EXTS 2 const char *expected_exts[NUM_EXTS] = { "EXT_first",