PulseCanvas / app · pulsecanvas.ai / scripts / ProjectMCompositorSmoke.swift
| 1 | import Foundation |
| 2 | import Metal |
| 3 | |
| 4 | @main struct ProjectMCompositorSmoke { |
| 5 | static func main() throws { |
| 6 | let device = MTLCreateSystemDefaultDevice()! |
| 7 | let sourceURL = URL(fileURLWithPath: "Sources/PulseCanvas/Resources/Strobe.metal") |
| 8 | let library = try device.makeLibrary(source: String(contentsOf: sourceURL, encoding: .utf8), options: nil) |
| 9 | let descriptor = MTLRenderPipelineDescriptor() |
| 10 | descriptor.vertexFunction = library.makeFunction(name: "whiteFlashVertex") |
| 11 | descriptor.fragmentFunction = library.makeFunction(name: "projectMFragment") |
| 12 | descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm |
| 13 | let pipeline = try device.makeRenderPipelineState(descriptor: descriptor) |
| 14 | let queue = device.makeCommandQueue()! |
| 15 | // Distinct quadrants expose edge clamping and cropping when source and drawable sizes differ. |
| 16 | let colors: [[UInt8]] = [[0,0,255,255], [0,255,0,255], [255,0,0,255], [255,255,255,255]] |
| 17 | for (width,height) in [(3840,2160), (2160,3840), (2560,1600), (577,325)] { |
| 18 | let size = ProjectMPreviewSize.dimensions(width: width, height: height) |
| 19 | let sourceDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm, width: size.width, height: size.height, mipmapped: false) |
| 20 | sourceDescriptor.storageMode = .shared; sourceDescriptor.usage = [.shaderRead] |
| 21 | let source = device.makeTexture(descriptor: sourceDescriptor)! |
| 22 | var pixels = [UInt8](repeating: 0, count: size.width * size.height * 4) |
| 23 | for y in 0..<size.height { for x in 0..<size.width { |
| 24 | let color = colors[(y >= size.height / 2 ? 2 : 0) + (x >= size.width / 2 ? 1 : 0)] |
| 25 | let offset = (y * size.width + x) * 4 |
| 26 | for channel in 0..<4 { pixels[offset + channel] = color[channel] } |
| 27 | }} |
| 28 | source.replace(region: MTLRegionMake2D(0,0,size.width,size.height), mipmapLevel: 0, withBytes: pixels, bytesPerRow: size.width * 4) |
| 29 | let targetDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm, width: width, height: height, mipmapped: false) |
| 30 | targetDescriptor.storageMode = .shared; targetDescriptor.usage = [.renderTarget] |
| 31 | let target = device.makeTexture(descriptor: targetDescriptor)! |
| 32 | let command = queue.makeCommandBuffer()! |
| 33 | let pass = MTLRenderPassDescriptor(); pass.colorAttachments[0].texture = target |
| 34 | pass.colorAttachments[0].loadAction = .dontCare; pass.colorAttachments[0].storeAction = .store |
| 35 | let encoder = command.makeRenderCommandEncoder(descriptor: pass)! |
| 36 | encoder.setRenderPipelineState(pipeline); encoder.setFragmentTexture(source, index: 0) |
| 37 | var style = SIMD4<Float>(1,1,0,0), dimensions = SIMD2<Float>(Float(width),Float(height)) |
| 38 | encoder.setFragmentBytes(&style, length: MemoryLayout<SIMD4<Float>>.stride, index: 0) |
| 39 | encoder.setFragmentBytes(&dimensions, length: MemoryLayout<SIMD2<Float>>.stride, index: 1) |
| 40 | encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3); encoder.endEncoding() |
| 41 | command.commit(); command.waitUntilCompleted() |
| 42 | if let error = command.error { throw error } |
| 43 | for yFraction in [0.05,0.25,0.75,0.95] { for xFraction in [0.05,0.25,0.75,0.95] { |
| 44 | let x = Int(Double(width) * xFraction), y = Int(Double(height) * yFraction) |
| 45 | var actual = [UInt8](repeating: 0, count: 4) |
| 46 | target.getBytes(&actual, bytesPerRow: 4, from: MTLRegionMake2D(x,y,1,1), mipmapLevel: 0) |
| 47 | let expected = colors[(yFraction < 0.5 ? 2 : 0) + (xFraction >= 0.5 ? 1 : 0)] |
| 48 | guard zip(actual,expected).allSatisfy({abs(Int($0)-Int($1)) <= 1}) else { |
| 49 | throw NSError(domain: "ProjectMCompositor", code: 1, userInfo: [NSLocalizedDescriptionKey: "Incorrect full-screen mapping at \(width)×\(height), (\(xFraction),\(yFraction)): \(actual), expected \(expected)"]) |
| 50 | } |
| 51 | }} |
| 52 | print("PASS full-screen mapping: \(size.width)×\(size.height) OpenGL source fills \(width)×\(height) drawable without edge smearing") |
| 53 | } |
| 54 | } |
| 55 | } |