Godot texture import (C++)
Godot's image stack seen from the engine side: one Image class that owns a byte vector, a format enum mixing raw and GPU block formats, and a table of function pointers that codec and compressor modules fill in; an editor-time importer that turns a source file into a .ctex per GPU format family; and a run-time loader that reads that file back and hands an Image to the renderer.
| Field | Value |
|---|---|
| Language | C++ (engine); bundled C and C++ libraries under thirdparty/ |
| License | MIT (LICENSE.txt); bundled codecs and compressors carry their own licences, listed with their pinned versions in thirdparty/README.md |
| Repository | godotengine/godot |
| Revision read | 944a3c6cbbbb88284feebcb0603464cb175fa18e (August 18, 2026, a merge on master) |
| Documentation | In-tree class reference doc/classes/Image.xml and doc/classes/ResourceImporterTexture.xml; manual page Importing images |
| Category | Consumers: game-engine asset pipeline (decode, mipmap, block-compress, serialize, upload) |
| Tier | B |
| Last reviewed | November 7, 2026 |
NOTE
Every quote was read with git show 944a3c6cb:<path> and every cited path checked with git cat-file -e. Claims marked verified were run against Godot 4.5.1 (godot_4 from nixpkgs, headless, GDScript probes) in /tmp/godot-probe, not against a build of the pinned revision. 4.5.1 (f62fdbde1) is not an ancestor of the pin, but git diff f62fdbde1 944a3c6cb shows no change to modules/hdr/image_loader_hdr.cpp or to png_to_image, and the int size checks in core/io/image.cpp read the same in both. No performance number appears on this page.
Overview
What it solves
A game engine needs images in two shapes. At edit time it reads whatever an artist saved (PNG, JPEG, WebP, SVG, TGA, BMP, Radiance HDR, OpenEXR, KTX, DDS), generates mipmaps and compresses to the block formats each GPU family samples directly. At run time it must get those blocks into video memory with as little work as possible. Godot does both with one class. Its header comment states the double role (core/io/image.h):
"Image storage class. This is used to store an image in user memory, as well as providing some basic methods for image manipulation. Images can be loaded from a file, or registered into the Render object as textures."
The importer's documentation frames the whole pipeline as a choice between five storage modes, each a trade of quality, memory and import time (ResourceImporterTexture.xml):
"Lossless: Original quality, high memory usage, high size on disk, fast import." … "Basis Universal: Reduced quality, low memory usage, lowest size on disk, slow import. Only use for textures in 3D scenes, not for 2D elements."
Design philosophy
- One concrete type, no genericity.
Imageis a reference-countedResourcewith five fields; every format is a value of one enum, and every operation switches on it. - Plug-ins by function pointer. Codecs, packers and block compressors are static function pointers on
Imagethat modules assign at start-up, so a build without a module simply leaves the pointernullptrand the operation fails withERR_UNAVAILABLE. - Decide at import, not at load. The editor writes one
.ctexper GPU format family, and the exported game picks the variant its platform supports; the run-time path only decodes PNG, WebP or Basis payloads, or copies raw blocks. - The GPU is the limit. The reference says plainly that "The maximum image size is 16384×16384 pixels due to graphics hardware limitations" (
Image.xml).
How it works
The Image class
The private state is a format, a byte vector, two dimensions and a flag (image.h):
// core/io/image.h (excerpt)
Format format = FORMAT_L8;
Vector<uint8_t> data;
int width = 0;
int height = 0;
bool mipmaps = false;The mip chain lives in the same data vector, level after level, and _get_dst_image_size walks it, rounding each level up to the format's block size (image.cpp). There is no stride: rows are tightly packed. Format (image.h) has 49 formats before FORMAT_MAX: 8-bit L8, LA8, R8 … RGBA8; packed RGBA4444 and RGB565; 32-bit float RF … RGBAF; half float RH … RGBAH; shared-exponent RGBE9995; 16-bit unsigned-normalised and integer R16 … RGBA16I; and the block formats DXT1/DXT3/DXT5 (BC1–BC3), RGTC_R/RGTC_RG (BC4/BC5), BPTC_RGBA (BC7), BPTC_RGBF/BPTC_RGBFU (BC6H), ETC1, seven ETC2 variants, ASTC 4×4, 6×6 and 8×8 (LDR and HDR), plus two swizzled normal-map forms, ETC2_RA_AS_RG and DXT5_RA_AS_RG. The size constants are (image.h):
MAX_WIDTH = (1 << 24), // Force a limit somehow.
MAX_HEIGHT = (1 << 24), // Force a limit somehow.
MAX_PIXELS = 268435456 // 16384 ^ 2Pixel-level operations (get_pixel, resize, blend_rect, generate_mipmaps) work on raw formats only; the reference warns that "Methods that modify the image data cannot be used on VRAM-compressed images" (Image.xml).
Codecs and compressors as function pointers
Image declares five groups of static pointers: savers (save_png_func, save_webp_func, …), memory loaders (_png_mem_loader_func, _jpg_mem_loader_func, _ktx_mem_loader_func, …), VRAM compressors, VRAM decompressors, and the packers and unpackers used inside .ctex (image.h):
// core/io/image.h (excerpt)
static void (*_image_compress_bc_func)(Image *, UsedChannels p_channels);
static void (*_image_compress_bptc_func)(Image *, UsedChannels p_channels, BPTCFormat p_bptc_format);
static void (*_image_compress_etc2_func)(Image *, UsedChannels p_channels);
static void (*_image_compress_astc_func)(Image *, UsedChannels p_channels, CompressProfile p_profile);
...
static Vector<uint8_t> (*basis_universal_packer)(const Ref<Image> &p_image, UsedChannels p_channels, const BasisUniversalPackerParams &p_basisu_params);
static Ref<Image> (*basis_universal_unpacker)(const Vector<uint8_t> &p_buffer);Each module fills its slots: etcpak provides ETC1, ETC2, BC1, BC3, BC4 and BC5 encoding (no BC2; behind ETCPAK_COMPRESS_ENABLED) and ETC decoding (etcpak/register_types.cpp); astcenc encodes ASTC only under TOOLS_ENABLED but always decodes (astcenc/register_types.cpp); cvtt encodes BC6H/BC7 and is built only for the editor unless cvtt_export_templates is set (cvtt/config.py); bcdec decodes BC; basis_universal packs only in the editor and unpacks everywhere (basis_universal/register_types.cpp). betsy is the GPU path: it registers _image_compress_bc_rd_func and _image_compress_bptc_rd_func (betsy/register_types.cpp), compute shaders run through a private RenderingDevice (image_compress_betsy.h). Image::_compress_from_channels tries the GPU first when the project setting asks, and falls back to the CPU encoder on failure (image.cpp):
// core/io/image.cpp (excerpt)
if (GLOBAL_GET("rendering/textures/vram_compression/compress_with_gpu")) {
switch (p_mode) {
case COMPRESS_BPTC: {
// BC7 is unsupported currently.
if ((format >= FORMAT_RF && format <= FORMAT_RGBE9995) && _image_compress_bptc_rd_func) {File loaders are a second, object-oriented layer: ImageFormatLoader with a virtual load_image(Ref<Image>, Ref<FileAccess>, flags, scale) and get_recognized_extensions (image_loader.h). ImageLoader::load_image picks loaders by file extension, not by content, and tries the next one only on ERR_FILE_UNRECOGNIZED (image_loader.cpp). The two flags are FLAG_FORCE_LINEAR and FLAG_CONVERT_COLORS; scripts can add loaders through ImageFormatLoaderExtension.
The importer: ResourceImporterTexture
The import options (resource_importer_texture.cpp) include compress/mode (Lossless, Lossy, VRAM Compressed, VRAM Uncompressed, Basis Universal), compress/high_quality, compress/hdr_compression, compress/normal_map, compress/channel_pack, mipmaps/generate and mipmaps/limit, roughness/mode, process/channel_remap/*, process/fix_alpha_border, process/premult_alpha, process/hdr_as_srgb, process/size_limit and detect_3d/compress_to, beside Basis (compress/uastc_level, compress/rdo_quality_loss), lossy, SVG and editor-icon options. For VRAM Compressed, import writes one file per family and reports each as a platform variant (resource_importer_texture.cpp):
// editor/import/resource_importer_texture.cpp (excerpt)
// Must import in desktop and mobile formats in order of priority, so platform chooses the best supported one (e.g. s3tc over etc2 on desktop).Desktop gets BC1/BC3 (s3tc), or BC7/BC6H (bptc) when high_quality is set or the source is HDR; mobile gets ETC2 (etc2), or ASTC (astc) under the same conditions. HDR with alpha cannot be block-compressed, so unless the user forces it (by dropping alpha) the importer converts to RGBE9995 and stores it uncompressed; it does the same for any HDR source when compress/hdr_compression is disabled. Before compression _save_ctex resizes to a power of two when asked, generates mipmaps (renormalising for normal maps), folds normal-map variance into roughness mipmaps (generate_mipmap_roughness), and calls detect_used_channels so a greyscale or two-channel image gets a one- or two-channel block format (resource_importer_texture.cpp).
The .ctex format
_save_ctex writes a fixed header, then save_to_ctex_format writes an inner image header and the payload (resource_importer_texture.cpp). The loader side is CompressedTexture2D::_load_data and load_image_from_file (compressed_texture.cpp, compressed_texture.cpp):
| Offset | Field | Written by |
|---|---|---|
| 0 | magic GST2 ("Godot Streamable Texture 2D") | store_8 ×4 |
| 4 | FORMAT_VERSION (1) | store_32 |
| 8, 12 | original width, height | store_32 |
| 16 | flags: FORMAT_BIT_STREAM, HAS_MIPMAPS, DETECT_3D, DETECT_NORMAL, DETECT_ROUGNESS | store_32 |
| 20 | mipmap limit | store_32 |
| 24–35 | three reserved words | store_32(0) ×3 |
| 36 | DataFormat: IMAGE, PNG, WEBP, BASIS_UNIVERSAL | store_32 |
| 40, 42 | width, height of the stored image | store_16 |
| 44, 48 | mipmap count, Image::Format | store_32 |
| 52 | payload | see below |
PNG and WEBP store each mip level as its own length-prefixed file; BASIS_UNIVERSAL stores one length-prefixed .basis blob; IMAGE stores Image::get_data() raw, which for a block format is the block stream ready for upload. The flag bits start at bit 22 and the "detect" bits are editor callbacks: on first use in 3D, as a normal map or with a roughness texture, the editor reimports the texture with new settings (compressed_texture.h).
Run-time loading
CompressedTexture2D::load reads the image and passes it straight to the rendering server (compressed_texture.cpp):
// scene/resources/compressed_texture.cpp (excerpt)
RETURN_IF_ERROR(_load_data(p_path, lw, lh, image, request_3d, request_normal, request_roughness, mipmap_limit));
if (texture.is_valid()) {
RID new_texture = RS::get_singleton()->texture_2d_create(image);The whole file passes through an Image in CPU memory; there is no direct file-to-GPU path. A Basis payload is transcoded at load to whichever format the device reports through has_os_feature (bptc, astc, rgtc, s3tc, etc2, astc_hdr), falling back to RGBA8 (image_compress_basisu.cpp); the KTX2 loader makes the same choice for Basis-supercompressed KTX2 files but transcodes sRGB-tagged ones to RGBA32 with the comment // TODO srgb native support (texture_loader_ktx.cpp). PortableCompressedTexture2D offers the same modes for textures built at run time (COMPRESSION_MODE_LOSSLESS … COMPRESSION_MODE_ASTC, portable_compressed_texture.h).
Analysis
1. Pixel and memory model
One buffer, one format value, a boolean "has mipmaps" and no stride, views, planes or array layers: layered and 3D textures are Vector<Ref<Image>> validated by validate_3d_image. What Godot adds that the codec libraries lack is a block-format-aware size model: _get_dst_image_size, get_format_block_size and get_format_pixels_shifted give each level's byte size for raw and compressed formats alike, which is what a GPU upload needs (compare KTX-Software and the block formats concept). The model is an engine's, not a library's: no sub-image views, no YCbCr, no planar data.
2. Genericity mechanism
None at compile time. Image is one non-template class; the element type is the run-time Format value, and each operation switches on it or calls get_format_pixel_size and get_format_block_size. Optional behaviour is the run-time function-pointer table described in Codecs and compressors as function pointers, and file loaders are virtual ImageFormatLoader subclasses. Templates appear only inside image.cpp helpers such as _generate_po2_mipmap, instantiated per component type.
3. Colour and metadata
Image has no colour field. Whether a texture is sRGB is a property of how a shader samples it, so loaders carry at most a FLAG_FORCE_LINEAR switch: the HDR, EXR and SVG loaders apply srgb_to_linear when it is set. The PNG loader drops 16-bit precision, applies no ICC profile and leaves gamma handling to libpng's simplified API: it masks out PNG_FORMAT_FLAG_LINEAR and, unless FLAG_FORCE_LINEAR is set, sets PNG_IMAGE_FLAG_16BIT_sRGB (png_driver_common.cpp). Verified: a 4×4 16-bit RGB PNG loads as FORMAT_RGB8 (format 4). The JPEG loader refuses precision above 8 and CMYK/YCCK (image_loader_libjpeg_turbo.cpp) and reads no EXIF orientation. No loader reads image metadata or CICP. HDR is a format question (RGBAH, RGBE9995, BC6H, ASTC HDR), not a colour-model question.
4. I/O and codec architecture
Every loader is synchronous and returns a finished Image. Most read the whole file into a Vector<uint8_t> first: the JPEG loader's load_image calls f->get_length() and get_buffer before decoding (image_loader_libjpeg_turbo.cpp), and the PNG loader does the same. The Radiance HDR loader instead reads scanlines byte by byte from the FileAccess (image_loader_hdr.cpp), still into one whole-image buffer. There is no incremental or suspendable decode (push and pull). The architecture lesson is the split: format selection, mipmap generation and block compression happen once, offline, and the shipped container is engine-private (.ctex) with a payload that is either another codec's file or raw upload-ready blocks.
5. Hostile input and safety
Godot treats imported files as trusted project assets, and the run-time Image.load API reaches the same code. Three findings, all verified on Godot 4.5.1:
- Allocation at the declared size.
png_to_imagesizes its buffer from the header withPNG_IMAGE_BUFFER_SIZEbefore reading pixels (png_driver_common.cpp). A 69-byte PNG declaring 30000×30000 RGBA produced a 4,294,971,392-byte anonymousmmap(observed withstrace) before libpng reported "Not enough image data". libpng's own limits are covered in the libpng deep-dive. intoverflow in the size check.initialize_datatestsp_width * p_height > MAX_PIXELSinint, and_get_dst_image_sizecomputesbw * bhinintbefore widening (image.cpp,image.cpp).Image.create(16385, 16385, …)is rejected, butImage.create(65536, 65536, false, FORMAT_L8)returns a 65536×65536 image withget_data_size() == 0, and 65536×65537 gives 65,536 bytes. On that imageget_pixel(0, 1)returned a value from past the buffer andget_pixel(0, 60000)killed the process with signal 11.- Unclamped RLE runs in the Radiance HDR loader. The run loop checks
i < widthonly before each run, then writescountpixels (image_loader_hdr.cpp). An 8×1.hdrwhose first run has length 127 returnedOKfromloadand then aborted the process with a glibc heap-corruption check (exit 134; the message depends on heap layout, and two runs gave "corrupted size vs. prev_size" and "malloc(): unaligned tcache chunk detected"). The dump branch has the same shape, and the buffer sizeheight * width * (int)sizeof(uint32_t)is itself anintproduct (read from source, not exercised).
There is no fuzzing harness in the tree (outside thirdparty/, a search for fuzz finds only FuzzySearch, PO "fuzzy" translation flags and the changelog). Compare Wuffs and the hostile-input concept.
6. Allocation and ownership
Vector<uint8_t> is copy-on-write, so Image::duplicate, get_data and passing images by Ref share storage until a write. Every transform (convert, resize, compress) replaces data in place on the object, and the importer calls duplicate() before each per-family compression so one decoded source feeds several .ctex files. Nothing lets a caller supply the destination buffer.
7. CPU parallelism and SIMD
Image itself has no threading or SIMD code. Parallelism sits at two other levels: the importer declares can_import_threaded() as true (resource_importer_texture.h), so the editor imports several textures at once, and the cvtt BC6H/BC7 encoder splits rows into tasks on the engine's WorkerThreadPool (image_compress_cvtt.cpp). Any SIMD lives inside the bundled third-party encoders and decoders, which this page does not read.
8. GPU offload and interop
Two lessons. First, GPU block compression (betsy: BC1, BC4 and BC6H compute shaders plus an alpha-stitch pass) is an optional accelerator behind the same function-pointer slot as the CPU encoder, with CPU fallback on any error; the comment notes BC7 is not implemented on the GPU. Second, run-time format choice is a capability query (has_os_feature("astc") and so on) feeding a transcoder, which is the Basis Universal model. Upload goes through RenderingServer from a CPU Image; see Vulkan transfer for what a direct path would add.
9. Temporal, animation and containers
No animation: GIF and animated WebP are not imported as textures. The relevant container idea is the platform variant: one source, several .ctex files named by suffix (.s3tc.ctex, .etc2.ctex, …), with the variant chosen at export time.
10. Testing and conformance
tests/core/io/test_image.cpp saves a 4×4 RGBA8 image as PNG (and EXR and JPEG), reloads the PNG and compares bytes, checks that icon fixtures in BMP, EXR, JPEG, SVG, WebP, PNG and TGA load without error, and checks instantiation, resizing, pixel access, custom mipmaps and conversion (test_image.cpp). There is no conformance corpus, no differential oracle and no fuzzing; compression quality is measured ad hoc through Image::compute_image_metrics.
Lessons for Sparkles
- Q1: A GPU-facing image type needs a block-aware size model (block dimensions, bytes per block, per-level offsets) even if its pixel API never touches compressed data. Godot's
get_image_mipmap_offset_and_dimensionsis that model in one function; it does not need Godot's lack of strides or views. - Q2: Function-pointer slots filled at start-up give optional codecs without templates; in D, version-gated sub-packages or a registry keyed by format serve the same purpose with compile-time checking. Godot's slot design hides missing modules until a call fails.
- Q4: Separate the offline pipeline (decode, mipmap, compress, write an engine container) from the run-time one (read, maybe transcode, upload). Extension-based loader dispatch is the wrong default for untrusted input; content sniffing is cheap.
- Q5: An engine can ship with no colour model because the shader decides sRGB. A general library cannot: Godot's dropped ICC, EXIF orientation and 16-bit PNG precision are the gaps a viewer would hit first.
- Q6:
.ctexshows the minimum a file-to-GPU container records: format, extent, mip count, then upload-ready bytes. KTX2 records more (level index, DFD, supercompression) and is the standard alternative. - Q7: GPU compression as an optional accelerator with CPU fallback, sharing one entry point, is a concrete pattern for the SIMD-versus-GPU question.
- Q8: All three hostile-input findings above come from arithmetic in
intand from trusting a declared size; overflow-checked size computation and a corpus of crafted headers would have caught each. They are cheap regression inputs for a Sparkles oracle. - Q9: Godot keeps image core, codecs and compressors in separate modules joined through one class; the split exists, but the class depends on all of them by name.
Strengths
- One type covers raw, float, half, shared-exponent, 16-bit and every major GPU block format, with correct per-level size arithmetic.
- Platform variants let one import serve desktop and mobile without run-time recompression.
- GPU compression with CPU fallback, and run-time Basis transcoding to the best supported format.
- Small, documented import options mapped directly onto engine needs (normal maps, roughness, channel packing, HDR policy).
Weaknesses
- No stride, view or plane model; layered images are vectors of
Image. - No colour metadata; 16-bit PNG and high-precision JPEG are reduced or refused.
- Size checks computed in
intoverflow; loaders allocate at the declared size; the HDR RLE decoder writes past its buffer (all verified on 4.5.1). - Extension-based loader dispatch; whole-file, synchronous decoding only.
- No fuzzing or conformance corpus.
- The
.ctexinner header stores dimensions withstore_16whileImageallows up to 2^24; the run-time size-limit branches forPNG/WEBPand forIMAGEcompare in opposite directions (sw > p_size_limitagainstp_size_limit > tw). Both read from source; neither was exercised.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
One Image class with a run-time Format enum | One type serves scripts, importer and renderer; GPU block formats fit beside raw ones | No strides, views or planes; every operation switches on the format |
| Codecs and compressors as static function pointers | Modules are optional per build; export templates drop editor-only encoders | A missing module shows up only as ERR_UNAVAILABLE at the call |
Compress at import, one .ctex per GPU family | No run-time encoding; the platform picks the variant it supports | Several files per texture; format choice is fixed at export |
GPU compression (betsy) behind the CPU slot | Offloads encoding when a RenderingDevice is available | Partial coverage (no BC7 on the GPU); two encoders per format to maintain |
Size checks in int, buffers at declared size | Simple code for trusted project assets | Overflow and huge allocations from crafted files reached through Image.load |
Sources
- Source at
944a3c6cbbbb88284feebcb0603464cb175fa18e:core/io/image.h,core/io/image.cpp,core/io/image_loader.{h,cpp},editor/import/resource_importer_texture.cpp,scene/resources/compressed_texture.{h,cpp},scene/resources/portable_compressed_texture.h,drivers/png/png_driver_common.cpp, the loaders and compressors undermodules/, andtests/core/io/test_image.cpp. - Class reference:
Image.xml,ResourceImporterTexture.xml. - Related deep-dives: KTX-Software, Basis Universal, libpng, libjpeg-turbo, libwebp, OpenEXR, Wuffs, Vulkan transfer; paper thread GPU texture compression.
- Probes: Godot 4.5.1 (nixpkgs
godot_4), headless GDScript,strace, in/tmp/godot-probe.