NinjaDeveloper
NinjaDeveloper

Reputation: 1712

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()

  }
}

enter image description here

Upvotes: 0

Views: 211

Answers (1)

Caroline
Caroline

Reputation: 4970

Your project name Metal conflicts with the Apple framework named Metal. You need to use a different name for your project.

Upvotes: 1

Related Questions