Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / VisionPresentationSmoke.swift

Raw 3.2 KB on 1cf89eeacc946d122aba3a62ada6cb1600fbf757
1import Foundation
2import Metal
3
4@main struct VisionPresentationSmoke {
5 static func main() throws {
6 guard let device = MTLCreateSystemDefaultDevice(), let queue = device.makeCommandQueue(),
7 let function = try device.makeLibrary(source: VisionPresentation.smoothingShader, options: nil)
8 .makeFunction(name: "smoothPresentation") else { fatalError("Metal unavailable") }
9 let pipeline = try device.makeComputePipelineState(function: function)
10 func filtered(_ pixels: [UInt8], width: Int, height: Int, enabled: Bool) throws -> [UInt8] {
11 let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm, width: width, height: height, mipmapped: false)
12 descriptor.storageMode = .shared; descriptor.usage = [.shaderRead, .shaderWrite]
13 let source = device.makeTexture(descriptor: descriptor)!, target = device.makeTexture(descriptor: descriptor)!
14 pixels.withUnsafeBytes { source.replace(region: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0, withBytes: $0.baseAddress!, bytesPerRow: width * 4) }
15 let command = queue.makeCommandBuffer()!, encoder = command.makeComputeCommandEncoder()!
16 encoder.setComputePipelineState(pipeline); encoder.setTexture(source, index: 0); encoder.setTexture(target, index: 1)
17 var enabled: UInt32 = enabled ? 1 : 0
18 encoder.setBytes(&enabled, length: 4, index: 0)
19 encoder.dispatchThreads(MTLSize(width: width, height: height, depth: 1), threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1))
20 encoder.endEncoding(); command.commit(); command.waitUntilCompleted()
21 if let error = command.error { throw error }
22 var output = pixels
23 output.withUnsafeMutableBytes { target.getBytes($0.baseAddress!, bytesPerRow: width * 4, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0) }
24 return output
25 }
26 let width = 67, height = 45 // Non-multiple-of-threadgroup bounds are intentional.
27 let color: [UInt8] = [70, 110, 160, 255]
28 let uniform = Array(repeating: color, count: width * height).flatMap { $0 }
29 let flat = try filtered(uniform, width: width, height: height, enabled: true)
30 precondition(flat == uniform)
31 var diagonal = [UInt8]()
32 for y in 0..<height { for x in 0..<width {
33 let value: UInt8 = x > y ? 255 : 0
34 diagonal += [value, value, value, 255]
35 } }
36 let unchanged = try filtered(diagonal, width: width, height: height, enabled: false)
37 precondition(unchanged == diagonal)
38 let smoothed = try filtered(diagonal, width: width, height: height, enabled: true)
39 let softened = stride(from: 0, to: smoothed.count, by: 4).filter { smoothed[$0] > 0 && smoothed[$0] < 255 }.count
40 precondition(softened > 0 && smoothed[4 * width * (height - 1)] == 0 && smoothed[4 * (width - 1)] == 255)
41 precondition(stride(from: 3, to: smoothed.count, by: 4).allSatisfy { smoothed[$0] == 255 })
42 print("PASS headset presentation Metal: flat colors preserved, smoothing-off exact bytes, diagonal edge coverage (\(softened) filtered pixels), clamped borders and alpha")
43 }
44}