Mipmap
Description
Section titled “Description”A Mipmap is a finished CPU mip chain (the base image plus every half-size copy down to 1x1) ready to upload as a Texture. Mipmaps are what lets a sampler pick a smaller, pre-filtered copy when a textured surface shrinks in screen space; without them, minified textures shimmer and alias. FragmentColor generates the chain for you whenever you call Renderer::create_texture(bytes), so you rarely need to build one by hand.
Reach for Mipmap when you want to drive the decode and resize work yourself: on a worker thread, in a tile-cache pipeline, or wherever you already own threading and want the renderer thread to do nothing but queue.write_texture. That’s the whole point of the type: keep CPU prep off the renderer thread, and hand the upload a chain that’s already done.
Typical reasons to build one explicitly:
- Your asset pipeline already runs on a worker (rayon, Swift
Task, KotlinDispatchers.Default, PythonThreadPoolExecutor, a Web Worker) and you want to fold mip generation into the same hop. - You stream tiles into a map or terrain renderer and need each tile’s GPU upload to be a single write, with decoding and resizing finished beforehand.
- You want to share one chain across many textures without rebuilding it.
Build with Mipmap::build(input); the input shapes match what Renderer::create_texture accepts. Whether the bytes get decoded depends on whether you pass a size:
build((bytes, format)): encoded image (PNG, JPEG, BMP, HDR, …). FC decodes and infers the dimensions.build((bytes, format, size)): raw pixel bytes already laid out forformatatsize. No decode.
Then hand the chain to Renderer::create_texture(chain) for the upload. The cross-language bindings expose build(bytes, format, size?) with size optional.
Supported formats: Rgba8Unorm, Rgba8UnormSrgb, Bgra8Unorm, R8Unorm, Rg8Unorm, R16Unorm, Rg16Unorm, Rgba16Unorm, Rgba16Float, Rgba32Float, and the Rgba, Bgra, L8 and Lab aliases. Anything else returns TextureError::UnsupportedMipmapFormat.
Per-language shapes
Section titled “Per-language shapes”- Rust:
Mipmap::build(input)whereinputis(bytes, format),(bytes, format, size), or a file path. - JS:
Mipmap.build(bytes, format, size?)withbytesasUint8Array/ArrayBuffer. - Python:
Mipmap.build(bytes, format, size=None)withbytesasbytes,list[int], or numpyndarray. - Swift / Kotlin:
Mipmap.build(bytes, format)orMipmap.build(bytes, format, size), withbytesasDataor[UInt8]in Swift and aByteArrayin Kotlin.
Example
Section titled “Example”use fragmentcolor::{Mipmap, Renderer, TextureFormat};
// Imagine `png` came off your asset loader on a worker thread.
// Decode + mipmap generation. Pure CPU; run it wherever you like.let chain = Mipmap::build((png.as_slice(), TextureFormat::Rgba8UnormSrgb))?;
// Back on the renderer thread, the upload is just a GPU write.let renderer = Renderer::new();let texture = renderer.create_texture(chain).await?;import { Mipmap, Renderer, TextureFormat } from "fragmentcolor";1 collapsed line
const png = new Uint8Array(await (await fetch("/healthcheck/public/favicon.png")).arrayBuffer());
// Imagine `png` came off your asset loader on a worker thread.
// Decode + mipmap generation. Pure CPU; run it wherever you like.const chain = Mipmap.build(png, TextureFormat.Rgba8UnormSrgb);
// Back on the renderer thread, the upload is just a GPU write.const renderer = new Renderer();const texture = await renderer.createTexture(chain);from fragmentcolor import Mipmap, Renderer, TextureFormat1 collapsed line
png = open("examples/assets/logo.png", "rb").read()
# Imagine `png` came off your asset loader on a worker thread.
# Decode + mipmap generation. Pure CPU; run it wherever you like.chain = Mipmap.build(png, TextureFormat.Rgba8UnormSrgb)
# Back on the renderer thread, the upload is just a GPU write.renderer = Renderer()texture = renderer.create_texture(chain)import FragmentColor2 collapsed lines
import Foundationlet png = try Data(contentsOf: URL(fileURLWithPath: "examples/assets/logo.png"))
// Imagine `png` came off your asset loader on a worker thread.
// Decode + mipmap generation. Pure CPU; run it wherever you like.let chain = try Mipmap.build(png, TextureFormat.rgba8UnormSrgb)
// Back on the renderer thread, the upload is just a GPU write.let renderer = Renderer()let texture = try await renderer.createTexture(chain)import org.fragmentcolor.*1 collapsed line
val png = java.io.File("examples/assets/logo.png").readBytes()
// Imagine `png` came off your asset loader on a worker thread.
// Decode + mipmap generation. Pure CPU; run it wherever you like.val chain = Mipmap.build(png, TextureFormat.RGBA8_UNORM_SRGB)
// Back on the renderer thread, the upload is just a GPU write.val renderer = Renderer()val texture = renderer.createTexture(chain)Methods
Section titled “Methods”Mipmap::build
Section titled “Mipmap::build”Build a mip chain on the calling thread, then hand it to Renderer::create_texture for a GPU-only upload. The work is pure CPU (decode if needed, then downsample to 1x1), so call it from wherever you already do heavy work: a worker thread, a thread pool, an async task, a Web Worker. The renderer thread sees only the finished chain.
build(input) takes the same input shapes as Renderer::create_texture and Renderer::create_storage_texture, so you write the call the same way regardless of which entry point you target. Whether build decodes the bytes depends on whether you pass a size:
- Encoded path,
sizeabsent. The bytes get decoded as PNG / JPEG / BMP / HDR / etc., and the chain inherits the image’s dimensions. The format comes from the bytes, as increate_texture, so a file name without an extension works. Use this when you have an encoded blob from disk, the network, or a decoder that emits encoded data. - Raw path,
sizepresent. The bytes are treated as already laid out forformatatsize, one layer deep and exactly that long. A file path takes its size from the file, so a path with a size is anInvalidInputerror. Use this when your decoder already produced pixel-format-matching bytes (typical for tile-cache pipelines that fold mip generation into the same step that produced the tile).
build runs synchronously and accepts only sync-friendly inputs: bytes or a file path. URL inputs (which need an async fetch), KTX2 inputs (already pre-baked), an existing chain, an existing texture, and empty inputs all return TextureError::InvalidInput with a message pointing at the right entry point.
A decoded image built for Bgra8Unorm or Bgra stores blue first, as create_texture does, and raw bytes keep their order.
Supported formats: Rgba8Unorm, Rgba8UnormSrgb, Bgra8Unorm, R8Unorm, Rg8Unorm, R16Unorm, Rg16Unorm, Rgba16Unorm, Rgba16Float, Rgba32Float, and the Rgba, Bgra, L8 and Lab aliases. Anything else returns TextureError::UnsupportedMipmapFormat. Decode failures surface as MalformedImageError; size and byte-count mismatches as InvalidInput.
Per-language shapes
Section titled “Per-language shapes”- Rust:
Mipmap::build((bytes, format))for the encoded path,Mipmap::build((bytes, format, size))for the raw path. Tuples implementInto<TextureInput>via theFrom<T>impls. - JS / Python:
Mipmap.build(bytes, format, size?)withsizeoptional. - Swift / Kotlin:
Mipmap.build(bytes, format)for the encoded path,Mipmap.build(bytes, format, size)for the raw path. Bytes areDataor[UInt8]in Swift and aByteArrayin Kotlin;sizeis[w, h]or aSize.
Example
Section titled “Example”use fragmentcolor::{Mipmap, Renderer, TextureFormat};
// Encoded path: bytes plus the format you want the chain to live in.// The dimensions come from the decoded image.let chain = Mipmap::build((encoded_png_bytes.as_slice(), TextureFormat::Rgba8UnormSrgb))?;
// Raw path: include the size so build skips decoding.let chain_raw = Mipmap::build((raw_rgba.as_slice(), TextureFormat::Rgba8UnormSrgb, [8, 8]))?;
// Either chain uploads the same way.let renderer = Renderer::new();let texture = renderer.create_texture(chain).await?;import { Mipmap, Renderer, TextureFormat } from "fragmentcolor";2 collapsed lines
const encoded_png_bytes = new Uint8Array(await (await fetch("/healthcheck/public/favicon.png")).arrayBuffer());const raw_rgba = new Uint8Array(8 * 8 * 4).fill(200);
// Encoded path: bytes plus the format you want the chain to live in.// The dimensions come from the decoded image.const chain = Mipmap.build(encoded_png_bytes, TextureFormat.Rgba8UnormSrgb);
// Raw path: include the size so build skips decoding.const chain_raw = Mipmap.build(raw_rgba, TextureFormat.Rgba8UnormSrgb, [8, 8]);
// Either chain uploads the same way.const renderer = new Renderer();const texture = await renderer.createTexture(chain);from fragmentcolor import Mipmap, Renderer, TextureFormat2 collapsed lines
encoded_png_bytes = open("examples/assets/logo.png", "rb").read()raw_rgba = bytes([200] * (8 * 8 * 4))
# Encoded path: bytes plus the format you want the chain to live in.# The dimensions come from the decoded image.chain = Mipmap.build(encoded_png_bytes, TextureFormat.Rgba8UnormSrgb)
# Raw path: include the size so build skips decoding.chain_raw = Mipmap.build(raw_rgba, TextureFormat.Rgba8UnormSrgb, size=[8, 8])
# Either chain uploads the same way.renderer = Renderer()texture = renderer.create_texture(chain)import FragmentColor3 collapsed lines
import Foundationlet encoded_png_bytes = try Data(contentsOf: URL(fileURLWithPath: "examples/assets/logo.png"))let raw_rgba = Data(repeating: 200, count: 8 * 8 * 4)
// Encoded path: bytes plus the format you want the chain to live in.// The dimensions come from the decoded image.let chain = try Mipmap.build(encoded_png_bytes, TextureFormat.rgba8UnormSrgb)
// Raw path: include the size so build skips decoding.let chain_raw = try Mipmap.build(raw_rgba, TextureFormat.rgba8UnormSrgb, [8, 8])
// Either chain uploads the same way.let renderer = Renderer()let texture = try await renderer.createTexture(chain)import org.fragmentcolor.*2 collapsed lines
val encoded_png_bytes = java.io.File("examples/assets/logo.png").readBytes()val raw_rgba = ByteArray(8 * 8 * 4) { 200.toByte() }
// Encoded path: bytes plus the format you want the chain to live in.// The dimensions come from the decoded image.val chain = Mipmap.build(encoded_png_bytes, TextureFormat.RGBA8_UNORM_SRGB)
// Raw path: include the size so build skips decoding.val chain_raw = Mipmap.build(raw_rgba, TextureFormat.RGBA8_UNORM_SRGB, listOf(8u, 8u))
// Either chain uploads the same way.val renderer = Renderer()val texture = renderer.createTexture(chain)Mipmap::format
Section titled “Mipmap::format”Return the texture format the chain was built for: the format you passed to build(...), with an alias such as Rgba returned as the concrete format it stands for, Rgba8Unorm. Useful when you receive a chain you didn’t build yourself (passed in from another thread or stored in a cache) and need to confirm it matches the format your shader expects before uploading.
Example
Section titled “Example”use fragmentcolor::{Mipmap, TextureFormat};
let chain = Mipmap::build((pixels.as_slice(), TextureFormat::Rgba8UnormSrgb, [4, 4]))?;let format = chain.format();import { Mipmap, TextureFormat } from "fragmentcolor";1 collapsed line
const pixels = new Uint8Array(4 * 4 * 4).fill(200);
const chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, [4, 4]);const format = chain.format();from fragmentcolor import Mipmap, TextureFormat1 collapsed line
pixels = bytes([200] * (4 * 4 * 4))
chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, size=[4, 4])format = chain.format()import FragmentColor2 collapsed lines
import Foundationlet pixels = Data(repeating: 200, count: 4 * 4 * 4)
let chain = try Mipmap.build(pixels, TextureFormat.rgba8UnormSrgb, [4, 4])let format = chain.format()import org.fragmentcolor.*1 collapsed line
val pixels = ByteArray(4 * 4 * 4) { 200.toByte() }
val chain = Mipmap.build(pixels, TextureFormat.RGBA8_UNORM_SRGB, listOf(4u, 4u))val format = chain.format()Mipmap::size
Section titled “Mipmap::size”Return the base level (level 0) dimensions as a Size with no depth, in every language. Useful when you want to know what you’re about to upload without keeping a separate copy of the source dimensions around. The chain has 1 + floor(log2(max(width, height))) levels, each one half the previous size down to 1x1.
Example
Section titled “Example”use fragmentcolor::{Mipmap, TextureFormat};
let chain = Mipmap::build((pixels.as_slice(), TextureFormat::Rgba8UnormSrgb, [16, 16]))?;let size = chain.size();let width = size.width;let height = size.height;import { Mipmap, TextureFormat } from "fragmentcolor";1 collapsed line
const pixels = new Uint8Array(16 * 16 * 4);
const chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, [16, 16]);const size = chain.size();const width = size.width;const height = size.height;from fragmentcolor import Mipmap, TextureFormat1 collapsed line
pixels = bytes(16 * 16 * 4)
chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, size=[16, 16])size = chain.size()width = size.widthheight = size.heightimport FragmentColor2 collapsed lines
import Foundationlet pixels = Data(repeating: 0, count: 16 * 16 * 4)
let chain = try Mipmap.build(pixels, TextureFormat.rgba8UnormSrgb, [16, 16])let size = chain.size()let width = size.widthlet height = size.heightimport org.fragmentcolor.*1 collapsed line
val pixels = ByteArray(16 * 16 * 4)
val chain = Mipmap.build(pixels, TextureFormat.RGBA8_UNORM_SRGB, listOf(16u, 16u))val size = chain.size()val width = size.widthval height = size.heightMipmap::levels
Section titled “Mipmap::levels”Return the tightly-packed bytes for each mip level, level 0 first. Each level holds the format’s bytes per pixel times max(1, base_w >> level) * max(1, base_h >> level) bytes. Reach for this when you want to inspect a level, dump the chain to disk, or feed the bytes into a tool that isn’t FragmentColor. When you upload via Renderer::create_texture(chain), the renderer reads the bytes directly and you don’t need to touch them yourself.
Example
Section titled “Example”use fragmentcolor::{Mipmap, TextureFormat};
let chain = Mipmap::build((pixels.as_slice(), TextureFormat::Rgba8UnormSrgb, [8, 8]))?;let level_zero_bytes = &chain.levels()[0];import { Mipmap, TextureFormat } from "fragmentcolor";1 collapsed line
const pixels = new Uint8Array(8 * 8 * 4);
const chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, [8, 8]);const level_zero_bytes = chain.levels()[0];from fragmentcolor import Mipmap, TextureFormat1 collapsed line
pixels = bytes(8 * 8 * 4)
chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, size=[8, 8])level_zero_bytes = chain.levels()[0]import FragmentColor2 collapsed lines
import Foundationlet pixels = Data(repeating: 0, count: 8 * 8 * 4)
let chain = try Mipmap.build(pixels, TextureFormat.rgba8UnormSrgb, [8, 8])let level_zero_bytes = chain.levels()[0]import org.fragmentcolor.*1 collapsed line
val pixels = ByteArray(8 * 8 * 4)
val chain = Mipmap.build(pixels, TextureFormat.RGBA8_UNORM_SRGB, listOf(8u, 8u))val level_zero_bytes = chain.levels()[0]Mipmap::level
Section titled “Mipmap::level”Return the bytes of one mip level, with level 0 the full-size image. An index past the last level returns TextureError::InvalidInput.
Example
Section titled “Example”use fragmentcolor::{Mipmap, TextureFormat};
let chain = Mipmap::build((pixels.as_slice(), TextureFormat::Rgba8UnormSrgb, [8, 8]))?;let level_zero_bytes = chain.level(0)?;import { Mipmap, TextureFormat } from "fragmentcolor";1 collapsed line
const pixels = new Uint8Array(8 * 8 * 4);
const chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, [8, 8]);const level_zero_bytes = chain.level(0);from fragmentcolor import Mipmap, TextureFormat1 collapsed line
pixels = bytes(8 * 8 * 4)
chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, size=[8, 8])level_zero_bytes = chain.level(0)import FragmentColor2 collapsed lines
import Foundationlet pixels = Data(repeating: 0, count: 8 * 8 * 4)
let chain = try Mipmap.build(pixels, TextureFormat.rgba8UnormSrgb, [8, 8])let level_zero_bytes = try chain.level(0)import org.fragmentcolor.*1 collapsed line
val pixels = ByteArray(8 * 8 * 4)
val chain = Mipmap.build(pixels, TextureFormat.RGBA8_UNORM_SRGB, listOf(8u, 8u))val level_zero_bytes = chain.level(0u)Mipmap::count
Section titled “Mipmap::count”Return how many mip levels the chain holds (always at least 1). For a base size of w x h, that’s 1 + floor(log2(max(w, h))). So an 8x8 source yields four levels (8, 4, 2, 1). Handy for asserting that the chain has the depth you expected before you upload, or for sizing a fixed-array view of the levels in your own code.
Example
Section titled “Example”use fragmentcolor::{Mipmap, TextureFormat};
let chain = Mipmap::build((pixels.as_slice(), TextureFormat::Rgba8UnormSrgb, [8, 8]))?;let count = chain.count();import { Mipmap, TextureFormat } from "fragmentcolor";1 collapsed line
const pixels = new Uint8Array(8 * 8 * 4);
const chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, [8, 8]);const count = chain.count();from fragmentcolor import Mipmap, TextureFormat1 collapsed line
pixels = bytes(8 * 8 * 4)
chain = Mipmap.build(pixels, TextureFormat.Rgba8UnormSrgb, size=[8, 8])count = chain.count()import FragmentColor2 collapsed lines
import Foundationlet pixels = Data(repeating: 0, count: 8 * 8 * 4)
let chain = try Mipmap.build(pixels, TextureFormat.rgba8UnormSrgb, [8, 8])let count = chain.count()import org.fragmentcolor.*1 collapsed line
val pixels = ByteArray(8 * 8 * 4)
val chain = Mipmap.build(pixels, TextureFormat.RGBA8_UNORM_SRGB, listOf(8u, 8u))val count = chain.count()