Skip to content

Commit

Permalink
Merge pull request #157 from white-axe/align
Browse files Browse the repository at this point in the history
Fix `TargetAlignmentGreaterAndInputNotAligned` panic when loading maps
  • Loading branch information
melody-rs authored Dec 25, 2024
2 parents 6378442 + bcadbe9 commit fbe3f66
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
33 changes: 32 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ naga = "0.20.0" # Wgpu shader parser
naga_oil = "0.14.0" # An extension to naga. Allows combining and manipulating wgpu shaders (for example, through '#import' statements)
glutin = "0.31" # Cross-platform OpenGL context provider
glutin-winit = "0.4" # Bootstrapping helpers with winit for Glutin
aligned-vec = "0.6.1" # Implementations of `Vec` and `Box` with runtime-definable memory alignment

image = { version = "0.25.0", features = [
"png",
Expand All @@ -105,6 +106,7 @@ bytemuck = { version = "1.14.0", features = [
"derive",
"min_const_generics",
] } # Bit casting between data types
num-integer = "0.1.46" # Integer trait and implementations of some integer algorithms such as GCD and LCM

# * Logging and diagnostics * #
log = { version = "0.4", features = [
Expand Down
2 changes: 2 additions & 0 deletions crates/graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ luminol-egui-wgpu.workspace = true
wgpu.workspace = true
naga.workspace = true
naga_oil.workspace = true
aligned-vec.workspace = true

image = "0.24.7"

# * Mathematics * #
glam.workspace = true
bytemuck.workspace = true
num-integer.workspace = true

# * Logging and diagnostics * #
color-eyre.workspace = true
Expand Down
17 changes: 8 additions & 9 deletions crates/graphics/src/primitives/tiles/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// terms of the Steamworks API by Valve Corporation, the licensors of this
// Program grant you additional permission to convey the resulting work.

use num_integer::Integer;
use wgpu::util::DeviceExt;

use crate::{BindGroupLayoutBuilder, GraphicsState};
Expand All @@ -34,8 +35,8 @@ pub struct Display {

#[derive(Debug)]
struct LayerData {
data: Vec<u8>,
min_alignment_size: u32,
data: aligned_vec::AVec<u8, aligned_vec::RuntimeAlign>,
min_alignment_size: usize,
}

#[repr(C, align(16))]
Expand All @@ -47,11 +48,8 @@ pub struct Data {
}

impl Data {
fn aligned_size_of(min_alignment_size: u32) -> usize {
wgpu::util::align_to(
std::mem::size_of::<Self>(),
(min_alignment_size as usize).max(std::mem::align_of::<Data>()),
)
fn aligned_size_of(min_alignment_size: usize) -> usize {
wgpu::util::align_to(std::mem::size_of::<Self>(), min_alignment_size)
}
}

Expand Down Expand Up @@ -90,11 +88,12 @@ impl Display {
layers: usize,
) -> Self {
let limits = graphics_state.render_state.device.limits();
let min_alignment_size = limits.min_uniform_buffer_offset_alignment;
let min_alignment_size = (limits.min_uniform_buffer_offset_alignment as usize)
.lcm(&std::mem::align_of::<Data>());

let data_size = Data::aligned_size_of(min_alignment_size);
let mut layer_data = LayerData {
data: vec![0; data_size * layers],
data: aligned_vec::avec_rt![[min_alignment_size]| 0; data_size * layers],
min_alignment_size,
};

Expand Down

0 comments on commit fbe3f66

Please sign in to comment.