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

Improve performance by binning together opaque items instead of sorting them. #12453

Merged
merged 25 commits into from Mar 30, 2024

Commits on Mar 13, 2024

  1. Improve performance by binning together opaque items instead of sorting

    them.
    
    Today, we sort all entities added to all phases, even the phases that
    don't strictly need sorting, such as the opaque and shadow phases. This
    results in a performance loss because our `PhaseItem`s are rather large
    in memory, so sorting is slow. Additionally, determining the boundaries
    of batches is an O(n) process.
    
    This commit makes Bevy instead applicable place phase items into *bins*
    keyed by *bin keys*, which have the invariant that everything in the
    same bin is potentially batchable. This makes determining batch
    boundaries O(1), because everything in the same bin can be batched.
    Instead of sorting each entity, we now sort only the bin keys. This
    drops the sorting time to near-zero on workloads with few bins like
    `many_cubes --no-frustum-culling`. Memory usage is improved too, with
    batch boundaries and dynamic indices now implicit instead of explicit.
    The improved memory usage results in a significant win even on
    unbatchable workloads like `many_cubes --no-frustum-culling
    --vary-material-data-per-instance`, presumably due to cache effects.
    
    Not all phases can be binned; some, such as transparent and transmissive
    phases, must still be sorted. To handle this, this commit splits
    `PhaseItem` into `BinnedPhaseItem` and `SortedPhaseItem`. Most of the
    logic that today deals with `PhaseItem`s has been moved to
    `SortedPhaseItem`. `BinnedPhaseItem` has the new logic.
    
    FPS results are as follows:
    
    | Benchmark                | `binning` | `main`  | Speedup |
    | ------------------------ | --------- | ------- | ------- |
    | `many_cubes -nfc -vmdpi` | 4.608     | 3.373   | 36.61%  |
    | `many_cubes -nfc`        | 42.161    | 35.437  | 18.97%  |
    | `many_foxes`             | 308.943   | 303.321 | 1.85%   |
    
    (`-nfc` is short for `--no-frustum-culling`; `-vmdpi` is short for
    `--vary-material-data-per-instance`.)
    pcwalton committed Mar 13, 2024
    Configuration menu
    Copy the full SHA
    8625fd3 View commit details
    Browse the repository at this point in the history
  2. Rustfmt police

    pcwalton committed Mar 13, 2024
    Configuration menu
    Copy the full SHA
    bf394b0 View commit details
    Browse the repository at this point in the history
  3. Rustdoc police

    pcwalton committed Mar 13, 2024
    Configuration menu
    Copy the full SHA
    f70baba View commit details
    Browse the repository at this point in the history
  4. Rustdoc police again

    pcwalton committed Mar 13, 2024
    Configuration menu
    Copy the full SHA
    1903a47 View commit details
    Browse the repository at this point in the history
  5. Bin alpha masks too

    pcwalton committed Mar 13, 2024
    Configuration menu
    Copy the full SHA
    6604632 View commit details
    Browse the repository at this point in the history

Commits on Mar 19, 2024

  1. Configuration menu
    Copy the full SHA
    f26961a View commit details
    Browse the repository at this point in the history

Commits on Mar 20, 2024

  1. Fix dynamic offsets

    pcwalton committed Mar 20, 2024
    Configuration menu
    Copy the full SHA
    dd53692 View commit details
    Browse the repository at this point in the history
  2. Address review comments

    pcwalton committed Mar 20, 2024
    Configuration menu
    Copy the full SHA
    c1e9035 View commit details
    Browse the repository at this point in the history
  3. Doc check police

    pcwalton committed Mar 20, 2024
    Configuration menu
    Copy the full SHA
    c017e6f View commit details
    Browse the repository at this point in the history

Commits on Mar 25, 2024

  1. Configuration menu
    Copy the full SHA
    d040787 View commit details
    Browse the repository at this point in the history
  2. Address review comments

    pcwalton committed Mar 25, 2024
    Configuration menu
    Copy the full SHA
    4178fe8 View commit details
    Browse the repository at this point in the history

Commits on Mar 26, 2024

  1. Configuration menu
    Copy the full SHA
    ef78989 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    6692299 View commit details
    Browse the repository at this point in the history
  3. Address review comments

    pcwalton committed Mar 26, 2024
    Configuration menu
    Copy the full SHA
    67a5f10 View commit details
    Browse the repository at this point in the history

Commits on Mar 27, 2024

  1. Fix shadows in 3d_shapes and dynamic uniform indices.

    This commit fixes two bugs:
    
    1. Shadow bin keys didn't contain the mesh IDs, causing us to
       incorrectly batch things together that shouldn't have been.
    
    2. Splitting batches for dynamic uniform IDs caused `batchable_keys` and
       `batches` to get out of sync, messing up the rendering in
       `many_cubes` on WebGL 2. This fixes the issue by explicitly storing
       both instance ranges and dynamic uniforms for both batchable and
       unbatchable items.
    pcwalton committed Mar 27, 2024
    Configuration menu
    Copy the full SHA
    ca56bac View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    3e4d2cd View commit details
    Browse the repository at this point in the history

Commits on Mar 28, 2024

  1. Configuration menu
    Copy the full SHA
    34d7160 View commit details
    Browse the repository at this point in the history
  2. Address review comments

    pcwalton committed Mar 28, 2024
    Configuration menu
    Copy the full SHA
    b7570d3 View commit details
    Browse the repository at this point in the history

Commits on Mar 29, 2024

  1. Configuration menu
    Copy the full SHA
    642ad9b View commit details
    Browse the repository at this point in the history
  2. Doc-check police

    pcwalton committed Mar 29, 2024
    Configuration menu
    Copy the full SHA
    ac9c978 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    269b385 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    23ab07d View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    638c7c2 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    18392b7 View commit details
    Browse the repository at this point in the history

Commits on Mar 30, 2024

  1. Configuration menu
    Copy the full SHA
    8959f0f View commit details
    Browse the repository at this point in the history