From 0e3f7b9cc5ac9b179c6847293483594002e7c450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 22 Oct 2023 00:19:46 +0200 Subject: [PATCH] Fix alignment on ios simulator (#10178) # Objective - Fix #10165 - On iOS simulator on apple silicon Macs, shader validation is going through the host, but device limits are reported for the device. They sometimes differ, and cause the validation to crash on something that should work ``` -[MTLDebugRenderCommandEncoder validateCommonDrawErrors:]:5775: failed assertion `Draw Errors Validation Fragment Function(fragment_): the offset into the buffer _naga_oil_mod_MJSXM6K7OBRHEOR2NVSXG2C7OZUWK527MJUW4ZDJNZTXG_memberfog that is bound at buffer index 6 must be a multiple of 256 but was set to 448. ``` ## Solution - Add a custom flag when building for the simulator and override the buffer alignment --- .../src/render_resource/uniform_buffer.rs | 12 ++++++++++-- examples/mobile/.cargo/config.toml | 5 +++++ examples/mobile/build_rust_deps.sh | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 examples/mobile/.cargo/config.toml diff --git a/crates/bevy_render/src/render_resource/uniform_buffer.rs b/crates/bevy_render/src/render_resource/uniform_buffer.rs index 95196568ff20ae..7e1b86869c4e05 100644 --- a/crates/bevy_render/src/render_resource/uniform_buffer.rs +++ b/crates/bevy_render/src/render_resource/uniform_buffer.rs @@ -277,8 +277,16 @@ impl DynamicUniformBuffer { device: &RenderDevice, queue: &'a RenderQueue, ) -> Option> { - let alignment = - AlignmentValue::new(device.limits().min_uniform_buffer_offset_alignment as u64); + let alignment = if cfg!(ios_simulator) { + // On iOS simulator on silicon macs, metal validation check that the host OS alignment + // is respected, but the device reports the correct value for iOS, which is smaller. + // Use the larger value. + // See https://github.com/bevyengine/bevy/pull/10178 - remove if it's not needed anymore. + AlignmentValue::new(256) + } else { + AlignmentValue::new(device.limits().min_uniform_buffer_offset_alignment as u64) + }; + let mut capacity = self.buffer.as_deref().map(wgpu::Buffer::size).unwrap_or(0); let size = alignment .round_up(T::min_size().get()) diff --git a/examples/mobile/.cargo/config.toml b/examples/mobile/.cargo/config.toml new file mode 100644 index 00000000000000..19a634093de056 --- /dev/null +++ b/examples/mobile/.cargo/config.toml @@ -0,0 +1,5 @@ +# Flag to notify the compiler we're building for the iOS simulator from an Apple silicon mac +# This needs some workarounds for now +# See https://github.com/bevyengine/bevy/pull/10178 - remove if it's not needed anymore. +[target.aarch64-apple-ios-sim] +rustflags = ["--cfg=ios_simulator"] diff --git a/examples/mobile/build_rust_deps.sh b/examples/mobile/build_rust_deps.sh index 2914faad5c0d0b..e232648a2be917 100755 --- a/examples/mobile/build_rust_deps.sh +++ b/examples/mobile/build_rust_deps.sh @@ -48,7 +48,7 @@ for arch in $ARCHS; do # Hardware iOS targets cargo rustc --crate-type staticlib --lib $RELFLAG --target aarch64-apple-ios else - # M1 iOS simulator -- currently in Nightly only and requires to build `libstd` + # M1 iOS simulator cargo rustc --crate-type staticlib --lib $RELFLAG --target aarch64-apple-ios-sim fi esac