Skip to content

Commit

Permalink
Manual clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jan 2, 2025
1 parent f918248 commit c151cac
Show file tree
Hide file tree
Showing 24 changed files with 370 additions and 395 deletions.
25 changes: 11 additions & 14 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ use core_graphics_types::geometry::CGSize;

use objc::{rc::autoreleasepool, runtime::YES};

use std::mem;

// Declare the data structures needed to carry vertex layout to
// metal shading language(MSL) program. Use #[repr(C)], to make
// the data structure compatible with C++ type data structure
// for vertex defined in MSL program as MSL program is broadly
// based on C++
#[repr(C)]
#[derive(Debug)]
pub struct position(std::ffi::c_float, std::ffi::c_float);
pub struct Position(std::ffi::c_float, std::ffi::c_float);
#[repr(C)]
#[derive(Debug)]
pub struct color(std::ffi::c_float, std::ffi::c_float, std::ffi::c_float);
pub struct Color(std::ffi::c_float, std::ffi::c_float, std::ffi::c_float);
#[repr(C)]
#[derive(Debug)]
pub struct AAPLVertex {
p: position,
c: color,
p: Position,
c: Color,
}

fn main() {
Expand Down Expand Up @@ -94,7 +92,7 @@ fn main() {
// Currently, MetalLayer is the only interface that provide
// layers to carry drawable texture from GPU rendaring through metal
// library to viewable windows.
let layer = MetalLayer::new();
let mut layer = MetalLayer::new();
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
layer.set_presents_with_transaction(false);
Expand All @@ -103,7 +101,7 @@ fn main() {
if let Ok(RawWindowHandle::AppKit(rw)) = window.window_handle().map(|wh| wh.as_raw()) {
let view = rw.ns_view.as_ptr() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(<*mut _>::cast(layer.as_mut()));
}
}

Expand Down Expand Up @@ -234,15 +232,15 @@ fn create_vertex_points_for_circle() -> Vec<AAPLVertex> {
let position_y: f32 = position_y * circle_size;

v.push(AAPLVertex {
p: position(position_x, position_y),
c: color(0.7, 0.3, 0.5),
p: Position(position_x, position_y),
c: Color(0.7, 0.3, 0.5),
});

if (i + 1) % 2 == 0 {
// For each two points on perimeter, push one point of origin
v.push(AAPLVertex {
p: position(origin_x, origin_y),
c: color(0.2, 0.7, 0.4),
p: Position(origin_x, origin_y),
c: Color(0.2, 0.7, 0.4),
});
}
}
Expand Down Expand Up @@ -381,6 +379,5 @@ fn fetch_timestamp_counter_set(device: &Device) -> metal::CounterSet {
fn microseconds_between_begin(begin: u64, end: u64, gpu_time_span: u64, cpu_time_span: u64) -> f64 {
let time_span = (end as f64) - (begin as f64);
let nanoseconds = time_span / (gpu_time_span as f64) * (cpu_time_span as f64);
let microseconds = nanoseconds / 1000.0;
microseconds
nanoseconds / 1000.0
}
8 changes: 4 additions & 4 deletions examples/compute/compute-argument-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ fn main() {
];

let buffer = device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.as_ptr().cast(),
mem::size_of_val(&data) as u64,
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.as_ptr().cast(),
mem::size_of_val(&data) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down
8 changes: 4 additions & 4 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ fn create_input_and_output_buffers(
let data = vec![1u32; num_elements as usize];

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

let sum = {
let data = [0u32];
device.new_buffer_with_data(
unsafe { std::mem::transmute(data.as_ptr()) },
(data.len() * std::mem::size_of::<u32>()) as u64,
data.as_ptr().cast(),
size_of_val(data.as_slice()) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down
2 changes: 1 addition & 1 deletion examples/headless-render/main.rs
Original file line number Diff line number Diff line change
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,
mem::size_of_val(&VERTEX_ATTRIBS) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
}
Expand Down
5 changes: 2 additions & 3 deletions examples/mesh-shader/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use core_graphics_types::geometry::CGSize;

use metal::*;
use objc::{rc::autoreleasepool, runtime::YES};
use std::mem;

use winit::{
event::{Event, WindowEvent},
Expand Down Expand Up @@ -34,7 +33,7 @@ fn main() {

let device = Device::system_default().expect("no device found");

let layer = MetalLayer::new();
let mut layer = MetalLayer::new();
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
layer.set_presents_with_transaction(false);
Expand All @@ -43,7 +42,7 @@ fn main() {
if let Ok(RawWindowHandle::AppKit(rw)) = window.window_handle().map(|wh| wh.as_raw()) {
let view = rw.ns_view.as_ptr() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(<*mut _>::cast(layer.as_mut()));
}
}

Expand Down
107 changes: 42 additions & 65 deletions examples/raytracing/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
mem::{size_of, transmute},
sync::Arc,
};
use std::sync::Arc;

use glam::{
f32::{Mat4, Vec3, Vec4},
Expand Down Expand Up @@ -95,10 +92,7 @@ impl TriangleGeometry {
&mut self,
cube_vertices: &[Vec3],
colour: Vec3,
i0: u16,
i1: u16,
i2: u16,
i3: u16,
[i0, i1, i2, i3]: [u16; 4],
inward_normals: bool,
) {
let v0 = cube_vertices[i0 as usize];
Expand Down Expand Up @@ -164,10 +158,10 @@ impl TriangleGeometry {
Vec3::new(0.5, 0.5, 0.5),
];

for i in 0..8 {
let transformed_vertex = Vec4::from((cube_vertices[i], 1.0));
for v in &mut cube_vertices {
let transformed_vertex = v.extend(1.0);
let transformed_vertex = transform * transformed_vertex;
cube_vertices[i] = transformed_vertex.xyz();
*v = transformed_vertex.xyz();
}

const CUBE_INDICES: [[u16; 4]; 6] = [
Expand All @@ -179,15 +173,12 @@ impl TriangleGeometry {
[4, 5, 7, 6],
];

for face in 0..6 {
for (face, indices) in CUBE_INDICES.into_iter().enumerate() {
if face_mask & (1 << face) != 0 {
self.add_cube_face_with_cube_vertices(
&cube_vertices,
colour,
CUBE_INDICES[face][0],
CUBE_INDICES[face][1],
CUBE_INDICES[face][2],
CUBE_INDICES[face][3],
indices,
inward_normals,
);
}
Expand All @@ -197,41 +188,31 @@ impl TriangleGeometry {

impl Geometry for TriangleGeometry {
fn upload_to_buffers(&mut self) {
self.index_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.indices.as_ptr()),
(self.indices.len() * size_of::<u16>()) as NSUInteger,
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,
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,
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,
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,
get_managed_buffer_storage_mode(),
)
});
self.index_buffer = Some(self.device.new_buffer_with_data(
self.indices.as_ptr().cast(),
size_of_val(self.indices.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.vertex_position_buffer = Some(self.device.new_buffer_with_data(
self.vertices.as_ptr().cast(),
size_of_val(self.vertices.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.vertex_normal_buffer = Some(self.device.new_buffer_with_data(
self.normals.as_ptr().cast(),
size_of_val(self.normals.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.vertex_colour_buffer = Some(self.device.new_buffer_with_data(
self.colours.as_ptr().cast(),
size_of_val(self.colours.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.per_primitive_data_buffer = Some(self.device.new_buffer_with_data(
self.triangles.as_ptr().cast(),
size_of_val(self.triangles.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.index_buffer
.as_ref()
.unwrap()
Expand Down Expand Up @@ -363,13 +344,11 @@ impl SphereGeometry {

impl Geometry for SphereGeometry {
fn upload_to_buffers(&mut self) {
self.sphere_buffer = Some(unsafe {
self.device.new_buffer_with_data(
transmute(self.spheres.as_ptr()),
(self.spheres.len() * size_of::<Sphere>()) as NSUInteger,
get_managed_buffer_storage_mode(),
)
});
self.sphere_buffer = Some(self.device.new_buffer_with_data(
self.spheres.as_ptr().cast(),
size_of_val(self.spheres.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.sphere_buffer
.as_ref()
.unwrap()
Expand All @@ -381,13 +360,11 @@ impl Geometry for SphereGeometry {
max: sphere.origin_radius_squared.xyz() + sphere.colour_radius.w,
});
}
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,
get_managed_buffer_storage_mode(),
)
});
self.bounding_box_buffer = Some(self.device.new_buffer_with_data(
bounding_boxes.as_ptr().cast(),
size_of_val(bounding_boxes.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
));
self.bounding_box_buffer
.as_ref()
.unwrap()
Expand Down
5 changes: 2 additions & 3 deletions examples/raytracing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;
use metal::*;
use objc::{rc::autoreleasepool, runtime::YES};
use std::mem;
use winit::{
event::{Event, WindowEvent},
event_loop::ControlFlow,
Expand Down Expand Up @@ -42,7 +41,7 @@ fn main() {

let device = find_raytracing_supporting_device();

let layer = MetalLayer::new();
let mut layer = MetalLayer::new();
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::RGBA16Float);
layer.set_presents_with_transaction(false);
Expand All @@ -51,7 +50,7 @@ fn main() {
if let Ok(RawWindowHandle::AppKit(rw)) = window.window_handle().map(|wh| wh.as_raw()) {
let view = rw.ns_view.as_ptr() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(<*mut _>::cast(layer.as_mut()));
}
}

Expand Down
20 changes: 9 additions & 11 deletions examples/raytracing/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core_graphics_types::{base::CGFloat, geometry::CGSize};
use std::{
collections::BTreeMap,
ffi::c_void,
mem::{size_of, transmute},
mem::{size_of_val, transmute},
ops::Index,
sync::{Arc, Condvar, Mutex},
};
Expand Down Expand Up @@ -107,24 +107,24 @@ impl Renderer {
let resource_buffer_begin_index = resources_stride * geometry_index;
let resources = geometry.get_resources();

for argument_index in 0..resources.len() {
for (argument_index, resource) in resources.iter().enumerate() {
let resource_buffer_index = resource_buffer_begin_index + argument_index;
let resource = resources[argument_index].clone();
let resource = resource.clone();
resource_buffer_data[resource_buffer_index] =
if resource.conforms_to_protocol::<MTLBuffer>().unwrap() {
let buffer = unsafe { Buffer::from_ptr(transmute(resource.into_ptr())) };
let buffer = unsafe { Buffer::from_ptr(resource.into_ptr().cast()) };
buffer.gpu_address()
} else if resource.conforms_to_protocol::<MTLTexture>().unwrap() {
let texture = unsafe { Texture::from_ptr(transmute(resource.into_ptr())) };
let texture = unsafe { Texture::from_ptr(resource.into_ptr().cast()) };
texture.gpu_resource_id()._impl
} else {
panic!("Unexpected resource!")
}
}
}
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.as_ptr().cast(),
size_of_val(resource_buffer_data.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
);
resource_buffer.set_label("resource buffer");
Expand Down Expand Up @@ -152,8 +152,7 @@ impl Renderer {
MTLAccelerationStructureInstanceDescriptor::default();
scene.geometry_instances.len()
];
for instance_index in 0..scene.geometry_instances.len() {
let instance = scene.geometry_instances[instance_index].as_ref();
for (instance_index, instance) in scene.geometry_instances.iter().enumerate() {
let geometry_index = instance.index_in_scene;
instance_descriptors[instance_index].acceleration_structure_index =
geometry_index as u32;
Expand All @@ -174,8 +173,7 @@ 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_val(instance_descriptors.as_slice()) as NSUInteger,
get_managed_buffer_storage_mode(),
);
instance_buffer.set_label("instance buffer");
Expand Down
Loading

0 comments on commit c151cac

Please sign in to comment.