gpu: use packed structs to wrap WebGPU's bitfield enums

This commit is contained in:
Silver 2022-04-07 00:14:32 +01:00 committed by Stephen Gutekanst
parent 232d0dc5ad
commit 47cd84f8ff
3 changed files with 61 additions and 26 deletions

View file

@ -37,7 +37,7 @@ pub fn main() !void {
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = app.swap_chain_format, .format = app.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = .all, .write_mask = gpu.ColorWriteMask.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState{
.module = fs_module, .module = fs_module,

View file

@ -104,7 +104,7 @@ pub fn main() !void {
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = window_data.swap_chain_format, .format = window_data.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = .all, .write_mask = gpu.ColorWriteMask.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState{
.module = fs_module, .module = fs_module,

View file

@ -221,34 +221,69 @@ pub const VertexStepMode = enum(u32) {
instance = 0x00000001, instance = 0x00000001,
}; };
pub const BufferUsage = enum(u32) { pub const BufferUsage = packed struct {
none = 0x00000000, map_read: bool = false,
map_read = 0x00000001, map_write: bool = false,
map_write = 0x00000002, copy_src: bool = false,
copy_src = 0x00000004, copy_dst: bool = false,
copy_dst = 0x00000008, index: bool = false,
index = 0x00000010, vertex: bool = false,
vertex = 0x00000020, uniform: bool = false,
uniform = 0x00000040, storage: bool = false,
storage = 0x00000080, indirect: bool = false,
indirect = 0x00000100, query_resolve: bool = false,
query_resolve = 0x00000200,
_pad0: u6 = 0,
_pad1: u16 = 0,
comptime {
std.debug.assert(
@sizeOf(@This()) == @sizeOf(u32) and
@bitSizeOf(@This()) == @bitSizeOf(u32),
);
}
}; };
pub const ColorWriteMask = enum(u32) { pub const ColorWriteMask = packed struct {
none = 0x00000000, red: bool = false,
red = 0x00000001, green: bool = false,
green = 0x00000002, blue: bool = false,
blue = 0x00000004, alpha: bool = false,
alpha = 0x00000008,
all = 0x0000000F, _pad0: u4 = 0,
_pad1: u8 = 0,
_pad2: u16 = 0,
comptime {
std.debug.assert(
@sizeOf(@This()) == @sizeOf(u32) and
@bitSizeOf(@This()) == @bitSizeOf(u32),
);
}
pub const all = ColorWriteMask{
.red = true,
.green = true,
.blue = true,
.alpha = true,
};
}; };
pub const ShaderStage = enum(u32) { pub const ShaderStage = packed struct {
none = 0x00000000, vertex: bool = false,
vertex = 0x00000001, fragment: bool = false,
fragment = 0x00000002, compute: bool = false,
compute = 0x00000004,
_pad0: u5 = 0,
_pad1: u8 = 0,
_pad2: u16 = 0,
comptime {
std.debug.assert(
@sizeOf(@This()) == @sizeOf(u32) and
@bitSizeOf(@This()) == @bitSizeOf(u32),
);
}
}; };
test "name" { test "name" {