pub fn update(
        &mut self,
        device: &wgpu::Device,
        encoder: &mut wgpu::CommandEncoder,
        object: K,
        data: &[T],
    ) {
        assert!(data.len() > 0, "cannot add an empty slice to a MultiBuffer");
        // Remove the object if it's already in the buffer
        self.remove(&object);
        // Try to find the position to insert
        let insert_position = self
            .segments
            .iter_mut()
            .position(|seg| seg.len >= data.len() && seg.free);
        let insert_position = insert_position.unwrap_or_else(|| {
            // Reallocate at least twice the size
            self.reallocate(device, encoder, (self.len + data.len()).max(2 * self.len));
            self.segments.len() - 1
        });
        // Copy data into the buffer
        let src_buffer = buffer_from_slice(
            device,
            wgpu::BufferUsage::COPY_SRC,
            to_u8_slice(data)
        );
        encoder.copy_buffer_to_buffer(
            &src_buffer,
            0,
            &self.buffer,
            (self.segments[insert_position].pos * std::mem::size_of::<T>()) as u64,
            (data.len() * std::mem::size_of::<T>()) as u64,
        );
        // Update current segment
        self.segments[insert_position].free = false;
        // Split the segment if necessary
        let extra_length = self.segments[insert_position].len - data.len();
        if extra_length > 0 {
            self.segments[insert_position].len -= extra_length;
            if insert_position < self.segments.len() - 1 && self.segments[insert_position + 1].free
            {
                self.segments[insert_position + 1].pos -= extra_length;
                self.segments[insert_position + 1].len += extra_length;
            } else {
                self.segments.insert(
                    insert_position + 1,
                    MultiBufferSegment {
                        free: true,
                        pos: self.segments[insert_position].pos + data.len(),
                        len: extra_length,
                    },
                );
            }
        }
        // Update the map
        self.objects
            .insert(object.clone(), self.segments[insert_position].pos);
    }
/// Update the data for object `object` in the buffer
///
/// # Panics
/// Will panic if `data` is empty.
// TODO: handle memory fragmentation
Update the data for object `object` in the buffer
Update the data for object ` object ` in the buffer
pub fn update(
        &mut self,
        device: &wgpu::Device,
        encoder: &mut wgpu::CommandEncoder,
        object: K,
        data: &[T],
    ) {
        assert!(data.len() > 0, "cannot add an empty slice to a MultiBuffer");
        // Remove the object if it's already in the buffer
        self.remove(&object);
        // Try to find the position to insert
        let insert_position = self
            .segments
            .iter_mut()
            .position(|seg| seg.len >= data.len() && seg.free);
        let insert_position = insert_position.unwrap_or_else(|| {
            // Reallocate at least twice the size
            self.reallocate(device, encoder, (self.len + data.len()).max(2 * self.len));
            self.segments.len() - 1
        });
        // Copy data into the buffer
        let src_buffer = buffer_from_slice(
            device,
            wgpu::BufferUsage::COPY_SRC,
            to_u8_slice(data)
        );
        encoder.copy_buffer_to_buffer(
            &src_buffer,
            0,
            &self.buffer,
            (self.segments[insert_position].pos * std::mem::size_of::<T>()) as u64,
            (data.len() * std::mem::size_of::<T>()) as u64,
        );
        // Update current segment
        self.segments[insert_position].free = false;
        // Split the segment if necessary
        let extra_length = self.segments[insert_position].len - data.len();
        if extra_length > 0 {
            self.segments[insert_position].len -= extra_length;
            if insert_position < self.segments.len() - 1 && self.segments[insert_position + 1].free
            {
                self.segments[insert_position + 1].pos -= extra_length;
                self.segments[insert_position + 1].len += extra_length;
            } else {
                self.segments.insert(
                    insert_position + 1,
                    MultiBufferSegment {
                        free: true,
                        pos: self.segments[insert_position].pos + data.len(),
                        len: extra_length,
                    },
                );
            }
        }
        // Update the map
        self.objects
            .insert(object.clone(), self.segments[insert_position].pos);
    }
pub fn update ( & mut self , device : & wgpu :: Device , encoder : & mut wgpu :: CommandEncoder , object : K , data : & [ T ] , ) { assert ! ( data len ( ) 0 " " ) ; // Remove the object if it's already in the buffer self . remove ( & object ) ; // Try to find the position to insert let insert_position = self . segments . iter_mut ( ) . position ( | seg | seg . len >= data . len ( ) && seg . free ) ; let insert_position = insert_position . unwrap_or_else ( | | { // Reallocate at least twice the size self . reallocate ( device , encoder , ( self . len + data . len ( ) ) . max ( 2 * self . len ) ) ; self . segments . len ( ) - 1 } ) ; // Copy data into the buffer let src_buffer = buffer_from_slice ( device , wgpu :: BufferUsage :: COPY_SRC , to_u8_slice ( data ) ) ; encoder . copy_buffer_to_buffer ( & src_buffer , 0 , & self . buffer , ( self . segments [ insert_position ] . pos * std :: mem :: size_of :: < T > ( ) ) as u64 , ( data . len ( ) * std :: mem :: size_of :: < T > ( ) ) as u64 , ) ; // Update current segment self . segments [ insert_position ] . free = false ; // Split the segment if necessary let extra_length = self . segments [ insert_position ] . len - data . len ( ) ; if extra_length > 0 { self . segments [ insert_position ] . len -= extra_length ; if insert_position < self . segments . len ( ) - 1 && self . segments [ insert_position + 1 ] . free { self . segments [ insert_position + 1 ] . pos -= extra_length ; self . segments [ insert_position + 1 ] . len += extra_length ; } else { self . segments . insert ( insert_position + 1 , MultiBufferSegment { free : true , pos : self . segments [ insert_position ] . pos + data . len ( ) , len : extra_length , } , ) ; } } // Update the map self . objects . insert ( object . clone ( ) , self . segments [ insert_position ] . pos ) ; }
Update the data for object `object` in the buffer
Update the data for object ` object ` in the buffer
&mut self
device
&wgpu::Device
encoder
&mut wgpu::CommandEncoder
object
K
data
&[T]
&mut self
device
&wgpu::Device
encoder
&mut wgpu::CommandEncoder
object
K
data
&[T]
panics
Will panic if `data` is empty.
TODO: handle memory fragmentation
Will panic if ` data ` is empty . TODO : handle memory fragmentation