Skip to content

Commit 67a62cf

Browse files
committed
Helper function for identifying Cx2 matrices in a uniform buffer
1 parent 28b8c9a commit 67a62cf

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

naga/src/back/hlsl/writer.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,37 +2891,13 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
28912891
//
28922892
// Note that this only works for `Load`s and we handle
28932893
// `Store`s differently in `Statement::Store`.
2894-
let cx2_columns;
28952894
if let Some(MatrixType {
28962895
columns,
28972896
rows: crate::VectorSize::Bi,
28982897
width: 4,
28992898
}) = get_inner_matrix_of_struct_array_member(module, base, func_ctx, true)
2899+
.or_else(|| get_global_uniform_matrix(module, base, func_ctx))
29002900
{
2901-
cx2_columns = Some(columns);
2902-
} else {
2903-
let base_tr = func_ctx
2904-
.resolve_type(base, &module.types)
2905-
.pointer_base_type();
2906-
let base_ty = base_tr.as_ref().map(|tr| tr.inner_with(&module.types));
2907-
match (&func_ctx.expressions[base], base_ty) {
2908-
(
2909-
&Expression::GlobalVariable(handle),
2910-
Some(&TypeInner::Matrix {
2911-
columns,
2912-
rows: crate::VectorSize::Bi,
2913-
..
2914-
}),
2915-
) if module.global_variables[handle].space
2916-
== crate::AddressSpace::Uniform =>
2917-
{
2918-
cx2_columns = Some(columns);
2919-
}
2920-
_ => cx2_columns = None,
2921-
}
2922-
}
2923-
2924-
if let Some(columns) = cx2_columns {
29252901
write!(self.out, "__get_col_of_mat{}x2(", columns as u8)?;
29262902
self.write_expr(module, base, func_ctx)?;
29272903
write!(self.out, ", ")?;
@@ -3033,38 +3009,16 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
30333009
{
30343010
// do nothing, the chain is written on `Load`/`Store`
30353011
} else {
3036-
// We write the matrix column access in a special way since
3037-
// the type of `base` is our special __matCx2 struct.
3038-
let is_cx2;
3012+
// See if we need to write the matrix column access in a
3013+
// special way since the type of `base` is our special
3014+
// __matCx2 struct.
30393015
if let Some(MatrixType {
30403016
rows: crate::VectorSize::Bi,
30413017
width: 4,
30423018
..
30433019
}) = get_inner_matrix_of_struct_array_member(module, base, func_ctx, true)
3020+
.or_else(|| get_global_uniform_matrix(module, base, func_ctx))
30443021
{
3045-
is_cx2 = true;
3046-
} else {
3047-
let base_tr = func_ctx
3048-
.resolve_type(base, &module.types)
3049-
.pointer_base_type();
3050-
let base_ty = base_tr.as_ref().map(|tr| tr.inner_with(&module.types));
3051-
match (&func_ctx.expressions[base], base_ty) {
3052-
(
3053-
&Expression::GlobalVariable(handle),
3054-
Some(&TypeInner::Matrix {
3055-
rows: crate::VectorSize::Bi,
3056-
..
3057-
}),
3058-
) if module.global_variables[handle].space
3059-
== crate::AddressSpace::Uniform =>
3060-
{
3061-
is_cx2 = true;
3062-
}
3063-
_ => is_cx2 = false,
3064-
}
3065-
}
3066-
3067-
if is_cx2 {
30683022
self.write_expr(module, base, func_ctx)?;
30693023
write!(self.out, "._{index}")?;
30703024
return Ok(());
@@ -4497,6 +4451,36 @@ pub(super) fn get_inner_matrix_of_struct_array_member(
44974451
None
44984452
}
44994453

4454+
/// Simpler version of get_inner_matrix_of_global_uniform that only looks at the
4455+
/// immediate expression, rather than traversing an access chain.
4456+
fn get_global_uniform_matrix(
4457+
module: &Module,
4458+
base: Handle<crate::Expression>,
4459+
func_ctx: &back::FunctionCtx<'_>,
4460+
) -> Option<MatrixType> {
4461+
let base_tr = func_ctx
4462+
.resolve_type(base, &module.types)
4463+
.pointer_base_type();
4464+
let base_ty = base_tr.as_ref().map(|tr| tr.inner_with(&module.types));
4465+
match (&func_ctx.expressions[base], base_ty) {
4466+
(
4467+
&crate::Expression::GlobalVariable(handle),
4468+
Some(&TypeInner::Matrix {
4469+
columns,
4470+
rows,
4471+
scalar,
4472+
}),
4473+
) if module.global_variables[handle].space == crate::AddressSpace::Uniform => {
4474+
Some(MatrixType {
4475+
columns,
4476+
rows,
4477+
width: scalar.width,
4478+
})
4479+
}
4480+
_ => None,
4481+
}
4482+
}
4483+
45004484
/// Returns the matrix data if the access chain starting at `base`:
45014485
/// - starts with an expression with resolved type of [`TypeInner::Matrix`]
45024486
/// - contains zero or more expressions with resolved type of [`TypeInner::Array`] of [`TypeInner::Matrix`]

0 commit comments

Comments
 (0)