gpu/vk_dev.rs#
Source: lib/gpu/vk_dev.rs
vk_dev -- Vulkan device and queue: creation, the timeline, deferred destruction, command
buffer slots, staged uploads, submission
adapter.create_device(&DeviceDesc) -> Result<Device>
one queue (the first family with graphics + compute, presentation checked per
surface); level Voll when the adapter has it and desc.max_level allows it. Enabled:
timeline semaphores, dynamic rendering (core 1.3 or the KHR extensions on 1.1 / 1.2),
VK_KHR_swapchain when the instance has surfaces, the optional features of Features,
the Vulkan memory model only together with its device scope (else Device-scope
atomics, as in the benchmark's cull, would be invalid); on Voll also descriptor
indexing (update after bind, partially bound, non-uniform indexing) and
draw-indirect-count; maintenance4 on Vulkan 1.3 (LocalSizeId); for shaders compiled
from WGSL (lib/wgsl): f16 in uniform buffers and push constants, the standard uniform
buffer layout, demote-to-helper (discard on Vulkan 1.3), clip distances, the
primitive index (geometryShader), sample-rate shading, dual-source blending in
shaders, the extended storage image formats, storage reads without format. Not
enabled: robust buffer access (speed; lib/wgsl's back end clamps indices instead).
device.queue() -> &Queue queue.submit([cmd, ..]) -> Result<SubmitId>, submit_after(&[id], [cmd, ..])
device.compute_queue() -> Option<&ComputeQueue> (G5d) a queue of its own for compute and copies when
DeviceOptions::compute_queue asked for it (Adapter::create_device_with): a family without graphics where the
device has one (jfgui_GPU_COMPUTE=0: none); encoder(), submit, submit_after
queue.write_buffer(&buf, off, &[T]) staged into an upload command buffer that
queue.write_texture(®ion, &[u8], layout, before, after) runs first in the next submit
queue.write_texture_rects(&tex, &rects, &[u8], stride, before, after) rectangles of a frame
queue.write_texture_rects_at(&tex, &rects, origin, &[u8], stride, before, after) of a larger image
queue.read_buffer(&buf, off, &mut [T]) copies, submits and waits (slow path)
queue.flush() -> Result<SubmitId> submit the pending uploads now
device.wait(id, timeout_ns) -> Result<bool> false: timed out
device.wait_idle(), poll() (frees what the GPU has finished with), is_lost(),
test_lose() (a test aid: the device behaves as lost from now on)
The timeline: one timeline semaphore per device; every submit signals the next value
(SubmitId). completed caches the last value seen reached (vkGetSemaphoreCounterValue at
submits, polls and waits). A dropped resource goes to the garbage with the value of the
latest submit and is destroyed once that value is reached -- earlier if no submit is
pending; a buffer or image, which the open upload command buffer (or one another thread took
and has not submitted yet) may name, waits until every such upload is submitted and then takes
the value reached by then (State::pending). Command buffers come from slots (a transient command pool with one buffer each),
reused once their submission completed. Uploads: the data goes into the staging ring, the
copies into one upload command buffer that ends with a global barrier (copy writes -> every
later access) and is put before the caller's command buffers in the next vkQueueSubmit.
The staging ring: 256 KiB chunks of Upload memory, filled front to back across submissions;
a full chunk is reused once the GPU has passed the last submission that reads from it (two
are kept free, more go back). An upload above half a chunk gets a buffer of its own (its size
rounded up to 64 KiB), reused by a later one that fits it without wasting half of it; once the GPU
is done with them only the largest free one stays, and only until a second after the last such
upload (Device::poll): a frame of small rectangles costs what they hold, not a full frame or a new
allocation, and a frame of many canvas tiles does not keep twice their bytes around afterwards.
Device lost: VK_ERROR_DEVICE_LOST from any call sets the device's lost flag; every later
call returns GpuError::DeviceLost (objects are still destroyed normally).
(G5d) The compute queue comes only when asked for (DeviceOptions; without it a device is what it was in 0.1.0: one
queue, every resource exclusive). It has a timeline of its own (two queues signalling one timeline semaphore could reach its
values out of order); its SubmitIds carry COMPUTE_ID. A submission waits for the other queue's values through
the timeline (submit_after, at all commands). A dropped resource waits for the latest submission of both queues;
command slots come from a pool of each family. Resources are shared between the families concurrently: every
buffer, and every texture that is no render target (render targets stay exclusive to the device's queue: render
passes run there only, and some GPUs compress exclusive targets) -- no ownership transfers in the caller's code.
Pending uploads run on the device's queue: a compute submission flushes them first and waits for them.
struct Devicea logical GPU device: creates resources, records commands, owns the queuestruct ComputeQueue(G5d) a queue of its own for compute shaders and copies, beside the device's queue (Device::compute_queue): its encoders record no render passes, its submissions run in parallel with the device queue's unless one waits for the other (submit_after)struct Queuethe device's queue (graphics, compute, copies, presentation)
impl Adapter
fn create_device(&self, desc: &DeviceDesc) -> Result<Device>a device of this adapter (level: the highest up to desc.max_level), with one queuefn create_device_with(&self, desc: &DeviceDesc, opts: &DeviceOptions) -> Result<Device>(G5d) a device of this adapter with options: a compute queue of its own (Device::compute_queue) where the adapter has a family without graphics and jfgui_GPU_COMPUTE is not 0
impl Device
fn queue(&self) -> &Queuefn compute_queue(&self) -> Option<&ComputeQueue>(G5d) the queue of its own for compute and copies (a family without graphics), when DeviceOptions asked for it; None where the device has none, without asking and with jfgui_GPU_COMPUTE=0 (then everything goes through queue())fn info(&self) -> &AdapterInfofn features(&self) -> &Featureswhat this device has enabledfn limits(&self) -> &Limitsfn level(&self) -> Levelfn is_lost(&self) -> boolthe device was lost: every call fails with DeviceLost; create a new devicefn test_lose(&self)(test aid) behave as a lost device from now on (a lost device runs nothing any more and its resources may go at once; here the GPU may still work on the last submission: it finishes first)fn validating(&self) -> boolthe validation layer watches this devicefn poll(&self) -> Result<()>free what the GPU has finished with (also done by submit and wait)fn wait(&self, id: SubmitId, timeout_ns: u64) -> Result<bool>wait for a submission (of either queue) -> false when timeout_ns passed firstfn wait_idle(&self) -> Result<()>wait for every submission so far (of both queues)fn completed(&self) -> SubmitIdthe last submission the GPU has finishedfn is_done(&self, id: SubmitId) -> boolthe submission id (of either queue) is done on the GPU (asks the driver; never waits)fn memory_reserved(&self) -> u64bytes of device and host memory the device holds in blocks
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 in order once the submissionswait(of either queue; Device::compute_queue) are done: a wait through the timeline, at all commandsfn 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): the region goes frombeforeto CopyDst, gets the data, then goes toafterfn 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 to the texture, empty ones skipped) of mip 0, layer 0 of a 2D texture fromdata, an image of the texture's size in rows ofstridebytes (a framebuffer), before the next submission; the staged bytes are those of the rectangles. The whole level goes frombeforeto CopyDst and then toafter, once for all of themfn 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 (x, y, w, h) ofdata, an image in rows ofstridebytes, go 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 (copies through a readback buffer when needed, submits and waits for the GPU: the slow path, for tools and tests)
impl ComputeQueue
fn encoder(&self) -> Result<crate::CommandEncoder<'_>>(G5d) an encoder of this queue's family: copies, fills, barriers, compute and timestamps (where the family has them); a render pass is Invalid (from finish)fn submit<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, cmds: I) -> Result<SubmitId>(G5d) run this queue's command buffers in order -> their SubmitId (Device::wait takes it); the pending uploads go first, on the device's queuefn submit_after<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, wait: &[SubmitId], cmds: I) -> Result<SubmitId>(G5d) the same once the submissionswait(of either queue) are done: a wait through the timeline, at all commands