From 9ed6f6ca8be0e0b5dce008256561655589f1dd3e Mon Sep 17 00:00:00 2001 From: Michal Ziulek Date: Sun, 10 Apr 2022 23:28:44 +0200 Subject: [PATCH] Added helper functions to BindGroupLayout.Entry (#213) * gpu: Added helper functions to BindGroupLayout.Entry * gpu: Changed default values for *.BindingLayout structures. Added comments for helpers. --- gpu/src/BindGroupLayout.zig | 86 +++++++++++++++++++++++++++++++++++-- gpu/src/Buffer.zig | 6 +-- gpu/src/Sampler.zig | 2 +- gpu/src/Texture.zig | 6 +-- gpu/src/structs.zig | 4 +- 5 files changed, 91 insertions(+), 13 deletions(-) diff --git a/gpu/src/BindGroupLayout.zig b/gpu/src/BindGroupLayout.zig index 98f090a2..d06fc148 100644 --- a/gpu/src/BindGroupLayout.zig +++ b/gpu/src/BindGroupLayout.zig @@ -1,7 +1,9 @@ const Buffer = @import("Buffer.zig"); const Sampler = @import("Sampler.zig"); const Texture = @import("Texture.zig"); +const TextureView = @import("TextureView.zig"); const StorageTextureBindingLayout = @import("structs.zig").StorageTextureBindingLayout; +const StorageTextureAccess = @import("enums.zig").StorageTextureAccess; const ShaderStage = @import("enums.zig").ShaderStage; const BindGroupLayout = @This(); @@ -38,10 +40,76 @@ pub const Entry = extern struct { reserved: ?*anyopaque = null, binding: u32, visibility: ShaderStage, - buffer: Buffer.BindingLayout, - sampler: Sampler.BindingLayout, - texture: Texture.BindingLayout, - storage_texture: StorageTextureBindingLayout, + buffer: Buffer.BindingLayout = .{ .type = .none }, + sampler: Sampler.BindingLayout = .{ .type = .none }, + texture: Texture.BindingLayout = .{ .sample_type = .none }, + storage_texture: StorageTextureBindingLayout = .{ .access = .none, .format = .none }, + + /// Helper to create a buffer BindGroupLayout.Entry. + pub fn buffer( + binding: u32, + visibility: ShaderStage, + binding_type: Buffer.BindingType, + has_dynamic_offset: bool, + min_binding_size: u64, + ) Entry { + return .{ + .binding = binding, + .visibility = visibility, + .buffer = .{ + .type = binding_type, + .has_dynamic_offset = has_dynamic_offset, + .min_binding_size = min_binding_size, + }, + }; + } + + /// Helper to create a sampler BindGroupLayout.Entry. + pub fn sampler(binding: u32, visibility: ShaderStage, binding_type: Sampler.BindingType) Entry { + return .{ + .binding = binding, + .visibility = visibility, + .sampler = .{ .type = binding_type }, + }; + } + + /// Helper to create a texture BindGroupLayout.Entry. + pub fn texture( + binding: u32, + visibility: ShaderStage, + sample_type: Texture.SampleType, + view_dimension: TextureView.Dimension, + multisampled: bool, + ) Entry { + return .{ + .binding = binding, + .visibility = visibility, + .texture = .{ + .sample_type = sample_type, + .view_dimension = view_dimension, + .multisampled = multisampled, + }, + }; + } + + /// Helper to create a storage texture BindGroupLayout.Entry. + pub fn storageTexture( + binding: u32, + visibility: ShaderStage, + access: StorageTextureAccess, + format: Texture.Format, + view_dimension: TextureView.Dimension, + ) Entry { + return .{ + .binding = binding, + .visibility = visibility, + .storage_texture = .{ + .access = access, + .format = format, + .view_dimension = view_dimension, + }, + }; + } }; test { @@ -51,4 +119,14 @@ test { _ = setLabel; _ = Descriptor; _ = Entry; + + const desc = BindGroupLayout.Descriptor{ + .entries = &.{ + BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0), + BindGroupLayout.Entry.sampler(1, .{ .vertex = true }, .filtering), + BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false), + BindGroupLayout.Entry.storageTexture(3, .{ .fragment = true }, .none, .rgba32_float, .dimension_2d), + }, + }; + _ = desc; } diff --git a/gpu/src/Buffer.zig b/gpu/src/Buffer.zig index c02197c7..883e0b98 100644 --- a/gpu/src/Buffer.zig +++ b/gpu/src/Buffer.zig @@ -103,9 +103,9 @@ pub const BindingType = enum(u32) { pub const BindingLayout = extern struct { reserved: ?*anyopaque = null, - type: BindingType, - has_dynamic_offset: bool, - min_binding_size: u64, + type: BindingType = .uniform, + has_dynamic_offset: bool = false, + min_binding_size: u64 = 0, }; pub const MapAsyncStatus = enum(u32) { diff --git a/gpu/src/Sampler.zig b/gpu/src/Sampler.zig index 4d5dfde5..2334a255 100644 --- a/gpu/src/Sampler.zig +++ b/gpu/src/Sampler.zig @@ -36,7 +36,7 @@ pub const BindingType = enum(u32) { pub const BindingLayout = extern struct { reserved: ?*anyopaque = null, - type: BindingType, + type: BindingType = .filtering, }; pub const Descriptor = extern struct { diff --git a/gpu/src/Texture.zig b/gpu/src/Texture.zig index f9489251..fc98f02e 100644 --- a/gpu/src/Texture.zig +++ b/gpu/src/Texture.zig @@ -211,9 +211,9 @@ pub const SampleType = enum(u32) { pub const BindingLayout = extern struct { reserved: ?*anyopaque = null, - sample_type: SampleType, - view_dimension: TextureView.Dimension, - multisampled: bool, + sample_type: SampleType = .float, + view_dimension: TextureView.Dimension = .dimension_2d, + multisampled: bool = false, }; pub const DataLayout = extern struct { diff --git a/gpu/src/structs.zig b/gpu/src/structs.zig index 5267f54d..ce8f3286 100644 --- a/gpu/src/structs.zig +++ b/gpu/src/structs.zig @@ -39,9 +39,9 @@ pub const PrimitiveState = struct { pub const StorageTextureBindingLayout = extern struct { reserved: ?*anyopaque = null, - access: StorageTextureAccess, + access: StorageTextureAccess = .write_only, format: Texture.Format, - view_dimension: TextureView.Dimension, + view_dimension: TextureView.Dimension = .dimension_2d, }; pub const DepthStencilState = struct {