Module metal

Module metal 

Source
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

  1. Get IOSurface from captured frame via CMSampleBuffer::image_buffer()
  2. Create Metal textures with IOSurface::create_metal_textures()
  3. 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:

FunctionDescription
vertex_fullscreenAspect-ratio-preserving fullscreen quad
fragment_texturedBGRA/L10R single-texture rendering
fragment_ycbcrYCbCr biplanar (420v/420f) to RGB conversion
vertex_colored / fragment_coloredUI overlay rendering

Modules§

pixel_format
Pixel format constants using FourCharCode

Structs§

CapturedTextures
Result of creating Metal textures from an IOSurface
IOSurfaceInfo
Information about an IOSurface for Metal texture creation
MetalBuffer
A Metal buffer for vertex/uniform data
MetalCommandBuffer
A Metal command buffer
MetalCommandQueue
A Metal command queue
MetalDevice
A Metal device (GPU)
MetalDrawable
A drawable from a Metal layer
MetalFunction
A Metal shader function
MetalLayer
A CAMetalLayer for rendering to a window
MetalLibrary
A Metal shader library
MetalRenderCommandEncoder
A render command encoder
MetalRenderPassDescriptor
A render pass descriptor
MetalRenderPipelineDescriptor
A render pipeline descriptor
MetalRenderPipelineState
A compiled render pipeline state
MetalTexture
A Metal texture
MetalVertexDescriptor
A vertex descriptor for specifying vertex buffer layout
PlaneInfo
Information about a single plane within an IOSurface
ResourceOptions
Resource options for buffer creation
TextureParams
Metal texture descriptor parameters for creating textures from IOSurface
Uniforms
Uniforms structure for Metal shaders

Enums§

MTLBlendFactor
Blend factor
MTLBlendOperation
Blend operation
MTLLoadAction
Load action for render pass attachments
MTLPixelFormat
Pixel format
MTLPrimitiveType
Primitive type for drawing
MTLStoreAction
Store action for render pass attachments
MTLVertexFormat
Vertex format for vertex attributes
MTLVertexStepFunction
Vertex step function
MetalPixelFormat
Metal pixel format enum matching MTLPixelFormat values

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 NSView for Metal rendering

Type Aliases§

MetalCapturedTextures
Result of creating Metal textures from an IOSurface