From ebb49d4b98373d613b1195f6b14af3cab243fbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rezan=20Kizilg=C3=BCn?= Date: Sun, 14 Jul 2024 13:39:52 +0200 Subject: [PATCH 1/4] Reapply "Prevent needing to copy shaders to your own repo" This reverts commit 2af868608be3a4f983a33be312cbe403560370c1. # Conflicts: # src/gi/compositing.rs # src/gi/constants.rs # Conflicts: # src/gi/compositing.rs --- src/gi/compositing.rs | 8 +-- src/gi/constants.rs | 7 --- src/gi/mod.rs | 55 +++++-------------- src/gi/pipeline.rs | 10 ++-- .../gi}/shaders/gi_post_processing.wgsl | 0 {assets => src/gi}/shaders/gi_sdf.wgsl | 0 {assets => src/gi}/shaders/gi_ss_blend.wgsl | 0 {assets => src/gi}/shaders/gi_ss_bounce.wgsl | 0 {assets => src/gi}/shaders/gi_ss_filter.wgsl | 0 {assets => src/gi}/shaders/gi_ss_probe.wgsl | 0 10 files changed, 21 insertions(+), 59 deletions(-) rename {assets => src/gi}/shaders/gi_post_processing.wgsl (100%) rename {assets => src/gi}/shaders/gi_sdf.wgsl (100%) rename {assets => src/gi}/shaders/gi_ss_blend.wgsl (100%) rename {assets => src/gi}/shaders/gi_ss_bounce.wgsl (100%) rename {assets => src/gi}/shaders/gi_ss_filter.wgsl (100%) rename {assets => src/gi}/shaders/gi_ss_probe.wgsl (100%) diff --git a/src/gi/compositing.rs b/src/gi/compositing.rs index 13b5313..f71b749 100644 --- a/src/gi/compositing.rs +++ b/src/gi/compositing.rs @@ -150,11 +150,9 @@ impl CameraTargets } } -impl Material2d for PostProcessingMaterial -{ - fn fragment_shader() -> ShaderRef - { - "shaders/gi_post_processing.wgsl".into() +impl Material2d for PostProcessingMaterial { + fn fragment_shader() -> ShaderRef { + "embedded://bevy_magic_light_2d/gi/shaders/gi_post_processing.wgsl".into() } fn specialize( diff --git a/src/gi/constants.rs b/src/gi/constants.rs index 5c339f3..f83616c 100644 --- a/src/gi/constants.rs +++ b/src/gi/constants.rs @@ -4,13 +4,6 @@ use crate::gi::compositing::PostProcessingMaterial; pub const GI_SCREEN_PROBE_SIZE: i32 = 8; -pub const SHADER_GI_CAMERA: Handle = Handle::weak_from_u128(1371231089456109822); -pub const SHADER_GI_TYPES: Handle = Handle::weak_from_u128(4462033275253590181); -pub const SHADER_GI_ATTENUATION: Handle = Handle::weak_from_u128(5254739165481917368); -pub const SHADER_GI_HALTON: Handle = Handle::weak_from_u128(1287391288877821366); -pub const SHADER_GI_MATH: Handle = Handle::weak_from_u128(2387462894328787238); -pub const SHADER_GI_RAYMARCH: Handle = Handle::weak_from_u128(9876835068496322894); - pub const POST_PROCESSING_RECT: Handle = Handle::weak_from_u128(23475629871623176235); pub const POST_PROCESSING_MATERIAL: Handle = Handle::weak_from_u128(52374048672736472871); diff --git a/src/gi/mod.rs b/src/gi/mod.rs index 0089ee4..c801b59 100644 --- a/src/gi/mod.rs +++ b/src/gi/mod.rs @@ -1,4 +1,4 @@ -use bevy::asset::load_internal_asset; +use bevy::asset::embedded_asset; use bevy::prelude::*; use bevy::render::extract_resource::ExtractResourcePlugin; use bevy::render::render_graph::{self, RenderGraph, RenderLabel}; @@ -66,48 +66,19 @@ impl Plugin for BevyMagicLight2DPlugin .chain(), ) .add_systems(PreUpdate, handle_window_resize); + embedded_asset!(app, "shaders/gi_attenuation.wgsl"); + embedded_asset!(app, "shaders/gi_camera.wgsl"); + embedded_asset!(app, "shaders/gi_halton.wgsl"); + embedded_asset!(app, "shaders/gi_math.wgsl"); + embedded_asset!(app, "shaders/gi_post_processing.wgsl"); + embedded_asset!(app, "shaders/gi_raymarch.wgsl"); + embedded_asset!(app, "shaders/gi_sdf.wgsl"); + embedded_asset!(app, "shaders/gi_ss_blend.wgsl"); + embedded_asset!(app, "shaders/gi_ss_bounce.wgsl"); + embedded_asset!(app, "shaders/gi_ss_filter.wgsl"); + embedded_asset!(app, "shaders/gi_ss_probe.wgsl"); + embedded_asset!(app, "shaders/gi_types.wgsl"); - load_internal_asset!( - app, - SHADER_GI_CAMERA, - "shaders/gi_camera.wgsl", - Shader::from_wgsl - ); - - load_internal_asset!( - app, - SHADER_GI_TYPES, - "shaders/gi_types.wgsl", - Shader::from_wgsl - ); - - load_internal_asset!( - app, - SHADER_GI_ATTENUATION, - "shaders/gi_attenuation.wgsl", - Shader::from_wgsl - ); - - load_internal_asset!( - app, - SHADER_GI_HALTON, - "shaders/gi_halton.wgsl", - Shader::from_wgsl - ); - - load_internal_asset!( - app, - SHADER_GI_MATH, - "shaders/gi_math.wgsl", - Shader::from_wgsl - ); - - load_internal_asset!( - app, - SHADER_GI_RAYMARCH, - "shaders/gi_raymarch.wgsl", - Shader::from_wgsl - ); let render_app = app.sub_app_mut(RenderApp); render_app .add_systems(ExtractSchedule, system_extract_pipeline_assets) diff --git a/src/gi/pipeline.rs b/src/gi/pipeline.rs index 1ae20bc..be8d115 100644 --- a/src/gi/pipeline.rs +++ b/src/gi/pipeline.rs @@ -777,11 +777,11 @@ impl FromWorld for LightPassPipeline let (shader_sdf, gi_ss_probe, gi_ss_bounce, gi_ss_blend, gi_ss_filter) = { let assets_server = world.resource::(); ( - assets_server.load("shaders/gi_sdf.wgsl"), - assets_server.load("shaders/gi_ss_probe.wgsl"), - assets_server.load("shaders/gi_ss_bounce.wgsl"), - assets_server.load("shaders/gi_ss_blend.wgsl"), - assets_server.load("shaders/gi_ss_filter.wgsl"), + assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_sdf.wgsl"), + assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_probe.wgsl"), + assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_bounce.wgsl"), + assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_blend.wgsl"), + assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_filter.wgsl"), ) }; diff --git a/assets/shaders/gi_post_processing.wgsl b/src/gi/shaders/gi_post_processing.wgsl similarity index 100% rename from assets/shaders/gi_post_processing.wgsl rename to src/gi/shaders/gi_post_processing.wgsl diff --git a/assets/shaders/gi_sdf.wgsl b/src/gi/shaders/gi_sdf.wgsl similarity index 100% rename from assets/shaders/gi_sdf.wgsl rename to src/gi/shaders/gi_sdf.wgsl diff --git a/assets/shaders/gi_ss_blend.wgsl b/src/gi/shaders/gi_ss_blend.wgsl similarity index 100% rename from assets/shaders/gi_ss_blend.wgsl rename to src/gi/shaders/gi_ss_blend.wgsl diff --git a/assets/shaders/gi_ss_bounce.wgsl b/src/gi/shaders/gi_ss_bounce.wgsl similarity index 100% rename from assets/shaders/gi_ss_bounce.wgsl rename to src/gi/shaders/gi_ss_bounce.wgsl diff --git a/assets/shaders/gi_ss_filter.wgsl b/src/gi/shaders/gi_ss_filter.wgsl similarity index 100% rename from assets/shaders/gi_ss_filter.wgsl rename to src/gi/shaders/gi_ss_filter.wgsl diff --git a/assets/shaders/gi_ss_probe.wgsl b/src/gi/shaders/gi_ss_probe.wgsl similarity index 100% rename from assets/shaders/gi_ss_probe.wgsl rename to src/gi/shaders/gi_ss_probe.wgsl From 56a3ecb1ac68e615d945da7f8806aab9c586d404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rezan=20Kizilg=C3=BCn?= Date: Sun, 14 Jul 2024 14:11:51 +0200 Subject: [PATCH 2/4] Fixed loading and lifetime of embedded shaders # Conflicts: # src/gi/mod.rs --- src/gi/mod.rs | 7 ++++--- src/gi/pipeline_assets.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/gi/mod.rs b/src/gi/mod.rs index c801b59..ad42e09 100644 --- a/src/gi/mod.rs +++ b/src/gi/mod.rs @@ -19,9 +19,8 @@ use crate::gi::pipeline::{ LightPassPipelineBindGroups, }; use crate::gi::pipeline_assets::{ - system_extract_pipeline_assets, - system_prepare_pipeline_assets, - LightPassPipelineAssets, + system_extract_pipeline_assets, system_prepare_pipeline_assets, system_load_embedded_shader_dependencies, + LightPassPipelineAssets, EmbeddedShaderDependencies }; use crate::gi::resource::ComputedTargetSizes; use crate::prelude::BevyMagicLight2DSettings; @@ -56,9 +55,11 @@ impl Plugin for BevyMagicLight2DPlugin .init_resource::() .init_resource::() .init_resource::() + .init_resource::() .add_systems( PreStartup, ( + system_load_embedded_shader_dependencies, detect_target_sizes, system_setup_gi_pipeline.after(detect_target_sizes), setup_post_processing_camera.after(system_setup_gi_pipeline), diff --git a/src/gi/pipeline_assets.rs b/src/gi/pipeline_assets.rs index 3249ace..6f856c6 100644 --- a/src/gi/pipeline_assets.rs +++ b/src/gi/pipeline_assets.rs @@ -21,6 +21,26 @@ use crate::gi::types_gpu::{ use crate::prelude::BevyMagicLight2DSettings; use crate::FloorCamera; +#[rustfmt::skip] +#[derive(Default, Resource)] +pub(crate) struct EmbeddedShaderDependencies { + loaded_shaders: Vec>, +} + +#[rustfmt::skip] +pub(crate) fn system_load_embedded_shader_dependencies( + mut embedded_shader_deps: ResMut, + asset_server: Res, +) { + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_attenuation.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_camera.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_halton.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_math.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_post_processing.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_raymarch.wgsl")); + embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_types.wgsl")); +} + #[rustfmt::skip] #[derive(Default, Resource)] pub struct LightPassPipelineAssets { From 24a154d1ce150cdba9f94a0244d9be27769f4b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rezan=20Kizilg=C3=BCn?= Date: Mon, 15 Jul 2024 00:34:38 +0200 Subject: [PATCH 3/4] Introduced load_embedded_shader function for more concise shader loading --- src/gi/pipeline.rs | 12 ++++++------ src/gi/pipeline_assets.rs | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/gi/pipeline.rs b/src/gi/pipeline.rs index be8d115..a8975b7 100644 --- a/src/gi/pipeline.rs +++ b/src/gi/pipeline.rs @@ -10,7 +10,7 @@ use bevy::render::texture::{ ImageSamplerDescriptor, }; -use crate::gi::pipeline_assets::LightPassPipelineAssets; +use crate::gi::pipeline_assets::{LightPassPipelineAssets, load_embedded_shader}; use crate::gi::resource::ComputedTargetSizes; use crate::gi::types_gpu::{ GpuCameraParams, @@ -777,11 +777,11 @@ impl FromWorld for LightPassPipeline let (shader_sdf, gi_ss_probe, gi_ss_bounce, gi_ss_blend, gi_ss_filter) = { let assets_server = world.resource::(); ( - assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_sdf.wgsl"), - assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_probe.wgsl"), - assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_bounce.wgsl"), - assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_blend.wgsl"), - assets_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_ss_filter.wgsl"), + load_embedded_shader(&assets_server, "gi_sdf.wgsl"), + load_embedded_shader(&assets_server, "gi_ss_probe.wgsl"), + load_embedded_shader(&assets_server, "gi_ss_bounce.wgsl"), + load_embedded_shader(&assets_server, "gi_ss_blend.wgsl"), + load_embedded_shader(&assets_server, "gi_ss_filter.wgsl"), ) }; diff --git a/src/gi/pipeline_assets.rs b/src/gi/pipeline_assets.rs index 6f856c6..898ebf6 100644 --- a/src/gi/pipeline_assets.rs +++ b/src/gi/pipeline_assets.rs @@ -2,6 +2,8 @@ use bevy::prelude::*; use bevy::render::render_resource::{StorageBuffer, UniformBuffer}; use bevy::render::renderer::{RenderDevice, RenderQueue}; use bevy::render::Extract; +use bevy::asset::{AssetPath, io::AssetSourceId}; +use std::path::Path; use rand::{thread_rng, Rng}; use crate::gi::constants::GI_SCREEN_PROBE_SIZE; @@ -32,13 +34,19 @@ pub(crate) fn system_load_embedded_shader_dependencies( mut embedded_shader_deps: ResMut, asset_server: Res, ) { - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_attenuation.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_camera.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_halton.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_math.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_post_processing.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_raymarch.wgsl")); - embedded_shader_deps.loaded_shaders.push(asset_server.load("embedded://bevy_magic_light_2d/gi/shaders/gi_types.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_attenuation.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_camera.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_halton.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_math.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_post_processing.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_raymarch.wgsl")); + embedded_shader_deps.loaded_shaders.push(load_embedded_shader(&asset_server, "gi_types.wgsl")); +} + +pub(crate) fn load_embedded_shader(asset_server: &AssetServer, shader_file: &str) -> Handle { + let source = AssetSourceId::from("embedded"); + let path = Path::new("bevy_magic_light_2d").join("gi/shaders/"); + asset_server.load(AssetPath::from_path(&path.join(shader_file)).with_source(&source)) } #[rustfmt::skip] From 16e09bd9c2588e41cb6d5779529dd174d3479340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rezan=20Kizilg=C3=BCn?= Date: Mon, 15 Jul 2024 22:58:37 +0200 Subject: [PATCH 4/4] Added "embedded_watcher" feature to bevy dependency --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 0e7e5df..9be3824 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ bevy = { version = "0.13", default-features = false, features = [ "bevy_asset", "bevy_sprite", "bevy_pbr", + "embedded_watcher", ] } bevy-inspector-egui = { version = "0.23.0", optional = true } log = "0.4.20"