Re-use the MSL types in snapshot code

This commit is contained in:
Dzmitry Malyshau
2021-04-07 10:29:35 -04:00
committed by Dzmitry Malyshau
parent 9e09c792dc
commit 7c11759041
16 changed files with 75 additions and 103 deletions

View File

@@ -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<sampler::InlineSampler>),
}
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct BindTarget {
#[cfg_attr(feature = "deserialize", serde(default))]
pub buffer: Option<u8>,
#[cfg_attr(feature = "deserialize", serde(default))]
pub texture: Option<u8>,
#[cfg_attr(feature = "deserialize", serde(default))]
pub sampler: Option<BindSamplerTarget>,
#[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.

View File

@@ -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],

View File

@@ -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,
),
)

View File

@@ -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,
)

View File

@@ -3,6 +3,5 @@
spv_capabilities: [ Shader ],
spv_debug: false,
spv_adjust_coordinate_space: false,
mtl_bindings: {
}
msl_custom: false,
)

View File

@@ -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,
)

View File

@@ -3,5 +3,5 @@
spv_capabilities: [ Shader ],
spv_debug: true,
spv_adjust_coordinate_space: true,
mtl_bindings: {}
msl_custom: false,
)

View File

@@ -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,
)

View File

@@ -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,
)

View File

@@ -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,
)
)

View File

@@ -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,
)

View File

@@ -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;

View File

@@ -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<int2>(metal::uint2(local_id.x, local_id.y))) % metal::int2(const_10i, const_20i);
metal::uint4 _expr13 = image_src.read(metal::uint2(_expr12));

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<u8>,
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
#[serde(default)]
texture: Option<u8>,
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
#[serde(default)]
sampler: Option<u8>,
#[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<BindSource, BindTarget>,
#[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 = &params.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);