Phase 12: Metal GPU Compositor (2/7)#
Goal: Implement MSL shaders and render pipeline state for 2D quad rendering.
Depends on: Metal device, command queue, and CAMetalLayer setup
Requirements#
-
Write Metal Shading Language (MSL) shaders compiled at runtime via
newLibraryWithSource::- Vertex shader: transform 2D vertex positions, pass through color and texture coordinates
- Fragment shader: support two modes — solid color fill and textured (sampled from texture with color tint)
- Use a uniform for the viewport size to convert pixel coordinates to normalized device coordinates
-
Define vertex format:
struct Vertex { position: [f32; 2], // pixel coordinates color: [f32; 4], // RGBA tex_coord: [f32; 2], // UV coordinates (0,0 for solid rects) use_texture: f32, // 0.0 = solid color, 1.0 = textured } -
Create
MTLRenderPipelineState:- Configure vertex descriptor matching the Vertex struct
- Set pixel format to match CAMetalLayer (BGRA8Unorm)
- Enable alpha blending:
source * sourceAlpha + dest * (1 - sourceAlpha)
-
Implement a
MetalRendererstruct (or extend existing platform code):- Hold device, command queue, pipeline state references
- Method to build a vertex buffer from a list of 2D quads
- Method to encode draw calls for a batch of quads
-
Proof-of-concept: render a colored rectangle using the Metal pipeline
Acceptance Criteria#
- Can render solid-color rectangles via Metal pipeline
- Alpha blending works correctly for semi-transparent fills
- Shader compiles at runtime without errors
- Vertex data is efficiently batched into a single buffer
- No external crate dependencies
-
cargo clippy --workspace -- -D warningspasses -
cargo test --workspacepasses