From c151cacebffa4002693cb67c8077dd4483b14e07 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 2 Jan 2025 22:29:57 +0100 Subject: [PATCH] Manual clippy fixes --- examples/circle/main.rs | 25 +- examples/compute/compute-argument-buffer.rs | 8 +- examples/compute/main.rs | 8 +- examples/headless-render/main.rs | 2 +- examples/mesh-shader/main.rs | 5 +- examples/raytracing/geometry.rs | 107 ++--- examples/raytracing/main.rs | 5 +- examples/raytracing/renderer.rs | 20 +- examples/raytracing/scene.rs | 18 +- examples/shader-dylib/main.rs | 14 +- examples/texture/Cargo.toml | 2 +- examples/texture/build.rs | 6 +- examples/window/main.rs | 10 +- src/capturemanager.rs | 1 + src/counters.rs | 2 +- src/device.rs | 472 ++++++++++---------- src/encoder.rs | 4 + src/indirect_encoder.rs | 3 + src/lib.rs | 7 +- src/library.rs | 24 +- src/mps.rs | 5 +- src/pipeline/compute.rs | 10 +- src/pipeline/render.rs | 5 +- src/sync.rs | 2 + 24 files changed, 370 insertions(+), 395 deletions(-) diff --git a/examples/circle/main.rs b/examples/circle/main.rs index 7b932a6c..04a0bf49 100644 --- a/examples/circle/main.rs +++ b/examples/circle/main.rs @@ -11,8 +11,6 @@ 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 @@ -20,15 +18,15 @@ use std::mem; // 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() { @@ -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); @@ -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())); } } @@ -234,15 +232,15 @@ fn create_vertex_points_for_circle() -> Vec { 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), }); } } @@ -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 } diff --git a/examples/compute/compute-argument-buffer.rs b/examples/compute/compute-argument-buffer.rs index 97527091..187d4690 100644 --- a/examples/compute/compute-argument-buffer.rs +++ b/examples/compute/compute-argument-buffer.rs @@ -22,16 +22,16 @@ fn main() { ]; let buffer = device.new_buffer_with_data( - unsafe { mem::transmute(data.as_ptr()) }, - (data.len() * mem::size_of::()) 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::()) as u64, + data.as_ptr().cast(), + mem::size_of_val(&data) as u64, MTLResourceOptions::CPUCacheModeDefaultCache, ) }; diff --git a/examples/compute/main.rs b/examples/compute/main.rs index b654a666..85b54995 100644 --- a/examples/compute/main.rs +++ b/examples/compute/main.rs @@ -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::()) 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::()) as u64, + data.as_ptr().cast(), + size_of_val(data.as_slice()) as u64, MTLResourceOptions::CPUCacheModeDefaultCache, ) }; diff --git a/examples/headless-render/main.rs b/examples/headless-render/main.rs index 59664081..b4a595d0 100644 --- a/examples/headless-render/main.rs +++ b/examples/headless-render/main.rs @@ -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::()) as u64, + mem::size_of_val(&VERTEX_ATTRIBS) as u64, MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged, ) } diff --git a/examples/mesh-shader/main.rs b/examples/mesh-shader/main.rs index 27b6dcbe..b10b941e 100644 --- a/examples/mesh-shader/main.rs +++ b/examples/mesh-shader/main.rs @@ -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}, @@ -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); @@ -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())); } } diff --git a/examples/raytracing/geometry.rs b/examples/raytracing/geometry.rs index e55d1aed..fe70453b 100644 --- a/examples/raytracing/geometry.rs +++ b/examples/raytracing/geometry.rs @@ -1,7 +1,4 @@ -use std::{ - mem::{size_of, transmute}, - sync::Arc, -}; +use std::sync::Arc; use glam::{ f32::{Mat4, Vec3, Vec4}, @@ -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]; @@ -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] = [ @@ -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, ); } @@ -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::()) 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::()) 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::()) 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::()) 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::()) 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() @@ -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::()) 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() @@ -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::()) 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() diff --git a/examples/raytracing/main.rs b/examples/raytracing/main.rs index 4c4aed12..a7daa614 100644 --- a/examples/raytracing/main.rs +++ b/examples/raytracing/main.rs @@ -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, @@ -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); @@ -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())); } } diff --git a/examples/raytracing/renderer.rs b/examples/raytracing/renderer.rs index 4f760d95..41898cf0 100644 --- a/examples/raytracing/renderer.rs +++ b/examples/raytracing/renderer.rs @@ -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}, }; @@ -107,15 +107,15 @@ 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::().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::().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!") @@ -123,8 +123,8 @@ impl Renderer { } } let resource_buffer = device.new_buffer_with_data( - resource_buffer_data.as_ptr() as *const c_void, - (resource_buffer_data.len() * size_of::()) 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"); @@ -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; @@ -174,8 +173,7 @@ impl Renderer { } let instance_buffer = device.new_buffer_with_data( instance_descriptors.as_ptr() as *const c_void, - (size_of::() - * 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"); diff --git a/examples/raytracing/scene.rs b/examples/raytracing/scene.rs index 8ecf8568..c8fc51fe 100644 --- a/examples/raytracing/scene.rs +++ b/examples/raytracing/scene.rs @@ -1,4 +1,4 @@ -use std::{ffi::c_void, mem::size_of, sync::Arc}; +use std::{ffi::c_void, sync::Arc}; use glam::{Mat4, Vec3, Vec4}; use rand::{thread_rng, Rng}; @@ -11,7 +11,7 @@ pub struct Scene { pub device: Device, pub camera: Camera, pub geometries: Vec>, - pub geometry_instances: Vec>, + pub geometry_instances: Vec, pub lights: Vec, pub lights_buffer: Buffer, } @@ -83,24 +83,24 @@ impl Scene { for x in -1..2 { let transform = Mat4::from_translation(Vec3::new(x as f32 * 2.5, y as f32 * 2.5, 0.0)); - geometry_instances.push(Arc::new(GeometryInstance { + geometry_instances.push(GeometryInstance { geometry: light_mesh.clone(), transform, mask: GEOMETRY_MASK_LIGHT, index_in_scene: 0, - })); - geometry_instances.push(Arc::new(GeometryInstance { + }); + geometry_instances.push(GeometryInstance { geometry: geometry_mesh.clone(), transform, mask: GEOMETRY_MASK_TRIANGLE, index_in_scene: 1, - })); - geometry_instances.push(Arc::new(GeometryInstance { + }); + geometry_instances.push(GeometryInstance { geometry: sphere_geometry.clone(), transform, mask: GEOMETRY_MASK_SPHERE, index_in_scene: 2, - })); + }); lights.push(AreaLight { position: Vec4::new(x as f32 * 2.5, y as f32 * 2.5 + 1.98, 0.0, 0.0), forward: Vec4::new(0.0, -1.0, 0.0, 0.0), @@ -117,7 +117,7 @@ impl Scene { } let lights_buffer = device.new_buffer_with_data( lights.as_ptr() as *const c_void, - (lights.len() * size_of::()) as NSUInteger, + size_of_val(lights.as_slice()) as NSUInteger, get_managed_buffer_storage_mode(), ); lights_buffer.did_modify_range(NSRange::new(0, lights_buffer.length())); diff --git a/examples/shader-dylib/main.rs b/examples/shader-dylib/main.rs index 143f22dc..376822a0 100644 --- a/examples/shader-dylib/main.rs +++ b/examples/shader-dylib/main.rs @@ -10,8 +10,6 @@ use winit::{ raw_window_handle::{HasWindowHandle, RawWindowHandle}, }; -use std::mem; - struct App { pub _device: Device, pub command_queue: CommandQueue, @@ -23,13 +21,7 @@ struct App { fn select_device() -> Option { let devices = Device::all(); - for device in devices { - if device.supports_dynamic_libraries() { - return Some(device); - } - } - - None + devices.into_iter().find(|d| d.supports_dynamic_libraries()) } impl App { @@ -37,7 +29,7 @@ impl App { let device = select_device().expect("no device found that supports dynamic libraries"); let command_queue = device.new_command_queue(); - 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); @@ -46,7 +38,7 @@ impl App { 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())); } } let draw_size = window.inner_size(); diff --git a/examples/texture/Cargo.toml b/examples/texture/Cargo.toml index 5dfdaece..33117bd0 100644 --- a/examples/texture/Cargo.toml +++ b/examples/texture/Cargo.toml @@ -18,4 +18,4 @@ winit = "0.29" [dependencies.objc] version = "0.2.4" -features = ["objc_exception"] +# features = ["objc_exception"] diff --git a/examples/texture/build.rs b/examples/texture/build.rs index 0ce24960..3d0bf9dd 100644 --- a/examples/texture/build.rs +++ b/examples/texture/build.rs @@ -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() diff --git a/examples/window/main.rs b/examples/window/main.rs index 276696c4..da6397ce 100644 --- a/examples/window/main.rs +++ b/examples/window/main.rs @@ -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, @@ -96,7 +96,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); @@ -105,7 +105,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())); } } @@ -202,13 +202,13 @@ fn main() { std::ptr::copy( vertex_data.as_ptr(), p as *mut f32, - (vertex_data.len() * mem::size_of::()), + size_of_val(&vertex_data), ); } vbuf.did_modify_range(crate::NSRange::new( 0_u64, - (vertex_data.len() * mem::size_of::()) as u64, + size_of_val(&vertex_data) as u64, )); let drawable = match layer.next_drawable() { diff --git a/src/capturemanager.rs b/src/capturemanager.rs index 7c9c407c..2a79b6b9 100644 --- a/src/capturemanager.rs +++ b/src/capturemanager.rs @@ -78,6 +78,7 @@ impl CaptureManagerRef { /// 2. Setting the environment variable `METAL_CAPTURE_ENABLED=1` /// 3. Adding an info.plist file containing the `MetalCaptureEnabled` key set to `YES` pub fn start_capture(&self, descriptor: &CaptureDescriptorRef) -> Result<(), String> { + #[allow(clippy::unit_arg)] unsafe { Ok(try_objc! { err => msg_send![self, startCaptureWithDescriptor: descriptor diff --git a/src/counters.rs b/src/counters.rs index b02fbb96..e984ffba 100644 --- a/src/counters.rs +++ b/src/counters.rs @@ -71,7 +71,7 @@ impl CounterSampleBufferRef { pub fn resolve_counter_range(&self, range: crate::NSRange) -> Vec { let mut data = vec![0 as NSUInteger; range.length as usize]; - let total_bytes = range.length * mem::size_of::() as u64; + let total_bytes = mem::size_of_val(data.as_slice()) as u64; unsafe { let ns_data: *mut crate::Object = msg_send![self, resolveCounterRange: range]; let () = msg_send![ns_data, getBytes: data.as_mut_ptr() length: total_bytes]; diff --git a/src/device.rs b/src/device.rs index 4fd76cf3..21824172 100644 --- a/src/device.rs +++ b/src/device.rs @@ -107,7 +107,7 @@ bitflags::bitflags! { #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] -enum OS { +enum Os { iOS, tvOS, macOS, @@ -118,14 +118,14 @@ const MB: u32 = 1024 * KB; const GB: u32 = 1024 * MB; impl MTLFeatureSet { - fn os(&self) -> OS { + fn os(&self) -> Os { let value = *self as u64; if value < 10_000 { - OS::iOS + Os::iOS } else if value < 20_000 { - OS::macOS + Os::macOS } else if value >= 30_000 || value < 40_000 { - OS::tvOS + Os::tvOS } else { unreachable!() } @@ -208,369 +208,369 @@ impl MTLFeatureSet { pub fn supports_metal_performance_shaders(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 2, - OS::tvOS => true, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 2, + Os::tvOS => true, + Os::macOS => self.os_version() >= 13, } } pub fn supports_programmable_blending(&self) -> bool { - self.os() != OS::macOS + self.os() != Os::macOS } pub fn supports_pvrtc_pixel_formats(&self) -> bool { - self.os() != OS::macOS + self.os() != Os::macOS } pub fn supports_eac_etc_pixel_formats(&self) -> bool { - self.os() != OS::macOS + self.os() != Os::macOS } pub fn supports_astc_pixel_formats(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 2, - OS::tvOS => true, - OS::macOS => false, + Os::iOS => self.gpu_family() >= 2, + Os::tvOS => true, + Os::macOS => false, } } pub fn supports_linear_textures(&self) -> bool { - self.os() != OS::macOS || self.os_version() >= 13 + self.os() != Os::macOS || self.os_version() >= 13 } pub fn supports_bc_pixel_formats(&self) -> bool { - self.os() == OS::macOS + self.os() == Os::macOS } pub fn supports_msaa_depth_resolve(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => false, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => false, } } pub fn supports_counting_occlusion_query(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, } } pub fn supports_base_vertex_instance_drawing(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, } } pub fn supports_indirect_buffers(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, } } pub fn supports_cube_map_texture_arrays(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 4, - OS::tvOS => false, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 4, + Os::tvOS => false, + Os::macOS => true, } } pub fn supports_texture_barriers(&self) -> bool { - self.os() == OS::macOS + self.os() == Os::macOS } pub fn supports_layered_rendering(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 5, - OS::tvOS => false, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 5, + Os::tvOS => false, + Os::macOS => true, } } pub fn supports_tessellation(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => self.os_version() >= 12, } } pub fn supports_resource_heaps(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 13, } } pub fn supports_memoryless_render_targets(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => false, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => false, } } pub fn supports_function_specialization(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 12, } } pub fn supports_function_buffer_read_writes(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => self.os_version() >= 12, } } pub fn supports_function_texture_read_writes(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 4, - OS::tvOS => false, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.gpu_family() >= 4, + Os::tvOS => false, + Os::macOS => self.os_version() >= 12, } } pub fn supports_array_of_textures(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => self.os_version() >= 13, } } pub fn supports_array_of_samplers(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 11, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 11, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => self.os_version() >= 12, } } pub fn supports_stencil_texture_views(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 12, } } pub fn supports_depth_16_pixel_format(&self) -> bool { - self.os() == OS::macOS && self.os_version() >= 12 + self.os() == Os::macOS && self.os_version() >= 12 } pub fn supports_extended_range_pixel_formats(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => false, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => false, } } pub fn supports_wide_color_pixel_format(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 11, - OS::tvOS => self.os_version() >= 11, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 11, + Os::tvOS => self.os_version() >= 11, + Os::macOS => self.os_version() >= 13, } } pub fn supports_combined_msaa_store_and_resolve_action(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.gpu_family() >= 3 && self.os_version() >= 10, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => self.os_version() >= 12, } } pub fn supports_deferred_store_action(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 12, } } pub fn supports_msaa_blits(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => true, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => true, } } pub fn supports_srgb_writes(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3 || (self.gpu_family() >= 2 && self.version() >= 3), - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.gpu_family() >= 2, + Os::iOS => self.gpu_family() >= 3 || (self.gpu_family() >= 2 && self.version() >= 3), + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.gpu_family() >= 2, } } pub fn supports_16_bit_unsigned_integer_coordinates(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 12, } } pub fn supports_extract_insert_and_reverse_bits(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 12, } } pub fn supports_simd_barrier(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 13, } } pub fn supports_sampler_max_anisotropy(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 13, } } pub fn supports_sampler_lod_clamp(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 10, - OS::tvOS => self.os_version() >= 10, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 10, + Os::tvOS => self.os_version() >= 10, + Os::macOS => self.os_version() >= 13, } } pub fn supports_border_color(&self) -> bool { - self.os() == OS::macOS && self.os_version() >= 12 + self.os() == Os::macOS && self.os_version() >= 12 } pub fn supports_dual_source_blending(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 11, - OS::tvOS => self.os_version() >= 11, - OS::macOS => self.os_version() >= 12, + Os::iOS => self.os_version() >= 11, + Os::tvOS => self.os_version() >= 11, + Os::macOS => self.os_version() >= 12, } } pub fn supports_argument_buffers(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 11, - OS::tvOS => self.os_version() >= 11, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 11, + Os::tvOS => self.os_version() >= 11, + Os::macOS => self.os_version() >= 13, } } pub fn supports_programmable_sample_positions(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 11, - OS::tvOS => self.os_version() >= 11, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 11, + Os::tvOS => self.os_version() >= 11, + Os::macOS => self.os_version() >= 13, } } pub fn supports_uniform_type(&self) -> bool { match self.os() { - OS::iOS => self.os_version() >= 11, - OS::tvOS => self.os_version() >= 11, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.os_version() >= 11, + Os::tvOS => self.os_version() >= 11, + Os::macOS => self.os_version() >= 13, } } pub fn supports_imageblocks(&self) -> bool { - self.os() == OS::iOS && self.gpu_family() >= 4 + self.os() == Os::iOS && self.gpu_family() >= 4 } pub fn supports_tile_shaders(&self) -> bool { - self.os() == OS::iOS && self.gpu_family() >= 4 + self.os() == Os::iOS && self.gpu_family() >= 4 } pub fn supports_imageblock_sample_coverage_control(&self) -> bool { - self.os() == OS::iOS && self.gpu_family() >= 4 + self.os() == Os::iOS && self.gpu_family() >= 4 } pub fn supports_threadgroup_sharing(&self) -> bool { - self.os() == OS::iOS && self.gpu_family() >= 4 + self.os() == Os::iOS && self.gpu_family() >= 4 } pub fn supports_post_depth_coverage(&self) -> bool { - self.os() == OS::iOS && self.gpu_family() >= 4 + self.os() == Os::iOS && self.gpu_family() >= 4 } pub fn supports_quad_scoped_permute_operations(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 4, - OS::tvOS => false, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 4, + Os::tvOS => false, + Os::macOS => self.os_version() >= 13, } } pub fn supports_raster_order_groups(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 4, - OS::tvOS => false, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 4, + Os::tvOS => false, + Os::macOS => self.os_version() >= 13, } } pub fn supports_non_uniform_threadgroup_size(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 4, - OS::tvOS => false, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 4, + Os::tvOS => false, + Os::macOS => self.os_version() >= 13, } } pub fn supports_multiple_viewports(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 5, - OS::tvOS => false, - OS::macOS => self.os_version() >= 13, + Os::iOS => self.gpu_family() >= 5, + Os::tvOS => false, + Os::macOS => self.os_version() >= 13, } } pub fn supports_device_notifications(&self) -> bool { - self.os() == OS::macOS && self.os_version() >= 13 + self.os() == Os::macOS && self.os_version() >= 13 } pub fn supports_stencil_feedback(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 5, - OS::tvOS => false, - OS::macOS => self.gpu_family() >= 2, + Os::iOS => self.gpu_family() >= 5, + Os::tvOS => false, + Os::macOS => self.gpu_family() >= 2, } } pub fn supports_stencil_resolve(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 5, - OS::tvOS => false, - OS::macOS => self.gpu_family() >= 2, + Os::iOS => self.gpu_family() >= 5, + Os::tvOS => false, + Os::macOS => self.gpu_family() >= 2, } } pub fn supports_binary_archive(&self) -> bool { match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 3, - OS::macOS => self.gpu_family() >= 1, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 3, + Os::macOS => self.gpu_family() >= 1, } } @@ -583,7 +583,7 @@ impl MTLFeatureSet { } pub fn max_texture_argument_entries(&self) -> u32 { - if self.os() == OS::macOS { + if self.os() == Os::macOS { 128 } else { 31 @@ -599,7 +599,7 @@ impl MTLFeatureSet { } pub fn max_inlined_constant_data_buffers(&self) -> u32 { - if self.os() == OS::macOS { + if self.os() == Os::macOS { 14 } else { 31 @@ -611,7 +611,7 @@ impl MTLFeatureSet { } pub fn max_threads_per_threadgroup(&self) -> u32 { - if self.os() == OS::macOS || self.gpu_family() >= 4 { + if self.os() == Os::macOS || self.gpu_family() >= 4 { 1024 } else { 512 @@ -620,24 +620,24 @@ impl MTLFeatureSet { pub fn max_total_threadgroup_memory_allocation(&self) -> u32 { match (self.os(), self.gpu_family()) { - (OS::iOS, 5) => 64 * KB, - (OS::iOS, 4) => { + (Os::iOS, 5) => 64 * KB, + (Os::iOS, 4) => { if self.os_version() >= 12 { 64 * KB } else { 32 * KB } } - (OS::iOS, 3) => 16 * KB, - (OS::iOS, _) => 16 * KB - 32, - (OS::tvOS, 1) => 16 * KB - 32, - (OS::tvOS, _) => 16 * KB, - (OS::macOS, _) => 32 * KB, + (Os::iOS, 3) => 16 * KB, + (Os::iOS, _) => 16 * KB - 32, + (Os::tvOS, 1) => 16 * KB - 32, + (Os::tvOS, _) => 16 * KB, + (Os::macOS, _) => 32 * KB, } } pub fn max_total_tile_memory_allocation(&self) -> u32 { - if self.os() == OS::iOS && self.gpu_family() == 4 { + if self.os() == Os::iOS && self.gpu_family() == 4 { 32 * KB } else { 0 @@ -649,7 +649,7 @@ impl MTLFeatureSet { } pub fn max_constant_buffer_function_memory_allocation(&self) -> Option { - if self.os() == OS::macOS { + if self.os() == Os::macOS { Some(64 * KB) } else { None @@ -657,7 +657,7 @@ impl MTLFeatureSet { } pub fn max_fragment_inputs(&self) -> u32 { - if self.os() == OS::macOS { + if self.os() == Os::macOS { 32 } else { 60 @@ -665,7 +665,7 @@ impl MTLFeatureSet { } pub fn max_fragment_input_components(&self) -> u32 { - if self.os() == OS::macOS { + if self.os() == Os::macOS { 128 } else { 60 @@ -674,9 +674,9 @@ impl MTLFeatureSet { pub fn max_function_constants(&self) -> u32 { match self.os() { - OS::iOS if self.os_version() >= 11 => 65536, - OS::tvOS if self.os_version() >= 10 => 65536, - OS::macOS if self.os_version() >= 12 => 65536, + Os::iOS if self.os_version() >= 11 => 65536, + Os::tvOS if self.os_version() >= 10 => 65536, + Os::macOS if self.os_version() >= 12 => 65536, _ => 0, } } @@ -684,10 +684,10 @@ impl MTLFeatureSet { pub fn max_tessellation_factor(&self) -> u32 { if self.supports_tessellation() { match self.os() { - OS::iOS if self.gpu_family() >= 5 => 64, - OS::iOS => 16, - OS::tvOS => 16, - OS::macOS => 64, + Os::iOS if self.gpu_family() >= 5 => 64, + Os::iOS => 16, + Os::tvOS => 16, + Os::macOS => 64, } } else { 0 @@ -711,7 +711,8 @@ impl MTLFeatureSet { } pub fn max_buffer_length(&self) -> u32 { - if self.os() == OS::macOS && self.os_version() >= 12 { + #[allow(clippy::identity_op)] + if self.os() == Os::macOS && self.os_version() >= 12 { 1 * GB } else { 256 * MB @@ -719,7 +720,7 @@ impl MTLFeatureSet { } pub fn min_buffer_offset_alignment(&self) -> u32 { - if self.os() == OS::macOS { + if self.os() == Os::macOS { 256 } else { 4 @@ -728,42 +729,42 @@ impl MTLFeatureSet { pub fn max_1d_texture_size(&self) -> u32 { match (self.os(), self.gpu_family()) { - (OS::iOS, 1) | (OS::iOS, 2) => { + (Os::iOS, 1) | (Os::iOS, 2) => { if self.version() <= 2 { 4096 } else { 8192 } } - (OS::tvOS, 1) => 8192, + (Os::tvOS, 1) => 8192, _ => 16384, } } pub fn max_2d_texture_size(&self) -> u32 { match (self.os(), self.gpu_family()) { - (OS::iOS, 1) | (OS::iOS, 2) => { + (Os::iOS, 1) | (Os::iOS, 2) => { if self.version() <= 2 { 4096 } else { 8192 } } - (OS::tvOS, 1) => 8192, + (Os::tvOS, 1) => 8192, _ => 16384, } } pub fn max_cube_map_texture_size(&self) -> u32 { match (self.os(), self.gpu_family()) { - (OS::iOS, 1) | (OS::iOS, 2) => { + (Os::iOS, 1) | (Os::iOS, 2) => { if self.version() <= 2 { 4096 } else { 8192 } } - (OS::tvOS, 1) => 8192, + (Os::tvOS, 1) => 8192, _ => 16384, } } @@ -778,9 +779,9 @@ impl MTLFeatureSet { pub fn copy_texture_buffer_alignment(&self) -> u32 { match (self.os(), self.gpu_family()) { - (OS::iOS, 1) | (OS::iOS, 2) | (OS::tvOS, 1) => 64, - (OS::iOS, _) | (OS::tvOS, _) => 16, - (OS::macOS, _) => 256, + (Os::iOS, 1) | (Os::iOS, 2) | (Os::tvOS, 1) => 64, + (Os::iOS, _) | (Os::tvOS, _) => 16, + (Os::macOS, _) => 256, } } @@ -788,7 +789,7 @@ impl MTLFeatureSet { /// the buffer alignment can be discovered via API query pub fn new_texture_buffer_alignment(&self) -> Option { match self.os() { - OS::iOS => { + Os::iOS => { if self.os_version() >= 11 { None } else if self.gpu_family() == 3 { @@ -797,19 +798,19 @@ impl MTLFeatureSet { Some(64) } } - OS::tvOS => { + Os::tvOS => { if self.os_version() >= 11 { None } else { Some(64) } } - OS::macOS => None, + Os::macOS => None, } } pub fn max_color_render_targets(&self) -> u32 { - if self.os() == OS::iOS && self.gpu_family() == 1 { + if self.os() == Os::iOS && self.gpu_family() == 1 { 4 } else { 8 @@ -822,11 +823,11 @@ impl MTLFeatureSet { pub fn max_total_color_render_target_size(&self) -> Option { match (self.os(), self.gpu_family()) { - (OS::iOS, 1) => Some(128), - (OS::iOS, 2) | (OS::iOS, 3) => Some(256), - (OS::iOS, _) => Some(512), - (OS::tvOS, _) => Some(256), - (OS::macOS, _) => None, + (Os::iOS, 1) => Some(128), + (Os::iOS, 2) | (Os::iOS, 3) => Some(256), + (Os::iOS, _) => Some(512), + (Os::tvOS, _) => Some(256), + (Os::macOS, _) => None, } } @@ -843,7 +844,7 @@ impl MTLFeatureSet { } pub fn r8_unorm_srgb_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else if self.supports_srgb_writes() { PixelFormatCapabilities::all() @@ -853,7 +854,7 @@ impl MTLFeatureSet { } pub fn r8_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.gpu_family() == 1 { + if self.os() == Os::iOS && self.gpu_family() == 1 { !PixelFormatCapabilities::Resolve } else { PixelFormatCapabilities::all() @@ -873,7 +874,7 @@ impl MTLFeatureSet { } pub fn r16_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() != OS::macOS { + if self.os() != Os::macOS { !PixelFormatCapabilities::Resolve } else { PixelFormatCapabilities::all() @@ -881,7 +882,7 @@ impl MTLFeatureSet { } pub fn r16_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() != OS::macOS { + if self.os() != Os::macOS { !PixelFormatCapabilities::Resolve } else { PixelFormatCapabilities::all() @@ -909,7 +910,7 @@ impl MTLFeatureSet { } pub fn rg8_unorm_srgb_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else if self.supports_srgb_writes() { PixelFormatCapabilities::all() @@ -919,7 +920,7 @@ impl MTLFeatureSet { } pub fn rg8_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.gpu_family() == 1 { + if self.os() == Os::iOS && self.gpu_family() == 1 { !PixelFormatCapabilities::Resolve } else { PixelFormatCapabilities::all() @@ -939,7 +940,7 @@ impl MTLFeatureSet { } pub fn b5_g6_r5_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else { !PixelFormatCapabilities::Write @@ -947,7 +948,7 @@ impl MTLFeatureSet { } pub fn a1_bgr5_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else { !PixelFormatCapabilities::Write @@ -955,7 +956,7 @@ impl MTLFeatureSet { } pub fn abgr4_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else { !PixelFormatCapabilities::Write @@ -963,7 +964,7 @@ impl MTLFeatureSet { } pub fn bgr5_a1_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::empty() } else { !PixelFormatCapabilities::Write @@ -971,9 +972,9 @@ impl MTLFeatureSet { } pub fn r32_uint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -983,9 +984,9 @@ impl MTLFeatureSet { } pub fn r32_sint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -995,11 +996,11 @@ impl MTLFeatureSet { } pub fn r32_float_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color | PixelFormatCapabilities::Blend | PixelFormatCapabilities::Msaa - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::all() } else { PixelFormatCapabilities::Write @@ -1010,7 +1011,7 @@ impl MTLFeatureSet { } pub fn rg16_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() } else { !PixelFormatCapabilities::Resolve @@ -1018,7 +1019,7 @@ impl MTLFeatureSet { } pub fn rg16_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() } else { !PixelFormatCapabilities::Resolve @@ -1054,7 +1055,7 @@ impl MTLFeatureSet { } pub fn rgba8_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.gpu_family() == 1 { + if self.os() == Os::iOS && self.gpu_family() == 1 { !PixelFormatCapabilities::Resolve } else { PixelFormatCapabilities::all() @@ -1087,9 +1088,9 @@ impl MTLFeatureSet { pub fn rgb10_a2_unorm_capabilities(&self) -> PixelFormatCapabilities { let supports_writes = match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, }; if supports_writes { PixelFormatCapabilities::all() @@ -1100,9 +1101,9 @@ impl MTLFeatureSet { pub fn rgb10_a2_uint_capabilities(&self) -> PixelFormatCapabilities { let supports_writes = match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, }; if supports_writes { PixelFormatCapabilities::Write @@ -1115,9 +1116,9 @@ impl MTLFeatureSet { pub fn rg11_b10_float_capabilities(&self) -> PixelFormatCapabilities { let supports_writes = match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => true, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => true, }; if supports_writes { PixelFormatCapabilities::all() @@ -1127,13 +1128,13 @@ impl MTLFeatureSet { } pub fn rgb9_e5_float_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::Filter } else { let supports_writes = match self.os() { - OS::iOS => self.gpu_family() >= 3, - OS::tvOS => self.gpu_family() >= 2, - OS::macOS => false, + Os::iOS => self.gpu_family() >= 3, + Os::tvOS => self.gpu_family() >= 2, + Os::macOS => false, }; if supports_writes { PixelFormatCapabilities::all() @@ -1144,9 +1145,9 @@ impl MTLFeatureSet { } pub fn rg32_uint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -1156,9 +1157,9 @@ impl MTLFeatureSet { } pub fn rg32_sint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -1168,9 +1169,9 @@ impl MTLFeatureSet { } pub fn rg32_float_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() - } else if self.os() == OS::iOS && self.os_version() == 8 { + } else if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color | PixelFormatCapabilities::Blend } else { PixelFormatCapabilities::Write @@ -1180,7 +1181,7 @@ impl MTLFeatureSet { } pub fn rgba16_unorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() } else { !PixelFormatCapabilities::Write @@ -1188,7 +1189,7 @@ impl MTLFeatureSet { } pub fn rgba16_snorm_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() } else { !PixelFormatCapabilities::Write @@ -1212,9 +1213,9 @@ impl MTLFeatureSet { } pub fn rgba32_uint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -1224,9 +1225,9 @@ impl MTLFeatureSet { } pub fn rgba32_sint_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::iOS && self.os_version() == 8 { + if self.os() == Os::iOS && self.os_version() == 8 { PixelFormatCapabilities::Color - } else if self.os() == OS::macOS { + } else if self.os() == Os::macOS { PixelFormatCapabilities::Color | PixelFormatCapabilities::Write | PixelFormatCapabilities::Msaa @@ -1236,9 +1237,9 @@ impl MTLFeatureSet { } pub fn rgba32_float_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::all() - } else if self.os() == OS::iOS && self.version() == 8 { + } else if self.os() == Os::iOS && self.version() == 8 { PixelFormatCapabilities::Color } else { PixelFormatCapabilities::Write | PixelFormatCapabilities::Color @@ -1296,7 +1297,7 @@ impl MTLFeatureSet { } pub fn depth32_float_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::Filter | PixelFormatCapabilities::Msaa | PixelFormatCapabilities::Resolve @@ -1312,7 +1313,7 @@ impl MTLFeatureSet { } pub fn depth24_unorm_stencil8_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::Filter | PixelFormatCapabilities::Msaa | PixelFormatCapabilities::Resolve @@ -1322,7 +1323,7 @@ impl MTLFeatureSet { } pub fn depth32_float_stencil8_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::Filter | PixelFormatCapabilities::Msaa | PixelFormatCapabilities::Resolve @@ -1334,7 +1335,7 @@ impl MTLFeatureSet { } pub fn x24_stencil8_capabilities(&self) -> PixelFormatCapabilities { - if self.os() == OS::macOS { + if self.os() == Os::macOS { PixelFormatCapabilities::Msaa } else { PixelFormatCapabilities::empty() @@ -1379,7 +1380,7 @@ impl MTLFeatureSet { pub fn bgr10_a2_unorm_capabilities(&self) -> PixelFormatCapabilities { if self.supports_wide_color_pixel_format() { - if self.os() == OS::macOS { + if self.os() == Os::macOS { !PixelFormatCapabilities::Write } else { PixelFormatCapabilities::all() @@ -1475,11 +1476,17 @@ type dispatch_block_t = *const Block<(), ()>; const DISPATCH_DATA_DESTRUCTOR_DEFAULT: dispatch_block_t = ptr::null(); #[cfg_attr( - all(feature = "link", any(target_os = "macos", target_os = "ios", target_os = "visionos")), + all( + feature = "link", + any(target_os = "macos", target_os = "ios", target_os = "visionos") + ), link(name = "System", kind = "dylib") )] #[cfg_attr( - all(feature = "link", not(any(target_os = "macos", target_os = "ios", target_os = "visionos"))), + all( + feature = "link", + not(any(target_os = "macos", target_os = "ios", target_os = "visionos")) + ), link(name = "dispatch", kind = "dylib") )] #[allow(improper_ctypes)] @@ -2121,14 +2128,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() } } } diff --git a/src/encoder.rs b/src/encoder.rs index e737a2f2..65c16309 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -1030,6 +1030,7 @@ impl RenderCommandEncoderRef { } } + #[allow(clippy::too_many_arguments)] pub fn draw_indexed_primitives_instanced_base_instance( &self, primitive_type: MTLPrimitiveType, @@ -1339,6 +1340,7 @@ impl BlitCommandEncoderRef { } } + #[allow(clippy::too_many_arguments)] pub fn copy_from_texture( &self, source_texture: &TextureRef, @@ -1366,6 +1368,7 @@ impl BlitCommandEncoderRef { } } + #[allow(clippy::too_many_arguments)] pub fn copy_from_buffer_to_texture( &self, source_buffer: &BufferRef, @@ -1396,6 +1399,7 @@ impl BlitCommandEncoderRef { } /// See + #[allow(clippy::too_many_arguments)] pub fn copy_from_texture_to_buffer( &self, source_texture: &TextureRef, diff --git a/src/indirect_encoder.rs b/src/indirect_encoder.rs index 5880bf07..236ec87f 100644 --- a/src/indirect_encoder.rs +++ b/src/indirect_encoder.rs @@ -171,6 +171,7 @@ impl IndirectRenderCommandRef { } } + #[allow(clippy::too_many_arguments)] pub fn draw_indexed_primitives( &self, primitive_type: MTLPrimitiveType, @@ -196,6 +197,7 @@ impl IndirectRenderCommandRef { } } + #[allow(clippy::too_many_arguments)] pub fn draw_patches( &self, number_of_patch_control_points: NSUInteger, @@ -225,6 +227,7 @@ impl IndirectRenderCommandRef { } } + #[allow(clippy::too_many_arguments)] pub fn draw_indexed_patches( &self, number_of_patch_control_points: NSUInteger, diff --git a/src/lib.rs b/src/lib.rs index 0b5c6d7d..3a2d68c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ #![allow(deprecated)] #![allow(non_snake_case)] #![allow(non_upper_case_globals)] +#![allow(clippy::new_without_default, clippy::new_ret_no_self)] #[macro_use] pub extern crate objc; @@ -148,7 +149,7 @@ macro_rules! foreign_obj_type { impl ::std::convert::From<$owned_ident> for $parent_ident { fn from(item: $owned_ident) -> Self { - unsafe { Self::from_ptr(::std::mem::transmute(item.into_ptr())) } + unsafe { Self::from_ptr(item.into_ptr().cast()) } } } }; @@ -349,7 +350,7 @@ where #[inline] fn deref(&self) -> &ArrayRef { - unsafe { mem::transmute(self.as_ptr()) } + unsafe { &*(self.as_ptr() as *const ArrayRef) } } } @@ -359,7 +360,7 @@ where T::Ref: objc::Message + 'static, { fn borrow(&self) -> &ArrayRef { - unsafe { mem::transmute(self.as_ptr()) } + unsafe { &*(self.as_ptr() as *const ArrayRef) } } } diff --git a/src/library.rs b/src/library.rs index 5b570627..d4778eaa 100644 --- a/src/library.rs +++ b/src/library.rs @@ -436,10 +436,14 @@ impl CompileOptions { } impl CompileOptionsRef { + /// # Safety + /// TODO pub unsafe fn preprocessor_macros(&self) -> *mut Object { msg_send![self, preprocessorMacros] } + /// # Safety + /// TODO pub unsafe fn set_preprocessor_macros(&self, defines: *mut Object) { msg_send![self, setPreprocessorMacros: defines] } @@ -495,13 +499,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() } } @@ -611,13 +614,12 @@ impl LibraryRef { unsafe { let names: *mut Object = msg_send![self, functionNames]; let count: NSUInteger = msg_send![names, count]; - let ret = (0..count) + (0..count) .map(|i| { let name = msg_send![names, objectAtIndex: i]; nsstring_as_str(name).to_string() }) - .collect(); - ret + .collect() } } @@ -859,13 +861,12 @@ impl LinkedFunctionsRef { 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() } } @@ -880,13 +881,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() } } diff --git a/src/mps.rs b/src/mps.rs index 3fae97a0..22c8d3d2 100644 --- a/src/mps.rs +++ b/src/mps.rs @@ -449,13 +449,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() } } diff --git a/src/pipeline/compute.rs b/src/pipeline/compute.rs index 4602078b..12e59eb7 100644 --- a/src/pipeline/compute.rs +++ b/src/pipeline/compute.rs @@ -186,13 +186,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 +207,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() } } diff --git a/src/pipeline/render.rs b/src/pipeline/render.rs index 57d793f7..a69f1609 100644 --- a/src/pipeline/render.rs +++ b/src/pipeline/render.rs @@ -647,13 +647,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() } } diff --git a/src/sync.rs b/src/sync.rs index 5895c4dd..e6a60195 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -90,6 +90,8 @@ foreign_obj_type! { } impl SharedEventListener { + /// # Safety + /// TODO pub unsafe fn from_queue_handle(queue: dispatch_queue_t) -> Self { let listener: SharedEventListener = msg_send![class!(MTLSharedEventListener), alloc]; let ptr: *mut Object = msg_send![listener.as_ref(), initWithDispatchQueue: queue];