Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] make use of resolve_type wherever possible #2565

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,7 @@ impl<'a, W: Write> Writer<'a, W> {
// This is where we can generate intermediate constants for some expression types.
Statement::Emit(ref range) => {
for handle in range.clone() {
let info = &ctx.info[handle];
let ptr_class = info.ty.inner_with(&self.module.types).pointer_space();
let ptr_class = ctx.resolve_type(handle, &self.module.types).pointer_space();
let expr_name = if ptr_class.is_some() {
// GLSL can't save a pointer-valued expression in a variable,
// but we shouldn't ever need to: they should never be named expressions,
Expand Down Expand Up @@ -1859,7 +1858,7 @@ impl<'a, W: Write> Writer<'a, W> {
if let TypeInner::Image {
class: crate::ImageClass::Sampled { .. },
..
} = *ctx.info[image].ty.inner_with(&self.module.types)
} = *ctx.resolve_type(image, &self.module.types)
{
if let proc::BoundsCheckPolicy::Restrict = self.policies.image_load {
write!(self.out, "{level}")?;
Expand Down Expand Up @@ -2225,7 +2224,7 @@ impl<'a, W: Write> Writer<'a, W> {
} => {
write!(self.out, "{level}")?;
let res_name = format!("{}{}", back::BAKE_PREFIX, result.index());
let res_ty = ctx.info[result].ty.inner_with(&self.module.types);
let res_ty = ctx.resolve_type(result, &self.module.types);
self.write_value_type(res_ty)?;
write!(self.out, " {res_name} = ")?;
self.named_expressions.insert(result, res_name);
Expand Down Expand Up @@ -2484,7 +2483,7 @@ impl<'a, W: Write> Writer<'a, W> {
level,
depth_ref,
} => {
let dim = match *ctx.info[image].ty.inner_with(&self.module.types) {
let dim = match *ctx.resolve_type(image, &self.module.types) {
TypeInner::Image { dim, .. } => dim,
_ => unreachable!(),
};
Expand Down Expand Up @@ -2545,7 +2544,7 @@ impl<'a, W: Write> Writer<'a, W> {

// We need to get the coordinates vector size to later build a vector that's `size + 1`
// if `depth_ref` is some, if it isn't a vector we panic as that's not a valid expression
let mut coord_dim = match *ctx.info[coordinate].ty.inner_with(&self.module.types) {
let mut coord_dim = match *ctx.resolve_type(coordinate, &self.module.types) {
TypeInner::Vector { size, .. } => size as u8,
TypeInner::Scalar { .. } => 1,
_ => unreachable!(),
Expand Down Expand Up @@ -2672,7 +2671,7 @@ impl<'a, W: Write> Writer<'a, W> {
use crate::ImageClass;

// This will only panic if the module is invalid
let (dim, class) = match *ctx.info[image].ty.inner_with(&self.module.types) {
let (dim, class) = match *ctx.resolve_type(image, &self.module.types) {
TypeInner::Image {
dim,
arrayed: _,
Expand Down Expand Up @@ -2704,7 +2703,7 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_expr(image, ctx)?;
if let Some(expr) = level {
let cast_to_int = matches!(
*ctx.info[expr].ty.inner_with(&self.module.types),
*ctx.resolve_type(expr, &self.module.types),
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
..
Expand Down Expand Up @@ -2779,7 +2778,7 @@ impl<'a, W: Write> Writer<'a, W> {
let operator_or_fn = match op {
crate::UnaryOperator::Negate => "-",
crate::UnaryOperator::LogicalNot => {
match *ctx.info[expr].ty.inner_with(&self.module.types) {
match *ctx.resolve_type(expr, &self.module.types) {
TypeInner::Vector { .. } => "not",
_ => "!",
}
Expand All @@ -2805,8 +2804,8 @@ impl<'a, W: Write> Writer<'a, W> {
// implemented as a function call
use crate::{BinaryOperator as Bo, ScalarKind as Sk, TypeInner as Ti};

let left_inner = ctx.info[left].ty.inner_with(&self.module.types);
let right_inner = ctx.info[right].ty.inner_with(&self.module.types);
let left_inner = ctx.resolve_type(left, &self.module.types);
let right_inner = ctx.resolve_type(right, &self.module.types);

let function = match (left_inner, right_inner) {
(&Ti::Vector { kind, .. }, &Ti::Vector { .. }) => match op {
Expand Down Expand Up @@ -2935,7 +2934,7 @@ impl<'a, W: Write> Writer<'a, W> {
accept,
reject,
} => {
let cond_ty = ctx.info[condition].ty.inner_with(&self.module.types);
let cond_ty = ctx.resolve_type(condition, &self.module.types);
let vec_select = if let TypeInner::Vector { .. } = *cond_ty {
true
} else {
Expand Down Expand Up @@ -3025,7 +3024,7 @@ impl<'a, W: Write> Writer<'a, W> {

self.write_expr(arg, ctx)?;

match *ctx.info[arg].ty.inner_with(&self.module.types) {
match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Vector { size, .. } => write!(
self.out,
", vec{}(0.0), vec{0}(1.0)",
Expand Down Expand Up @@ -3072,7 +3071,7 @@ impl<'a, W: Write> Writer<'a, W> {
Mf::Log2 => "log2",
Mf::Pow => "pow",
// geometry
Mf::Dot => match *ctx.info[arg].ty.inner_with(&self.module.types) {
Mf::Dot => match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Vector {
kind: crate::ScalarKind::Float,
..
Expand Down Expand Up @@ -3128,7 +3127,7 @@ impl<'a, W: Write> Writer<'a, W> {
Mf::Determinant => "determinant",
// bits
Mf::CountTrailingZeros => {
match *ctx.info[arg].ty.inner_with(&self.module.types) {
match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Vector { size, kind, .. } => {
let s = back::vector_size_str(size);
if let crate::ScalarKind::Uint = kind {
Expand Down Expand Up @@ -3158,7 +3157,7 @@ impl<'a, W: Write> Writer<'a, W> {
}
Mf::CountLeadingZeros => {
if self.options.version.supports_integer_functions() {
match *ctx.info[arg].ty.inner_with(&self.module.types) {
match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Vector { size, kind, .. } => {
let s = back::vector_size_str(size);

Expand Down Expand Up @@ -3189,7 +3188,7 @@ impl<'a, W: Write> Writer<'a, W> {
_ => unreachable!(),
};
} else {
match *ctx.info[arg].ty.inner_with(&self.module.types) {
match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Vector { size, kind, .. } => {
let s = back::vector_size_str(size);

Expand Down Expand Up @@ -3262,7 +3261,7 @@ impl<'a, W: Write> Writer<'a, W> {

// Check if the argument is an unsigned integer and return the vector size
// in case it's a vector
let maybe_uint_size = match *ctx.info[arg].ty.inner_with(&self.module.types) {
let maybe_uint_size = match *ctx.resolve_type(arg, &self.module.types) {
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
..
Expand Down Expand Up @@ -3349,7 +3348,7 @@ impl<'a, W: Write> Writer<'a, W> {
kind: target_kind,
convert,
} => {
let inner = ctx.info[expr].ty.inner_with(&self.module.types);
let inner = ctx.resolve_type(expr, &self.module.types);
match convert {
Some(width) => {
// this is similar to `write_type`, but with the target kind
Expand Down Expand Up @@ -3515,7 +3514,7 @@ impl<'a, W: Write> Writer<'a, W> {
}
// Otherwise write just the expression (and the 1D hack if needed)
None => {
let uvec_size = match *ctx.info[coordinate].ty.inner_with(&self.module.types) {
let uvec_size = match *ctx.resolve_type(coordinate, &self.module.types) {
TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
..
Expand Down Expand Up @@ -3563,7 +3562,7 @@ impl<'a, W: Write> Writer<'a, W> {
// so we don't need to generate bounds checks (OpenGL 4.2 Core §3.9.20)

// This will only panic if the module is invalid
let dim = match *ctx.info[image].ty.inner_with(&self.module.types) {
let dim = match *ctx.resolve_type(image, &self.module.types) {
TypeInner::Image { dim, .. } => dim,
_ => unreachable!(),
};
Expand Down Expand Up @@ -3626,7 +3625,7 @@ impl<'a, W: Write> Writer<'a, W> {
// in bounds (`ReadZeroSkipWrite`) or make them a valid texel (`Restrict`).

// This will only panic if the module is invalid
let (dim, class) = match *ctx.info[image].ty.inner_with(&self.module.types) {
let (dim, class) = match *ctx.resolve_type(image, &self.module.types) {
TypeInner::Image {
dim,
arrayed: _,
Expand Down Expand Up @@ -3891,8 +3890,7 @@ impl<'a, W: Write> Writer<'a, W> {
}
}

let base_ty_res = &ctx.info[named].ty;
let resolved = base_ty_res.inner_with(&self.module.types);
let resolved = ctx.resolve_type(named, &self.module.types);

write!(self.out, " {name}")?;
if let TypeInner::Array { base, size, .. } = *resolved {
Expand Down
11 changes: 5 additions & 6 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
const MIP_LEVEL_PARAM: &str = "mip_level";

// Write function return type and name
let ret_ty = func_ctx.info[expr_handle].ty.inner_with(&module.types);
let ret_ty = func_ctx.resolve_type(expr_handle, &module.types);
self.write_value_type(module, ret_ty)?;
write!(self.out, " ")?;
self.write_wrapped_image_query_function_name(wiq)?;
Expand Down Expand Up @@ -891,7 +891,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
}
}
crate::Expression::ImageQuery { image, query } => {
let wiq = match *func_ctx.info[image].ty.inner_with(&module.types) {
let wiq = match *func_ctx.resolve_type(image, &module.types) {
crate::TypeInner::Image {
dim,
arrayed,
Expand All @@ -912,9 +912,8 @@ impl<'a, W: Write> super::Writer<'a, W> {
// Write `WrappedConstructor` for structs that are loaded from `AddressSpace::Storage`
// since they will later be used by the fn `write_storage_load`
crate::Expression::Load { pointer } => {
let pointer_space = func_ctx.info[pointer]
.ty
.inner_with(&module.types)
let pointer_space = func_ctx
.resolve_type(pointer, &module.types)
.pointer_space();

if let Some(crate::AddressSpace::Storage { .. }) = pointer_space {
Expand Down Expand Up @@ -1016,7 +1015,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
if extra == 0 {
self.write_expr(module, coordinate, func_ctx)?;
} else {
let num_coords = match *func_ctx.info[coordinate].ty.inner_with(&module.types) {
let num_coords = match *func_ctx.resolve_type(coordinate, &module.types) {
crate::TypeInner::Scalar { .. } => 1,
crate::TypeInner::Vector { size, .. } => size as usize,
_ => unreachable!(),
Expand Down
2 changes: 1 addition & 1 deletion src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl<W: fmt::Write> super::Writer<'_, W> {
}
};

let parent = match *func_ctx.info[next_expr].ty.inner_with(&module.types) {
let parent = match *func_ctx.resolve_type(next_expr, &module.types) {
crate::TypeInner::Pointer { base, .. } => match module.types[base].inner {
crate::TypeInner::Struct { ref members, .. } => Parent::Struct(members),
crate::TypeInner::Array { stride, .. } => Parent::Array { stride },
Expand Down
Loading