From 614322edc733886e95c0b67f083cc41ab51281ce Mon Sep 17 00:00:00 2001 From: Silver Date: Thu, 7 Apr 2022 03:25:58 +0100 Subject: [PATCH] gpu: convert Texture.Usage to packed struct --- gpu/src/NativeInstance.zig | 4 ++-- gpu/src/SwapChain.zig | 2 +- gpu/src/Texture.zig | 37 +++++++++++++++++++++++++++++-------- src/main.zig | 6 +++--- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index f5597fd2..09fcbaa6 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -504,7 +504,7 @@ const device_vtable = Device.VTable{ const desc = c.WGPUSwapChainDescriptor{ .nextInChain = null, .label = if (descriptor.label) |l| l else null, - .usage = @enumToInt(descriptor.usage), + .usage = @bitCast(u32, descriptor.usage), .format = @enumToInt(descriptor.format), .width = descriptor.width, .height = descriptor.height, @@ -1017,7 +1017,7 @@ const swap_chain_vtable = SwapChain.VTable{ c.wgpuSwapChainConfigure( @ptrCast(c.WGPUSwapChain, ptr), @enumToInt(format), - @enumToInt(allowed_usage), + @bitCast(u32, allowed_usage), width, height, ); diff --git a/gpu/src/SwapChain.zig b/gpu/src/SwapChain.zig index 0275f15b..a3a7db33 100644 --- a/gpu/src/SwapChain.zig +++ b/gpu/src/SwapChain.zig @@ -57,7 +57,7 @@ pub const Descriptor = struct { pub fn equal(a: *const Descriptor, b: *const Descriptor) bool { if ((a.label == null) != (b.label == null)) return false; if (a.label != null and !std.mem.eql(u8, a.label.?, b.label.?)) return false; - if (a.usage != b.usage) return false; + if (!a.usage.equal(b.usage)) return false; if (a.format != b.format) return false; if (a.width != b.width) return false; if (a.height != b.height) return false; diff --git a/gpu/src/Texture.zig b/gpu/src/Texture.zig index 1385c07f..f9489251 100644 --- a/gpu/src/Texture.zig +++ b/gpu/src/Texture.zig @@ -1,3 +1,5 @@ +const std = @import("std"); + const Extent3D = @import("data.zig").Extent3D; const TextureView = @import("TextureView.zig"); @@ -48,14 +50,33 @@ pub const Descriptor = struct { sample_count: u32, }; -pub const Usage = enum(u32) { - none = 0x00000000, - copy_src = 0x00000001, - copy_dst = 0x00000002, - texture_binding = 0x00000004, - storage_binding = 0x00000008, - render_attachment = 0x00000010, - present = 0x00000020, +pub const Usage = packed struct { + copy_src: bool = false, + copy_dst: bool = false, + texture_binding: bool = false, + storage_binding: bool = false, + render_attachment: bool = false, + present: bool = false, + + _pad0: u2 = 0, + _pad1: u8 = 0, + _pad2: u16 = 0, + + comptime { + std.debug.assert( + @sizeOf(@This()) == @sizeOf(u32) and + @bitSizeOf(@This()) == @bitSizeOf(u32), + ); + } + + pub fn equal(a: Usage, b: Usage) bool { + return a.copy_src == b.copy_src and + a.copy_dst == b.copy_dst and + a.texture_binding == b.texture_binding and + a.storage_binding == b.storage_binding and + a.render_attachment == b.render_attachment and + a.present == b.present; + } }; pub const Format = enum(u32) { diff --git a/src/main.zig b/src/main.zig index 34da037f..47567634 100644 --- a/src/main.zig +++ b/src/main.zig @@ -150,7 +150,7 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type { swap_chain_format = .bgra8_unorm; descriptor = .{ .label = "basic swap chain", - .usage = .render_attachment, + .usage = .{ .render_attachment = true }, .format = swap_chain_format, .width = framebuffer_size.width, .height = framebuffer_size.height, @@ -174,7 +174,7 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type { swap_chain_format = @intToEnum(gpu.Texture.Format, @intCast(u32, c.machUtilsBackendBinding_getPreferredSwapChainTextureFormat(binding))); swap_chain.?.configure( swap_chain_format, - .render_attachment, + .{ .render_attachment = true }, framebuffer_size.width, framebuffer_size.height, ); @@ -221,7 +221,7 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type { app.swap_chain = app.device.nativeCreateSwapChain(app.surface, &app.target_desc); } else app.swap_chain.?.configure( app.swap_chain_format, - .render_attachment, + .{ .render_attachment = true }, app.target_desc.width, app.target_desc.height, );