Skip to content

Consider builder pattern for adding images #19101

Open
@torsteingrindvik

Description

@torsteingrindvik

What problem does this solve or what need does it fill?

We have lots of code such as this:

let image = Image {

Search for TextureDimension in the examples folder and there are many hits.

It typically looks like so:

    let size = Extent3d {
        width: window_size.x,
        height: window_size.y,
        ..default()
    };
    let format = TextureFormat::Bgra8UnormSrgb;
    let image = Image {
        data: Some(vec![0; size.volume() * format.pixel_size()]),
        texture_descriptor: TextureDescriptor {
            label: None,
            size,
            dimension: TextureDimension::D2,
            format,
            mip_level_count: 1,
            sample_count: 1,
            usage: TextureUsages::TEXTURE_BINDING
                | TextureUsages::COPY_DST
                | TextureUsages::RENDER_ATTACHMENT,
            view_formats: &[],
        },
        ..default()
    };
    let image_handle = images.add(image);

It's quite verbose and is (arguably) needlessly close to the wgpu innards.

What solution would you like?

We could expose a builder pattern which would look along the lines of:

let image = ImageBuilder::new_2d(vec2(window_size.x, window_size.y))
    .data(vec![0; size.volume() * format.pixel_size()])
    .usage(TextureUsages::TEXTURE_BINDING
        | TextureUsages::COPY_DST    
        | TextureUsages::RENDER_ATTACHMENT);

// mip level, sample count, texture format, view formats have sane defaults
// and have builder methods for users who need them

let image_handle = images.add(image.build());

and similar for 3d (maybe 1d?).

Some bikeshedding opportunities:

  • usage(flags) vs usage_texture_binding() + usage_copy_destination() + ...
  • new_2d(size) could zero by default, making .data() unnecessary unless there was actual non-zero data to provide

What alternative(s) have you considered?

Just keeping things as-is.

Additional context

Some discord context: https://discordapp.com/channels/691052431525675048/692572690833473578/1369263245608620082

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-UsabilityA targeted quality-of-life change that makes Bevy easier to useS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions