
# Objective Now that #13432 has been merged, it's important we update our reflected types to properly opt into this feature. If we do not, then this could cause issues for users downstream who want to make use of reflection-based cloning. ## Solution This PR is broken into 4 commits: 1. Add `#[reflect(Clone)]` on all types marked `#[reflect(opaque)]` that are also `Clone`. This is mandatory as these types would otherwise cause the cloning operation to fail for any type that contains it at any depth. 2. Update the reflection example to suggest adding `#[reflect(Clone)]` on opaque types. 3. Add `#[reflect(clone)]` attributes on all fields marked `#[reflect(ignore)]` that are also `Clone`. This prevents the ignored field from causing the cloning operation to fail. Note that some of the types that contain these fields are also `Clone`, and thus can be marked `#[reflect(Clone)]`. This makes the `#[reflect(clone)]` attribute redundant. However, I think it's safer to keep it marked in the case that the `Clone` impl/derive is ever removed. I'm open to removing them, though, if people disagree. 4. Finally, I added `#[reflect(Clone)]` on all types that are also `Clone`. While not strictly necessary, it enables us to reduce the generated output since we can just call `Clone::clone` directly instead of calling `PartialReflect::reflect_clone` on each variant/field. It also means we benefit from any optimizations or customizations made in the `Clone` impl, including directly dereferencing `Copy` values and increasing reference counters. Along with that change I also took the liberty of adding any missing registrations that I saw could be applied to the type as well, such as `Default`, `PartialEq`, and `Hash`. There were hundreds of these to edit, though, so it's possible I missed quite a few. That last commit is **_massive_**. There were nearly 700 types to update. So it's recommended to review the first three before moving onto that last one. Additionally, I can break the last commit off into its own PR or into smaller PRs, but I figured this would be the easiest way of doing it (and in a timely manner since I unfortunately don't have as much time as I used to for code contributions). ## Testing You can test locally with a `cargo check`: ``` cargo check --workspace --all-features ```
86 lines
2.7 KiB
Rust
86 lines
2.7 KiB
Rust
use crate::{
|
|
extract_resource::ExtractResource,
|
|
prelude::Shader,
|
|
render_resource::{ShaderType, UniformBuffer},
|
|
renderer::{RenderDevice, RenderQueue},
|
|
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
|
|
};
|
|
use bevy_app::{App, Plugin};
|
|
use bevy_asset::{load_internal_asset, weak_handle, Handle};
|
|
use bevy_diagnostic::FrameCount;
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_reflect::prelude::*;
|
|
use bevy_time::Time;
|
|
|
|
pub const GLOBALS_TYPE_HANDLE: Handle<Shader> =
|
|
weak_handle!("9e22a765-30ca-4070-9a4c-34ac08f1c0e7");
|
|
|
|
pub struct GlobalsPlugin;
|
|
|
|
impl Plugin for GlobalsPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
load_internal_asset!(app, GLOBALS_TYPE_HANDLE, "globals.wgsl", Shader::from_wgsl);
|
|
app.register_type::<GlobalsUniform>();
|
|
|
|
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
|
|
render_app
|
|
.init_resource::<GlobalsBuffer>()
|
|
.init_resource::<Time>()
|
|
.add_systems(ExtractSchedule, (extract_frame_count, extract_time))
|
|
.add_systems(
|
|
Render,
|
|
prepare_globals_buffer.in_set(RenderSet::PrepareResources),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn extract_frame_count(mut commands: Commands, frame_count: Extract<Res<FrameCount>>) {
|
|
commands.insert_resource(**frame_count);
|
|
}
|
|
|
|
fn extract_time(mut commands: Commands, time: Extract<Res<Time>>) {
|
|
commands.insert_resource(**time);
|
|
}
|
|
|
|
/// Contains global values useful when writing shaders.
|
|
/// Currently only contains values related to time.
|
|
#[derive(Default, Clone, Resource, ExtractResource, Reflect, ShaderType)]
|
|
#[reflect(Resource, Default, Clone)]
|
|
pub struct GlobalsUniform {
|
|
/// The time since startup in seconds.
|
|
/// Wraps to 0 after 1 hour.
|
|
time: f32,
|
|
/// The delta time since the previous frame in seconds
|
|
delta_time: f32,
|
|
/// Frame count since the start of the app.
|
|
/// It wraps to zero when it reaches the maximum value of a u32.
|
|
frame_count: u32,
|
|
/// WebGL2 structs must be 16 byte aligned.
|
|
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
|
|
_wasm_padding: f32,
|
|
}
|
|
|
|
/// The buffer containing the [`GlobalsUniform`]
|
|
#[derive(Resource, Default)]
|
|
pub struct GlobalsBuffer {
|
|
pub buffer: UniformBuffer<GlobalsUniform>,
|
|
}
|
|
|
|
fn prepare_globals_buffer(
|
|
render_device: Res<RenderDevice>,
|
|
render_queue: Res<RenderQueue>,
|
|
mut globals_buffer: ResMut<GlobalsBuffer>,
|
|
time: Res<Time>,
|
|
frame_count: Res<FrameCount>,
|
|
) {
|
|
let buffer = globals_buffer.buffer.get_mut();
|
|
buffer.time = time.elapsed_secs_wrapped();
|
|
buffer.delta_time = time.delta_secs();
|
|
buffer.frame_count = frame_count.0;
|
|
|
|
globals_buffer
|
|
.buffer
|
|
.write_buffer(&render_device, &render_queue);
|
|
}
|