mach/gpu/src
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
..
adapter.zig gpu: add TODOs regarding nullability 2022-08-12 00:43:43 -07:00
bind_group.zig gpu: add BindGroup.Descriptor 2022-08-12 00:43:43 -07:00
bind_group_layout.zig gpu: add BindGroupLayout.Descriptor 2022-08-12 00:43:43 -07:00
buffer.zig gpu: fix filename case sensitivity 2022-08-12 00:43:43 -07:00
command_buffer.zig gpu: correct extern declarations 2022-08-12 00:43:43 -07:00
command_encoder.zig gpu: correct extern declarations 2022-08-12 00:43:43 -07:00
compute_pass_encoder.zig gpu: add TODOs regarding nullability 2022-08-12 00:43:43 -07:00
compute_pipeline.zig gpu: add ComputePipeline.Descriptor 2022-08-12 00:43:43 -07:00
dawn.zig gpu: correct extern declarations 2022-08-12 00:43:43 -07:00
device.zig gpu: add Device.Descriptor 2022-08-12 00:43:43 -07:00
external_texture.zig gpu: add PipelineLayout.Descriptor 2022-08-12 00:43:43 -07:00
instance.zig gpu: begin switching from enum(usize) to opaque pointers 2022-08-12 00:43:43 -07:00
interface.zig gpu: begin switching from enum(usize) to opaque pointers 2022-08-12 00:43:43 -07:00
main.zig gpu: begin switching from enum(usize) to opaque pointers 2022-08-12 00:43:43 -07:00
pipeline_layout.zig gpu: add PipelineLayout.Descriptor 2022-08-12 00:43:43 -07:00
query_set.zig gpu: add QuerySet.Descriptor 2022-08-12 00:43:43 -07:00
queue.zig gpu: add Queue.Descriptor 2022-08-12 00:43:43 -07:00
render_bundle.zig gpu: add RenderBundle.Descriptor 2022-08-12 00:43:43 -07:00
render_bundle_encoder.zig gpu: add RenderBundleEncoder.Descriptor 2022-08-12 00:43:43 -07:00
render_pass_encoder.zig gpu: add TODOs regarding nullability 2022-08-12 00:43:43 -07:00
render_pipeline.zig gpu: add RenderPipeline.Descriptor 2022-08-12 00:43:43 -07:00
sampler.zig gpu: add Sampler.Descriptor 2022-08-12 00:43:43 -07:00
shader_module.zig gpu: add ShaderModule.WGSLDescriptor 2022-08-12 00:43:43 -07:00
surface.zig gpu: add Surface.DescriptorFromXlibWindow 2022-08-12 00:43:43 -07:00
swap_chain.zig gpu: add SwapChain.Descriptor 2022-08-12 00:43:43 -07:00
texture.zig gpu: add Texture.Descriptor 2022-08-12 00:43:43 -07:00
texture_view.zig gpu: add TextureView.Descriptor 2022-08-12 00:43:43 -07:00
types.zig gpu: add RenderPipeline.Descriptor 2022-08-12 00:43:43 -07:00