Swiftpack.co - otabuzzman/MetalRTOW as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by otabuzzman.
otabuzzman/MetalRTOW 0.4.9
Ray tracing with Metal.
⭐️ 0
🕓 1 week ago
iOS
.package(url: "https://github.com/otabuzzman/MetalRTOW.git", from: "0.4.9")

MetalRTOW

Ray tracing with Metal. The lab aims to get Peter Shirley's ray tracer from his mini-book Ray Tracing in one weekend (RTOW) with Metal up and running.

Tools

Apps used on iPad

Apps used on Winos 10

Setup

  • Install Cygwin with development tools (for what it's good for)

  • Install Swift on Windows (according to Traditional Installation section)

  • Create repository on GitHub (default settings)

  • Clone repository from GitHub

    Cygwin command prompt (bash)

    git clone https://github.com/otabuzzman/MetalRTOW
    # use SSH (optional)
    # git clone git://github.com/otabuzzman/MetalRTOW
    
    cd MetalRTOW
    
  • Create SSH keys (optional)

    ssh-keygen -t ed25519 -C iuergen.schuck@gmail.com -f ~/.ssh/github.com.MetalRTOW
    chmod 400 ~/.ssh/github.com.MetalRTOW
    
  • Swift package initialization

    Winos command prompt (CMD)

    cd MetalRTOW
    
    swift package init
    
    rem check
    swift build
    
  • Sync package with GitHub

    Cygwin command prompt (bash)

    # enable SSH key usage for session
    # eval "$(ssh-agent -s)"
    # ssh-add ~/.ssh/github.com.MetalRTOW
    
    git add -A
    git push origin main
    
    # tag required to load package with SP4
    git tag -a 0.1.0 -m 'initial commit'
    git push origin 0.1.0
    

Which file for what

File Comment
MUIView.swift A SwiftUI wrapper for MRTView.
MRTView.swift A Metal ray tracer view protocol derived from MTKView.
MRTRenderer.swift A Metal ray tracing renderer protocol.
MRTObject.swift A class to read a Waveform OBJ file.
MTLDevice.swift A protocol extension for MTLDevice to read Metal files from resource files in SP4.
MRTError.swift Error exceptions.

Usage

  • Setup new app in SP4
    Name must differ from MetalRTOW

  • Delete *.swift files

  • Add MetalRTOW package

  • Create file MetalRTOW.swift :

    import SwiftUI
    import MetalRTOW
    
    struct ContentView: View {
        var body: some View {
            MUIView {
                RtwmView()
            }
        }
    }
    
    @main
    struct MyApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
  • Create file RtwmView.swift :

    import MetalKit
    import MetalRTOW
    
    class RtwmView: MTKView, MRTView {
        var renderer: MRTRenderer!
    
        required init() {
            guard let device = MTLCreateSystemDefaultDevice() else {
                let error: Error = MRTError.noDefaultDevice
                fatalError(error.localizedDescription)
            }
            super.init(frame: .zero, device: device)
    
            tune()
        }
    
        required init(coder: NSCoder) {
            super.init(coder: coder)
        }
    
        func tuneMTKView() {
            colorPixelFormat = .bgra8Unorm
            clearColor = MTLClearColor(red: 0.3, green: 0.1, blue: 0.2, alpha: 1.0)
        }
    
        func tuneMRTView() {
            renderer = try? RtwmRenderer(view: self)
        }
    }
    
  • Create file RtwmRenderer.swift :

    import MetalKit
    import MetalRTOW
    
    final class RtwmRenderer: NSObject, MRTRenderer {
        var mtlLibrary: [MTLLibrary]!
    
        func makeAccelerationStructure() {
        }
    
        func makeShaderPipeline() {
        }
    
        func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
        }
    
        func draw(in view: MTKView) {
        }
    }
    
  • Run app and check result :
    A white display with no errors.

Findings

  • No .metal file support in SP4. Metal Shader Language (MSL) code via String class in Swift source files (e.g. ending on .metal.swift) ok for SP4 playgrounds and apps. The latter allows MSL files as resources. File suffixes must have three characters (e.g. .msl). Otherwise (e.g. when using .metal suffix) SP4 will report an unknown resource error on app open.
  • Removing resource files only make them disappear in file system but app still works as if they remain present.

GitHub

link
Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics