From e32112a89a389708983f550feac568cc173a919e Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 16 Jan 2019 09:15:56 -0500 Subject: [PATCH] Swapchain exposed on Rust side --- wgpu-native/src/instance.rs | 9 ++++++++- wgpu-native/src/lib.rs | 2 +- wgpu-native/src/swap_chain.rs | 13 +++++++++++-- wgpu-rs/Cargo.toml | 7 ++++--- wgpu-rs/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 38b84e355..8256d1dbc 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -50,7 +50,14 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { } } -#[cfg(not(feature = "remote"))] +#[cfg(all( + not(feature = "remote"), + any( + feature = "gfx-backend-vulkan", + feature = "gfx-backend-dx12", + feature = "gfx-backend-metal" + ) +))] #[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 c957c1dbe..eb5728e1b 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -1,5 +1,5 @@ #[cfg(not(feature = "remote"))] -extern crate winit; +pub extern crate winit; #[cfg(feature = "gfx-backend-dx12")] extern crate gfx_backend_dx12 as back; diff --git a/wgpu-native/src/swap_chain.rs b/wgpu-native/src/swap_chain.rs index 0957f1cb1..3456b386b 100644 --- a/wgpu-native/src/swap_chain.rs +++ b/wgpu-native/src/swap_chain.rs @@ -52,10 +52,16 @@ pub struct SwapChainDescriptor { pub height: u32, } +#[repr(C)] +pub struct SwapChainOutput { + pub texture_id: TextureId, + pub view_id: TextureViewId, +} + #[no_mangle] pub extern "C" fn wgpu_swap_chain_get_next_texture( swap_chain_id: SwapChainId, -) -> TextureId { +) -> SwapChainOutput { let mut swap_chain_guard = HUB.swap_chains.write(); let swap_chain = swap_chain_guard.get_mut(swap_chain_id); let device_guard = HUB.devices.read(); @@ -81,7 +87,10 @@ pub extern "C" fn wgpu_swap_chain_get_next_texture( None => unreachable!(), } - frame.texture_id.value + SwapChainOutput { + texture_id: frame.texture_id.value, + view_id: frame.view_id.value, + } } #[no_mangle] diff --git a/wgpu-rs/Cargo.toml b/wgpu-rs/Cargo.toml index 3a64e84e2..3d421d62a 100644 --- a/wgpu-rs/Cargo.toml +++ b/wgpu-rs/Cargo.toml @@ -11,9 +11,10 @@ edition = "2018" [features] default = [] -metal = ["wgpu-native/gfx-backend-metal"] -dx12 = ["wgpu-native/gfx-backend-dx12"] -vulkan = ["wgpu-native/gfx-backend-vulkan"] +backed = [] +metal = ["wgpu-native/gfx-backend-metal", "backed"] +dx12 = ["wgpu-native/gfx-backend-dx12", "backed"] +vulkan = ["wgpu-native/gfx-backend-vulkan", "backed"] [dependencies] wgpu-native = { path = "../wgpu-native" } diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index f74343620..f0ca2af2c 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -13,6 +13,7 @@ pub use wgn::{ RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor, RenderPassDescriptor, ShaderModuleDescriptor, ShaderStage, ShaderStageFlags, StoreOp, TextureDescriptor, TextureDimension, TextureFormat, TextureUsageFlags, TextureViewDescriptor, + SwapChainDescriptor, }; pub struct Instance { @@ -35,6 +36,14 @@ pub struct TextureView { id: wgn::TextureViewId, } +pub struct Surface { + id: wgn::SurfaceId, +} + +pub struct SwapChain { + id: wgn::SwapChainId, +} + pub struct BindGroupLayout { id: wgn::BindGroupLayoutId, } @@ -125,6 +134,13 @@ impl Instance { id: wgn::wgpu_instance_get_adapter(self.id, desc), } } + + #[cfg(feature = "backed")] + pub fn create_surface(&self, window: &::winit::Window) -> Surface { + Surface { + id: wgn::wgpu_instance_create_surface_from_winit(self.id, window) + } + } } impl Adapter { @@ -148,6 +164,7 @@ impl Device { } } + //TODO: borrow instead of new object? pub fn get_queue(&self) -> Queue { Queue { id: wgn::wgpu_device_get_queue(self.id), @@ -255,6 +272,12 @@ impl Device { id: wgn::wgpu_device_create_texture(self.id, &desc), } } + + pub fn create_swap_chain(&self, surface: &Surface, desc: &SwapChainDescriptor) -> SwapChain { + SwapChain { + id: wgn::wgpu_device_create_swap_chain(self.id, surface.id, desc), + } + } } impl Texture { @@ -351,3 +374,15 @@ impl Queue { ); } } + +impl SwapChain { + //TODO: borrow instead of new object? + pub fn get_next_texture(&self) -> (Texture, TextureView) { + let output = wgn::wgpu_swap_chain_get_next_texture(self.id); + (Texture { id: output.texture_id} , TextureView { id: output.view_id }) + } + + pub fn present(&self) { + wgn::wgpu_swap_chain_present(self.id); + } +}