bevy/crates/bevy_render/src/render_resource
Sludge 7bb76ab74b
Add VertexBufferLayout::offset_locations (#9805)
# Objective

When using instancing, 2 `VertexBufferLayout`s are needed, one for
per-vertex and one for per-instance data. Shader locations of all
attributes must not overlap, so one of the layouts needs to start its
locations at an offset. However,
`VertexBufferLayout::from_vertex_formats` will always start locations at
0, requiring manual adjustment, which is currently pretty verbose.

## Solution

Add `VertexBufferLayout::offset_locations`, which adds an offset to all
attribute locations.

Code using this method looks like this:

```rust
VertexState {
    shader: BACKBUFFER_SHADER_HANDLE.typed(),
    shader_defs: Vec::new(),
    entry_point: "vertex".into(),
    buffers: vec![
        VertexBufferLayout::from_vertex_formats(
            VertexStepMode::Vertex,
            [VertexFormat::Float32x2],
        ),
        VertexBufferLayout::from_vertex_formats(
            VertexStepMode::Instance,
            [VertexFormat::Float32x2, VertexFormat::Float32x3],
        )
        .offset_locations(1),
    ],
}
```

Alternative solutions include:

- Pass the starting location to `from_vertex_formats` – this is a bit
simpler than my solution here, but most calls don't need an offset, so
they'd always pass 0 there.
- Do nothing and make the user hand-write this.

---

## Changelog

- Add `VertexBufferLayout::offset_locations` to simplify buffer layout
construction when using instancing.

---------

Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-08-26 17:54:33 +00:00
..
batched_uniform_buffer.rs Disentangle bevy_utils/bevy_core's reexported dependencies (#12313) 2024-03-07 02:30:15 +00:00
bind_group_entries.rs Replace or document ignored doctests (#11040) 2024-01-01 16:50:56 +00:00
bind_group_layout_entries.rs Apply Clippy lints regarding lazy evaluation and closures (#14015) 2024-07-01 15:54:40 +00:00
bind_group_layout.rs Replace UUID based IDs with a atomic-counted ones (#6988) 2022-12-25 00:23:15 +00:00
bind_group.rs Refactor AsBindGroup to use a associated SystemParam. (#14909) 2024-08-25 20:16:34 +00:00
buffer_vec.rs Apply unused_qualifications lint (#14828) 2024-08-21 12:29:33 +00:00
buffer.rs Replace UUID based IDs with a atomic-counted ones (#6988) 2022-12-25 00:23:15 +00:00
gpu_array_buffer.rs Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
mod.rs Added AstcBlock and AstcChannel to the forwarded wgpu types. (#14410) 2024-07-22 19:14:14 +00:00
pipeline_cache.rs Apply unused_qualifications lint (#14828) 2024-08-21 12:29:33 +00:00
pipeline_specializer.rs Micro-optimize queue_material_meshes, primarily to remove bit manipulation. (#12791) 2024-04-01 21:58:53 +00:00
pipeline.rs Add VertexBufferLayout::offset_locations (#9805) 2024-08-26 17:54:33 +00:00
resource_macros.rs Improve performance by binning together opaque items instead of sorting them. (#12453) 2024-03-30 02:55:02 +00:00
shader.rs Optimize common usages of AssetReader (#14082) 2024-07-01 19:59:42 +00:00
storage_buffer.rs Only create changed buffer if it already exists (#13242) 2024-05-05 22:16:11 +00:00
texture.rs Add screenshot api (#7163) 2023-04-19 21:28:42 +00:00
uniform_buffer.rs Only create changed buffer if it already exists (#13242) 2024-05-05 22:16:11 +00:00