From c492b2a69ddabf4dca1b5ffd5feb169f84620ead Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 4 May 2024 22:27:44 +0200 Subject: [PATCH] Detect more augmentable operators a=3+a => a+=3 --- src/rewriter.fs | 7 +++++++ tests/compression_results.log | 20 +++++++++---------- .../from-the-seas-to-the-stars.frag.expected | 4 ++-- tests/real/leizex.expected | 2 +- tests/real/lunaquatic.frag.expected | 2 +- tests/real/mouton/mouton.expected | 2 +- tests/real/ohanami.frag.expected | 2 +- tests/real/orchard.frag.expected | 2 +- tests/real/slisesix.frag.expected | 2 +- tests/real/valley_ball.glsl.expected | 2 +- tests/unit/inline.no.expected | 4 ++-- tests/unit/reuse-var.frag.expected | 6 +++--- 12 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/rewriter.fs b/src/rewriter.fs index 9e1564d6..bf438ee6 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -215,6 +215,13 @@ module private RewriterImpl = | FunCall(Op "=", [Var x; FunCall(Op op, [Var y; e])]) when x.Name = y.Name && augmentableOperators.Contains op -> FunCall(Op (op + "="), [Var x; e]) + + // Match: x = ... + x + // works only if the operator is commutative + | FunCall(Op "=", [Var x; FunCall(Op ("+"|"*"|"&"|"^"|"|" as op), [e; Var y])]) + when x.Name = y.Name -> + FunCall(Op (op + "="), [Var x; e]) + // Unsafe when x contains NaN or Inf values. //| FunCall(Op "=", [Var x; FunCall(Var fctName, [Int (0, _)])]) // when List.contains fctName.Name ["vec2"; "vec3"; "vec4"; "ivec2"; "ivec3"; "ivec4"] -> diff --git a/tests/compression_results.log b/tests/compression_results.log index 514dbe92..fb7824be 100644 --- a/tests/compression_results.log +++ b/tests/compression_results.log @@ -1,24 +1,24 @@ -clod.frag... 8844 => 1513.863 -mouton/mouton.vert... 16973 => 2441.227 +clod.frag... 8842 => 1513.669 +mouton/mouton.vert... 16972 => 2439.859 audio-flight-v2.frag 4526 => 883.304 buoy.frag 4070 => 619.904 controllable-machinery.frag 7708 => 1220.329 ed-209.frag 7734 => 1333.865 elevated.hlsl 3405 => 603.218 endeavour.frag 2589 => 529.811 -from-the-seas-to-the-stars.frag 14252 => 2316.095 +from-the-seas-to-the-stars.frag 14250 => 2312.836 frozen-wasteland.frag 4566 => 806.475 kinder_painter.frag 2847 => 442.771 -leizex.frag 2276 => 509.507 -lunaquatic.frag 5236 => 1044.110 +leizex.frag 2275 => 509.430 +lunaquatic.frag 5235 => 1044.000 mandelbulb.frag 2347 => 537.664 -ohanami.frag 3256 => 722.517 -orchard.frag 5537 => 1022.773 +ohanami.frag 3255 => 723.026 +orchard.frag 5536 => 1022.550 oscars_chair.frag 4651 => 986.364 robin.frag 6268 => 1043.078 -slisesix.frag 4550 => 914.573 +slisesix.frag 4549 => 913.642 terrarium.frag 3611 => 744.367 the_real_party_is_in_your_pocket.frag 12101 => 1787.736 -valley_ball.glsl 4335 => 891.345 +valley_ball.glsl 4334 => 890.606 yx_long_way_from_home.frag 2926 => 599.317 -Total: 134608 => 23514.210 +Total: 134597 => 23507.820 diff --git a/tests/real/from-the-seas-to-the-stars.frag.expected b/tests/real/from-the-seas-to-the-stars.frag.expected index cc92d270..dfd26597 100644 --- a/tests/real/from-the-seas-to-the-stars.frag.expected +++ b/tests/real/from-the-seas-to-the-stars.frag.expected @@ -126,7 +126,7 @@ bool jellyfish() { col=vec3(.2,.4,1)/3+pow(w.w,8); float to=(of.x+of.y*8)*.3; - to=time/2+to; + to+=time/2; w.x=R(); w.y=R(); w.z=R(); @@ -758,7 +758,7 @@ void main() p.y+=.5; vec3 r=normalize(camtarget-campos),s=normalize(cross(vec3(0,1,0),r)),t=cross(s,r); p-=campos; - p=transpose(mat3(s,-t,r))*p; + p*=transpose(mat3(s,-t,r)); p.xy*=rotmat(sin(time/2)*.05*cameraroll+cameraroll2); p.y+=(fbm(vec2(time/16))-.5)*.1*cameraroll; p.x+=(fbm(vec2(time/14+10))-.5)*.1*cameraroll; diff --git a/tests/real/leizex.expected b/tests/real/leizex.expected index cd0827c2..72f84e12 100644 --- a/tests/real/leizex.expected +++ b/tests/real/leizex.expected @@ -13,7 +13,7 @@ _leizex_frag: db 'uniform sampler2D f,m,o,y;' db 'float t(int v)' db '{' - db 'v=v<<13^v;' + db 'v^=v<<13;' db 'v=v*(v*v*15731+789221)+1376312589&2147483647;' db 'return float(v);' db '}' diff --git a/tests/real/lunaquatic.frag.expected b/tests/real/lunaquatic.frag.expected index d5877c18..9538d8e4 100644 --- a/tests/real/lunaquatic.frag.expected +++ b/tests/real/lunaquatic.frag.expected @@ -25,7 +25,7 @@ vec3 rotateX(vec3 v,float x) float rnd(vec2 x) { int n=int(x.x*40.+x.y*6400.); - n=n<<13^n; + n^=n<<13; return 1.-float(n*(n*n*15731+789221)+1376312589&2147483647)/1073741824.; } float norm(float x) diff --git a/tests/real/mouton/mouton.expected b/tests/real/mouton/mouton.expected index d23bb98a..b471afea 100644 --- a/tests/real/mouton/mouton.expected +++ b/tests/real/mouton/mouton.expected @@ -761,7 +761,7 @@ const char *mouton_frag = "b=mix(b,mix(mix(vec3(1,.5,0),vec3(.8,.5,1),(cos(Y*5.+t*5.)*.5+.5)*a.y),vec3(1),smoothstep(-Y,Y,q)),a.x);" "}" "l=vec2(abs(f.x*5.-.35)-1.8,f.y*5.-1.4);" - "l=E(t*5.)*l;" + "l*=E(t*5.);" "G=(1.4+.2*sin(t*20.))*smoothstep(.5,1.,a.x);" "O=J(l,G);" "P=mix(vec3(1,.6,0),vec3(1,.2,0),smoothstep(-.1,.6,J(l,G*.5)))*1.3;" diff --git a/tests/real/ohanami.frag.expected b/tests/real/ohanami.frag.expected index eb0f04e1..50040245 100644 --- a/tests/real/ohanami.frag.expected +++ b/tests/real/ohanami.frag.expected @@ -57,7 +57,7 @@ void main() if(ti==10) ro.y+=2; vec3 w=normalize(vec3(0,1.3,0)-ro),u=normalize(cross(w,vec3(0,1,0))); - rd=mat3(u,normalize(cross(w,-u)),w)*rd; + rd*=mat3(u,normalize(cross(w,-u)),w); gl_FragColor.xyz=vec3(.8,.8,1)/6; float t=0.,d=0.; for(int i=0;i<100;++i) diff --git a/tests/real/orchard.frag.expected b/tests/real/orchard.frag.expected index bf400599..7be53637 100644 --- a/tests/real/orchard.frag.expected +++ b/tests/real/orchard.frag.expected @@ -279,7 +279,7 @@ void mainImage(out vec4 fragColour,vec2 fragCoord) #endif vec3 seedDir=normalize(vec3(0,0,1)); - seedDir=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5)*seedDir; + seedDir*=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5); vec3 rd=seedDir,col=vec3(0),sky=mix(vec3(FOG_COLOUR),vec3(0,.4,.6),abs(rd.y)); float alpha=marchScene(camera,rd,fragCoord); vec4 mat; diff --git a/tests/real/slisesix.frag.expected b/tests/real/slisesix.frag.expected index 42c710e0..4cecaa81 100644 --- a/tests/real/slisesix.frag.expected +++ b/tests/real/slisesix.frag.expected @@ -5,7 +5,7 @@ uniform float time; uniform sampler2D tex0; int icoolfFunc3d2(int n) { - n=n<<13^n; + n^=n<<13; return n*(n*n*15731+789221)+1376312589&2147483647; } float coolfFunc3d2(int n) diff --git a/tests/real/valley_ball.glsl.expected b/tests/real/valley_ball.glsl.expected index 8dd21780..9ec05d31 100644 --- a/tests/real/valley_ball.glsl.expected +++ b/tests/real/valley_ball.glsl.expected @@ -9,7 +9,7 @@ float sphereRadius,gf_DetailLevel,pi,eps,bigeps; float rnd(vec2 x) { int n=int(x.x*40.+x.y*6400.); - n=n<<13^n; + n^=n<<13; return 1.-float(n*(n*n*15731+789221)+1376312589&2147483647)/1073741824.; } float smoothrnd(vec2 x) diff --git a/tests/unit/inline.no.expected b/tests/unit/inline.no.expected index 3129e3c0..6400c5ce 100644 --- a/tests/unit/inline.no.expected +++ b/tests/unit/inline.no.expected @@ -4,7 +4,7 @@ float result; void main() { float x=.5; - x=.6*x*x; + x*=.6*x; result=x; } int arithmetic() @@ -18,7 +18,7 @@ int vars(int arg,int arg2) int arithmetic2() { int a=2,b=3; - b=a+b; + b+=a; return 4*a*b; } int unusedVars() diff --git a/tests/unit/reuse-var.frag.expected b/tests/unit/reuse-var.frag.expected index 414e18db..ee8392f6 100644 --- a/tests/unit/reuse-var.frag.expected +++ b/tests/unit/reuse-var.frag.expected @@ -6,7 +6,7 @@ float simple_var_decl_reuse(float x) int b2=5+int(a); float c=a; sep+=vec3(0); - a=9.+a; + a+=9.; int d=3+b+b2; sep+=vec3(0); b2=d+b2-b; @@ -28,7 +28,7 @@ float multidecl_var_decl_reuse(float x) float a=1.+x; int b=3+int(x),b2=5+int(a); float c=a; - a=9.+a; + a+=9.; int d=3+b+b2; b=d+b2-b; float e=c*a; @@ -36,7 +36,7 @@ float multidecl_var_decl_reuse(float x) b2=3*d*b; d=4/b-d; a=e-float(b2)+c; - e=4.-a+c+e; + e+=4.-a+c; b=3*b2-d; d=7*d-b2; return float(b*d)*a*e*x+e/float(d-b);