PulseCanvas / app · pulsecanvas.ai / experiments / metal-studies / Render.swift
| 1 | import Foundation |
| 2 | import Metal |
| 3 | import AppKit |
| 4 | struct Frame { var time: Float; var frame: UInt32; var style: UInt32; var pulse: Float } |
| 5 | let root = URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent("experiments/metal-studies") |
| 6 | let device = MTLCreateSystemDefaultDevice()!, queue=device.makeCommandQueue()! |
| 7 | let library=try device.makeLibrary(source:String(contentsOf:root.appendingPathComponent("Creative.metal"),encoding:.utf8),options:nil) |
| 8 | let pipeline=try device.makeComputePipelineState(function:library.makeFunction(name:"creativeFrame")!) |
| 9 | let width=960,height=540 |
| 10 | let descriptor=MTLTextureDescriptor.texture2DDescriptor(pixelFormat:.rgba8Unorm,width:width,height:height,mipmapped:false) |
| 11 | descriptor.storageMode = .shared;descriptor.usage=[.shaderRead,.shaderWrite] |
| 12 | var previous=device.makeTexture(descriptor:descriptor)!,target=device.makeTexture(descriptor:descriptor)! |
| 13 | let blank=[UInt8](repeating:0,count:width*height*4) |
| 14 | func zero(_ t:MTLTexture) {blank.withUnsafeBytes {t.replace(region:MTLRegionMake2D(0,0,width,height),mipmapLevel:0,withBytes:$0.baseAddress!,bytesPerRow:width*4)}} |
| 15 | for style in 0..<2 { |
| 16 | zero(previous);zero(target) |
| 17 | let out=URL(fileURLWithPath:CommandLine.arguments.count>1 ? CommandLine.arguments[1] : ".build/metal-studies",isDirectory:true) |
| 18 | try FileManager.default.createDirectory(at:out,withIntermediateDirectories:true) |
| 19 | let raw=out.appendingPathComponent("creative-\(style).rgba");FileManager.default.createFile(atPath:raw.path,contents:nil);let file=try FileHandle(forWritingTo:raw) |
| 20 | var pixels=blank, middle=blank |
| 21 | for index in 0..<240 { |
| 22 | let t=Float(index)/60;var f=Frame(time:t,frame:UInt32(index),style:UInt32(style),pulse:pow(max(0,sin(t * .pi*4)),6)) |
| 23 | let command=queue.makeCommandBuffer()!,encoder=command.makeComputeCommandEncoder()!;encoder.setComputePipelineState(pipeline);encoder.setTexture(previous,index:0);encoder.setTexture(target,index:1);encoder.setBytes(&f,length:16,index:0) |
| 24 | encoder.dispatchThreads(MTLSize(width:width,height:height,depth:1),threadsPerThreadgroup:MTLSize(width:16,height:16,depth:1));encoder.endEncoding();command.commit();command.waitUntilCompleted();precondition(command.status == .completed) |
| 25 | pixels.withUnsafeMutableBytes {target.getBytes($0.baseAddress!,bytesPerRow:width*4,from:MTLRegionMake2D(0,0,width,height),mipmapLevel:0)} |
| 26 | if index == 120 { middle = pixels } |
| 27 | try file.write(contentsOf:Data(pixels));swap(&previous,&target) |
| 28 | } |
| 29 | try file.close() |
| 30 | var rgbSum = 0.0, differenceSum = 0.0 |
| 31 | for pixel in stride(from: 0, to: pixels.count, by: 4) { |
| 32 | for channel in 0..<3 { |
| 33 | rgbSum += Double(pixels[pixel + channel]) |
| 34 | differenceSum += abs(Double(pixels[pixel + channel]) - Double(middle[pixel + channel])) |
| 35 | } |
| 36 | } |
| 37 | let channelCount = Double(width * height * 3) |
| 38 | let rgbMean = rgbSum / channelCount, movement = differenceSum / channelCount |
| 39 | precondition(rgbMean > 2 && movement > 1) |
| 40 | print("style \(style): mean RGB \(rgbMean), temporal difference \(movement)") |
| 41 | } |
| 42 | print("PASS: two creative Metal studies rendered at 960×540, 60 fps; all 480 GPU commands completed; last frames contain visible content; synthetic rhythm, no external assets or OpenGL") |