From ed636624de919a812a3c88103d2eff8b19383061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=94=E7=A9=B6=E7=A4=BE=E4=BA=A4?= Date: Mon, 7 Nov 2022 19:44:15 +0000 Subject: [PATCH] Macro for Loading Internal Binary Assets (#6478) # Objective The `load_internal_asset` macro is helpful when creating rendering plugins, but it doesn't support load binary assets (like those compiled as spir-v). ## Solution Add a `load_internal_binary_asset` macro that use `include_bytes!`. --- crates/bevy_asset/src/assets.rs | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/bevy_asset/src/assets.rs b/crates/bevy_asset/src/assets.rs index f637250780b6a..19749df7cf897 100644 --- a/crates/bevy_asset/src/assets.rs +++ b/crates/bevy_asset/src/assets.rs @@ -443,6 +443,44 @@ macro_rules! load_internal_asset { }}; } +/// Loads an internal binary asset. +/// +/// Internal binary assets (e.g. spir-v shaders) are bundled directly into the app and can't be hot reloaded +/// using the conventional API. See `DebugAssetServerPlugin`. +#[cfg(feature = "debug_asset_server")] +#[macro_export] +macro_rules! load_internal_binary_asset { + ($app: ident, $handle: ident, $path_str: expr, $loader: expr) => {{ + { + let mut debug_app = $app + .world + .non_send_resource_mut::<$crate::debug_asset_server::DebugAssetApp>(); + $crate::debug_asset_server::register_handle_with_loader( + $loader, + &mut debug_app, + $handle, + file!(), + $path_str, + ); + } + let mut assets = $app.world.resource_mut::<$crate::Assets<_>>(); + assets.set_untracked($handle, ($loader)(include_bytes!($path_str).as_ref())); + }}; +} + +/// Loads an internal binary asset. +/// +/// Internal binary assets (e.g. spir-v shaders) are bundled directly into the app and can't be hot reloaded +/// using the conventional API. See `DebugAssetServerPlugin`. +#[cfg(not(feature = "debug_asset_server"))] +#[macro_export] +macro_rules! load_internal_binary_asset { + ($app: ident, $handle: ident, $path_str: expr, $loader: expr) => {{ + let mut assets = $app.world.resource_mut::<$crate::Assets<_>>(); + assets.set_untracked($handle, ($loader)(include_bytes!($path_str).as_ref())); + }}; +} + #[cfg(test)] mod tests { use bevy_app::App;