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

glsl-in: Fix using swizzle as out arguments #1632

Merged
merged 1 commit into from
Dec 28, 2021
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
37 changes: 34 additions & 3 deletions src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,6 @@ impl Parser {
// If the argument is to be passed as a pointer but the type of the
// expression returns a vector it must mean that it was for example
// swizzled and it must be spilled into a local before calling
// TODO: this part doesn't work because of #1385 once that's sorted out
// revisit this part.
TypeInner::Vector { size, kind, width } => (
self.module.types.insert(
Type {
Expand Down Expand Up @@ -829,7 +827,40 @@ impl Parser {
arguments.push(temp_expr);
// Register the temporary local to be written back to it's original
// place after the function call
proxy_writes.push((handle, temp_expr));
if let Expression::Swizzle {
size,
mut vector,
pattern,
} = ctx.expressions[value]
{
if let Expression::Load { pointer } = ctx.expressions[vector] {
vector = pointer;
}

for (i, component) in pattern.iter().take(size as usize).enumerate() {
let original = ctx.add_expression(
Expression::AccessIndex {
base: vector,
index: *component as u32,
},
Span::default(),
body,
);

let temp = ctx.add_expression(
Expression::AccessIndex {
base: temp_expr,
index: i as u32,
},
Span::default(),
body,
);

proxy_writes.push((original, temp));
}
} else {
proxy_writes.push((handle, temp_expr));
}
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/in/glsl/swizzle_write.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#version 450

void foo(inout vec2 p) {}

void main() {
vec3 x = vec3(2.0);
x.zxy.xy = vec2(3.0, 4.0);
x.rg *= 5.0;
x.zy++;
foo(x.xz);
}
13 changes: 13 additions & 0 deletions tests/out/wgsl/swizzle_write-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
fn foo(p: ptr<function, vec2<f32>>) {
return;
}

fn main_1() {
var x: vec3<f32> = vec3<f32>(2.0, 2.0, 2.0);
var local: vec2<f32>;

let _e3 = x;
let _e8 = vec2<f32>(3.0, 4.0);
Expand All @@ -14,6 +19,14 @@ fn main_1() {
let _e27 = (_e23.zy + vec2<f32>(1.0));
x.z = _e27.x;
x.y = _e27.y;
let _e32 = x;
let _e34 = x;
local = _e34.xz;
foo((&local));
let _e41 = local.x;
x.x = _e41;
let _e42 = local.y;
x.z = _e42;
return;
}

Expand Down