| 1 |
# `wgpu_hal`: a cross-platform unsafe graphics abstraction |
| 2 |
|
| 3 |
This crate defines a set of traits abstracting over modern graphics APIs, |
| 4 |
with implementations ("backends") for Vulkan, Metal, Direct3D, and GL. |
| 5 |
|
| 6 |
`wgpu_hal` is a spiritual successor to |
| 7 |
[gfx-hal](https://github.com/gfx-rs/gfx), but with reduced scope, and |
| 8 |
oriented towards WebGPU implementation goals. It has no overhead for |
| 9 |
validation or tracking, and the API translation overhead is kept to the bare |
| 10 |
minimum by the design of WebGPU. This API can be used for resource-demanding |
| 11 |
applications and engines. |
| 12 |
|
| 13 |
The `wgpu_hal` crate's main design choices: |
| 14 |
|
| 15 |
- Our traits are meant to be *portable*: proper use |
| 16 |
should get equivalent results regardless of the backend. |
| 17 |
|
| 18 |
- Our traits' contracts are *unsafe*: implementations perform minimal |
| 19 |
validation, if any, and incorrect use will often cause undefined behavior. |
| 20 |
This allows us to minimize the overhead we impose over the underlying |
| 21 |
graphics system. If you need safety, the [`wgpu-core`] crate provides a |
| 22 |
safe API for driving `wgpu_hal`, implementing all necessary validation, |
| 23 |
resource state tracking, and so on. (Note that `wgpu-core` is designed for |
| 24 |
use via FFI; the [`wgpu`] crate provides more idiomatic Rust bindings for |
| 25 |
`wgpu-core`.) Or, you can do your own validation. |
| 26 |
|
| 27 |
- In the same vein, returned errors *only cover cases the user can't |
| 28 |
anticipate*, like running out of memory or losing the device. Any errors |
| 29 |
that the user could reasonably anticipate are their responsibility to |
| 30 |
avoid. For example, `wgpu_hal` returns no error for mapping a buffer that's |
| 31 |
not mappable: as the buffer creator, the user should already know if they |
| 32 |
can map it. |
| 33 |
|
| 34 |
- We use *static dispatch*. The traits are not |
| 35 |
generally object-safe. You must select a specific backend type |
| 36 |
like [`vulkan::Api`] or [`metal::Api`], and then use that |
| 37 |
according to the main traits, or call backend-specific methods. |
| 38 |
|
| 39 |
- We use *idiomatic Rust parameter passing*, |
| 40 |
taking objects by reference, returning them by value, and so on, |
| 41 |
unlike `wgpu-core`, which refers to objects by ID. |
| 42 |
|
| 43 |
- We map buffer contents *persistently*. This means that the buffer |
| 44 |
can remain mapped on the CPU while the GPU reads or writes to it. |
| 45 |
You must explicitly indicate when data might need to be |
| 46 |
transferred between CPU and GPU, if `wgpu_hal` indicates that the |
| 47 |
mapping is not coherent (that is, automatically synchronized |
| 48 |
between the two devices). |
| 49 |
|
| 50 |
- You must record *explicit barriers* between different usages of a |
| 51 |
resource. For example, if a buffer is written to by a compute |
| 52 |
shader, and then used as and index buffer to a draw call, you |
| 53 |
must use [`CommandEncoder::transition_buffers`] between those two |
| 54 |
operations. |
| 55 |
|
| 56 |
- Pipeline layouts are *explicitly specified* when setting bind |
| 57 |
group. Incompatible layouts disturb groups bound at higher indices. |
| 58 |
|
| 59 |
- The API *accepts collections as iterators*, to avoid forcing the user to |
| 60 |
store data in particular containers. The implementation doesn't guarantee |
| 61 |
that any of the iterators are drained, unless stated otherwise by the |
| 62 |
function documentation. For this reason, we recommend that iterators don't |
| 63 |
do any mutating work. |
| 64 |
|
| 65 |
Unfortunately, `wgpu_hal`'s safety requirements are not fully documented. |
| 66 |
Ideally, all trait methods would have doc comments setting out the |
| 67 |
requirements users must meet to ensure correct and portable behavior. If you |
| 68 |
are aware of a specific requirement that a backend imposes that is not |
| 69 |
ensured by the traits' documented rules, please file an issue. Or, if you are |
| 70 |
a capable technical writer, please file a pull request! |
| 71 |
|
| 72 |
[`wgpu-core`]: https://crates.io/crates/wgpu-core |
| 73 |
[`wgpu`]: https://crates.io/crates/wgpu |
| 74 |
[`vulkan::Api`]: vulkan/struct.Api.html |
| 75 |
[`metal::Api`]: metal/struct.Api.html |
| 76 |
|
| 77 |
## Primary backends |
| 78 |
|
| 79 |
The `wgpu_hal` crate has full-featured backends implemented on the following |
| 80 |
platform graphics APIs: |
| 81 |
|
| 82 |
- Vulkan, available on Linux, Android, and Windows, using the [`ash`] crate's |
| 83 |
Vulkan bindings. It's also available on macOS, if you install [MoltenVK]. |
| 84 |
|
| 85 |
- Metal on macOS, using the [`metal`] crate's bindings. |
| 86 |
|
| 87 |
- Direct3D 12 on Windows, using the [`windows`] crate's bindings. |
| 88 |
|
| 89 |
[`ash`]: https://crates.io/crates/ash |
| 90 |
[MoltenVK]: https://github.com/KhronosGroup/MoltenVK |
| 91 |
[`metal`]: https://crates.io/crates/metal |
| 92 |
[`windows`]: https://crates.io/crates/windows |
| 93 |
|
| 94 |
## Secondary backends |
| 95 |
|
| 96 |
The `wgpu_hal` crate has a partial implementation based on the following |
| 97 |
platform graphics API: |
| 98 |
|
| 99 |
- The GL backend is available anywhere OpenGL, OpenGL ES, or WebGL are |
| 100 |
available. See the [`gles`] module documentation for details. |
| 101 |
|
| 102 |
[`gles`]: gles/index.html |
| 103 |
|
| 104 |
You can see what capabilities an adapter is missing by checking the |
| 105 |
[`DownlevelCapabilities`][tdc] in [`ExposedAdapter::capabilities`], available |
| 106 |
from [`Instance::enumerate_adapters`]. |
| 107 |
|
| 108 |
The API is generally designed to fit the primary backends better than the |
| 109 |
secondary backends, so the latter may impose more overhead. |
| 110 |
|
| 111 |
[tdc]: wgt::DownlevelCapabilities |
| 112 |
|
| 113 |
## Debugging |
| 114 |
|
| 115 |
Most of the information on the wiki [Debugging wgpu Applications][wiki-debug] |
| 116 |
page still applies to this API, with the exception of API tracing/replay |
| 117 |
functionality, which is only available in `wgpu-core`. |
| 118 |
|
| 119 |
[wiki-debug]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications |
| 120 |
|