Skip to content

Conversation

@vmarcella
Copy link
Member

Summary

Adds write_slice and write_bytes methods to Buffer in lambda-rs, providing more flexible ways to upload data to GPU buffers. These complement the existing write_value method by supporting raw byte slices and typed slices of plain-old-data values.

Related Issues

Changes

  • Added write_bytes(&self, gpu: &Gpu, offset: u64, data: &[u8]) for writing raw byte slices directly to a buffer
  • Added write_slice<T: Copy>(&self, gpu: &Gpu, offset: u64, data: &[T]) for writing slices of POD values (vertices, indices, instance data, uniform blocks)
  • Extracted value_as_bytes and slice_as_bytes helper functions to convert typed data to byte slices
  • Refactored write_value to use the new write_bytes internally for consistency
  • Added overflow protection in slice_as_bytes with a debug assertion for byte length overflow
  • Added unit tests: value_as_bytes_matches_native_bytes, slice_as_bytes_matches_native_bytes, and slice_as_bytes_empty_is_empty

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • Feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (updates to docs, specs, tutorials, or comments)
  • Refactor (code change that neither fixes a bug nor adds a feature)
  • Performance (change that improves performance)
  • Test (adding or updating tests)
  • Build/CI (changes to build process or CI configuration)

Affected Crates

  • lambda-rs
  • lambda-rs-platform
  • lambda-rs-args
  • lambda-rs-logging
  • Other:

Checklist

  • Code follows the repository style guidelines (cargo +nightly fmt --all)
  • Code passes clippy (cargo clippy --workspace --all-targets -- -D warnings)
  • Tests pass (cargo test --workspace)
  • New code includes appropriate documentation
  • Public API changes are documented
  • Breaking changes are noted in this PR description

Testing

Commands run:

cargo build --workspace
cargo test --workspace
cargo test -p lambda-rs -- --nocapture

Manual verification steps (if applicable):

  1. Run unit tests to verify byte conversion correctness
  2. Verify existing examples that use write_value continue to work

Screenshots/Recordings

N/A - No visual changes.

Platform Testing

  • macOS
  • Windows
  • Linux

Additional Notes

  • The T: Copy bound ensures only trivially copyable types can be written, which is required for safe byte reinterpretation
  • The slice_as_bytes function includes overflow protection that returns an empty slice and triggers a debug assertion if the byte length calculation would overflow
  • These methods are useful for uploading arrays of vertices, indices, instance transforms, or any other bulk data to GPU buffers

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds higher-level buffer upload APIs to lambda-rs by introducing Buffer::write_bytes and Buffer::write_slice, and refactors write_value to use shared byte-conversion helpers with accompanying unit tests.

Changes:

  • Added Buffer::write_bytes for uploading raw &[u8] directly to GPU buffers
  • Added Buffer::write_slice<T> plus value_as_bytes / slice_as_bytes helpers for uploading typed data as bytes
  • Added unit tests validating byte conversion for single values and slices (including empty slices)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vmarcella vmarcella merged commit 229960f into main Jan 24, 2026
5 checks passed
@vmarcella vmarcella deleted the vmarcella/buffer-writes branch January 24, 2026 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add write_bytes and write_slice Methods to Buffer

2 participants