Documentation
¶
Overview ¶
Package screentensor converts IOSurface-backed screen frames into normalized NCHW Metal tensor buffers for vision-language models without CPU pixel copies.
Each [IOSurface] is bound zero-copy as a metal.MTLTexture via NewTextureWithDescriptorIosurfacePlane. When Config.DiffThreshold is positive, a GPU tile-diff compute kernel compares the incoming surface against the previous keyframe texture and skips full resize/normalization when no tile exceeds the threshold.
Basic usage:
dev := metal.MTLCreateSystemDefaultDevice()
p, err := screentensor.NewPipeline(dev, screentensor.Config{
Width: 384,
Height: 384,
DType: screentensor.FP16,
DiffThreshold: 0.005,
})
if err != nil {
return err
}
defer p.Close()
res, err := p.ProcessSurface(surface)
if err != nil {
return err
}
if res.Changed {
// Read normalized NCHW tensor from res.Buffer.Contents().
}
The zero value of Pipeline is not usable; create one with NewPipeline and release its Metal resources with Pipeline.Close.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/metal"
"github.com/tmc/apple/x/screentensor"
)
func main() {
dev := metal.MTLCreateSystemDefaultDevice()
if dev.GetID() == 0 {
fmt.Println("no Metal device")
return
}
p, err := screentensor.NewPipeline(dev, screentensor.Config{
Width: 384,
Height: 384,
Mean: screentensor.CLIPMean,
Std: screentensor.CLIPStd,
DType: screentensor.FP16,
DiffThreshold: 0.005,
})
if err != nil {
log.Fatal(err)
}
defer p.Close()
fmt.Printf("dtype=%s tensor_bytes=%d\n", p.Config().DType, p.TensorBytes())
}
Output: dtype=fp16 tensor_bytes=884736
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // CLIPMean and CLIPStd are OpenAI CLIP / ViT-L normalization constants. CLIPMean = [3]float32{0.48145466, 0.4578275, 0.40821073} CLIPStd = [3]float32{0.26862954, 0.26130258, 0.27577711} // ImageNetMean and ImageNetStd are standard ImageNet normalization constants. ImageNetMean = [3]float32{0.485, 0.456, 0.406} ImageNetStd = [3]float32{0.229, 0.224, 0.225} )
Standard per-channel RGB normalization statistics.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Width and Height specify the output NCHW tensor spatial dimensions.
// If both are zero, they default to 384x384.
Width int
Height int
// Mean and Std specify per-channel RGB normalization statistics.
// If Std is all zeros, Mean and Std default to CLIPMean and CLIPStd.
Mean [3]float32
Std [3]float32
// DType selects FP32 (default) or FP16 output elements.
DType DType
// DiffThreshold is the maximum per-tile mean absolute RGB difference in [0, 1]
// required to treat a frame as changed. When <= 0, change gating is disabled
// and every frame runs full VLM preprocessing.
DiffThreshold float32
// TileSize is the square tile dimension in pixels used by the GPU tile-diff
// shader. If zero, it defaults to 32.
TileSize int
}
Config configures a Pipeline.
type DType ¶
type DType uint8
DType selects the element precision of the output NCHW tensor buffer.
type FrameResult ¶
type FrameResult struct {
// Changed reports whether the frame exceeded DiffThreshold (or was the
// first keyframe / had gating disabled) and triggered VLM preprocessing.
Changed bool
// DiffScore is the maximum per-tile mean absolute RGB difference in [0, 1]
// relative to the previous keyframe. It is 1.0 for initial keyframes and
// when change gating is disabled.
DiffScore float32
// Buffer is the shared Metal buffer containing the normalized planar NCHW
// tensor ([3, Height, Width] in Config.DType). When Changed is false,
// Buffer retains the previous keyframe's tensor.
Buffer metal.MTLBuffer
// GPUDuration is the GPU execution time reported by Metal command buffers
// for this frame.
GPUDuration time.Duration
}
FrameResult holds the outcome of processing one IOSurface frame.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline executes zero-copy IOSurface -> MTLTexture -> Metal VLM preprocessing with an optional GPU tile-diff change gate.
A Pipeline is safe for concurrent use. Its zero value is not usable; create one with NewPipeline and release its GPU resources with Pipeline.Close.
func NewPipeline ¶
NewPipeline creates a zero-copy screen tensor preprocessing pipeline on dev. If dev is nil or zero, the system default Metal device is used.
func (*Pipeline) Close ¶
Close releases all Metal resources owned by the pipeline. Calling Close multiple times is safe.
func (*Pipeline) ProcessSurface ¶
func (p *Pipeline) ProcessSurface(surf iosurface.IOSurfaceRef) (FrameResult, error)
ProcessSurface binds surf zero-copy as a Metal texture and runs the tile-diff change gate (when Config.DiffThreshold > 0) followed by bilinear resize and RGB normalization into the pipeline's output Metal buffer.
func (*Pipeline) Reset ¶
func (p *Pipeline) Reset()
Reset discards the cached keyframe texture so that the next call to Pipeline.ProcessSurface is treated as a keyframe.
func (*Pipeline) TensorBytes ¶
TensorBytes returns the byte length of the output NCHW tensor buffer.