Remove Shader weak_handles from bevy_render. (#19362)

# Objective

- Related to #19024

## Solution

- Use the new `load_shader_library` macro for the shader libraries and
`embedded_asset`/`load_embedded_asset` for the "shader binaries" in
bevy_render.

## Testing

- `animate_shader` example still works

P.S. I don't think this needs a migration guide. Technically users could
be using the `pub` weak handles, but there's no actual good use for
them, so omitting it seems fine. Alternatively, we could mix this in
with the migration guide notes for #19137.
This commit is contained in:
andriyDev 2025-05-26 13:20:25 -07:00 committed by GitHub
parent f04c0ef689
commit 8db7b6e122
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 37 deletions

View File

@ -4,19 +4,13 @@
//! Bevy. //! Bevy.
use bevy_app::{App, Plugin}; use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, weak_handle, Handle};
use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent}; use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent};
use bevy_reflect::{prelude::ReflectDefault, Reflect}; use bevy_reflect::{prelude::ReflectDefault, Reflect};
use crate::{ use crate::{
extract_component::ExtractComponent, extract_component::ExtractComponent, load_shader_library, render_resource::TextureView,
render_resource::{Shader, TextureView},
}; };
/// The handle to the `mesh_preprocess_types.wgsl` compute shader.
pub const MESH_PREPROCESS_TYPES_SHADER_HANDLE: Handle<Shader> =
weak_handle!("7bf7bdb1-ec53-4417-987f-9ec36533287c");
/// Enables GPU occlusion culling. /// Enables GPU occlusion culling.
/// ///
/// See [`OcclusionCulling`] for a detailed description of occlusion culling in /// See [`OcclusionCulling`] for a detailed description of occlusion culling in
@ -25,12 +19,7 @@ pub struct OcclusionCullingPlugin;
impl Plugin for OcclusionCullingPlugin { impl Plugin for OcclusionCullingPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
load_internal_asset!( load_shader_library!(app, "mesh_preprocess_types.wgsl");
app,
MESH_PREPROCESS_TYPES_SHADER_HANDLE,
"mesh_preprocess_types.wgsl",
Shader::from_wgsl
);
} }
} }

View File

@ -1,25 +1,21 @@
use crate::{ use crate::{
extract_resource::ExtractResource, extract_resource::ExtractResource,
prelude::Shader, load_shader_library,
render_resource::{ShaderType, UniformBuffer}, render_resource::{ShaderType, UniformBuffer},
renderer::{RenderDevice, RenderQueue}, renderer::{RenderDevice, RenderQueue},
Extract, ExtractSchedule, Render, RenderApp, RenderSystems, Extract, ExtractSchedule, Render, RenderApp, RenderSystems,
}; };
use bevy_app::{App, Plugin}; use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, weak_handle, Handle};
use bevy_diagnostic::FrameCount; use bevy_diagnostic::FrameCount;
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_reflect::prelude::*; use bevy_reflect::prelude::*;
use bevy_time::Time; use bevy_time::Time;
pub const GLOBALS_TYPE_HANDLE: Handle<Shader> =
weak_handle!("9e22a765-30ca-4070-9a4c-34ac08f1c0e7");
pub struct GlobalsPlugin; pub struct GlobalsPlugin;
impl Plugin for GlobalsPlugin { impl Plugin for GlobalsPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
load_internal_asset!(app, GLOBALS_TYPE_HANDLE, "globals.wgsl", Shader::from_wgsl); load_shader_library!(app, "globals.wgsl");
app.register_type::<GlobalsUniform>(); app.register_type::<GlobalsUniform>();
if let Some(render_app) = app.get_sub_app_mut(RenderApp) { if let Some(render_app) = app.get_sub_app_mut(RenderApp) {

View File

@ -1,7 +1,6 @@
pub mod visibility; pub mod visibility;
pub mod window; pub mod window;
use bevy_asset::{load_internal_asset, weak_handle, Handle};
use bevy_diagnostic::FrameCount; use bevy_diagnostic::FrameCount;
pub use visibility::*; pub use visibility::*;
pub use window::*; pub use window::*;
@ -13,7 +12,7 @@ use crate::{
}, },
experimental::occlusion_culling::OcclusionCulling, experimental::occlusion_culling::OcclusionCulling,
extract_component::ExtractComponentPlugin, extract_component::ExtractComponentPlugin,
prelude::Shader, load_shader_library,
primitives::Frustum, primitives::Frustum,
render_asset::RenderAssets, render_asset::RenderAssets,
render_phase::ViewRangefinder3d, render_phase::ViewRangefinder3d,
@ -46,8 +45,6 @@ use wgpu::{
TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}; };
pub const VIEW_TYPE_HANDLE: Handle<Shader> = weak_handle!("7234423c-38bb-411c-acec-f67730f6db5b");
/// The matrix that converts from the RGB to the LMS color space. /// The matrix that converts from the RGB to the LMS color space.
/// ///
/// To derive this, first we convert from RGB to [CIE 1931 XYZ]: /// To derive this, first we convert from RGB to [CIE 1931 XYZ]:
@ -101,7 +98,7 @@ pub struct ViewPlugin;
impl Plugin for ViewPlugin { impl Plugin for ViewPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
load_internal_asset!(app, VIEW_TYPE_HANDLE, "view.wgsl", Shader::from_wgsl); load_shader_library!(app, "view.wgsl");
app.register_type::<InheritedVisibility>() app.register_type::<InheritedVisibility>()
.register_type::<ViewVisibility>() .register_type::<ViewVisibility>()

View File

@ -17,7 +17,7 @@ use crate::{
}; };
use alloc::{borrow::Cow, sync::Arc}; use alloc::{borrow::Cow, sync::Arc};
use bevy_app::{First, Plugin, Update}; use bevy_app::{First, Plugin, Update};
use bevy_asset::{load_internal_asset, weak_handle, Handle}; use bevy_asset::{embedded_asset, load_embedded_asset, Handle};
use bevy_derive::{Deref, DerefMut}; use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{ use bevy_ecs::{
entity::EntityHashMap, event::event_update_system, prelude::*, system::SystemState, entity::EntityHashMap, event::event_update_system, prelude::*, system::SystemState,
@ -392,9 +392,6 @@ fn prepare_screenshot_state(
pub struct ScreenshotPlugin; pub struct ScreenshotPlugin;
const SCREENSHOT_SHADER_HANDLE: Handle<Shader> =
weak_handle!("c31753d6-326a-47cb-a359-65c97a471fda");
impl Plugin for ScreenshotPlugin { impl Plugin for ScreenshotPlugin {
fn build(&self, app: &mut bevy_app::App) { fn build(&self, app: &mut bevy_app::App) {
app.add_systems( app.add_systems(
@ -406,12 +403,7 @@ impl Plugin for ScreenshotPlugin {
.register_type::<Screenshot>() .register_type::<Screenshot>()
.register_type::<ScreenshotCaptured>(); .register_type::<ScreenshotCaptured>();
load_internal_asset!( embedded_asset!(app, "screenshot.wgsl");
app,
SCREENSHOT_SHADER_HANDLE,
"screenshot.wgsl",
Shader::from_wgsl
);
} }
fn finish(&self, app: &mut bevy_app::App) { fn finish(&self, app: &mut bevy_app::App) {
@ -441,6 +433,7 @@ impl Plugin for ScreenshotPlugin {
#[derive(Resource)] #[derive(Resource)]
pub struct ScreenshotToScreenPipeline { pub struct ScreenshotToScreenPipeline {
pub bind_group_layout: BindGroupLayout, pub bind_group_layout: BindGroupLayout,
pub shader: Handle<Shader>,
} }
impl FromWorld for ScreenshotToScreenPipeline { impl FromWorld for ScreenshotToScreenPipeline {
@ -455,7 +448,12 @@ impl FromWorld for ScreenshotToScreenPipeline {
), ),
); );
Self { bind_group_layout } let shader = load_embedded_asset!(render_world, "screenshot.wgsl");
Self {
bind_group_layout,
shader,
}
} }
} }
@ -470,7 +468,7 @@ impl SpecializedRenderPipeline for ScreenshotToScreenPipeline {
buffers: vec![], buffers: vec![],
shader_defs: vec![], shader_defs: vec![],
entry_point: Cow::Borrowed("vs_main"), entry_point: Cow::Borrowed("vs_main"),
shader: SCREENSHOT_SHADER_HANDLE, shader: self.shader.clone(),
}, },
primitive: wgpu::PrimitiveState { primitive: wgpu::PrimitiveState {
cull_mode: Some(wgpu::Face::Back), cull_mode: Some(wgpu::Face::Back),
@ -479,7 +477,7 @@ impl SpecializedRenderPipeline for ScreenshotToScreenPipeline {
depth_stencil: None, depth_stencil: None,
multisample: Default::default(), multisample: Default::default(),
fragment: Some(FragmentState { fragment: Some(FragmentState {
shader: SCREENSHOT_SHADER_HANDLE, shader: self.shader.clone(),
entry_point: Cow::Borrowed("fs_main"), entry_point: Cow::Borrowed("fs_main"),
shader_defs: vec![], shader_defs: vec![],
targets: vec![Some(wgpu::ColorTargetState { targets: vec![Some(wgpu::ColorTargetState {