pub trait Device: WasmNotSendSync {
type A: Api;
Show 48 methods
// Required methods
unsafe fn create_buffer(
&self,
desc: &BufferDescriptor<'_>,
) -> Result<<Self::A as Api>::Buffer, DeviceError>;
unsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer);
unsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer);
unsafe fn map_buffer(
&self,
buffer: &<Self::A as Api>::Buffer,
range: MemoryRange,
) -> Result<BufferMapping, DeviceError>;
unsafe fn unmap_buffer(&self, buffer: &<Self::A as Api>::Buffer);
unsafe fn flush_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)
where I: Iterator<Item = MemoryRange>;
unsafe fn invalidate_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)
where I: Iterator<Item = MemoryRange>;
unsafe fn create_texture(
&self,
desc: &TextureDescriptor<'_>,
) -> Result<<Self::A as Api>::Texture, DeviceError>;
unsafe fn destroy_texture(&self, texture: <Self::A as Api>::Texture);
unsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture);
unsafe fn create_texture_view(
&self,
texture: &<Self::A as Api>::Texture,
desc: &TextureViewDescriptor<'_>,
) -> Result<<Self::A as Api>::TextureView, DeviceError>;
unsafe fn destroy_texture_view(&self, view: <Self::A as Api>::TextureView);
unsafe fn create_sampler(
&self,
desc: &SamplerDescriptor<'_>,
) -> Result<<Self::A as Api>::Sampler, DeviceError>;
unsafe fn destroy_sampler(&self, sampler: <Self::A as Api>::Sampler);
unsafe fn create_command_encoder(
&self,
desc: &CommandEncoderDescriptor<'_, <Self::A as Api>::Queue>,
) -> Result<<Self::A as Api>::CommandEncoder, DeviceError>;
unsafe fn create_bind_group_layout(
&self,
desc: &BindGroupLayoutDescriptor<'_>,
) -> Result<<Self::A as Api>::BindGroupLayout, DeviceError>;
unsafe fn destroy_bind_group_layout(
&self,
bg_layout: <Self::A as Api>::BindGroupLayout,
);
unsafe fn create_pipeline_layout(
&self,
desc: &PipelineLayoutDescriptor<'_, <Self::A as Api>::BindGroupLayout>,
) -> Result<<Self::A as Api>::PipelineLayout, DeviceError>;
unsafe fn destroy_pipeline_layout(
&self,
pipeline_layout: <Self::A as Api>::PipelineLayout,
);
unsafe fn create_bind_group(
&self,
desc: &BindGroupDescriptor<'_, <Self::A as Api>::BindGroupLayout, <Self::A as Api>::Buffer, <Self::A as Api>::Sampler, <Self::A as Api>::TextureView, <Self::A as Api>::AccelerationStructure>,
) -> Result<<Self::A as Api>::BindGroup, DeviceError>;
unsafe fn destroy_bind_group(&self, group: <Self::A as Api>::BindGroup);
unsafe fn create_shader_module(
&self,
desc: &ShaderModuleDescriptor<'_>,
shader: ShaderInput<'_>,
) -> Result<<Self::A as Api>::ShaderModule, ShaderError>;
unsafe fn destroy_shader_module(
&self,
module: <Self::A as Api>::ShaderModule,
);
unsafe fn create_render_pipeline(
&self,
desc: &RenderPipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>,
) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>;
unsafe fn create_mesh_pipeline(
&self,
desc: &MeshPipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>,
) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>;
unsafe fn destroy_render_pipeline(
&self,
pipeline: <Self::A as Api>::RenderPipeline,
);
unsafe fn create_compute_pipeline(
&self,
desc: &ComputePipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>,
) -> Result<<Self::A as Api>::ComputePipeline, PipelineError>;
unsafe fn destroy_compute_pipeline(
&self,
pipeline: <Self::A as Api>::ComputePipeline,
);
unsafe fn create_pipeline_cache(
&self,
desc: &PipelineCacheDescriptor<'_>,
) -> Result<<Self::A as Api>::PipelineCache, PipelineCacheError>;
unsafe fn destroy_pipeline_cache(
&self,
cache: <Self::A as Api>::PipelineCache,
);
unsafe fn create_query_set(
&self,
desc: &QuerySetDescriptor<Label<'_>>,
) -> Result<<Self::A as Api>::QuerySet, DeviceError>;
unsafe fn destroy_query_set(&self, set: <Self::A as Api>::QuerySet);
unsafe fn create_fence(
&self,
) -> Result<<Self::A as Api>::Fence, DeviceError>;
unsafe fn destroy_fence(&self, fence: <Self::A as Api>::Fence);
unsafe fn get_fence_value(
&self,
fence: &<Self::A as Api>::Fence,
) -> Result<FenceValue, DeviceError>;
unsafe fn wait(
&self,
fence: &<Self::A as Api>::Fence,
value: FenceValue,
timeout_ms: u32,
) -> Result<bool, DeviceError>;
unsafe fn start_graphics_debugger_capture(&self) -> bool;
unsafe fn stop_graphics_debugger_capture(&self);
unsafe fn create_acceleration_structure(
&self,
desc: &AccelerationStructureDescriptor<'_>,
) -> Result<<Self::A as Api>::AccelerationStructure, DeviceError>;
unsafe fn get_acceleration_structure_build_sizes(
&self,
desc: &GetAccelerationStructureBuildSizesDescriptor<'_, <Self::A as Api>::Buffer>,
) -> AccelerationStructureBuildSizes;
unsafe fn get_acceleration_structure_device_address(
&self,
acceleration_structure: &<Self::A as Api>::AccelerationStructure,
) -> BufferAddress;
unsafe fn destroy_acceleration_structure(
&self,
acceleration_structure: <Self::A as Api>::AccelerationStructure,
);
fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> ⓘ;
fn get_internal_counters(&self) -> HalCounters;
fn check_if_oom(&self) -> Result<(), DeviceError>;
// Provided methods
fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> { ... }
unsafe fn pipeline_cache_get_data(
&self,
cache: &<Self::A as Api>::PipelineCache,
) -> Option<Vec<u8>> { ... }
fn generate_allocator_report(&self) -> Option<AllocatorReport> { ... }
}Expand description
A connection to a GPU and a pool of resources to use with it.
A wgpu-hal Device represents an open connection to a specific graphics
processor, controlled via the backend Device::A. A Device is mostly
used for creating resources. Each Device has an associated Queue used
for command submission.
On Vulkan a Device corresponds to a logical device (VkDevice). Other
backends don’t have an exact analog: for example, ID3D12Devices and
MTLDevices are owned by the backends’ wgpu_hal::Adapter
implementations, and shared by all wgpu_hal::Devices created from that
Adapter.
A Device’s life cycle is generally:
-
Obtain a
Deviceand its associatedQueueby callingAdapter::open.Alternatively, the backend-specific types that implement
Adapteroften have methods for creating awgpu-halDevicefrom a platform-specific handle. For example,vulkan::Adapter::device_from_rawcan create avulkan::Devicefrom anash::Device. -
Create resources to use on the device by calling methods like
Device::create_textureorDevice::create_shader_module. -
Call
Device::create_command_encoderto obtain aCommandEncoder, which you can use to buildCommandBuffers holding commands to be executed on the GPU. -
Call
Queue::submiton theDevice’s associatedQueueto submitCommandBuffers for execution on the GPU. If needed, callDevice::waitto wait for them to finish execution. -
Free resources with methods like
Device::destroy_textureorDevice::destroy_shader_module. -
Drop the device.
§Safety
As with other wgpu-hal APIs, validation is the caller’s
responsibility. Here are the general requirements for all Device
methods:
-
Any resource passed to a
Devicemethod must have been created by thatDevice. For example, aTexturepassed toDevice::destroy_texturemust have been created with theDevicepassed asself. -
Resources may not be destroyed if they are used by any submitted command buffers that have not yet finished execution.
Required Associated Types§
Required Methods§
Sourceunsafe fn create_buffer(
&self,
desc: &BufferDescriptor<'_>,
) -> Result<<Self::A as Api>::Buffer, DeviceError>
unsafe fn create_buffer( &self, desc: &BufferDescriptor<'_>, ) -> Result<<Self::A as Api>::Buffer, DeviceError>
Creates a new buffer.
The initial usage is wgt::BufferUses::empty().
Sourceunsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer)
unsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer)
Free buffer and any GPU resources it owns.
Note that backends are allowed to allocate GPU memory for buffers from
allocation pools, and this call is permitted to simply return buffer’s
storage to that pool, without making it available to other applications.
§Safety
- The given
buffermust not currently be mapped.
Sourceunsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer)
unsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer)
A hook for when a wgpu-core buffer is created from a raw wgpu-hal buffer.
Sourceunsafe fn map_buffer(
&self,
buffer: &<Self::A as Api>::Buffer,
range: MemoryRange,
) -> Result<BufferMapping, DeviceError>
unsafe fn map_buffer( &self, buffer: &<Self::A as Api>::Buffer, range: MemoryRange, ) -> Result<BufferMapping, DeviceError>
Return a pointer to CPU memory mapping the contents of buffer.
Buffer mappings are persistent: the buffer may remain mapped on the CPU
while the GPU reads or writes to it. (Note that wgpu_core does not use
this feature: when a wgpu_core::Buffer is unmapped, the underlying
wgpu_hal buffer is also unmapped.)
If this function returns Ok(mapping), then:
-
mapping.ptris the CPU address of the start of the mapped memory. -
If
mapping.is_coherentistrue, then CPU writes to the mapped memory are immediately visible on the GPU, and vice versa.
§Safety
-
The given
buffermust have been created with theMAP_READorMAP_WRITEflags set inBufferDescriptor::usage. -
The given
rangemust fall within the size ofbuffer. -
The caller must avoid data races between the CPU and the GPU. A data race is any pair of accesses to a particular byte, one of which is a write, that are not ordered with respect to each other by some sort of synchronization operation.
-
If this function returns
Ok(mapping)andmapping.is_coherentisfalse, then:-
Every CPU write to a mapped byte followed by a GPU read of that byte must have at least one call to
Device::flush_mapped_rangescovering that byte that occurs between those two accesses. -
Every GPU write to a mapped byte followed by a CPU read of that byte must have at least one call to
Device::invalidate_mapped_rangescovering that byte that occurs between those two accesses.
Note that the data race rule above requires that all such access pairs be ordered, so it is meaningful to talk about what must occur “between” them.
-
-
Zero-sized mappings are not allowed.
-
The returned
BufferMapping::ptrmust not be used after a call toDevice::unmap_buffer.
Sourceunsafe fn unmap_buffer(&self, buffer: &<Self::A as Api>::Buffer)
unsafe fn unmap_buffer(&self, buffer: &<Self::A as Api>::Buffer)
Remove the mapping established by the last call to Device::map_buffer.
§Safety
- The given
buffermust be currently mapped.
Sourceunsafe fn flush_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)where
I: Iterator<Item = MemoryRange>,
unsafe fn flush_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)where
I: Iterator<Item = MemoryRange>,
Indicate that CPU writes to mapped buffer memory should be made visible to the GPU.
§Safety
-
The given
buffermust be currently mapped. -
All ranges produced by
rangesmust fall withinbuffer’s size.
Sourceunsafe fn invalidate_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)where
I: Iterator<Item = MemoryRange>,
unsafe fn invalidate_mapped_ranges<I>(
&self,
buffer: &<Self::A as Api>::Buffer,
ranges: I,
)where
I: Iterator<Item = MemoryRange>,
Indicate that GPU writes to mapped buffer memory should be made visible to the CPU.
§Safety
-
The given
buffermust be currently mapped. -
All ranges produced by
rangesmust fall withinbuffer’s size.
Sourceunsafe fn create_texture(
&self,
desc: &TextureDescriptor<'_>,
) -> Result<<Self::A as Api>::Texture, DeviceError>
unsafe fn create_texture( &self, desc: &TextureDescriptor<'_>, ) -> Result<<Self::A as Api>::Texture, DeviceError>
Creates a new texture.
The initial usage for all subresources is wgt::TextureUses::UNINITIALIZED.
unsafe fn destroy_texture(&self, texture: <Self::A as Api>::Texture)
Sourceunsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture)
unsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture)
A hook for when a wgpu-core texture is created from a raw wgpu-hal texture.
unsafe fn create_texture_view( &self, texture: &<Self::A as Api>::Texture, desc: &TextureViewDescriptor<'_>, ) -> Result<<Self::A as Api>::TextureView, DeviceError>
unsafe fn destroy_texture_view(&self, view: <Self::A as Api>::TextureView)
unsafe fn create_sampler( &self, desc: &SamplerDescriptor<'_>, ) -> Result<<Self::A as Api>::Sampler, DeviceError>
unsafe fn destroy_sampler(&self, sampler: <Self::A as Api>::Sampler)
Sourceunsafe fn create_command_encoder(
&self,
desc: &CommandEncoderDescriptor<'_, <Self::A as Api>::Queue>,
) -> Result<<Self::A as Api>::CommandEncoder, DeviceError>
unsafe fn create_command_encoder( &self, desc: &CommandEncoderDescriptor<'_, <Self::A as Api>::Queue>, ) -> Result<<Self::A as Api>::CommandEncoder, DeviceError>
Create a fresh CommandEncoder.
The new CommandEncoder is in the “closed” state.
Sourceunsafe fn create_bind_group_layout(
&self,
desc: &BindGroupLayoutDescriptor<'_>,
) -> Result<<Self::A as Api>::BindGroupLayout, DeviceError>
unsafe fn create_bind_group_layout( &self, desc: &BindGroupLayoutDescriptor<'_>, ) -> Result<<Self::A as Api>::BindGroupLayout, DeviceError>
Creates a bind group layout.
unsafe fn destroy_bind_group_layout( &self, bg_layout: <Self::A as Api>::BindGroupLayout, )
unsafe fn create_pipeline_layout( &self, desc: &PipelineLayoutDescriptor<'_, <Self::A as Api>::BindGroupLayout>, ) -> Result<<Self::A as Api>::PipelineLayout, DeviceError>
unsafe fn destroy_pipeline_layout( &self, pipeline_layout: <Self::A as Api>::PipelineLayout, )
unsafe fn create_bind_group( &self, desc: &BindGroupDescriptor<'_, <Self::A as Api>::BindGroupLayout, <Self::A as Api>::Buffer, <Self::A as Api>::Sampler, <Self::A as Api>::TextureView, <Self::A as Api>::AccelerationStructure>, ) -> Result<<Self::A as Api>::BindGroup, DeviceError>
unsafe fn destroy_bind_group(&self, group: <Self::A as Api>::BindGroup)
unsafe fn create_shader_module( &self, desc: &ShaderModuleDescriptor<'_>, shader: ShaderInput<'_>, ) -> Result<<Self::A as Api>::ShaderModule, ShaderError>
unsafe fn destroy_shader_module(&self, module: <Self::A as Api>::ShaderModule)
unsafe fn create_render_pipeline( &self, desc: &RenderPipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>, ) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>
unsafe fn create_mesh_pipeline( &self, desc: &MeshPipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>, ) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>
unsafe fn destroy_render_pipeline( &self, pipeline: <Self::A as Api>::RenderPipeline, )
unsafe fn create_compute_pipeline( &self, desc: &ComputePipelineDescriptor<'_, <Self::A as Api>::PipelineLayout, <Self::A as Api>::ShaderModule, <Self::A as Api>::PipelineCache>, ) -> Result<<Self::A as Api>::ComputePipeline, PipelineError>
unsafe fn destroy_compute_pipeline( &self, pipeline: <Self::A as Api>::ComputePipeline, )
unsafe fn create_pipeline_cache( &self, desc: &PipelineCacheDescriptor<'_>, ) -> Result<<Self::A as Api>::PipelineCache, PipelineCacheError>
unsafe fn destroy_pipeline_cache(&self, cache: <Self::A as Api>::PipelineCache)
unsafe fn create_query_set( &self, desc: &QuerySetDescriptor<Label<'_>>, ) -> Result<<Self::A as Api>::QuerySet, DeviceError>
unsafe fn destroy_query_set(&self, set: <Self::A as Api>::QuerySet)
unsafe fn create_fence(&self) -> Result<<Self::A as Api>::Fence, DeviceError>
unsafe fn destroy_fence(&self, fence: <Self::A as Api>::Fence)
unsafe fn get_fence_value( &self, fence: &<Self::A as Api>::Fence, ) -> Result<FenceValue, DeviceError>
Sourceunsafe fn wait(
&self,
fence: &<Self::A as Api>::Fence,
value: FenceValue,
timeout_ms: u32,
) -> Result<bool, DeviceError>
unsafe fn wait( &self, fence: &<Self::A as Api>::Fence, value: FenceValue, timeout_ms: u32, ) -> Result<bool, DeviceError>
Wait for fence to reach value.
Operations like Queue::submit can accept a Fence and a
FenceValue to store in it, so you can use this wait function
to wait for a given queue submission to finish execution.
The value argument must be a value that some actual operation you have
already presented to the device is going to store in fence. You cannot
wait for values yet to be submitted. (This restriction accommodates
implementations like the vulkan backend’s FencePool that must
allocate a distinct synchronization object for each fence value one is
able to wait for.)
Calling wait with a lower FenceValue than fence’s current value
returns immediately.
Returns Ok(true) on success and Ok(false) on timeout.
Sourceunsafe fn start_graphics_debugger_capture(&self) -> bool
unsafe fn start_graphics_debugger_capture(&self) -> bool
Start a graphics debugger capture.
§Safety
See wgpu::Device::start_graphics_debugger_capture for more details.
Sourceunsafe fn stop_graphics_debugger_capture(&self)
unsafe fn stop_graphics_debugger_capture(&self)
Stop a graphics debugger capture.
§Safety
See wgpu::Device::stop_graphics_debugger_capture for more details.
unsafe fn create_acceleration_structure( &self, desc: &AccelerationStructureDescriptor<'_>, ) -> Result<<Self::A as Api>::AccelerationStructure, DeviceError>
unsafe fn get_acceleration_structure_build_sizes( &self, desc: &GetAccelerationStructureBuildSizesDescriptor<'_, <Self::A as Api>::Buffer>, ) -> AccelerationStructureBuildSizes
unsafe fn get_acceleration_structure_device_address( &self, acceleration_structure: &<Self::A as Api>::AccelerationStructure, ) -> BufferAddress
unsafe fn destroy_acceleration_structure( &self, acceleration_structure: <Self::A as Api>::AccelerationStructure, )
fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> ⓘ
fn get_internal_counters(&self) -> HalCounters
fn check_if_oom(&self) -> Result<(), DeviceError>
Provided Methods§
fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]>
unsafe fn pipeline_cache_get_data( &self, cache: &<Self::A as Api>::PipelineCache, ) -> Option<Vec<u8>>
fn generate_allocator_report(&self) -> Option<AllocatorReport>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.