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 3, 2025
1 parent f918248 commit 5a0ebc6
Show file tree
Hide file tree
Showing 27 changed files with 384 additions and 408 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ name = "fence"
members = [
"examples/texture",
]

[workspace.lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(cargo-clippy)'] }
30 changes: 13 additions & 17 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 All @@ -112,11 +110,10 @@ fn main() {

let vbuf = {
let vertex_data = create_vertex_points_for_circle();
let vertex_data = vertex_data.as_slice();

device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
std::mem::size_of_val(vertex_data) as u64,
vertex_data.as_ptr().cast(),
std::mem::size_of_val(vertex_data.as_slice()) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
};
Expand Down Expand Up @@ -234,15 +231,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 +378,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
6 changes: 3 additions & 3 deletions examples/mps/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {

// Simple vertex/index buffer data

let vertices: [Vertex; 3] = [
let vertices = [
Vertex {
xyz: [0.25, 0.25, 0.0],
},
Expand All @@ -41,7 +41,7 @@ fn main() {

let vertex_stride = mem::size_of::<Vertex>();

let indices: [u32; 3] = [0, 1, 2];
let indices = [0, 1, 2];

// Vertex data should be stored in private or managed buffers on discrete GPU systems (AMD, NVIDIA).
// Private buffers are stored entirely in GPU memory and cannot be accessed by the CPU. Managed
Expand All @@ -56,7 +56,7 @@ fn main() {

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

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
Loading

0 comments on commit 5a0ebc6

Please sign in to comment.