diff --git a/Makefile b/Makefile index 85a139952..380bca01f 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ validate-spv: $(SNAPSHOTS_BASE_OUT)/spv/*.spvasm validate-msl: $(SNAPSHOTS_BASE_OUT)/msl/*.msl @set -e && for file in $^ ; do \ echo "Validating" $${file#"$(SNAPSHOTS_BASE_OUT)/"}; \ - cat $${file} | xcrun -sdk macosx metal -mmacosx-version-min=10.11 -x metal - -o /dev/null; \ + cat $${file} | xcrun -sdk macosx metal -mmacosx-version-min=10.15 -x metal - -o /dev/null; \ done validate-glsl: $(SNAPSHOTS_BASE_OUT)/glsl/*.glsl diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index 66209e6b3..5a5b879da 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -2447,6 +2447,7 @@ fn glsl_built_in(built_in: crate::BuiltIn, output: bool) -> &'static str { // fragment Bi::FragDepth => "gl_FragDepth", Bi::FrontFacing => "gl_FrontFacing", + Bi::PrimitiveIndex => "uint(gl_PrimitiveID)", Bi::SampleIndex => "gl_SampleID", Bi::SampleMask => { if output { diff --git a/src/back/hlsl/writer.rs b/src/back/hlsl/writer.rs index 418e73565..2e80a0803 100644 --- a/src/back/hlsl/writer.rs +++ b/src/back/hlsl/writer.rs @@ -1625,6 +1625,7 @@ fn builtin_str(built_in: crate::BuiltIn) -> &'static str { // fragment Bi::FragDepth => "SV_Depth", Bi::FrontFacing => "SV_IsFrontFace", + Bi::PrimitiveIndex => "SV_PrimitiveID", Bi::SampleIndex => "SV_SampleIndex", Bi::SampleMask => "SV_Coverage", // compute diff --git a/src/back/msl/mod.rs b/src/back/msl/mod.rs index 33d2e1a15..3277991b3 100644 --- a/src/back/msl/mod.rs +++ b/src/back/msl/mod.rs @@ -348,6 +348,7 @@ impl ResolvedBinding { // fragment Bi::FragDepth => "depth(any)", Bi::FrontFacing => "front_facing", + Bi::PrimitiveIndex => "primitive_id", Bi::SampleIndex => "sample_id", Bi::SampleMask => "sample_mask", // compute diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index 8346e8f42..7fd292129 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -1012,6 +1012,10 @@ impl Writer { // fragment Bi::FragDepth => BuiltIn::FragDepth, Bi::FrontFacing => BuiltIn::FrontFacing, + Bi::PrimitiveIndex => { + self.capabilities.insert(spirv::Capability::Geometry); + BuiltIn::PrimitiveId + } Bi::SampleIndex => BuiltIn::SampleId, Bi::SampleMask => BuiltIn::SampleMask, // compute diff --git a/src/back/wgsl/writer.rs b/src/back/wgsl/writer.rs index 7e7db5f02..8cfafd122 100644 --- a/src/back/wgsl/writer.rs +++ b/src/back/wgsl/writer.rs @@ -1479,6 +1479,7 @@ fn builtin_str(built_in: crate::BuiltIn) -> Option<&'static str> { Bi::WorkGroupSize => Some("workgroup_size"), Bi::SampleIndex => Some("sample_index"), Bi::SampleMask => Some("sample_mask"), + Bi::PrimitiveIndex => Some("primitive_index"), _ => None, } } diff --git a/src/front/glsl/variables.rs b/src/front/glsl/variables.rs index 98994bed2..c0aae1876 100644 --- a/src/front/glsl/variables.rs +++ b/src/front/glsl/variables.rs @@ -166,6 +166,16 @@ impl Program<'_> { PrologueStage::FRAGMENT, StorageQualifier::Input, ), + "gl_PrimitiveID" => add_builtin( + TypeInner::Scalar { + kind: ScalarKind::Uint, + width: 4, + }, + BuiltIn::PrimitiveIndex, + false, + PrologueStage::FRAGMENT, + StorageQualifier::Input, + ), _ => Ok(None), } } diff --git a/src/front/spv/convert.rs b/src/front/spv/convert.rs index dc429c290..3bb35249a 100644 --- a/src/front/spv/convert.rs +++ b/src/front/spv/convert.rs @@ -132,6 +132,7 @@ pub(super) fn map_builtin(word: spirv::Word) -> Result { // fragment Some(Bi::FragDepth) => crate::BuiltIn::FragDepth, Some(Bi::FrontFacing) => crate::BuiltIn::FrontFacing, + Some(Bi::PrimitiveId) => crate::BuiltIn::PrimitiveIndex, Some(Bi::SampleId) => crate::BuiltIn::SampleIndex, Some(Bi::SampleMask) => crate::BuiltIn::SampleMask, // compute diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index d03ea8b33..01ecda65d 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -69,6 +69,7 @@ pub const SUPPORTED_CAPABILITIES: &[spirv::Capability] = &[ spirv::Capability::Int8, spirv::Capability::Int16, spirv::Capability::Int64, + spirv::Capability::Geometry, // tricky ones spirv::Capability::UniformBufferArrayDynamicIndexing, spirv::Capability::StorageBufferArrayDynamicIndexing, @@ -3482,6 +3483,7 @@ impl> Parser { | crate::BuiltIn::InstanceIndex | crate::BuiltIn::SampleIndex | crate::BuiltIn::VertexIndex + | crate::BuiltIn::PrimitiveIndex | crate::BuiltIn::LocalInvocationIndex => Some(crate::TypeInner::Scalar { kind: crate::ScalarKind::Uint, width: 4, diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index edd4e3019..6f7d1565b 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -20,6 +20,7 @@ pub fn map_built_in(word: &str, span: Span) -> Result> // fragment "front_facing" => crate::BuiltIn::FrontFacing, "frag_depth" => crate::BuiltIn::FragDepth, + "primitive_index" => crate::BuiltIn::PrimitiveIndex, "sample_index" => crate::BuiltIn::SampleIndex, "sample_mask" => crate::BuiltIn::SampleMask, // compute diff --git a/src/lib.rs b/src/lib.rs index 540d6ef5f..14997a734 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -269,6 +269,7 @@ pub enum BuiltIn { // fragment FragDepth, FrontFacing, + PrimitiveIndex, SampleIndex, SampleMask, // compute diff --git a/src/valid/interface.rs b/src/valid/interface.rs index d4050aea1..c4b4f83ae 100644 --- a/src/valid/interface.rs +++ b/src/valid/interface.rs @@ -53,6 +53,8 @@ pub enum VaryingError { BindingCollision { location: u32 }, #[error("Built-in {0:?} is present more than once")] DuplicateBuiltIn(crate::BuiltIn), + #[error("Capability {0:?} is not supported")] + UnsupportedCapability(Capabilities), } #[derive(Clone, Debug, thiserror::Error)] @@ -99,6 +101,7 @@ struct VaryingContext<'a> { types: &'a Arena, location_mask: &'a mut BitSet, built_in_mask: u32, + capabilities: Capabilities, } impl VaryingContext<'_> { @@ -176,6 +179,21 @@ impl VaryingContext<'_> { width: crate::BOOL_WIDTH, }, ), + Bi::PrimitiveIndex => { + if !self.capabilities.contains(Capabilities::PRIMITIVE_INDEX) { + return Err(VaryingError::UnsupportedCapability( + Capabilities::PRIMITIVE_INDEX, + )); + } + ( + self.stage == St::Fragment && !self.output, + *ty_inner + == Ti::Scalar { + kind: Sk::Uint, + width, + }, + ) + } Bi::SampleIndex => ( self.stage == St::Fragment && !self.output, *ty_inner @@ -429,6 +447,7 @@ impl super::Validator { types: &module.types, location_mask: &mut self.location_mask, built_in_mask: argument_built_ins, + capabilities: self.capabilities, }; ctx.validate(fa.binding.as_ref()) .map_err(|e| EntryPointError::Argument(index as u32, e))?; @@ -444,6 +463,7 @@ impl super::Validator { types: &module.types, location_mask: &mut self.location_mask, built_in_mask: 0, + capabilities: self.capabilities, }; ctx.validate(fr.binding.as_ref()) .map_err(EntryPointError::Result)?; diff --git a/src/valid/mod.rs b/src/valid/mod.rs index 09a5c0127..d8b0be0d1 100644 --- a/src/valid/mod.rs +++ b/src/valid/mod.rs @@ -58,6 +58,8 @@ bitflags::bitflags! { const PUSH_CONSTANT = 0x1; /// Float values with width = 8. const FLOAT64 = 0x2; + /// Support for `Builtin:PrimitiveIndex`. + const PRIMITIVE_INDEX = 0x4; } } diff --git a/tests/in/extra.wgsl b/tests/in/extra.wgsl index 11b8a2eb6..9f9acb033 100644 --- a/tests/in/extra.wgsl +++ b/tests/in/extra.wgsl @@ -5,7 +5,16 @@ struct PushConstants { }; var pc: PushConstants; +struct FragmentIn { + [[location(0)]] color: vec4; + [[builtin(primitive_index)]] primitive_index: u32; +}; + [[stage(fragment)]] -fn main([[location(0)]] color: vec4) -> [[location(0)]] vec4 { - return color; +fn main(in: FragmentIn) -> [[location(0)]] vec4 { + if (in.primitive_index % 2u == 0u) { + return in.color; + } else { + return vec4(vec3(1.0) - in.color.rgb, in.color.a); + } } diff --git a/tests/out/msl/extra.msl b/tests/out/msl/extra.msl index b2a2363d9..a568e14fa 100644 --- a/tests/out/msl/extra.msl +++ b/tests/out/msl/extra.msl @@ -6,6 +6,10 @@ struct PushConstants { char _pad1[12]; metal::float2 double1; }; +struct FragmentIn { + metal::float4 color; + metal::uint primitive_index; +}; struct main1Input { metal::float4 color [[user(loc0), center_perspective]]; @@ -15,7 +19,12 @@ struct main1Output { }; fragment main1Output main1( main1Input varyings [[stage_in]] +, metal::uint primitive_index [[primitive_id]] ) { - const auto color = varyings.color; - return main1Output { color }; + const FragmentIn in = { varyings.color, primitive_index }; + if ((in.primitive_index % 2u) == 0u) { + return main1Output { in.color }; + } else { + return main1Output { metal::float4(metal::float3(1.0) - in.color.xyz, in.color.w) }; + } } diff --git a/tests/out/spv/extra.spvasm b/tests/out/spv/extra.spvasm index ec05bdbdf..1131df183 100644 --- a/tests/out/spv/extra.spvasm +++ b/tests/out/spv/extra.spvasm @@ -1,37 +1,70 @@ ; SPIR-V ; Version: 1.0 ; Generator: rspirv -; Bound: 20 +; Bound: 44 OpCapability Shader OpCapability Float64 +OpCapability Geometry %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 -OpEntryPoint Fragment %17 "main" %12 %15 -OpExecutionMode %17 OriginUpperLeft -OpDecorate %6 Block -OpMemberDecorate %6 0 Offset 0 -OpMemberDecorate %6 1 Offset 16 -OpDecorate %12 Location 0 -OpDecorate %15 Location 0 +OpEntryPoint Fragment %25 "main" %17 %20 %23 +OpExecutionMode %25 OriginUpperLeft +OpDecorate %10 Block +OpMemberDecorate %10 0 Offset 0 +OpMemberDecorate %10 1 Offset 16 +OpMemberDecorate %12 0 Offset 0 +OpMemberDecorate %12 1 Offset 16 +OpDecorate %17 Location 0 +OpDecorate %20 BuiltIn PrimitiveId +OpDecorate %23 Location 0 %2 = OpTypeVoid -%3 = OpTypeInt 32 0 -%5 = OpTypeFloat 64 -%4 = OpTypeVector %5 2 -%6 = OpTypeStruct %3 %4 -%8 = OpTypeFloat 32 -%7 = OpTypeVector %8 4 -%10 = OpTypePointer PushConstant %6 -%9 = OpVariable %10 PushConstant -%13 = OpTypePointer Input %7 -%12 = OpVariable %13 Input -%16 = OpTypePointer Output %7 -%15 = OpVariable %16 Output -%18 = OpTypeFunction %2 -%17 = OpFunction %2 None %18 -%11 = OpLabel -%14 = OpLoad %7 %12 -OpBranch %19 -%19 = OpLabel -OpStore %15 %14 +%4 = OpTypeInt 32 0 +%3 = OpConstant %4 2 +%5 = OpConstant %4 0 +%7 = OpTypeFloat 32 +%6 = OpConstant %7 1.0 +%9 = OpTypeFloat 64 +%8 = OpTypeVector %9 2 +%10 = OpTypeStruct %4 %8 +%11 = OpTypeVector %7 4 +%12 = OpTypeStruct %11 %4 +%14 = OpTypePointer PushConstant %10 +%13 = OpVariable %14 PushConstant +%18 = OpTypePointer Input %11 +%17 = OpVariable %18 Input +%21 = OpTypePointer Input %4 +%20 = OpVariable %21 Input +%24 = OpTypePointer Output %11 +%23 = OpVariable %24 Output +%26 = OpTypeFunction %2 +%30 = OpTypeBool +%36 = OpTypeVector %7 3 +%25 = OpFunction %2 None %26 +%15 = OpLabel +%19 = OpLoad %11 %17 +%22 = OpLoad %4 %20 +%16 = OpCompositeConstruct %12 %19 %22 +OpBranch %27 +%27 = OpLabel +%28 = OpCompositeExtract %4 %16 1 +%29 = OpUMod %4 %28 %3 +%31 = OpIEqual %30 %29 %5 +OpSelectionMerge %32 None +OpBranchConditional %31 %33 %34 +%33 = OpLabel +%35 = OpCompositeExtract %11 %16 0 +OpStore %23 %35 +OpReturn +%34 = OpLabel +%37 = OpCompositeConstruct %36 %6 %6 %6 +%38 = OpCompositeExtract %11 %16 0 +%39 = OpVectorShuffle %36 %38 %38 0 1 2 +%40 = OpFSub %36 %37 %39 +%41 = OpCompositeExtract %11 %16 0 +%42 = OpCompositeExtract %7 %41 3 +%43 = OpCompositeConstruct %11 %40 %42 +OpStore %23 %43 +OpReturn +%32 = OpLabel OpReturn OpFunctionEnd \ No newline at end of file diff --git a/tests/out/wgsl/extra.wgsl b/tests/out/wgsl/extra.wgsl index b68b85568..710519b8e 100644 --- a/tests/out/wgsl/extra.wgsl +++ b/tests/out/wgsl/extra.wgsl @@ -4,9 +4,18 @@ struct PushConstants { double: vec2; }; +struct FragmentIn { + [[location(0)]] color: vec4; + [[builtin(primitive_index)]] primitive_index: u32; +}; + var pc: PushConstants; [[stage(fragment)]] -fn main([[location(0)]] color: vec4) -> [[location(0)]] vec4 { - return color; +fn main(in: FragmentIn) -> [[location(0)]] vec4 { + if (((in.primitive_index % 2u) == 0u)) { + return in.color; + } else { + return vec4((vec3(1.0) - in.color.xyz), in.color.w); + } }