gpu/mtl_dev.rs (Apple)#
Source: lib/gpu/mtl_dev.rs
mtl_dev -- Metal on macOS and iOS (AP6, docs/APPLE.md): instance, adapters, device and queue
Instance::new() -> Result<Instance> Err(Disabled) with jfgui_GPU=0, Err(NoAdapter) without
a Metal device; jfgui_GPU_VALIDATE=1 turns Metal's API validation on (MTL_DEBUG_LAYER)
instance.adapters() macOS: MTLCopyAllDevices, iOS: the system's device
adapter.create_device(&DeviceDesc) a command queue and a timeline (MTLSharedEvent); level
Voll on Metal 3 devices of the Apple7 or Mac2 family (A14, M1 and newer), else Basis
device.queue() -> &Queue submit([cmd, ..]) -> SubmitId (the event's value),
write_buffer / write_texture / write_texture_rects(_at) (staged in shared buffers, copied by
a blit encoder before the next submission), read_buffer (submits and waits)
device.wait(id, timeout) / is_done / completed / wait_idle on the event
Lifetimes: command buffers retain what they use (retained references), so a dropped resource
lives as long as a submission needs it -- no deferred freeing here. Synchronization: resources
with Metal's hazard tracking; barriers end the current encoder (mtl_cmd.rs).
A command buffer that ends in an error (MTLCommandBufferStatusError) loses the device: every
later call gives DeviceLost, as on Vulkan after a driver reset.
struct Instancethe system's GPU API (Metal)
impl Instance
fn new() -> Result<Instance>fn with(desc: &InstanceDesc) -> Result<Instance>fn validating(&self) -> boolMetal's API validation is on (jfgui_GPU_VALIDATE=1: MTL_DEBUG_LAYER; it stops the process at a misuse instead of counting messages)fn adapters(&self) -> Result<Vec<Adapter>>every GPU of the system, discrete ones firstfn prepare(&self) -> Result<bool>(the browser: adapter and device come later; here they are there)fn adapter(&self) -> Result<Adapter>the adapter to use: jfgui_GPU_ADAPTER (index or name part), else the first usable onefn adapters_low_power(&self, _display: Option<(u32, u32)>) -> Result<Vec<Adapter>>the adapters for showing windows, frugal ones first (integrated before discrete)fn low_power_rank(kind: AdapterKind, drives_display: bool) -> Option<u32>the order of adapters for showing windows, lower first (None: not for this)fn validation_messages() -> (u32, u32)(errors, warnings) of the validation: Metal's validation stops the process insteadstruct Adaptera GPU
impl Adapter
fn info(&self) -> &AdapterInfofn features(&self) -> &Featuresfn limits(&self) -> &Limitsfn level(&self) -> Levelthe highest level the adapter supports (Voll: Metal 3 on Apple7 and newer or Mac2)fn can_present(&self, _surface: &crate::Surface) -> boolevery Metal device can show in a CAMetalLayerfn create_device(&self, desc: &DeviceDesc) -> Result<Device>fn create_device_with(&self, desc: &DeviceDesc, _opts: &DeviceOptions) -> Result<Device>(G5d) DeviceOptions::compute_queue: Metal has one kind of queue; Device::compute_queue is Nonestruct Devicea logical GPU device: creates resources, records commands, owns the queuestruct Queuethe device's queuestruct ComputeQueue(G5d) a queue of its own for compute: Metal has none apart (Device::compute_queue is None)
impl ComputeQueue
fn encoder(&self) -> Result<crate::CommandEncoder<'_>>fn submit<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, _cmds: I) -> Result<SubmitId>fn submit_after<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, _wait: &[SubmitId], _cmds: I) -> Result<SubmitId>
impl Device
fn queue(&self) -> &Queuefn compute_queue(&self) -> Option<&ComputeQueue>(G5d) None: Metal has one kind of queuefn info(&self) -> &AdapterInfofn features(&self) -> &Featuresfn limits(&self) -> &Limitsfn level(&self) -> Levelfn is_lost(&self) -> boolfn test_lose(&self)(test aid) behave as a lost device from now on (the last submission finishes first)fn validating(&self) -> boolfn poll(&self) -> Result<()>look at what the GPU finished (also done by submit and wait)fn wait(&self, id: SubmitId, timeout_ns: u64) -> Result<bool>wait for a submission -> false when timeout_ns passed firstfn wait_idle(&self) -> Result<()>wait for every submission so farfn completed(&self) -> SubmitIdfn is_done(&self, id: SubmitId) -> boolfn memory_reserved(&self) -> u64bytes of the buffers and textures the device holds
impl Queue
fn submit<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, cmds: I) -> Result<SubmitId>run command buffers in order (after the pending uploads) -> their timeline valuefn submit_after<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, wait: &[SubmitId], cmds: I) -> Result<SubmitId>(G5d) run command buffers after the submissionswait: one queue keeps the order anywayfn flush(&self) -> Result<SubmitId>submit the pending uploads nowfn write_buffer<T: Pod>(&self, buf: &crate::Buffer, offset: u64, data: &[T]) -> Result<()>write data into a buffer at offset before the next submission (staged)fn write_texture(&self, dst: &TextureRegion, data: &[u8], layout: DataLayout, _before: Layout, _after: Layout) -> Result<()>write texels into a texture region before the next submission (staged)fn write_texture_rects(&self, t: &crate::Texture, rects: &[[i32; 4]], data: &[u8], stride: u32, before: Layout, after: Layout) -> Result<()>write rectangles (x, y, w, h; clipped, empty ones skipped) of mip 0, layer 0 of a 2D texture fromdata, an image of the texture's size in rows ofstridebytesfn write_texture_rects_at(&self, t: &crate::Texture, rects: &[[i32; 4]], origin: (i32, i32), data: &[u8], stride: u32, _before: Layout, _after: Layout) -> Result<()>write_texture_rects from an image of another size: rectangles ofdatago to (x - origin.0, y - origin.1) of the texture; what lies outside the image or the texture is skippedfn read_buffer<T: Pod>(&self, buf: &crate::Buffer, offset: u64, out: &mut [T]) -> Result<()>read a buffer's contents at offset (through a readback buffer when needed; submits and waits: the slow path, for tools and tests)