mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 07:51:08 +01:00
Support builtin(primitive_index) (#1097)
* Hack in support for PrimitiveID on Vulkan * Rename to PrimitiveIndex and add preliminary support for GLSL, HLSL and MSL * Implement as an extra WGSL built-in * Update extra.wgsl outputs * Run rustfmt; fix WGSL writer * Add rustfmt changes I forgot * Update extra.wgsl test output for WGSL fix * Bump macOS version to 10.15 in validate-msl to support primitive_index
This commit is contained in:
1 parent
8eef784416
commit
602368d9f0
17 files changed
+139
-34
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,7 @@ pub(super) fn map_builtin(word: spirv::Word) -> Result<crate::BuiltIn, Error> {
|
||||
// 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
|
||||
|
||||
@@ -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<I: Iterator<Item = u32>> Parser<I> {
|
||||
| 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,
|
||||
|
||||
@@ -20,6 +20,7 @@ pub fn map_built_in(word: &str, span: Span) -> Result<crate::BuiltIn, Error<'_>>
|
||||
// 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
|
||||
|
||||
@@ -269,6 +269,7 @@ pub enum BuiltIn {
|
||||
// fragment
|
||||
FragDepth,
|
||||
FrontFacing,
|
||||
PrimitiveIndex,
|
||||
SampleIndex,
|
||||
SampleMask,
|
||||
// compute
|
||||
|
||||
@@ -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<crate::Type>,
|
||||
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)?;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -5,7 +5,16 @@ struct PushConstants {
|
||||
};
|
||||
var<push_constant> pc: PushConstants;
|
||||
|
||||
struct FragmentIn {
|
||||
[[location(0)]] color: vec4<f32>;
|
||||
[[builtin(primitive_index)]] primitive_index: u32;
|
||||
};
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main([[location(0)]] color: vec4<f32>) -> [[location(0)]] vec4<f32> {
|
||||
return color;
|
||||
fn main(in: FragmentIn) -> [[location(0)]] vec4<f32> {
|
||||
if (in.primitive_index % 2u == 0u) {
|
||||
return in.color;
|
||||
} else {
|
||||
return vec4<f32>(vec3<f32>(1.0) - in.color.rgb, in.color.a);
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -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) };
|
||||
}
|
||||
}
|
||||
+60
-27
@@ -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
|
||||
@@ -4,9 +4,18 @@ struct PushConstants {
|
||||
double: vec2<f32>;
|
||||
};
|
||||
|
||||
struct FragmentIn {
|
||||
[[location(0)]] color: vec4<f32>;
|
||||
[[builtin(primitive_index)]] primitive_index: u32;
|
||||
};
|
||||
|
||||
var<push_constant> pc: PushConstants;
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main([[location(0)]] color: vec4<f32>) -> [[location(0)]] vec4<f32> {
|
||||
return color;
|
||||
fn main(in: FragmentIn) -> [[location(0)]] vec4<f32> {
|
||||
if (((in.primitive_index % 2u) == 0u)) {
|
||||
return in.color;
|
||||
} else {
|
||||
return vec4<f32>((vec3<f32>(1.0) - in.color.xyz), in.color.w);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user