Skip to content

Commit

Permalink
Fix volatile helper invocation in non-main functions
Browse files Browse the repository at this point in the history
  • Loading branch information
etang-cw committed Aug 5, 2024
1 parent 66363ac commit 228d148
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"

#include <metal_stdlib>
#include <simd/simd.h>

Expand All @@ -8,18 +10,24 @@ struct main0_out
float FragColor [[color(0)]];
};

static inline __attribute__((always_inline))
void func(thread float& FragColor, volatile thread bool& gl_HelperInvocation)
{
bool _14 = gl_HelperInvocation;
float _17 = float(_14);
FragColor = _17;
gl_HelperInvocation = true, discard_fragment();
bool _18 = gl_HelperInvocation;
float _19 = float(_18);
FragColor = _19;
}

fragment main0_out main0()
{
main0_out out = {};
bool gl_HelperInvocation = {};
gl_HelperInvocation = simd_is_helper_thread();
bool _12 = gl_HelperInvocation;
float _15 = float(_12);
out.FragColor = _15;
gl_HelperInvocation = true, discard_fragment();
bool _16 = gl_HelperInvocation;
float _17 = float(_16);
out.FragColor = _17;
func(out.FragColor, gl_HelperInvocation);
return out;
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

layout(location = 0) out float FragColor;

void main()
void func()
{
FragColor = float(gl_HelperInvocation);
demote;
FragColor = float(gl_HelperInvocation);
}

void main()
{
func();
}
7 changes: 4 additions & 3 deletions spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,14 @@ void CompilerMSL::build_implicit_builtins()
active_input_builtins.get(BuiltInInstanceIndex) || active_input_builtins.get(BuiltInBaseInstance));
bool need_local_invocation_index = msl_options.emulate_subgroups && active_input_builtins.get(BuiltInSubgroupId);
bool need_workgroup_size = msl_options.emulate_subgroups && active_input_builtins.get(BuiltInNumSubgroups);
bool needs_helper_invocation_ = needs_helper_invocation || active_input_builtins.get(BuiltInHelperInvocation);
bool force_frag_depth_passthrough =
get_execution_model() == ExecutionModelFragment && !uses_explicit_early_fragment_test() && need_subpass_input &&
msl_options.enable_frag_depth_builtin && msl_options.input_attachment_is_ds_attachment;

if (need_subpass_input || need_sample_pos || need_subgroup_mask || need_vertex_params || need_tesc_params ||
need_tese_params || need_multiview || need_dispatch_base || need_vertex_base_params || need_grid_params ||
needs_sample_id || needs_subgroup_invocation_id || needs_subgroup_size || needs_helper_invocation ||
needs_sample_id || needs_subgroup_invocation_id || needs_subgroup_size || needs_helper_invocation_ ||
has_additional_fixed_sample_mask() || need_local_invocation_index || need_workgroup_size || force_frag_depth_passthrough)
{
bool has_frag_coord = false;
Expand Down Expand Up @@ -457,7 +458,7 @@ void CompilerMSL::build_implicit_builtins()
}
}

if (needs_helper_invocation && builtin == BuiltInHelperInvocation)
if (needs_helper_invocation_ && builtin == BuiltInHelperInvocation)
{
builtin_helper_invocation_id = var.self;
mark_implicit_builtin(StorageClassInput, BuiltInHelperInvocation, var.self);
Expand Down Expand Up @@ -840,7 +841,7 @@ void CompilerMSL::build_implicit_builtins()
mark_implicit_builtin(StorageClassOutput, BuiltInSampleMask, var_id);
}

if (!has_helper_invocation && needs_helper_invocation)
if (!has_helper_invocation && needs_helper_invocation_)
{
uint32_t offset = ir.increase_bound_by(3);
uint32_t type_id = offset;
Expand Down

0 comments on commit 228d148

Please sign in to comment.