MaikKlein / rlsl

Rust to SPIR-V compiler
Apache License 2.0
557 stars 14 forks source link

Specialization constants #23

Open MaikKlein opened 6 years ago

MaikKlein commented 6 years ago

My first idea was

#[spirv(fragment)]
fn frag(color: Vec4<f32>, c: Constant<Foo>) -> Vec4<f32> {
    color
}

but that doesn't work because Rust does not have default arguments.

#[spirv(fragment)]
fn frag(color: Vec4<f32>, ) -> Vec4<f32> {
    const c: Constant<Foo> = ...;
    color
}

That doesn't work because const is compiled away.

static FOO: Foo = ..;
#[spirv(fragment)]
fn frag(color: Vec4<f32>, ) -> Vec4<f32> {
    let f = FOO;
    color
}

That should work but it is too easy to add and remove statics inside an entry point. Commenting out let f = FOO; would change the shader signature. Maybe it is not too bad?


#[spirv(fragment)]
fn frag(color: Vec4<f32>, ) -> Vec4<f32> {
    static FOO: Foo = ..;
    let f = FOO;
    color
}

Maybe statics should be defined inside the entry point? But that also doesn't feel quite right.

static FOO: Foo = ..;
#[spirv(fragment)]
#[spirv(constant = FOO)]
fn frag(color: Vec4<f32>, ) -> Vec4<f32> {
    let f = FOO; // Now FOO is a specialization constant
    color
}

Maybe specialization constants should be added as an attribute?

#[spirv(fragment)]
fn frag(color: Vec4<f32>, c: Constant<Foo>) -> Vec4<f32> {
    color
}

Maybe this still works if I force Constants to implement Default or a similar trait? that doesn't seem straight forward to implement. I would have to evaluate it manually with miri.