Skip gl_PerVertex unused builtins in the SPIR-V frontend (#2272)

Co-authored-by: Jim Blandy <jimb@red-bean.com>
This commit is contained in:
Teodor TanasoaiaandJim Blandy authored and GitHub committed 2023-03-15 18:46:25 +01:00
1 parent 7f829c6ac6
commit 63e91faecb
11 files changed
+125 -214

No files matched your search

+2 -2
View File
@@ -200,8 +200,8 @@ impl Frontend {
stride: 4,
},
builtin: match name {
"gl_ClipDistance" => BuiltIn::PointSize,
"gl_CullDistance" => BuiltIn::FragDepth,
"gl_ClipDistance" => BuiltIn::ClipDistance,
"gl_CullDistance" => BuiltIn::CullDistance,
_ => unreachable!(),
},
mutable: self.meta.stage == ShaderStage::Vertex,
+38 -10
View File
@@ -370,22 +370,50 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
let expr_handle = function
.expressions
.append(crate::Expression::GlobalVariable(lvar.handle), span);
// Cull problematic builtins of gl_PerVertex.
// See the docs for `Frontend::gl_per_vertex_builtin_access`.
{
let ty = &module.types[result.ty];
match ty.inner {
crate::TypeInner::Struct {
members: ref original_members,
span,
} if ty.name.as_deref() == Some("gl_PerVertex") => {
let mut new_members = original_members.clone();
for member in &mut new_members {
if let Some(crate::Binding::BuiltIn(built_in)) = member.binding
{
if !self.gl_per_vertex_builtin_access.contains(&built_in) {
member.binding = None
}
}
}
if &new_members != original_members {
module.types.replace(
result.ty,
crate::Type {
name: ty.name.clone(),
inner: crate::TypeInner::Struct {
members: new_members,
span,
},
},
);
}
}
_ => {}
}
}
match module.types[result.ty].inner {
crate::TypeInner::Struct {
members: ref sub_members,
..
} => {
for (index, sm) in sub_members.iter().enumerate() {
match sm.binding {
Some(crate::Binding::BuiltIn(built_in)) => {
// Cull unused builtins to preserve performances
if !self.builtin_usage.contains(&built_in) {
continue;
}
}
// unrecognized binding, skip
None => continue,
_ => {}
if sm.binding.is_none() {
continue;
}
let mut sm = sm.clone();
+18 -9
View File
@@ -560,9 +560,15 @@ pub struct Frontend<I> {
std::hash::BuildHasherDefault<rustc_hash::FxHasher>,
>,
/// Tracks usage of builtins, used to cull unused builtins since they can
/// have serious performance implications.
builtin_usage: FastHashSet<crate::BuiltIn>,
/// Tracks access to gl_PerVertex's builtins, it is used to cull unused builtins since initializing those can
/// affect performance and the mere presence of some of these builtins might cause backends to error since they
/// might be unsupported.
///
/// The problematic builtins are: PointSize, ClipDistance and CullDistance.
///
/// glslang declares those by default even though they are never written to
/// (see <https://github.com/KhronosGroup/glslang/issues/1868>)
gl_per_vertex_builtin_access: FastHashSet<crate::BuiltIn>,
}
impl<I: Iterator<Item = u32>> Frontend<I> {
@@ -596,7 +602,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
index_constants: Vec::new(),
index_constant_expressions: Vec::new(),
switch_cases: indexmap::IndexMap::default(),
builtin_usage: FastHashSet::default(),
gl_per_vertex_builtin_access: FastHashSet::default(),
}
}
@@ -1480,7 +1486,8 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
log::trace!("\t\t\tlooking up type {:?}", acex.type_id);
let type_lookup = self.lookup_type.lookup(acex.type_id)?;
acex = match ctx.type_arena[type_lookup.handle].inner {
let ty = &ctx.type_arena[type_lookup.handle];
acex = match ty.inner {
// can only index a struct with a constant
crate::TypeInner::Struct { ref members, .. } => {
let index = index_maybe
@@ -1498,10 +1505,12 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
span,
);
if let Some(crate::Binding::BuiltIn(built_in)) =
members[index as usize].binding
{
self.builtin_usage.insert(built_in);
if ty.name.as_deref() == Some("gl_PerVertex") {
if let Some(crate::Binding::BuiltIn(built_in)) =
members[index as usize].binding
{
self.gl_per_vertex_builtin_access.insert(built_in);
}
}
AccessExpression {