Module luminance::buffer [] [src]

Static GPU typed arrays.

A GPU buffer is a typed continuous region of data. It has a size and can hold several elements.

Buffers are created with the new associated function. You pass in the number of elements you want in the buffer.

let buffer: Buffer<f32> = Buffer::new(5);

Once the buffer is created, you can perform several operations on them:

  • writing to them ;
  • reading from them ;
  • passing them around as uniforms ;
  • etc.

However, you cannot change their size.

Writing to a buffer

Buffers support several write methods. The simple one is clearing. That is, replacing the whole content of the buffer with a single value. Use the clear function to do so.

buffer.clear(0.);

If you want to clear the buffer by providing a value for each elements, you want filling. Use the fill function:

buffer.fill([1, 2, 3, 4, 5]);

If you want to change a value at a given index, you can use the set function.

buffer.set(3, 3.14);

Reading from the buffer

You can either retrieve the whole content of the Buffer or get a value with an index.

// get the whole content
let all_elems = buffer.whole();
assert_eq!(all_elems, vec![1, 2, 3, 3.14, 5]); // admit floating equalities

// get the element at index 3
assert_eq!(buffer.at(3), Some(3.14));

Uniform buffer

It’s possible to use buffers as uniform buffers. That is, buffers that will be in bound at rendering time and which content will be available for a shader to read (no write).

In order to use your buffers in a uniform context, the inner type has to implement UniformBlock. Keep in mind alignment must be respected and is a bit peculiar. TODO: explain std140 here.

Structs

Buffer

A Buffer is a GPU region you can picture as an array. It has a static size and cannot be resized. The size is expressed in number of elements lying in the buffer, not in bytes.

BufferSlice

A buffer slice mapped into GPU memory.

BufferSliceMut

A buffer mutable slice into GPU memory.

RawBuffer

Raw buffer. Any buffer can be converted to that type. However, keep in mind that even though type erasure is safe, creating a buffer from a raw buffer is not.

Enums

BufferError

Buffer errors.

Traits

UniformBlock

Typeclass of types that can be used inside a uniform block. You have to be extra careful when using uniform blocks and ensure you respect the OpenGL std140 alignment / size rules. This will be fixed in a future release.