Skip to content

TextureTarget

The TextureTarget is an offscreen Target backed by a GPU texture.

Use it for headless rendering, tests, server-side image generation, or CI.

use fragmentcolor::{Renderer, Shader, Target};
let renderer = Renderer::new();
let mut target = renderer.create_texture_target([64, 64]).await?;
let shader = Shader::default();
renderer.render(&shader, &target)?;
let image = target.get_image().await?;

Return a sampleable Texture handle that aliases the offscreen render target’s storage. Useful for multi-pass pipelines: render into the TextureTarget, then bind the returned Texture as a shader uniform on the next pass to read it back.

Every call returns a handle to the target’s own texture, under one TextureId. The handle follows TextureTarget::resize, and resizing the handle resizes the target. It is Clone; pass it around freely.

use fragmentcolor::{Renderer, Shader};
let renderer = Renderer::new();
let target = renderer.create_texture_target([256, 256]).await?;
// Bind the offscreen target's contents as a uniform on a downstream
// post-processing shader.
let post = Shader::new(r#"
@group(0) @binding(0) var input_image : texture_2d<f32>;
@group(0) @binding(1) var input_sampler : sampler;
@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
let p = array<vec2<f32>, 3>(vec2f(-1.0,-1.0), vec2f(3.0,-1.0), vec2f(-1.0,3.0));
return vec4<f32>(p[i], 0.0, 1.0);
}
@fragment fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
return textureSample(input_image, input_sampler, vec2<f32>(0.5, 0.5));
}
"#)?;
post.set("input_image", &target.texture())?;

TextureTarget::size() -> Size { width: u32, height: u32, depth: Option<u32> }

Section titled “TextureTarget::size() -> Size { width: u32, height: u32, depth: Option<u32> }”

Returns the size of the TextureTarget in pixels.

use fragmentcolor::{Renderer, Target};
let renderer = Renderer::new();
let target = renderer.create_texture_target([64, 64]).await?;
let size = target.size();
let width = size.width;
let height = size.height;
let depth = size.depth;

TextureTarget::resize(size: [u32; 2] | (u32, u32))

Section titled “TextureTarget::resize(size: [u32; 2] | (u32, u32))”

Resizes the TextureTarget to the given width and height.

Passes that draw into the target, shaders that sample it and every texture() handle use the new texture. Its pixels start blank, and the current size keeps them. A width or height of zero, a side past the device limit, or a layer count other than 1 returns TextureError::InvalidInput and keeps the texture.

use fragmentcolor::{Renderer, Target};
let renderer = Renderer::new();
let mut target = renderer.create_texture_target([64, 64]).await?;
target.resize([128, 32])?;

Returns the current contents of the target as a byte array in RGBA8 format.

Rows run from top to bottom with no padding, so the pixel at (x, y) starts at byte (y * width + x) * 4.

Python returns a NumPy array of shape (height, width, 4), indexed image[y, x]. That is the layout Renderer.create_texture reads, so renderer.create_texture(target.get_image()) uploads the same image.

A readback that fails or times out, or that needs a buffer past the device’s largest, returns an error: a thrown Error named FragmentColorError in JavaScript, a FragmentColorError in Python and Swift, and a FragmentColorException in Kotlin.

In JavaScript, getImage also throws the GPU error of a frame rendered before it. It waits for that frame to report first, so a call right after a failed render throws, as Renderer::render describes.

use fragmentcolor::{Renderer, Target, Shader};
let renderer = Renderer::new();
let target = renderer.create_texture_target([16, 16]).await?;
renderer.render(&Shader::default(), &target)?;
let image = target.get_image().await?;