From aa45e7668bc167adf4cbc470ede31a2d6e5d1f61 Mon Sep 17 00:00:00 2001 From: Aron Granberg Date: Wed, 17 Mar 2021 14:09:58 +0100 Subject: [PATCH] [rs] Fix excessive buffer padding (#795) * Fix excessive buffer padding * Prettify * cargo fmt * Simplify code * Style --- wgpu/src/util/device.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wgpu/src/util/device.rs b/wgpu/src/util/device.rs index dff6423674..06775ee305 100644 --- a/wgpu/src/util/device.rs +++ b/wgpu/src/util/device.rs @@ -37,8 +37,12 @@ pub trait DeviceExt { impl DeviceExt for crate::Device { fn create_buffer_init(&self, descriptor: &BufferInitDescriptor<'_>) -> crate::Buffer { let unpadded_size = descriptor.contents.len() as crate::BufferAddress; - let padding = crate::COPY_BUFFER_ALIGNMENT - unpadded_size % crate::COPY_BUFFER_ALIGNMENT; - let padded_size = padding + unpadded_size; + // Valid vulkan usage is + // 1. buffer size must be a multiple of COPY_BUFFER_ALIGNMENT. + // 2. buffer size must be greater than 0. + // Therefore we round the value up to the nearest multiple, and ensure it's at least COPY_BUFFER_ALIGNMENT. + let align_mask = crate::COPY_BUFFER_ALIGNMENT - 1; + let padded_size = ((unpadded_size + align_mask) & !align_mask).max(crate::COPY_BUFFER_ALIGNMENT); let wgt_descriptor = crate::BufferDescriptor { label: descriptor.label,