Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an error instead of panicking when canvas context is unavailable #3052

Merged
merged 2 commits into from Dec 1, 2022

Conversation

kpreid
Copy link
Contributor

@kpreid kpreid commented Sep 27, 2022

Checklist

  • Run cargo clippy.
  • Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections
Fixes #3017 "WebGL and WebGPU backends should not panic if canvas context is unavailable".

Description

  • Add a Result return value to wgpu::Instance::create_surface() and related functions.
  • Add wgpu::CreateSurfaceError.
  • Expand documentation of create_surface().

Testing
There is no current way to run automated tests under WASM, but I ran cargo check with and without the webgl feature; cargo nextest run; and some run-wasm examples.

Further notes and concerns

  • This is a breaking change (changing return types to be Result<Surface> instead of Surface) — is there anything to improve about the new API?

  • It would be really nice to have more details from the error, so that it is diagnosable rather than “Sorry, something wrong happened”. The main obstacle to that is the non-specificity of wgpu_hal::InstanceError. However, this PR doesn't strictly need that, and it can be enhanced later without wgpu API changes.

  • The docs for create_surface() say more things than they used to. I think they're better, but since this is an unsafe fn, everything it says is important. Particularly, I moved the remark about panicking on Metal from Safety to Panics on the assumption that if it's a panic it's not unsound (and therefore should not be in the Safety section), but perhaps that's wrong.

  • This is a draft because some changes are missing for features = ["webgl"]. I discovered that wgpu_hal is not depended on directly from wgpu (when the target is wasm32) even though it is indirectly depended on, so wgpu_hal::InstanceError isn't usable for communicating errors from wgpu_core to wgpu in the WebGL backend's context creation. So, a different error type will be needed, or that dependency added, or a reexport. I'm posting this PR anyway (as a draft) because it's mostly complete from an API and docs perspective. Resolved.

@cwfitzgerald
Copy link
Member

Is there a feasible way to run automated tests under WASM?

Yeah, through wasm-bindgen/wasm-pack, it's on my list to implement soon.

So, a different error type will be needed, or that dependency added,

We can take a dep on hal, that's fine.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Nothing looks too bad, please re-request review once this is fully ready!

@kpreid kpreid marked this pull request as ready for review November 25, 2022 02:19
@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2022

Codecov Report

Merging #3052 (c28eab2) into master (46b1216) will decrease coverage by 18.27%.
The diff coverage is 59.49%.

@@             Coverage Diff             @@
##           master    #3052       +/-   ##
===========================================
- Coverage   65.41%   47.14%   -18.28%     
===========================================
  Files          82       82               
  Lines       39455    39439       -16     
===========================================
- Hits        25809    18592     -7217     
- Misses      13646    20847     +7201     
Impacted Files Coverage Δ
wgpu-core/src/instance.rs 65.17% <ø> (-0.75%) ⬇️
wgpu/src/backend/direct.rs 56.44% <0.00%> (+1.04%) ⬆️
wgpu/src/lib.rs 51.43% <0.00%> (+0.88%) ⬆️
wgpu-core/src/command/transfer.rs 75.49% <64.00%> (+5.46%) ⬆️
wgpu-core/src/device/queue.rs 70.40% <75.00%> (-0.30%) ⬇️
wgpu-core/src/command/clear.rs 89.58% <100.00%> (-0.04%) ⬇️
wgpu-core/src/track/texture.rs 59.46% <100.00%> (-21.54%) ⬇️
wgpu-hal/src/gles/device.rs 0.00% <0.00%> (-80.19%) ⬇️
wgpu-hal/src/vulkan/adapter.rs 0.00% <0.00%> (-77.52%) ⬇️
wgpu-hal/src/vulkan/device.rs 0.00% <0.00%> (-75.42%) ⬇️
... and 24 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

wgpu/Cargo.toml Show resolved Hide resolved
wgpu/src/lib.rs Outdated Show resolved Hide resolved
wgpu/src/lib.rs Show resolved Hide resolved
@cwfitzgerald
Copy link
Member

Needs conflict resolution, then g2g

…ebGL 2).

Part of fixing gfx-rs#3017. This commit changes the handling of `web_sys`
errors and nulls from `expect()` to returning `Err`, but it doesn't
actually affect the public API — that will be done in the next commit.
@kpreid
Copy link
Contributor Author

kpreid commented Dec 1, 2022

I've rebased, resolving the conflict and squashing the previous changes from review. The 2 commits now on this branch are separate because they are intentionally self-contained to separate the implementation logic and the breaking type change. (By the way, it would be nice if there was a CONTRIBUTING.md or other documentation that specified project preferences for commit/PR management, such as whether I ought to have just fully squashed unconditionally and not thought about it.)

Also, please check on how I resolved the CHANGELOG conflict — I wasn't sure from the beginning whether this is really “major” (and now, deserving of its own entire section heading), but that does seem to be how breaking changes are reported, so perhaps it's just right.

@cwfitzgerald
Copy link
Member

By the way, it would be nice if there was a CONTRIBUTING.md or other documentation that specified project preferences for commit/PR management, such as whether I ought to have just fully squashed unconditionally and not thought about it.)

That would be great to have.

I wasn't sure from the beginning whether this is really “major” (and now, deserving of its own entire section heading), but that does seem to be how breaking changes are reported, so perhaps it's just right.

Yeah any breaking changes we should call out explicitly even if they aren't "major" in scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebGL and WebGPU backends should not panic if canvas context is unavailable
3 participants