Skip to content

Commit

Permalink
Use common types from objc2::foundation
Browse files Browse the repository at this point in the history
NSUInteger is now usize, so a bunch of `as u64` casts were removed
  • Loading branch information
madsmtm committed Sep 1, 2022
1 parent 2954cbe commit ad7d76a
Show file tree
Hide file tree
Showing 31 changed files with 70 additions and 85 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ private = []
mps = []

[dependencies]
# Branch: objc2. TODO: Remove this
core-graphics-types = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" }
bitflags = "1"
log = "0.4"
block2 = "=0.2.0-alpha.6"
Expand Down
1 change: 1 addition & 0 deletions examples/bindless/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// copied, modified, or distributed except according to those terms.

use metal::*;
use objc2::foundation::NSUInteger;
use objc2::rc::autoreleasepool;

const BINDLESS_TEXTURE_COUNT: NSUInteger = 100_000; // ~25Mb
Expand Down
4 changes: 2 additions & 2 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use winit::{
};

use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;

use objc2::foundation::CGSize;
use objc2::rc::autoreleasepool;
use objc2::runtime::Bool;

Expand Down Expand Up @@ -103,7 +103,7 @@ fn main() {

device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
(vertex_data.len() * mem::size_of::<AAPLVertex>()) as u64,
vertex_data.len() * mem::size_of::<AAPLVertex>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
};
Expand Down
6 changes: 3 additions & 3 deletions examples/compute/compute-argument-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ fn main() {

let buffer = device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
);

let sum = {
let data = [0u32];
device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down Expand Up @@ -77,7 +77,7 @@ fn main() {
};

let thread_group_size = MTLSize {
width: (data.len() as u64 + width) / width,
width: (data.len() + width) / width,
height: 1,
depth: 1,
};
Expand Down
6 changes: 3 additions & 3 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ fn main() {

let buffer = device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
);

let sum = {
let data = [0u32];
device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down Expand Up @@ -73,7 +73,7 @@ fn main() {
};

let thread_group_size = MTLSize {
width: (data.len() as u64 + width) / width,
width: (data.len() + width) / width,
height: 1,
depth: 1,
};
Expand Down
8 changes: 4 additions & 4 deletions examples/headless-render/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use metal::{
};
use png::ColorType;

const VIEW_WIDTH: u64 = 512;
const VIEW_HEIGHT: u64 = 512;
const TOTAL_BYTES: usize = (VIEW_WIDTH * VIEW_HEIGHT * 4) as usize;
const VIEW_WIDTH: usize = 512;
const VIEW_HEIGHT: usize = 512;
const TOTAL_BYTES: usize = VIEW_WIDTH * VIEW_HEIGHT * 4;

const VERTEX_SHADER: &'static str = "triangle_vertex";
const FRAGMENT_SHADER: &'static str = "triangle_fragment";
Expand Down Expand Up @@ -144,7 +144,7 @@ fn prepare_pipeline_state(device: &DeviceRef, library: &LibraryRef) -> RenderPip
fn create_vertex_buffer(device: &DeviceRef) -> Buffer {
device.new_buffer_with_data(
VERTEX_ATTRIBS.as_ptr() as *const _,
(VERTEX_ATTRIBS.len() * mem::size_of::<f32>()) as u64,
VERTEX_ATTRIBS.len() * mem::size_of::<f32>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
}
Expand Down
16 changes: 8 additions & 8 deletions examples/mps/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ fn main() {

let vertex_buffer = device.new_buffer_with_data(
vertices.as_ptr() as *const c_void,
(vertex_stride * vertices.len()) as u64,
vertex_stride * vertices.len(),
buffer_opts,
);

let index_buffer = device.new_buffer_with_data(
indices.as_ptr() as *const c_void,
(mem::size_of::<u32>() * indices.len()) as u64,
mem::size_of::<u32>() * indices.len(),
buffer_opts,
);

Expand All @@ -65,7 +65,7 @@ fn main() {
.expect("Failed to create acceleration structure");

acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
acceleration_structure.set_vertex_stride(vertex_stride as u64);
acceleration_structure.set_vertex_stride(vertex_stride);
acceleration_structure.set_index_buffer(Some(&index_buffer));
acceleration_structure.set_index_type(MPSDataType::UInt32);
acceleration_structure.set_triangle_count(1);
Expand All @@ -75,21 +75,21 @@ fn main() {
let ray_intersector =
RayIntersector::from_device(&device).expect("Failed to create ray intersector");

ray_intersector.set_ray_stride(mem::size_of::<Ray>() as u64);
ray_intersector.set_ray_stride(mem::size_of::<Ray>());
ray_intersector.set_ray_data_type(MPSRayDataType::OriginMinDistanceDirectionMaxDistance);
ray_intersector.set_intersection_stride(mem::size_of::<Intersection>() as u64);
ray_intersector.set_intersection_stride(mem::size_of::<Intersection>());
ray_intersector
.set_intersection_data_type(MPSIntersectionDataType::DistancePrimitiveIndexCoordinates);

// Create a buffer to hold generated rays and intersection results
let ray_count = 1024;
let ray_buffer = device.new_buffer(
(mem::size_of::<Ray>() * ray_count) as u64,
mem::size_of::<Ray>() * ray_count,
MTLResourceOptions::StorageModePrivate,
);

let intersection_buffer = device.new_buffer(
(mem::size_of::<Intersection>() * ray_count) as u64,
mem::size_of::<Intersection>() * ray_count,
MTLResourceOptions::StorageModePrivate,
);

Expand Down Expand Up @@ -119,7 +119,7 @@ fn main() {
0,
&intersection_buffer,
0,
ray_count as u64,
ray_count,
&acceleration_structure,
);

Expand Down
2 changes: 1 addition & 1 deletion examples/shader-dylib/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;

use metal::*;
use objc2::foundation::CGSize;
use objc2::rc::autoreleasepool;
use objc2::runtime::Bool;

Expand Down
2 changes: 0 additions & 2 deletions examples/texture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ edition = "2018"
bindgen = { version = "0.60", default-features = false, features = ["logging", "runtime", "which-rustfmt"] }

[dependencies]
# Branch: objc2. TODO: Remove this
core-graphics-types = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" }
cocoa = "0.24"
png = "0.17"
metal = { path = "../../" }
Expand Down
16 changes: 8 additions & 8 deletions examples/texture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::path::PathBuf;

use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;
use objc2::foundation::{CGSize, NSRange};
use objc2::rc::autoreleasepool;
use winit::dpi::LogicalSize;
use winit::event_loop::{ControlFlow, EventLoop};
Expand Down Expand Up @@ -40,7 +40,7 @@ fn main() {
let vertex_data = vertices();
let vertex_buffer = device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
(vertex_data.len() * std::mem::size_of::<TexturedVertex>()) as u64,
vertex_data.len() * std::mem::size_of::<TexturedVertex>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
);

Expand Down Expand Up @@ -113,7 +113,7 @@ fn create_texture_to_display(device: &Device) -> Texture {
let mut reader = decoder.read_info().unwrap();

let info = reader.info();
let (width, height) = (info.width as u64, info.height as u64);
let (width, height) = (info.width as usize, info.height as usize);

let mut buf = vec![0; reader.output_buffer_size()];
reader.next_frame(&mut buf).unwrap();
Expand Down Expand Up @@ -226,12 +226,12 @@ fn handle_window_event(
fn update_viewport_size_buffer(viewport_size_buffer: &Buffer, size: (u32, u32)) {
let contents = viewport_size_buffer.contents();
let viewport_size: [u32; 2] = [size.0, size.1];
let byte_count = (viewport_size.len() * std::mem::size_of::<u32>()) as usize;
let byte_count = viewport_size.len() * std::mem::size_of::<u32>();

unsafe {
std::ptr::copy(viewport_size.as_ptr(), contents as *mut u32, byte_count);
}
viewport_size_buffer.did_modify_range(metal::NSRange::new(0, byte_count as u64));
viewport_size_buffer.did_modify_range(NSRange::new(0, byte_count));
}

fn redraw(
Expand All @@ -255,9 +255,9 @@ fn redraw(
let encoder = command_buffer.new_render_command_encoder(&render_pass_descriptor);
encoder.set_render_pipeline_state(&pipeline_state);

encoder.set_vertex_buffer(VerticesBufferIdx as u64, Some(vertex_buffer), 0);
encoder.set_vertex_buffer(ViewportSizeBufferIdx as u64, Some(viewport_size_buffer), 0);
encoder.set_fragment_texture(TextureBaseColorIdx as u64, Some(texture_to_render));
encoder.set_vertex_buffer(VerticesBufferIdx as usize, Some(vertex_buffer), 0);
encoder.set_vertex_buffer(ViewportSizeBufferIdx as usize, Some(viewport_size_buffer), 0);
encoder.set_fragment_texture(TextureBaseColorIdx as usize, Some(texture_to_render));

encoder.draw_primitives(MTLPrimitiveType::Triangle, 0, 6);
encoder.end_encoding();
Expand Down
14 changes: 7 additions & 7 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// copied, modified, or distributed except according to those terms.

use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;

use metal::*;
use objc2::foundation::{CGSize, NSRange};
use objc2::rc::autoreleasepool;
use objc2::runtime::Bool;
use std::mem;
Expand Down Expand Up @@ -132,7 +132,7 @@ fn main() {

device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
(vertex_data.len() * mem::size_of::<f32>()) as u64,
vertex_data.len() * mem::size_of::<f32>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
};
Expand All @@ -156,7 +156,7 @@ fn main() {

let clear_rect_buffer = device.new_buffer_with_data(
clear_rect.as_ptr() as *const _,
mem::size_of::<ClearRect>() as u64,
mem::size_of::<ClearRect>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
);

Expand Down Expand Up @@ -199,13 +199,13 @@ fn main() {
std::ptr::copy(
vertex_data.as_ptr(),
p as *mut f32,
(vertex_data.len() * mem::size_of::<f32>()) as usize,
vertex_data.len() * mem::size_of::<f32>(),
);
}

vbuf.did_modify_range(crate::NSRange::new(
0 as u64,
(vertex_data.len() * mem::size_of::<f32>()) as u64,
vbuf.did_modify_range(NSRange::new(
0,
vertex_data.len() * mem::size_of::<f32>(),
));

let drawable = match layer.next_drawable() {
Expand Down
3 changes: 2 additions & 1 deletion src/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use super::{MTLTextureType, NSUInteger};
use super::MTLTextureType;
use objc2::foundation::NSUInteger;
use objc2::{Encode, Encoding};

#[repr(u64)]
Expand Down
5 changes: 3 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// copied, modified, or distributed except according to those terms.

use super::*;
use objc2::foundation::NSRange;

pub enum MTLBuffer {}

Expand All @@ -25,7 +26,7 @@ impl BufferRef {
unsafe { msg_send![self, contents] }
}

pub fn did_modify_range(&self, range: crate::NSRange) {
pub fn did_modify_range(&self, range: NSRange) {
unsafe { msg_send![self, didModifyRange: range] }
}

Expand Down Expand Up @@ -54,7 +55,7 @@ impl BufferRef {
unsafe { msg_send![self, newRemoteBufferViewForDevice: device] }
}

pub fn add_debug_marker(&self, name: &str, range: crate::NSRange) {
pub fn add_debug_marker(&self, name: &str, range: NSRange) {
unsafe {
let name = crate::nsstring_from_str(name);
msg_send![self, addDebugMarker:name range:range]
Expand Down
3 changes: 2 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;

use block2::{Block, ConcreteBlock};
use foreign_types::ForeignType;
use objc2::foundation::NSUInteger;
use objc2::runtime::Object;
use objc2::{Encode, Encoding};

Expand Down Expand Up @@ -1880,7 +1881,7 @@ impl DeviceRef {
}
}

pub fn new_buffer(&self, length: u64, options: MTLResourceOptions) -> Buffer {
pub fn new_buffer(&self, length: NSUInteger, options: MTLResourceOptions) -> Buffer {
unsafe {
msg_send![self, newBufferWithLength:length
options:options]
Expand Down
2 changes: 1 addition & 1 deletion src/drawable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use super::NSUInteger;
use objc2::foundation::NSUInteger;

pub enum MTLDrawable {}

Expand Down
3 changes: 2 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;

use std::ops::Range;

use objc2::foundation::{NSInteger, NSRange, NSUInteger};
use objc2::{Encode, Encoding};

#[repr(u64)]
Expand Down Expand Up @@ -904,7 +905,7 @@ impl BlitCommandEncoderRef {
unsafe { msg_send![self, synchronizeResource: resource] }
}

pub fn fill_buffer(&self, destination_buffer: &BufferRef, range: crate::NSRange, value: u8) {
pub fn fill_buffer(&self, destination_buffer: &BufferRef, range: NSRange, value: u8) {
unsafe {
msg_send![self,
fillBuffer: destination_buffer
Expand Down
1 change: 1 addition & 0 deletions src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use super::*;

use objc2::foundation::NSUInteger;
use objc2::{Encode, Encoding};

/// Only available on macos(10.15), ios(13.0)
Expand Down
4 changes: 3 additions & 1 deletion src/indirect_encoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

use objc2::foundation::{NSRange, NSUInteger};

bitflags! {
#[allow(non_upper_case_globals)]
pub struct MTLIndirectCommandType: NSUInteger {
Expand Down Expand Up @@ -99,7 +101,7 @@ impl IndirectCommandBufferRef {
unsafe { msg_send![self, indirectComputeCommandAtIndex: index] }
}

pub fn reset_with_range(&self, range: crate::NSRange) {
pub fn reset_with_range(&self, range: NSRange) {
unsafe { msg_send![self, resetWithRange: range] }
}
}
Expand Down
Loading

0 comments on commit ad7d76a

Please sign in to comment.