mod allocator;
mod bind;
mod bundle;
mod clear;
mod compute;
mod compute_command;
mod draw;
mod encoder;
mod encoder_command;
pub mod ffi;
mod memory_init;
mod pass;
mod query;
mod ray_tracing;
mod render;
mod render_command;
mod timestamp_writes;
mod transfer;
mod transition_resources;
use alloc::{borrow::ToOwned as _, boxed::Box, string::String, sync::Arc, vec::Vec};
use core::convert::Infallible;
use core::mem::{self, ManuallyDrop};
use core::{ops, panic};
#[cfg(feature = "serde")]
pub(crate) use self::encoder_command::serde_object_reference_struct;
#[cfg(any(feature = "trace", feature = "replay"))]
#[doc(hidden)]
pub use self::encoder_command::PointerReferences;
pub use self::{
bundle::{
bundle_ffi, CreateRenderBundleError, ExecutionError, RenderBundle, RenderBundleDescriptor,
RenderBundleEncoder, RenderBundleEncoderDescriptor, RenderBundleError,
RenderBundleErrorInner,
},
clear::ClearError,
compute::{
ComputeBasePass, ComputePass, ComputePassDescriptor, ComputePassError,
ComputePassErrorInner, DispatchError,
},
compute_command::ArcComputeCommand,
draw::{DrawError, Rect, RenderCommandError},
encoder_command::{ArcCommand, ArcReferences, Command, IdReferences, ReferenceType},
query::{QueryError, QueryUseError, ResolveError, SimplifiedQueryType},
render::{
ArcRenderPassColorAttachment, AttachmentError, AttachmentErrorLocation,
ColorAttachmentError, ColorAttachments, LoadOp, PassChannel, RenderBasePass, RenderPass,
RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor,
RenderPassError, RenderPassErrorInner, ResolvedPassChannel,
ResolvedRenderPassDepthStencilAttachment, StoreOp,
},
render_command::ArcRenderCommand,
transfer::{CopySide, TransferError},
transition_resources::TransitionResourcesError,
};
pub(crate) use self::{
clear::clear_texture,
encoder::EncodingState,
memory_init::CommandBufferTextureMemoryActions,
render::{get_stride_of_indirect_args, VertexLimits},
transfer::{
extract_texture_selector, validate_linear_texture_data, validate_texture_buffer_copy,
validate_texture_copy_dst_format, validate_texture_copy_range,
},
};
pub(crate) use allocator::CommandAllocator;
pub use self::{compute_command::ComputeCommand, render_command::RenderCommand};
pub(crate) use timestamp_writes::ArcPassTimestampWrites;
pub use timestamp_writes::PassTimestampWrites;
use crate::binding_model::BindingError;
use crate::device::queue::TempResource;
use crate::device::{Device, DeviceError, MissingFeatures};
use crate::id::Id;
use crate::lock::{rank, Mutex};
use crate::snatch::SnatchGuard;
use crate::init_tracker::BufferInitTrackerAction;
use crate::ray_tracing::{AsAction, BuildAccelerationStructureError};
use crate::resource::{
DestroyedResourceError, Fallible, InvalidResourceError, Labeled, ParentDevice as _, QuerySet,
};
use crate::storage::Storage;
use crate::track::{DeviceTracker, ResourceUsageCompatibilityError, Tracker, UsageScope};
use crate::{api_log, global::Global, id, resource_log, Label};
use crate::{hal_label, LabelHelpers};
use wgt::error::{ErrorType, WebGpuError};
use thiserror::Error;
pub type TexelCopyBufferInfo = ffi::TexelCopyBufferInfo;
pub type TexelCopyTextureInfo = ffi::TexelCopyTextureInfo;
pub type CopyExternalImageDestInfo = ffi::CopyExternalImageDestInfo;
const IMMEDIATES_CLEAR_ARRAY: &[u32] = &[0_u32; 64];
pub(crate) struct EncoderErrorState {
error: CommandEncoderError,
#[cfg(feature = "trace")]
trace_commands: Option<Vec<Command<PointerReferences>>>,
}
fn make_error_state<E: Into<CommandEncoderError>>(error: E) -> CommandEncoderStatus {
CommandEncoderStatus::Error(EncoderErrorState {
error: error.into(),
#[cfg(feature = "trace")]
trace_commands: None,
})
}
pub(crate) enum CommandEncoderStatus {
Recording(CommandBufferMutable),
Locked(CommandBufferMutable),
Consumed,
Finished(CommandBufferMutable),
Error(EncoderErrorState),
Transitioning,
}
impl CommandEncoderStatus {
#[doc(hidden)]
fn replay(&mut self, commands: Vec<Command<ArcReferences>>) {
let Self::Recording(cmd_buf_data) = self else {
panic!("encoder should be in the recording state");
};
cmd_buf_data.commands.extend(commands);
}
fn push_with<F: FnOnce() -> Result<ArcCommand, E>, E: Clone + Into<CommandEncoderError>>(
&mut self,
f: F,
) -> Result<(), EncoderStateError> {
match self {
Self::Recording(cmd_buf_data) => {
cmd_buf_data.encoder.api.set(EncodingApi::Wgpu);
match f() {
Ok(cmd) => cmd_buf_data.commands.push(cmd),
Err(err) => {
self.invalidate(err);
}
}
Ok(())
}
Self::Locked(_) => {
self.invalidate(EncoderStateError::Locked);
Ok(())
}
Self::Finished(_) => Err(self.invalidate(EncoderStateError::Ended)),
Self::Consumed => Err(EncoderStateError::Ended),
Self::Error(_) => Ok(()),
Self::Transitioning => unreachable!(),
}
}
fn with_buffer<
F: FnOnce(&mut CommandBufferMutable) -> Result<(), E>,
E: Clone + Into<CommandEncoderError>,
>(
&mut self,
api: EncodingApi,
f: F,
) -> Result<(), EncoderStateError> {
match self {
Self::Recording(inner) => {
inner.encoder.api.set(api);
RecordingGuard { inner: self }.record(f);
Ok(())
}
Self::Locked(_) => {
self.invalidate(EncoderStateError::Locked);
Ok(())
}
Self::Finished(_) => Err(self.invalidate(EncoderStateError::Ended)),
Self::Consumed => Err(EncoderStateError::Ended),
Self::Error(_) => Ok(()),
Self::Transitioning => unreachable!(),
}
}
pub(crate) fn record_as_hal_mut<T, F: FnOnce(Option<&mut CommandBufferMutable>) -> T>(
&mut self,
f: F,
) -> T {
match self {
Self::Recording(inner) => {
inner.encoder.api.set(EncodingApi::Raw);
RecordingGuard { inner: self }.record_as_hal_mut(f)
}
Self::Locked(_) => {
self.invalidate(EncoderStateError::Locked);
f(None)
}
Self::Finished(_) => {
self.invalidate(EncoderStateError::Ended);
f(None)
}
Self::Consumed => f(None),
Self::Error(_) => f(None),
Self::Transitioning => unreachable!(),
}
}
fn lock_encoder(&mut self) -> Result<(), EncoderStateError> {
match mem::replace(self, Self::Transitioning) {
Self::Recording(inner) => {
*self = Self::Locked(inner);
Ok(())
}
st @ Self::Finished(_) => {
*self = st;
Err(EncoderStateError::Ended)
}
Self::Locked(_) => Err(self.invalidate(EncoderStateError::Locked)),
st @ Self::Consumed => {
*self = st;
Err(EncoderStateError::Ended)
}
st @ Self::Error(_) => {
*self = st;
Err(EncoderStateError::Invalid)
}
Self::Transitioning => unreachable!(),
}
}
fn unlock_encoder(&mut self) -> Result<(), EncoderStateError> {
match mem::replace(self, Self::Transitioning) {
Self::Locked(inner) => {
*self = Self::Recording(inner);
Ok(())
}
st @ Self::Finished(_) => {
*self = st;
Err(EncoderStateError::Ended)
}
Self::Recording(_) => {
*self = make_error_state(EncoderStateError::Unlocked);
Err(EncoderStateError::Unlocked)
}
st @ Self::Consumed => {
*self = st;
Err(EncoderStateError::Ended)
}
st @ Self::Error(_) => {
*self = st;
Ok(())
}
Self::Transitioning => unreachable!(),
}
}
fn finish(&mut self) -> Self {
match mem::replace(self, Self::Consumed) {
Self::Recording(inner) => {
if inner.encoder.api != EncodingApi::Raw {
assert!(!inner.encoder.is_open);
}
Self::Finished(inner)
}
Self::Consumed | Self::Finished(_) => make_error_state(EncoderStateError::Ended),
Self::Locked(_) => make_error_state(EncoderStateError::Locked),
st @ Self::Error(_) => st,
Self::Transitioning => unreachable!(),
}
}
fn invalidate<E: Clone + Into<CommandEncoderError>>(&mut self, err: E) -> E {
#[cfg(feature = "trace")]
let trace_commands = match self {
Self::Recording(cmd_buf_data) => Some(
mem::take(&mut cmd_buf_data.commands)
.into_iter()
.map(crate::device::trace::IntoTrace::into_trace)
.collect(),
),
_ => None,
};
let enc_err = err.clone().into();
api_log!("Invalidating command encoder: {enc_err:?}");
*self = Self::Error(EncoderErrorState {
error: enc_err,
#[cfg(feature = "trace")]
trace_commands,
});
err
}
}
pub(crate) struct RecordingGuard<'a> {
inner: &'a mut CommandEncoderStatus,
}
impl<'a> RecordingGuard<'a> {
pub(crate) fn mark_successful(self) {
mem::forget(self)
}
fn record<
F: FnOnce(&mut CommandBufferMutable) -> Result<(), E>,
E: Clone + Into<CommandEncoderError>,
>(
mut self,
f: F,
) {
match f(&mut self) {
Ok(()) => self.mark_successful(),
Err(err) => {
self.inner.invalidate(err);
}
}
}
pub(crate) fn record_as_hal_mut<T, F: FnOnce(Option<&mut CommandBufferMutable>) -> T>(
mut self,
f: F,
) -> T {
let res = f(Some(&mut self));
self.mark_successful();
res
}
}
impl<'a> Drop for RecordingGuard<'a> {
fn drop(&mut self) {
if matches!(*self.inner, CommandEncoderStatus::Error(_)) {
return;
}
self.inner.invalidate(EncoderStateError::Invalid);
}
}
impl<'a> ops::Deref for RecordingGuard<'a> {
type Target = CommandBufferMutable;
fn deref(&self) -> &Self::Target {
match &*self.inner {
CommandEncoderStatus::Recording(command_buffer_mutable) => command_buffer_mutable,
_ => unreachable!(),
}
}
}
impl<'a> ops::DerefMut for RecordingGuard<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self.inner {
CommandEncoderStatus::Recording(command_buffer_mutable) => command_buffer_mutable,
_ => unreachable!(),
}
}
}
pub(crate) struct CommandEncoder {
pub(crate) device: Arc<Device>,
pub(crate) label: String,
pub(crate) data: Mutex<CommandEncoderStatus>,
}
crate::impl_resource_type!(CommandEncoder);
crate::impl_labeled!(CommandEncoder);
crate::impl_parent_device!(CommandEncoder);
crate::impl_storage_item!(CommandEncoder);
impl Drop for CommandEncoder {
fn drop(&mut self) {
resource_log!("Drop {}", self.error_ident());
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum EncodingApi {
Wgpu,
Raw,
Undecided,
InternalUse,
}
impl EncodingApi {
pub(crate) fn set(&mut self, api: EncodingApi) {
match *self {
EncodingApi::Undecided => {
*self = api;
}
self_api if self_api != api => {
panic!("Mixing the wgpu encoding API with the raw encoding API is not permitted");
}
_ => {}
}
}
}
pub(crate) struct InnerCommandEncoder {
pub(crate) raw: ManuallyDrop<Box<dyn hal::DynCommandEncoder>>,
pub(crate) list: Vec<Box<dyn hal::DynCommandBuffer>>,
pub(crate) device: Arc<Device>,
pub(crate) is_open: bool,
pub(crate) api: EncodingApi,
pub(crate) label: String,
}
impl InnerCommandEncoder {
fn close_and_swap(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;
let new =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.insert(self.list.len() - 1, new);
Ok(())
}
pub(crate) fn close_and_push_front(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;
let new =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.insert(0, new);
Ok(())
}
pub(crate) fn close(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;
let cmd_buf =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.push(cmd_buf);
Ok(())
}
fn close_if_open(&mut self) -> Result<(), DeviceError> {
if self.is_open {
self.is_open = false;
let cmd_buf =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.push(cmd_buf);
}
Ok(())
}
fn open_if_closed(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
if !self.is_open {
let hal_label = hal_label(Some(self.label.as_str()), self.device.instance_flags);
unsafe { self.raw.begin_encoding(hal_label) }
.map_err(|e| self.device.handle_hal_error(e))?;
self.is_open = true;
}
Ok(self.raw.as_mut())
}
pub(crate) fn open(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
if !self.is_open {
let hal_label = hal_label(Some(self.label.as_str()), self.device.instance_flags);
unsafe { self.raw.begin_encoding(hal_label) }
.map_err(|e| self.device.handle_hal_error(e))?;
self.is_open = true;
}
Ok(self.raw.as_mut())
}
pub(crate) fn open_pass(
&mut self,
label: Option<&str>,
) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
assert!(!self.is_open);
let hal_label = hal_label(label, self.device.instance_flags);
unsafe { self.raw.begin_encoding(hal_label) }
.map_err(|e| self.device.handle_hal_error(e))?;
self.is_open = true;
Ok(self.raw.as_mut())
}
}
impl Drop for InnerCommandEncoder {
fn drop(&mut self) {
if self.is_open {
unsafe { self.raw.discard_encoding() };
}
unsafe {
self.raw.reset_all(mem::take(&mut self.list));
}
let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
self.device.command_allocator.release_encoder(raw);
}
}
pub(crate) struct BakedCommands {
pub(crate) encoder: InnerCommandEncoder,
pub(crate) trackers: Tracker,
pub(crate) temp_resources: Vec<TempResource>,
pub(crate) indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
texture_memory_actions: CommandBufferTextureMemoryActions,
}
pub struct CommandBufferMutable {
pub(crate) encoder: InnerCommandEncoder,
pub(crate) trackers: Tracker,
buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
texture_memory_actions: CommandBufferTextureMemoryActions,
as_actions: Vec<AsAction>,
temp_resources: Vec<TempResource>,
indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
pub(crate) commands: Vec<Command<ArcReferences>>,
#[cfg(feature = "trace")]
pub(crate) trace_commands: Option<Vec<Command<PointerReferences>>>,
}
impl CommandBufferMutable {
pub(crate) fn into_baked_commands(self) -> BakedCommands {
BakedCommands {
encoder: self.encoder,
trackers: self.trackers,
temp_resources: self.temp_resources,
indirect_draw_validation_resources: self.indirect_draw_validation_resources,
buffer_memory_init_actions: self.buffer_memory_init_actions,
texture_memory_actions: self.texture_memory_actions,
}
}
}
pub struct CommandBuffer {
pub(crate) device: Arc<Device>,
label: String,
pub(crate) data: Mutex<CommandEncoderStatus>,
}
impl Drop for CommandBuffer {
fn drop(&mut self) {
resource_log!("Drop {}", self.error_ident());
}
}
impl CommandEncoder {
pub(crate) fn new(
encoder: Box<dyn hal::DynCommandEncoder>,
device: &Arc<Device>,
label: &Label,
) -> Self {
CommandEncoder {
device: device.clone(),
label: label.to_string(),
data: Mutex::new(
rank::COMMAND_BUFFER_DATA,
CommandEncoderStatus::Recording(CommandBufferMutable {
encoder: InnerCommandEncoder {
raw: ManuallyDrop::new(encoder),
list: Vec::new(),
device: device.clone(),
is_open: false,
api: EncodingApi::Undecided,
label: label.to_string(),
},
trackers: Tracker::new(
device.ordered_buffer_usages,
device.ordered_texture_usages,
),
buffer_memory_init_actions: Default::default(),
texture_memory_actions: Default::default(),
as_actions: Default::default(),
temp_resources: Default::default(),
indirect_draw_validation_resources:
crate::indirect_validation::DrawResources::new(device.clone()),
commands: Vec::new(),
#[cfg(feature = "trace")]
trace_commands: if device.trace.lock().is_some() {
Some(Vec::new())
} else {
None
},
}),
),
}
}
pub(crate) fn new_invalid(
device: &Arc<Device>,
label: &Label,
err: CommandEncoderError,
) -> Self {
CommandEncoder {
device: device.clone(),
label: label.to_string(),
data: Mutex::new(rank::COMMAND_BUFFER_DATA, make_error_state(err)),
}
}
pub(crate) fn insert_barriers_from_tracker(
raw: &mut dyn hal::DynCommandEncoder,
base: &mut Tracker,
head: &Tracker,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("insert_barriers");
base.buffers.set_from_tracker(&head.buffers);
base.textures.set_from_tracker(&head.textures);
Self::drain_barriers(raw, base, snatch_guard);
}
pub(crate) fn insert_barriers_from_scope(
raw: &mut dyn hal::DynCommandEncoder,
base: &mut Tracker,
head: &UsageScope,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("insert_barriers");
base.buffers.set_from_usage_scope(&head.buffers);
base.textures.set_from_usage_scope(&head.textures);
Self::drain_barriers(raw, base, snatch_guard);
}
pub(crate) fn drain_barriers(
raw: &mut dyn hal::DynCommandEncoder,
base: &mut Tracker,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("drain_barriers");
let buffer_barriers = base
.buffers
.drain_transitions(snatch_guard)
.collect::<Vec<_>>();
let (transitions, textures) = base.textures.drain_transitions(snatch_guard);
let texture_barriers = transitions
.into_iter()
.enumerate()
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw()))
.collect::<Vec<_>>();
unsafe {
raw.transition_buffers(&buffer_barriers);
raw.transition_textures(&texture_barriers);
}
}
pub(crate) fn insert_barriers_from_device_tracker(
raw: &mut dyn hal::DynCommandEncoder,
base: &mut DeviceTracker,
head: &Tracker,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("insert_barriers_from_device_tracker");
let buffer_barriers = base
.buffers
.set_from_tracker_and_drain_transitions(&head.buffers, snatch_guard)
.collect::<Vec<_>>();
let texture_barriers = base
.textures
.set_from_tracker_and_drain_transitions(&head.textures, snatch_guard)
.collect::<Vec<_>>();
unsafe {
raw.transition_buffers(&buffer_barriers);
raw.transition_textures(&texture_barriers);
}
}
fn encode_commands(
device: &Arc<Device>,
cmd_buf_data: &mut CommandBufferMutable,
) -> Result<(), CommandEncoderError> {
device.check_is_valid()?;
let snatch_guard = device.snatchable_lock.read();
let mut debug_scope_depth = 0;
if cmd_buf_data.encoder.api == EncodingApi::Raw {
assert!(cmd_buf_data.commands.is_empty());
}
let commands = mem::take(&mut cmd_buf_data.commands);
#[cfg(feature = "trace")]
if device.trace.lock().is_some() {
cmd_buf_data.trace_commands = Some(
commands
.iter()
.map(crate::device::trace::IntoTrace::to_trace)
.collect(),
);
}
for command in commands {
if matches!(
command,
ArcCommand::RunRenderPass { .. } | ArcCommand::RunComputePass { .. }
) {
let mut state = EncodingState {
device,
raw_encoder: &mut cmd_buf_data.encoder,
tracker: &mut cmd_buf_data.trackers,
buffer_memory_init_actions: &mut cmd_buf_data.buffer_memory_init_actions,
texture_memory_actions: &mut cmd_buf_data.texture_memory_actions,
as_actions: &mut cmd_buf_data.as_actions,
temp_resources: &mut cmd_buf_data.temp_resources,
indirect_draw_validation_resources: &mut cmd_buf_data
.indirect_draw_validation_resources,
snatch_guard: &snatch_guard,
debug_scope_depth: &mut debug_scope_depth,
};
match command {
ArcCommand::RunRenderPass {
pass,
color_attachments,
depth_stencil_attachment,
timestamp_writes,
occlusion_query_set,
multiview_mask,
} => {
api_log!(
"Begin encoding render pass with '{}' label",
pass.label.as_deref().unwrap_or("")
);
let res = render::encode_render_pass(
&mut state,
pass,
color_attachments,
depth_stencil_attachment,
timestamp_writes,
occlusion_query_set,
multiview_mask,
);
match res.as_ref() {
Err(err) => {
api_log!("Finished encoding render pass ({err:?})")
}
Ok(_) => {
api_log!("Finished encoding render pass (success)")
}
}
res?;
}
ArcCommand::RunComputePass {
pass,
timestamp_writes,
} => {
api_log!(
"Begin encoding compute pass with '{}' label",
pass.label.as_deref().unwrap_or("")
);
let res = compute::encode_compute_pass(&mut state, pass, timestamp_writes);
match res.as_ref() {
Err(err) => {
api_log!("Finished encoding compute pass ({err:?})")
}
Ok(_) => {
api_log!("Finished encoding compute pass (success)")
}
}
res?;
}
_ => unreachable!(),
}
} else {
let raw_encoder = cmd_buf_data.encoder.open_if_closed()?;
let mut state = EncodingState {
device,
raw_encoder,
tracker: &mut cmd_buf_data.trackers,
buffer_memory_init_actions: &mut cmd_buf_data.buffer_memory_init_actions,
texture_memory_actions: &mut cmd_buf_data.texture_memory_actions,
as_actions: &mut cmd_buf_data.as_actions,
temp_resources: &mut cmd_buf_data.temp_resources,
indirect_draw_validation_resources: &mut cmd_buf_data
.indirect_draw_validation_resources,
snatch_guard: &snatch_guard,
debug_scope_depth: &mut debug_scope_depth,
};
match command {
ArcCommand::CopyBufferToBuffer {
src,
src_offset,
dst,
dst_offset,
size,
} => {
transfer::copy_buffer_to_buffer(
&mut state, &src, src_offset, &dst, dst_offset, size,
)?;
}
ArcCommand::CopyBufferToTexture { src, dst, size } => {
transfer::copy_buffer_to_texture(&mut state, &src, &dst, &size)?;
}
ArcCommand::CopyTextureToBuffer { src, dst, size } => {
transfer::copy_texture_to_buffer(&mut state, &src, &dst, &size)?;
}
ArcCommand::CopyTextureToTexture { src, dst, size } => {
transfer::copy_texture_to_texture(&mut state, &src, &dst, &size)?;
}
ArcCommand::ClearBuffer { dst, offset, size } => {
clear::clear_buffer(&mut state, dst, offset, size)?;
}
ArcCommand::ClearTexture {
dst,
subresource_range,
} => {
clear::clear_texture_cmd(&mut state, dst, &subresource_range)?;
}
ArcCommand::WriteTimestamp {
query_set,
query_index,
} => {
query::write_timestamp(&mut state, query_set, query_index)?;
}
ArcCommand::ResolveQuerySet {
query_set,
start_query,
query_count,
destination,
destination_offset,
} => {
query::resolve_query_set(
&mut state,
query_set,
start_query,
query_count,
destination,
destination_offset,
)?;
}
ArcCommand::PushDebugGroup(label) => {
push_debug_group(&mut state, &label)?;
}
ArcCommand::PopDebugGroup => {
pop_debug_group(&mut state)?;
}
ArcCommand::InsertDebugMarker(label) => {
insert_debug_marker(&mut state, &label)?;
}
ArcCommand::BuildAccelerationStructures { blas, tlas } => {
ray_tracing::build_acceleration_structures(&mut state, blas, tlas)?;
}
ArcCommand::TransitionResources {
buffer_transitions,
texture_transitions,
} => {
transition_resources::transition_resources(
&mut state,
buffer_transitions,
texture_transitions,
)?;
}
ArcCommand::RunComputePass { .. } | ArcCommand::RunRenderPass { .. } => {
unreachable!()
}
}
}
}
if debug_scope_depth > 0 {
Err(CommandEncoderError::DebugGroupError(
DebugGroupError::MissingPop,
))?;
}
cmd_buf_data.encoder.close_if_open()?;
Ok(())
}
fn finish(
self: &Arc<Self>,
desc: &wgt::CommandBufferDescriptor<Label>,
) -> (Arc<CommandBuffer>, Option<CommandEncoderError>) {
let mut cmd_enc_status = self.data.lock();
let res = match cmd_enc_status.finish() {
CommandEncoderStatus::Finished(mut cmd_buf_data) => {
match Self::encode_commands(&self.device, &mut cmd_buf_data) {
Ok(()) => Ok(cmd_buf_data),
Err(error) => Err(EncoderErrorState {
error,
#[cfg(feature = "trace")]
trace_commands: mem::take(&mut cmd_buf_data.trace_commands),
}),
}
}
CommandEncoderStatus::Error(error_state) => Err(error_state),
_ => unreachable!(),
};
let (data, error) = match res {
Err(EncoderErrorState {
error,
#[cfg(feature = "trace")]
trace_commands,
}) => {
#[cfg(feature = "trace")]
if let Some(trace) = self.device.trace.lock().as_mut() {
use alloc::string::ToString;
trace.add(crate::device::trace::Action::FailedCommands {
commands: trace_commands,
failed_at_submit: None,
error: error.to_string(),
});
}
if error.is_destroyed_error() {
(make_error_state(error), None)
} else {
(make_error_state(error.clone()), Some(error))
}
}
Ok(data) => (CommandEncoderStatus::Finished(data), None),
};
let cmd_buf = Arc::new(CommandBuffer {
device: self.device.clone(),
label: desc.label.to_string(),
data: Mutex::new(rank::COMMAND_BUFFER_DATA, data),
});
(cmd_buf, error)
}
}
impl CommandBuffer {
#[doc(hidden)]
pub fn from_trace(device: &Arc<Device>, commands: Vec<Command<ArcReferences>>) -> Arc<Self> {
let encoder = device.create_command_encoder(&None).unwrap();
let mut cmd_enc_status = encoder.data.lock();
cmd_enc_status.replay(commands);
drop(cmd_enc_status);
let (cmd_buf, error) = encoder.finish(&wgt::CommandBufferDescriptor { label: None });
if let Some(err) = error {
panic!("CommandEncoder::finish failed: {err}");
}
cmd_buf
}
pub fn take_finished(&self) -> Result<CommandBufferMutable, CommandEncoderError> {
use CommandEncoderStatus as St;
match mem::replace(
&mut *self.data.lock(),
make_error_state(EncoderStateError::Submitted),
) {
St::Finished(command_buffer_mutable) => Ok(command_buffer_mutable),
St::Error(EncoderErrorState {
#[cfg(feature = "trace")]
trace_commands: _,
error,
}) => Err(error),
St::Recording(_) | St::Locked(_) | St::Consumed | St::Transitioning => unreachable!(),
}
}
}
crate::impl_resource_type!(CommandBuffer);
crate::impl_labeled!(CommandBuffer);
crate::impl_parent_device!(CommandBuffer);
crate::impl_storage_item!(CommandBuffer);
#[doc(hidden)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BasePass<C, E> {
pub label: Option<String>,
#[cfg_attr(feature = "serde", serde(skip, default = "Option::default"))]
pub error: Option<E>,
pub commands: Vec<C>,
pub dynamic_offsets: Vec<wgt::DynamicOffset>,
pub string_data: Vec<u8>,
pub immediates_data: Vec<u32>,
}
impl<C: Clone, E: Clone> BasePass<C, E> {
fn new(label: &Label) -> Self {
Self {
label: label.as_deref().map(str::to_owned),
error: None,
commands: Vec::new(),
dynamic_offsets: Vec::new(),
string_data: Vec::new(),
immediates_data: Vec::new(),
}
}
fn new_invalid(label: &Label, err: E) -> Self {
Self {
label: label.as_deref().map(str::to_owned),
error: Some(err),
commands: Vec::new(),
dynamic_offsets: Vec::new(),
string_data: Vec::new(),
immediates_data: Vec::new(),
}
}
fn take(&mut self) -> Result<BasePass<C, Infallible>, E> {
match self.error.as_ref() {
Some(err) => Err(err.clone()),
None => Ok(BasePass {
label: self.label.clone(),
error: None,
commands: mem::take(&mut self.commands),
dynamic_offsets: mem::take(&mut self.dynamic_offsets),
string_data: mem::take(&mut self.string_data),
immediates_data: mem::take(&mut self.immediates_data),
}),
}
}
}
macro_rules! pass_base {
($pass:expr, $scope:expr $(,)?) => {
match (&$pass.parent, &$pass.base.error) {
(&None, _) => return Err(EncoderStateError::Ended).map_pass_err($scope),
(&Some(_), &Some(_)) => return Ok(()),
(&Some(_), &None) => &mut $pass.base,
}
};
}
pub(crate) use pass_base;
macro_rules! pass_try {
($base:expr, $scope:expr, $res:expr $(,)?) => {
match $res.map_pass_err($scope) {
Ok(val) => val,
Err(err) => {
$base.error.get_or_insert(err);
return Ok(());
}
}
};
}
pub(crate) use pass_try;
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum EncoderStateError {
#[error("Encoder is invalid")]
Invalid,
#[error("Encoding must not have ended")]
Ended,
#[error("Encoder is locked by a previously created render/compute pass. Before recording any new commands, the pass must be ended.")]
Locked,
#[error(
"Encoder is not currently locked. A pass can only be ended while the encoder is locked."
)]
Unlocked,
#[error("This command buffer has already been submitted.")]
Submitted,
}
impl WebGpuError for EncoderStateError {
fn webgpu_error_type(&self) -> ErrorType {
match self {
EncoderStateError::Invalid
| EncoderStateError::Ended
| EncoderStateError::Locked
| EncoderStateError::Unlocked
| EncoderStateError::Submitted => ErrorType::Validation,
}
}
}
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum CommandEncoderError {
#[error(transparent)]
State(#[from] EncoderStateError),
#[error(transparent)]
Device(#[from] DeviceError),
#[error(transparent)]
InvalidResource(#[from] InvalidResourceError),
#[error(transparent)]
DestroyedResource(#[from] DestroyedResourceError),
#[error(transparent)]
ResourceUsage(#[from] ResourceUsageCompatibilityError),
#[error(transparent)]
DebugGroupError(#[from] DebugGroupError),
#[error(transparent)]
MissingFeatures(#[from] MissingFeatures),
#[error(transparent)]
Transfer(#[from] TransferError),
#[error(transparent)]
Clear(#[from] ClearError),
#[error(transparent)]
Query(#[from] QueryError),
#[error(transparent)]
BuildAccelerationStructure(#[from] BuildAccelerationStructureError),
#[error(transparent)]
TransitionResources(#[from] TransitionResourcesError),
#[error(transparent)]
ComputePass(#[from] ComputePassError),
#[error(transparent)]
RenderPass(#[from] RenderPassError),
}
impl CommandEncoderError {
fn is_destroyed_error(&self) -> bool {
matches!(
self,
Self::DestroyedResource(_)
| Self::Clear(ClearError::DestroyedResource(_))
| Self::Query(QueryError::DestroyedResource(_))
| Self::ComputePass(ComputePassError {
inner: ComputePassErrorInner::DestroyedResource(_),
..
})
| Self::RenderPass(RenderPassError {
inner: RenderPassErrorInner::DestroyedResource(_),
..
})
| Self::RenderPass(RenderPassError {
inner: RenderPassErrorInner::RenderCommand(
RenderCommandError::DestroyedResource(_)
),
..
})
| Self::RenderPass(RenderPassError {
inner: RenderPassErrorInner::RenderCommand(RenderCommandError::BindingError(
BindingError::DestroyedResource(_)
)),
..
})
)
}
}
impl WebGpuError for CommandEncoderError {
fn webgpu_error_type(&self) -> ErrorType {
match self {
Self::Device(e) => e.webgpu_error_type(),
Self::InvalidResource(e) => e.webgpu_error_type(),
Self::DebugGroupError(e) => e.webgpu_error_type(),
Self::MissingFeatures(e) => e.webgpu_error_type(),
Self::State(e) => e.webgpu_error_type(),
Self::DestroyedResource(e) => e.webgpu_error_type(),
Self::Transfer(e) => e.webgpu_error_type(),
Self::Clear(e) => e.webgpu_error_type(),
Self::Query(e) => e.webgpu_error_type(),
Self::BuildAccelerationStructure(e) => e.webgpu_error_type(),
Self::TransitionResources(e) => e.webgpu_error_type(),
Self::ResourceUsage(e) => e.webgpu_error_type(),
Self::ComputePass(e) => e.webgpu_error_type(),
Self::RenderPass(e) => e.webgpu_error_type(),
}
}
}
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum DebugGroupError {
#[error("Cannot pop debug group, because number of pushed debug groups is zero")]
InvalidPop,
#[error("A debug group was not popped before the encoder was finished")]
MissingPop,
}
impl WebGpuError for DebugGroupError {
fn webgpu_error_type(&self) -> ErrorType {
match self {
Self::InvalidPop | Self::MissingPop => ErrorType::Validation,
}
}
}
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum TimestampWritesError {
#[error(
"begin and end indices of pass timestamp writes are both set to {idx}, which is not allowed"
)]
IndicesEqual { idx: u32 },
#[error("no begin or end indices were specified for pass timestamp writes, expected at least one to be set")]
IndicesMissing,
}
impl WebGpuError for TimestampWritesError {
fn webgpu_error_type(&self) -> ErrorType {
match self {
Self::IndicesEqual { .. } | Self::IndicesMissing => ErrorType::Validation,
}
}
}
impl Global {
fn resolve_buffer_id(
&self,
buffer_id: Id<id::markers::Buffer>,
) -> Result<Arc<crate::resource::Buffer>, InvalidResourceError> {
self.hub.buffers.get(buffer_id).get()
}
fn resolve_texture_id(
&self,
texture_id: Id<id::markers::Texture>,
) -> Result<Arc<crate::resource::Texture>, InvalidResourceError> {
self.hub.textures.get(texture_id).get()
}
fn resolve_query_set(
&self,
query_set_id: Id<id::markers::QuerySet>,
) -> Result<Arc<QuerySet>, InvalidResourceError> {
self.hub.query_sets.get(query_set_id).get()
}
pub fn command_encoder_finish(
&self,
encoder_id: id::CommandEncoderId,
desc: &wgt::CommandBufferDescriptor<Label>,
id_in: Option<id::CommandBufferId>,
) -> (id::CommandBufferId, Option<(String, CommandEncoderError)>) {
profiling::scope!("CommandEncoder::finish");
let hub = &self.hub;
let cmd_enc = hub.command_encoders.get(encoder_id);
let (cmd_buf, opt_error) = cmd_enc.finish(desc);
let cmd_buf_id = hub.command_buffers.prepare(id_in).assign(cmd_buf);
(
cmd_buf_id,
opt_error.map(|error| (cmd_enc.label.clone(), error)),
)
}
pub fn command_encoder_push_debug_group(
&self,
encoder_id: id::CommandEncoderId,
label: &str,
) -> Result<(), EncoderStateError> {
profiling::scope!("CommandEncoder::push_debug_group");
api_log!("CommandEncoder::push_debug_group {label}");
let hub = &self.hub;
let cmd_enc = hub.command_encoders.get(encoder_id);
let mut cmd_buf_data = cmd_enc.data.lock();
cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> {
Ok(ArcCommand::PushDebugGroup(label.to_owned()))
})
}
pub fn command_encoder_insert_debug_marker(
&self,
encoder_id: id::CommandEncoderId,
label: &str,
) -> Result<(), EncoderStateError> {
profiling::scope!("CommandEncoder::insert_debug_marker");
api_log!("CommandEncoder::insert_debug_marker {label}");
let hub = &self.hub;
let cmd_enc = hub.command_encoders.get(encoder_id);
let mut cmd_buf_data = cmd_enc.data.lock();
cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> {
Ok(ArcCommand::InsertDebugMarker(label.to_owned()))
})
}
pub fn command_encoder_pop_debug_group(
&self,
encoder_id: id::CommandEncoderId,
) -> Result<(), EncoderStateError> {
profiling::scope!("CommandEncoder::pop_debug_marker");
api_log!("CommandEncoder::pop_debug_group");
let hub = &self.hub;
let cmd_enc = hub.command_encoders.get(encoder_id);
let mut cmd_buf_data = cmd_enc.data.lock();
cmd_buf_data
.push_with(|| -> Result<_, CommandEncoderError> { Ok(ArcCommand::PopDebugGroup) })
}
fn validate_pass_timestamp_writes<E>(
device: &Device,
query_sets: &Storage<Fallible<QuerySet>>,
timestamp_writes: &PassTimestampWrites,
) -> Result<ArcPassTimestampWrites, E>
where
E: From<TimestampWritesError>
+ From<QueryUseError>
+ From<DeviceError>
+ From<MissingFeatures>
+ From<InvalidResourceError>,
{
let &PassTimestampWrites {
query_set,
beginning_of_pass_write_index,
end_of_pass_write_index,
} = timestamp_writes;
device.require_features(wgt::Features::TIMESTAMP_QUERY)?;
let query_set = query_sets.get(query_set).get()?;
query_set.same_device(device)?;
for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
.into_iter()
.flatten()
{
query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None)?;
}
if let Some((begin, end)) = beginning_of_pass_write_index.zip(end_of_pass_write_index) {
if begin == end {
return Err(TimestampWritesError::IndicesEqual { idx: begin }.into());
}
}
if beginning_of_pass_write_index
.or(end_of_pass_write_index)
.is_none()
{
return Err(TimestampWritesError::IndicesMissing.into());
}
Ok(ArcPassTimestampWrites {
query_set,
beginning_of_pass_write_index,
end_of_pass_write_index,
})
}
}
pub(crate) fn push_debug_group(
state: &mut EncodingState,
label: &str,
) -> Result<(), CommandEncoderError> {
*state.debug_scope_depth += 1;
if !state
.device
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
unsafe { state.raw_encoder.begin_debug_marker(label) };
}
Ok(())
}
pub(crate) fn insert_debug_marker(
state: &mut EncodingState,
label: &str,
) -> Result<(), CommandEncoderError> {
if !state
.device
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
unsafe { state.raw_encoder.insert_debug_marker(label) };
}
Ok(())
}
pub(crate) fn pop_debug_group(state: &mut EncodingState) -> Result<(), CommandEncoderError> {
if *state.debug_scope_depth == 0 {
return Err(DebugGroupError::InvalidPop.into());
}
*state.debug_scope_depth -= 1;
if !state
.device
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
unsafe { state.raw_encoder.end_debug_marker() };
}
Ok(())
}
fn immediates_clear<PushFn>(offset: u32, size_bytes: u32, mut push_fn: PushFn)
where
PushFn: FnMut(u32, &[u32]),
{
let mut count_words = 0_u32;
let size_words = size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT;
while count_words < size_words {
let count_bytes = count_words * wgt::IMMEDIATE_DATA_ALIGNMENT;
let size_to_write_words =
(size_words - count_words).min(IMMEDIATES_CLEAR_ARRAY.len() as u32);
push_fn(
offset + count_bytes,
&IMMEDIATES_CLEAR_ARRAY[0..size_to_write_words as usize],
);
count_words += size_to_write_words;
}
}
#[derive(Debug, Copy, Clone)]
struct StateChange<T> {
last_state: Option<T>,
}
impl<T: Copy + PartialEq> StateChange<T> {
fn new() -> Self {
Self { last_state: None }
}
fn set_and_check_redundant(&mut self, new_state: T) -> bool {
let already_set = self.last_state == Some(new_state);
self.last_state = Some(new_state);
already_set
}
fn reset(&mut self) {
self.last_state = None;
}
}
impl<T: Copy + PartialEq> Default for StateChange<T> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
struct BindGroupStateChange {
last_states: [StateChange<Option<id::BindGroupId>>; hal::MAX_BIND_GROUPS],
}
impl BindGroupStateChange {
fn new() -> Self {
Self {
last_states: [StateChange::new(); hal::MAX_BIND_GROUPS],
}
}
fn set_and_check_redundant(
&mut self,
bind_group_id: Option<id::BindGroupId>,
index: u32,
dynamic_offsets: &mut Vec<u32>,
offsets: &[wgt::DynamicOffset],
) -> bool {
if offsets.is_empty() {
if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
if current_bind_group.set_and_check_redundant(bind_group_id) {
return true;
}
}
} else {
if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
current_bind_group.reset();
}
dynamic_offsets.extend_from_slice(offsets);
}
false
}
fn reset(&mut self) {
self.last_states = [StateChange::new(); hal::MAX_BIND_GROUPS];
}
}
impl Default for BindGroupStateChange {
fn default() -> Self {
Self::new()
}
}
trait MapPassErr<T> {
fn map_pass_err(self, scope: PassErrorScope) -> T;
}
impl<T, E, F> MapPassErr<Result<T, F>> for Result<T, E>
where
E: MapPassErr<F>,
{
fn map_pass_err(self, scope: PassErrorScope) -> Result<T, F> {
self.map_err(|err| err.map_pass_err(scope))
}
}
impl MapPassErr<PassStateError> for EncoderStateError {
fn map_pass_err(self, scope: PassErrorScope) -> PassStateError {
PassStateError { scope, inner: self }
}
}
#[derive(Clone, Copy, Debug)]
pub enum DrawKind {
Draw,
DrawIndirect,
MultiDrawIndirect,
MultiDrawIndirectCount,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DrawCommandFamily {
Draw,
DrawIndexed,
DrawMeshTasks,
}
#[derive(Clone, Copy, Debug, Error)]
pub enum PassErrorScope {
#[error("In a bundle parameter")]
Bundle,
#[error("In a pass parameter")]
Pass,
#[error("In a set_bind_group command")]
SetBindGroup,
#[error("In a set_pipeline command")]
SetPipelineRender,
#[error("In a set_pipeline command")]
SetPipelineCompute,
#[error("In a set_immediates command")]
SetImmediate,
#[error("In a set_vertex_buffer command")]
SetVertexBuffer,
#[error("In a set_index_buffer command")]
SetIndexBuffer,
#[error("In a set_blend_constant command")]
SetBlendConstant,
#[error("In a set_stencil_reference command")]
SetStencilReference,
#[error("In a set_viewport command")]
SetViewport,
#[error("In a set_scissor_rect command")]
SetScissorRect,
#[error("In a draw command, kind: {kind:?}")]
Draw {
kind: DrawKind,
family: DrawCommandFamily,
},
#[error("In a write_timestamp command")]
WriteTimestamp,
#[error("In a begin_occlusion_query command")]
BeginOcclusionQuery,
#[error("In a end_occlusion_query command")]
EndOcclusionQuery,
#[error("In a begin_pipeline_statistics_query command")]
BeginPipelineStatisticsQuery,
#[error("In a end_pipeline_statistics_query command")]
EndPipelineStatisticsQuery,
#[error("In a execute_bundle command")]
ExecuteBundle,
#[error("In a dispatch command, indirect:{indirect}")]
Dispatch { indirect: bool },
#[error("In a push_debug_group command")]
PushDebugGroup,
#[error("In a pop_debug_group command")]
PopDebugGroup,
#[error("In a insert_debug_marker command")]
InsertDebugMarker,
}
#[derive(Clone, Debug, Error)]
#[error("{scope}")]
pub struct PassStateError {
pub scope: PassErrorScope,
#[source]
pub(super) inner: EncoderStateError,
}
impl WebGpuError for PassStateError {
fn webgpu_error_type(&self) -> ErrorType {
let Self { scope: _, inner } = self;
inner.webgpu_error_type()
}
}