1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;
use metal::*;
use objc::{rc::autoreleasepool, runtime::YES};
use winit::{
event::{Event, WindowEvent},
event_loop::ControlFlow,
platform::macos::WindowExtMacOS,
};
use std::mem;
struct App {
pub _device: Device,
pub command_queue: CommandQueue,
pub layer: MetalLayer,
pub image_fill_cps: ComputePipelineState,
pub width: u32,
pub height: u32,
}
fn select_device() -> Option<Device> {
let devices = Device::all();
for device in devices {
if device.supports_dynamic_libraries() {
return Some(device);
}
}
None
}
impl App {
fn new(window: &winit::window::Window) -> Self {
let device = select_device().expect("no device found that supports dynamic libraries");
let command_queue = device.new_command_queue();
let layer = MetalLayer::new();
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
layer.set_presents_with_transaction(false);
layer.set_framebuffer_only(false);
unsafe {
let view = window.ns_view() as cocoa_id;
view.setWantsLayer(YES);
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));
// compile dynamic lib shader
let dylib_src_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("examples/shader-dylib/test_dylib.metal");
let install_path =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/test_dylib.metallib");
let dylib_src = std::fs::read_to_string(dylib_src_path).expect("bad shit");
let opts = metal::CompileOptions::new();
opts.set_library_type(MTLLibraryType::Dynamic);
opts.set_install_name(install_path.to_str().unwrap());
let lib = device
.new_library_with_source(dylib_src.as_str(), &opts)
.unwrap();
// create dylib
let dylib = device.new_dynamic_library(&lib).unwrap();
dylib.set_label("test_dylib");
// optional: serialize binary blob that can be loaded later
let blob_url = String::from("file://") + install_path.to_str().unwrap();
let url = URL::new_with_string(&blob_url);
dylib.serialize_to_url(&url).unwrap();
// create shader that links with dylib
let shader_src_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("examples/shader-dylib/test_shader.metal");
let shader_src = std::fs::read_to_string(shader_src_path).expect("bad shit");
let opts = metal::CompileOptions::new();
// add dynamic library to link with
let libraries = [dylib.as_ref()];
opts.set_libraries(&libraries);
// compile
let shader_lib = device
.new_library_with_source(shader_src.as_str(), &opts)
.unwrap();
let func = shader_lib.get_function("test_kernel", None).unwrap();
// create pipeline state
// linking occurs here
let image_fill_cps = device
.new_compute_pipeline_state_with_function(&func)
.unwrap();
Self {
_device: device,
command_queue,
layer,
image_fill_cps,
width: draw_size.width,
height: draw_size.height,
}
}
fn resize(&mut self, width: u32, height: u32) {
self.layer
.set_drawable_size(CGSize::new(width as f64, height as f64));
self.width = width;
self.height = height;
}
fn draw(&self) {
let drawable = match self.layer.next_drawable() {
Some(drawable) => drawable,
None => return,
};
let w = self.image_fill_cps.thread_execution_width();
let h = self.image_fill_cps.max_total_threads_per_threadgroup() / w;
let threads_per_threadgroup = MTLSize::new(w, h, 1);
let threads_per_grid = MTLSize::new(self.width as _, self.height as _, 1);
let command_buffer = self.command_queue.new_command_buffer();
{
let encoder = command_buffer.new_compute_command_encoder();
encoder.set_compute_pipeline_state(&self.image_fill_cps);
encoder.set_texture(0, Some(&drawable.texture()));
encoder.dispatch_threads(threads_per_grid, threads_per_threadgroup);
encoder.end_encoding();
}
command_buffer.present_drawable(&drawable);
command_buffer.commit();
}
}
fn main() {
let events_loop = winit::event_loop::EventLoop::new();
let size = winit::dpi::LogicalSize::new(800, 600);
let window = winit::window::WindowBuilder::new()
.with_inner_size(size)
.with_title("Metal Shader Dylib Example".to_string())
.build(&events_loop)
.unwrap();
let mut app = App::new(&window);
events_loop.run(move |event, _, control_flow| {
autoreleasepool(|| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::Resized(size) => {
app.resize(size.width, size.height);
}
_ => (),
},
Event::MainEventsCleared => {
window.request_redraw();
}
Event::RedrawRequested(_) => {
app.draw();
}
_ => {}
}
});
});
}
|