
# Objective - Add a type for uploading a Rust `Vec<T>` to a GPU `array<T>`. - Makes progress towards https://github.com/bevyengine/bevy/issues/89. ## Solution - Port @superdump's `BatchedUniformBuffer` to bevy main, as a fallback for WebGL2, which doesn't support storage buffers. - Rather than getting an `array<T>` in a shader, you get an `array<T, N>`, and have to rebind every N elements via dynamic offsets. - Add `GpuArrayBuffer` to abstract over `StorageBuffer<Vec<T>>`/`BatchedUniformBuffer`. ## Future Work Add a shader macro kinda thing to abstract over the following automatically: https://github.com/bevyengine/bevy/pull/8204#pullrequestreview-1396911727 --- ## Changelog * Added `GpuArrayBuffer`, `GpuComponentArrayBufferPlugin`, `GpuArrayBufferable`, and `GpuArrayBufferIndex` types. * Added `DynamicUniformBuffer::new_with_alignment()`. --------- Co-authored-by: Robert Swain <robert.swain@gmail.com> Co-authored-by: François <mockersf@gmail.com> Co-authored-by: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com> Co-authored-by: IceSentry <IceSentry@users.noreply.github.com> Co-authored-by: Vincent <9408210+konsolas@users.noreply.github.com> Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
56 lines
1.8 KiB
Rust
56 lines
1.8 KiB
Rust
use crate::{
|
|
render_resource::{GpuArrayBuffer, GpuArrayBufferable},
|
|
renderer::{RenderDevice, RenderQueue},
|
|
Render, RenderApp, RenderSet,
|
|
};
|
|
use bevy_app::{App, Plugin};
|
|
use bevy_ecs::{
|
|
prelude::{Component, Entity},
|
|
schedule::IntoSystemConfigs,
|
|
system::{Commands, Query, Res, ResMut},
|
|
};
|
|
use std::marker::PhantomData;
|
|
|
|
/// This plugin prepares the components of the corresponding type for the GPU
|
|
/// by storing them in a [`GpuArrayBuffer`].
|
|
pub struct GpuComponentArrayBufferPlugin<C: Component + GpuArrayBufferable>(PhantomData<C>);
|
|
|
|
impl<C: Component + GpuArrayBufferable> Plugin for GpuComponentArrayBufferPlugin<C> {
|
|
fn build(&self, app: &mut App) {
|
|
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
|
|
render_app
|
|
.insert_resource(GpuArrayBuffer::<C>::new(
|
|
render_app.world.resource::<RenderDevice>(),
|
|
))
|
|
.add_systems(
|
|
Render,
|
|
prepare_gpu_component_array_buffers::<C>.in_set(RenderSet::Prepare),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<C: Component + GpuArrayBufferable> Default for GpuComponentArrayBufferPlugin<C> {
|
|
fn default() -> Self {
|
|
Self(PhantomData::<C>)
|
|
}
|
|
}
|
|
|
|
fn prepare_gpu_component_array_buffers<C: Component + GpuArrayBufferable>(
|
|
mut commands: Commands,
|
|
render_device: Res<RenderDevice>,
|
|
render_queue: Res<RenderQueue>,
|
|
mut gpu_array_buffer: ResMut<GpuArrayBuffer<C>>,
|
|
components: Query<(Entity, &C)>,
|
|
) {
|
|
gpu_array_buffer.clear();
|
|
|
|
let entities = components
|
|
.iter()
|
|
.map(|(entity, component)| (entity, gpu_array_buffer.push(component.clone())))
|
|
.collect::<Vec<_>>();
|
|
commands.insert_or_spawn_batch(entities);
|
|
|
|
gpu_array_buffer.write_buffer(&render_device, &render_queue);
|
|
}
|