Struct wgpu_core::hub::Hub

source ·
pub struct Hub<A: HalApi, F: GlobalIdentityHandlerFactory> {
Show 16 fields pub adapters: Registry<Adapter<A>, AdapterId, F>, pub devices: Registry<Device<A>, DeviceId, F>, pub pipeline_layouts: Registry<PipelineLayout<A>, PipelineLayoutId, F>, pub shader_modules: Registry<ShaderModule<A>, ShaderModuleId, F>, pub bind_group_layouts: Registry<BindGroupLayout<A>, BindGroupLayoutId, F>, pub bind_groups: Registry<BindGroup<A>, BindGroupId, F>, pub command_buffers: Registry<CommandBuffer<A>, CommandBufferId, F>, pub render_bundles: Registry<RenderBundle<A>, RenderBundleId, F>, pub render_pipelines: Registry<RenderPipeline<A>, RenderPipelineId, F>, pub compute_pipelines: Registry<ComputePipeline<A>, ComputePipelineId, F>, pub query_sets: Registry<QuerySet<A>, QuerySetId, F>, pub buffers: Registry<Buffer<A>, BufferId, F>, pub staging_buffers: Registry<StagingBuffer<A>, StagingBufferId, F>, pub textures: Registry<Texture<A>, TextureId, F>, pub texture_views: Registry<TextureView<A>, TextureViewId, F>, pub samplers: Registry<Sampler<A>, SamplerId, F>,
}
Expand description

All the resources for a particular backend in a Global.

To obtain global’s Hub for some HalApi backend type A, call A::hub(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 Hubs 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. To avoid deadlock, there is an ordering imposed on the fields, and you may only acquire new locks on fields that come after all those you are already holding locks on, in this ordering. (The ordering is described in the documentation for the Access trait.)

We use Rust’s type system to statically check that wgpu_core can only ever acquire locks in the correct order:

  • A value of type Token<T> represents proof that the owner only holds locks on the Hub fields holding resources of type T or earlier in the lock ordering. A special value of type Token<Root>, obtained by calling [Token::root], represents proof that no Hub field locks are held.

  • To lock the Hub field holding resources of type T, you must call its read or write methods. These require you to pass in a &mut Token<A>, for some A that implements Access<T>. This implementation exists only if T follows A in the field ordering, which statically ensures that you are indeed allowed to lock this new Hub field.

  • The locking methods return both an RwLock guard that you can use to access the field’s resources, and a new Token<T> value. These both borrow from the lifetime of your Token<A>, so since you passed that by mutable reference, you cannot access it again until you drop the new token and lock guard.

Because a thread only ever has access to the Token<T> for the last resource type T it holds a lock for, and the Access trait implementations only permit acquiring locks for types U that follow T in the lock ordering, it is statically impossible for a program to violate the locking order.

This does assume that threads cannot call Token<Root> when they already hold locks (dynamically enforced in debug builds) and that threads cannot send their Tokens to other threads (enforced by making Token neither Send nor Sync).

Fields§

§adapters: Registry<Adapter<A>, AdapterId, F>§devices: Registry<Device<A>, DeviceId, F>§pipeline_layouts: Registry<PipelineLayout<A>, PipelineLayoutId, F>§shader_modules: Registry<ShaderModule<A>, ShaderModuleId, F>§bind_group_layouts: Registry<BindGroupLayout<A>, BindGroupLayoutId, F>§bind_groups: Registry<BindGroup<A>, BindGroupId, F>§command_buffers: Registry<CommandBuffer<A>, CommandBufferId, F>§render_bundles: Registry<RenderBundle<A>, RenderBundleId, F>§render_pipelines: Registry<RenderPipeline<A>, RenderPipelineId, F>§compute_pipelines: Registry<ComputePipeline<A>, ComputePipelineId, F>§query_sets: Registry<QuerySet<A>, QuerySetId, F>§buffers: Registry<Buffer<A>, BufferId, F>§staging_buffers: Registry<StagingBuffer<A>, StagingBufferId, F>§textures: Registry<Texture<A>, TextureId, F>§texture_views: Registry<TextureView<A>, TextureViewId, F>§samplers: Registry<Sampler<A>, SamplerId, F>

Implementations§

Auto Trait Implementations§

§

impl<A, F> !RefUnwindSafe for Hub<A, F>

§

impl<A, F> Send for Hub<A, F>where A: Send, <F as IdentityHandlerFactory<Id<CommandBuffer<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<RenderPipeline<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<Texture<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<TextureView<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<Sampler<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<QuerySet<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<Device<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<RenderBundle<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<Adapter<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<Buffer<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<StagingBuffer<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<BindGroupLayout<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<PipelineLayout<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<BindGroup<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<ShaderModule<Api>>>>::Filter: Send, <F as IdentityHandlerFactory<Id<ComputePipeline<Api>>>>::Filter: Send,

§

impl<A, F> Sync for Hub<A, F>where A: Send + Sync, <F as IdentityHandlerFactory<Id<CommandBuffer<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<RenderPipeline<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<Texture<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<TextureView<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<Sampler<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<QuerySet<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<Device<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<RenderBundle<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<Adapter<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<Buffer<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<StagingBuffer<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<BindGroupLayout<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<PipelineLayout<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<BindGroup<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<ShaderModule<Api>>>>::Filter: Sync, <F as IdentityHandlerFactory<Id<ComputePipeline<Api>>>>::Filter: Sync,

§

impl<A, F> Unpin for Hub<A, F>where A: Unpin, <A as Api>::Adapter: Unpin, <A as Api>::BindGroup: Unpin, <A as Api>::BindGroupLayout: Unpin, <A as Api>::Buffer: Unpin, <A as Api>::CommandBuffer: Unpin, <A as Api>::CommandEncoder: Unpin, <A as Api>::ComputePipeline: Unpin, <A as Api>::Device: Unpin, <A as Api>::Fence: Unpin, <F as IdentityHandlerFactory<Id<CommandBuffer<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<RenderPipeline<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<Texture<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<TextureView<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<Sampler<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<QuerySet<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<Device<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<RenderBundle<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<Adapter<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<Buffer<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<StagingBuffer<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<BindGroupLayout<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<PipelineLayout<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<BindGroup<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<ShaderModule<Api>>>>::Filter: Unpin, <F as IdentityHandlerFactory<Id<ComputePipeline<Api>>>>::Filter: Unpin, <A as Api>::PipelineLayout: Unpin, <A as Api>::QuerySet: Unpin, <A as Api>::Queue: Unpin, <A as Api>::RenderPipeline: Unpin, <A as Api>::Sampler: Unpin, <A as Api>::ShaderModule: Unpin, <A as Api>::SurfaceTexture: Unpin, <A as Api>::Texture: Unpin, <A as Api>::TextureView: Unpin,

§

impl<A, F> !UnwindSafe for Hub<A, F>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.