aexlo 0.1.0

Run After Effects plugins without Adobe dependencies.
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! GPU rendering support: Metal on macOS, CUDA on Windows/Linux.
//!
//! Plugins that declare `PF_OutFlag2_SUPPORTS_GPU_RENDER_F32` render into
//! 32-bit-float `PF_PixelFormat_GPU_BGRA128` worlds that live in GPU memory. To
//! drive such a plugin we expose a real device + command queue/stream through
//! [`PF_GPUDeviceSuite1`](crate::suites), back each input/output world with a
//! device buffer, and translate 8-bit CPU pixels to/from that float layout
//! around the `PF_Cmd_SMART_RENDER_GPU` call.
//!
//! The backend is chosen at compile time by swapping the `imp` module: Metal on
//! macOS, CUDA (via `cudarc`, dlopen'ing the NVIDIA driver at runtime) on
//! Windows and Linux, and an inert stub elsewhere. Runtime availability is
//! dynamic either way: [`GpuContext::new`] returns `None` when the machine has
//! no usable device, and callers fall back to CPU rendering.
//!
//! Unlike Metal's shared (host-visible) buffers, CUDA device memory cannot be
//! addressed from the CPU, so the context exposes explicit
//! [`write_buffer`](GpuContext::write_buffer) / [`read_buffer`](GpuContext::read_buffer)
//! copies instead of raw contents pointers.

use std::collections::HashSet;
use std::sync::{LazyLock, Mutex};

/// Bytes per pixel for AE's F32 GPU worlds (`PF_PixelFormat_GPU_BGRA128`: 4 × f32).
pub const GPU_BYTES_PER_PIXEL: usize = 16;

/// Process-global set of `PF_EffectWorld*` values (as `usize`) currently backed by
/// a GPU float buffer.
///
/// `PF_WorldSuite2::PF_GetPixelFormat` receives only a world pointer -- no
/// `effect_ref` -- so it cannot recover the owning [`crate::PluginInstance`]. It
/// consults this set to decide whether to report the GPU float format
/// (`PF_PixelFormat_GPU_BGRA128`) or the CPU `ARGB32` format for a given world.
static GPU_WORLD_PTRS: LazyLock<Mutex<HashSet<usize>>> = LazyLock::new(|| Mutex::new(HashSet::new()));

/// Whether `world_ptr` is currently a GPU (float) world.
pub fn is_gpu_world(world_ptr: usize) -> bool {
	GPU_WORLD_PTRS
		.lock()
		.map(|set| set.contains(&world_ptr))
		.unwrap_or(false)
}

fn register_gpu_world(world_ptr: usize) {
	if let Ok(mut set) = GPU_WORLD_PTRS.lock() {
		set.insert(world_ptr);
	}
}

fn unregister_gpu_world(world_ptr: usize) {
	if let Ok(mut set) = GPU_WORLD_PTRS.lock() {
		set.remove(&world_ptr);
	}
}

#[cfg(target_os = "macos")]
mod imp {
	use std::collections::HashMap;
	use std::ffi::c_void;

	use after_effects_sys::{PF_GPU_Framework, PF_GPU_Framework_METAL};
	use foreign_types::ForeignType;
	use metal::{Buffer, CommandQueue, Device, MTLResourceOptions};

	use super::{register_gpu_world, unregister_gpu_world};

	/// Owns the Metal device/queue for one plugin instance plus the `MTLBuffer`s
	/// that back its GPU worlds.
	///
	/// The buffers are keyed by the `PF_EffectWorld*` (as `usize`) they stand in
	/// for, so `GetGPUWorldData` can map a world the plugin was handed back to the
	/// buffer object it should render into.
	pub struct GpuContext {
		device: Device,
		queue: CommandQueue,
		buffers: HashMap<usize, Buffer>,
		/// Buffers handed out via `AllocateDeviceMemory`, keyed by their object
		/// pointer so `FreeDeviceMemory` can release them.
		raw_allocs: HashMap<usize, Buffer>,
	}

	impl GpuContext {
		/// Create a context on the system default Metal device, or `None` if the
		/// machine has no usable Metal device.
		pub fn new() -> Option<Self> {
			let device = Device::system_default()?;
			let queue = device.new_command_queue();
			Some(Self {
				device,
				queue,
				buffers: HashMap::new(),
				raw_allocs: HashMap::new(),
			})
		}

		/// The GPU framework this backend drives plugins through
		/// (`PF_GPU_Framework_METAL`).
		pub fn framework(&self) -> PF_GPU_Framework {
			PF_GPU_Framework_METAL as PF_GPU_Framework
		}

		/// Raw `id<MTLDevice>` pointer, as handed to the plugin via
		/// `PF_GPUDeviceInfo::devicePV` (it `__bridge`-casts it back to `id<MTLDevice>`).
		pub fn device_ptr(&self) -> *mut c_void {
			self.device.as_ptr() as *mut c_void
		}

		/// Raw `id<MTLCommandQueue>` pointer, for `PF_GPUDeviceInfo::command_queuePV`.
		pub fn queue_ptr(&self) -> *mut c_void {
			self.queue.as_ptr() as *mut c_void
		}

		/// Metal has no separate context object; `PF_GPUDeviceInfo::contextPV` stays null.
		pub fn context_ptr(&self) -> *mut c_void {
			std::ptr::null_mut()
		}

		/// No-op: Metal has no notion of a thread-current context.
		pub fn make_current(&self) {}

		/// No-op: Metal has no thread-current context to release.
		pub fn release_thread(&self) {}

		/// Ensure a shared `MTLBuffer` of exactly `byte_len` bytes backs `world_ptr`,
		/// (re)allocating when the size changed, and mark the world as a GPU world.
		pub fn ensure_buffer(&mut self, world_ptr: usize, byte_len: usize) -> bool {
			let needs_alloc = self
				.buffers
				.get(&world_ptr)
				.is_none_or(|b| b.length() as usize != byte_len);

			if needs_alloc {
				let buffer = self
					.device
					.new_buffer(byte_len as u64, MTLResourceOptions::StorageModeShared);
				self.buffers.insert(world_ptr, buffer);
			}

			register_gpu_world(world_ptr);
			true
		}

		/// Copy `data` into the buffer backing `world_ptr` (buffers use
		/// `StorageModeShared`, so `contents()` is a valid CPU mapping on both
		/// Apple Silicon and Intel).
		pub fn write_buffer(&mut self, world_ptr: usize, data: &[u8]) -> bool {
			let Some(buffer) = self.buffers.get(&world_ptr) else {
				return false;
			};
			if (buffer.length() as usize) < data.len() {
				return false;
			}
			// SAFETY: `buffer` is a CPU-visible shared-storage `MTLBuffer` of at least `data.len()` bytes (checked above), and a Rust slice never overlaps it.
			unsafe {
				std::ptr::copy_nonoverlapping(data.as_ptr(), buffer.contents() as *mut u8, data.len());
			}
			true
		}

		/// Copy the buffer backing `world_ptr` out into `out`.
		pub fn read_buffer(&self, world_ptr: usize, out: &mut [u8]) -> bool {
			let Some(buffer) = self.buffers.get(&world_ptr) else {
				return false;
			};
			if (buffer.length() as usize) < out.len() {
				return false;
			}
			// SAFETY: `buffer` is a CPU-visible shared-storage `MTLBuffer` of at least `out.len()` bytes (checked above), and a Rust slice never overlaps it.
			unsafe {
				std::ptr::copy_nonoverlapping(buffer.contents() as *const u8, out.as_mut_ptr(), out.len());
			}
			true
		}

		/// The `MTLBuffer` object pointer for `world_ptr`, as returned by
		/// `GetGPUWorldData` (the plugin `__bridge`-casts it to `id<MTLBuffer>`), or
		/// `None` if no buffer is registered for that world.
		pub fn buffer_object_ptr(&self, world_ptr: usize) -> Option<*mut c_void> {
			self.buffers.get(&world_ptr).map(|b| b.as_ptr() as *mut c_void)
		}

		/// Allocate a device buffer for `GPUDeviceSuite/AllocateDeviceMemory`,
		/// returning its `id<MTLBuffer>` object pointer (AE's Metal convention).
		pub fn alloc_raw(&mut self, byte_len: usize) -> Option<*mut c_void> {
			let buffer = self
				.device
				.new_buffer(byte_len as u64, MTLResourceOptions::StorageModeShared);
			let ptr = buffer.as_ptr() as *mut c_void;
			self.raw_allocs.insert(ptr as usize, buffer);
			Some(ptr)
		}

		/// Release a buffer previously returned by [`Self::alloc_raw`].
		pub fn free_raw(&mut self, ptr: *mut c_void) -> bool {
			self.raw_allocs.remove(&(ptr as usize)).is_some()
		}

		/// Drop the buffer backing `world_ptr` and its GPU-world registration.
		pub fn release_buffer(&mut self, world_ptr: usize) -> bool {
			unregister_gpu_world(world_ptr);
			self.buffers.remove(&world_ptr).is_some()
		}

		/// Block until every command buffer committed on our queue has finished.
		///
		/// The targeted plugins commit their render command buffer but deliberately
		/// do not wait (they expect the host to own queue synchronisation). Because a
		/// `MTLCommandQueue` executes committed buffers in order, committing one more
		/// empty buffer and waiting on it flushes all prior GPU work - so the output
		/// buffer is safe to read back afterwards.
		pub fn wait_for_completion(&self) {
			let command_buffer = self.queue.new_command_buffer();
			command_buffer.commit();
			command_buffer.wait_until_completed();
		}

		/// Drop the GPU-world registrations for every buffer this context owns.
		///
		/// Called when tearing down GPU rendering so stale world pointers stop being
		/// reported as GPU worlds. The `MTLBuffer`s themselves are retained for reuse
		/// until the context is dropped.
		pub fn unregister_all_worlds(&self) {
			for world_ptr in self.buffers.keys() {
				unregister_gpu_world(*world_ptr);
			}
		}
	}

	impl Drop for GpuContext {
		fn drop(&mut self) {
			self.unregister_all_worlds();
		}
	}
}

#[cfg(any(target_os = "windows", target_os = "linux"))]
mod imp {
	use std::collections::HashMap;
	use std::ffi::c_void;
	use std::sync::Arc;

	use after_effects_sys::{PF_GPU_Framework, PF_GPU_Framework_CUDA};
	use cudarc::driver::result::stream::StreamKind;
	use cudarc::driver::{CudaContext, CudaSlice, CudaStream, DevicePtr, sys};

	use super::{register_gpu_world, unregister_gpu_world};

	/// Owns the CUDA primary context and streams for one plugin instance plus the
	/// device buffers that back its GPU worlds.
	///
	/// `ctx` retains the device *primary* context (`cuDevicePrimaryCtxRetain`), so
	/// CUDA *runtime* API calls made inside the plugin (`cudaMalloc`, kernel
	/// launches) operate in the same context as our allocations. `stream` carries
	/// aexlo's own transfers; `plugin_stream` is handed to the plugin as
	/// `PF_GPUDeviceInfo::command_queuePV` (a `cudaStream_t`), matching AE's
	/// convention that plugin kernels launch on the host-provided stream.
	pub struct GpuContext {
		ctx: Arc<CudaContext>,
		stream: Arc<CudaStream>,
		/// A blocking stream (`CU_STREAM_DEFAULT`), so work on it is implicitly
		/// ordered against the legacy default stream. Plugins rely on that
		/// ordering: on a non-blocking stream Magic Bullet Looks writes its output
		/// before its Edge Softness blur has finished.
		plugin_stream: sys::CUstream,
		buffers: HashMap<usize, CudaSlice<u8>>,
		/// Allocations handed out via `AllocateDeviceMemory`, keyed by their
		/// `CUdeviceptr` so `FreeDeviceMemory` can release them.
		raw_allocs: HashMap<usize, CudaSlice<u8>>,
		/// Buffers the plugin has disposed of but whose memory is not yet freed.
		/// Plugins dispose of scratch worlds while kernels that use them are still
		/// queued on `plugin_stream`, and a free is ordered only on `stream`, so
		/// they are freed once [`Self::wait_for_completion`] has drained the
		/// context.
		retired: Vec<CudaSlice<u8>>,
	}

	impl GpuContext {
		/// Create a context on CUDA device 0, or `None` when no NVIDIA driver or
		/// device is available (so callers fall back to CPU rendering).
		pub fn new() -> Option<Self> {
			// cudarc panics (rather than erroring) when the driver library is
			// missing entirely; probe for it first so machines without an NVIDIA
			// driver degrade to the CPU path.
			// SAFETY: `is_culib_present` only tries to load the driver library; it takes no pointers.
			if !unsafe { cudarc::driver::sys::is_culib_present() } {
				log::info!("CUDA driver library not found; GPU rendering unavailable");
				return None;
			}

			let ctx = match CudaContext::new(0) {
				Ok(ctx) => ctx,
				Err(err) => {
					log::info!("CUDA context creation failed ({err}); GPU rendering unavailable");
					return None;
				}
			};
			let stream = match ctx.new_stream() {
				Ok(stream) => stream,
				Err(err) => {
					log::info!("CUDA stream creation failed ({err}); GPU rendering unavailable");
					return None;
				}
			};
			let plugin_stream = match cudarc::driver::result::stream::create(StreamKind::Default) {
				Ok(plugin_stream) => plugin_stream,
				Err(err) => {
					log::info!("CUDA stream creation failed ({err}); GPU rendering unavailable");
					return None;
				}
			};

			Some(Self {
				ctx,
				stream,
				plugin_stream,
				buffers: HashMap::new(),
				raw_allocs: HashMap::new(),
				retired: Vec::new(),
			})
		}

		/// The GPU framework this backend drives plugins through
		/// (`PF_GPU_Framework_CUDA`).
		pub fn framework(&self) -> PF_GPU_Framework {
			PF_GPU_Framework_CUDA as PF_GPU_Framework
		}

		/// The `CUdevice` ordinal, value-cast into `PF_GPUDeviceInfo::devicePV`
		/// (the SDK convention for CUDA; it is a small integer, not a pointer).
		pub fn device_ptr(&self) -> *mut c_void {
			self.ctx.cu_device() as usize as *mut c_void
		}

		/// The raw `CUstream` (== `cudaStream_t`) the plugin launches on, for
		/// `PF_GPUDeviceInfo::command_queuePV`.
		pub fn queue_ptr(&self) -> *mut c_void {
			self.plugin_stream as *mut c_void
		}

		/// The raw `CUcontext`, for `PF_GPUDeviceInfo::contextPV`.
		pub fn context_ptr(&self) -> *mut c_void {
			self.ctx.cu_ctx() as *mut c_void
		}

		/// Bind the CUDA context to the calling thread (`cuCtxSetCurrent`).
		///
		/// Must run before handing control to the plugin: its runtime-API calls
		/// resolve against the thread-current context.
		pub fn make_current(&self) {
			if let Err(err) = self.ctx.bind_to_thread() {
				log::warn!("Failed to bind CUDA context to thread: {err}");
			}
		}

		/// Unbind the CUDA context from the calling thread (`cuCtxSetCurrent(NULL)`).
		///
		/// The context stays alive. A plugin needs it current only while one of
		/// its commands runs; [`Self::make_current`] binds it again for the next.
		pub fn release_thread(&self) {
			// SAFETY: Binding a null context is how CUDA unbinds the current one; no pointer is dereferenced.
			if let Err(err) = unsafe { cudarc::driver::result::ctx::set_current(std::ptr::null_mut()) } {
				log::warn!("Failed to release CUDA context from thread: {err}");
			}
		}

		/// Ensure a device buffer of exactly `byte_len` bytes backs `world_ptr`,
		/// (re)allocating when the size changed, and mark the world as a GPU world.
		pub fn ensure_buffer(&mut self, world_ptr: usize, byte_len: usize) -> bool {
			let needs_alloc = self.buffers.get(&world_ptr).is_none_or(|b| b.len() != byte_len);

			if needs_alloc {
				match self.stream.alloc_zeros::<u8>(byte_len) {
					Ok(buffer) => {
						self.buffers.insert(world_ptr, buffer);
						// Allocation may be stream-ordered (cuMemAllocAsync); make the
						// pointer valid device-wide before the plugin can touch it from
						// another stream.
						let _ = self.stream.synchronize();
					}
					Err(err) => {
						log::error!("CUDA buffer allocation of {byte_len} bytes failed: {err}");
						return false;
					}
				}
			}

			register_gpu_world(world_ptr);
			true
		}

		/// Copy `data` into the device buffer backing `world_ptr` (`cuMemcpyHtoD`).
		pub fn write_buffer(&mut self, world_ptr: usize, data: &[u8]) -> bool {
			let Some(buffer) = self.buffers.get_mut(&world_ptr) else {
				return false;
			};
			if buffer.len() < data.len() {
				return false;
			}
			if let Err(err) = self.stream.memcpy_htod(data, buffer) {
				log::error!("CUDA host-to-device copy failed: {err}");
				return false;
			}
			// The copy is stream-ordered; flush it so the plugin sees the pixels no
			// matter which stream it launches on.
			if let Err(err) = self.stream.synchronize() {
				log::error!("CUDA stream synchronize failed after upload: {err}");
				return false;
			}
			true
		}

		/// Copy the device buffer backing `world_ptr` out into `out` (`cuMemcpyDtoH`).
		pub fn read_buffer(&self, world_ptr: usize, out: &mut [u8]) -> bool {
			let Some(buffer) = self.buffers.get(&world_ptr) else {
				return false;
			};
			if out.len() < buffer.len() {
				return false;
			}
			if let Err(err) = self.stream.memcpy_dtoh(buffer, out) {
				log::error!("CUDA device-to-host copy failed: {err}");
				return false;
			}
			// memcpy_dtoh is asynchronous; the host buffer is only valid after the
			// stream drains.
			if let Err(err) = self.stream.synchronize() {
				log::error!("CUDA stream synchronize failed after readback: {err}");
				return false;
			}
			true
		}

		/// The raw `CUdeviceptr` for `world_ptr`'s buffer, as returned by
		/// `GetGPUWorldData` (the plugin uses it directly as a device pointer), or
		/// `None` if no buffer is registered for that world.
		pub fn buffer_object_ptr(&self, world_ptr: usize) -> Option<*mut c_void> {
			self.buffers.get(&world_ptr).map(|b| {
				let (ptr, _sync) = b.device_ptr(&self.stream);
				ptr as usize as *mut c_void
			})
		}

		/// Allocate device memory for `GPUDeviceSuite/AllocateDeviceMemory`,
		/// returning the raw `CUdeviceptr` (AE's CUDA convention).
		pub fn alloc_raw(&mut self, byte_len: usize) -> Option<*mut c_void> {
			match self.stream.alloc_zeros::<u8>(byte_len) {
				Ok(buffer) => {
					let key = {
						let (ptr, _sync) = buffer.device_ptr(&self.stream);
						ptr as usize
					};
					// Stream-ordered allocation: settle it before the plugin uses the
					// pointer on a stream of its own.
					let _ = self.stream.synchronize();
					self.raw_allocs.insert(key, buffer);
					Some(key as *mut c_void)
				}
				Err(err) => {
					log::error!("CUDA AllocateDeviceMemory of {byte_len} bytes failed: {err}");
					None
				}
			}
		}

		/// Release memory previously returned by [`Self::alloc_raw`]. The memory
		/// is freed at the next [`Self::wait_for_completion`].
		pub fn free_raw(&mut self, ptr: *mut c_void) -> bool {
			match self.raw_allocs.remove(&(ptr as usize)) {
				Some(buffer) => {
					self.retired.push(buffer);
					true
				}
				None => false,
			}
		}

		/// Drop the GPU-world registration for `world_ptr`. Its device buffer is
		/// freed at the next [`Self::wait_for_completion`].
		pub fn release_buffer(&mut self, world_ptr: usize) -> bool {
			unregister_gpu_world(world_ptr);
			match self.buffers.remove(&world_ptr) {
				Some(buffer) => {
					self.retired.push(buffer);
					true
				}
				None => false,
			}
		}

		/// Block until all outstanding GPU work has finished, then free the
		/// buffers the plugin released meanwhile.
		///
		/// A context synchronize (`cuCtxSynchronize`) waits for *every* stream in
		/// the context - ours and any the plugin created - so a separate stream
		/// synchronize beforehand would be pure redundant latency.
		pub fn wait_for_completion(&mut self) {
			match self.ctx.synchronize() {
				Ok(()) => self.retired.clear(),
				Err(err) => log::warn!("CUDA context synchronize failed: {err}"),
			}
		}

		/// Drop the GPU-world registrations for every buffer this context owns.
		///
		/// Called when tearing down GPU rendering so stale world pointers stop being
		/// reported as GPU worlds. The device buffers themselves are retained for
		/// reuse until the context is dropped.
		pub fn unregister_all_worlds(&self) {
			for world_ptr in self.buffers.keys() {
				unregister_gpu_world(*world_ptr);
			}
		}
	}

	impl Drop for GpuContext {
		fn drop(&mut self) {
			self.unregister_all_worlds();
			let _ = self.ctx.synchronize();
			// SAFETY: created in `new`, never handed out beyond the plugin, and the
			// context has just drained everything queued on it.
			let _ = unsafe { cudarc::driver::result::stream::destroy(self.plugin_stream) };
		}
	}
}

#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
mod imp {
	use std::ffi::c_void;

	use after_effects_sys::{PF_GPU_Framework, PF_GPU_Framework_NONE};

	/// Fallback stub: no GPU backend exists for this platform, so a context can
	/// never be created and every accessor is inert. Keeps the crate compiling.
	pub struct GpuContext;

	impl GpuContext {
		pub fn new() -> Option<Self> {
			None
		}
		pub fn framework(&self) -> PF_GPU_Framework {
			PF_GPU_Framework_NONE as PF_GPU_Framework
		}
		pub fn device_ptr(&self) -> *mut c_void {
			std::ptr::null_mut()
		}
		pub fn queue_ptr(&self) -> *mut c_void {
			std::ptr::null_mut()
		}
		pub fn context_ptr(&self) -> *mut c_void {
			std::ptr::null_mut()
		}
		pub fn make_current(&self) {}
		pub fn release_thread(&self) {}
		pub fn ensure_buffer(&mut self, _world_ptr: usize, _byte_len: usize) -> bool {
			false
		}
		pub fn write_buffer(&mut self, _world_ptr: usize, _data: &[u8]) -> bool {
			false
		}
		pub fn read_buffer(&self, _world_ptr: usize, _out: &mut [u8]) -> bool {
			false
		}
		pub fn buffer_object_ptr(&self, _world_ptr: usize) -> Option<*mut c_void> {
			None
		}
		pub fn alloc_raw(&mut self, _byte_len: usize) -> Option<*mut c_void> {
			None
		}
		pub fn free_raw(&mut self, _ptr: *mut c_void) -> bool {
			false
		}
		pub fn release_buffer(&mut self, _world_ptr: usize) -> bool {
			false
		}
		pub fn unregister_all_worlds(&self) {}
		pub fn wait_for_completion(&self) {}
	}
}

pub use imp::GpuContext;

/// The name of the GPU this host renders on, when it can tell without
/// creating a render context: the default Metal device on macOS. `None`
/// elsewhere or without a GPU.
pub fn device_name() -> Option<String> {
	#[cfg(target_os = "macos")]
	{
		metal::Device::system_default().map(|device| device.name().to_string())
	}
	#[cfg(not(target_os = "macos"))]
	{
		None
	}
}