Skip to main content

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        _id: AssetId<Self::SourceAsset>,
31        asset: Self::SourceAsset,
32        render_instance: &mut bevy::ecs::system::SystemParamItem<Self::Params>
33    ) -> Result<Self, super::asset::PrepareAssetError<Self::SourceAsset>> {
34        let render_instance = render_instance.0.read().unwrap();
35        let buffer = WBuffer::new(
36            &render_instance,
37            asset.label.as_str(),
38            asset.size,
39            asset.usage,
40            asset.content.as_deref()
41        );
42        Ok(GpuBuffer {
43            label: asset.label,
44            buffer
45        })
46    }
47
48    fn label(&self) -> &str {
49        &self.label
50    }
51}