Skip to content
Merged
Changes from 2 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
24 changes: 17 additions & 7 deletions onnxruntime/core/providers/webgpu/buffer_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ constexpr size_t NormalizeBufferSize(size_t size) {
return (size + 15) / 16 * 16;
}

// WebGPU requires that the copy size in CopyBufferToBuffer must be a multiple of 4 bytes.
constexpr size_t NormalizeCopySize(size_t size) {
return (size + 3) / 4 * 4;
}

void EnforceBufferUnmapped(WebGpuContext& context, WGPUBuffer buffer) {
if (context.ValidationMode() > ValidationMode::Basic) {
ORT_ENFORCE(wgpuBufferGetMapState(buffer) == WGPUBufferMapState_Unmapped, "Buffer is still mapped.");
Expand Down Expand Up @@ -450,21 +455,26 @@ void BufferManager::Upload(void* src, WGPUBuffer dst, size_t size) const {
}

// Otherwise, we need to use a staging buffer to upload data.
auto buffer_size = NormalizeBufferSize(size);
auto copy_size = NormalizeCopySize(size);

wgpu::BufferDescriptor desc{};
desc.size = buffer_size;
desc.size = copy_size;
desc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::MapWrite;
desc.mappedAtCreation = true;

auto staging_buffer = context_.Device().CreateBuffer(&desc);
mapped_data = staging_buffer.GetMappedRange();
memcpy(mapped_data, src, size);
// Zero padding bytes beyond the actual data to prevent copying garbage
// into the destination buffer when copy_size > size.
if (copy_size > size) {
memset(static_cast<uint8_t*>(mapped_data) + size, 0, copy_size - size);
}
Comment thread
qjia7 marked this conversation as resolved.
Outdated
staging_buffer.Unmap();

auto& command_encoder = context_.GetCommandEncoder();
context_.EndComputePass();
command_encoder.CopyBufferToBuffer(staging_buffer, 0, dst, 0, buffer_size);
command_encoder.CopyBufferToBuffer(staging_buffer, 0, dst, 0, copy_size);
context_.Flush(*this);
}

Expand All @@ -473,16 +483,16 @@ void BufferManager::MemCpy(WGPUBuffer src, WGPUBuffer dst, size_t size) const {
EnforceBufferUnmapped(context_, src);
EnforceBufferUnmapped(context_, dst);

auto buffer_size = NormalizeBufferSize(size);
auto copy_size = NormalizeCopySize(size);
auto src_size = static_cast<size_t>(wgpuBufferGetSize(src));
auto dst_size = static_cast<size_t>(wgpuBufferGetSize(dst));
ORT_ENFORCE(buffer_size <= src_size && buffer_size <= dst_size,
ORT_ENFORCE(copy_size <= src_size && copy_size <= dst_size,
"Source and destination buffers must have enough space for the copy operation. src_size=",
src_size, ", dst_size=", dst_size, ", copy_size=", buffer_size, ".");
src_size, ", dst_size=", dst_size, ", copy_size=", copy_size, ".");

auto& command_encoder = context_.GetCommandEncoder();
context_.EndComputePass();
command_encoder.CopyBufferToBuffer(src, 0, dst, 0, buffer_size);
command_encoder.CopyBufferToBuffer(src, 0, dst, 0, copy_size);
}

WGPUBuffer BufferManager::Create(size_t size, wgpu::BufferUsage usage) const {
Expand Down
Loading