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
Stephen Gutekanst
2ccf510696
gpu: use std.testing.refAllDeclsRecursive(@This());
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
fd948f4e8c
gpu: correct extern declarations
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
616395c583
gpu: add RenderPipeline.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
122713a44d
gpu: add FragmentState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
1632a5305e
gpu: add VertexState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2fa5e5adf4
gpu: add RenderPassDescriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
8484d47e78
gpu: add Device.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
286cbb6160
gpu: add ComputePipeline.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
44d378e102
gpu: add ColorTargetState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
5b66e3fbe0
gpu: add BindGroupLayout.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
26a29e1b2a
gpu: add VertexBufferLayout
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
ed1232f59d
gpu: add Texture.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
7f8dedcbd5
gpu: add SupportedLimits
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
cb68331f6c
gpu: add RequiredLimits
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
ad6d037d8f
gpu: add RenderPassColorAttachment
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
8683fa8aa9
gpu: add ProgrammableStageDescriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
74b7e66793
gpu: add ImageCopyTexture
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
326d66127e
gpu: add ImageCopyBuffer
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
9b615f212d
gpu: add DepthStencilState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
7a8f1b0cf9
gpu: add ComputePassDescriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
04fd1c14d3
gpu: add CompilationInfo
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
cca71c4a2b
gpu: add BlendState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
4497f6dd69
gpu: add BindGroupLayout.Entry
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
4fd4c5db7e
gpu: add BindGroup.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
82bff3e9b4
gpu: add VertexAttribute
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2e9ef64479
gpu: add TextureView.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2a50f3c3bd
gpu: add Texture.DataLayout
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
54b0ffdcc6
gpu: add Texture.BindingLayout
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
9ee22aeaa8
gpu: add SwapChain.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
2273d2517e
gpu: add Surface.DescriptorFromXlibWindow
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
7b748eaca1
gpu: add Surface.DescriptorFromWindowsSwapChainPanel
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
1b7e753209
gpu: add Surface.DescriptorFromWindowsHWND
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
84300bd6fc
gpu: add Surface.DescriptorFromWindowsCoreWindow
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
3cadc1ec7f
gpu: add Surface.DescriptorFromWaylandSurface
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
f721102a3a
gpu: add Surface.DescriptorFromMetalLayer
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
9c6882ba8a
gpu: add Surface.DescriptorFromCanvasHTMLSelector
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
598a99d3ed
gpu: add Surface.DescriptorFromAndroidNativeWindow
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
637e0c92d3
gpu: add Surface.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
340739ad44
gpu: add StorageTextureBindingLayout
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
19ced8efc6
gpu: add StencilFaceState
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
cd14f1a195
gpu: add ShaderModule.WGSLDescriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
25ac58ebc8
gpu: add ShaderModule.SPIRVDescriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
efae1c2b91
gpu: add ShaderModule.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
80b8f9224c
gpu: add Sampler.Descriptor
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
5b4c30871e
gpu: add Sampler.BindingLayout
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
b90658aba8
gpu: add RequestAdapterOptions
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
a91186c2c9
gpu: add RenderPassTimestampWrite
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00
Stephen Gutekanst
eead8fd29c
gpu: add RenderPassDescriptorMaxDrawCount
...
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-12 00:43:43 -07:00