Phase 12: Metal GPU Compositor (4/7)#
Goal: Pack rasterized glyph bitmaps into a GPU texture atlas for efficient text rendering.
Depends on: Metal texture management and image upload
Requirements#
-
Implement a texture atlas allocator:
- Shelf-packing algorithm: maintain rows ("shelves") of glyphs sorted by height
- Start with a reasonable atlas size (e.g., 1024×1024 R8 texture)
- Grow or create additional atlas pages when full
- Track allocated regions:
(glyph_id, font_size) → AtlasRegion { x, y, width, height, atlas_page }
-
Integrate with existing glyph cache in
textcrate:- When a glyph is first rasterized (CPU scanline rasterizer), upload its grayscale bitmap to the atlas
- Return UV coordinates for the glyph's region in the atlas
- Existing CPU glyph cache can remain for software fallback
-
Provide an API for the Metal renderer:
- Given a
TextLine(list of positioned glyphs), produce a list of textured quads with atlas UV coordinates - Each quad: position from glyph metrics, UVs from atlas region, color from text color
- Given a
-
Handle atlas invalidation:
- Clear atlas when font size distribution changes significantly (rare)
- Track atlas utilization metrics
Acceptance Criteria#
- Glyph bitmaps are packed into a GPU texture atlas
- Atlas lookup returns correct UV coordinates for each glyph
- Text renders correctly using atlas quads (visual parity with software renderer)
- Atlas grows or pages as needed when many unique glyphs are used
- No external crate dependencies
-
cargo clippy --workspace -- -D warningspasses -
cargo test --workspacepasses