From d7df6cf1b4a15fb4305e2b7fd93166b577d9524a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Fri, 22 Dec 2023 18:15:52 -0800 Subject: [PATCH] Updated spirv-cross. --- 3rdparty/spirv-cross/main.cpp | 14 + 3rdparty/spirv-cross/spirv_common.hpp | 5 +- 3rdparty/spirv-cross/spirv_cross.cpp | 71 +-- 3rdparty/spirv-cross/spirv_cross.hpp | 5 +- 3rdparty/spirv-cross/spirv_cross_c.cpp | 12 + 3rdparty/spirv-cross/spirv_cross_c.h | 5 +- 3rdparty/spirv-cross/spirv_glsl.cpp | 134 ++++-- 3rdparty/spirv-cross/spirv_hlsl.cpp | 12 +- 3rdparty/spirv-cross/spirv_msl.cpp | 620 +++++++++++++++---------- 3rdparty/spirv-cross/spirv_msl.hpp | 15 +- 3rdparty/spirv-cross/spirv_parser.cpp | 55 ++- 3rdparty/spirv-cross/spirv_reflect.cpp | 8 +- 12 files changed, 565 insertions(+), 391 deletions(-) diff --git a/3rdparty/spirv-cross/main.cpp b/3rdparty/spirv-cross/main.cpp index 907cf1c22..7e14f1102 100644 --- a/3rdparty/spirv-cross/main.cpp +++ b/3rdparty/spirv-cross/main.cpp @@ -678,6 +678,8 @@ struct CLIArguments bool msl_sample_dref_lod_array_as_grad = false; bool msl_runtime_array_rich_descriptor = false; bool msl_replace_recursive_inputs = false; + bool msl_readwrite_texture_fences = true; + bool msl_agx_manual_cube_grad_fixup = false; const char *msl_combined_sampler_suffix = nullptr; bool glsl_emit_push_constant_as_ubo = false; bool glsl_emit_ubo_as_plain_uniforms = false; @@ -958,6 +960,14 @@ static void print_help_msl() "\t\tSome Metal devices have a bug where the level() argument to\n" "\t\tdepth2d_array::sample_compare() in a fragment shader is biased by some\n" "\t\tunknown amount. This prevents the bias from being added.\n" + "\t[--msl-no-readwrite-texture-fences]:\n\t\tDo not insert fences before each read of a\n" + "\t\tread_write texture. MSL does not guarantee coherence between writes and later reads\n" + "\t\tof read_write textures. If you don't rely on this, you can disable this for a\n" + "\t\tpossible performance improvement.\n" + "\t[--msl-agx-manual-cube-grad-fixup]:\n\t\tManually transform cube texture gradients.\n" + "\t\tAll released Apple Silicon GPUs to date ignore one of the three partial derivatives\n" + "\t\tbased on the selected major axis, and expect the remaining derivatives to be\n" + "\t\tpartially transformed. This fixup gives correct results on Apple Silicon.\n" "\t[--msl-combined-sampler-suffix ]:\n\t\tUses a custom suffix for combined samplers.\n"); // clang-format on } @@ -1236,6 +1246,8 @@ static string compile_iteration(const CLIArguments &args, std::vector msl_opts.ios_support_base_vertex_instance = true; msl_opts.runtime_array_rich_descriptor = args.msl_runtime_array_rich_descriptor; msl_opts.replace_recursive_inputs = args.msl_replace_recursive_inputs; + msl_opts.readwrite_texture_fences = args.msl_readwrite_texture_fences; + msl_opts.agx_manual_cube_grad_fixup = args.msl_agx_manual_cube_grad_fixup; msl_comp->set_msl_options(msl_opts); for (auto &v : args.msl_discrete_descriptor_sets) msl_comp->add_discrete_descriptor_set(v); @@ -1790,6 +1802,8 @@ static int main_inner(int argc, char *argv[]) cbs.add("--msl-check-discarded-frag-stores", [&args](CLIParser &) { args.msl_check_discarded_frag_stores = true; }); cbs.add("--msl-sample-dref-lod-array-as-grad", [&args](CLIParser &) { args.msl_sample_dref_lod_array_as_grad = true; }); + cbs.add("--msl-no-readwrite-texture-fences", [&args](CLIParser &) { args.msl_readwrite_texture_fences = false; }); + cbs.add("--msl-agx-manual-cube-grad-fixup", [&args](CLIParser &) { args.msl_agx_manual_cube_grad_fixup = true; }); cbs.add("--msl-combined-sampler-suffix", [&args](CLIParser &parser) { args.msl_combined_sampler_suffix = parser.next_string(); }); diff --git a/3rdparty/spirv-cross/spirv_common.hpp b/3rdparty/spirv-cross/spirv_common.hpp index 2e259228a..7fce7ae7e 100644 --- a/3rdparty/spirv-cross/spirv_common.hpp +++ b/3rdparty/spirv-cross/spirv_common.hpp @@ -548,6 +548,9 @@ struct SPIRType : IVariant type = TypeType }; + spv::Op op = spv::Op::OpNop; + explicit SPIRType(spv::Op op_) : op(op_) {} + enum BaseType { Unknown, @@ -618,7 +621,7 @@ struct SPIRType : IVariant uint32_t sampled; spv::ImageFormat format; spv::AccessQualifier access; - } image; + } image = {}; // Structs can be declared multiple times if they are used as part of interface blocks. // We want to detect this so that we only emit the struct definition once. diff --git a/3rdparty/spirv-cross/spirv_cross.cpp b/3rdparty/spirv-cross/spirv_cross.cpp index 88539550a..4ab985efd 100644 --- a/3rdparty/spirv-cross/spirv_cross.cpp +++ b/3rdparty/spirv-cross/spirv_cross.cpp @@ -627,15 +627,22 @@ bool Compiler::is_matrix(const SPIRType &type) const bool Compiler::is_array(const SPIRType &type) const { - return !type.array.empty(); + return type.op == OpTypeArray || type.op == OpTypeRuntimeArray; +} + +bool Compiler::is_pointer(const SPIRType &type) const +{ + return type.op == OpTypePointer && type.basetype != SPIRType::Unknown; // Ignore function pointers. +} + +bool Compiler::is_physical_pointer(const SPIRType &type) const +{ + return type.op == OpTypePointer && type.storage == StorageClassPhysicalStorageBuffer; } bool Compiler::is_runtime_size_array(const SPIRType &type) { - if (type.array.empty()) - return false; - assert(type.array.size() == type.array_size_literal.size()); - return type.array_size_literal.back() && type.array.back() == 0; + return type.op == OpTypeRuntimeArray; } ShaderResources Compiler::get_shader_resources() const @@ -2738,8 +2745,8 @@ void Compiler::CombinedImageSamplerHandler::register_combined_image_sampler(SPIR auto ptr_type_id = id + 1; auto combined_id = id + 2; auto &base = compiler.expression_type(image_id); - auto &type = compiler.set(type_id); - auto &ptr_type = compiler.set(ptr_type_id); + auto &type = compiler.set(type_id, OpTypeSampledImage); + auto &ptr_type = compiler.set(ptr_type_id, OpTypePointer); type = base; type.self = type_id; @@ -2998,7 +3005,7 @@ bool Compiler::CombinedImageSamplerHandler::handle(Op opcode, const uint32_t *ar { // Have to invent the sampled image type. sampled_type = compiler.ir.increase_bound_by(1); - auto &type = compiler.set(sampled_type); + auto &type = compiler.set(sampled_type, OpTypeSampledImage); type = compiler.expression_type(args[2]); type.self = sampled_type; type.basetype = SPIRType::SampledImage; @@ -3017,7 +3024,7 @@ bool Compiler::CombinedImageSamplerHandler::handle(Op opcode, const uint32_t *ar // Make a new type, pointer to OpTypeSampledImage, so we can make a variable of this type. // We will probably have this type lying around, but it doesn't hurt to make duplicates for internal purposes. - auto &type = compiler.set(type_id); + auto &type = compiler.set(type_id, OpTypePointer); auto &base = compiler.get(sampled_type); type = base; type.pointer = true; @@ -3063,11 +3070,10 @@ VariableID Compiler::build_dummy_sampler_for_combined_images() auto ptr_type_id = offset + 1; auto var_id = offset + 2; - SPIRType sampler_type; - auto &sampler = set(type_id); + auto &sampler = set(type_id, OpTypeSampler); sampler.basetype = SPIRType::Sampler; - auto &ptr_sampler = set(ptr_type_id); + auto &ptr_sampler = set(ptr_type_id, OpTypePointer); ptr_sampler = sampler; ptr_sampler.self = type_id; ptr_sampler.storage = StorageClassUniformConstant; @@ -5497,7 +5503,7 @@ bool Compiler::type_contains_recursion(const SPIRType &type) bool Compiler::type_is_array_of_pointers(const SPIRType &type) const { - if (!type_is_top_level_array(type)) + if (!is_array(type)) return false; // BDA types must have parent type hierarchy. @@ -5506,45 +5512,10 @@ bool Compiler::type_is_array_of_pointers(const SPIRType &type) const // Punch through all array layers. auto *parent = &get(type.parent_type); - while (type_is_top_level_array(*parent)) + while (is_array(*parent)) parent = &get(parent->parent_type); - return type_is_top_level_pointer(*parent); -} - -bool Compiler::type_is_top_level_pointer(const SPIRType &type) const -{ - if (!type.pointer) - return false; - - // Function pointers, should not be hit by valid SPIR-V. - // Parent type will be SPIRFunction instead. - if (type.basetype == SPIRType::Unknown) - return false; - - // Some types are synthesized in-place without complete type hierarchy and might not have parent types, - // but these types are never array-of-pointer or any complicated BDA type, infer reasonable defaults. - if (type.parent_type) - return type.pointer_depth > get(type.parent_type).pointer_depth; - else - return true; -} - -bool Compiler::type_is_top_level_physical_pointer(const SPIRType &type) const -{ - return type_is_top_level_pointer(type) && type.storage == StorageClassPhysicalStorageBuffer; -} - -bool Compiler::type_is_top_level_array(const SPIRType &type) const -{ - if (type.array.empty()) - return false; - - // If we have pointer and array, we infer pointer-to-array as it's the only meaningful thing outside BDA. - if (type.parent_type) - return type.array.size() > get(type.parent_type).array.size(); - else - return !type.pointer; + return is_pointer(*parent); } bool Compiler::flush_phi_required(BlockID from, BlockID to) const diff --git a/3rdparty/spirv-cross/spirv_cross.hpp b/3rdparty/spirv-cross/spirv_cross.hpp index b1fca07f0..9fe6c41c4 100644 --- a/3rdparty/spirv-cross/spirv_cross.hpp +++ b/3rdparty/spirv-cross/spirv_cross.hpp @@ -683,6 +683,8 @@ protected: bool is_vector(const SPIRType &type) const; bool is_matrix(const SPIRType &type) const; bool is_array(const SPIRType &type) const; + bool is_pointer(const SPIRType &type) const; + bool is_physical_pointer(const SPIRType &type) const; static bool is_runtime_size_array(const SPIRType &type); uint32_t expression_type_id(uint32_t id) const; const SPIRType &expression_type(uint32_t id) const; @@ -1148,9 +1150,6 @@ protected: bool check_internal_recursion(const SPIRType &type, std::unordered_set &checked_ids); bool type_contains_recursion(const SPIRType &type); bool type_is_array_of_pointers(const SPIRType &type) const; - bool type_is_top_level_physical_pointer(const SPIRType &type) const; - bool type_is_top_level_pointer(const SPIRType &type) const; - bool type_is_top_level_array(const SPIRType &type) const; bool type_is_block_like(const SPIRType &type) const; bool type_is_top_level_block(const SPIRType &type) const; bool type_is_opaque_value(const SPIRType &type) const; diff --git a/3rdparty/spirv-cross/spirv_cross_c.cpp b/3rdparty/spirv-cross/spirv_cross_c.cpp index c21fdeb6e..dc2e5ea81 100644 --- a/3rdparty/spirv-cross/spirv_cross_c.cpp +++ b/3rdparty/spirv-cross/spirv_cross_c.cpp @@ -742,6 +742,18 @@ spvc_result spvc_compiler_options_set_uint(spvc_compiler_options options, spvc_c case SPVC_COMPILER_OPTION_MSL_SAMPLE_DREF_LOD_ARRAY_AS_GRAD: options->msl.sample_dref_lod_array_as_grad = value != 0; break; + + case SPVC_COMPILER_OPTION_MSL_READWRITE_TEXTURE_FENCES: + options->msl.readwrite_texture_fences = value != 0; + break; + + case SPVC_COMPILER_OPTION_MSL_REPLACE_RECURSIVE_INPUTS: + options->msl.replace_recursive_inputs = value != 0; + break; + + case SPVC_COMPILER_OPTION_MSL_AGX_MANUAL_CUBE_GRAD_FIXUP: + options->msl.agx_manual_cube_grad_fixup = value != 0; + break; #endif default: diff --git a/3rdparty/spirv-cross/spirv_cross_c.h b/3rdparty/spirv-cross/spirv_cross_c.h index 0d8e6e10a..dd9884f5d 100644 --- a/3rdparty/spirv-cross/spirv_cross_c.h +++ b/3rdparty/spirv-cross/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 57 +#define SPVC_C_API_VERSION_MINOR 58 /* Bumped if internal implementation details change. */ #define SPVC_C_API_VERSION_PATCH 0 @@ -725,6 +725,9 @@ typedef enum spvc_compiler_option SPVC_COMPILER_OPTION_MSL_ARGUMENT_BUFFERS_TIER = 84 | SPVC_COMPILER_OPTION_MSL_BIT, SPVC_COMPILER_OPTION_MSL_SAMPLE_DREF_LOD_ARRAY_AS_GRAD = 85 | SPVC_COMPILER_OPTION_MSL_BIT, + SPVC_COMPILER_OPTION_MSL_READWRITE_TEXTURE_FENCES = 86 | SPVC_COMPILER_OPTION_MSL_BIT, + SPVC_COMPILER_OPTION_MSL_REPLACE_RECURSIVE_INPUTS = 87 | SPVC_COMPILER_OPTION_MSL_BIT, + SPVC_COMPILER_OPTION_MSL_AGX_MANUAL_CUBE_GRAD_FIXUP = 88 | SPVC_COMPILER_OPTION_MSL_BIT, SPVC_COMPILER_OPTION_INT_MAX = 0x7fffffff } spvc_compiler_option; diff --git a/3rdparty/spirv-cross/spirv_glsl.cpp b/3rdparty/spirv-cross/spirv_glsl.cpp index 0d64edd77..9b8c7d792 100644 --- a/3rdparty/spirv-cross/spirv_glsl.cpp +++ b/3rdparty/spirv-cross/spirv_glsl.cpp @@ -223,7 +223,7 @@ static const char *to_pls_layout(PlsFormat format) } } -static SPIRType::BaseType pls_format_to_basetype(PlsFormat format) +static std::pair pls_format_to_basetype(PlsFormat format) { switch (format) { @@ -234,17 +234,17 @@ static SPIRType::BaseType pls_format_to_basetype(PlsFormat format) case PlsRGB10A2: case PlsRGBA8: case PlsRG16: - return SPIRType::Float; + return std::make_pair(spv::OpTypeFloat, SPIRType::Float); case PlsRGBA8I: case PlsRG16I: - return SPIRType::Int; + return std::make_pair(spv::OpTypeInt, SPIRType::Int); case PlsRGB10A2UI: case PlsRGBA8UI: case PlsRG16UI: case PlsR32UI: - return SPIRType::UInt; + return std::make_pair(spv::OpTypeInt, SPIRType::UInt); } } @@ -1529,7 +1529,7 @@ uint32_t CompilerGLSL::type_to_packed_alignment(const SPIRType &type, const Bits { // If using PhysicalStorageBufferEXT storage class, this is a pointer, // and is 64-bit. - if (type_is_top_level_physical_pointer(type)) + if (is_physical_pointer(type)) { if (!type.pointer) SPIRV_CROSS_THROW("Types in PhysicalStorageBufferEXT must be pointers."); @@ -1544,7 +1544,7 @@ uint32_t CompilerGLSL::type_to_packed_alignment(const SPIRType &type, const Bits else SPIRV_CROSS_THROW("AddressingModelPhysicalStorageBuffer64EXT must be used for PhysicalStorageBufferEXT."); } - else if (type_is_top_level_array(type)) + else if (is_array(type)) { uint32_t minimum_alignment = 1; if (packing_is_vec4_padded(packing)) @@ -1652,7 +1652,7 @@ uint32_t CompilerGLSL::type_to_packed_size(const SPIRType &type, const Bitset &f { // If using PhysicalStorageBufferEXT storage class, this is a pointer, // and is 64-bit. - if (type_is_top_level_physical_pointer(type)) + if (is_physical_pointer(type)) { if (!type.pointer) SPIRV_CROSS_THROW("Types in PhysicalStorageBufferEXT must be pointers."); @@ -1662,7 +1662,7 @@ uint32_t CompilerGLSL::type_to_packed_size(const SPIRType &type, const Bitset &f else SPIRV_CROSS_THROW("AddressingModelPhysicalStorageBuffer64EXT must be used for PhysicalStorageBufferEXT."); } - else if (type_is_top_level_array(type)) + else if (is_array(type)) { uint32_t packed_size = to_array_size_literal(type) * type_to_packed_array_stride(type, flags, packing); @@ -1840,7 +1840,7 @@ bool CompilerGLSL::buffer_is_packing_standard(const SPIRType &type, BufferPackin } // Verify array stride rules. - if (type_is_top_level_array(memb_type) && + if (is_array(memb_type) && type_to_packed_array_stride(memb_type, member_flags, packing) != type_struct_member_array_stride(type, i)) { @@ -2489,7 +2489,7 @@ void CompilerGLSL::emit_buffer_block_flattened(const SPIRVariable &var) SPIRType::BaseType basic_type; if (get_common_basic_type(type, basic_type)) { - SPIRType tmp; + SPIRType tmp { OpTypeVector }; tmp.basetype = basic_type; tmp.vecsize = 4; if (basic_type != SPIRType::Float && basic_type != SPIRType::Int && basic_type != SPIRType::UInt) @@ -3926,6 +3926,7 @@ void CompilerGLSL::emit_output_variable_initializer(const SPIRVariable &var) auto &member_type = get(member_type_id); auto array_type = member_type; array_type.parent_type = member_type_id; + array_type.op = OpTypeArray; array_type.array.push_back(array_size); array_type.array_size_literal.push_back(true); @@ -3949,10 +3950,9 @@ void CompilerGLSL::emit_output_variable_initializer(const SPIRVariable &var) if (is_control_point) { uint32_t ids = ir.increase_bound_by(3); - SPIRType uint_type; + auto &uint_type = set(ids, OpTypeInt); uint_type.basetype = SPIRType::UInt; uint_type.width = 32; - set(ids, uint_type); set(ids + 1, builtin_to_glsl(BuiltInInvocationId, StorageClassInput), ids, true); set(ids + 2, ids, i, false); invocation_id = ids + 1; @@ -5148,7 +5148,7 @@ string CompilerGLSL::to_rerolled_array_expression(const SPIRType &parent_type, type.basetype == SPIRType::Boolean && backend.boolean_in_struct_remapped_type != SPIRType::Boolean; - SPIRType tmp_type; + SPIRType tmp_type { OpNop }; if (remapped_boolean) { tmp_type = get(type.parent_type); @@ -5169,7 +5169,7 @@ string CompilerGLSL::to_rerolled_array_expression(const SPIRType &parent_type, for (uint32_t i = 0; i < size; i++) { auto subexpr = join(base_expr, "[", convert_to_string(i), "]"); - if (!type_is_top_level_array(parent)) + if (!is_array(parent)) { if (remapped_boolean) subexpr = join(type_to_glsl(tmp_type), "(", subexpr, ")"); @@ -5195,7 +5195,7 @@ string CompilerGLSL::to_composite_constructor_expression(const SPIRType &parent_ type.basetype == SPIRType::Boolean && backend.boolean_in_struct_remapped_type != SPIRType::Boolean; - if (type_is_top_level_array(type)) + if (is_array(type)) { reroll_array = !backend.array_is_value_type || (block_like_type && !backend.array_is_value_type_in_buffer_blocks); @@ -5748,7 +5748,7 @@ string CompilerGLSL::constant_expression(const SPIRConstant &c, { auto &type = get(c.constant_type); - if (type_is_top_level_pointer(type)) + if (is_pointer(type)) { return backend.null_pointer_literal; } @@ -5763,21 +5763,21 @@ string CompilerGLSL::constant_expression(const SPIRConstant &c, // with Offset = 0, using no ArrayStride on the enclosed array type. // A particular CTS test hits this scenario. bool array_type_decays = inside_block_like_struct_scope && - type_is_top_level_array(type) && + is_array(type) && !backend.array_is_value_type_in_buffer_blocks; // Allow Metal to use the array template to make arrays a value type bool needs_trailing_tracket = false; if (backend.use_initializer_list && backend.use_typed_initializer_list && type.basetype == SPIRType::Struct && - !type_is_top_level_array(type)) + !is_array(type)) { res = type_to_glsl_constructor(type) + "{ "; } else if (backend.use_initializer_list && backend.use_typed_initializer_list && backend.array_is_value_type && - type_is_top_level_array(type) && !array_type_decays) + is_array(type) && !array_type_decays) { const auto *p_type = &type; - SPIRType tmp_type; + SPIRType tmp_type { OpNop }; if (inside_struct_scope && backend.boolean_in_struct_remapped_type != SPIRType::Boolean && @@ -5818,7 +5818,7 @@ string CompilerGLSL::constant_expression(const SPIRConstant &c, res += to_name(elem); else { - if (!type_is_top_level_array(type) && type.basetype == SPIRType::Struct) + if (!is_array(type) && type.basetype == SPIRType::Struct) { // When we get down to emitting struct members, override the block-like information. // For constants, we can freely mix and match block-like state. @@ -5916,7 +5916,7 @@ string CompilerGLSL::convert_half_to_string(const SPIRConstant &c, uint32_t col, // of complicated workarounds, just value-cast to the half type always. if (std::isnan(float_value) || std::isinf(float_value)) { - SPIRType type; + SPIRType type { OpTypeFloat }; type.basetype = SPIRType::Half; type.vecsize = 1; type.columns = 1; @@ -5932,7 +5932,7 @@ string CompilerGLSL::convert_half_to_string(const SPIRConstant &c, uint32_t col, } else { - SPIRType type; + SPIRType type { OpTypeFloat }; type.basetype = SPIRType::Half; type.vecsize = 1; type.columns = 1; @@ -5952,8 +5952,8 @@ string CompilerGLSL::convert_float_to_string(const SPIRConstant &c, uint32_t col // Use special representation. if (!is_legacy()) { - SPIRType out_type; - SPIRType in_type; + SPIRType out_type { OpTypeFloat }; + SPIRType in_type { OpTypeInt }; out_type.basetype = SPIRType::Float; in_type.basetype = SPIRType::UInt; out_type.vecsize = 1; @@ -6022,8 +6022,8 @@ std::string CompilerGLSL::convert_double_to_string(const SPIRConstant &c, uint32 // Use special representation. if (!is_legacy()) { - SPIRType out_type; - SPIRType in_type; + SPIRType out_type { OpTypeFloat }; + SPIRType in_type { OpTypeInt }; out_type.basetype = SPIRType::Double; in_type.basetype = SPIRType::UInt64; out_type.vecsize = 1; @@ -6731,7 +6731,7 @@ SPIRType CompilerGLSL::binary_op_bitcast_helper(string &cast_op0, string &cast_o // Create a fake type so we can bitcast to it. // We only deal with regular arithmetic types here like int, uints and so on. - SPIRType expected_type; + SPIRType expected_type{type0.op}; expected_type.basetype = input_type; expected_type.vecsize = type0.vecsize; expected_type.columns = type0.columns; @@ -7085,7 +7085,9 @@ void CompilerGLSL::emit_bitfield_insert_op(uint32_t result_type, uint32_t result auto op2_expr = to_unpacked_expression(op2); auto op3_expr = to_unpacked_expression(op3); - SPIRType target_type; + assert(offset_count_type == SPIRType::UInt || offset_count_type == SPIRType::Int); + SPIRType target_type { OpTypeInt }; + target_type.width = 32; target_type.vecsize = 1; target_type.basetype = offset_count_type; @@ -7876,7 +7878,7 @@ bool CompilerGLSL::expression_is_constant_null(uint32_t id) const bool CompilerGLSL::expression_is_non_value_type_array(uint32_t ptr) { auto &type = expression_type(ptr); - if (!type_is_top_level_array(get_pointee_type(type))) + if (!is_array(get_pointee_type(type))) return false; if (!backend.array_is_value_type) @@ -9610,6 +9612,8 @@ string CompilerGLSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage) return "gl_TessLevelInner"; case BuiltInTessCoord: return "gl_TessCoord"; + case BuiltInPatchVertices: + return "gl_PatchVerticesIn"; case BuiltInFragCoord: return "gl_FragCoord"; case BuiltInPointCoord: @@ -9912,16 +9916,21 @@ void CompilerGLSL::access_chain_internal_append_index(std::string &expr, uint32_ if (ptr_chain && access_chain_is_arrayed) { size_t split_pos = expr.find_last_of(']'); - string expr_front = expr.substr(0, split_pos); - string expr_back = expr.substr(split_pos); - expr = expr_front + " + " + enclose_expression(idx_expr) + expr_back; - } - else - { - expr += "["; - expr += idx_expr; - expr += "]"; + size_t enclose_split = expr.find_last_of(')'); + + // If we have already enclosed the expression, don't try to be clever, it will break. + if (split_pos > enclose_split || enclose_split == string::npos) + { + string expr_front = expr.substr(0, split_pos); + string expr_back = expr.substr(split_pos); + expr = expr_front + " + " + enclose_expression(idx_expr) + expr_back; + return; + } } + + expr += "["; + expr += idx_expr; + expr += "]"; } bool CompilerGLSL::access_chain_needs_stage_io_builtin_translation(uint32_t) @@ -9956,6 +9965,7 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice // Start traversing type hierarchy at the proper non-pointer types, // but keep type_id referencing the original pointer for use below. uint32_t type_id = expression_type_id(base); + const auto *type = &get_pointee_type(type_id); if (!backend.native_pointers) { @@ -9965,13 +9975,10 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice // Wrapped buffer reference pointer types will need to poke into the internal "value" member before // continuing the access chain. if (should_dereference(base)) - { - auto &type = get(type_id); - expr = dereference_expression(type, expr); - } + expr = dereference_expression(get(type_id), expr); } - - const auto *type = &get_pointee_type(type_id); + else if (should_dereference(base) && type->basetype != SPIRType::Struct && !ptr_chain) + expr = join("(", dereference_expression(*type, expr), ")"); bool access_chain_is_arrayed = expr.find_first_of('[') != string::npos; bool row_major_matrix_needs_conversion = is_non_native_row_major_matrix(base); @@ -10012,9 +10019,21 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice index &= 0x7fffffffu; } - // Pointer chains + bool ptr_chain_array_entry = ptr_chain && i == 0 && is_array(*type); + + if (ptr_chain_array_entry) + { + // This is highly unusual code, since normally we'd use plain AccessChain, but it's still allowed. + // We are considered to have a pointer to array and one element shifts by one array at a time. + // If we use normal array indexing, we'll first decay to pointer, and lose the array-ness, + // so we have to take pointer to array explicitly. + if (!should_dereference(base)) + expr = enclose_expression(address_of_expression(expr)); + } + if (ptr_chain && i == 0) { + // Pointer chains // If we are flattening multidimensional arrays, only create opening bracket on first // array index. if (options.flatten_multidimensional_arrays) @@ -10059,6 +10078,12 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice } access_chain_is_arrayed = true; + + // Explicitly enclose the expression if this is one of the weird pointer-to-array cases. + // We don't want any future indexing to add to this array dereference. + // Enclosing the expression blocks that and avoids any shenanigans with operand priority. + if (ptr_chain_array_entry) + expr = join("(", expr, ")"); } // Arrays else if (!type->array.empty()) @@ -15340,9 +15365,16 @@ string CompilerGLSL::pls_decl(const PlsRemap &var) { auto &variable = get(var.id); - SPIRType type; - type.vecsize = pls_format_to_components(var.format); - type.basetype = pls_format_to_basetype(var.format); + auto op_and_basetype = pls_format_to_basetype(var.format); + + SPIRType type { op_and_basetype.first }; + type.basetype = op_and_basetype.second; + auto vecsize = pls_format_to_components(var.format); + if (vecsize > 1) + { + type.op = OpTypeVector; + type.vecsize = vecsize; + } return join(to_pls_layout(var.format), to_pls_qualifiers_glsl(variable), type_to_glsl(type), " ", to_name(variable.self)); @@ -17653,7 +17685,7 @@ bool CompilerGLSL::unroll_array_to_complex_store(uint32_t target_id, uint32_t so else array_expr = to_expression(type.array.back()); - SPIRType target_type; + SPIRType target_type { OpTypeInt }; target_type.basetype = SPIRType::Int; statement("for (int i = 0; i < int(", array_expr, "); i++)"); @@ -17718,7 +17750,7 @@ void CompilerGLSL::unroll_array_from_complex_load(uint32_t target_id, uint32_t s statement(new_expr, "[i] = gl_in[i].", expr, ";"); else if (is_sample_mask) { - SPIRType target_type; + SPIRType target_type { OpTypeInt }; target_type.basetype = SPIRType::Int; statement(new_expr, "[i] = ", bitcast_expression(target_type, type.basetype, join(expr, "[i]")), ";"); } diff --git a/3rdparty/spirv-cross/spirv_hlsl.cpp b/3rdparty/spirv-cross/spirv_hlsl.cpp index c0706e413..9b8349365 100644 --- a/3rdparty/spirv-cross/spirv_hlsl.cpp +++ b/3rdparty/spirv-cross/spirv_hlsl.cpp @@ -2432,7 +2432,7 @@ void CompilerHLSL::analyze_meshlet_writes() uint32_t op_ptr = op_type + 2; uint32_t op_var = op_type + 3; - auto &type = set(op_type); + auto &type = set(op_type, OpTypeStruct); type.basetype = SPIRType::Struct; set_name(op_type, block_name); set_decoration(op_type, DecorationBlock); @@ -4508,7 +4508,7 @@ void CompilerHLSL::read_access_chain(string *expr, const string &lhs, const SPIR { auto &type = get(chain.basetype); - SPIRType target_type; + SPIRType target_type { is_scalar(type) ? OpTypeInt : type.op }; target_type.basetype = SPIRType::UInt; target_type.vecsize = type.vecsize; target_type.columns = type.columns; @@ -4755,7 +4755,7 @@ void CompilerHLSL::write_access_chain_array(const SPIRAccessChain &chain, uint32 uint32_t id = ir.increase_bound_by(2); uint32_t int_type_id = id + 1; - SPIRType int_type; + SPIRType int_type { OpTypeInt }; int_type.basetype = SPIRType::Int; int_type.width = 32; set(int_type_id, int_type); @@ -4843,7 +4843,7 @@ void CompilerHLSL::write_access_chain(const SPIRAccessChain &chain, uint32_t val // Make sure we trigger a read of the constituents in the access chain. track_expression_read(chain.self); - SPIRType target_type; + SPIRType target_type { is_scalar(type) ? OpTypeInt : type.op }; target_type.basetype = SPIRType::UInt; target_type.vecsize = type.vecsize; target_type.columns = type.columns; @@ -6583,14 +6583,14 @@ VariableID CompilerHLSL::remap_num_workgroups_builtin() uint32_t block_pointer_type_id = offset + 2; uint32_t variable_id = offset + 3; - SPIRType uint_type; + SPIRType uint_type { OpTypeVector }; uint_type.basetype = SPIRType::UInt; uint_type.width = 32; uint_type.vecsize = 3; uint_type.columns = 1; set(uint_type_id, uint_type); - SPIRType block_type; + SPIRType block_type { OpTypeStruct }; block_type.basetype = SPIRType::Struct; block_type.member_types.push_back(uint_type_id); set(block_type_id, block_type); diff --git a/3rdparty/spirv-cross/spirv_msl.cpp b/3rdparty/spirv-cross/spirv_msl.cpp index f6d3aaa3b..1167e26c0 100644 --- a/3rdparty/spirv-cross/spirv_msl.cpp +++ b/3rdparty/spirv-cross/spirv_msl.cpp @@ -478,14 +478,14 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 2; // Create gl_FragCoord. - SPIRType vec4_type; + SPIRType vec4_type { OpTypeVector }; vec4_type.basetype = SPIRType::Float; vec4_type.width = 32; vec4_type.vecsize = 4; set(type_id, vec4_type); - SPIRType vec4_type_ptr; - vec4_type_ptr = vec4_type; + SPIRType vec4_type_ptr = vec4_type; + vec4_type_ptr.op = OpTypePointer; vec4_type_ptr.pointer = true; vec4_type_ptr.pointer_depth++; vec4_type_ptr.parent_type = type_id; @@ -506,8 +506,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_Layer. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -528,8 +528,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_ViewIndex. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -551,8 +551,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_SampleID. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -571,8 +571,8 @@ void CompilerMSL::build_implicit_builtins() { uint32_t type_ptr_id = ir.increase_bound_by(1); - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -631,8 +631,8 @@ void CompilerMSL::build_implicit_builtins() // Note that we can't just abuse gl_ViewIndex for this purpose: it's an input, but // gl_Layer is an output in vertex-pipeline shaders. uint32_t type_ptr_out_id = ir.increase_bound_by(2); - SPIRType uint_type_ptr_out; - uint_type_ptr_out = get_uint_type(); + SPIRType uint_type_ptr_out = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr_out.pointer = true; uint_type_ptr_out.pointer_depth++; uint_type_ptr_out.parent_type = get_uint_type_id(); @@ -663,8 +663,8 @@ void CompilerMSL::build_implicit_builtins() { uint32_t type_ptr_id = ir.increase_bound_by(1); - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -723,8 +723,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_SubgroupInvocationID. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -745,8 +745,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_SubgroupSize. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -804,8 +804,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_SampleMask. - SPIRType uint_type_ptr_out; - uint_type_ptr_out = get_uint_type(); + SPIRType uint_type_ptr_out = get_uint_type(); + uint_type_ptr_out.op = OpTypePointer; uint_type_ptr_out.pointer = true; uint_type_ptr_out.pointer_depth++; uint_type_ptr_out.parent_type = get_uint_type_id(); @@ -827,14 +827,14 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 2; // Create gl_HelperInvocation. - SPIRType bool_type; + SPIRType bool_type { OpTypeBool }; bool_type.basetype = SPIRType::Boolean; bool_type.width = 8; bool_type.vecsize = 1; set(type_id, bool_type); - SPIRType bool_type_ptr_in; - bool_type_ptr_in = bool_type; + SPIRType bool_type_ptr_in = bool_type; + bool_type_ptr_in.op = spv::OpTypePointer; bool_type_ptr_in.pointer = true; bool_type_ptr_in.pointer_depth++; bool_type_ptr_in.parent_type = type_id; @@ -855,8 +855,8 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 1; // Create gl_LocalInvocationIndex. - SPIRType uint_type_ptr; - uint_type_ptr = get_uint_type(); + SPIRType uint_type_ptr = get_uint_type(); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = get_uint_type_id(); @@ -879,6 +879,7 @@ void CompilerMSL::build_implicit_builtins() // Create gl_WorkgroupSize. uint32_t type_id = build_extended_vector_type(get_uint_type_id(), 3); SPIRType uint_type_ptr = get(type_id); + uint_type_ptr.op = OpTypePointer; uint_type_ptr.pointer = true; uint_type_ptr.pointer_depth++; uint_type_ptr.parent_type = type_id; @@ -986,14 +987,14 @@ void CompilerMSL::build_implicit_builtins() uint32_t var_id = offset + 2; // Create gl_Position. - SPIRType vec4_type; + SPIRType vec4_type { OpTypeVector }; vec4_type.basetype = SPIRType::Float; vec4_type.width = 32; vec4_type.vecsize = 4; set(type_id, vec4_type); - SPIRType vec4_type_ptr; - vec4_type_ptr = vec4_type; + SPIRType vec4_type_ptr = vec4_type; + vec4_type_ptr.op = OpTypePointer; vec4_type_ptr.pointer = true; vec4_type_ptr.pointer_depth++; vec4_type_ptr.parent_type = type_id; @@ -1070,6 +1071,7 @@ uint32_t CompilerMSL::build_constant_uint_array_pointer() // Create a buffer to hold extra data, including the swizzle constants. SPIRType uint_type_pointer = get_uint_type(); + uint_type_pointer.op = OpTypePointer; uint_type_pointer.pointer = true; uint_type_pointer.pointer_depth++; uint_type_pointer.parent_type = get_uint_type_id(); @@ -1148,7 +1150,7 @@ uint32_t CompilerMSL::get_uint_type_id() uint_type_id = ir.increase_bound_by(1); - SPIRType type; + SPIRType type { OpTypeInt }; type.basetype = SPIRType::UInt; type.width = 32; set(uint_type_id, type); @@ -1462,7 +1464,7 @@ void CompilerMSL::emit_entry_point_declarations() { auto &var = get(var_id); add_local_variable_name(var_id); - statement(variable_decl(var), ";"); + statement(CompilerGLSL::variable_decl(var), ";"); var.deferred_declaration = false; } } @@ -1921,8 +1923,11 @@ void CompilerMSL::extract_global_variables_from_function(uint32_t func_id, std:: // When using the pointer, we need to know which variable it is actually loaded from. uint32_t base_id = ops[2]; auto *var = maybe_get_backing_variable(base_id); - if (var && atomic_image_vars.count(var->self)) + if (var && atomic_image_vars_emulated.count(var->self)) { + if (!get(var->basetype).array.empty()) + SPIRV_CROSS_THROW("Cannot emulate array of storage images with atomics. Use MSL 3.1 for native support."); + if (global_var_ids.find(base_id) != global_var_ids.end()) added_arg_ids.insert(base_id); } @@ -2328,9 +2333,27 @@ uint32_t CompilerMSL::get_target_components_for_fragment_location(uint32_t locat uint32_t CompilerMSL::build_extended_vector_type(uint32_t type_id, uint32_t components, SPIRType::BaseType basetype) { + assert(components > 1); uint32_t new_type_id = ir.increase_bound_by(1); - auto &old_type = get(type_id); - auto *type = &set(new_type_id, old_type); + const auto *p_old_type = &get(type_id); + const SPIRType *old_ptr_t = nullptr; + const SPIRType *old_array_t = nullptr; + + if (is_pointer(*p_old_type)) + { + old_ptr_t = p_old_type; + p_old_type = &get_pointee_type(*old_ptr_t); + } + + if (is_array(*p_old_type)) + { + old_array_t = p_old_type; + p_old_type = &get_type(old_array_t->parent_type); + } + + auto *type = &set(new_type_id, *p_old_type); + assert(is_scalar(*type) || is_vector(*type)); + type->op = OpTypeVector; type->vecsize = components; if (basetype != SPIRType::Unknown) type->basetype = basetype; @@ -2340,23 +2363,24 @@ uint32_t CompilerMSL::build_extended_vector_type(uint32_t type_id, uint32_t comp type->array_size_literal.clear(); type->pointer = false; - if (is_array(old_type)) + if (old_array_t) { uint32_t array_type_id = ir.increase_bound_by(1); type = &set(array_type_id, *type); + type->op = OpTypeArray; type->parent_type = new_type_id; - type->array = old_type.array; - type->array_size_literal = old_type.array_size_literal; + type->array = old_array_t->array; + type->array_size_literal = old_array_t->array_size_literal; new_type_id = array_type_id; } - if (old_type.pointer) + if (old_ptr_t) { uint32_t ptr_type_id = ir.increase_bound_by(1); type = &set(ptr_type_id, *type); - type->self = new_type_id; + type->op = OpTypePointer; type->parent_type = new_type_id; - type->storage = old_type.storage; + type->storage = old_ptr_t->storage; type->pointer = true; type->pointer_depth++; new_type_id = ptr_type_id; @@ -3433,9 +3457,6 @@ void CompilerMSL::emit_local_masked_variable(const SPIRVariable &masked_var, boo auto &type = get_variable_data_type(masked_var); add_local_variable_name(masked_var.self); - bool old_is_builtin = is_using_builtin_array; - is_using_builtin_array = true; - const uint32_t max_control_points_per_patch = 32u; uint32_t max_num_instances = (max_control_points_per_patch + get_entry_point().output_vertices - 1u) / @@ -3451,14 +3472,12 @@ void CompilerMSL::emit_local_masked_variable(const SPIRVariable &masked_var, boo // since Metal does not allow that. :( // FIXME: We will likely need an option to support passing down target workgroup size, // so we can emit appropriate size here. - statement("threadgroup ", type_to_glsl(type), " ", - "(&", to_name(masked_var.self), ")", - type_to_array_glsl(type), " = spvStorage", to_name(masked_var.self), "[", + statement("threadgroup auto ", + "&", to_name(masked_var.self), + " = spvStorage", to_name(masked_var.self), "[", "(", to_expression(builtin_invocation_id_id), ".x / ", get_entry_point().output_vertices, ") % ", max_num_instances, "];"); - - is_using_builtin_array = old_is_builtin; }); } else @@ -3934,7 +3953,7 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) // declaraion is emitted, because it is cleared after each compilation pass. uint32_t next_id = ir.increase_bound_by(3); uint32_t ib_type_id = next_id++; - auto &ib_type = set(ib_type_id); + auto &ib_type = set(ib_type_id, OpTypeStruct); ib_type.basetype = SPIRType::Struct; ib_type.storage = storage; set_decoration(ib_type_id, DecorationBlock); @@ -4157,13 +4176,14 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) continue; // Create a fake variable to put at the location. - uint32_t offset = ir.increase_bound_by(4); + uint32_t offset = ir.increase_bound_by(5); uint32_t type_id = offset; - uint32_t array_type_id = offset + 1; - uint32_t ptr_type_id = offset + 2; - uint32_t var_id = offset + 3; + uint32_t vec_type_id = offset + 1; + uint32_t array_type_id = offset + 2; + uint32_t ptr_type_id = offset + 3; + uint32_t var_id = offset + 4; - SPIRType type; + SPIRType type { OpTypeInt }; switch (input.second.format) { case MSL_SHADER_VARIABLE_FORMAT_UINT16: @@ -4177,14 +4197,23 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) type.width = 32; break; } - type.vecsize = input.second.vecsize; set(type_id, type); + if (input.second.vecsize > 1) + { + type.op = OpTypeVector; + type.vecsize = input.second.vecsize; + set(vec_type_id, type); + type_id = vec_type_id; + } + type.op = OpTypeArray; type.array.push_back(0); type.array_size_literal.push_back(true); type.parent_type = type_id; set(array_type_id, type); + type.self = type_id; + type.op = OpTypePointer; type.pointer = true; type.pointer_depth++; type.parent_type = array_type_id; @@ -4215,13 +4244,14 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) continue; // Create a fake variable to put at the location. - uint32_t offset = ir.increase_bound_by(4); + uint32_t offset = ir.increase_bound_by(5); uint32_t type_id = offset; - uint32_t array_type_id = offset + 1; - uint32_t ptr_type_id = offset + 2; - uint32_t var_id = offset + 3; + uint32_t vec_type_id = offset + 1; + uint32_t array_type_id = offset + 2; + uint32_t ptr_type_id = offset + 3; + uint32_t var_id = offset + 4; - SPIRType type; + SPIRType type { OpTypeInt }; switch (output.second.format) { case MSL_SHADER_VARIABLE_FORMAT_UINT16: @@ -4235,17 +4265,25 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch) type.width = 32; break; } - type.vecsize = output.second.vecsize; set(type_id, type); + if (output.second.vecsize > 1) + { + type.op = OpTypeVector; + type.vecsize = output.second.vecsize; + set(vec_type_id, type); + type_id = vec_type_id; + } if (is_tesc_shader()) { + type.op = OpTypeArray; type.array.push_back(0); type.array_size_literal.push_back(true); type.parent_type = type_id; set(array_type_id, type); } + type.op = OpTypePointer; type.pointer = true; type.pointer_depth++; type.parent_type = is_tesc_shader() ? array_type_id : type_id; @@ -4332,6 +4370,7 @@ uint32_t CompilerMSL::add_interface_block_pointer(uint32_t ib_var_id, StorageCla // do the same with our struct here. uint32_t ib_ptr_type_id = next_id++; auto &ib_ptr_type = set(ib_ptr_type_id, ib_type); + ib_ptr_type.op = OpTypePointer; ib_ptr_type.parent_type = ib_ptr_type.type_alias = ib_type.self; ib_ptr_type.pointer = true; ib_ptr_type.pointer_depth++; @@ -4386,23 +4425,24 @@ uint32_t CompilerMSL::add_interface_block_pointer(uint32_t ib_var_id, StorageCla uint32_t CompilerMSL::ensure_correct_builtin_type(uint32_t type_id, BuiltIn builtin) { auto &type = get(type_id); + auto &pointee_type = get_pointee_type(type); - if ((builtin == BuiltInSampleMask && is_array(type)) || + if ((builtin == BuiltInSampleMask && is_array(pointee_type)) || ((builtin == BuiltInLayer || builtin == BuiltInViewportIndex || builtin == BuiltInFragStencilRefEXT) && - type.basetype != SPIRType::UInt)) + pointee_type.basetype != SPIRType::UInt)) { - uint32_t next_id = ir.increase_bound_by(type.pointer ? 2 : 1); + uint32_t next_id = ir.increase_bound_by(type_is_pointer(type) ? 2 : 1); uint32_t base_type_id = next_id++; - auto &base_type = set(base_type_id); + auto &base_type = set(base_type_id, OpTypeInt); base_type.basetype = SPIRType::UInt; base_type.width = 32; - if (!type.pointer) + if (!type_is_pointer(type)) return base_type_id; uint32_t ptr_type_id = next_id++; - auto &ptr_type = set(ptr_type_id); - ptr_type = base_type; + auto &ptr_type = set(ptr_type_id, base_type); + ptr_type.op = spv::OpTypePointer; ptr_type.pointer = true; ptr_type.pointer_depth++; ptr_type.storage = type.storage; @@ -4892,6 +4932,7 @@ void CompilerMSL::ensure_member_packing_rules_msl(SPIRType &ib_type, uint32_t in { type.columns = 1; assert(type.array.empty()); + type.op = OpTypeArray; type.array.push_back(1); type.array_size_literal.push_back(true); } @@ -4908,6 +4949,7 @@ void CompilerMSL::ensure_member_packing_rules_msl(SPIRType &ib_type, uint32_t in type.vecsize = type.columns; type.columns = 1; assert(type.array.empty()); + type.op = OpTypeArray; type.array.push_back(1); type.array_size_literal.push_back(true); } @@ -4932,7 +4974,7 @@ void CompilerMSL::emit_store_statement(uint32_t lhs_expression, uint32_t rhs_exp if (has_decoration(lhs_expression, DecorationBuiltIn) && BuiltIn(get_decoration(lhs_expression, DecorationBuiltIn)) == BuiltInSampleMask && - type_is_top_level_array(type)) + is_array(type)) { // Storing an array to SampleMask, have to remove the array-ness before storing. statement(to_expression(lhs_expression), " = ", to_enclosed_unpacked_expression(rhs_expression), "[0];"); @@ -5725,6 +5767,31 @@ void CompilerMSL::emit_custom_functions() break; } + // Fix up gradient vectors when sampling a cube texture for Apple Silicon. + // h/t Alexey Knyazev (https://github.com/KhronosGroup/MoltenVK/issues/2068#issuecomment-1817799067) for the code. + case SPVFuncImplGradientCube: + statement("static inline gradientcube spvGradientCube(float3 P, float3 dPdx, float3 dPdy)"); + begin_scope(); + statement("// Major axis selection"); + statement("float3 absP = abs(P);"); + statement("bool xMajor = absP.x >= max(absP.y, absP.z);"); + statement("bool yMajor = absP.y >= absP.z;"); + statement("float3 Q = xMajor ? P.yzx : (yMajor ? P.xzy : P);"); + statement("float3 dQdx = xMajor ? dPdx.yzx : (yMajor ? dPdx.xzy : dPdx);"); + statement("float3 dQdy = xMajor ? dPdy.yzx : (yMajor ? dPdy.xzy : dPdy);"); + statement_no_indent(""); + statement("// Skip a couple of operations compared to usual projection"); + statement("float4 d = float4(dQdx.xy, dQdy.xy) - (Q.xy / Q.z).xyxy * float4(dQdx.zz, dQdy.zz);"); + statement_no_indent(""); + statement("// Final swizzle to put the intermediate values into non-ignored components"); + statement("// X major: X and Z"); + statement("// Y major: X and Y"); + statement("// Z major: Y and Z"); + statement("return gradientcube(xMajor ? d.xxy : d.xyx, xMajor ? d.zzw : d.zwz);"); + end_scope(); + statement(""); + break; + // "fadd" intrinsic support case SPVFuncImplFAdd: statement("template"); @@ -7423,7 +7490,7 @@ void CompilerMSL::declare_constant_arrays() // FIXME: However, hoisting constants to main() means we need to pass down constant arrays to leaf functions if they are used there. // If there are multiple functions in the module, drop this case to avoid breaking use cases which do not need to // link into Metal libraries. This is hacky. - if (type_is_top_level_array(type) && (!fully_inlined || is_scalar(type) || is_vector(type))) + if (is_array(type) && (!fully_inlined || is_scalar(type) || is_vector(type))) { add_resource_name(c.self); auto name = to_name(c.self); @@ -7455,7 +7522,7 @@ void CompilerMSL::declare_complex_constant_arrays() return; auto &type = this->get(c.constant_type); - if (type_is_top_level_array(type) && !(is_scalar(type) || is_vector(type))) + if (is_array(type) && !(is_scalar(type) || is_vector(type))) { add_resource_name(c.self); auto name = to_name(c.self); @@ -8194,8 +8261,9 @@ bool CompilerMSL::emit_tessellation_access_chain(const uint32_t *ops, uint32_t l // We're not going to emit the actual member name, we let any further OpLoad take care of that. // Tag the access chain with the member index we're referencing. - bool defer_access_chain = flatten_composites && (is_matrix(result_ptr_type) || is_array(result_ptr_type) || - result_ptr_type.basetype == SPIRType::Struct); + auto &result_pointee_type = get_pointee_type(result_ptr_type); + bool defer_access_chain = flatten_composites && (is_matrix(result_pointee_type) || is_array(result_pointee_type) || + result_pointee_type.basetype == SPIRType::Struct); if (!defer_access_chain) { @@ -8731,7 +8799,7 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) uint32_t ptr = ops[2]; uint32_t mem_sem = ops[4]; uint32_t val = ops[5]; - emit_atomic_func_op(result_type, id, "atomic_exchange_explicit", opcode, mem_sem, mem_sem, false, ptr, val); + emit_atomic_func_op(result_type, id, "atomic_exchange", opcode, mem_sem, mem_sem, false, ptr, val); break; } @@ -8744,7 +8812,7 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) uint32_t mem_sem_fail = ops[5]; uint32_t val = ops[6]; uint32_t comp = ops[7]; - emit_atomic_func_op(result_type, id, "atomic_compare_exchange_weak_explicit", opcode, + emit_atomic_func_op(result_type, id, "atomic_compare_exchange_weak", opcode, mem_sem_pass, mem_sem_fail, true, ptr, comp, true, false, val); break; @@ -8759,7 +8827,8 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) uint32_t id = ops[1]; uint32_t ptr = ops[2]; uint32_t mem_sem = ops[4]; - emit_atomic_func_op(result_type, id, "atomic_load_explicit", opcode, mem_sem, mem_sem, false, ptr, 0); + check_atomic_image(ptr); + emit_atomic_func_op(result_type, id, "atomic_load", opcode, mem_sem, mem_sem, false, ptr, 0); break; } @@ -8770,7 +8839,8 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) uint32_t ptr = ops[0]; uint32_t mem_sem = ops[2]; uint32_t val = ops[3]; - emit_atomic_func_op(result_type, id, "atomic_store_explicit", opcode, mem_sem, mem_sem, false, ptr, val); + check_atomic_image(ptr); + emit_atomic_func_op(result_type, id, "atomic_store", opcode, mem_sem, mem_sem, false, ptr, val); break; } @@ -8782,7 +8852,7 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) uint32_t ptr = ops[2]; \ uint32_t mem_sem = ops[4]; \ uint32_t val = valsrc; \ - emit_atomic_func_op(result_type, id, "atomic_fetch_" #op "_explicit", opcode, \ + emit_atomic_func_op(result_type, id, "atomic_fetch_" #op, opcode, \ mem_sem, mem_sem, false, ptr, val, \ false, valconst); \ } while (false) @@ -8860,7 +8930,7 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) { // When using the pointer, we need to know which variable it is actually loaded from. auto *var = maybe_get_backing_variable(ops[2]); - if (var && atomic_image_vars.count(var->self)) + if (var && atomic_image_vars_emulated.count(var->self)) { uint32_t result_type = ops[0]; uint32_t id = ops[1]; @@ -8880,8 +8950,14 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) { uint32_t result_type = ops[0]; uint32_t id = ops[1]; + + // Virtual expression. Split this up in the actual image atomic. + // In GLSL and HLSL we are able to resolve the dereference inline, but MSL has + // image.op(coord, ...) syntax. auto &e = - set(id, join(to_expression(ops[2]), ", ", to_expression(ops[3])), result_type, true); + set(id, join(to_expression(ops[2]), "@", + bitcast_expression(SPIRType::UInt, ops[3])), + result_type, true); // When using the pointer, we need to know which variable it is actually loaded from. e.loaded_from = var ? var->self : ID(0); @@ -9436,7 +9512,7 @@ void CompilerMSL::emit_instruction(const Instruction &instruction) add_spv_func_and_recompile(SPVFuncImplRayQueryIntersectionParams); statement(to_expression(ops[0]), ".reset(", "ray(", to_expression(ops[4]), ", ", to_expression(ops[6]), ", ", - to_expression(ops[5]), ", ", to_expression(ops[7]), "), ", to_expression(ops[1]), + to_expression(ops[5]), ", ", to_expression(ops[7]), "), ", to_expression(ops[1]), ", ", to_expression(ops[3]), ", spvMakeIntersectionParams(", to_expression(ops[2]), "));"); break; } @@ -9689,8 +9765,8 @@ bool CompilerMSL::emit_array_copy(const char *expr, uint32_t lhs_id, uint32_t rh bool lhs_is_thread_storage = storage_class_array_is_thread(lhs_storage); bool rhs_is_thread_storage = storage_class_array_is_thread(rhs_storage); - bool lhs_is_array_template = lhs_is_thread_storage; - bool rhs_is_array_template = rhs_is_thread_storage; + bool lhs_is_array_template = lhs_is_thread_storage || lhs_storage == StorageClassWorkgroup; + bool rhs_is_array_template = rhs_is_thread_storage || rhs_storage == StorageClassWorkgroup; // Special considerations for stage IO variables. // If the variable is actually backed by non-user visible device storage, we use array templates for those. @@ -9705,15 +9781,13 @@ bool CompilerMSL::emit_array_copy(const char *expr, uint32_t lhs_id, uint32_t rh auto *lhs_var = maybe_get_backing_variable(lhs_id); if (lhs_var && lhs_storage == StorageClassStorageBuffer && storage_class_array_is_thread(lhs_var->storage)) lhs_is_array_template = true; - else if (lhs_var && (lhs_storage == StorageClassFunction || lhs_storage == StorageClassPrivate) && - type_is_block_like(get(lhs_var->basetype))) + else if (lhs_var && lhs_storage != StorageClassGeneric && type_is_block_like(get(lhs_var->basetype))) lhs_is_array_template = false; auto *rhs_var = maybe_get_backing_variable(rhs_id); if (rhs_var && rhs_storage == StorageClassStorageBuffer && storage_class_array_is_thread(rhs_var->storage)) rhs_is_array_template = true; - else if (rhs_var && (rhs_storage == StorageClassFunction || rhs_storage == StorageClassPrivate) && - type_is_block_like(get(rhs_var->basetype))) + else if (rhs_var && rhs_storage != StorageClassGeneric && type_is_block_like(get(rhs_var->basetype))) rhs_is_array_template = false; // If threadgroup storage qualifiers are *not* used: @@ -9827,8 +9901,8 @@ uint32_t CompilerMSL::get_physical_tess_level_array_size(spv::BuiltIn builtin) c bool CompilerMSL::maybe_emit_array_assignment(uint32_t id_lhs, uint32_t id_rhs) { // We only care about assignments of an entire array - auto &type = expression_type(id_rhs); - if (!type_is_top_level_array(get_pointee_type(type))) + auto &type = expression_type(id_lhs); + if (!is_array(get_pointee_type(type))) return false; auto *var = maybe_get(id_lhs); @@ -9890,6 +9964,12 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, else if (opcode == OpAtomicSMax || opcode == OpAtomicSMin) expected_type = to_signed_basetype(type.width); + bool use_native_image_atomic; + if (msl_options.supports_msl_version(3, 1)) + use_native_image_atomic = check_atomic_image(obj); + else + use_native_image_atomic = false; + if (type.width == 64) SPIRV_CROSS_THROW("MSL currently does not support 64-bit atomics."); @@ -9907,12 +9987,31 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, ((res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) || var->storage == StorageClassStorageBuffer || var->storage == StorageClassUniform); + // Even compare exchange atomics are vec4 on metal for ... reasons :v + uint32_t vec4_temporary_id = 0; + if (use_native_image_atomic && is_atomic_compare_exchange_strong) + { + uint32_t &tmp_id = extra_sub_expressions[result_id]; + if (!tmp_id) + { + tmp_id = ir.increase_bound_by(2); + + auto vec4_type = get(result_type); + vec4_type.vecsize = 4; + set(tmp_id + 1, vec4_type); + } + + vec4_temporary_id = tmp_id; + } + if (check_discard) { if (is_atomic_compare_exchange_strong) { // We're already emitting a CAS loop here; a conditional won't hurt. emit_uninitialized_temporary_expression(result_type, result_id); + if (vec4_temporary_id) + emit_uninitialized_temporary_expression(vec4_temporary_id + 1, vec4_temporary_id); statement("if (!", builtin_to_glsl(BuiltInHelperInvocation, StorageClassInput), ")"); begin_scope(); } @@ -9920,131 +10019,132 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, exp = join("(!", builtin_to_glsl(BuiltInHelperInvocation, StorageClassInput), " ? "); } - exp += string(op) + "("; - exp += "("; - // Emulate texture2D atomic operations - if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) + if (use_native_image_atomic) { - exp += "device"; + auto obj_expression = to_expression(obj); + auto split_index = obj_expression.find_first_of('@'); + + // Will only be false if we're in "force recompile later" mode. + if (split_index != string::npos) + exp += join(obj_expression.substr(0, split_index), ".", op, "(", obj_expression.substr(split_index + 1)); + else + exp += obj_expression; } else { - exp += get_argument_address_space(*var); + exp += string(op) + "_explicit("; + exp += "("; + // Emulate texture2D atomic operations + if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) + { + exp += "device"; + } + else + { + exp += get_argument_address_space(*var); + } + + exp += " atomic_"; + // For signed and unsigned min/max, we can signal this through the pointer type. + // There is no other way, since C++ does not have explicit signage for atomics. + exp += type_to_glsl(remapped_type); + exp += "*)"; + + exp += "&"; + exp += to_enclosed_expression(obj); } - exp += " atomic_"; - // For signed and unsigned min/max, we can signal this through the pointer type. - // There is no other way, since C++ does not have explicit signage for atomics. - exp += type_to_glsl(remapped_type); - exp += "*)"; - - exp += "&"; - exp += to_enclosed_expression(obj); - if (is_atomic_compare_exchange_strong) { - assert(strcmp(op, "atomic_compare_exchange_weak_explicit") == 0); + assert(strcmp(op, "atomic_compare_exchange_weak") == 0); assert(op2); assert(has_mem_order_2); exp += ", &"; - exp += to_name(result_id); + exp += to_name(vec4_temporary_id ? vec4_temporary_id : result_id); exp += ", "; exp += to_expression(op2); - exp += ", "; - exp += get_memory_order(mem_order_1); - exp += ", "; - exp += get_memory_order(mem_order_2); + + if (!use_native_image_atomic) + { + exp += ", "; + exp += get_memory_order(mem_order_1); + exp += ", "; + exp += get_memory_order(mem_order_2); + } exp += ")"; // MSL only supports the weak atomic compare exchange, so emit a CAS loop here. // The MSL function returns false if the atomic write fails OR the comparison test fails, // so we must validate that it wasn't the comparison test that failed before continuing // the CAS loop, otherwise it will loop infinitely, with the comparison test always failing. - // The function updates the comparitor value from the memory value, so the additional + // The function updates the comparator value from the memory value, so the additional // comparison test evaluates the memory value against the expected value. if (!check_discard) + { emit_uninitialized_temporary_expression(result_type, result_id); + if (vec4_temporary_id) + emit_uninitialized_temporary_expression(vec4_temporary_id + 1, vec4_temporary_id); + } + statement("do"); begin_scope(); - statement(to_name(result_id), " = ", to_expression(op1), ";"); - end_scope_decl(join("while (!", exp, " && ", to_name(result_id), " == ", to_enclosed_expression(op1), ")")); + + string scalar_expression; + if (vec4_temporary_id) + scalar_expression = join(to_expression(vec4_temporary_id), ".x"); + else + scalar_expression = to_expression(result_id); + + statement(scalar_expression, " = ", to_expression(op1), ";"); + end_scope_decl(join("while (!", exp, " && ", scalar_expression, " == ", to_enclosed_expression(op1), ")")); + if (vec4_temporary_id) + statement(to_expression(result_id), " = ", scalar_expression, ";"); + + // Vulkan: (section 9.29: ... and values returned by atomic instructions in helper invocations are undefined) if (check_discard) { end_scope(); statement("else"); begin_scope(); - exp = "atomic_load_explicit("; - exp += "("; - // Emulate texture2D atomic operations - if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) - exp += "device"; - else - exp += get_argument_address_space(*var); - - exp += " atomic_"; - exp += type_to_glsl(remapped_type); - exp += "*)"; - - exp += "&"; - exp += to_enclosed_expression(obj); - - if (has_mem_order_2) - exp += string(", ") + get_memory_order(mem_order_2); - else - exp += string(", ") + get_memory_order(mem_order_1); - - exp += ")"; - - statement(to_name(result_id), " = ", exp, ";"); + statement(to_expression(result_id), " = {};"); end_scope(); } } else { - assert(strcmp(op, "atomic_compare_exchange_weak_explicit") != 0); + assert(strcmp(op, "atomic_compare_exchange_weak") != 0); + if (op1) { + exp += ", "; if (op1_is_literal) - exp += join(", ", op1); + exp += to_string(op1); else - exp += ", " + bitcast_expression(expected_type, op1); + exp += bitcast_expression(expected_type, op1); } + if (op2) exp += ", " + to_expression(op2); - exp += string(", ") + get_memory_order(mem_order_1); - if (has_mem_order_2) - exp += string(", ") + get_memory_order(mem_order_2); + if (!use_native_image_atomic) + { + exp += string(", ") + get_memory_order(mem_order_1); + if (has_mem_order_2) + exp += string(", ") + get_memory_order(mem_order_2); + } exp += ")"; + // For some particular reason, atomics return vec4 in Metal ... + if (use_native_image_atomic) + exp += ".x"; + + // Vulkan: (section 9.29: ... and values returned by atomic instructions in helper invocations are undefined) if (check_discard) { exp += " : "; - if (strcmp(op, "atomic_store_explicit") != 0) - { - exp += "atomic_load_explicit("; - exp += "("; - // Emulate texture2D atomic operations - if (res_type.storage == StorageClassUniformConstant && res_type.basetype == SPIRType::Image) - exp += "device"; - else - exp += get_argument_address_space(*var); - - exp += " atomic_"; - exp += type_to_glsl(remapped_type); - exp += "*)"; - - exp += "&"; - exp += to_enclosed_expression(obj); - - if (has_mem_order_2) - exp += string(", ") + get_memory_order(mem_order_2); - else - exp += string(", ") + get_memory_order(mem_order_1); - - exp += ")"; - } + if (strcmp(op, "atomic_store") != 0) + exp += join(type_to_glsl(get(result_type)), "{}"); else exp += "((void)0)"; exp += ")"; @@ -10053,7 +10153,7 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, if (expected_type != type.basetype) exp = bitcast_expression(type, expected_type, exp); - if (strcmp(op, "atomic_store_explicit") != 0) + if (strcmp(op, "atomic_store") != 0) emit_op(result_type, result_id, exp, false); else statement(exp, ";"); @@ -10393,6 +10493,11 @@ void CompilerMSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, break; } + case GLSLstd450Pow: + // powr makes x < 0.0 undefined, just like SPIR-V. + emit_binary_func_op(result_type, id, args[0], args[1], "powr"); + break; + default: CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); break; @@ -10747,7 +10852,7 @@ string CompilerMSL::to_function_name(const TextureFunctionNameArguments &args) string CompilerMSL::convert_to_f32(const string &expr, uint32_t components) { - SPIRType t; + SPIRType t { components > 1 ? OpTypeVector : OpTypeFloat }; t.basetype = SPIRType::Float; t.vecsize = components; t.columns = 1; @@ -11123,29 +11228,38 @@ string CompilerMSL::to_function_args(const TextureFunctionArguments &args, bool // rhoX = dP/dx * extent; rhoY = dP/dy * extent // Therefore, dP/dx = dP/dy = exp2(lod)/extent. // (Subtracting 0.5 before exponentiation gives better results.) - string grad_opt, extent; + string grad_opt, extent, grad_coord; VariableID base_img = img; if (auto *combined = maybe_get(img)) base_img = combined->image; switch (imgtype.image.dim) { case Dim1D: - grad_opt = "2d"; + grad_opt = "gradient2d"; extent = join("float2(", to_expression(base_img), ".get_width(), 1.0)"); break; case Dim2D: - grad_opt = "2d"; + grad_opt = "gradient2d"; extent = join("float2(", to_expression(base_img), ".get_width(), ", to_expression(base_img), ".get_height())"); break; case DimCube: if (imgtype.image.arrayed && msl_options.emulate_cube_array) { - grad_opt = "2d"; + grad_opt = "gradient2d"; extent = join("float2(", to_expression(base_img), ".get_width())"); } else { - grad_opt = "cube"; + if (msl_options.agx_manual_cube_grad_fixup) + { + add_spv_func_and_recompile(SPVFuncImplGradientCube); + grad_opt = "spvGradientCube"; + grad_coord = tex_coords + ", "; + } + else + { + grad_opt = "gradientcube"; + } extent = join("float3(", to_expression(base_img), ".get_width())"); } break; @@ -11154,8 +11268,8 @@ string CompilerMSL::to_function_args(const TextureFunctionArguments &args, bool extent = "float3(1.0)"; break; } - farg_str += join(", gradient", grad_opt, "(exp2(", to_expression(lod), " - 0.5) / ", extent, ", exp2(", - to_expression(lod), " - 0.5) / ", extent, ")"); + farg_str += join(", ", grad_opt, "(", grad_coord, "exp2(", to_expression(lod), " - 0.5) / ", extent, + ", exp2(", to_expression(lod), " - 0.5) / ", extent, ")"); } else { @@ -11175,27 +11289,37 @@ string CompilerMSL::to_function_args(const TextureFunctionArguments &args, bool { forward = forward && should_forward(grad_x); forward = forward && should_forward(grad_y); - string grad_opt; + string grad_opt, grad_coord; switch (imgtype.image.dim) { case Dim1D: case Dim2D: - grad_opt = "2d"; + grad_opt = "gradient2d"; break; case Dim3D: - grad_opt = "3d"; + grad_opt = "gradient3d"; break; case DimCube: if (imgtype.image.arrayed && msl_options.emulate_cube_array) - grad_opt = "2d"; + { + grad_opt = "gradient2d"; + } + else if (msl_options.agx_manual_cube_grad_fixup) + { + add_spv_func_and_recompile(SPVFuncImplGradientCube); + grad_opt = "spvGradientCube"; + grad_coord = tex_coords + ", "; + } else - grad_opt = "cube"; + { + grad_opt = "gradientcube"; + } break; default: grad_opt = "unsupported_gradient_dimension"; break; } - farg_str += ", gradient" + grad_opt + "(" + to_expression(grad_x) + ", " + to_expression(grad_y) + ")"; + farg_str += join(", ", grad_opt, "(", grad_coord, to_expression(grad_x), ", ", to_expression(grad_y), ")"); } if (args.min_lod) @@ -11664,7 +11788,7 @@ string CompilerMSL::to_func_call_arg(const SPIRFunction::Parameter &arg, uint32_ // Emulate texture2D atomic operations auto *backing_var = maybe_get_backing_variable(var_id); - if (backing_var && atomic_image_vars.count(backing_var->self)) + if (backing_var && atomic_image_vars_emulated.count(backing_var->self)) { arg_str += ", " + to_expression(var_id) + "_atomic"; } @@ -11837,7 +11961,7 @@ string CompilerMSL::to_struct_member(const SPIRType &type, uint32_t member_type_ if (is_matrix(physical_type)) row_major = has_member_decoration(type.self, index, DecorationRowMajor); - SPIRType row_major_physical_type; + SPIRType row_major_physical_type { OpTypeMatrix }; const SPIRType *declared_type = &physical_type; // If a struct is being declared with physical layout, @@ -13200,7 +13324,7 @@ void CompilerMSL::entry_point_args_discrete_descriptors(string &ep_args) // Emulate texture2D atomic operations uint32_t secondary_index = 0; - if (atomic_image_vars.count(var.self)) + if (atomic_image_vars_emulated.count(var.self)) { secondary_index = get_metal_resource_index(var, SPIRType::AtomicCounter, 0); } @@ -13390,7 +13514,7 @@ void CompilerMSL::entry_point_args_discrete_descriptors(string &ep_args) } // Emulate texture2D atomic operations - if (atomic_image_vars.count(var.self)) + if (atomic_image_vars_emulated.count(var.self)) { ep_args += ", device atomic_" + type_to_glsl(get(basetype.image.type), 0); ep_args += "* " + r.name + "_atomic"; @@ -13598,14 +13722,31 @@ void CompilerMSL::fix_up_shader_inputs_outputs() break; case BuiltInPatchVertices: if (is_tese_shader()) - entry_func.fixup_hooks_in.push_back([=]() { - statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", - to_expression(patch_stage_in_var_id), ".gl_in.size();"); - }); + { + if (msl_options.raw_buffer_tese_input) + { + entry_func.fixup_hooks_in.push_back( + [=]() { + statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", + get_entry_point().output_vertices, ";"); + }); + } + else + { + entry_func.fixup_hooks_in.push_back( + [=]() + { + statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = ", + to_expression(patch_stage_in_var_id), ".gl_in.size();"); + }); + } + } else + { entry_func.fixup_hooks_in.push_back([=]() { statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = spvIndirectParams[0];"); }); + } break; case BuiltInTessCoord: if (get_entry_point().flags.get(ExecutionModeQuads)) @@ -14215,7 +14356,6 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) auto &type = get_variable_data_type(var); auto &var_type = get(arg.type); StorageClass type_storage = var_type.storage; - bool is_pointer = var_type.pointer; // If we need to modify the name of the variable, make sure we use the original variable. // Our alias is just a shadow variable. @@ -14223,7 +14363,7 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) if (arg.alias_global_variable && var.basevariable) name_id = var.basevariable; - bool constref = !arg.alias_global_variable && is_pointer && arg.write_count == 0; + bool constref = !arg.alias_global_variable && is_pointer(var_type) && arg.write_count == 0; // Framebuffer fetch is plain value, const looks out of place, but it is not wrong. if (type_is_msl_framebuffer_fetch(type)) constref = false; @@ -14252,9 +14392,6 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) bool builtin = has_decoration(var.self, DecorationBuiltIn); auto builtin_type = BuiltIn(get_decoration(arg.id, DecorationBuiltIn)); - if (address_space == "threadgroup") - is_using_builtin_array = true; - if (var.basevariable && (var.basevariable == stage_in_ptr_var_id || var.basevariable == stage_out_ptr_var_id)) decl = join(cv_qualifier, type_to_glsl(type, arg.id)); else if (builtin) @@ -14321,7 +14458,7 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) } } - if (!builtin && !is_pointer && + if (!builtin && !is_pointer(var_type) && (type_storage == StorageClassFunction || type_storage == StorageClassGeneric)) { // If the argument is a pure value and not an opaque type, we will pass by value. @@ -14468,7 +14605,7 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) // Emulate texture2D atomic operations auto *backing_var = maybe_get_backing_variable(name_id); - if (backing_var && atomic_image_vars.count(backing_var->self)) + if (backing_var && atomic_image_vars_emulated.count(backing_var->self)) { decl += ", device atomic_" + type_to_glsl(get(var_type.image.type), 0); decl += "* " + to_expression(name_id) + "_atomic"; @@ -14870,7 +15007,7 @@ string CompilerMSL::to_member_reference(uint32_t base, const SPIRType &type, uin bool is_buffer_variable = is_block && (var->storage == StorageClassUniform || var->storage == StorageClassStorageBuffer); - declared_as_pointer = is_buffer_variable && is_array(get(var->basetype)); + declared_as_pointer = is_buffer_variable && is_array(get_pointee_type(var->basetype)); } if (declared_as_pointer || (!ptr_chain_is_resolved && should_dereference(base))) @@ -14900,7 +15037,7 @@ string CompilerMSL::type_to_glsl(const SPIRType &type, uint32_t id, bool member) string type_name; // Pointer? - if (type_is_top_level_pointer(type) || type_is_array_of_pointers(type)) + if (is_pointer(type) || type_is_array_of_pointers(type)) { assert(type.pointer_depth > 0); @@ -14928,7 +15065,7 @@ string CompilerMSL::type_to_glsl(const SPIRType &type, uint32_t id, bool member) // the C-style nesting works right. // FIXME: This is somewhat of a hack. bool old_is_using_builtin_array = is_using_builtin_array; - if (type_is_top_level_physical_pointer(type)) + if (is_physical_pointer(type)) is_using_builtin_array = false; type_name = join(type_address_space, " ", type_to_glsl(*p_parent_type, id)); @@ -15180,19 +15317,6 @@ bool CompilerMSL::variable_decl_is_remapped_storage(const SPIRVariable &variable } } -std::string CompilerMSL::variable_decl(const SPIRVariable &variable) -{ - bool old_is_using_builtin_array = is_using_builtin_array; - - // Threadgroup arrays can't have a wrapper type. - if (variable_decl_is_remapped_storage(variable, StorageClassWorkgroup)) - is_using_builtin_array = true; - - auto expr = CompilerGLSL::variable_decl(variable); - is_using_builtin_array = old_is_using_builtin_array; - return expr; -} - // GCC workaround of lambdas calling protected funcs std::string CompilerMSL::variable_decl(const SPIRType &type, const std::string &name, uint32_t id) { @@ -16696,8 +16820,11 @@ bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, ui // Emulate texture2D atomic operations case OpImageTexelPointer: { - auto *var = compiler.maybe_get_backing_variable(args[2]); - image_pointers[args[1]] = var ? var->self : ID(0); + if (!compiler.msl_options.supports_msl_version(3, 1)) + { + auto *var = compiler.maybe_get_backing_variable(args[2]); + image_pointers_emulated[args[1]] = var ? var->self : ID(0); + } break; } @@ -16727,11 +16854,11 @@ bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, ui case OpAtomicXor: { uses_atomics = true; - auto it = image_pointers.find(args[2]); - if (it != image_pointers.end()) + auto it = image_pointers_emulated.find(args[2]); + if (it != image_pointers_emulated.end()) { uses_image_write = true; - compiler.atomic_image_vars.insert(it->second); + compiler.atomic_image_vars_emulated.insert(it->second); } else check_resource_write(args[2]); @@ -16741,10 +16868,10 @@ bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, ui case OpAtomicStore: { uses_atomics = true; - auto it = image_pointers.find(args[0]); - if (it != image_pointers.end()) + auto it = image_pointers_emulated.find(args[0]); + if (it != image_pointers_emulated.end()) { - compiler.atomic_image_vars.insert(it->second); + compiler.atomic_image_vars_emulated.insert(it->second); uses_image_write = true; } else @@ -16755,10 +16882,10 @@ bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, ui case OpAtomicLoad: { uses_atomics = true; - auto it = image_pointers.find(args[2]); - if (it != image_pointers.end()) + auto it = image_pointers_emulated.find(args[2]); + if (it != image_pointers_emulated.end()) { - compiler.atomic_image_vars.insert(it->second); + compiler.atomic_image_vars_emulated.insert(it->second); } break; } @@ -16784,8 +16911,7 @@ bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t *args, ui auto *var = compiler.maybe_get_backing_variable(args[2]); if (var != nullptr) { - auto &type = compiler.get(var->basetype); - if (!is_runtime_size_array(type)) + if (!compiler.is_var_runtime_size_array(*var)) compiler.buffers_requiring_array_length.insert(var->self); } break; @@ -16938,8 +17064,8 @@ CompilerMSL::SPVFuncImpl CompilerMSL::OpCodePreprocessor::get_spv_func_impl(Op o case OpAtomicLoad: case OpAtomicStore: { - auto it = image_pointers.find(args[opcode == OpAtomicStore ? 0 : 2]); - if (it != image_pointers.end()) + auto it = image_pointers_emulated.find(args[opcode == OpAtomicStore ? 0 : 2]); + if (it != image_pointers_emulated.end()) { uint32_t tid = compiler.get(it->second).basetype; if (tid && compiler.get(tid).image.dim == Dim2D) @@ -17198,7 +17324,7 @@ void CompilerMSL::cast_from_variable_load(uint32_t source_id, std::string &expr, // Type fixups for workgroup variables if they are booleans. if (rewrite_boolean_load) { - if (type_is_top_level_array(expr_type)) + if (is_array(expr_type)) expr = to_rerolled_array_expression(expr_type, expr, expr_type); else expr = join(type_to_glsl(expr_type), "(", expr, ")"); @@ -17275,7 +17401,7 @@ void CompilerMSL::cast_from_variable_load(uint32_t source_id, std::string &expr, break; } - if (type_is_top_level_array(expr_type) && builtin == BuiltInSampleMask) + if (is_array(expr_type) && builtin == BuiltInSampleMask) { // Needs special handling. auto wrap_expr = join(type_to_glsl(expr_type), "({ "); @@ -17285,7 +17411,7 @@ void CompilerMSL::cast_from_variable_load(uint32_t source_id, std::string &expr, } else if (expected_type != expr_type.basetype) { - if (type_is_top_level_array(expr_type) && (builtin == BuiltInTessLevelInner || builtin == BuiltInTessLevelOuter)) + if (is_array(expr_type) && (builtin == BuiltInTessLevelInner || builtin == BuiltInTessLevelOuter)) { // Triggers when loading TessLevel directly as an array. // Need explicit padding + cast. @@ -17344,7 +17470,7 @@ void CompilerMSL::cast_to_variable_store(uint32_t target_id, std::string &expr, // Type fixups for workgroup variables or struct members if they are booleans. if (rewrite_boolean_store) { - if (type_is_top_level_array(expr_type)) + if (is_array(expr_type)) { expr = to_rerolled_array_expression(*var_type, expr, expr_type); } @@ -17588,7 +17714,7 @@ void CompilerMSL::analyze_argument_buffers() { &var, descriptor_alias, to_name(var_id), type.basetype, resource_index, 0 }); // Emulate texture2D atomic operations - if (atomic_image_vars.count(var.self)) + if (atomic_image_vars_emulated.count(var.self)) { uint32_t buffer_resource_index = get_metal_resource_index(var, SPIRType::AtomicCounter, 0); resources_in_set[desc_set].push_back( @@ -17623,6 +17749,7 @@ void CompilerMSL::analyze_argument_buffers() // Create a buffer to hold extra data, including the swizzle constants. SPIRType uint_type_pointer = get_uint_type(); + uint_type_pointer.op = OpTypePointer; uint_type_pointer.pointer = true; uint_type_pointer.pointer_depth++; uint_type_pointer.parent_type = get_uint_type_id(); @@ -17678,7 +17805,7 @@ void CompilerMSL::analyze_argument_buffers() uint32_t ptr_type_id = next_id + 2; argument_buffer_ids[desc_set] = next_id; - auto &buffer_type = set(type_id); + auto &buffer_type = set(type_id, OpTypeStruct); buffer_type.basetype = SPIRType::Struct; @@ -17695,8 +17822,9 @@ void CompilerMSL::analyze_argument_buffers() set_name(type_id, join("spvDescriptorSetBuffer", desc_set)); - auto &ptr_type = set(ptr_type_id); + auto &ptr_type = set(ptr_type_id, OpTypePointer); ptr_type = buffer_type; + ptr_type.op = spv::OpTypePointer; ptr_type.pointer = true; ptr_type.pointer_depth++; ptr_type.parent_type = type_id; @@ -17779,14 +17907,14 @@ void CompilerMSL::analyze_argument_buffers() bool type_is_array = !type.array.empty(); uint32_t sampler_type_id = ir.increase_bound_by(type_is_array ? 2 : 1); - auto &new_sampler_type = set(sampler_type_id); + auto &new_sampler_type = set(sampler_type_id, OpTypeSampler); new_sampler_type.basetype = SPIRType::Sampler; new_sampler_type.storage = StorageClassUniformConstant; if (type_is_array) { uint32_t sampler_type_array_id = sampler_type_id + 1; - auto &sampler_type_array = set(sampler_type_array_id); + auto &sampler_type_array = set(sampler_type_array_id, OpTypeArray); sampler_type_array = new_sampler_type; sampler_type_array.array = type.array; sampler_type_array.array_size_literal = type.array_size_literal; @@ -17827,7 +17955,7 @@ void CompilerMSL::analyze_argument_buffers() buffer_type.member_types.push_back(get_variable_data_type_id(var)); set_qualified_name(var.self, join(to_name(buffer_variable_id), ".", mbr_name)); } - else if (atomic_image_vars.count(var.self)) + else if (atomic_image_vars_emulated.count(var.self)) { // Emulate texture2D atomic operations. // Don't set the qualified name: it's already set for this variable, @@ -17837,12 +17965,13 @@ void CompilerMSL::analyze_argument_buffers() uint32_t atomic_type_id = offset; uint32_t type_ptr_id = offset + 1; - SPIRType atomic_type; + SPIRType atomic_type { OpTypeInt }; atomic_type.basetype = SPIRType::AtomicCounter; atomic_type.width = 32; atomic_type.vecsize = 1; set(atomic_type_id, atomic_type); + atomic_type.op = OpTypePointer; atomic_type.pointer = true; atomic_type.pointer_depth++; atomic_type.parent_type = atomic_type_id; @@ -17903,13 +18032,14 @@ void CompilerMSL::add_argument_buffer_padding_buffer_type(SPIRType &struct_type, if (!argument_buffer_padding_buffer_type_id) { uint32_t buff_type_id = ir.increase_bound_by(2); - auto &buff_type = set(buff_type_id); + auto &buff_type = set(buff_type_id, OpNop); buff_type.basetype = rez_bind.basetype; buff_type.storage = StorageClassUniformConstant; uint32_t ptr_type_id = buff_type_id + 1; - auto &ptr_type = set(ptr_type_id); + auto &ptr_type = set(ptr_type_id, OpTypePointer); ptr_type = buff_type; + ptr_type.op = spv::OpTypePointer; ptr_type.pointer = true; ptr_type.pointer_depth++; ptr_type.parent_type = buff_type_id; @@ -17927,12 +18057,12 @@ void CompilerMSL::add_argument_buffer_padding_image_type(SPIRType &struct_type, if (!argument_buffer_padding_image_type_id) { uint32_t base_type_id = ir.increase_bound_by(2); - auto &base_type = set(base_type_id); + auto &base_type = set(base_type_id, OpTypeFloat); base_type.basetype = SPIRType::Float; base_type.width = 32; uint32_t img_type_id = base_type_id + 1; - auto &img_type = set(img_type_id); + auto &img_type = set(img_type_id, OpTypeImage); img_type.basetype = SPIRType::Image; img_type.storage = StorageClassUniformConstant; @@ -17958,7 +18088,7 @@ void CompilerMSL::add_argument_buffer_padding_sampler_type(SPIRType &struct_type if (!argument_buffer_padding_sampler_type_id) { uint32_t samp_type_id = ir.increase_bound_by(1); - auto &samp_type = set(samp_type_id); + auto &samp_type = set(samp_type_id, OpTypeSampler); samp_type.basetype = SPIRType::Sampler; samp_type.storage = StorageClassUniformConstant; @@ -17977,8 +18107,8 @@ void CompilerMSL::add_argument_buffer_padding_type(uint32_t mbr_type_id, SPIRTyp if (count > 1) { uint32_t ary_type_id = ir.increase_bound_by(1); - auto &ary_type = set(ary_type_id); - ary_type = get(type_id); + auto &ary_type = set(ary_type_id, get(type_id)); + ary_type.op = OpTypeArray; ary_type.array.push_back(count); ary_type.array_size_literal.push_back(true); ary_type.parent_type = type_id; diff --git a/3rdparty/spirv-cross/spirv_msl.hpp b/3rdparty/spirv-cross/spirv_msl.hpp index 7fef3d48f..05c09fde5 100644 --- a/3rdparty/spirv-cross/spirv_msl.hpp +++ b/3rdparty/spirv-cross/spirv_msl.hpp @@ -512,6 +512,13 @@ public: // The bug has been reported to Apple, and will hopefully be fixed in future releases. bool replace_recursive_inputs = false; + // If set, manual fixups of gradient vectors for cube texture lookups will be performed. + // All released Apple Silicon GPUs to date behave incorrectly when sampling a cube texture + // with explicit gradients. They will ignore one of the three partial derivatives based + // on the selected major axis, and expect the remaining derivatives to be partially + // transformed. + bool agx_manual_cube_grad_fixup = false; + bool is_ios() const { return platform == iOS; @@ -756,6 +763,7 @@ protected: SPVFuncImplArrayOfArrayCopy6Dim = SPVFuncImplArrayCopyMultidimBase + 6, SPVFuncImplTexelBufferCoords, SPVFuncImplImage2DAtomicCoords, // Emulate texture2D atomic operations + SPVFuncImplGradientCube, SPVFuncImplFMul, SPVFuncImplFAdd, SPVFuncImplFSub, @@ -849,9 +857,6 @@ protected: std::string type_to_array_glsl(const SPIRType &type) override; std::string constant_op_expression(const SPIRConstantOp &cop) override; - // Threadgroup arrays can't have a wrapper type - std::string variable_decl(const SPIRVariable &variable) override; - bool variable_decl_is_remapped_storage(const SPIRVariable &variable, spv::StorageClass storage) const override; // GCC workaround of lambdas calling protected functions (for older GCC versions) @@ -1201,7 +1206,7 @@ protected: std::unordered_set buffers_requiring_array_length; SmallVector> buffer_aliases_argument; SmallVector buffer_aliases_discrete; - std::unordered_set atomic_image_vars; // Emulate texture2D atomic operations + std::unordered_set atomic_image_vars_emulated; // Emulate texture2D atomic operations std::unordered_set pull_model_inputs; std::unordered_set recursive_inputs; @@ -1271,7 +1276,7 @@ protected: CompilerMSL &compiler; std::unordered_map result_types; - std::unordered_map image_pointers; // Emulate texture2D atomic operations + std::unordered_map image_pointers_emulated; // Emulate texture2D atomic operations bool suppress_missing_prototypes = false; bool uses_atomics = false; bool uses_image_write = false; diff --git a/3rdparty/spirv-cross/spirv_parser.cpp b/3rdparty/spirv-cross/spirv_parser.cpp index 01c2e3812..6108dbb65 100644 --- a/3rdparty/spirv-cross/spirv_parser.cpp +++ b/3rdparty/spirv-cross/spirv_parser.cpp @@ -517,7 +517,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeVoid: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::Void; break; } @@ -525,7 +525,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeBool: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::Boolean; type.width = 1; break; @@ -535,7 +535,7 @@ void Parser::parse(const Instruction &instruction) { uint32_t id = ops[0]; uint32_t width = ops[1]; - auto &type = set(id); + auto &type = set(id, op); if (width == 64) type.basetype = SPIRType::Double; else if (width == 32) @@ -553,7 +553,7 @@ void Parser::parse(const Instruction &instruction) uint32_t id = ops[0]; uint32_t width = ops[1]; bool signedness = ops[2] != 0; - auto &type = set(id); + auto &type = set(id, op); type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width); type.width = width; break; @@ -568,9 +568,9 @@ void Parser::parse(const Instruction &instruction) uint32_t vecsize = ops[2]; auto &base = get(ops[1]); - auto &vecbase = set(id); + auto &vecbase = set(id, base); - vecbase = base; + vecbase.op = op; vecbase.vecsize = vecsize; vecbase.self = id; vecbase.parent_type = ops[1]; @@ -583,9 +583,9 @@ void Parser::parse(const Instruction &instruction) uint32_t colcount = ops[2]; auto &base = get(ops[1]); - auto &matrixbase = set(id); + auto &matrixbase = set(id, base); - matrixbase = base; + matrixbase.op = op; matrixbase.columns = colcount; matrixbase.self = id; matrixbase.parent_type = ops[1]; @@ -595,12 +595,11 @@ void Parser::parse(const Instruction &instruction) case OpTypeArray: { uint32_t id = ops[0]; - auto &arraybase = set(id); - uint32_t tid = ops[1]; auto &base = get(tid); + auto &arraybase = set(id, base); - arraybase = base; + arraybase.op = op; arraybase.parent_type = tid; uint32_t cid = ops[2]; @@ -615,7 +614,9 @@ void Parser::parse(const Instruction &instruction) arraybase.array_size_literal.push_back(literal); arraybase.array.push_back(literal ? c->scalar() : cid); - // Do NOT set arraybase.self! + + // .self resolves down to non-array/non-pointer type. + arraybase.self = base.self; break; } @@ -624,25 +625,27 @@ void Parser::parse(const Instruction &instruction) uint32_t id = ops[0]; auto &base = get(ops[1]); - auto &arraybase = set(id); + auto &arraybase = set(id, base); // We're copying type information into Array types, so we'll need a fixup for any physical pointer // references. if (base.forward_pointer) forward_pointer_fixups.push_back({ id, ops[1] }); - arraybase = base; + arraybase.op = op; arraybase.array.push_back(0); arraybase.array_size_literal.push_back(true); arraybase.parent_type = ops[1]; - // Do NOT set arraybase.self! + + // .self resolves down to non-array/non-pointer type. + arraybase.self = base.self; break; } case OpTypeImage: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::Image; type.image.type = ops[1]; type.image.dim = static_cast(ops[2]); @@ -659,7 +662,7 @@ void Parser::parse(const Instruction &instruction) { uint32_t id = ops[0]; uint32_t imagetype = ops[1]; - auto &type = set(id); + auto &type = set(id, op); type = get(imagetype); type.basetype = SPIRType::SampledImage; type.self = id; @@ -669,7 +672,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeSampler: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::Sampler; break; } @@ -682,10 +685,13 @@ void Parser::parse(const Instruction &instruction) // We won't be able to compile it, but we shouldn't crash when parsing. // We should be able to reflect. auto *base = maybe_get(ops[2]); - auto &ptrbase = set(id); + auto &ptrbase = set(id, op); if (base) + { ptrbase = *base; + ptrbase.op = op; + } ptrbase.pointer = true; ptrbase.pointer_depth++; @@ -706,7 +712,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeForwardPointer: { uint32_t id = ops[0]; - auto &ptrbase = set(id); + auto &ptrbase = set(id, op); ptrbase.pointer = true; ptrbase.pointer_depth++; ptrbase.storage = static_cast(ops[1]); @@ -721,7 +727,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeStruct: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::Struct; for (uint32_t i = 1; i < length; i++) type.member_types.push_back(ops[i]); @@ -770,7 +776,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeAccelerationStructureKHR: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::AccelerationStructure; break; } @@ -778,7 +784,7 @@ void Parser::parse(const Instruction &instruction) case OpTypeRayQueryKHR: { uint32_t id = ops[0]; - auto &type = set(id); + auto &type = set(id, op); type.basetype = SPIRType::RayQuery; break; } @@ -1025,10 +1031,9 @@ void Parser::parse(const Instruction &instruction) { uint32_t ids = ir.increase_bound_by(2); - SPIRType type; + auto &type = set(ids, OpTypeInt); type.basetype = SPIRType::Int; type.width = 32; - set(ids, type); auto &c = set(ids + 1, ids); current_block->condition = c.self; diff --git a/3rdparty/spirv-cross/spirv_reflect.cpp b/3rdparty/spirv-cross/spirv_reflect.cpp index 9fcd3bc09..b02773722 100644 --- a/3rdparty/spirv-cross/spirv_reflect.cpp +++ b/3rdparty/spirv-cross/spirv_reflect.cpp @@ -291,7 +291,7 @@ static bool naturally_emit_type(const SPIRType &type) bool CompilerReflection::type_is_reference(const SPIRType &type) const { // Physical pointers and arrays of physical pointers need to refer to the pointee's type. - return type_is_top_level_physical_pointer(type) || + return is_physical_pointer(type) || (type_is_array_of_pointers(type) && type.storage == StorageClassPhysicalStorageBuffer); } @@ -341,7 +341,7 @@ void CompilerReflection::emit_type(uint32_t type_id, bool &emitted_open_tag) json_stream->emit_json_key_object("_" + std::to_string(type_id)); json_stream->emit_json_key_value("name", name); - if (type_is_top_level_physical_pointer(type)) + if (is_physical_pointer(type)) { json_stream->emit_json_key_value("type", "_" + std::to_string(type.parent_type)); json_stream->emit_json_key_value("physical_pointer", true); @@ -404,7 +404,7 @@ void CompilerReflection::emit_type_member(const SPIRType &type, uint32_t index) void CompilerReflection::emit_type_array(const SPIRType &type) { - if (!type_is_top_level_physical_pointer(type) && !type.array.empty()) + if (!is_physical_pointer(type) && !type.array.empty()) { json_stream->emit_json_key_array("array"); // Note that we emit the zeros here as a means of identifying @@ -444,7 +444,7 @@ void CompilerReflection::emit_type_member_qualifiers(const SPIRType &type, uint3 if (dec.decoration_flags.get(DecorationRowMajor)) json_stream->emit_json_key_value("row_major", true); - if (type_is_top_level_physical_pointer(membertype)) + if (is_physical_pointer(membertype)) json_stream->emit_json_key_value("physical_pointer", true); } }