Skip to content

Commit

Permalink
Make the command_encoder_clear_buffer's size an Option<BufferAddress> (
Browse files Browse the repository at this point in the history
…#4737)

* Make the size parameter of command_encoder_clear_buffer an Option<BufferAddress>

* Add a changelog entry
  • Loading branch information
nical authored Nov 22, 2023
1 parent dec907a commit 7dad106
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ By @gents83 in [#3626](https://github.com/gfx-rs/wgpu/pull/3626) and tnx also to
- Log vulkan validation layer messages during instance creation and destruction: By @exrook in [#4586](https://github.com/gfx-rs/wgpu/pull/4586)
- `TextureFormat::block_size` is deprecated, use `TextureFormat::block_copy_size` instead: By @wumpf in [#4647](https://github.com/gfx-rs/wgpu/pull/4647)
- Rename of `DispatchIndirect`, `DrawIndexedIndirect`, and `DrawIndirect` types in the `wgpu::util` module to `DispatchIndirectArgs`, `DrawIndexedIndirectArgs`, and `DrawIndirectArgs`. By @cwfitzgerald in [#4723](https://github.com/gfx-rs/wgpu/pull/4723).
- Make the size parameter of `encoder.clear_buffer` an `Option<u64>` instead of `Option<NonZero<u64>>`. By @nical in [#4737](https://github.com/gfx-rs/wgpu/pull/4737)

#### Safe `Surface` creation

Expand Down
2 changes: 1 addition & 1 deletion deno_webgpu/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pub fn op_webgpu_command_encoder_clear_buffer(
command_encoder,
destination_resource.1,
offset,
std::num::NonZeroU64::new(size)
Some(size)
))
}

Expand Down
8 changes: 2 additions & 6 deletions tests/tests/regression/issue_4122.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{num::NonZeroU64, ops::Range};
use std::ops::Range;

use wgpu_test::{gpu_test, GpuTestConfiguration, TestingContext};

Expand Down Expand Up @@ -27,11 +27,7 @@ fn fill_test(ctx: &TestingContext, range: Range<u64>, size: u64) -> bool {
label: Some("encoder"),
});

encoder.clear_buffer(
&gpu_buffer,
range.start,
NonZeroU64::new(range.end - range.start),
);
encoder.clear_buffer(&gpu_buffer, range.start, Some(range.end - range.start));
encoder.copy_buffer_to_buffer(&gpu_buffer, 0, &cpu_buffer, 0, size);

ctx.queue.submit(Some(encoder.finish()));
Expand Down
14 changes: 6 additions & 8 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ use crate::{

use hal::CommandEncoder as _;
use thiserror::Error;
use wgt::{
math::align_to, BufferAddress, BufferSize, BufferUsages, ImageSubresourceRange, TextureAspect,
};
use wgt::{math::align_to, BufferAddress, BufferUsages, ImageSubresourceRange, TextureAspect};

/// Error encountered while attempting a clear.
#[derive(Clone, Debug, Error)]
Expand All @@ -37,7 +35,7 @@ pub enum ClearError {
#[error("Texture {0:?} can not be cleared")]
NoValidTextureClearMode(TextureId),
#[error("Buffer clear size {0:?} is not a multiple of `COPY_BUFFER_ALIGNMENT`")]
UnalignedFillSize(BufferSize),
UnalignedFillSize(BufferAddress),
#[error("Buffer offset {0:?} is not a multiple of `COPY_BUFFER_ALIGNMENT`")]
UnalignedBufferOffset(BufferAddress),
#[error("Clear of {start_offset}..{end_offset} would end up overrunning the bounds of the buffer of size {buffer_size}")]
Expand Down Expand Up @@ -75,7 +73,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
command_encoder_id: CommandEncoderId,
dst: BufferId,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferAddress>,
) -> Result<(), ClearError> {
profiling::scope!("CommandEncoder::clear_buffer");
log::trace!("CommandEncoder::clear_buffer {dst:?}");
Expand Down Expand Up @@ -116,10 +114,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
return Err(ClearError::UnalignedBufferOffset(offset));
}
if let Some(size) = size {
if size.get() % wgt::COPY_BUFFER_ALIGNMENT != 0 {
if size % wgt::COPY_BUFFER_ALIGNMENT != 0 {
return Err(ClearError::UnalignedFillSize(size));
}
let destination_end_offset = offset + size.get();
let destination_end_offset = offset + size;
if destination_end_offset > dst_buffer.size {
return Err(ClearError::BufferOverrun {
start_offset: offset,
Expand All @@ -130,7 +128,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}

let end = match size {
Some(size) => offset + size.get(),
Some(size) => offset + size,
None => dst_buffer.size,
};
if offset == end {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/device/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub enum Command {
ClearBuffer {
dst: id::BufferId,
offset: wgt::BufferAddress,
size: Option<wgt::BufferSize>,
size: Option<wgt::BufferAddress>,
},
ClearTexture {
dst: id::TextureId,
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@ impl crate::Context for Context {
encoder_data: &Self::CommandEncoderData,
buffer: &crate::Buffer,
offset: wgt::BufferAddress,
size: Option<wgt::BufferSize>,
size: Option<wgt::BufferAddress>,
) {
let global = &self.0;
if let Err(cause) = wgc::gfx_select!(encoder => global.command_encoder_clear_buffer(
Expand Down
12 changes: 6 additions & 6 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,15 +2446,15 @@ impl crate::context::Context for Context {
encoder_data: &Self::CommandEncoderData,
buffer: &crate::Buffer,
offset: wgt::BufferAddress,
size: Option<wgt::BufferSize>,
size: Option<wgt::BufferAddress>,
) {
let buffer: &<Context as crate::Context>::BufferData = downcast_ref(buffer.data.as_ref());
match size {
Some(size) => encoder_data.0.clear_buffer_with_f64_and_f64(
&buffer.0,
offset as f64,
size.get() as f64,
),
Some(size) => {
encoder_data
.0
.clear_buffer_with_f64_and_f64(&buffer.0, offset as f64, size as f64)
}
None => encoder_data
.0
.clear_buffer_with_f64(&buffer.0, offset as f64),
Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ pub trait Context: Debug + WasmNotSendSync + Sized {
encoder_data: &Self::CommandEncoderData,
buffer: &Buffer,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferAddress>,
);

fn command_encoder_insert_debug_marker(
Expand Down Expand Up @@ -1570,7 +1570,7 @@ pub(crate) trait DynContext: Debug + WasmNotSendSync {
encoder_data: &crate::Data,
buffer: &Buffer,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferAddress>,
);

fn command_encoder_insert_debug_marker(
Expand Down Expand Up @@ -2914,7 +2914,7 @@ where
encoder_data: &crate::Data,
buffer: &Buffer,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferAddress>,
) {
let encoder = <T::CommandEncoderId>::from(*encoder);
let encoder_data = downcast_ref(encoder_data);
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3643,7 +3643,7 @@ impl CommandEncoder {
&mut self,
buffer: &Buffer,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferAddress>,
) {
DynContext::command_encoder_clear_buffer(
&*self.context,
Expand Down

0 comments on commit 7dad106

Please sign in to comment.