PulseCanvas / app · pulsecanvas.ai / scripts / RendererStartupSmoke.swift
| 1 | import AppKit |
| 2 | import MetalKit |
| 3 | import Foundation |
| 4 | |
| 5 | @main struct RendererStartupSmoke { |
| 6 | @MainActor static func main() async throws { |
| 7 | _ = NSApplication.shared |
| 8 | var heartbeats = 0 |
| 9 | let heartbeat = Task { @MainActor in |
| 10 | while !Task.isCancelled { |
| 11 | heartbeats += 1 |
| 12 | try? await Task.sleep(for: .milliseconds(5)) |
| 13 | } |
| 14 | } |
| 15 | let start = ContinuousClock.now |
| 16 | async let first: Void = VisualizerRenderer.prepareLiveAssets() |
| 17 | async let second: Void = VisualizerRenderer.prepareLiveAssets() |
| 18 | try await first |
| 19 | try await second |
| 20 | heartbeat.cancel() |
| 21 | precondition(heartbeats > 1, "Shader preparation blocked the main actor") |
| 22 | precondition(VisualizerRenderer.shaderResourceBuilds == 1, "Concurrent preparation compiled duplicate resources") |
| 23 | |
| 24 | let state = RenderState(), renderer = VisualizerRenderer(state: RenderState()) |
| 25 | renderer.state = state |
| 26 | try renderer.prepareOffscreen(width: 160, height: 100) |
| 27 | precondition(renderer.isReady) |
| 28 | precondition(VisualizerRenderer.shaderResourceBuilds == 1, "Preview/export did not reuse prepared programs") |
| 29 | let bands = (0..<32).map { Float($0 % 8) / 10 } |
| 30 | for scene in [NativeEffects.activeIDs[0], NativeEffects.defaultScene, NativeEffects.activeIDs.last!] { |
| 31 | state.scene = scene |
| 32 | let texture = try renderer.renderOffscreen(time: 4.2, audioBands: bands) |
| 33 | var pixels = [UInt8](repeating: 0, count: texture.width * texture.height * 4) |
| 34 | pixels.withUnsafeMutableBytes { |
| 35 | texture.getBytes($0.baseAddress!, bytesPerRow: texture.width * 4, |
| 36 | from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0) |
| 37 | } |
| 38 | precondition(pixels.enumerated().contains { $0.offset % 4 != 3 && $0.element > 30 }, "Prepared shaders produced an empty frame") |
| 39 | precondition(state.errorMessage == nil) |
| 40 | } |
| 41 | print("PASS: one shared worker compilation, \(heartbeats) main-actor heartbeats; prepared GPU programs render and reuse correctly") |
| 42 | print("Preparation and three render checks: \(start.duration(to: .now))") |
| 43 | } |
| 44 | } |