From ca21c4fa80b716aa2bf716014c58f0003c1aec91 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 24 Jul 2022 14:48:12 -0700 Subject: [PATCH] gpu: convert Buffer from enum(usize) to *opaque Signed-off-by: Stephen Gutekanst --- gpu/src/bind_group.zig | 2 +- gpu/src/bind_group_layout.zig | 3 +- gpu/src/buffer.zig | 120 ++++++++++++++++------------------ 3 files changed, 61 insertions(+), 64 deletions(-) diff --git a/gpu/src/bind_group.zig b/gpu/src/bind_group.zig index 038fde03..f22f6578 100644 --- a/gpu/src/bind_group.zig +++ b/gpu/src/bind_group.zig @@ -9,7 +9,7 @@ pub const BindGroup = *opaque {}; pub const BindGroupEntry = extern struct { next_in_chain: *const ChainedStruct, binding: u32, - buffer: Buffer = Buffer.none, // nullable + buffer: ?Buffer, offset: u64, size: u64, sampler: Sampler = Sampler.none, // nullable diff --git a/gpu/src/bind_group_layout.zig b/gpu/src/bind_group_layout.zig index 9eb6c095..817892af 100644 --- a/gpu/src/bind_group_layout.zig +++ b/gpu/src/bind_group_layout.zig @@ -1,6 +1,7 @@ const ChainedStruct = @import("types.zig").ChainedStruct; const ShaderStageFlags = @import("types.zig").ShaderStageFlags; const Buffer = @import("buffer.zig").Buffer; +const BufferBindingLayout = @import("buffer.zig").BufferBindingLayout; const Sampler = @import("sampler.zig").Sampler; const Texture = @import("texture.zig").Texture; const StorageTextureBindingLayout = @import("types.zig").StorageTextureBindingLayout; @@ -11,7 +12,7 @@ pub const BindGroupLayoutEntry = extern struct { next_in_chain: *const ChainedStruct, binding: u32, visibility: ShaderStageFlags, - buffer: Buffer.BindingLayout, + buffer: BufferBindingLayout, sampler: Sampler.BindingLayout, texture: Texture.BindingLayout, storage_texture: StorageTextureBindingLayout, diff --git a/gpu/src/buffer.zig b/gpu/src/buffer.zig index d6c7f210..2d2c03da 100644 --- a/gpu/src/buffer.zig +++ b/gpu/src/buffer.zig @@ -1,67 +1,63 @@ const std = @import("std"); const ChainedStruct = @import("types.zig").ChainedStruct; -pub const Buffer = enum(usize) { - _, +pub const Buffer = *opaque {}; - pub const none: Buffer = @intToEnum(Buffer, 0); - - pub const BindingType = enum(u32) { - undef = 0x00000000, - uniform = 0x00000001, - storage = 0x00000002, - read_only_storage = 0x00000003, - }; - - pub const MapAsyncStatus = enum(u32) { - success = 0x00000000, - err = 0x00000001, - unknown = 0x00000002, - device_lost = 0x00000003, - destroyed_before_callback = 0x00000004, - unmapped_before_callback = 0x00000005, - }; - - pub const Usage = 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, - - _padding: u22 = 0, - - comptime { - std.debug.assert( - @sizeOf(@This()) == @sizeOf(u32) and - @bitSizeOf(@This()) == @bitSizeOf(u32), - ); - } - - pub const none = Usage{}; - - pub fn equal(a: Usage, b: Usage) bool { - return @truncate(u10, @bitCast(u32, a)) == @truncate(u10, @bitCast(u32, b)); - } - }; - - pub const BindingLayout = extern struct { - next_in_chain: *const ChainedStruct, - type: BindingType, - has_dynamic_offset: bool = false, - min_binding_size: u64 = 0, - }; - - pub const Descriptor = extern struct { - next_in_chain: *const ChainedStruct, - label: ?[*:0]const u8 = null, - usage: Usage, - size: u64, - mapped_at_creation: bool, - }; +pub const BufferBindingType = enum(u32) { + undef = 0x00000000, + uniform = 0x00000001, + storage = 0x00000002, + read_only_storage = 0x00000003, +}; + +pub const BufferMapAsyncStatus = enum(u32) { + success = 0x00000000, + err = 0x00000001, + unknown = 0x00000002, + device_lost = 0x00000003, + destroyed_before_callback = 0x00000004, + unmapped_before_callback = 0x00000005, +}; + +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, + + _padding: u22 = 0, + + comptime { + std.debug.assert( + @sizeOf(@This()) == @sizeOf(u32) and + @bitSizeOf(@This()) == @bitSizeOf(u32), + ); + } + + pub const none = BufferUsage{}; + + pub fn equal(a: BufferUsage, b: BufferUsage) bool { + return @truncate(u10, @bitCast(u32, a)) == @truncate(u10, @bitCast(u32, b)); + } +}; + +pub const BufferBindingLayout = extern struct { + next_in_chain: *const ChainedStruct, + type: BufferBindingType, + has_dynamic_offset: bool = false, + min_binding_size: u64 = 0, +}; + +pub const BufferDescriptor = extern struct { + next_in_chain: *const ChainedStruct, + label: ?[*:0]const u8 = null, + usage: BufferUsage, + size: u64, + mapped_at_creation: bool, };