Creating Metal Renderer many object out of scopes?

I am trying to create a Renderer class using Metal. I have issues with some classes “objects” Cannot find found in the scope

import Foundation
import MetalKit

class Renderer: NSObject {
  static var device: MTLDevice!
  let commandQueue: MTLCommandQueue
  static var library: MTLLibrary!
  let pipelineState: MTLRenderPipelineState

  init(view: MTKView) {
    guard let device = MTLCreateSystemDefaultDevice(),
      let commandQueue = device.makeCommandQueue() else {
        fatalError("Unable to connect to GPU")
    }
    Renderer.device = device
    self.commandQueue = commandQueue
    Renderer.library = device.makeDefaultLibrary()!
    pipelineState = Renderer.createPipelineState()
    super.init()
  }

  static func createPipelineState() -> MTLRenderPipelineState {
    let pipelineStateDescriptor = MTLRenderPipelineDescriptor()

    // pipeline state properties
    pipelineStateDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
    let vertexFunction = Renderer.library.makeFunction(name: "vertex_main")
    let fragmentFunction = Renderer.library.makeFunction(name: "fragment_main")
    pipelineStateDescriptor.vertexFunction = vertexFunction
    pipelineStateDescriptor.fragmentFunction = fragmentFunction

    return try! Renderer.device.makeRenderPipelineState(descriptor: pipelineStateDescriptor)
  }
}

extension Renderer: MTKViewDelegate {
  func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {

  }

  func draw(in view: MTKView) {
    guard let commandBuffer = commandQueue.makeCommandBuffer(),
      let drawable = view.currentDrawable,
    let descriptor = view.currentRenderPassDescriptor,
    let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor) else {
        return
    }

    commandEncoder.setRenderPipelineState(pipelineState)
    // draw call
    commandEncoder.drawPrimitives(type: .point,
                                  vertexStart: 0,
                                  vertexCount: 1)
    commandEncoder.endEncoding()

    commandBuffer.present(drawable)
    commandBuffer.commit()

  }
}

Hi @a-elnajjar - I have copied and pasted that code into new iOS and macOS apps, and looked at it, and I can’t see anything wrong with it.

If you’re still getting errors, what exact error message are you getting? Are you able to zip up the entire project and upload it?

I get this error on my xcode

ijqOg

Are you creating a project like this?
If not, try to choose as figure
image

Hi @a-elnajjar

You have named your project Metal. This name conflicts with the Metal framework that Apple uses.

Start again with a new project and call it something that doesn’t conflict, and you should be fine.

I’m sorry about the late answer - I’ve been on vacation without internet for a week, but I’m back now.

Also, if you’re using the latest edition (V3 of Metal by Tutorials), it uses SwiftUI for the user interface. You have created a Storyboard app, which is correct, if you are using V1 or V2 of the book.