JFLib

gpu/web_dev.rs#

Source: lib/gpu/web_dev.rs

web_dev -- WebGPU in the browser: instance, adapter, device and queue, through web/gpu.js

  Instance::new() -> Result<Instance>     Err(Disabled) with jfgui_GPU=0, Err(NoLoader) without
       navigator.gpu; starts requestAdapter and requestDevice, which answer later
  instance.prepare() -> Result<bool>      true: adapter and device are there (never waits; the
       worker has to return to the browser's event loop for it); Err(NoAdapter), Err(Failed)
  instance.adapter() / adapters() / adapters_low_power(display)   Err(NotReady) before that; one
       adapter (the browser chooses, "low-power" asked); a fallback adapter (software) is Cpu and
       not among adapters_low_power
  adapter.create_device(&DeviceDesc)      the device that was prepared, level Basis; once per
       instance: a new device takes a new Instance (a WebGPU adapter gives one)
  device.queue() -> &Queue                queue.submit([cmd, ..]) -> Result<SubmitId>
  queue.write_buffer, write_texture, write_texture_rects, write_texture_rects_at   the browser's
       queue.writeBuffer / writeTexture: copied at the call, before the next submission, from
       the WASM memory itself (no staging ring here)
  device.is_done(id), completed(), poll()  never wait

What the API promises and the browser cannot do, said as errors and not faked: the worker cannot
wait for the GPU (the answer needs its event loop), so wait(id, _) answers at once (false: not done
yet), wait_idle and queue.read_buffer are Unsupported, Buffer::read says NotReady until the
submission has finished.  SPIR-V is Unsupported (ShaderModule::from_wgsl).  Errors the browser finds
(validation, memory) come later than the call that caused them, as its uncaptured errors, and are
returned by the next submit, try_acquire or poll.  Bindless, indirect count, multi-draw, timestamps
and subgroup sizes are not built here (the level Basis; Features says so).

WebGPU's objects belong to this thread: the types of this backend hold Rc, so the compiler keeps
them from the compute workers of the threads bundle (docs/GPU.md "Ausgabe im Browser").
  • struct Instance the browser's WebGPU; adapter and device come asynchronously (prepare)

impl Instance

  • fn new() -> Result<Instance> the browser's WebGPU, adapter and device asked for (prepare says when they are there)

  • fn with(_desc: &InstanceDesc) -> Result<Instance> (app_name and validate mean nothing here: the browser validates every call)

  • fn prepare(&self) -> Result<bool> have the browser's adapter and device arrived? Never waits; a worker gets them by returning to the browser's event loop. (true at once on the other targets.)

  • fn validating(&self) -> bool

  • fn adapters(&self) -> Result<Vec<Adapter>> the one adapter the browser offers

  • fn adapter(&self) -> Result<Adapter>

  • fn adapters_low_power(&self, _display: Option<(u32, u32)>) -> Result<Vec<Adapter>> the adapter for showing windows: not a software one, which is no gain over the CPU path

  • fn low_power_rank(kind: AdapterKind, _drives_display: bool) -> Option<u32> the order of adapters for showing windows (the browser has one; see adapters_low_power)

  • fn validation_messages() -> (u32, u32) nothing counts them here: the browser's console shows what its validation says

  • struct Adapter(Rc<AdapterInner>) the GPU the browser offers

impl Adapter

  • fn info(&self) -> &AdapterInfo

  • fn features(&self) -> &Features

  • fn limits(&self) -> &Limits

  • fn level(&self) -> Level always Basis in the browser

  • fn can_present(&self, _surface: &crate::Surface) -> bool

  • fn create_device_with(&self, desc: &DeviceDesc, _opts: &DeviceOptions) -> Result<Device> (G5d) as create_device: WebGPU has one queue, so a compute queue asked for stays None

  • fn create_device(&self, _desc: &DeviceDesc) -> Result<Device> the device of this adapter, which prepare got (WebGPU gives one per adapter: a second one takes a new Instance)

  • struct Device a logical GPU device: creates resources, records commands, owns the queue

  • struct Queue the device's queue

  • struct ComputeQueue (G5d) a queue of its own for compute: WebGPU has one queue, so Device::compute_queue is None here and there is no value of this type

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) -> &Queue
  • fn compute_queue(&self) -> Option<&ComputeQueue> (G5d) WebGPU has one queue: None
  • fn info(&self) -> &AdapterInfo
  • fn features(&self) -> &Features
  • fn limits(&self) -> &Limits
  • fn level(&self) -> Level
  • fn is_lost(&self) -> bool the device was lost: every call fails with DeviceLost; create a new device
  • fn test_lose(&self) (test aid) the device is destroyed: the browser's own way of losing it
  • fn validating(&self) -> bool
  • fn poll(&self) -> Result<()> what the browser has said since the last look (nothing to free here)
  • fn wait(&self, id: SubmitId, _timeout_ns: u64) -> Result<bool> the browser cannot wait on this thread: at once whether the submission is done
  • fn wait_idle(&self) -> Result<()>
  • fn completed(&self) -> SubmitId
  • fn is_done(&self, id: SubmitId) -> bool the submission id is done on the GPU (the browser's answer comes with its event loop)
  • fn memory_reserved(&self) -> u64 what the buffers and textures asked for (the browser says nothing about its own memory)

impl Queue

  • fn submit<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, cmds: I) -> Result<SubmitId> run command buffers in order -> their number
  • fn submit_after<'a, I: IntoIterator<Item = crate::CommandBuffer<'a>>>(&self, _wait: &[SubmitId], cmds: I) -> Result<SubmitId> (G5d) one queue: its submissions run in order, so this is submit
  • fn flush(&self) -> Result<SubmitId> nothing waits for a submission here
  • fn write_buffer<T: Pod>(&self, buf: &crate::Buffer, offset: u64, data: &[T]) -> Result<()> write data into a buffer at offset, before the next submission
  • 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 (no layouts here)
  • 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 to the texture, empty ones skipped) of mip 0, layer 0 from data, an image of the texture's size in rows of stride bytes (a framebuffer)
  • fn 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 of data go to (x - origin.0, y - origin.1) of the texture; what lies outside the image or the texture is skipped
  • fn read_buffer<T: Pod>(&self, _buf: &crate::Buffer, _offset: u64, _out: &mut [T]) -> Result<()> the worker cannot wait for the GPU: a Readback buffer and Buffer::read

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