PulseCanvas / app · pulsecanvas.ai / scripts / EncoderSurfaceSmoke.swift
| 1 | import AppKit |
| 2 | import CoreVideo |
| 3 | @preconcurrency import Metal |
| 4 | |
| 5 | @main struct EncoderSurfaceSmoke { |
| 6 | @MainActor static func main() async throws { |
| 7 | _ = NSApplication.shared |
| 8 | for scene in NativeEffects.activeIDs { |
| 9 | let a = RenderState(), b = RenderState() |
| 10 | for state in [a,b] { |
| 11 | state.scene = scene; state.chaos = 0.8; state.motionBlur = 0.55 |
| 12 | state.strobeEnabled = true; state.strobeSensitivity = 0.8; state.strobeStrength = 0.65 |
| 13 | } |
| 14 | let reference = VisualizerRenderer(state: a), shared = VisualizerRenderer(state: b) |
| 15 | try reference.prepareOffscreen(width: 480, height: 270) |
| 16 | try shared.prepareOffscreen(width: 480, height: 270) |
| 17 | var cache: CVMetalTextureCache? |
| 18 | precondition(CVMetalTextureCacheCreate(nil, nil, shared.offscreenDevice!, nil, &cache) == kCVReturnSuccess) |
| 19 | for frame in 0..<4 { |
| 20 | let time = Double(frame) / 30 |
| 21 | let bands: [Float] = [0.4,0.7,0.1,0.9,0.2,0.6,0.8,0.3] |
| 22 | let source = try reference.renderOffscreen(time: time, audioBands: bands, audioPulse: 0.8, audioMomentum: 0.7, audioBeatMotionTime: time, audioTreblePulse: 0.9, audioTrebleAttackTime: time) |
| 23 | var buffer: CVPixelBuffer? |
| 24 | precondition(CVPixelBufferCreate(nil,480,270,kCVPixelFormatType_32BGRA,[kCVPixelBufferMetalCompatibilityKey: true, kCVPixelBufferIOSurfacePropertiesKey: [:]] as CFDictionary,&buffer) == kCVReturnSuccess) |
| 25 | var surface: CVMetalTexture? |
| 26 | precondition(CVMetalTextureCacheCreateTextureFromImage(nil,cache!,buffer!,nil,.bgra8Unorm,480,270,0,&surface) == kCVReturnSuccess) |
| 27 | let target = CVMetalTextureGetTexture(surface!)! |
| 28 | try await shared.renderOffscreen(into: target,time: time,audioBands: bands,audioPulse: 0.8,audioMomentum: 0.7,audioBeatMotionTime: time,audioTreblePulse: 0.9,audioTrebleAttackTime: time) |
| 29 | withExtendedLifetime(surface) {} |
| 30 | var expected = [UInt8](repeating:0,count:480*270*4) |
| 31 | source.getBytes(&expected,bytesPerRow:480*4,from:MTLRegionMake2D(0,0,480,270),mipmapLevel:0) |
| 32 | CVPixelBufferLockBaseAddress(buffer!, .readOnly) |
| 33 | let bytes = CVPixelBufferGetBaseAddress(buffer!)!, stride = CVPixelBufferGetBytesPerRow(buffer!) |
| 34 | for row in 0..<270 { |
| 35 | let actual = Array(UnsafeBufferPointer(start:bytes.advanced(by:row*stride).assumingMemoryBound(to:UInt8.self),count:480*4)) |
| 36 | precondition(actual == Array(expected[(row*480*4)..<((row+1)*480*4)]), "Encoder surface differs for scene \(scene), frame \(frame)") |
| 37 | } |
| 38 | CVPixelBufferUnlockBaseAddress(buffer!, .readOnly) |
| 39 | } |
| 40 | } |
| 41 | print("PASS: all \(NativeEffects.activeIDs.count) visualizers produce byte-identical encoder-surface frames, including content blur, music response and strobe over consecutive frames") |
| 42 | } |
| 43 | } |