Path: blob/main/crates/bevy_render/src/gpu_component_array_buffer.rs
6595 views
use crate::{1render_resource::{GpuArrayBuffer, GpuArrayBufferable},2renderer::{RenderDevice, RenderQueue},3Render, RenderApp, RenderSystems,4};5use bevy_app::{App, Plugin};6use bevy_ecs::{7prelude::{Component, Entity},8schedule::IntoScheduleConfigs,9system::{Commands, Query, Res, ResMut},10};11use core::marker::PhantomData;1213/// This plugin prepares the components of the corresponding type for the GPU14/// by storing them in a [`GpuArrayBuffer`].15pub struct GpuComponentArrayBufferPlugin<C: Component + GpuArrayBufferable>(PhantomData<C>);1617impl<C: Component + GpuArrayBufferable> Plugin for GpuComponentArrayBufferPlugin<C> {18fn build(&self, app: &mut App) {19if let Some(render_app) = app.get_sub_app_mut(RenderApp) {20render_app.add_systems(21Render,22prepare_gpu_component_array_buffers::<C>.in_set(RenderSystems::PrepareResources),23);24}25}2627fn finish(&self, app: &mut App) {28if let Some(render_app) = app.get_sub_app_mut(RenderApp) {29render_app.insert_resource(GpuArrayBuffer::<C>::new(30render_app.world().resource::<RenderDevice>(),31));32}33}34}3536impl<C: Component + GpuArrayBufferable> Default for GpuComponentArrayBufferPlugin<C> {37fn default() -> Self {38Self(PhantomData::<C>)39}40}4142fn prepare_gpu_component_array_buffers<C: Component + GpuArrayBufferable>(43mut commands: Commands,44render_device: Res<RenderDevice>,45render_queue: Res<RenderQueue>,46mut gpu_array_buffer: ResMut<GpuArrayBuffer<C>>,47components: Query<(Entity, &C)>,48) {49gpu_array_buffer.clear();5051let entities = components52.iter()53.map(|(entity, component)| (entity, gpu_array_buffer.push(component.clone())))54.collect::<Vec<_>>();55commands.try_insert_batch(entities);5657gpu_array_buffer.write_buffer(&render_device, &render_queue);58}596061