From 9ddc30ed88e217ae596bb04d396b8f60527c3faf Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sun, 5 May 2024 20:51:52 +0200 Subject: [PATCH] Reuse function parameters inside the function (#370) --- src/ast.fs | 4 +- src/rewriter.fs | 38 +-- tests/compression_results.log | 34 +-- tests/real/audio-flight-v2.frag.expected | 12 +- tests/real/buoy.frag.expected | 25 +- .../real/controllable-machinery.frag.expected | 4 +- tests/real/ed-209.frag.expected | 33 +-- .../from-the-seas-to-the-stars.frag.expected | 30 +-- tests/real/leizex.expected | 94 +++---- tests/real/lunaquatic.frag.expected | 5 +- tests/real/mouton/mouton.expected | 170 ++++++------- tests/real/ohanami.frag.expected | 5 +- tests/real/orchard.frag.expected | 34 +-- tests/real/robin.frag.expected | 25 +- tests/real/terrarium.frag.expected | 14 +- ...real_party_is_in_your_pocket.frag.expected | 230 +++++++++--------- tests/real/valley_ball.glsl.expected | 30 +-- .../real/yx_long_way_from_home.frag.expected | 77 +++--- tests/unit/inout.expected | 58 ++--- tests/unit/many_variables.expected | 32 ++- tests/unit/operators.expected | 4 +- tests/unit/shadowing.frag.expected | 6 +- tests/unit/simplify.expected | 10 +- 23 files changed, 494 insertions(+), 480 deletions(-) diff --git a/src/ast.fs b/src/ast.fs index e5ba61d0..56aaf990 100644 --- a/src/ast.fs +++ b/src/ast.fs @@ -191,7 +191,7 @@ type Shader = { // while also collecting visible variable and function declarations along the way. [] [] -type BlockLevel = FunctionRoot | Nested | Unknown +type BlockLevel = FunctionRoot of FunctionType | Nested | Unknown [] type MapEnv = { @@ -315,7 +315,7 @@ let mapTopLevel env li = let env = env.withFunction(fct, body, replaceMostRecentOverload = true) // Transform the body. The env modifications (local variables) are discarded. - let _, body = mapStmt BlockLevel.FunctionRoot env body + let _, body = mapStmt (BlockLevel.FunctionRoot fct) env body // Update env.fns with the transformed body. let env = env.withFunction(fct, body, replaceMostRecentOverload = true) diff --git a/src/rewriter.fs b/src/rewriter.fs index bf438ee6..1a4edb78 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -428,7 +428,7 @@ module private RewriterImpl = // Reuse an existing local variable declaration that won't be used anymore, instead of introducing a new one. // The reused identifier gets compressed better, and the declaration is sometimes removed. // float d1 = f(); float d2 = g(); -> float d1 = f(); d1 = g(); - let reuseExistingVarDecl b = + let reuseExistingVarDecl blockLevel b = let tryReplaceWithPrecedingAndFollowing f (xs : Stmt list) = let rec go f preceding = function | head :: tail -> @@ -440,20 +440,22 @@ module private RewriterImpl = b |> tryReplaceWithPrecedingAndFollowing (function | (preceding2, Decl (ty2, declElts), following2) -> let findAssignmentReplacementFor declElt2 declBefore2 declAfter2 = - let compatibleDeclElts = (preceding2 @ declBefore2) |> List.collect (function - // The previous declaration must have the same type. - | Decl (ty1, declElts1) when ty2 = ty1 -> - let firstIsNotUsedAfterSecondDeclared (declElt1 : DeclElt) followingDecl2 = - // The first variable must not be used after the second is declared. - Analyzer.varUsesInStmt (Block followingDecl2) |> List.forall (fun i -> i.Name <> declElt1.name.Name) - declElts1 |> List.filter (fun declElt1 -> - // The previous declaration must have the same size and semantics - declElt1.size = declElt2.size && - declElt1.semantics = declElt2.semantics && - firstIsNotUsedAfterSecondDeclared declElt1 (declAfter2 @ following2) - ) - | _ -> []) - match compatibleDeclElts |> List.tryHead with + // Collect previous declarations of the same type. + let localDecls = (preceding2 @ declBefore2) |> List.collect (function Decl (ty1, declElts1) when ty2 = ty1 -> declElts1 | _ -> []) + let args = + match blockLevel with + | BlockLevel.FunctionRoot fct -> + fct.parameters |> List.choose (function ty, decl -> if not ty.isOutOrInout && ty = ty2 then Some decl else None) + | _ -> [] + + let compatibleDeclElt = (localDecls @ args) |> List.tryFind (fun declElt1 -> + declElt1.size = declElt2.size && + declElt1.semantics = declElt2.semantics && + // The first variable must not be used after the second is declared. + Analyzer.varUsesInStmt (Block (declAfter2 @ following2)) |> List.forall (fun i -> i.Name <> declElt1.name.Name) + ) + + match compatibleDeclElt with | None -> None | Some declElt1 -> debug $"{declElt2.name.Loc}: eliminating local variable '{declElt2.name}' by reusing existing local variable '{declElt1.name}'" @@ -592,9 +594,9 @@ module private RewriterImpl = let b = // We ensure control flow analysis is trivial by only doing this at the root block level. - if blockLevel <> BlockLevel.FunctionRoot || hasPreprocessor + if (match blockLevel with BlockLevel.FunctionRoot _ -> false | _ -> true) || hasPreprocessor then b - else reuseExistingVarDecl b + else reuseExistingVarDecl blockLevel b // Consecutive declarations of the same type become one. float a;float b; -> float a,b; let b = squeezeConsecutiveDeclarations b @@ -772,7 +774,7 @@ module private ArgumentInlining = let applyTopLevel = function | Function(fct, body) as f -> // Handle argument inlining for other functions called by f. - let _, body = mapStmt BlockLevel.FunctionRoot (mapEnvExpr applyExpr) body + let _, body = mapStmt (BlockLevel.FunctionRoot fct) (mapEnvExpr applyExpr) body // Handle argument inlining for f. Remove the parameter from the declaration. let fct = {fct with args = removeInlined f fct.args} // Handle argument inlining for f. Insert in front of the body a declaration for each inlined argument. diff --git a/tests/compression_results.log b/tests/compression_results.log index fb7824be..f14fede3 100644 --- a/tests/compression_results.log +++ b/tests/compression_results.log @@ -1,24 +1,24 @@ -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 +clod.frag... 8836 => 1515.830 +mouton/mouton.vert... 16951 => 2447.462 +audio-flight-v2.frag 4509 => 881.311 +buoy.frag 4051 => 607.428 +controllable-machinery.frag 7703 => 1216.907 +ed-209.frag 7702 => 1341.619 elevated.hlsl 3405 => 603.218 endeavour.frag 2589 => 529.811 -from-the-seas-to-the-stars.frag 14250 => 2312.836 +from-the-seas-to-the-stars.frag 14250 => 2308.842 frozen-wasteland.frag 4566 => 806.475 kinder_painter.frag 2847 => 442.771 -leizex.frag 2275 => 509.430 -lunaquatic.frag 5235 => 1044.000 +leizex.frag 2270 => 510.375 +lunaquatic.frag 5239 => 1044.087 mandelbulb.frag 2347 => 537.664 -ohanami.frag 3255 => 723.026 -orchard.frag 5536 => 1022.550 +ohanami.frag 3255 => 723.732 +orchard.frag 5510 => 1027.042 oscars_chair.frag 4651 => 986.364 -robin.frag 6268 => 1043.078 +robin.frag 6257 => 1047.684 slisesix.frag 4549 => 913.642 -terrarium.frag 3611 => 744.367 -the_real_party_is_in_your_pocket.frag 12101 => 1787.736 -valley_ball.glsl 4334 => 890.606 -yx_long_way_from_home.frag 2926 => 599.317 -Total: 134597 => 23507.820 +terrarium.frag 3593 => 741.557 +the_real_party_is_in_your_pocket.frag 12087 => 1780.714 +valley_ball.glsl 4307 => 881.820 +yx_long_way_from_home.frag 2942 => 598.650 +Total: 134416 => 23495.003 diff --git a/tests/real/audio-flight-v2.frag.expected b/tests/real/audio-flight-v2.frag.expected index f3233896..b31e758a 100644 --- a/tests/real/audio-flight-v2.frag.expected +++ b/tests/real/audio-flight-v2.frag.expected @@ -51,13 +51,13 @@ float vmax(vec3 v) } float fBox(vec3 p,vec3 b) { - vec3 d=abs(p)-b; - return length(max(d,vec3(0)))+vmax(min(d,vec3(0))); + p=abs(p)-b; + return length(max(p,vec3(0)))+vmax(min(p,vec3(0))); } float fBox2(vec2 p,vec2 b) { - vec2 d=abs(p)-b; - return length(max(d,vec2(0)))+vmax(min(d,vec2(0))); + p=abs(p)-b; + return length(max(p,vec2(0)))+vmax(min(p,vec2(0))); } float sdCap(vec3 p) { @@ -188,9 +188,9 @@ vec2 marcher(vec3 ro,vec3 rd) } vec3 normal(vec3 p,float t) { - float e=MINDIST*t; + t*=MINDIST; vec2 h=vec2(1,-1)*.5773; - return normalize(h.xyy*map(p+h.xyy*e,0.).x+h.yyx*map(p+h.yyx*e,0.).x+h.yxy*map(p+h.yxy*e,0.).x+h.xxx*map(p+h.xxx*e,0.).x); + return normalize(h.xyy*map(p+h.xyy*t,0.).x+h.yyx*map(p+h.yyx*t,0.).x+h.yxy*map(p+h.yxy*t,0.).x+h.xxx*map(p+h.xxx*t,0.).x); } vec3 hsv2rgb(vec3 c) { diff --git a/tests/real/buoy.frag.expected b/tests/real/buoy.frag.expected index 2efa529e..aae36b7a 100644 --- a/tests/real/buoy.frag.expected +++ b/tests/real/buoy.frag.expected @@ -77,17 +77,16 @@ float WavesSmooth(vec3 pos) } float WaveCrests(vec3 ipos,vec2 fragCoord) { - vec3 pos=ipos; - pos*=.2*vec3(1); + ipos*=.2*vec3(1); float f=0.; - pos+=iTime*vec3(0,.1,.1); - vec3 pos2=pos; + ipos+=iTime*vec3(0,.1,.1); + vec3 pos2=ipos; for(int i=0;i<6;i++) - pos=(pos.yzx+pos.zyx*vec3(1,-1,1))/sqrt(2.),f=f*1.5+abs(Noise(pos).x-.5)*2.,pos*=2.; - pos=pos2*exp2(float(6)); - pos.y=-.05*iTime; + ipos=(ipos.yzx+ipos.zyx*vec3(1,-1,1))/sqrt(2.),f=f*1.5+abs(Noise(ipos).x-.5)*2.,ipos*=2.; + ipos=pos2*exp2(float(6)); + ipos.y=-.05*iTime; for(int i=6;i<16;i++) - pos=(pos.yzx+pos.zyx*vec3(1,-1,1))/sqrt(2.),f=f*1.5+pow(abs(Noise(pos).x-.5)*2.,1.),pos*=2.; + ipos=(ipos.yzx+ipos.zyx*vec3(1,-1,1))/sqrt(2.),f=f*1.5+pow(abs(Noise(ipos).x-.5)*2.,1.),ipos*=2.; f/=1500.; f-=Noise(ivec2(fragCoord.xy)).x*.01; return pow(smoothstep(.4,-.1,f),6.); @@ -145,8 +144,8 @@ vec3 ShadeBoat(vec3 pos,vec3 ray) albedo=mix(vec3(1,.8,.08),albedo,smoothstep(.05-aa,.05,abs(abs(pos.y)-.65))); albedo*=smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1); lightDir=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1); - vec3 rr=reflect(ray,norm); - lightDir+=mix(vec3(0,.04,.04),Sky(rr),smoothstep(-.1,.1,rr.y)); + pos=reflect(ray,norm); + lightDir+=mix(vec3(0,.04,.04),Sky(pos),smoothstep(-.1,.1,pos.y)); aa=pow(1.-abs(dot(norm,ray)),5.); aa=mix(.001,1.,aa); return mix(albedo,lightDir,aa); @@ -189,15 +188,15 @@ vec3 ShadeOcean(vec3 pos,vec3 ray,vec2 fragCoord) vec3 reflectedRay=ray-2.*norm*ndotr; norm=ray+(-cos(1.33*acos(-ndotr))-ndotr)*norm; norm=normalize(norm); - vec3 reflection=Sky(reflectedRay); + ray=Sky(reflectedRay); float t=TraceBoat(pos-0.*reflectedRay,reflectedRay); if(t>0.) - reflection=ShadeBoat(pos+t*reflectedRay,reflectedRay); + ray=ShadeBoat(pos+t*reflectedRay,reflectedRay); t=TraceBoat(pos-0.*norm,norm); reflectedRay=vec3(0,.04,.04); if(t>0.) reflectedRay=mix(reflectedRay,ShadeBoat(pos+t*norm,norm),exp(-t)); - reflectedRay=mix(reflectedRay,reflection,pow(1.-abs(ndotr),5.)); + reflectedRay=mix(reflectedRay,ray,pow(1.-abs(ndotr),5.)); return mix(reflectedRay,vec3(1),WaveCrests(pos,fragCoord)); } void mainImage(out vec4 fragColor,vec2 fragCoord) diff --git a/tests/real/controllable-machinery.frag.expected b/tests/real/controllable-machinery.frag.expected index f5353dc7..b99d576e 100644 --- a/tests/real/controllable-machinery.frag.expected +++ b/tests/real/controllable-machinery.frag.expected @@ -40,8 +40,8 @@ vec4 BPos(float t) } float PrBoxDf(vec3 p,vec3 b) { - vec3 d=abs(p)-b; - return min(max(d.x,max(d.y,d.z)),0.)+length(max(d,0.)); + p=abs(p)-b; + return min(max(p.x,max(p.y,p.z)),0.)+length(max(p,0.)); } float PrRoundBoxDf(vec3 p,vec3 b) { diff --git a/tests/real/ed-209.frag.expected b/tests/real/ed-209.frag.expected index 546426ba..9808d77a 100644 --- a/tests/real/ed-209.frag.expected +++ b/tests/real/ed-209.frag.expected @@ -10,8 +10,9 @@ float stretch,gunsUp,gunsForward,edWalk,edTwist,edDown,edShoot,doorOpen,glow; struct MarchData{float d;vec3 mat;float specPower;}; mat2 rot(float a) { - float c=cos(a),s=sin(a); - return mat2(c,s,-s,c); + float c=cos(a); + a=sin(a); + return mat2(c,a,-a,c); } float remap(float f,float in1,float in2,float out1,float out2) { @@ -19,8 +20,8 @@ float remap(float f,float in1,float in2,float out1,float out2) } float sdBox(vec3 p,vec3 b) { - vec3 q=abs(p)-b; - return length(max(q,0.))+min(max(q.x,max(q.y,q.z)),0.); + p=abs(p)-b; + return length(max(p,0.))+min(max(p.x,max(p.y,p.z)),0.); } float sdChamferedCube(vec3 p,vec3 r) { @@ -54,8 +55,9 @@ float sdCappedCylinder(vec3 p,float h,float r) } float sdCapsule(vec3 p,vec3 a,vec3 b) { - vec3 pa=p-a,ba=b-a; - return length(pa-ba*clamp(dot(pa,ba)/dot(ba,ba),0.,1.))-.2; + p-=a; + a=b-a; + return length(p-a*clamp(dot(p,a)/dot(a,a),0.,1.))-.2; } float sdOctogon(vec2 p,float r) { @@ -68,8 +70,9 @@ float sdOctogon(vec2 p,float r) } vec3 getRayDir(vec3 ro,vec3 lookAt,vec2 uv) { - vec3 forward=normalize(lookAt-ro),right=normalize(cross(vec3(0,1,0),forward)); - return normalize(forward+right*uv.x+cross(forward,right)*uv.y); + ro=normalize(lookAt-ro); + lookAt=normalize(cross(vec3(0,1,0),ro)); + return normalize(ro+lookAt*uv.x+cross(ro,lookAt)*uv.y); } MarchData minResult(MarchData a,MarchData b) { @@ -306,11 +309,11 @@ MarchData map(vec3 p) } float calcShadow(vec3 p,vec3 lightPos) { - vec3 rd=normalize(lightPos-p); + lightPos=normalize(lightPos-p); float res=1.,t=.1; for(float i=0.;i25.) @@ -325,17 +328,17 @@ vec3 calcNormal(vec3 p,float t) } vec3 vignette(vec3 col,vec2 fragCoord) { - vec2 q=fragCoord.xy/iResolution.xy; - col*=.5+.5*pow(16.*q.x*q.y*(1.-q.x)*(1.-q.y),.4); + fragCoord=fragCoord.xy/iResolution.xy; + col*=.5+.5*pow(16.*fragCoord.x*fragCoord.y*(1.-fragCoord.x)*(1.-fragCoord.y),.4); return col; } vec3 applyLighting(vec3 p,vec3 rd,float d,MarchData data) { const vec3 sunPos=vec3(10,10,-10); vec3 sunDir=normalize(sunPos-p),n=calcNormal(p,d); - float primary=max(0.,dot(sunDir,n)); - primary*=mix(.2,1.,calcShadow(p,sunPos)); - sunDir=((primary+max(0.,dot(-sunDir,n))*.2)*(map(p+.33*n).d/.33)+pow(max(0.,dot(rd,reflect(sunDir,n))),data.specPower)*2.)*vec3(2,1.6,1.7); + d=max(0.,dot(sunDir,n)); + d*=mix(.2,1.,calcShadow(p,sunPos)); + sunDir=((d+max(0.,dot(-sunDir,n))*.2)*(map(p+.33*n).d/.33)+pow(max(0.,dot(rd,reflect(sunDir,n))),data.specPower)*2.)*vec3(2,1.6,1.7); return mix(data.mat*sunDir,vec3(.01),smoothstep(.7,1.,1.+dot(rd,n)))*exp(-length(p)*.05); } vec3 getSceneColor(vec3 ro,vec3 rd) 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 dfd26597..b14b9181 100644 --- a/tests/real/from-the-seas-to-the-stars.frag.expected +++ b/tests/real/from-the-seas-to-the-stars.frag.expected @@ -12,8 +12,9 @@ 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)),s(c+vec2(1,0)),f.x),mix(s(c+vec2(0,1)),s(c+vec2(1)),f.x),f.y); + vec2 c=floor(p); + p=smoothstep(0.,1.,fract(p)); + return mix(mix(s(c+vec2(0)),s(c+vec2(1,0)),p.x),mix(s(c+vec2(0,1)),s(c+vec2(1)),p.x),p.y); } float fbm(vec2 p) { @@ -49,18 +50,19 @@ float koi(vec2 t) return 1.; if(abs(t.y)>.5) return 1.; - vec2 p=t.xy*1.8,oop=p; - p.y*=.8; - p.x*=.9; - p.y+=pow(clamp(p.x-.7,0,1),2)*.2; - vec2 op=p; - if(p.y<0) - p.y*=mix(1.5,1.6,clamp(p.x,0,1)); - p.y*=mix(1.6,1.,.5+.5*cos(clamp((p.x-.6)*11,0.,6.2831852))); - p.y-=p.x*.02; - p.x=mix(p.x,-.4,pow(clamp((p.x-.6)*1.1,0,1),1.4)); - float d=abs(p.y)+p.x*p.x*.35-.34; - p.x-=.9; + t=t.xy*1.8; + vec2 oop=t; + t.y*=.8; + t.x*=.9; + t.y+=pow(clamp(t.x-.7,0,1),2)*.2; + vec2 op=t; + if(t.y<0) + t.y*=mix(1.5,1.6,clamp(t.x,0,1)); + t.y*=mix(1.6,1.,.5+.5*cos(clamp((t.x-.6)*11,0.,6.2831852))); + t.y-=t.x*.02; + t.x=mix(t.x,-.4,pow(clamp((t.x-.6)*1.1,0,1),1.4)); + float d=abs(t.y)+t.x*t.x*.35-.34; + t.x-=.9; op.y-=.05; op.y=abs(op.y); d=max(d,op.x-pow(op.y/2,.7)-1.1); diff --git a/tests/real/leizex.expected b/tests/real/leizex.expected index 72f84e12..0bf30460 100644 --- a/tests/real/leizex.expected +++ b/tests/real/leizex.expected @@ -17,26 +17,26 @@ _leizex_frag: db 'v=v*(v*v*15731+789221)+1376312589&2147483647;' db 'return float(v);' db '}' - db 'float t(vec3 v,int x)' + db 'float t(vec3 v,int f)' db '{' db 'ivec3 n=ivec3(floor(v));' - db 'vec3 f=fract(v);' - db 'f=f*f*(3.-2.*f);' - db 'int y=n.x+n.y*57+113*n.z+x;' - db 'float s=mix(mix(mix(t(y),t(y+1),f.x),mix(t(y+57),t(y+58),f.x),f.y),mix(mix(t(y+113),t(y+114),f.x),mix(t(y+170),t(y+171),f.x),f.y),f.z);' - db 'return 1.-s*(1./1073741824.);' + db 'vec3 x=fract(v);' + db 'x=x*x*(3.-2.*x);' + db 'f+=n.x+n.y*57+113*n.z;' + db 'float y=mix(mix(mix(t(f),t(f+1),x.x),mix(t(f+57),t(f+58),x.x),x.y),mix(mix(t(f+113),t(f+114),x.x),mix(t(f+170),t(f+171),x.x),x.y),x.z);' + db 'return 1.-y*(1./1073741824.);' db '}' db 'vec2 s(vec3 v)' db '{' db 'ivec3 f=ivec3(floor(v));' - db 'vec3 y=fract(v);' + db 'vec3 x=fract(v);' db 'vec2 n=vec2(1);' - db 'for(int x=-1;x<=1;x++)' + db 'for(int y=-1;y<=1;y++)' db 'for(int i=-1;i<=1;i++)' db 'for(int r=-1;r<=1;r++)' db '{' - db 'int c=f.x+r+57*(f.y+i)+113*(f.z+x);' - db 'vec3 m=vec3(float(r),float(i),float(x))-y+vec3(t(c),t(c+1217),t(c+2513))/2147483647.;' + db 'int c=f.x+r+57*(f.y+i)+113*(f.z+y);' + db 'vec3 m=vec3(float(r),float(i),float(y))-x+vec3(t(c),t(c+1217),t(c+2513))/2147483647.;' db 'float s=dot(m,m);' db 'if(s>8U^C.yzx)*1103515245U;" - "C=(C>>8U^C.yzx)*1103515245U;" - "C=(C>>8U^C.yzx)*1103515245U;" - "return vec3(C)*(1./float(-1U));" + "uvec3 f=uvec3((v+1e2)*1e4);" + "f=(f>>8U^f.yzx)*1103515245U;" + "f=(f>>8U^f.yzx)*1103515245U;" + "f=(f>>8U^f.yzx)*1103515245U;" + "return vec3(f)*(1./float(-1U));" "}" "float B(vec3 v)" "{" - "vec3 C=floor(v),m=fract(v);" - "m=m*m*m*(m*(m*6.-15.)+10.);" - "return mix(mix(mix(h(C+vec3(0)).x,h(C+vec3(1,0,0)).x,m.x),mix(h(C+vec3(0,1,0)).x,h(C+vec3(1,1,0)).x,m.x),m.y),mix(mix(h(C+vec3(0,0,1)).x,h(C+vec3(1,0,1)).x,m.x),mix(h(C+vec3(0,1,1)).x,h(C+vec3(1)).x,m.x),m.y),m.z)*2.-1.;" + "vec3 C=floor(v);" + "v=fract(v);" + "v=v*v*v*(v*(v*6.-15.)+10.);" + "return mix(mix(mix(h(C+vec3(0)).x,h(C+vec3(1,0,0)).x,v.x),mix(h(C+vec3(0,1,0)).x,h(C+vec3(1,1,0)).x,v.x),v.y),mix(mix(h(C+vec3(0,0,1)).x,h(C+vec3(1,0,1)).x,v.x),mix(h(C+vec3(0,1,1)).x,h(C+vec3(1)).x,v.x),v.y),v.z)*2.-1.;" "}" "vec2 A(vec3 v)" "{" "v.xz-=i.xz;" "v.y-=-i.y;" - "float C=v.y+smoothstep(1.,20.,length(v.xz));" - "return C<.4?" - "(C-=pow((B(v*.9)*.5+B(v*1.6)*.3+B(v*2.7)*.1)*.5+.5,3.)*.45*(1.-exp((-t+137.3)*3.)),vec2(C,12)):" + "float y=v.y+smoothstep(1.,20.,length(v.xz));" + "return y<.4?" + "(y-=pow((B(v*.9)*.5+B(v*1.6)*.3+B(v*2.7)*.1)*.5+.5,3.)*.45*(1.-exp((-t+137.3)*3.)),vec2(y,12)):" "vec2(9e7,0);" "}" - "mat3 A(vec3 v,vec3 C)" + "mat3 A(vec3 v,vec3 y)" "{" - "vec3 y=normalize(C-v),m=normalize(cross(y,normalize(vec3(0,1,0))));" - "return mat3(m,cross(m,y),y);" + "v=normalize(y-v);" + "y=normalize(cross(v,normalize(vec3(0,1,0))));" + "return mat3(y,cross(y,v),v);" "}" "mat2 E(float v)" "{" - "float C=cos(v),y=sin(v);" - "return mat2(C,y,-y,C);" + "float C=cos(v);" + "v=sin(v);" + "return mat2(C,v,-v,C);" "}" - "vec3 A(vec3 i,vec2 v,float C)" + "vec3 A(vec3 v,vec2 C,float x)" "{" "vec2 y=vec2(cos(t*.7+2.),sin(t*.7+2.)*.75);" - "float m=smoothstep(.151,.15,length(v-y)),s=m*smoothstep(.13,.2701,length(v-y-vec2(.05))+.004*B(1e2*vec3(v-y,0)));" - "vec2 x=E(t*2e-4)*v*2e2,c=floor(x);" - "vec3 g=h(vec3(abs(c),abs(c.x)));" - "float f=g.z*.06;" - "return vec3(1,.9,.1)*s*smoothstep(.5,-1.,r.y)+smoothstep(f,f*.01,length(fract(x)-.5+(g.xy-.5)))*(1.-m)+exp(-length(v-y)*2.)*.1+pow(C,2.);" + "float m=smoothstep(.151,.15,length(C-y)),s=m*smoothstep(.13,.2701,length(C-y-vec2(.05))+.004*B(1e2*vec3(C-y,0)));" + "vec2 f=E(t*2e-4)*C*2e2,c=floor(f);" + "v=h(vec3(abs(c),abs(c.x)));" + "float U=v.z*.06;" + "return vec3(1,.9,.1)*s*smoothstep(.5,-1.,r.y)+smoothstep(U,U*.01,length(fract(f)-.5+(v.xy-.5)))*(1.-m)+exp(-length(C-y)*2.)*.1+pow(x,2.);" "}" "float B(vec3 v,vec3 C)" "{" - "vec3 m=abs(v)-C;" - "return length(max(m,0.))+min(max(m.x,max(m.y,m.z)),0.);" + "v=abs(v)-C;" + "return length(max(v,0.))+min(max(v.x,max(v.y,v.z)),0.);" "}" "vec2 F(vec3 v)" "{" "v-=i;" "v.xz=E(1.)*v.xz;" - "float C=pow(clamp(v.y-1.,.004,1.),.5);" - "C=B(v-vec3(0,1,0),vec3(1.5-C,1,2.5-C));" - "if(C<10.)" + "float f=pow(clamp(v.y-1.,.004,1.),.5);" + "f=B(v-vec3(0,1,0),vec3(1.5-f,1,2.5-f));" + "if(f<10.)" "{" - "C=min(C,B(v-vec3(0,3,0),vec3(2,1,3)));" - "float f=length((v.yz-vec2(4.5,3))*vec2(1,.8))-2.;" - "f=max(f,abs(v.x)-.5);" - "f=max(f,v.y-3.5);" - "C=min(C,f);" - "return vec2(C-.1,11);" + "f=min(f,B(v-vec3(0,3,0),vec3(2,1,3)));" + "float C=length((v.yz-vec2(4.5,3))*vec2(1,.8))-2.;" + "C=max(C,abs(v.x)-.5);" + "C=max(C,v.y-3.5);" + "f=min(f,C);" + "return vec2(f-.1,11);" "}" "return vec2(9e7,0);" "}" @@ -418,18 +422,19 @@ const char *mouton_frag = "}" "float A(vec3 v,float C,float i,float y)" "{" - "vec2 m=vec2(length(v.xz),v.y),c=vec2(y,C),f=vec2(y-i,2.*C),x=vec2(m.x-min(m.x,m.y<0.?" + "vec2 m=vec2(length(v.xz),v.y),f=vec2(y,C),x=vec2(y-i,2.*C),c=vec2(m.x-min(m.x,m.y<0.?" "i:" "y),abs(m.y)-C);" - "f=m-c+f*clamp(dot(c-m,f)/dot(f,f),0.,1.);" - "return(f.x<0.&&x.y<0.?" + "x=m-f+x*clamp(dot(f-m,x)/dot(x,x),0.,1.);" + "return(x.x<0.&&c.y<0.?" "-1.:" - "1.)*sqrt(min(dot(x,x),dot(f,f)));" + "1.)*sqrt(min(dot(c,c),dot(x,x)));" "}" "float E(vec3 v,vec3 C)" "{" - "vec3 y=v-C,x=vec3(0,2,4.9)-C;" - "return length(y-x*clamp(dot(y,x)/dot(x,x),0.,1.))-.2;" + "v-=C;" + "C=vec3(0,2,4.9)-C;" + "return length(v-C*clamp(dot(v,C)/dot(C,C),0.,1.))-.2;" "}" "float F(vec3 v,vec2 C)" "{" @@ -450,18 +455,18 @@ const char *mouton_frag = "float C=G(m-vec3(0,.3,0),vec3(1,.2+cos(m.x*150.)*sin(m.z*150.)*.05,1)*.25);" "if(C<5.)" "{" - "vec2 i=vec2(C,8);" - "vec3 f=m;" - "float x=U*.2,c=atan(f.z,f.x),s=length(f.xz);" - "c=mod(c-x/2.,x)-x/2.;" - "f.xz=vec2(cos(c),sin(c))*s;" - "float g=G(f-vec3(.5,.2+sin(f.x*2.)*.2,0),vec3(2,.1+sin(f.z*40.)*.02,.75)*.25);" - "if(gf*x?" + "m>c*x?" "length(v-vec2(0,x))-y:" - "dot(v,vec2(f,m))-C;" + "dot(v,vec2(c,f))-C;" "}" "float J(vec2 v,float C)" "{" @@ -636,8 +642,8 @@ const char *mouton_frag = "v.x=abs(v.x);" "v.y-=C;" "f=.5*vec2(-y.y,y)-vec2(0,1);" - "float m=clamp(dot(v,f)/dot(f,f),0.,C);" - "return length(v-f*m)*sign(v.y*f.x-v.x*f.y);" + "float x=clamp(dot(v,f)/dot(f,f),0.,C);" + "return length(v-f*x)*sign(v.y*f.x-v.x*f.y);" "}" "void main()" "{" @@ -675,9 +681,9 @@ const char *mouton_frag = "b=mix(ay*.3,b,smoothstep(0.,.05,abs(q-.3)+.01));" "k=mix(normalize(k+(d+k)*4.),k,smoothstep(.3,.32,q));" "{" - "vec3 am=reflect(s,k),av=normalize(vec3(1,1.5,-1)),ab=vec3(-av.x,av.y*.5,av.z);" - "float ac=h(am,av,.1)+h(am,ab,2.)*.1+h(am,normalize(av+vec3(.2,0,0)),.3)+h(am,normalize(av+vec3(.2,0,.2)),.5)+h(am,normalize(ab+vec3(.1,0,.2)),8.)*.5;" - "R=(mix(mix(vec3(.3,.3,0),vec3(.1),smoothstep(-.7,.2,am.y)),vec3(.3,.65,1),smoothstep(0.,1.,am.y))+ac*vec3(1,.9,.8))*mix(.15,.2,aC)*sqrt(L)*2.5;" + "vec3 av=reflect(s,k),af=normalize(vec3(1,1.5,-1)),ab=vec3(-af.x,af.y*.5,af.z);" + "float ac=h(av,af,.1)+h(av,ab,2.)*.1+h(av,normalize(af+vec3(.2,0,0)),.3)+h(av,normalize(af+vec3(.2,0,.2)),.5)+h(av,normalize(ab+vec3(.1,0,.2)),8.)*.5;" + "R=(mix(mix(vec3(.3,.3,0),vec3(.1),smoothstep(-.7,.2,av.y)),vec3(.3,.65,1),smoothstep(0.,1.,av.y))+ac*vec3(1,.9,.8))*mix(.15,.2,aC)*sqrt(L)*2.5;" "}" "K(i);" "b*=smoothstep(0.,.015,D)*.4+.6;" @@ -785,16 +791,16 @@ const char *fxaa_frag = "void main()" "{" "vec2 v=1./b,f=gl_FragCoord.xy*v;" - "vec4 m=vec4(f,f-v*.5);" - "vec3 y=vec3(.299,.587,.114);" - "float s=dot(textureLod(k,m.zw,0.).xyz,y),x=dot(textureLod(k,m.zw+vec2(1,0)*v.xy,0.).xyz,y),c=dot(textureLod(k,m.zw+vec2(0,1)*v.xy,0.).xyz,y),U=dot(textureLod(k,m.zw+vec2(1)*v.xy,0.).xyz,y),d=dot(textureLod(k,m.xy,0.).xyz,y),e=min(d,min(min(s,x),min(c,U)));" - "d=max(d,max(max(s,x),max(c,U)));" - "f=vec2(-s-x+c+U,s+c-x-U);" - "s=1./(min(abs(f.x),abs(f.y))+1./128.);" - "f=min(vec2(8),max(vec2(-8),f*s))*v.xy;" - "vec3 i=.5*(textureLod(k,m.xy+f*(1./3.-.5),0.).xyz+textureLod(k,m.xy+f*(2./3.-.5),0.).xyz),a=i*.5+.25*(textureLod(k,m.xy+f*-.5,0.).xyz+textureLod(k,m.xy+f*.5,0.).xyz);" - "s=dot(a,y);" - "C=sd?" + "vec4 y=vec4(f,f-v*.5);" + "vec3 x=vec3(.299,.587,.114);" + "float m=dot(textureLod(k,y.zw,0.).xyz,x),c=dot(textureLod(k,y.zw+vec2(1,0)*v.xy,0.).xyz,x),s=dot(textureLod(k,y.zw+vec2(0,1)*v.xy,0.).xyz,x),U=dot(textureLod(k,y.zw+vec2(1)*v.xy,0.).xyz,x),d=dot(textureLod(k,y.xy,0.).xyz,x),e=min(d,min(min(m,c),min(s,U)));" + "d=max(d,max(max(m,c),max(s,U)));" + "f=vec2(-m-c+s+U,m+s-c-U);" + "m=1./(min(abs(f.x),abs(f.y))+1./128.);" + "f=min(vec2(8),max(vec2(-8),f*m))*v.xy;" + "vec3 i=.5*(textureLod(k,y.xy+f*(1./3.-.5),0.).xyz+textureLod(k,y.xy+f*(2./3.-.5),0.).xyz),a=i*.5+.25*(textureLod(k,y.xy+f*-.5,0.).xyz+textureLod(k,y.xy+f*.5,0.).xyz);" + "m=dot(a,x);" + "C=md?" "vec4(i,1):" "vec4(a,1);" "}"; diff --git a/tests/real/ohanami.frag.expected b/tests/real/ohanami.frag.expected index 50040245..fe9932c5 100644 --- a/tests/real/ohanami.frag.expected +++ b/tests/real/ohanami.frag.expected @@ -32,8 +32,9 @@ 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)),noise(c+vec2(1,0)),f.x),mix(noise(c+vec2(0,1)),noise(c+vec2(1)),f.x),f.y); + vec2 c=floor(p); + p=smoothstep(0.,1,fract(p)); + return mix(mix(noise(c+vec2(0)),noise(c+vec2(1,0)),p.x),mix(noise(c+vec2(0,1)),noise(c+vec2(1)),p.x),p.y); } vec4 samp(vec2 p) { diff --git a/tests/real/orchard.frag.expected b/tests/real/orchard.frag.expected index 7be53637..3c727409 100644 --- a/tests/real/orchard.frag.expected +++ b/tests/real/orchard.frag.expected @@ -58,12 +58,12 @@ vec3 texCube(sampler2D sam,vec3 p,vec3 n) vec4 grassTexture(vec3 pos,vec3 nor) { float g=texture(iChannel1,pos.xz*.5).x,s=texture(iChannel1,pos.xz*.015).x*.2; - vec3 flower=texture(iChannel2,pos.xz*.15).xyz; + nor=texture(iChannel2,pos.xz*.15).xyz; float rand=texture(iChannel1,pos.xz*.003).x; rand*=rand*rand; - flower=pow(flower,vec3(8,15,5))*10.*rand; + nor=pow(nor,vec3(8,15,5))*10.*rand; vec4 mat=vec4(g*.05+s,g*.65,0,g*.1); - mat.xyz+=flower; + mat.xyz+=nor; pos=fract(pos); mat=mix(mat,vec4(.2,0,0,0),smoothstep(.05,0.,min(pos.x,pos.z))+smoothstep(.95,1.,max(pos.x,pos.z))); return min(mat,1.); @@ -77,15 +77,15 @@ vec4 barkTexture(vec3 p,vec3 nor) } vec4 leavesTexture(vec3 p,vec3 nor) { - vec3 rand=texCube(iChannel2,p*.15,nor); - return vec4(vec3(.4,1.2,0)*rand,0); + p=texCube(iChannel2,p*.15,nor); + return vec4(vec3(.4,1.2,0)*p,0); } vec4 fruitTexture(vec3 p,vec3 nor,float i) { float rand=texCube(iChannel2,p*.1,nor).x; - vec3 mat=vec3(1,abs(dot(nor,normalize(vec3(.8,.1,.1))))*rand,0); - mat=mix(vec3(0,1,0),mat,i/10.); - return vec4(mat,.5); + p=vec3(1,abs(dot(nor,normalize(vec3(.8,.1,.1))))*rand,0); + p=mix(vec3(0,1,0),p,i/10.); + return vec4(p,.5); } float distanceRayPoint(vec3 ro,vec3 rd,vec3 p,out float h) { @@ -130,8 +130,8 @@ vec4 getClouds(vec3 pos,vec3 dir) vec2 p=pos.xz+dir.xz*(4e3/dir.y); float r=findClouds2D(p),t=findClouds2D(p+normalize(sunLight.xz)*30.); t=sqrt(max((r-t)*20.,.2))*2.; - vec3 col=vec3(t)*SUN_COLOUR; - return vec4(col,r); + pos=vec3(t)*SUN_COLOUR; + return vec4(pos,r); } vec2 map(vec3 p,float t) { @@ -146,18 +146,18 @@ vec2 map(vec3 p,float t) p/=3.5; vec3 q=p; int it=10-int(min(t*.03,9.)); - float h=hash11(num*7.)*.3+.3; + t=hash11(num*7.)*.3+.3; vec3 uwc=normalize(cross(u,w)); int dontFold=int(hash11(num*23.)*9.)+3; - float thick=.2/(h-.24); + float thick=.2/(t-.24); for(int i=0;i.01)" - "i+=mix(.02,pow(max(0.,v(y.xy-vec2(0,.25+pow(abs(y.x),3.)),vec2(.32,.94))-.05)*1e2,1.3)/1500.,1.-s(-.001,d));" + "f+=mix(.02,pow(max(0.,v(y.xy-vec2(0,.25+pow(abs(y.x),3.)),vec2(.32,.94))-.05)*1e2,1.3)/1500.,1.-s(-.001,d));" "if(y.z<0.)" "z=v(z,m(y-vec3(0,0,-.1),vec3(.5,.5,.055))-.04,64.);" "a.x-=.09;" "a.y+=.635;" - "f=m(f,i,192.);" - "i=f;" + "i=m(i,f,192.);" + "f=i;" "float c=.001+v(length(a)-.01,v(length(a-vec2(.036,0))-.01,length(a-vec2(-.036,0))-.01,40.),40.);" - "f=m(f,-y.z-.09,64.);" + "i=m(i,-y.z-.09,64.);" "if(y.z<0.)" - "f=v(f,m(i+.03,z,64.),256.);" - "i=1.;" - "f=m(m(f,min(y.z,-c+.003),256.),min(y.z,-abs(d)-.001),448.);" - "f=m(f,min(y.z-.13,-v(y.xy-vec2(0,-.118))),512.);" - "f=m(f,min(y.z-.13,-abs(length(vec2(max(abs(y.x)-.12,0.),y.y+.117))-.035)+.003),512.);" - "f=m(m(f,min(1.,-min(1.,max(m(abs(y.x)-.31,abs(y.y-.49)-.29,5e2),abs(y.z-.185)-.1))+.004),576.),-abs(y.z-.01)-1e-4,384.);" + "i=v(i,m(f+.03,z,64.),256.);" + "f=1.;" + "i=m(m(i,min(y.z,-c+.003),256.),min(y.z,-abs(d)-.001),448.);" + "i=m(i,min(y.z-.13,-v(y.xy-vec2(0,-.118))),512.);" + "i=m(i,min(y.z-.13,-abs(length(vec2(max(abs(y.x)-.12,0.),y.y+.117))-.035)+.003),512.);" + "i=m(m(i,min(1.,-min(1.,max(m(abs(y.x)-.31,abs(y.y-.49)-.29,5e2),abs(y.z-.185)-.1))+.004),576.),-abs(y.z-.01)-1e-4,384.);" "if(y.y<0.)" "{" "if(y.y<-.5)" - "f+=(1.-smoothstep(-.001,.002,x(y+vec3(0,.03,-.2),true)))*.005,f=m(f,-x(y+vec3(0,0,-.2),false),1024.);" + "i+=(1.-smoothstep(-.001,.002,x(y+vec3(0,.03,-.2),true)))*.005,i=m(i,-x(y+vec3(0,0,-.2),false),1024.);" "vec2 k=vec2(-.32,-.314),p=vec2(.124,.033);" "float b=length(y.xy-vec2(.405,-.28))-.07,o=length(y.xy-vec2(.202,-.345))-.07,e=v(v(y.xy-k,p)-.005,v(y.xy-k,p.yx)-.005,512.);" - "f=m(f,min(y.z,-b+.004),320.);" - "f=m(f,min(y.z,-o+.004),320.);" - "f=m(f,min(y.z,-min(1.,e)+.006),192.);" + "i=m(i,min(y.z,-b+.004),320.);" + "i=m(i,min(y.z,-o+.004),320.);" + "i=m(i,min(y.z,-min(1.,e)+.006),192.);" "y.xy-=vec2(.405,-.28);" "float g=(1.-s(.002,min(min(n(y.xy,vec2(0,-.035),vec2(-.014,.024)),n(y.xy,vec2(-.03,-.035),vec2(-.014,.024))),n(y.xy,vec2(-.02),vec2(-.01,-.02)))-.0025))*.002;" "y.xy+=vec2(.405,-.28);" @@ -163,42 +162,43 @@ const char *the_real_party_is_in_your_pocket_frag = "a=vec2(a.x-C*a.y,-C*a.x-a.y)/2.;" "a.x-=clamp(a.x,-2.,0.);" "t=m(t,-max(-w.z,-length(a)*sign(a.y)+.565),512.);" - "if(t0.&&w.xw.y||abs(d)<1e-5)" "break;" @@ -214,27 +214,27 @@ const char *the_real_party_is_in_your_pocket_frag = "}" "if(r0.)" - "return l=f,a;" + "if(l.x>0.)" + "return i=a,l;" "m=-1.;" - "vec2 i=vec2(-1,-3);" - "float x=(i.x-y.y)/v.y,d=(i.y-y.z)/v.z;" - "vec2 w=h((y-vec3(0,i+2.))*vec3(0,.5,.5),v*vec3(0,.5,.5));" - "i=y.yz+v.yz*w.y-i-2.;" - "if(w.x0.&&i.x<0.&&i.y<0.)" - "l=vec3(-i*.5,0).zxy,m=w.y;" - "else if(x>0.&&(x0.&&(d5.||abs(f.y)>2.5||abs(f.z)>3.3?" + "vec2 x=vec2(-1,-3);" + "float f=(x.x-y.y)/v.y,d=(x.y-y.z)/v.z;" + "vec2 w=h((y-vec3(0,x+2.))*vec3(0,.5,.5),v*vec3(0,.5,.5));" + "x=y.yz+v.yz*w.y-x-2.;" + "if(w.x0.&&x.x<0.&&x.y<0.)" + "i=vec3(-x*.5,0).zxy,m=w.y;" + "else if(f>0.&&(f0.&&(d5.||abs(a.y)>2.5||abs(a.z)>3.3?" "vec4(-1):" "vec4(m,0,0,0);" "}" @@ -245,36 +245,36 @@ const char *the_real_party_is_in_your_pocket_frag = "}" "vec3 h(vec3 y)" "{" - "vec3 v=vec3(0,0,4.8),f=vec3(0),a=vec3(1);" + "vec3 v=vec3(0,0,4.8),i=vec3(0),a=vec3(1);" "bool l=false;" - "for(int i=0;i<4;++i)" + "for(int x=0;x<4;++x)" "{" "y=normalize(y);" - "vec3 x;" - "vec4 w=h(v,y,x);" + "vec3 f;" + "vec4 w=h(v,y,f);" "float d=w.x;" "if(d<0.)" "{" - "f+=max(a*vec3(.9)*(.5+.5*dot(y,normalize(vec3(-2,4,1))))*.4*1.3*vec3(1,.9,.65)+a*vec3(.9)*step(.9,dot(y,normalize(vec3(6,2,1))))*10.4*2.3+a*vec3(.9)*step(.95,dot(y,normalize(vec3(-2,1,3))))*10.4*1.5,0.)*pow(max(.5+.5*-y.z,0.),.2);" + "i+=max(a*vec3(.9)*(.5+.5*dot(y,normalize(vec3(-2,4,1))))*.4*1.3*vec3(1,.9,.65)+a*vec3(.9)*step(.9,dot(y,normalize(vec3(6,2,1))))*10.4*2.3+a*vec3(.9)*step(.95,dot(y,normalize(vec3(-2,1,3))))*10.4*1.5,0.)*pow(max(.5+.5*-y.z,0.),.2);" "break;" "}" "float r=floor(w.y/8.);" "w.y=mod(w.y,8.);" "vec3 e=v+y*d;" "vec2 c=vec2(6.28319*h(),h()*2.-1.);" - "vec3 k=x+vec3(sqrt(1.-c.y*c.y)*vec2(cos(c.x),sin(c.x)),c.y);" + "vec3 k=f+vec3(sqrt(1.-c.y*c.y)*vec2(cos(c.x),sin(c.x)),c.y);" "float z=w.y>1.5?" "mix(e.x>0.?" ".1:" - ".05,.5,pow(1.-clamp(dot(-y,x),0.,1.),2.)):" - "mix(.2,.8,pow(1.-clamp(dot(-y,x),0.,1.),1.5));" + ".05,.5,pow(1.-clamp(dot(-y,f),0.,1.),2.)):" + "mix(.2,.8,pow(1.-clamp(dot(-y,f),0.,1.),1.5));" "if(w.y>5.5&&w.y<6.5)" "{" "if(l)" "break;" - "v=e+x*2e-4;" - "a*=mix(.1,.8,pow(1.-clamp(dot(-y,x),0.,1.),3.))*mix(vec3(1),vec3(1,.9,.5),.5);" - "y=reflect(y,x)+(vec3(h(),h(),h())-.5)*.3;" + "v=e+f*2e-4;" + "a*=mix(.1,.8,pow(1.-clamp(dot(-y,f),0.,1.),3.))*mix(vec3(1),vec3(1,.9,.5),.5);" + "y=reflect(y,f)+(vec3(h(),h(),h())-.5)*.3;" "}" "else if(w.y>4.5&&w.y<5.5)" "{" @@ -282,9 +282,9 @@ const char *the_real_party_is_in_your_pocket_frag = "break;" "z=.02;" "if(h()>z)" - "v=e-x*2e-4*-sign(y.z),y+=(vec3(h(),h(),h())-.5)*.1;" + "v=e-f*2e-4*-sign(y.z),y+=(vec3(h(),h(),h())-.5)*.1;" "else" - " v=e+x*2e-4,y=reflect(y,x)+(vec3(h(),h(),h())-.5)*.3;" + " v=e+f*2e-4,y=reflect(y,f)+(vec3(h(),h(),h())-.5)*.3;" "}" "else if(w.y<.5||w.y>2.5)" "{" @@ -315,7 +315,7 @@ const char *the_real_party_is_in_your_pocket_frag = "t=mix(t,vec3(.3),step(b-.0018,0.));" "}" "if(length(w.zw-vec2(-.43,.605))<.014)" - "f+=a*3.*vec3(1,.01,.01)*(.8+8.*(1.-s(.01,length(w.zw-vec2(-.43,.605)))));" + "i+=a*3.*vec3(1,.01,.01)*(.8+8.*(1.-s(.01,length(w.zw-vec2(-.43,.605)))));" "if(w.w>0.&&e.y<.2)" "{" "t=mix(t,vec3(.3),step(n(w.zw-vec2(0,.103)),0.));" @@ -358,7 +358,7 @@ const char *the_real_party_is_in_your_pocket_frag = "C=mix(C,vec3(1),step(D,0.));" "C*=vec3(1,1,.8);" "C*=smoothstep(.1,.2,o.x)*smoothstep(.1,.2,o.y);" - "f+=a*3.*C;" + "i+=a*3.*C;" "t=vec3(.1);" "}" "}" @@ -367,7 +367,7 @@ const char *the_real_party_is_in_your_pocket_frag = "if(w.y>3.&&w.y<3.2)" "t=vec3(.4);" "a*=t;" - "v=e+x*2e-4;" + "v=e+f*2e-4;" "y=k;" "l=true;" "}" @@ -383,7 +383,7 @@ const char *the_real_party_is_in_your_pocket_frag = "if(w.y>1.5)" "p=vec3(.02);" "a*=p;" - "v=e+x*2e-4;" + "v=e+f*2e-4;" "y=k;" "l=true;" "}" @@ -394,29 +394,29 @@ const char *the_real_party_is_in_your_pocket_frag = "a*=w.y>1.5?" "vec3(.5):" ".9*mix(p,vec3(1),.25);" - "v=e+x*2e-4;" - "y=reflect(y,x)+(vec3(h(),h(),h())-.5)*.4;" + "v=e+f*2e-4;" + "y=reflect(y,f)+(vec3(h(),h(),h())-.5)*.4;" "}" "}" "if(max(a.x,max(a.y,a.z))<.001)" "break;" "}" - "return f;" + "return i;" "}" "void main()" "{" - "vec3 m=vec3(0);" - "for(int v=0;v<16;++v)" + "vec3 v=vec3(0);" + "for(int m=0;m<16;++m)" "{" - "y=float(v)+gl_TexCoord[0].x*16.;" + "y=float(m)+gl_TexCoord[0].x*16.;" "float l=h()*6.28319;" "vec2 a=vec2(cos(l),sin(l))*sqrt(1.-sqrt(1.-h()))*1.2;" "if(int(y)%4==0)" "a*=15.*length(a);" - "m+=h(vec3((gl_FragCoord.xy-vec2(960,540)+a)/540.,-3.5))/48.;" + "v+=h(vec3((gl_FragCoord.xy-vec2(960,540)+a)/540.,-3.5))/48.;" "}" - "m/=(m+1.)/2.;" - "gl_FragColor.xyz=pow(m+.01*vec3(1,1,.5),vec3(1./2.2))+h()/1e2;" + "v/=(v+1.)/2.;" + "gl_FragColor.xyz=pow(v+.01*vec3(1,1,.5),vec3(1./2.2))+h()/1e2;" "}"; #endif // THE_REAL_PARTY_IS_IN_YOUR_POCKET_FRAG_EXPECTED_ diff --git a/tests/real/valley_ball.glsl.expected b/tests/real/valley_ball.glsl.expected index 9ec05d31..b2ff687f 100644 --- a/tests/real/valley_ball.glsl.expected +++ b/tests/real/valley_ball.glsl.expected @@ -26,8 +26,8 @@ float norm(float x) } float caustic(float u,float v,float t) { - float a=(norm(sin(pi*2.*(u+v+Y.y*t)))+norm(sin(pi*(v-u-Y.y*t)))+norm(sin(pi*(v+Y.y*t)))+norm(sin(pi*3.*(u-Y.y*t))))*.3; - return pow(a,2.); + u=(norm(sin(pi*2.*(u+v+Y.y*t)))+norm(sin(pi*(v-u-Y.y*t)))+norm(sin(pi*(v+Y.y*t)))+norm(sin(pi*3.*(u-Y.y*t))))*.3; + return pow(u,2.); } vec3 pp(vec3 color) { @@ -64,10 +64,11 @@ vec3 getWaterNormal(vec3 p) } vec3 shadeTerrain(vec3 p,vec3 rd) { - vec3 n=getTerrainNormal(p),color=mix(vec3(.66,.55,.4)-.2*smoothrnd(abs(p.xz*150.))-.2*smoothrnd(abs(p.yy+.002*smoothrnd(abs(p.xz*150.)))*3e3),vec3(.1,.3,0)*(smoothrnd(p.xz*7e3)*.4+.5),clamp(n.y*(caustic(p.x*111.,p.z*111.,0.)*.5-p.y*40.),0.,1.)); + rd=getTerrainNormal(p); + vec3 color=mix(vec3(.66,.55,.4)-.2*smoothrnd(abs(p.xz*150.))-.2*smoothrnd(abs(p.yy+.002*smoothrnd(abs(p.xz*150.)))*3e3),vec3(.1,.3,0)*(smoothrnd(p.xz*7e3)*.4+.5),clamp(rd.y*(caustic(p.x*111.,p.z*111.,0.)*.5-p.y*40.),0.,1.)); if(p.y<=0.) color+=5.*getWaterNormal(.8*p).x*min(.3,-p.y*8.); - return diffuseLight(color,n); + return diffuseLight(color,rd); } vec3 shadeSky(vec3 ro,vec3 rd) { @@ -88,12 +89,11 @@ float traceTerrain(vec3 ro,vec3 rd,float maxt) { float delt,lh,ly,samplePosY; delt=0.; - vec3 samplePos=ro; for(float t=0.;t0.) return 9.; - D=B*B-dot(dst,dst)+sphereRadius*sphereRadius; + D=B*B-dot(ro,ro)+sphereRadius*sphereRadius; return D>0.? -B-sqrt(D): 9.; @@ -137,13 +137,13 @@ vec4 traceRay(vec3 ro,vec3 rd,int ignore) minDist=min(terrain,min(water,min(attractor,9.))); if(minDist==9.) return vec4(0); - vec3 hitPos=ro+rd*minDist; + ro+=rd*minDist; if(minDist==terrain) - return vec4(hitPos,1); + return vec4(ro,1); if(minDist==water) - return vec4(hitPos,2); + return vec4(ro,2); if(minDist==attractor) - return vec4(hitPos,3); + return vec4(ro,3); } vec3 shade(vec4 hitPoint,vec3 newRo,vec3 rd) { diff --git a/tests/real/yx_long_way_from_home.frag.expected b/tests/real/yx_long_way_from_home.frag.expected index d31c65b8..2942bd99 100644 --- a/tests/real/yx_long_way_from_home.frag.expected +++ b/tests/real/yx_long_way_from_home.frag.expected @@ -13,8 +13,9 @@ const char *yx_long_way_from_home_frag = "uniform sampler2D v;" "mat2 n(float v)" "{" - "float x=cos(v),l=sin(v);" - "return mat2(x,-l,l,x);" + "float x=cos(v);" + "v=sin(v);" + "return mat2(x,-v,v,x);" "}" "vec2 s(const float v)" "{" @@ -74,43 +75,45 @@ const char *yx_long_way_from_home_frag = "}" "vec3 m(vec3 v)" "{" + "float x=1.;" "v=normalize(v);" - "vec3 y=normalize(c(v)),x=normalize(cross(v,y));" - "vec2 f=i;" - "f.x=f.x*2.*pi;" - "f.y=pow(f.y,.5);" - "float z=sqrt(1.-f.y*f.y);" - "return cos(f.x)*z*y+sin(f.x)*z*x+f.y*v;" + "vec3 y=normalize(c(v)),z=normalize(cross(v,y));" + "vec2 s=i;" + "s.x=s.x*2.*pi;" + "s.y=pow(s.y,1./(x+1.));" + "x=sqrt(1.-s.y*s.y);" + "return cos(s.x)*x*y+sin(s.x)*x*z+s.y*v;" "}" - "bool c(vec3 v,vec3 x,out vec3 y,out vec3 i,out float f)" + "bool c(vec3 v,vec3 x,out vec3 y,out vec3 i,out float s)" "{" - "float z=0.;" + "float f=0.;" "for(int c=0;c<100;++c)" "{" - "f=t(v+x*z);" - "z+=f;" - "if(abs(f)<.004)" + "s=t(v+x*f);" + "f+=s;" + "if(abs(s)<.004)" "break;" "}" - "y=v+x*z;" - "if(abs(f)<.004)" + "y=v+x*f;" + "if(abs(s)<.004)" "{" "vec2 c=vec2(.001,0);" - "f=t(y);" - "i=normalize(vec3(t(y+c.xyy)-f,t(y+c.yxy)-f,t(y+c.yyx)-f));" + "s=t(y);" + "i=normalize(vec3(t(y+c.xyy)-s,t(y+c.yxy)-s,t(y+c.yyx)-s));" "return true;" "}" "return false;" "}" "vec3 h(vec3 v)" "{" + "float x=1e-4;" "v=normalize(v);" - "vec3 y=normalize(c(v)),x=normalize(cross(v,y));" - "vec2 f=i;" - "f.x=f.x*2.*pi;" - "f.y=1.-f.y*1e-4;" - "float z=sqrt(1.-f.y*f.y);" - "return cos(f.x)*z*y+sin(f.x)*z*x+f.y*v;" + "vec3 y=normalize(c(v)),z=normalize(cross(v,y));" + "vec2 s=i;" + "s.x=s.x*2.*pi;" + "s.y=1.-s.y*x;" + "x=sqrt(1.-s.y*s.y);" + "return cos(s.x)*x*y+sin(s.x)*x*z+s.y*v;" "}" "vec3 c(vec3 v,vec3 x)" "{" @@ -160,24 +163,24 @@ const char *yx_long_way_from_home_frag = "void main()" "{" "vec2 v=gl_FragCoord.xy/iResolution.xy-.5;" - "float f=iTime+(v.x+iResolution.x*v.y)*1.51269341231;" - "i=s(f);" + "float x=iTime+(v.x+iResolution.x*v.y)*1.51269341231;" + "i=s(x);" "v+=(i-.5)/iResolution.xy;" "v.x*=iResolution.x/iResolution.y;" - "const vec3 x=vec3(-4,2,3),y=vec3(0);" - "const vec2 z=vec2(1,2)*.015;" + "const vec3 y=vec3(-4,2,3),z=vec3(0);" + "const vec2 f=vec2(1,2)*.015;" "vec3 r=vec3(0),o=normalize(vec3(v,2));" "v=c();" - "r.xy+=v*z;" - "o.xy-=v*z*o.z/distance(x,y);" - "vec3 l=y-x;" - "f=-atan(l.y,length(l.xz));" - "float a=-atan(l.x,l.z);" - "r.yz*=n(f);" - "o.yz*=n(f);" - "r.xz*=n(a);" - "o.xz*=n(a);" - "r+=x;" + "r.xy+=v*f;" + "o.xy-=v*f*o.z/distance(y,z);" + "vec3 a=z-y;" + "x=-atan(a.y,length(a.xz));" + "float e=-atan(a.x,a.z);" + "r.yz*=n(x);" + "o.yz*=n(x);" + "r.xz*=n(e);" + "o.xz*=n(e);" + "r+=y;" "vec4 B=vec4(c(r,o),1);" "gl_FragColor=!isnan(B.x)&&B.x>=0.?" "B:" diff --git a/tests/unit/inout.expected b/tests/unit/inout.expected index 93d53a0b..043cef7d 100644 --- a/tests/unit/inout.expected +++ b/tests/unit/inout.expected @@ -2,13 +2,13 @@ #ifndef SHADER_MINIFIER_IMPL #ifndef SHADER_MINIFIER_HEADER # define SHADER_MINIFIER_HEADER -# define VAR_ambientLight "r" -# define VAR_diffuseColor "c" -# define VAR_emissiveColor "p" -# define VAR_fragmentColor "f" -# define VAR_mediumDensity "s" +# define VAR_ambientLight "c" +# define VAR_diffuseColor "p" +# define VAR_emissiveColor "z" +# define VAR_fragmentColor "r" +# define VAR_mediumDensity "f" # define VAR_normal "m" -# define VAR_specularColor "i" +# define VAR_specularColor "t" # define VAR_texture0 "v" # define VAR_viewVec "d" #endif @@ -19,47 +19,47 @@ "#version 330\n" "uniform samplerCube v;" "in vec3 m,d;" - "out vec4 f;" + "out vec4 r;" "void main()" "{" - "vec3 c=normalize(d),r=normalize(m);" - "f=vec4(mix(vec3(.1,.2,.3)*texture(v,refract(-c,r,1./1.5)).xyz,texture(v,reflect(-c,r)).xyz,.1),1);" + "vec3 c=normalize(d),f=normalize(m);" + "r=vec4(mix(vec3(.1,.2,.3)*texture(v,refract(-c,f,1./1.5)).xyz,texture(v,reflect(-c,f)).xyz,.1),1);" "}" - "vec3 e(vec3 v,vec3 m,vec3 r)" + "vec3 e(vec3 v,vec3 m,vec3 f)" "{" - "float f=1.-clamp(dot(m,r),0.,1.);" - "return f*f*f*f*f*(1.-v)+v;" + "float r=1.-clamp(dot(m,f),0.,1.);" + "return r*r*r*r*r*(1.-v)+v;" "}" - "vec3 e(vec3 v,vec3 m,vec3 r,vec3 c,vec3 d,float f)" + "vec3 e(vec3 v,vec3 m,vec3 f,vec3 c,vec3 d,float r)" "{" - "vec3 p=normalize(v+m),i;" - "float s=1.+2048.*(1.-f)*(1.-f);" - "i=e(d,v,p);" - "return mix(c,vec3(pow(clamp(dot(p,r),0.,1.),s)*(s+4.)/8.),i);" + "m=normalize(v+m);" + "r=1.+2048.*(1.-r)*(1.-r);" + "v=e(d,v,m);" + "return mix(c,vec3(pow(clamp(dot(m,f),0.,1.),r)*(r+4.)/8.),v);" "}", // tests/unit/inout2.frag "#version 330\n" "uniform samplerCube v;" - "uniform float s;" - "uniform vec3 r,c,p,i;" + "uniform float f;" + "uniform vec3 c,p,z,t;" "in vec3 m,d;" - "out vec4 f;" - "vec3 e(vec3 v,vec3 m,vec3 r)" + "out vec4 r;" + "vec3 e(vec3 v,vec3 m,vec3 f)" "{" - "float f=1.-clamp(dot(m,r),0.,1.);" - "return f*f*f*f*f*(1.-v)+v;" + "float r=1.-clamp(dot(m,f),0.,1.);" + "return r*r*r*r*r*(1.-v)+v;" "}" "void main()" "{" - "f=vec4(p+mix(c*r,r,.5),1);" + "r=vec4(z+mix(p*c,c,.5),1);" "}" - "vec3 e(vec3 v,vec3 m,vec3 r,vec3 c,vec3 d,float f)" + "vec3 e(vec3 v,vec3 m,vec3 f,vec3 c,vec3 d,float r)" "{" - "vec3 p=normalize(v+m),i;" - "float s=1.+2048.*(1.-f)*(1.-f);" - "i=e(d,v,p);" - "return mix(c,vec3(pow(clamp(dot(p,r),0.,1.),s)*(s+4.)/8.),i);" + "m=normalize(v+m);" + "r=1.+2048.*(1.-r)*(1.-r);" + "v=e(d,v,m);" + "return mix(c,vec3(pow(clamp(dot(m,f),0.,1.),r)*(r+4.)/8.),v);" "}", #endif diff --git a/tests/unit/many_variables.expected b/tests/unit/many_variables.expected index 2c6b4639..2994d8d9 100644 --- a/tests/unit/many_variables.expected +++ b/tests/unit/many_variables.expected @@ -7,26 +7,22 @@ #else // if SHADER_MINIFIER_IMPL // tests/unit/many_variables.frag - "int L(float L,float K,float J,float I,float H,float G,float F,float E,float D,float C,float B,float A)" + "int L(float i,float L,float J,float I,float H,float G,float F,float E,float D,float C,float B,float A)" "{" - "float i=L,f=K,r=J;" - "r=I;" - "f=H;" "i=G;" - "int M=1,N=2,O=3,P=4;" - "P=5;" - "O=6;" - "N=7;" - "M=8;" - "i=D;" - "i=C;" - "float Q=B,R=A;" - "int S=1,T=2,U=3,V=4,W=5,X=6,Y=7,Z=8;" - "float _=0.;" - "int a=1,b=2,c=3,d=4,e=5,g=6,h=7,j=8;" - "float k=0.;" - "int l=1,m=2,n=3,o=4,p=5,q=6,s=7,t=8;" - "return M+Z+j+t;" + "int t=1,f=2,r=3,K=4;" + "K=5;" + "r=6;" + "f=7;" + "t=8;" + "i=F;" + "float M=E,N=D,O=C,P=B,Q=A;" + "int R=1,S=2,T=3,U=4,V=5,W=6,X=7,Y=8;" + "float Z=0.;" + "int _=1,a=2,b=3,c=4,d=5,e=6,g=7,h=8;" + "float j=0.;" + "int k=1,l=2,m=3,n=4,o=5,p=6,q=7,s=8;" + "return t+Y+h+s;" "}", #endif diff --git a/tests/unit/operators.expected b/tests/unit/operators.expected index a61946e4..bd426f1c 100644 --- a/tests/unit/operators.expected +++ b/tests/unit/operators.expected @@ -29,8 +29,8 @@ "}" "float f(float x)" "{" - "float a=(x+=1.,length(vec3(x)));" - "return a*a*sin((a*=a,a));" + "x=(x+=1.,length(vec3(x)));" + "return x*x*sin((x*=x,x));" "}", #endif diff --git a/tests/unit/shadowing.frag.expected b/tests/unit/shadowing.frag.expected index 88c10734..a06cdec4 100644 --- a/tests/unit/shadowing.frag.expected +++ b/tests/unit/shadowing.frag.expected @@ -1,8 +1,8 @@ float f(float g) { - float f=0.; - f+=f+++++f; - return f; + g=0.; + g+=g+++++g; + return g; } float g(float f) { diff --git a/tests/unit/simplify.expected b/tests/unit/simplify.expected index 92b4fdf6..8617beb0 100644 --- a/tests/unit/simplify.expected +++ b/tests/unit/simplify.expected @@ -11,16 +11,16 @@ float bar(float x) b+=x; float arr[2]=float[2](7.,8.); arr=float[2](5.,float(arr.length())); - float m=(a*=10,a*=20,a*=30,b++,i++,58.); - return a+b+m; + x=(a*=10,a*=20,a*=30,b++,i++,58.); + return a+b+x; } float baz(float a) { float b=a+4.; b+=sin(a); - float c=b+5.; - c+=sin(b); - return-c-c+c; + a=b+5.; + a+=sin(b); + return-a-a+a; } out vec3 output; void notMain(float x)