Permalink
Cannot retrieve contributors at this time
x-metal-source-on-harvested-gpu-archive/x-metal-source-on-harvested-gpu-archive/main.swift
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
105 lines (80 sloc)
2.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import Metal | |
| import MetalKit | |
| func shell(_ command: String) throws { | |
| let task = Process() | |
| let pipe = Pipe() | |
| print("\nCommand: \(command)"); | |
| task.standardOutput = pipe | |
| task.standardError = pipe | |
| task.arguments = ["-c", command] | |
| task.executableURL = URL(fileURLWithPath: "/bin/sh") | |
| task.standardInput = nil | |
| try task.run() | |
| let result = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)! | |
| print("Result: \(result)"); | |
| } | |
| print(""" | |
| ----------- | |
| Environment | |
| ----------- | |
| """) | |
| try shell("xcrun --show-sdk-path") | |
| try shell("xcrun xcodebuild -version") | |
| print(""" | |
| ------------------------ | |
| Creating Render Pipeline | |
| ------------------------ | |
| """) | |
| let device = MTLCreateSystemDefaultDevice()! | |
| let commandQueue = device.makeCommandQueue()! | |
| let lib = try device.makeLibrary( | |
| source: """ | |
| #include <metal_stdlib> | |
| using namespace metal; | |
| struct VertexOut { | |
| float4 position [[position]]; | |
| float point_size [[point_size]]; | |
| }; | |
| [[vertex]] | |
| VertexOut main_vertex() { | |
| return { | |
| .position = float4(0), | |
| .point_size = 128.0 | |
| }; | |
| } | |
| [[fragment]] | |
| half4 main_fragment() { | |
| return half4(1); | |
| } | |
| """, | |
| options: nil | |
| ) | |
| let pipelineDesc = MTLRenderPipelineDescriptor() | |
| pipelineDesc.vertexFunction = lib.makeFunction(name: "main_vertex") | |
| pipelineDesc.fragmentFunction = lib.makeFunction(name: "main_fragment") | |
| pipelineDesc.colorAttachments[0]?.pixelFormat = .bgra8Unorm | |
| print(""" | |
| ---------------------- | |
| Harvesting GPU Archive | |
| ---------------------- | |
| """) | |
| let archivePath = NSTemporaryDirectory().appending("harvested-archive.metallib") | |
| let archiveDesc = MTLBinaryArchiveDescriptor() | |
| let archive = try device.makeBinaryArchive(descriptor: archiveDesc) | |
| try archive.addRenderPipelineFunctions(descriptor: pipelineDesc) | |
| try archive.serialize(to: NSURL.fileURL(withPath: archivePath)) | |
| print("Created archive: \(archivePath)") | |
| print(""" | |
| -------------------------------------------- | |
| Verify/Display Information about GPU Archive | |
| -------------------------------------------- | |
| """) | |
| try shell("xcrun metal-readobj \(archivePath)") | |
| print(""" | |
| --------------------------------------------- | |
| Using metal-source to get pipeline descriptor | |
| --------------------------------------------- | |
| """) | |
| let descriptorsPath = NSTemporaryDirectory().appending("descriptors.json") | |
| try shell("xcrun metal-source -flatbuffers=json \(archivePath) -o \(descriptorsPath)") | |