diff --git a/Cargo.lock b/Cargo.lock index 0b0f37f3..63446497 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,15 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c86699c3f02778ec07158376991c8f783dd1f2f95c579ffaf0738dc984b2fe2" +[[package]] +name = "base64" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" +dependencies = [ + "byteorder", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -482,12 +491,43 @@ version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +[[package]] +name = "ron" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ece421e0c4129b90e4a35b6f625e472e96c552136f5093a2f4fa2bbb75a62d5" +dependencies = [ + "base64", + "bitflags", + "serde", +] + [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "serde" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "slab" version = "0.4.2" @@ -612,7 +652,7 @@ checksum = "daf76fe7d25ac79748a37538b7daeed1c7a6867c92d3245c12c6222e4a20d639" [[package]] name = "wgpu-core" version = "0.5.0" -source = "git+https://github.com/gfx-rs/wgpu#d45323505c63dc9b5b61c720948f4b20fb0e8a66" +source = "git+https://github.com/gfx-rs/wgpu#e2100b6911168bf10d8543380681ebb4ca33b9b1" dependencies = [ "arrayvec", "bitflags", @@ -629,6 +669,9 @@ dependencies = [ "log", "parking_lot", "peek-poke", + "raw-window-handle", + "ron", + "serde", "smallvec", "vec_map", "wgpu-types", @@ -642,7 +685,6 @@ dependencies = [ "lazy_static", "libc", "log", - "objc", "parking_lot", "raw-window-handle", "wgpu-core", @@ -652,10 +694,11 @@ dependencies = [ [[package]] name = "wgpu-types" version = "0.5.0" -source = "git+https://github.com/gfx-rs/wgpu#d45323505c63dc9b5b61c720948f4b20fb0e8a66" +source = "git+https://github.com/gfx-rs/wgpu#e2100b6911168bf10d8543380681ebb4ca33b9b1" dependencies = [ "bitflags", "peek-poke", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4db97613..68a4b61d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,17 +18,16 @@ crate-type = ["lib", "cdylib", "staticlib"] [features] default = [] #metal-auto-capture = ["gfx-backend-metal/auto-capture"] -vulkan-portability = ["core/gfx-backend-vulkan"] +vulkan-portability = ["wgc/gfx-backend-vulkan"] -[dependencies.core] +[dependencies.wgc] package = "wgpu-core" -#path = "../wgpu-core" git = "https://github.com/gfx-rs/wgpu" version = "0.5" +features = ["raw-window-handle", "trace"] [dependencies.wgt] package = "wgpu-types" -#path = "../wgpu-types" git = "https://github.com/gfx-rs/wgpu" version = "0.5" @@ -39,6 +38,3 @@ lazy_static = "1.1" parking_lot = "0.10" raw-window-handle = "0.3" libc = {version="0.2", features=[]} - -[target.'cfg(target_os = "macos")'.dependencies] -objc = "0.2.7" diff --git a/cbindgen.toml b/cbindgen.toml index c7c02fb0..c084e969 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -20,6 +20,12 @@ language = "C" prefix = "WGPU" exclude = ["Option_AdapterId", "Option_SurfaceId", "Option_TextureViewId"] +[export.rename] +"BufferDescriptor_Label" = "BufferDescriptor" +"TextureDescriptor_Label" = "TextureDescriptor" +"TextureViewDescriptor_Label" = "TextureViewDescriptor" +"SamplerDescriptor_Label" = "SamplerDescriptor" + [parse] parse_deps = true include = ["wgpu-core", "wgpu-types"] diff --git a/ffi/wgpu.h b/ffi/wgpu.h index d02890bd..92e92f3e 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -521,6 +521,8 @@ typedef struct { uintptr_t entries_length; } WGPUBindGroupLayoutDescriptor; +typedef const char *WGPULabel; + typedef uint32_t WGPUBufferUsage; #define WGPUBufferUsage_MAP_READ 1 #define WGPUBufferUsage_MAP_WRITE 2 @@ -533,7 +535,7 @@ typedef uint32_t WGPUBufferUsage; #define WGPUBufferUsage_INDIRECT 256 typedef struct { - const char *label; + WGPULabel label; WGPUBufferAddress size; WGPUBufferUsage usage; } WGPUBufferDescriptor; @@ -652,6 +654,7 @@ typedef struct { } WGPURenderPipelineDescriptor; typedef struct { + WGPULabel label; WGPUAddressMode address_mode_u; WGPUAddressMode address_mode_v; WGPUAddressMode address_mode_w; @@ -692,7 +695,7 @@ typedef struct { } WGPUSwapChainDescriptor; typedef struct { - const char *label; + WGPULabel label; WGPUExtent3d size; uint32_t mip_level_count; uint32_t sample_count; @@ -725,6 +728,7 @@ typedef struct { } WGPUSwapChainOutput; typedef struct { + WGPULabel label; WGPUTextureFormat format; WGPUTextureViewDimension dimension; WGPUTextureAspect aspect; diff --git a/src/command.rs b/src/command.rs index 243cc5e7..c09007fe 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,8 +1,8 @@ use crate::GLOBAL; -pub use core::command::{compute_ffi::*, render_ffi::*}; +pub use wgc::command::{compute_ffi::*, render_ffi::*}; -use core::{gfx_select, id}; +use wgc::{gfx_select, id}; #[no_mangle] pub extern "C" fn wgpu_command_encoder_finish( @@ -33,8 +33,8 @@ pub extern "C" fn wgpu_command_encoder_copy_buffer_to_buffer( #[no_mangle] pub extern "C" fn wgpu_command_encoder_copy_buffer_to_texture( command_encoder_id: id::CommandEncoderId, - source: &core::command::BufferCopyView, - destination: &core::command::TextureCopyView, + source: &wgc::command::BufferCopyView, + destination: &wgc::command::TextureCopyView, copy_size: wgt::Extent3d, ) { gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_buffer_to_texture( @@ -47,8 +47,8 @@ pub extern "C" fn wgpu_command_encoder_copy_buffer_to_texture( #[no_mangle] pub extern "C" fn wgpu_command_encoder_copy_texture_to_buffer( command_encoder_id: id::CommandEncoderId, - source: &core::command::TextureCopyView, - destination: &core::command::BufferCopyView, + source: &wgc::command::TextureCopyView, + destination: &wgc::command::BufferCopyView, copy_size: wgt::Extent3d, ) { gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_texture_to_buffer( @@ -61,8 +61,8 @@ pub extern "C" fn wgpu_command_encoder_copy_texture_to_buffer( #[no_mangle] pub extern "C" fn wgpu_command_encoder_copy_texture_to_texture( command_encoder_id: id::CommandEncoderId, - source: &core::command::TextureCopyView, - destination: &core::command::TextureCopyView, + source: &wgc::command::TextureCopyView, + destination: &wgc::command::TextureCopyView, copy_size: wgt::Extent3d, ) { gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_texture_to_texture( @@ -80,9 +80,9 @@ pub extern "C" fn wgpu_command_encoder_copy_texture_to_texture( #[no_mangle] pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( encoder_id: id::CommandEncoderId, - desc: &core::command::RenderPassDescriptor, -) -> *mut core::command::RawPass { - let pass = core::command::RawPass::new_render(encoder_id, desc); + desc: &wgc::command::RenderPassDescriptor, +) -> *mut wgc::command::RawPass { + let pass = wgc::command::RawPass::new_render(encoder_id, desc); Box::into_raw(Box::new(pass)) } @@ -98,7 +98,7 @@ pub unsafe extern "C" fn wgpu_render_pass_end_pass(pass_id: id::RenderPassId) { } #[no_mangle] -pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut core::command::RawPass) { +pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut wgc::command::RawPass) { let _ = Box::from_raw(pass).into_vec(); } @@ -110,9 +110,9 @@ pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut core::command::RawP #[no_mangle] pub unsafe extern "C" fn wgpu_command_encoder_begin_compute_pass( encoder_id: id::CommandEncoderId, - _desc: Option<&core::command::ComputePassDescriptor>, -) -> *mut core::command::RawPass { - let pass = core::command::RawPass::new_compute(encoder_id); + _desc: Option<&wgc::command::ComputePassDescriptor>, +) -> *mut wgc::command::RawPass { + let pass = wgc::command::RawPass::new_compute(encoder_id); Box::into_raw(Box::new(pass)) } @@ -123,6 +123,6 @@ pub unsafe extern "C" fn wgpu_compute_pass_end_pass(pass_id: id::ComputePassId) } #[no_mangle] -pub unsafe extern "C" fn wgpu_compute_pass_destroy(pass: *mut core::command::RawPass) { +pub unsafe extern "C" fn wgpu_compute_pass_destroy(pass: *mut wgc::command::RawPass) { let _ = Box::from_raw(pass).into_vec(); } diff --git a/src/device.rs b/src/device.rs index 4ad0ec3f..7e97943b 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,98 +1,15 @@ use crate::GLOBAL; -use core::{gfx_select, hub::Token, id}; +use wgc::{device::Label, gfx_select, hub::Token, id}; use wgt::{BackendBit, DeviceDescriptor, Limits}; use std::{marker::PhantomData, slice}; -#[cfg(target_os = "macos")] -use objc::{msg_send, runtime::Object, sel, sel_impl}; - pub type RequestAdapterCallback = unsafe extern "C" fn(id: Option, userdata: *mut std::ffi::c_void); pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> id::SurfaceId { - use raw_window_handle::RawWindowHandle as Rwh; - - let instance = &GLOBAL.instance; - let surface = match raw_handle { - #[cfg(target_os = "ios")] - Rwh::IOS(h) => core::instance::Surface { - #[cfg(feature = "vulkan-portability")] - vulkan: None, - metal: instance - .metal - .create_surface_from_uiview(h.ui_view, cfg!(debug_assertions)), - }, - #[cfg(target_os = "macos")] - Rwh::MacOS(h) => { - let ns_view = if h.ns_view.is_null() { - let ns_window = h.ns_window as *mut Object; - unsafe { msg_send![ns_window, contentView] } - } else { - h.ns_view - }; - core::instance::Surface { - #[cfg(feature = "vulkan-portability")] - vulkan: instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_from_ns_view(ns_view)), - metal: instance - .metal - .create_surface_from_nsview(ns_view, cfg!(debug_assertions)), - } - } - #[cfg(all( - unix, - not(target_os = "android"), - not(target_os = "ios"), - not(target_os = "macos") - ))] - Rwh::Xlib(h) => core::instance::Surface { - vulkan: instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_from_xlib(h.display as _, h.window as _)), - }, - #[cfg(all( - unix, - not(target_os = "android"), - not(target_os = "ios"), - not(target_os = "macos") - ))] - Rwh::Wayland(h) => core::instance::Surface { - vulkan: instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_from_wayland(h.display, h.surface)), - }, - #[cfg(target_os = "android")] - Rwh::Android(h) => core::instance::Surface { - vulkan: instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_android(h.a_native_window)), - }, - #[cfg(windows)] - Rwh::Windows(h) => core::instance::Surface { - vulkan: instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_from_hwnd(std::ptr::null_mut(), h.hwnd)), - dx12: instance - .dx12 - .as_ref() - .map(|inst| inst.create_surface_from_hwnd(h.hwnd)), - dx11: instance.dx11.create_surface_from_hwnd(h.hwnd), - }, - _ => panic!("Unsupported window handle"), - }; - - let mut token = Token::root(); - GLOBAL - .surfaces - .register_identity(PhantomData, surface, &mut token) + GLOBAL.instance_create_surface(raw_handle, PhantomData) } #[cfg(all( @@ -150,7 +67,7 @@ pub extern "C" fn wgpu_create_surface_from_android( pub extern "C" fn wgpu_create_surface_from_metal_layer( layer: *mut std::ffi::c_void, ) -> id::SurfaceId { - let surface = core::instance::Surface { + let surface = wgc::instance::Surface { #[cfg(feature = "vulkan-portability")] vulkan: None, //TODO: currently requires `NSView` metal: GLOBAL @@ -180,7 +97,7 @@ pub extern "C" fn wgpu_create_surface_from_windows_hwnd( } pub fn wgpu_enumerate_adapters(mask: BackendBit) -> Vec { - GLOBAL.enumerate_adapters(core::instance::AdapterInputs::Mask(mask, || PhantomData)) + GLOBAL.enumerate_adapters(wgc::instance::AdapterInputs::Mask(mask, |_| PhantomData)) } /// # Safety @@ -188,28 +105,38 @@ pub fn wgpu_enumerate_adapters(mask: BackendBit) -> Vec { /// This function is unsafe as it calls an unsafe extern callback. #[no_mangle] pub unsafe extern "C" fn wgpu_request_adapter_async( - desc: Option<&core::instance::RequestAdapterOptions>, + desc: Option<&wgc::instance::RequestAdapterOptions>, mask: BackendBit, callback: RequestAdapterCallback, userdata: *mut std::ffi::c_void, ) { let id = GLOBAL.pick_adapter( &desc.cloned().unwrap_or_default(), - core::instance::AdapterInputs::Mask(mask, || PhantomData), + wgc::instance::AdapterInputs::Mask(mask, |_| PhantomData), ); callback(id, userdata); } #[no_mangle] -pub extern "C" fn wgpu_adapter_request_device( +pub unsafe extern "C" fn wgpu_adapter_request_device( adapter_id: id::AdapterId, desc: Option<&DeviceDescriptor>, + trace_path: *const std::os::raw::c_char, ) -> id::DeviceId { let desc = &desc.cloned().unwrap_or_default(); - gfx_select!(adapter_id => GLOBAL.adapter_request_device(adapter_id, desc, PhantomData)) + let trace_cstr = if trace_path.is_null() { + None + } else { + Some(std::ffi::CStr::from_ptr(trace_path)) + }; + let trace_cow = trace_cstr.as_ref().map(|cstr| cstr.to_string_lossy()); + let trace_path = trace_cow + .as_ref() + .map(|cow| std::path::Path::new(cow.as_ref())); + gfx_select!(adapter_id => GLOBAL.adapter_request_device(adapter_id, desc, trace_path, PhantomData)) } -pub fn adapter_get_info(adapter_id: id::AdapterId) -> core::instance::AdapterInfo { +pub fn adapter_get_info(adapter_id: id::AdapterId) -> wgc::instance::AdapterInfo { gfx_select!(adapter_id => GLOBAL.adapter_get_info(adapter_id)) } @@ -226,7 +153,7 @@ pub extern "C" fn wgpu_device_get_limits(_device_id: id::DeviceId, limits: &mut #[no_mangle] pub extern "C" fn wgpu_device_create_buffer( device_id: id::DeviceId, - desc: &wgt::BufferDescriptor, + desc: &wgt::BufferDescriptor