Skip to content

Commit 0350d1d

Browse files
committed
attempt to fix unaligned read in renderer
- #906
1 parent a4907aa commit 0350d1d

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

fyrox-impl/src/renderer/light.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::{
3636
ColorMask, CompareFunc, CullFace, DrawParameters, ElementRange, StencilAction, StencilFunc,
3737
StencilOp,
3838
},
39+
include_bytes_align_as,
3940
renderer::{
4041
bundle::{LightSourceKind, RenderDataBundleStorage},
4142
cache::{
@@ -204,7 +205,13 @@ impl DeferredLightRenderer {
204205
quality_defaults.csm_settings.precision,
205206
)?,
206207
// Use `test_write_brdf_lut` to re-generate the BRDF if needed.
207-
brdf_lut: make_brdf_lut(server, 256, include_bytes!("brdf_256x256_256samples.bin"))?,
208+
brdf_lut: {
209+
make_brdf_lut(
210+
server,
211+
256,
212+
include_bytes_align_as!(half::f16, "brdf_256x256_256samples.bin"),
213+
)?
214+
},
208215
})
209216
}
210217

fyrox-impl/src/renderer/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ pub fn write_brdf_lut(path: &Path, size: usize, sample_count: usize) -> std::io:
220220
Ok(())
221221
}
222222

223+
#[repr(C)] // guarantee 'bytes' comes after '_align'
224+
pub(crate) struct AlignedAs<Align, Bytes: ?Sized> {
225+
pub _align: [Align; 0],
226+
pub bytes: Bytes,
227+
}
228+
229+
// https://users.rust-lang.org/t/can-i-conveniently-compile-bytes-into-a-rust-program-with-a-specific-alignment/24049/2
230+
#[macro_export]
231+
macro_rules! include_bytes_align_as {
232+
($align_ty:ty, $path:literal) => {{
233+
// const block expression to encapsulate the static
234+
use $crate::renderer::utils::AlignedAs;
235+
236+
// this assignment is made possible by CoerceUnsized
237+
static ALIGNED: &AlignedAs<$align_ty, [u8]> = &AlignedAs {
238+
_align: [],
239+
bytes: *include_bytes!($path),
240+
};
241+
242+
&ALIGNED.bytes
243+
}};
244+
}
245+
223246
#[cfg(test)]
224247
mod test {
225248
use crate::renderer::utils::write_brdf_lut;

0 commit comments

Comments
 (0)