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

Skip useless vec constructors #191

Merged
merged 1 commit into from
Dec 31, 2022
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,14 @@ Calls to `vec2`, `vec3`, and `vec4` can be simplified using swizzles.

Input:
```glsl
vec4(v1.x, v1.z, v2.r, v2.t)
vec4(v1.x, v1.z, v2.r, v2.t);
vec2(v1.xx);
```

Output:
```glsl
vec4(v1.xz,v2.xy)
vec4(v1.xz,v2.xy);
v1.xx;
```

<!--
Expand Down
16 changes: 12 additions & 4 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,22 @@ let private simplifyOperator env = function

// Simplify calls to the vec constructor.
let private simplifyVec constr args =
// Combine swizzles, e.g.
// vec4(v1.x, v1.z, v2.r, v2.t) => vec4(v1.xz, v2.xy)
let rec combineSwizzles = function
| [] -> []
| Dot (Var v1, fields1) :: Dot (Var v2, fields2) :: args
when isFieldSwizzle fields1 && isFieldSwizzle fields2 && v1.Name = v2.Name ->
combineSwizzles (Dot (Var v1, fields1 + fields2) :: args)
| Dot (Var v1, field1) :: Dot (Var v2, field2) :: args
when isFieldSwizzle field1 && isFieldSwizzle field2 && v1.Name = v2.Name ->
combineSwizzles (Dot (Var v1, field1 + field2) :: args)

| e::l -> e :: combineSwizzles l
FunCall (Var constr, combineSwizzles args)
let args = combineSwizzles args
match args with
| [Dot (_, field) as arg] when field.Length > 1 && isFieldSwizzle field ->
// vec3(v.xxy) => v.xxy
// However, vec3(v.x) should be preserved.
arg
| _ -> FunCall (Var constr, args)

let private simplifyExpr (didInline: bool ref) env = function
| FunCall(Var v, passedArgs) as e when v.ToBeInlined ->
Expand Down
2 changes: 1 addition & 1 deletion tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ float sdOctogon(vec2 p,float r)
{
const vec3 k=vec3(-.9238795325,.3826834323,.4142135623);
p=abs(p);
p-=2.*min(dot(vec2(k.xy),p),0.)*vec2(k.xy);
p-=2.*min(dot(k.xy,p),0.)*k.xy;
p-=2.*min(dot(vec2(-k.x,k.y),p),0.)*vec2(-k.x,k.y);
p-=vec2(clamp(p.x,-k.z*r,k.z*r),r);
return length(p)*sign(p.y);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/vectors.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ float swizzles() {
vec3 v3 = vec3(v1.x, v2.x, v2.r);
vec4 v4 = vec4(v1.xx, v1.y, v1.x);
vec4 v5 = vec4(1., v2.z, v2.g, 2.);
vec4 v6 = vec4(v1.x, v1.z, v2.r, v2.t);
vec4 v6 = vec4(v1.x, v1.y, v2.r, v2.t);
return v1.x + v2.x + v3.x + v4.x + v5.x + v6.x;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/vectors.frag.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
float swizzles()
{
vec2 v1=vec2(1.,1.);
vec3 v2=vec3(v1.xyx),v3=vec3(v1.x,v2.xx);
vec4 v4=vec4(v1.xxyx),v5=vec4(1.,v2.zy,2.),v6=vec4(v1.xz,v2.xy);
vec3 v2=v1.xyx,v3=vec3(v1.x,v2.xx);
vec4 v4=v1.xxyx,v5=vec4(1.,v2.zy,2.),v6=vec4(v1.xy,v2.xy);
return v1.x+v2.x+v3.x+v4.x+v5.x+v6.x;
}
void main()
Expand Down