Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all clippy lints #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = ["link"]
private = []
mps = []
link = ["core-graphics-types/link"]
cargo-clippy = [] # Workaround for https://github.com/gfx-rs/metal-rs/pull/344#issuecomment-2569042111

[dependencies]
core-graphics-types = { version = "0.1.3", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion examples/bindless/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main() {
let textures = (0..BINDLESS_TEXTURE_COUNT)
.map(|i| {
heap.new_texture(&texture_descriptor)
.expect(&format!("Failed to allocate texture {}", i))
.unwrap_or_else(|| panic!("Failed to allocate texture {}", i))
})
.collect::<Vec<_>>();

Expand Down
50 changes: 23 additions & 27 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 All @@ -52,7 +50,7 @@ fn main() {
device.sample_timestamps(&mut cpu_start, &mut gpu_start);
let counter_sample_buffer = create_counter_sample_buffer(&device);
let destination_buffer = device.new_buffer(
(std::mem::size_of::<u64>() * 4 as usize) as u64,
(std::mem::size_of::<u64>() * 4_usize) as u64,
MTLResourceOptions::StorageModeShared,
);
let counter_sampling_point = MTLCounterSamplingPoint::AtStageBoundary;
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 _,
(vertex_data.len() * mem::size_of::<AAPLVertex>()) 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 @@ -152,17 +149,17 @@ fn main() {
// Obtain a renderPassDescriptor generated from the view's drawable textures.
let render_pass_descriptor = RenderPassDescriptor::new();
handle_render_pass_color_attachment(
&render_pass_descriptor,
render_pass_descriptor,
drawable.texture(),
);
handle_render_pass_sample_buffer_attachment(
&render_pass_descriptor,
render_pass_descriptor,
&counter_sample_buffer,
);

// Create a render command encoder.
let encoder = command_buffer
.new_render_command_encoder(&render_pass_descriptor);
.new_render_command_encoder(render_pass_descriptor);
encoder.set_render_pipeline_state(&pipeline_state);
// Pass in the parameter data.
encoder.set_vertex_buffer(0, Some(&vbuf), 0);
Expand All @@ -171,13 +168,13 @@ fn main() {
encoder.end_encoding();

resolve_samples_into_buffer(
&command_buffer,
command_buffer,
&counter_sample_buffer,
&destination_buffer,
);

// Schedule a present once the framebuffer is complete using the current drawable.
command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);

// Finalize rendering here & push the command buffer to the GPU.
command_buffer.commit();
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 All @@ -256,7 +253,7 @@ 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_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);
Expand Down Expand Up @@ -309,9 +306,9 @@ fn resolve_samples_into_buffer(
) {
let blit_encoder = command_buffer.new_blit_command_encoder();
blit_encoder.resolve_counters(
&counter_sample_buffer,
counter_sample_buffer,
crate::NSRange::new(0_u64, 4),
&destination_buffer,
destination_buffer,
0_u64,
);
blit_encoder.end_encoding();
Expand All @@ -325,7 +322,7 @@ fn handle_timestamps(
gpu_end: u64,
) {
let samples = unsafe {
std::slice::from_raw_parts(resolved_sample_buffer.contents() as *const u64, 4 as usize)
std::slice::from_raw_parts(resolved_sample_buffer.contents() as *const u64, 4_usize)
};
let vertex_pass_start = samples[0];
let vertex_pass_end = samples[1];
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;
return 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
12 changes: 6 additions & 6 deletions examples/headless-render/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const VIEW_WIDTH: u64 = 512;
const VIEW_HEIGHT: u64 = 512;
const TOTAL_BYTES: usize = (VIEW_WIDTH * VIEW_HEIGHT * 4) as usize;

const VERTEX_SHADER: &'static str = "triangle_vertex";
const FRAGMENT_SHADER: &'static str = "triangle_fragment";
const VERTEX_SHADER: &str = "triangle_vertex";
const FRAGMENT_SHADER: &str = "triangle_fragment";

// [2 bytes position, 3 bytes color] * 3
#[rustfmt::skip]
Expand Down Expand Up @@ -54,10 +54,10 @@ fn main() {
let vertex_buffer = create_vertex_buffer(&device);

let render_pass_descriptor = RenderPassDescriptor::new();
initialize_color_attachment(&render_pass_descriptor, &texture);
initialize_color_attachment(render_pass_descriptor, &texture);

let command_buffer = command_queue.new_command_buffer();
let rc_encoder = command_buffer.new_render_command_encoder(&render_pass_descriptor);
let rc_encoder = command_buffer.new_render_command_encoder(render_pass_descriptor);
rc_encoder.set_render_pipeline_state(&pipeline_state);
rc_encoder.set_vertex_buffer(0, Some(&vertex_buffer), 0);
rc_encoder.draw_primitives(MTLPrimitiveType::Triangle, 0, 3);
Expand Down Expand Up @@ -100,7 +100,7 @@ fn save_image(texture: &TextureRef) {
let out_file =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/headless-render/out.png");
let file = File::create(&out_file).unwrap();
let ref mut w = BufWriter::new(file);
let w = &mut BufWriter::new(file);

let mut encoder = png::Encoder::new(w, VIEW_WIDTH as u32, VIEW_HEIGHT as u32);
encoder.set_color(ColorType::Rgba);
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,
mem::size_of_val(&VERTEX_ATTRIBS) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/library/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use metal::*;

const PROGRAM: &'static str = "";
const PROGRAM: &str = "";

fn main() {
let device = Device::system_default().expect("no device found");
Expand Down
11 changes: 5 additions & 6 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 Expand Up @@ -98,13 +97,13 @@ fn main() {
let render_pass_descriptor = RenderPassDescriptor::new();

prepare_render_pass_descriptor(
&render_pass_descriptor,
render_pass_descriptor,
drawable.texture(),
);

let command_buffer = command_queue.new_command_buffer();
let encoder =
command_buffer.new_render_command_encoder(&render_pass_descriptor);
command_buffer.new_render_command_encoder(render_pass_descriptor);

encoder.set_render_pipeline_state(&pipeline_state);
encoder.draw_mesh_threads(
Expand All @@ -115,7 +114,7 @@ fn main() {

encoder.end_encoding();

command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);
command_buffer.commit();
}
_ => (),
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
6 changes: 6 additions & 0 deletions examples/raytracing/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub struct Camera {
pub forward: Vec4,
}

impl Default for Camera {
fn default() -> Self {
Self::new()
}
}

impl Camera {
pub fn new() -> Self {
Self {
Expand Down
Loading