Skip to content

Shaders & Pipelines ​

Functions for shader compilation, pipeline creation, reflection, and uniform management.

vio_shader ​

php
VioShader|false vio_shader(VioContext $context, array $config)

Compile a shader program.

Config:

KeyTypeRequiredDescription
vertexstringYesVertex shader source
fragmentstringYesFragment shader source
formatintNoShader format (default: VIO_SHADER_AUTO)
geometrystringNoGeometry stage (VIO_FEATURE_GEOMETRY)
tess_control, tess_evalstringNoTessellation pair, always both (VIO_FEATURE_TESSELLATION); the pipeline then draws VIO_PATCHES
mesh, taskstringNoMesh pipeline: mesh replaces vertex, task is optional (VIO_FEATURE_MESH_SHADER, see Mesh Shaders)
hlslarrayNoD3D11 / D3D12: ['geometry' | 'tess_control' | 'tess_eval' | 'fragment' => HLSL] compiled as-is instead of the transpiled GLSL stage (VIO_FEATURE_HLSL_STAGE_OVERRIDE); the GLSL stage stays required
view_countintNo2–4: multiview shader (gl_ViewIndex, VIO_FEATURE_MULTIVIEW), drawn into the layers of a target bound with VIO_RT_ALL_LAYERS

Backends that report a stage's feature as 0 return false with a warning.

Shader Format Constants ​

ConstantValueDescription
VIO_SHADER_AUTO0Auto-detect (checks for SPIR-V magic number)
VIO_SHADER_SPIRV1Raw SPIR-V binary
VIO_SHADER_GLSL2GLSL → compile to SPIR-V → cross-compile
VIO_SHADER_MSL3Metal Shading Language (Metal backend only)
VIO_SHADER_GLSL_RAW4GLSL passed directly (OpenGL only)

HLSL is not a format: the D3D backends take HLSL through the hlsl key above (VIO_SHADER_HLSL is not registered as a PHP constant).

vio_pipeline ​

php
VioPipeline|false vio_pipeline(VioContext $context, array $config)

Create a render pipeline combining shader and render state.

Config:

KeyTypeDefaultDescription
shaderVioShaderrequiredCompiled shader
topologyintVIO_TRIANGLESPrimitive topology
cull_modeintVIO_CULL_NONEFace culling mode
depth_testboolfalseEnable depth testing
depth_writebooltrueWrite depth (disable for skies / transparents that still test)
depth_funcintVIO_DEPTH_LESSDepth comparison
blendintVIO_BLEND_NONEBlend mode
color_maskintVIO_COLOR_RGBAColour channels written (VIO_COLOR_R|G|B|A, VIO_COLOR_RGB)
depth_biasfloat0Constant depth bias (shadow acne)
slope_scaled_depth_biasfloat0Slope-scaled depth bias
hdrboolfalsePipeline renders into an RGBA16F (hdr) target – needed for the D3D12 / Metal PSO format
attachmentsint[]–VIO_FORMAT_* per colour attachment when rendering into an MRT target (D3D12 PSO)

Cull Mode Constants ​

ConstantValueDescription
VIO_CULL_NONE0No culling
VIO_CULL_BACK1Cull back faces
VIO_CULL_FRONT2Cull front faces

Blend Mode Constants ​

ConstantValueDescription
VIO_BLEND_NONE0No blending
VIO_BLEND_ALPHA1Standard alpha blending
VIO_BLEND_ADDITIVE2Additive blending
VIO_BLEND_PREMULTIPLIED3Premultiplied alpha (1, 1-src.a)
VIO_BLEND_MULTIPLY4dst.rgb * src.rgb
VIO_BLEND_SCREEN5Screen (1, 1-src.rgb)
VIO_BLEND_MIN6min(src, dst)
VIO_BLEND_MAX7max(src, dst)

Depth Constants ​

ConstantValueDescription
VIO_DEPTH_LESS0Pass if depth < buffer
VIO_DEPTH_LEQUAL1Pass if depth <= buffer

Colour Mask Constants ​

ConstantValue
VIO_COLOR_R1
VIO_COLOR_G2
VIO_COLOR_B4
VIO_COLOR_A8
VIO_COLOR_RGB7
VIO_COLOR_RGBA15

vio_bind_pipeline ​

php
void vio_bind_pipeline(VioContext $context, VioPipeline $pipeline)

Bind a pipeline for subsequent draw calls. Must be called within a frame (vio_begin / vio_end).

vio_shader_reflect ​

php
array|false vio_shader_reflect(VioShader $shader)

Introspect a compiled shader's interface. Requires SPIRV-Cross.

Returns:

php
[
    "vertex" => [
        "inputs" => [
            ["name" => "aPos", "location" => 0, "format" => "vec3"],
        ],
        "ubos" => [
            ["name" => "Matrices", "set" => 0, "binding" => 0, "size" => 192],
        ],
        "textures" => [],
        "uniforms" => [],
        "storage_buffers" => [
            ["name" => "Instances", "set" => 0, "binding" => 0],
        ],
    ],
    "fragment" => [
        "inputs" => [...],
        "ubos" => [],
        "textures" => [
            ["name" => "uTexture", "set" => 0, "binding" => 0],
        ],
        "uniforms" => [],
        "storage_buffers" => [],
    ],
]

Each stage also reports storage_buffers (SSBO / StructuredBuffer) as {name, set, binding}. This lets the engine validate a vertex-stage storage buffer binding for readback-free instancing (see vio_bind_storage_buffer and VIO_FEATURE_VERTEX_STORAGE).

vio_set_uniform ​

php
void vio_set_uniform(VioContext $context, string $name, int|float|array $value)

Set a uniform value by name. For simple uniforms outside of UBOs.

php
vio_set_uniform($ctx, "uTime", 3.14);                      // float
vio_set_uniform($ctx, "uColor", [1.0, 0.0, 0.0, 1.0]);    // vec4
vio_set_uniform($ctx, "uEnabled", 1);                       // int

Supports int, float, vec2/3/4 (flat array), mat3 (9 floats) and mat4 (16 floats). Uniforms that are not in the shader are silently ignored.

vio_set_uniforms ​

php
void vio_set_uniforms(VioContext $context, array $uniforms)

Batch form of vio_set_uniform(): apply a map of ['u_name' => value, ...] in a single native call. Each value follows the rules of vio_set_uniform(); the resulting uniform state is identical, without the per-uniform PHP → C call overhead on the hot draw path.

php
vio_set_uniforms($ctx, ['u_model' => $model, 'u_color' => [1.0, 1.0, 1.0, 1.0], 'u_time' => $t]);

Released under the MIT License.