[rs] Fix excessive buffer padding (#795)

* Fix excessive buffer padding

* Prettify

* cargo fmt

* Simplify code

* Style
This commit is contained in:
Aron Granberg 2021-03-17 14:09:58 +01:00 committed by GitHub
parent 2bda1508f1
commit aa45e7668b

View File

@ -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,