Skip to content
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
59 changes: 59 additions & 0 deletions native/ios/Cargo.lock

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

49 changes: 47 additions & 2 deletions native/ios/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ unsafe impl RefEncode for CGRect {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

#[repr(C)]
#[derive(Copy, Clone)]
struct UIEdgeInsets {
top: f64,
left: f64,
bottom: f64,
right: f64,
}

unsafe impl Encode for UIEdgeInsets {
const ENCODING: Encoding = Encoding::Struct(
"UIEdgeInsets",
&[Encoding::Double, Encoding::Double, Encoding::Double, Encoding::Double],
);
}
unsafe impl RefEncode for UIEdgeInsets {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

// ============================================================
// ObjC runtime
// ============================================================
Expand Down Expand Up @@ -444,7 +463,10 @@ unsafe extern "C" fn scene_will_connect(
.unwrap_or(surface_caps.formats[0]);

let surface_config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
// COPY_SRC (where the surface offers it, as Metal does): bloom_take_screenshot reads
// the swapchain back, and without the flag the copy is a validation error that aborts.
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| (surface_caps.usages & wgpu::TextureUsages::COPY_SRC),
format,
width: pixel_width,
height: pixel_height,
Expand Down Expand Up @@ -609,7 +631,10 @@ pub unsafe extern "C" fn perry_scene_will_connect(scene: *const c_void) {
.unwrap_or(surface_caps.formats[0]);

let surface_config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
// COPY_SRC (where the surface offers it, as Metal does): bloom_take_screenshot reads
// the swapchain back, and without the flag the copy is a validation error that aborts.
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| (surface_caps.usages & wgpu::TextureUsages::COPY_SRC),
format,
width: pixel_width,
height: pixel_height,
Expand Down Expand Up @@ -968,6 +993,26 @@ pub extern "C" fn bloom_begin_drawing() {
let view_ptr = Retained::as_ptr(view);
let layer: Retained<AnyObject> = msg_send![&*view_ptr, layer];
let view_bounds: CGRect = msg_send![&*view_ptr, bounds];
// The view's own display scale and safe area, not UIScreen.mainScreen's:
// on a device with two displays (iPhone Duo) the main screen is
// ambiguous, and the safe area follows rotation, split view and the
// device opening or closing. Published for getDisplayScale() /
// getSafeAreaInset(), in the pixels getScreenWidth() reports.
let traits: *const AnyObject = msg_send![&*view_ptr, traitCollection];
if !traits.is_null() {
let display_scale: f64 = msg_send![&*traits, displayScale];
if display_scale.is_finite() && display_scale > 0.0 {
SCREEN_SCALE = display_scale;
}
}
let insets: UIEdgeInsets = msg_send![&*view_ptr, safeAreaInsets];
bloom_shared::display::set_display_scale(SCREEN_SCALE);
bloom_shared::display::set_safe_area_insets(
insets.top * SCREEN_SCALE,
insets.right * SCREEN_SCALE,
insets.bottom * SCREEN_SCALE,
insets.left * SCREEN_SCALE,
);
let scale = SCREEN_SCALE;
let pw = (view_bounds.size.width * scale) as u32;
let ph = (view_bounds.size.height * scale) as u32;
Expand Down
10 changes: 9 additions & 1 deletion native/shared/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,17 @@ fn build_jolt() {
// 2. The Jolt build is expensive and identical across every cargo target
// hash — caching it once per profile lets `cargo clean` not nuke a
// multi-minute compile.
// Apple simulators share target_os/target_arch with the device (only the
// "sim" ABI tells them apart), so they need their own cache: a shared one
// hands simulator objects to a device link and ld rejects the platform.
let sim_suffix = if std::env::var("CARGO_CFG_TARGET_ABI").as_deref() == Ok("sim") {
"-sim"
} else {
""
};
let dst = shim_dir
.join("build")
.join(format!("{}-{}", target_os, target_arch));
.join(format!("{}-{}{}", target_os, target_arch, sim_suffix));

let lib_ext = if target_os == "windows" { "lib" } else { "a" };
let lib_prefix = if target_os == "windows" { "" } else { "lib" };
Expand Down
59 changes: 59 additions & 0 deletions native/shared/src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! What the platform knows about the display beyond the window's size: its
//! scale (physical pixels per point) and the safe-area insets (the part of the
//! window the Dynamic Island, a notch, rounded corners, the home indicator or
//! a foldable's status-bar column cover).
//!
//! Platforms that have these values report them every frame (iOS from the
//! view's trait collection and `safeAreaInsets`, which follow rotation, split
//! view and an iPhone Duo opening or closing); everywhere else the scale is 1
//! and the insets are 0. The insets are in the units of `getScreenWidth()`
//! (pixels on iOS, points on macOS — see EN-024).
//!
//! Plain atomics, not EngineState: iOS writes them from the frame, the FFI
//! reads them from the game thread, and neither needs the engine to exist.
use std::sync::atomic::{AtomicU64, Ordering};

static SCALE: AtomicU64 = AtomicU64::new(0);
static INSETS: [AtomicU64; 4] = [AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0)];

/// Edges, in the order `getSafeAreaInset(edge)` takes them.
pub const TOP: usize = 0;
pub const RIGHT: usize = 1;
pub const BOTTOM: usize = 2;
pub const LEFT: usize = 3;

pub fn set_display_scale(scale: f64) {
if scale.is_finite() && scale > 0.0 {
SCALE.store(scale.to_bits(), Ordering::Relaxed);
}
}

/// Physical pixels per point; 1 where the platform doesn't report it.
pub fn display_scale() -> f64 {
let v = f64::from_bits(SCALE.load(Ordering::Relaxed));
if v > 0.0 { v } else { 1.0 }
}

/// tvOS: whether the Siri Remote's Menu button leaves the app (hands the press to UIKit, which
/// goes back to the Home screen) instead of reaching the game. Apple requires that at the app's
/// top level (its title screen); everywhere else Menu is the game's "back".
static MENU_EXITS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

pub fn set_menu_exits(on: bool) {
MENU_EXITS.store(on, Ordering::Relaxed);
}

pub fn menu_exits() -> bool {
MENU_EXITS.load(Ordering::Relaxed)
}

pub fn set_safe_area_insets(top: f64, right: f64, bottom: f64, left: f64) {
for (slot, v) in INSETS.iter().zip([top, right, bottom, left]) {
slot.store(if v.is_finite() && v > 0.0 { v } else { 0.0 }.to_bits(), Ordering::Relaxed);
}
}

/// One edge's inset (TOP, RIGHT, BOTTOM, LEFT); 0 for an unknown edge.
pub fn safe_area_inset(edge: usize) -> f64 {
INSETS.get(edge).map_or(0.0, |v| f64::from_bits(v.load(Ordering::Relaxed)))
}
23 changes: 23 additions & 0 deletions native/shared/src/ffi_core/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ macro_rules! __bloom_ffi_game_loop {
$crate::ffi::guard("bloom_get_fps", move || engine().get_fps())
}

// bloom_get_display_scale [source: ios]
#[no_mangle]
pub extern "C" fn bloom_get_display_scale() -> f64 {
$crate::ffi::guard("bloom_get_display_scale", move || $crate::display::display_scale())
}

// bloom_get_safe_area_inset [source: ios]
#[no_mangle]
pub extern "C" fn bloom_get_safe_area_inset(edge: f64) -> f64 {
$crate::ffi::guard("bloom_get_safe_area_inset", move || {
if edge >= 0.0 { $crate::display::safe_area_inset(edge as usize) } else { 0.0 }
})
}

// bloom_set_menu_button_exits [source: tvos]
#[no_mangle]
pub extern "C" fn bloom_set_menu_button_exits(on: f64) -> f64 {
$crate::ffi::guard("bloom_set_menu_button_exits", move || {
$crate::display::set_menu_exits(on != 0.0);
1.0
})
}

// bloom_get_screen_width [source: macos]
#[no_mangle]
pub extern "C" fn bloom_get_screen_width() -> f64 {
Expand Down
8 changes: 8 additions & 0 deletions native/shared/src/ffi_core/visual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,14 @@ macro_rules! __bloom_ffi_visual {
})
}

// bloom_set_camera_clip_planes [source: macos]
#[no_mangle]
pub extern "C" fn bloom_set_camera_clip_planes(near: f64, far: f64) -> f64 {
$crate::ffi::guard_applied("bloom_set_camera_clip_planes", move || {
engine().renderer.set_camera_clip_planes(near as f32, far as f32);
})
}

// bloom_set_taa_enabled [source: macos]
#[no_mangle]
pub extern "C" fn bloom_set_taa_enabled(on: f64) -> f64 {
Expand Down
34 changes: 29 additions & 5 deletions native/shared/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub struct InputState {
mouse_down: [bool; MAX_MOUSE_BUTTONS],
mouse_released: [bool; MAX_MOUSE_BUTTONS],
prev_mouse_down: [bool; MAX_MOUSE_BUTTONS],
// A press / release seen since the last begin_frame. iOS delivers touches
// (synthesized into mouse button 0) on the UI thread while the game thread
// renders, so a whole tap can start and end between two begin_frames;
// these latches keep its edges for the next frame to report.
mouse_press_latch: [bool; MAX_MOUSE_BUTTONS],
mouse_release_latch: [bool; MAX_MOUSE_BUTTONS],

// Gamepad
pub gamepad_available: bool,
Expand Down Expand Up @@ -131,6 +137,8 @@ impl InputState {
mouse_down: [false; MAX_MOUSE_BUTTONS],
mouse_released: [false; MAX_MOUSE_BUTTONS],
prev_mouse_down: [false; MAX_MOUSE_BUTTONS],
mouse_press_latch: [false; MAX_MOUSE_BUTTONS],
mouse_release_latch: [false; MAX_MOUSE_BUTTONS],
gamepad_available: false,
gamepad_axes: [0.0; MAX_GAMEPAD_AXES],
gamepad_buttons_down: [false; MAX_GAMEPAD_BUTTONS],
Expand Down Expand Up @@ -190,9 +198,22 @@ impl InputState {
self.repeat_pending[i] = false;
}
for i in 0..MAX_MOUSE_BUTTONS {
self.mouse_pressed[i] = self.mouse_down[i] && !self.prev_mouse_down[i];
self.mouse_released[i] = !self.mouse_down[i] && self.prev_mouse_down[i];
self.mouse_pressed[i] =
(self.mouse_down[i] && !self.prev_mouse_down[i]) || self.mouse_press_latch[i];
self.mouse_released[i] =
(!self.mouse_down[i] && self.prev_mouse_down[i]) || self.mouse_release_latch[i];
self.mouse_press_latch[i] = false;
self.mouse_release_latch[i] = false;
}
// The state these edges were computed from is the previous state for
// the next frame. Taking it here rather than in end_frame matters when
// input arrives from another thread (iOS touches): a change landing
// between begin_frame and end_frame was otherwise copied into prev_*
// without any frame ever reporting its edge, so a released finger never
// produced a release and a tap never produced a click.
self.prev_keys_down = self.keys_down;
self.prev_mouse_down = self.mouse_down;
self.prev_gamepad_buttons = self.gamepad_buttons_down;
for i in 0..MAX_GAMEPAD_BUTTONS {
self.gamepad_buttons_pressed[i] =
self.gamepad_buttons_down[i] && !self.prev_gamepad_buttons[i];
Expand All @@ -202,9 +223,6 @@ impl InputState {
}

pub fn end_frame(&mut self) {
self.prev_keys_down = self.keys_down;
self.prev_mouse_down = self.mouse_down;
self.prev_gamepad_buttons = self.gamepad_buttons_down;
self.prev_mouse_x = self.mouse_x;
self.prev_mouse_y = self.mouse_y;
let mut flushed = false;
Expand Down Expand Up @@ -308,11 +326,17 @@ impl InputState {

pub fn set_mouse_button_down(&mut self, button: usize) {
if button < MAX_MOUSE_BUTTONS {
if !self.mouse_down[button] {
self.mouse_press_latch[button] = true;
}
self.mouse_down[button] = true;
}
}
pub fn set_mouse_button_up(&mut self, button: usize) {
if button < MAX_MOUSE_BUTTONS {
if self.mouse_down[button] {
self.mouse_release_latch[button] = true;
}
self.mouse_down[button] = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions native/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod audio;
pub mod cooked_scene_store;
#[cfg(all(feature = "image-extras", not(target_arch = "wasm32")))]
pub mod cooked_texture_store;
pub mod display;
pub mod ffi;
#[cfg(not(target_arch = "wasm32"))]
pub mod ffi_core;
Expand Down
Loading