bevyengine / bevyengine/bevy

Improve vertex attribute cache hit rate

Open
#4,720 1 comment 0 reactions 0 assignees View on GitHub
A-Assets A-Rendering C-Performance
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

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

Vertex buffer data is loaded into caches without knowledge of the attributes accessed. As such, if a subset of the available attributes are used in a shader, the other attributes are reducing the cache hit rate by filling cache lines with unused data.

A particularly important and common situation is using vertex buffers for z-only passes such as depth-prepasses or shadow mapping passes. These shaders only need the position attribute. All other attributes are unused (except possibly UVs for alpha mask materials...?)

This also reduces bandwidth usage which is specifically valuable on mobile devices which are very bandwidth-constrained.

## What solution would you like?

- Rework `Mesh` API to get vertex buffer layouts and vertex buffer data to allow specification of the attributes to include/exclude. This would enable full flexibility for custom implementations.
- Store position attribute data in its own vertex buffer and everything else in a separate buffer as the default `Mesh` asset handling approach. If people need something else then they can implement a custom asset wrapper type around `Mesh` that uses the aforementioned API and does what they need it to do.

## What alternative(s) have you considered?

We currently have the extreme of all vertex attributes being stored in one buffer.

The other extreme is having one buffer per attribute. One problem with that approach is that if there is a cache miss due to the order of indices into the vertex buffer, the cache miss is incurred once for each vertex buffer.

Another possibility would be to build interleaved buffers for each configuration of attributes actually used by a shader. However, if many different shaders are used for a mesh and they have different required vertex attributes, this would amplify the amount of VRAM used for storing those vertex buffers. So while this may be computationally optimal, it would have a VRAM usage cost.

## Additional context

See this thread for details (note, it branches in places so pay attention to the reply counts on each tweet): https://twitter.com/thesolidpixel/status/1524061068225818625

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.