Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support optional number of components on an Image to improve public facing API #1031

Merged
merged 3 commits into from
Apr 13, 2023

Conversation

oisyn
Copy link
Contributor

@oisyn oisyn commented Apr 12, 2023

This PR adds a components parameter to the Image type and Image! macro. The value (in range 1..=4) is implied for any specified format other than unknown, and can be explicitely specified when used together with the type argument (when omitted, it defaults to 4, which makes it backwards compatible with old code).

This causes the relevant image sampling functions, such as fetch(), sample(), and read(), to return a scalar or vector with those exact number of dimensions, rather than always a 4d vector.

Example usage:

#[spirv(fragment)]
pub fn main(
    #[spirv(descriptor_set = 0, binding = 0)] image1: &Image!(2D, type=f32, sampled, components=1),
    #[spirv(descriptor_set = 0, binding = 1)] image2: &Image!(2D, type=f32, sampled, components=2),
    #[spirv(descriptor_set = 0, binding = 2)] image3: &Image!(2D, type=f32, sampled, components=3),
    #[spirv(descriptor_set = 0, binding = 3)] image4: &Image!(2D, type=f32, sampled),
    #[spirv(descriptor_set = 0, binding = 4)] image2_implied: &Image!(2D, format=rg16f, sampled),
    #[spirv(descriptor_set = 0, binding = 5)] image3_implied: &Image!(2D, format=r11f_g11f_b10f, sampled),
    output: &mut glam::Vec4,
) {
    let coords = glam::IVec2::new(0, 1);
    let t1 = image1.fetch(coords);         // f32
    let t2 = image2.fetch(coords);         // Vec2
    let t3 = image3.fetch(coords);         // Vec3
    let t4 = image4.fetch(coords);         // Vec4
    let t5 = image2_implied.fetch(coords); // Vec2
    let t6 = image3_implied.fetch(coords); // Vec3
    *output = Vec4::splat(t1) + ((t2 + t5).extend(0.0) + t3 + t6).extend(0.0) + t4;
}

@oisyn oisyn marked this pull request as draft April 12, 2023 13:58
@oisyn oisyn marked this pull request as ready for review April 12, 2023 14:36
@eddyb eddyb merged commit 3e2df02 into main Apr 13, 2023
@eddyb eddyb deleted the image-components branch April 13, 2023 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants