Branchwell

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

Raw 2.8 KB on 87dd19288bbd69b69b6ac6e250fb5d18bfebb877
1import AppKit
2import MetalKit
3import Foundation
4
5/// Repeated identical frames catch rare surface-lighting instability that a
6/// single music/silence comparison can miss. Every render completes on the GPU.
7@main struct SurfaceDeterminismSmoke {
8 static func pixels(_ texture: MTLTexture) -> [UInt8] {
9 var bytes = [UInt8](repeating: 0, count: texture.width * texture.height * 4)
10 bytes.withUnsafeMutableBytes {
11 texture.getBytes($0.baseAddress!, bytesPerRow: texture.width * 4,
12 from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0)
13 }
14 return bytes
15 }
16
17 static func difference(_ a: [UInt8], _ b: [UInt8]) -> String {
18 let changes = zip(a,b).map { abs(Int($0.0)-Int($0.1)) }.filter { $0 > 0 }
19 return "channels=\(changes.count) max=\(changes.max() ?? 0) sum=\(changes.reduce(0,+))"
20 }
21
22 @MainActor static func main() throws {
23 _ = NSApplication.shared
24 let state = RenderState(), renderer = VisualizerRenderer(state: state)
25 let silence = [Float](repeating: 0, count: 32)
26 for (width,height) in [(320,180),(180,320)] {
27 try renderer.prepareOffscreen(width: width, height: height)
28 for scene in [15,16] {
29 for zoom in [Float(0),1.5,3] {
30 state.scene = scene
31 state.chaos = 0
32 state.zoomSpeed = zoom
33 state.motionBlur = 0.4
34 state.strobeEnabled = true
35 state.particleSize = 1.6
36 for index in 0..<200 {
37 let clean = pixels(try renderer.renderOffscreen(time: 0.801, audioBands: silence))
38 let loud = pixels(try renderer.renderOffscreen(time: 0.801,
39 audioBands: (0..<32).map { Float(($0+index)%3)/2 },
40 audioPulse: 1, audioMomentum: 1, audioBeatMotionTime: 4,
41 audioTreblePulse: 1, audioTrebleAttackTime: 0.801))
42 let repeated = pixels(try renderer.renderOffscreen(time: 0.801, audioBands: silence))
43 if clean != loud || clean != repeated {
44 let message = "Surface \(scene), \(width)x\(height), zoom \(zoom), pair \(index): music \(difference(clean,loud)); repeated silence \(difference(clean,repeated))"
45 throw NSError(domain: "SurfaceDeterminism", code: 1,
46 userInfo: [NSLocalizedDescriptionKey: message])
47 }
48 }
49 print("PASS surface \(scene), \(width)x\(height), zoom \(zoom): 200 exact music/silence/repeated-silence pairs")
50 }
51 }
52 }
53 print("PASS 7,200 completed GPU frames retain exact surface pixels at zero Chaos")
54 }
55}