wgpu_core/command/
transition_resources.rs

1use alloc::{sync::Arc, vec::Vec};
2
3use thiserror::Error;
4use wgt::error::{ErrorType, WebGpuError};
5
6use crate::{
7    command::{encoder::EncodingState, ArcCommand, CommandEncoder, EncoderStateError},
8    device::DeviceError,
9    global::Global,
10    id::{BufferId, CommandEncoderId, TextureId},
11    resource::{Buffer, InvalidResourceError, ParentDevice, Texture},
12    track::ResourceUsageCompatibilityError,
13};
14
15impl Global {
16    pub fn command_encoder_transition_resources(
17        &self,
18        command_encoder_id: CommandEncoderId,
19        buffer_transitions: impl Iterator<Item = wgt::BufferTransition<BufferId>>,
20        texture_transitions: impl Iterator<Item = wgt::TextureTransition<TextureId>>,
21    ) -> Result<(), EncoderStateError> {
22        profiling::scope!("CommandEncoder::transition_resources");
23
24        let hub = &self.hub;
25
26        // Lock command encoder for recording
27        let cmd_enc = hub.command_encoders.get(command_encoder_id);
28        let mut cmd_buf_data = cmd_enc.data.lock();
29        cmd_buf_data.push_with(|| -> Result<_, TransitionResourcesError> {
30            Ok(ArcCommand::TransitionResources {
31                buffer_transitions: buffer_transitions
32                    .map(|t| {
33                        Ok(wgt::BufferTransition {
34                            buffer: self.resolve_buffer_id(t.buffer)?,
35                            state: t.state,
36                        })
37                    })
38                    .collect::<Result<_, TransitionResourcesError>>()?,
39                texture_transitions: texture_transitions
40                    .map(|t| {
41                        let texture = self.resolve_texture_id(t.texture);
42                        texture.check_valid()?;
43                        Ok(wgt::TextureTransition {
44                            texture,
45                            selector: t.selector,
46                            state: t.state,
47                        })
48                    })
49                    .collect::<Result<_, TransitionResourcesError>>()?,
50            })
51        })
52    }
53}
54
55pub(crate) fn transition_resources(
56    state: &mut EncodingState,
57    buffer_transitions: Vec<wgt::BufferTransition<Arc<Buffer>>>,
58    texture_transitions: Vec<wgt::TextureTransition<Arc<Texture>>>,
59) -> Result<(), TransitionResourcesError> {
60    let mut usage_scope = state.device.new_usage_scope();
61    let indices = &state.device.tracker_indices;
62    usage_scope.buffers.set_size(indices.buffers.size());
63    usage_scope.textures.set_size(indices.textures.size());
64
65    // Process buffer transitions
66    for buffer_transition in buffer_transitions {
67        buffer_transition.buffer.same_device(state.device)?;
68
69        usage_scope
70            .buffers
71            .merge_single(&buffer_transition.buffer, buffer_transition.state)?;
72    }
73
74    // Process texture transitions
75    for texture_transition in texture_transitions {
76        texture_transition.texture.same_device(state.device)?;
77
78        unsafe {
79            usage_scope.textures.merge_single(
80                &texture_transition.texture,
81                texture_transition.selector,
82                texture_transition.state,
83            )
84        }?;
85    }
86
87    // Record any needed barriers based on tracker data
88    CommandEncoder::insert_barriers_from_scope(
89        state.raw_encoder,
90        state.tracker,
91        &usage_scope,
92        state.snatch_guard,
93    );
94    Ok(())
95}
96
97/// Error encountered while attempting to perform [`Global::command_encoder_transition_resources`].
98#[derive(Clone, Debug, Error)]
99#[non_exhaustive]
100pub enum TransitionResourcesError {
101    #[error(transparent)]
102    Device(#[from] DeviceError),
103    #[error(transparent)]
104    EncoderState(#[from] EncoderStateError),
105    #[error(transparent)]
106    InvalidResource(#[from] InvalidResourceError),
107    #[error(transparent)]
108    ResourceUsage(#[from] ResourceUsageCompatibilityError),
109}
110
111impl WebGpuError for TransitionResourcesError {
112    fn webgpu_error_type(&self) -> ErrorType {
113        match self {
114            Self::Device(e) => e.webgpu_error_type(),
115            Self::EncoderState(e) => e.webgpu_error_type(),
116            Self::InvalidResource(e) => e.webgpu_error_type(),
117            Self::ResourceUsage(e) => e.webgpu_error_type(),
118        }
119    }
120}