Skip to content

[Feature] Add write_bytes and write_slice Methods to Buffer #80

@vmarcella

Description

@vmarcella

Overview

Extend the high-level Buffer API with methods for writing raw bytes and typed slices, complementing the existing single-value write_value<T>

Current State

Buffer currently only exposes write_value<T> for uploading a single piece of data value:

// crates/lambda-rs/src/render/buffer.rs
impl Buffer {
  /// Write a single plain-old-data value into this buffer at the specified
  /// byte offset.
  pub fn write_value<T: Copy>(
    &self,
    render_context: &RenderContext,
    offset: u64,
    data: &T,
  ) {
    let bytes = unsafe {
      std::slice::from_raw_parts(
        (data as *const T) as *const u8,
        std::mem::size_of::<T>(),
      )
    };
    self.buffer.write_bytes(render_context.gpu(), offset, bytes);
  }
}

Common use cases however require writing arrays of vertices, instance data, or raw byte buffers.

Scope

  • Add write_bytes(&self, ctx, offset, &[u8]) for raw byte uploads
  • Add write_slice<T: Copy>(&self, ctx, offset, &[T]) for typed array uploads

Proposed API

impl Buffer {
  /// Write raw bytes into this buffer at the specified byte offset.
  pub fn write_bytes(
    &self,
    render_context: &RenderContext,
    offset: u64,
    data: &[u8],
  );

  /// Write a slice of plain-old-data values into this buffer at the
  /// specified byte offset.
  pub fn write_slice<T: Copy>(
    &self,
    render_context: &RenderContext,
    offset: u64,
    data: &[T],
  );
}

Example Usage

// Update instance transforms each frame
let transforms: Vec<InstanceTransform> = compute_transforms();
instance_buffer.write_slice(&render_context, 0, &transforms);

// Or raw bytes from an external source
let raw_data: &[u8] = load_binary_data();
buffer.write_bytes(&render_context, 0, raw_data);

Acceptance criteria

  • Buffer::write_bytes method
  • Buffer::write_slice<T> method
  • Unit tests for each implementation
  • Documentation with examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions