Commit graph

65 commits

Author SHA1 Message Date
Stephen Gutekanst
898963619b gpu: rename NullInterface -> StubInterface
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
0b63b6a41b gpu: fully implement NullInterface
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
b0adf42c70 gpu: inline TODOs
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
8453b0d2a6 gpu: add Adapter.release
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
6a3e011809 gpu: add Adapter.reference
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
6d954555fb gpu: add Adapter.requestDevice
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
c9914d6d94 gpu: add Adapter.hasFeature
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
c3af237271 gpu: prepare to type check all API methods of a gpu.Interface at comptime
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
6ea76e770f gpu: add Adapter.getProperties
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2b7241be0a gpu: add Adapter.getLimits
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
60496259b2 gpu: add Adapter.enumerateFeatures
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
5b0ae990e3 gpu: add Adapter.createDevice and comptime interface strategy
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2d6dbd3351 gpu: add getProcAddress
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
5741082848 gpu: begin switching from enum(usize) to opaque pointers
WebGPU objects, like say `WGPUTexture`, could be represented in one of two ways:

1. As an `enum(usize)` as we were doing previously
2. As an `*opaque` pointer

In `webgpu.h` natively, `void*` opaque pointers are used. However, WebGPU objects are not
actually pointers: they are just IDs. A concrete example of this is how it is almost always
valid to for example pass a `WGPUTexture` that has been freed into the API. Doing so does
not result in undefined behavior, instead the implementation considers the texture to be an
ID and since it can't find such an ID it knows that texture has been free'd and to generate
an error instead of undefined behavior. In this regard, we can say that `enum(usize)` is the
more faithful representation.

`enum(usize)` is not without issues, though: it cannot effectively represent nullability in
Zig, which is used in the `webgpu.h` C ABI to represent _optionality_. `?enum(usize)` is not
a valid exported C type, the Zig compiler rejects it. And so in practice to maintain C ABI
compatability with descriptor struct types (which we do not want to copy or bitcast), we must
represent nullability with a `null` value:

```zig
pub const Texture = enum(usize) {
    _,

    pub const null: Texture = @intToEnum(Texture, 0);

    pub const Descriptor = extern struct {
        next_in_chain: *const ChainedStruct,
    };

    pub fn init() Texture {
        return Texture.null;
    }
};
```

The downside to this is that `init` cannot return a clear optional `?Texture`, and instead
must return a `Texture` which may be `== .null`. This downside seems significant enough to
warrant _not_ going with `enum(usize)` and instead going with `*opaque`:

```zig
pub const Texture = *opaque {
    pub fn init() ?Texture { return null }
};

pub const TextureDescriptor = extern struct {
    next_in_chain: *const ChainedStruct,
};
```

The downside to `*opaque` is that the namespace cannot have types nested below it.
`gpu.Texture.Descriptor` becomes `gpu.TextureDescriptor`.

Not ideal, but a tradeoff we'll accept to be able to represent optionality with the actual
Zig type system.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
7f6cb8b511 gpu: add comptime gpu.Interface, gpu.Export for C ABI exports
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00