PulseCanvas / app · pulsecanvas.ai / scripts / CanvasCoverageSmoke.swift
| 1 | import AppKit |
| 2 | import MetalKit |
| 3 | |
| 4 | @main struct CanvasCoverageSmoke { |
| 5 | static func require(_ condition: Bool, _ message: String) throws { |
| 6 | if !condition { throw NSError(domain: message, code: 1) } |
| 7 | } |
| 8 | struct Coverage { let lit: Double; let width: Double; let height: Double; let occupied: Int } |
| 9 | static func pixels(_ texture: MTLTexture) -> [UInt8] { |
| 10 | var bytes = [UInt8](repeating: 0, count: texture.width * texture.height * 4) |
| 11 | bytes.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: texture.width * 4, |
| 12 | from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0) } |
| 13 | return bytes |
| 14 | } |
| 15 | static func coverage(_ p: [UInt8], width: Int, height: Int) -> Coverage { |
| 16 | var count = 0, minX = width, maxX = 0, minY = height, maxY = 0 |
| 17 | var cells = Set<Int>() |
| 18 | for i in stride(from: 0, to: p.count, by: 4) where max(p[i], max(p[i+1], p[i+2])) > 16 { |
| 19 | let x = (i/4) % width, y = (i/4) / width |
| 20 | count += 1; minX = min(minX, x); maxX = max(maxX, x); minY = min(minY, y); maxY = max(maxY, y) |
| 21 | cells.insert((y * 4 / height) * 4 + x * 4 / width) |
| 22 | } |
| 23 | return Coverage(lit: Double(count) / Double(width * height), width: Double(maxX-minX) / Double(width), |
| 24 | height: Double(maxY-minY) / Double(height), occupied: cells.count) |
| 25 | } |
| 26 | @MainActor static func main() throws { |
| 27 | _ = NSApplication.shared |
| 28 | let state = RenderState(), renderer = VisualizerRenderer(state: state) |
| 29 | var checks = 0, leastWidth = 1.0, leastHeight = 1.0, leastLit = 1.0, leastCells = 16 |
| 30 | let settings: [(Double, Float, Float)] = [(2.3, 1, 0), (0, 0.6, 2.7), (8, 1.55, -2.7), (24, 1, 1.1)] |
| 31 | for (tag, w, h) in [("wide", 256, 144), ("phone", 144, 256)] { |
| 32 | try renderer.prepareOffscreen(width: w, height: h) |
| 33 | for scene in NativeEffects.activeIDs { |
| 34 | state.scene = scene; state.speed = 1; state.brightness = 1; state.particleSize = 1; state.palette = 0 |
| 35 | state.zoom = 1; state.rotation = 0 |
| 36 | let first = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: Array(repeating: 0.2, count: 32))) |
| 37 | let later = pixels(try renderer.renderOffscreen(time: 3.3, audioBands: Array(repeating: 0.2, count: 32))) |
| 38 | let repeated = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: Array(repeating: 0.2, count: 32))) |
| 39 | try require(first == repeated, "Revisiting a frame must be deterministic: \(scene)") |
| 40 | let changed = zip(first, later).filter { abs(Int($0)-Int($1)) > 8 }.count |
| 41 | try require(Double(changed) / Double(w*h*3) > 0.01, "Visible movement required: \(scene) \(tag)") |
| 42 | for (time, zoom, rotation) in settings { |
| 43 | // Check the established fullscreen modes at defaults; the revised fields also cover varied framing. |
| 44 | if [0, 1, 2, 5, 7].contains(scene) && time != 2.3 { continue } |
| 45 | state.zoom = zoom; state.rotation = rotation |
| 46 | let p = pixels(try renderer.renderOffscreen(time: time, audioBands: Array(repeating: 0.2, count: 32))) |
| 47 | let c = coverage(p, width: w, height: h) |
| 48 | if c.width <= 0.95 || c.height <= 0.95 || c.lit <= 0.05 || c.occupied < 14 { |
| 49 | let image = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: w, pixelsHigh: h, |
| 50 | bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, |
| 51 | colorSpaceName: .deviceRGB, bytesPerRow: w*4, bitsPerPixel: 32)! |
| 52 | for pixel in stride(from: 0, to: p.count, by: 4) { |
| 53 | image.bitmapData![pixel] = p[pixel+2]; image.bitmapData![pixel+1] = p[pixel+1] |
| 54 | image.bitmapData![pixel+2] = p[pixel]; image.bitmapData![pixel+3] = 255 |
| 55 | } |
| 56 | let file = URL(fileURLWithPath: ".build/validation/coverage-failure-\(scene)-\(tag).png") |
| 57 | try image.representation(using: .png, properties: [:])!.write(to: file) |
| 58 | print("Failed frame saved: \(file.path)") |
| 59 | } |
| 60 | try require(c.width > 0.95 && c.height > 0.95, "Canvas span failed \(scene) \(tag) t=\(time) zoom=\(zoom): \(c.width),\(c.height)") |
| 61 | try require(c.lit > 0.05 && c.occupied >= 14, "Too sparse or confined: \(scene) \(tag) t=\(time) lit=\(c.lit) cells=\(c.occupied)") |
| 62 | leastWidth = min(leastWidth,c.width); leastHeight = min(leastHeight,c.height) |
| 63 | leastLit = min(leastLit,c.lit); leastCells = min(leastCells,c.occupied); checks += 1 |
| 64 | if time == 2.3 { print("\(tag) \(RenderState.sceneNames[scene]): lit \(String(format: "%.1f", c.lit * 100))%, span \(String(format: "%.1f × %.1f", c.width * 100, c.height * 100))%, occupied \(c.occupied)/16") } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | print("PASS: \(checks) GPU canvas coverage checks; \(NativeEffects.activeIDs.count * 2) motion/determinism checks; minimum spans \(leastWidth), \(leastHeight), lit \(leastLit), cells \(leastCells)/16") |
| 69 | } |
| 70 | } |