The stride of the Buffer, in bytes. When nil, the stride will be automatically computed based on the fields. The stride can not be zero or smaller than the max byte occupied by one of the fields. The layout of the Buffer may adjust the stride.
The stride of the Buffer, in bytes. When nil, the stride will be automatically computed based on the fields. The stride can not be zero or smaller than the max byte occupied by one of the fields. The layout of the Buffer may adjust the stride.
The initial data to put into the Buffer. The length of the Buffer will be determined by the length of the table or the size of the Blob, combined with the format information.
The format table can contain a list of DataTypes or a list of tables to provide extra information about each field. Each inner table has the following keys:
type is the DataType of the field and is required.
name is the name of the field. The field name is used to match table keys up to buffer
fields when writing table data to the Buffer, and is also used to match up buffer fields with
vertex attribute names declared in a Shader. LÖVR has a set of default vertex attributes that shaders will automatically
use, allowing you to create a custom mesh without having to write shader code or add custom
vertex attributes in a shader.
offset is the byte offset of the field. Any fields with a nil offset will be placed next
to each other sequentially in memory, subject to any padding required by the Buffer's layout.
In practice this means that you probably want to provide an offset for either all of the
fields or none of them.
length is the array size of the field (optional, leave as nil for non-arrays).
stride is the number of bytes between each item in an array (optional).
As a shorthand, the name, type, and optionally the length of a field can be provided as a list instead of using keys.
If no table or Blob is used to define the initial Buffer contents, its data will be undefined.
Example
Examples of different buffer formats.
-- 2 matrices
lovr.graphics.newBuffer('mat4', 2)
-- 3 integers, with initial data
lovr.graphics.newBuffer('int', { 1, 2, 3 })
-- a simple mesh:
lovr.graphics.newBuffer({
{ name = 'VertexPosition', type = 'vec3' },
{ name = 'VertexColor', type = 'color' }
}, 4)
-- a uniform buffer with vec3's, using the std140 packing
lovr.graphics.newBuffer({ 'vec3', layout = 'std140' }, data)
-- a uniform buffer with key-value fields
lovr.graphics.newBuffer({
{ 'AmbientColor', 'vec3' },
{ 'LightPosition', 'vec3' },
{ 'LightType', 'u32' },
{ 'LightColor', 'vec4' },
layout = 'std140'
})
-- a buffer with nested structure and array types
lovr.graphics.newBuffer({
{ 'globals', {
{ 'ObjectCount', 'int' },
{ 'WorldSize', 'vec2' },
{ 'Scale', 'float' }
}},
{ 'materials', {
{ 'Color', 'vec4' },
{ 'Glow', 'vec3' },
{ 'Roughness', 'float' }
}, length = 32 },
layout = 'std430'
})
-- a buffer using a variable from a shader:
lovr.graphics.newBuffer(shader:getBufferFormat('transforms'))