Skip to content

bevy_shader#20330

Merged
alice-i-cecile merged 17 commits intobevyengine:mainfrom
atlv24:ad/shader-cache
Jul 30, 2025
Merged

bevy_shader#20330
alice-i-cecile merged 17 commits intobevyengine:mainfrom
atlv24:ad/shader-cache

Conversation

@atlv24
Copy link
Contributor

@atlv24 atlv24 commented Jul 30, 2025

Objective

  • split off bevy_shader from bevy_render to allow for defining shader libraries and materials for scenes without depending on a renderer

Solution

  • a bunch of small refactor commits, then bevy_shader! (review commit by commit)

Testing

  • please help with this, especially with spirv passthrough and decoupled_naga testing: i only ran 3d_scene at every step to make sure i didnt break anything.

EDIT: this has been tested now, decoupled_naga works, and I also fixed a bug that was on main when wesl and decoupled_naga were both present it didnt compile, now it does

Future Work

  • Split ShaderCacheError out of PipelineCacheError (or just rename) (punted because breaking change)
  • Consider renaming PipelineCacheId to ShaderCacheId ? (punted because breaking change)
  • bevy_material
  • bevy_bsdf (bindingless pbr shader library)
  • move view.wgsl into bevy_camera after Move view transformations to bevy_render #20313 lands ?

@atlv24 atlv24 added A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 30, 2025
@atlv24 atlv24 added C-Code-Quality A section of code that is hard to understand or change D-Shaders This code uses GPU shader languages labels Jul 30, 2025
@atlv24 atlv24 force-pushed the ad/shader-cache branch 3 times, most recently from b54cdf3 to 7f07213 Compare July 30, 2025 03:57
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reviewer note: this is needed because in a future commit I make ShaderData generic over ShaderModule, but the Default derive does not pick up on the fact that it doesnt need ShaderData: Default to make this struct Default, since its in a HashMap (which is Default regardless of K and V)


/// A shader, as defined by its [`ShaderSource`](wgpu::ShaderSource) and [`ShaderStage`](naga::ShaderStage)
/// This is an "unprocessed" shader. It can contain preprocessor directives.
/// A an "unprocessed" shader. It can contain preprocessor directives.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reviewer note: this comment was really outdated, ShaderSource and ShaderStage are not involved at this point of a Shader's lifecycle, its just text at this point.

fn from(value: NonZero<u32>) -> Self {
Self(value)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reviewer note: this is necessary because define_atomic_id lives in bevy_render

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it should be elsewhere? bevy_util or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ive considered it, there was some opposition back when i made bevy_image, so i ended up figuring out a way to avoid pulling the stuff that needed it out. There is growing evidence to needing this generally though, so perhaps a good followup, allowing more image stuff to be pulled into bevy_image

Wgsl(String),
/// Naga module.
Naga(naga::Module),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reviewer note: this is introduced to avoid taking on a dependency on wgpu just for the ShaderSource struct, but also is a looooot simpler than that struct.

@atlv24 atlv24 force-pushed the ad/shader-cache branch from 7f07213 to a4bce89 Compare July 30, 2025 13:22
Copy link
Contributor

@superdump superdump left a comment

Choose a reason for hiding this comment

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

LGTM. Minor doc typo.

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jul 30, 2025
@alice-i-cecile alice-i-cecile added S-Needs-Testing Testing must be done before this is safe to merge and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 30, 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.

On board with this plan, and the changes seem fine. I don't have time to do testing right now though, so I would like more testing before we merge.

shader_cache: Arc::new(Mutex::new(ShaderCache::new(
device.features(),
render_adapter.get_downlevel_capabilities().flags,
load_module,
Copy link
Contributor

@mate-h mate-h Jul 30, 2025

Choose a reason for hiding this comment

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

Here it's not clear that the load_module function is coming from the global scope (unless you open the file, go to reference with the language server), maybe rename the top level function to load_module_global or load_cache_module just to make it more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true! i think its fine though since this would only be confusing upon review, not during actual code maintenance though? since we'll have our editor open and it'll be navigable most of the time we care about this going forward

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think the only thing that would make it obvious to me is wrapping it in a closure, but i think that looks ugly so i dont wanna do it lmao

@atlv24 atlv24 added C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it P-Compile-Failure A failure to compile Bevy apps and removed S-Needs-Testing Testing must be done before this is safe to merge labels Jul 30, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 30, 2025
Merged via the queue into bevyengine:main with commit c430855 Jul 30, 2025
42 checks passed
tychedelia pushed a commit to tychedelia/bevy that referenced this pull request Jul 31, 2025
# Objective

- split off bevy_shader from bevy_render to allow for defining shader
libraries and materials for scenes without depending on a renderer

## Solution

- a bunch of small refactor commits, then bevy_shader! (review commit by
commit)

## Testing

- please help with this, especially with spirv passthrough and
decoupled_naga testing: i only ran 3d_scene at every step to make sure i
didnt break anything.

EDIT: this has been tested now, decoupled_naga works, and I also fixed a
bug that was on main when wesl and decoupled_naga were both present it
didnt compile, now it does


# Future Work

- Split ShaderCacheError out of PipelineCacheError (or just rename)
(punted because breaking change)
- Consider renaming PipelineCacheId to ShaderCacheId ? (punted because
breaking change)
- bevy_material
- bevy_bsdf (bindingless pbr shader library)
- move view.wgsl into bevy_camera after bevyengine#20313 lands ?
@atlv24 atlv24 deleted the ad/shader-cache branch August 1, 2025 01:04
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-Bug An unexpected or incorrect behavior C-Code-Quality A section of code that is hard to understand or change D-Shaders This code uses GPU shader languages P-Compile-Failure A failure to compile Bevy apps S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants