Files
voxelotl-engine/Sources/Voxelotl/Renderer.swift

382 lines
15 KiB
Swift
Raw Normal View History

2024-08-05 15:44:51 +10:00
import Foundation
import Metal
import QuartzCore.CAMetalLayer
import simd
2024-08-05 00:19:49 -07:00
import ShaderTypes
2024-08-05 15:44:51 +10:00
2024-08-05 20:09:33 +10:00
fileprivate let cubeVertices: [ShaderVertex] = [
.init(position: .init(-1, -1, 1, 1), normal: .init( 0, 0, 1, 0), texCoord: .init(0, 0)),
.init(position: .init( 1, -1, 1, 1), normal: .init( 0, 0, 1, 0), texCoord: .init(1, 0)),
.init(position: .init(-1, 1, 1, 1), normal: .init( 0, 0, 1, 0), texCoord: .init(0, 1)),
.init(position: .init( 1, 1, 1, 1), normal: .init( 0, 0, 1, 0), texCoord: .init(1, 1)),
.init(position: .init( 1, -1, 1, 1), normal: .init( 1, 0, 0, 0), texCoord: .init(0, 0)),
.init(position: .init( 1, -1, -1, 1), normal: .init( 1, 0, 0, 0), texCoord: .init(1, 0)),
.init(position: .init( 1, 1, 1, 1), normal: .init( 1, 0, 0, 0), texCoord: .init(0, 1)),
.init(position: .init( 1, 1, -1, 1), normal: .init( 1, 0, 0, 0), texCoord: .init(1, 1)),
.init(position: .init( 1, -1, -1, 1), normal: .init( 0, 0, -1, 0), texCoord: .init(0, 0)),
.init(position: .init(-1, -1, -1, 1), normal: .init( 0, 0, -1, 0), texCoord: .init(1, 0)),
.init(position: .init( 1, 1, -1, 1), normal: .init( 0, 0, -1, 0), texCoord: .init(0, 1)),
.init(position: .init(-1, 1, -1, 1), normal: .init( 0, 0, -1, 0), texCoord: .init(1, 1)),
.init(position: .init(-1, -1, -1, 1), normal: .init(-1, 0, 0, 0), texCoord: .init(0, 0)),
.init(position: .init(-1, -1, 1, 1), normal: .init(-1, 0, 0, 0), texCoord: .init(1, 0)),
.init(position: .init(-1, 1, -1, 1), normal: .init(-1, 0, 0, 0), texCoord: .init(0, 1)),
.init(position: .init(-1, 1, 1, 1), normal: .init(-1, 0, 0, 0), texCoord: .init(1, 1)),
.init(position: .init(-1, -1, -1, 1), normal: .init( 0, -1, 0, 0), texCoord: .init(0, 0)),
.init(position: .init( 1, -1, -1, 1), normal: .init( 0, -1, 0, 0), texCoord: .init(1, 0)),
.init(position: .init(-1, -1, 1, 1), normal: .init( 0, -1, 0, 0), texCoord: .init(0, 1)),
.init(position: .init( 1, -1, 1, 1), normal: .init( 0, -1, 0, 0), texCoord: .init(1, 1)),
.init(position: .init(-1, 1, 1, 1), normal: .init( 0, 1, 0, 0), texCoord: .init(0, 0)),
.init(position: .init( 1, 1, 1, 1), normal: .init( 0, 1, 0, 0), texCoord: .init(1, 0)),
.init(position: .init(-1, 1, -1, 1), normal: .init( 0, 1, 0, 0), texCoord: .init(0, 1)),
.init(position: .init( 1, 1, -1, 1), normal: .init( 0, 1, 0, 0), texCoord: .init(1, 1)),
]
fileprivate let cubeIndices: [UInt16] = [
0, 1, 2, 2, 1, 3,
4, 5, 6, 6, 5, 7,
8, 9, 10, 10, 9, 11,
12, 13, 14, 14, 13, 15,
16, 17, 18, 18, 17, 19,
20, 21, 22, 22, 21, 23
]
2024-08-05 15:44:51 +10:00
fileprivate let numFramesInFlight: Int = 3
fileprivate let depthFormat: MTLPixelFormat = .depth16Unorm
2024-08-07 16:36:23 +10:00
2024-08-13 08:38:21 +10:00
public class Renderer {
2024-08-05 15:44:51 +10:00
private var device: MTLDevice
private var layer: CAMetalLayer
2024-08-13 08:38:21 +10:00
private var backBufferSize: Size<Int>
private var _aspectRatio: Float
2024-08-05 15:44:51 +10:00
private var queue: MTLCommandQueue
private var lib: MTLLibrary
private let passDescription = MTLRenderPassDescriptor()
private var pso: MTLRenderPipelineState
2024-08-07 16:36:23 +10:00
private var depthStencilState: MTLDepthStencilState
private var depthTextures: [MTLTexture]
2024-08-05 15:44:51 +10:00
2024-08-13 21:04:16 +10:00
private var _encoder: MTLRenderCommandEncoder! = nil
2024-08-13 08:38:21 +10:00
2024-08-05 20:09:33 +10:00
private var vtxBuffer: MTLBuffer, idxBuffer: MTLBuffer
private var defaultTexture: MTLTexture
private var cubeTexture: MTLTexture? = nil
2024-08-05 15:44:51 +10:00
private let inFlightSemaphore = DispatchSemaphore(value: numFramesInFlight)
2024-08-13 08:38:21 +10:00
private var currentFrame = 0
var frame: Rect<Int> { .init(origin: .zero, size: self.backBufferSize) }
var aspectRatio: Float { self._aspectRatio }
2024-08-05 15:44:51 +10:00
fileprivate static func createMetalDevice() -> MTLDevice? {
MTLCopyAllDevices().reduce(nil, { best, dev in
if best == nil { dev }
else if !best!.isLowPower || dev.isLowPower { best }
else if best!.supportsRaytracing || !dev.supportsRaytracing { best }
else { dev }
})
}
2024-08-13 08:38:21 +10:00
internal init(layer metalLayer: CAMetalLayer, size: Size<Int>) throws {
2024-08-05 15:44:51 +10:00
self.layer = metalLayer
// Select best Metal device
guard let device = Self.createMetalDevice() else {
throw RendererError.initFailure("Failed to create Metal device")
}
self.device = device
layer.device = device
layer.pixelFormat = MTLPixelFormat.bgra8Unorm
// Setup command queue
guard let queue = device.makeCommandQueue() else {
throw RendererError.initFailure("Failed to create command queue")
}
self.queue = queue
2024-08-07 16:36:23 +10:00
2024-08-13 08:38:21 +10:00
self.backBufferSize = size
self._aspectRatio = Float(self.backBufferSize.w) / Float(self.backBufferSize.w)
2024-08-07 16:36:23 +10:00
passDescription.colorAttachments[0].loadAction = .clear
passDescription.colorAttachments[0].storeAction = .store
2024-08-05 15:44:51 +10:00
passDescription.colorAttachments[0].clearColor = MTLClearColorMake(0.1, 0.1, 0.1, 1.0)
2024-08-07 16:36:23 +10:00
passDescription.depthAttachment.loadAction = .clear
passDescription.depthAttachment.storeAction = .dontCare
passDescription.depthAttachment.clearDepth = 1.0
self.depthTextures = try (0..<numFramesInFlight).map { _ in
guard let depthStencilTexture = Self.createDepthTexture(device, size, format: depthFormat) else {
throw RendererError.initFailure("Failed to create depth buffer")
}
return depthStencilTexture
2024-08-07 16:36:23 +10:00
}
let stencilDepthDescription = MTLDepthStencilDescriptor()
stencilDepthDescription.depthCompareFunction = .less // OpenGL default
stencilDepthDescription.isDepthWriteEnabled = true
guard let depthStencilState = device.makeDepthStencilState(descriptor: stencilDepthDescription) else {
throw RendererError.initFailure("Failed to create depth stencil state")
}
self.depthStencilState = depthStencilState
2024-08-05 15:44:51 +10:00
// Create shader library & grab functions
do {
2024-08-05 00:08:16 -07:00
self.lib = try device.makeDefaultLibrary(bundle: Bundle.main)
} catch {
2024-08-05 15:44:51 +10:00
throw RendererError.initFailure("Metal shader compilation failed:\n\(error.localizedDescription)")
}
let vertexProgram = lib.makeFunction(name: "vertexMain")
let fragmentProgram = lib.makeFunction(name: "fragmentMain")
// Set up pipeline state
let pipeDescription = MTLRenderPipelineDescriptor()
pipeDescription.vertexFunction = vertexProgram
pipeDescription.fragmentFunction = fragmentProgram
pipeDescription.colorAttachments[0].pixelFormat = layer.pixelFormat
2024-08-07 16:36:23 +10:00
pipeDescription.depthAttachmentPixelFormat = depthFormat
2024-08-05 15:44:51 +10:00
do {
self.pso = try device.makeRenderPipelineState(descriptor: pipeDescription)
2024-08-05 00:08:16 -07:00
} catch {
2024-08-05 15:44:51 +10:00
throw RendererError.initFailure("Failed to create pipeline state: \(error.localizedDescription)")
}
2024-08-05 20:09:33 +10:00
// Create cube mesh buffers
2024-08-05 15:44:51 +10:00
guard let vtxBuffer = device.makeBuffer(
2024-08-05 20:09:33 +10:00
bytes: cubeVertices,
length: cubeVertices.count * MemoryLayout<ShaderVertex>.stride,
2024-08-05 15:44:51 +10:00
options: .storageModeManaged)
else {
throw RendererError.initFailure("Failed to create vertex buffer")
}
self.vtxBuffer = vtxBuffer
2024-08-05 20:09:33 +10:00
guard let idxBuffer = device.makeBuffer(
bytes: cubeIndices,
length: cubeIndices.count * MemoryLayout<UInt16>.stride,
options: .storageModeManaged)
else {
throw RendererError.initFailure("Failed to create index buffer")
}
self.idxBuffer = idxBuffer
2024-08-05 20:56:06 +10:00
// Create a default texture
2024-08-05 20:09:33 +10:00
do {
2024-08-06 16:34:51 +10:00
self.defaultTexture = try Self.loadTexture(device, queue, image2D: Image2D(Data([
2024-08-05 20:09:33 +10:00
0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF,
0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF
]), format: .abgr8888, width: 2, height: 2, stride: 2 * 4))
} catch {
throw RendererError.initFailure("Failed to create default texture")
}
2024-08-05 20:56:06 +10:00
// Load texture from a file in the bundle
2024-08-05 20:09:33 +10:00
do {
2024-08-06 16:34:51 +10:00
self.cubeTexture = try Self.loadTexture(device, queue, resourcePath: "test.png")
2024-08-05 20:09:33 +10:00
} catch RendererError.loadFailure(let message) {
2024-08-05 20:56:06 +10:00
printErr("Failed to load texture image: \(message)")
2024-08-05 20:09:33 +10:00
} catch {
2024-08-05 20:56:06 +10:00
printErr("Failed to load texture image: unknown error")
2024-08-05 20:09:33 +10:00
}
2024-08-05 15:44:51 +10:00
}
deinit {
}
2024-08-06 16:34:51 +10:00
static func loadTexture(_ device: MTLDevice, _ queue: MTLCommandQueue, resourcePath path: String) throws -> MTLTexture {
2024-08-05 20:09:33 +10:00
do {
2024-08-06 16:34:51 +10:00
return try loadTexture(device, queue, url: Bundle.main.getResource(path))
2024-08-05 20:09:33 +10:00
} catch ContentError.resourceNotFound(let message) {
throw RendererError.loadFailure(message)
}
}
2024-08-06 16:34:51 +10:00
static func loadTexture(_ device: MTLDevice, _ queue: MTLCommandQueue, url imageUrl: URL) throws -> MTLTexture {
2024-08-05 20:09:33 +10:00
do {
2024-08-06 16:34:51 +10:00
return try loadTexture(device, queue, image2D: try NSImageLoader.open(url: imageUrl))
2024-08-05 20:09:33 +10:00
} catch ImageLoaderError.openFailed(let message) {
throw RendererError.loadFailure(message)
}
}
2024-08-06 16:34:51 +10:00
static func loadTexture(_ device: MTLDevice, _ queue: MTLCommandQueue, image2D image: Image2D) throws -> MTLTexture {
2024-08-13 21:04:16 +10:00
try autoreleasepool {
let texDesc = MTLTextureDescriptor()
texDesc.width = image.width
texDesc.height = image.height
texDesc.pixelFormat = .rgba8Unorm_srgb
texDesc.textureType = .type2D
texDesc.storageMode = .private
texDesc.usage = .shaderRead
guard let newTexture = device.makeTexture(descriptor: texDesc) else {
throw RendererError.loadFailure("Failed to create texture descriptor")
}
2024-08-06 16:34:51 +10:00
2024-08-13 21:04:16 +10:00
guard let texData = image.data.withUnsafeBytes({ bytes in
device.makeBuffer(bytes: bytes.baseAddress!, length: bytes.count, options: [ .storageModeShared ])
}) else {
throw RendererError.loadFailure("Failed to create shared texture data buffer")
}
2024-08-06 16:34:51 +10:00
2024-08-13 21:04:16 +10:00
guard let cmdBuffer = queue.makeCommandBuffer(),
let blitEncoder = cmdBuffer.makeBlitCommandEncoder()
else {
throw RendererError.loadFailure("Failed to create blit command encoder")
}
2024-08-06 16:34:51 +10:00
2024-08-13 21:04:16 +10:00
blitEncoder.copy(
from: texData,
sourceOffset: 0,
sourceBytesPerRow: image.stride,
sourceBytesPerImage: image.stride * image.height,
sourceSize: .init(width: image.width, height: image.height, depth: 1),
to: newTexture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin: .init(x: 0, y: 0, z: 0))
blitEncoder.endEncoding()
cmdBuffer.addCompletedHandler { _ in
//FIXME: look into if this needs to be synchronised
//printErr("Texture was added?")
}
cmdBuffer.commit()
2024-08-06 16:34:51 +10:00
2024-08-13 21:04:16 +10:00
return newTexture
}
2024-08-05 20:09:33 +10:00
}
2024-08-13 08:38:21 +10:00
private static func createDepthTexture(_ device: MTLDevice, _ size: Size<Int>, format: MTLPixelFormat
2024-08-07 16:36:23 +10:00
) -> MTLTexture? {
2024-08-13 21:04:16 +10:00
autoreleasepool {
let texDescriptor = MTLTextureDescriptor.texture2DDescriptor(
pixelFormat: format,
width: size.w,
height: size.h,
mipmapped: false)
texDescriptor.depth = 1
texDescriptor.sampleCount = 1
texDescriptor.usage = [ .renderTarget, .shaderRead ]
#if !NDEBUG
texDescriptor.storageMode = .private
#else
texDescriptor.storageMode = .memoryless
#endif
guard let depthStencilTexture = device.makeTexture(descriptor: texDescriptor) else { return nil }
depthStencilTexture.label = "Depth buffer"
return depthStencilTexture
}
2024-08-07 16:36:23 +10:00
}
2024-08-13 08:38:21 +10:00
static func makeViewport(rect: Rect<Int>, znear: Double = 0.0, zfar: Double = 1.0) -> MTLViewport {
2024-08-09 20:53:30 +10:00
MTLViewport(
2024-08-13 08:38:21 +10:00
originX: Double(rect.x),
originY: Double(rect.y),
width: Double(rect.w),
height: Double(rect.h),
znear: znear, zfar: zfar)
2024-08-09 20:53:30 +10:00
}
2024-08-13 08:38:21 +10:00
func resize(size: Size<Int>) {
if self.backBufferSize.w != size.w || self.backBufferSize.h != size.h {
self.depthTextures = (0..<numFramesInFlight).map { _ in
Self.createDepthTexture(device, size, format: depthFormat)!
2024-08-07 16:36:23 +10:00
}
}
2024-08-13 08:38:21 +10:00
self.backBufferSize = size
self._aspectRatio = Float(self.backBufferSize.w) / Float(self.backBufferSize.h)
2024-08-05 15:44:51 +10:00
}
2024-08-13 21:04:16 +10:00
func newFrame(_ frameFunc: (Renderer) -> Void) throws {
try autoreleasepool {
guard let rt = layer.nextDrawable() else {
throw RendererError.drawFailure("Failed to get next drawable render target")
}
2024-08-05 15:44:51 +10:00
2024-08-13 21:04:16 +10:00
passDescription.colorAttachments[0].texture = rt.texture
passDescription.depthAttachment.texture = self.depthTextures[self.currentFrame]
2024-08-05 15:44:51 +10:00
2024-08-13 21:04:16 +10:00
// Lock the semaphore here if too many frames are "in flight"
_ = inFlightSemaphore.wait(timeout: .distantFuture)
2024-08-13 21:04:16 +10:00
guard let commandBuf: MTLCommandBuffer = queue.makeCommandBuffer() else {
throw RendererError.drawFailure("Failed to make command buffer from queue")
}
commandBuf.addCompletedHandler { _ in
self.inFlightSemaphore.signal()
}
2024-08-13 08:38:21 +10:00
2024-08-13 21:04:16 +10:00
guard let encoder = commandBuf.makeRenderCommandEncoder(descriptor: passDescription) else {
throw RendererError.drawFailure("Failed to make render encoder from command buffer")
}
2024-08-05 15:44:51 +10:00
2024-08-13 21:04:16 +10:00
encoder.setCullMode(.back)
encoder.setFrontFacing(.counterClockwise) // OpenGL default
encoder.setViewport(Self.makeViewport(rect: self.frame))
encoder.setRenderPipelineState(pso)
encoder.setDepthStencilState(depthStencilState)
encoder.setFragmentTexture(cubeTexture ?? defaultTexture, index: 0)
encoder.setVertexBuffer(vtxBuffer,
offset: 0,
index: ShaderInputIdx.vertices.rawValue)
self._encoder = encoder
frameFunc(self)
self._encoder = nil
encoder.endEncoding()
commandBuf.present(rt)
commandBuf.commit()
self.currentFrame &+= 1
if self.currentFrame == numFramesInFlight {
self.currentFrame = 0
}
}
2024-08-13 08:38:21 +10:00
}
2024-08-05 15:44:51 +10:00
2024-08-13 08:38:21 +10:00
func batch(instances: [Instance], camera: Camera) {
2024-08-13 21:04:16 +10:00
assert(self._encoder != nil, "batch can't be called outside of a frame being rendered")
2024-08-13 08:38:21 +10:00
assert(instances.count < 52)
var uniforms = ShaderUniforms(projView: camera.viewProjection)
let instances = instances.map { (instance: Instance) -> ShaderInstance in
ShaderInstance(
model:
.translate(instance.position) *
matrix_float4x4(instance.rotation) *
.scale(instance.scale),
color: .init(
UInt8(instance.color.x * 0xFF),
UInt8(instance.color.y * 0xFF),
UInt8(instance.color.z * 0xFF),
UInt8(instance.color.w * 0xFF)))
}
2024-08-06 16:51:29 +10:00
// Ideal as long as our uniforms total 4 KB or less
2024-08-13 08:38:21 +10:00
self._encoder.setVertexBytes(instances,
2024-08-09 21:16:07 +10:00
length: instances.count * MemoryLayout<ShaderInstance>.stride,
index: ShaderInputIdx.instance.rawValue)
2024-08-13 08:38:21 +10:00
self._encoder.setVertexBytes(&uniforms,
2024-08-06 16:51:29 +10:00
length: MemoryLayout<ShaderUniforms>.stride,
index: ShaderInputIdx.uniforms.rawValue)
2024-08-09 21:16:07 +10:00
2024-08-13 08:38:21 +10:00
self._encoder.drawIndexedPrimitives(
2024-08-05 20:09:33 +10:00
type: .triangle,
indexCount: cubeIndices.count,
indexType: .uint16,
indexBuffer: idxBuffer,
2024-08-09 21:16:07 +10:00
indexBufferOffset: 0,
instanceCount: instances.count)
2024-08-13 08:38:21 +10:00
}
2024-08-05 15:44:51 +10:00
}
enum RendererError: Error {
case initFailure(_ message: String)
2024-08-05 20:09:33 +10:00
case loadFailure(_ message: String)
2024-08-05 15:44:51 +10:00
case drawFailure(_ message: String)
}