markusmoenig / metalRay

Code games in Swift and C in this "down to the metal" game framework for Xcode
https://moenig.io
MIT License
9 stars 0 forks source link

nice! #1

Closed meshula closed 1 year ago

meshula commented 1 year ago

Hi, I had a play with your library, adapting to work with my own NSView and engine. In order to make that work, I had to make the Game object agnostic about the View, and instead of pulling data out of an MTKView, I passed in the actual info metalRay needed.

In case you're interested, this is the gist of what I did to adapt metalRay to my own application. Note that I stripped MetalKit entirely, aside from the use of MTKTextureLoader, which is pretty handy. The end result is I can run metalRay as a pass between my 3d rendering and DearImGui, all in the same CommandBuffer.


@objc
class ViewportDescriptor: NSObject {
    @objc var colorAttachmentFormat: MTLPixelFormat = MTLPixelFormat.rgba8Uint
    @objc var viewportSize = vector_uint2(100, 100)
    @objc var scale: Float = 1.0 // dpi scaling factor
    @objc override init() {}
}

@objc
class MetalResources: NSObject {
    @objc var device : MTLDevice?
    @objc var textureLoader : MTKTextureLoader?
    @objc var renderpassDescriptor: MTLRenderPassDescriptor?
    @objc var commandBuffer: MTLCommandBuffer?

    @objc
    init(device: MTLDevice?) {
        self.textureLoader = MTKTextureLoader(device: device!)
    }
}

....

@objc
class Game: NSObject {

    enum State {
        case Idle, Running, Paused
    }

    var state                                   : State = .Idle
    var device                                  : MTLDevice!
    var metalStates                             : MetalStates!
    var draw2D                                  : MetalDraw2D!

    @objc var metalResources : MetalResources
    @objc var viewportDescriptor : ViewportDescriptor

    @objc
    init(metalResources: MetalResources, viewport: ViewportDescriptor) {
        self.metalResources = metalResources
        self.viewportDescriptor = viewport

....  
meshula commented 1 year ago

FWIW, the next thing I want to play with is welding metalRay to Randy Gaul's spritebatch (https://github.com/RandyGaul/cute_headers/blob/master/cute_spritebatch.h), which basically means getting DrawTexture working.

markusmoenig commented 1 year ago

Hi, sorry, just saw your post.

The user input events will come from the MTKView so when you strip it out the future events in metalRay will not work.

btw; I just added SDF text rendering and text size functions.

meshula commented 1 year ago

Awesome! I will give the new text rendering a play. For context, I have a bunch of raylib drawing code for specialized visualization, and the ability to run it under Metal is what attracted me to your work. I have my own event handling already ~ my apps are mostly native Cocoa, so I'm not after a pure raylib port particularly. That's of course not the same as your aim.

Anyway, I wanted to let you know I'm appreciating your effort here, and how I switched it around a bit it for my own needs :)

markusmoenig commented 1 year ago

Ok, got it! :) Honestly I am now moving away from the raylib API as I cannot make it work in metal (rlgl). So better to use my own lol.

meshula commented 1 year ago
image
meshula commented 1 year ago

Since I was getting some use out of your shape drawing routines, I added them back in to my copy ~ adding one more function (and moving the type argument from endShape to startShape) allows all of them to work just fine :)

    func addVertex(_ vertex: float2,_ color: float4) {
        vertexData.append(-Float(viewport.viewportSize.x) / 2.0 + vertex.x * viewport.scale)
        vertexData.append(Float(viewport.viewportSize.y) / 2.0 - vertex.y * viewport.scale)
        vertexData.append(0) // texture coordinate
        vertexData.append(0) // texture coordinate
        vertexData.append(color.x)
        vertexData.append(color.y)
        vertexData.append(color.z)
        vertexData.append(color.w)
        vertexCount += 1
    }

(The DearImGui bit is running through my backend, not metalRay, in case you were wondering.)

meshula commented 1 year ago

Also, the way you set up the render-to-texture target in your API is cool.

markusmoenig commented 1 year ago

Nice! Happy you can use some stuff !

Next up is programmable shaders. Just love that you can share C structs between Swift / C and Metal !