Expand description
Metal texture helpers for IOSurface
This module provides utilities for creating Metal textures from IOSurface
with zero-copy GPU access. This is the most efficient way to use captured
frames with Metal rendering.
§Features
- Zero-copy texture creation from
IOSurface - Automatic pixel format detection and Metal format mapping
- Multi-plane support for YCbCr formats (420v, 420f)
- Native Metal device and texture types (no external crate needed)
- Embedded Metal shaders for common rendering scenarios
§When to Use
Use this module when you need:
- Real-time rendering - Display captured frames in a Metal view
- GPU processing - Apply compute shaders to captured content
- Zero-copy performance - Avoid CPU-GPU memory transfers
For CPU-based processing, use CVPixelBuffer with lock guards instead.
§Workflow
- Get
IOSurfacefrom captured frame viaCMSampleBuffer::image_buffer() - Create Metal textures with
IOSurface::create_metal_textures() - Render using the built-in shaders or your own
§Example
use screencapturekit::metal::MetalDevice;
use screencapturekit::cm::{CMSampleBuffer, IOSurface};
// Get the system default Metal device
let device = MetalDevice::system_default().expect("No Metal device");
// In your frame handler
fn handle_frame(sample: &CMSampleBuffer, device: &MetalDevice) {
if let Some(pixel_buffer) = sample.image_buffer() {
if let Some(surface) = pixel_buffer.io_surface() {
// Create textures directly - no closures or factories needed
if let Some(textures) = surface.create_metal_textures(device) {
if textures.is_ycbcr() {
// Use YCbCr shader with plane0 (Y) and plane1 (CbCr)
println!("YCbCr texture: {}x{}",
textures.plane0.width(), textures.plane0.height());
} else {
// Use single-plane shader (BGRA, l10r)
println!("Single-plane texture: {}x{}",
textures.plane0.width(), textures.plane0.height());
}
}
}
}
}§Built-in Shaders
The SHADER_SOURCE constant contains Metal shaders for common rendering scenarios:
| Function | Description |
|---|---|
vertex_fullscreen | Aspect-ratio-preserving fullscreen quad |
fragment_textured | BGRA/L10R single-texture rendering |
fragment_ycbcr | YCbCr biplanar (420v/420f) to RGB conversion |
vertex_colored / fragment_colored | UI overlay rendering |
Modules§
- pixel_
format - Pixel format constants using
FourCharCode
Structs§
- Captured
Textures - Result of creating Metal textures from an
IOSurface - IOSurface
Info - Information about an
IOSurfacefor Metal texture creation - Metal
Buffer - A Metal buffer for vertex/uniform data
- Metal
Command Buffer - A Metal command buffer
- Metal
Command Queue - A Metal command queue
- Metal
Device - A Metal device (GPU)
- Metal
Drawable - A drawable from a Metal layer
- Metal
Function - A Metal shader function
- Metal
Layer - A
CAMetalLayerfor rendering to a window - Metal
Library - A Metal shader library
- Metal
Render Command Encoder - A render command encoder
- Metal
Render Pass Descriptor - A render pass descriptor
- Metal
Render Pipeline Descriptor - A render pipeline descriptor
- Metal
Render Pipeline State - A compiled render pipeline state
- Metal
Texture - A Metal texture
- Metal
Vertex Descriptor - A vertex descriptor for specifying vertex buffer layout
- Plane
Info - Information about a single plane within an
IOSurface - Resource
Options - Resource options for buffer creation
- Texture
Params - Metal texture descriptor parameters for creating textures from
IOSurface - Uniforms
- Uniforms structure for Metal shaders
Enums§
- MTLBlend
Factor - Blend factor
- MTLBlend
Operation - Blend operation
- MTLLoad
Action - Load action for render pass attachments
- MTLPixel
Format - Pixel format
- MTLPrimitive
Type - Primitive type for drawing
- MTLStore
Action - Store action for render pass attachments
- MTLVertex
Format - Vertex format for vertex attributes
- MTLVertex
Step Function - Vertex step function
- Metal
Pixel Format - Metal pixel format enum matching
MTLPixelFormatvalues
Constants§
- SHADER_
SOURCE - Metal shader source for rendering captured frames
Functions§
- autoreleasepool
- Execute a closure within an autorelease pool
- setup_
metal_ ⚠view - Set up an
NSViewfor Metal rendering
Type Aliases§
- Metal
Captured Textures - Result of creating Metal textures from an
IOSurface