summaryrefslogtreecommitdiff
path: root/spirv_msl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'spirv_msl.cpp')
-rw-r--r--spirv_msl.cpp216
1 files changed, 144 insertions, 72 deletions
diff --git a/spirv_msl.cpp b/spirv_msl.cpp
index 5570a211..8a5a22f8 100644
--- a/spirv_msl.cpp
+++ b/spirv_msl.cpp
@@ -1991,6 +1991,92 @@ uint32_t CompilerMSL::build_msl_interpolant_type(uint32_t type_id, bool is_noper
return new_type_id;
}
+bool CompilerMSL::add_component_variable_to_interface_block(spv::StorageClass storage, const std::string &ib_var_ref,
+ SPIRVariable &var,
+ const SPIRType &type,
+ InterfaceBlockMeta &meta)
+{
+ // Deal with Component decorations.
+ const InterfaceBlockMeta::LocationMeta *location_meta = nullptr;
+ uint32_t location = ~0u;
+ if (has_decoration(var.self, DecorationLocation))
+ {
+ location = get_decoration(var.self, DecorationLocation);
+ auto location_meta_itr = meta.location_meta.find(location);
+ if (location_meta_itr != end(meta.location_meta))
+ location_meta = &location_meta_itr->second;
+ }
+
+ // Check if we need to pad fragment output to match a certain number of components.
+ if (location_meta)
+ {
+ bool pad_fragment_output = has_decoration(var.self, DecorationLocation) &&
+ msl_options.pad_fragment_output_components &&
+ get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput;
+
+ auto &entry_func = get<SPIRFunction>(ir.default_entry_point);
+ uint32_t start_component = get_decoration(var.self, DecorationComponent);
+ uint32_t type_components = type.vecsize;
+ uint32_t num_components = location_meta->num_components;
+
+ if (pad_fragment_output)
+ {
+ uint32_t locn = get_decoration(var.self, DecorationLocation);
+ num_components = std::max(num_components, get_target_components_for_fragment_location(locn));
+ }
+
+ // We have already declared an IO block member as m_location_N.
+ // Just emit an early-declared variable and fixup as needed.
+ // Arrays need to be unrolled here since each location might need a different number of components.
+ entry_func.add_local_variable(var.self);
+ vars_needing_early_declaration.push_back(var.self);
+
+ if (var.storage == StorageClassInput)
+ {
+ entry_func.fixup_hooks_in.push_back([=, &type, &var]() {
+ if (!type.array.empty())
+ {
+ uint32_t array_size = to_array_size_literal(type);
+ for (uint32_t loc_off = 0; loc_off < array_size; loc_off++)
+ {
+ statement(to_name(var.self), "[", loc_off, "]", " = ", ib_var_ref,
+ ".m_location_", location + loc_off,
+ vector_swizzle(type_components, start_component), ";");
+ }
+ }
+ else
+ {
+ statement(to_name(var.self), " = ", ib_var_ref, ".m_location_", location,
+ vector_swizzle(type_components, start_component), ";");
+ }
+ });
+ }
+ else
+ {
+ entry_func.fixup_hooks_out.push_back([=, &type, &var]() {
+ if (!type.array.empty())
+ {
+ uint32_t array_size = to_array_size_literal(type);
+ for (uint32_t loc_off = 0; loc_off < array_size; loc_off++)
+ {
+ statement(ib_var_ref, ".m_location_", location + loc_off,
+ vector_swizzle(type_components, start_component), " = ",
+ to_name(var.self), "[", loc_off, "];");
+ }
+ }
+ else
+ {
+ statement(ib_var_ref, ".m_location_", location,
+ vector_swizzle(type_components, start_component), " = ", to_name(var.self), ";");
+ }
+ });
+ }
+ return true;
+ }
+ else
+ return false;
+}
+
void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, const string &ib_var_ref,
SPIRType &ib_type, SPIRVariable &var, InterfaceBlockMeta &meta)
{
@@ -2019,65 +2105,14 @@ void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, co
auto &entry_func = get<SPIRFunction>(ir.default_entry_point);
- // Deal with Component decorations.
- InterfaceBlockMeta::LocationMeta *location_meta = nullptr;
- if (has_decoration(var.self, DecorationLocation))
- {
- auto location_meta_itr = meta.location_meta.find(get_decoration(var.self, DecorationLocation));
- if (location_meta_itr != end(meta.location_meta))
- location_meta = &location_meta_itr->second;
- }
+ if (add_component_variable_to_interface_block(storage, ib_var_ref, var, type, meta))
+ return;
bool pad_fragment_output = has_decoration(var.self, DecorationLocation) &&
msl_options.pad_fragment_output_components &&
get_entry_point().model == ExecutionModelFragment && storage == StorageClassOutput;
- // Check if we need to pad fragment output to match a certain number of components.
- if (location_meta)
- {
- start_component = get_decoration(var.self, DecorationComponent);
- uint32_t num_components = location_meta->num_components;
- if (pad_fragment_output)
- {
- uint32_t locn = get_decoration(var.self, DecorationLocation);
- num_components = std::max(num_components, get_target_components_for_fragment_location(locn));
- }
-
- if (location_meta->ib_index != ~0u)
- {
- // We have already declared the variable. Just emit an early-declared variable and fixup as needed.
- entry_func.add_local_variable(var.self);
- vars_needing_early_declaration.push_back(var.self);
-
- if (var.storage == StorageClassInput)
- {
- uint32_t ib_index = location_meta->ib_index;
- entry_func.fixup_hooks_in.push_back([=, &var]() {
- statement(to_name(var.self), " = ", ib_var_ref, ".", to_member_name(ib_type, ib_index),
- vector_swizzle(type_components, start_component), ";");
- });
- }
- else
- {
- uint32_t ib_index = location_meta->ib_index;
- entry_func.fixup_hooks_out.push_back([=, &var]() {
- statement(ib_var_ref, ".", to_member_name(ib_type, ib_index),
- vector_swizzle(type_components, start_component), " = ", to_name(var.self), ";");
- });
- }
- return;
- }
- else
- {
- location_meta->ib_index = uint32_t(ib_type.member_types.size());
- type_id = build_extended_vector_type(type_id, num_components);
- if (var.storage == StorageClassInput)
- padded_input = true;
- else
- padded_output = true;
- }
- }
- else if (pad_fragment_output)
+ if (pad_fragment_output)
{
uint32_t locn = get_decoration(var.self, DecorationLocation);
target_components = get_target_components_for_fragment_location(locn);
@@ -2169,11 +2204,8 @@ void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, co
uint32_t locn = get_decoration(var.self, DecorationLocation);
if (storage == StorageClassInput)
{
- type_id = ensure_correct_input_type(var.basetype, locn,
- location_meta ? location_meta->num_components : 0,
- meta.strip_array);
- if (!location_meta)
- var.basetype = type_id;
+ type_id = ensure_correct_input_type(var.basetype, locn, 0, meta.strip_array);
+ var.basetype = type_id;
type_id = get_pointee_type_id(type_id);
if (meta.strip_array && is_array(get<SPIRType>(type_id)))
@@ -2193,13 +2225,10 @@ void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, co
mark_location_as_used_by_shader(locn, type, storage);
}
- if (!location_meta)
+ if (get_decoration_bitset(var.self).get(DecorationComponent))
{
- if (get_decoration_bitset(var.self).get(DecorationComponent))
- {
- uint32_t component = get_decoration(var.self, DecorationComponent);
- set_member_decoration(ib_type.self, ib_mbr_idx, DecorationComponent, component);
- }
+ uint32_t component = get_decoration(var.self, DecorationComponent);
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationComponent, component);
}
if (get_decoration_bitset(var.self).get(DecorationIndex))
@@ -2229,10 +2258,7 @@ void CompilerMSL::add_plain_variable_to_interface_block(StorageClass storage, co
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample);
}
- // If we have location meta, there is no unique OrigID. We won't need it, since we flatten/unflatten
- // the variable to stack anyways here.
- if (!location_meta)
- set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self);
+ set_extended_member_decoration(ib_type.self, ib_mbr_idx, SPIRVCrossDecorationInterfaceOrigID, var.self);
}
void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage, const string &ib_var_ref,
@@ -2243,6 +2269,9 @@ void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage
auto &var_type = meta.strip_array ? get_variable_element_type(var) : get_variable_data_type(var);
uint32_t elem_cnt = 0;
+ if (add_component_variable_to_interface_block(storage, ib_var_ref, var, var_type, meta))
+ return;
+
if (is_matrix(var_type))
{
if (is_array(var_type))
@@ -2339,6 +2368,7 @@ void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage
if (get_decoration_bitset(var.self).get(DecorationLocation))
{
uint32_t locn = get_decoration(var.self, DecorationLocation) + i;
+ uint32_t comp = get_decoration(var.self, DecorationComponent);
if (storage == StorageClassInput)
{
var.basetype = ensure_correct_input_type(var.basetype, locn, 0, meta.strip_array);
@@ -2349,6 +2379,8 @@ void CompilerMSL::add_composite_variable_to_interface_block(StorageClass storage
ib_type.member_types[ib_mbr_idx] = mbr_type_id;
}
set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, locn);
+ if (comp)
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationComponent, comp);
mark_location_as_used_by_shader(locn, *usable_type, storage);
}
else if (is_builtin && is_tessellation_shader() && inputs_by_builtin.count(builtin))
@@ -3319,7 +3351,6 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch)
// Need to deal specially with DecorationComponent.
// Multiple variables can alias the same Location, and try to make sure each location is declared only once.
// We will swizzle data in and out to make this work.
- // We only need to consider plain variables here, not composites.
// This is only relevant for vertex inputs and fragment outputs.
// Technically tessellation as well, but it is too complicated to support.
uint32_t component = get_decoration(var_id, DecorationComponent);
@@ -3329,8 +3360,22 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch)
SPIRV_CROSS_THROW("Component decoration is not supported in tessellation shaders.");
else if (pack_components)
{
- auto &location_meta = meta.location_meta[location];
- location_meta.num_components = std::max(location_meta.num_components, component + type.vecsize);
+ uint32_t array_size = 1;
+ if (!type.array.empty())
+ array_size = to_array_size_literal(type);
+
+ for (uint32_t location_offset = 0; location_offset < array_size; location_offset++)
+ {
+ auto &location_meta = meta.location_meta[location + location_offset];
+ location_meta.num_components = std::max(location_meta.num_components, component + type.vecsize);
+
+ // For variables sharing location, decorations and base type must match.
+ location_meta.base_type_id = type.self;
+ location_meta.flat = has_decoration(var.self, DecorationFlat);
+ location_meta.noperspective = has_decoration(var.self, DecorationNoPerspective);
+ location_meta.centroid = has_decoration(var.self, DecorationCentroid);
+ location_meta.sample = has_decoration(var.self, DecorationSample);
+ }
}
}
}
@@ -3579,6 +3624,31 @@ uint32_t CompilerMSL::add_interface_block(StorageClass storage, bool patch)
}
}
+ // When multiple variables need to access same location,
+ // unroll locations one by one and we will flatten output or input as necessary.
+ for (auto &loc : meta.location_meta)
+ {
+ uint32_t location = loc.first;
+ auto &location_meta = loc.second;
+
+ uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size());
+ uint32_t type_id = build_extended_vector_type(location_meta.base_type_id, location_meta.num_components);
+ ib_type.member_types.push_back(type_id);
+
+ set_member_name(ib_type.self, ib_mbr_idx, join("m_location_", location));
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationLocation, location);
+ mark_location_as_used_by_shader(location, get<SPIRType>(type_id), storage);
+
+ if (location_meta.flat)
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationFlat);
+ if (location_meta.noperspective)
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationNoPerspective);
+ if (location_meta.centroid)
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationCentroid);
+ if (location_meta.sample)
+ set_member_decoration(ib_type.self, ib_mbr_idx, DecorationSample);
+ }
+
// Sort the members of the structure by their locations.
MemberSorter member_sorter(ib_type, ir.meta[ib_type_id], MemberSorter::LocationThenBuiltInType);
member_sorter.sort();
@@ -15112,6 +15182,8 @@ bool CompilerMSL::MemberSorter::operator()(uint32_t mbr_idx1, uint32_t mbr_idx2)
return mbr_meta2.builtin;
else if (mbr_meta1.builtin)
return mbr_meta1.builtin_type < mbr_meta2.builtin_type;
+ else if (mbr_meta1.location == mbr_meta2.location)
+ return mbr_meta1.component < mbr_meta2.component;
else
return mbr_meta1.location < mbr_meta2.location;
}