Skip to content

Data buffers

zeganstyl edited this page Sep 18, 2020 · 1 revision

Buffers are needed to store data, for example bytes, and transfer them to the GPU.

Each platform has own implementation of buffers. For example in browser used ArrayBuffer, in JVM used Buffer and in Kotlin/Native used CPointer.

The principle of working with these buffers is similar to the principle of working with Java buffers.

The most used buffer is IByteData. It provides methods to access bytes, read and write any byte by index. From byte buffer may be created views to interpret bytes as floats, ints, shorts or may be created byte view to access only part of source buffer.

You can use DATA singleton to create byte buffers. And then you can create a view from this byte buffer for other types of buffers.

// create byte buffer with 16 bytes capacity.
val bytes = DATA.bytes(16)

// read first byte
val firstByte = bytes[0]

// write first byte
bytes[0] = 123

// set buffer part that will be viewed
bytes.position = 2
bytes.size = 4

// interpret bytes as floats
// we take only 4 bytes, so we will have only 1 float.
val floats = bytes.floatView()

// revert byte buffer to initial viewing state
bytes.position = 0
bytes.size = bytes.capacity
Clone this wiki locally