Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

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

Merged
eddyb merged 3 commits intomainfrom
image-components
Apr 13, 2023
Merged

Support optional number of components on an Image to improve public facing API#1031
eddyb merged 3 commits intomainfrom
image-components

Conversation

@oisyn
Copy link
Copy Markdown
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 requested a review from eddyb as a code owner April 12, 2023 13:28
@oisyn oisyn force-pushed the image-components branch from ad2d71c to 0f74233 Compare April 12, 2023 13:39
@oisyn oisyn force-pushed the image-components branch from 0f74233 to bcc0939 Compare April 12, 2023 13:43
@oisyn oisyn marked this pull request as draft April 12, 2023 13:58
@oisyn oisyn force-pushed the image-components branch from 6b3436f to 2758791 Compare April 12, 2023 14:22
@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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants