Skip to content

Conversation

@ecoskey
Copy link
Contributor

@ecoskey ecoskey commented Sep 13, 2025

Objective

#19667 introduced a type-erased material system that effectively puts all material instances through a single cache: during asset preparation (ErasedRenderAssetPlugin), materials are processed into a set of MaterialProperties that contains all the data needed to render them, and these are all cached and deduplicated as needed.

This allows for maximal flexibility (every single material instance could have a different ""type"") but complicates the logic and makes cache keys really big. So, one goal for the material revamp I have (and I think @tychedelia is on board) is to cache materials at two levels: material "types" and material "instances", where material types roughly map to the rust types that currently implement Material. Without getting into implementation details, storing mostly static data separate from instance data would let us simplify a lot of the logic, while only requiring a little more work for fully-dynamic use cases.

This PR is a first step in that direction, which stores all the available draw functions in MaterialProperties, and pushes the decision for "what draw function should I use for this material?" to queue time, where before it was split between there and asset preparation. This makes the list of available draw functions instance-independent, and will later allow us to store it with other "static" material data.

Solution

  • Make draw function labels 1:1 with render phases, and include all of them in the list in MaterialProperties

Testing

  • Ran 3d_scene
  • Ran manual_material

@ecoskey ecoskey added A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 13, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needs a migration guide :)

@alice-i-cecile alice-i-cecile added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 1, 2025
@ecoskey ecoskey force-pushed the util/draw_functions branch from a2ab7f7 to 003936a Compare October 23, 2025 02:37
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any obvious problems, but this is beyond my current rendering expertise so I can't leave a full approval.

@IceSentry IceSentry added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Nov 3, 2025
@alice-i-cecile alice-i-cecile self-requested a review November 3, 2025 18:06
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Nov 3, 2025
@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Nov 3, 2025
@alice-i-cecile
Copy link
Member

@ecoskey can you edit the PR description to make the motivation more clear? I'm mostly content to merge this (especially with IceSentry's approval), but "why are we doing this" isn't immediately apparent to me (skill issue, I know :p), and I think that future readers will benefit from a more detailed log.

@ecoskey ecoskey force-pushed the util/draw_functions branch from 53db377 to 056e2ed Compare November 3, 2025 22:49
@superdump
Copy link
Contributor

I don't suppose this has any impact on benchmarks?

@ecoskey
Copy link
Contributor Author

ecoskey commented Nov 4, 2025

I don't suppose this has any impact on benchmarks?

I doubt it, since the code paths (in queue_ etc) are nearly identical. I can still check though if you'd like to be sure?

Copy link
Member

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with making things more flexible, especially because this will make it easier to add custom queue logic / phase items in the future, but am not totally sure how this relates to the stated goal of making draw functions instance independent?

Also note that the claim in the PR that material properties inflates the cache key size isn't correct. This is what the material_key field on material properties is for.

I'm still not totally convinced that denormalization is actually a problem, but this is totally a good incremental change regardless.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Nov 4, 2025
Merged via the queue into bevyengine:main with commit a4cfc24 Nov 4, 2025
36 checks passed
@github-project-automation github-project-automation bot moved this to Done in Rendering Nov 4, 2025
@ecoskey
Copy link
Contributor Author

ecoskey commented Nov 4, 2025

Oops sorry for the misinfo! I think I could have phrased that better as "we have to reconstruct the specializer metadata for each instance"

I'm not sure what you mean by denormalization, but the main idea here is to not narrow down the set of available draw functions at asset-prep time. Then, the list should only depend on info from the type and not instance-specific data, so we can store it independently later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants