leod / posh

Type-Safe Graphics Programming with Functional Shaders
136 stars 2 forks source link

Fix redundant struct literal expressions in generated code #96

Open leod opened 1 year ago

leod commented 1 year ago

Currently, the implementation of Object::expr() returns a new Rc in each call. This is necessary to allow users to construct structs as normal Rust literals.

Since we use Rcs to deduplicate expressions, this has the downside of generating redundant struct literal expressions:

    vec4 var_0 = texture(uniforms_b, vertex_output_uv);
    float var_1 = var_0.x;
    float var_2 = var_0.y;
    float var_3 = var_0.z;
    vec3 var_4 = mix(((1.055 * pow(vec3(var_1, var_2, var_3), vec3(0.41666666, 0.41666666, 0.41666666))) - 0.055), (vec3(var_1, var_2, var_3) * 12.92), lessThan(vec3(var_1, var_2, var_3), vec3(0.0031308, 0.0031308, 0.0031308)));

Here, the vec3(var_1, var_2, var_3) is redundant.

We could fix this by caching struct literal expressions in Trace somehow. Maybe.