Branchwell

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

Raw 3.5 KB on 32796b3b2cff314d20e7c78bdc17d74e2eace8c1
1import AppKit
2import MetalKit
3import Foundation
4
5@MainActor
6private final class LiveRendererCheck: NSObject {
7 let app: NSApplication
8 let window: NSWindow
9 let state: RenderState
10 let output: VisualizerRenderer
11 var completed = 0
12 var failures = 0
13 let scenes = NativeEffects.activeIDs
14 var count: Int { scenes.count }
15
16 init(app: NSApplication, window: NSWindow, state: RenderState) throws {
17 self.app = app; self.window = window; self.state = state
18 output = VisualizerRenderer(state: state)
19 super.init()
20 try output.prepareOffscreen(width: 400, height: 300)
21 }
22
23 @objc func check(_ timer: Timer) {
24 // Leave the live MTKView drawing between checks. Read pixels through the
25 // same offscreen path used by MP4 export, without capture hooks in the app.
26 let liveError = state.errorMessage
27 do {
28 let texture = try output.renderOffscreen(time: 4.2, audioBands: state.audioBands)
29 var pixels = [UInt8](repeating: 0, count: texture.width * texture.height * 4)
30 texture.getBytes(&pixels, bytesPerRow: texture.width * 4, from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0)
31 var lit = 0; var maxRGB = 0
32 for p in stride(from: 0, to: pixels.count, by: 4) {
33 let v = max(Int(pixels[p]), max(Int(pixels[p+1]), Int(pixels[p+2])))
34 if v > 12 { lit += 1 }; maxRGB = max(maxRGB, v)
35 }
36 let ok = lit > 50 && maxRGB > 30 && liveError == nil && state.errorMessage == nil
37 if !ok { failures += 1 }
38 print("\(ok ? "PASS" : "FAIL") scene \(state.scene) \(RenderState.sceneNames[state.scene]), blur \(state.motionBlur): \(lit) lit pixels, max \(maxRGB), live error \(liveError ?? "none")")
39 } catch { failures += 1; print("FAIL: \(error)") }
40 completed += 1
41 if completed >= count * 2 {
42 timer.invalidate()
43 print("Completed \(completed) live preview/error and offline GPU checks, failures=\(failures)")
44 window.orderOut(nil); app.stop(nil)
45 let exitCode: Int32 = failures == 0 ? 0 : 1
46 DispatchQueue.main.async { exit(exitCode) }
47 } else {
48 state.scene = scenes[completed % count]
49 state.motionBlur = completed >= count ? 0.75 : 0
50 }
51 }
52}
53
54@main struct RendererSmokeTest {
55 @MainActor static func main() throws {
56 let app = NSApplication.shared
57 app.setActivationPolicy(.accessory)
58 let state = RenderState()
59 state.audioBands = (0..<32).map { Float($0 % 8) / 10 }
60 let renderer = VisualizerRenderer(state: state)
61 let view = MTKView(frame: NSRect(x: 0, y: 0, width: 400, height: 300), device: MTLCreateSystemDefaultDevice())
62 view.colorPixelFormat = .bgra8Unorm; view.framebufferOnly = false
63 view.preferredFramesPerSecond = 60; view.delegate = renderer
64 let window = NSWindow(contentRect: view.frame, styleMask: [.titled], backing: .buffered, defer: false)
65 window.title = "Visual renderer validation"; window.contentView = view
66 let check = try LiveRendererCheck(app: app, window: window, state: state)
67 renderer.attach(view)
68 if let error = state.errorMessage { print("FAIL: \(error)"); exit(1) }
69 window.orderFront(nil)
70 Timer.scheduledTimer(timeInterval: 0.2, target: check, selector: #selector(LiveRendererCheck.check(_:)), userInfo: nil, repeats: true)
71 app.run()
72 _ = renderer
73 }
74}