Skip to content
modkitv0.2

texture.h

#include <modkit/texture.h>36 functions ยท 3 structs ยท 4 enums ยท 7 typedefs ยท 1 macros

Texture loading and management.

GPU texture loading and management with stb_image. Supports: PNG, JPG/JPEG, BMP, TGA, GIF, PSD, HDR, PIC For low-level GPU interop, include modkit.h and use the escape hatch accessor mk_texture_get_bgfx_handle() (via modkit.h).

Functions

mk_texture_calc_infofunction

mk_texture_info_t mk_texture_calc_info(uint16_t width, uint16_t height, uint16_t depth, bool cube, bool has_mips, uint16_t num_layers, mk_texture_format_t format)

Calculate storage size / mip count / bits-per-pixel for a texture shape without creating it.

Useful to size a staging buffer before mk_texture_update_*.

ParameterTypeDescription
widthuint16_tValue for width.
heightuint16_tValue for height.
depthuint16_tValue for depth.
cubeboolValue for cube.
has_mipsboolValue for has mips.
num_layersuint16_tValue for num layers.
formatmk_texture_format_tValue for format.

Returns The resulting handle or value.

mk_texture_createfunction

mk_texture_t mk_texture_create(uint16_t width, uint16_t height, const void *pixels, uint32_t flags)

Create texture from raw RGBA pixels.

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
pixelsconst void *RGBA pixel data (4 bytes per pixel)
flagsuint32_tTexture flags (MK_TEXTURE_*)

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_create_2d_rtfunction

mk_texture_t mk_texture_create_2d_rt(uint16_t width, uint16_t height, mk_texture_format_t format, uint8_t num_mips, uint64_t sampler_flags)

Create an empty 2D render-target texture, optionally with a mip chain.

Target each mip by building a framebuffer via mk_framebuf_create_from_attachments() with layer=0, mip=k. Used for the GPU-generated BRDF LUT (num_mips=1) and the prefiltered equirect specular map (one mip per roughness level).

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
formatmk_texture_format_tTexture format (e.g. MK_TEXTURE_FORMAT_RGBA16F)
num_mipsuint8_tMip count (1 = no mips)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*); the render-target flag is added internally

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_create_3dfunction

mk_texture_t mk_texture_create_3d(uint16_t width, uint16_t height, uint16_t depth, mk_texture_format_t format, uint64_t sampler_flags, const void *data, uint32_t data_size, bool has_mips)

Create a 3D (volume) texture.

NULL data (data_size 0) creates a mutable empty texture updatable later via mk_texture_update_3d (e.g. a runtime color LUT).

ParameterTypeDescription
widthuint16_tVolume dimensions
heightuint16_tVolume dimensions
depthuint16_tVolume dimensions
formatmk_texture_format_tTexture format (MK_TEXTURE_FORMAT_*)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*)
dataconst void *Pixel data for the whole volume, or NULL for mutable empty
data_sizeuint32_tSize of data in bytes (0 with NULL data)
has_mipsboolAllocate a mip chain

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_create_cubefunction

mk_texture_t mk_texture_create_cube(uint16_t face_size, mk_texture_format_t format, uint64_t sampler_flags, const void *data, uint32_t data_size, bool has_mips)

Create a (non-render-target) cubemap.

NULL data creates a mutable empty cube updatable later via mk_texture_update_cube. When data is provided it must hold all six faces in order +X,-X,+Y,-Y,+Z,-Z, each face's mip chain packed in order. For a renderable cubemap (IBL prefilter targets) use mk_texture_create_cube_rt.

ParameterTypeDescription
face_sizeuint16_tEdge length of each cube face (mip 0)
formatmk_texture_format_tTexture format (MK_TEXTURE_FORMAT_*)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*)
dataconst void *Packed 6-face data, or NULL for mutable empty
data_sizeuint32_tSize of data in bytes (0 with NULL data)
has_mipsboolAllocate a mip chain

Returns Cubemap texture or MK_TEXTURE_INVALID on failure

mk_texture_create_cube_rtfunction

mk_texture_t mk_texture_create_cube_rt(uint16_t face_size, mk_texture_format_t format, uint8_t num_mips, uint64_t sampler_flags)

Create an empty render-target cubemap with an optional mip chain.

Each face+mip can be targeted individually by building a framebuffer via mk_framebuf_create_from_attachments(). Used for GPU IBL prefiltering (panorama->cube, GGX prefilter).

ParameterTypeDescription
face_sizeuint16_tEdge length of each cube face (mip 0)
formatmk_texture_format_tTexture format (e.g. MK_TEXTURE_FORMAT_RGBA16F)
num_mipsuint8_tMip count (1 = no mips)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*); render-target and blit-destination flags are added internally

Returns Cubemap texture or MK_TEXTURE_INVALID on failure

mk_texture_create_hdr_mipsfunction

mk_texture_t mk_texture_create_hdr_mips(int width, int height, const float *pixels, uint64_t sampler_flags)

Upload linear float RGBA pixels to a MIPPED RGBA16F 2D texture (CPU-built mip chain).

For IBL filtered-importance-sampling: the GGX prefilter reads pre-blurred mips so glossy reflections stay low-variance/smooth instead of grainy. The HDR file loader cannot mip float data; build the env once and pass its pixels here.

ParameterTypeDescription
widthintTexture width
heightintTexture height
pixelsconst float *Tightly packed RGBA float pixels (4 components/texel, linear)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*)

Returns Mipped texture or MK_TEXTURE_INVALID on failure

mk_texture_create_r8function

mk_texture_t mk_texture_create_r8(uint16_t width, uint16_t height, const void *pixels, uint32_t flags)

Create single-channel (R8) texture.

Useful for font atlases, masks, etc.

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
pixelsconst void *R8 pixel data (1 byte per pixel), or NULL for empty texture
flagsuint32_tTexture flags (MK_TEXTURE_*)

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_create_rawfunction

mk_texture_t mk_texture_create_raw(uint16_t width, uint16_t height, mk_texture_format_t format, uint64_t sampler_flags, const void *data, uint32_t data_size)

Create texture with explicit format and sampler flags.

Used for specialized textures like HDR panoramas, BRDF LUTs, etc.

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
formatmk_texture_format_tTexture format (MK_TEXTURE_FORMAT_*)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*)
dataconst void *Raw pixel data
data_sizeuint32_tSize of data in bytes

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_create_readbackfunction

mk_texture_t mk_texture_create_readback(uint16_t width, uint16_t height, mk_texture_format_t format)

Create an empty texture suitable as a GPU->CPU read-back / blit destination (created with blit-dst + read-back flags).

Blit a render target into it with mk_encoder_blit(), then retrieve the pixels with mk_texture_read(). Requires BGFX_CAPS_TEXTURE_READ_BACK (see mk_has_feature). Returns MK_TEXTURE_INVALID on failure.

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
formatmk_texture_format_tTexture format (match the source you will blit, e.g. RGBA8)

Returns Texture or MK_TEXTURE_INVALID

mk_texture_create_storagefunction

mk_texture_t mk_texture_create_storage(uint16_t width, uint16_t height, mk_texture_format_t format, uint64_t sampler_flags)

Create a compute-writable storage image (empty 2D texture).

Bind it to a compute dispatch via mk_dispatch (image binding) / mk_encoder_set_image with MK_ACCESS_WRITE or _READWRITE; the texture is also sampleable, so a later draw pass can read what the compute shader wrote. Use a compute-writable format (e.g. MK_TEXTURE_FORMAT_RGBA8).

ParameterTypeDescription
widthuint16_tTexture width
heightuint16_tTexture height
formatmk_texture_format_tTexture format (e.g. MK_TEXTURE_FORMAT_RGBA8)
sampler_flagsuint64_tSampler flags (MK_SAMPLER_*); the compute-write flag is added internally

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_cube_rt_supportedfunction

bool mk_texture_cube_rt_supported(mk_texture_format_t format)

Query whether a format can be used as a render-target cubemap (and as a 2D render target).

Use to gate GPU IBL prefiltering before creating resources.

ParameterTypeDescription
formatmk_texture_format_tTexture format to test

Returns true if the backend supports rendering into a cubemap of this format

mk_texture_destroyfunction

void mk_texture_destroy(mk_texture_t texture)

Release the public owner reference.

The handle becomes publicly invalid immediately; resources that already retained it keep it alive until release.

ParameterTypeDescription
texturemk_texture_tValue for texture.

mk_texture_format_mip_autogenfunction

bool mk_texture_format_mip_autogen(mk_texture_format_t format)

Query whether render-target resolves can auto-generate mipmaps for this format.

Required for framebuffers created with mk_framebuf_create_from_attachments_autogen_mips.

ParameterTypeDescription
formatmk_texture_format_tTexture format to test

Returns true if the backend can auto-generate mipmaps for this format

mk_texture_free_hdr_rgba_ffunction

void mk_texture_free_hdr_rgba_f(float *pixels)

Free pixels returned by mk_texture_load_hdr_rgba_f.

ParameterTypeDescription
pixelsfloat *Value for pixels.

mk_texture_get_depthfunction

uint16_t mk_texture_get_depth(mk_texture_t texture)

Get texture depth (1 for 2D/cube textures; the depth for 3D textures).

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns The resulting value.

mk_texture_get_direct_access_ptrfunction

void * mk_texture_get_direct_access_ptr(mk_texture_t texture)

Direct CPU-visible pointer to the texture's memory, if the backend supports it (e.g.

unified-memory GPUs). Returns NULL when direct access is unavailable. The pointer is stable until the texture is destroyed.

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns A borrowed pointer, or NULL when unavailable.

mk_texture_get_heightfunction

uint16_t mk_texture_get_height(mk_texture_t texture)

Get texture height.

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns The resulting value.

mk_texture_get_sizefunction

void mk_texture_get_size(mk_texture_t texture, uint16_t *width, uint16_t *height)

Get texture dimensions.

ParameterTypeDescription
texturemk_texture_tValue for texture.
widthuint16_t *Value for width.
heightuint16_t *Value for height.

mk_texture_get_widthfunction

uint16_t mk_texture_get_width(mk_texture_t texture)

Get texture width.

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns The resulting value.

mk_texture_is_creatablefunction

bool mk_texture_is_creatable(uint16_t depth, bool cube, uint16_t num_layers, mk_texture_format_t format, uint64_t texture_flags)

Whether a texture with the given properties can be created on this backend.

texture_flags is the full bgfx texture flag bitset (BGFX_TEXTURE_ and BGFX_SAMPLER_ flags), not just sampler bits.

ParameterTypeDescription
depthuint16_tValue for depth.
cubeboolValue for cube.
num_layersuint16_tValue for num layers.
formatmk_texture_format_tValue for format.
texture_flagsuint64_tValue for texture flags.

Returns True when the condition holds.

mk_texture_is_validfunction

bool mk_texture_is_valid(mk_texture_t texture)

Check if a texture is publicly valid and not pending destruction.

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns True when the condition holds.

mk_texture_loadfunction

mk_texture_t mk_texture_load(const char *path, uint32_t flags)

Load texture from file.

Supports PNG, JPG, BMP, TGA, GIF, PSD, HDR, PIC. Floating-point HDR (.hdr): pass MK_TEXTURE_HDR to decode as float into an RGBA16F texture (linear; sRGB is ignored). HDR files are also auto-detected, so a .hdr without the flag still loads as float (never silently tonemapped to 8-bit). MK_TEXTURE_MIPS is ignored on HDR loads (warns) โ€” build a prefilter mip chain via render targets. For RGBA32F or fine control, use mk_texture_load_ex with mk_texture_load_desc.hdr / .hdr_format.

ParameterTypeDescription
pathconst char *Path to image file
flagsuint32_tTexture flags (MK_TEXTURE_*)

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_load_cubefunction

mk_texture_t mk_texture_load_cube(const char *const faces[6], uint32_t flags)

Load a cubemap from six image files (one per face).

Face order: +X, -X, +Y, -Y, +Z, -Z (right, left, top, bottom, front, back). All faces must be square and the same size. A full mip chain is generated per face. Sample in shaders with SAMPLERCUBE; bind like any texture.

ParameterTypeDescription
facesconst char *constSix file paths in +X -X +Y -Y +Z -Z order
flagsuint32_tTexture flags (MK_TEXTURE_*)

Returns Texture or MK_TEXTURE_INVALID on failure

const char* faces[6] = {
    "sky/right.jpg", "sky/left.jpg",
    "sky/top.jpg",   "sky/bottom.jpg",
    "sky/front.jpg", "sky/back.jpg",
};
mk_texture_t sky = mk_texture_load_cube(faces, MK_TEXTURE_CLAMP);

mk_texture_load_exfunction

mk_texture_t mk_texture_load_ex(const mk_texture_load_desc *desc)

Load a texture from a full configuration descriptor (the power path).

Use this when you need per-load control over mips, filtering and anisotropy beyond what the flag shorthand expresses. mk_texture_load()/_mem() are sugar over this.

ParameterTypeDescription
descconst mk_texture_load_desc *Load configuration (path OR bytes+bytes_size)

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_t t = mk_texture_load_ex(&(mk_texture_load_desc){
    .path = "wall.png", .srgb = true,
    .mips = MK_MIPS_ON, .filter = MK_FILTER_ANISOTROPIC, .aniso = 8,
});

mk_texture_load_hdr_rgba_ffunction

float * mk_texture_load_hdr_rgba_f(const char *path, bool flip_v, int *out_w, int *out_h)

Decode an .hdr file to tightly packed RGBA float pixels in CPU RAM (4 floats per texel, linear).

The IBL package uses this to build the GPU equirect and to project SH-9 from the same pixels. Free the result with mk_texture_free_hdr_rgba_f.

ParameterTypeDescription
pathconst char *Path to a .hdr (radiance) file
flip_vboolReverse row order (stb decodes top-down)
out_wint *Receives width (may be NULL)
out_hint *Receives height (may be NULL)

Returns malloc'd RGBA float pixels, or NULL on failure

mk_texture_load_memfunction

mk_texture_t mk_texture_load_mem(const void *data, uint32_t size, uint32_t flags)

Load texture from memory.

ParameterTypeDescription
dataconst void *Pointer to image data (encoded PNG/JPG/etc)
sizeuint32_tSize of image data in bytes
flagsuint32_tTexture flags (MK_TEXTURE_*)

Returns Texture or MK_TEXTURE_INVALID on failure

mk_texture_pinfunction

bool mk_texture_pin(mk_texture_t texture)

Retain a texture for an existing resource dependency or resolved draw.

ParameterTypeDescription
texturemk_texture_tValue for texture.

Returns True when the operation succeeds.

mk_texture_readfunction

uint32_t mk_texture_read(mk_texture_t texture, void *dest)

Read a texture back from the GPU into CPU memory (async).

The texture must have been created read-back capable (created blit-readable; e.g. a render target, or a texture made with the read-back flag). dest must be large enough for the full mip-0 image (width * height * bytes-per-pixel of the texture's format). The copy is NOT ready immediately: it completes on a future frame. The return value is the frame number at which dest will contain valid data โ€” keep calling mk_frame_end(); query mk_frame_number() for the submitted frame number and only read dest once that counter reaches the returned frame.

ParameterTypeDescription
texturemk_texture_tValue for texture.
destvoid *Value for dest.

Returns The frame number when the data will be available, or 0 on error (invalid texture / not read-back capable on this renderer).

mk_texture_set_namefunction

void mk_texture_set_name(mk_texture_t texture, const char *name)

Set debug name for texture.

ParameterTypeDescription
texturemk_texture_tTexture handle
nameconst char *Debug name string

mk_texture_system_initfunction

void mk_texture_system_init(void)

Initialize the texture system.

mk_texture_system_shutdownfunction

void mk_texture_system_shutdown(void)

Perform the texture system shutdown operation.

mk_texture_unpinfunction

void mk_texture_unpin(mk_texture_t texture)

Perform the texture unpin operation.

ParameterTypeDescription
texturemk_texture_tValue for texture.

mk_texture_updatefunction

void mk_texture_update(mk_texture_t texture, uint16_t x, uint16_t y, uint16_t width, uint16_t height, const void *pixels, uint16_t pitch)

Update a region of a texture.

ParameterTypeDescription
texturemk_texture_tTexture to update
xuint16_tX offset in texture
yuint16_tY offset in texture
widthuint16_tWidth of region to update
heightuint16_tHeight of region to update
pixelsconst void *Pixel data (format must match texture format)
pitchuint16_tRow pitch in bytes (0 = tightly packed)

mk_texture_update_3dfunction

void mk_texture_update_3d(mk_texture_t texture, uint8_t mip, uint16_t x, uint16_t y, uint16_t z, uint16_t width, uint16_t height, uint16_t depth, const void *data, uint32_t data_size)

Update a sub-volume of a 3D texture (mip level mip, origin x/y/z).

data_size must cover width*height*depth*bytes-per-texel.

ParameterTypeDescription
texturemk_texture_tValue for texture.
mipuint8_tValue for mip.
xuint16_tValue for x.
yuint16_tValue for y.
zuint16_tValue for z.
widthuint16_tValue for width.
heightuint16_tValue for height.
depthuint16_tValue for depth.
dataconst void *Data buffer.
data_sizeuint32_tData size in bytes.

mk_texture_update_cubefunction

void mk_texture_update_cube(mk_texture_t texture, uint16_t layer, uint8_t face, uint8_t mip, uint16_t x, uint16_t y, uint16_t width, uint16_t height, const void *data, uint32_t data_size, uint16_t pitch)

Update a region of one cubemap face.

face is 0..5 (+X,-X,+Y,-Y,+Z,-Z); layer is the array layer (0 for a non-array cubemap). pitch 0 => tightly packed.

ParameterTypeDescription
texturemk_texture_tValue for texture.
layeruint16_tValue for layer.
faceuint8_tValue for face.
mipuint8_tValue for mip.
xuint16_tValue for x.
yuint16_tValue for y.
widthuint16_tValue for width.
heightuint16_tValue for height.
dataconst void *Data buffer.
data_sizeuint32_tData size in bytes.
pitchuint16_tValue for pitch.

Structs

mk_texturestruct

Data for texture.

FieldTypeDescription
iduint32_tThe ID.

mk_texture_infostruct

Computed storage info for a texture of the given shape (does NOT allocate).

Mirrors bgfx_texture_info_t.

FieldTypeDescription
formatmk_texture_format_tThe format.
storage_sizeuint32_ttotal bytes required to store the texture
widthuint16_tThe width.
heightuint16_tThe height.
depthuint16_tThe depth.
num_layersuint16_tThe num layers.
num_mipsuint8_tThe num mips.
bits_per_pixeluint8_tThe bits per pixel.
cubeboolThe cube.

mk_texture_load_descstruct

Full texture-load configuration (power path; mk_texture_load(path,flags) is sugar over this).

Zero-init = sane defaults: no mips, linear filter, repeat.

FieldTypeDescription
pathconst char *file path; OR set bytes+bytes_size for a memory load
bytesconst void *encoded image bytes (PNG/JPG/...) for memory load
bytes_sizesize_tThe bytes size.
mipsmk_mip_mode_tdefault MK_MIPS_NONE (opt-in)
mip_genmk_mipgen_thint, only meaningful with MK_MIPS_ON
filtermk_filter_mode_tdefault MK_FILTER_LINEAR
anisouint8_tmax anisotropy for ANISOTROPIC (0 = driver default)
srgbboolignored on the HDR path (HDR data is already linear)
flip_vboolThe flip v.
clamp_uboolThe clamp u.
clamp_vboolThe clamp v.
hdrbooldecode as float; auto-set when the source is detected HDR
hdr_formatmk_texture_format_tHDR only: 0 = default (RGBA16F), or RGBA16F / RGBA32F.

Enums

mk_filter_modeenum

Values for filter mode.

ValueDescription
MK_FILTER_LINEARbilinear (+ trilinear when mipped)
MK_FILTER_POINTnearest (pixel art)
MK_FILTER_ANISOTROPIClinear + anisotropic

mk_mip_modeenum

Values for mip mode.

ValueDescription
MK_MIPS_NONECreate no mip chain.
MK_MIPS_ONCreate or load a mip chain.

mk_mipgenenum

Values for mipgen.

ValueDescription
MK_MIPGEN_CPUGenerate missing mips on the CPU.
MK_MIPGEN_GPUGenerate missing mips on the GPU when supported.

mk_texture_flagsenum

Values for texture flags.

ValueDescription
MK_TEXTURE_NONESelects none.
MK_TEXTURE_CLAMP_UClamp U coordinate.
MK_TEXTURE_CLAMP_VClamp V coordinate.
MK_TEXTURE_CLAMPSelects clamp.
MK_TEXTURE_POINTPoint/nearest filtering.
MK_TEXTURE_SRGBsRGB color space
MK_TEXTURE_FLIP_VFlip texture vertically on load.
MK_TEXTURE_MIPSGenerate a mip chain (OPT-IN; off by default).
MK_TEXTURE_MIPS_GPUHint (only with MK_TEXTURE_MIPS): GPU-generate, not CPU.
MK_TEXTURE_HDRDecode as float (e.g.

Typedefs

mk_filter_mode_ttypedef

typedef enum mk_filter_mode mk_filter_mode_t

Values for filter mode.

mk_mip_mode_ttypedef

typedef enum mk_mip_mode mk_mip_mode_t

Values for mip mode.

mk_mipgen_ttypedef

typedef enum mk_mipgen mk_mipgen_t

Values for mipgen.

mk_texture_flags_ttypedef

typedef enum mk_texture_flags mk_texture_flags_t

Values for texture flags.

mk_texture_info_ttypedef

typedef struct mk_texture_info mk_texture_info_t

Computed storage info for a texture of the given shape (does NOT allocate).

Mirrors bgfx_texture_info_t.

mk_texture_load_desctypedef

typedef struct mk_texture_load_desc mk_texture_load_desc

Full texture-load configuration (power path; mk_texture_load(path,flags) is sugar over this).

Zero-init = sane defaults: no mips, linear filter, repeat.

mk_texture_ttypedef

typedef struct mk_texture mk_texture_t

Data for texture.

Macros

MK_TEXTURE_INVALIDdefine

MK_TEXTURE_INVALID

Invalid sentinel for texture.