wgpu_core/command/
clear.rs

1use alloc::{string::String, sync::Arc, vec::Vec};
2use core::ops::Range;
3
4use crate::{
5    api_log,
6    command::{encoder::EncodingState, ArcCommand, EncoderStateError},
7    device::{DeviceError, MissingFeatures},
8    get_lowest_common_denom,
9    global::Global,
10    hal_label,
11    id::{BufferId, CommandEncoderId, TextureId},
12    init_tracker::{MemoryInitKind, TextureInitRange},
13    resource::{
14        Buffer, DestroyedResourceError, InvalidOrDestroyedResourceError, InvalidResourceError,
15        Labeled, MissingBufferUsageError, ParentDevice, RawResourceAccess, ResourceErrorIdent,
16        Texture, TextureClearMode,
17    },
18    snatch::SnatchGuard,
19    track::TextureTrackerSetSingle,
20};
21
22use thiserror::Error;
23use wgt::{
24    error::{ErrorType, WebGpuError},
25    math::align_to,
26    BufferAddress, BufferUsages, ImageSubresourceRange, TextureAspect, TextureSelector,
27};
28
29/// Error encountered while attempting a clear.
30#[derive(Clone, Debug, Error)]
31#[non_exhaustive]
32pub enum ClearError {
33    #[error(transparent)]
34    DestroyedResource(#[from] DestroyedResourceError),
35    #[error(transparent)]
36    MissingFeatures(#[from] MissingFeatures),
37    #[error("{0} can not be cleared")]
38    NoValidTextureClearMode(ResourceErrorIdent),
39    #[error("Buffer clear size {0:?} is not a multiple of `COPY_BUFFER_ALIGNMENT`")]
40    UnalignedFillSize(BufferAddress),
41    #[error("Buffer offset {0:?} is not a multiple of `COPY_BUFFER_ALIGNMENT`")]
42    UnalignedBufferOffset(BufferAddress),
43    #[error("Clear starts at offset {start_offset} with size of {requested_size}, but these added together exceed `u64::MAX`")]
44    OffsetPlusSizeExceeds64BitBounds {
45        start_offset: BufferAddress,
46        requested_size: BufferAddress,
47    },
48    #[error("Clear of {start_offset}..{end_offset} would end up overrunning the bounds of the buffer of size {buffer_size}")]
49    BufferOverrun {
50        start_offset: BufferAddress,
51        end_offset: BufferAddress,
52        buffer_size: BufferAddress,
53    },
54    #[error(transparent)]
55    MissingBufferUsage(#[from] MissingBufferUsageError),
56    #[error("Texture lacks the aspects that were specified in the image subresource range. Texture with format {texture_format:?}, specified was {subresource_range_aspects:?}")]
57    MissingTextureAspect {
58        texture_format: wgt::TextureFormat,
59        subresource_range_aspects: TextureAspect,
60    },
61    #[error("Image subresource level range is outside of the texture's level range. texture range is {texture_level_range:?},  \
62whereas subesource range specified start {subresource_base_mip_level} and count {subresource_mip_level_count:?}")]
63    InvalidTextureLevelRange {
64        texture_level_range: Range<u32>,
65        subresource_base_mip_level: u32,
66        subresource_mip_level_count: Option<u32>,
67    },
68    #[error("Image subresource layer range is outside of the texture's layer range. texture range is {texture_layer_range:?},  \
69whereas subesource range specified start {subresource_base_array_layer} and count {subresource_array_layer_count:?}")]
70    InvalidTextureLayerRange {
71        texture_layer_range: Range<u32>,
72        subresource_base_array_layer: u32,
73        subresource_array_layer_count: Option<u32>,
74    },
75    #[error(transparent)]
76    Device(#[from] DeviceError),
77    #[error(transparent)]
78    EncoderState(#[from] EncoderStateError),
79    #[error(transparent)]
80    InvalidResource(#[from] InvalidResourceError),
81}
82
83impl From<InvalidOrDestroyedResourceError> for ClearError {
84    fn from(value: InvalidOrDestroyedResourceError) -> Self {
85        match value {
86            InvalidOrDestroyedResourceError::InvalidResource(e) => Self::InvalidResource(e),
87            InvalidOrDestroyedResourceError::DestroyedResource(e) => Self::DestroyedResource(e),
88        }
89    }
90}
91
92impl WebGpuError for ClearError {
93    fn webgpu_error_type(&self) -> ErrorType {
94        match self {
95            Self::DestroyedResource(e) => e.webgpu_error_type(),
96            Self::MissingFeatures(e) => e.webgpu_error_type(),
97            Self::MissingBufferUsage(e) => e.webgpu_error_type(),
98            Self::Device(e) => e.webgpu_error_type(),
99            Self::EncoderState(e) => e.webgpu_error_type(),
100            Self::InvalidResource(e) => e.webgpu_error_type(),
101            Self::NoValidTextureClearMode(..)
102            | Self::UnalignedFillSize(..)
103            | Self::UnalignedBufferOffset(..)
104            | Self::OffsetPlusSizeExceeds64BitBounds { .. }
105            | Self::BufferOverrun { .. }
106            | Self::MissingTextureAspect { .. }
107            | Self::InvalidTextureLevelRange { .. }
108            | Self::InvalidTextureLayerRange { .. } => ErrorType::Validation,
109        }
110    }
111}
112
113impl Global {
114    pub fn command_encoder_clear_buffer(
115        &self,
116        command_encoder_id: CommandEncoderId,
117        dst: BufferId,
118        offset: BufferAddress,
119        size: Option<BufferAddress>,
120    ) -> Result<(), EncoderStateError> {
121        profiling::scope!("CommandEncoder::clear_buffer");
122        api_log!("CommandEncoder::clear_buffer {dst:?}");
123
124        let hub = &self.hub;
125
126        let cmd_enc = hub.command_encoders.get(command_encoder_id);
127        let mut cmd_buf_data = cmd_enc.data.lock();
128
129        cmd_buf_data.push_with(|| -> Result<_, ClearError> {
130            Ok(ArcCommand::ClearBuffer {
131                dst: self.resolve_buffer_id(dst)?,
132                offset,
133                size,
134            })
135        })
136    }
137
138    pub fn command_encoder_clear_texture(
139        &self,
140        command_encoder_id: CommandEncoderId,
141        dst: TextureId,
142        subresource_range: &ImageSubresourceRange,
143    ) -> Result<(), EncoderStateError> {
144        profiling::scope!("CommandEncoder::clear_texture");
145        api_log!("CommandEncoder::clear_texture {dst:?}");
146
147        let hub = &self.hub;
148
149        let cmd_enc = hub.command_encoders.get(command_encoder_id);
150
151        let mut cmd_buf_data = cmd_enc.data.lock();
152
153        cmd_buf_data.push_with(|| -> Result<_, ClearError> {
154            let texture = self.resolve_texture_id(dst);
155            texture.check_valid()?;
156            Ok(ArcCommand::ClearTexture {
157                dst: texture,
158                subresource_range: *subresource_range,
159            })
160        })
161    }
162}
163
164pub(super) fn clear_buffer(
165    state: &mut EncodingState,
166    dst_buffer: Arc<Buffer>,
167    offset: BufferAddress,
168    size: Option<BufferAddress>,
169) -> Result<(), ClearError> {
170    dst_buffer.same_device(state.device)?;
171
172    let dst_pending = state
173        .tracker
174        .buffers
175        .set_single(&dst_buffer, wgt::BufferUses::COPY_DST);
176
177    let dst_raw = dst_buffer.try_raw(state.snatch_guard)?;
178    dst_buffer.check_usage(BufferUsages::COPY_DST)?;
179
180    // Check if offset & size are valid.
181    if !offset.is_multiple_of(wgt::COPY_BUFFER_ALIGNMENT) {
182        return Err(ClearError::UnalignedBufferOffset(offset));
183    }
184
185    let size = size.unwrap_or(dst_buffer.size.saturating_sub(offset));
186    if !size.is_multiple_of(wgt::COPY_BUFFER_ALIGNMENT) {
187        return Err(ClearError::UnalignedFillSize(size));
188    }
189    let end_offset =
190        offset
191            .checked_add(size)
192            .ok_or(ClearError::OffsetPlusSizeExceeds64BitBounds {
193                start_offset: offset,
194                requested_size: size,
195            })?;
196    if end_offset > dst_buffer.size {
197        return Err(ClearError::BufferOverrun {
198            start_offset: offset,
199            end_offset,
200            buffer_size: dst_buffer.size,
201        });
202    }
203
204    // This must happen after parameter validation (so that errors are reported
205    // as required by the spec), but before any side effects.
206    if offset == end_offset {
207        log::trace!("Ignoring fill_buffer of size 0");
208        return Ok(());
209    }
210
211    // Mark dest as initialized.
212    state
213        .buffer_memory_init_actions
214        .extend(dst_buffer.initialization_status.read().create_action(
215            &dst_buffer,
216            offset..end_offset,
217            MemoryInitKind::ImplicitlyInitialized,
218        ));
219
220    // actual hal barrier & operation
221    let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, state.snatch_guard));
222    unsafe {
223        state.raw_encoder.transition_buffers(dst_barrier.as_slice());
224        state.raw_encoder.clear_buffer(dst_raw, offset..end_offset);
225    }
226
227    Ok(())
228}
229
230/// Validate and encode a "Clear Texture" command.
231///
232/// This function implements `CommandEncoder::clear_texture` when invoked via
233/// the command encoder APIs or trace playback. It has the suffix `_cmd` to
234/// distinguish it from [`clear_texture`]. [`clear_texture`], used internally by
235/// this function, is a lower-level function that encodes a texture clear
236/// operation without validating it.
237pub(super) fn clear_texture_cmd(
238    state: &mut EncodingState,
239    dst_texture: Arc<Texture>,
240    subresource_range: &ImageSubresourceRange,
241) -> Result<(), ClearError> {
242    dst_texture.same_device(state.device)?;
243    state
244        .device
245        .require_features(wgt::Features::CLEAR_TEXTURE)?;
246
247    // Check if subresource aspects are valid.
248    let clear_aspects = hal::FormatAspects::new(dst_texture.desc.format, subresource_range.aspect);
249    if clear_aspects.is_empty() {
250        return Err(ClearError::MissingTextureAspect {
251            texture_format: dst_texture.desc.format,
252            subresource_range_aspects: subresource_range.aspect,
253        });
254    };
255
256    // Check if subresource level range is valid
257    let subresource_mip_range = subresource_range.mip_range(dst_texture.full_range.mips.end);
258    if dst_texture.full_range.mips.start > subresource_mip_range.start
259        || dst_texture.full_range.mips.end < subresource_mip_range.end
260    {
261        return Err(ClearError::InvalidTextureLevelRange {
262            texture_level_range: dst_texture.full_range.mips.clone(),
263            subresource_base_mip_level: subresource_range.base_mip_level,
264            subresource_mip_level_count: subresource_range.mip_level_count,
265        });
266    }
267    // Check if subresource layer range is valid
268    let subresource_layer_range = subresource_range.layer_range(dst_texture.full_range.layers.end);
269    if dst_texture.full_range.layers.start > subresource_layer_range.start
270        || dst_texture.full_range.layers.end < subresource_layer_range.end
271    {
272        return Err(ClearError::InvalidTextureLayerRange {
273            texture_layer_range: dst_texture.full_range.layers.clone(),
274            subresource_base_array_layer: subresource_range.base_array_layer,
275            subresource_array_layer_count: subresource_range.array_layer_count,
276        });
277    }
278
279    clear_texture(
280        &dst_texture,
281        TextureInitRange {
282            mip_range: subresource_mip_range,
283            layer_range: subresource_layer_range,
284        },
285        state.raw_encoder,
286        &mut state.tracker.textures,
287        &state.device.alignments,
288        state.device.zero_buffer.as_ref(),
289        state.snatch_guard,
290        state.device.instance_flags,
291    )?;
292
293    Ok(())
294}
295
296/// Encode a texture clear operation.
297///
298/// This function encodes a texture clear operation without validating it.
299/// Texture clears requested via the API call this function via
300/// [`clear_texture_cmd`], which does the validation. This function is also
301/// called directly from various places within wgpu that need to clear a
302/// texture.
303pub(crate) fn clear_texture<T: TextureTrackerSetSingle>(
304    dst_texture: &Arc<Texture>,
305    range: TextureInitRange,
306    encoder: &mut dyn hal::DynCommandEncoder,
307    texture_tracker: &mut T,
308    alignments: &hal::Alignments,
309    zero_buffer: &dyn hal::DynBuffer,
310    snatch_guard: &SnatchGuard<'_>,
311    instance_flags: wgt::InstanceFlags,
312) -> Result<(), ClearError> {
313    let dst_raw = dst_texture.try_inner(snatch_guard)?.raw();
314
315    // Issue the right barrier.
316    let clear_usage = match *dst_texture.clear_mode.read() {
317        TextureClearMode::BufferCopy => wgt::TextureUses::COPY_DST,
318        TextureClearMode::RenderPass {
319            is_color: false, ..
320        } => wgt::TextureUses::DEPTH_STENCIL_WRITE,
321        TextureClearMode::Surface { .. } | TextureClearMode::RenderPass { is_color: true, .. } => {
322            wgt::TextureUses::COLOR_TARGET
323        }
324        TextureClearMode::None => {
325            return Err(ClearError::NoValidTextureClearMode(
326                dst_texture.error_ident(),
327            ));
328        }
329    };
330
331    let selector = TextureSelector {
332        mips: range.mip_range.clone(),
333        layers: range.layer_range.clone(),
334    };
335
336    // If we're in a texture-init usecase, we know that the texture is already
337    // tracked since whatever caused the init requirement, will have caused the
338    // usage tracker to be aware of the texture. Meaning, that it is safe to
339    // call call change_replace_tracked if the life_guard is already gone (i.e.
340    // the user no longer holds on to this texture).
341    //
342    // On the other hand, when coming via command_encoder_clear_texture, the
343    // life_guard is still there since in order to call it a texture object is
344    // needed.
345    //
346    // We could in theory distinguish these two scenarios in the internal
347    // clear_texture api in order to remove this check and call the cheaper
348    // change_replace_tracked whenever possible.
349    let dst_barrier = texture_tracker
350        .set_single(dst_texture, selector, clear_usage)
351        .map(|pending| pending.into_hal(dst_raw))
352        .collect::<Vec<_>>();
353    unsafe {
354        encoder.transition_textures(&dst_barrier);
355    }
356
357    // Record actual clearing
358    let clear_mode = dst_texture.clear_mode.read();
359    match *clear_mode {
360        TextureClearMode::BufferCopy => clear_texture_via_buffer_copies(
361            &dst_texture.desc,
362            alignments,
363            zero_buffer,
364            range,
365            encoder,
366            dst_raw,
367        ),
368        TextureClearMode::Surface { .. } => {
369            drop(clear_mode);
370            clear_texture_via_render_passes(dst_texture, range, true, encoder, instance_flags)?
371        }
372        TextureClearMode::RenderPass { is_color, .. } => {
373            drop(clear_mode);
374            clear_texture_via_render_passes(dst_texture, range, is_color, encoder, instance_flags)?
375        }
376        TextureClearMode::None => {
377            return Err(ClearError::NoValidTextureClearMode(
378                dst_texture.error_ident(),
379            ));
380        }
381    }
382    Ok(())
383}
384
385fn clear_texture_via_buffer_copies(
386    texture_desc: &wgt::TextureDescriptor<String, Vec<wgt::TextureFormat>>,
387    alignments: &hal::Alignments,
388    zero_buffer: &dyn hal::DynBuffer, // Buffer of size device::ZERO_BUFFER_SIZE
389    range: TextureInitRange,
390    encoder: &mut dyn hal::DynCommandEncoder,
391    dst_raw: &dyn hal::DynTexture,
392) {
393    assert!(!texture_desc.format.is_depth_stencil_format());
394
395    if texture_desc.format == wgt::TextureFormat::NV12
396        || texture_desc.format == wgt::TextureFormat::P010
397    {
398        // TODO: Currently COPY_DST for NV12 and P010 textures is unsupported.
399        return;
400    }
401
402    // Gather list of zero_buffer copies and issue a single command then to perform them
403    let mut zero_buffer_copy_regions = Vec::new();
404    let buffer_copy_pitch = alignments.buffer_copy_pitch.get() as u32;
405    let (block_width, block_height) = texture_desc.format.block_dimensions();
406    let block_size = texture_desc.format.block_copy_size(None).unwrap();
407
408    let bytes_per_row_alignment = get_lowest_common_denom(buffer_copy_pitch, block_size);
409
410    for mip_level in range.mip_range {
411        let mut mip_size = texture_desc.mip_level_size(mip_level).unwrap();
412        // Round to multiple of block size
413        mip_size.width = align_to(mip_size.width, block_width);
414        mip_size.height = align_to(mip_size.height, block_height);
415
416        let bytes_per_row = align_to(
417            mip_size.width / block_width * block_size,
418            bytes_per_row_alignment,
419        );
420
421        let max_rows_per_copy = crate::device::ZERO_BUFFER_SIZE as u32 / bytes_per_row;
422        // round down to a multiple of rows needed by the texture format
423        let max_rows_per_copy = max_rows_per_copy / block_height * block_height;
424        assert!(
425            max_rows_per_copy > 0,
426            "Zero buffer size is too small to fill a single row \
427            of a texture with format {:?} and desc {:?}",
428            texture_desc.format,
429            texture_desc.size
430        );
431
432        let z_range = 0..(if texture_desc.dimension == wgt::TextureDimension::D3 {
433            mip_size.depth_or_array_layers
434        } else {
435            1
436        });
437
438        for array_layer in range.layer_range.clone() {
439            // TODO: Only doing one layer at a time for volume textures right now.
440            for z in z_range.clone() {
441                // May need multiple copies for each subresource! However, we
442                // assume that we never need to split a row.
443                let mut num_rows_left = mip_size.height;
444                while num_rows_left > 0 {
445                    let num_rows = num_rows_left.min(max_rows_per_copy);
446
447                    zero_buffer_copy_regions.push(hal::BufferTextureCopy {
448                        buffer_layout: wgt::TexelCopyBufferLayout {
449                            offset: 0,
450                            bytes_per_row: Some(bytes_per_row),
451                            rows_per_image: None,
452                        },
453                        texture_base: hal::TextureCopyBase {
454                            mip_level,
455                            array_layer,
456                            origin: wgt::Origin3d {
457                                x: 0, // Always full rows
458                                y: mip_size.height - num_rows_left,
459                                z,
460                            },
461                            aspect: hal::FormatAspects::COLOR,
462                        },
463                        size: hal::CopyExtent {
464                            width: mip_size.width, // full row
465                            height: num_rows,
466                            depth: 1, // Only single slice of volume texture at a time right now
467                        },
468                    });
469
470                    num_rows_left -= num_rows;
471                }
472            }
473        }
474    }
475
476    unsafe {
477        encoder.copy_buffer_to_texture(zero_buffer, dst_raw, &zero_buffer_copy_regions);
478    }
479}
480
481fn clear_texture_via_render_passes(
482    dst_texture: &Texture,
483    range: TextureInitRange,
484    is_color: bool,
485    encoder: &mut dyn hal::DynCommandEncoder,
486    instance_flags: wgt::InstanceFlags,
487) -> Result<(), ClearError> {
488    assert_eq!(dst_texture.desc.dimension, wgt::TextureDimension::D2);
489
490    let extent_base = wgt::Extent3d {
491        width: dst_texture.desc.size.width,
492        height: dst_texture.desc.size.height,
493        depth_or_array_layers: 1, // Only one layer is cleared at a time.
494    };
495
496    let clear_mode = dst_texture.clear_mode.read();
497
498    for mip_level in range.mip_range {
499        let extent = extent_base.mip_level_size(mip_level, dst_texture.desc.dimension);
500        for depth_or_layer in range.layer_range.clone() {
501            let color_attachments_tmp;
502            let (color_attachments, depth_stencil_attachment) = if is_color {
503                color_attachments_tmp = [Some(hal::ColorAttachment {
504                    target: hal::Attachment {
505                        view: Texture::get_clear_view(
506                            &clear_mode,
507                            &dst_texture.desc,
508                            mip_level,
509                            depth_or_layer,
510                        ),
511                        usage: wgt::TextureUses::COLOR_TARGET,
512                    },
513                    depth_slice: None,
514                    resolve_target: None,
515                    ops: hal::AttachmentOps::STORE | hal::AttachmentOps::LOAD_CLEAR,
516                    clear_value: wgt::Color::TRANSPARENT,
517                })];
518                (&color_attachments_tmp[..], None)
519            } else {
520                (
521                    &[][..],
522                    Some(hal::DepthStencilAttachment {
523                        target: hal::Attachment {
524                            view: Texture::get_clear_view(
525                                &clear_mode,
526                                &dst_texture.desc,
527                                mip_level,
528                                depth_or_layer,
529                            ),
530                            usage: wgt::TextureUses::DEPTH_STENCIL_WRITE,
531                        },
532                        depth_ops: hal::AttachmentOps::STORE | hal::AttachmentOps::LOAD_CLEAR,
533                        stencil_ops: hal::AttachmentOps::STORE | hal::AttachmentOps::LOAD_CLEAR,
534                        clear_value: (0.0, 0),
535                    }),
536                )
537            };
538            unsafe {
539                encoder
540                    .begin_render_pass(&hal::RenderPassDescriptor {
541                        label: hal_label(
542                            Some("(wgpu internal) clear_texture clear pass"),
543                            instance_flags,
544                        ),
545                        extent,
546                        sample_count: dst_texture.desc.sample_count,
547                        color_attachments,
548                        depth_stencil_attachment,
549                        multiview_mask: None,
550                        timestamp_writes: None,
551                        occlusion_query_set: None,
552                    })
553                    .map_err(|e| dst_texture.device.handle_hal_error(e))?;
554                encoder.end_render_pass();
555            }
556        }
557    }
558
559    Ok(())
560}