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: Address most Clippy warnings #311

Closed
wants to merge 1 commit into from
Closed
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
fix: Address most trivial Clippy warnings
These categories of warnings were addressed:

- Automatic fixes from Clippy (`cargo clippy --fix`), such as removing
  redundant references
- Replace transmutes with safe pointer casts
- Remove lets that just get returned
- Add `Default` impl for types with `new()`
- Use iterators instead of indices
- Add `Send` and `Sync` bounds to `dyn` traits in `Arc`
tim-harding committed Apr 23, 2024
commit 03bdf51e8411a918f7c5e41a94c367130326998d
2 changes: 1 addition & 1 deletion examples/bindless/main.rs
Original file line number Diff line number Diff line change
@@ -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<_>>();

36 changes: 16 additions & 20 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
@@ -11,18 +11,18 @@ 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)]
#[allow(non_camel_case_types)]
pub struct position(cty::c_float, cty::c_float);
#[repr(C)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct color(cty::c_float, cty::c_float, cty::c_float);
#[repr(C)]
#[derive(Debug)]
@@ -52,7 +52,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;
@@ -102,7 +102,7 @@ fn main() {
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(std::ptr::from_ref(layer.as_ref()).cast_mut().cast());
}

let draw_size = window.inner_size();
@@ -114,7 +114,7 @@ fn main() {

device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
(vertex_data.len() * mem::size_of::<AAPLVertex>()) as u64,
std::mem::size_of_val(vertex_data) as u64,
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
};
@@ -152,18 +152,14 @@ 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,
drawable.texture(),
);
handle_render_pass_color_attachment(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);
let encoder = command_buffer.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);
@@ -172,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();
@@ -247,7 +243,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);
@@ -300,9 +296,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();
@@ -316,7 +312,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];
@@ -372,6 +368,6 @@ 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;
const NS_TO_MS: f64 = 1. / 1000.;
nanoseconds * NS_TO_MS
}
10 changes: 5 additions & 5 deletions examples/headless-render/main.rs
Original file line number Diff line number Diff line change
@@ -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]
@@ -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);
@@ -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);
2 changes: 1 addition & 1 deletion examples/library/main.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

use metal::*;

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

fn main() {
let device = Device::system_default().expect("no device found");
10 changes: 4 additions & 6 deletions examples/mesh-shader/main.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ use core_graphics_types::geometry::CGSize;

use metal::*;
use objc::{rc::autoreleasepool, runtime::YES};
use std::mem;
use winit::platform::macos::WindowExtMacOS;

use winit::{
@@ -42,7 +41,7 @@ fn main() {
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(std::ptr::from_ref(layer.as_ref()).cast_mut().cast());
}

let draw_size = window.inner_size();
@@ -93,11 +92,10 @@ fn main() {

let render_pass_descriptor = RenderPassDescriptor::new();

prepare_render_pass_descriptor(&render_pass_descriptor, drawable.texture());
prepare_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);
let encoder = command_buffer.new_render_command_encoder(render_pass_descriptor);

encoder.set_render_pipeline_state(&pipeline_state);
encoder.draw_mesh_threads(
@@ -108,7 +106,7 @@ fn main() {

encoder.end_encoding();

command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);
command_buffer.commit();
}
_ => {}
8 changes: 3 additions & 5 deletions examples/mps/main.rs
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ fn main() {

// Intersect rays with triangles inside acceleration structure
ray_intersector.encode_intersection_to_command_buffer(
&command_buffer,
command_buffer,
mps::MPSIntersectionType::Nearest,
&ray_buffer,
0,
@@ -140,9 +140,7 @@ fn create_pipeline(func: &str, library: &LibraryRef, device: &DeviceRef) -> Comp
let function = library.get_function(func, None).unwrap();
compute_descriptor.set_compute_function(Some(&function));

let pipeline = device
device
.new_compute_pipeline_state(&compute_descriptor)
.unwrap();

pipeline
.unwrap()
}
6 changes: 6 additions & 0 deletions examples/raytracing/camera.rs
Original file line number Diff line number Diff line change
@@ -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 {
31 changes: 16 additions & 15 deletions examples/raytracing/geometry.rs
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ pub trait Geometry {
pub fn compute_triangle_normal(v0: &Vec3, v1: &Vec3, v2: &Vec3) -> Vec3 {
let e1 = Vec3::normalize(*v1 - *v0);
let e2 = Vec3::normalize(*v2 - *v0);
return Vec3::cross(e1, e2);
Vec3::cross(e1, e2)
}

#[derive(Default)]
@@ -55,7 +55,7 @@ pub struct Triangle {
}

pub fn get_managed_buffer_storage_mode() -> MTLResourceOptions {
return MTLResourceOptions::StorageModeManaged;
MTLResourceOptions::StorageModeManaged
}

pub struct TriangleGeometry {
@@ -91,6 +91,7 @@ impl TriangleGeometry {
}
}

#[allow(clippy::too_many_arguments)]
pub fn add_cube_face_with_cube_vertices(
&mut self,
cube_vertices: &[Vec3],
@@ -112,10 +113,10 @@ impl TriangleGeometry {
let first_index = self.indices.len();
let base_index = self.vertices.len() as u16;

self.indices.push(base_index + 0);
self.indices.push(base_index);
self.indices.push(base_index + 1);
self.indices.push(base_index + 2);
self.indices.push(base_index + 0);
self.indices.push(base_index);
self.indices.push(base_index + 2);
self.indices.push(base_index + 3);

@@ -164,10 +165,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 vertex in &mut cube_vertices {
let transformed_vertex = Vec4::from((*vertex, 1.0));
let transformed_vertex = transform * transformed_vertex;
cube_vertices[i] = transformed_vertex.xyz();
*vertex = transformed_vertex.xyz();
}

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

for face in 0..6 {
for (face, cube) in CUBE_INDICES.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],
cube[0],
cube[1],
cube[2],
cube[3],
inward_normals,
);
}
@@ -416,14 +417,14 @@ impl Geometry for SphereGeometry {
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_primitive_data_buffer(Some(&self.sphere_buffer.as_ref().unwrap()));
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);
From::from(descriptor)
}

fn get_resources(&self) -> Vec<Resource> {
return vec![From::from(self.sphere_buffer.as_ref().unwrap().clone())];
vec![From::from(self.sphere_buffer.as_ref().unwrap().clone())]
}

fn get_intersection_function_name(&self) -> Option<&str> {
@@ -432,7 +433,7 @@ impl Geometry for SphereGeometry {
}

pub struct GeometryInstance {
pub geometry: Arc<dyn Geometry>,
pub geometry: Arc<dyn Geometry + Send + Sync>,
pub transform: Mat4,
pub mask: u32,
pub index_in_scene: NSUInteger,
3 changes: 1 addition & 2 deletions examples/raytracing/main.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -50,7 +49,7 @@ fn main() {
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(std::ptr::from_ref(layer.as_ref()).cast_mut().cast());
}

let draw_size = window.inner_size();
17 changes: 8 additions & 9 deletions examples/raytracing/renderer.rs
Original file line number Diff line number Diff line change
@@ -107,9 +107,9 @@ 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())) };
@@ -137,7 +137,7 @@ impl Renderer {
geometry_descriptor.set_intersection_function_table_offset(i as NSUInteger);
let geometry_descriptors = Array::from_owned_slice(&[geometry_descriptor]);
let accel_descriptor = PrimitiveAccelerationStructureDescriptor::descriptor();
accel_descriptor.set_geometry_descriptors(&geometry_descriptors);
accel_descriptor.set_geometry_descriptors(geometry_descriptors);
let accel_descriptor: AccelerationStructureDescriptor = From::from(accel_descriptor);
primitive_acceleration_structures.push(
Self::new_acceleration_structure_with_descriptor(
@@ -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;
@@ -164,7 +163,7 @@ impl Renderer {
MTLAccelerationStructureInstanceOptions::None
};
instance_descriptors[instance_index].intersection_function_table_offset = 0;
instance_descriptors[instance_index].mask = instance.mask as u32;
instance_descriptors[instance_index].mask = instance.mask;
for column in 0..4 {
for row in 0..3 {
instance_descriptors[instance_index].transformation_matrix[column][row] =
@@ -182,7 +181,7 @@ impl Renderer {
instance_buffer.did_modify_range(NSRange::new(0, instance_buffer.length()));

let accel_descriptor = InstanceAccelerationStructureDescriptor::descriptor();
accel_descriptor.set_instanced_acceleration_structures(&Array::from_owned_slice(
accel_descriptor.set_instanced_acceleration_structures(Array::from_owned_slice(
&primitive_acceleration_structures,
));
accel_descriptor.set_instance_count(scene.geometry_instances.len() as NSUInteger);
@@ -415,7 +414,7 @@ impl Renderer {
render_encoder.set_fragment_texture(0, Some(&self.accumulation_targets[0]));
render_encoder.draw_primitives(MTLPrimitiveType::Triangle, 0, 6);
render_encoder.end_encoding();
command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);
}
command_buffer.commit();
}
@@ -440,7 +439,7 @@ impl Renderer {
);
command_encoder.build_acceleration_structure(
&acceleration_structure,
&descriptor,
descriptor,
&scratch_buffer,
0,
);
2 changes: 1 addition & 1 deletion examples/reflection/main.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
use metal::*;
use objc::rc::autoreleasepool;

const PROGRAM: &'static str = r"
const PROGRAM: &str = r"
#include <metal_stdlib>
using namespace metal;
19 changes: 6 additions & 13 deletions examples/shader-dylib/main.rs
Original file line number Diff line number Diff line change
@@ -10,8 +10,6 @@ use winit::{
platform::macos::WindowExtMacOS,
};

use std::mem;

struct App {
pub _device: Device,
pub command_queue: CommandQueue,
@@ -22,14 +20,9 @@ struct App {
}

fn select_device() -> Option<Device> {
let devices = Device::all();
for device in devices {
if device.supports_dynamic_libraries() {
return Some(device);
}
}

None
Device::all()
.into_iter()
.find(|device| device.supports_dynamic_libraries())
}

impl App {
@@ -45,7 +38,7 @@ impl App {
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(std::ptr::from_ref(layer.as_ref()).cast_mut().cast());
}
let draw_size = window.inner_size();
layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64));
@@ -130,12 +123,12 @@ impl App {
{
let encoder = command_buffer.new_compute_command_encoder();
encoder.set_compute_pipeline_state(&self.image_fill_cps);
encoder.set_texture(0, Some(&drawable.texture()));
encoder.set_texture(0, Some(drawable.texture()));
encoder.dispatch_threads(threads_per_grid, threads_per_threadgroup);
encoder.end_encoding();
}

command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);
command_buffer.commit();
}
}
6 changes: 3 additions & 3 deletions examples/texture/build.rs
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ fn compile_shaders() {
.arg("-sdk")
.arg("macosx")
.arg("metal")
.args(&["-c", "shaders.metal"])
.args(&["-o", "shaders.air"])
.args(["-c", "shaders.metal"])
.args(["-o", "shaders.air"])
.spawn()
.unwrap()
.wait_with_output()
@@ -41,7 +41,7 @@ stderr: {}
.arg("macosx")
.arg("metallib")
.arg("shaders.air")
.args(&["-o", "shaders.metallib"])
.args(["-o", "shaders.metallib"])
.spawn()
.unwrap()
.wait()
21 changes: 8 additions & 13 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ struct ClearRect {
pub color: Color,
}

fn prepare_pipeline_state<'a>(
fn prepare_pipeline_state(
device: &DeviceRef,
library: &LibraryRef,
vertex_shader: &str,
@@ -104,7 +104,7 @@ fn main() {
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
view.setLayer(mem::transmute(layer.as_ref()));
view.setLayer(std::ptr::from_ref(layer.as_ref()).cast_mut().cast());
}

let draw_size = window.inner_size();
@@ -140,7 +140,7 @@ fn main() {

let mut r = 0.0f32;

let clear_rect = vec![ClearRect {
let clear_rect = [ClearRect {
rect: Rect {
x: -1.0,
y: -1.0,
@@ -197,15 +197,11 @@ fn main() {
];

unsafe {
std::ptr::copy(
vertex_data.as_ptr(),
p as *mut f32,
(vertex_data.len() * mem::size_of::<f32>()) as usize,
);
std::ptr::copy(vertex_data.as_ptr(), p as *mut f32, vertex_data.len());
}

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

@@ -216,11 +212,10 @@ fn main() {

let render_pass_descriptor = RenderPassDescriptor::new();

prepare_render_pass_descriptor(&render_pass_descriptor, drawable.texture());
prepare_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);
let encoder = command_buffer.new_render_command_encoder(render_pass_descriptor);

encoder.set_scissor_rect(MTLScissorRect {
x: 20,
@@ -249,7 +244,7 @@ fn main() {
encoder.draw_primitives(MTLPrimitiveType::Triangle, 0, 3);
encoder.end_encoding();

command_buffer.present_drawable(&drawable);
command_buffer.present_drawable(drawable);
command_buffer.commit();

r += 0.01f32;
6 changes: 6 additions & 0 deletions src/accelerator_structure.rs
Original file line number Diff line number Diff line change
@@ -327,6 +327,12 @@ impl IntersectionFunctionTableDescriptor {
}
}

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

impl IntersectionFunctionTableDescriptorRef {
pub fn set_function_count(&self, count: NSUInteger) {
unsafe { msg_send![self, setFunctionCount: count] }
6 changes: 6 additions & 0 deletions src/blitpass.rs
Original file line number Diff line number Diff line change
@@ -65,6 +65,12 @@ impl BlitPassSampleBufferAttachmentDescriptor {
}
}

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

impl BlitPassSampleBufferAttachmentDescriptorRef {
pub fn sample_buffer(&self) -> &CounterSampleBufferRef {
unsafe { msg_send![self, sampleBuffer] }
6 changes: 6 additions & 0 deletions src/capturedescriptor.rs
Original file line number Diff line number Diff line change
@@ -35,6 +35,12 @@ impl CaptureDescriptor {
}
}

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

impl CaptureDescriptorRef {
/// See <https://developer.apple.com/documentation/metal/mtlcapturedescriptor/3237248-captureobject>
pub fn set_capture_device(&self, device: &DeviceRef) {
5 changes: 3 additions & 2 deletions src/capturemanager.rs
Original file line number Diff line number Diff line change
@@ -79,11 +79,12 @@ impl CaptureManagerRef {
/// 3. Adding an info.plist file containing the `MetalCaptureEnabled` key set to `YES`
pub fn start_capture(&self, descriptor: &CaptureDescriptorRef) -> Result<(), String> {
unsafe {
Ok(try_objc! { err =>
try_objc! { err =>
msg_send![self, startCaptureWithDescriptor: descriptor
error: &mut err]
})
}
}
Ok(())
}

pub fn start_capture_with_device(&self, device: &DeviceRef) {
6 changes: 6 additions & 0 deletions src/computepass.rs
Original file line number Diff line number Diff line change
@@ -66,6 +66,12 @@ impl ComputePassSampleBufferAttachmentDescriptor {
}
}

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

impl ComputePassSampleBufferAttachmentDescriptorRef {
pub fn sample_buffer(&self) -> &CounterSampleBufferRef {
unsafe { msg_send![self, sampleBuffer] }
6 changes: 6 additions & 0 deletions src/counters.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,12 @@ impl CounterSampleBufferDescriptor {
}
}

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

impl CounterSampleBufferDescriptorRef {
pub fn counter_set(&self) -> &CounterSetRef {
unsafe { msg_send![self, counterSet] }
12 changes: 12 additions & 0 deletions src/depthstencil.rs
Original file line number Diff line number Diff line change
@@ -53,6 +53,12 @@ impl StencilDescriptor {
}
}

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

impl StencilDescriptorRef {
pub fn stencil_compare_function(&self) -> MTLCompareFunction {
unsafe { msg_send![self, stencilCompareFunction] }
@@ -120,6 +126,12 @@ impl DepthStencilDescriptor {
}
}

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

impl DepthStencilDescriptorRef {
pub fn depth_compare_function(&self) -> MTLCompareFunction {
unsafe { msg_send![self, depthCompareFunction] }
11 changes: 5 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ bitflags! {
}
}

#[allow(non_camel_case_types)]
#[allow(non_camel_case_types, clippy::enum_variant_names)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
enum OS {
iOS,
@@ -711,7 +711,7 @@ impl MTLFeatureSet {

pub fn max_buffer_length(&self) -> u32 {
if self.os() == OS::macOS && self.os_version() >= 12 {
1 * GB
GB
} else {
256 * MB
}
@@ -1709,7 +1709,7 @@ impl DeviceRef {
library_data.as_ptr() as *const std::ffi::c_void,
library_data.len() as crate::c_size_t,
&_dispatch_main_q as *const _ as dispatch_queue_t,
&*destructor_block.deref(),
destructor_block.deref(),
);

let library: *mut MTLLibrary = try_objc! { err =>
@@ -2105,14 +2105,13 @@ impl DeviceRef {
unsafe {
let counter_sets: *mut Object = msg_send![self, counterSets];
let count: NSUInteger = msg_send![counter_sets, count];
let ret = (0..count)
(0..count)
.map(|i| {
let csp: *mut MTLCounterSet = msg_send![counter_sets, objectAtIndex: i];
let () = msg_send![csp, retain];
CounterSet::from_ptr(csp)
})
.collect();
ret
.collect()
}
}
}
4 changes: 4 additions & 0 deletions src/encoder.rs
Original file line number Diff line number Diff line change
@@ -974,6 +974,7 @@ impl RenderCommandEncoderRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn draw_indexed_primitives_instanced_base_instance(
&self,
primitive_type: MTLPrimitiveType,
@@ -1283,6 +1284,7 @@ impl BlitCommandEncoderRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn copy_from_texture(
&self,
source_texture: &TextureRef,
@@ -1310,6 +1312,7 @@ impl BlitCommandEncoderRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn copy_from_buffer_to_texture(
&self,
source_buffer: &BufferRef,
@@ -1340,6 +1343,7 @@ impl BlitCommandEncoderRef {
}

/// See <https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400756-copy>
#[allow(clippy::too_many_arguments)]
pub fn copy_from_texture_to_buffer(
&self,
source_texture: &TextureRef,
6 changes: 6 additions & 0 deletions src/heap.rs
Original file line number Diff line number Diff line change
@@ -167,6 +167,12 @@ impl HeapDescriptor {
}
}

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

impl HeapDescriptorRef {
pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode {
unsafe { msg_send![self, cpuCacheMode] }
9 changes: 9 additions & 0 deletions src/indirect_encoder.rs
Original file line number Diff line number Diff line change
@@ -29,6 +29,12 @@ impl IndirectCommandBufferDescriptor {
}
}

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

impl IndirectCommandBufferDescriptorRef {
pub fn command_types(&self) -> MTLIndirectCommandType {
unsafe { msg_send![self, commandTypes] }
@@ -171,6 +177,7 @@ impl IndirectRenderCommandRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn draw_indexed_primitives(
&self,
primitive_type: MTLPrimitiveType,
@@ -196,6 +203,7 @@ impl IndirectRenderCommandRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn draw_patches(
&self,
number_of_patch_control_points: NSUInteger,
@@ -225,6 +233,7 @@ impl IndirectRenderCommandRef {
}
}

#[allow(clippy::too_many_arguments)]
pub fn draw_indexed_patches(
&self,
number_of_patch_control_points: NSUInteger,
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -353,7 +353,7 @@ where

#[inline]
fn deref(&self) -> &ArrayRef<T> {
unsafe { mem::transmute(self.as_ptr()) }
unsafe { &*(self.as_ptr() as *const ArrayRef<T>) }
}
}

@@ -363,7 +363,7 @@ where
T::Ref: objc::Message + 'static,
{
fn borrow(&self) -> &ArrayRef<T> {
unsafe { mem::transmute(self.as_ptr()) }
unsafe { &*(self.as_ptr() as *const ArrayRef<T>) }
}
}

@@ -433,6 +433,12 @@ impl MetalLayer {
}
}

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

impl MetalLayerRef {
pub fn device(&self) -> &DeviceRef {
unsafe { msg_send![self, device] }
45 changes: 36 additions & 9 deletions src/library.rs
Original file line number Diff line number Diff line change
@@ -178,6 +178,12 @@ impl FunctionDescriptor {
}
}

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

impl FunctionDescriptorRef {
pub fn name(&self) -> &str {
unsafe {
@@ -383,6 +389,12 @@ impl FunctionConstantValues {
}
}

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

impl FunctionConstantValuesRef {
pub fn set_constant_value_at_index(
&self,
@@ -437,6 +449,12 @@ impl CompileOptions {
}
}

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

impl CompileOptionsRef {
pub unsafe fn preprocessor_macros(&self) -> *mut Object {
msg_send![self, preprocessorMacros]
@@ -497,13 +515,12 @@ impl CompileOptionsRef {
unsafe {
let libraries: *mut Object = msg_send![self, libraries];
let count: NSUInteger = msg_send![libraries, count];
let ret = (0..count)
(0..count)
.map(|i| {
let lib = msg_send![libraries, objectAtIndex: i];
DynamicLibrary::from_ptr(lib)
})
.collect();
ret
.collect()
}
}

@@ -753,6 +770,12 @@ impl BinaryArchiveDescriptor {
}
}

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

impl BinaryArchiveDescriptorRef {
pub fn url(&self) -> &URLRef {
unsafe { msg_send![self, url] }
@@ -855,19 +878,24 @@ impl LinkedFunctions {
}
}

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

impl LinkedFunctionsRef {
/// Marshal to Rust Vec
pub fn functions(&self) -> Vec<Function> {
unsafe {
let functions: *mut Object = msg_send![self, functions];
let count: NSUInteger = msg_send![functions, count];
let ret = (0..count)
(0..count)
.map(|i| {
let f = msg_send![functions, objectAtIndex: i];
Function::from_ptr(f)
})
.collect();
ret
.collect()
}
}

@@ -882,13 +910,12 @@ impl LinkedFunctionsRef {
unsafe {
let functions: *mut Object = msg_send![self, binaryFunctions];
let count: NSUInteger = msg_send![functions, count];
let ret = (0..count)
(0..count)
.map(|i| {
let f = msg_send![functions, objectAtIndex: i];
Function::from_ptr(f)
})
.collect();
ret
.collect()
}
}

6 changes: 3 additions & 3 deletions src/mps.rs
Original file line number Diff line number Diff line change
@@ -229,6 +229,7 @@ impl RayIntersectorRef {
unsafe { msg_send![self, setTriangleIntersectionTestType: test_type] }
}

#[allow(clippy::too_many_arguments)]
pub fn encode_intersection_to_command_buffer(
&self,
command_buffer: &CommandBufferRef,
@@ -449,13 +450,12 @@ impl InstanceAccelerationStructureRef {
unsafe {
let acs: *mut Object = msg_send![self, accelerationStructures];
let count: NSUInteger = msg_send![acs, count];
let ret = (0..count)
(0..count)
.map(|i| {
let ac = msg_send![acs, objectAtIndex: i];
PolygonAccelerationStructure::from_ptr(ac)
})
.collect();
ret
.collect()
}
}

16 changes: 10 additions & 6 deletions src/pipeline/compute.rs
Original file line number Diff line number Diff line change
@@ -101,6 +101,12 @@ impl ComputePipelineDescriptor {
}
}

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

impl ComputePipelineDescriptorRef {
pub fn label(&self) -> &str {
unsafe {
@@ -186,13 +192,12 @@ impl ComputePipelineDescriptorRef {
unsafe {
let libraries: *mut Object = msg_send![self, insertLibraries];
let count: NSUInteger = msg_send![libraries, count];
let ret = (0..count)
(0..count)
.map(|i| {
let lib = msg_send![libraries, objectAtIndex: i];
DynamicLibrary::from_ptr(lib)
})
.collect();
ret
.collect()
}
}

@@ -208,13 +213,12 @@ impl ComputePipelineDescriptorRef {
unsafe {
let archives: *mut Object = msg_send![self, binaryArchives];
let count: NSUInteger = msg_send![archives, count];
let ret = (0..count)
(0..count)
.map(|i| {
let a = msg_send![archives, objectAtIndex: i];
BinaryArchive::from_ptr(a)
})
.collect();
ret
.collect()
}
}

17 changes: 14 additions & 3 deletions src/pipeline/render.rs
Original file line number Diff line number Diff line change
@@ -259,6 +259,12 @@ impl MeshRenderPipelineDescriptor {
}
}

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

impl MeshRenderPipelineDescriptorRef {
pub fn color_attachments(&self) -> &RenderPipelineColorAttachmentDescriptorArrayRef {
unsafe { msg_send![self, colorAttachments] }
@@ -492,6 +498,12 @@ impl RenderPipelineDescriptor {
}
}

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

impl RenderPipelineDescriptorRef {
pub fn label(&self) -> &str {
unsafe {
@@ -647,13 +659,12 @@ impl RenderPipelineDescriptorRef {
unsafe {
let archives: *mut Object = msg_send![self, binaryArchives];
let count: NSUInteger = msg_send![archives, count];
let ret = (0..count)
(0..count)
.map(|i| {
let a = msg_send![archives, objectAtIndex: i];
BinaryArchive::from_ptr(a)
})
.collect();
ret
.collect()
}
}

12 changes: 12 additions & 0 deletions src/renderpass.rs
Original file line number Diff line number Diff line change
@@ -166,6 +166,12 @@ impl RenderPassColorAttachmentDescriptor {
}
}

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

impl RenderPassColorAttachmentDescriptorRef {
pub fn clear_color(&self) -> MTLClearColor {
unsafe { msg_send![self, clearColor] }
@@ -265,6 +271,12 @@ impl RenderPassSampleBufferAttachmentDescriptor {
}
}

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

impl RenderPassSampleBufferAttachmentDescriptorRef {
pub fn sample_buffer(&self) -> &CounterSampleBufferRef {
unsafe { msg_send![self, sampleBuffer] }
6 changes: 6 additions & 0 deletions src/sampler.rs
Original file line number Diff line number Diff line change
@@ -62,6 +62,12 @@ impl SamplerDescriptor {
}
}

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

impl SamplerDescriptorRef {
pub fn set_min_filter(&self, filter: MTLSamplerMinMagFilter) {
unsafe { msg_send![self, setMinFilter: filter] }
2 changes: 1 addition & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ impl SharedEventListener {
#[cfg(feature = "dispatch")]
pub fn from_queue(queue: &dispatch::Queue) -> Self {
unsafe {
let raw_queue = std::mem::transmute::<&dispatch::Queue, *const dispatch_queue_t>(queue);
let raw_queue: *const dispatch_queue_t = std::ptr::from_ref(queue).cast_mut().cast();
Self::from_queue_handle(*raw_queue)
}
}
6 changes: 6 additions & 0 deletions src/texture.rs
Original file line number Diff line number Diff line change
@@ -62,6 +62,12 @@ impl TextureDescriptor {
}
}

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

impl TextureDescriptorRef {
pub fn texture_type(&self) -> MTLTextureType {
unsafe { msg_send![self, textureType] }
12 changes: 12 additions & 0 deletions src/vertexdescriptor.rs
Original file line number Diff line number Diff line change
@@ -94,6 +94,12 @@ impl VertexBufferLayoutDescriptor {
}
}

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

impl VertexBufferLayoutDescriptorRef {
pub fn stride(&self) -> NSUInteger {
unsafe { msg_send![self, stride] }
@@ -162,6 +168,12 @@ impl VertexAttributeDescriptor {
}
}

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

impl VertexAttributeDescriptorRef {
pub fn format(&self) -> MTLVertexFormat {
unsafe { msg_send![self, format] }