Skip to content

Commit

Permalink
Simple vec when all arguments are equal
Browse files Browse the repository at this point in the history
vec2(1,1)  =>  vec2(1)
  • Loading branch information
laurentlb committed Dec 31, 2022
1 parent 55c3432 commit 055e352
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 16 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,25 @@ stored the result of a computation), so be careful with it.

### Vector constructors

Calls to `vec2`, `vec3`, and `vec4` can be simplified using swizzles.
- Calls to `vec2`, `vec3`, and `vec4` can be simplified using swizzles.
- Remove useless constructor, when the argument is already a vec using swizzles.
- If all arguments are equal (but not function calls), use only one argument.
- Replace floats with ints, as it is safe inside the vec constructors.

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

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

<!--
Expand Down
11 changes: 11 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,18 @@ let private simplifyVec constr args =
| Float (f, _) when Decimal.Round(f) = f -> Int (int f, "")
| e -> e

// vec3(1,1,1) => vec3(1)
// For safety, do not merge if there are function calls, e.g. vec2(rand(), rand()).
// Iterate over the args as long as the arguments are equal.
let rec mergeAllEquals allArgs = function
| FunCall _ :: _ -> allArgs
| e1 :: e2 :: rest when Printer.exprToS e1 = Printer.exprToS e2 ->
mergeAllEquals allArgs (e2 :: rest)
| [e] -> [e]
| _ -> allArgs

let args = combineSwizzles args |> List.map useInts
let args = mergeAllEquals args args
match args with
| [Dot (_, field) as arg] when field.Length > 1 && isFieldSwizzle field ->
// vec3(v.xxy) => v.xxy
Expand Down
4 changes: 2 additions & 2 deletions tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ float s(vec2 p)
float valnoise(vec2 p)
{
vec2 c=floor(p),f=smoothstep(0.,1.,fract(p));
return mix(mix(s(c+vec2(0,0)),s(c+vec2(1,0)),f.x),mix(s(c+vec2(0,1)),s(c+vec2(1,1)),f.x),f.y);
return mix(mix(s(c+vec2(0)),s(c+vec2(1,0)),f.x),mix(s(c+vec2(0,1)),s(c+vec2(1)),f.x),f.y);
}
float fbm(vec2 p)
{
Expand Down Expand Up @@ -561,7 +561,7 @@ void main()
w.w=R();
if(w.w<.5)
{
p=(w.xyz*2-1)*vec3(.1,.1,.1);
p=(w.xyz*2-1)*vec3(.1);
vec3 op=p;
p.y-=.3;
p.xz*=mix(.1,.5*p.y*4.-1.,pow(fract(p.y*8.),4));
Expand Down
2 changes: 1 addition & 1 deletion tests/real/leizex.expected
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _leizex_frag:
db '{'
db 'ivec3 ip=ivec3(floor(x));'
db 'vec3 f=fract(x);'
db 'vec2 dmin=vec2(1,1);'
db 'vec2 dmin=vec2(1);'
db 'for(int k=-1;k<=1;k++)'
db 'for(int j=-1;j<=1;j++)'
db 'for(int i=-1;i<=1;i++)'
Expand Down
4 changes: 2 additions & 2 deletions tests/real/mandelbulb.expected
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const char *mandelbulb_frag =
"void main()"
"{"
"vec2 v=-1.+2.*gl_FragCoord.xy/resolution.xy,c=v*vec2(1.33,1);"
"vec3 x=vec3(.577,.577,.577),o=vec3(-.707,0,.707);"
"vec3 x=vec3(.577),o=vec3(-.707,0,.707);"
"float y=1.,s=1.4+.2*cos(6.28318*time/20.);"
"vec3 z=vec3(s*sin(6.28318*time/20.),.3-.4*sin(6.28318*time/20.),s*cos(6.28318*time/20.)),p=vec3(0,.1,0),m=normalize(p-z),t=vec3(0,1,0),r=normalize(cross(m,t)),w=normalize(cross(r,m)),e=normalize(c.x*r+c.y*w+1.5*m),l,i;"
"vec4 u;"
Expand All @@ -111,7 +111,7 @@ const char *mandelbulb_frag =
"if(d>.001)"
"if(f(n,x,q,1e20,g,C,y))"
"d=.1;"
"i=vec3(1,1,1);"
"i=vec3(1);"
"i=mix(i,vec3(.8,.6,.2),sqrt(u.x)*1.25);"
"i=mix(i,vec3(.8,.3,.3),sqrt(u.y)*1.25);"
"i=mix(i,vec3(.7,.4,.3),sqrt(u.z)*1.25);"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/ohanami.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ float noise(vec2 p)
float valnoise(vec2 p)
{
vec2 c=floor(p),f=smoothstep(0.,1,fract(p));
return mix(mix(noise(c+vec2(0,0)),noise(c+vec2(1,0)),f.x),mix(noise(c+vec2(0,1)),noise(c+vec2(1,1)),f.x),f.y);
return mix(mix(noise(c+vec2(0)),noise(c+vec2(1,0)),f.x),mix(noise(c+vec2(0,1)),noise(c+vec2(1)),f.x),f.y);
}
vec4 samp(vec2 p)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/real/sult.expected
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const char *sult_frag =
"float v=5.,y=.9,a=0.,f=90.,e=0.;"
"vec3 r=vec3(1,1,1),c=vec3(0,0,1),t=vec3(0,0,1.5);"
"vec3 r=vec3(1),c=vec3(0,0,1),t=vec3(0,0,1.5);"
"uniform vec2 m;"
"uniform float l;"
"vec3 rotatey(vec3 v,float m)"
Expand Down
8 changes: 4 additions & 4 deletions tests/real/the_real_party_is_in_your_pocket.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"m.x-=.023;"
"m.x=abs(m.x);"
"m.x-=max(m.y/2.+.002,0.);"
"l=min(l,v(m-vec2(0,0),vec2(.0065,.028)));"
"l=min(l,v(m-vec2(0),vec2(.0065,.028)));"
"return l;"
"}"
"float v(vec2 l)"
Expand Down Expand Up @@ -135,7 +135,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"a=m(a,min(l.z,-t+.004),320.);"
"a=m(a,min(l.z,-min(1.,e)+.006),192.);"
"l.xy-=vec2(.405,-.28);"
"float g=(1.-x(.002,min(min(n(l.xy,vec2(0,-.035),vec2(-.014,.024)),n(l.xy,vec2(-.03,-.035),vec2(-.014,.024))),n(l.xy,vec2(-.02,-.02),vec2(-.01,-.02)))-.0025))*.002;"
"float g=(1.-x(.002,min(min(n(l.xy,vec2(0,-.035),vec2(-.014,.024)),n(l.xy,vec2(-.03,-.035),vec2(-.014,.024))),n(l.xy,vec2(-.02),vec2(-.01,-.02)))-.0025))*.002;"
"l.xy+=vec2(.405,-.28);"
"l.xy-=vec2(.202,-.345);"
"float w=min(min(min(n(l.xy,vec2(-.03,-.035),vec2(-.03,.024)),n(l.xy,vec2(-.03,-.035),vec2(-.018,-.035))),n(l.xy,vec2(-.03,.024),vec2(-.018,.024))),n(l.xy,vec2(-.03,-.0055),vec2(-.018,-.0055)));"
Expand Down Expand Up @@ -295,7 +295,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"if(d.y>.5)"
"{"
"vec2 h=o.zw-vec2(-.38,.6);"
"float g=min(min(min(min(min(min(max(length(h-vec2(.005,.005))-.0125,-(length(h-vec2(-.012,.005))-.025)),max(length(h-vec2(-.02,.005))-.0125,-(length(h-vec2(-.045,.005))-.03))),max(length(h-vec2(.005,.005))-.0125,-(length(h-vec2(-.012,.005))-.025))),max(length(h-vec2(.03,.005))-.0125,-(length(h-vec2(.02,.005))-.02))),abs(length(h-vec2(-.037,-.0398))-.0104)),n(h,vec2(-.064,-.05),vec2(-.064,-.03))),max(-h.x-.063,abs(length(vec2(max(0.,h.x+.06),h.y+.035))-.006)));"
"float g=min(min(min(min(min(min(max(length(h-vec2(.005))-.0125,-(length(h-vec2(-.012,.005))-.025)),max(length(h-vec2(-.02,.005))-.0125,-(length(h-vec2(-.045,.005))-.03))),max(length(h-vec2(.005))-.0125,-(length(h-vec2(-.012,.005))-.025))),max(length(h-vec2(.03,.005))-.0125,-(length(h-vec2(.02,.005))-.02))),abs(length(h-vec2(-.037,-.0398))-.0104)),n(h,vec2(-.064,-.05),vec2(-.064,-.03))),max(-h.x-.063,abs(length(vec2(max(0.,h.x+.06),h.y+.035))-.006)));"
"h.x-=.091;"
"g=min(g,n(h,vec2(-.064,-.05),vec2(-.064,-.03)));"
"g=min(g,max(-h.x-.063,abs(length(vec2(max(0.,h.x+.06),h.y+.035))-.006)));"
Expand Down Expand Up @@ -338,7 +338,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"t=mix(t,vec3(.730857,.454964,.000553633),step(abs(length(h)-.02)-.007,0.));"
"}"
"{"
"vec2 h=o.zw-vec2(.24,.103),g=vec2(-.006,-.026),u=vec2(-.002,0),q=vec2(-.007,.016),C=q+normalize(q-u)*.012,F=vec2(.02,.02),T=C+normalize(F-C)*.05,Z=vec2(.002,-.007),Y=vec2(.025,-.028);"
"vec2 h=o.zw-vec2(.24,.103),g=vec2(-.006,-.026),u=vec2(-.002,0),q=vec2(-.007,.016),C=q+normalize(q-u)*.012,F=vec2(.02),T=C+normalize(F-C)*.05,Z=vec2(.002,-.007),Y=vec2(.025,-.028);"
"t=mix(t,vec3(0,.332318,.292872),step(min(min(min(m(h,q,C,F),m(h,g,u,q)),m(h,F,T,Z)),m(h,Z,(Z+Y)/2.+vec2(.001),Y))-.007,0.));"
"}"
"}"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/valley_ball.glsl.expected
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ float smoothrnd(vec2 x)
vec2 a=fract(x);
x-=a;
vec2 u=a*a*(3.-2.*a);
return mix(mix(rnd(x+vec2(0,0)),rnd(x+vec2(1,0)),u.x),mix(rnd(x+vec2(0,1)),rnd(x+vec2(1,1)),u.x),u.y);
return mix(mix(rnd(x+vec2(0)),rnd(x+vec2(1,0)),u.x),mix(rnd(x+vec2(0,1)),rnd(x+vec2(1)),u.x),u.y);
}
float norm(float x)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/real/yx_long_way_from_home.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const char *yx_long_way_from_home_frag =
"vec2 i;"
"vec3 h(vec3 v)"
"{"
"vec3 i=cross(vec3(-1,-1,-1),v);"
"vec3 i=cross(vec3(-1),v);"
"return i;"
"}"
"vec3 e(vec3 v,float y)"
Expand Down Expand Up @@ -174,7 +174,7 @@ const char *yx_long_way_from_home_frag =
"i=s(x);"
"v+=(i-.5)/iResolution.xy;"
"v.x*=iResolution.x/iResolution.y;"
"const vec3 f=vec3(-4,2,3),y=vec3(0,0,0);"
"const vec3 f=vec3(-4,2,3),y=vec3(0);"
"const float z=distance(f,y);"
"const vec2 r=vec2(1,2)*.015;"
"vec3 c=vec3(0),o=normalize(vec3(v,2));"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/vectors.frag.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
float swizzles()
{
vec2 v1=vec2(1,1);
vec2 v1=vec2(1);
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;
Expand Down

0 comments on commit 055e352

Please sign in to comment.