From 6df1653d398ac3f911fb5755d8994c99a787e793 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Wed, 13 Nov 2024 14:47:07 -0800 Subject: [PATCH 01/18] Initial emscripten commit --- src/wgpu.zig | 197 +++++++++++++++++++++++++++++++++++++-------------- src/zgpu.zig | 76 ++++++++++++++++++-- 2 files changed, 212 insertions(+), 61 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index 6597b63..d890b23 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const emscripten = @import("builtin").target.os.tag == .emscripten; test "extern struct ABI compatibility" { @setEvalBranchQuota(10_000); @@ -66,28 +67,56 @@ pub const BackendType = enum(u32) { opengles, }; -pub const BlendFactor = enum(u32) { - zero = 0x00000000, - one = 0x00000001, - src = 0x00000002, - one_minus_src = 0x00000003, - src_alpha = 0x00000004, - one_minus_src_alpha = 0x00000005, - dst = 0x00000006, - one_minus_dst = 0x00000007, - dst_alpha = 0x00000008, - one_minus_dst_alpha = 0x00000009, - src_alpha_saturated = 0x0000000A, - constant = 0x0000000B, - one_minus_constant = 0x0000000C, -}; - -pub const BlendOperation = enum(u32) { - add = 0x00000000, - subtract = 0x00000001, - reverse_subtract = 0x00000002, - min = 0x00000003, - max = 0x00000004, +pub const BlendFactor = switch (emscripten) { + true => enum(u32) { + undef = 0x00000000, + zero = 0x00000001, + one = 0x00000002, + src = 0x00000003, + one_minus_src = 0x00000004, + src_alpha = 0x00000005, + one_minus_src_alpha = 0x00000006, + dst = 0x00000007, + one_minus_dst = 0x00000008, + dst_alpha = 0x00000009, + one_minus_dst_alpha = 0x0000000A, + src_alpha_saturated = 0x0000000B, + constant = 0x0000000C, + one_minus_constant = 0x0000000D, + }, + false => enum(u32) { + zero = 0x00000000, + one = 0x00000001, + src = 0x00000002, + one_minus_src = 0x00000003, + src_alpha = 0x00000004, + one_minus_src_alpha = 0x00000005, + dst = 0x00000006, + one_minus_dst = 0x00000007, + dst_alpha = 0x00000008, + one_minus_dst_alpha = 0x00000009, + src_alpha_saturated = 0x0000000A, + constant = 0x0000000B, + one_minus_constant = 0x0000000C, + }, +}; + +pub const BlendOperation = switch (emscripten) { + true => enum(u32) { + undef = 0x00000000, + add = 0x00000001, + subtract = 0x00000002, + reverse_subtract = 0x00000003, + min = 0x00000008, + max = 0x00000004, + }, + false => enum(u32) { + add = 0x00000000, + subtract = 0x00000001, + reverse_subtract = 0x00000002, + min = 0x00000003, + max = 0x00000004, + }, }; pub const BufferBindingType = enum(u32) { @@ -268,12 +297,22 @@ pub const PresentMode = enum(u32) { fifo = 0x00000002, }; -pub const PrimitiveTopology = enum(u32) { - point_list = 0x00000000, - line_list = 0x00000001, - line_strip = 0x00000002, - triangle_list = 0x00000003, - triangle_strip = 0x00000004, +pub const PrimitiveTopology = switch (emscripten) { + true => enum(u32) { + undefined = 0x00000000, + point_list = 0x00000001, + line_list = 0x00000002, + line_strip = 0x00000003, + triangle_list = 0x00000004, + triangle_strip = 0x00000005, + }, + false => enum(u32) { + point_list = 0x00000000, + line_list = 0x00000001, + line_strip = 0x00000002, + triangle_list = 0x00000003, + triangle_strip = 0x00000004, + }, }; pub const QueryType = enum(u32) { @@ -410,10 +449,18 @@ pub const TextureAspect = enum(u32) { plane1_only = 0x00000004, }; -pub const TextureDimension = enum(u32) { - tdim_1d = 0x00000000, - tdim_2d = 0x00000001, - tdim_3d = 0x00000002, +pub const TextureDimension = switch (emscripten) { + true => enum(u32) { + undef = 0x00000000, + tdim_1d = 0x00000001, + tdim_2d = 0x00000002, + tdim_3d = 0x00000003, + }, + false => enum(u32) { + tdim_1d = 0x00000000, + tdim_2d = 0x00000001, + tdim_3d = 0x00000002, + }, }; pub const TextureFormat = enum(u32) { @@ -568,10 +615,18 @@ pub const VertexFormat = enum(u32) { sint32x4 = 0x0000001E, }; -pub const VertexStepMode = enum(u32) { - vertex = 0x00000000, - instance = 0x00000001, - vertex_buffer_not_used = 0x00000002, +pub const VertexStepMode = switch (emscripten) { + true => enum(u32) { + undefined = 0x00000000, + vertex_buffer_not_used = 0x00000001, + vertex = 0x00000002, + instance = 0x00000003, + }, + false => enum(u32) { + vertex = 0x00000000, + instance = 0x00000001, + vertex_buffer_not_used = 0x00000002, + }, }; pub const BufferUsage = packed struct(u32) { @@ -1059,13 +1114,24 @@ pub const Color = extern struct { a: f64, }; -pub const RenderPassColorAttachment = extern struct { - next_in_chain: ?*const ChainedStruct = null, - view: ?TextureView, - resolve_target: ?TextureView = null, - load_op: LoadOp, - store_op: StoreOp, - clear_value: Color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 }, +pub const RenderPassColorAttachment = switch (emscripten) { + true => extern struct { + next_in_chain: ?*const ChainedStruct = null, + view: ?TextureView, + depth_slice: u32 = std.math.maxInt(u32), + resolve_target: ?TextureView = null, + load_op: LoadOp, + store_op: StoreOp, + clear_value: Color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 }, + }, + false => extern struct { + next_in_chain: ?*const ChainedStruct = null, + view: ?TextureView, + resolve_target: ?TextureView = null, + load_op: LoadOp, + store_op: StoreOp, + clear_value: Color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 }, + }, }; pub const RenderPassDepthStencilAttachment = extern struct { @@ -2133,15 +2199,29 @@ pub const Queue = *opaque { callback: QueueWorkDoneCallback, userdata: ?*anyopaque, ) void { - wgpuQueueOnSubmittedWorkDone(queue, signal_value, callback, userdata); + if (emscripten) { + const oswd = @extern( + *const fn ( + queue: Queue, + callback: QueueWorkDoneCallback, + userdata: ?*anyopaque, + ) callconv(.C) void, + .{ .name = "wgpuQueueOnSubmittedWorkDone" }, + ); + oswd(queue, callback, userdata); + } else { + const oswd = @extern( + *const fn ( + queue: Queue, + signal_value: u64, + callback: QueueWorkDoneCallback, + userdata: ?*anyopaque, + ) callconv(.C) void, + .{ .name = "wgpuQueueOnSubmittedWorkDone" }, + ); + oswd(queue, signal_value, callback, userdata); + } } - extern fn wgpuQueueOnSubmittedWorkDone( - queue: Queue, - signal_value: u64, - callback: QueueWorkDoneCallback, - userdata: ?*anyopaque, - ) void; - pub fn setLabel(queue: Queue, label: ?[*:0]const u8) void { wgpuQueueSetLabel(queue, label); } @@ -2164,7 +2244,7 @@ pub const Queue = *opaque { buffer, buffer_offset, @as(*const anyopaque, @ptrCast(data.ptr)), - @as(u64, @intCast(data.len)) * @sizeOf(T), + data.len * @sizeOf(T), ); } extern fn wgpuQueueWriteBuffer( @@ -2172,7 +2252,7 @@ pub const Queue = *opaque { buffer: Buffer, buffer_offset: u64, data: *const anyopaque, - size: u64, + size: usize, ) void; pub fn writeTexture( @@ -2196,7 +2276,7 @@ pub const Queue = *opaque { queue: Queue, destination: *const ImageCopyTexture, data: *const anyopaque, - data_size: u64, + data_size: usize, data_layout: *const TextureDataLayout, write_size: *const Extent3D, ) void; @@ -2850,3 +2930,12 @@ pub const TextureView = *opaque { } extern fn wgpuTextureViewRelease(texture_view: TextureView) void; }; + +pub const InstanceDescriptor = extern struct { + next_in_chain: ?*const ChainedStruct = null, +}; +pub inline fn createInstance(desc: InstanceDescriptor) Instance { + _ = desc; + return wgpuCreateInstance(null); +} +extern fn wgpuCreateInstance(desc: ?*const InstanceDescriptor) Instance; diff --git a/src/zgpu.zig b/src/zgpu.zig index 526be6c..c8d97e8 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -11,6 +11,7 @@ const assert = std.debug.assert; const wgsl = @import("common_wgsl.zig"); const zgpu_options = @import("zgpu_options"); pub const wgpu = @import("wgpu.zig"); +const emscripten = @import("builtin").target.os.tag == .emscripten; test { _ = wgpu; @@ -117,12 +118,12 @@ pub const GraphicsContext = struct { window_provider: WindowProvider, options: GraphicsContextOptions, ) !*GraphicsContext { - dawnProcSetProcs(dnGetProcs()); + if (!emscripten) dawnProcSetProcs(dnGetProcs()); - const native_instance = dniCreate(); - errdefer dniDestroy(native_instance); + const native_instance = if (!emscripten) dniCreate(); + errdefer if (!emscripten) dniDestroy(native_instance); - const instance = dniGetWgpuInstance(native_instance).?; + const instance = if (emscripten) wgpu.createInstance(.{}) else dniGetWgpuInstance(native_instance).?; const adapter = adapter: { const Response = struct { @@ -151,6 +152,14 @@ pub const GraphicsContext = struct { @ptrCast(&response), ); + if (emscripten) { + // wait for response. requires emscripten `-sASYNC` flag + // otherwise whole api would need to be changed in a way that allows whole program to return from main and wait to js to call back + std.log.debug("wait for instance.requestAdapter...", .{}); + while (response.status == .unknown) emscripten_sleep(5); + std.log.debug("{}", .{response.status}); + } + if (response.status != .success) { std.log.err("Failed to request GPU adapter (status: {s}).", .{@tagName(response.status)}); return error.NoGraphicsAdapter; @@ -162,6 +171,17 @@ pub const GraphicsContext = struct { var properties: wgpu.AdapterProperties = undefined; properties.next_in_chain = null; adapter.getProperties(&properties); + + //webgpu updated since our dawn builds. + //getInfo is new way to do this, some errors though + //var info: wgpu.AdapterInfo = undefined; + //info.next_in_chain = null; + //adapter.getInfo(&info); + if (emscripten) { + properties.adapter_type = .unknown; + properties.backend_type = .undef; + } + std.log.info("[zgpu] High-performance device has been selected:", .{}); std.log.info("[zgpu] Name: {s}", .{properties.name}); std.log.info("[zgpu] Driver: {s}", .{properties.driver_description}); @@ -210,6 +230,14 @@ pub const GraphicsContext = struct { @ptrCast(&response), ); + if (emscripten) { + // wait for response. requires emscripten `-sASYNC` flag + // otherwise whole api would need to be changed in a way that allows whole program to return from main and wait to js to call back + std.log.debug("wait for adapter.requestDevice...", .{}); + while (response.status == .unknown) emscripten_sleep(5); + std.log.debug("{}", .{response.status}); + } + if (response.status != .success) { std.log.err("Failed to request GPU device (status: {s}).", .{@tagName(response.status)}); return error.NoGraphicsDevice; @@ -239,7 +267,7 @@ pub const GraphicsContext = struct { const gctx = try allocator.create(GraphicsContext); gctx.* = .{ .window_provider = window_provider, - .native_instance = native_instance, + .native_instance = if (emscripten) null else native_instance, .instance = instance, .device = device, .queue = device.getQueue(), @@ -294,7 +322,7 @@ pub const GraphicsContext = struct { gctx.swapchain.release(); gctx.queue.release(); gctx.device.release(); - dniDestroy(gctx.native_instance); + if (!emscripten) dniDestroy(gctx.native_instance); allocator.destroy(gctx); } @@ -464,7 +492,7 @@ pub const GraphicsContext = struct { normal_execution, swap_chain_resized, } { - gctx.swapchain.present(); + if (!emscripten) gctx.swapchain.present(); const fb_size = gctx.window_provider.getFramebufferSize(); if (gctx.swapchain_descriptor.width != fb_size[0] or @@ -1074,6 +1102,8 @@ extern fn dnGetProcs() DawnProcsTable; // Defined in Dawn codebase extern fn dawnProcSetProcs(procs: DawnProcsTable) void; +extern fn emscripten_sleep(ms: u32) void; + /// Helper to create a buffer BindGroupLayoutEntry. pub fn bufferEntry( binding: u32, @@ -1563,6 +1593,10 @@ const SurfaceDescriptor = union(SurfaceDescriptorTag) { display: *anyopaque, surface: *anyopaque, }, + canvas_html: struct { + label: ?[*:0]const u8 = null, + selector: [*:0]const u8, + }, }; fn isLinuxDesktopLike(tag: std.Target.Os.Tag) bool { @@ -1608,6 +1642,12 @@ fn createSurfaceForWindow(instance: wgpu.Instance, window_provider: WindowProvid }, }; }, + .emscripten => SurfaceDescriptor{ + .canvas_html = .{ + .label = "basic surface", + .selector = "#canvas", // TODO: can this be somehow exposed through api? + }, + }, else => if (isLinuxDesktopLike(os_tag)) linux: { if (window_provider.getWaylandDisplay()) |wl_display| { break :linux SurfaceDescriptor{ @@ -1673,6 +1713,16 @@ fn createSurfaceForWindow(instance: wgpu.Instance, window_provider: WindowProvid .label = if (src.label) |l| l else null, }); }, + .canvas_html => |src| blk: { + var desc: wgpu.SurfaceDescriptorFromCanvasHTMLSelector = .{ + .chain = .{ .struct_type = .surface_descriptor_from_canvas_html_selector, .next = null }, + .selector = src.selector, + }; + break :blk instance.createSurface(.{ + .next_in_chain = @as(*const wgpu.ChainedStruct, @ptrCast(&desc)), + .label = if (src.label) |l| l else null, + }); + }, }; } @@ -1781,3 +1831,15 @@ fn formatToShaderFormat(format: wgpu.TextureFormat) []const u8 { else => unreachable, }; } + +usingnamespace if (emscripten) struct { + // Missing symbols + var wgpuDeviceTickWarnPrinted: bool = false; + pub export fn wgpuDeviceTick() void { + if (!wgpuDeviceTickWarnPrinted) { + std.log.warn("wgpuDeviceTick(): this fn should be avoided! RequestAnimationFrame() is advised for smooth rendering in browser.", .{}); + wgpuDeviceTickWarnPrinted = true; + } + emscripten_sleep(1); + } +} else struct {}; From 17a662f3623deafe5abca5bb7343358c29a7e30e Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Wed, 13 Nov 2024 14:48:59 -0800 Subject: [PATCH 02/18] Initial emscripten commit --- src/zgpu.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/zgpu.zig b/src/zgpu.zig index c8d97e8..ec629d2 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -516,6 +516,22 @@ pub const GraphicsContext = struct { return .normal_execution; } + pub fn canRender(gctx: *GraphicsContext) bool { + if (emscripten) { + if (gctx.uniforms.stage.buffers[gctx.uniforms.stage.current].slice == null) { + var i: u32 = 0; + while (i < gctx.uniforms.stage.num) : (i += 1) { + if (gctx.uniforms.stage.buffers[i].slice != null) { + gctx.uniforms.stage.current = i; + return true; + } + } + return false; + } + } + return true; + } + // // Resources // From 5cc17503bd431330b7cdb6cef0f52e4ba47d3f19 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Wed, 13 Nov 2024 15:26:39 -0800 Subject: [PATCH 03/18] Initial emscripten commit --- src/wgpu.zig | 2 +- src/zgpu.zig | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index d890b23..70ac0be 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -2235,7 +2235,7 @@ pub const Queue = *opaque { pub fn writeBuffer( queue: Queue, buffer: Buffer, - buffer_offset: u64, + buffer_offset: usize, comptime T: type, data: []const T, ) void { diff --git a/src/zgpu.zig b/src/zgpu.zig index ec629d2..1e43d6f 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -565,6 +565,7 @@ pub const GraphicsContext = struct { var dim = descriptor.dimension; if (dim == .undef) { dim = switch (info.dimension) { + .undef => .undef, .tdim_1d => .tvdim_1d, .tdim_2d => .tvdim_2d, .tdim_3d => .tvdim_3d, From 57cecd14b116b56b637a55b44f3fb8989fbe3427 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Wed, 13 Nov 2024 16:10:26 -0800 Subject: [PATCH 04/18] Initial emscripten commit --- src/wgpu.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index 70ac0be..c59ff09 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -2242,7 +2242,7 @@ pub const Queue = *opaque { wgpuQueueWriteBuffer( queue, buffer, - buffer_offset, + @intCast(buffer_offset), @as(*const anyopaque, @ptrCast(data.ptr)), data.len * @sizeOf(T), ); From 75fbf3a9ac0bfaac1c88c81fddd46ea91772f419 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Wed, 13 Nov 2024 19:12:31 -0800 Subject: [PATCH 05/18] Initial emscripten commit --- src/zgpu.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/zgpu.zig b/src/zgpu.zig index 1e43d6f..565c35b 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -11,6 +11,7 @@ const assert = std.debug.assert; const wgsl = @import("common_wgsl.zig"); const zgpu_options = @import("zgpu_options"); pub const wgpu = @import("wgpu.zig"); +pub const slog = std.log.scoped(.zgpu); // scoped log that can be comptime processed in main logger const emscripten = @import("builtin").target.os.tag == .emscripten; test { From 6c758f66fb3eb48ae8d0c2cb5f715824f0a700bd Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Mon, 18 Nov 2024 10:58:39 -0800 Subject: [PATCH 06/18] Initial emscripten commit --- src/zgpu.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/zgpu.zig b/src/zgpu.zig index 565c35b..c2fad4b 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -1335,7 +1335,7 @@ pub fn imageInfoToTextureFormat(num_components: u32, bytes_per_component: u32, i pub const BufferInfo = struct { gpuobj: ?wgpu.Buffer = null, - size: usize = 0, + size: u64 = 0, usage: wgpu.BufferUsage = .{}, }; @@ -1589,6 +1589,7 @@ const SurfaceDescriptorTag = enum { windows_hwnd, xlib, wayland, + canvas_html, }; const SurfaceDescriptor = union(SurfaceDescriptorTag) { From c944f0e49c6eb0d9ab4203f2b12471f225f4e7e8 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Tue, 19 Nov 2024 14:32:52 -0800 Subject: [PATCH 07/18] Initial emscripten commit --- src/wgpu.zig | 15 +++++++++++---- src/zgpu.zig | 8 ++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index c59ff09..ec5539c 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -291,10 +291,17 @@ pub const PowerPreference = enum(u32) { high_performance = 0x00000002, }; -pub const PresentMode = enum(u32) { - immediate = 0x00000000, - mailbox = 0x00000001, - fifo = 0x00000002, +pub const PresentMode = switch (emscripten) { + true => enum(u32) { + fifo = 0x00000001, + immediate = 0x00000003, + mailbox = 0x00000004, + }, + false => enum(u32) { + immediate = 0x00000000, + mailbox = 0x00000001, + fifo = 0x00000002, + }, }; pub const PrimitiveTopology = switch (emscripten) { diff --git a/src/zgpu.zig b/src/zgpu.zig index c2fad4b..22368d0 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -173,16 +173,12 @@ pub const GraphicsContext = struct { properties.next_in_chain = null; adapter.getProperties(&properties); - //webgpu updated since our dawn builds. - //getInfo is new way to do this, some errors though - //var info: wgpu.AdapterInfo = undefined; - //info.next_in_chain = null; - //adapter.getInfo(&info); if (emscripten) { + properties.name = "emscripten"; + properties.driver_description = "emscripten"; properties.adapter_type = .unknown; properties.backend_type = .undef; } - std.log.info("[zgpu] High-performance device has been selected:", .{}); std.log.info("[zgpu] Name: {s}", .{properties.name}); std.log.info("[zgpu] Driver: {s}", .{properties.driver_description}); From 43d16c8d65117fd6ade653207c6a77c686ec99bb Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Tue, 19 Nov 2024 15:16:55 -0800 Subject: [PATCH 08/18] Emscripten takes care of this --- src/zgpu.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/zgpu.zig b/src/zgpu.zig index 22368d0..ea4c271 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -560,9 +560,8 @@ pub const GraphicsContext = struct { const texture = gctx.lookupResource(texture_handle).?; const info = gctx.lookupResourceInfo(texture_handle).?; var dim = descriptor.dimension; - if (dim == .undef) { + if (!emscripten and dim == .undef) { dim = switch (info.dimension) { - .undef => .undef, .tdim_1d => .tvdim_1d, .tdim_2d => .tvdim_2d, .tdim_3d => .tvdim_3d, From c0a150087415a12192d2380b784af7b48ca37732 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Tue, 19 Nov 2024 15:42:34 -0800 Subject: [PATCH 09/18] Initial emscripten commit --- src/wgpu.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index ec5539c..79210f0 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -1526,10 +1526,10 @@ pub const CommandEncoder = *opaque { pub fn copyBufferToBuffer( command_encoder: CommandEncoder, source: Buffer, - source_offset: usize, + source_offset: u64, destination: Buffer, - destination_offset: usize, - size: usize, + destination_offset: u64, + size: u64, ) void { wgpuCommandEncoderCopyBufferToBuffer( command_encoder, @@ -1543,10 +1543,10 @@ pub const CommandEncoder = *opaque { extern fn wgpuCommandEncoderCopyBufferToBuffer( command_encoder: CommandEncoder, source: Buffer, - source_offset: usize, + source_offset: u64, destination: Buffer, - destination_offset: usize, - size: usize, + destination_offset: u64, + size: u64, ) void; pub fn copyBufferToTexture( From a7b8b3e800645863d6779fe471e3405a8846f20b Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 29 Nov 2024 14:55:26 -0700 Subject: [PATCH 10/18] Emscripten uses u32 as bools --- src/wgpu.zig | 4 ++-- src/zgpu.zig | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index 79210f0..b3b246f 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -727,7 +727,7 @@ pub const BindGroupDescriptor = extern struct { pub const BufferBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, binding_type: BufferBindingType = .uniform, - has_dynamic_offset: bool = false, + has_dynamic_offset: u32 = 0, min_binding_size: u64 = 0, }; @@ -1150,7 +1150,7 @@ pub const RenderPassDepthStencilAttachment = extern struct { stencil_load_op: LoadOp = .undef, stencil_store_op: StoreOp = .undef, stencil_clear_value: u32 = 0, - stencil_read_only: bool = false, + stencil_read_only: u32 = 0, }; pub const RenderPassDescriptor = extern struct { diff --git a/src/zgpu.zig b/src/zgpu.zig index ea4c271..85c35c1 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -1122,7 +1122,7 @@ pub fn bufferEntry( binding: u32, visibility: wgpu.ShaderStage, binding_type: wgpu.BufferBindingType, - has_dynamic_offset: bool, + has_dynamic_offset: u32, min_binding_size: u64, ) wgpu.BindGroupLayoutEntry { return .{ From aee888e6afbbef418da8e3a810f584de6e201004 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 29 Nov 2024 15:02:34 -0700 Subject: [PATCH 11/18] Emscripten bool --- src/wgpu.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index b3b246f..a37482d 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -1146,7 +1146,7 @@ pub const RenderPassDepthStencilAttachment = extern struct { depth_load_op: LoadOp = .undef, depth_store_op: StoreOp = .undef, depth_clear_value: f32 = 0.0, - depth_read_only: bool = false, + depth_read_only: u32 = 0, stencil_load_op: LoadOp = .undef, stencil_store_op: StoreOp = .undef, stencil_clear_value: u32 = 0, From ab320d44516fceefd9ddfc5b3a169be3fb3e477c Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 29 Nov 2024 17:22:31 -0700 Subject: [PATCH 12/18] Convert dynamic offset to u32 --- src/zgpu.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/zgpu.zig b/src/zgpu.zig index 85c35c1..889e54a 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -1122,15 +1122,19 @@ pub fn bufferEntry( binding: u32, visibility: wgpu.ShaderStage, binding_type: wgpu.BufferBindingType, - has_dynamic_offset: u32, + has_dynamic_offset: bool, min_binding_size: u64, ) wgpu.BindGroupLayoutEntry { + const emscripten_bool: u32 = switch (has_dynamic_offset) { + true => 1, + false => 0, + }; return .{ .binding = binding, .visibility = visibility, .buffer = .{ .binding_type = binding_type, - .has_dynamic_offset = has_dynamic_offset, + .has_dynamic_offset = emscripten_bool, .min_binding_size = min_binding_size, }, }; From 277109796f6902f623b40ba9928f43c1f9f54017 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 29 Nov 2024 17:27:42 -0700 Subject: [PATCH 13/18] Simplify --- src/zgpu.zig | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/zgpu.zig b/src/zgpu.zig index 889e54a..256a9e4 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -1125,16 +1125,15 @@ pub fn bufferEntry( has_dynamic_offset: bool, min_binding_size: u64, ) wgpu.BindGroupLayoutEntry { - const emscripten_bool: u32 = switch (has_dynamic_offset) { - true => 1, - false => 0, - }; return .{ .binding = binding, .visibility = visibility, .buffer = .{ .binding_type = binding_type, - .has_dynamic_offset = emscripten_bool, + .has_dynamic_offset = switch (has_dynamic_offset) { + true => 1, + false => 0, + }, .min_binding_size = min_binding_size, }, }; From 4e03f91d7831fcf25c4b4c28e77f4519f564e23a Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 1 Dec 2024 15:02:41 -0800 Subject: [PATCH 14/18] BufferMapState was updated by webgpu --- src/wgpu.zig | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index a37482d..d3ec1eb 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -138,10 +138,17 @@ pub const BufferMapAsyncStatus = enum(u32) { size_out_of_range = 0x00000008, }; -pub const BufferMapState = enum(u32) { - unmapped = 0x00000000, - pending = 0x00000001, - mapped = 0x00000002, +pub const BufferMapState = switch (emscripten) { + true => enum(u32) { + unmapped = 0x00000001, + pending = 0x00000002, + mapped = 0x00000003, + }, + false => enum(u32) { + unmapped = 0x00000000, + pending = 0x00000001, + mapped = 0x00000002, + }, }; pub const CompareFunction = enum(u32) { From fb9e8d3bc34d1ed4f5b8d0db1fbd6feea5f298cb Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 1 Dec 2024 15:09:04 -0800 Subject: [PATCH 15/18] More WebGPU Bools --- src/wgpu.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index d3ec1eb..167b200 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -779,7 +779,7 @@ pub const BufferDescriptor = extern struct { label: ?[*:0]const u8 = null, usage: BufferUsage, size: u64, - mapped_at_creation: bool = false, + mapped_at_creation: u32 = 0, }; pub const CommandEncoderDescriptor = extern struct { From 3c89073cafff5e08668f7b81f99b9af458329ddb Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 1 Dec 2024 15:21:01 -0800 Subject: [PATCH 16/18] Create U32Bool --- src/wgpu.zig | 13 +++++++++---- src/zgpu.zig | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/wgpu.zig b/src/wgpu.zig index 167b200..3788528 100644 --- a/src/wgpu.zig +++ b/src/wgpu.zig @@ -734,7 +734,7 @@ pub const BindGroupDescriptor = extern struct { pub const BufferBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, binding_type: BufferBindingType = .uniform, - has_dynamic_offset: u32 = 0, + has_dynamic_offset: U32Bool = .false, min_binding_size: u64 = 0, }; @@ -774,12 +774,17 @@ pub const BindGroupLayoutDescriptor = extern struct { entries: ?[*]const BindGroupLayoutEntry, }; +pub const U32Bool = enum(u32) { + false = 0, + true = 1, +}; + pub const BufferDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, usage: BufferUsage, size: u64, - mapped_at_creation: u32 = 0, + mapped_at_creation: U32Bool = .false, }; pub const CommandEncoderDescriptor = extern struct { @@ -1153,11 +1158,11 @@ pub const RenderPassDepthStencilAttachment = extern struct { depth_load_op: LoadOp = .undef, depth_store_op: StoreOp = .undef, depth_clear_value: f32 = 0.0, - depth_read_only: u32 = 0, + depth_read_only: U32Bool = .false, stencil_load_op: LoadOp = .undef, stencil_store_op: StoreOp = .undef, stencil_clear_value: u32 = 0, - stencil_read_only: u32 = 0, + stencil_read_only: U32Bool = .false, }; pub const RenderPassDescriptor = extern struct { diff --git a/src/zgpu.zig b/src/zgpu.zig index 256a9e4..5078005 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -426,7 +426,7 @@ pub const GraphicsContext = struct { const buffer_handle = gctx.createBuffer(.{ .usage = .{ .copy_src = true, .map_write = true }, .size = uniforms_buffer_size, - .mapped_at_creation = true, + .mapped_at_creation = .true, }); // Add new (mapped) staging buffer to the buffer list. From 43562d7017312abc5e6d2ec95adb0c19282c0927 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 1 Dec 2024 15:24:00 -0800 Subject: [PATCH 17/18] Update dynamic offset --- src/zgpu.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zgpu.zig b/src/zgpu.zig index 5078005..e735371 100644 --- a/src/zgpu.zig +++ b/src/zgpu.zig @@ -1131,8 +1131,8 @@ pub fn bufferEntry( .buffer = .{ .binding_type = binding_type, .has_dynamic_offset = switch (has_dynamic_offset) { - true => 1, - false => 0, + true => .true, + false => .false, }, .min_binding_size = min_binding_size, }, From 1c93b0a03a7d6de4163669d5f219b6b9d18e69e9 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 15:36:59 -0800 Subject: [PATCH 18/18] Don't link Emscripten builds --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f5caa71..6d9a6a0 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,10 @@ pub fn build(b: *std.Build) void { const zgpu = b.dependency("zgpu", .{}); exe.root_module.addImport("zgpu", zgpu.module("root")); - exe.linkLibrary(zgpu.artifact("zdawn")); + + if (target.result.os.tag != .emscripten) { + exe.linkLibrary(zgpu.artifact("zdawn")); + } } ```