Skip to main content

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