From 726bdf3fbafe593c0102d3eea702087d3e560d76 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Thu, 11 Aug 2022 03:44:34 -0700 Subject: [PATCH] gpu: adopt new dawn.json policy for optionality of slice fields, default zero values Signed-off-by: Stephen Gutekanst --- gpu/README.md | 7 +++--- gpu/src/bind_group.zig | 11 ++++----- gpu/src/bind_group_layout.zig | 5 ++-- gpu/src/compute_pipeline.zig | 2 +- gpu/src/dawn.zig | 5 ++-- gpu/src/device.zig | 4 +-- gpu/src/external_texture.zig | 4 +-- gpu/src/pipeline_layout.zig | 5 ++-- gpu/src/query_set.zig | 1 - gpu/src/render_bundle_encoder.zig | 5 ++-- gpu/src/render_pipeline.zig | 10 ++++---- gpu/src/texture.zig | 3 +-- gpu/src/types.zig | 41 +++++++++++-------------------- 13 files changed, 40 insertions(+), 63 deletions(-) diff --git a/gpu/README.md b/gpu/README.md index d29d2fd2..c56af44a 100644 --- a/gpu/README.md +++ b/gpu/README.md @@ -140,11 +140,10 @@ We make the following quality of life improvements. TODO: explain it -### Nullability +### Optionality & nullability -* `label: ?[*:0]const u8` fields have a default `null` value added to them. -* Where a struct has a slice `_count` field, with an optional pointer, if the `_count` field defaults to zero we also enforce the optional pointer defaults to `null`. Specifically we do this for: -* `next_in_chain: *const ChainedStruct` fields, which enable optional implementation-specific extensions to the WebGPU API, default to `null`. +* Optional values default to their zero value (either `null` or a struct constructor `.{}`) when specified as `optional` in `dawn.json`. This means things like `label`, `next_in_chain`, etc. do not need to be specified. +* Fields representing a slice with a `_count` field are nullable pointers defaulting to null and 0 by default. ### Slice helpers diff --git a/gpu/src/bind_group.zig b/gpu/src/bind_group.zig index 414b614d..11da0a73 100644 --- a/gpu/src/bind_group.zig +++ b/gpu/src/bind_group.zig @@ -9,20 +9,19 @@ pub const BindGroup = opaque { pub const Entry = extern struct { next_in_chain: ?*const ChainedStruct = null, binding: u32, - buffer: ?*Buffer, + buffer: ?*Buffer = null, offset: u64 = 0, size: u64, - sampler: ?*Sampler, - texture_view: ?*TextureView, + sampler: ?*Sampler = null, + texture_view: ?*TextureView = null, }; pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, layout: *BindGroupLayout, - entry_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - entries: ?[*]const Entry, + entry_count: u32 = 0, + entries: ?[*]const Entry = null, }; pub inline fn setLabel(bind_group: *BindGroup, label: [*:0]const u8) void { diff --git a/gpu/src/bind_group_layout.zig b/gpu/src/bind_group_layout.zig index 4357e02d..6a888c35 100644 --- a/gpu/src/bind_group_layout.zig +++ b/gpu/src/bind_group_layout.zig @@ -20,9 +20,8 @@ pub const BindGroupLayout = opaque { pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - entry_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - entries: ?[*]const Entry, + entry_count: u32 = 0, + entries: ?[*]const Entry = null, }; pub inline fn setLabel(bind_group_layout: *BindGroupLayout, label: [*:0]const u8) void { diff --git a/gpu/src/compute_pipeline.zig b/gpu/src/compute_pipeline.zig index 6651c9fa..964c4f56 100644 --- a/gpu/src/compute_pipeline.zig +++ b/gpu/src/compute_pipeline.zig @@ -8,7 +8,7 @@ pub const ComputePipeline = opaque { pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - layout: ?*PipelineLayout, + layout: ?*PipelineLayout = null, compute: ProgrammableStageDescriptor, }; diff --git a/gpu/src/dawn.zig b/gpu/src/dawn.zig index 995db746..99762363 100644 --- a/gpu/src/dawn.zig +++ b/gpu/src/dawn.zig @@ -17,9 +17,8 @@ pub const EncoderInternalUsageDescriptor = extern struct { /// TODO: Can be chained in gpu.Instance.Descriptor pub const InstanceDescriptor = extern struct { chain: ChainedStruct, - additional_runtime_search_paths_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - additional_runtime_search_paths: ?[*]const u8, + additional_runtime_search_paths_count: u32 = 0, + additional_runtime_search_paths: ?[*]const u8 = null, }; /// TODO: Can be chained in gpu.Texture.Descriptor diff --git a/gpu/src/device.zig b/gpu/src/device.zig index a2c5669b..eeec0f2b 100644 --- a/gpu/src/device.zig +++ b/gpu/src/device.zig @@ -45,9 +45,7 @@ pub const Device = opaque { label: ?[*:0]const u8 = null, required_features_count: u32 = 0, required_features: ?[*]const FeatureName = null, - required_limits: ?*const RequiredLimits, - // TODO: file issue on Dawn, dawn.json doesn't communicate that a zero value here is the - // acceptable default value due to internal nullability of Queue.Descriptor. + required_limits: ?*const RequiredLimits = null, default_queue: Queue.Descriptor = Queue.Descriptor{}, }; diff --git a/gpu/src/external_texture.zig b/gpu/src/external_texture.zig index 7a55f37f..6f4e2a3a 100644 --- a/gpu/src/external_texture.zig +++ b/gpu/src/external_texture.zig @@ -18,10 +18,10 @@ pub const ExternalTexture = opaque { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, plane0: *TextureView, - plane1: ?*TextureView, + plane1: ?*TextureView = null, do_yuv_to_rgb_conversion_only: bool = false, // TODO: dawn.json says length 12, does it mean array length? - yuv_to_rgb_conversion_matrix: ?[*]const f32, + yuv_to_rgb_conversion_matrix: ?[*]const f32 = null, // TODO: dawn.json says length 7, does it mean array length? src_transform_function_parameters: [*]const f32, // TODO: dawn.json says length 7, does it mean array length? diff --git a/gpu/src/pipeline_layout.zig b/gpu/src/pipeline_layout.zig index b2770c60..a5c6820a 100644 --- a/gpu/src/pipeline_layout.zig +++ b/gpu/src/pipeline_layout.zig @@ -6,9 +6,8 @@ pub const PipelineLayout = opaque { pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - bind_group_layout_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - bind_group_layouts: ?[*]const *BindGroupLayout, + bind_group_layout_count: u32 = 0, + bind_group_layouts: ?[*]const *BindGroupLayout = null, }; pub inline fn setLabel(pipeline_layout: *PipelineLayout, label: [*:0]const u8) void { diff --git a/gpu/src/query_set.zig b/gpu/src/query_set.zig index 80fbbcbf..f076cdaf 100644 --- a/gpu/src/query_set.zig +++ b/gpu/src/query_set.zig @@ -9,7 +9,6 @@ pub const QuerySet = opaque { label: ?[*:0]const u8 = null, type: QueryType, count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. pipeline_statistics: ?[*]const PipelineStatisticName = null, pipeline_statistics_count: u32 = 0, }; diff --git a/gpu/src/render_bundle_encoder.zig b/gpu/src/render_bundle_encoder.zig index 5c857025..3ca34a63 100644 --- a/gpu/src/render_bundle_encoder.zig +++ b/gpu/src/render_bundle_encoder.zig @@ -11,9 +11,8 @@ pub const RenderBundleEncoder = opaque { pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - color_formats_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - color_formats: ?[*]const Texture.Format, + color_formats_count: u32 = 0, + color_formats: ?[*]const Texture.Format = null, depth_stencil_format: Texture.Format = .undef, sample_count: u32 = 1, depth_read_only: bool = false, diff --git a/gpu/src/render_pipeline.zig b/gpu/src/render_pipeline.zig index 380ecfba..4585b6e1 100644 --- a/gpu/src/render_pipeline.zig +++ b/gpu/src/render_pipeline.zig @@ -12,12 +12,12 @@ pub const RenderPipeline = opaque { pub const Descriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - layout: ?*PipelineLayout, + layout: ?*PipelineLayout = null, vertex: VertexState, - primitive: PrimitiveState, - depth_stencil: ?*const DepthStencilState, - multisample: MultisampleState, - fragment: ?*const FragmentState, + primitive: PrimitiveState = .{}, + depth_stencil: ?*const DepthStencilState = null, + multisample: MultisampleState = .{}, + fragment: ?*const FragmentState = null, }; pub inline fn getBindGroupLayout(render_pipeline: *RenderPipeline, group_index: u32) *BindGroupLayout { diff --git a/gpu/src/texture.zig b/gpu/src/texture.zig index b9b74118..116052fa 100644 --- a/gpu/src/texture.zig +++ b/gpu/src/texture.zig @@ -183,8 +183,7 @@ pub const Texture = opaque { mip_level_count: u32 = 1, sample_count: u32 = 1, view_format_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - view_formats: ?[*]const Format, + view_formats: ?[*]const Format = null, }; pub inline fn createView(texture: *Texture, descriptor: ?*const TextureView.Descriptor) *TextureView { diff --git a/gpu/src/types.zig b/gpu/src/types.zig index fefd2bfa..00067677 100644 --- a/gpu/src/types.zig +++ b/gpu/src/types.zig @@ -42,33 +42,27 @@ pub const RenderPassTimestampWrite = extern struct { }; pub const RequestAdapterOptions = extern struct { - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. next_in_chain: ?*const ChainedStruct = null, - compatible_surface: ?*Surface, + compatible_surface: ?*Surface = null, power_preference: PowerPreference = .undef, force_fallback_adapter: bool = false, }; pub const ComputePassDescriptor = extern struct { - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, timestamp_write_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. timestamp_writes: ?[*]const ComputePassTimestampWrite = null, }; pub const RenderPassDescriptor = extern struct { - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, - color_attachment_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - color_attachments: ?[*]const RenderPassColorAttachment, - depth_stencil_attachment: ?[*]const RenderPassDepthStencilAttachment, + color_attachment_count: u32 = 0, + color_attachments: ?[*]const RenderPassColorAttachment = null, + depth_stencil_attachment: ?[*]const RenderPassDepthStencilAttachment = null, occlusion_query_set: ?*QuerySet = null, timestamp_write_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. timestamp_writes: ?[*]const RenderPassTimestampWrite = null, }; @@ -500,7 +494,7 @@ pub const Origin3D = extern struct { pub const CompilationMessage = extern struct { next_in_chain: ?*const ChainedStruct = null, - message: ?[*:0]const u8, + message: ?[*:0]const u8 = null, type: CompilationMessageType, line_num: u64, line_pos: u64, @@ -520,11 +514,11 @@ pub const CopyTextureForBrowserOptions = extern struct { needs_color_space_conversion: bool = false, src_alpha_mode: AlphaMode = .unpremultiplied, // TODO: dawn.json says length 7, does it mean array length? - src_transfer_function_parameters: ?*const f32, + src_transfer_function_parameters: ?*const f32 = null, // TODO: dawn.json says length 9, does it mean array length? - conversion_matrix: ?*const f32, + conversion_matrix: ?*const f32 = null, // TODO: dawn.json says length 7, does it mean array length? - dst_transfer_function_parameters: ?*const f32, + dst_transfer_function_parameters: ?*const f32 = null, dst_alpha_mode: AlphaMode = .unpremultiplied, internal_usage: bool = false, }; @@ -584,8 +578,7 @@ pub const BlendState = extern struct { pub const CompilationInfo = extern struct { next_in_chain: ?*const ChainedStruct = null, message_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - messages: ?[*]const CompilationMessage, + messages: ?[*]const CompilationMessage = null, }; pub const DepthStencilState = extern struct { @@ -621,13 +614,12 @@ pub const ProgrammableStageDescriptor = extern struct { module: *ShaderModule, entry_point: [*:0]const u8, constant_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. constants: ?[*]const ConstantEntry = null, }; pub const RenderPassColorAttachment = extern struct { - view: ?*TextureView, - resolve_target: ?*TextureView, + view: ?*TextureView = null, + resolve_target: ?*TextureView = null, load_op: LoadOp, store_op: StoreOp, /// deprecated @@ -654,14 +646,13 @@ pub const VertexBufferLayout = extern struct { array_stride: u64, step_mode: VertexStepMode = .vertex, attribute_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - attributes: ?[*]const VertexAttribute, + attributes: ?[*]const VertexAttribute = null, }; pub const ColorTargetState = extern struct { next_in_chain: ?*const ChainedStruct = null, format: Texture.Format, - blend: ?*const BlendState, + blend: ?*const BlendState = null, write_mask: ColorWriteMaskFlags = ColorWriteMaskFlags.all, }; @@ -670,10 +661,8 @@ pub const VertexState = extern struct { module: *ShaderModule, entry_point: [*:0]const u8, constant_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. constants: ?[*]const ConstantEntry = null, buffer_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. buffers: ?[*]const VertexBufferLayout = null, }; @@ -682,11 +671,9 @@ pub const FragmentState = extern struct { module: *ShaderModule, entry_point: [*:0]const u8, constant_count: u32 = 0, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. constants: ?[*]const ConstantEntry = null, target_count: u32, - // TODO: file a bug on Dawn, this is not marked as nullable but in fact is. - targets: ?[*]const ColorTargetState, + targets: ?[*]const ColorTargetState = null, }; test "BackendType name" {