Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unaligned slice::from_raw_parts in gles push contant handling. #6341

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216).
#### GLES / OpenGL

- Fix GL debug message callbacks not being properly cleaned up (causing UB). By @Imberflur in [#6114](https://github.com/gfx-rs/wgpu/pull/6114)
- Fix calling `slice::from_raw_parts` with unaligned pointers in push constant handling. By @Imberflur in [#6341](https://github.com/gfx-rs/wgpu/pull/6341)

#### WebGPU

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ arrayvec = "0.7"
bincode = "1"
bit-vec = "0.8"
bitflags = "2.6"
bytemuck = { version = "1.18", features = ["derive"] }
bytemuck = { version = "1.18" }
ErichDonGubler marked this conversation as resolved.
Show resolved Hide resolved
cfg_aliases = "0.1"
cfg-if = "1"
criterion = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ webgl = ["wgpu/webgl"]
webgpu = ["wgpu/webgpu"]

[dependencies]
bytemuck.workspace = true
bytemuck = { workspace = true, features = ["derive"] }
cfg-if.workspace = true
encase = { workspace = true, features = ["glam"] }
flume.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ vulkan = [
]
gles = [
"naga/glsl-out",
"dep:bytemuck",
"dep:glow",
"dep:glutin_wgl_sys",
"dep:khronos-egl",
Expand Down Expand Up @@ -126,6 +127,7 @@ rustc-hash.workspace = true
log.workspace = true

# backend: Gles
bytemuck = { workspace = true, optional = true }
glow = { workspace = true, optional = true }

[dependencies.wgt]
Expand Down
58 changes: 26 additions & 32 deletions wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,19 +1603,13 @@ impl super::Queue {
ref uniform,
offset,
} => {
// T must be POD
//
// This function is absolutely sketchy and we really should be using bytemuck.
unsafe fn get_data<T, const COUNT: usize>(data: &[u8], offset: u32) -> &[T; COUNT] {
fn get_data<T, const COUNT: usize>(data: &[u8], offset: u32) -> [T; COUNT]
where
[T; COUNT]: bytemuck::AnyBitPattern,
{
let data_required = size_of::<T>() * COUNT;

let raw = &data[(offset as usize)..][..data_required];

debug_assert_eq!(data_required, raw.len());

let slice: &[T] = unsafe { slice::from_raw_parts(raw.as_ptr().cast(), COUNT) };

slice.try_into().unwrap()
bytemuck::pod_read_unaligned(raw)
}

let location = Some(&uniform.location);
Expand All @@ -1625,86 +1619,86 @@ impl super::Queue {
// --- Float 1-4 Component ---
//
naga::TypeInner::Scalar(naga::Scalar::F32) => {
let data = unsafe { get_data::<f32, 1>(data_bytes, offset)[0] };
let data = get_data::<f32, 1>(data_bytes, offset)[0];
unsafe { gl.uniform_1_f32(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Bi,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 2>(data_bytes, offset) };
let data = &get_data::<f32, 2>(data_bytes, offset);
unsafe { gl.uniform_2_f32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Tri,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 3>(data_bytes, offset) };
let data = &get_data::<f32, 3>(data_bytes, offset);
unsafe { gl.uniform_3_f32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Quad,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 4>(data_bytes, offset) };
let data = &get_data::<f32, 4>(data_bytes, offset);
unsafe { gl.uniform_4_f32_slice(location, data) };
}

//
// --- Int 1-4 Component ---
//
naga::TypeInner::Scalar(naga::Scalar::I32) => {
let data = unsafe { get_data::<i32, 1>(data_bytes, offset)[0] };
let data = get_data::<i32, 1>(data_bytes, offset)[0];
unsafe { gl.uniform_1_i32(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Bi,
scalar: naga::Scalar::I32,
} => {
let data = unsafe { get_data::<i32, 2>(data_bytes, offset) };
let data = &get_data::<i32, 2>(data_bytes, offset);
unsafe { gl.uniform_2_i32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Tri,
scalar: naga::Scalar::I32,
} => {
let data = unsafe { get_data::<i32, 3>(data_bytes, offset) };
let data = &get_data::<i32, 3>(data_bytes, offset);
unsafe { gl.uniform_3_i32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Quad,
scalar: naga::Scalar::I32,
} => {
let data = unsafe { get_data::<i32, 4>(data_bytes, offset) };
let data = &get_data::<i32, 4>(data_bytes, offset);
unsafe { gl.uniform_4_i32_slice(location, data) };
}

//
// --- Uint 1-4 Component ---
//
naga::TypeInner::Scalar(naga::Scalar::U32) => {
let data = unsafe { get_data::<u32, 1>(data_bytes, offset)[0] };
let data = get_data::<u32, 1>(data_bytes, offset)[0];
unsafe { gl.uniform_1_u32(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Bi,
scalar: naga::Scalar::U32,
} => {
let data = unsafe { get_data::<u32, 2>(data_bytes, offset) };
let data = &get_data::<u32, 2>(data_bytes, offset);
unsafe { gl.uniform_2_u32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Tri,
scalar: naga::Scalar::U32,
} => {
let data = unsafe { get_data::<u32, 3>(data_bytes, offset) };
let data = &get_data::<u32, 3>(data_bytes, offset);
unsafe { gl.uniform_3_u32_slice(location, data) };
}
naga::TypeInner::Vector {
size: naga::VectorSize::Quad,
scalar: naga::Scalar::U32,
} => {
let data = unsafe { get_data::<u32, 4>(data_bytes, offset) };
let data = &get_data::<u32, 4>(data_bytes, offset);
unsafe { gl.uniform_4_u32_slice(location, data) };
}

Expand All @@ -1716,7 +1710,7 @@ impl super::Queue {
rows: naga::VectorSize::Bi,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 4>(data_bytes, offset) };
let data = &get_data::<f32, 4>(data_bytes, offset);
unsafe { gl.uniform_matrix_2_f32_slice(location, false, data) };
}
naga::TypeInner::Matrix {
Expand All @@ -1725,7 +1719,7 @@ impl super::Queue {
scalar: naga::Scalar::F32,
} => {
// repack 2 vec3s into 6 values.
let unpacked_data = unsafe { get_data::<f32, 8>(data_bytes, offset) };
let unpacked_data = &get_data::<f32, 8>(data_bytes, offset);
#[rustfmt::skip]
let packed_data = [
unpacked_data[0], unpacked_data[1], unpacked_data[2],
Expand All @@ -1738,7 +1732,7 @@ impl super::Queue {
rows: naga::VectorSize::Quad,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 8>(data_bytes, offset) };
let data = &get_data::<f32, 8>(data_bytes, offset);
unsafe { gl.uniform_matrix_2x4_f32_slice(location, false, data) };
}

Expand All @@ -1750,7 +1744,7 @@ impl super::Queue {
rows: naga::VectorSize::Bi,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 6>(data_bytes, offset) };
let data = &get_data::<f32, 6>(data_bytes, offset);
unsafe { gl.uniform_matrix_3x2_f32_slice(location, false, data) };
}
naga::TypeInner::Matrix {
Expand All @@ -1759,7 +1753,7 @@ impl super::Queue {
scalar: naga::Scalar::F32,
} => {
// repack 3 vec3s into 9 values.
let unpacked_data = unsafe { get_data::<f32, 12>(data_bytes, offset) };
let unpacked_data = &get_data::<f32, 12>(data_bytes, offset);
#[rustfmt::skip]
let packed_data = [
unpacked_data[0], unpacked_data[1], unpacked_data[2],
Expand All @@ -1773,7 +1767,7 @@ impl super::Queue {
rows: naga::VectorSize::Quad,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 12>(data_bytes, offset) };
let data = &get_data::<f32, 12>(data_bytes, offset);
unsafe { gl.uniform_matrix_3x4_f32_slice(location, false, data) };
}

Expand All @@ -1785,7 +1779,7 @@ impl super::Queue {
rows: naga::VectorSize::Bi,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 8>(data_bytes, offset) };
let data = &get_data::<f32, 8>(data_bytes, offset);
unsafe { gl.uniform_matrix_4x2_f32_slice(location, false, data) };
}
naga::TypeInner::Matrix {
Expand All @@ -1794,7 +1788,7 @@ impl super::Queue {
scalar: naga::Scalar::F32,
} => {
// repack 4 vec3s into 12 values.
let unpacked_data = unsafe { get_data::<f32, 16>(data_bytes, offset) };
let unpacked_data = &get_data::<f32, 16>(data_bytes, offset);
#[rustfmt::skip]
let packed_data = [
unpacked_data[0], unpacked_data[1], unpacked_data[2],
Expand All @@ -1809,7 +1803,7 @@ impl super::Queue {
rows: naga::VectorSize::Quad,
scalar: naga::Scalar::F32,
} => {
let data = unsafe { get_data::<f32, 16>(data_bytes, offset) };
let data = &get_data::<f32, 16>(data_bytes, offset);
unsafe { gl.uniform_matrix_4_f32_slice(location, false, data) };
}
_ => panic!("Unsupported uniform datatype: {:?}!", uniform.ty),
Expand Down