Skip to content

Commit af787a8

Browse files
Merge pull request #1264 from KhronosGroup/msl-argument-buffer-persist
MSL: Add support for force-activating IAB resources.
2 parents 4054d65 + c3bd136 commit af787a8

9 files changed

Lines changed: 85 additions & 2 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ if (SPIRV_CROSS_STATIC)
312312
endif()
313313

314314
set(spirv-cross-abi-major 0)
315-
set(spirv-cross-abi-minor 22)
315+
set(spirv-cross-abi-minor 23)
316316
set(spirv-cross-abi-patch 0)
317317

318318
if (SPIRV_CROSS_SHARED)

main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ struct CLIArguments
521521
bool msl_view_index_from_device_index = false;
522522
bool msl_dispatch_base = false;
523523
bool msl_decoration_binding = false;
524+
bool msl_force_active_argument_buffer_resources = false;
524525
bool glsl_emit_push_constant_as_ubo = false;
525526
bool glsl_emit_ubo_as_plain_uniforms = false;
526527
bool vulkan_glsl_disable_ext_samplerless_texture_functions = false;
@@ -612,6 +613,7 @@ static void print_help()
612613
"\t[--msl-dispatch-base]\n"
613614
"\t[--msl-dynamic-buffer <set index> <binding>]\n"
614615
"\t[--msl-decoration-binding]\n"
616+
"\t[--msl-force-active-argument-buffer-resources]\n"
615617
"\t[--hlsl]\n"
616618
"\t[--reflect]\n"
617619
"\t[--shader-model]\n"
@@ -801,6 +803,7 @@ static string compile_iteration(const CLIArguments &args, std::vector<uint32_t>
801803
msl_opts.view_index_from_device_index = args.msl_view_index_from_device_index;
802804
msl_opts.dispatch_base = args.msl_dispatch_base;
803805
msl_opts.enable_decoration_binding = args.msl_decoration_binding;
806+
msl_opts.force_active_argument_buffer_resources = args.msl_force_active_argument_buffer_resources;
804807
msl_comp->set_msl_options(msl_opts);
805808
for (auto &v : args.msl_discrete_descriptor_sets)
806809
msl_comp->add_discrete_descriptor_set(v);
@@ -1148,6 +1151,9 @@ static int main_inner(int argc, char *argv[])
11481151
args.msl_dynamic_buffers.push_back(make_pair(desc_set, binding));
11491152
});
11501153
cbs.add("--msl-decoration-binding", [&args](CLIParser &) { args.msl_decoration_binding = true; });
1154+
cbs.add("--msl-force-active-argument-buffer-resources", [&args](CLIParser &) {
1155+
args.msl_force_active_argument_buffer_resources = true;
1156+
});
11511157
cbs.add("--extension", [&args](CLIParser &parser) { args.extensions.push_back(parser.next_string()); });
11521158
cbs.add("--rename-entry-point", [&args](CLIParser &parser) {
11531159
auto old_name = parser.next_string();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <metal_stdlib>
2+
#include <simd/simd.h>
3+
4+
using namespace metal;
5+
6+
struct spvDescriptorSetBuffer0
7+
{
8+
texture2d<float> uTexture2 [[id(0)]];
9+
sampler uTexture2Smplr [[id(1)]];
10+
texture2d<float> uTexture1 [[id(2)]];
11+
sampler uTexture1Smplr [[id(3)]];
12+
};
13+
14+
struct main0_out
15+
{
16+
float4 FragColor [[color(0)]];
17+
};
18+
19+
struct main0_in
20+
{
21+
float2 vUV [[user(locn0)]];
22+
};
23+
24+
fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], texture2d<float> uTextureDiscrete2 [[texture(0)]], sampler uTextureDiscrete2Smplr [[sampler(0)]])
25+
{
26+
main0_out out = {};
27+
out.FragColor = spvDescriptorSet0.uTexture2.sample(spvDescriptorSet0.uTexture2Smplr, in.vUV);
28+
out.FragColor += uTextureDiscrete2.sample(uTextureDiscrete2Smplr, in.vUV);
29+
return out;
30+
}
31+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#version 450
2+
3+
layout(location = 0) in vec2 vUV;
4+
layout(location = 0) out vec4 FragColor;
5+
6+
layout(set = 0, binding = 0) uniform sampler2D uTexture1;
7+
layout(set = 0, binding = 1) uniform sampler2D uTexture2;
8+
layout(set = 2, binding = 0) uniform sampler2D uTextureDiscrete1;
9+
layout(set = 2, binding = 1) uniform sampler2D uTextureDiscrete2;
10+
11+
void main()
12+
{
13+
FragColor = texture(uTexture2, vUV);
14+
FragColor += texture(uTextureDiscrete2, vUV);
15+
}

spirv_cross_c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ spvc_result spvc_compiler_options_set_uint(spvc_compiler_options options, spvc_c
593593
case SPVC_COMPILER_OPTION_MSL_ENABLE_DECORATION_BINDING:
594594
options->msl.enable_decoration_binding = value != 0;
595595
break;
596+
597+
case SPVC_COMPILER_OPTION_MSL_FORCE_ACTIVE_ARGUMENT_BUFFER_RESOURCES:
598+
options->msl.force_active_argument_buffer_resources = value != 0;
599+
break;
596600
#endif
597601

598602
default:

spirv_cross_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C" {
3333
/* Bumped if ABI or API breaks backwards compatibility. */
3434
#define SPVC_C_API_VERSION_MAJOR 0
3535
/* Bumped if APIs or enumerations are added in a backwards compatible way. */
36-
#define SPVC_C_API_VERSION_MINOR 22
36+
#define SPVC_C_API_VERSION_MINOR 23
3737
/* Bumped if internal implementation details change. */
3838
#define SPVC_C_API_VERSION_PATCH 0
3939

@@ -571,6 +571,7 @@ typedef enum spvc_compiler_option
571571
SPVC_COMPILER_OPTION_MSL_INVARIANT_FP_MATH = 47 | SPVC_COMPILER_OPTION_MSL_BIT,
572572
SPVC_COMPILER_OPTION_MSL_EMULATE_CUBEMAP_ARRAY = 48 | SPVC_COMPILER_OPTION_MSL_BIT,
573573
SPVC_COMPILER_OPTION_MSL_ENABLE_DECORATION_BINDING = 49 | SPVC_COMPILER_OPTION_MSL_BIT,
574+
SPVC_COMPILER_OPTION_MSL_FORCE_ACTIVE_ARGUMENT_BUFFER_RESOURCES = 50 | SPVC_COMPILER_OPTION_MSL_BIT,
574575

575576
SPVC_COMPILER_OPTION_INT_MAX = 0x7fffffff
576577
} spvc_compiler_option;

spirv_msl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,9 @@ string CompilerMSL::compile()
10031003
fixup_image_load_store_access();
10041004

10051005
set_enabled_interface_variables(get_active_interface_variables());
1006+
if (msl_options.force_active_argument_buffer_resources)
1007+
activate_argument_buffer_resources();
1008+
10061009
if (swizzle_buffer_id)
10071010
active_interface_variables.insert(swizzle_buffer_id);
10081011
if (buffer_size_buffer_id)
@@ -12691,3 +12694,17 @@ void CompilerMSL::analyze_argument_buffers()
1269112694
}
1269212695
}
1269312696
}
12697+
12698+
void CompilerMSL::activate_argument_buffer_resources()
12699+
{
12700+
// For ABI compatibility, force-enable all resources which are part of argument buffers.
12701+
ir.for_each_typed_id<SPIRVariable>([&](uint32_t self, const SPIRVariable &) {
12702+
if (!has_decoration(self, DecorationDescriptorSet))
12703+
return;
12704+
12705+
uint32_t desc_set = get_decoration(self, DecorationDescriptorSet);
12706+
if (descriptor_set_is_argument_buffer(desc_set))
12707+
active_interface_variables.insert(self);
12708+
});
12709+
}
12710+

spirv_msl.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ class CompilerMSL : public CompilerGLSL
307307
// Requires MSL 2.1, use the native support for texel buffers.
308308
bool texture_buffer_native = false;
309309

310+
// Forces all resources which are part of an argument buffer to be considered active.
311+
// This ensures ABI compatibility between shaders where some resources might be unused,
312+
// and would otherwise declare a different IAB.
313+
bool force_active_argument_buffer_resources = false;
314+
310315
bool is_ios()
311316
{
312317
return platform == iOS;
@@ -863,6 +868,8 @@ class CompilerMSL : public CompilerGLSL
863868

864869
void add_spv_func_and_recompile(SPVFuncImpl spv_func);
865870

871+
void activate_argument_buffer_resources();
872+
866873
// OpcodeHandler that handles several MSL preprocessing operations.
867874
struct OpCodePreprocessor : OpcodeHandler
868875
{

test_shaders.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ def cross_compile_msl(shader, spirv, opt, iterations, paths):
226226
msl_args.append('2')
227227
msl_args.append('--msl-discrete-descriptor-set')
228228
msl_args.append('3')
229+
if '.force-active.' in shader:
230+
msl_args.append('--msl-force-active-argument-buffer-resources')
229231
if '.line.' in shader:
230232
msl_args.append('--emit-line-directives')
231233
if '.multiview.' in shader:

0 commit comments

Comments
 (0)