JFLib

gpu#

the GPU layer: adapters, devices, buffers, textures, pipelines, command lists, timestamps, swapchains (Vulkan, Metal, WebGPU) -- crate root lib/gpu/lib.rs

gpu -- GPU access for the UI, 2D CAD and 3D games: an explicit API in the manner of Vulkan
and WebGPU over one backend per target (docs/GPU.md).  Backends now: Vulkan on Windows, on Linux,
there in programs linked against glibc, and on Android (vulkan-1.dll, libvulkan.so.1, libvulkan.so
loaded at run time; vk_*.rs; on Linux the windows go through dma-bufs, vk_dmabuf.rs), WebGPU in the
browser (web_*.rs, web/gpu.js; WGSL instead of SPIR-V, what it cannot do is said in web_dev.rs),
Metal on macOS and iOS (mtl_*.rs, AP6 in docs/APPLE.md: SPIR-V translated to MSL by shader::msl,
level Voll on Metal 3 devices of the Apple7 or Mac2 family (A14, M1 and newer), else Basis; what it
lacks is said in mtl_dev.rs).

  Instance::new() / with(&InstanceDesc) -> Result<Instance>   the system's GPU API;
                   Err(Disabled) with jfgui_GPU=0, Err(NoLoader) without a loader (Vulkan,
                   WebGPU), Err(NoAdapter) without a GPU (Metal: without a Metal device)
  instance.adapters() -> Result<Vec<Adapter>>   every GPU, discrete ones first
  instance.adapter() -> Result<Adapter>         the first usable one (jfgui_GPU_ADAPTER: an
                   index into adapters() or a part of the name)
  instance.adapters_low_power(display)          the GPUs for showing windows, frugal ones first
  instance.prepare() -> Result<bool>             adapter and device are there (false: the browser's
                   answer is on its way; never waits; true at once elsewhere)
  adapter.info() / features() / limits() / level() / create_device(&DeviceDesc)
         create_device_with(&DeviceDesc, &DeviceOptions)   (G5d) options: compute_queue (asked for, off by default)
  instance.validating(), validation_messages() -> (errors, warnings)   jfgui_GPU_VALIDATE
  device.queue() -> &Queue; features(), limits(), info(), level(), is_lost(), poll(),
         wait(id, timeout_ns), wait_idle(), memory_reserved()
  device.compute_queue() -> Option<&ComputeQueue>   (G5d) a queue of its own for compute and copies, when
         DeviceOptions asked for it (Vulkan: a family without graphics; None in the browser, without one
         and when not asked for): encoder(), submit, submit_after(&[id], ..); queue.submit_after(&[id], ..)
         waits for the other
  device.create_buffer(&BufferDesc)               Buffer: size(), write(off, &[T]),
                                                  read(off, &mut [T]) (Upload / Readback)
  device.create_texture(&TextureDesc)             Texture: view(), create_view(&ViewDesc),
                                                  width(), height(), format(), mips(), layers()
  device.create_sampler(&SamplerDesc)             Sampler
  ShaderModule::from_spirv(&device, &[u32])       SPIR-V words (1.3 on Vulkan 1.1, up to 1.6);
                                                  entry_points(), constants() (its overrides)
  device.create_bind_group_layout(&[Binding])     BindGroupLayout
  device.create_pipeline_layout(&PipelineLayoutDesc)   PipelineLayout
  device.create_bind_group(&BindGroupDesc)        BindGroup
  device.create_bindless(&BindlessDesc)           Bindless (level Voll): set_texture /
                                                  set_sampler / set_buffer / set_storage_texture
  device.create_compute_pipeline(&ComputePipelineDesc)  ComputePipeline
  device.create_render_pipeline(&RenderPipelineDesc)    RenderPipeline
                   each ShaderStage with its pipeline constants (WGSL override, SPIR-V
                   SpecId): &[Constant { id, value: f64 }], converted to the override's type
                   as WebGPU does; the workgroup size checked against the limits
  device.create_timestamps(n)                     Timestamps: read_ms(first, n) (since the
                                                  device's first read), read_ticks
  device.encoder() -> CommandEncoder              copies, fill, barriers, compute, render
                   passes, draws, indirect draws, timestamps; finish() -> CommandBuffer
  queue.submit([cmd, ..]) -> SubmitId             timeline semaphore value
  queue.write_buffer / write_texture / write_texture_rects / write_texture_rects_at (staged in
                   a ring, before the next submit), read_buffer (waits)
  Surface::new(&instance, plat_window)            a plat window (Windows: HWND; Linux: what its
                                                  display accepts, plat::win_gpu_child)
  Surface::native(&instance, handles, plat_window)  a window given by its handles (a child);
                   track_size(): the size for a rebuild on a worker (Linux)
  device.create_swapchain(&surface, &SwapchainDesc) -> Swapchain: acquire() -> Option<Frame>,
                   view(&frame), texture(&frame), present(frame, [cmd, ..]), resize(),
                   try_acquire() (never waits or rebuilds), rebuild(), and for the same code on
                   every target flush(), busy(), next_look(retry) (a frame reaches a Linux window
                   after the GPU has finished it: vk_dmabuf.rs), damage(rects)

Conventions shared by every backend (and the WGSL compiler): normalized device coordinates
with y up and depth 0 ..= 1, framebuffer and texture origin top left (Vulkan: the viewport
is flipped); front faces counter-clockwise in framebuffer coordinates by default; group g /
binding b of a pipeline layout = WGSL @group(g) @binding(b) = SPIR-V DescriptorSet g /
Binding b; push constants: one block from offset 0 visible to every stage, at most
PUSH_CONSTANTS_MAX bytes; vertex input is optional (vertex pulling from storage buffers).

Feature levels: Basis = what WebGPU and Vulkan 1.1 do (compute, storage buffers, single
indirect draws and dispatches, texture arrays, timestamps where present); Voll = Vulkan 1.3
with descriptor indexing (Bindless), draw_indexed_indirect_count, multi-draw indirect, draw
parameters and subgroup operations.  Mesh shaders and ray tracing are reported in Features,
not enabled.  Renderers ask device.level() / features().

Errors: every call returns Result; misuse the backend can detect (sizes, alignments, states,
a draw without pipeline, a Voll command on Basis) is GpuError::Invalid -- recorded commands
report it from finish().  A lost device (driver reset, GPU removed) is GpuError::DeviceLost
on every later call; drop the Device and create a new one from the adapter.

Lifetimes: resources are reference-counted handles; a command encoder borrows what it
records, and a dropped resource is destroyed once the GPU has finished every submission made
before the drop (the device's timeline).  Nothing waits on the CPU unless asked (wait,
read_buffer, Timestamps::read_*).

Environment: jfgui_GPU=0 (off), jfgui_GPU_DLL (the loader's file name or path),
jfgui_GPU_ADAPTER (index or name part), jfgui_GPU_PRESENT=fifo|mailbox|immediate, jfgui_GPU_COMPUTE=0 (no
queue of its own for compute, even when DeviceOptions asks for one),
jfgui_GPU_VALIDATE=1 (the Khronos validation layer with synchronization validation when it is
installed, messages on stderr), jfgui_GPU_DEBUG=1 (the driver's debug messages).

Sections

Source: docs/API.md in the JFLib repository.