Skip to content

Commit

Permalink
Macro for Loading Internal Binary Assets (bevyengine#6478)
Browse files Browse the repository at this point in the history
# 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!`.
  • Loading branch information
cryscan authored and ItsDoot committed Feb 1, 2023
1 parent a742f56 commit ed63662
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/bevy_asset/src/assets.rs
Expand Up @@ -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;
Expand Down

0 comments on commit ed63662

Please sign in to comment.