From 52ee3e019cc968947dee32ee908aa8d58cd05102 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 29 Apr 2019 22:41:09 -0400 Subject: [PATCH] Remote example and improved header, tested on CI --- .travis.yml | 2 +- Makefile | 13 +- examples/hello_remote_c/CMakeLists.txt | 15 ++ examples/hello_remote_c/main.c | 18 +++ examples/hello_triangle_c/main.c | 2 +- wgpu-bindings/src/main.rs | 13 +- wgpu-bindings/wgpu-native.h | 196 +++++++++++++++++++++++++ wgpu-bindings/wgpu-remote.h | 15 +- wgpu-bindings/wgpu.h | 17 ++- wgpu-native/src/binding_model.rs | 2 + wgpu-native/src/command/render.rs | 12 +- wgpu-native/src/device.rs | 10 ++ wgpu-native/src/hub.rs | 34 ++--- wgpu-native/src/instance.rs | 3 + wgpu-native/src/lib.rs | 21 ++- wgpu-native/src/resource.rs | 1 + wgpu-remote/Cargo.toml | 9 +- wgpu-remote/src/lib.rs | 70 +++++++-- wgpu-remote/src/server.rs | 32 +++- wgpu-rs/Cargo.toml | 12 +- wgpu-rs/src/lib.rs | 10 +- 21 files changed, 450 insertions(+), 57 deletions(-) create mode 100644 examples/hello_remote_c/CMakeLists.txt create mode 100644 examples/hello_remote_c/main.c create mode 100644 wgpu-bindings/wgpu-native.h diff --git a/.travis.yml b/.travis.yml index 29cd64187..33e5af6a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,4 +43,4 @@ before_install: script: - cargo test - - if [[ $TRAVIS_OS_NAME == "osx" ]]; then (brew update && brew upgrade cmake && brew install glfw3 && cd wgpu-native && cargo build --features=local,gfx-backend-metal && cd ../examples/hello_triangle_c && mkdir build && cd build && cmake .. && make); fi + - if [[ $TRAVIS_OS_NAME == "osx" ]]; then (brew update && brew upgrade cmake && brew install glfw3 && make ci-examples); fi diff --git a/Makefile b/Makefile index 61c00448a..93b21226f 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ else endif -.PHONY: all check test doc clear lib-native lib-rust examples-native examples-rust gfx +.PHONY: all check test doc clear lib-native lib-remote lib-rust ci-examples examples-native examples-rust examples-gfx gfx all: examples-native examples-rust examples-gfx @@ -50,15 +50,24 @@ clear: lib-native: Cargo.lock wgpu-native/Cargo.toml $(wildcard wgpu-native/**/*.rs) cargo build --manifest-path wgpu-native/Cargo.toml --features "local,$(FEATURE_NATIVE)" +lib-remote: Cargo.lock wgpu-remote/Cargo.toml $(wildcard wgpu-native/**/*.rs wgpu-remote/**/*.rs) + cargo build --manifest-path wgpu-remote/Cargo.toml --features $(FEATURE_RUST) + lib-rust: Cargo.lock wgpu-rs/Cargo.toml $(wildcard wgpu-rs/**/*.rs) cargo build --manifest-path wgpu-rs/Cargo.toml --features $(FEATURE_RUST) -wgpu-bindings/*.h: Cargo.lock wgpu-bindings/src/*.rs lib-native +wgpu-bindings/*.h: Cargo.lock $(wildcard wgpu-bindings/src/*.rs) lib-native lib-remote cargo +nightly run --manifest-path wgpu-bindings/Cargo.toml examples-native: lib-native wgpu-bindings/wgpu.h $(wildcard wgpu-native/**/*.c) #$(MAKE) -C examples +ci-examples: + cargo build --manifest-path wgpu-native/Cargo.toml --features=local,$(FEATURE_NATIVE) + cargo build --manifest-path wgpu-remote/Cargo.toml --features=$(FEATURE_RUST) + cd examples/hello_triangle_c && mkdir -p build && cd build && cmake .. && make + cd examples/hello_remote_c && mkdir -p build && cd build && cmake .. && make + examples-rust: lib-rust examples/Cargo.toml $(wildcard wgpu-native/**/*.rs) cargo build --manifest-path examples/Cargo.toml --features $(FEATURE_RUST) diff --git a/examples/hello_remote_c/CMakeLists.txt b/examples/hello_remote_c/CMakeLists.txt new file mode 100644 index 000000000..035845275 --- /dev/null +++ b/examples/hello_remote_c/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.11b) + +project(hello_remote) + +set(TARGET_NAME hello_remote) + +add_executable(hello_remote main.c) + +find_package(glfw3) + +find_library(WGPU_LIBRARY wgpu_remote + HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../../target/debug" +) + +target_link_libraries(${TARGET_NAME} ${WGPU_LIBRARY}) diff --git a/examples/hello_remote_c/main.c b/examples/hello_remote_c/main.c new file mode 100644 index 000000000..631f19352 --- /dev/null +++ b/examples/hello_remote_c/main.c @@ -0,0 +1,18 @@ +#include "./../../wgpu-bindings/wgpu-native.h" +#include "./../../wgpu-bindings/wgpu-remote.h" +#include + +int main() { + WGPUInfrastructure infra = wgpu_initialize(); + + if (!infra.client || !infra.server || infra.error) { + printf("Cannot initialize WGPU: %s", infra.error); + return 1; + } + + //TODO: do something meaningful + + wgpu_terminate(infra.client); + + return 0; +} diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index cc7e84883..c9c509390 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -219,7 +219,7 @@ int main() { }); wgpu_render_pass_set_pipeline(rpass, render_pipeline); - wgpu_render_pass_set_bind_group(rpass, 0, bind_group); + wgpu_render_pass_set_bind_group(rpass, 0, bind_group, NULL, 0); wgpu_render_pass_draw(rpass, 3, 1, 0, 0); WGPUQueueId queue = wgpu_device_get_queue(device); WGPUCommandBufferId cmd_buf = wgpu_render_pass_end_pass(rpass); diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index 0eafa66f5..3efc23de8 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -1,23 +1,33 @@ extern crate cbindgen; +use cbindgen::ItemType as It; use std::path::PathBuf; struct Binding<'a> { library: &'a str, features: &'a [&'a str], output: &'a str, + item_types: &'a [It], } -const BINDINGS: [Binding; 2] = [ +const BINDINGS: [Binding; 3] = [ Binding { library: "wgpu-native", features: &["local"], output: "wgpu.h", + item_types: &[It::Enums, It::Constants, It::Structs, It::Typedefs, It::Functions], + }, + Binding { + library: "wgpu-native", + features: &["remote"], + output: "wgpu-native.h", + item_types: &[It::Enums, It::Constants, It::Structs, It::Typedefs], }, Binding { library: "wgpu-remote", features: &[], output: "wgpu-remote.h", + item_types: &[It::Enums, It::Constants, It::Structs, It::Typedefs, It::OpaqueItems, It::Functions], }, ]; @@ -33,6 +43,7 @@ fn main() { }, export: cbindgen::ExportConfig { prefix: Some(String::from("WGPU")), + item_types: bind.item_types.to_vec(), ..Default::default() }, language: cbindgen::Language::C, diff --git a/wgpu-bindings/wgpu-native.h b/wgpu-bindings/wgpu-native.h new file mode 100644 index 000000000..91743091b --- /dev/null +++ b/wgpu-bindings/wgpu-native.h @@ -0,0 +1,196 @@ +#include +#include +#include +#include + +#define WGPUBITS_PER_BYTE 8 + +#define WGPUMAX_BIND_GROUPS 4 + +#define WGPUMAX_COLOR_TARGETS 4 + +typedef enum { + WGPUBufferMapAsyncStatus_Success, + WGPUBufferMapAsyncStatus_Error, + WGPUBufferMapAsyncStatus_Unknown, + WGPUBufferMapAsyncStatus_ContextLost, +} WGPUBufferMapAsyncStatus; + +typedef enum { + WGPUPowerPreference_Default = 0, + WGPUPowerPreference_LowPower = 1, + WGPUPowerPreference_HighPerformance = 2, +} WGPUPowerPreference; + +typedef uint32_t WGPUIndex; + +typedef uint32_t WGPUEpoch; + +typedef struct { + WGPUIndex _0; + WGPUEpoch _1; +} WGPUId; + +typedef WGPUId WGPUAdapterId; + +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + +typedef struct { + bool anisotropic_filtering; +} WGPUExtensions; + +typedef struct { + WGPUExtensions extensions; +} WGPUDeviceDescriptor; + +typedef struct { + WGPUAdapterId adapter; + WGPUAdapterDescriptor adapter_desc; + WGPUDeviceDescriptor device_desc; +} WGPUForcedExports; + +typedef WGPUId WGPUBindGroupId; + +typedef WGPUId WGPUBufferId; + +typedef void (*WGPUBufferMapReadCallback)(WGPUBufferMapAsyncStatus status, const uint8_t *data, uint8_t *userdata); + +typedef void (*WGPUBufferMapWriteCallback)(WGPUBufferMapAsyncStatus status, uint8_t *data, uint8_t *userdata); + +typedef WGPUId WGPUCommandBufferId; + +typedef struct { + WGPUBufferId buffer; + uint32_t offset; + uint32_t row_pitch; + uint32_t image_height; +} WGPUBufferCopyView; + +typedef WGPUId WGPUTextureId; + +typedef struct { + float x; + float y; + float z; +} WGPUOrigin3d; + +typedef struct { + WGPUTextureId texture; + uint32_t level; + uint32_t slice; + WGPUOrigin3d origin; +} WGPUTextureCopyView; + +typedef struct { + uint32_t width; + uint32_t height; + uint32_t depth; +} WGPUExtent3d; + +typedef WGPUCommandBufferId WGPUCommandEncoderId; + +typedef WGPUId WGPUComputePassId; + +typedef WGPUId WGPUComputePipelineId; + +typedef WGPUId WGPUDeviceId; + +typedef WGPUDeviceId WGPUQueueId; + +typedef WGPUId WGPURenderPassId; + +typedef struct { + float r; + float g; + float b; + float a; +} WGPUColor; + +typedef WGPUId WGPURenderPipelineId; + +typedef WGPUId WGPUTextureViewId; + +typedef struct { + WGPUTextureId texture_id; + WGPUTextureViewId view_id; +} WGPUSwapChainOutput; + +typedef WGPUId WGPUSurfaceId; + +typedef WGPUSurfaceId WGPUSwapChainId; + +#define WGPUBufferUsageFlags_INDEX 16 + +#define WGPUBufferUsageFlags_MAP_READ 1 + +#define WGPUBufferUsageFlags_MAP_WRITE 2 + +#define WGPUBufferUsageFlags_NONE 0 + +#define WGPUBufferUsageFlags_STORAGE 128 + +#define WGPUBufferUsageFlags_TRANSFER_DST 8 + +#define WGPUBufferUsageFlags_TRANSFER_SRC 4 + +#define WGPUBufferUsageFlags_UNIFORM 64 + +#define WGPUBufferUsageFlags_VERTEX 32 + +#define WGPUColorWriteFlags_ALL 15 + +#define WGPUColorWriteFlags_ALPHA 8 + +#define WGPUColorWriteFlags_BLUE 4 + +#define WGPUColorWriteFlags_COLOR 7 + +#define WGPUColorWriteFlags_GREEN 2 + +#define WGPUColorWriteFlags_RED 1 + +#define WGPUColor_BLACK (WGPUColor){ .r = 0, .g = 0, .b = 0, .a = 1 } + +#define WGPUColor_BLUE (WGPUColor){ .r = 0, .g = 0, .b = 1, .a = 1 } + +#define WGPUColor_GREEN (WGPUColor){ .r = 0, .g = 1, .b = 0, .a = 1 } + +#define WGPUColor_RED (WGPUColor){ .r = 1, .g = 0, .b = 0, .a = 1 } + +#define WGPUColor_TRANSPARENT (WGPUColor){ .r = 0, .g = 0, .b = 0, .a = 0 } + +#define WGPUColor_WHITE (WGPUColor){ .r = 1, .g = 1, .b = 1, .a = 1 } + +#define WGPUPipelineFlags_BLEND_COLOR 1 + +#define WGPUShaderStageFlags_COMPUTE 4 + +#define WGPUShaderStageFlags_FRAGMENT 2 + +#define WGPUShaderStageFlags_VERTEX 1 + +#define WGPUTextureAspectFlags_COLOR 1 + +#define WGPUTextureAspectFlags_DEPTH 2 + +#define WGPUTextureAspectFlags_STENCIL 4 + +#define WGPUTextureUsageFlags_NONE 0 + +#define WGPUTextureUsageFlags_OUTPUT_ATTACHMENT 16 + +#define WGPUTextureUsageFlags_SAMPLED 4 + +#define WGPUTextureUsageFlags_STORAGE 8 + +#define WGPUTextureUsageFlags_TRANSFER_DST 2 + +#define WGPUTextureUsageFlags_TRANSFER_SRC 1 + +#define WGPUTextureUsageFlags_UNINITIALIZED 65535 + +#define WGPUTrackPermit_EXTEND (WGPUTrackPermit){ .bits = 1 } + +#define WGPUTrackPermit_REPLACE (WGPUTrackPermit){ .bits = 2 } diff --git a/wgpu-bindings/wgpu-remote.h b/wgpu-bindings/wgpu-remote.h index 9d55b1a32..b07a9194c 100644 --- a/wgpu-bindings/wgpu-remote.h +++ b/wgpu-bindings/wgpu-remote.h @@ -5,10 +5,23 @@ typedef struct WGPUClient WGPUClient; +typedef struct WGPUServer WGPUServer; + +typedef struct { + WGPUClient *client; + WGPUServer *server; + const uint8_t *error; +} WGPUInfrastructure; + WGPUDeviceId wgpu_adapter_create_device(const WGPUClient *client, WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); +WGPUInfrastructure wgpu_initialize(void); + WGPUAdapterId wgpu_instance_get_adapter(const WGPUClient *client, - WGPUInstanceId instance_id, const WGPUAdapterDescriptor *desc); + +void wgpu_server_process(const WGPUServer *server); + +void wgpu_terminate(WGPUClient *client); diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 55cf6edf0..5dc554747 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -55,6 +55,13 @@ typedef enum { WGPUBorderColor_OpaqueWhite = 2, } WGPUBorderColor; +typedef enum { + WGPUBufferMapAsyncStatus_Success, + WGPUBufferMapAsyncStatus_Error, + WGPUBufferMapAsyncStatus_Unknown, + WGPUBufferMapAsyncStatus_ContextLost, +} WGPUBufferMapAsyncStatus; + typedef enum { WGPUCompareFunction_Never = 0, WGPUCompareFunction_Less = 1, @@ -244,8 +251,6 @@ typedef enum { WGPUVertexFormat_Int4 = 48, } WGPUVertexFormat; -typedef struct WGPUBufferMapAsyncStatus WGPUBufferMapAsyncStatus; - typedef uint32_t WGPUIndex; typedef uint32_t WGPUEpoch; @@ -713,10 +718,6 @@ void wgpu_compute_pass_dispatch(WGPUComputePassId pass_id, uint32_t x, uint32_t WGPUCommandBufferId wgpu_compute_pass_end_pass(WGPUComputePassId pass_id); -void wgpu_compute_pass_set_bind_group(WGPUComputePassId pass_id, - uint32_t index, - WGPUBindGroupId bind_group_id); - void wgpu_compute_pass_set_pipeline(WGPUComputePassId pass_id, WGPUComputePipelineId pipeline_id); WGPUInstanceId wgpu_create_instance(void); @@ -797,7 +798,9 @@ WGPUCommandBufferId wgpu_render_pass_end_pass(WGPURenderPassId pass_id); void wgpu_render_pass_set_bind_group(WGPURenderPassId pass_id, uint32_t index, - WGPUBindGroupId bind_group_id); + WGPUBindGroupId bind_group_id, + const uint32_t *offsets_ptr, + uintptr_t offsets_count); void wgpu_render_pass_set_blend_color(WGPURenderPassId pass_id, const WGPUColor *color); diff --git a/wgpu-native/src/binding_model.rs b/wgpu-native/src/binding_model.rs index fe7b5b187..f2c7c6759 100644 --- a/wgpu-native/src/binding_model.rs +++ b/wgpu-native/src/binding_model.rs @@ -42,6 +42,7 @@ pub struct BindGroupLayoutDescriptor { pub struct BindGroupLayout { pub(crate) raw: B::DescriptorSetLayout, pub(crate) bindings: Vec, + pub(crate) dynamic_count: usize, } #[repr(C)] @@ -87,4 +88,5 @@ pub struct BindGroup { pub(crate) layout_id: BindGroupLayoutId, pub(crate) life_guard: LifeGuard, pub(crate) used: TrackerSet, + pub(crate) dynamic_count: usize, } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 67e710adf..56e1bf37c 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -224,13 +224,23 @@ pub extern "C" fn wgpu_render_pass_set_bind_group( pass_id: RenderPassId, index: u32, bind_group_id: BindGroupId, - offsets: &[u32], + offsets_ptr: *const u32, + offsets_count: usize, ) { let mut pass_guard = HUB.render_passes.write(); let pass = &mut pass_guard[pass_id]; let bind_group_guard = HUB.bind_groups.read(); let bind_group = &bind_group_guard[bind_group_id]; + assert_eq!(bind_group.dynamic_count, offsets_count); + let offsets = if offsets_count != 0 { + unsafe { + slice::from_raw_parts(offsets_ptr, offsets_count) + } + } else { + &[] + }; + pass.trackers.consume_by_extend(&bind_group.used); if let Some((pipeline_layout_id, follow_up)) = diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 610d39427..01201f217 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -856,6 +856,7 @@ pub extern "C" fn wgpu_texture_create_default_view(texture_id: TextureId) -> Tex id } + #[no_mangle] pub extern "C" fn wgpu_texture_destroy(texture_id: TextureId) { let texture_guard = HUB.textures.read(); @@ -952,6 +953,14 @@ pub fn device_create_bind_group_layout( binding_model::BindGroupLayout { raw, bindings: bindings.to_vec(), + dynamic_count: bindings + .iter() + .filter(|b| match b.ty { + binding_model::BindingType::UniformBufferDynamic | + binding_model::BindingType::StorageBufferDynamic => true, + _ => false, + }) + .count(), } } @@ -1091,6 +1100,7 @@ pub fn device_create_bind_group( layout_id: desc.layout, life_guard: LifeGuard::new(), used, + dynamic_count: bind_group_layout.dynamic_count, } } diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index d34c00b2e..14ad6e755 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -171,23 +171,23 @@ impl Registry { #[derive(Default)] pub struct Hub { - pub(crate) instances: Arc>, - pub(crate) adapters: Arc>, - pub(crate) devices: Arc>, - pub(crate) pipeline_layouts: Arc>, - pub(crate) bind_group_layouts: Arc>, - pub(crate) bind_groups: Arc>, - pub(crate) shader_modules: Arc>, - pub(crate) command_buffers: Arc>, - pub(crate) render_pipelines: Arc>, - pub(crate) compute_pipelines: Arc>, - pub(crate) render_passes: Arc>, - pub(crate) compute_passes: Arc>, - pub(crate) buffers: Arc>, - pub(crate) textures: Arc>, - pub(crate) texture_views: Arc>, - pub(crate) samplers: Arc>, - pub(crate) surfaces: Arc>, + pub instances: Arc>, + pub adapters: Arc>, + pub devices: Arc>, + pub pipeline_layouts: Arc>, + pub bind_group_layouts: Arc>, + pub bind_groups: Arc>, + pub shader_modules: Arc>, + pub command_buffers: Arc>, + pub render_pipelines: Arc>, + pub compute_pipelines: Arc>, + pub render_passes: Arc>, + pub compute_passes: Arc>, + pub buffers: Arc>, + pub textures: Arc>, + pub texture_views: Arc>, + pub samplers: Arc>, + pub surfaces: Arc>, } lazy_static! { diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index a41d18783..0b4239a8c 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -19,18 +19,21 @@ pub enum PowerPreference { } #[repr(C)] +#[derive(Debug)] #[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))] pub struct AdapterDescriptor { pub power_preference: PowerPreference, } #[repr(C)] +#[derive(Debug)] #[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))] pub struct Extensions { pub anisotropic_filtering: bool, } #[repr(C)] +#[derive(Debug)] #[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))] pub struct DeviceDescriptor { pub extensions: Extensions, diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 25a99c1bf..e596980bc 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -35,7 +35,7 @@ pub use self::binding_model::*; pub use self::command::*; pub use self::device::*; #[cfg(feature = "remote")] -pub use self::hub::{Id, IdentityManager, Registry, HUB}; +pub use self::hub::{IdentityManager, Registry, HUB}; pub use self::instance::*; pub use self::pipeline::*; pub use self::resource::*; @@ -46,6 +46,25 @@ use std::sync::atomic::{AtomicUsize, Ordering}; type SubmissionIndex = usize; +//TODO: remove this structure and entry point. +// They are currently needed to force `cbindgen` to export some of +// the types when building with `remote` feature excluding the +// functions export (as a `wgpu-remote.h` dependency). +#[cfg(feature = "remote")] +#[repr(C)] +#[derive(Debug)] +pub struct ForcedExports { + pub adapter: AdapterId, + pub adapter_desc: AdapterDescriptor, + pub device_desc: DeviceDescriptor, +} +#[cfg(feature = "remote")] +#[no_mangle] +pub extern "C" fn forced_exports(fe: ForcedExports) { + println!("{:?}", fe); +} + + //TODO: make it private. Currently used for swapchain creation impl. #[derive(Debug)] pub struct RefCount(ptr::NonNull); diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index f76354447..ab15d706a 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -39,6 +39,7 @@ pub struct BufferDescriptor { pub usage: BufferUsageFlags, } +#[repr(C)] pub enum BufferMapAsyncStatus { Success, Error, diff --git a/wgpu-remote/Cargo.toml b/wgpu-remote/Cargo.toml index 63dcadbc0..5da029b3e 100644 --- a/wgpu-remote/Cargo.toml +++ b/wgpu-remote/Cargo.toml @@ -7,11 +7,18 @@ authors = [ ] edition = "2018" +[lib] +crate-type = ["lib", "cdylib", "staticlib"] + [features] default = [] +vulkan = ["wgn/gfx-backend-vulkan"] +dx11 = ["wgn/gfx-backend-dx11"] +dx12 = ["wgn/gfx-backend-dx12"] +metal = ["wgn/gfx-backend-metal"] [dependencies] -wgpu-native = { path = "../wgpu-native", features = ["remote"] } +wgn = { package = "wgpu-native", path = "../wgpu-native", features = ["remote"] } ipc-channel = "0.11" log = "0.4" parking_lot = { version = "0.7" } diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index c2c67f349..01b72b365 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -1,18 +1,26 @@ -use ipc_channel::ipc::IpcSender; +use crate::server::Server; + +use ipc_channel::ipc; +use log::error; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; +use wgn; + +use std::ptr; + +mod server; -use wgpu_native as wgn; #[derive(Serialize, Deserialize)] -pub enum InstanceMessage { +enum InstanceMessage { InstanceGetAdapter(wgn::InstanceId, wgn::AdapterDescriptor, wgn::AdapterId), AdapterCreateDevice(wgn::AdapterId, wgn::DeviceDescriptor, wgn::DeviceId), + Terminate, } /// A message on the timeline of devices, queues, and resources. #[derive(Serialize, Deserialize)] -pub enum GlobalMessage { +enum GlobalMessage { Instance(InstanceMessage), //Device(DeviceMessage), //Queue(QueueMessage), @@ -21,34 +29,78 @@ pub enum GlobalMessage { } #[derive(Default)] -pub struct IdentityHub { +struct IdentityHub { adapters: wgn::IdentityManager, devices: wgn::IdentityManager, } pub struct Client { - channel: IpcSender, + channel: ipc::IpcSender, + instance_id: wgn::InstanceId, identity: Mutex, } impl Client { - pub fn new(channel: IpcSender) -> Self { + fn new( + channel: ipc::IpcSender, + instance_id: wgn::InstanceId, + ) -> Self { Client { channel, + instance_id, identity: Mutex::new(IdentityHub::default()), } } } +#[repr(C)] +pub struct Infrastructure { + pub client: *mut Client, + pub server: *mut Server, + pub error: *const u8, +} + +#[no_mangle] +pub extern "C" fn wgpu_initialize() -> Infrastructure { + match ipc::channel() { + Ok((sender, receiver)) => { + let instance_id = wgn::IdentityManager::default().alloc(); // TODO: static + let client = Client::new(sender, instance_id); + let server = Server::new(receiver, instance_id); + Infrastructure { + client: Box::into_raw(Box::new(client)), + server: Box::into_raw(Box::new(server)), + error: ptr::null(), + } + } + Err(e) => { + error!("WGPU initialize failed: {:?}", e); + Infrastructure { + client: ptr::null_mut(), + server: ptr::null_mut(), + error: ptr::null(), //TODO + } + } + } + +} + +#[no_mangle] +pub extern "C" fn wgpu_terminate(client: *mut Client) { + let client = unsafe { + Box::from_raw(client) + }; + let _ = client.channel.send(GlobalMessage::Instance(InstanceMessage::Terminate)); +} + #[no_mangle] pub extern "C" fn wgpu_instance_get_adapter( client: &Client, - instance_id: wgn::InstanceId, desc: &wgn::AdapterDescriptor, ) -> wgn::AdapterId { let id = client.identity.lock().adapters.alloc(); let msg = GlobalMessage::Instance(InstanceMessage::InstanceGetAdapter( - instance_id, + client.instance_id, desc.clone(), id, )); diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index f3a67d993..d505f79db 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -1,25 +1,29 @@ -use crate::{GlobalMessage, InstanceMessage} +use crate::{GlobalMessage, InstanceMessage}; use ipc_channel::ipc::IpcReceiver; - use wgn; -struct Server { +pub struct Server { channel: IpcReceiver, - instance_id: wgn::IntanceId, } impl Server { - pub fn new(channel: IpcReceiver) -> Self { + pub(crate) fn new(channel: IpcReceiver, instance_id: wgn::InstanceId) -> Self { + let instance = wgn::create_instance(); + wgn::HUB.instances.register(instance_id, instance); Server { channel, - instance_id: wgn::wgpu_create_instance(), } } } -pub fn process(message: GlobalMessage) { +enum ControlFlow { + Continue, + Terminate, +} + +fn process(message: GlobalMessage) -> ControlFlow { match message { GlobalMessage::Instance(msg) => match msg { InstanceMessage::InstanceGetAdapter(instance_id, ref desc, id) => { @@ -30,6 +34,20 @@ pub fn process(message: GlobalMessage) { let device = wgn::adapter_create_device(adapter_id, desc); wgn::HUB.devices.register(id, device); } + InstanceMessage::Terminate => return ControlFlow::Terminate, }, } + + ControlFlow::Continue +} + + +#[no_mangle] +pub extern "C" fn wgpu_server_process(server: &Server) { + while let Ok(message) = server.channel.try_recv() { + match process(message) { + ControlFlow::Continue => {}, + ControlFlow::Terminate => break, + } + } } diff --git a/wgpu-rs/Cargo.toml b/wgpu-rs/Cargo.toml index 2ffa0380f..c5046d8d5 100644 --- a/wgpu-rs/Cargo.toml +++ b/wgpu-rs/Cargo.toml @@ -16,12 +16,12 @@ license = "MPL-2.0" [features] default = [] -metal-auto-capture = ["wgpu-native/metal-auto-capture"] -metal = ["wgpu-native/gfx-backend-metal"] -dx11 = ["wgpu-native/gfx-backend-dx11"] -dx12 = ["wgpu-native/gfx-backend-dx12"] -vulkan = ["wgpu-native/gfx-backend-vulkan"] +metal-auto-capture = ["wgn/metal-auto-capture"] +metal = ["wgn/gfx-backend-metal"] +dx11 = ["wgn/gfx-backend-dx11"] +dx12 = ["wgn/gfx-backend-dx12"] +vulkan = ["wgn/gfx-backend-vulkan"] [dependencies] -wgpu-native = { version = "0.2.5", path = "../wgpu-native", features = ["local", "window-winit"] } +wgn = { package = "wgpu-native", version = "0.2.5", path = "../wgpu-native", features = ["local", "window-winit"] } arrayvec = "0.4" diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index cd97d2438..7bdb0f337 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -1,5 +1,5 @@ extern crate arrayvec; -extern crate wgpu_native as wgn; +extern crate wgn; use arrayvec::ArrayVec; @@ -839,7 +839,13 @@ impl CommandEncoder { impl<'a> RenderPass<'a> { pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[u32]) { - wgn::wgpu_render_pass_set_bind_group(self.id, index, bind_group.id, offsets); + wgn::wgpu_render_pass_set_bind_group( + self.id, + index, + bind_group.id, + offsets.as_ptr(), + offsets.len(), + ); } pub fn set_pipeline(&mut self, pipeline: &RenderPipeline) {