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                        Ok(wgt::TextureTransition {
42                            texture: self.resolve_texture_id(t.texture)?,
43                            selector: t.selector,
44                            state: t.state,
45                        })
46                    })
47                    .collect::<Result<_, TransitionResourcesError>>()?,
48            })
49        })
50    }
51}
52
53pub(crate) fn transition_resources(
54    state: &mut EncodingState,
55    buffer_transitions: Vec<wgt::BufferTransition<Arc<Buffer>>>,
56    texture_transitions: Vec<wgt::TextureTransition<Arc<Texture>>>,
57) -> Result<(), TransitionResourcesError> {
58    let mut usage_scope = state.device.new_usage_scope();
59    let indices = &state.device.tracker_indices;
60    usage_scope.buffers.set_size(indices.buffers.size());
61    usage_scope.textures.set_size(indices.textures.size());
62
63    // Process buffer transitions
64    for buffer_transition in buffer_transitions {
65        buffer_transition.buffer.same_device(state.device)?;
66
67        usage_scope
68            .buffers
69            .merge_single(&buffer_transition.buffer, buffer_transition.state)?;
70    }
71
72    // Process texture transitions
73    for texture_transition in texture_transitions {
74        texture_transition.texture.same_device(state.device)?;
75
76        unsafe {
77            usage_scope.textures.merge_single(
78                &texture_transition.texture,
79                texture_transition.selector,
80                texture_transition.state,
81            )
82        }?;
83    }
84
85    // Record any needed barriers based on tracker data
86    CommandEncoder::insert_barriers_from_scope(
87        state.raw_encoder,
88        state.tracker,
89        &usage_scope,
90        state.snatch_guard,
91    );
92    Ok(())
93}
94
95/// Error encountered while attempting to perform [`Global::command_encoder_transition_resources`].
96#[derive(Clone, Debug, Error)]
97#[non_exhaustive]
98pub enum TransitionResourcesError {
99    #[error(transparent)]
100    Device(#[from] DeviceError),
101    #[error(transparent)]
102    EncoderState(#[from] EncoderStateError),
103    #[error(transparent)]
104    InvalidResource(#[from] InvalidResourceError),
105    #[error(transparent)]
106    ResourceUsage(#[from] ResourceUsageCompatibilityError),
107}
108
109impl WebGpuError for TransitionResourcesError {
110    fn webgpu_error_type(&self) -> ErrorType {
111        let e: &dyn WebGpuError = match self {
112            Self::Device(e) => e,
113            Self::EncoderState(e) => e,
114            Self::InvalidResource(e) => e,
115            Self::ResourceUsage(e) => e,
116        };
117        e.webgpu_error_type()
118    }
119}