wde_renderer/assets/
buffer.rs

1use super::asset::RenderAsset;
2use crate::core::RenderInstance;
3use bevy::{ecs::system::lifetimeless::SRes, prelude::*};
4use wde_wgpu::buffer::{Buffer as WBuffer, BufferUsage as WBufferUsage};
5
6/// Stores a CPU buffer with raw byte data and metadata for GPU allocation.
7/// This buffer will be uploaded to the GPU, represented by a [`GpuBuffer`] asset.
8#[derive(Asset, TypePath, Clone)]
9pub struct Buffer {
10    pub label: String,
11    /// Size in bytes of the allocation.
12    pub size: usize,
13    /// Usage flags (storage, uniform, copy, etc.).
14    pub usage: WBufferUsage,
15    /// Optional initial payload copied to the GPU when present.
16    pub content: Option<Vec<u8>>
17}
18
19/// Represents a GPU buffer resource allocated from a CPU [`Buffer`] asset.
20pub struct GpuBuffer {
21    pub label: String,
22    /// Handle to the GPU buffer allocated via `wde-wgpu`.
23    pub buffer: WBuffer
24}
25impl RenderAsset for GpuBuffer {
26    type SourceAsset = Buffer;
27    type Params = SRes<RenderInstance>;
28
29    fn prepare(
30        asset: Self::SourceAsset,
31        render_instance: &mut bevy::ecs::system::SystemParamItem<Self::Params>
32    ) -> Result<Self, super::asset::PrepareAssetError<Self::SourceAsset>> {
33        let render_instance = render_instance.0.read().unwrap();
34        let buffer = WBuffer::new(
35            &render_instance,
36            asset.label.as_str(),
37            asset.size,
38            asset.usage,
39            asset.content.as_deref()
40        );
41        Ok(GpuBuffer {
42            label: asset.label,
43            buffer
44        })
45    }
46
47    fn label(&self) -> &str {
48        &self.label
49    }
50}