godot/thirdparty/spirv-reflect/spirv_reflect.c

/*
 Copyright 2017-2022 Google Inc.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#include "spirv_reflect.h"

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#if defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>
#else
#include <stdlib.h>
#endif

#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 7) || defined(__APPLE_CC__)
#define FALLTHROUGH
#else
#define FALLTHROUGH
#endif

#if defined(SPIRV_REFLECT_ENABLE_ASSERTS)
#define SPV_REFLECT_ASSERT
#else
#define SPV_REFLECT_ASSERT(COND)
#endif

// clang-format off
enum {};

enum {};

enum {};

enum {};

SpvReflectPrvArrayTraits;

SpvReflectPrvImageTraits;

SpvReflectPrvNumberDecoration;

SpvReflectPrvStringDecoration;

SpvReflectPrvDecorations;

SpvReflectPrvNode;

SpvReflectPrvString;

// There are a limit set of instructions that can touch an OpVariable,
// these are represented here with how it was accessed
// Examples:
//    OpImageRead  -> OpLoad -> OpVariable
//    OpImageWrite -> OpLoad -> OpVariable
//    OpStore      -> OpAccessChain -> OpAccessChain -> OpVariable
//    OpAtomicIAdd -> OpAccessChain -> OpVariable
//    OpAtomicLoad -> OpImageTexelPointer -> OpVariable
SpvReflectPrvAccessedVariable;

SpvReflectPrvFunction;

SpvReflectPrvAccessChain;

// To prevent infinite recursion, we never walk down a
// PhysicalStorageBuffer struct twice, but incase a 2nd variable
// needs to use that struct, save a copy
SpvReflectPrvPhysicalPointerStruct;

SpvReflectPrvParser;
// clang-format on

static uint32_t Max(uint32_t a, uint32_t b) {}
static uint32_t Min(uint32_t a, uint32_t b) {}

static uint32_t RoundUp(uint32_t value, uint32_t multiple) {}

#define IsNull(ptr)

#define IsNotNull(ptr)

#define SafeFree(ptr)

static int SortCompareUint32(const void* a, const void* b) {}

static int SortCompareAccessedVariable(const void* a, const void* b) {}

//
// De-duplicates a sorted array and returns the new size.
//
// Note: The array doesn't actually need to be sorted, just
// arranged into "runs" so that all the entries with one
// value are adjacent.
//
static size_t DedupSortedUint32(uint32_t* arr, size_t size) {}

static bool SearchSortedUint32(const uint32_t* arr, size_t size, uint32_t target) {}

static SpvReflectResult IntersectSortedAccessedVariable(const SpvReflectPrvAccessedVariable* p_arr0, size_t arr0_size,
                                                        const uint32_t* p_arr1, size_t arr1_size, uint32_t** pp_res,
                                                        size_t* res_size) {}

static bool InRange(const SpvReflectPrvParser* p_parser, uint32_t index) {}

static SpvReflectResult ReadU32(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t* p_value) {}

#define UNCHECKED_READU32(parser, word_offset, value)

#define CHECKED_READU32(parser, word_offset, value)

#define CHECKED_READU32_CAST(parser, word_offset, cast_to_type, value)

#define IF_READU32(result, parser, word_offset, value)

#define IF_READU32_CAST(result, parser, word_offset, cast_to_type, value)

static SpvReflectResult ReadStr(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t word_index, uint32_t word_count,
                                uint32_t* p_buf_size, char* p_buf) {}

static SpvReflectDecorationFlags ApplyDecorations(const SpvReflectPrvDecorations* p_decoration_fields) {}

static void ApplyNumericTraits(const SpvReflectTypeDescription* p_type, SpvReflectNumericTraits* p_numeric_traits) {}

static void ApplyArrayTraits(const SpvReflectTypeDescription* p_type, SpvReflectArrayTraits* p_array_traits) {}

static bool IsSpecConstant(const SpvReflectPrvNode* p_node) {}

static SpvReflectPrvNode* FindNode(SpvReflectPrvParser* p_parser, uint32_t result_id) {}

static SpvReflectTypeDescription* FindType(SpvReflectShaderModule* p_module, uint32_t type_id) {}

static SpvReflectPrvAccessChain* FindAccessChain(SpvReflectPrvParser* p_parser, uint32_t id) {}

// Access Chains mostly have their Base ID pointed directly to a OpVariable, but sometimes
// it will be through a load and this funciton handles the edge cases how to find that
static uint32_t FindAccessChainBaseVariable(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) {}

static SpvReflectBlockVariable* GetRefBlkVar(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) {}

bool IsPointerToPointer(SpvReflectPrvParser* p_parser, uint32_t type_id) {}

static SpvReflectResult CreateParser(size_t size, void* p_code, SpvReflectPrvParser* p_parser) {}

static void DestroyParser(SpvReflectPrvParser* p_parser) {}

static SpvReflectResult ParseNodes(SpvReflectPrvParser* p_parser) {}

static SpvReflectResult ParseStrings(SpvReflectPrvParser* p_parser) {}

static SpvReflectResult ParseSource(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseFunction(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_func_node, SpvReflectPrvFunction* p_func,
                                      size_t first_label_index) {}

static int SortCompareFunctions(const void* a, const void* b) {}

static SpvReflectResult ParseFunctions(SpvReflectPrvParser* p_parser) {}

static SpvReflectResult ParseMemberCounts(SpvReflectPrvParser* p_parser) {}

static SpvReflectResult ParseNames(SpvReflectPrvParser* p_parser) {}

// Returns true if user_type matches pattern or if user_type begins with pattern and the next character is ':'
// For example, UserTypeMatches("rwbuffer", "rwbuffer") will be true, UserTypeMatches("rwbuffer", "rwbuffer:<S>") will be true, and
// UserTypeMatches("rwbuffer", "rwbufferfoo") will be false.
static bool UserTypeMatches(const char* user_type, const char* pattern) {}

static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult EnumerateAllUniforms(SpvReflectShaderModule* p_module, size_t* p_uniform_count, uint32_t** pp_uniforms) {}

static SpvReflectResult ParseType(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_node,
                                  SpvReflectPrvDecorations* p_struct_member_decorations, SpvReflectShaderModule* p_module,
                                  SpvReflectTypeDescription* p_type) {}

static SpvReflectResult ParseTypes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseCapabilities(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static int SortCompareDescriptorBinding(const void* a, const void* b) {}

static SpvReflectResult ParseDescriptorBindings(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseDescriptorType(SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseUAVCounterBindings(SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseDescriptorBlockVariable(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module,
                                                     SpvReflectTypeDescription* p_type, SpvReflectBlockVariable* p_var) {}

static uint32_t GetPhysicalPointerStructSize(SpvReflectPrvParser* p_parser, uint32_t id) {}

static SpvReflectResult ParseDescriptorBlockVariableSizes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module,
                                                          bool is_parent_root, bool is_parent_aos, bool is_parent_rta,
                                                          SpvReflectBlockVariable* p_var) {}

static void MarkSelfAndAllMemberVarsAsUsed(SpvReflectBlockVariable* p_var) {}

static SpvReflectResult ParseDescriptorBlockVariableUsage(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module,
                                                          SpvReflectPrvAccessChain* p_access_chain, uint32_t index_index,
                                                          SpvOp override_op_type, SpvReflectBlockVariable* p_var) {}

static SpvReflectResult ParseDescriptorBlocks(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseFormat(const SpvReflectTypeDescription* p_type, SpvReflectFormat* p_format) {}

static SpvReflectResult ParseInterfaceVariable(SpvReflectPrvParser* p_parser,
                                               const SpvReflectPrvDecorations* p_var_node_decorations,
                                               const SpvReflectPrvDecorations* p_type_node_decorations,
                                               SpvReflectShaderModule* p_module, SpvReflectTypeDescription* p_type,
                                               SpvReflectInterfaceVariable* p_var, bool* p_has_built_in) {}

static SpvReflectResult ParseInterfaceVariables(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module,
                                                SpvReflectEntryPoint* p_entry, uint32_t interface_variable_count,
                                                uint32_t* p_interface_variable_ids) {}

static SpvReflectResult EnumerateAllPushConstants(SpvReflectShaderModule* p_module, size_t* p_push_constant_count,
                                                  uint32_t** p_push_constants) {}

static SpvReflectResult TraverseCallGraph(SpvReflectPrvParser* p_parser, SpvReflectPrvFunction* p_func, size_t* p_func_count,
                                          uint32_t* p_func_ids, uint32_t depth) {}

static uint32_t GetUint32Constant(SpvReflectPrvParser* p_parser, uint32_t id) {}

static bool HasByteAddressBufferOffset(SpvReflectPrvNode* p_node, SpvReflectDescriptorBinding* p_binding) {}

static SpvReflectResult ParseByteAddressBuffer(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_node,
                                               SpvReflectDescriptorBinding* p_binding) {}

static SpvReflectResult ParseStaticallyUsedResources(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module,
                                                     SpvReflectEntryPoint* p_entry, size_t uniform_count, uint32_t* uniforms,
                                                     size_t push_constant_count, uint32_t* push_constants) {}

static SpvReflectResult ParseEntryPoints(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseExecutionModes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParsePushConstantBlocks(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {}

static int SortCompareDescriptorSet(const void* a, const void* b) {}

static SpvReflectResult ParseEntrypointDescriptorSets(SpvReflectShaderModule* p_module) {}

static SpvReflectResult ParseDescriptorSets(SpvReflectShaderModule* p_module) {}

static SpvReflectResult DisambiguateStorageBufferSrvUav(SpvReflectShaderModule* p_module) {}

static SpvReflectResult SynchronizeDescriptorSets(SpvReflectShaderModule* p_module) {}

static SpvReflectResult CreateShaderModule(uint32_t flags, size_t size, const void* p_code, SpvReflectShaderModule* p_module) {}

SpvReflectResult spvReflectCreateShaderModule(size_t size, const void* p_code, SpvReflectShaderModule* p_module) {}

SpvReflectResult spvReflectCreateShaderModule2(uint32_t flags, size_t size, const void* p_code, SpvReflectShaderModule* p_module) {}

SpvReflectResult spvReflectGetShaderModule(size_t size, const void* p_code, SpvReflectShaderModule* p_module) {}

static void SafeFreeTypes(SpvReflectTypeDescription* p_type) {}

static void SafeFreeBlockVariables(SpvReflectBlockVariable* p_block) {}

static void SafeFreeInterfaceVariable(SpvReflectInterfaceVariable* p_interface) {}

void spvReflectDestroyShaderModule(SpvReflectShaderModule* p_module) {}

uint32_t spvReflectGetCodeSize(const SpvReflectShaderModule* p_module) {}

const uint32_t* spvReflectGetCode(const SpvReflectShaderModule* p_module) {}

const SpvReflectEntryPoint* spvReflectGetEntryPoint(const SpvReflectShaderModule* p_module, const char* entry_point) {}

SpvReflectResult spvReflectEnumerateDescriptorBindings(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                       SpvReflectDescriptorBinding** pp_bindings) {}

SpvReflectResult spvReflectEnumerateEntryPointDescriptorBindings(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                                 uint32_t* p_count, SpvReflectDescriptorBinding** pp_bindings) {}

SpvReflectResult spvReflectEnumerateDescriptorSets(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                   SpvReflectDescriptorSet** pp_sets) {}

SpvReflectResult spvReflectEnumerateEntryPointDescriptorSets(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                             uint32_t* p_count, SpvReflectDescriptorSet** pp_sets) {}

SpvReflectResult spvReflectEnumerateInterfaceVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                       SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumerateEntryPointInterfaceVariables(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                                 uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumerateInputVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                   SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumerateEntryPointInputVariables(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                             uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumerateOutputVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                    SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumerateEntryPointOutputVariables(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                              uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) {}

SpvReflectResult spvReflectEnumeratePushConstantBlocks(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                       SpvReflectBlockVariable** pp_blocks) {}
SpvReflectResult spvReflectEnumeratePushConstants(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                  SpvReflectBlockVariable** pp_blocks) {}

SpvReflectResult spvReflectEnumerateEntryPointPushConstantBlocks(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                                 uint32_t* p_count, SpvReflectBlockVariable** pp_blocks) {}

SpvReflectResult spvReflectEnumerateSpecializationConstants(const SpvReflectShaderModule* p_module, uint32_t* p_count,
                                                            SpvReflectSpecializationConstant** pp_constants) {}

const SpvReflectDescriptorBinding* spvReflectGetDescriptorBinding(const SpvReflectShaderModule* p_module, uint32_t binding_number,
                                                                  uint32_t set_number, SpvReflectResult* p_result) {}

const SpvReflectDescriptorBinding* spvReflectGetEntryPointDescriptorBinding(const SpvReflectShaderModule* p_module,
                                                                            const char* entry_point, uint32_t binding_number,
                                                                            uint32_t set_number, SpvReflectResult* p_result) {}

const SpvReflectDescriptorSet* spvReflectGetDescriptorSet(const SpvReflectShaderModule* p_module, uint32_t set_number,
                                                          SpvReflectResult* p_result) {}

const SpvReflectDescriptorSet* spvReflectGetEntryPointDescriptorSet(const SpvReflectShaderModule* p_module, const char* entry_point,
                                                                    uint32_t set_number, SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetInputVariableByLocation(const SpvReflectShaderModule* p_module, uint32_t location,
                                                                        SpvReflectResult* p_result) {}
const SpvReflectInterfaceVariable* spvReflectGetInputVariable(const SpvReflectShaderModule* p_module, uint32_t location,
                                                              SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableByLocation(const SpvReflectShaderModule* p_module,
                                                                                  const char* entry_point, uint32_t location,
                                                                                  SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetInputVariableBySemantic(const SpvReflectShaderModule* p_module,
                                                                        const char* semantic, SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableBySemantic(const SpvReflectShaderModule* p_module,
                                                                                  const char* entry_point, const char* semantic,
                                                                                  SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetOutputVariableByLocation(const SpvReflectShaderModule* p_module, uint32_t location,
                                                                         SpvReflectResult* p_result) {}
const SpvReflectInterfaceVariable* spvReflectGetOutputVariable(const SpvReflectShaderModule* p_module, uint32_t location,
                                                               SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableByLocation(const SpvReflectShaderModule* p_module,
                                                                                   const char* entry_point, uint32_t location,
                                                                                   SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetOutputVariableBySemantic(const SpvReflectShaderModule* p_module,
                                                                         const char* semantic, SpvReflectResult* p_result) {}

const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableBySemantic(const SpvReflectShaderModule* p_module,
                                                                                   const char* entry_point, const char* semantic,
                                                                                   SpvReflectResult* p_result) {}

const SpvReflectBlockVariable* spvReflectGetPushConstantBlock(const SpvReflectShaderModule* p_module, uint32_t index,
                                                              SpvReflectResult* p_result) {}
const SpvReflectBlockVariable* spvReflectGetPushConstant(const SpvReflectShaderModule* p_module, uint32_t index,
                                                         SpvReflectResult* p_result) {}

const SpvReflectBlockVariable* spvReflectGetEntryPointPushConstantBlock(const SpvReflectShaderModule* p_module,
                                                                        const char* entry_point, SpvReflectResult* p_result) {}

SpvReflectResult spvReflectChangeDescriptorBindingNumbers(SpvReflectShaderModule* p_module,
                                                          const SpvReflectDescriptorBinding* p_binding, uint32_t new_binding_number,
                                                          uint32_t new_set_binding) {}
SpvReflectResult spvReflectChangeDescriptorBindingNumber(SpvReflectShaderModule* p_module,
                                                         const SpvReflectDescriptorBinding* p_descriptor_binding,
                                                         uint32_t new_binding_number, uint32_t optional_new_set_number) {}

SpvReflectResult spvReflectChangeDescriptorSetNumber(SpvReflectShaderModule* p_module, const SpvReflectDescriptorSet* p_set,
                                                     uint32_t new_set_number) {}

static SpvReflectResult ChangeVariableLocation(SpvReflectShaderModule* p_module, SpvReflectInterfaceVariable* p_variable,
                                               uint32_t new_location) {}

SpvReflectResult spvReflectChangeInputVariableLocation(SpvReflectShaderModule* p_module,
                                                       const SpvReflectInterfaceVariable* p_input_variable, uint32_t new_location) {}

SpvReflectResult spvReflectChangeOutputVariableLocation(SpvReflectShaderModule* p_module,
                                                        const SpvReflectInterfaceVariable* p_output_variable,
                                                        uint32_t new_location) {}

const char* spvReflectSourceLanguage(SpvSourceLanguage source_lang) {}

const char* spvReflectBlockVariableTypeName(const SpvReflectBlockVariable* p_var) {}