Users that want to assign resource ids themselves pass in the id they
want as the `id_in` argument, whereas users that want `wgpu_core`
itself to choose ids always pass `()`. In either case, the id
ultimately assigned is returned as the first element of the tuple.
Producing true identifiers from `id_in` values is the job of an
[`crate::identity::IdentityManager`] or ids will be received from outside through `Option<Id>` arguments.
## Id allocation and streaming
Perhaps surprisingly, allowing users to assign resource ids themselves
enables major performance improvements in some applications.
The `wgpu_core` API is designed foruse by Firefox's [WebGPU]
implementation. For security, web content and GPU use must be kept
segregated in separate processes, with all interaction between them
mediated by an inter-process communication protocol. As web content uses
the WebGPU API, the content process sends messages to the GPU process,
which interacts with the platform's GPU APIs on content's behalf,
occasionally sending results back.
In a classic Rust API, a resource allocation function takes parameters
describing the resource to create, and if creation succeeds, it returns
the resource id in a `Result::Ok` value. However, this design is a poor
fit for the split-process design described above: content must wait for
the reply to its buffer-creation message (say) before it can know which
id it can usein the next message that uses that buffer. On a common
usage pattern, the classic Rust design imposes the latency of a full
cross-process round trip.
We can avoid incurring these round-trip latencies simply by letting the
content process assign resource ids itself. With this approach, content
can choose an id for the new buffer, send a message to create the
buffer, and then immediately send the next message operating on that
buffer, since it already knows its id. Allowing content and GPU process
activity to be pipelined greatly improves throughput.
To help propagate errors correctly in this style of usage, when resource
creation fails, the id supplied for that resource is marked to indicate as much, allowing subsequent operations using that id to be properly
flagged as errors as well.
[`process`]: crate::identity::IdentityManager::process
[`Id<R>`]: crate::id::Id
[wrapped in a mutex]: trait.IdentityHandler.html#impl-IdentityHandler%3CI%3E-for-Mutex%3CIdentityManager%3E
[WebGPU]: https://www.w3.org/TR/webgpu/
#[allow(rustdoc::private_intra_doc_links)] /// All the resources tracked by a [`crate::global::Global`]. /// /// ## Locking /// /// Each field in `Hub` is a [`Registry`] holding all the values of a /// particular type of resource, all protected by a single RwLock. /// So for example, to access any [`Buffer`], you must acquire a read /// lock on the `Hub`s entire buffers registry. The lock guard /// gives you access to the `Registry`'s [`Storage`], which you can /// then index with the buffer's id. (Yes, this design causes /// contention; see [#2272].) /// /// But most `wgpu` operations require access to several different /// kinds of resource, so you often need to hold locks on several /// different fields of your [`Hub`] simultaneously. /// /// Inside the `Registry` there are `Arc<T>` where `T` is a Resource /// Lock of `Registry` happens only when accessing to get the specific resource /// /// [`Storage`]: crate::storage::Storage pubstruct Hub { pub(crate) adapters: Registry<Arc<Adapter>>, pub(crate) devices: Registry<Arc<Device>>, pub(crate) queues: Registry<Arc<Queue>>, pub(crate) pipeline_layouts: Registry<Fallible<PipelineLayout>>, pub(crate) shader_modules: Registry<Fallible<ShaderModule>>, pub(crate) bind_group_layouts: Registry<Fallible<BindGroupLayout>>, pub(crate) bind_groups: Registry<Fallible<BindGroup>>, pub(crate) command_buffers: Registry<Arc<CommandBuffer>>, pub(crate) render_bundles: Registry<Fallible<RenderBundle>>, pub(crate) render_pipelines: Registry<Fallible<RenderPipeline>>, pub(crate) compute_pipelines: Registry<Fallible<ComputePipeline>>, pub(crate) pipeline_caches: Registry<Fallible<PipelineCache>>, pub(crate) query_sets: Registry<Fallible<QuerySet>>, pub(crate) buffers: Registry<Fallible<Buffer>>, pub(crate) staging_buffers: Registry<StagingBuffer>, pub(crate) textures: Registry<Fallible<Texture>>, pub(crate) texture_views: Registry<Fallible<TextureView>>, pub(crate) samplers: Registry<Fallible<Sampler>>, pub(crate) blas_s: Registry<Fallible<Blas>>, pub(crate) tlas_s: Registry<Fallible<Tlas>>,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.