Skip to content

Commit

Permalink
Merge pull request #60 from Rezan7CC/main
Browse files Browse the repository at this point in the history
Usage of embedded assets for shaders which are loaded and stored to ensure lifetime
  • Loading branch information
zaycev authored Jul 18, 2024
2 parents 4a95369 + 16e09bd commit 8ed784e
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions src/gi/compositing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 0 additions & 7 deletions src/gi/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ use crate::gi::compositing::PostProcessingMaterial;

pub const GI_SCREEN_PROBE_SIZE: i32 = 8;

pub const SHADER_GI_CAMERA: Handle<Shader> = Handle::weak_from_u128(1371231089456109822);
pub const SHADER_GI_TYPES: Handle<Shader> = Handle::weak_from_u128(4462033275253590181);
pub const SHADER_GI_ATTENUATION: Handle<Shader> = Handle::weak_from_u128(5254739165481917368);
pub const SHADER_GI_HALTON: Handle<Shader> = Handle::weak_from_u128(1287391288877821366);
pub const SHADER_GI_MATH: Handle<Shader> = Handle::weak_from_u128(2387462894328787238);
pub const SHADER_GI_RAYMARCH: Handle<Shader> = Handle::weak_from_u128(9876835068496322894);

pub const POST_PROCESSING_RECT: Handle<Mesh> = Handle::weak_from_u128(23475629871623176235);
pub const POST_PROCESSING_MATERIAL: Handle<PostProcessingMaterial> =
Handle::weak_from_u128(52374048672736472871);
62 changes: 17 additions & 45 deletions src/gi/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -56,58 +55,31 @@ impl Plugin for BevyMagicLight2DPlugin
.init_resource::<GiTargetsWrapper>()
.init_resource::<BevyMagicLight2DSettings>()
.init_resource::<ComputedTargetSizes>()
.init_resource::<EmbeddedShaderDependencies>()
.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),
)
.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)
Expand Down
12 changes: 6 additions & 6 deletions src/gi/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<AssetServer>();
(
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"),
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"),
)
};

Expand Down
28 changes: 28 additions & 0 deletions src/gi/pipeline_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +23,32 @@ use crate::gi::types_gpu::{
use crate::prelude::BevyMagicLight2DSettings;
use crate::FloorCamera;

#[rustfmt::skip]
#[derive(Default, Resource)]
pub(crate) struct EmbeddedShaderDependencies {
loaded_shaders: Vec<Handle<Shader>>,
}

#[rustfmt::skip]
pub(crate) fn system_load_embedded_shader_dependencies(
mut embedded_shader_deps: ResMut<EmbeddedShaderDependencies>,
asset_server: Res<AssetServer>,
) {
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<Shader> {
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]
#[derive(Default, Resource)]
pub struct LightPassPipelineAssets {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8ed784e

Please sign in to comment.