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

Add web backend #193

Merged
merged 17 commits into from
Apr 18, 2020
Merged

Add web backend #193

merged 17 commits into from
Apr 18, 2020

Conversation

grovesNL
Copy link
Collaborator

This branch has a bunch of WIP changes as I work through #101

The branch is still very unstable but any feedback about the approach so far would be great.

I tried to use a backend trait initially (with associated types and functions) but it didn't work great here. For example, async fn can't be used on traits yet.

Instead, with the current approach we have to manually ensure that all type signatures match. This seems mostly ok because we wrap most types/functions in wgpu-rs anyway, but we do have to be careful wherever we directly expose types. For example, if an exposed type is Copy in one backend and not the other, we'd probably have to newtype them (probably relevant for all of the XyzId types).

Copy link
Member

@kvark kvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's both sad and relieving that we can't use a trait:

  • sad because it's harder to guarantee the implementations match perfectly
  • relieving because the users don't need to suffer :)

examples/hello-compute/main.rs Outdated Show resolved Hide resolved
src/backend/web.rs Show resolved Hide resolved
@grovesNL
Copy link
Collaborator Author

I was able to run hello-compute in Firefox Nightly with the latest changes (top wasm, bottom native):

wasm-native

@grovesNL
Copy link
Collaborator Author

And now hello-triangle is able to run on the web too:

wasm-triangle

(it's upside-down due to gfx-rs/wgpu#519)

@@ -60,7 +62,7 @@ async fn run() {
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
format: wgpu::TextureFormat::Bgra8Unorm,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting. Is it now the case that Bgra8Unorm is assumed to be sRGB?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope both Bgra8Unorm and Bgra8UnormSrgb still exist – this is just a temporary hack for the hello-triangle example because Chrome Canary didn't seem to allow me to use Bgra8UnormSrgb for the swapchain :) I haven't looked into it yet but a swapchain with Bgra8UnormSrgb is supposed to work fine in browsers as far as I know.

@grovesNL grovesNL mentioned this pull request Mar 16, 2020
4 tasks
@grovesNL grovesNL force-pushed the wasm1 branch 2 times, most recently from f33513d to 4c2744b Compare March 16, 2020 11:21
bors bot added a commit that referenced this pull request Mar 16, 2020
204: Return futures from request adapter/request device r=kvark a=grovesNL

Relevant to #175

Backporting some parts of #193 for async request adapter and async request device.

Co-authored-by: Joshua Groves <josh@joshgroves.com>
bors bot added a commit that referenced this pull request Mar 16, 2020
204: Return futures from request adapter/request device r=kvark a=grovesNL

Relevant to #175

Backporting some parts of #193 for async request adapter and async request device.

Co-authored-by: Joshua Groves <josh@joshgroves.com>
@Songtronix
Copy link

If you want to use async traits there is async-trait. Supports a wide range of usecases but has it's limitations.

@grovesNL grovesNL force-pushed the wasm1 branch 2 times, most recently from 1565ef7 to 4545236 Compare March 27, 2020 03:05
@grovesNL
Copy link
Collaborator Author

grovesNL commented Apr 4, 2020

msaa-line and boids are working now too

image

image

@grovesNL
Copy link
Collaborator Author

grovesNL commented Apr 5, 2020

The next working example is cube:

image

@grovesNL
Copy link
Collaborator Author

grovesNL commented Apr 6, 2020

Skybox!
image

Copy link

@monocodus monocodus bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is autogenerated code-style review, new suggestions: 14

examples/cube/main.rs Outdated Show resolved Hide resolved
examples/cube/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/skybox/main.rs Outdated Show resolved Hide resolved
examples/skybox/main.rs Outdated Show resolved Hide resolved
@grovesNL
Copy link
Collaborator Author

All examples are available at http://joshgroves.com/gfx/wgpu/examples/?example=hello-triangle for now.

Each example is built in debug and much larger than the final wasm files will be (built in release/stripped of unused symbols). There are also still some issues with the winit integration, so the wasm file has to be fully loaded before sending any events to the canvas. ControlFlow::Poll isn't being used yet so interaction (e.g. by moving the cursor over the canvas) is needed to continue the event loop.

Both Chrome Canary and Firefox Nightly have issues running all examples at the moment, but eventually all examples should work fine 🤞

Copy link

@monocodus monocodus bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is autogenerated code-style review, new suggestions: 14

examples/cube/main.rs Outdated Show resolved Hide resolved
examples/cube/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/mipmap/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/skybox/main.rs Outdated Show resolved Hide resolved
examples/skybox/main.rs Outdated Show resolved Hide resolved
@expenses
Copy link
Contributor

Both Chrome Canary and Firefox Nightly have issues running all examples at the moment, but eventually all examples should work fine crossed_fingers

No luck on Safari here either.

@grovesNL
Copy link
Collaborator Author

@expenses I also forgot to mention that these examples are currently using SPIR-V, which won't work in Safari. We'll eventually need to support WGSL here, which will allow Safari to run them (once WebGPU support is ready in Safari in general)

Copy link

@monocodus monocodus bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is autogenerated code-style review, new suggestions: 4

examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
examples/shadow/main.rs Outdated Show resolved Hide resolved
@grovesNL grovesNL changed the title [WIP] Add web backend Add web backend Apr 18, 2020
@grovesNL
Copy link
Collaborator Author

bors r=kvark

@grovesNL
Copy link
Collaborator Author

@kvark I need to merge this manually due to https://forum.bors.tech/t/resource-not-accessible-by-integration/408/3 – I'm updating the CI actions but bors isn't allowed to merge those changes

@grovesNL grovesNL merged commit 423f576 into gfx-rs:master Apr 18, 2020
@grovesNL grovesNL deleted the wasm1 branch April 18, 2020 03:34
@Keavon
Copy link

Keavon commented Apr 18, 2020

🎉

@grovesNL
Copy link
Collaborator Author

For anyone interested in continuing to follow the progress here, I'll keep updating the examples at http://joshgroves.com/gfx/wgpu/examples/.

Some of the recent fixes from @kvark should be landing in Firefox Nightly soon (enable WebGPU using the instructions above) which should get most of these examples working.

@Keavon
Copy link

Keavon commented Apr 19, 2020

Should Currently wgpu-rs works on native platforms, but WASM support is currently being added as well. be removed from the project's readme?

@grovesNL
Copy link
Collaborator Author

@Keavon yeah, we should be able to remove that and clarify that both native and wasm are supported now 👍 Would you like to submit a PR to update it?

@Keavon
Copy link

Keavon commented Apr 19, 2020

I haven't been following this closely enough to know what would be most appropriate to write so I'd probably feel better not doing the PR myself.

@grovesNL
Copy link
Collaborator Author

Maybe we could replace it with something like

wgpu-rs can be run both natively (using wgpu-core and gfx-hal) and on the web (using the WebGPU API from wasm).

@kvark kvark mentioned this pull request Apr 21, 2020
@AJTJ AJTJ mentioned this pull request Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants