Skip to content

Commit 7a1ac8d

Browse files
committed
Fix hardcoded alignment value and zero-init padding
1 parent 656c6fb commit 7a1ac8d

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

wgpu-core/src/device/resource.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ pub struct ExternalTextureParams {
173173

174174
const _: () =
175175
assert!(size_of::<ExternalTextureParams>() == wgpu_hal::EXTERNAL_TEXTURE_PARAMS_SIZE as usize);
176+
const EXTERNAL_TEXTURE_PARAMS_BUFFER_SIZE: usize = {
177+
size_of::<ExternalTextureParams>().next_multiple_of(wgpu_hal::UNIVERSAL_BUFFER_SIZE_ALIGNMENT as usize)
178+
};
176179

177180
impl ExternalTextureParams {
178181
pub fn from_desc<L>(desc: &wgt::ExternalTextureDescriptor<L>) -> Self {
@@ -587,7 +590,7 @@ impl Device {
587590
Some("(wgpu internal) default external texture params buffer"),
588591
instance_flags,
589592
),
590-
size: align_to(size_of::<ExternalTextureParams>() as _, 256),
593+
size: EXTERNAL_TEXTURE_PARAMS_BUFFER_SIZE as _,
591594
usage: wgt::BufferUses::COPY_DST | wgt::BufferUses::UNIFORM,
592595
memory_flags: hal::MemoryFlags::empty(),
593596
})
@@ -718,7 +721,8 @@ impl Device {
718721
_padding: Default::default(),
719722
};
720723
let mut staging_buffer =
721-
StagingBuffer::new(self, wgt::BufferSize::new(size_of_val(&data) as _).unwrap())?;
724+
StagingBuffer::new(self, wgt::BufferSize::new(EXTERNAL_TEXTURE_PARAMS_BUFFER_SIZE as _).unwrap())?;
725+
staging_buffer.write_zeros();
722726
staging_buffer.write(bytemuck::bytes_of(&data));
723727
let staging_buffer = staging_buffer.flush();
724728

wgpu-hal/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,13 @@ pub const MAX_MIP_LEVELS: u32 = 16;
330330
/// Size of a single occlusion/timestamp query, when copied into a buffer, in bytes.
331331
/// cbindgen:ignore
332332
pub const QUERY_SIZE: wgt::BufferAddress = 8;
333+
// The struct itself is defined in core, but we need to know the size.
334+
// There is a const assert for correctness located with the struct definition.
333335
pub const EXTERNAL_TEXTURE_PARAMS_SIZE: wgt::BufferAddress = 208;
336+
/// Universally safe value for buffer size alignment.
337+
///
338+
/// This is determined by `D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT`.
339+
pub const UNIVERSAL_BUFFER_SIZE_ALIGNMENT: wgt::BufferAddress = 256;
334340

335341
pub type Label<'a> = Option<&'a str>;
336342
pub type MemoryRange = Range<wgt::BufferAddress>;

0 commit comments

Comments
 (0)