Module nannou_wgpu::bytes

source ·
Expand description

The functions within this module use unsafe in order to retrieve their input as a slice of bytes. This is necessary in order to upload data to the GPU via the wgpu DeviceExt::create_buffer_init buffer constructor. This method is unsafe as the type T may contain padding which is considered to be uninitialised memory in Rust and may potentially lead to undefined behaviour.

These should be replaced in the future with something similar to zerocopy. Unfortunately, we don’t gain much benefit from using zerocopy in our case as zerocopy provides no way to implement the AsBytes trait for generic types (e.g. Vector*), even with their type parameters filled (e.g. Vector2<f32>). This means we can’t derive AsBytes for the majority of the types where we need to as derive(AsBytes) requires that all fields implement AsBytes, and neither our Vector types or the palette color types can implement it.

There is a relatively new crate bytemuck which provides traits for this, however these traits are unsafe and so we don’t gain much benefit in terms of safety, especially for our simple use-case. There is a zeroable crate that attempts to derive the Zeroable trait from bytemuck, however:

  1. there not yet any other publicly dependent crates or public discussion around the safety of the provided derives and
  2. we would still require implementing Pod unsafely.

Functions