Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use objc2 instead of objc #241

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ mps = []
link = []

[dependencies]
core-graphics-types = "0.1"
bitflags = "2"
log = "0.4"
block = "0.1.6"
block2 = "0.3.0"
foreign-types = "0.5"
dispatch = { version = "0.2", optional = true }
paste = "1"
objc2 = "0.4.1"

[dependencies.objc]
version = "0.2.4"
features = ["objc_exception"]
[dependencies.icrate]
version = "0.0.4"
features = ["Foundation"]

[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
8 changes: 4 additions & 4 deletions examples/bindless/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
// 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
const BINDLESS_TEXTURE_COUNT: usize = 100_000; // ~25Mb

/// This example demonstrates:
/// - How to create a heap
/// - How to allocate textures from heap.
/// - 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 Expand Up @@ -93,7 +93,7 @@ fn main() {
// Encode textures to the argument buffer.
textures.iter().enumerate().for_each(|(index, texture)| {
// Offset encoder to a proper texture slot
let offset = index as NSUInteger * encoder.encoded_length();
let offset = index * encoder.encoded_length();
encoder.set_argument_buffer(&argument_buffer, offset);
encoder.set_texture(0, texture);
});
Expand Down
29 changes: 12 additions & 17 deletions examples/circle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ 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 @@ -52,7 +52,7 @@ fn main() {
device.sample_timestamps(&mut cpu_start, &mut gpu_start);
let counter_sample_buffer = create_counter_sample_buffer(&device);
let destination_buffer = device.new_buffer(
(std::mem::size_of::<u64>() * 4 as usize) as u64,
std::mem::size_of::<u64>() * 4,
MTLResourceOptions::StorageModeShared,
);
let counter_sampling_point = MTLCounterSamplingPoint::AtStageBoundary;
Expand Down Expand Up @@ -101,26 +101,26 @@ 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()));
}

let draw_size = window.inner_size();
layer.set_drawable_size(CGSize::new(draw_size.width as f64, draw_size.height as f64));
layer.set_drawable_size(draw_size.width as f64, draw_size.height as f64);

let vbuf = {
let vertex_data = create_vertex_points_for_circle();
let vertex_data = vertex_data.as_slice();

device.new_buffer_with_data(
vertex_data.as_ptr() as *const _,
(vertex_data.len() * mem::size_of::<AAPLVertex>()) as u64,
vertex_data.len() * mem::size_of::<AAPLVertex>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
};

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 Expand Up @@ -248,10 +248,10 @@ fn handle_render_pass_sample_buffer_attachment(
let sample_buffer_attachment_descriptor =
descriptor.sample_buffer_attachments().object_at(0).unwrap();
sample_buffer_attachment_descriptor.set_sample_buffer(&counter_sample_buffer);
sample_buffer_attachment_descriptor.set_start_of_vertex_sample_index(0 as NSUInteger);
sample_buffer_attachment_descriptor.set_end_of_vertex_sample_index(1 as NSUInteger);
sample_buffer_attachment_descriptor.set_start_of_fragment_sample_index(2 as NSUInteger);
sample_buffer_attachment_descriptor.set_end_of_fragment_sample_index(3 as NSUInteger);
sample_buffer_attachment_descriptor.set_start_of_vertex_sample_index(0);
sample_buffer_attachment_descriptor.set_end_of_vertex_sample_index(1);
sample_buffer_attachment_descriptor.set_start_of_fragment_sample_index(2);
sample_buffer_attachment_descriptor.set_end_of_fragment_sample_index(3);
}

fn handle_render_pass_color_attachment(descriptor: &RenderPassDescriptorRef, texture: &TextureRef) {
Expand Down Expand Up @@ -299,12 +299,7 @@ fn resolve_samples_into_buffer(
destination_buffer: &BufferRef,
) {
let blit_encoder = command_buffer.new_blit_command_encoder();
blit_encoder.resolve_counters(
&counter_sample_buffer,
crate::NSRange::new(0_u64, 4),
&destination_buffer,
0_u64,
);
blit_encoder.resolve_counters(&counter_sample_buffer, 0..4, &destination_buffer, 0);
blit_encoder.end_encoding();
}

Expand Down
10 changes: 5 additions & 5 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 All @@ -23,15 +23,15 @@ fn main() {

let buffer = device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
);

let sum = {
let data = [0u32];
device.new_buffer_with_data(
unsafe { mem::transmute(data.as_ptr()) },
(data.len() * mem::size_of::<u32>()) as u64,
data.len() * mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down Expand Up @@ -77,7 +77,7 @@ fn main() {
};

let thread_group_size = MTLSize {
width: (data.len() as u64 + width) / width,
width: (data.len() + width) / width,
height: 1,
depth: 1,
};
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
38 changes: 14 additions & 24 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use metal::*;
use objc::rc::autoreleasepool;
use objc2::rc::autoreleasepool;
use std::path::PathBuf;

const NUM_SAMPLES: u64 = 2;
const NUM_SAMPLES: usize = 2;

fn main() {
let num_elements = std::env::args()
.nth(1)
.map(|s| s.parse::<u32>().unwrap())
.map(|s| s.parse::<usize>().unwrap())
.unwrap_or(64 * 64);

autoreleasepool(|| {
autoreleasepool(|_| {
let device = Device::system_default().expect("No device found");
let mut cpu_start = 0;
let mut gpu_start = 0;
device.sample_timestamps(&mut cpu_start, &mut gpu_start);

let counter_sample_buffer = create_counter_sample_buffer(&device);
let destination_buffer = device.new_buffer(
(std::mem::size_of::<u64>() * NUM_SAMPLES as usize) as u64,
std::mem::size_of::<u64>() * NUM_SAMPLES,
MTLResourceOptions::StorageModeShared,
);

Expand Down Expand Up @@ -46,7 +46,7 @@ fn main() {
let num_threads = pipeline_state.thread_execution_width();

let thread_group_count = MTLSize {
width: ((num_elements as NSUInteger + num_threads) / num_threads),
width: (num_elements + num_threads) / num_threads,
height: 1,
depth: 1,
};
Expand All @@ -71,9 +71,7 @@ fn main() {
let ptr = sum.contents() as *mut u32;
println!("Compute shader sum: {}", unsafe { *ptr });

unsafe {
assert_eq!(num_elements, *ptr);
}
assert_eq!(num_elements, unsafe { *ptr } as usize);

handle_timestamps(&destination_buffer, cpu_start, cpu_end, gpu_start, gpu_end);
});
Expand Down Expand Up @@ -115,12 +113,7 @@ fn resolve_samples_into_buffer(
destination_buffer: &BufferRef,
) {
let blit_encoder = command_buffer.new_blit_command_encoder();
blit_encoder.resolve_counters(
counter_sample_buffer,
crate::NSRange::new(0_u64, NUM_SAMPLES),
destination_buffer,
0_u64,
);
blit_encoder.resolve_counters(counter_sample_buffer, 0..NUM_SAMPLES, destination_buffer, 0);
blit_encoder.end_encoding();
}

Expand All @@ -132,10 +125,7 @@ fn handle_timestamps(
gpu_end: u64,
) {
let samples = unsafe {
std::slice::from_raw_parts(
resolved_sample_buffer.contents() as *const u64,
NUM_SAMPLES as usize,
)
std::slice::from_raw_parts(resolved_sample_buffer.contents() as *const u64, NUM_SAMPLES)
};
let pass_start = samples[0];
let pass_end = samples[1];
Expand All @@ -150,7 +140,7 @@ fn handle_timestamps(
fn create_counter_sample_buffer(device: &Device) -> CounterSampleBuffer {
let counter_sample_buffer_desc = metal::CounterSampleBufferDescriptor::new();
counter_sample_buffer_desc.set_storage_mode(metal::MTLStorageMode::Shared);
counter_sample_buffer_desc.set_sample_count(NUM_SAMPLES);
counter_sample_buffer_desc.set_sample_count(NUM_SAMPLES as u64);
let counter_sets = device.counter_sets();

let timestamp_counter = counter_sets.iter().find(|cs| cs.name() == "timestamp");
Expand All @@ -165,21 +155,21 @@ fn create_counter_sample_buffer(device: &Device) -> CounterSampleBuffer {

fn create_input_and_output_buffers(
device: &Device,
num_elements: u32,
num_elements: usize,
) -> (metal::Buffer, metal::Buffer) {
let data = vec![1u32; num_elements as usize];
let data = vec![1u32; num_elements];

let buffer = device.new_buffer_with_data(
unsafe { std::mem::transmute(data.as_ptr()) },
(data.len() * std::mem::size_of::<u32>()) as u64,
data.len() * std::mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
);

let sum = {
let data = [0u32];
device.new_buffer_with_data(
unsafe { std::mem::transmute(data.as_ptr()) },
(data.len() * std::mem::size_of::<u32>()) as u64,
data.len() * std::mem::size_of::<u32>(),
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
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
8 changes: 4 additions & 4 deletions examples/headless-render/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use metal::{
};
use png::ColorType;

const VIEW_WIDTH: u64 = 512;
const VIEW_HEIGHT: u64 = 512;
const TOTAL_BYTES: usize = (VIEW_WIDTH * VIEW_HEIGHT * 4) as usize;
const VIEW_WIDTH: usize = 512;
const VIEW_HEIGHT: usize = 512;
const TOTAL_BYTES: usize = VIEW_WIDTH * VIEW_HEIGHT * 4;

const VERTEX_SHADER: &'static str = "triangle_vertex";
const FRAGMENT_SHADER: &'static str = "triangle_fragment";
Expand Down Expand Up @@ -144,7 +144,7 @@ fn prepare_pipeline_state(device: &DeviceRef, library: &LibraryRef) -> RenderPip
fn create_vertex_buffer(device: &DeviceRef) -> Buffer {
device.new_buffer_with_data(
VERTEX_ATTRIBS.as_ptr() as *const _,
(VERTEX_ATTRIBS.len() * mem::size_of::<f32>()) as u64,
VERTEX_ATTRIBS.len() * mem::size_of::<f32>(),
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged,
)
}
Expand Down
Loading