[glsl-in] Be more conservative with vector resizes

This commit is contained in:
João Capucho
2021-08-03 00:02:21 +01:00
committed by Dzmitry Malyshau
parent de6399a9c7
commit 151a36352e
2 changed files with 33 additions and 38 deletions

View File

@@ -925,6 +925,22 @@ impl<'function> Context<'function> {
Ok(())
}
pub fn vector_resize(
&mut self,
size: VectorSize,
vector: Handle<Expression>,
body: &mut Block,
) -> Handle<Expression> {
self.add_expression(
Expression::Swizzle {
size,
vector,
pattern: crate::SwizzleComponent::XYZW,
},
body,
)
}
}
impl<'function> Index<Handle<Expression>> for Context<'function> {

View File

@@ -1,12 +1,11 @@
use super::{ast::*, error::ErrorKind, SourceMetadata};
use crate::{
proc::ensure_block_returns, Arena, BinaryOperator, Block, Constant, ConstantInner, EntryPoint,
Expression, Function, FunctionArgument, FunctionResult, Handle, ImageClass, ImageDimension,
ImageQuery, LocalVariable, MathFunction, Module, RelationalFunction, SampleLevel, ScalarKind,
ScalarValue, Statement, StructMember, SwizzleComponent, Type, TypeInner, VectorSize,
ScalarValue, Statement, StructMember, Type, TypeInner, VectorSize,
};
use super::{ast::*, error::ErrorKind, SourceMetadata};
/// Helper struct for texture calls with the separate components from the vector argument
///
/// Obtained by calling [`coordinate_components`](Program::coordinate_components)
@@ -99,14 +98,11 @@ impl Program {
body,
),
TypeInner::Vector { size, kind, width } => {
let expr = ctx.add_expression(
Expression::Swizzle {
size,
vector: args[0].0,
pattern: SwizzleComponent::XYZW,
},
body,
);
let mut expr = args[0].0;
if vector_size.map_or(true, |s| s != size) {
expr = ctx.vector_resize(size, expr, body);
}
ctx.add_expression(
Expression::As {
@@ -137,11 +133,11 @@ impl Program {
let column = match *self.resolve_type(ctx, args[0].0, args[0].1)? {
TypeInner::Scalar { .. } => ctx
.add_expression(Expression::Splat { size: rows, value }, body),
TypeInner::Matrix { .. } => {
TypeInner::Matrix { rows: ori_rows, .. } => {
let mut components = Vec::new();
for n in 0..columns as u32 {
let vector = ctx.add_expression(
let mut vector = ctx.add_expression(
Expression::AccessIndex {
base: value,
index: n,
@@ -149,16 +145,11 @@ impl Program {
body,
);
let c = ctx.add_expression(
Expression::Swizzle {
size: rows,
vector,
pattern: SwizzleComponent::XYZW,
},
body,
);
if ori_rows != rows {
vector = ctx.vector_resize(rows, vector, body);
}
components.push(c)
components.push(vector)
}
let h = ctx.add_expression(
@@ -398,14 +389,7 @@ impl Program {
_ => VectorSize::Tri,
};
right = ctx.add_expression(Expression::Splat { size, value: right }, body);
ctx.add_expression(
Expression::Swizzle {
size,
vector: base,
pattern: SwizzleComponent::XYZW,
},
body,
)
ctx.vector_resize(size, base, body)
};
let coords = ctx.add_expression(
Expression::Binary {
@@ -1282,14 +1266,9 @@ impl Program {
};
let coordinate = match (image_size, coord_size) {
(Some(size), Some(coord_s)) if size != coord_s => ctx.add_expression(
Expression::Swizzle {
size,
vector: coord,
pattern: SwizzleComponent::XYZW,
},
body,
),
(Some(size), Some(coord_s)) if size != coord_s => {
ctx.vector_resize(size, coord, body)
}
(None, Some(_)) => ctx.add_expression(
Expression::AccessIndex {
base: coord,