dymokomi/luce-gpu
Source of release 0.5.2
0.5.2 / src / gpu / metal / device.lucb
#==============================================================================================
#
# device - The Metal device handle
#
# DESCRIPTION:
# The Metal device handle, kept private to the backend.
#
#==============================================================================================
struct MetalDevice:
var device: void*?
var queue: void*?
var drawing: MetalDrawing*?
# The readback ring; see metal/transfer.lucb.
var reads: MetalRingSlot[3]
# Compute state; see metal/compute.lucb.
var compute: MetalCompute*?
# The newest submission's serial, and the ring of recent ones (metal/submission.lucb).
var serial: u64
var submissions: MetalSubmission[32]
# The open command buffer, its reserved serial and how many encodings it holds.
var open: void*?
var open_serial: u64
var open_uses: usize
# Render-pass uploads, copies and readback (metal/copies.lucb).
var copies: MetalCopies*?
# The upload ring (metal/ring.lucb).
var ring: MetalChunk*?
var ring_current: MetalChunk*?
func metal_device_open() -> MetalDevice*!:
let pool = objc_autoreleasePoolPush()
defer objc_autoreleasePoolPop(pool)
let state = try new MetalDevice() in memory.heap
errdefer metal_device_destroy(state)
# The C creation function returns +1, as do Objective-C new methods.
state.device = MTLCreateSystemDefaultDevice() else error(unavailable, "no Metal device is available")
state.queue = metal_ptr(metal_required(state.device), metal_sel("newCommandQueue")) else error(failed, "could not create a Metal command queue")
return state
## The recommended working set, what the device holds now, and unified memory.
func metal_memory_report(state: MetalDevice*) -> DeviceMemory:
let device = state.device else return DeviceMemory()
let unified = (metal_uint(device, metal_sel("hasUnifiedMemory")) & 0xff) != 0
return DeviceMemory(budget = metal_uint(device, metal_sel("recommendedMaxWorkingSetSize")), used = metal_uint(device, metal_sel("currentAllocatedSize")), unified = unified)
func metal_device_destroy(state: MetalDevice*):
let pool = objc_autoreleasePoolPush()
defer objc_autoreleasePoolPop(pool)
if let drawing = state.drawing:
metal_drawing_destroy(drawing)
metal_rings_destroy(state)
metal_compute_destroy(state)
metal_submissions_destroy(state)
metal_ring_destroy(state)
if let copies = state.copies: metal_copies_destroy(copies)
if let queue = state.queue:
metal_void(queue, metal_sel("release"))
if let device = state.device:
metal_void(device, metal_sel("release"))
free(state) in memory.heap