From 96fb19cc1ec5d242a9064def467ff9b5153ca44a Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 1 Aug 2023 16:16:05 +0200 Subject: [PATCH] Make CGSize invisible to outside crates --- examples/circle/main.rs | 2 +- examples/mesh-shader/main.rs | 4 ++-- examples/raytracing/main.rs | 10 ++++------ examples/raytracing/renderer.rs | 32 ++++++++++++++++---------------- examples/shader-dylib/main.rs | 5 ++--- examples/texture/src/main.rs | 6 +++--- examples/window/main.rs | 4 ++-- src/lib.rs | 13 +++++++------ 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/examples/circle/main.rs b/examples/circle/main.rs index 21d3cbf..b0baebf 100644 --- a/examples/circle/main.rs +++ b/examples/circle/main.rs @@ -106,7 +106,7 @@ fn main() { } let draw_size = window.inner_size(); - layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64)); + layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64); let vbuf = { let vertex_data = create_vertex_points_for_circle(); diff --git a/examples/mesh-shader/main.rs b/examples/mesh-shader/main.rs index 717aab6..3a2f372 100644 --- a/examples/mesh-shader/main.rs +++ b/examples/mesh-shader/main.rs @@ -43,7 +43,7 @@ fn main() { } let draw_size = window.inner_size(); - layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64)); + layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64); let library_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("examples/mesh-shader/shaders.metallib"); @@ -75,7 +75,7 @@ fn main() { Event::WindowEvent { event, .. } => match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::Resized(size) => { - layer.set_drawable_size(CGSize::new(size.width as f64, size.height as f64)); + layer.set_drawable_size(size.width as f64, size.height as f64); } _ => (), }, diff --git a/examples/raytracing/main.rs b/examples/raytracing/main.rs index 70fb9cc..927406b 100644 --- a/examples/raytracing/main.rs +++ b/examples/raytracing/main.rs @@ -51,11 +51,10 @@ fn main() { } let draw_size = window.inner_size(); - let cg_size = CGSize::new(draw_size.width as f64, draw_size.height as f64); - layer.set_drawable_size(cg_size); + layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64); let mut renderer = renderer::Renderer::new(device); - renderer.window_resized(cg_size); + renderer.window_resized(draw_size.width as usize, draw_size.height as usize); events_loop.run(move |event, _, control_flow| { autoreleasepool(|_| { @@ -65,9 +64,8 @@ fn main() { Event::WindowEvent { event, .. } => match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::Resized(size) => { - let size = CGSize::new(size.width as f64, size.height as f64); - layer.set_drawable_size(size); - renderer.window_resized(size); + layer.set_drawable_size(size.width as f64, size.height as f64); + renderer.window_resized(size.width as usize, size.height as usize); } _ => (), }, diff --git a/examples/raytracing/renderer.rs b/examples/raytracing/renderer.rs index 84aa8f8..61613c4 100644 --- a/examples/raytracing/renderer.rs +++ b/examples/raytracing/renderer.rs @@ -64,7 +64,8 @@ pub struct Renderer { pub frame_index: usize, pub uniform_buffer_index: usize, pub uniform_buffer_offset: usize, - pub size: CGSize, + pub width: usize, + pub height: usize, semaphore: Semaphore, pub queue: CommandQueue, instance_buffer: Buffer, @@ -265,7 +266,8 @@ impl Renderer { frame_index: 0, uniform_buffer_index: 0, uniform_buffer_offset: 0, - size: CGSize::new(1024.0, 1024.0), + width: 1024, + height: 1024, semaphore: Semaphore::new((MAX_FRAMES_IN_FLIGHT - 2) as usize), instance_buffer, queue, @@ -287,10 +289,10 @@ impl Renderer { texture_descriptor } - pub fn window_resized(&mut self, size: CGSize) { - self.size = size; - let texture_descriptor = - Self::create_target_descriptor(size.width as usize, size.height as usize); + pub fn window_resized(&mut self, width: usize, height: usize) { + self.width = width; + self.height = height; + let texture_descriptor = Self::create_target_descriptor(width, height); self.accumulation_targets[0] = self.device.new_texture(&texture_descriptor); self.accumulation_targets[1] = self.device.new_texture(&texture_descriptor); texture_descriptor.set_pixel_format(MTLPixelFormat::R32Uint); @@ -298,15 +300,15 @@ impl Renderer { texture_descriptor.set_storage_mode(MTLStorageMode::Managed); self.random_texture = self.device.new_texture(&texture_descriptor); let mut rng = thread_rng(); - let mut random_values = vec![0u32; (size.width * size.height) as usize]; + let mut random_values = vec![0u32; width * height]; for v in &mut random_values { *v = rng.next_u32(); } self.random_texture.replace_region( - MTLRegion::new_2d(0, 0, size.width as usize, size.height as usize), + MTLRegion::new_2d(0, 0, width, height), 0, random_values.as_ptr() as *const c_void, - size_of::() * size.width as usize, + size_of::() * width, ); self.frame_index = 0; } @@ -333,15 +335,15 @@ impl Renderer { uniforms.camera.up = Vec4::from((up, 0.0)); let field_of_view = 45.0 * (std::f32::consts::PI / 180.0); - let aspect_ratio = self.size.width as f32 / self.size.height as f32; + let aspect_ratio = self.width as f32 / self.height as f32; let image_plane_height = f32::tan(field_of_view / 2.0); let image_plane_width = aspect_ratio * image_plane_height; uniforms.camera.right *= image_plane_width; uniforms.camera.up *= image_plane_height; - uniforms.width = self.size.width as u32; - uniforms.height = self.size.height as u32; + uniforms.width = self.width as u32; + uniforms.height = self.height as u32; uniforms.frame_index = self.frame_index as u32; self.frame_index += 1; @@ -365,12 +367,10 @@ impl Renderer { }) .copy(); command_buffer.add_completed_handler(&block); - let width = self.size.width as usize; - let height = self.size.height as usize; let threads_per_thread_group = MTLSize::new(8, 8, 1); let thread_groups = MTLSize::new( - (width + threads_per_thread_group.width - 1) / threads_per_thread_group.width, - (height + threads_per_thread_group.height - 1) / threads_per_thread_group.height, + (self.width + threads_per_thread_group.width - 1) / threads_per_thread_group.width, + (self.height + threads_per_thread_group.height - 1) / threads_per_thread_group.height, 1, ); let compute_encoder = command_buffer.new_compute_command_encoder(); diff --git a/examples/shader-dylib/main.rs b/examples/shader-dylib/main.rs index 03c259e..0c20601 100644 --- a/examples/shader-dylib/main.rs +++ b/examples/shader-dylib/main.rs @@ -48,7 +48,7 @@ impl App { view.setLayer(mem::transmute(layer.as_ref())); } let draw_size = window.inner_size(); - layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64)); + layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64); // compile dynamic lib shader let dylib_src_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -108,8 +108,7 @@ impl App { } fn resize(&mut self, width: u32, height: u32) { - self.layer - .set_drawable_size(CGSize::new(width as f64, height as f64)); + self.layer.set_drawable_size(width as f64, height as f64); self.width = width; self.height = height; } diff --git a/examples/texture/src/main.rs b/examples/texture/src/main.rs index b3a0292..50cd00c 100644 --- a/examples/texture/src/main.rs +++ b/examples/texture/src/main.rs @@ -167,10 +167,10 @@ fn get_window_layer(window: &Window, device: &Device) -> MetalLayer { // https://developer.apple.com/documentation/quartzcore/cametallayer/1478157-presentswithtransaction layer.set_presents_with_transaction(false); - layer.set_drawable_size(CGSize::new( + layer.set_drawable_size( window.inner_size().width as f64, window.inner_size().height as f64, - )); + ); unsafe { let view = window.ns_view() as cocoa_id; @@ -214,7 +214,7 @@ fn handle_window_event( match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::Resized(size) => { - layer.set_drawable_size(CGSize::new(size.width as f64, size.height as f64)); + layer.set_drawable_size(size.width as f64, size.height as f64); update_viewport_size_buffer(viewport_size_buffer, (size.width, size.height)); } diff --git a/examples/window/main.rs b/examples/window/main.rs index 8fed626..c202ede 100644 --- a/examples/window/main.rs +++ b/examples/window/main.rs @@ -106,7 +106,7 @@ fn main() { } let draw_size = window.inner_size(); - layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64)); + layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64); let library_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("examples/window/shaders.metallib"); @@ -167,7 +167,7 @@ fn main() { Event::WindowEvent { event, .. } => match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::Resized(size) => { - layer.set_drawable_size(CGSize::new(size.width as f64, size.height as f64)); + layer.set_drawable_size(size.width as f64, size.height as f64); } _ => (), }, diff --git a/src/lib.rs b/src/lib.rs index 90d8294..a2fe586 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,9 +29,8 @@ use std::{ }; use foreign_types::ForeignType; -pub(crate) use icrate::Foundation::{CGFloat, NSRange}; -pub use icrate::Foundation::{NSSize as CGSize}; -pub(crate) use objc2::encode::{Encode, Encoding, RefEncode}; +use icrate::Foundation::{CGFloat, CGSize, NSRange}; +use objc2::encode::{Encode, Encoding, RefEncode}; use objc2::runtime::{AnyObject, Bool, Protocol}; // Explicitly doesn't use `icrate::Foundation::NS[U]Integer`, so that the @@ -450,11 +449,13 @@ impl MetalLayerRef { unsafe { msg_send![self, setPixelFormat: pixel_format] } } - pub fn drawable_size(&self) -> CGSize { - unsafe { msg_send![self, drawableSize] } + pub fn drawable_size(&self) -> (f64, f64) { + let res: CGSize = unsafe { msg_send![self, drawableSize] }; + (res.width as _, res.height as _) } - pub fn set_drawable_size(&self, size: CGSize) { + pub fn set_drawable_size(&self, width: f64, height: f64) { + let size = CGSize::new(width as _, height as _); unsafe { msg_send![self, setDrawableSize: size] } }