Skip to content

Commit

Permalink
[rs] Skip mapping of zero sized buffers in create_buffer_init to avoi…
Browse files Browse the repository at this point in the history
…d a panic and avoid creating an unused padded buffer
  • Loading branch information
Imberflur committed May 4, 2021
1 parent e83e4ff commit bbc5ba3
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions wgpu/src/util/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,43 @@ 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;
// 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,
size: padded_size,
usage: descriptor.usage,
mapped_at_creation: true,
};

let buffer = self.create_buffer(&wgt_descriptor);
buffer
.slice(..unpadded_size)
.get_mapped_range_mut()
.copy_from_slice(descriptor.contents);
buffer.unmap();
buffer
// Skip mapping if the buffer is zero sized
if descriptor.contents.is_empty() {
let wgt_descriptor = crate::BufferDescriptor {
label: descriptor.label,
size: 0,
usage: descriptor.usage,
mapped_at_creation: false,
};

self.create_buffer(&wgt_descriptor)
} else {
let unpadded_size = descriptor.contents.len() as crate::BufferAddress;
// 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,
size: padded_size,
usage: descriptor.usage,
mapped_at_creation: true,
};

let buffer = self.create_buffer(&wgt_descriptor);

buffer
.slice(..unpadded_size)
.get_mapped_range_mut()
.copy_from_slice(descriptor.contents);
buffer.unmap();

buffer
}
}

fn create_texture_with_data(
Expand Down

0 comments on commit bbc5ba3

Please sign in to comment.