Difference between revisions of "Buffer luau"

From GiderosMobile
(wip)
 
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
=== Description ===
 
=== Description ===
 
This is the Luau buffer library implemented in Gideros.
 
This is the Luau buffer library implemented in Gideros.
 +
 +
Buffers are not just fast, they also consume less memory. In Luau, every value is 16 bytes long, whatever it is. If you just need an array of bytes, using a buffer instead of a table saves 16x the memory.
 +
 +
'''A few Gideros calls have been updated to accept buffers as input, since it is also more efficient to use buffer data directly than to grab each array value individually. Specifically, you can now construct textures from buffers and even provide raw texture data, not necessarily RGBA'''
 +
 +
A buffer is an object that represents a fixed-size mutable block of memory. The buffer library provides functions for creation and manipulation of buffer objects, providing all its functions inside the global buffer variable.
 +
 +
Buffer is intended to be used as a low-level binary data storage structure, replacing the uses of string.pack() and string.unpack(). Use cases include reading and writing existing binary formats, working with data in a more compact form, serialization to custom binary formats, and general work with native memory types like fixed-length integers and floats.
 +
 +
Many of the functions accept an offset in bytes from the start of the buffer. Offset of 0 from the start of the buffer memory block accesses the first byte. All offsets, counts and sizes should be non-negative integer numbers. If the bytes that are accessed by any read or write operation are outside the buffer memory, an error is thrown.
 +
 +
The read and write methods that work with integers and floats use little-endian encoding.
  
 
=== Example ===
 
=== Example ===
Line 17: Line 29:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Reference ===
+
=== Disclaimer ===
 +
Documentation is based on '''create.roblox.com''' documentation.
 +
 
 
https://luau.org/library#buffer-library</br>
 
https://luau.org/library#buffer-library</br>
 
https://create.roblox.com/docs/reference/engine/libraries/buffer
 
https://create.roblox.com/docs/reference/engine/libraries/buffer
Line 24: Line 38:
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== Methods ===
 
=== Methods ===
[[assert]] ''error if v nil or false, otherwise returns v''<br/><!--GIDEROSMTD:assert(v,message) error if v nil or false, otherwise returns v-->
+
[[buffer.copy]] ''copies bytes between buffers''<br/><!--GIDEROSMTD:buffer.copy(targetbuffer,targetoffset,sourcebuffer,sourceoffset,count) copies bytes between buffers-->
[[collectgarbage]] ''opts: stop, restart, collect, count, step, setpause, setstepmul''<br/><!--GIDEROSMTD:collectgarbage(opt,arg) opts: stop, restart, collect, count, step, setpause, setstepmul-->
+
[[buffer.create]] ''creates a buffer''<br/><!--GIDEROSMTD:buffer.create(size) creates a buffer-->
[[ColorValue]] ''sets a Color the r,g,b,a channel values''<br/><!--GIDEROSMTD:ColorValue(r,g,b,a) sets a Color the r,g,b,a channel values-->
+
[[buffer.extract]] ''extracts from a buffer''<br/><!--GIDEROSMTD:buffer.extract(buffer,offset,count) extracts from a buffer-->
[[dofile]] ''executes as Lua chunk, default stdin, returns value''<br/><!--GIDEROSMTD:dofile(filename) executes as Lua chunk, default stdin, returns value-->
+
[[buffer.fill]] ''sets a region of the buffer memory to some 8-bit unsigned integer value''<br/><!--GIDEROSMTD:buffer.fill(buffer,offset,value,count) sets a region of the buffer memory to some 8-bit unsigned integer value-->
[[error]] ''terminates protected func, never returns''<br/><!--GIDEROSMTD:error(message,level) terminates protected func, never returns-->
+
[[buffer.fromstring]] ''creates a buffer from a string''<br/><!--GIDEROSMTD:buffer.fromstring(string) creates a buffer from a string-->
[[xpcall]] ''pcall function f with new error handler err''<br/><!--GIDEROSMTD:xpcall(f,err) pcall function f with new error handler err-->
+
[[buffer.len]] ''returns the size of the buffer in bytes''<br/><!--GIDEROSMTD:buffer.len(buffer) returns the size of the buffer in bytes-->
 +
[[buffer.read]] ''reads from the buffer''<br/><!--reads from the buffer-->
 +
[[buffer.readstring]] ''reads a string from the buffer''<br/><!--GIDEROSMTD:buffer.readstring(buffer,offset,count) reads a string from the buffer-->
 +
[[buffer.resize]] ''resizes the buffer''<br/><!--GIDEROSMTD:buffer.resize(buffer,newSize,offset) resizes the buffer-->
 +
[[buffer.setarrayaccess]] ''sets the buffer array access''<br/><!--GIDEROSMTD:buffer.setarrayaccess(buffer,dataType,dim2,dim3) sets the buffer array access-->
 +
[[buffer.tostring]] ''converts a buffer to a string''<br/><!--GIDEROSMTD:buffer.tostring(buffer) converts a buffer to a string-->
 +
[[buffer.write]] ''writes to the buffer''<br/><!--writes to the buffer-->
 +
[[buffer.writestring]] ''writes a string to the buffer''<br/><!--GIDEROSMTD:buffer.writestring(buffer,offset,value,count) writes a string to the buffer-->
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
Line 35: Line 56:
 
=== Constants ===
 
=== Constants ===
 
|}
 
|}
 +
 +
<!--GIDEROSMTD:buffer.readi8(buffer,offset) reads an 8-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu8(buffer,offset) reads an 8-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readi16(buffer,offset) reads a 16-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu16(buffer,offset) reads a 16-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readi32(buffer,offset) reads a 32-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu32(buffer,offset) reads a 32-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readf32(buffer,offset) reads a 32-bit floating-point value from the buffer-->
 +
<!--GIDEROSMTD:buffer.readf64(buffer,offset) reads a 64-bit floating-point value from the buffer-->
 +
<!--GIDEROSMTD:buffer.writei8(buffer,offset,value) writes an 8-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu8(buffer,offset,value) writes an 8-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writei16(buffer,offset,value) writes a 16-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu16(buffer,offset,value) writes a 16-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writei32(buffer,offset,value) writes a 32-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu32(buffer,offset,value) writes a 32-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writef32(buffer,offset,value) writes a 32-bit floating-point value to the buffer-->
 +
<!--GIDEROSMTD:buffer.writef64(buffer,offset,value) writes a 64-bit floating-point value to the buffer-->
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 08:49, 6 February 2025

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform linux.png
Available since: Gideros 2025.1

Description

This is the Luau buffer library implemented in Gideros.

Buffers are not just fast, they also consume less memory. In Luau, every value is 16 bytes long, whatever it is. If you just need an array of bytes, using a buffer instead of a table saves 16x the memory.

A few Gideros calls have been updated to accept buffers as input, since it is also more efficient to use buffer data directly than to grab each array value individually. Specifically, you can now construct textures from buffers and even provide raw texture data, not necessarily RGBA

A buffer is an object that represents a fixed-size mutable block of memory. The buffer library provides functions for creation and manipulation of buffer objects, providing all its functions inside the global buffer variable.

Buffer is intended to be used as a low-level binary data storage structure, replacing the uses of string.pack() and string.unpack(). Use cases include reading and writing existing binary formats, working with data in a more compact form, serialization to custom binary formats, and general work with native memory types like fixed-length integers and floats.

Many of the functions accept an offset in bytes from the start of the buffer. Offset of 0 from the start of the buffer memory block accesses the first byte. All offsets, counts and sizes should be non-negative integer numbers. If the bytes that are accessed by any read or write operation are outside the buffer memory, an error is thrown.

The read and write methods that work with integers and floats use little-endian encoding.

Example

-- creates a buffer initialized to the contents of the string
local str = "Hello Gideros!"
b = buffer.fromstring(str)
print(buffer.len(b))
-- returns the buffer data as a string
print(buffer.tostring(b))

Disclaimer

Documentation is based on create.roblox.com documentation.

https://luau.org/library#buffer-library
https://create.roblox.com/docs/reference/engine/libraries/buffer

Methods

buffer.copy copies bytes between buffers
buffer.create creates a buffer
buffer.extract extracts from a buffer
buffer.fill sets a region of the buffer memory to some 8-bit unsigned integer value
buffer.fromstring creates a buffer from a string
buffer.len returns the size of the buffer in bytes
buffer.read reads from the buffer
buffer.readstring reads a string from the buffer
buffer.resize resizes the buffer
buffer.setarrayaccess sets the buffer array access
buffer.tostring converts a buffer to a string
buffer.write writes to the buffer
buffer.writestring writes a string to the buffer

Events

Constants