dymokomi/luce-gpu
Source of release 0.5.4
0.5.4 / src / gpu / device.lucb
#==============================================================================================
#
# device - An owned GPU device
#
# DESCRIPTION:
# An owned device. All GPU operations currently require the main thread. The zero value is
# closed; copies alias ownership, so borrow and destroy one owner. Surfaces retain the
# underlying device and remain usable after this handle is destroyed. No window system is
# initialized by opening a device alone.
#
#==============================================================================================
## An owned GPU device; the zero value is closed and copies alias ownership.
pub struct Device:
var state: DeviceState*?
## Open a device on the selected backend, probing the hardware.
pub static func open(backend: Backend = Backend.automatic) -> Device!:
try check_thread()
let selected = try select_backend(backend)
let native = try backend_device_open(selected)
errdefer backend_device_destroy(selected, native)
let state = try new DeviceState(backend = selected, native = native, references = 1, destroy_texture = backend_texture_destroyer(selected), destroy_shader = backend_shader_destroyer(selected),
destroy_pipeline = backend_pipeline_destroyer(selected), destroy_geometry = backend_geometry_destroyer(selected), destroy_buffer = backend_buffer_destroyer(selected), destroy_kernel = backend_kernel_destroyer(selected),
destroy_device = backend_device_destroy, limits = backend_limits(selected, native)) in memory.heap
return Device(state = state)
## Whether this handle still owns a device (the zero value is closed).
pub func is_open() -> bool:
return self.state != none
## The backend this device runs on.
pub func backend() -> Backend!:
return (try self.checked()).backend
## Another owner of the same device, destroyed on its own: documents that
## share their textures share one device.
pub func share() -> Device!:
let state = try self.checked()
state.references += 1
return Device(state = state)
## How many pooled memory pages the device holds for textures, after
## releasing what finished work no longer needs: a diagnostic for tests
## and memory reports. Backends without pages report zero.
pub func memory_pages() -> usize!:
let state = try self.checked()
return backend_memory_pages(state)
## How much memory the device offers this process and how much it holds
## now, for budgets that keep work inside it.
pub func memory() -> DeviceMemory!:
let state = try self.checked()
return backend_memory(state)
## What the device can do: buffer sizes, compute group sizes and counts,
## and optional features such as float atomics.
pub func limits() -> Limits!:
return (try self.checked()).limits
## Whether `submission` has finished on the GPU, without waiting.
pub func done(submission: Submission) -> bool!:
let state = try self.checked()
return backend_submission_done(state, submission)
## Wait until `submission` has finished; `execution_failed` if the GPU
## failed while running it.
pub func wait(submission: Submission) -> !:
let state = try self.checked()
try backend_submission_wait(state, submission)
## When the GPU started and finished `submission`, once it has finished:
## none while it runs, for submissions older than the last
## `timed_submissions`, and where the device keeps no time (`limits().timestamps`).
pub func gpu_time(submission: Submission) -> GpuTime?!:
let state = try self.checked()
if not backend_submission_done(state, submission):
return none
return backend_submission_time(state, submission)
## Whether `other` is the same device.
pub func same(other: const Device*) -> bool:
return self.state != none and self.state == other.state
## Release the device on the main thread; open surfaces keep it alive until they close.
pub func destroy():
let state = self.state else return
require_thread()
self.state = none
release_device(state)
func checked() -> DeviceState*!:
try check_thread()
return self.state else error(closed, "the GPU device is closed")
## A device's memory as its backend reports it. `budget` is what the process
## may use without the system paging GPU memory (Metal's recommended working
## set; Vulkan's largest device-local heap), `used` what this process holds
## there now. On `unified` memory (Apple silicon, integrated GPUs) the GPU's
## memory is the machine's RAM too.
pub struct DeviceMemory:
pub var budget: u64 = 0
pub var used: u64 = 0
pub var unified: bool = false
## A device's limits and optional features, as its backend reports them.
pub struct Limits:
## The most bytes one buffer holds (Metal's maxBufferLength; Vulkan's
## maxStorageBufferRange, capped by the largest single allocation).
pub var buffer_bytes: u64 = 0
## The most invocations one compute workgroup holds, in all.
pub var group_threads: u32 = 0
## The largest workgroup size in each dimension.
pub var group_size: u32[3]
## The most workgroups one dispatch launches in each dimension.
pub var group_count: u32[3]
## The bytes of workgroup-shared memory (GLSL `shared`) a kernel may use.
pub var shared_bytes: u32 = 0
## Whether kernels may add to 32-bit floats atomically in storage buffers
## (GL_EXT_shader_atomic_float's atomicAdd).
pub var float_atomic_add: bool = false
## The invocations in one subgroup (a SIMD group on Metal, a wave or warp
## elsewhere): what GLSL's gl_SubgroupSize reports in a kernel.
pub var subgroup_size: u32 = 0
## Whether kernels may use the KHR subgroup operations: basic, vote,
## arithmetic, ballot and shuffle (GL_KHR_shader_subgroup_*).
pub var subgroup_ops: bool = false
## Whether the device times submissions and compute passes' timestamps
## (`Device.gpu_time`, `Compute.timestamp`).
pub var timestamps: bool = false
## Whether buffers have GPU addresses (`Buffer.address`) for
## GL_EXT_buffer_reference: Vulkan's bufferDeviceAddress, Metal 3's gpuAddress.
pub var buffer_addresses: bool = false
## Whether kernels can read texture tables (`TextureTable`): Vulkan's
## descriptor indexing, Metal's tier-2 argument buffers.
pub var texture_tables: bool = false
## The most slots a texture table has.
pub var texture_table_size: u32 = 0
## Whether kernels can trace rays with GL_EXT_ray_query against Tlases
## (`Blas`, `Tlas`): Metal's supportsRaytracing; Vulkan's
## VK_KHR_acceleration_structure and VK_KHR_ray_query.
pub var ray_query: bool = false
struct DeviceState:
var backend: Backend
var native: void*
var references: usize
# Installed by the backend when the device opens. Texture release is reached
# from portable finalizers (a canvas retains its textures), and a direct call
# into a backend from there would make every program link that backend's
# library whether or not it ever opens a device.
var destroy_texture: func(void*, void*) -> unit
var destroy_shader: func(void*, void*) -> unit
var destroy_pipeline: func(void*, void*) -> unit
var destroy_geometry: func(void*, void*) -> unit
var destroy_buffer: func(void*, void*) -> unit
var destroy_kernel: func(void*, void*) -> unit
var destroy_device: func(Backend, void*) -> unit
var limits: Limits
# Open kernels, by what they were built from (kernel_build.lucb).
var kernels: KernelState*[]
var kernel_count: usize
# Buffers whose addresses were taken (Buffer.address): every compute
# pass counts as using them.
var addressed: BufferState*[]
var addressed_count: usize
func release_device(state: DeviceState*):
state.references -= 1
if state.references == 0:
state.destroy_device(state.backend, state.native)
if state.addressed.length > 0: free(state.addressed) in memory.heap
if state.kernels.length > 0: free(state.kernels) in memory.heap
free(state) in memory.heap