From 846cec9ed233747762d86d95a204593169187f3c Mon Sep 17 00:00:00 2001 From: andriyDev Date: Tue, 27 May 2025 15:32:32 -0700 Subject: [PATCH] Remove Shader weak_handles from bevy_gizmos. (#19394) # 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_gizmos`. ## Testing - `2d_gizmos` example still works. - `3d_gizmos` 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. --- crates/bevy_gizmos/src/lib.rs | 23 ++++++----------------- crates/bevy_gizmos/src/pipeline_2d.rs | 15 ++++++++++----- crates/bevy_gizmos/src/pipeline_3d.rs | 15 ++++++++++----- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 581a30091d..a59a80e89e 100755 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -102,7 +102,7 @@ use crate::{config::ErasedGizmoConfigGroup, gizmos::GizmoBuffer}; #[cfg(feature = "bevy_render")] use { crate::retained::extract_linegizmos, - bevy_asset::{weak_handle, AssetId}, + bevy_asset::AssetId, bevy_ecs::{ component::Component, entity::Entity, @@ -119,8 +119,8 @@ use { render_phase::{PhaseItem, RenderCommand, RenderCommandResult, TrackedRenderPass}, render_resource::{ binding_types::uniform_buffer, BindGroup, BindGroupEntries, BindGroupLayout, - BindGroupLayoutEntries, Buffer, BufferInitDescriptor, BufferUsages, Shader, - ShaderStages, ShaderType, VertexFormat, + BindGroupLayoutEntries, Buffer, BufferInitDescriptor, BufferUsages, ShaderStages, + ShaderType, VertexFormat, }, renderer::RenderDevice, sync_world::{MainEntity, TemporaryRenderEntity}, @@ -144,12 +144,6 @@ use gizmos::{GizmoStorage, Swap}; #[cfg(all(feature = "bevy_pbr", feature = "bevy_render"))] use light::LightGizmoPlugin; -#[cfg(feature = "bevy_render")] -const LINE_SHADER_HANDLE: Handle = weak_handle!("15dc5869-ad30-4664-b35a-4137cb8804a1"); -#[cfg(feature = "bevy_render")] -const LINE_JOINT_SHADER_HANDLE: Handle = - weak_handle!("7b5bdda5-df81-4711-a6cf-e587700de6f2"); - /// A [`Plugin`] that provides an immediate mode drawing api for visual debugging. /// /// Requires to be loaded after [`PbrPlugin`](bevy_pbr::PbrPlugin) or [`SpritePlugin`](bevy_sprite::SpritePlugin). @@ -160,14 +154,9 @@ impl Plugin for GizmoPlugin { fn build(&self, app: &mut App) { #[cfg(feature = "bevy_render")] { - use bevy_asset::load_internal_asset; - load_internal_asset!(app, LINE_SHADER_HANDLE, "lines.wgsl", Shader::from_wgsl); - load_internal_asset!( - app, - LINE_JOINT_SHADER_HANDLE, - "line_joints.wgsl", - Shader::from_wgsl - ); + use bevy_asset::embedded_asset; + embedded_asset!(app, "lines.wgsl"); + embedded_asset!(app, "line_joints.wgsl"); } app.register_type::() diff --git a/crates/bevy_gizmos/src/pipeline_2d.rs b/crates/bevy_gizmos/src/pipeline_2d.rs index 15ed1c3ab0..a97071249d 100644 --- a/crates/bevy_gizmos/src/pipeline_2d.rs +++ b/crates/bevy_gizmos/src/pipeline_2d.rs @@ -2,9 +2,10 @@ use crate::{ config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig}, line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, DrawLineJointGizmo, GizmoRenderSystems, GpuLineGizmo, LineGizmoUniformBindgroupLayout, - SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE, LINE_SHADER_HANDLE, + SetLineGizmoBindGroup, }; use bevy_app::{App, Plugin}; +use bevy_asset::{load_embedded_asset, Handle}; use bevy_core_pipeline::core_2d::{Transparent2d, CORE_2D_DEPTH_FORMAT}; use bevy_ecs::{ @@ -75,6 +76,7 @@ impl Plugin for LineGizmo2dPlugin { struct LineGizmoPipeline { mesh_pipeline: Mesh2dPipeline, uniform_layout: BindGroupLayout, + shader: Handle, } impl FromWorld for LineGizmoPipeline { @@ -85,6 +87,7 @@ impl FromWorld for LineGizmoPipeline { .resource::() .layout .clone(), + shader: load_embedded_asset!(render_world, "lines.wgsl"), } } } @@ -124,13 +127,13 @@ impl SpecializedRenderPipeline for LineGizmoPipeline { RenderPipelineDescriptor { vertex: VertexState { - shader: LINE_SHADER_HANDLE, + shader: self.shader.clone(), entry_point: "vertex".into(), shader_defs: shader_defs.clone(), buffers: line_gizmo_vertex_buffer_layouts(key.strip), }, fragment: Some(FragmentState { - shader: LINE_SHADER_HANDLE, + shader: self.shader.clone(), shader_defs, entry_point: fragment_entry_point.into(), targets: vec![Some(ColorTargetState { @@ -173,6 +176,7 @@ impl SpecializedRenderPipeline for LineGizmoPipeline { struct LineJointGizmoPipeline { mesh_pipeline: Mesh2dPipeline, uniform_layout: BindGroupLayout, + shader: Handle, } impl FromWorld for LineJointGizmoPipeline { @@ -183,6 +187,7 @@ impl FromWorld for LineJointGizmoPipeline { .resource::() .layout .clone(), + shader: load_embedded_asset!(render_world, "line_joints.wgsl"), } } } @@ -225,13 +230,13 @@ impl SpecializedRenderPipeline for LineJointGizmoPipeline { RenderPipelineDescriptor { vertex: VertexState { - shader: LINE_JOINT_SHADER_HANDLE, + shader: self.shader.clone(), entry_point: entry_point.into(), shader_defs: shader_defs.clone(), buffers: line_joint_gizmo_vertex_buffer_layouts(), }, fragment: Some(FragmentState { - shader: LINE_JOINT_SHADER_HANDLE, + shader: self.shader.clone(), shader_defs, entry_point: "fragment".into(), targets: vec![Some(ColorTargetState { diff --git a/crates/bevy_gizmos/src/pipeline_3d.rs b/crates/bevy_gizmos/src/pipeline_3d.rs index 87d865f8ca..1cc70c67cb 100644 --- a/crates/bevy_gizmos/src/pipeline_3d.rs +++ b/crates/bevy_gizmos/src/pipeline_3d.rs @@ -2,9 +2,10 @@ use crate::{ config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig}, line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, DrawLineJointGizmo, GizmoRenderSystems, GpuLineGizmo, LineGizmoUniformBindgroupLayout, - SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE, LINE_SHADER_HANDLE, + SetLineGizmoBindGroup, }; use bevy_app::{App, Plugin}; +use bevy_asset::{load_embedded_asset, Handle}; use bevy_core_pipeline::{ core_3d::{Transparent3d, CORE_3D_DEPTH_FORMAT}, oit::OrderIndependentTransparencySettings, @@ -75,6 +76,7 @@ impl Plugin for LineGizmo3dPlugin { struct LineGizmoPipeline { mesh_pipeline: MeshPipeline, uniform_layout: BindGroupLayout, + shader: Handle, } impl FromWorld for LineGizmoPipeline { @@ -85,6 +87,7 @@ impl FromWorld for LineGizmoPipeline { .resource::() .layout .clone(), + shader: load_embedded_asset!(render_world, "lines.wgsl"), } } } @@ -131,13 +134,13 @@ impl SpecializedRenderPipeline for LineGizmoPipeline { RenderPipelineDescriptor { vertex: VertexState { - shader: LINE_SHADER_HANDLE, + shader: self.shader.clone(), entry_point: "vertex".into(), shader_defs: shader_defs.clone(), buffers: line_gizmo_vertex_buffer_layouts(key.strip), }, fragment: Some(FragmentState { - shader: LINE_SHADER_HANDLE, + shader: self.shader.clone(), shader_defs, entry_point: fragment_entry_point.into(), targets: vec![Some(ColorTargetState { @@ -171,6 +174,7 @@ impl SpecializedRenderPipeline for LineGizmoPipeline { struct LineJointGizmoPipeline { mesh_pipeline: MeshPipeline, uniform_layout: BindGroupLayout, + shader: Handle, } impl FromWorld for LineJointGizmoPipeline { @@ -181,6 +185,7 @@ impl FromWorld for LineJointGizmoPipeline { .resource::() .layout .clone(), + shader: load_embedded_asset!(render_world, "line_joints.wgsl"), } } } @@ -230,13 +235,13 @@ impl SpecializedRenderPipeline for LineJointGizmoPipeline { RenderPipelineDescriptor { vertex: VertexState { - shader: LINE_JOINT_SHADER_HANDLE, + shader: self.shader.clone(), entry_point: entry_point.into(), shader_defs: shader_defs.clone(), buffers: line_joint_gizmo_vertex_buffer_layouts(), }, fragment: Some(FragmentState { - shader: LINE_JOINT_SHADER_HANDLE, + shader: self.shader.clone(), shader_defs, entry_point: "fragment".into(), targets: vec![Some(ColorTargetState {