From 406af8ff4df5132ef85854570863a32daaab1ecd Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Fri, 16 Apr 2021 14:12:07 +0200 Subject: [PATCH] c: Add C API for builtin stage IO reflection. --- CMakeLists.txt | 2 +- spirv_cross.hpp | 1 + spirv_cross_c.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ spirv_cross_c.h | 22 ++++++++++++++++- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 35979249..39acf9a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -332,7 +332,7 @@ if (SPIRV_CROSS_STATIC) endif() set(spirv-cross-abi-major 0) -set(spirv-cross-abi-minor 46) +set(spirv-cross-abi-minor 47) set(spirv-cross-abi-patch 0) if (SPIRV_CROSS_SHARED) diff --git a/spirv_cross.hpp b/spirv_cross.hpp index d818bb66..c603f841 100644 --- a/spirv_cross.hpp +++ b/spirv_cross.hpp @@ -70,6 +70,7 @@ struct BuiltInResource // This is the actual value type of the builtin. // Typically float4, float, array for the gl_PerVertex builtins. + // If the builtin is a control point, the control point array type will be stripped away here as appropriate. TypeID value_type_id; // This refers to the base resource which contains the builtin. diff --git a/spirv_cross_c.cpp b/spirv_cross_c.cpp index f5645645..866c25d1 100644 --- a/spirv_cross_c.cpp +++ b/spirv_cross_c.cpp @@ -197,8 +197,11 @@ struct spvc_resources_s : ScratchMemoryAllocation SmallVector separate_images; SmallVector separate_samplers; SmallVector acceleration_structures; + SmallVector builtin_inputs; + SmallVector builtin_outputs; bool copy_resources(SmallVector &outputs, const SmallVector &inputs); + bool copy_resources(SmallVector &outputs, const SmallVector &inputs); bool copy_resources(const ShaderResources &resources); }; @@ -1589,6 +1592,30 @@ bool spvc_resources_s::copy_resources(SmallVector &outp return true; } +bool spvc_resources_s::copy_resources(SmallVector &outputs, + const SmallVector &inputs) +{ + for (auto &i : inputs) + { + spvc_reflected_builtin_resource br; + + br.value_type_id = i.value_type_id; + br.builtin = SpvBuiltIn(i.builtin); + + auto &r = br.resource; + r.base_type_id = i.resource.base_type_id; + r.type_id = i.resource.type_id; + r.id = i.resource.id; + r.name = context->allocate_name(i.resource.name); + if (!r.name) + return false; + + outputs.push_back(br); + } + + return true; +} + bool spvc_resources_s::copy_resources(const ShaderResources &resources) { if (!copy_resources(uniform_buffers, resources.uniform_buffers)) @@ -1615,6 +1642,10 @@ bool spvc_resources_s::copy_resources(const ShaderResources &resources) return false; if (!copy_resources(acceleration_structures, resources.acceleration_structures)) return false; + if (!copy_resources(builtin_inputs, resources.builtin_inputs)) + return false; + if (!copy_resources(builtin_outputs, resources.builtin_outputs)) + return false; return true; } @@ -1773,6 +1804,37 @@ spvc_result spvc_resources_get_resource_list_for_type(spvc_resources resources, return SPVC_SUCCESS; } +spvc_result spvc_resources_get_builtin_resource_list_for_type( + spvc_resources resources, spvc_builtin_resource_type type, + const spvc_reflected_builtin_resource **resource_list, + size_t *resource_size) +{ + const SmallVector *list = nullptr; + switch (type) + { + case SPVC_BUILTIN_RESOURCE_TYPE_STAGE_INPUT: + list = &resources->builtin_inputs; + break; + + case SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT: + list = &resources->builtin_outputs; + break; + + default: + break; + } + + if (!list) + { + resources->context->report_error("Invalid argument."); + return SPVC_ERROR_INVALID_ARGUMENT; + } + + *resource_size = list->size(); + *resource_list = list->data(); + return SPVC_SUCCESS; +} + void spvc_compiler_set_decoration(spvc_compiler compiler, SpvId id, SpvDecoration decoration, unsigned argument) { compiler->compiler->set_decoration(id, static_cast(decoration), argument); diff --git a/spirv_cross_c.h b/spirv_cross_c.h index 07aecb41..1495a7ea 100644 --- a/spirv_cross_c.h +++ b/spirv_cross_c.h @@ -40,7 +40,7 @@ extern "C" { /* Bumped if ABI or API breaks backwards compatibility. */ #define SPVC_C_API_VERSION_MAJOR 0 /* Bumped if APIs or enumerations are added in a backwards compatible way. */ -#define SPVC_C_API_VERSION_MINOR 46 +#define SPVC_C_API_VERSION_MINOR 47 /* Bumped if internal implementation details change. */ #define SPVC_C_API_VERSION_PATCH 0 @@ -99,6 +99,13 @@ typedef struct spvc_reflected_resource const char *name; } spvc_reflected_resource; +typedef struct spvc_reflected_builtin_resource +{ + SpvBuiltIn builtin; + spvc_type_id value_type_id; + spvc_reflected_resource resource; +} spvc_reflected_builtin_resource; + /* See C++ API. */ typedef struct spvc_entry_point { @@ -221,6 +228,14 @@ typedef enum spvc_resource_type SPVC_RESOURCE_TYPE_INT_MAX = 0x7fffffff } spvc_resource_type; +typedef enum spvc_builtin_resource_type +{ + SPVC_BUILTIN_RESOURCE_TYPE_UNKNOWN = 0, + SPVC_BUILTIN_RESOURCE_TYPE_STAGE_INPUT = 1, + SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT = 2, + SPVC_BUILTIN_RESOURCE_TYPE_INT_MAX = 0x7fffffff +} spvc_builtin_resource_type; + /* Maps to spirv_cross::SPIRType::BaseType. */ typedef enum spvc_basetype { @@ -809,6 +824,11 @@ SPVC_PUBLIC_API spvc_result spvc_resources_get_resource_list_for_type(spvc_resou const spvc_reflected_resource **resource_list, size_t *resource_size); +SPVC_PUBLIC_API spvc_result spvc_resources_get_builtin_resource_list_for_type( + spvc_resources resources, spvc_builtin_resource_type type, + const spvc_reflected_builtin_resource **resource_list, + size_t *resource_size); + /* * Decorations. * Maps to C++ API.