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

Commit e30a428

Browse files
committed
Port the Web backend to the Context trait
1 parent 0d85635 commit e30a428

File tree

8 files changed

+919
-825
lines changed

8 files changed

+919
-825
lines changed

examples/capture/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ async fn run() {
2323
},
2424
limits: wgpu::Limits::default(),
2525
})
26-
.await;
26+
.await
27+
.unwrap();
2728

2829
// Rendered image is 256×256 with 32-bit RGBA color
2930
let size = 256u32;

examples/framework.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
5050
log::info!("Initializing the surface...");
5151

5252
let instance = wgpu::Instance::new();
53-
let (size, surface) = {
53+
let (size, surface) = unsafe {
5454
let size = window.inner_size();
5555
let surface = instance.create_surface(&window);
5656
(size, surface)
@@ -74,7 +74,8 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
7474
},
7575
limits: wgpu::Limits::default(),
7676
})
77-
.await;
77+
.await
78+
.unwrap();
7879

7980
let mut sc_desc = wgpu::SwapChainDescriptor {
8081
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,

examples/hello-compute/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
3939
},
4040
limits: wgpu::Limits::default(),
4141
})
42-
.await;
42+
.await
43+
.unwrap();
4344

4445
let cs = include_bytes!("shader.comp.spv");
4546
let cs_module =

examples/hello-triangle/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) {
88
let size = window.inner_size();
99
let instance = wgpu::Instance::new();
10-
let surface = instance.create_surface(&window);
10+
let surface = unsafe { instance.create_surface(&window) };
1111
let adapter = instance
1212
.request_adapter(
1313
&wgpu::RequestAdapterOptions {
@@ -26,7 +26,8 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
2626
},
2727
limits: wgpu::Limits::default(),
2828
})
29-
.await;
29+
.await
30+
.unwrap();
3031

3132
let vs = include_bytes!("shader.vert.spv");
3233
let vs_module =

src/backend/direct.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@ macro_rules! gfx_select {
2626
};
2727
}
2828

29-
fn map_buffer_copy_view(view: crate::BufferCopyView<'_>) -> wgc::command::BufferCopyView {
30-
wgc::command::BufferCopyView {
31-
buffer: view.buffer.id,
32-
offset: view.offset,
33-
bytes_per_row: view.bytes_per_row,
34-
rows_per_image: view.rows_per_image,
35-
}
36-
}
37-
38-
fn map_texture_copy_view<'a>(view: crate::TextureCopyView<'a>) -> wgc::command::TextureCopyView {
39-
wgc::command::TextureCopyView {
40-
texture: view.texture.id,
41-
mip_level: view.mip_level,
42-
array_layer: view.array_layer,
43-
origin: view.origin,
44-
}
45-
}
46-
4729
pub type Context = wgc::hub::Global<wgc::hub::IdentityManagerFactory>;
4830

4931
mod pass_impl {
@@ -184,6 +166,24 @@ mod pass_impl {
184166
}
185167
}
186168

169+
fn map_buffer_copy_view(view: crate::BufferCopyView<'_>) -> wgc::command::BufferCopyView {
170+
wgc::command::BufferCopyView {
171+
buffer: view.buffer.id,
172+
offset: view.offset,
173+
bytes_per_row: view.bytes_per_row,
174+
rows_per_image: view.rows_per_image,
175+
}
176+
}
177+
178+
fn map_texture_copy_view<'a>(view: crate::TextureCopyView<'a>) -> wgc::command::TextureCopyView {
179+
wgc::command::TextureCopyView {
180+
texture: view.texture.id,
181+
mip_level: view.mip_level,
182+
array_layer: view.array_layer,
183+
origin: view.origin,
184+
}
185+
}
186+
187187
impl crate::Context for Context {
188188
type AdapterId = wgc::id::AdapterId;
189189
type DeviceId = wgc::id::DeviceId;
@@ -211,11 +211,12 @@ impl crate::Context for Context {
211211
type SwapChainOutputDetail = SwapChainOutputDetail;
212212

213213
type RequestAdapterFuture = Ready<Option<Self::AdapterId>>;
214-
type RequestDeviceFuture = Ready<(Self::DeviceId, Self::QueueId)>;
214+
type RequestDeviceFuture =
215+
Ready<Result<(Self::DeviceId, Self::QueueId), crate::RequestDeviceError>>;
215216
type MapReadFuture =
216-
native_gpu_future::GpuFuture<Result<BufferReadMappingDetail, crate::BufferAsyncErr>>;
217+
native_gpu_future::GpuFuture<Result<BufferReadMappingDetail, crate::BufferAsyncError>>;
217218
type MapWriteFuture =
218-
native_gpu_future::GpuFuture<Result<BufferWriteMappingDetail, crate::BufferAsyncErr>>;
219+
native_gpu_future::GpuFuture<Result<BufferWriteMappingDetail, crate::BufferAsyncError>>;
219220

220221
fn init() -> Self {
221222
wgc::hub::Global::new("wgpu", wgc::hub::IdentityManagerFactory)
@@ -319,7 +320,7 @@ impl crate::Context for Context {
319320
) -> Self::RequestDeviceFuture {
320321
let device_id =
321322
gfx_select!(*adapter => self.adapter_request_device(*adapter, desc, PhantomData));
322-
ready((device_id, device_id))
323+
ready(Ok((device_id, device_id)))
323324
}
324325

325326
fn device_create_swap_chain(
@@ -694,7 +695,7 @@ impl crate::Context for Context {
694695
buffer_id,
695696
}));
696697
} else {
697-
completion.complete(Err(crate::BufferAsyncErr));
698+
completion.complete(Err(crate::BufferAsyncError));
698699
}
699700
}
700701

@@ -731,7 +732,7 @@ impl crate::Context for Context {
731732
buffer_id,
732733
}));
733734
} else {
734-
completion.complete(Err(crate::BufferAsyncErr));
735+
completion.complete(Err(crate::BufferAsyncError));
735736
}
736737
}
737738

src/backend/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(target_arch = "wasm32")]
22
mod web;
3-
//#[cfg(target_arch = "wasm32")]
4-
//pub use web::*;
3+
#[cfg(target_arch = "wasm32")]
4+
pub(crate) use web::Context;
55

66
#[cfg(not(target_arch = "wasm32"))]
77
mod direct;

0 commit comments

Comments
 (0)