Skip to content

Make the copy_buffer_to_buffer size parameter optional #7659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Naga now infers the correct binding layout when a resource appears only in an as

- Use highest SPIR-V version supported by Vulkan API version. By @robamler in [#7595](https://github.com/gfx-rs/wgpu/pull/7595)

#### WebGPU

- The type of the `size` parameter to `copy_buffer_to_buffer` has changed from `BufferAddress` to `impl Into<Option<BufferAddress>>`. This achieves the spec-defined behavior of the value being optional, while still accepting existing calls without changes. By @andyleiserson in [#7659](https://github.com/gfx-rs/wgpu/pull/7659).

### Bug Fixes

#### Naga
Expand Down
5 changes: 5 additions & 0 deletions cts_runner/test.lst
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
unittests:*
webgpu:api,operation,command_buffer,basic:*
webgpu:api,operation,command_buffer,copyBufferToBuffer:single:newSig=false;*
// https://github.com/gfx-rs/wgpu/issues/7391
//FAIL: webgpu:api,operation,command_buffer,copyBufferToBuffer:single:newSig=true;*
webgpu:api,operation,command_buffer,copyBufferToBuffer:state_transitions:*
webgpu:api,operation,command_buffer,copyBufferToBuffer:copy_order:*
webgpu:api,operation,compute,basic:memcpy:*
//FAIL: webgpu:api,operation,compute,basic:large_dispatch:*
webgpu:api,operation,device,lost:*
Expand Down
83 changes: 75 additions & 8 deletions deno_webgpu/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use std::cell::RefCell;

use deno_core::cppgc::Ptr;
use deno_core::op2;
use deno_core::v8;
use deno_core::webidl::{IntOptions, WebIdlConverter, WebIdlError};
use deno_core::GarbageCollected;
use deno_core::WebIDL;
use deno_error::JsErrorBox;
use wgpu_core::command::PassChannel;
use wgpu_types::TexelCopyBufferInfo;
use wgpu_types::{BufferAddress, TexelCopyBufferInfo};

use crate::buffer::GPUBuffer;
use crate::command_buffer::GPUCommandBuffer;
Expand Down Expand Up @@ -171,15 +173,78 @@ impl GPUCommandEncoder {
}
}

#[required(5)]
fn copy_buffer_to_buffer(
#[required(2)]
fn copy_buffer_to_buffer<'a>(
&self,
scope: &mut v8::HandleScope<'a>,
#[webidl] source: Ptr<GPUBuffer>,
#[webidl(options(enforce_range = true))] source_offset: u64,
#[webidl] destination: Ptr<GPUBuffer>,
#[webidl(options(enforce_range = true))] destination_offset: u64,
#[webidl(options(enforce_range = true))] size: u64,
) {
arg2: v8::Local<'a, v8::Value>,
arg3: v8::Local<'a, v8::Value>,
arg4: v8::Local<'a, v8::Value>,
arg5: v8::Local<'a, v8::Value>,
) -> Result<(), WebIdlError> {
let prefix = "Failed to execute 'GPUCommandEncoder.copyBufferToBuffer'";
let int_options = IntOptions {
clamp: false,
enforce_range: true,
};

let source_offset: BufferAddress;
let destination: Ptr<GPUBuffer>;
let destination_offset: BufferAddress;
let size: Option<BufferAddress>;
// Note that the last argument to either overload of `copy_buffer_to_buffer`
// is optional, so `arg5.is_undefined()` would not work here.
if arg4.is_undefined() {
// 3-argument overload
source_offset = 0;
destination = Ptr::<GPUBuffer>::convert(
scope,
arg2,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("destination")).into(),
&(),
)?;
destination_offset = 0;
size = <Option<u64>>::convert(
scope,
arg3,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("size")).into(),
&int_options,
)?;
} else {
// 5-argument overload
source_offset = u64::convert(
scope,
arg2,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("sourceOffset")).into(),
&int_options,
)?;
destination = Ptr::<GPUBuffer>::convert(
scope,
arg3,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("destination")).into(),
&(),
)?;
destination_offset = u64::convert(
scope,
arg4,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("destinationOffset")).into(),
&int_options,
)?;
size = <Option<u64>>::convert(
scope,
arg5,
Cow::Borrowed(prefix),
(|| Cow::Borrowed("size")).into(),
&int_options,
)?;
}

let err = self
.instance
.command_encoder_copy_buffer_to_buffer(
Expand All @@ -193,6 +258,8 @@ impl GPUCommandEncoder {
.err();

self.error_handler.push_error(err);

Ok(())
}

#[required(3)]
Expand Down
8 changes: 6 additions & 2 deletions wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl Global {
source_offset: BufferAddress,
destination: BufferId,
destination_offset: BufferAddress,
size: BufferAddress,
size: Option<BufferAddress>,
) -> Result<(), CopyError> {
profiling::scope!("CommandEncoder::copy_buffer_to_buffer");
api_log!(
Expand Down Expand Up @@ -598,6 +598,11 @@ impl Global {
.map_err(TransferError::MissingBufferUsage)?;
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard));

let (size, source_end_offset) = match size {
Some(size) => (size, source_offset + size),
None => (src_buffer.size - source_offset, src_buffer.size),
};

if size % wgt::COPY_BUFFER_ALIGNMENT != 0 {
return Err(TransferError::UnalignedCopySize(size).into());
}
Expand Down Expand Up @@ -628,7 +633,6 @@ impl Global {
}
}

let source_end_offset = source_offset + size;
let destination_end_offset = destination_offset + size;
if source_end_offset > src_buffer.size {
return Err(TransferError::BufferOverrun {
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 @@ -151,7 +151,7 @@ pub enum Command {
src_offset: wgt::BufferAddress,
dst: id::BufferId,
dst_offset: wgt::BufferAddress,
size: wgt::BufferAddress,
size: Option<wgt::BufferAddress>,
},
CopyBufferToTexture {
src: crate::command::TexelCopyBufferInfo,
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/api/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ impl CommandEncoder {
source_offset: BufferAddress,
destination: &Buffer,
destination_offset: BufferAddress,
copy_size: BufferAddress,
copy_size: impl Into<Option<BufferAddress>>,
) {
self.inner.copy_buffer_to_buffer(
&source.inner,
source_offset,
&destination.inner,
destination_offset,
copy_size,
copy_size.into(),
);
}

Expand Down
31 changes: 21 additions & 10 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2813,20 +2813,31 @@ impl dispatch::CommandEncoderInterface for WebCommandEncoder {
source_offset: crate::BufferAddress,
destination: &dispatch::DispatchBuffer,
destination_offset: crate::BufferAddress,
copy_size: crate::BufferAddress,
copy_size: Option<crate::BufferAddress>,
) {
let source = source.as_webgpu();
let destination = destination.as_webgpu();

self.inner
.copy_buffer_to_buffer_with_f64_and_f64_and_f64(
&source.inner,
source_offset as f64,
&destination.inner,
destination_offset as f64,
copy_size as f64,
)
.unwrap();
if let Some(size) = copy_size {
self.inner
.copy_buffer_to_buffer_with_f64_and_f64_and_f64(
&source.inner,
source_offset as f64,
&destination.inner,
destination_offset as f64,
size as f64,
)
.unwrap();
} else {
self.inner
.copy_buffer_to_buffer_with_f64_and_f64(
&source.inner,
source_offset as f64,
&destination.inner,
destination_offset as f64,
)
.unwrap();
}
}

fn copy_buffer_to_texture(
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_Gpu.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuAdapter.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuAdapterInfo.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuAddressMode.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuBindGroup.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuBlendFactor.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuBlendState.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu/webgpu_sys/gen_GpuBuffer.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading