Skip to content

Commit

Permalink
Make CGSize invisible to outside crates
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 1, 2023
1 parent d63a095 commit 96fb19c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions examples/mesh-shader/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
_ => (),
},
Expand Down
10 changes: 4 additions & 6 deletions examples/raytracing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|_| {
Expand All @@ -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);
}
_ => (),
},
Expand Down
32 changes: 16 additions & 16 deletions examples/raytracing/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -287,26 +289,26 @@ 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);
texture_descriptor.set_usage(MTLTextureUsage::ShaderRead);
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::<u32>() * size.width as usize,
size_of::<u32>() * width,
);
self.frame_index = 0;
}
Expand All @@ -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;
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions examples/shader-dylib/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/texture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
_ => (),
},
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] }
}

Expand Down

0 comments on commit 96fb19c

Please sign in to comment.