From 52ee3e019cc968947dee32ee908aa8d58cd05102 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 29 Apr 2019 22:41:09 -0400 Subject: [PATCH 1/4] 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 29cd641878..33e5af6a68 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 61c00448a2..93b21226f9 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 0000000000..035845275b --- /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 0000000000..631f19352e --- /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 cc7e848831..c9c509390d 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 0eafa66f5e..3efc23de83 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 0000000000..91743091b2 --- /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 9d55b1a327..b07a9194ce 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 55cf6edf04..5dc5547477 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 fe7b5b1873..f2c7c6759d 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 67e710adf9..56e1bf37c4 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 610d394273..01201f217a 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 d34c00b2e6..14ad6e7559 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 a41d187834..0b4239a8c0 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 25a99c1bf6..e596980bc0 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 f76354447c..ab15d706a1 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 63dcadbc09..5da029b3e3 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 c2c67f3494..01b72b365d 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 f3a67d993d..d505f79db8 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 2ffa0380f8..c5046d8d56 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 cd97d24388..7bdb0f337c 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) { From 0011d9f4ff3a007cf893913b9b1219bd1a421d4c Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 30 Apr 2019 09:19:40 -0400 Subject: [PATCH 2/4] Global manager for instance names on the remote client --- Cargo.lock | 1 + Makefile | 7 ++----- examples/hello_triangle_c/main.c | 4 +++- wgpu-native/Cargo.toml | 2 +- wgpu-native/src/lib.rs | 3 --- wgpu-remote/Cargo.toml | 1 + wgpu-remote/src/lib.rs | 8 +++++++- wgpu-rs/src/lib.rs | 3 --- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bd9b226d5..6ddbbffd00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1428,6 +1428,7 @@ name = "wgpu-remote" version = "0.1.0" dependencies = [ "ipc-channel 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Makefile b/Makefile index 93b21226f9..2a6ce98bf4 100644 --- a/Makefile +++ b/Makefile @@ -30,9 +30,9 @@ else endif -.PHONY: all check test doc clear lib-native lib-remote lib-rust ci-examples examples-native examples-rust examples-gfx gfx +.PHONY: all check test doc clear lib-native lib-remote lib-rust ci-examples examples-rust examples-gfx gfx -all: examples-native examples-rust examples-gfx +all: ci-examples examples-rust examples-gfx check: cargo check --all @@ -59,9 +59,6 @@ lib-rust: Cargo.lock wgpu-rs/Cargo.toml $(wildcard wgpu-rs/**/*.rs) 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) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index c9c509390d..e0c692f6c7 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -156,7 +156,7 @@ int main() { return 1; } - WGPUSurfaceId surface = {}; + WGPUSurfaceId surface; #if WGPU_TARGET == WGPU_TARGET_MACOS { @@ -183,6 +183,8 @@ int main() { surface = wgpu_instance_create_surface_from_windows_hwnd( instance, hinstance, hwnd); } +#else + #error "Unsupported WGPU_TARGET" #endif WGPUSwapChainId swap_chain = wgpu_device_create_swap_chain(device, surface, diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 7f5de81dc1..40ac4c5757 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -29,7 +29,7 @@ copyless = "0.1" lazy_static = "1.1.0" log = "0.4" parking_lot = { version = "0.7" } -gfx-hal = "0.1.0" +hal = { package = "gfx-hal", version = "0.1" } gfx-backend-empty = { version = "0.1.1" } gfx-backend-vulkan = { version = "0.1", optional = true } gfx-backend-dx11 = { version = "0.1", optional = true } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index e596980bc0..e1ef90dc76 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -17,9 +17,6 @@ extern crate gfx_backend_metal as back; #[cfg(feature = "gfx-backend-vulkan")] extern crate gfx_backend_vulkan as back; -extern crate gfx_hal as hal; -//extern crate rendy_memory; - mod binding_model; mod command; mod conv; diff --git a/wgpu-remote/Cargo.toml b/wgpu-remote/Cargo.toml index 5da029b3e3..a95e4a2200 100644 --- a/wgpu-remote/Cargo.toml +++ b/wgpu-remote/Cargo.toml @@ -20,6 +20,7 @@ metal = ["wgn/gfx-backend-metal"] [dependencies] wgn = { package = "wgpu-native", path = "../wgpu-native", features = ["remote"] } ipc-channel = "0.11" +lazy_static = "1" log = "0.4" parking_lot = { version = "0.7" } serde = { version = "1.0", features = ["serde_derive"] } diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index 01b72b365d..bb2b693d59 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -1,6 +1,7 @@ use crate::server::Server; use ipc_channel::ipc; +use lazy_static::lazy_static; use log::error; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; @@ -11,6 +12,10 @@ use std::ptr; mod server; +lazy_static! { + static ref INSTANCE_IDENTITIES: Mutex = Mutex::new(wgn::IdentityManager::default()); +} + #[derive(Serialize, Deserialize)] enum InstanceMessage { InstanceGetAdapter(wgn::InstanceId, wgn::AdapterDescriptor, wgn::AdapterId), @@ -64,7 +69,7 @@ pub struct Infrastructure { pub extern "C" fn wgpu_initialize() -> Infrastructure { match ipc::channel() { Ok((sender, receiver)) => { - let instance_id = wgn::IdentityManager::default().alloc(); // TODO: static + let instance_id = INSTANCE_IDENTITIES.lock().alloc(); let client = Client::new(sender, instance_id); let server = Server::new(receiver, instance_id); Infrastructure { @@ -90,6 +95,7 @@ pub extern "C" fn wgpu_terminate(client: *mut Client) { let client = unsafe { Box::from_raw(client) }; + INSTANCE_IDENTITIES.lock().free(client.instance_id); let _ = client.channel.send(GlobalMessage::Instance(InstanceMessage::Terminate)); } diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 7bdb0f337c..150dca9681 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -1,6 +1,3 @@ -extern crate arrayvec; -extern crate wgn; - use arrayvec::ArrayVec; use std::ffi::CString; From 61fca00f349c79af656a8a10d85134950b60553d Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 30 Apr 2019 12:19:37 -0400 Subject: [PATCH 3/4] Remove wgpu-bindings in favor of configuration scripts --- .gitignore | 1 + .travis.yml | 2 +- Cargo.lock | 100 --------------- Cargo.toml | 1 - Makefile | 18 +-- README.md | 1 - examples/hello_remote_c/main.c | 5 +- examples/hello_triangle_c/main.c | 2 +- ffi/wgpu-remote.h | 67 ++++++++++ {wgpu-bindings => ffi}/wgpu.h | 147 +++++++++++----------- wgpu-bindings/Cargo.toml | 14 --- wgpu-bindings/src/main.rs | 63 ---------- wgpu-bindings/wgpu-native.h | 196 ----------------------------- wgpu-bindings/wgpu-remote.h | 27 ---- wgpu-native/cbindgen.toml | 31 +++++ wgpu-native/src/command/compute.rs | 15 ++- wgpu-native/src/instance.rs | 5 +- wgpu-native/src/lib.rs | 20 +-- wgpu-remote/Cargo.toml | 10 +- wgpu-remote/cbindgen.toml | 25 ++++ wgpu-remote/src/lib.rs | 12 +- wgpu-rs/src/lib.rs | 8 +- 22 files changed, 245 insertions(+), 525 deletions(-) create mode 100644 ffi/wgpu-remote.h rename {wgpu-bindings => ffi}/wgpu.h (96%) delete mode 100644 wgpu-bindings/Cargo.toml delete mode 100644 wgpu-bindings/src/main.rs delete mode 100644 wgpu-bindings/wgpu-native.h delete mode 100644 wgpu-bindings/wgpu-remote.h create mode 100644 wgpu-native/cbindgen.toml create mode 100644 wgpu-remote/cbindgen.toml diff --git a/.gitignore b/.gitignore index b7894bfb62..8200d56c84 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .vscode .vs build +ffi/dawn*.h diff --git a/.travis.yml b/.travis.yml index 33e5af6a68..771708989d 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 && make ci-examples); fi + - if [[ $TRAVIS_OS_NAME == "osx" ]]; then (brew update && brew upgrade cmake && brew install glfw3 && make ffi-examples); fi diff --git a/Cargo.lock b/Cargo.lock index 6ddbbffd00..b7ff4d4cc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,14 +26,6 @@ name = "android_glue" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "approx" version = "0.3.1" @@ -141,21 +133,6 @@ name = "byteorder" version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "cbindgen" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "cc" version = "1.0.30" @@ -176,20 +153,6 @@ dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "clap" -version = "2.32.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "cloudabi" version = "0.0.3" @@ -560,11 +523,6 @@ dependencies = [ "uuid 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "itoa" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "kernel32-sys" version = "0.2.2" @@ -1054,11 +1012,6 @@ dependencies = [ "stb_truetype 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ryu" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "same-file" version = "1.0.4" @@ -1103,16 +1056,6 @@ dependencies = [ "syn 0.13.11 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "serde_json" -version = "1.0.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", - "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "sha2" version = "0.7.1" @@ -1189,11 +1132,6 @@ dependencies = [ "lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "strsim" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "syn" version = "0.13.11" @@ -1256,14 +1194,6 @@ dependencies = [ "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "textwrap" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "thread_local" version = "0.3.6" @@ -1272,14 +1202,6 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "toml" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "typenum" version = "1.10.0" @@ -1290,11 +1212,6 @@ name = "ucd-util" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicode-width" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-xid" version = "0.1.0" @@ -1395,13 +1312,6 @@ dependencies = [ "wgpu-native 0.2.6", ] -[[package]] -name = "wgpu-bindings" -version = "0.1.0" -dependencies = [ - "cbindgen 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "wgpu-native" version = "0.2.6" @@ -1562,7 +1472,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" "checksum andrew 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "142e9e6a99ad0d63a4cf6ce58a4c979f472c5815cbf7e5ca4e47b26a10dc728e" "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" -"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum approx 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3c57ff1a5b00753647aebbbcf4ea67fa1e711a65ea7a30eb90dbf07de2485aee" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" @@ -1577,11 +1486,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" -"checksum cbindgen 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "32e01024aaf5390d6a8145047371a4f5b0063a14c1e411bc731353bd2278ca44" "checksum cc 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)" = "d01c69d08ff207f231f07196e30f84c70f1c815b04f980f8b7b01ff01f05eb92" "checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4" "checksum cgmath 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "283944cdecc44bf0b8dd010ec9af888d3b4f142844fdbe026c20ef68148d6fe7" -"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cmake 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "6ec65ee4f9c9d16f335091d23693457ed4928657ba4982289d7fafee03bc614a" "checksum cocoa 0.18.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cf79daa4e11e5def06e55306aa3601b87de6b5149671529318da048f67cdd77b" @@ -1619,7 +1526,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum ipc-channel 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f82db24b0c53ee2d54b420bb9258f2b787611fe3e7a28d514b5ea54fe65cd365" -"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" @@ -1677,14 +1583,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusttype 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ce3926a2057b315b3e8bca6d1cec1e97f19436a8f9127621cd538cda9c96a38b" -"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560" "checksum serde_derive 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)" = "ac38f51a52a556cd17545798e29536885fb1a3fa63d6399f5ef650f4a7d35901" -"checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" @@ -1694,19 +1598,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum stb_truetype 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "69b7df505db8e81d54ff8be4693421e5b543e08214bd8d99eb761fcb4d5668ba" "checksum storage-map 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cb94f167ccba0941876c8e722e888be8b4c05511ffdacc8cfcd4c647adfd424d" -"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.13.11 (registry+https://github.com/rust-lang/crates.io-index)" = "14f9bf6292f3a61d2c716723fdb789a41bbe104168e6f496dc6497e531ea1b9b" "checksum syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)" = "525bd55255f03c816e5d7f615587bd13030c7103354fadb104993dcee6a788ec" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" -"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" -"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" -"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum uuid 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0238db0c5b605dd1cf51de0f21766f97fba2645897024461d6a00c036819a768" diff --git a/Cargo.toml b/Cargo.toml index 16fdbeca9f..e3a0ffa1bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] members = [ "wgpu-native", - "wgpu-bindings", "wgpu-remote", "wgpu-rs", "examples", diff --git a/Makefile b/Makefile index 2a6ce98bf4..ebefe95b55 100644 --- a/Makefile +++ b/Makefile @@ -30,9 +30,9 @@ else endif -.PHONY: all check test doc clear lib-native lib-remote lib-rust ci-examples examples-rust examples-gfx gfx +.PHONY: all check test doc clear lib-native lib-remote lib-rust examples-rust examples-gfx gfx ffi-examples -all: ci-examples examples-rust examples-gfx +all: examples-rust examples-gfx ffi-examples check: cargo check --all @@ -56,19 +56,23 @@ lib-remote: Cargo.lock wgpu-remote/Cargo.toml $(wildcard wgpu-native/**/*.rs wgp lib-rust: Cargo.lock wgpu-rs/Cargo.toml $(wildcard wgpu-rs/**/*.rs) cargo build --manifest-path wgpu-rs/Cargo.toml --features $(FEATURE_RUST) +ffi/wgpu.h: wgpu-native/cbindgen.toml $(wildcard wgpu-native/**/*.rs) + cbindgen wgpu-native >ffi/wgpu.h + +ffi/wgpu-remote.h: wgpu-remote/cbindgen.toml $(wildcard wgpu-native/**/*.rs wgpu-remote/**/*.rs) + cbindgen wgpu-remote >ffi/wgpu-remote.h + wgpu-bindings/*.h: Cargo.lock $(wildcard wgpu-bindings/src/*.rs) lib-native lib-remote cargo +nightly run --manifest-path wgpu-bindings/Cargo.toml -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) +ffi-examples: lib-native lib-remote ffi/wgpu.h ffi/wgpu-remote.h 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) +examples-rust: examples/Cargo.toml $(wildcard wgpu-native/**/*.rs wgpu-rs/**/*.rs) cargo build --manifest-path examples/Cargo.toml --features $(FEATURE_RUST) -examples-gfx: lib-rust gfx-examples/Cargo.toml $(wildcard gfx-examples/*.rs) +examples-gfx: examples-rust gfx-examples/Cargo.toml $(wildcard gfx-examples/*.rs) cargo build --manifest-path gfx-examples/Cargo.toml --features $(FEATURE_RUST) gfx: diff --git a/README.md b/README.md index ad7576ca2e..27cd0e5099 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ This is an experimental [WebGPU](https://www.w3.org/community/gpu/) implementati The implementation consists of the following parts: - `wgpu-native` - the native implementation of WebGPU as a C API library - - `wgpu-bindings` - automatic generator of actual C headers - `wgpu-remote` - remoting layer to work with WebGPU across the process boundary - `wgpu-rs` - idiomatic Rust wrapper of the native library diff --git a/examples/hello_remote_c/main.c b/examples/hello_remote_c/main.c index 631f19352e..f0e4a61ad9 100644 --- a/examples/hello_remote_c/main.c +++ b/examples/hello_remote_c/main.c @@ -1,5 +1,4 @@ -#include "./../../wgpu-bindings/wgpu-native.h" -#include "./../../wgpu-bindings/wgpu-remote.h" +#include "./../../ffi/wgpu-remote.h" #include int main() { @@ -12,7 +11,7 @@ int main() { //TODO: do something meaningful - wgpu_terminate(infra.client); + wgpu_client_terminate(infra.client); return 0; } diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index e0c692f6c7..a83883d232 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -1,4 +1,4 @@ -#include "./../../wgpu-bindings/wgpu.h" +#include "./../../ffi/wgpu.h" #include #define WGPU_TARGET_MACOS 1 diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h new file mode 100644 index 0000000000..408d95ebef --- /dev/null +++ b/ffi/wgpu-remote.h @@ -0,0 +1,67 @@ + + +/* Generated with cbindgen:0.8.3 */ + +#include +#include +#include +#include + +typedef enum { + WGPUPowerPreference_Default = 0, + WGPUPowerPreference_LowPower = 1, + WGPUPowerPreference_HighPerformance = 2, +} WGPUPowerPreference; + +typedef struct WGPUClient WGPUClient; + +typedef struct WGPUServer WGPUServer; + +typedef struct WGPUTrackPermit WGPUTrackPermit; + +typedef uint32_t WGPUIndex; + +typedef uint32_t WGPUEpoch; + +typedef struct { + WGPUIndex _0; + WGPUEpoch _1; +} WGPUId; + +typedef WGPUId WGPUDeviceId; + +typedef WGPUId WGPUAdapterId; + +typedef struct { + bool anisotropic_filtering; +} WGPUExtensions; + +typedef struct { + WGPUExtensions extensions; +} WGPUDeviceDescriptor; + +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + +typedef struct { + WGPUClient *client; + WGPUServer *server; + const uint8_t *error; +} WGPUInfrastructure; + + + + + +WGPUDeviceId wgpu_client_adapter_create_device(const WGPUClient *client, + WGPUAdapterId adapter_id, + const WGPUDeviceDescriptor *desc); + +WGPUAdapterId wgpu_client_get_adapter(const WGPUClient *client, const WGPUAdapterDescriptor *desc); + +void wgpu_client_terminate(WGPUClient *client); + +WGPUInfrastructure wgpu_initialize(void); + +void wgpu_server_process(const WGPUServer *server); diff --git a/wgpu-bindings/wgpu.h b/ffi/wgpu.h similarity index 96% rename from wgpu-bindings/wgpu.h rename to ffi/wgpu.h index 5dc5547477..f88466a86a 100644 --- a/wgpu-bindings/wgpu.h +++ b/ffi/wgpu.h @@ -1,10 +1,12 @@ + + +/* Generated with cbindgen:0.8.3 */ + #include #include #include #include -#define WGPUBITS_PER_BYTE 8 - #define WGPUMAX_BIND_GROUPS 4 #define WGPUMAX_COLOR_TARGETS 4 @@ -251,6 +253,8 @@ typedef enum { WGPUVertexFormat_Int4 = 48, } WGPUVertexFormat; +typedef struct WGPUTrackPermit WGPUTrackPermit; + typedef uint32_t WGPUIndex; typedef uint32_t WGPUEpoch; @@ -260,10 +264,12 @@ typedef struct { WGPUEpoch _1; } WGPUId; -typedef WGPUId WGPUDeviceId; - typedef WGPUId WGPUAdapterId; +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + typedef struct { bool anisotropic_filtering; } WGPUExtensions; @@ -272,6 +278,16 @@ typedef struct { WGPUExtensions extensions; } WGPUDeviceDescriptor; +#if defined(WGPU_REMOTE) +typedef struct { + WGPUAdapterId adapter; + WGPUAdapterDescriptor adapter_desc; + WGPUDeviceDescriptor device_desc; +} WGPUForcedExports; +#endif + +typedef WGPUId WGPUDeviceId; + typedef WGPUId WGPUBindGroupId; typedef WGPUId WGPUBufferId; @@ -324,6 +340,12 @@ typedef struct { float b; float a; } WGPUColor; +#define WGPUColor_TRANSPARENT (WGPUColor){ .r = 0, .g = 0, .b = 0, .a = 0 } +#define WGPUColor_BLACK (WGPUColor){ .r = 0, .g = 0, .b = 0, .a = 1 } +#define WGPUColor_WHITE (WGPUColor){ .r = 1, .g = 1, .b = 1, .a = 1 } +#define WGPUColor_RED (WGPUColor){ .r = 1, .g = 0, .b = 0, .a = 1 } +#define WGPUColor_GREEN (WGPUColor){ .r = 0, .g = 1, .b = 0, .a = 1 } +#define WGPUColor_BLUE (WGPUColor){ .r = 0, .g = 0, .b = 1, .a = 1 } typedef struct { WGPUTextureViewId attachment; @@ -401,6 +423,9 @@ typedef struct { } WGPUBindGroupDescriptor; typedef uint32_t WGPUShaderStageFlags; +#define WGPUShaderStageFlags_VERTEX 1 +#define WGPUShaderStageFlags_FRAGMENT 2 +#define WGPUShaderStageFlags_COMPUTE 4 typedef struct { uint32_t binding; @@ -414,6 +439,16 @@ typedef struct { } WGPUBindGroupLayoutDescriptor; typedef uint32_t WGPUBufferUsageFlags; +#define WGPUBufferUsageFlags_MAP_READ 1 +#define WGPUBufferUsageFlags_MAP_WRITE 2 +#define WGPUBufferUsageFlags_TRANSFER_SRC 4 +#define WGPUBufferUsageFlags_TRANSFER_DST 8 +#define WGPUBufferUsageFlags_INDEX 16 +#define WGPUBufferUsageFlags_VERTEX 32 +#define WGPUBufferUsageFlags_UNIFORM 64 +#define WGPUBufferUsageFlags_STORAGE 128 +#define WGPUBufferUsageFlags_NONE 0 +#define WGPUBufferUsageFlags_WRITE_ALL 2 + 8 + 128 typedef struct { uint32_t size; @@ -460,6 +495,12 @@ typedef struct { } WGPUBlendDescriptor; typedef uint32_t WGPUColorWriteFlags; +#define WGPUColorWriteFlags_RED 1 +#define WGPUColorWriteFlags_GREEN 2 +#define WGPUColorWriteFlags_BLUE 4 +#define WGPUColorWriteFlags_ALPHA 8 +#define WGPUColorWriteFlags_COLOR 7 +#define WGPUColorWriteFlags_ALL 15 typedef struct { WGPUTextureFormat format; @@ -547,6 +588,14 @@ typedef WGPUId WGPUSurfaceId; typedef WGPUSurfaceId WGPUSwapChainId; typedef uint32_t WGPUTextureUsageFlags; +#define WGPUTextureUsageFlags_TRANSFER_SRC 1 +#define WGPUTextureUsageFlags_TRANSFER_DST 2 +#define WGPUTextureUsageFlags_SAMPLED 4 +#define WGPUTextureUsageFlags_STORAGE 8 +#define WGPUTextureUsageFlags_OUTPUT_ATTACHMENT 16 +#define WGPUTextureUsageFlags_NONE 0 +#define WGPUTextureUsageFlags_WRITE_ALL 2 + 8 + 16 +#define WGPUTextureUsageFlags_UNINITIALIZED 65535 typedef struct { WGPUTextureUsageFlags usage; @@ -565,16 +614,15 @@ typedef struct { typedef WGPUDeviceId WGPUQueueId; -typedef struct { - WGPUPowerPreference power_preference; -} WGPUAdapterDescriptor; - typedef struct { WGPUTextureId texture_id; WGPUTextureViewId view_id; } WGPUSwapChainOutput; typedef uint32_t WGPUTextureAspectFlags; +#define WGPUTextureAspectFlags_COLOR 1 +#define WGPUTextureAspectFlags_DEPTH 2 +#define WGPUTextureAspectFlags_STENCIL 4 typedef struct { WGPUTextureFormat format; @@ -586,79 +634,13 @@ typedef struct { uint32_t array_count; } WGPUTextureViewDescriptor; -#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 } +#if defined(WGPU_REMOTE) +void forced_exports(WGPUForcedExports fe); +#endif WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); @@ -718,6 +700,12 @@ 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, + const uint32_t *offsets_ptr, + uintptr_t offsets_count); + void wgpu_compute_pass_set_pipeline(WGPUComputePassId pass_id, WGPUComputePipelineId pipeline_id); WGPUInstanceId wgpu_create_instance(void); @@ -770,6 +758,11 @@ WGPUSurfaceId wgpu_instance_create_surface_from_windows_hwnd(WGPUInstanceId inst void *hinstance, void *hwnd); +#if defined(WGPU_WINDOW_WINIT) +WGPUSurfaceId wgpu_instance_create_surface_from_winit(WGPUInstanceId instance_id, + const WGPUWindow *window); +#endif + WGPUSurfaceId wgpu_instance_create_surface_from_xlib(WGPUInstanceId instance_id, const void **display, uint64_t window); diff --git a/wgpu-bindings/Cargo.toml b/wgpu-bindings/Cargo.toml deleted file mode 100644 index 5056e4f524..0000000000 --- a/wgpu-bindings/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "wgpu-bindings" -version = "0.1.0" -authors = [ - "Dzmitry Malyshau ", - "Joshua Groves ", -] -edition = "2018" - -[features] -default = [] - -[dependencies] -cbindgen = "0.7.1" diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs deleted file mode 100644 index 3efc23de83..0000000000 --- a/wgpu-bindings/src/main.rs +++ /dev/null @@ -1,63 +0,0 @@ -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; 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], - }, -]; - -fn main() { - let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let parent = crate_dir.parent().unwrap(); - - for bind in &BINDINGS { - let config = cbindgen::Config { - enumeration: cbindgen::EnumConfig { - prefix_with_name: true, - ..Default::default() - }, - export: cbindgen::ExportConfig { - prefix: Some(String::from("WGPU")), - item_types: bind.item_types.to_vec(), - ..Default::default() - }, - language: cbindgen::Language::C, - ..Default::default() - }; - - println!("Generating {}...", bind.output); - cbindgen::Builder::new() - .with_crate(parent.join(bind.library)) - .with_config(config) - .with_parse_expand(&[bind.library]) - .with_parse_expand_features(bind.features) - .generate() - .unwrap() - .write_to_file(crate_dir.join(bind.output)); - } -} diff --git a/wgpu-bindings/wgpu-native.h b/wgpu-bindings/wgpu-native.h deleted file mode 100644 index 91743091b2..0000000000 --- a/wgpu-bindings/wgpu-native.h +++ /dev/null @@ -1,196 +0,0 @@ -#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 deleted file mode 100644 index b07a9194ce..0000000000 --- a/wgpu-bindings/wgpu-remote.h +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include -#include - -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, - const WGPUAdapterDescriptor *desc); - -void wgpu_server_process(const WGPUServer *server); - -void wgpu_terminate(WGPUClient *client); diff --git a/wgpu-native/cbindgen.toml b/wgpu-native/cbindgen.toml new file mode 100644 index 0000000000..bb01f70453 --- /dev/null +++ b/wgpu-native/cbindgen.toml @@ -0,0 +1,31 @@ +header = "" +include_version = true +braces = "SameLine" +line_length = 100 +tab_width = 2 +language = "C" + +[export] +prefix = "WGPU" + +[parse] +parse_deps = false + +[parse.expand] +features = ["local"] + +[fn] + +[struct] +derive_eq = true + +[enum] +prefix_with_name = true +derive_helper_methods = true + +[macro_expansion] +bitflags = true + +[defines] +"feature = window-winit" = "WGPU_WINDOW_WINIT" +"feature = remote" = "WGPU_REMOTE" diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 6ed464b784..032b4d2c8e 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -12,7 +12,8 @@ use crate::{ use hal::{self, command::RawCommandBuffer}; -use std::iter; +use std::{iter, slice}; + pub struct ComputePass { raw: B::CommandBuffer, @@ -56,13 +57,23 @@ pub extern "C" fn wgpu_compute_pass_set_bind_group( pass_id: ComputePassId, index: u32, bind_group_id: BindGroupId, - offsets: &[u32], + offsets_ptr: *const u32, + offsets_count: usize, ) { let mut pass_guard = HUB.compute_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 { + &[] + }; + //Note: currently, WebGPU compute passes have synchronization defined // at a dispatch granularity, so we insert the necessary barriers here. diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 0b4239a8c0..752a5e6fa9 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -43,14 +43,15 @@ pub fn create_instance() -> ::back::Instance { ::back::Instance::create("wgpu", 1) } -#[no_mangle] #[cfg(feature = "local")] +#[no_mangle] pub extern "C" fn wgpu_create_instance() -> InstanceId { let inst = create_instance(); HUB.instances.register_local(inst) } -#[cfg(all(feature = "local", feature = "window-winit"))] + +#[cfg(feature = "window-winit")] #[no_mangle] pub extern "C" fn wgpu_instance_create_surface_from_winit( instance_id: InstanceId, diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index e1ef90dc76..5b64d54946 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -41,27 +41,9 @@ pub use self::swap_chain::*; use std::ptr; 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-remote/Cargo.toml b/wgpu-remote/Cargo.toml index a95e4a2200..c2bef8fef3 100644 --- a/wgpu-remote/Cargo.toml +++ b/wgpu-remote/Cargo.toml @@ -12,13 +12,13 @@ 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"] +vulkan = ["wgpu-native/gfx-backend-vulkan"] +dx11 = ["wgpu-native/gfx-backend-dx11"] +dx12 = ["wgpu-native/gfx-backend-dx12"] +metal = ["wgpu-native/gfx-backend-metal"] [dependencies] -wgn = { package = "wgpu-native", path = "../wgpu-native", features = ["remote"] } +wgpu-native = { path = "../wgpu-native", version = "0.2", features = ["remote"] } ipc-channel = "0.11" lazy_static = "1" log = "0.4" diff --git a/wgpu-remote/cbindgen.toml b/wgpu-remote/cbindgen.toml new file mode 100644 index 0000000000..c0c50b493c --- /dev/null +++ b/wgpu-remote/cbindgen.toml @@ -0,0 +1,25 @@ +header = "" +include_version = true +braces = "SameLine" +line_length = 100 +tab_width = 2 +language = "C" + +[export] +prefix = "WGPU" + +[parse] +parse_deps = true +include = ["wgpu_native"] + +[fn] + +[struct] +derive_eq = true + +[enum] +prefix_with_name = true +derive_helper_methods = true + +[macro_expansion] +bitflags = true diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index bb2b693d59..c4e4daa1c1 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -1,3 +1,6 @@ +//TODO: remove once `cbindgen` is smart enough +extern crate wgpu_native as wgn; + use crate::server::Server; use ipc_channel::ipc; @@ -5,7 +8,6 @@ use lazy_static::lazy_static; use log::error; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; -use wgn; use std::ptr; @@ -83,7 +85,7 @@ pub extern "C" fn wgpu_initialize() -> Infrastructure { Infrastructure { client: ptr::null_mut(), server: ptr::null_mut(), - error: ptr::null(), //TODO + error: ptr::null(), //TODO_remote_ } } } @@ -91,7 +93,7 @@ pub extern "C" fn wgpu_initialize() -> Infrastructure { } #[no_mangle] -pub extern "C" fn wgpu_terminate(client: *mut Client) { +pub extern "C" fn wgpu_client_terminate(client: *mut Client) { let client = unsafe { Box::from_raw(client) }; @@ -100,7 +102,7 @@ pub extern "C" fn wgpu_terminate(client: *mut Client) { } #[no_mangle] -pub extern "C" fn wgpu_instance_get_adapter( +pub extern "C" fn wgpu_client_get_adapter( client: &Client, desc: &wgn::AdapterDescriptor, ) -> wgn::AdapterId { @@ -115,7 +117,7 @@ pub extern "C" fn wgpu_instance_get_adapter( } #[no_mangle] -pub extern "C" fn wgpu_adapter_create_device( +pub extern "C" fn wgpu_client_adapter_create_device( client: &Client, adapter_id: wgn::AdapterId, desc: &wgn::DeviceDescriptor, diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 150dca9681..dd5d480e74 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -906,7 +906,13 @@ impl<'a> Drop for RenderPass<'a> { impl<'a> ComputePass<'a> { pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[u32]) { - wgn::wgpu_compute_pass_set_bind_group(self.id, index, bind_group.id, offsets); + wgn::wgpu_compute_pass_set_bind_group( + self.id, + index, + bind_group.id, + offsets.as_ptr(), + offsets.len(), + ); } pub fn set_pipeline(&mut self, pipeline: &ComputePipeline) { From 6ac666a5aa3bb2df7852540b034f111605eaf996 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 30 Apr 2019 15:37:36 -0400 Subject: [PATCH 4/4] Introduce ClientFactory --- Cargo.lock | 1 - examples/hello_remote_c/main.c | 7 ++-- ffi/wgpu-remote.h | 12 +++++-- ffi/wgpu.h | 24 ++++--------- wgpu-native/src/conv.rs | 2 ++ wgpu-native/src/resource.rs | 1 + wgpu-remote/Cargo.toml | 1 - wgpu-remote/src/lib.rs | 66 ++++++++++++++++++++-------------- wgpu-remote/src/server.rs | 13 ++++--- 9 files changed, 71 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b7ff4d4cc3..08efbdbcf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1338,7 +1338,6 @@ name = "wgpu-remote" version = "0.1.0" dependencies = [ "ipc-channel 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/examples/hello_remote_c/main.c b/examples/hello_remote_c/main.c index f0e4a61ad9..5819615512 100644 --- a/examples/hello_remote_c/main.c +++ b/examples/hello_remote_c/main.c @@ -4,14 +4,17 @@ int main() { WGPUInfrastructure infra = wgpu_initialize(); - if (!infra.client || !infra.server || infra.error) { + if (!infra.factory || !infra.server || infra.error) { printf("Cannot initialize WGPU: %s", infra.error); return 1; } + WGPUClient* client = wgpu_client_create(infra.factory); + //TODO: do something meaningful - wgpu_client_terminate(infra.client); + wgpu_client_destroy(infra.factory, client); + wgpu_terminate(infra.factory); return 0; } diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h index 408d95ebef..fe38e949e0 100644 --- a/ffi/wgpu-remote.h +++ b/ffi/wgpu-remote.h @@ -15,6 +15,8 @@ typedef enum { typedef struct WGPUClient WGPUClient; +typedef struct WGPUClientFactory WGPUClientFactory; + typedef struct WGPUServer WGPUServer; typedef struct WGPUTrackPermit WGPUTrackPermit; @@ -45,7 +47,7 @@ typedef struct { } WGPUAdapterDescriptor; typedef struct { - WGPUClient *client; + WGPUClientFactory *factory; WGPUServer *server; const uint8_t *error; } WGPUInfrastructure; @@ -58,10 +60,14 @@ WGPUDeviceId wgpu_client_adapter_create_device(const WGPUClient *client, WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); -WGPUAdapterId wgpu_client_get_adapter(const WGPUClient *client, const WGPUAdapterDescriptor *desc); +WGPUClient *wgpu_client_create(const WGPUClientFactory *factory); -void wgpu_client_terminate(WGPUClient *client); +void wgpu_client_destroy(const WGPUClientFactory *factory, WGPUClient *client); + +WGPUAdapterId wgpu_client_get_adapter(const WGPUClient *client, const WGPUAdapterDescriptor *desc); WGPUInfrastructure wgpu_initialize(void); void wgpu_server_process(const WGPUServer *server); + +void wgpu_terminate(WGPUClientFactory *factory); diff --git a/ffi/wgpu.h b/ffi/wgpu.h index f88466a86a..b37a23f2d8 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -264,11 +264,9 @@ typedef struct { WGPUEpoch _1; } WGPUId; -typedef WGPUId WGPUAdapterId; +typedef WGPUId WGPUDeviceId; -typedef struct { - WGPUPowerPreference power_preference; -} WGPUAdapterDescriptor; +typedef WGPUId WGPUAdapterId; typedef struct { bool anisotropic_filtering; @@ -278,16 +276,6 @@ typedef struct { WGPUExtensions extensions; } WGPUDeviceDescriptor; -#if defined(WGPU_REMOTE) -typedef struct { - WGPUAdapterId adapter; - WGPUAdapterDescriptor adapter_desc; - WGPUDeviceDescriptor device_desc; -} WGPUForcedExports; -#endif - -typedef WGPUId WGPUDeviceId; - typedef WGPUId WGPUBindGroupId; typedef WGPUId WGPUBufferId; @@ -614,6 +602,10 @@ typedef struct { typedef WGPUDeviceId WGPUQueueId; +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + typedef struct { WGPUTextureId texture_id; WGPUTextureViewId view_id; @@ -638,10 +630,6 @@ typedef struct { -#if defined(WGPU_REMOTE) -void forced_exports(WGPUForcedExports fe); -#endif - WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); void wgpu_bind_group_destroy(WGPUBindGroupId bind_group_id); diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index b74144cd8e..3451c0042d 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -452,6 +452,7 @@ pub fn map_texture_dimension_size( } } +#[cfg(feature = "local")] pub fn map_texture_view_dimension( dimension: resource::TextureViewDimension, ) -> hal::image::ViewKind { @@ -467,6 +468,7 @@ pub fn map_texture_view_dimension( } } +#[cfg(feature = "local")] pub fn map_texture_aspect_flags(aspect: resource::TextureAspectFlags) -> hal::format::Aspects { use crate::resource::TextureAspectFlags as Taf; use hal::format::Aspects; diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index ab15d706a1..046007806e 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -173,6 +173,7 @@ pub struct TextureDescriptor { } pub(crate) enum TexturePlacement { + #[cfg_attr(feature = "remote", allow(unused))] SwapChain(SwapChainLink>), Memory(B::Memory), Void, diff --git a/wgpu-remote/Cargo.toml b/wgpu-remote/Cargo.toml index c2bef8fef3..1ff445b502 100644 --- a/wgpu-remote/Cargo.toml +++ b/wgpu-remote/Cargo.toml @@ -20,7 +20,6 @@ metal = ["wgpu-native/gfx-backend-metal"] [dependencies] wgpu-native = { path = "../wgpu-native", version = "0.2", features = ["remote"] } ipc-channel = "0.11" -lazy_static = "1" log = "0.4" parking_lot = { version = "0.7" } serde = { version = "1.0", features = ["serde_derive"] } diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index c4e4daa1c1..5aca466158 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -4,7 +4,6 @@ extern crate wgpu_native as wgn; use crate::server::Server; use ipc_channel::ipc; -use lazy_static::lazy_static; use log::error; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; @@ -14,15 +13,12 @@ use std::ptr; mod server; -lazy_static! { - static ref INSTANCE_IDENTITIES: Mutex = Mutex::new(wgn::IdentityManager::default()); -} - #[derive(Serialize, Deserialize)] enum InstanceMessage { + Create(wgn::InstanceId), InstanceGetAdapter(wgn::InstanceId, wgn::AdapterDescriptor, wgn::AdapterId), AdapterCreateDevice(wgn::AdapterId, wgn::DeviceDescriptor, wgn::DeviceId), - Terminate, + Destroy(wgn::InstanceId), } /// A message on the timeline of devices, queues, and resources. @@ -33,6 +29,7 @@ enum GlobalMessage { //Queue(QueueMessage), //Texture(TextureMessage), //Command(CommandMessage), + Terminate, } #[derive(Default)] @@ -47,22 +44,14 @@ pub struct Client { identity: Mutex, } -impl Client { - fn new( - channel: ipc::IpcSender, - instance_id: wgn::InstanceId, - ) -> Self { - Client { - channel, - instance_id, - identity: Mutex::new(IdentityHub::default()), - } - } +pub struct ClientFactory { + channel: ipc::IpcSender, + instance_identities: Mutex, } #[repr(C)] pub struct Infrastructure { - pub client: *mut Client, + pub factory: *mut ClientFactory, pub server: *mut Server, pub error: *const u8, } @@ -71,11 +60,13 @@ pub struct Infrastructure { pub extern "C" fn wgpu_initialize() -> Infrastructure { match ipc::channel() { Ok((sender, receiver)) => { - let instance_id = INSTANCE_IDENTITIES.lock().alloc(); - let client = Client::new(sender, instance_id); - let server = Server::new(receiver, instance_id); + let factory = ClientFactory { + channel: sender, + instance_identities: Mutex::new(wgn::IdentityManager::default()), + }; + let server = Server::new(receiver); Infrastructure { - client: Box::into_raw(Box::new(client)), + factory: Box::into_raw(Box::new(factory)), server: Box::into_raw(Box::new(server)), error: ptr::null(), } @@ -83,22 +74,43 @@ pub extern "C" fn wgpu_initialize() -> Infrastructure { Err(e) => { error!("WGPU initialize failed: {:?}", e); Infrastructure { - client: ptr::null_mut(), + factory: ptr::null_mut(), server: ptr::null_mut(), error: ptr::null(), //TODO_remote_ } } } - } #[no_mangle] -pub extern "C" fn wgpu_client_terminate(client: *mut Client) { +pub extern "C" fn wgpu_terminate(factory: *mut ClientFactory) { + let factory = unsafe { + Box::from_raw(factory) + }; + let _ = factory.channel.send(GlobalMessage::Terminate); +} + +#[no_mangle] +pub extern "C" fn wgpu_client_create(factory: &ClientFactory) -> *mut Client { + let instance_id = factory.instance_identities.lock().alloc(); + let msg = GlobalMessage::Instance(InstanceMessage::Create(instance_id)); + factory.channel.send(msg).unwrap(); + let client = Client { + channel: factory.channel.clone(), + instance_id, + identity: Mutex::new(IdentityHub::default()), + }; + Box::into_raw(Box::new(client)) +} + +#[no_mangle] +pub extern "C" fn wgpu_client_destroy(factory: &ClientFactory, client: *mut Client) { let client = unsafe { Box::from_raw(client) }; - INSTANCE_IDENTITIES.lock().free(client.instance_id); - let _ = client.channel.send(GlobalMessage::Instance(InstanceMessage::Terminate)); + factory.instance_identities.lock().free(client.instance_id); + let msg = GlobalMessage::Instance(InstanceMessage::Destroy(client.instance_id)); + client.channel.send(msg).unwrap(); } #[no_mangle] diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index d505f79db8..96a359a6df 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -9,9 +9,7 @@ pub struct Server { } impl Server { - pub(crate) fn new(channel: IpcReceiver, instance_id: wgn::InstanceId) -> Self { - let instance = wgn::create_instance(); - wgn::HUB.instances.register(instance_id, instance); + pub(crate) fn new(channel: IpcReceiver) -> Self { Server { channel, } @@ -26,6 +24,10 @@ enum ControlFlow { fn process(message: GlobalMessage) -> ControlFlow { match message { GlobalMessage::Instance(msg) => match msg { + InstanceMessage::Create(instance_id) => { + let instance = wgn::create_instance(); + wgn::HUB.instances.register(instance_id, instance); + } InstanceMessage::InstanceGetAdapter(instance_id, ref desc, id) => { let adapter = wgn::instance_get_adapter(instance_id, desc); wgn::HUB.adapters.register(id, adapter); @@ -34,8 +36,11 @@ fn process(message: GlobalMessage) -> ControlFlow { let device = wgn::adapter_create_device(adapter_id, desc); wgn::HUB.devices.register(id, device); } - InstanceMessage::Terminate => return ControlFlow::Terminate, + InstanceMessage::Destroy(instance_id) => { + wgn::HUB.instances.unregister(instance_id); + } }, + GlobalMessage::Terminate => return ControlFlow::Terminate, } ControlFlow::Continue