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.
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_reflect::{prelude::ReflectDefault, Reflect};
use crate::{
extract_component::ExtractComponent,
render_resource::{Shader, TextureView},
extract_component::ExtractComponent, load_shader_library, render_resource::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.
///
/// See [`OcclusionCulling`] for a detailed description of occlusion culling in
@ -25,12 +19,7 @@ pub struct OcclusionCullingPlugin;
impl Plugin for OcclusionCullingPlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
MESH_PREPROCESS_TYPES_SHADER_HANDLE,
"mesh_preprocess_types.wgsl",
Shader::from_wgsl
);
load_shader_library!(app, "mesh_preprocess_types.wgsl");
}
}

View File

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

View File

@ -1,7 +1,6 @@
pub mod visibility;
pub mod window;
use bevy_asset::{load_internal_asset, weak_handle, Handle};
use bevy_diagnostic::FrameCount;
pub use visibility::*;
pub use window::*;
@ -13,7 +12,7 @@ use crate::{
},
experimental::occlusion_culling::OcclusionCulling,
extract_component::ExtractComponentPlugin,
prelude::Shader,
load_shader_library,
primitives::Frustum,
render_asset::RenderAssets,
render_phase::ViewRangefinder3d,
@ -46,8 +45,6 @@ use wgpu::{
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.
///
/// To derive this, first we convert from RGB to [CIE 1931 XYZ]:
@ -101,7 +98,7 @@ pub struct ViewPlugin;
impl Plugin for ViewPlugin {
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>()
.register_type::<ViewVisibility>()

View File

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