mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 16:01:04 +01:00
Move invariant bit from Binding::Builtin to Builtin::Position. (#1822)
Use the type system to enforce the rule that the invariant attribute may only appear on `Position` builtins.
This commit is contained in:
1 parent
78975f25da
commit
170faab6db
26 files changed
+107
-168
No files matched your search
@@ -404,7 +404,7 @@ impl<'a, W> Writer<'a, W> {
|
||||
_ => {
|
||||
if let Some(binding) = binding {
|
||||
match *binding {
|
||||
Binding::BuiltIn { built_in, .. } => match built_in {
|
||||
Binding::BuiltIn(built_in) => match built_in {
|
||||
crate::BuiltIn::ClipDistance => {
|
||||
self.features.request(Features::CLIP_DISTANCE)
|
||||
}
|
||||
|
||||
+6
-11
@@ -336,7 +336,7 @@ impl fmt::Display for VaryingName<'_> {
|
||||
};
|
||||
write!(f, "_{}_location{}", prefix, location,)
|
||||
}
|
||||
crate::Binding::BuiltIn { built_in, .. } => {
|
||||
crate::Binding::BuiltIn(built_in) => {
|
||||
write!(f, "{}", glsl_built_in(built_in, self.output))
|
||||
}
|
||||
}
|
||||
@@ -1170,11 +1170,8 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
interpolation,
|
||||
sampling,
|
||||
} => (location, interpolation, sampling),
|
||||
crate::Binding::BuiltIn {
|
||||
built_in,
|
||||
invariant,
|
||||
} => {
|
||||
if invariant {
|
||||
crate::Binding::BuiltIn(built_in) => {
|
||||
if let crate::BuiltIn::Position { invariant: true } = built_in {
|
||||
writeln!(self.out, "invariant {};", glsl_built_in(built_in, output))?;
|
||||
}
|
||||
return Ok(());
|
||||
@@ -1830,10 +1827,8 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
|
||||
for (index, member) in members.iter().enumerate() {
|
||||
// TODO: handle builtin in better way
|
||||
if let Some(crate::Binding::BuiltIn {
|
||||
built_in: builtin,
|
||||
..
|
||||
}) = member.binding
|
||||
if let Some(crate::Binding::BuiltIn(builtin)) =
|
||||
member.binding
|
||||
{
|
||||
match builtin {
|
||||
crate::BuiltIn::ClipDistance
|
||||
@@ -3170,7 +3165,7 @@ fn glsl_built_in(built_in: crate::BuiltIn, output: bool) -> &'static str {
|
||||
use crate::BuiltIn as Bi;
|
||||
|
||||
match built_in {
|
||||
Bi::Position => {
|
||||
Bi::Position { .. } => {
|
||||
if output {
|
||||
"gl_Position"
|
||||
} else {
|
||||
|
||||
@@ -151,7 +151,7 @@ impl crate::StorageFormat {
|
||||
impl crate::BuiltIn {
|
||||
pub(super) fn to_hlsl_str(self) -> Result<&'static str, Error> {
|
||||
Ok(match self {
|
||||
Self::Position => "SV_Position",
|
||||
Self::Position { .. } => "SV_Position",
|
||||
// vertex
|
||||
Self::ClipDistance => "SV_ClipDistance",
|
||||
Self::CullDistance => "SV_CullDistance",
|
||||
|
||||
+6
-10
@@ -60,7 +60,7 @@ impl InterfaceKey {
|
||||
fn new(binding: Option<&crate::Binding>) -> Self {
|
||||
match binding {
|
||||
Some(&crate::Binding::Location { location, .. }) => Self::Location(location),
|
||||
Some(&crate::Binding::BuiltIn { built_in, .. }) => Self::BuiltIn(built_in),
|
||||
Some(&crate::Binding::BuiltIn(built_in)) => Self::BuiltIn(built_in),
|
||||
None => Self::Other,
|
||||
}
|
||||
}
|
||||
@@ -309,9 +309,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
|
||||
fn write_modifier(&mut self, binding: &crate::Binding) -> BackendResult {
|
||||
match *binding {
|
||||
crate::Binding::BuiltIn {
|
||||
invariant: true, ..
|
||||
} => {
|
||||
crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant: true }) => {
|
||||
write!(self.out, "precise ")?;
|
||||
}
|
||||
crate::Binding::Location {
|
||||
@@ -345,9 +343,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
stage: Option<(ShaderStage, Io)>,
|
||||
) -> BackendResult {
|
||||
match *binding {
|
||||
crate::Binding::BuiltIn {
|
||||
built_in: builtin, ..
|
||||
} => {
|
||||
crate::Binding::BuiltIn(builtin) => {
|
||||
let builtin_str = builtin.to_hlsl_str()?;
|
||||
write!(self.out, " : {}", builtin_str)?;
|
||||
}
|
||||
@@ -963,9 +959,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
if let Some(crate::FunctionResult {
|
||||
binding:
|
||||
Some(
|
||||
ref binding @ crate::Binding::BuiltIn {
|
||||
invariant: true, ..
|
||||
},
|
||||
ref binding @ crate::Binding::BuiltIn(crate::BuiltIn::Position {
|
||||
invariant: true,
|
||||
}),
|
||||
),
|
||||
..
|
||||
}) = func.result
|
||||
|
||||
+2
-2
@@ -96,14 +96,14 @@ impl<'a> FunctionCtx<'_> {
|
||||
match self.expressions[expression] {
|
||||
crate::Expression::FunctionArgument(arg_index) => {
|
||||
return match ep_function.arguments[arg_index as usize].binding {
|
||||
Some(crate::Binding::BuiltIn { built_in: bi, .. }) => Some(bi),
|
||||
Some(crate::Binding::BuiltIn(bi)) => Some(bi),
|
||||
_ => built_in,
|
||||
};
|
||||
}
|
||||
crate::Expression::AccessIndex { base, index } => {
|
||||
match *self.info[base].ty.inner_with(&module.types) {
|
||||
crate::TypeInner::Struct { ref members, .. } => {
|
||||
if let Some(crate::Binding::BuiltIn { built_in: bi, .. }) =
|
||||
if let Some(crate::Binding::BuiltIn(bi)) =
|
||||
members[index as usize].binding
|
||||
{
|
||||
built_in = Some(bi);
|
||||
|
||||
+16
-25
@@ -100,10 +100,7 @@ impl ops::Index<crate::ShaderStage> for PerStageMap {
|
||||
}
|
||||
|
||||
enum ResolvedBinding {
|
||||
BuiltIn {
|
||||
built_in: crate::BuiltIn,
|
||||
invariant: bool,
|
||||
},
|
||||
BuiltIn(crate::BuiltIn),
|
||||
Attribute(u32),
|
||||
Color(u32),
|
||||
User {
|
||||
@@ -241,20 +238,20 @@ impl Options {
|
||||
mode: LocationMode,
|
||||
) -> Result<ResolvedBinding, Error> {
|
||||
match *binding {
|
||||
crate::Binding::BuiltIn {
|
||||
built_in,
|
||||
invariant,
|
||||
} => {
|
||||
if invariant && self.lang_version < (2, 1) {
|
||||
return Err(Error::UnsupportedAttribute("invariant".to_string()));
|
||||
crate::Binding::BuiltIn(mut built_in) => {
|
||||
if let crate::BuiltIn::Position { ref mut invariant } = built_in {
|
||||
if *invariant && self.lang_version < (2, 1) {
|
||||
return Err(Error::UnsupportedAttribute("invariant".to_string()));
|
||||
}
|
||||
|
||||
// The 'invariant' attribute may only appear on vertex
|
||||
// shader outputs, not fragment shader inputs.
|
||||
if !matches!(mode, LocationMode::VertexOutput) {
|
||||
*invariant = false;
|
||||
}
|
||||
}
|
||||
|
||||
// The 'invariant' attribute can only appear on vertex
|
||||
// shader outputs, but not fragment shader inputs.
|
||||
Ok(ResolvedBinding::BuiltIn {
|
||||
built_in,
|
||||
invariant: invariant && matches!(mode, LocationMode::VertexOutput),
|
||||
})
|
||||
Ok(ResolvedBinding::BuiltIn(built_in))
|
||||
}
|
||||
crate::Binding::Location {
|
||||
location,
|
||||
@@ -369,13 +366,11 @@ impl ResolvedBinding {
|
||||
fn try_fmt<W: Write>(&self, out: &mut W) -> Result<(), Error> {
|
||||
write!(out, " [[")?;
|
||||
match *self {
|
||||
Self::BuiltIn {
|
||||
built_in,
|
||||
invariant,
|
||||
} => {
|
||||
Self::BuiltIn(built_in) => {
|
||||
use crate::BuiltIn as Bi;
|
||||
let name = match built_in {
|
||||
Bi::Position => "position",
|
||||
Bi::Position { invariant: false } => "position",
|
||||
Bi::Position { invariant: true } => "position, invariant",
|
||||
// vertex
|
||||
Bi::BaseInstance => "base_instance",
|
||||
Bi::BaseVertex => "base_vertex",
|
||||
@@ -401,10 +396,6 @@ impl ResolvedBinding {
|
||||
}
|
||||
};
|
||||
write!(out, "{}", name)?;
|
||||
|
||||
if invariant {
|
||||
write!(out, ", invariant")?;
|
||||
}
|
||||
}
|
||||
Self::Attribute(index) => write!(out, "attribute({})", index)?,
|
||||
Self::Color(index) => write!(out, "color({})", index)?,
|
||||
|
||||
+4
-16
@@ -2136,19 +2136,13 @@ impl<W: Write> Writer<W> {
|
||||
|
||||
for (index, member) in members.iter().enumerate() {
|
||||
match member.binding {
|
||||
Some(crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::PointSize,
|
||||
..
|
||||
}) => {
|
||||
Some(crate::Binding::BuiltIn(crate::BuiltIn::PointSize)) => {
|
||||
has_point_size = true;
|
||||
if !context.pipeline_options.allow_point_size {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Some(crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::CullDistance,
|
||||
..
|
||||
}) => {
|
||||
Some(crate::Binding::BuiltIn(crate::BuiltIn::CullDistance)) => {
|
||||
log::warn!("Ignoring CullDistance built-in");
|
||||
continue;
|
||||
}
|
||||
@@ -3437,10 +3431,7 @@ impl<W: Write> Writer<W> {
|
||||
match *binding {
|
||||
// Point size is only supported in VS of pipelines with
|
||||
// point primitive topology.
|
||||
crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::PointSize,
|
||||
..
|
||||
} => {
|
||||
crate::Binding::BuiltIn(crate::BuiltIn::PointSize) => {
|
||||
has_point_size = true;
|
||||
if !pipeline_options.allow_point_size {
|
||||
continue;
|
||||
@@ -3450,10 +3441,7 @@ impl<W: Write> Writer<W> {
|
||||
// But we can't return UnsupportedBuiltIn error to user.
|
||||
// Because otherwise we can't generate msl shader from any glslang SPIR-V shaders.
|
||||
// glslang generates gl_PerVertex struct with gl_CullDistance builtin inside by default.
|
||||
crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::CullDistance,
|
||||
..
|
||||
} => {
|
||||
crate::Binding::BuiltIn(crate::BuiltIn::CullDistance) => {
|
||||
log::warn!("Ignoring CullDistance BuiltIn");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ impl Writer {
|
||||
body.push(Instruction::store(res_member.id, member_value_id, None));
|
||||
|
||||
match res_member.built_in {
|
||||
Some(crate::BuiltIn::Position)
|
||||
Some(crate::BuiltIn::Position { .. })
|
||||
if self.flags.contains(WriterFlags::ADJUST_COORDINATE_SPACE) =>
|
||||
{
|
||||
self.write_epilogue_position_y_flip(res_member.id, body)?;
|
||||
|
||||
@@ -38,7 +38,7 @@ pub(super) fn contains_builtin(
|
||||
arena: &UniqueArena<crate::Type>,
|
||||
built_in: crate::BuiltIn,
|
||||
) -> bool {
|
||||
if let Some(&crate::Binding::BuiltIn { built_in: bi, .. }) = binding {
|
||||
if let Some(&crate::Binding::BuiltIn(bi)) = binding {
|
||||
bi == built_in
|
||||
} else if let crate::TypeInner::Struct { ref members, .. } = arena[ty].inner {
|
||||
members
|
||||
|
||||
+11
-26
@@ -398,11 +398,8 @@ impl Writer {
|
||||
let mut has_point_size = false;
|
||||
let class = spirv::StorageClass::Output;
|
||||
if let Some(ref binding) = result.binding {
|
||||
has_point_size |= *binding
|
||||
== crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::PointSize,
|
||||
invariant: false,
|
||||
};
|
||||
has_point_size |=
|
||||
*binding == crate::Binding::BuiltIn(crate::BuiltIn::PointSize);
|
||||
let type_id = self.get_type_id(LookupType::Handle(result.ty));
|
||||
let varying_id =
|
||||
self.write_varying(ir_module, class, None, result.ty, binding)?;
|
||||
@@ -419,11 +416,8 @@ impl Writer {
|
||||
let type_id = self.get_type_id(LookupType::Handle(member.ty));
|
||||
let name = member.name.as_ref().map(AsRef::as_ref);
|
||||
let binding = member.binding.as_ref().unwrap();
|
||||
has_point_size |= *binding
|
||||
== crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::PointSize,
|
||||
invariant: false,
|
||||
};
|
||||
has_point_size |=
|
||||
*binding == crate::Binding::BuiltIn(crate::BuiltIn::PointSize);
|
||||
let varying_id =
|
||||
self.write_varying(ir_module, class, name, member.ty, binding)?;
|
||||
iface.varying_ids.push(varying_id);
|
||||
@@ -1180,13 +1174,14 @@ impl Writer {
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::Binding::BuiltIn {
|
||||
built_in,
|
||||
invariant,
|
||||
} => {
|
||||
crate::Binding::BuiltIn(built_in) => {
|
||||
use crate::BuiltIn as Bi;
|
||||
let built_in = match built_in {
|
||||
Bi::Position => {
|
||||
Bi::Position { invariant } => {
|
||||
if invariant {
|
||||
self.decorate(id, Decoration::Invariant, &[]);
|
||||
}
|
||||
|
||||
if class == spirv::StorageClass::Output {
|
||||
BuiltIn::Position
|
||||
} else {
|
||||
@@ -1234,10 +1229,6 @@ impl Writer {
|
||||
};
|
||||
|
||||
self.decorate(id, Decoration::BuiltIn, &[built_in as u32]);
|
||||
|
||||
if invariant {
|
||||
self.decorate(id, Decoration::Invariant, &[]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1365,13 +1356,7 @@ impl Writer {
|
||||
crate::TypeInner::Struct { ref members, .. } => members.iter().any(|member| {
|
||||
has_view_index_check(ir_module, member.binding.as_ref(), member.ty)
|
||||
}),
|
||||
_ => {
|
||||
binding
|
||||
== Some(&crate::Binding::BuiltIn {
|
||||
built_in: crate::BuiltIn::ViewIndex,
|
||||
invariant: false,
|
||||
})
|
||||
}
|
||||
_ => binding == Some(&crate::Binding::BuiltIn(crate::BuiltIn::ViewIndex)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-10
@@ -402,7 +402,7 @@ impl<W: Write> Writer<W> {
|
||||
writeln!(self.out)?;
|
||||
for (index, member) in members.iter().enumerate() {
|
||||
// Skip struct member with unsupported built in
|
||||
if let Some(crate::Binding::BuiltIn { built_in, .. }) = member.binding {
|
||||
if let Some(crate::Binding::BuiltIn(built_in)) = member.binding {
|
||||
if builtin_str(built_in).is_none() {
|
||||
log::warn!("Skip member with unsupported builtin {:?}", built_in);
|
||||
continue;
|
||||
@@ -1747,7 +1747,7 @@ impl<W: Write> Writer<W> {
|
||||
|
||||
// Write the comma separated constants
|
||||
for (index, constant) in components.iter().enumerate() {
|
||||
if let Some(&crate::Binding::BuiltIn { built_in, .. }) =
|
||||
if let Some(&crate::Binding::BuiltIn(built_in)) =
|
||||
members.and_then(|members| members.get(index)?.binding.as_ref())
|
||||
{
|
||||
if builtin_str(built_in).is_none() {
|
||||
@@ -1849,7 +1849,7 @@ fn builtin_str(built_in: crate::BuiltIn) -> Option<&'static str> {
|
||||
match built_in {
|
||||
Bi::VertexIndex => Some("vertex_index"),
|
||||
Bi::InstanceIndex => Some("instance_index"),
|
||||
Bi::Position => Some("position"),
|
||||
Bi::Position { .. } => Some("position"),
|
||||
Bi::FrontFacing => Some("front_facing"),
|
||||
Bi::FragDepth => Some("frag_depth"),
|
||||
Bi::LocalInvocationId => Some("local_invocation_id"),
|
||||
@@ -1977,11 +1977,8 @@ fn map_binding_to_attribute(
|
||||
scalar_kind: Option<crate::ScalarKind>,
|
||||
) -> Vec<Attribute> {
|
||||
match *binding {
|
||||
crate::Binding::BuiltIn {
|
||||
built_in,
|
||||
invariant,
|
||||
} => {
|
||||
if invariant {
|
||||
crate::Binding::BuiltIn(built_in) => {
|
||||
if let crate::BuiltIn::Position { invariant: true } = built_in {
|
||||
vec![Attribute::BuiltIn(built_in), Attribute::Invariant]
|
||||
} else {
|
||||
vec![Attribute::BuiltIn(built_in)]
|
||||
@@ -2017,8 +2014,7 @@ fn access_to_unsupported_builtin(
|
||||
{
|
||||
// Let's check that we try to access a struct member with unsupported built-in and skip it.
|
||||
if let TypeInner::Struct { ref members, .. } = module.types[pointer_base_handle].inner {
|
||||
if let Some(crate::Binding::BuiltIn { built_in, .. }) = members[index as usize].binding
|
||||
{
|
||||
if let Some(crate::Binding::BuiltIn(built_in)) = members[index as usize].binding {
|
||||
if builtin_str(built_in).is_none() {
|
||||
log::warn!("Skip component with unsupported builtin {:?}", built_in);
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user