Skip to content

Meshes ​

Functions for creating and drawing 3D geometry.

vio_mesh ​

php
VioMesh|false vio_mesh(VioContext $context, array $config)

Create a mesh from vertex data.

Config:

KeyTypeRequiredDescription
verticesfloat[]YesFlat array of vertex data
layoutarrayYesVertex attribute layout
indicesint[]NoIndex buffer data
topologyintNoPrimitive topology (default: VIO_TRIANGLES)

Layout Format ​

Simple – auto-assigned locations starting at 0:

php
"layout" => [VIO_FLOAT3, VIO_FLOAT2, VIO_FLOAT3]
// location 0: vec3 (12 bytes)
// location 1: vec2 (8 bytes)
// location 2: vec3 (12 bytes)

Explicit – specify locations manually:

php
"layout" => [
    ["location" => 0, "components" => 3],
    ["location" => 2, "components" => 2],
]

Vertex Format Constants ​

ConstantComponentsSize
VIO_FLOAT11 float4 bytes
VIO_FLOAT22 floats8 bytes
VIO_FLOAT33 floats12 bytes
VIO_FLOAT44 floats16 bytes
VIO_INT11 int4 bytes
VIO_INT22 ints8 bytes
VIO_INT33 ints12 bytes
VIO_INT44 ints16 bytes

Vertex data is always uploaded as 32-bit floats, and only VIO_FLOAT1 – VIO_FLOAT4 set the component count – in a flat layout any other value (the VIO_INT* constants included) counts as 3 floats. VIO_UINT1 – VIO_UINT4 are deliberately not PHP constants (there is no integer vertex path; see Vertex Formats).

Indices ​

indices are stored as uint16 when every index is < 65536 (half the index bandwidth), otherwise as uint32 – on every backend. 'index_type' => VIO_INDEX_UINT32 forces 32-bit indices. 'adjacency' => true builds VIO_TRIANGLES_ADJACENCY indices for geometry shaders.

vio_mesh_index_bytes ​

php
int vio_mesh_index_bytes(VioMesh $mesh)

Bytes per index of the mesh's index buffer: 2 (uint16), 4 (uint32), 0 when the mesh is not indexed.

php
$small = vio_mesh($ctx, ['vertices' => $v, 'indices' => [0, 1, 2], 'layout' => [VIO_FLOAT3]]);
vio_mesh_index_bytes($small);   // 2

$wide = vio_mesh($ctx, ['vertices' => $v, 'indices' => [0, 1, 2], 'layout' => [VIO_FLOAT3],
                        'index_type' => VIO_INDEX_UINT32]);
vio_mesh_index_bytes($wide);    // 4

Topology Constants ​

ConstantDescription
VIO_TRIANGLESEvery 3 vertices form a triangle
VIO_TRIANGLE_STRIPEach new vertex forms a triangle with previous two
VIO_TRIANGLE_FANTriangles share the first vertex
VIO_LINESEvery 2 vertices form a line
VIO_LINE_STRIPConnected line segments
VIO_POINTSIndividual points
VIO_PATCHESTessellation patches (pipelines with tessellation stages)
VIO_LINES_ADJACENCY, VIO_LINE_STRIP_ADJACENCY, VIO_TRIANGLES_ADJACENCY, VIO_TRIANGLE_STRIP_ADJACENCYPrimitives with adjacency for geometry shaders

For draws whose arguments come from a GPU buffer, batched submission and recorded bundles see Batches, Bundles & Indirect Draws; for mesh / task shaders see Mesh Shaders.

vio_draw ​

php
void vio_draw(VioContext $context, VioMesh $mesh)

Draw a mesh using the currently bound pipeline.

vio_draw_instanced ​

php
void vio_draw_instanced(VioContext $context, VioMesh $mesh, float[] $matrices, int $instanceCount)

Draw a mesh multiple times with per-instance model matrices.

  • $matrices – Flat array of 4x4 matrices (16 floats per instance)
  • $instanceCount – Number of instances to draw
php
$matrices = [];
for ($i = 0; $i < 100; $i++) {
    // Identity matrix translated by ($i * 2, 0, 0)
    array_push($matrices,
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        $i * 2, 0, 0, 1
    );
}
vio_draw_instanced($ctx, $mesh, $matrices, 100);

vio_draw_3d ​

php
void vio_draw_3d(VioContext $context)

Flush/finalize 3D draw calls for the current frame.

Released under the MIT License.