Skip to content

Commit

Permalink
Use objc2
Browse files Browse the repository at this point in the history
- Implement Encode and RefEncode for most types
- Simply return `bool` from `msg_send!` (now sound)
  • Loading branch information
madsmtm committed Sep 1, 2022
1 parent fb46081 commit 2954cbe
Show file tree
Hide file tree
Showing 37 changed files with 611 additions and 461 deletions.
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions examples/argument-buffer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

/*
Expand Down
4 changes: 2 additions & 2 deletions examples/bind/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions examples/bindless/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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");

/*
Expand Down
7 changes: 4 additions & 3 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
}

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions examples/compute/compute-argument-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions examples/compute/embedded-lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/events/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions examples/reflection/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <metal_stdlib>
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions examples/shader-dylib/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions examples/texture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions examples/texture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
}

Expand Down Expand Up @@ -162,7 +161,7 @@ fn main() {
);

events_loop.run(move |event, _, control_flow| {
autoreleasepool(|| {
autoreleasepool(|_| {
*control_flow = ControlFlow::Poll;

match event {
Expand Down
22 changes: 14 additions & 8 deletions src/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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! {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/capturedescriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {}

Expand Down
5 changes: 3 additions & 2 deletions src/capturemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

Expand Down
Loading

0 comments on commit 2954cbe

Please sign in to comment.