mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-22 23:41:01 +01:00
[hlsl-out] fix matCx2 translation for uniform buffers (#1802)
* [hlsl-out] fix matCx2 translation * fix msl validation (warning: unused variable) * fix msl buffer declaration * address comments
This commit is contained in:
1 parent
5ba2d4d079
commit
7aaac25fbf
12 files changed
+1344
-291
No files matched your search
+408
-6
@@ -48,6 +48,12 @@ pub(super) struct WrappedConstructor {
|
||||
pub(super) ty: Handle<crate::Type>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub(super) struct WrappedStructMatrixAccess {
|
||||
pub(super) ty: Handle<crate::Type>,
|
||||
pub(super) index: u32,
|
||||
}
|
||||
|
||||
/// HLSL backend requires its own `ImageQuery` enum.
|
||||
///
|
||||
/// It is used inside `WrappedImageQuery` and should be unique per ImageQuery function.
|
||||
@@ -393,13 +399,39 @@ impl<'a, W: Write> super::Writer<'a, W> {
|
||||
INDENT, struct_name, RETURN_VARIABLE_NAME
|
||||
)?;
|
||||
for i in 0..members.len() as u32 {
|
||||
let member = &members[i as usize];
|
||||
|
||||
let field_name = &self.names[&NameKey::StructMember(constructor.ty, i)];
|
||||
//TODO: handle arrays?
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}{}.{} = {}{};",
|
||||
INDENT, RETURN_VARIABLE_NAME, field_name, ARGUMENT_VARIABLE_NAME, i,
|
||||
)?;
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix {
|
||||
columns,
|
||||
rows: crate::VectorSize::Bi,
|
||||
..
|
||||
} if member.binding.is_none() => {
|
||||
for j in 0..columns as u8 {
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}{}.{}_{} = {}{}[{}];",
|
||||
INDENT,
|
||||
RETURN_VARIABLE_NAME,
|
||||
field_name,
|
||||
j,
|
||||
ARGUMENT_VARIABLE_NAME,
|
||||
i,
|
||||
j
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
//TODO: handle arrays?
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}{}.{} = {}{};",
|
||||
INDENT, RETURN_VARIABLE_NAME, field_name, ARGUMENT_VARIABLE_NAME, i,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write return value
|
||||
@@ -413,6 +445,335 @@ impl<'a, W: Write> super::Writer<'a, W> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_wrapped_struct_matrix_get_function_name(
|
||||
&mut self,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
let name = &self.names[&NameKey::Type(access.ty)];
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
write!(self.out, "GetMat{}On{}", field_name, name)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a function used to get a matCx2 from within a structure.
|
||||
pub(super) fn write_wrapped_struct_matrix_get_function(
|
||||
&mut self,
|
||||
module: &crate::Module,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
use crate::back::INDENT;
|
||||
|
||||
const STRUCT_ARGUMENT_VARIABLE_NAME: &str = "obj";
|
||||
|
||||
// Write function return type and name
|
||||
let member = match module.types[access.ty].inner {
|
||||
crate::TypeInner::Struct { ref members, .. } => &members[access.index as usize],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let ret_ty = &module.types[member.ty].inner;
|
||||
self.write_value_type(module, ret_ty)?;
|
||||
write!(self.out, " ")?;
|
||||
self.write_wrapped_struct_matrix_get_function_name(access)?;
|
||||
|
||||
// Write function parameters
|
||||
write!(self.out, "(")?;
|
||||
let struct_name = &self.names[&NameKey::Type(access.ty)];
|
||||
write!(
|
||||
self.out,
|
||||
"{} {}",
|
||||
struct_name, STRUCT_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
|
||||
// Write function body
|
||||
writeln!(self.out, ") {{")?;
|
||||
|
||||
// Write return value
|
||||
write!(self.out, "{}return ", INDENT)?;
|
||||
self.write_value_type(module, ret_ty)?;
|
||||
write!(self.out, "(")?;
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { columns, .. } => {
|
||||
for i in 0..columns as u8 {
|
||||
if i != 0 {
|
||||
write!(self.out, ", ")?;
|
||||
}
|
||||
write!(
|
||||
self.out,
|
||||
"{}.{}_{}",
|
||||
STRUCT_ARGUMENT_VARIABLE_NAME, field_name, i
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
writeln!(self.out, ");")?;
|
||||
|
||||
// End of function body
|
||||
writeln!(self.out, "}}")?;
|
||||
// Write extra new line
|
||||
writeln!(self.out)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_wrapped_struct_matrix_set_function_name(
|
||||
&mut self,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
let name = &self.names[&NameKey::Type(access.ty)];
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
write!(self.out, "SetMat{}On{}", field_name, name)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a function used to set a matCx2 from within a structure.
|
||||
pub(super) fn write_wrapped_struct_matrix_set_function(
|
||||
&mut self,
|
||||
module: &crate::Module,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
use crate::back::INDENT;
|
||||
|
||||
const STRUCT_ARGUMENT_VARIABLE_NAME: &str = "obj";
|
||||
const MATRIX_ARGUMENT_VARIABLE_NAME: &str = "mat";
|
||||
|
||||
// Write function return type and name
|
||||
write!(self.out, "void ")?;
|
||||
self.write_wrapped_struct_matrix_set_function_name(access)?;
|
||||
|
||||
// Write function parameters
|
||||
write!(self.out, "(")?;
|
||||
let struct_name = &self.names[&NameKey::Type(access.ty)];
|
||||
write!(
|
||||
self.out,
|
||||
"{} {}, ",
|
||||
struct_name, STRUCT_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
let member = match module.types[access.ty].inner {
|
||||
crate::TypeInner::Struct { ref members, .. } => &members[access.index as usize],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.write_type(module, member.ty)?;
|
||||
write!(self.out, " {}", MATRIX_ARGUMENT_VARIABLE_NAME)?;
|
||||
// Write function body
|
||||
writeln!(self.out, ") {{")?;
|
||||
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { columns, .. } => {
|
||||
for i in 0..columns as u8 {
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}{}.{}_{} = {}[{}];",
|
||||
INDENT,
|
||||
STRUCT_ARGUMENT_VARIABLE_NAME,
|
||||
field_name,
|
||||
i,
|
||||
MATRIX_ARGUMENT_VARIABLE_NAME,
|
||||
i
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
// End of function body
|
||||
writeln!(self.out, "}}")?;
|
||||
// Write extra new line
|
||||
writeln!(self.out)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_wrapped_struct_matrix_set_vec_function_name(
|
||||
&mut self,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
let name = &self.names[&NameKey::Type(access.ty)];
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
write!(self.out, "SetMatVec{}On{}", field_name, name)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a function used to set a vec2 on a matCx2 from within a structure.
|
||||
pub(super) fn write_wrapped_struct_matrix_set_vec_function(
|
||||
&mut self,
|
||||
module: &crate::Module,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
use crate::back::INDENT;
|
||||
|
||||
const STRUCT_ARGUMENT_VARIABLE_NAME: &str = "obj";
|
||||
const VECTOR_ARGUMENT_VARIABLE_NAME: &str = "vec";
|
||||
const MATRIX_INDEX_ARGUMENT_VARIABLE_NAME: &str = "mat_idx";
|
||||
|
||||
// Write function return type and name
|
||||
write!(self.out, "void ")?;
|
||||
self.write_wrapped_struct_matrix_set_vec_function_name(access)?;
|
||||
|
||||
// Write function parameters
|
||||
write!(self.out, "(")?;
|
||||
let struct_name = &self.names[&NameKey::Type(access.ty)];
|
||||
write!(
|
||||
self.out,
|
||||
"{} {}, ",
|
||||
struct_name, STRUCT_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
let member = match module.types[access.ty].inner {
|
||||
crate::TypeInner::Struct { ref members, .. } => &members[access.index as usize],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let vec_ty = match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { rows, width, .. } => crate::TypeInner::Vector {
|
||||
size: rows,
|
||||
kind: crate::ScalarKind::Float,
|
||||
width,
|
||||
},
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.write_value_type(module, &vec_ty)?;
|
||||
write!(
|
||||
self.out,
|
||||
" {}, uint {}",
|
||||
VECTOR_ARGUMENT_VARIABLE_NAME, MATRIX_INDEX_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
|
||||
// Write function body
|
||||
writeln!(self.out, ") {{")?;
|
||||
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}switch({}) {{",
|
||||
INDENT, MATRIX_INDEX_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { columns, .. } => {
|
||||
for i in 0..columns as u8 {
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}case {}: {}.{}_{} = {};",
|
||||
INDENT,
|
||||
i,
|
||||
STRUCT_ARGUMENT_VARIABLE_NAME,
|
||||
field_name,
|
||||
i,
|
||||
VECTOR_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
writeln!(self.out, "{}}}", INDENT)?;
|
||||
|
||||
// End of function body
|
||||
writeln!(self.out, "}}")?;
|
||||
// Write extra new line
|
||||
writeln!(self.out)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_wrapped_struct_matrix_set_scalar_function_name(
|
||||
&mut self,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
let name = &self.names[&NameKey::Type(access.ty)];
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
write!(self.out, "SetMatScalar{}On{}", field_name, name)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a function used to set a float on a matCx2 from within a structure.
|
||||
pub(super) fn write_wrapped_struct_matrix_set_scalar_function(
|
||||
&mut self,
|
||||
module: &crate::Module,
|
||||
access: WrappedStructMatrixAccess,
|
||||
) -> BackendResult {
|
||||
use crate::back::INDENT;
|
||||
|
||||
const STRUCT_ARGUMENT_VARIABLE_NAME: &str = "obj";
|
||||
const SCALAR_ARGUMENT_VARIABLE_NAME: &str = "scalar";
|
||||
const MATRIX_INDEX_ARGUMENT_VARIABLE_NAME: &str = "mat_idx";
|
||||
const VECTOR_INDEX_ARGUMENT_VARIABLE_NAME: &str = "vec_idx";
|
||||
|
||||
// Write function return type and name
|
||||
write!(self.out, "void ")?;
|
||||
self.write_wrapped_struct_matrix_set_scalar_function_name(access)?;
|
||||
|
||||
// Write function parameters
|
||||
write!(self.out, "(")?;
|
||||
let struct_name = &self.names[&NameKey::Type(access.ty)];
|
||||
write!(
|
||||
self.out,
|
||||
"{} {}, ",
|
||||
struct_name, STRUCT_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
let member = match module.types[access.ty].inner {
|
||||
crate::TypeInner::Struct { ref members, .. } => &members[access.index as usize],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let scalar_ty = match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { width, .. } => crate::TypeInner::Scalar {
|
||||
kind: crate::ScalarKind::Float,
|
||||
width,
|
||||
},
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.write_value_type(module, &scalar_ty)?;
|
||||
write!(
|
||||
self.out,
|
||||
" {}, uint {}, uint {}",
|
||||
SCALAR_ARGUMENT_VARIABLE_NAME,
|
||||
MATRIX_INDEX_ARGUMENT_VARIABLE_NAME,
|
||||
VECTOR_INDEX_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
|
||||
// Write function body
|
||||
writeln!(self.out, ") {{")?;
|
||||
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}switch({}) {{",
|
||||
INDENT, MATRIX_INDEX_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
|
||||
let field_name = &self.names[&NameKey::StructMember(access.ty, access.index)];
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix { columns, .. } => {
|
||||
for i in 0..columns as u8 {
|
||||
writeln!(
|
||||
self.out,
|
||||
"{}case {}: {}.{}_{}[{}] = {};",
|
||||
INDENT,
|
||||
i,
|
||||
STRUCT_ARGUMENT_VARIABLE_NAME,
|
||||
field_name,
|
||||
i,
|
||||
VECTOR_INDEX_ARGUMENT_VARIABLE_NAME,
|
||||
SCALAR_ARGUMENT_VARIABLE_NAME
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
writeln!(self.out, "{}}}", INDENT)?;
|
||||
|
||||
// End of function body
|
||||
writeln!(self.out, "}}")?;
|
||||
// Write extra new line
|
||||
writeln!(self.out)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Helper function that write wrapped function for `Expression::ImageQuery` and `Expression::ArrayLength`
|
||||
///
|
||||
/// <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-getdimensions>
|
||||
@@ -478,6 +839,47 @@ impl<'a, W: Write> super::Writer<'a, W> {
|
||||
self.wrapped.constructors.insert(constructor);
|
||||
}
|
||||
}
|
||||
// We treat matrices of the form `matCx2` as a sequence of C `vec2`s
|
||||
// (see top level module docs for details).
|
||||
//
|
||||
// The functions injected here are required to get the matrix accesses working.
|
||||
crate::Expression::AccessIndex { base, index } => {
|
||||
let base_ty_res = &func_ctx.info[base].ty;
|
||||
let mut resolved = base_ty_res.inner_with(&module.types);
|
||||
let base_ty_handle = match *resolved {
|
||||
crate::TypeInner::Pointer { base, .. } => {
|
||||
resolved = &module.types[base].inner;
|
||||
Some(base)
|
||||
}
|
||||
_ => base_ty_res.handle(),
|
||||
};
|
||||
if let crate::TypeInner::Struct { ref members, .. } = *resolved {
|
||||
let member = &members[index as usize];
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
crate::TypeInner::Matrix {
|
||||
rows: crate::VectorSize::Bi,
|
||||
..
|
||||
} if member.binding.is_none() => {
|
||||
let ty = base_ty_handle.unwrap();
|
||||
let access = WrappedStructMatrixAccess { ty, index };
|
||||
|
||||
if !self.wrapped.struct_matrix_access.contains(&access) {
|
||||
self.write_wrapped_struct_matrix_get_function(module, access)?;
|
||||
self.write_wrapped_struct_matrix_set_function(module, access)?;
|
||||
self.write_wrapped_struct_matrix_set_vec_function(
|
||||
module, access,
|
||||
)?;
|
||||
self.write_wrapped_struct_matrix_set_scalar_function(
|
||||
module, access,
|
||||
)?;
|
||||
self.wrapped.struct_matrix_access.insert(access);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ Backend for [HLSL][hlsl] (High-Level Shading Language).
|
||||
- 5.1
|
||||
- 6.0
|
||||
|
||||
# General Matrix Note
|
||||
|
||||
All matrix construction/deconstruction is row based in HLSL. This means that when
|
||||
we construct a matrix from column vectors, our matrix will be implicitly transposed.
|
||||
The inverse transposition happens when we call `[0]` to get the zeroth column vector.
|
||||
@@ -19,6 +21,13 @@ To deal with this, we add `row_major` to all declarations of matrices in Uniform
|
||||
Finally because all of our matrices are transposed, if you use `mat3x4`, it'll become `float3x4` in HLSL
|
||||
(HLSL has inverted col/row notation).
|
||||
|
||||
# Matrix struct member of the form `matCx2` Note
|
||||
|
||||
Struct member matrices of the form `matCx2` are translated to a sequence of C `vec2`s due to
|
||||
differences in alignment between WGSL and HLSL for uniform buffers.
|
||||
|
||||
Accesses to these matrices are handled by injected functions.
|
||||
|
||||
[hlsl]: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl
|
||||
*/
|
||||
|
||||
@@ -169,6 +178,7 @@ struct Wrapped {
|
||||
array_lengths: crate::FastHashSet<help::WrappedArrayLength>,
|
||||
image_queries: crate::FastHashSet<help::WrappedImageQuery>,
|
||||
constructors: crate::FastHashSet<help::WrappedConstructor>,
|
||||
struct_matrix_access: crate::FastHashSet<help::WrappedStructMatrixAccess>,
|
||||
}
|
||||
|
||||
impl Wrapped {
|
||||
@@ -176,6 +186,7 @@ impl Wrapped {
|
||||
self.array_lengths.clear();
|
||||
self.image_queries.clear();
|
||||
self.constructors.clear();
|
||||
self.struct_matrix_access.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+219
-7
@@ -1,5 +1,5 @@
|
||||
use super::{
|
||||
help::{WrappedArrayLength, WrappedConstructor, WrappedImageQuery},
|
||||
help::{WrappedArrayLength, WrappedConstructor, WrappedImageQuery, WrappedStructMatrixAccess},
|
||||
storage::StoreValue,
|
||||
BackendResult, Error, Options,
|
||||
};
|
||||
@@ -784,6 +784,28 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
// Write [size]
|
||||
self.write_array_size(module, size)?;
|
||||
}
|
||||
// We treat matrices of the form `matCx2` as a sequence of C `vec2`s
|
||||
// (see top level module docs for details).
|
||||
TypeInner::Matrix {
|
||||
rows,
|
||||
columns,
|
||||
width,
|
||||
} if member.binding.is_none() && rows == crate::VectorSize::Bi => {
|
||||
let vec_ty = crate::TypeInner::Vector {
|
||||
size: rows,
|
||||
kind: crate::ScalarKind::Float,
|
||||
width,
|
||||
};
|
||||
let field_name_key = NameKey::StructMember(handle, index as u32);
|
||||
|
||||
for i in 0..columns as u8 {
|
||||
if i != 0 {
|
||||
write!(self.out, "; ")?;
|
||||
}
|
||||
self.write_value_type(module, &vec_ty)?;
|
||||
write!(self.out, " {}_{}", &self.names[&field_name_key], i)?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Write modifier before type
|
||||
if let Some(ref binding) = member.binding {
|
||||
@@ -1253,11 +1275,175 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
writeln!(self.out, "[_i] = _result[_i];")?;
|
||||
writeln!(self.out, "{}}}", level)?;
|
||||
} else {
|
||||
// We treat matrices of the form `matCx2` as a sequence of C `vec2`s
|
||||
// (see top level module docs for details).
|
||||
//
|
||||
// We handle matrix Stores here directly (including sub accesses for Vectors and Scalars).
|
||||
// Loads are handled by `Expression::AccessIndex` (since sub accesses work fine for Loads).
|
||||
struct MatrixAccess {
|
||||
base: Handle<crate::Expression>,
|
||||
index: u32,
|
||||
}
|
||||
enum Index {
|
||||
Expression(Handle<crate::Expression>),
|
||||
Static(u32),
|
||||
}
|
||||
|
||||
let get_members = |expr: Handle<crate::Expression>| {
|
||||
let base_ty_res = &func_ctx.info[expr].ty;
|
||||
let resolved = base_ty_res.inner_with(&module.types);
|
||||
match *resolved {
|
||||
TypeInner::Pointer { base, .. } => match module.types[base].inner {
|
||||
TypeInner::Struct { ref members, .. } => Some(members),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
|
||||
let mut matrix = None;
|
||||
let mut vector = None;
|
||||
let mut scalar = None;
|
||||
|
||||
let mut current_expr = pointer;
|
||||
for _ in 0..3 {
|
||||
let resolved = func_ctx.info[current_expr].ty.inner_with(&module.types);
|
||||
|
||||
match (resolved, &func_ctx.expressions[current_expr]) {
|
||||
(
|
||||
&TypeInner::Pointer { base: ty, .. },
|
||||
&crate::Expression::AccessIndex { base, index },
|
||||
) if matches!(
|
||||
module.types[ty].inner,
|
||||
TypeInner::Matrix {
|
||||
rows: crate::VectorSize::Bi,
|
||||
..
|
||||
}
|
||||
) && get_members(base)
|
||||
.map(|members| members[index as usize].binding.is_none())
|
||||
== Some(true) =>
|
||||
{
|
||||
matrix = Some(MatrixAccess { base, index });
|
||||
break;
|
||||
}
|
||||
(
|
||||
&TypeInner::ValuePointer {
|
||||
size: Some(crate::VectorSize::Bi),
|
||||
..
|
||||
},
|
||||
&crate::Expression::Access { base, index },
|
||||
) => {
|
||||
vector = Some(Index::Expression(index));
|
||||
current_expr = base;
|
||||
}
|
||||
(
|
||||
&TypeInner::ValuePointer {
|
||||
size: Some(crate::VectorSize::Bi),
|
||||
..
|
||||
},
|
||||
&crate::Expression::AccessIndex { base, index },
|
||||
) => {
|
||||
vector = Some(Index::Static(index));
|
||||
current_expr = base;
|
||||
}
|
||||
(
|
||||
&TypeInner::ValuePointer { size: None, .. },
|
||||
&crate::Expression::Access { base, index },
|
||||
) => {
|
||||
scalar = Some(Index::Expression(index));
|
||||
current_expr = base;
|
||||
}
|
||||
(
|
||||
&TypeInner::ValuePointer { size: None, .. },
|
||||
&crate::Expression::AccessIndex { base, index },
|
||||
) => {
|
||||
scalar = Some(Index::Static(index));
|
||||
current_expr = base;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
||||
write!(self.out, "{}", level)?;
|
||||
self.write_expr(module, pointer, func_ctx)?;
|
||||
write!(self.out, " = ")?;
|
||||
self.write_expr(module, value, func_ctx)?;
|
||||
writeln!(self.out, ";")?
|
||||
|
||||
if let Some(MatrixAccess { index, base }) = matrix {
|
||||
let base_ty_res = &func_ctx.info[base].ty;
|
||||
let resolved = base_ty_res.inner_with(&module.types);
|
||||
let ty = match *resolved {
|
||||
TypeInner::Pointer { base, .. } => base,
|
||||
_ => base_ty_res.handle().unwrap(),
|
||||
};
|
||||
|
||||
if let Some(Index::Static(vec_index)) = vector {
|
||||
self.write_expr(module, base, func_ctx)?;
|
||||
write!(
|
||||
self.out,
|
||||
".{}_{}",
|
||||
&self.names[&NameKey::StructMember(ty, index)],
|
||||
vec_index
|
||||
)?;
|
||||
|
||||
if let Some(scalar_index) = scalar {
|
||||
write!(self.out, "[")?;
|
||||
match scalar_index {
|
||||
Index::Static(index) => {
|
||||
write!(self.out, "{}", index)?;
|
||||
}
|
||||
Index::Expression(index) => {
|
||||
self.write_expr(module, index, func_ctx)?;
|
||||
}
|
||||
}
|
||||
write!(self.out, "]")?;
|
||||
}
|
||||
|
||||
write!(self.out, " = ")?;
|
||||
self.write_expr(module, value, func_ctx)?;
|
||||
writeln!(self.out, ";")?;
|
||||
} else {
|
||||
let access = WrappedStructMatrixAccess { ty, index };
|
||||
match (&vector, &scalar) {
|
||||
(&Some(_), &Some(_)) => {
|
||||
self.write_wrapped_struct_matrix_set_scalar_function_name(
|
||||
access,
|
||||
)?;
|
||||
}
|
||||
(&Some(_), &None) => {
|
||||
self.write_wrapped_struct_matrix_set_vec_function_name(access)?;
|
||||
}
|
||||
(&None, _) => {
|
||||
self.write_wrapped_struct_matrix_set_function_name(access)?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(self.out, "(")?;
|
||||
self.write_expr(module, base, func_ctx)?;
|
||||
write!(self.out, ", ")?;
|
||||
self.write_expr(module, value, func_ctx)?;
|
||||
|
||||
if let Some(Index::Expression(vec_index)) = vector {
|
||||
write!(self.out, ", ")?;
|
||||
self.write_expr(module, vec_index, func_ctx)?;
|
||||
|
||||
if let Some(scalar_index) = scalar {
|
||||
write!(self.out, ", ")?;
|
||||
match scalar_index {
|
||||
Index::Static(index) => {
|
||||
write!(self.out, "{}", index)?;
|
||||
}
|
||||
Index::Expression(index) => {
|
||||
self.write_expr(module, index, func_ctx)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln!(self.out, ");")?;
|
||||
}
|
||||
} else {
|
||||
self.write_expr(module, pointer, func_ctx)?;
|
||||
write!(self.out, " = ")?;
|
||||
self.write_expr(module, value, func_ctx)?;
|
||||
writeln!(self.out, ";")?
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::Loop {
|
||||
@@ -1592,8 +1778,6 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
{
|
||||
// do nothing, the chain is written on `Load`/`Store`
|
||||
} else {
|
||||
self.write_expr(module, base, func_ctx)?;
|
||||
|
||||
let base_ty_res = &func_ctx.info[base].ty;
|
||||
let mut resolved = base_ty_res.inner_with(&module.types);
|
||||
let base_ty_handle = match *resolved {
|
||||
@@ -1604,6 +1788,34 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
_ => base_ty_res.handle(),
|
||||
};
|
||||
|
||||
// We treat matrices of the form `matCx2` as a sequence of C `vec2`s
|
||||
// (see top level module docs for details).
|
||||
//
|
||||
// We handle matrix reconstruction here for Loads.
|
||||
// Stores are handled directly by `Statement::Store`.
|
||||
if let TypeInner::Struct { ref members, .. } = *resolved {
|
||||
let member = &members[index as usize];
|
||||
|
||||
match module.types[member.ty].inner {
|
||||
TypeInner::Matrix {
|
||||
rows: crate::VectorSize::Bi,
|
||||
..
|
||||
} if member.binding.is_none() => {
|
||||
let ty = base_ty_handle.unwrap();
|
||||
self.write_wrapped_struct_matrix_get_function_name(
|
||||
WrappedStructMatrixAccess { ty, index },
|
||||
)?;
|
||||
write!(self.out, "(")?;
|
||||
self.write_expr(module, base, func_ctx)?;
|
||||
write!(self.out, ")")?;
|
||||
return Ok(());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
|
||||
self.write_expr(module, base, func_ctx)?;
|
||||
|
||||
match *resolved {
|
||||
TypeInner::Vector { .. } => {
|
||||
// Write vector access as a swizzle
|
||||
|
||||
Reference in new issue
Block a user