Skip to content

Textures ​

Functions for loading, creating, and binding GPU textures.

vio_texture ​

php
VioTexture|false vio_texture(VioContext $context, array $config)

Create a texture from a file or raw pixel data.

Config (file-based):

KeyTypeDescription
filestringPath to image file (PNG, JPG, BMP, TGA)

Config (raw data):

KeyTypeDescription
datastringRaw RGBA pixel data
widthintTexture width in pixels
heightintTexture height in pixels

Optional keys (both modes):

KeyTypeDefaultDescription
filterintVIO_FILTER_LINEARTexture filtering
wrapintVIO_WRAP_REPEATTexture wrapping
mipmapsboolfalseUpload a full mip chain (trilinear sampling)
anisotropyint1Anisotropic filtering level 1–16 (v2.10; OpenGL, D3D11, D3D12, Vulkan – Metal ignores it)
storageboolfalseStorage image for compute (image2D, see Compute); data is optional (zero-initialised)
formatintVIO_FORMAT_RGBA8Or VIO_FORMAT_BC1 / BC3 / BC4 / BC5 / BC7 for block-compressed data (VIO_FEATURE_TEXTURE_COMPRESSION_BC), VIO_FORMAT_ASTC_* (VIO_FEATURE_TEXTURE_COMPRESSION_ASTC)
layersint1> 1: 2D texture array for sampler2DArray (VIO_FEATURE_TEXTURE_ARRAY)
mip_levelsint–Levels already contained in data: level-major, the layers consecutive inside a level, images tightly packed (the KTX2 layout)

Filter Constants ​

ConstantValueDescription
VIO_FILTER_NEAREST0Nearest-neighbor (pixelated)
VIO_FILTER_LINEAR1Bilinear filtering (smooth)

Wrap Constants ​

ConstantValueDescription
VIO_WRAP_REPEAT0Tile the texture
VIO_WRAP_CLAMP1Clamp to edge pixels
VIO_WRAP_MIRROR2Mirror at edges

vio_texture_3d ​

php
VioTexture|false vio_texture_3d(VioContext $context, array $config)

Create a 3D / volume texture from raw RGBA8 voxel data. Bind it with vio_bind_texture() and sample it in GLSL with a sampler3D.

Volume textures back features like Fieldtracing's baked Signed Distance Field and SH-L1 irradiance fields, where each voxel stores per-point data the shader trilinearly interpolates.

Config:

KeyTypeDefaultDescription
datastring–Raw RGBA8 voxels, width*height*depth*4 bytes, Z-slices in ascending order
widthint–Volume width in voxels
heightint–Volume height in voxels
depthint–Volume depth in voxels (number of Z-slices)
filterintVIO_FILTER_LINEARTexture filtering
wrapintVIO_WRAP_CLAMPTexture wrapping (clamp is the usual choice for volumes)
anisotropyint1Anisotropic filtering level 1–16
storageboolfalseStorage image for compute (image3D)

Returns false on a backend that has no 3D-texture path, on a size/data mismatch, or on upload failure. Probe support with vio_supports_feature($ctx, VIO_FEATURE_TEXTURE_3D) before relying on it – OpenGL, D3D11, D3D12, Metal and Vulkan all report it.

php
// width*height*depth voxels, 4 bytes each (RGBA8)
$volume = vio_texture_3d($ctx, [
    "data"   => $voxels,            // string of w*h*d*4 bytes
    "width"  => 64,
    "height" => 64,
    "depth"  => 64,
    "filter" => VIO_FILTER_LINEAR,  // trilinear interpolation
    "wrap"   => VIO_WRAP_CLAMP,
]);

vio_bind_texture($ctx, $volume, 3);  // sample with a sampler3D bound to slot 3

vio_texture_ktx2 ​

php
VioTexture|VioCubemap|false vio_texture_ktx2(VioContext $context, string $bytes, ?array $options = null)

Create a texture from a KTX2 container held in memory. The stored mip chain is uploaded as-is.

  • 2D and 2D-array files in R8, RGBA8, BC1 / BC3 / BC4 / BC5 / BC7 return a VioTexture. sRGB variants load as UNORM; supercompressed files are not supported.
  • A cubemap file (faceCount 6) returns a VioCubemap, a 3D file (pixelDepth > 1) a 3D VioTexture – both uncompressed (RGBA8; R8 expands to grey), the base level is mip_offset; a cube with more levels gets its chain rebuilt.
OptionDefaultDescription
mip_offset0Drop the N largest levels – texture-quality tiers
filterVIO_FILTER_LINEARTexture filtering
wrapVIO_WRAP_REPEATTexture wrapping
anisotropy–1–16
mipmapsfalseGenerate a chain for a single-level uncompressed file

Returns false with a warning for unsupported containers or formats. Block-compressed files need VIO_FEATURE_TEXTURE_COMPRESSION_BC, arrays VIO_FEATURE_TEXTURE_ARRAY; both are reported by OpenGL, D3D11, D3D12, Vulkan and Metal.

php
$albedo = vio_texture_ktx2($ctx, file_get_contents('rock_bc7.ktx2'), [
    'mip_offset' => $lowQuality ? 1 : 0,
    'anisotropy' => 8,
]);
vio_bind_texture($ctx, $albedo, 0);

Sampler budget

A shader stage can bind as many textures as the backend has texture units – vio_texture_units()['units'], 16 on Direct3D 11 and Metal and at least that elsewhere (since v2.42.0; before, 12 regular and 4 comparison samplers). 2D and 3D textures, arrays and cubemaps all draw from this pool, and any unit may be a comparison (shadow) sampler.

vio_bind_texture ​

php
void vio_bind_texture(VioContext $context, VioTexture $texture, int $slot = 0)

Bind a texture to a texture unit. Slot corresponds to the binding qualifier in GLSL. Works for 2D textures, 3D / volume textures (vio_texture_3d) and cubemaps alike.

vio_texture_size ​

php
array{0: int, 1: int} vio_texture_size(VioTexture $texture)

Returns [width, height] in pixels.

vio_texture_update ​

php
bool vio_texture_update(VioContext $context, VioTexture $texture, string $data, int $x = 0, int $y = 0, int $width = 0, int $height = 0)

Upload RGBA8 pixels into an existing texture – a sub-region ($x, $y, $width, $height) or, with the size arguments omitted, the whole texture. Meant for streaming content (video frames, dynamic atlases) without recreating the texture. Available on every backend (D3D12 since v2.10).

php
vio_texture_update($ctx, $tex, $frameRgba);                  // whole texture
vio_texture_update($ctx, $atlas, $glyphRgba, 128, 64, 32, 32); // 32x32 region at (128,64)

Mip chains of textures and cubemaps can be (re)built with vio_generate_mipmaps().

vio_texture_load_async ​

php
mixed vio_texture_load_async(string $path)

Start loading a texture file asynchronously in a background thread. Returns an opaque handle.

vio_texture_load_poll ​

php
array|null|false vio_texture_load_poll(mixed $handle)

Poll the status of an async texture load.

Returns:

  • null – Still loading
  • false – Load failed
  • array – Load complete: ["width" => int, "height" => int, "data" => string]
php
$handle = vio_texture_load_async("large_texture.png");

// Check later
while (true) {
    $result = vio_texture_load_poll($handle);
    if ($result === null) {
        // Still loading, do other work
        continue;
    }
    if ($result === false) {
        echo "Failed to load texture\n";
        break;
    }
    // Create GPU texture from loaded data
    $tex = vio_texture($ctx, [
        "data"   => $result["data"],
        "width"  => $result["width"],
        "height" => $result["height"],
    ]);
    break;
}

Released under the MIT License.