Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
Update for wgpu-core
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Apr 30, 2020
1 parent 49640d2 commit 89331a4
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 156 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ vulkan = ["wgc/gfx-backend-vulkan"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgc]
package = "wgpu-core"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "5c172dd4756aa152b4f3350e624d7b1b5d24ddda"
#git = "https://github.com/gfx-rs/wgpu"
#rev = "5c172dd4756aa152b4f3350e624d7b1b5d24ddda"
path = "../wgpu/wgpu-core"
features = ["raw-window-handle", "trace"] #TEMP

[dependencies.wgt]
package = "wgpu-types"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "5c172dd4756aa152b4f3350e624d7b1b5d24ddda"
#git = "https://github.com/gfx-rs/wgpu"
#rev = "5c172dd4756aa152b4f3350e624d7b1b5d24ddda"
path = "../wgpu/wgpu-types"

[dependencies]
arrayvec = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn run() {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
})
}, None)
.await
.unwrap();

Expand Down
5 changes: 3 additions & 2 deletions examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl framework::Example for Example {

// Create pipeline layout
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
bindings: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
Expand All @@ -156,7 +157,6 @@ impl framework::Example for Example {
ty: wgpu::BindingType::Sampler { comparison: false },
},
],
label: None,
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
Expand All @@ -171,13 +171,13 @@ impl framework::Example for Example {
depth: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: texture_extent,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
label: None,
});
let texture_view = texture.create_default_view();
let temp_buf =
Expand All @@ -200,6 +200,7 @@ impl framework::Example for Example {

// Create other resources
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
Expand Down
3 changes: 2 additions & 1 deletion examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
})
}, None)
.await
.unwrap();

Expand All @@ -99,6 +99,7 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {

log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); // force ownership by the closure
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
Expand Down
10 changes: 6 additions & 4 deletions examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{convert::TryInto, str::FromStr};
async fn run() {
let numbers = if std::env::args().len() <= 1 {
let default = vec![1, 2, 3, 4];
log::info!("No numbers were provided, defaulting to {:?}", default);
println!("No numbers were provided, defaulting to {:?}", default);
default
} else {
std::env::args()
Expand All @@ -12,8 +12,10 @@ async fn run() {
.collect()
};

// To see the output, run `RUST_LOG=info cargo run --example hello-compute`.
log::info!("Times: {:?}", execute_gpu(numbers).await);
let times = execute_gpu(numbers).await;
println!("Times: {:?}", times);
#[cfg(target_arch = "wasm32")]
log::info!("Times: {:?}", times);
}

async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
Expand All @@ -38,7 +40,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
})
}, None)
.await
.unwrap();

Expand Down
11 changes: 10 additions & 1 deletion examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
})
}, None)
.await
.unwrap();

Expand Down Expand Up @@ -86,6 +86,15 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);

event_loop.run(move |event, _, control_flow| {
// force ownership by the closure
let _ = (
&instance,
&adapter,
&vs_module,
&fs_module,
&pipeline_layout,
);

*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => window.request_redraw(),
Expand Down
3 changes: 3 additions & 0 deletions examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl Example {
});

let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("mip"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
Expand All @@ -161,6 +162,7 @@ impl Example {
let views = (0..mip_count)
.map(|mip| {
texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("mip"),
format: TEXTURE_FORMAT,
dimension: wgpu::TextureViewDimension::D2,
aspect: wgpu::TextureAspect::All,
Expand Down Expand Up @@ -293,6 +295,7 @@ impl framework::Example for Example {

// Create other resources
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
Expand Down
2 changes: 2 additions & 0 deletions examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ impl framework::Example for Example {

// Create other resources
let shadow_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("shadow"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
Expand All @@ -371,6 +372,7 @@ impl framework::Example for Example {
let mut shadow_target_views = (0..2)
.map(|i| {
Some(shadow_texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("shadow"),
format: Self::SHADOW_FORMAT,
dimension: wgpu::TextureViewDimension::D2,
aspect: wgpu::TextureAspect::All,
Expand Down
2 changes: 2 additions & 0 deletions examples/skybox/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl framework::Example for Skybox {
});

let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
Expand Down Expand Up @@ -219,6 +220,7 @@ impl framework::Example for Skybox {
}

let texture_view = texture.create_view(&wgpu::TextureViewDescriptor {
label: None,
format: SKYBOX_FORMAT,
dimension: wgpu::TextureViewDimension::Cube,
aspect: wgpu::TextureAspect::default(),
Expand Down
116 changes: 19 additions & 97 deletions src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{ffi::CString, marker::PhantomData, ptr, slice};
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),+ )) => {
match $id.backend() {
#[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))]
#[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "vulkan-portability"))]
wgt::Backend::Vulkan => $global.$method::<wgc::backend::Vulkan>( $($param),+ ),
#[cfg(any(target_os = "ios", target_os = "macos"))]
wgt::Backend::Metal => $global.$method::<wgc::backend::Metal>( $($param),+ ),
Expand Down Expand Up @@ -222,80 +222,11 @@ impl crate::Context for Context {
wgc::hub::Global::new("wgpu", wgc::hub::IdentityManagerFactory)
}

fn instance_create_surface<W: raw_window_handle::HasRawWindowHandle>(
fn instance_create_surface(
&self,
window: &W,
handle: raw_window_handle::RawWindowHandle,
) -> Self::SurfaceId {
use raw_window_handle::RawWindowHandle as Rwh;

let surface = match window.raw_window_handle() {
#[cfg(target_os = "ios")]
Rwh::IOS(h) => wgc::instance::Surface {
#[cfg(feature = "vulkan-portability")]
vulkan: None,
metal: self
.instance
.metal
.create_surface_from_uiview(h.ui_view, cfg!(debug_assertions)),
},
#[cfg(target_os = "macos")]
Rwh::MacOS(h) => {
use objc::{msg_send, runtime::Object, sel, sel_impl};
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
};
wgc::instance::Surface {
#[cfg(feature = "vulkan-portability")]
vulkan: self
.instance
.vulkan
.as_ref()
.map(|inst| inst.create_surface_from_ns_view(ns_view)),
metal: self
.instance
.metal
.create_surface_from_nsview(ns_view, cfg!(debug_assertions)),
}
}
#[cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))]
Rwh::Xlib(h) => wgc::instance::Surface {
vulkan: self
.instance
.vulkan
.as_ref()
.map(|inst| inst.create_surface_from_xlib(h.display as _, h.window as _)),
},
#[cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))]
Rwh::Wayland(h) => wgc::instance::Surface {
vulkan: self
.instance
.vulkan
.as_ref()
.map(|inst| inst.create_surface_from_wayland(h.display, h.surface)),
},
#[cfg(windows)]
Rwh::Windows(h) => wgc::instance::Surface {
vulkan: self
.instance
.vulkan
.as_ref()
.map(|inst| inst.create_surface_from_hwnd(std::ptr::null_mut(), h.hwnd)),
dx12: self
.instance
.dx12
.as_ref()
.map(|inst| inst.create_surface_from_hwnd(h.hwnd)),
dx11: self.instance.dx11.create_surface_from_hwnd(h.hwnd),
},
_ => panic!("Unsupported window handle"),
};

let mut token = wgc::hub::Token::root();
self.surfaces
.register_identity(PhantomData, surface, &mut token)
self.instance_create_surface(handle, PhantomData)
}

fn instance_request_adapter(
Expand All @@ -308,7 +239,7 @@ impl crate::Context for Context {
power_preference: options.power_preference,
compatible_surface: options.compatible_surface.map(|surface| surface.id),
},
wgc::instance::AdapterInputs::Mask(backends, || PhantomData),
wgc::instance::AdapterInputs::Mask(backends, |_| PhantomData),
);
ready(id)
}
Expand All @@ -317,9 +248,9 @@ impl crate::Context for Context {
&self,
adapter: &Self::AdapterId,
desc: &crate::DeviceDescriptor,
trace_dir: Option<&std::path::Path>,
) -> Self::RequestDeviceFuture {
let device_id =
gfx_select!(*adapter => self.adapter_request_device(*adapter, desc, PhantomData));
let device_id = gfx_select!(*adapter => self.adapter_request_device(*adapter, desc, trace_dir, PhantomData));
ready(Ok((device_id, device_id)))
}

Expand Down Expand Up @@ -579,11 +510,7 @@ impl crate::Context for Context {
unsafe {
let (id, ptr) = gfx_select!(*device => self.device_create_buffer_mapped(
*device,
&wgt::BufferDescriptor {
label: owned_label.as_ptr(),
size: desc.size,
usage: desc.usage,
},
&desc.map_label(|_| owned_label.as_ptr()),
PhantomData
));
let mapped_data = std::slice::from_raw_parts_mut(ptr, desc.size as usize);
Expand All @@ -599,11 +526,7 @@ impl crate::Context for Context {
let owned_label = OwnedLabel::new(desc.label.as_deref());
gfx_select!(*device => self.device_create_buffer(
*device,
&wgt::BufferDescriptor {
label: owned_label.as_ptr(),
size: desc.size,
usage: desc.usage,
},
&desc.map_label(|_| owned_label.as_ptr()),
PhantomData
))
}
Expand All @@ -616,15 +539,7 @@ impl crate::Context for Context {
let owned_label = OwnedLabel::new(desc.label.as_deref());
gfx_select!(*device => self.device_create_texture(
*device,
&wgt::TextureDescriptor {
label: owned_label.as_ptr(),
size: desc.size,
mip_level_count: desc.mip_level_count,
sample_count: desc.sample_count,
dimension: desc.dimension,
format: desc.format,
usage: desc.usage,
},
&desc.map_label(|_| owned_label.as_ptr()),
PhantomData
))
}
Expand All @@ -634,7 +549,12 @@ impl crate::Context for Context {
device: &Self::DeviceId,
desc: &SamplerDescriptor,
) -> Self::SamplerId {
gfx_select!(*device => self.device_create_sampler(*device, desc, PhantomData))
let owned_label = OwnedLabel::new(desc.label.as_deref());
gfx_select!(*device => self.device_create_sampler(
*device,
&desc.map_label(|_| owned_label.as_ptr()),
PhantomData
))
}

fn device_create_command_encoder(
Expand Down Expand Up @@ -774,7 +694,9 @@ impl crate::Context for Context {
texture: &Self::TextureId,
desc: Option<&TextureViewDescriptor>,
) -> Self::TextureViewId {
gfx_select!(*texture => self.texture_create_view(*texture, desc, PhantomData))
let owned_label = OwnedLabel::new(desc.and_then(|d| d.label.as_deref()));
let descriptor = desc.map(|d| d.map_label(|_| owned_label.as_ptr()));
gfx_select!(*texture => self.texture_create_view(*texture, descriptor.as_ref(), PhantomData))
}

fn texture_drop(&self, texture: &Self::TextureId) {
Expand Down
Loading

0 comments on commit 89331a4

Please sign in to comment.