Skip to main content

Module instance

Module instance 

Source
Expand description

Instanced rendering for repeated UI primitives.

InstancedRectPipeline renders many axis-aligned rectangles (with optional uniform corner radius) using a single indexed quad mesh and a per-instance vertex buffer. This is much more efficient than emitting individual Vertex quads when rendering large numbers of identical primitives (buttons, table cells, list items).

§Pipeline design

  • Mesh vertex buffer (step_mode=Vertex): 4 corners of a unit quad as [f32; 2] UV coordinates in [0,1]². Reused across all instances.
  • Index buffer: 6 indices forming 2 triangles for the unit quad.
  • Instance vertex buffer (step_mode=Instance): one InstanceRect per rectangle, carrying position, size, colour, and corner radius.

The vertex shader computes the pixel-space position from inst_pos + uv * inst_size and applies the 2-D orthographic projection. The fragment shader runs a rounded-rect SDF when corner_radius > 0.

§Usage

let pipeline = InstancedRectPipeline::new(&device, sample_count);
let mut renderer = InstancedRectRenderer::new(&device, 256);
renderer.push(InstanceRect { pos: [10.0, 10.0], size: [80.0, 30.0],
                              color: [1.0, 0.0, 0.0, 1.0], corner_radius: 4.0 });
renderer.flush(&device, &queue, &mut encoder, &pipeline, &globals_bind_group, ...);

Structs§

InstanceRect
Per-instance data for a single instanced rectangle.
InstancedRectPipeline
The compiled instanced-rect render pipeline.
InstancedRectRenderer
A frame-scoped collector of InstanceRect data and a flusher that issues a single instanced draw call.