Path: blob/main/crates/bevy_render/src/render_resource/buffer.rs
6596 views
use crate::define_atomic_id;1use crate::renderer::WgpuWrapper;2use core::ops::{Bound, Deref, RangeBounds};34define_atomic_id!(BufferId);56#[derive(Clone, Debug)]7pub struct Buffer {8id: BufferId,9value: WgpuWrapper<wgpu::Buffer>,10}1112impl Buffer {13#[inline]14pub fn id(&self) -> BufferId {15self.id16}1718pub fn slice(&self, bounds: impl RangeBounds<wgpu::BufferAddress>) -> BufferSlice<'_> {19// need to compute and store this manually because wgpu doesn't export offset and size on wgpu::BufferSlice20let offset = match bounds.start_bound() {21Bound::Included(&bound) => bound,22Bound::Excluded(&bound) => bound + 1,23Bound::Unbounded => 0,24};25let size = match bounds.end_bound() {26Bound::Included(&bound) => bound + 1,27Bound::Excluded(&bound) => bound,28Bound::Unbounded => self.value.size(),29} - offset;30BufferSlice {31id: self.id,32offset,33size,34value: self.value.slice(bounds),35}36}3738#[inline]39pub fn unmap(&self) {40self.value.unmap();41}42}4344impl From<wgpu::Buffer> for Buffer {45fn from(value: wgpu::Buffer) -> Self {46Buffer {47id: BufferId::new(),48value: WgpuWrapper::new(value),49}50}51}5253impl Deref for Buffer {54type Target = wgpu::Buffer;5556#[inline]57fn deref(&self) -> &Self::Target {58&self.value59}60}6162#[derive(Clone, Debug)]63pub struct BufferSlice<'a> {64id: BufferId,65offset: wgpu::BufferAddress,66value: wgpu::BufferSlice<'a>,67size: wgpu::BufferAddress,68}6970impl<'a> BufferSlice<'a> {71#[inline]72pub fn id(&self) -> BufferId {73self.id74}7576#[inline]77pub fn offset(&self) -> wgpu::BufferAddress {78self.offset79}8081#[inline]82pub fn size(&self) -> wgpu::BufferAddress {83self.size84}85}8687impl<'a> Deref for BufferSlice<'a> {88type Target = wgpu::BufferSlice<'a>;8990#[inline]91fn deref(&self) -> &Self::Target {92&self.value93}94}959697