libktx - The KTX Library
Introduction

libktx is a small library of functions for creating and reading KTX (Khronos TeXture) files and instantiating OpenGL® and OpenGL® ES textures and Vulkan images from them.

For information about the KTX format see the formal specification.

The library is open source software. Source code is available at GitHub. Most of the source code and the documentation is licensed under the Apache 2.0 license. See License and Attribution Notices for details. When distributing the library, whether in source or binary form, this documentation must be included in the distribution or otherwise made available to recipients.

See Revision History for the list of changes.

See To Do List for the current To Do list.

Authors
Mark Callow, Edgewise Consulting, formerly at HI Corporation
Georg Kolling, Imagination Technology
Jacob Ström, Ericsson AB
Version
3.0.0
Date

Usage Overview

Reading a KTX file for non-GL and non-Vulkan Use

#include <ktx.h>
ktxTexture* texture;
ktx_size_t offset;
ktx_uint8_t* image;
ktx_uint32_t level, layer, faceSlice;
result = ktxTexture_CreateFromNamedFile("mytex3d.ktx",
&texture);
// Retrieve information about the texture from fields in the ktxTexture
// such as:
ktx_uint32 numLevels = texture->numLevels;
ktx_uint32 baseWidth = texture->baseWidth;
ktx_bool_t isArray = texture->isArray;
// Retrieve a pointer to the image for a specific mip level, array layer
// & face or depth slice.
level = 1; layer = 0; faceSlice = 3;
result = ktxTexture_GetImageOffset(texture, level, layer, faceSlice, &offset);
image = ktxTexture_GetData(texture) + offset;
// ...
// Do something with the texture image.
// ...

Creating a GL texture object from a KTX file.

#include <ktx.h>
ktxTexture* kTexture;
ktx_size_t offset;
ktx_uint8_t* image;
ktx_uint32_t level, layer, faceSlice;
GLuint texture = 0;
GLenum target, glerror;
result = ktxTexture_CreateFromNamedFile("mytex3d.ktx",
KTX_TEXTURE_CREATE_NO_FLAGS,
&kTexture);
glGenTextures(1, &texture); // Optional. GLUpload can generate a texture.
result = ktxtexture_GLUpload(kTexture, &texture, &target, &glerror);
// ...
// GL rendering using the texture
// ...

Creating a Vulkan image object from a KTX file.

#include <ktxvulkan.h>
ktxTexture* kTexture;
ktx_size_t offset;
ktx_uint8_t* image;
ktx_uint32_t level, layer, faceSlice;
// Set up Vulkan physical device (gpu), logical device (device), queue
// and command pool. Save the handles to these in a struct called vkctx.
// ktx VulkanDeviceInfo is used to pass these with the expectation that
// apps are likely to upload a large number of textures.
ktxVulkanDeviceInfo_Construct(&vdi, vkctx.gpu, vkctx.device,
vkctx.queue, vkctx.commandPool, nullptr);
ktxresult = ktxTexture_CreateFromNamedFile("mytex3d.ktx",
KTX_TEXTURE_CREATE_NO_FLAGS,
&kTexture);
ktxresult = ktxTexture_VkUploadEx(kTexture, &vdi, &texture,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
// ...
// Vulkan rendering using the texture
// ...
// When done using the image in Vulkan...
ktxVulkanTexture_Destruct(&texture, vkctx.device, nullptr);

Extracting Metadata

Once a ktxTexture object has been created, metadata can be easily found and extracted. The following can be added to any of the above.

char* pValue;
uint32_t valueLen;
&valueLen, (void**)&pValue))
{
char s, t;
if (sscanf(pValue, KTX_ORIENTATION2_FMT, &s, &t) == 2) {
...
}
}

Writing a KTX file

#include <ktx.h>
ktxTexture* texture;
ktx_uint32_t level, layer, faceSlice;
FILE* src;
ktx_size_t srcSize;
createInfo.glInternalformat = GL_RGB8;
createInfo.baseWidth = 2048;
createInfo.baseHeight = 1024;
createInfo.baseDepth = 16;
createInfo.numDimensions = 3.
// Note: it is not necessary to provide a full mipmap pyramid.
createInfo.numLevels = log2(createInfo.baseWidth) + 1
createInfo.numLayers = 1;
createInfo.numFaces = 1;
createInfo.isArray = KTX_FALSE;
createInfo.generateMipmaps = KTX_FALSE;
result = ktxTexture_Create(createInfo,
&texture);
src = // Open a stdio FILE* on the baseLevel image, slice 0.
srcSize = // Query size of the file.
level = 0;
layer = 0;
faceSlice = 0;
result = ktxTexture_SetImageFromMemory(texture, level, layer, faceSlice,
src, srcSize);
// Repeat for the other 15 slices of the base level and all other levels
// up to createInfo.numLevels.
ktxTexture_WriteToNamedFile(texture, "mytex3d.ktx");

Modifying a KTX file

#include <ktx.h>
ktxTexture* texture;
ktx_size_t offset;
ktx_uint8_t* image;
ktx_uint32_t level, layer, faceSlice;
result = ktxTexture_CreateFromNamedFile("mytex3d.ktx",
&texture);
// The file is closed after all the data has been read.
// It is the responsibilty of the application to make sure its
// modifications are valid.
texture->generateMipmaps = KTX_TRUE;
ktxTexture_WriteToNamedFile(texture, "mytex3d.ktx");