Skip to content

Commit

Permalink
Make NSInteger and NSUInteger 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 3e29b91 commit d21bf30
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
4 changes: 2 additions & 2 deletions examples/bindless/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use metal::*;
use objc2::rc::autoreleasepool;

const BINDLESS_TEXTURE_COUNT: NSUInteger = 100_000; // ~25Mb
const BINDLESS_TEXTURE_COUNT: usize = 100_000; // ~25Mb

/// This example demonstrates:
/// - How to create a heap
Expand Down Expand Up @@ -93,7 +93,7 @@ fn main() {
// Encode textures to the argument buffer.
textures.iter().enumerate().for_each(|(index, texture)| {
// Offset encoder to a proper texture slot
let offset = index as NSUInteger * encoder.encoded_length();
let offset = index * encoder.encoded_length();
encoder.set_argument_buffer(&argument_buffer, offset);
encoder.set_texture(0, texture);
});
Expand Down
8 changes: 4 additions & 4 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ fn handle_render_pass_sample_buffer_attachment(
let sample_buffer_attachment_descriptor =
descriptor.sample_buffer_attachments().object_at(0).unwrap();
sample_buffer_attachment_descriptor.set_sample_buffer(&counter_sample_buffer);
sample_buffer_attachment_descriptor.set_start_of_vertex_sample_index(0 as NSUInteger);
sample_buffer_attachment_descriptor.set_end_of_vertex_sample_index(1 as NSUInteger);
sample_buffer_attachment_descriptor.set_start_of_fragment_sample_index(2 as NSUInteger);
sample_buffer_attachment_descriptor.set_end_of_fragment_sample_index(3 as NSUInteger);
sample_buffer_attachment_descriptor.set_start_of_vertex_sample_index(0);
sample_buffer_attachment_descriptor.set_end_of_vertex_sample_index(1);
sample_buffer_attachment_descriptor.set_start_of_fragment_sample_index(2);
sample_buffer_attachment_descriptor.set_end_of_fragment_sample_index(3);
}

fn handle_render_pass_color_attachment(descriptor: &RenderPassDescriptorRef, texture: &TextureRef) {
Expand Down
12 changes: 5 additions & 7 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const NUM_SAMPLES: usize = 2;
fn main() {
let num_elements = std::env::args()
.nth(1)
.map(|s| s.parse::<u32>().unwrap())
.map(|s| s.parse::<usize>().unwrap())
.unwrap_or(64 * 64);

autoreleasepool(|_| {
Expand Down Expand Up @@ -46,7 +46,7 @@ fn main() {
let num_threads = pipeline_state.thread_execution_width();

let thread_group_count = MTLSize {
width: ((num_elements as NSUInteger + num_threads) / num_threads),
width: (num_elements + num_threads) / num_threads,
height: 1,
depth: 1,
};
Expand All @@ -71,9 +71,7 @@ fn main() {
let ptr = sum.contents() as *mut u32;
println!("Compute shader sum: {}", unsafe { *ptr });

unsafe {
assert_eq!(num_elements, *ptr);
}
assert_eq!(num_elements, unsafe { *ptr } as usize);

handle_timestamps(&destination_buffer, cpu_start, cpu_end, gpu_start, gpu_end);
});
Expand Down Expand Up @@ -162,9 +160,9 @@ fn create_counter_sample_buffer(device: &Device) -> CounterSampleBuffer {

fn create_input_and_output_buffers(
device: &Device,
num_elements: u32,
num_elements: usize,
) -> (metal::Buffer, metal::Buffer) {
let data = vec![1u32; num_elements as usize];
let data = vec![1u32; num_elements];

let buffer = device.new_buffer_with_data(
unsafe { std::mem::transmute(data.as_ptr()) },
Expand Down
30 changes: 15 additions & 15 deletions examples/raytracing/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,35 @@ impl Geometry for TriangleGeometry {
self.index_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.indices.as_ptr()),
(self.indices.len() * size_of::<u16>()) as NSUInteger,
self.indices.len() * size_of::<u16>(),
get_managed_buffer_storage_mode(),
)
});
self.vertex_position_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.vertices.as_ptr()),
(self.vertices.len() * size_of::<Vec4>()) as NSUInteger,
self.vertices.len() * size_of::<Vec4>(),
get_managed_buffer_storage_mode(),
)
});
self.vertex_normal_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.normals.as_ptr()),
(self.normals.len() * size_of::<Vec4>()) as NSUInteger,
self.normals.len() * size_of::<Vec4>(),
get_managed_buffer_storage_mode(),
)
});
self.vertex_colour_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.colours.as_ptr()),
(self.colours.len() * size_of::<Vec4>()) as NSUInteger,
self.colours.len() * size_of::<Vec4>(),
get_managed_buffer_storage_mode(),
)
});
self.per_primitive_data_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.triangles.as_ptr()),
(self.triangles.len() * size_of::<Triangle>()) as NSUInteger,
self.triangles.len() * size_of::<Triangle>(),
get_managed_buffer_storage_mode(),
)
});
Expand Down Expand Up @@ -304,12 +304,12 @@ impl Geometry for TriangleGeometry {
descriptor.set_index_buffer(Some(self.index_buffer.as_ref().unwrap()));
descriptor.set_index_type(MTLIndexType::UInt16);
descriptor.set_vertex_buffer(Some(self.vertex_position_buffer.as_ref().unwrap()));
descriptor.set_vertex_stride(size_of::<Vec4>() as NSUInteger);
descriptor.set_triangle_count((self.indices.len() / 3) as NSUInteger);
descriptor.set_vertex_stride(size_of::<Vec4>());
descriptor.set_triangle_count(self.indices.len() / 3);
descriptor
.set_primitive_data_buffer(Some(self.per_primitive_data_buffer.as_ref().unwrap()));
descriptor.set_primitive_data_stride(size_of::<Triangle>() as NSUInteger);
descriptor.set_primitive_data_element_size(size_of::<Triangle>() as NSUInteger);
descriptor.set_primitive_data_stride(size_of::<Triangle>());
descriptor.set_primitive_data_element_size(size_of::<Triangle>());
From::from(descriptor)
}

Expand Down Expand Up @@ -366,7 +366,7 @@ impl Geometry for SphereGeometry {
self.sphere_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.spheres.as_ptr()),
(self.spheres.len() * size_of::<Sphere>()) as NSUInteger,
self.spheres.len() * size_of::<Sphere>(),
get_managed_buffer_storage_mode(),
)
});
Expand All @@ -384,7 +384,7 @@ impl Geometry for SphereGeometry {
self.bounding_box_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(bounding_boxes.as_ptr()),
(bounding_boxes.len() * size_of::<BoundingBox>()) as NSUInteger,
bounding_boxes.len() * size_of::<BoundingBox>(),
get_managed_buffer_storage_mode(),
)
});
Expand Down Expand Up @@ -415,10 +415,10 @@ impl Geometry for SphereGeometry {
fn get_geometry_descriptor(&self) -> AccelerationStructureGeometryDescriptor {
let descriptor = AccelerationStructureBoundingBoxGeometryDescriptor::descriptor();
descriptor.set_bounding_box_buffer(Some(self.bounding_box_buffer.as_ref().unwrap()));
descriptor.set_bounding_box_count(self.spheres.len() as NSUInteger);
descriptor.set_bounding_box_count(self.spheres.len());
descriptor.set_primitive_data_buffer(Some(&self.sphere_buffer.as_ref().unwrap()));
descriptor.set_primitive_data_stride(size_of::<Sphere>() as NSUInteger);
descriptor.set_primitive_data_element_size(size_of::<Sphere>() as NSUInteger);
descriptor.set_primitive_data_stride(size_of::<Sphere>());
descriptor.set_primitive_data_element_size(size_of::<Sphere>());
From::from(descriptor)
}

Expand All @@ -435,7 +435,7 @@ pub struct GeometryInstance {
pub geometry: Arc<dyn Geometry>,
pub transform: Mat4,
pub mask: u32,
pub index_in_scene: NSUInteger,
pub index_in_scene: usize,
}

#[repr(C)]
Expand Down
43 changes: 21 additions & 22 deletions examples/raytracing/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ struct Uniforms {
pub camera: Camera,
}

pub const MAX_FRAMES_IN_FLIGHT: NSUInteger = 3;
pub const ALIGNED_UNIFORMS_SIZE: NSUInteger = (size_of::<Uniforms>() as NSUInteger + 255) & !255;
pub const UNIFORM_BUFFER_SIZE: NSUInteger = MAX_FRAMES_IN_FLIGHT * ALIGNED_UNIFORMS_SIZE;
pub const MAX_FRAMES_IN_FLIGHT: usize = 3;
pub const ALIGNED_UNIFORMS_SIZE: usize = (size_of::<Uniforms>() + 255) & !255;
pub const UNIFORM_BUFFER_SIZE: usize = MAX_FRAMES_IN_FLIGHT * ALIGNED_UNIFORMS_SIZE;

#[derive(Clone)]
struct Semaphore {
Expand Down Expand Up @@ -61,9 +61,9 @@ pub struct Renderer {
pub instance_acceleration_structure: AccelerationStructure,
pub accumulation_targets: [Texture; 2],
pub random_texture: Texture,
pub frame_index: NSUInteger,
pub uniform_buffer_index: NSUInteger,
pub uniform_buffer_offset: NSUInteger,
pub frame_index: usize,
pub uniform_buffer_index: usize,
pub uniform_buffer_offset: usize,
pub size: CGSize,
semaphore: Semaphore,
pub queue: CommandQueue,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Renderer {
}
let resource_buffer = device.new_buffer_with_data(
resource_buffer_data.as_ptr() as *const c_void,
(resource_buffer_data.len() * size_of::<u64>()) as NSUInteger,
resource_buffer_data.len() * size_of::<u64>(),
get_managed_buffer_storage_mode(),
);
resource_buffer.set_label("resource buffer");
Expand All @@ -133,7 +133,7 @@ impl Renderer {
for i in 0..scene.geometries.len() {
let mesh = scene.geometries[i].as_ref();
let geometry_descriptor = mesh.get_geometry_descriptor();
geometry_descriptor.set_intersection_function_table_offset(i as NSUInteger);
geometry_descriptor.set_intersection_function_table_offset(i);
let geometry_descriptors = Array::from_owned_slice(&[geometry_descriptor]);
let accel_descriptor = PrimitiveAccelerationStructureDescriptor::descriptor();
accel_descriptor.set_geometry_descriptors(&geometry_descriptors);
Expand Down Expand Up @@ -173,8 +173,8 @@ impl Renderer {
}
let instance_buffer = device.new_buffer_with_data(
instance_descriptors.as_ptr() as *const c_void,
(size_of::<MTLAccelerationStructureInstanceDescriptor>()
* scene.geometry_instances.len()) as NSUInteger,
size_of::<MTLAccelerationStructureInstanceDescriptor>()
* scene.geometry_instances.len(),
get_managed_buffer_storage_mode(),
);
instance_buffer.set_label("instance buffer");
Expand All @@ -184,7 +184,7 @@ impl Renderer {
accel_descriptor.set_instanced_acceleration_structures(&Array::from_owned_slice(
&primitive_acceleration_structures,
));
accel_descriptor.set_instance_count(scene.geometry_instances.len() as NSUInteger);
accel_descriptor.set_instance_count(scene.geometry_instances.len());
accel_descriptor.set_instance_descriptor_buffer(&instance_buffer);
let accel_descriptor: AccelerationStructureDescriptor = From::from(accel_descriptor);
let instance_acceleration_structure =
Expand Down Expand Up @@ -218,8 +218,7 @@ impl Renderer {
&intersection_function_array,
);
let intersection_function_table_descriptor = IntersectionFunctionTableDescriptor::new();
intersection_function_table_descriptor
.set_function_count(scene.geometries.len() as NSUInteger);
intersection_function_table_descriptor.set_function_count(scene.geometries.len());
let intersection_function_table = raytracing_pipeline
.new_intersection_function_table_with_descriptor(
&intersection_function_table_descriptor,
Expand All @@ -231,7 +230,7 @@ impl Renderer {
let handle = raytracing_pipeline
.function_handle_with_function(intersection_function)
.unwrap();
intersection_function_table.set_function(handle, geometry_index as NSUInteger);
intersection_function_table.set_function(handle, geometry_index);
}
}
let render_descriptor = RenderPipelineDescriptor::new();
Expand Down Expand Up @@ -277,7 +276,7 @@ impl Renderer {
}
}

fn create_target_descriptor(width: NSUInteger, height: NSUInteger) -> TextureDescriptor {
fn create_target_descriptor(width: usize, height: usize) -> TextureDescriptor {
let texture_descriptor = TextureDescriptor::new();
texture_descriptor.set_pixel_format(MTLPixelFormat::RGBA32Float);
texture_descriptor.set_texture_type(MTLTextureType::D2);
Expand All @@ -291,7 +290,7 @@ impl Renderer {
pub fn window_resized(&mut self, size: CGSize) {
self.size = size;
let texture_descriptor =
Self::create_target_descriptor(size.width as NSUInteger, size.height as NSUInteger);
Self::create_target_descriptor(size.width as usize, size.height as usize);
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);
Expand All @@ -304,10 +303,10 @@ impl Renderer {
*v = rng.next_u32();
}
self.random_texture.replace_region(
MTLRegion::new_2d(0, 0, size.width as NSUInteger, size.height as NSUInteger),
MTLRegion::new_2d(0, 0, size.width as usize, size.height as usize),
0,
random_values.as_ptr() as *const c_void,
size_of::<u32>() as NSUInteger * size.width as NSUInteger,
size_of::<u32>() * size.width as usize,
);
self.frame_index = 0;
}
Expand Down Expand Up @@ -367,8 +366,8 @@ impl Renderer {
})
.copy();
command_buffer.add_completed_handler(&block);
let width = self.size.width as NSUInteger;
let height = self.size.height as NSUInteger;
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,
Expand Down Expand Up @@ -434,7 +433,7 @@ impl Renderer {
let command_buffer = queue.new_command_buffer();
let command_encoder = command_buffer.new_acceleration_structure_command_encoder();
let compacted_size_buffer = device.new_buffer(
size_of::<u32>() as NSUInteger,
size_of::<u32>() as usize,
MTLResourceOptions::StorageModeShared,
);
command_encoder.build_acceleration_structure(
Expand All @@ -452,7 +451,7 @@ impl Renderer {
command_buffer.commit();
command_buffer.wait_until_completed();
let compacted_size: *const u32 = unsafe { transmute(compacted_size_buffer.contents()) };
let compacted_size = unsafe { *compacted_size } as NSUInteger;
let compacted_size = unsafe { *compacted_size } as usize;
let compacted_acceleration_structure =
device.new_acceleration_structure_with_size(compacted_size);
let command_buffer = queue.new_command_buffer();
Expand Down
4 changes: 2 additions & 2 deletions examples/raytracing/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{ffi::c_void, mem::size_of, sync::Arc};
use glam::{Mat4, Vec3, Vec4};
use rand::{thread_rng, Rng};

use metal::{Buffer, Device, NSRange, NSUInteger};
use metal::{Buffer, Device, NSRange};

use super::{camera::Camera, geometry::*};

Expand Down Expand Up @@ -117,7 +117,7 @@ impl Scene {
}
let lights_buffer = device.new_buffer_with_data(
lights.as_ptr() as *const c_void,
(lights.len() * size_of::<AreaLight>()) as NSUInteger,
lights.len() * size_of::<AreaLight>(),
get_managed_buffer_storage_mode(),
);
lights_buffer.did_modify_range(NSRange::new(0, lights_buffer.length()));
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ use std::{
};

use foreign_types::ForeignType;
pub use icrate::Foundation::{CGFloat, NSInteger, NSRange, NSSize as CGSize, NSUInteger};
pub use icrate::Foundation::{CGFloat, NSRange, NSSize as CGSize};
pub(crate) use objc2::encode::{Encode, Encoding, RefEncode};
use objc2::runtime::{AnyObject, Bool, Protocol};

// Explicitly doesn't use `icrate::Foundation::NS[U]Integer`, so that the
// documentation will just show the simple Rust type.
pub(crate) type NSInteger = isize;
pub(crate) type NSUInteger = usize;

fn nsstring_as_str(nsstr: &AnyObject) -> &str {
let bytes = unsafe {
let bytes: *const std::os::raw::c_char = msg_send![nsstr, UTF8String];
Expand Down

0 comments on commit d21bf30

Please sign in to comment.