Make pgpu-render compile on non apple platforms

Also add `resolver="2"` to the entire workspace, in case we gain a wgpu
dependency
This commit is contained in:
Daniel McNab 2022-06-25 14:11:13 +01:00
parent 60d197bb4e
commit 28cbc8f199
6 changed files with 34 additions and 1 deletions

View file

@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"pgpu-render",
@ -7,5 +8,5 @@ members = [
"piet-gpu-hal",
"piet-gpu-types",
"piet-scene",
"tests"
"tests",
]

View file

@ -13,6 +13,7 @@ piet-gpu = { path = "../piet-gpu" }
piet-gpu-hal = { path = "../piet-gpu-hal" }
piet-scene = { path = "../piet-scene" }
[target.'cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))'.dependencies]
metal = "0.22"
cocoa = "0.24.0"
objc = "0.2.5"

View file

@ -22,6 +22,7 @@ fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_define("target_os", "ios", "__APPLE__")
.with_header("/** Automatically generated from pgpu-render/src/lib.rs with cbindgen. **/")
.generate()
.expect("Unable to generate bindings")

View file

@ -61,13 +61,16 @@ struct PgpuRect {
extern "C" {
#if defined(__APPLE__)
/// Creates a new piet-gpu renderer for the specified Metal device and
/// command queue.
///
/// device: MTLDevice*
/// queue: MTLCommandQueue*
PgpuRenderer *pgpu_renderer_new(void *device, void *queue);
#endif
#if defined(__APPLE__)
/// Renders a prepared scene into a texture target. Commands for rendering are
/// recorded into the specified command buffer. Returns an id representing
/// resources that may have been allocated during this process. After the
@ -80,6 +83,7 @@ uint32_t pgpu_renderer_render(PgpuRenderer *renderer,
const PgpuScene *scene,
void *target,
void *cmdbuf);
#endif
/// Releases the internal resources associated with the specified id from a
/// previous render operation.

View file

@ -14,6 +14,16 @@
//
// Also licensed under MIT license, at your choice.
// We only really have implementations for IOS targets so far
// Note that this is the same cfg that wgpu uses for metal support
#![cfg_attr(
not(all(
not(target_arch = "wasm32"),
any(target_os = "ios", target_os = "macos")
)),
allow(unused)
)]
mod render;
use render::*;
@ -26,6 +36,10 @@ use std::mem::transmute;
/// device: MTLDevice*
/// queue: MTLCommandQueue*
#[no_mangle]
#[cfg(all(
not(target_arch = "wasm32"),
any(target_os = "ios", target_os = "macos")
))]
pub unsafe extern "C" fn pgpu_renderer_new(
device: *mut c_void,
queue: *mut c_void,
@ -44,6 +58,10 @@ pub unsafe extern "C" fn pgpu_renderer_new(
/// target: MTLTexture*
/// cmdbuf: MTLCommandBuffer*
#[no_mangle]
#[cfg(all(
not(target_arch = "wasm32"),
any(target_os = "ios", target_os = "macos")
))]
pub unsafe extern "C" fn pgpu_renderer_render(
renderer: *mut PgpuRenderer,
scene: *const PgpuScene,

View file

@ -33,6 +33,10 @@ pub struct PgpuRenderer {
}
impl PgpuRenderer {
#[cfg(all(
not(target_arch = "wasm32"),
any(target_os = "ios", target_os = "macos")
))]
pub fn new(device: &metal::DeviceRef, queue: &metal::CommandQueueRef) -> Self {
let piet_device = piet_gpu_hal::Device::new_from_raw_mtl(device, &queue);
let session = Session::new(piet_device);
@ -47,6 +51,10 @@ impl PgpuRenderer {
}
}
#[cfg(all(
not(target_arch = "wasm32"),
any(target_os = "ios", target_os = "macos")
))]
pub fn render(
&mut self,
scene: &PgpuScene,