diff --git a/Cargo.toml b/Cargo.toml index cf92b46f..c85488b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,16 +21,14 @@ private = [] mps = [] [dependencies] -core-graphics-types = "0.1" +# Branch: objc2. TODO: Remove this +core-graphics-types = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" } bitflags = "1" log = "0.4" -block = "0.1.6" +block2 = "=0.2.0-alpha.6" foreign-types = "0.3.2" dispatch = { version = "0.2", optional = true } - -[dependencies.objc] -version = "0.2.4" -features = ["objc_exception"] +objc2 = "=0.3.0-beta.3" [dev-dependencies] cocoa = "0.24.0" diff --git a/examples/argument-buffer/main.rs b/examples/argument-buffer/main.rs index 23e88990..8e0f4326 100644 --- a/examples/argument-buffer/main.rs +++ b/examples/argument-buffer/main.rs @@ -6,10 +6,10 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); /* diff --git a/examples/bind/main.rs b/examples/bind/main.rs index 811b1c5a..13e57bd7 100644 --- a/examples/bind/main.rs +++ b/examples/bind/main.rs @@ -6,10 +6,10 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); let buffer = device.new_buffer(4, MTLResourceOptions::empty()); diff --git a/examples/bindless/main.rs b/examples/bindless/main.rs index 09c3a59a..e7e90ed1 100644 --- a/examples/bindless/main.rs +++ b/examples/bindless/main.rs @@ -6,7 +6,7 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; const BINDLESS_TEXTURE_COUNT: NSUInteger = 100_000; // ~25Mb @@ -16,7 +16,7 @@ const BINDLESS_TEXTURE_COUNT: NSUInteger = 100_000; // ~25Mb /// - How to create bindless resources via Metal's argument buffers. /// - How to bind argument buffer to render encoder fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); /* diff --git a/examples/circle/main.rs b/examples/circle/main.rs index 04c521fe..f0d8e29f 100644 --- a/examples/circle/main.rs +++ b/examples/circle/main.rs @@ -9,7 +9,8 @@ use winit::{ use cocoa::{appkit::NSView, base::id as cocoa_id}; use core_graphics_types::geometry::CGSize; -use objc::{rc::autoreleasepool, runtime::YES}; +use objc2::rc::autoreleasepool; +use objc2::runtime::Bool; use std::mem; @@ -89,7 +90,7 @@ fn main() { unsafe { let view = window.ns_view() as cocoa_id; - view.setWantsLayer(YES); + view.setWantsLayer(Bool::YES.as_raw()); view.setLayer(mem::transmute(layer.as_ref())); } @@ -108,7 +109,7 @@ fn main() { }; event_loop.run(move |event, _, control_flow| { - autoreleasepool(|| { + autoreleasepool(|_| { // ControlFlow::Wait pauses the event loop if no events are available to process. // This is ideal for non-game applications that only update in response to user // input, and uses significantly less power/CPU time than ControlFlow::Poll. diff --git a/examples/compute/compute-argument-buffer.rs b/examples/compute/compute-argument-buffer.rs index 97527091..cc4535fb 100644 --- a/examples/compute/compute-argument-buffer.rs +++ b/examples/compute/compute-argument-buffer.rs @@ -6,13 +6,13 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; use std::mem; static LIBRARY_SRC: &str = include_str!("compute-argument-buffer.metal"); fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); let command_queue = device.new_command_queue(); diff --git a/examples/compute/embedded-lib.rs b/examples/compute/embedded-lib.rs index 0fd193ab..9f3692f5 100644 --- a/examples/compute/embedded-lib.rs +++ b/examples/compute/embedded-lib.rs @@ -6,12 +6,12 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; fn main() { let library_data = include_bytes!("shaders.metallib"); - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); let library = device.new_library_with_data(&library_data[..]).unwrap(); diff --git a/examples/compute/main.rs b/examples/compute/main.rs index 6497c790..1a1df0a5 100644 --- a/examples/compute/main.rs +++ b/examples/compute/main.rs @@ -6,11 +6,11 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; use std::mem; fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); let command_queue = device.new_command_queue(); @@ -37,7 +37,7 @@ fn main() { let command_buffer = command_queue.new_command_buffer(); command_buffer.set_label("label"); - let block = block::ConcreteBlock::new(move |buffer: &metal::CommandBufferRef| { + let block = block2::ConcreteBlock::new(move |buffer: &metal::CommandBufferRef| { println!("{}", buffer.label()); }) .copy(); diff --git a/examples/events/main.rs b/examples/events/main.rs index 9e4fe0e8..5f482db5 100644 --- a/examples/events/main.rs +++ b/examples/events/main.rs @@ -30,7 +30,7 @@ fn main() { let shared_event_listener = SharedEventListener::from_queue(&my_queue); // Register CPU work - let notify_block = block::ConcreteBlock::new(move |evt: &SharedEventRef, val: u64| { + let notify_block = block2::ConcreteBlock::new(move |evt: &SharedEventRef, val: u64| { println!("Got notification from GPU: {}", val); evt.set_signaled_value(3); }); diff --git a/examples/reflection/main.rs b/examples/reflection/main.rs index 058199cc..c06ffc46 100644 --- a/examples/reflection/main.rs +++ b/examples/reflection/main.rs @@ -6,7 +6,7 @@ // copied, modified, or distributed except according to those terms. use metal::*; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; const PROGRAM: &'static str = r" #include @@ -41,7 +41,7 @@ const PROGRAM: &'static str = r" "; fn main() { - autoreleasepool(|| { + autoreleasepool(|_| { let device = Device::system_default().expect("no device found"); let options = CompileOptions::new(); diff --git a/examples/shader-dylib/main.rs b/examples/shader-dylib/main.rs index 60b6aa0d..b9f52372 100644 --- a/examples/shader-dylib/main.rs +++ b/examples/shader-dylib/main.rs @@ -2,7 +2,8 @@ use cocoa::{appkit::NSView, base::id as cocoa_id}; use core_graphics_types::geometry::CGSize; use metal::*; -use objc::{rc::autoreleasepool, runtime::YES}; +use objc2::rc::autoreleasepool; +use objc2::runtime::Bool; use winit::{ event::{Event, WindowEvent}, @@ -44,7 +45,7 @@ impl App { layer.set_framebuffer_only(false); unsafe { let view = window.ns_view() as cocoa_id; - view.setWantsLayer(YES); + view.setWantsLayer(Bool::YES.as_raw()); view.setLayer(mem::transmute(layer.as_ref())); } let draw_size = window.inner_size(); @@ -153,7 +154,7 @@ fn main() { let mut app = App::new(&window); events_loop.run(move |event, _, control_flow| { - autoreleasepool(|| { + autoreleasepool(|_| { *control_flow = ControlFlow::Poll; match event { diff --git a/examples/texture/Cargo.toml b/examples/texture/Cargo.toml index 8af54a9a..34950f4c 100644 --- a/examples/texture/Cargo.toml +++ b/examples/texture/Cargo.toml @@ -9,13 +9,10 @@ edition = "2018" bindgen = { version = "0.60", default-features = false, features = ["logging", "runtime", "which-rustfmt"] } [dependencies] -core-graphics-types = "0.1" +# Branch: objc2. TODO: Remove this +core-graphics-types = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" } cocoa = "0.24" -core-graphics = "0.22" png = "0.17" metal = { path = "../../" } winit = "0.27" - -[dependencies.objc] -version = "0.2.4" -features = ["objc_exception"] +objc2 = "=0.3.0-beta.3" diff --git a/examples/texture/src/main.rs b/examples/texture/src/main.rs index de7a4765..406b4652 100644 --- a/examples/texture/src/main.rs +++ b/examples/texture/src/main.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use cocoa::{appkit::NSView, base::id as cocoa_id}; use core_graphics_types::geometry::CGSize; -use objc::rc::autoreleasepool; +use objc2::rc::autoreleasepool; use winit::dpi::LogicalSize; use winit::event_loop::{ControlFlow, EventLoop}; use winit::platform::macos::WindowExtMacOS; @@ -58,7 +58,7 @@ fn main() { let command_queue = device.new_command_queue(); event_loop.run(move |event, _, control_flow| { - autoreleasepool(|| { + autoreleasepool(|_| { *control_flow = ControlFlow::Poll; match event { diff --git a/examples/window/main.rs b/examples/window/main.rs index 08936e82..ba439a3a 100644 --- a/examples/window/main.rs +++ b/examples/window/main.rs @@ -5,13 +5,12 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. -extern crate objc; - use cocoa::{appkit::NSView, base::id as cocoa_id}; use core_graphics_types::geometry::CGSize; use metal::*; -use objc::{rc::autoreleasepool, runtime::YES}; +use objc2::rc::autoreleasepool; +use objc2::runtime::Bool; use std::mem; use winit::platform::macos::WindowExtMacOS; @@ -103,7 +102,7 @@ fn main() { unsafe { let view = window.ns_view() as cocoa_id; - view.setWantsLayer(YES); + view.setWantsLayer(Bool::YES.as_raw()); view.setLayer(mem::transmute(layer.as_ref())); } @@ -162,7 +161,7 @@ fn main() { ); events_loop.run(move |event, _, control_flow| { - autoreleasepool(|| { + autoreleasepool(|_| { *control_flow = ControlFlow::Poll; match event { diff --git a/src/argument.rs b/src/argument.rs index 90792170..8474c686 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -6,7 +6,7 @@ // copied, modified, or distributed except according to those terms. use super::{MTLTextureType, NSUInteger}; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[allow(non_camel_case_types)] @@ -107,6 +107,10 @@ pub enum MTLDataType { RGB9E5Float = 77, } +unsafe impl Encode for MTLDataType { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -119,6 +123,10 @@ pub enum MTLArgumentType { Imageblock = 17, } +unsafe impl Encode for MTLArgumentType { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -128,6 +136,10 @@ pub enum MTLArgumentAccess { WriteOnly = 2, } +unsafe impl Encode for MTLArgumentAccess { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLStructMember {} foreign_obj_type! { @@ -258,13 +270,7 @@ impl ArgumentRef { } pub fn is_active(&self) -> bool { - unsafe { - match msg_send![self, isActive] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isActive] } } pub fn buffer_alignment(&self) -> NSUInteger { diff --git a/src/capturedescriptor.rs b/src/capturedescriptor.rs index 6f6e369f..d3270b04 100644 --- a/src/capturedescriptor.rs +++ b/src/capturedescriptor.rs @@ -9,6 +9,8 @@ use super::*; use std::path::Path; +use objc2::{Encode, Encoding}; + /// https://developer.apple.com/documentation/metal/mtlcapturedestination?language=objc #[repr(u64)] #[allow(non_camel_case_types)] @@ -18,6 +20,10 @@ pub enum MTLCaptureDestination { GpuTraceDocument = 2, } +unsafe impl Encode for MTLCaptureDestination { + const ENCODING: Encoding = u64::ENCODING; +} + /// https://developer.apple.com/documentation/metal/mtlcapturedescriptor pub enum MTLCaptureDescriptor {} diff --git a/src/capturemanager.rs b/src/capturemanager.rs index 95180c42..a9788a40 100644 --- a/src/capturemanager.rs +++ b/src/capturemanager.rs @@ -73,10 +73,11 @@ impl CaptureManagerRef { /// https://developer.apple.com/documentation/metal/mtlcapturemanager/3237259-startcapture pub fn start_capture(&self, descriptor: &CaptureDescriptorRef) -> Result<(), String> { unsafe { - try_objc! { err => + let _: () = try_objc! { err => msg_send![self, startCaptureWithDescriptor: descriptor error: &mut err] - } + }; + Ok(()) } } diff --git a/src/commandbuffer.rs b/src/commandbuffer.rs index 32a1d8fb..42674d5d 100644 --- a/src/commandbuffer.rs +++ b/src/commandbuffer.rs @@ -7,7 +7,8 @@ use super::*; -use block::Block; +use block2::Block; +use objc2::{Encode, Encoding}; #[repr(u32)] #[allow(non_camel_case_types)] @@ -21,6 +22,10 @@ pub enum MTLCommandBufferStatus { Error = 5, } +unsafe impl Encode for MTLCommandBufferStatus { + const ENCODING: Encoding = u32::ENCODING; +} + #[repr(u32)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -37,6 +42,10 @@ pub enum MTLCommandBufferError { DeviceRemoved = 11, } +unsafe impl Encode for MTLCommandBufferError { + const ENCODING: Encoding = u32::ENCODING; +} + #[repr(u32)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -45,6 +54,10 @@ pub enum MTLDispatchType { Concurrent = 1, } +unsafe impl Encode for MTLDispatchType { + const ENCODING: Encoding = u32::ENCODING; +} + type CommandBufferHandler<'a> = Block<(&'a CommandBufferRef,), ()>; pub enum MTLCommandBuffer {} @@ -55,6 +68,10 @@ foreign_obj_type! { pub struct CommandBufferRef; } +unsafe impl Encode for CommandBufferRef { + const ENCODING: Encoding = Encoding::Unknown; // TODO +} + impl CommandBufferRef { pub fn label(&self) -> &str { unsafe { diff --git a/src/constants.rs b/src/constants.rs index 0afbdd7e..fbf4f63a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -5,6 +5,8 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. +use objc2::{Encode, Encoding}; + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -149,3 +151,7 @@ pub enum MTLPixelFormat { BGR10_XR = 554, BGR10_XR_SRGB = 555, } + +unsafe impl Encode for MTLPixelFormat { + const ENCODING: Encoding = u64::ENCODING; +} diff --git a/src/depthstencil.rs b/src/depthstencil.rs index 5b578e6d..14e55214 100644 --- a/src/depthstencil.rs +++ b/src/depthstencil.rs @@ -6,7 +6,7 @@ // copied, modified, or distributed except according to those terms. use crate::DeviceRef; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -21,6 +21,10 @@ pub enum MTLCompareFunction { Always = 7, } +unsafe impl Encode for MTLCompareFunction { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLStencilOperation { @@ -34,6 +38,10 @@ pub enum MTLStencilOperation { DecrementWrap = 7, } +unsafe impl Encode for MTLStencilOperation { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLStencilDescriptor {} foreign_obj_type! { @@ -128,13 +136,7 @@ impl DepthStencilDescriptorRef { } pub fn depth_write_enabled(&self) -> bool { - unsafe { - match msg_send![self, isDepthWriteEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isDepthWriteEnabled] } } pub fn set_depth_write_enabled(&self, enabled: bool) { diff --git a/src/device.rs b/src/device.rs index 1cb0a407..1708a74a 100644 --- a/src/device.rs +++ b/src/device.rs @@ -7,9 +7,10 @@ use super::*; -use block::{Block, ConcreteBlock}; +use block2::{Block, ConcreteBlock}; use foreign_types::ForeignType; -use objc::runtime::{Object, NO, YES}; +use objc2::runtime::Object; +use objc2::{Encode, Encoding}; use std::{ffi::CStr, os::raw::c_char, path::Path, ptr}; @@ -52,6 +53,10 @@ pub enum MTLFeatureSet { macOS_GPUFamily2_v1 = 10005, } +unsafe impl Encode for MTLFeatureSet { + const ENCODING: Encoding = u64::ENCODING; +} + // Available on macOS 10.15+, iOS 13.0+ #[repr(i64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -75,6 +80,10 @@ pub enum MTLGPUFamily { MacCatalyst2 = 4002, } +unsafe impl Encode for MTLGPUFamily { + const ENCODING: Encoding = i64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLDeviceLocation { @@ -84,6 +93,10 @@ pub enum MTLDeviceLocation { Unspecified = u64::MAX, } +unsafe impl Encode for MTLDeviceLocation { + const ENCODING: Encoding = u64::ENCODING; +} + bitflags! { pub struct PixelFormatCapabilities: u32 { const Filter = 1 << 0; @@ -1387,6 +1400,10 @@ pub enum MTLArgumentBuffersTier { Tier2 = 1, } +unsafe impl Encode for MTLArgumentBuffersTier { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLReadWriteTextureTier { @@ -1395,6 +1412,10 @@ pub enum MTLReadWriteTextureTier { Tier2 = 2, } +unsafe impl Encode for MTLReadWriteTextureTier { + const ENCODING: Encoding = u64::ENCODING; +} + /// Only available on (macos(11.0), ios(14.0)) #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -1406,6 +1427,10 @@ pub enum MTLCounterSamplingPoint { AtBlitBoundary = 4, } +unsafe impl Encode for MTLCounterSamplingPoint { + const ENCODING: Encoding = u64::ENCODING; +} + /// Only available on (macos(11.0), macCatalyst(14.0), ios(13.0)) #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -1414,6 +1439,10 @@ pub enum MTLSparseTextureRegionAlignmentMode { Inward = 1, } +unsafe impl Encode for MTLSparseTextureRegionAlignmentMode { + const ENCODING: Encoding = u64::ENCODING; +} + bitflags! { /// Options that determine how Metal prepares the pipeline. pub struct MTLPipelineOption: NSUInteger { @@ -1431,6 +1460,10 @@ bitflags! { } } +unsafe impl Encode for MTLPipelineOption { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[repr(C)] pub struct MTLAccelerationStructureSizes { @@ -1557,54 +1590,24 @@ impl DeviceRef { } pub fn is_low_power(&self) -> bool { - unsafe { - match msg_send![self, isLowPower] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isLowPower] } } pub fn is_headless(&self) -> bool { - unsafe { - match msg_send![self, isHeadless] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isHeadless] } } pub fn is_removable(&self) -> bool { - unsafe { - match msg_send![self, isRemovable] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isRemovable] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_raytracing(&self) -> bool { - unsafe { - match msg_send![self, supportsRaytracing] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsRaytracing] } } pub fn has_unified_memory(&self) -> bool { - unsafe { - match msg_send![self, hasUnifiedMemory] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, hasUnifiedMemory] } } pub fn recommended_max_working_set_size(&self) -> u64 { @@ -1616,95 +1619,41 @@ impl DeviceRef { } pub fn supports_feature_set(&self, feature: MTLFeatureSet) -> bool { - unsafe { - match msg_send![self, supportsFeatureSet: feature] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsFeatureSet: feature] } } pub fn supports_family(&self, family: MTLGPUFamily) -> bool { - unsafe { - match msg_send![self, supportsFamily: family] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsFamily: family] } } pub fn supports_vertex_amplification_count(&self, count: NSUInteger) -> bool { - unsafe { - match msg_send![self, supportsVertexAmplificationCount: count] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsVertexAmplificationCount: count] } } pub fn supports_texture_sample_count(&self, count: NSUInteger) -> bool { - unsafe { - match msg_send![self, supportsTextureSampleCount: count] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsTextureSampleCount: count] } } pub fn supports_shader_barycentric_coordinates(&self) -> bool { - unsafe { - match msg_send![self, supportsShaderBarycentricCoordinates] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsShaderBarycentricCoordinates] } } pub fn supports_function_pointers(&self) -> bool { - unsafe { - match msg_send![self, supportsFunctionPointers] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsFunctionPointers] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_dynamic_libraries(&self) -> bool { - unsafe { - match msg_send![self, supportsDynamicLibraries] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsDynamicLibraries] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_counter_sampling(&self, sampling_point: MTLCounterSamplingPoint) -> bool { - unsafe { - match msg_send![self, supportsCounterSampling: sampling_point] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsCounterSampling: sampling_point] } } pub fn d24_s8_supported(&self) -> bool { - unsafe { - match msg_send![self, isDepth24Stencil8PixelFormatSupported] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isDepth24Stencil8PixelFormatSupported] } } pub fn new_fence(&self) -> Fence { @@ -1990,68 +1939,32 @@ impl DeviceRef { } pub fn raster_order_groups_supported(&self) -> bool { - unsafe { - match msg_send![self, rasterOrderGroupsSupported] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, rasterOrderGroupsSupported] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_32bit_float_filtering(&self) -> bool { - unsafe { - match msg_send![self, supports32BitFloatFiltering] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supports32BitFloatFiltering] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_32bit_MSAA(&self) -> bool { - unsafe { - match msg_send![self, supports32BitMSAA] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supports32BitMSAA] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_query_texture_LOD(&self) -> bool { - unsafe { - match msg_send![self, supportsQueryTextureLOD] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsQueryTextureLOD] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_BC_texture_compression(&self) -> bool { - unsafe { - match msg_send![self, supportsBCTextureCompression] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsBCTextureCompression] } } /// Only available on (macos(11.0), ios(14.0)) pub fn supports_pull_model_interpolation(&self) -> bool { - unsafe { - match msg_send![self, supportsPullModelInterpolation] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportsPullModelInterpolation] } } pub fn new_argument_encoder( diff --git a/src/encoder.rs b/src/encoder.rs index 91bb4ba0..ace17c68 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -9,6 +9,8 @@ use super::*; use std::ops::Range; +use objc2::{Encode, Encoding}; + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLPrimitiveType { @@ -19,6 +21,10 @@ pub enum MTLPrimitiveType { TriangleStrip = 4, } +unsafe impl Encode for MTLPrimitiveType { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -27,6 +33,10 @@ pub enum MTLIndexType { UInt32 = 1, } +unsafe impl Encode for MTLIndexType { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLVisibilityResultMode { @@ -35,6 +45,10 @@ pub enum MTLVisibilityResultMode { Counting = 2, } +unsafe impl Encode for MTLVisibilityResultMode { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLCullMode { @@ -43,6 +57,10 @@ pub enum MTLCullMode { Back = 2, } +unsafe impl Encode for MTLCullMode { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLWinding { @@ -50,6 +68,10 @@ pub enum MTLWinding { CounterClockwise = 1, } +unsafe impl Encode for MTLWinding { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLDepthClipMode { @@ -57,6 +79,10 @@ pub enum MTLDepthClipMode { Clamp = 1, } +unsafe impl Encode for MTLDepthClipMode { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLTriangleFillMode { @@ -64,6 +90,10 @@ pub enum MTLTriangleFillMode { Lines = 1, } +unsafe impl Encode for MTLTriangleFillMode { + const ENCODING: Encoding = u64::ENCODING; +} + bitflags! { /// https://developer.apple.com/documentation/metal/mtlblitoption #[allow(non_upper_case_globals)] @@ -79,6 +109,10 @@ bitflags! { } } +unsafe impl Encode for MTLBlitOption { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + #[repr(C)] #[derive(Copy, Clone, Debug)] pub struct MTLScissorRect { @@ -88,6 +122,18 @@ pub struct MTLScissorRect { pub height: NSUInteger, } +unsafe impl Encode for MTLScissorRect { + const ENCODING: Encoding = Encoding::Struct( + "MTLScissorRect", + &[ + NSUInteger::ENCODING, + NSUInteger::ENCODING, + NSUInteger::ENCODING, + NSUInteger::ENCODING, + ], + ); +} + #[repr(C)] #[derive(Copy, Clone, Debug)] pub struct MTLViewport { @@ -99,6 +145,20 @@ pub struct MTLViewport { pub zfar: f64, } +unsafe impl Encode for MTLViewport { + const ENCODING: Encoding = Encoding::Struct( + "MTLViewport", + &[ + f64::ENCODING, + f64::ENCODING, + f64::ENCODING, + f64::ENCODING, + f64::ENCODING, + f64::ENCODING, + ], + ); +} + #[repr(C)] #[derive(Copy, Clone, Debug)] pub struct MTLDrawPrimitivesIndirectArguments { @@ -125,6 +185,17 @@ pub struct VertexAmplificationViewMapping { pub viewportArrayIndexOffset: u32, } +unsafe impl Encode for VertexAmplificationViewMapping { + const ENCODING: Encoding = Encoding::Struct( + "VertexAmplificationViewMapping", + &[u32::ENCODING, u32::ENCODING], + ); +} + +unsafe impl RefEncode for VertexAmplificationViewMapping { + const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING); +} + pub enum MTLCommandEncoder {} foreign_obj_type! { diff --git a/src/heap.rs b/src/heap.rs index 9d60142a..1276b490 100644 --- a/src/heap.rs +++ b/src/heap.rs @@ -7,6 +7,8 @@ use super::*; +use objc2::{Encode, Encoding}; + /// Only available on macos(10.15), ios(13.0) #[repr(u64)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -17,6 +19,10 @@ pub enum MTLHeapType { Sparse = 2, } +unsafe impl Encode for MTLHeapType { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLHeap {} foreign_obj_type! { diff --git a/src/indirect_encoder.rs b/src/indirect_encoder.rs index 88ac41d3..49ae90d2 100644 --- a/src/indirect_encoder.rs +++ b/src/indirect_encoder.rs @@ -12,6 +12,10 @@ bitflags! { } } +unsafe impl Encode for MTLIndirectCommandType { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + pub enum MTLIndirectCommandBufferDescriptor {} foreign_obj_type! { @@ -30,13 +34,7 @@ impl IndirectCommandBufferDescriptorRef { } pub fn inherit_buffers(&self) -> bool { - unsafe { - match msg_send![self, inheritBuffers] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, inheritBuffers] } } pub fn set_inherit_buffers(&self, inherit: bool) { @@ -44,13 +42,7 @@ impl IndirectCommandBufferDescriptorRef { } pub fn inherit_pipeline_state(&self) -> bool { - unsafe { - match msg_send![self, inheritPipelineState] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, inheritPipelineState] } } pub fn set_inherit_pipeline_state(&self, inherit: bool) { diff --git a/src/lib.rs b/src/lib.rs index bc842792..c8d57159 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ extern crate bitflags; #[macro_use] extern crate log; #[macro_use] -extern crate objc; +extern crate objc2; #[macro_use] extern crate foreign_types; @@ -27,7 +27,8 @@ use std::{ use core_graphics_types::{base::CGFloat, geometry::CGSize}; use foreign_types::ForeignType; -use objc::runtime::{Object, NO, YES}; +use objc2::runtime::Object; +use objc2::{Encode, Encoding, RefEncode}; #[cfg(target_pointer_width = "64")] pub type NSInteger = i64; @@ -45,6 +46,11 @@ pub struct NSRange { pub length: NSUInteger, } +unsafe impl objc2::Encode for NSRange { + const ENCODING: objc2::Encoding = + objc2::Encoding::Struct("_NSRange", &[NSUInteger::ENCODING, NSUInteger::ENCODING]); +} + impl NSRange { #[inline] pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange { @@ -52,7 +58,7 @@ impl NSRange { } } -fn nsstring_as_str(nsstr: &objc::runtime::Object) -> &str { +fn nsstring_as_str(nsstr: &objc2::runtime::Object) -> &str { let bytes = unsafe { let bytes: *const std::os::raw::c_char = msg_send![nsstr, UTF8String]; bytes as *const u8 @@ -64,20 +70,20 @@ fn nsstring_as_str(nsstr: &objc::runtime::Object) -> &str { } } -fn nsstring_from_str(string: &str) -> *mut objc::runtime::Object { +fn nsstring_from_str(string: &str) -> *mut objc2::runtime::Object { const UTF8_ENCODING: usize = 4; let cls = class!(NSString); let bytes = string.as_ptr() as *const c_void; unsafe { - let obj: *mut objc::runtime::Object = msg_send![cls, alloc]; - let obj: *mut objc::runtime::Object = msg_send![ + let obj: *mut objc2::runtime::Object = msg_send![cls, alloc]; + let obj: *mut objc2::runtime::Object = msg_send![ obj, initWithBytes:bytes length:string.len() encoding:UTF8_ENCODING ]; - let _: *mut c_void = msg_send![obj, autorelease]; + let _: *mut objc2::runtime::Object = msg_send![obj, autorelease]; obj } } @@ -115,15 +121,24 @@ macro_rules! foreign_obj_type { pub struct $ref_ident; } - unsafe impl ::objc::Message for $raw_ident { + unsafe impl ::objc2::Encode for $owned_ident { + const ENCODING: ::objc2::Encoding = ::objc2::Encoding::Object; + } + unsafe impl ::objc2::RefEncode for $raw_ident { + const ENCODING_REF: ::objc2::Encoding = ::objc2::Encoding::Object; } - unsafe impl ::objc::Message for $ref_ident { + unsafe impl ::objc2::RefEncode for $ref_ident { + const ENCODING_REF: ::objc2::Encoding = ::objc2::Encoding::Object; + } + unsafe impl ::objc2::Message for $raw_ident { + } + unsafe impl ::objc2::Message for $ref_ident { } impl ::std::fmt::Debug for $ref_ident { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { unsafe { - let string: *mut ::objc::runtime::Object = msg_send![self, debugDescription]; + let string: *mut ::objc2::runtime::Object = msg_send![self, debugDescription]; write!(f, "{}", crate::nsstring_as_str(&*string)) } } @@ -142,7 +157,7 @@ macro_rules! try_objc { $err_name: ident => $body:expr } => { { - let mut $err_name: *mut ::objc::runtime::Object = ::std::ptr::null_mut(); + let mut $err_name: *mut ::objc2::runtime::Object = ::std::ptr::null_mut(); let value = $body; if !$err_name.is_null() { let desc: *mut Object = msg_send![$err_name, localizedDescription]; @@ -163,16 +178,16 @@ pub struct NSArray { pub struct Array(*mut NSArray) where T: ForeignType + 'static, - T::Ref: objc::Message + 'static; + T::Ref: objc2::Message + 'static; pub struct ArrayRef(foreign_types::Opaque, PhantomData) where T: ForeignType + 'static, - T::Ref: objc::Message + 'static; + T::Ref: objc2::Message + 'static; impl Drop for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { fn drop(&mut self) { unsafe { @@ -184,30 +199,63 @@ where impl Clone for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { fn clone(&self) -> Self { unsafe { Array(msg_send![self.0, retain]) } } } -unsafe impl objc::Message for NSArray +unsafe impl RefEncode for NSArray +where + T: ForeignType + 'static, + T::Ref: objc2::Message + 'static, +{ + const ENCODING_REF: Encoding = Encoding::Object; +} + +unsafe impl objc2::Message for NSArray +where + T: ForeignType + 'static, + T::Ref: objc2::Message + 'static, +{ +} + +unsafe impl RefEncode for ArrayRef +where + T: ForeignType + 'static, + T::Ref: objc2::Message + 'static, +{ + const ENCODING_REF: Encoding = Encoding::Object; +} + +unsafe impl objc2::Message for ArrayRef +where + T: ForeignType + 'static, + T::Ref: objc2::Message + 'static, +{ +} +unsafe impl Encode for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { + const ENCODING: ::objc2::Encoding = Encoding::Object; } -unsafe impl objc::Message for ArrayRef + +unsafe impl RefEncode for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { + // TODO: Remove this? + const ENCODING_REF: Encoding = Encoding::Pointer(&>::ENCODING); } impl Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { pub fn from_slice<'a>(s: &[&T::Ref]) -> &'a ArrayRef { unsafe { @@ -215,7 +263,13 @@ where msg_send![class, arrayWithObjects: s.as_ptr() count: s.len()] } } +} +impl Array +where + T: ForeignType + objc2::RefEncode + 'static, + T::Ref: objc2::Message + 'static, +{ pub fn from_owned_slice<'a>(s: &[T]) -> &'a ArrayRef { unsafe { let class = class!(NSArray); @@ -227,7 +281,7 @@ where impl foreign_types::ForeignType for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { type CType = NSArray; type Ref = ArrayRef; @@ -244,7 +298,7 @@ where impl foreign_types::ForeignTypeRef for ArrayRef where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { type CType = NSArray; } @@ -252,7 +306,7 @@ where impl Deref for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { type Target = ArrayRef; @@ -265,7 +319,7 @@ where impl Borrow> for Array where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { fn borrow(&self) -> &ArrayRef { unsafe { mem::transmute(self.as_ptr()) } @@ -275,7 +329,7 @@ where impl ToOwned for ArrayRef where T: ForeignType + 'static, - T::Ref: objc::Message + 'static, + T::Ref: objc2::Message + 'static, { type Owned = Array; @@ -342,13 +396,7 @@ impl MetalLayerRef { } pub fn presents_with_transaction(&self) -> bool { - unsafe { - match msg_send![self, presentsWithTransaction] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, presentsWithTransaction] } } pub fn set_presents_with_transaction(&self, transaction: bool) { @@ -356,13 +404,7 @@ impl MetalLayerRef { } pub fn display_sync_enabled(&self) -> bool { - unsafe { - match msg_send![self, displaySyncEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, displaySyncEnabled] } } pub fn set_display_sync_enabled(&self, enabled: bool) { @@ -403,13 +445,7 @@ impl MetalLayerRef { /// [framebufferOnly Apple Docs](https://developer.apple.com/documentation/metal/mtltexture/1515749-framebufferonly?language=objc) pub fn framebuffer_only(&self) -> bool { - unsafe { - match msg_send![self, framebufferOnly] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, framebufferOnly] } } pub fn set_framebuffer_only(&self, framebuffer_only: bool) { @@ -417,13 +453,7 @@ impl MetalLayerRef { } pub fn is_opaque(&self) -> bool { - unsafe { - match msg_send![self, isOpaque] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isOpaque] } } pub fn set_opaque(&self, opaque: bool) { @@ -431,13 +461,7 @@ impl MetalLayerRef { } pub fn wants_extended_dynamic_range_content(&self) -> bool { - unsafe { - match msg_send![self, wantsExtendedDynamicRangeContent] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, wantsExtendedDynamicRangeContent] } } pub fn set_wants_extended_dynamic_range_content( @@ -509,12 +533,13 @@ pub use mps::*; #[inline] unsafe fn obj_drop(p: *mut T) { - msg_send![(p as *mut Object), release] + msg_send![p as *mut Object, release] } #[inline] unsafe fn obj_clone(p: *mut T) -> *mut T { - msg_send![(p as *mut Object), retain] + let p: *mut Object = msg_send![p as *mut Object, retain]; + p as *mut T } #[allow(non_camel_case_types)] diff --git a/src/library.rs b/src/library.rs index 9a5a36b6..7a38ef56 100644 --- a/src/library.rs +++ b/src/library.rs @@ -8,7 +8,8 @@ use super::*; use foreign_types::ForeignType; -use objc::runtime::{Object, BOOL, NO, YES}; +use objc2::runtime::Object; +use objc2::{Encode, Encoding}; use std::ffi::CStr; use std::os::raw::{c_char, c_void}; @@ -23,6 +24,10 @@ pub enum MTLPatchType { Quad = 2, } +unsafe impl Encode for MTLPatchType { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLVertexAttribute {} foreign_obj_type! { @@ -48,35 +53,17 @@ impl VertexAttributeRef { } pub fn is_active(&self) -> bool { - unsafe { - match msg_send![self, isActive] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isActive] } } /// Only available on (macos(10.12), ios(10.0) pub fn is_patch_data(&self) -> bool { - unsafe { - match msg_send![self, isPatchData] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isPatchData] } } /// Only available on (macos(10.12), ios(10.0) pub fn is_patch_control_point_data(&self) -> bool { - unsafe { - match msg_send![self, isPatchControlPointData] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isPatchControlPointData] } } } @@ -106,35 +93,17 @@ impl AttributeRef { } pub fn is_active(&self) -> bool { - unsafe { - match msg_send![self, isActive] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isActive] } } /// Only available on (macos(10.12), ios(10.0)) pub fn is_patch_data(&self) -> bool { - unsafe { - match msg_send![self, isPatchData] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isPatchData] } } /// Only available on (macos(10.12), ios(10.0)) pub fn is_patch_control_point_data(&self) -> bool { - unsafe { - match msg_send![self, isPatchControlPointData] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isPatchControlPointData] } } } @@ -150,6 +119,10 @@ pub enum MTLFunctionType { Intersection = 6, } +unsafe impl Encode for MTLFunctionType { + const ENCODING: Encoding = u64::ENCODING; +} + /// Only available on (macos(10.12), ios(10.0)) pub enum MTLFunctionConstant {} @@ -176,13 +149,7 @@ impl FunctionConstantRef { } pub fn required(&self) -> bool { - unsafe { - match msg_send![self, required] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, required] } } } @@ -194,6 +161,10 @@ bitflags! { } } +unsafe impl Encode for MTLFunctionOptions { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + /// Only available on (macos(11.0), ios(14.0)) pub enum MTLFunctionDescriptor {} @@ -393,6 +364,10 @@ pub enum MTLLanguageVersion { V2_4 = 0x20004, } +unsafe impl Encode for MTLLanguageVersion { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLFunctionConstantValues {} foreign_obj_type! { @@ -445,6 +420,10 @@ pub enum MTLLibraryType { Dynamic = 1, } +unsafe impl Encode for MTLLibraryType { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLCompileOptions {} foreign_obj_type! { @@ -472,13 +451,7 @@ impl CompileOptionsRef { } pub fn is_fast_math_enabled(&self) -> bool { - unsafe { - match msg_send![self, fastMathEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, fastMathEnabled] } } pub fn set_fast_math_enabled(&self, enabled: bool) { @@ -562,13 +535,7 @@ impl CompileOptionsRef { /// Only available on (macos(11.0), macCatalyst(14.0), ios(13.0)) pub fn preserve_invariance(&self) -> bool { - unsafe { - match msg_send![self, preserveInvariance] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, preserveInvariance] } } /// Only available on (macos(11.0), macCatalyst(14.0), ios(13.0)) @@ -590,6 +557,10 @@ pub enum MTLLibraryError { FileNotFound = 6, } +unsafe impl Encode for MTLLibraryError { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLLibrary {} foreign_obj_type! { @@ -730,6 +701,10 @@ pub enum MTLDynamicLibraryError { Unsupported = 5, } +unsafe impl Encode for MTLDynamicLibraryError { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLDynamicLibrary {} foreign_obj_type! { @@ -767,8 +742,7 @@ impl DynamicLibraryRef { pub fn serialize_to_url(&self, url: &URLRef) -> Result { unsafe { let mut err: *mut Object = ptr::null_mut(); - let result: BOOL = msg_send![self, serializeToURL:url - error:&mut err]; + let result = msg_send![self, serializeToURL: url, error: &mut err]; if !err.is_null() { // FIXME: copy pasta let desc: *mut Object = msg_send![err, localizedDescription]; @@ -776,11 +750,7 @@ impl DynamicLibraryRef { let message = CStr::from_ptr(c_msg).to_string_lossy().into_owned(); Err(message) } else { - match result { - YES => Ok(true), - NO => Ok(false), - _ => unreachable!(), - } + Ok(result) } } } @@ -847,8 +817,11 @@ impl BinaryArchiveRef { ) -> Result { unsafe { let mut err: *mut Object = ptr::null_mut(); - let result: BOOL = msg_send![self, addComputePipelineFunctionsWithDescriptor:descriptor - error:&mut err]; + let result = msg_send![ + self, + addComputePipelineFunctionsWithDescriptor: descriptor, + error: &mut err + ]; if !err.is_null() { // FIXME: copy pasta let desc: *mut Object = msg_send![err, localizedDescription]; @@ -856,11 +829,7 @@ impl BinaryArchiveRef { let message = CStr::from_ptr(c_msg).to_string_lossy().into_owned(); Err(message) } else { - match result { - YES => Ok(true), - NO => Ok(false), - _ => unreachable!(), - } + Ok(result) } } } @@ -871,8 +840,11 @@ impl BinaryArchiveRef { ) -> Result { unsafe { let mut err: *mut Object = ptr::null_mut(); - let result: BOOL = msg_send![self, addRenderPipelineFunctionsWithDescriptor:descriptor - error:&mut err]; + let result = msg_send![ + self, + addRenderPipelineFunctionsWithDescriptor: descriptor, + error: &mut err + ]; if !err.is_null() { // FIXME: copy pasta let desc: *mut Object = msg_send![err, localizedDescription]; @@ -880,11 +852,7 @@ impl BinaryArchiveRef { let message = CStr::from_ptr(c_msg).to_string_lossy().into_owned(); Err(message) } else { - match result { - YES => Ok(true), - NO => Ok(false), - _ => unreachable!(), - } + Ok(result) } } } @@ -896,8 +864,7 @@ impl BinaryArchiveRef { pub fn serialize_to_url(&self, url: &URLRef) -> Result { unsafe { let mut err: *mut Object = ptr::null_mut(); - let result: BOOL = msg_send![self, serializeToURL:url - error:&mut err]; + let result = msg_send![self, serializeToURL: url, error: &mut err]; if !err.is_null() { // FIXME: copy pasta let desc: *mut Object = msg_send![err, localizedDescription]; @@ -905,11 +872,7 @@ impl BinaryArchiveRef { let message = CStr::from_ptr(c_msg).to_string_lossy().into_owned(); Err(message) } else { - match result { - YES => Ok(true), - NO => Ok(false), - _ => unreachable!(), - } + Ok(result) } } } diff --git a/src/mps.rs b/src/mps.rs index 0d4da2ee..97de1246 100644 --- a/src/mps.rs +++ b/src/mps.rs @@ -7,19 +7,19 @@ use super::*; -use objc::runtime::{BOOL, YES}; +use objc2::runtime::Bool; +use objc2::{Encode, Encoding}; #[link(name = "MetalPerformanceShaders", kind = "framework")] extern "C" { - fn MPSSupportsMTLDevice(device: *const std::ffi::c_void) -> BOOL; + fn MPSSupportsMTLDevice(device: *const std::ffi::c_void) -> Bool; } pub fn mps_supports_device(device: &DeviceRef) -> bool { - let b: BOOL = unsafe { + unsafe { let ptr: *const DeviceRef = device; - MPSSupportsMTLDevice(ptr as _) - }; - b == YES + MPSSupportsMTLDevice(ptr as _).as_bool() + } } pub enum MPSKernel {} @@ -30,12 +30,17 @@ foreign_obj_type! { pub struct KernelRef; } +#[repr(usize)] // NSUInteger pub enum MPSRayDataType { OriginDirection = 0, OriginMinDistanceDirectionMaxDistance = 1, OriginMaskDirectionMaxDistance = 2, } +unsafe impl Encode for MPSRayDataType { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + bitflags! { #[allow(non_upper_case_globals)] pub struct MPSRayMaskOptions: NSUInteger { @@ -46,7 +51,12 @@ bitflags! { } } +unsafe impl Encode for MPSRayMaskOptions { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + /// Options that determine the data contained in an intersection result. +#[repr(usize)] // NSUInteger pub enum MPSIntersectionDataType { Distance = 0, DistancePrimitiveIndex = 1, @@ -55,6 +65,11 @@ pub enum MPSIntersectionDataType { DistancePrimitiveIndexInstanceIndexCoordinates = 4, } +unsafe impl Encode for MPSIntersectionDataType { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + +#[repr(usize)] // NSUInteger pub enum MPSIntersectionType { /// Find the closest intersection to the ray's origin along the ray direction. /// This is potentially slower than `Any` but is well suited to primary visibility rays. @@ -64,6 +79,11 @@ pub enum MPSIntersectionType { Any = 1, } +unsafe impl Encode for MPSIntersectionType { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + +#[repr(usize)] // NSUInteger pub enum MPSRayMaskOperator { /// Accept the intersection if `(primitive mask & ray mask) != 0`. And = 0, @@ -89,6 +109,11 @@ pub enum MPSRayMaskOperator { GreaterThanOrEqualTo = 9, } +unsafe impl Encode for MPSRayMaskOperator { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + +#[repr(usize)] // NSUInteger pub enum MPSTriangleIntersectionTestType { /// Use the default ray/triangle intersection test Default = 0, @@ -98,12 +123,21 @@ pub enum MPSTriangleIntersectionTestType { Watertight = 1, } +unsafe impl Encode for MPSTriangleIntersectionTestType { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +#[repr(usize)] // NSUInteger pub enum MPSAccelerationStructureStatus { Unbuilt = 0, Built = 1, } +unsafe impl Encode for MPSAccelerationStructureStatus { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + bitflags! { #[allow(non_upper_case_globals)] pub struct MPSAccelerationStructureUsage: NSUInteger { @@ -118,11 +152,16 @@ bitflags! { } } +unsafe impl Encode for MPSAccelerationStructureUsage { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + /// A common bit for all floating point data types. -const MPSDataTypeFloatBit: isize = 0x10000000; -const MPSDataTypeSignedBit: isize = 0x20000000; -const MPSDataTypeNormalizedBit: isize = 0x40000000; +const MPSDataTypeFloatBit: u32 = 0x10000000; +const MPSDataTypeSignedBit: u32 = 0x20000000; +const MPSDataTypeNormalizedBit: u32 = 0x40000000; +#[repr(u32)] // uint32_t pub enum MPSDataType { Invalid = 0, @@ -144,6 +183,10 @@ pub enum MPSDataType { Unorm8 = MPSDataTypeNormalizedBit | 8, } +unsafe impl Encode for MPSDataType { + const ENCODING: Encoding = u32::ENCODING; +} + /// A kernel that performs intersection tests between rays and geometry. pub enum MPSRayIntersector {} @@ -397,6 +440,10 @@ pub enum MPSTransformType { Identity = 1, } +unsafe impl Encode for MPSTransformType { + const ENCODING: Encoding = u64::ENCODING; +} + /// An acceleration structure built over instances of other acceleration structures pub enum MPSInstanceAccelerationStructure {} diff --git a/src/pipeline/compute.rs b/src/pipeline/compute.rs index 138b561c..0fb486ed 100644 --- a/src/pipeline/compute.rs +++ b/src/pipeline/compute.rs @@ -7,7 +7,7 @@ use super::*; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[allow(non_camel_case_types)] @@ -67,6 +67,10 @@ pub enum MTLAttributeFormat { Half = 53, } +unsafe impl Encode for MTLAttributeFormat { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -82,6 +86,10 @@ pub enum MTLStepFunction { ThreadPositionInGridYIndexed = 8, } +unsafe impl Encode for MTLStepFunction { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLComputePipelineDescriptor {} foreign_obj_type! { @@ -123,13 +131,7 @@ impl ComputePipelineDescriptorRef { } pub fn thread_group_size_is_multiple_of_thread_execution_width(&self) -> bool { - unsafe { - match msg_send![self, threadGroupSizeIsMultipleOfThreadExecutionWidth] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, threadGroupSizeIsMultipleOfThreadExecutionWidth] } } pub fn set_thread_group_size_is_multiple_of_thread_execution_width( @@ -156,13 +158,7 @@ impl ComputePipelineDescriptorRef { /// API_AVAILABLE(ios(13.0),macos(11.0)); pub fn support_indirect_command_buffers(&self) -> bool { - unsafe { - match msg_send![self, supportIndirectCommandBuffers] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportIndirectCommandBuffers] } } /// API_AVAILABLE(ios(13.0),macos(11.0)); @@ -172,13 +168,7 @@ impl ComputePipelineDescriptorRef { /// API_AVAILABLE(macos(11.0), ios(14.0)); pub fn support_adding_binary_functions(&self) -> bool { - unsafe { - match msg_send![self, supportAddingBinaryFunctions] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportAddingBinaryFunctions] } } /// API_AVAILABLE(macos(11.0), ios(14.0)); @@ -303,13 +293,7 @@ impl ComputePipelineStateRef { /// Only available on (ios(13.0), macos(11.0)) pub fn support_indirect_command_buffers(&self) -> bool { - unsafe { - match msg_send![self, supportIndirectCommandBuffers] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportIndirectCommandBuffers] } } /// Only available on (macos(11.0), ios(14.0)) diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index e65d28d6..9fdffe8f 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -13,6 +13,8 @@ mod render; pub use self::compute::*; pub use self::render::*; +use objc2::{Encode, Encoding}; + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -22,6 +24,10 @@ pub enum MTLMutability { Immutable = 2, } +unsafe impl Encode for MTLMutability { + const ENCODING: Encoding = u64::ENCODING; +} + impl Default for MTLMutability { #[inline] fn default() -> Self { diff --git a/src/pipeline/render.rs b/src/pipeline/render.rs index a4b3a629..8233bc81 100644 --- a/src/pipeline/render.rs +++ b/src/pipeline/render.rs @@ -7,7 +7,7 @@ use super::*; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[allow(non_camel_case_types)] @@ -34,6 +34,10 @@ pub enum MTLBlendFactor { OneMinusSource1Alpha = 18, } +unsafe impl Encode for MTLBlendFactor { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -45,6 +49,10 @@ pub enum MTLBlendOperation { Max = 4, } +unsafe impl Encode for MTLBlendOperation { + const ENCODING: Encoding = u64::ENCODING; +} + bitflags! { pub struct MTLColorWriteMask: NSUInteger { const None = 0; @@ -56,6 +64,10 @@ bitflags! { } } +unsafe impl Encode for MTLColorWriteMask { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -66,6 +78,10 @@ pub enum MTLPrimitiveTopologyClass { Triangle = 3, } +unsafe impl Encode for MTLPrimitiveTopologyClass { + const ENCODING: Encoding = u64::ENCODING; +} + // TODO: MTLTessellationPartitionMode // TODO: MTLTessellationFactorStepFunction // TODO: MTLTessellationFactorFormat @@ -89,13 +105,7 @@ impl RenderPipelineColorAttachmentDescriptorRef { } pub fn is_blending_enabled(&self) -> bool { - unsafe { - match msg_send![self, isBlendingEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isBlendingEnabled] } } pub fn set_blending_enabled(&self, enabled: bool) { @@ -325,13 +335,7 @@ impl RenderPipelineDescriptorRef { } pub fn is_alpha_to_coverage_enabled(&self) -> bool { - unsafe { - match msg_send![self, isAlphaToCoverageEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isAlphaToCoverageEnabled] } } pub fn set_alpha_to_coverage_enabled(&self, enabled: bool) { @@ -339,13 +343,7 @@ impl RenderPipelineDescriptorRef { } pub fn is_alpha_to_one_enabled(&self) -> bool { - unsafe { - match msg_send![self, isAlphaToOneEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isAlphaToOneEnabled] } } pub fn set_alpha_to_one_enabled(&self, enabled: bool) { @@ -353,13 +351,7 @@ impl RenderPipelineDescriptorRef { } pub fn is_rasterization_enabled(&self) -> bool { - unsafe { - match msg_send![self, isRasterizationEnabled] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isRasterizationEnabled] } } pub fn set_rasterization_enabled(&self, enabled: bool) { @@ -409,13 +401,7 @@ impl RenderPipelineDescriptorRef { } pub fn support_indirect_command_buffers(&self) -> bool { - unsafe { - match msg_send![self, supportIndirectCommandBuffers] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, supportIndirectCommandBuffers] } } pub fn set_support_indirect_command_buffers(&self, support: bool) { diff --git a/src/renderpass.rs b/src/renderpass.rs index ed4f60a4..96233169 100644 --- a/src/renderpass.rs +++ b/src/renderpass.rs @@ -7,6 +7,8 @@ use super::*; +use objc2::{Encode, Encoding}; + #[repr(u64)] #[derive(Copy, Clone, Debug)] pub enum MTLLoadAction { @@ -15,6 +17,10 @@ pub enum MTLLoadAction { Clear = 2, } +unsafe impl Encode for MTLLoadAction { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug)] pub enum MTLStoreAction { @@ -26,6 +32,10 @@ pub enum MTLStoreAction { CustomSampleDepthStore = 5, } +unsafe impl Encode for MTLStoreAction { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(C)] #[derive(Copy, Clone, Debug)] pub struct MTLClearColor { @@ -35,6 +45,13 @@ pub struct MTLClearColor { pub alpha: f64, } +unsafe impl Encode for MTLClearColor { + const ENCODING: Encoding = Encoding::Struct( + "MTLClearColor", + &[f64::ENCODING, f64::ENCODING, f64::ENCODING, f64::ENCODING], + ); +} + impl MTLClearColor { #[inline] pub fn new(red: f64, green: f64, blue: f64, alpha: f64) -> Self { @@ -54,6 +71,10 @@ pub enum MTLMultisampleStencilResolveFilter { DepthResolvedSample = 1, } +unsafe impl Encode for MTLMultisampleStencilResolveFilter { + const ENCODING: Encoding = u32::ENCODING; +} + pub enum MTLRenderPassAttachmentDescriptor {} foreign_obj_type! { diff --git a/src/resource.rs b/src/resource.rs index 8986a9c8..b7fa192f 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -6,7 +6,7 @@ // copied, modified, or distributed except according to those terms. use super::{DeviceRef, HeapRef, NSUInteger}; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -17,6 +17,10 @@ pub enum MTLPurgeableState { Empty = 4, } +unsafe impl Encode for MTLPurgeableState { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum MTLCPUCacheMode { @@ -24,6 +28,10 @@ pub enum MTLCPUCacheMode { WriteCombined = 1, } +unsafe impl Encode for MTLCPUCacheMode { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum MTLStorageMode { @@ -34,6 +42,10 @@ pub enum MTLStorageMode { Memoryless = 3, } +unsafe impl Encode for MTLStorageMode { + const ENCODING: Encoding = u64::ENCODING; +} + /// Only available on macos(10.15), ios(13.0) #[repr(u64)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] @@ -43,6 +55,10 @@ pub enum MTLHazardTrackingMode { Tracked = 2, } +unsafe impl Encode for MTLHazardTrackingMode { + const ENCODING: Encoding = u64::ENCODING; +} + pub const MTLResourceCPUCacheModeShift: NSUInteger = 0; pub const MTLResourceCPUCacheModeMask: NSUInteger = 0xf << MTLResourceCPUCacheModeShift; pub const MTLResourceStorageModeShift: NSUInteger = 4; @@ -70,6 +86,10 @@ bitflags! { } } +unsafe impl Encode for MTLResourceOptions { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + bitflags! { /// Options that describe how a graphics or compute function uses an argument buffer’s resource. /// @@ -87,6 +107,10 @@ bitflags! { } } +unsafe impl Encode for MTLResourceUsage { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[repr(C)] pub struct MTLSizeAndAlign { @@ -94,6 +118,13 @@ pub struct MTLSizeAndAlign { pub align: NSUInteger, } +unsafe impl Encode for MTLSizeAndAlign { + const ENCODING: Encoding = Encoding::Struct( + "MTLSizeAndAlign", + &[NSUInteger::ENCODING, NSUInteger::ENCODING], + ); +} + pub enum MTLResource {} foreign_obj_type! { @@ -165,12 +196,6 @@ impl ResourceRef { /// Only available on macos(10.13), ios(10.0) pub fn is_aliasable(&self) -> bool { - unsafe { - match msg_send![self, isAliasable] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isAliasable] } } } diff --git a/src/sampler.rs b/src/sampler.rs index 3dd871a3..c49ded44 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -7,6 +7,8 @@ use super::{depthstencil::MTLCompareFunction, DeviceRef, NSUInteger}; +use objc2::{Encode, Encoding}; + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLSamplerMinMagFilter { @@ -14,6 +16,10 @@ pub enum MTLSamplerMinMagFilter { Linear = 1, } +unsafe impl Encode for MTLSamplerMinMagFilter { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLSamplerMipFilter { @@ -22,6 +28,10 @@ pub enum MTLSamplerMipFilter { Linear = 2, } +unsafe impl Encode for MTLSamplerMipFilter { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLSamplerAddressMode { @@ -33,6 +43,10 @@ pub enum MTLSamplerAddressMode { ClampToBorderColor = 5, } +unsafe impl Encode for MTLSamplerAddressMode { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLSamplerBorderColor { @@ -41,6 +55,10 @@ pub enum MTLSamplerBorderColor { OpaqueWhite = 2, } +unsafe impl Encode for MTLSamplerBorderColor { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLSamplerDescriptor {} foreign_obj_type! { diff --git a/src/sync.rs b/src/sync.rs index e4b3d8aa..01ee9a0a 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -6,7 +6,8 @@ // copied, modified, or distributed except according to those terms. use super::*; -use block::{Block, RcBlock}; +use block2::{Block, RcBlock}; +use objc2::{Encode, EncodeArguments, Encoding}; use std::mem; #[cfg(feature = "dispatch_queue")] @@ -153,6 +154,10 @@ bitflags! { } } +unsafe impl Encode for MTLRenderStages { + const ENCODING: Encoding = u64::ENCODING; +} + const BLOCK_HAS_COPY_DISPOSE: i32 = 0x02000000; const BLOCK_HAS_SIGNATURE: i32 = 0x40000000; @@ -167,6 +172,10 @@ struct BlockBase { type BlockExtraDtor = extern "C" fn(*mut BlockBase); +unsafe impl RefEncode for BlockBase { + const ENCODING_REF: Encoding = Encoding::Block; +} + #[repr(C)] struct BlockExtra { unknown0: *mut i32, // 0x00 diff --git a/src/texture.rs b/src/texture.rs index 5e9f1b3d..776fd3fb 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -7,7 +7,7 @@ use super::*; -use objc::runtime::{NO, YES}; +use objc2::{Encode, Encoding}; #[repr(u64)] #[allow(non_camel_case_types)] @@ -23,6 +23,10 @@ pub enum MTLTextureType { D3 = 7, } +unsafe impl Encode for MTLTextureType { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] pub enum MTLTextureCompressionType { @@ -30,6 +34,10 @@ pub enum MTLTextureCompressionType { Lossy = 1, } +unsafe impl Encode for MTLTextureCompressionType { + const ENCODING: Encoding = u64::ENCODING; +} + bitflags! { pub struct MTLTextureUsage: NSUInteger { const Unknown = 0x0000; @@ -40,6 +48,10 @@ bitflags! { } } +unsafe impl Encode for MTLTextureUsage { + const ENCODING: Encoding = NSUInteger::ENCODING; +} + pub enum MTLTextureDescriptor {} foreign_obj_type! { @@ -250,13 +262,7 @@ impl TextureRef { /// [framebufferOnly Apple Docs](https://developer.apple.com/documentation/metal/mtltexture/1515749-framebufferonly?language=objc) pub fn framebuffer_only(&self) -> bool { - unsafe { - match msg_send![self, isFramebufferOnly] { - YES => true, - NO => false, - _ => unreachable!(), - } - } + unsafe { msg_send![self, isFramebufferOnly] } } pub fn get_bytes( diff --git a/src/types.rs b/src/types.rs index 3ea937f0..90b3e76b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,6 +8,8 @@ use super::NSUInteger; use std::default::Default; +use objc2::{Encode, Encoding}; + #[repr(C)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Default)] pub struct MTLOrigin { @@ -16,6 +18,17 @@ pub struct MTLOrigin { pub z: NSUInteger, } +unsafe impl Encode for MTLOrigin { + const ENCODING: Encoding = Encoding::Struct( + "?", + &[ + NSUInteger::ENCODING, + NSUInteger::ENCODING, + NSUInteger::ENCODING, + ], + ); +} + #[repr(C)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Default)] pub struct MTLSize { @@ -24,6 +37,17 @@ pub struct MTLSize { pub depth: NSUInteger, } +unsafe impl Encode for MTLSize { + const ENCODING: Encoding = Encoding::Struct( + "?", + &[ + NSUInteger::ENCODING, + NSUInteger::ENCODING, + NSUInteger::ENCODING, + ], + ); +} + impl MTLSize { pub fn new(width: NSUInteger, height: NSUInteger, depth: NSUInteger) -> Self { Self { @@ -41,6 +65,11 @@ pub struct MTLRegion { pub size: MTLSize, } +unsafe impl Encode for MTLRegion { + const ENCODING: Encoding = + Encoding::Struct("MTLRegion", &[MTLOrigin::ENCODING, MTLSize::ENCODING]); +} + impl MTLRegion { #[inline] pub fn new_1d(x: NSUInteger, width: NSUInteger) -> Self { @@ -78,3 +107,8 @@ pub struct MTLSamplePosition { pub x: f32, pub y: f32, } + +unsafe impl Encode for MTLSamplePosition { + const ENCODING: Encoding = + Encoding::Struct("MTLSamplePosition", &[f32::ENCODING, f32::ENCODING]); +} diff --git a/src/vertexdescriptor.rs b/src/vertexdescriptor.rs index 201e1e30..b66dae75 100644 --- a/src/vertexdescriptor.rs +++ b/src/vertexdescriptor.rs @@ -7,6 +7,8 @@ use super::NSUInteger; +use objc2::{Encode, Encoding}; + #[repr(u64)] #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -65,6 +67,10 @@ pub enum MTLVertexFormat { Half = 53, } +unsafe impl Encode for MTLVertexFormat { + const ENCODING: Encoding = u64::ENCODING; +} + #[repr(u64)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MTLVertexStepFunction { @@ -75,6 +81,10 @@ pub enum MTLVertexStepFunction { PerPatchControlPoint = 4, } +unsafe impl Encode for MTLVertexStepFunction { + const ENCODING: Encoding = u64::ENCODING; +} + pub enum MTLVertexBufferLayoutDescriptor {} foreign_obj_type! {