From 47cd84f8ffbf85e6105c88a12d0a7e3ef3c4cc39 Mon Sep 17 00:00:00 2001 From: Silver Date: Thu, 7 Apr 2022 00:14:32 +0100 Subject: [PATCH] gpu: use packed structs to wrap WebGPU's bitfield enums --- examples/main.zig | 2 +- gpu/examples/main.zig | 2 +- gpu/src/enums.zig | 83 ++++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/examples/main.zig b/examples/main.zig index 2701f32b..37c09fb6 100644 --- a/examples/main.zig +++ b/examples/main.zig @@ -37,7 +37,7 @@ pub fn main() !void { const color_target = gpu.ColorTargetState{ .format = app.swap_chain_format, .blend = &blend, - .write_mask = .all, + .write_mask = gpu.ColorWriteMask.all, }; const fragment = gpu.FragmentState{ .module = fs_module, diff --git a/gpu/examples/main.zig b/gpu/examples/main.zig index c6728246..926487df 100644 --- a/gpu/examples/main.zig +++ b/gpu/examples/main.zig @@ -104,7 +104,7 @@ pub fn main() !void { const color_target = gpu.ColorTargetState{ .format = window_data.swap_chain_format, .blend = &blend, - .write_mask = .all, + .write_mask = gpu.ColorWriteMask.all, }; const fragment = gpu.FragmentState{ .module = fs_module, diff --git a/gpu/src/enums.zig b/gpu/src/enums.zig index f0aeffc4..ed194beb 100644 --- a/gpu/src/enums.zig +++ b/gpu/src/enums.zig @@ -221,34 +221,69 @@ pub const VertexStepMode = enum(u32) { instance = 0x00000001, }; -pub const BufferUsage = enum(u32) { - none = 0x00000000, - map_read = 0x00000001, - map_write = 0x00000002, - copy_src = 0x00000004, - copy_dst = 0x00000008, - index = 0x00000010, - vertex = 0x00000020, - uniform = 0x00000040, - storage = 0x00000080, - indirect = 0x00000100, - query_resolve = 0x00000200, +pub const BufferUsage = packed struct { + map_read: bool = false, + map_write: bool = false, + copy_src: bool = false, + copy_dst: bool = false, + index: bool = false, + vertex: bool = false, + uniform: bool = false, + storage: bool = false, + indirect: bool = false, + query_resolve: bool = false, + + _pad0: u6 = 0, + _pad1: u16 = 0, + + comptime { + std.debug.assert( + @sizeOf(@This()) == @sizeOf(u32) and + @bitSizeOf(@This()) == @bitSizeOf(u32), + ); + } }; -pub const ColorWriteMask = enum(u32) { - none = 0x00000000, - red = 0x00000001, - green = 0x00000002, - blue = 0x00000004, - alpha = 0x00000008, - all = 0x0000000F, +pub const ColorWriteMask = packed struct { + red: bool = false, + green: bool = false, + blue: bool = false, + alpha: bool = false, + + _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) { - none = 0x00000000, - vertex = 0x00000001, - fragment = 0x00000002, - compute = 0x00000004, +pub const ShaderStage = packed struct { + vertex: bool = false, + fragment: bool = false, + compute: bool = false, + + _pad0: u5 = 0, + _pad1: u8 = 0, + _pad2: u16 = 0, + + comptime { + std.debug.assert( + @sizeOf(@This()) == @sizeOf(u32) and + @bitSizeOf(@This()) == @bitSizeOf(u32), + ); + } }; test "name" {