Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make WGSL more suitable for minification #3503

@munrocket

Description

@munrocket

There is some culture for shader minification, some people even write shaders in twits.
We can take one example for analysis: https://twitter.com/gaziya5/status/1365834701215203329

GLSL->WGSL example

GLSL before porting (267 chars)

float i,g,R,e=1.;vec3 p;for(;e>.001&&i++<1e2;g+=e=.7*length(p-clamp(p,-.4,.4))*R-
snoise3D(p*5.)*.005)p=vec3((FC.xy-.5*r)/r.y*g+.2*sin(t),g-3.),p.yz*=rotate2D(4.),
R=length(p.xy),p.y=atan(p.y,p.x),p.x=log(R)-p.y/3.,p.xy=fract(p.xy*3./PI-t*.3)-.5,
p.z/=R;o+=15.*p.z/i;

WGSL after porting (386 chars, +44%)

var i:f32;var g:f32;var R:f32;var e=1.;var p:v3f;for(;e>.001&&i<1e2;g+=e){i+=1.;p=
v3f((FC.xy- .5*r)/r.y*g+v2f(.2*sin(t)),g- 3.);let q=rotate2D(4.);p=v3f(p.x,dot(p.yz,
q[0]),dot(p.yz,q[1]));R=length(p.xy);p.y=atan2(p.y,p.x);p.x=log(R)-p.y/3.;let w=fract(
p.xy*3./PI-t*.3)-v2f(.5);p.x=w.x;p.y=w.y;p.z/=R;o+=v4f(15.*p.z/i);e=.7*length(p-
clamp(p,v3f(-.4),v3f(.4)))*R-snoise3D(p*5.)*.005;}

WGSL with proposed fixes 1+2+8 (303 chars, +13%)

var i,g,R,e:f32,p:v3f;e=1.;for(;e>.001&&i<1e2;g+=e){i+=1.;p=v3f((FC.xy-.5*r)
/r.y*g+v2f(.2*sin(t)),g-3.);p.yz*=rotate2D(4.);R=length(p.xy);p.y=atan2(p.y,p.x);p.x
=log(R)-p.y/3.;p.xy=fract(p.xy*3./PI-t*.3)-v2f(.5);p.z/=R;o+=v4f(15.*p.z/i);e=.7*
length(p-clamp(p,v3f(-.4),v3f(.4)))*R-snoise3D(p*5.)*.005;}

WGSL check with 1+2+3+8 fix (278 chars, +4%)

var i,g,R,e:f32,p:v3f;e=1.;for(;e>.001&&i<1e2;g+=e){i+=1.;p=v3f((FC.xy-.5*r)
/r.y*g+.2*sin(t),g-3.);p.yz*=rotate2D(4.);R=length(p.xy);p.y=atan2(p.y,p.x);p.x
=log(R)-p.y/3.;p.xy=fract(p.xy*3./PI-t*.3)-.5;p.z/=R;o+=15.*p.z/i;e=.7*
length(p-clamp(p,-.4,.4))*R-snoise3D(p*5.)*.005;}

Here list of problems that I found:

  1. Initialising a sequence with comma-separated values ([wgsl] Comma operator #581)
//this gives an error
var a, b, c:vec2<f32>;

//correct syntax
var a:vec2<f32>;var b:vec2<f32>;var c: vec2<f32>;
  1. Assignment of swizzled values (Assignment of swizzled values should be permitted #737)
//this gives an error
p.yz*=rotate2D(t);

//omg, how to use it?
let r=rotate2D(t);p=vec4<f32>(p.x,dot(p.yz,r[0]),dot(p.yz,r[1]),p.z);
  1. Implicit float -> vecN conversion
//this gives an error
let st=uv+.2*sin(t);

//correct syntax
let st=uv+vec2<f32>(.2*sin(t));
  1. Overload for built-in functions (special case of 3. that produce more symbols)
//this gives an error
return clamp(c, 0., 1.);

//correct syntax
return clamp(c,vec4<f32>(0.),vec4<f32>(1.));
  1. Pipeline of variables (this can reduce code more if name of variable is bigger)
//this gives an error
g+=e=.7*length(p);

//correct syntax
e=.7*length(p);g+=e;
  1. operator++, braces for if and for, comma separator
//this gives an error
for(var i=0;i<10;i++)a=i,b=i*20,c=i*30;

//correct syntax
for(var i=0;i<10;i+=1){a=i;b=i*20;c=i*30;}
  1. vec3<f32> is too hard to type (fixable!)
//glsl
#define vec3 v3f
v3f a=v3f(1.);

//wgsl
type v3f = vec3<f32>;
let a=v3f(1.);
  1. Weird minus problem (A-5. fixed in spec but 5.-A is not?)
//this gives an error
p=b-vec3<f32>(1.,2.,3.);

//correct syntax
p=b- vec3<f32>(1.,2.,3.);

WGSL become much better since first text based proposal but looks like we should consider (1), (2) in v1 directly if we want to continue this culture. Note that this is real example, and kind of sugar benchmark of a language itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgslWebGPU Shading Language Issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions