diff --git a/src/back/msl/mod.rs b/src/back/msl/mod.rs index f9ddf51c7d..234aaf2844 100644 --- a/src/back/msl/mod.rs +++ b/src/back/msl/mod.rs @@ -37,20 +37,27 @@ mod writer; pub use writer::Writer; #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] pub enum BindSamplerTarget { Resource(u8), Inline(Handle), } #[derive(Clone, Debug, Default, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] pub struct BindTarget { + #[cfg_attr(feature = "deserialize", serde(default))] pub buffer: Option, + #[cfg_attr(feature = "deserialize", serde(default))] pub texture: Option, + #[cfg_attr(feature = "deserialize", serde(default))] pub sampler: Option, + #[cfg_attr(feature = "deserialize", serde(default))] pub mutable: bool, } #[derive(Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)] +#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] pub struct BindSource { pub stage: crate::ShaderStage, pub group: u32, @@ -102,6 +109,7 @@ enum LocationMode { } #[derive(Debug)] +#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] pub struct Options { /// (Major, Minor) target version of the Metal Shading Language. pub lang_version: (u8, u8), @@ -129,6 +137,7 @@ impl Default for Options { // A subset of options that are meant to be changed per pipeline. #[derive(Debug, Clone)] +#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] pub struct SubOptions { /// Allow `BuiltIn::PointSize` in the vertex shader. /// Metal doesn't like this for non-point primitive topologies. diff --git a/src/back/msl/sampler.rs b/src/back/msl/sampler.rs index f9b57e4b57..be44045c71 100644 --- a/src/back/msl/sampler.rs +++ b/src/back/msl/sampler.rs @@ -1,4 +1,8 @@ +#[cfg(feature = "deserialize")] +use serde::Deserialize; + #[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum Coord { Normalized, Pixel, @@ -11,6 +15,7 @@ impl Default for Coord { } #[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum Address { Repeat, MirroredRepeat, @@ -38,6 +43,7 @@ impl Address { } #[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum BorderColor { TransparentBlack, OpaqueBlack, @@ -61,6 +67,7 @@ impl BorderColor { } #[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum Filter { Nearest, Linear, @@ -82,6 +89,7 @@ impl Default for Filter { } #[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum CompareFunc { Never, Less, @@ -115,6 +123,7 @@ impl CompareFunc { } #[derive(Clone, Debug, Default, PartialEq)] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] pub struct InlineSampler { pub coord: Coord, pub address: [Address; 3], diff --git a/tests/in/boids.param.ron b/tests/in/boids.param.ron index 16eca7eba4..06a8df2daf 100644 --- a/tests/in/boids.param.ron +++ b/tests/in/boids.param.ron @@ -4,9 +4,16 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: false, - mtl_bindings: { - (stage: Compute, group: 0, binding: 0): (buffer: Some(0), mutable: false), - (stage: Compute, group: 0, binding: 1): (buffer: Some(1), mutable: true), - (stage: Compute, group: 0, binding: 2): (buffer: Some(2), mutable: true), - } + msl_custom: true, + msl: ( + lang_version: (2, 0), + binding_map: { + (stage: Compute, group: 0, binding: 0): (buffer: Some(0), mutable: false), + (stage: Compute, group: 0, binding: 1): (buffer: Some(1), mutable: true), + (stage: Compute, group: 0, binding: 2): (buffer: Some(2), mutable: true), + }, + inline_samplers: [], + spirv_cross_compatibility: false, + fake_missing_bindings: false, + ), ) diff --git a/tests/in/collatz.param.ron b/tests/in/collatz.param.ron index e27b195681..984ca763e0 100644 --- a/tests/in/collatz.param.ron +++ b/tests/in/collatz.param.ron @@ -4,7 +4,5 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: false, - mtl_bindings: { - (stage: Compute, group: 0, binding: 0): (buffer: Some(0), mutable: true), - } + msl_custom: false, ) diff --git a/tests/in/empty.param.ron b/tests/in/empty.param.ron index cd1d89ab89..6de22960da 100644 --- a/tests/in/empty.param.ron +++ b/tests/in/empty.param.ron @@ -3,6 +3,5 @@ spv_capabilities: [ Shader ], spv_debug: false, spv_adjust_coordinate_space: false, - mtl_bindings: { - } + msl_custom: false, ) diff --git a/tests/in/image-copy.param.ron b/tests/in/image-copy.param.ron index 297ffec8d8..52f49f360f 100644 --- a/tests/in/image-copy.param.ron +++ b/tests/in/image-copy.param.ron @@ -3,8 +3,5 @@ spv_capabilities: [ Shader, Image1D, Sampled1D ], spv_debug: true, spv_adjust_coordinate_space: false, - mtl_bindings: { - (stage: Compute, group: 0, binding: 1): (texture: Some(0), mutable: false), - (stage: Compute, group: 0, binding: 2): (texture: Some(1), mutable: true), - } + msl_custom: false, ) diff --git a/tests/in/quad-glsl.param.ron b/tests/in/quad-glsl.param.ron index 3fe357ca6c..efded54c28 100644 --- a/tests/in/quad-glsl.param.ron +++ b/tests/in/quad-glsl.param.ron @@ -3,5 +3,5 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: true, - mtl_bindings: {} + msl_custom: false, ) diff --git a/tests/in/quad.param.ron b/tests/in/quad.param.ron index fcddac8b50..efded54c28 100644 --- a/tests/in/quad.param.ron +++ b/tests/in/quad.param.ron @@ -3,8 +3,5 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: true, - mtl_bindings: { - (stage: Fragment, group: 0, binding: 0): (texture: Some(0)), - (stage: Fragment, group: 0, binding: 1): (sampler: Some(0)), - } + msl_custom: false, ) diff --git a/tests/in/shadow.param.ron b/tests/in/shadow.param.ron index 643ac437be..dc51d3ff45 100644 --- a/tests/in/shadow.param.ron +++ b/tests/in/shadow.param.ron @@ -4,10 +4,5 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: true, - mtl_bindings: { - (stage: Fragment, group: 0, binding: 0): (buffer: Some(0), mutable: false), - (stage: Fragment, group: 0, binding: 1): (buffer: Some(1), mutable: false), - (stage: Fragment, group: 0, binding: 2): (texture: Some(0), mutable: false), - (stage: Fragment, group: 0, binding: 3): (sampler: Some(0), mutable: false), - } + msl_custom: false, ) diff --git a/tests/in/skybox.param.ron b/tests/in/skybox.param.ron index 2373569120..30aded5357 100644 --- a/tests/in/skybox.param.ron +++ b/tests/in/skybox.param.ron @@ -4,9 +4,16 @@ spv_capabilities: [ Shader ], spv_debug: false, spv_adjust_coordinate_space: false, - mtl_bindings: { - (stage: Vertex, group: 0, binding: 0): (buffer: Some(0)), - (stage: Fragment, group: 0, binding: 1): (texture: Some(0)), - (stage: Fragment, group: 0, binding: 2): (sampler: Some(1)), - } + msl_custom: true, + msl: ( + lang_version: (2, 1), + binding_map: { + (stage: Vertex, group: 0, binding: 0): (buffer: Some(0)), + (stage: Fragment, group: 0, binding: 1): (texture: Some(0)), + (stage: Fragment, group: 0, binding: 2): (sampler: Some(Resource(1))), + }, + inline_samplers: [], + spirv_cross_compatibility: false, + fake_missing_bindings: false, + ) ) diff --git a/tests/in/texture-array.param.ron b/tests/in/texture-array.param.ron index 6e3432a8a3..b46594e319 100644 --- a/tests/in/texture-array.param.ron +++ b/tests/in/texture-array.param.ron @@ -4,9 +4,5 @@ spv_capabilities: [ Shader ], spv_debug: true, spv_adjust_coordinate_space: false, - mtl_bindings: { - (stage: Fragment, group: 0, binding: 0): (texture: Some(0)), - (stage: Fragment, group: 0, binding: 1): (texture: Some(1)), - (stage: Fragment, group: 0, binding: 2): (sampler: Some(0)), - } + msl_custom: false, ) diff --git a/tests/out/collatz.msl.snap b/tests/out/collatz.msl.snap index f921cb575c..1a0e7d95d0 100644 --- a/tests/out/collatz.msl.snap +++ b/tests/out/collatz.msl.snap @@ -39,7 +39,7 @@ struct main1Input { }; kernel void main1( type2 global_id [[thread_position_in_grid]] -, device PrimeIndices& v_indices [[buffer(0)]] +, device PrimeIndices& v_indices [[user(fake0)]] ) { type _expr9 = collatz_iterations(v_indices.data[global_id.x]); v_indices.data[global_id.x] = _expr9; diff --git a/tests/out/image-copy.msl.snap b/tests/out/image-copy.msl.snap index 81329b6bd3..6584e9463e 100644 --- a/tests/out/image-copy.msl.snap +++ b/tests/out/image-copy.msl.snap @@ -16,8 +16,8 @@ struct main1Input { }; kernel void main1( type2 local_id [[thread_position_in_threadgroup]] -, type image_src [[texture(0)]] -, type1 image_dst [[texture(1)]] +, type image_src [[user(fake0)]] +, type1 image_dst [[user(fake0)]] ) { metal::int2 _expr12 = (int2(image_src.get_width(), image_src.get_height()) * static_cast(metal::uint2(local_id.x, local_id.y))) % metal::int2(const_10i, const_20i); metal::uint4 _expr13 = image_src.read(metal::uint2(_expr12)); diff --git a/tests/out/quad.msl.snap b/tests/out/quad.msl.snap index 084d7fb87b..47a48a2d18 100644 --- a/tests/out/quad.msl.snap +++ b/tests/out/quad.msl.snap @@ -45,8 +45,8 @@ struct main2Output { }; fragment main2Output main2( main2Input varyings1 [[stage_in]] -, type3 u_texture [[texture(0)]] -, type4 u_sampler [[sampler(0)]] +, type3 u_texture [[user(fake0)]] +, type4 u_sampler [[user(fake0)]] ) { const auto uv2 = varyings1.uv2; metal::float4 _expr4 = u_texture.sample(u_sampler, uv2); diff --git a/tests/out/shadow.msl.snap b/tests/out/shadow.msl.snap index e2a9c1c866..94cd7982e8 100644 --- a/tests/out/shadow.msl.snap +++ b/tests/out/shadow.msl.snap @@ -58,10 +58,10 @@ struct fs_mainOutput { }; fragment fs_mainOutput fs_main( fs_mainInput varyings [[stage_in]] -, constant Globals& u_globals [[buffer(0)]] -, constant Lights& s_lights [[buffer(1)]] -, type4 t_shadow [[texture(0)]] -, type5 sampler_shadow [[sampler(0)]] +, constant Globals& u_globals [[user(fake0)]] +, constant Lights& s_lights [[user(fake0)]] +, type4 t_shadow [[user(fake0)]] +, type5 sampler_shadow [[user(fake0)]] ) { const auto raw_normal = varyings.raw_normal; const auto position = varyings.position; diff --git a/tests/snapshots.rs b/tests/snapshots.rs index a42e7470f2..e0c343c749 100644 --- a/tests/snapshots.rs +++ b/tests/snapshots.rs @@ -9,36 +9,6 @@ bitflags::bitflags! { } } -#[derive(Hash, PartialEq, Eq, serde::Deserialize)] -enum Stage { - Vertex, - Fragment, - Compute, -} - -#[derive(Hash, PartialEq, Eq, serde::Deserialize)] -struct BindSource { - stage: Stage, - group: u32, - binding: u32, -} - -#[derive(serde::Deserialize)] -struct BindTarget { - #[cfg_attr(not(feature = "msl-out"), allow(dead_code))] - #[serde(default)] - buffer: Option, - #[cfg_attr(not(feature = "msl-out"), allow(dead_code))] - #[serde(default)] - texture: Option, - #[cfg_attr(not(feature = "msl-out"), allow(dead_code))] - #[serde(default)] - sampler: Option, - #[cfg_attr(not(feature = "msl-out"), allow(dead_code))] - #[serde(default)] - mutable: bool, -} - #[derive(Default, serde::Deserialize)] struct Parameters { #[cfg_attr(not(feature = "spv-out"), allow(dead_code))] @@ -49,8 +19,11 @@ struct Parameters { spv_debug: bool, #[cfg_attr(not(feature = "spv-out"), allow(dead_code))] spv_adjust_coordinate_space: bool, - #[cfg_attr(not(feature = "msl-out"), allow(dead_code))] - mtl_bindings: naga::FastHashMap, + #[cfg(all(feature = "deserialize", feature = "msl-out"))] + #[serde(default)] + msl: naga::back::msl::Options, + #[cfg(all(not(feature = "deserialize"), feature = "msl-out"))] + msl_custom: bool, } #[allow(dead_code)] @@ -162,38 +135,23 @@ fn check_output_msl( ) { use naga::back::msl; - let mut binding_map = msl::BindingMap::default(); - for (key, value) in params.mtl_bindings.iter() { - binding_map.insert( - msl::BindSource { - stage: match key.stage { - Stage::Vertex => naga::ShaderStage::Vertex, - Stage::Fragment => naga::ShaderStage::Fragment, - Stage::Compute => naga::ShaderStage::Compute, - }, - group: key.group, - binding: key.binding, - }, - msl::BindTarget { - buffer: value.buffer, - texture: value.texture, - sampler: value.sampler.map(msl::BindSamplerTarget::Resource), - mutable: value.mutable, - }, - ); - } - let options = msl::Options { - lang_version: (1, 0), - binding_map, - inline_samplers: naga::Arena::new(), - spirv_cross_compatibility: false, - fake_missing_bindings: false, + #[cfg_attr(feature = "deserialize", allow(unused_variables))] + let default_options = msl::Options::default(); + #[cfg(feature = "deserialize")] + let options = ¶ms.msl; + #[cfg(not(feature = "deserialize"))] + let options = if params.msl_custom { + println!("Skipping {}", name); + return; + } else { + &default_options }; + let sub_options = msl::SubOptions { allow_point_size: true, }; - let (msl, _) = msl::write_string(module, info, &options, &sub_options).unwrap(); + let (msl, _) = msl::write_string(module, info, options, &sub_options).unwrap(); with_snapshot_settings(|| { insta::assert_snapshot!(format!("{}.msl", name), msl);