Skip to content

Commit

Permalink
Make NSRange 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 d21bf30 commit 7f191d1
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 68 deletions.
7 changes: 1 addition & 6 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ fn resolve_samples_into_buffer(
destination_buffer: &BufferRef,
) {
let blit_encoder = command_buffer.new_blit_command_encoder();
blit_encoder.resolve_counters(
&counter_sample_buffer,
NSRange::new(0, 4),
&destination_buffer,
0,
);
blit_encoder.resolve_counters(&counter_sample_buffer, 0..4, &destination_buffer, 0);
blit_encoder.end_encoding();
}

Expand Down
7 changes: 1 addition & 6 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ fn resolve_samples_into_buffer(
destination_buffer: &BufferRef,
) {
let blit_encoder = command_buffer.new_blit_command_encoder();
blit_encoder.resolve_counters(
counter_sample_buffer,
crate::NSRange::new(0, NUM_SAMPLES),
destination_buffer,
0,
);
blit_encoder.resolve_counters(counter_sample_buffer, 0..NUM_SAMPLES, destination_buffer, 0);
blit_encoder.end_encoding();
}

Expand Down
35 changes: 7 additions & 28 deletions examples/raytracing/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,38 +235,23 @@ impl Geometry for TriangleGeometry {
self.index_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.index_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.index_buffer.as_ref().unwrap().length());
self.vertex_position_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.vertex_position_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.vertex_position_buffer.as_ref().unwrap().length());
self.vertex_normal_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.vertex_normal_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.vertex_normal_buffer.as_ref().unwrap().length());
self.vertex_colour_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.vertex_colour_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.vertex_colour_buffer.as_ref().unwrap().length());
self.per_primitive_data_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.per_primitive_data_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.per_primitive_data_buffer.as_ref().unwrap().length());

self.index_buffer
.as_ref()
Expand Down Expand Up @@ -395,17 +380,11 @@ impl Geometry for SphereGeometry {
self.sphere_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.sphere_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.sphere_buffer.as_ref().unwrap().length());
self.bounding_box_buffer
.as_ref()
.unwrap()
.did_modify_range(NSRange::new(
0,
self.bounding_box_buffer.as_ref().unwrap().length(),
));
.did_modify_range(0..self.bounding_box_buffer.as_ref().unwrap().length());
}

fn clear(&mut self) {
Expand Down
11 changes: 5 additions & 6 deletions examples/raytracing/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Renderer {
get_managed_buffer_storage_mode(),
);
resource_buffer.set_label("resource buffer");
resource_buffer.did_modify_range(NSRange::new(0, resource_buffer.length()));
resource_buffer.did_modify_range(0..resource_buffer.length());

let mut primitive_acceleration_structures = Vec::new();
for i in 0..scene.geometries.len() {
Expand Down Expand Up @@ -178,7 +178,7 @@ impl Renderer {
get_managed_buffer_storage_mode(),
);
instance_buffer.set_label("instance buffer");
instance_buffer.did_modify_range(NSRange::new(0, instance_buffer.length()));
instance_buffer.did_modify_range(0..instance_buffer.length());

let accel_descriptor = InstanceAccelerationStructureDescriptor::descriptor();
accel_descriptor.set_instanced_acceleration_structures(&Array::from_owned_slice(
Expand Down Expand Up @@ -348,10 +348,9 @@ impl Renderer {

uniforms.light_count = self.scene.lights.len() as u32;

self.uniform_buffer.did_modify_range(NSRange {
location: self.uniform_buffer_offset,
length: ALIGNED_UNIFORMS_SIZE,
});
self.uniform_buffer.did_modify_range(
self.uniform_buffer_offset..(self.uniform_buffer_offset + ALIGNED_UNIFORMS_SIZE),
);

self.uniform_buffer_index = (self.uniform_buffer_index + 1) % MAX_FRAMES_IN_FLIGHT;
}
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};
use metal::{Buffer, Device};

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

Expand Down Expand Up @@ -120,7 +120,7 @@ impl Scene {
lights.len() * size_of::<AreaLight>(),
get_managed_buffer_storage_mode(),
);
lights_buffer.did_modify_range(NSRange::new(0, lights_buffer.length()));
lights_buffer.did_modify_range(0..lights_buffer.length());
lights_buffer.set_label("lights buffer");

Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/texture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn update_viewport_size_buffer(viewport_size_buffer: &Buffer, size: (u32, u32))
unsafe {
std::ptr::copy(viewport_size.as_ptr(), contents as *mut u32, byte_count);
}
viewport_size_buffer.did_modify_range(NSRange::new(0, byte_count));
viewport_size_buffer.did_modify_range(0..byte_count);
}

fn redraw(
Expand Down
5 changes: 1 addition & 4 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ fn main() {
);
}

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

let drawable = match layer.next_drawable() {
Some(drawable) => drawable,
Expand Down
6 changes: 4 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ impl BufferRef {
unsafe { msg_send![self, contents] }
}

pub fn did_modify_range(&self, range: NSRange) {
pub fn did_modify_range(&self, range: Range<usize>) {
let range: NSRange = range.into();
unsafe { msg_send![self, didModifyRange: range] }
}

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

pub fn add_debug_marker(&self, name: &str, range: NSRange) {
pub fn add_debug_marker(&self, name: &str, range: Range<usize>) {
let range: NSRange = range.into();
unsafe {
let name = crate::nsstring_from_str(name);
msg_send![self, addDebugMarker:name range:range]
Expand Down
20 changes: 13 additions & 7 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,9 +1262,10 @@ impl RenderCommandEncoderRef {
pub fn execute_commands_in_buffer(
&self,
buffer: &IndirectCommandBufferRef,
with_range: NSRange,
range: Range<usize>,
) {
unsafe { msg_send![self, executeCommandsInBuffer:buffer withRange:with_range] }
let range: NSRange = range.into();
unsafe { msg_send![self, executeCommandsInBuffer:buffer withRange: range] }
}

pub fn update_fence(&self, fence: &FenceRef, after_stages: MTLRenderStages) {
Expand Down Expand Up @@ -1300,7 +1301,8 @@ impl BlitCommandEncoderRef {
unsafe { msg_send![self, synchronizeResource: resource] }
}

pub fn fill_buffer(&self, destination_buffer: &BufferRef, range: NSRange, value: u8) {
pub fn fill_buffer(&self, destination_buffer: &BufferRef, range: Range<usize>, value: u8) {
let range: NSRange = range.into();
unsafe {
msg_send![self,
fillBuffer: destination_buffer
Expand Down Expand Up @@ -1468,10 +1470,11 @@ impl BlitCommandEncoderRef {
pub fn copy_indirect_command_buffer(
&self,
source: &IndirectCommandBufferRef,
source_range: NSRange,
source_range: Range<usize>,
destination: &IndirectCommandBufferRef,
destination_index: NSUInteger,
) {
let source_range: NSRange = source_range.into();
unsafe {
msg_send![self,
copyIndirectCommandBuffer: source
Expand All @@ -1482,7 +1485,8 @@ impl BlitCommandEncoderRef {
}
}

pub fn reset_commands_in_buffer(&self, buffer: &IndirectCommandBufferRef, range: NSRange) {
pub fn reset_commands_in_buffer(&self, buffer: &IndirectCommandBufferRef, range: Range<usize>) {
let range: NSRange = range.into();
unsafe {
msg_send![self,
resetCommandsInBuffer: buffer
Expand All @@ -1494,8 +1498,9 @@ impl BlitCommandEncoderRef {
pub fn optimize_indirect_command_buffer(
&self,
buffer: &IndirectCommandBufferRef,
range: NSRange,
range: Range<usize>,
) {
let range: NSRange = range.into();
unsafe {
msg_send![self,
optimizeIndirectCommandBuffer: buffer
Expand Down Expand Up @@ -1524,10 +1529,11 @@ impl BlitCommandEncoderRef {
pub fn resolve_counters(
&self,
sample_buffer: &CounterSampleBufferRef,
range: crate::NSRange,
range: Range<usize>,
destination_buffer: &BufferRef,
destination_offset: NSUInteger,
) {
let range: NSRange = range.into();
unsafe {
msg_send![self,
resolveCounters: sample_buffer
Expand Down
3 changes: 2 additions & 1 deletion src/indirect_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ impl IndirectCommandBufferRef {
unsafe { msg_send![self, indirectComputeCommandAtIndex: index] }
}

pub fn reset_with_range(&self, range: NSRange) {
pub fn reset_with_range(&self, range: Range<usize>) {
let range: NSRange = range.into();
unsafe { msg_send![self, resetWithRange: range] }
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use std::{
borrow::{Borrow, ToOwned},
marker::PhantomData,
mem,
ops::Deref,
ops::{Deref, Range},
os::raw::c_void,
};

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

Expand Down
3 changes: 2 additions & 1 deletion src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ impl FunctionConstantValuesRef {
&self,
values: *const c_void,
ty: MTLDataType,
range: NSRange,
range: Range<usize>,
) {
let range: NSRange = range.into();
unsafe { msg_send![self, setConstantValues:values type:ty withRange:range] }
}

Expand Down
6 changes: 4 additions & 2 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,11 @@ impl TextureRef {
&self,
pixel_format: MTLPixelFormat,
texture_type: MTLTextureType,
mipmap_levels: NSRange,
slices: NSRange,
mipmap_levels: Range<usize>,
slices: Range<usize>,
) -> Texture {
let mipmap_levels: NSRange = mipmap_levels.into();
let slices: NSRange = slices.into();
unsafe {
msg_send![self, newTextureViewWithPixelFormat:pixel_format
textureType:texture_type
Expand Down

0 comments on commit 7f191d1

Please sign in to comment.