Skip to content

Commit

Permalink
MSL: Handle stores to struct bool[].
Browse files Browse the repository at this point in the history
  • Loading branch information
HansKristian-Work committed Jun 6, 2023
1 parent c4eb231 commit 01b9826
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
8 changes: 6 additions & 2 deletions spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5109,9 +5109,12 @@ 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 = type;
SPIRType tmp_type;
if (remapped_boolean)
{
tmp_type = get<SPIRType>(type.parent_type);
tmp_type.basetype = backend.boolean_in_struct_remapped_type;
}

uint32_t size = to_array_size_literal(type);
auto &parent = get<SPIRType>(type.parent_type);
Expand Down Expand Up @@ -17374,9 +17377,10 @@ uint32_t CompilerGLSL::mask_relevant_memory_semantics(uint32_t semantics)
MemorySemanticsCrossWorkgroupMemoryMask | MemorySemanticsSubgroupMemoryMask);
}

void CompilerGLSL::emit_array_copy(const string &lhs, uint32_t, uint32_t rhs_id, StorageClass, StorageClass)
bool CompilerGLSL::emit_array_copy(const string &lhs, uint32_t, uint32_t rhs_id, StorageClass, StorageClass)
{
statement(lhs, " = ", to_expression(rhs_id), ";");
return true;
}

bool CompilerGLSL::unroll_array_to_complex_store(uint32_t target_id, uint32_t source_id)
Expand Down
2 changes: 1 addition & 1 deletion spirv_glsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ class CompilerGLSL : public Compiler
std::string layout_for_variable(const SPIRVariable &variable);
std::string to_combined_image_sampler(VariableID image_id, VariableID samp_id);
virtual bool skip_argument(uint32_t id) const;
virtual void emit_array_copy(const std::string &lhs, uint32_t lhs_id, uint32_t rhs_id,
virtual bool emit_array_copy(const std::string &lhs, uint32_t lhs_id, uint32_t rhs_id,
spv::StorageClass lhs_storage, spv::StorageClass rhs_storage);
virtual void emit_block_hints(const SPIRBlock &block);
virtual std::string to_initializer_expression(const SPIRVariable &var);
Expand Down
36 changes: 27 additions & 9 deletions spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9470,7 +9470,7 @@ static bool storage_class_array_is_thread(StorageClass storage)
}
}

void CompilerMSL::emit_array_copy(const string &lhs, uint32_t lhs_id, uint32_t rhs_id,
bool CompilerMSL::emit_array_copy(const string &lhs, uint32_t lhs_id, uint32_t rhs_id,
StorageClass lhs_storage, StorageClass rhs_storage)
{
// Allow Metal to use the array<T> template to make arrays a value type.
Expand Down Expand Up @@ -9509,7 +9509,8 @@ void CompilerMSL::emit_array_copy(const string &lhs, uint32_t lhs_id, uint32_t r
// Avoid spvCopy* wrapper functions; Otherwise, spvUnsafeArray<> template cannot be used with that storage qualifier.
if (lhs_is_array_template && rhs_is_array_template && !using_builtin_array())
{
statement(lhs, " = ", to_expression(rhs_id), ";");
// Fall back to normal copy path.
return false;
}
else
{
Expand Down Expand Up @@ -9586,6 +9587,8 @@ void CompilerMSL::emit_array_copy(const string &lhs, uint32_t lhs_id, uint32_t r
else
statement("spvArrayCopy", tag, type.array.size(), "(", lhs, ", ", to_expression(rhs_id), ");");
}

return true;
}

uint32_t CompilerMSL::get_physical_tess_level_array_size(spv::BuiltIn builtin) const
Expand Down Expand Up @@ -9649,7 +9652,9 @@ bool CompilerMSL::maybe_emit_array_assignment(uint32_t id_lhs, uint32_t id_rhs)

auto lhs_storage = get_expression_effective_storage_class(id_lhs);
auto rhs_storage = get_expression_effective_storage_class(id_rhs);
emit_array_copy(to_expression(id_lhs), id_lhs, id_rhs, lhs_storage, rhs_storage);
if (!emit_array_copy(to_expression(id_lhs), id_lhs, id_rhs, lhs_storage, rhs_storage))
return false;

register_write(id_lhs);

return true;
Expand Down Expand Up @@ -16911,24 +16916,37 @@ void CompilerMSL::cast_to_variable_store(uint32_t target_id, std::string &expr,
auto *target_expr = maybe_get<SPIRExpression>(target_id);
auto *var = maybe_get_backing_variable(target_id);
const SPIRType *var_type = nullptr, *phys_type = nullptr;

if (uint32_t phys_id = get_extended_decoration(target_id, SPIRVCrossDecorationPhysicalTypeID))
phys_type = &get<SPIRType>(phys_id);
else
phys_type = &expr_type;

if (var)
{
target_id = var->self;
var_type = &get_variable_data_type(*var);
}

// Type fixups for workgroup variables if they are booleans.
if (var && (var->storage == StorageClassWorkgroup || var_type->basetype == SPIRType::Struct) &&
expr_type.basetype == SPIRType::Boolean)
bool rewrite_boolean_store =
expr_type.basetype == SPIRType::Boolean &&
(var && (var->storage == StorageClassWorkgroup || var_type->basetype == SPIRType::Struct));

// Type fixups for workgroup variables or struct members if they are booleans.
if (rewrite_boolean_store)
{
auto short_type = expr_type;
short_type.basetype = SPIRType::Short;
expr = join(type_to_glsl(short_type), "(", expr, ")");
if (type_is_top_level_array(expr_type))
{
expr = to_rerolled_array_expression(*var_type, expr, expr_type);
}
else
{
auto short_type = expr_type;
short_type.basetype = SPIRType::Short;
expr = join(type_to_glsl(short_type), "(", expr, ")");
}
}

// Type fixups for workgroup variables if they are matrices.
// Don't do fixup for packed types; those are handled specially.
// FIXME: Maybe use a type like spvStorageMatrix for packed matrices?
Expand Down
2 changes: 1 addition & 1 deletion spirv_msl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ class CompilerMSL : public CompilerGLSL
void add_pragma_line(const std::string &line);
void add_typedef_line(const std::string &line);
void emit_barrier(uint32_t id_exe_scope, uint32_t id_mem_scope, uint32_t id_mem_sem);
void emit_array_copy(const std::string &lhs, uint32_t lhs_id, uint32_t rhs_id,
bool emit_array_copy(const std::string &lhs, uint32_t lhs_id, uint32_t rhs_id,
spv::StorageClass lhs_storage, spv::StorageClass rhs_storage) override;
void build_implicit_builtins();
uint32_t build_constant_uint_array_pointer();
Expand Down

0 comments on commit 01b9826

Please sign in to comment.