BarycentricNoPerspective (#8852)

This commit is contained in:
atlv authored and GitHub committed 2026-01-15 19:42:44 -05:00
1 parent bdc8f618c1
commit fc9060f510
25 files changed
+152 -38

No files matched your search

+1
View File
@@ -44,6 +44,7 @@ Bottom level categories:
### New Features
- Added support for cooperative load/store operations in shaders. Currently only WGSL on the input and SPIR-V, METAL, and WGSL on the output are supported. By @kvark in [#8251](https://github.com/gfx-rs/wgpu/issues/8251).
- Added support for no-perspective barycentric coordinates. By @atlv24 in [#8852](https://github.com/gfx-rs/wgpu/issues/8852).
- Added support for obtaining `AdapterInfo` from `Device`. By @sagudev in [#8807](https://github.com/gfx-rs/wgpu/pull/8807).
- Added `Limits::or_worse_values_from`. By @atlv24 in [#8870](https://github.com/gfx-rs/wgpu/pull/8870).
+2 -1
View File
@@ -94,7 +94,8 @@ pub(in crate::back::glsl) const fn glsl_built_in(
Bi::PointCoord => "gl_PointCoord",
Bi::FrontFacing => "gl_FrontFacing",
Bi::PrimitiveIndex => "uint(gl_PrimitiveID)",
Bi::Barycentric => "gl_BaryCoordEXT",
Bi::Barycentric { perspective: true } => "gl_BaryCoordEXT",
Bi::Barycentric { perspective: false } => "gl_BaryCoordNoPerspEXT",
Bi::SampleIndex => "gl_SampleID",
Bi::SampleMask => {
if options.output {
+1 -1
View File
@@ -613,7 +613,7 @@ impl<W> Writer<'_, W> {
crate::BuiltIn::InstanceIndex | crate::BuiltIn::DrawID => {
self.features.request(Features::INSTANCE_INDEX)
}
crate::BuiltIn::Barycentric => {
crate::BuiltIn::Barycentric { .. } => {
self.features.request(Features::SHADER_BARYCENTRICS)
}
_ => {}
+1 -1
View File
@@ -161,7 +161,7 @@ impl crate::BuiltIn {
Self::FragDepth => "SV_Depth",
Self::FrontFacing => "SV_IsFrontFace",
Self::PrimitiveIndex => "SV_PrimitiveID",
Self::Barycentric => "SV_Barycentrics",
Self::Barycentric { .. } => "SV_Barycentrics",
Self::SampleIndex => "SV_SampleIndex",
Self::SampleMask => "SV_Coverage",
// compute
+3
View File
@@ -537,6 +537,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant: true }) => {
write!(self.out, "precise ")?;
}
crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { perspective: false }) => {
write!(self.out, "noperspective ")?;
}
crate::Binding::Location {
interpolation,
sampling,
+5 -2
View File
@@ -540,7 +540,7 @@ impl Options {
}
// macOS: Since Metal 2.2
// iOS: Since Metal 2.3 (check depends on https://github.com/gfx-rs/wgpu/issues/4414)
crate::BuiltIn::Barycentric if self.lang_version < (2, 3) => {
crate::BuiltIn::Barycentric { .. } if self.lang_version < (2, 3) => {
return Err(Error::UnsupportedAttribute("barycentric_coord".to_string()));
}
_ => {}
@@ -695,7 +695,10 @@ impl ResolvedBinding {
Bi::PointCoord => "point_coord",
Bi::FrontFacing => "front_facing",
Bi::PrimitiveIndex => "primitive_id",
Bi::Barycentric => "barycentric_coord",
Bi::Barycentric { perspective: true } => "barycentric_coord",
Bi::Barycentric { perspective: false } => {
"barycentric_coord, center_no_perspective"
}
Bi::SampleIndex => "sample_id",
Bi::SampleMask => "sample_mask",
// compute
+16 -8
View File
@@ -7024,11 +7024,8 @@ template <typename A>
writeln!(self.out, "struct {stage_in_name} {{")?;
}
for &(ref name_key, ty, binding) in flattened_arguments.iter() {
let (binding, location) = match binding {
Some(ref binding @ &crate::Binding::Location { location, .. }) => {
(binding, location)
}
_ => continue,
let Some(binding) = binding else {
continue;
};
let name = match *name_key {
NameKey::StructMember(..) => &flattened_member_names[name_key],
@@ -7042,7 +7039,15 @@ template <typename A>
first_time: false,
};
let resolved = options.resolve_local_binding(binding, in_mode)?;
let location = match *binding {
crate::Binding::Location { location, .. } => Some(location),
crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. }) => None,
crate::Binding::BuiltIn(_) => continue,
};
if do_vertex_pulling {
let Some(location) = location else {
continue;
};
// Update our attribute mapping.
am_resolved.insert(
location,
@@ -7190,6 +7195,7 @@ template <typename A>
// struct.
for &(ref name_key, ty, binding) in flattened_arguments.iter() {
let binding = match binding {
Some(&crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. })) => continue,
Some(binding @ &crate::Binding::BuiltIn { .. }) => binding,
_ => continue,
};
@@ -7774,8 +7780,9 @@ template <typename A>
}
writeln!(self.out, " }};")?;
}
_ => {
if let Some(crate::Binding::Location { .. }) = arg.binding {
_ => match arg.binding {
Some(crate::Binding::Location { .. })
| Some(crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. })) => {
if has_varyings {
writeln!(
self.out,
@@ -7787,7 +7794,8 @@ template <typename A>
)?;
}
}
}
_ => {}
},
}
}
+6 -2
View File
@@ -3080,13 +3080,17 @@ impl Writer {
}
BuiltIn::PrimitiveId
}
Bi::Barycentric => {
Bi::Barycentric { perspective } => {
self.require_any(
"`barycentric` built-in",
&[spirv::Capability::FragmentBarycentricKHR],
)?;
self.use_extension("SPV_KHR_fragment_shader_barycentric");
BuiltIn::BaryCoordKHR
if perspective {
BuiltIn::BaryCoordKHR
} else {
BuiltIn::BaryCoordNoPerspKHR
}
}
Bi::SampleIndex => {
self.require_any(
+2 -1
View File
@@ -169,7 +169,8 @@ impl TryToWgsl for crate::BuiltIn {
Bi::FragDepth => "frag_depth",
Bi::FrontFacing => "front_facing",
Bi::PrimitiveIndex => "primitive_index",
Bi::Barycentric => "barycentric",
Bi::Barycentric { perspective: true } => "barycentric",
Bi::Barycentric { perspective: false } => "barycentric_no_perspective",
Bi::SampleIndex => "sample_index",
Bi::SampleMask => "sample_mask",
Bi::GlobalInvocationId => "global_invocation_id",
+2 -1
View File
@@ -201,7 +201,8 @@ impl Frontend {
"gl_BaseVertex" => BuiltIn::BaseVertex,
"gl_BaseInstance" => BuiltIn::BaseInstance,
"gl_PrimitiveID" => BuiltIn::PrimitiveIndex,
"gl_BaryCoordEXT" => BuiltIn::Barycentric,
"gl_BaryCoordEXT" => BuiltIn::Barycentric { perspective: true },
"gl_BaryCoordNoPerspEXT" => BuiltIn::Barycentric { perspective: false },
"gl_InstanceIndex" => BuiltIn::InstanceIndex,
"gl_VertexIndex" => BuiltIn::VertexIndex,
"gl_SampleID" => BuiltIn::SampleIndex,
+2 -1
View File
@@ -147,7 +147,8 @@ pub(super) fn map_builtin(word: spirv::Word, invariant: bool) -> Result<crate::B
Some(Bi::PointCoord) => crate::BuiltIn::PointCoord,
Some(Bi::FrontFacing) => crate::BuiltIn::FrontFacing,
Some(Bi::PrimitiveId) => crate::BuiltIn::PrimitiveIndex,
Some(Bi::BaryCoordKHR) => crate::BuiltIn::Barycentric,
Some(Bi::BaryCoordKHR) => crate::BuiltIn::Barycentric { perspective: true },
Some(Bi::BaryCoordNoPerspKHR) => crate::BuiltIn::Barycentric { perspective: false },
Some(Bi::SampleId) => crate::BuiltIn::SampleIndex,
Some(Bi::SampleMask) => crate::BuiltIn::SampleMask,
// compute
+6 -4
View File
@@ -2972,10 +2972,12 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
size: crate::VectorSize::Tri,
scalar: crate::Scalar::U32,
}),
crate::BuiltIn::Barycentric => Some(crate::TypeInner::Vector {
size: crate::VectorSize::Tri,
scalar: crate::Scalar::F32,
}),
crate::BuiltIn::Barycentric { perspective: false } => {
Some(crate::TypeInner::Vector {
size: crate::VectorSize::Tri,
scalar: crate::Scalar::F32,
})
}
_ => None,
};
if let (Some(inner), Some(crate::ScalarKind::Sint)) =
+2 -1
View File
@@ -50,7 +50,8 @@ pub fn map_built_in(
"front_facing" => crate::BuiltIn::FrontFacing,
"frag_depth" => crate::BuiltIn::FragDepth,
"primitive_index" => crate::BuiltIn::PrimitiveIndex,
"barycentric" => crate::BuiltIn::Barycentric,
"barycentric" => crate::BuiltIn::Barycentric { perspective: true },
"barycentric_no_perspective" => crate::BuiltIn::Barycentric { perspective: false },
"sample_index" => crate::BuiltIn::SampleIndex,
"sample_mask" => crate::BuiltIn::SampleMask,
// compute
+1 -1
View File
@@ -412,7 +412,7 @@ pub enum BuiltIn {
/// Read in fragment shaders, written in mesh shaders
PrimitiveIndex,
/// Read in fragment shaders
Barycentric,
Barycentric { perspective: bool },
/// Read in fragment shaders
SampleIndex,
/// Read or written in fragment shaders
+10 -6
View File
@@ -217,10 +217,14 @@ impl VaryingContext<'_> {
crate::Binding::BuiltIn(built_in) => {
// Ignore the `invariant` field for the sake of duplicate checks,
// but use the original in error messages.
let canonical = if let crate::BuiltIn::Position { .. } = built_in {
crate::BuiltIn::Position { invariant: false }
} else {
built_in
let canonical = match built_in {
crate::BuiltIn::Position { .. } => {
crate::BuiltIn::Position { invariant: false }
}
crate::BuiltIn::Barycentric { .. } => {
crate::BuiltIn::Barycentric { perspective: false }
}
x => x,
};
if self.built_ins.contains(&canonical) {
@@ -232,7 +236,7 @@ impl VaryingContext<'_> {
Bi::ClipDistance => Capabilities::CLIP_DISTANCE,
Bi::CullDistance => Capabilities::CULL_DISTANCE,
Bi::PrimitiveIndex => Capabilities::PRIMITIVE_INDEX,
Bi::Barycentric => Capabilities::SHADER_BARYCENTRICS,
Bi::Barycentric { .. } => Capabilities::SHADER_BARYCENTRICS,
Bi::ViewIndex => Capabilities::MULTIVIEW,
Bi::SampleIndex => Capabilities::MULTISAMPLED_SHADING,
Bi::NumSubgroups
@@ -325,7 +329,7 @@ impl VaryingContext<'_> {
&& self.mesh_output_type == MeshOutputType::PrimitiveOutput),
*ty_inner == Ti::Scalar(crate::Scalar::U32),
),
Bi::Barycentric => (
Bi::Barycentric { .. } => (
self.stage == St::Fragment && !self.output,
*ty_inner
== Ti::Vector {
+5
View File
@@ -2,3 +2,8 @@
fn fs_main(@builtin(barycentric) bary: vec3<f32>) -> @location(0) vec4<f32> {
return vec4(bary, 1.0);
}
@fragment
fn fs_main_no_perspective(@builtin(barycentric_no_perspective) bary: vec3<f32>) -> @location(0) vec4<f32> {
return vec4(bary, 1.0);
}
@@ -0,0 +1,10 @@
#version 450 core
#extension GL_EXT_fragment_shader_barycentric : require
layout(location = 0) out vec4 _fs2p_location0;
void main() {
vec3 bary_1 = gl_BaryCoordNoPerspEXT;
_fs2p_location0 = vec4(bary_1, 1.0);
return;
}
+12 -2
View File
@@ -1,9 +1,19 @@
struct FragmentInput_fs_main {
float3 bary_1 : SV_Barycentrics;
float3 bary_2 : SV_Barycentrics;
};
struct FragmentInput_fs_main_no_perspective {
noperspective float3 bary_3 : SV_Barycentrics;
};
float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
float3 bary = fragmentinput_fs_main.bary_1;
float3 bary = fragmentinput_fs_main.bary_2;
return float4(bary, 1.0);
}
float4 fs_main_no_perspective(FragmentInput_fs_main_no_perspective fragmentinput_fs_main_no_perspective) : SV_Target0
{
float3 bary_1 = fragmentinput_fs_main_no_perspective.bary_3;
return float4(bary_1, 1.0);
}
@@ -6,6 +6,10 @@
entry_point:"fs_main",
target_profile:"ps_6_1",
),
(
entry_point:"fs_main_no_perspective",
target_profile:"ps_6_1",
),
],
compute:[
],
+17 -1
View File
@@ -6,12 +6,28 @@ using metal::uint;
struct fs_mainInput {
metal::float3 bary [[barycentric_coord]];
};
struct fs_mainOutput {
metal::float4 member [[color(0)]];
};
fragment fs_mainOutput fs_main(
metal::float3 bary [[barycentric_coord]]
fs_mainInput varyings [[stage_in]]
) {
const auto bary = varyings.bary;
return fs_mainOutput { metal::float4(bary, 1.0) };
}
struct fs_main_no_perspectiveInput {
metal::float3 bary_1 [[barycentric_coord, center_no_perspective]];
};
struct fs_main_no_perspectiveOutput {
metal::float4 member_1 [[color(0)]];
};
fragment fs_main_no_perspectiveOutput fs_main_no_perspective(
fs_main_no_perspectiveInput varyings_1 [[stage_in]]
) {
const auto bary_1 = varyings_1.bary_1;
return fs_main_no_perspectiveOutput { metal::float4(bary_1, 1.0) };
}
+16 -1
View File
@@ -1,16 +1,20 @@
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 17
; Bound: 24
OpCapability Shader
OpCapability FragmentBarycentricKHR
OpExtension "SPV_KHR_fragment_shader_barycentric"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %12 "fs_main" %7 %10
OpEntryPoint Fragment %21 "fs_main_no_perspective" %18 %20
OpExecutionMode %12 OriginUpperLeft
OpExecutionMode %21 OriginUpperLeft
OpDecorate %7 BuiltIn BaryCoordKHR
OpDecorate %10 Location 0
OpDecorate %18 BuiltIn BaryCoordNoPerspKHR
OpDecorate %20 Location 0
%2 = OpTypeVoid
%4 = OpTypeFloat 32
%3 = OpTypeVector %4 3
@@ -21,6 +25,8 @@ OpDecorate %10 Location 0
%10 = OpVariable %11 Output
%13 = OpTypeFunction %2
%14 = OpConstant %4 1
%18 = OpVariable %8 Input
%20 = OpVariable %11 Output
%12 = OpFunction %2 None %13
%6 = OpLabel
%9 = OpLoad %3 %7
@@ -29,4 +35,13 @@ OpBranch %15
%16 = OpCompositeConstruct %5 %9 %14
OpStore %10 %16
OpReturn
OpFunctionEnd
%21 = OpFunction %2 None %13
%17 = OpLabel
%19 = OpLoad %3 %18
OpBranch %22
%22 = OpLabel
%23 = OpCompositeConstruct %5 %19 %14
OpStore %20 %23
OpReturn
OpFunctionEnd
@@ -2,3 +2,8 @@
fn fs_main(@builtin(barycentric) bary: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(bary, 1f);
}
@fragment
fn fs_main_no_perspective(@builtin(barycentric_no_perspective) bary_1: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(bary_1, 1f);
}
@@ -7,3 +7,8 @@ fn vs_main(@location(0) xy: vec2<f32>) -> @builtin(position) vec4<f32> {
fn fs_main(@builtin(barycentric) bary: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(bary * 1.1 - 0.05, 1.0);
}
@fragment
fn fs_main_no_perspective(@builtin(barycentric_no_perspective) bary: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(bary * 1.1 - 0.05, 1.0);
}
+17 -3
View File
@@ -3,6 +3,7 @@ use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters, TestingContext};
pub fn all_tests(vec: &mut Vec<wgpu_test::GpuTestInitializer>) {
vec.push(BARYCENTRIC);
vec.push(BARYCENTRIC_NO_PERSPECTIVE);
}
//
@@ -36,9 +37,18 @@ static BARYCENTRIC: GpuTestConfiguration = GpuTestConfiguration::new()
.test_features_limits()
.features(wgpu::Features::SHADER_BARYCENTRICS),
)
.run_async(barycentric);
.run_async(|ctx| barycentric(ctx, false));
async fn barycentric(ctx: TestingContext) {
#[gpu_test]
static BARYCENTRIC_NO_PERSPECTIVE: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(
TestParameters::default()
.test_features_limits()
.features(wgpu::Features::SHADER_BARYCENTRICS),
)
.run_async(|ctx| barycentric(ctx, true));
async fn barycentric(ctx: TestingContext, no_perspective: bool) {
let shader = ctx
.device
.create_shader_module(wgpu::include_wgsl!("barycentric.wgsl"));
@@ -87,7 +97,11 @@ async fn barycentric(ctx: TestingContext) {
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
entry_point: if no_perspective {
Some("fs_main_no_perspective")
} else {
Some("fs_main")
},
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8Unorm,
@@ -67,7 +67,7 @@ impl MaxFragmentShaderInputDeduction {
BuiltIn::SubgroupInvocationId => InterStageBuiltIn::SubgroupInvocationId,
BuiltIn::PointCoord => InterStageBuiltIn::PointCoord,
BuiltIn::Barycentric => InterStageBuiltIn::Barycentric,
BuiltIn::Barycentric { .. } => InterStageBuiltIn::Barycentric,
BuiltIn::ViewIndex => InterStageBuiltIn::ViewIndex,
BuiltIn::BaseInstance