Merge pull request #2155 from tklajnscek/tk_glsl_get_extensions_api

Added get_required_extensions() API to GLSL compiler
This commit is contained in:
Hans-Kristian Arntzen 2023-06-06 15:21:42 +02:00 committed by GitHub
commit 2d4587cd2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 0 deletions

View File

@ -820,6 +820,42 @@ 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_GLSL)
{
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
return static_cast<CompilerGLSL *>(compiler->compiler.get())->get_required_extensions().size();
#else
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
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_GLSL)
{
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return nullptr;
}
auto &exts = static_cast<CompilerGLSL *>(compiler->compiler.get())->get_required_extensions();
if (index < exts.size())
return exts[index].c_str();
else
return nullptr;
#else
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return nullptr;
#endif
}
spvc_result spvc_compiler_flatten_buffer_block(spvc_compiler compiler, spvc_variable_id id)
{
#if SPIRV_CROSS_C_API_GLSL

View File

@ -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);

View File

@ -15565,6 +15565,11 @@ void CompilerGLSL::require_extension(const std::string &ext)
forced_extensions.push_back(ext);
}
const SmallVector<std::string> &CompilerGLSL::get_required_extensions() const
{
return forced_extensions;
}
void CompilerGLSL::require_extension_internal(const string &ext)
{
if (backend.supports_extensions && !has_extension(ext))

View File

@ -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<std::string> &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

View File

@ -7,6 +7,7 @@
#include <spirv_cross_c.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SPVC_CHECKED_CALL(x) do { \
if ((x) != SPVC_SUCCESS) { \
@ -175,6 +176,20 @@ 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));
#define 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 +198,25 @@ 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;