Skip to content

Commit

Permalink
Fix alignment on ios simulator (bevyengine#10178)
Browse files Browse the repository at this point in the history
# Objective

- Fix bevyengine#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
  • Loading branch information
mockersf authored and ameknite committed Nov 6, 2023
1 parent fea81c6 commit 0e3f7b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions crates/bevy_render/src/render_resource/uniform_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,16 @@ impl<T: ShaderType + WriteInto> DynamicUniformBuffer<T> {
device: &RenderDevice,
queue: &'a RenderQueue,
) -> Option<DynamicUniformBufferWriter<'a, T>> {
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())
Expand Down
5 changes: 5 additions & 0 deletions examples/mobile/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion examples/mobile/build_rust_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e3f7b9

Please sign in to comment.