Skip to main content

Texture API / writer

Write one compressed texture for many GPU families.

KTX2BasisWriter encodes supported RGBA pixel data with Basis Universal and packages the result as KTX2. The output can be transcoded by a consuming runtime to a device-compatible compressed format.

Portable texture output

Compress once, transcode near the device.

Basis Universal separates distribution compression from the final GPU block format. Write a KTX2 asset once, then let the runtime choose the supported target where it is loaded.

Input

RGBA8, RGBA16F, or RGBA32F pixel data.

Encode

Compress pixels with Basis Universal.

Package

Store the result in a KTX2 container.

Transcode

Choose a device-compatible GPU format at load time.

Detailed guide

KTX2BasisWriter reference

The detailed reference covers accepted pixel data, encoding options, output structure, Node.js requirements, and runtime transcoding.

From-v3.1Node.js-only

KTX2BasisWriter encodes RGBA8, RGBA16F, or RGBA32F pixels into a Basis Universal KTX2 texture.

KTX2BasisWriter
File Extension.ktx2
File TypeBinary
Data Formathttps://github.com/KhronosGroup/KTX-Specification/blob/main/ktxspec.adoc
File FormatKTX2
Encoder TypeAsynchronous
Worker Thread❌ No (but may run on separate native thread in browsers)
Streaming❌ No

Usage​

import '@loaders.gl/core/polyfills'; // only if using under Node
import {load, encode} from '@loaders.gl/core';
import {ImageLoader, getImageData} from '@loaders.gl/images/image';
import {KTX2BasisWriter} from '@loaders.gl/images/basis';

const shannonPNG = 'shannon.png';

const image = getImageData(await load(shannonPNG, ImageLoader));
const encodedData = await encode(image, KTX2BasisWriter, {
'ktx2-basis-writer': {
format: 'uastc-ldr-4x4',
quality: 80,
effort: 5,
contentType: 'srgb',
mipmaps: true,
zstd: true
}
});

Data Format​

https://github.com/KhronosGroup/KTX-Specification/blob/main/ktxspec.adoc

Options​

Options are nested under ktx2-basis-writer.

OptionTypeDefaultDescription
formatBasisEncoderFormat'etc1s'ETC1S, UASTC LDR/HDR, XUASTC, ASTC LDR/HDR, or XUBC7 source codec.
qualitynumber 0–100codec defaultUnified quality/bitrate control. ETC1S defaults to the upstream midpoint.
effortnumber 0–10codec defaultUnified encoding-effort control.
contentType'linear' | 'srgb' | 'normal-map''linear'Applies the corresponding Basis encoder preset.
mipmapsbooleanfalseGenerate a complete mip chain.
zstdbooleanfalseApply Zstandard to UASTC LDR 4×4, UASTC HDR 4×4, or ASTC HDR 6×6.
ldrToHdrNitMultipliernumber100Absolute-light scale for RGBA8-to-HDR conversion.

LDR formats require RGBA8 input. HDR formats accept RGBA8, half-float bit patterns in a Uint16Array, or RGBA float values in a Float32Array. An sRGB HDR input is converted to linear absolute-light values before encoding.

WASM module​

The writer loads the Binomial LLC Basis Universal encoder independently from the decoder:

Module Overrides​

Use options.modules to override the Basis encoder runtime used by KTX2BasisWriter.

  • modules.basisEncoder: supply a preloaded Basis encoder module containing BasisEncoder.
  • 'basis_encoder.js': override the URL used for the Basis encoder JavaScript wrapper.
  • 'basis_encoder.wasm': override the URL used for the Basis encoder WebAssembly binary.