Unnamed: 0
int64
0
0
repo_id
stringlengths
5
186
file_path
stringlengths
15
223
content
stringlengths
1
32.8M
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/compute/consumer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_consumers) { return; } // User removed producer this consumer was targeting if (consumers[index].producer_id >= i32(stats.num_producers)) { search_for_producer(index); } let c = consumers[index]; let moving_rate = consumer_params.moving_rate; let max_demand_rate = consumer_params.max_demand_rate; let income = consumer_params.income; consumers[index].position[0] += c.step_size[0]; consumers[index].position[1] += c.step_size[1]; if (c.balance + income < c.max_balance) { consumers[index].balance += income; } let dist = abs(c.position - c.destination); let at_destination = all(dist.xy <= vec2<f32>(0.1)); if (at_destination) { consumers[index].step_size = vec2<f32>(0); consumers[index].position = c.destination; let at_home = all(c.destination == c.home); if (at_home) { if (c.inventory >= u32(1)) { consumers[index].inventory -= u32(1); return; } consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); let demand_rate = min(c.balance, max_demand_rate); if (demand_rate > 0) { search_for_producer(index); } return; } // At Producer let pid = c.producer_id; let max_consumer_can_buy = c.balance / producers[pid].price; let demand_rate = min(max_consumer_can_buy, max_demand_rate); let old_val = atomicSub(&producers[pid].inventory, i32(demand_rate)); // Went negative, revert inventory if (i32(demand_rate) > old_val) { atomicAdd(&producers[pid].inventory, i32(demand_rate)); return; } consumers[index].color = vec4(0.0, 1.0, 0.0, 0.0); consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, moving_rate); consumers[index].inventory += demand_rate; consumers[index].balance -= demand_rate * producers[pid].price; consumers[index].producer_id = -1; stats.transactions += u32(1); } } fn search_for_producer(index: u32){ let c = consumers[index]; let moving_rate = consumer_params.moving_rate; var pid = find_nearest_stocked_producer(c, index); if (pid == -1) { consumers[index].destination = c.home; consumers[index].step_size = step_sizes( c.position.xy, c.home.xy, moving_rate, ); return; } let p_pos = producers[pid].home; consumers[index].destination = p_pos; consumers[index].step_size = step_sizes(c.position.xy, p_pos.xy, moving_rate); consumers[index].producer_id = pid; } // Returns the pid of nearest stocked producer, -1 for failure fn find_nearest_stocked_producer(c: Consumer, index: u32) -> i32 { var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); var shortest_distance = 100000.0; var pid: i32 = -1; for(var i: u32 = 0; i < stats.num_producers; i++){ let dist = distance(c.home, producers[i].home); let inventory = u32(atomicLoad(&producers[i].inventory)); let demand_rate = min(c.balance, consumer_params.max_demand_rate); if (dist < shortest_distance && inventory > demand_rate) { shortest_distance = dist; pid = i32(i); } } return pid; }
0
repos/simulations/src
repos/simulations/src/editor/camera.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zmath = @import("zmath"); // Camera Settings pub const POS: [3]f32 = .{ 0.0, 0.0, -3000.0 }; pub const FOCUS: [3]f32 = .{ 0.0, 0.0, 0.0 }; pub const UP: [4]f32 = .{ 0.0, 1.0, 0.0, 0.0 }; pub const FOV_Y: f32 = 0.22 * std.math.pi; pub const NEAR_PLANE: f32 = 0.01; pub const FAR_PLANE: f32 = 3000.0; // Grid limits for absolute positions (without aspect ratio) pub const MAX_X: i32 = 1000; pub const MIN_X: i32 = -1000; pub const MAX_Y: i32 = 1000; pub const MIN_Y: i32 = -1000; pub const TOTAL_X: i32 = 2000; pub const TOTAL_Y: i32 = 2000; // Viewport size relative to total window size pub const VP_X_SIZE: f32 = 0.75; pub const VP_Y_SIZE: f32 = 0.75; pub fn getViewportPixelSize(gctx: *zgpu.GraphicsContext) [2]f32 { return .{ @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)) * VP_X_SIZE, @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)) * VP_Y_SIZE, }; } pub fn getAspectRatio(gctx: *zgpu.GraphicsContext) f32 { const sd = gctx.swapchain_descriptor; return @as(f32, @floatFromInt(sd.width)) / @as(f32, @floatFromInt(sd.height)); } // Given a world position (grid position with aspect), return grid position pub fn getGridFromWorld(gctx: *zgpu.GraphicsContext, world_pos: [2]f32) [2]i32 { const aspect = getAspectRatio(gctx); return .{ @as(i32, @intFromFloat(world_pos[0] / aspect)), @as(i32, @intFromFloat(world_pos[1])), // world_pos[2], // world_pos[3], }; } // Given a grid position, return a world position pub fn getWorldPosition(gctx: *zgpu.GraphicsContext, grid_pos: [4]i32) [4]f32 { const aspect = getAspectRatio(gctx); return .{ @as(f32, @floatFromInt(grid_pos[0])) * aspect, @as(f32, @floatFromInt(grid_pos[1])), @as(f32, @floatFromInt(grid_pos[2])), @as(f32, @floatFromInt(grid_pos[3])), }; } // Given a grid position, return a pixel position pub fn getPixelPosition(gctx: *zgpu.GraphicsContext, g_pos: [2]i32) [2]f32 { const grid_pos = .{ g_pos[0], g_pos[1], 1, 1 }; const world_pos = zmath.loadArr4(getWorldPosition(gctx, grid_pos)); const camera_pos = zmath.mul(world_pos, getObjectToClipMat(gctx)); const rel_pos = [4]f32{ camera_pos[0] / -POS[2], camera_pos[1] / -POS[2], 0, 1 }; const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const cursor_in_vp_x = ((rel_pos[0] + 1) * viewport_size[0]) / 2; const cursor_in_vp_y = ((-rel_pos[1] + 1) * viewport_size[1]) / 2; return .{ cursor_in_vp_x + xOffset, cursor_in_vp_y }; } // Given a pixel position, return a grid position pub fn getGridPosition(gctx: *zgpu.GraphicsContext, p_pos: [2]f32) [2]i32 { const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const rel_pos_x = (((p_pos[0] - xOffset) * 2) / viewport_size[0]) - 1; const rel_pos_y = ((p_pos[1] * 2) / viewport_size[1]) - 1; const camera_pos = zmath.loadArr4(.{ rel_pos_x * -POS[2], rel_pos_y * POS[2], 1, 1, }); const inverse_mat = zmath.inverse(getObjectToClipMat(gctx)); const world_pos = zmath.mul(camera_pos, inverse_mat); return getGridFromWorld(gctx, .{ world_pos[0], world_pos[1] }); } pub fn getObjectToClipMat(gctx: *zgpu.GraphicsContext) zmath.Mat { const camWorldToView = zmath.lookAtLh( zmath.loadArr3(POS), zmath.loadArr3(FOCUS), zmath.loadArr4(UP), ); const camViewToClip = zmath.perspectiveFovLh( FOV_Y, getAspectRatio(gctx), NEAR_PLANE, FAR_PLANE, ); const camWorldToClip = zmath.mul(camWorldToView, camViewToClip); // return zmath.transpose(camWorldToClip); return camWorldToClip; }
0
repos/simulations/src
repos/simulations/src/editor/consumer_hover.zig
const std = @import("std"); const zgpu = @import("zgpu"); const Wgpu = @import("wgpu.zig"); const Consumer = @import("consumer.zig"); const Self = @This(); absolute_home: [4]i32, home: [4]f32, color: [4]f32 = .{ 0, 0, 0, 0 }, grouping_id: u32 = 0, pub const z_pos = 0; pub fn create(args: Consumer.Args) Self { return Self{ .absolute_home = .{ args.absolute_home[0], args.absolute_home[1], z_pos, 1 }, .home = .{ args.home[0], args.home[1], z_pos, 1 }, .grouping_id = args.grouping_id, }; } pub const AppendArgs = struct { args: Consumer.Args, buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { var hovers: [1]Self = .{ create(args.args), }; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.buf.list.items.len)), .buf = args.buf.buf, .structs = hovers[0..], }); args.buf.list.append(hovers[0]) catch unreachable; } pub fn highlightConsumers( gctx: *zgpu.GraphicsContext, gui_id: usize, obj_buf: *Wgpu.ObjectBuffer(Self), ) void { for (obj_buf.list.items, 0..) |h, i| { if (gui_id == h.grouping_id) { Wgpu.writeToObjectBuffer(gctx, Self, [4]f32, "color", .{ .obj_buf = obj_buf.*, .index = i, .value = .{ 0, 0.5, 1, 0 }, }); } } } pub fn clearHover( gctx: *zgpu.GraphicsContext, obj_buf: *Wgpu.ObjectBuffer(Self), ) void { for (obj_buf.list.items, 0..) |_, i| { Wgpu.writeToObjectBuffer(gctx, Self, [4]f32, "color", .{ .obj_buf = obj_buf.*, .index = i, .value = .{ 0, 0, 0, 0 }, }); } }
0
repos/simulations/src
repos/simulations/src/editor/main.zig
const std = @import("std"); const math = std.math; const zglfw = @import("zglfw"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const zgui = @import("zgui"); const zm = @import("zmath"); const zstbi = @import("zstbi"); const Statistics = @import("statistics.zig"); const Gui = @import("gui.zig"); const Wgpu = @import("wgpu.zig"); const config = @import("config.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const ConsumerHover = @import("consumer_hover.zig"); const Image = @import("images.zig"); const Camera = @import("camera.zig"); const Square = @import("square.zig"); const Circle = @import("circle.zig"); const Mouse = @import("mouse.zig"); const Hover = @import("hover.zig"); const Popups = @import("popups.zig"); pub const NUM_CONSUMER_SIDES: u32 = 80; pub const MAX_NUM_AGENTS: u32 = Wgpu.MAX_NUM_STRUCTS; pub const MAX_NUM_PRODUCERS: u32 = 100; pub const MAX_NUM_CONSUMERS: u32 = Wgpu.MAX_NUM_STRUCTS; pub const PRODUCER_WIDTH: u32 = 40; pub const DemoState = struct { gctx: *zgpu.GraphicsContext, window: *zglfw.Window, allocator: std.mem.Allocator = undefined, running: bool = false, push_clear: bool = false, push_coord_update: bool = false, content_scale: f32, gui: Gui.State, mouse: Mouse.MouseButton = .{}, popups: Popups, render_pipelines: struct { circle: zgpu.RenderPipelineHandle, consumer_hover: zgpu.RenderPipelineHandle, hover: zgpu.RenderPipelineHandle, square: zgpu.RenderPipelineHandle, }, compute_pipelines: struct { consumer: zgpu.ComputePipelineHandle, producer: zgpu.ComputePipelineHandle, }, bind_groups: struct { render: zgpu.BindGroupHandle, compute: zgpu.BindGroupHandle, }, buffers: struct { data: struct { consumers: Wgpu.ObjectBuffer(Consumer), consumer_hovers: Wgpu.ObjectBuffer(ConsumerHover), hover: zgpu.BufferHandle, producers: Wgpu.ObjectBuffer(Producer), stats: Wgpu.ObjectBuffer(u32), }, index: struct { circle: zgpu.BufferHandle, }, vertex: struct { circle: zgpu.BufferHandle, hover: zgpu.BufferHandle, square: zgpu.BufferHandle, }, }, depth_texture: zgpu.TextureHandle, depth_texture_view: zgpu.TextureViewHandle, params: Parameters, stats: Statistics, }; const Parameters = struct { max_num_producers: u32 = 100, max_num_consumers: u32 = 10000, max_num_stats: u32 = 3, production_rate: u32 = 300, demand_rate: u32 = 100, max_inventory: u32 = 10000, moving_rate: f32 = 5.0, consumer_radius: f32 = 20.0, num_consumer_sides: u32 = 20, hover_radius: f32 = 70.0, aspect: f32, }; pub fn init(gctx: *zgpu.GraphicsContext, allocator: std.mem.Allocator, window: *zglfw.Window) !DemoState { const params = Parameters{ .aspect = Camera.getAspectRatio(gctx) }; const hover_buffer = Hover.initBuffer(gctx); const consumer_object = Wgpu.createObjectBuffer( allocator, gctx, Consumer, MAX_NUM_CONSUMERS, 0, ); const consumer_hover_object = Wgpu.createObjectBuffer( allocator, gctx, ConsumerHover, MAX_NUM_CONSUMERS, 0, ); const producer_object = Wgpu.createObjectBuffer( allocator, gctx, Producer, MAX_NUM_PRODUCERS, 0, ); const stats_object = Wgpu.createObjectBuffer( allocator, gctx, u32, Statistics.NUM_STATS, Statistics.NUM_STATS, ); const compute_bind_group = Wgpu.createComputeBindGroup(gctx, .{ .consumer = consumer_object.buf, .producer = producer_object.buf, .stats = stats_object.buf, }); Statistics.setNum(gctx, .{ .stat_obj = stats_object, .num = 0, .param = .consumers, }); Statistics.setNum(gctx, .{ .stat_obj = stats_object, .num = 0, .param = .producers, }); Statistics.setNum(gctx, .{ .stat_obj = stats_object, .num = 0, .param = .consumers, }); const depth = Wgpu.createDepthTexture(gctx); const gui_state = Gui.State{ .consumer = try Image.createTextureView( gctx, "content/pngs/consumer.png", ), .consumers = try Image.createTextureView( gctx, "content/pngs/consumerBrush.png", ), .producer = try Image.createTextureView( gctx, "content/pngs/producer.png", ), }; return DemoState{ .gctx = gctx, .window = window, .allocator = allocator, .gui = gui_state, .content_scale = getContentScale(window), .render_pipelines = .{ .circle = Wgpu.createRenderPipeline(gctx, config.cpi), .consumer_hover = Wgpu.createRenderPipeline(gctx, config.chpi), .hover = Wgpu.createRenderPipeline(gctx, config.hpi), .square = Wgpu.createRenderPipeline(gctx, config.ppi), }, .compute_pipelines = .{ .producer = Wgpu.createComputePipeline(gctx, config.pcpi), .consumer = Wgpu.createComputePipeline(gctx, config.ccpi), }, .bind_groups = .{ .render = Wgpu.createUniformBindGroup(gctx), .compute = compute_bind_group, }, .buffers = .{ .data = .{ .consumers = consumer_object, .consumer_hovers = consumer_hover_object, .hover = hover_buffer, .producers = producer_object, .stats = stats_object, }, .index = .{ .circle = Circle.createIndexBuffer( gctx, NUM_CONSUMER_SIDES, ), }, .vertex = .{ .circle = Circle.createVertexBuffer( gctx, NUM_CONSUMER_SIDES, params.consumer_radius, ), .hover = Circle.createVertexBuffer( gctx, NUM_CONSUMER_SIDES, params.hover_radius, ), .square = Square.createVertexBuffer(gctx, 40), }, }, .depth_texture = depth.texture, .depth_texture_view = depth.view, .popups = Popups.init(allocator), .params = params, .stats = Statistics.init(allocator), }; } pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { if (demo.push_clear) clearSimulation(demo); if (demo.push_coord_update) updateAspectRatio(demo); demo.mouse.update(demo); Gui.update(demo, selection_gui); } pub fn draw(demo: *DemoState) void { const gctx = demo.gctx; const cam_world_to_clip = Camera.getObjectToClipMat(gctx); const back_buffer_view = gctx.swapchain.getCurrentTextureView(); defer back_buffer_view.release(); const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); const data = demo.buffers.data; const num_consumers = @as(u32, @intCast(data.consumers.list.items.len)); const num_producers = @as(u32, @intCast(data.producers.list.items.len)); const num_consumer_hovers = @as(u32, @intCast(data.consumer_hovers.list.items.len)); // Compute shaders if (demo.running) { pass: { const pcp = gctx.lookupResource(demo.compute_pipelines.producer) orelse break :pass; const ccp = gctx.lookupResource(demo.compute_pipelines.consumer) orelse break :pass; const bg = gctx.lookupResource(demo.bind_groups.compute) orelse break :pass; const pass = encoder.beginComputePass(null); defer { pass.end(); pass.release(); } pass.setBindGroup(0, bg, &.{}); pass.setPipeline(pcp); pass.dispatchWorkgroups(@divFloor(num_producers, 64) + 1, 1, 1); pass.setPipeline(ccp); pass.dispatchWorkgroups(@divFloor(num_consumers, 64) + 1, 1, 1); } } // Copy data to mapped buffers so we can retrieve it on demand pass: { if (!demo.buffers.data.stats.mapping.waiting) { const s = gctx.lookupResource(data.stats.buf) orelse break :pass; const s_info = gctx.lookupResourceInfo(data.stats.buf) orelse break :pass; const sm = gctx.lookupResource(data.stats.mapping.buf) orelse break :pass; const s_size = @as(usize, @intCast(s_info.size)); encoder.copyBufferToBuffer(s, 0, sm, 0, s_size); } if (!demo.buffers.data.producers.mapping.waiting) { const p = gctx.lookupResource(data.producers.buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(data.producers.buf) orelse break :pass; const pm = gctx.lookupResource(data.producers.mapping.buf) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } if (!demo.buffers.data.consumers.mapping.waiting) { const c = gctx.lookupResource(data.consumers.buf) orelse break :pass; const c_info = gctx.lookupResourceInfo(data.consumers.buf) orelse break :pass; const cm = gctx.lookupResource(data.consumers.mapping.buf) orelse break :pass; const c_size = @as(usize, @intCast(c_info.size)); encoder.copyBufferToBuffer(c, 0, cm, 0, c_size); } if (!demo.buffers.data.consumer_hovers.mapping.waiting) { const ch = gctx.lookupResource(data.consumer_hovers.buf) orelse break :pass; const ch_info = gctx.lookupResourceInfo(data.consumer_hovers.buf) orelse break :pass; const chm = gctx.lookupResource(data.consumer_hovers.mapping.buf) orelse break :pass; const ch_size = @as(usize, @intCast(ch_info.size)); encoder.copyBufferToBuffer(ch, 0, chm, 0, ch_size); } } // Draw the circles and squares in our defined viewport pass: { const hoverRP = gctx.lookupResource(demo.render_pipelines.hover) orelse break :pass; const hoverVB = gctx.lookupResourceInfo(demo.buffers.vertex.hover) orelse break :pass; const hoverB = gctx.lookupResourceInfo(data.hover) orelse break :pass; const svb_info = gctx.lookupResourceInfo(demo.buffers.vertex.square) orelse break :pass; const pb_info = gctx.lookupResourceInfo(data.producers.buf) orelse break :pass; const cvb_info = gctx.lookupResourceInfo(demo.buffers.vertex.circle) orelse break :pass; const cb_info = gctx.lookupResourceInfo(data.consumers.buf) orelse break :pass; const cib_info = gctx.lookupResourceInfo(demo.buffers.index.circle) orelse break :pass; const square_rp = gctx.lookupResource(demo.render_pipelines.square) orelse break :pass; const circle_rp = gctx.lookupResource(demo.render_pipelines.circle) orelse break :pass; const chrp = gctx.lookupResource(demo.render_pipelines.consumer_hover) orelse break :pass; const ch_info = gctx.lookupResourceInfo(data.consumer_hovers.buf) orelse break :pass; const render_bind_group = gctx.lookupResource(demo.bind_groups.render) orelse break :pass; const depth_view = gctx.lookupResource(demo.depth_texture_view) orelse break :pass; const color_attachments = [_]wgpu.RenderPassColorAttachment{.{ .view = back_buffer_view, .load_op = .clear, .store_op = .store, }}; const depth_attachment = wgpu.RenderPassDepthStencilAttachment{ .view = depth_view, .depth_load_op = .clear, .depth_store_op = .store, .depth_clear_value = 1.0, }; const render_pass_info = wgpu.RenderPassDescriptor{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, .depth_stencil_attachment = &depth_attachment, }; const pass = encoder.beginRenderPass(render_pass_info); defer { pass.end(); pass.release(); } const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width / 4; const height = @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)); const yOffset = height / 4; pass.setViewport(xOffset, 0, width - xOffset, height - yOffset, 0, 1); var mem = gctx.uniformsAllocate(zm.Mat, 1); mem.slice[0] = cam_world_to_clip; pass.setBindGroup(0, render_bind_group, &.{mem.offset}); const num_indices_circle = @as(u32, @intCast(cib_info.size / @sizeOf(f32))); pass.setPipeline(hoverRP); pass.setVertexBuffer(0, hoverVB.gpuobj.?, 0, hoverVB.size); pass.setVertexBuffer(1, hoverB.gpuobj.?, 0, hoverB.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, 1, 0, 0, 0); pass.setPipeline(chrp); pass.setVertexBuffer(0, hoverVB.gpuobj.?, 0, hoverVB.size); pass.setVertexBuffer(1, ch_info.gpuobj.?, 0, ch_info.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, num_consumer_hovers, 0, 0, 0); pass.setPipeline(circle_rp); pass.setVertexBuffer(0, cvb_info.gpuobj.?, 0, cvb_info.size); pass.setVertexBuffer(1, cb_info.gpuobj.?, 0, cb_info.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, num_consumers, 0, 0, 0); pass.setPipeline(square_rp); pass.setVertexBuffer(0, svb_info.gpuobj.?, 0, svb_info.size); pass.setVertexBuffer(1, pb_info.gpuobj.?, 0, pb_info.size); pass.draw(6, num_producers, 0, 0); } // Draw ImGui { const pass = zgpu.beginRenderPassSimple( encoder, .load, back_buffer_view, null, null, null, ); defer zgpu.endReleasePass(pass); zgui.backend.draw(pass); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); if (demo.gctx.present() == .swap_chain_resized) { demo.content_scale = getContentScale(demo.window); setImguiContentScale(demo.content_scale); updateAspectRatio(demo); } } pub fn clearSimulation(demo: *DemoState) void { const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; const stats_waiting = demo.buffers.data.stats.mapping.waiting; if (consumer_waiting or producer_waiting or stats_waiting) { demo.push_clear = true; return; } const gctx = demo.gctx; Wgpu.clearObjBuffer(gctx, Consumer, &demo.buffers.data.consumers); Wgpu.clearObjBuffer(gctx, Producer, &demo.buffers.data.producers); Wgpu.clearObjBuffer(gctx, ConsumerHover, &demo.buffers.data.consumer_hovers); Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = 0, .param = .num_transactions, }); Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = 0, .param = .consumers, }); Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = 0, .param = .producers, }); Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = 0, .param = .consumer_hovers, }); demo.stats.clear(); demo.popups.clear(); demo.push_clear = false; demo.running = false; } fn updateDepthTexture(demo: *DemoState) void { // Release old depth texture. demo.gctx.releaseResource(demo.depth_texture_view); demo.gctx.destroyResource(demo.depth_texture); // Create a new depth texture to match the new window size. const depth = Wgpu.createDepthTexture(demo.gctx); demo.depth_texture = depth.texture; demo.depth_texture_view = depth.view; } fn updateAspectRatio(demo: *DemoState) void { updateDepthTexture(demo); const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; const hovers_waiting = demo.buffers.data.consumer_hovers.mapping.waiting; if (consumer_waiting or producer_waiting or hovers_waiting) { demo.push_coord_update = true; return; } Wgpu.updateCoords(demo.gctx, Consumer, demo.buffers.data.consumers); Wgpu.updateCoords(demo.gctx, Producer, demo.buffers.data.producers); Wgpu.updateCoords(demo.gctx, ConsumerHover, demo.buffers.data.consumer_hovers); demo.push_coord_update = false; demo.params.aspect = Camera.getAspectRatio(demo.gctx); } fn getContentScale(window: *zglfw.Window) f32 { const content_scale = window.getContentScale(); return @max(content_scale[0], content_scale[1]); } fn setImguiContentScale(scale: f32) void { zgui.getStyle().* = zgui.Style.init(); zgui.getStyle().scaleAllSizes(scale); } pub fn deinit(demo: *DemoState) void { demo.popups.deinit(); demo.stats.deinit(); demo.buffers.data.consumers.list.deinit(); demo.buffers.data.consumer_hovers.list.deinit(); demo.buffers.data.producers.list.deinit(); demo.buffers.data.stats.list.deinit(); }
0
repos/simulations/src
repos/simulations/src/editor/mouse.zig
const std = @import("std"); const zglfw = @import("zglfw"); const zm = @import("zmath"); const zgpu = @import("zgpu"); const Camera = @import("camera.zig"); const DemoState = @import("main.zig").DemoState; pub const MouseButton = struct { name: [:0]const u8 = "Primary", button: zglfw.MouseButton = zglfw.MouseButton.left, state: bool = false, previousState: bool = false, grid_pos: [2]i32 = .{ 0, 0 }, pixel_pos: [2]u32 = .{ 0, 0 }, world_pos: [2]f32 = .{ 0, 0 }, pub fn update(self: *MouseButton, demo: *DemoState) void { self.previousState = self.state; const action = demo.window.getMouseButton(self.button); switch (action) { .release => { self.state = false; }, .repeat, .press => { self.state = true; }, } self.world_pos = getWorldPosition(demo); self.grid_pos = getGridPosition(demo); const content_scale = demo.window.getContentScale(); var pixel_pos = demo.window.getCursorPos(); pixel_pos[0] = @abs(pixel_pos[0]); pixel_pos[1] = @abs(pixel_pos[1]); self.pixel_pos = .{ @as(u32, @intFromFloat(pixel_pos[0] * content_scale[0])), @as(u32, @intFromFloat(pixel_pos[1] * content_scale[1])), }; } /// Returns true the frame the mouse button was pressed. pub fn pressed(self: MouseButton) bool { return self.state == true and self.state != self.previousState; } /// Returns true while the mouse button is pressed down. pub fn down(self: MouseButton) bool { return self.state == true; } /// Returns true the frame the mouse button was released. pub fn released(self: MouseButton) bool { return self.state == false and self.state != self.previousState; } /// Returns true while the mouse button is released. pub fn up(self: MouseButton) bool { return self.state == false; } }; // Return world position of current cursor pos pub fn getWorldPosition(demo: *DemoState) [2]f32 { const viewport_size = Camera.getViewportPixelSize(demo.gctx); const width = @as(f32, @floatFromInt(demo.gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const cursor_pos = demo.window.getCursorPos(); const content_scale = demo.window.getContentScale(); const vp_cursor_pos = [2]f32{ @as(f32, @floatCast(cursor_pos[0])) * content_scale[0] - xOffset, @as(f32, @floatCast(cursor_pos[1])) * content_scale[1], }; const rx = (vp_cursor_pos[0] * 2) / viewport_size[0] - 1; const ry = 1 - (vp_cursor_pos[1] * 2) / viewport_size[1]; const vec = zm.f32x4(rx, ry, 1, 1); const inv = zm.inverse(Camera.getObjectToClipMat(demo.gctx)); const world_pos = zm.mul(vec, inv); return .{ world_pos[0] / world_pos[3], world_pos[1] / world_pos[3], }; } pub fn getGridPosition(demo: *DemoState) [2]i32 { const world_pos = getWorldPosition(demo); const full_grid_pos = Camera.getGridFromWorld(demo.gctx, world_pos); return .{ full_grid_pos[0], full_grid_pos[1] }; } pub fn onGrid(demo: *DemoState) bool { const grid_pos = getGridPosition(demo); const x = grid_pos[0]; const y = grid_pos[1]; return x > Camera.MIN_X and x < Camera.MAX_X and y > Camera.MIN_Y and y < Camera.MAX_Y; }
0
repos/simulations/src
repos/simulations/src/editor/statistics.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); } pub fn generateAndFillRandomColor(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 4 * @sizeOf(u32), f32, &.{ random.float(f32), random.float(f32), random.float(f32) }, ); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const setArgs = struct { stat_obj: Wgpu.ObjectBuffer(u32), num: u32, param: enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }, }; pub fn setNum(gctx: *zgpu.GraphicsContext, args: setArgs) void { gctx.queue.writeBuffer( gctx.lookupResource(args.stat_obj.buf).?, @intFromEnum(args.param) * @sizeOf(u32), u32, &.{args.num}, ); }
0
repos/simulations/src
repos/simulations/src/editor/config.zig
const Hover = @import("hover.zig"); const Consumer = @import("consumer.zig"); const HoverConsumer = @import("consumer_hover.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); pub const cpi = .{ .vs = @embedFile("shaders/vertex/consumer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Consumer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "position", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "demand_rate", .type = u32, }, }, }; pub const ppi = .{ .vs = @embedFile("shaders/vertex/producer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Producer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "home", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "max_inventory", .type = u32, }, }, }; pub const hpi = .{ .vs = @embedFile("shaders/vertex/hover.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Hover, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "position", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, }, }; pub const chpi = .{ .vs = @embedFile("shaders/vertex/consumer_hover.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = HoverConsumer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "home", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, }, }; const common = @embedFile("shaders/compute/common.wgsl"); pub const ccpi = .{ .cs = common ++ @embedFile("shaders/compute/consumer.wgsl"), .entry_point = "main", }; pub const pcpi = .{ .cs = common ++ @embedFile("shaders/compute/producer.wgsl"), .entry_point = "main", };
0
repos/simulations/src
repos/simulations/src/editor/producer.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const Allocator = std.mem.Allocator; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const DemoState = @import("main.zig"); const Parameters = DemoState.Parameters; const Camera = @import("camera.zig"); const Self = @This(); absolute_home: [4]i32, home: [4]f32, color: [4]f32, production_rate: u32, inventory: i32, max_inventory: u32, _padding1: u32 = 0, pub const z_pos = 0; pub const Parameter = enum { production_rate, supply_shock, max_inventory, }; pub const DEFAULT_PRODUCTION_RATE: u32 = 300; pub const DEFAULT_MAX_INVENTORY: u32 = 10000; pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = .{ 1, 1, 1, 0 }, production_rate: u32 = DEFAULT_PRODUCTION_RATE, inventory: i32 = 0, max_inventory: u32 = DEFAULT_MAX_INVENTORY, }; pub fn generateBulk( gctx: *zgpu.GraphicsContext, obj_buf: *Wgpu.ObjectBuffer(Self), params: Parameters, num: u32, ) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); createAndAppend(gctx, .{ .obj_buf = obj_buf, .producer = .{ .absolute_home = .{ x, y }, .home = [2]f32{ @as(f32, @floatFromInt(x)) * params.aspect, @as(f32, @floatFromInt(y)) }, .production_rate = params.production_rate, .inventory = @as(i32, @intCast(params.max_inventory)), .max_inventory = params.max_inventory, }, }); i += 1; } // Wgpu.writeToMappedBuffer(gctx, obj_buf.buf, obj_buf.mapping.buf); } pub const AppendArgs = struct { producer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const home: [4]f32 = .{ args.producer.home[0], args.producer.home[1], z_pos, 1, }; const absolute_home: [4]i32 = .{ args.producer.absolute_home[0], args.producer.absolute_home[1], z_pos, 1, }; const producer = Self{ .absolute_home = absolute_home, .home = home, .color = args.producer.color, .production_rate = args.producer.production_rate, .inventory = args.producer.inventory, .max_inventory = args.producer.max_inventory, }; var producers: [1]Self = .{producer}; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.obj_buf.list.items.len)), .buf = args.obj_buf.buf, .structs = producers[0..], }); args.obj_buf.list.append(producers[0]) catch unreachable; args.obj_buf.mapping.num_structs += 1; } // pub const updateCoordsArgs = struct { // producers: Wgpu.ObjectBuffer, // stats: Wgpu.ObjectBuffer, // }; // pub fn updateCoords(gctx: *zgpu.GraphicsContext, args: updateCoordsArgs) void { // const producers = Wgpu.getAll(gctx, Self, .{ // .structs = args.producers, // .num_structs = Wgpu.getNumStructs(gctx, Self, args.stats), // }) catch return; // var new_producers: [DemoState.MAX_NUM_PRODUCERS]Self = undefined; // for (producers, 0..) |p, i| { // new_producers[i] = p; // new_producers[i].home = Camera.getWorldPosition(gctx, p.absolute_home); // } // gctx.queue.writeBuffer( // gctx.lookupResource(args.producers.data).?, // 0, // Self, // new_producers[0..producers.len], // ); // }
0
repos/simulations/src
repos/simulations/src/editor/circle.zig
const std = @import("std"); const math = std.math; const zgpu = @import("zgpu"); const Self = @This(); position: [4]f32, color: [4]f32, radius: f32, pub fn createIndexBuffer(gctx: *zgpu.GraphicsContext, comptime num_vertices: u32) zgpu.BufferHandle { const num_triangles = num_vertices - 1; const consumer_index_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .index = true }, .size = num_triangles * 3 * @sizeOf(u32), }); const num_indices = num_triangles * 3; var indices: [num_indices]u32 = undefined; var i: usize = 0; while (i < num_triangles) { indices[i * 3] = 0; indices[i * 3 + 1] = @as(u32, @intCast(i)) + 1; indices[i * 3 + 2] = @as(u32, @intCast(i)) + 2; i += 1; } indices[num_indices - 1] = 1; gctx.queue.writeBuffer(gctx.lookupResource(consumer_index_buffer).?, 0, u32, indices[0..]); return consumer_index_buffer; } pub fn createVertexBuffer( gctx: *zgpu.GraphicsContext, comptime num_vertices: u32, radius: f32, ) zgpu.BufferHandle { const consumer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = num_vertices * @sizeOf(f32) * 3, }); var consumer_vertex_data: [num_vertices][3]f32 = undefined; const num_sides = @as(f32, num_vertices - 1); const angle = 2 * math.pi / num_sides; consumer_vertex_data[0] = [3]f32{ 0, 0, 0 }; var i: u32 = 1; while (i < num_vertices) { const current_angle = angle * @as(f32, @floatFromInt(i)); const x = @cos(current_angle) * radius; const y = @sin(current_angle) * radius; consumer_vertex_data[i] = [3]f32{ x, y, 0 }; i += 1; } gctx.queue.writeBuffer(gctx.lookupResource(consumer_vertex_buffer).?, 0, [3]f32, consumer_vertex_data[0..]); return consumer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/editor/images.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zstbi = @import("zstbi"); pub fn createTextureView(gctx: *zgpu.GraphicsContext, path: [:0]const u8) !zgpu.TextureViewHandle { //const dir = std.fs.cwd(); //dir.access(path, .{}) catch unreachable; var image = try zstbi.Image.loadFromFile(path, 4); defer image.deinit(); const texture = gctx.createTexture(.{ .usage = .{ .texture_binding = true, .copy_dst = true }, .size = .{ .width = image.width, .height = image.height, .depth_or_array_layers = 1, }, .format = zgpu.imageInfoToTextureFormat( image.num_components, image.bytes_per_component, image.is_hdr, ), .mip_level_count = 1, }); const textureView = gctx.createTextureView(texture, .{}); gctx.queue.writeTexture( .{ .texture = gctx.lookupResource(texture).? }, .{ .bytes_per_row = image.bytes_per_row, .rows_per_image = image.height, }, .{ .width = image.width, .height = image.height }, u8, image.data, ); return textureView; }
0
repos/simulations/src
repos/simulations/src/editor/popups.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zgui = @import("zgui"); const zmath = @import("zmath"); const Wgpu = @import("wgpu.zig"); const Windows = @import("windows.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Mouse = @import("mouse.zig"); const Camera = @import("camera.zig"); const ConsumerHover = @import("consumer_hover.zig"); const Callbacks = @import("callbacks.zig"); pub const WINDOW_SIZE_PIXELS: [2]u32 = .{ 350, 150 }; const HOVER_SIZE_GRID = 40; const HoverSquareID = struct { hs_id: u32, gui_id: u32, }; pub const HoverSquare = struct { id: HoverSquareID, corners_grid: [4]i32, }; pub const Popup = struct { id: HoverSquareID = undefined, grid_center: [2]i32, pixel_center: [2]f32 = undefined, open: bool = false, pivot: bool = false, open_grid: [4]i32 = undefined, closed_grid: [4]i32 = undefined, type_popup: enum { consumers, producer }, parameters: union { consumer: struct { demand_rate: u32, moving_rate: f32, }, producer: struct { production_rate: u32, max_inventory: u32, }, }, }; const Self = @This(); hover_square_len: u32, popups: std.ArrayList(Popup), x_axis: std.AutoHashMap(i32, std.AutoHashMap(HoverSquareID, void)), y_axis: std.AutoHashMap(i32, std.AutoHashMap(HoverSquareID, void)), pub fn init(allocator: std.mem.Allocator) Self { return Self{ .hover_square_len = 0, .popups = std.ArrayList(Popup).init(allocator), .x_axis = std.AutoHashMap( i32, std.AutoHashMap(HoverSquareID, void), ).init(allocator), .y_axis = std.AutoHashMap( i32, std.AutoHashMap(HoverSquareID, void), ).init(allocator), }; } pub fn deinit(self: *Self) void { self.popups.deinit(); var x_arrays = self.x_axis.valueIterator(); while (x_arrays.next()) |set| { set.deinit(); } self.x_axis.deinit(); var y_arrays = self.y_axis.valueIterator(); while (y_arrays.next()) |set| { set.deinit(); } self.y_axis.deinit(); } pub fn clear(self: *Self) void { self.hover_square_len = 0; self.popups.clearAndFree(); var x_arrays = self.x_axis.valueIterator(); while (x_arrays.next()) |set| { set.clearAndFree(); } self.x_axis.clearAndFree(); var y_arrays = self.y_axis.valueIterator(); while (y_arrays.next()) |set| { set.clearAndFree(); } self.y_axis.clearAndFree(); } pub fn appendPopup(self: *Self, popup: Popup) void { var copy = popup; copy.id.gui_id = @as(u32, @intCast(self.popups.items.len)); copy.id.hs_id = self.hover_square_len; self.popups.append(copy) catch unreachable; } pub fn appendSquare( self: *Self, allocator: std.mem.Allocator, grid_pos: [2]i32, ) void { const gui_id = @as(u32, @intCast(self.popups.items.len)); const square = HoverSquare{ .id = .{ .hs_id = self.hover_square_len, .gui_id = gui_id, }, .corners_grid = .{ grid_pos[0] - HOVER_SIZE_GRID, grid_pos[0] + HOVER_SIZE_GRID, grid_pos[1] - HOVER_SIZE_GRID, grid_pos[1] + HOVER_SIZE_GRID, }, }; self.appendRange(allocator, square); self.hover_square_len += 1; } fn inBetween(bottom: i32, value: i32, top: i32) bool { return bottom < value and value < top; } fn resetCoords( axis: *std.AutoHashMap(i32, std.AutoHashMap(HoverSquareID, void)), closed_grid: *[2]i32, open_grid: *[2]i32, hsid: HoverSquareID, ) void { var it = axis.iterator(); while (it.next()) |kv| { const coord = kv.key_ptr.*; const pixel_has_gui = kv.value_ptr.contains(hsid); const in_open_grid = inBetween(open_grid[0], coord, open_grid[1]); const not_in_closed_grid = !inBetween( closed_grid[0], coord, closed_grid[1], ); if (pixel_has_gui and in_open_grid and not_in_closed_grid) { _ = kv.value_ptr.remove(hsid); if (kv.value_ptr.count() == 0) { kv.value_ptr.deinit(); _ = axis.remove(coord); } } } } fn addCoords( axis: *std.AutoHashMap(i32, std.AutoHashMap(HoverSquareID, void)), allocator: std.mem.Allocator, range: *const [2]i32, hsid: HoverSquareID, ) !void { var coord = range[0]; while (coord <= range[1]) { const set = axis.getPtr(coord); if (set) |s| { if (!s.contains(hsid)) { try s.put(hsid, {}); } } else { var new_set = std.AutoHashMap(HoverSquareID, void).init(allocator); try new_set.put(hsid, {}); try axis.put(coord, new_set); } coord += 1; } } fn resetRange(self: *Self, edges: [4]i32, gui_id: u32) void { resetCoords(&self.x_axis, edges[0..2], gui_id); resetCoords(&self.y_axis, edges[2..4], gui_id); } fn appendRange(self: *Self, allocator: std.mem.Allocator, hs: HoverSquare) void { addCoords(&self.x_axis, allocator, hs.corners_grid[0..2], hs.id) catch unreachable; addCoords(&self.y_axis, allocator, hs.corners_grid[2..4], hs.id) catch unreachable; } pub fn anyOpen(self: *Self) bool { var b = false; for (self.popups.items) |w| { b = b or w.open; } return b; } fn isPopupOpen(self: *Self, gui_id: u32) bool { for (self.popups.items) |p| { if (p.id.gui_id == gui_id) { return p.open; } } return false; } pub fn doesAgentExist(self: *Self, grid_pos: [2]i32) bool { var x_set = self.x_axis.get(grid_pos[0]); var y_set = self.y_axis.get(grid_pos[1]); if (x_set == null or y_set == null) { return false; } const gui_id: ?u32 = blk: { var last_gui_id: ?u32 = null; var it = x_set.?.keyIterator(); while (it.next()) |key| { if (y_set.?.contains(key.*)) { last_gui_id = key.gui_id; if (isPopupOpen(self, key.gui_id)) { break :blk key.gui_id; } } } // If neither gui is open, just open the last one we saw if (last_gui_id) |gid| { break :blk gid; } break :blk null; }; if (gui_id == null) { return false; } return true; } fn getPopupIndex(self: *Self, grid_pos: [2]i32) !usize { var x_set = self.x_axis.get(grid_pos[0]); var y_set = self.y_axis.get(grid_pos[1]); if (x_set == null or y_set == null) { return error.PopupNotFound; } const gui_id: ?u32 = blk: { var last_gui_id: ?u32 = null; var it = x_set.?.keyIterator(); while (it.next()) |key| { if (y_set.?.contains(key.*)) { last_gui_id = key.gui_id; if (isPopupOpen(self, key.gui_id)) { break :blk key.gui_id; } } } // If neither gui is open, just open the last one we saw if (last_gui_id) |gid| { break :blk gid; } break :blk null; }; if (gui_id == null) { return error.PopupNotFound; } if (gui_id.? >= self.popups.items.len) { return error.PopupNotFound; } return @as(usize, @intCast(gui_id.?)); } pub const popupArgs = struct { consumers: *Wgpu.ObjectBuffer(Consumer), consumer_hovers: *Wgpu.ObjectBuffer(ConsumerHover), mouse: Mouse.MouseButton, producers: Wgpu.ObjectBuffer(Producer), stats: Wgpu.ObjectBuffer(u32), allocator: std.mem.Allocator, }; pub fn display(self: *Self, gctx: *zgpu.GraphicsContext, args: popupArgs) void { ConsumerHover.clearHover(gctx, args.consumer_hovers); const popup_idx = getPopupIndex(self, args.mouse.grid_pos) catch { for (self.popups.items, 0..) |p, i| { if (p.open) { self.closePopup(&self.popups.items[i]); } } return; }; ConsumerHover.highlightConsumers( gctx, popup_idx, args.consumer_hovers, ); const popup = &self.popups.items[popup_idx]; if (!popup.open) { popup.open = true; var center = popup.grid_center; const pixel_center = Camera.getPixelPosition(gctx, center); const min_x_pixel = pixel_center[0] - HOVER_SIZE_GRID; const max_x_pixel = pixel_center[0] + WINDOW_SIZE_PIXELS[0]; const min_y_pixel = pixel_center[1] - HOVER_SIZE_GRID; const max_y_pixel = pixel_center[1] + WINDOW_SIZE_PIXELS[1]; var open_grid: [4]i32 = .{ popup.grid_center[0] - HOVER_SIZE_GRID, Camera.getGridPosition(gctx, .{ max_x_pixel, min_y_pixel })[0], Camera.getGridPosition(gctx, .{ min_x_pixel, max_y_pixel })[1], popup.grid_center[1] + HOVER_SIZE_GRID, }; if (open_grid[1] >= Camera.MAX_X) { const len_x = open_grid[1] - open_grid[0] - (2 * HOVER_SIZE_GRID); open_grid[1] = open_grid[0] + (2 * HOVER_SIZE_GRID); open_grid[0] -= len_x; center = .{ open_grid[1] - HOVER_SIZE_GRID, open_grid[3] - HOVER_SIZE_GRID, }; popup.pivot = true; } popup.pixel_center = pixel_center; popup.open_grid = open_grid; self.appendRange(args.allocator, .{ .id = popup.id, .corners_grid = popup.open_grid, }); } if (popup.pivot) { zgui.setNextWindowPos(.{ .x = popup.pixel_center[0], .y = popup.pixel_center[1], .pivot_x = 1.0, }); } else { zgui.setNextWindowPos(.{ .x = popup.pixel_center[0], .y = popup.pixel_center[1], }); } zgui.setNextWindowSize(.{ .w = WINDOW_SIZE_PIXELS[0], .h = WINDOW_SIZE_PIXELS[1], }); switch (popup.type_popup) { .consumers => consumerGui(gctx, popup_idx, popup, args), .producer => producerGui(gctx, popup_idx, popup, args), } } fn closePopup(self: *Self, popup: *Popup) void { popup.open = false; resetCoords( &self.x_axis, popup.closed_grid[0..2], popup.open_grid[0..2], popup.id, ); resetCoords( &self.y_axis, popup.closed_grid[2..4], popup.open_grid[2..4], popup.id, ); } fn consumerGui(gctx: *zgpu.GraphicsContext, idx: usize, popup: *Popup, args: popupArgs) void { if (zgui.begin("Test", Windows.window_flags)) { zgui.pushIntId(@as(i32, @intCast(idx)) + 3); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); demandRateButton(gctx, popup, args); movingRateSlider(gctx, popup, args); zgui.popId(); } zgui.end(); } fn demandRateButton(gctx: *zgpu.GraphicsContext, popup: *Popup, args: popupArgs) void { zgui.text("Demand Rate", .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted("How much consumers demand from producers on a single trip."); zgui.endTooltip(); } const demand_rate_ptr = &popup.parameters.consumer.demand_rate; if (zgui.sliderScalar( "##dr", u32, .{ .v = demand_rate_ptr, .min = 1, .max = 1000 }, )) { for (args.consumers.list.items, 0..) |c, i| { if (popup.id.gui_id == c.grouping_id) { gctx.queue.writeBuffer( gctx.lookupResource(args.consumers.buf).?, i * @sizeOf(Consumer) + @offsetOf(Consumer, "demand_rate"), u32, &.{demand_rate_ptr.*}, ); } } } } fn movingRateSlider(gctx: *zgpu.GraphicsContext, popup: *Popup, args: popupArgs) void { zgui.text("Moving Rate", .{}); const moving_rate_ptr = &popup.parameters.consumer.moving_rate; if (zgui.sliderScalar("##mr", f32, .{ .v = moving_rate_ptr, .min = 1.0, .max = 20 })) { for (args.consumers.list.items, 0..) |c, i| { if (popup.id.gui_id == c.grouping_id) { gctx.queue.writeBuffer( gctx.lookupResource(args.consumers.buf).?, i * @sizeOf(Consumer) + @offsetOf(Consumer, "moving_rate"), f32, &.{moving_rate_ptr.*}, ); } } } } fn producerGui(gctx: *zgpu.GraphicsContext, idx: usize, popup: *Popup, args: popupArgs) void { if (zgui.begin("Test", Windows.window_flags)) { zgui.pushIntId(@as(i32, @intCast(idx)) + 3); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); productionRateButton(gctx, popup, args); maxInventoryButton(gctx, popup, args); zgui.popId(); } zgui.end(); } fn productionRateButton(gctx: *zgpu.GraphicsContext, popup: *Popup, args: popupArgs) void { zgui.text("Production Rate", .{}); const production_rate_ptr = &popup.parameters.producer.production_rate; if (zgui.sliderScalar("##pr", u32, .{ .v = production_rate_ptr, .min = 1, .max = 1000 })) { for (args.producers.list.items, 0..) |p, i| { if (std.mem.eql(i32, popup.grid_center[0..2], p.absolute_home[0..2])) { gctx.queue.writeBuffer( gctx.lookupResource(args.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "production_rate"), u32, &.{production_rate_ptr.*}, ); } } } } fn maxInventoryButton(gctx: *zgpu.GraphicsContext, popup: *Popup, args: popupArgs) void { zgui.text("Max Inventory", .{}); const max_inventory_ptr = &popup.parameters.producer.max_inventory; if (zgui.sliderScalar("##mi", u32, .{ .v = max_inventory_ptr, .min = 10, .max = 10000 })) { for (args.producers.list.items, 0..) |p, i| { if (std.mem.eql(i32, popup.grid_center[0..2], p.absolute_home[0..2])) { gctx.queue.writeBuffer( gctx.lookupResource(args.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "max_inventory"), u32, &.{max_inventory_ptr.*}, ); } } } }
0
repos/simulations/src
repos/simulations/src/editor/consumer.zig
const std = @import("std"); const math = std.math; const array = std.ArrayList; const Allocator = std.mem.Allocator; const random = std.crypto.random; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const DemoState = @import("main.zig"); const Parameters = DemoState.Parameters; const Wgpu = @import("wgpu.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Self = @This(); pub const defaults = DEFAULTS{}; const DEFAULTS = struct { color: [4]f32 = .{ 1, 0, 0, 0 }, moving_rate: f32 = 5.0, demand_rate: u32 = 100, radius: f32 = 20.0, }; absolute_home: [4]i32, position: [4]f32, home: [4]f32, destination: [4]f32, color: [4]f32 = defaults.color, step_size: [2]f32 = .{ 0, 0 }, moving_rate: f32, demand_rate: u32, inventory: u32 = 0, radius: f32, producer_id: i32 = -1, grouping_id: u32 = 0, pub const z_pos = 0; pub fn generateBulk( gctx: *zgpu.GraphicsContext, obj_buf: *Wgpu.ObjectBuffer(Self), params: Parameters, num: u32, ) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); const aspect_home = [2]f32{ @as(f32, @floatFromInt(x)) * params.aspect, @as(f32, @floatFromInt(y)), }; createAndAppend(gctx, .{ .consumer = .{ .absolute_home = .{ x, y }, .home = aspect_home, .moving_rate = params.moving_rate, .demand_rate = params.demand_rate, .radius = params.consumer_radius, }, .obj_buf = obj_buf, }); i += 1; } } pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = defaults.color, moving_rate: f32 = defaults.moving_rate, demand_rate: u32 = defaults.demand_rate, radius: f32 = defaults.radius, grouping_id: u32 = 0, }; pub const AppendArgs = struct { consumer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const home: [4]f32 = .{ args.consumer.home[0], args.consumer.home[1], z_pos, 1, }; const absolute_home: [4]i32 = .{ args.consumer.absolute_home[0], args.consumer.absolute_home[1], z_pos, 1, }; const consumer = Self{ .absolute_home = absolute_home, .position = home, .home = home, .destination = home, .color = args.consumer.color, .moving_rate = args.consumer.moving_rate, .demand_rate = args.consumer.demand_rate, .radius = args.consumer.radius, .grouping_id = args.consumer.grouping_id, }; var consumers: [1]Self = .{consumer}; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.obj_buf.list.items.len)), .buf = args.obj_buf.buf, .structs = consumers[0..], }); args.obj_buf.list.append(consumers[0]) catch unreachable; args.obj_buf.mapping.num_structs += 1; }
0
repos/simulations/src
repos/simulations/src/editor/build.zig
const std = @import("std"); const Options = @import("../../../build.zig").Options; pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = "Simulations", .root_source_file = b.path("src/resources/editor/main.zig"), .target = options.target, .optimize = options.optimize, }); @import("system_sdk").addLibraryPathsTo(exe); const zglfw = b.dependency("zglfw", .{ .target = options.target, }); exe.root_module.addImport("zglfw", zglfw.module("root")); exe.linkLibrary(zglfw.artifact("glfw")); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{ .target = options.target, }); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); const zmath = b.dependency("zmath", .{ .target = options.target, }); exe.root_module.addImport("zmath", zmath.module("root")); const zgui = b.dependency("zgui", .{ .target = options.target, .backend = .glfw_wgpu, }); exe.root_module.addImport("zgui", zgui.module("root")); exe.linkLibrary(zgui.artifact("imgui")); const zpool = b.dependency("zpool", .{ .target = options.target, }); exe.root_module.addImport("zpool", zpool.module("root")); const zstbi = b.dependency("zstbi", .{ .target = options.target, }); exe.root_module.addImport("zstbi", zstbi.module("root")); exe.linkLibrary(zstbi.artifact("zstbi")); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "" }, .install_subdir = "bin/content", }); exe.step.dependOn(&install_content_step.step); return exe; } inline fn thisDir() []const u8 { return comptime std.fs.path.dirname(@src().file) orelse "."; }
0
repos/simulations/src
repos/simulations/src/editor/hover.zig
const zgpu = @import("zgpu"); const Self = @This(); position: [4]f32, color: [4]f32, radius: f32, pub const z_pos = 0; pub fn initBuffer(gctx: *zgpu.GraphicsContext) zgpu.BufferHandle { const buf = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .copy_src = true, .vertex = true, .storage = true }, .size = @sizeOf(Self), }); const hoverCircle = Self{ .position = .{ 0, 0, z_pos, 0 }, .color = .{ 0, 0, 1, 0 }, .radius = 300, }; gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 0, Self, &.{hoverCircle}, ); return buf; }
0
repos/simulations/src
repos/simulations/src/editor/gui.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zgui = @import("zgui"); const wgpu = zgpu.wgpu; const zmath = @import("zmath"); const Main = @import("main.zig"); const DemoState = Main.DemoState; const Camera = @import("camera.zig"); const Circle = @import("circle.zig"); const Consumer = @import("consumer.zig"); const ConsumerHover = @import("consumer_hover.zig"); const Hover = @import("hover.zig"); const Producer = @import("producer.zig"); const Statistics = @import("statistics.zig"); const Wgpu = @import("wgpu.zig"); const Window = @import("windows.zig"); const Mouse = @import("mouse.zig"); const Popups = @import("popups.zig"); const Callbacks = @import("callbacks.zig"); pub const State = struct { pub const Selection = enum { none, consumer, consumers, producer, }; selection: Selection = .consumer, producer: zgpu.TextureViewHandle = undefined, consumer: zgpu.TextureViewHandle = undefined, consumers: zgpu.TextureViewHandle = undefined, consumer_grouping_id: u32 = 0, }; pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { const gctx = demo.gctx; Window.setNextWindow(gctx, Window.ParametersWindow); if (zgui.begin("Parameters", Window.window_flags)) { zgui.pushIntId(2); selection_gui(); parameters(demo, gctx); zgui.popId(); } zgui.end(); Window.setNextWindow(gctx, Window.StatsWindow); if (zgui.begin("Data", Window.window_flags)) { zgui.pushIntId(3); plots(demo); zgui.popId(); } zgui.end(); Wgpu.runCallbackIfReady(u32, &demo.buffers.data.stats.mapping); Wgpu.runCallbackIfReady(Producer, &demo.buffers.data.producers.mapping); Wgpu.runCallbackIfReady(Consumer, &demo.buffers.data.consumers.mapping); Wgpu.runCallbackIfReady(ConsumerHover, &demo.buffers.data.consumer_hovers.mapping); demo.popups.display(gctx, .{ .consumers = &demo.buffers.data.consumers, .consumer_hovers = &demo.buffers.data.consumer_hovers, .mouse = demo.mouse, .producers = demo.buffers.data.producers, .stats = demo.buffers.data.stats, .allocator = demo.allocator, }); hoverUpdate(gctx, demo); if (!demo.popups.anyOpen() and Mouse.onGrid(demo)) { _ = switch (demo.gui.selection) { .none => {}, .consumer => { addingConsumer(gctx, demo, addConsumer); }, .consumers => { addingConsumer(gctx, demo, addConsumerBrush); }, .producer => { addingProducer(gctx, demo); }, }; } if (demo.running) { //Helpful for debugging shaders //Statistics.generateAndFillRandomColor(gctx, demo.buffers.data.stats.data); const current_time = @as(f32, @floatCast(gctx.stats.time)); const seconds_passed = current_time - demo.stats.second; if (seconds_passed >= 1) { demo.stats.second = current_time; Wgpu.getAllAsync(u32, Callbacks.numTransactions, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.stats, .stats = &demo.stats, }); Wgpu.getAllAsync(Consumer, Callbacks.emptyConsumers, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.consumers, .stats = &demo.stats, }); Wgpu.getAllAsync(Producer, Callbacks.totalInventory, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.producers, .stats = &demo.stats, }); } } } fn hoverUpdate(gctx: *zgpu.GraphicsContext, demo: *DemoState) void { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.hover).?, @offsetOf(Hover, "position"), [2]f32, &.{demo.mouse.world_pos}, ); } fn addingConsumer( gctx: *zgpu.GraphicsContext, demo: *DemoState, addFn: *const fn ( gctx: *zgpu.GraphicsContext, demo: *DemoState, ) void, ) void { const space_taken = demo.popups.doesAgentExist(demo.mouse.grid_pos); if (demo.mouse.down() and !space_taken) { addFn(gctx, demo); } else if (demo.mouse.released()) { const items = demo.buffers.data.consumers.list.items; const last_consumer = items[items.len - 1]; demo.popups.appendPopup(.{ .grid_center = last_consumer.absolute_home[0..2].*, .type_popup = .consumers, .parameters = .{ .consumer = .{ .demand_rate = Consumer.defaults.demand_rate, .moving_rate = Consumer.defaults.moving_rate, }, }, }); } } fn addConsumer(gctx: *zgpu.GraphicsContext, demo: *DemoState) void { const gui_id = @as(u32, @intCast(demo.popups.popups.items.len)); const consumer_args = Consumer.Args{ .absolute_home = Camera.getGridFromWorld(gctx, demo.mouse.world_pos), .home = demo.mouse.world_pos, .grouping_id = gui_id, }; Consumer.createAndAppend(gctx, .{ .consumer = consumer_args, .obj_buf = &demo.buffers.data.consumers, }); demo.buffers.data.consumers.mapping.staging.num_structs += 1; Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = @as(u32, @intCast(demo.buffers.data.consumers.list.items.len)), .param = .consumers, }); ConsumerHover.createAndAppend(gctx, .{ .args = consumer_args, .buf = &demo.buffers.data.consumer_hovers, }); demo.buffers.data.consumer_hovers.mapping.staging.num_structs += 1; demo.popups.appendSquare(demo.allocator, demo.mouse.grid_pos); } fn addConsumerBrush(gctx: *zgpu.GraphicsContext, demo: *DemoState) void { const world_pos = demo.mouse.world_pos; const offset = 20; const array_positions: [5][2]f32 = .{ world_pos, .{ world_pos[0] + offset, world_pos[1] + offset }, .{ world_pos[0] - offset, world_pos[1] + offset }, .{ world_pos[0] - offset, world_pos[1] - offset }, .{ world_pos[0] + offset, world_pos[1] - offset }, }; const gui_id = @as(u32, @intCast(demo.popups.popups.items.len)); for (array_positions) |pos| { Consumer.createAndAppend(gctx, .{ .consumer = .{ .absolute_home = Camera.getGridFromWorld(gctx, pos), .home = pos, .grouping_id = gui_id, }, .obj_buf = &demo.buffers.data.consumers, }); demo.buffers.data.consumers.mapping.staging.num_structs += 1; } Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = @as(u32, @intCast(demo.buffers.data.consumers.list.items.len)), .param = .consumers, }); ConsumerHover.createAndAppend(gctx, .{ .args = .{ .absolute_home = Camera.getGridFromWorld(gctx, world_pos), .home = world_pos, .grouping_id = gui_id, }, .buf = &demo.buffers.data.consumer_hovers, }); demo.buffers.data.consumer_hovers.mapping.staging.num_structs += 1; demo.popups.appendSquare(demo.allocator, demo.mouse.grid_pos); } fn addingProducer(gctx: *zgpu.GraphicsContext, demo: *DemoState) void { const space_taken = demo.popups.doesAgentExist(demo.mouse.grid_pos); if (demo.mouse.pressed() and !space_taken) { Producer.createAndAppend(gctx, .{ .producer = .{ .home = demo.mouse.world_pos, .absolute_home = demo.mouse.grid_pos, }, .obj_buf = &demo.buffers.data.producers, }); Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = @as(u32, @intCast(demo.buffers.data.producers.list.items.len)), .param = .producers, }); demo.popups.appendSquare(demo.allocator, demo.mouse.grid_pos); demo.popups.appendPopup(.{ .grid_center = demo.mouse.grid_pos, .type_popup = .producer, .parameters = .{ .producer = .{ .production_rate = Producer.DEFAULT_PRODUCTION_RATE, .max_inventory = Producer.DEFAULT_MAX_INVENTORY, }, }, }); } } fn plots(demo: *DemoState) void { const font_size = zgui.getFontSize() * 0.8; zgui.plot.pushStyleVar2f(.{ .idx = .plot_padding, .v = .{ font_size, font_size }, }); const window_size = zgui.getWindowSize(); const margin = 15; const plot_width = window_size[0] - margin; const plot_height = window_size[1] - margin; if (zgui.plot.beginPlot("", .{ .w = plot_width, .h = plot_height, .flags = .{} })) { zgui.plot.setupAxis(.x1, .{ .label = "", .flags = .{ .auto_fit = true } }); zgui.plot.setupAxis(.y1, .{ .label = "", .flags = .{ .auto_fit = true } }); zgui.plot.setupLegend(.{ .north = true, .west = true }, .{}); zgui.plot.plotLineValues("Transactions", u32, .{ .v = demo.stats.num_transactions.items[0..], }); zgui.plot.plotLineValues("Empty Consumers", u32, .{ .v = demo.stats.num_empty_consumers.items[0..], }); zgui.plot.plotLineValues("Total Producer Inventory", u32, .{ .v = demo.stats.num_total_producer_inventory.items[0..], }); zgui.plot.endPlot(); } } fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void { zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); const pressedColor = [4]f32{ 0.0, 0.5, 1.0, 1.0 }; const size = 3 * zgui.getFontSize(); const buttonSize = [2]f32{ size, size }; zgui.text("Consumers", .{}); if (demo.gui.selection == .consumers) { coloredButton( gctx, &demo.gui, State.Selection.consumers, demo.gui.consumers, pressedColor, buttonSize, ); } else { coloredButton( gctx, &demo.gui, State.Selection.consumer, demo.gui.consumer, pressedColor, buttonSize, ); } zgui.sameLine(.{}); if (zgui.arrowButton("left_button_id", .{ .dir = .left })) { demo.gui.selection = .consumer; } zgui.sameLine(.{}); if (zgui.arrowButton("right_button_id", .{ .dir = .right })) { demo.gui.selection = .consumers; } zgui.dummy(.{ .w = 1, .h = 10 }); zgui.text("Producers", .{}); coloredButton( gctx, &demo.gui, State.Selection.producer, demo.gui.producer, pressedColor, buttonSize, ); zgui.dummy(.{ .w = 1, .h = 10 }); zgui.text("Consumer Size", .{}); if (zgui.sliderScalar("##cs", f32, .{ .v = &demo.params.consumer_radius, .min = 1, .max = 40 })) { demo.buffers.vertex.circle = Circle.createVertexBuffer( gctx, Main.NUM_CONSUMER_SIDES, demo.params.consumer_radius, ); } if (zgui.button("Start", .{})) { demo.running = true; } if (zgui.button("Stop", .{})) { demo.running = false; } if (zgui.button("Clear", .{})) { demo.running = true; Main.clearSimulation(demo); } if (zgui.button("Supply Shock", .{})) { for (demo.buffers.data.producers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "inventory"), i32, &.{0}, ); } } zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted("Set all producer inventory to 0."); zgui.endTooltip(); } } fn coloredButton( gctx: *zgpu.GraphicsContext, guiState: *State, buttonState: State.Selection, textureView: zgpu.TextureViewHandle, color: [4]f32, size: [2]f32, ) void { const tex_id = gctx.lookupResource(textureView).?; const id = @tagName(buttonState); const pixel_size = .{ .w = size[0], .h = size[1], }; if (guiState.selection == buttonState) { zgui.pushStyleColor4f(.{ .idx = .button, .c = color }); defer zgui.popStyleColor(.{}); if (zgui.imageButton(id, tex_id, pixel_size)) { guiState.selection = .none; } } else { if (zgui.imageButton(id, tex_id, pixel_size)) { guiState.selection = buttonState; } } }
0
repos/simulations/src
repos/simulations/src/editor/wgpu.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zm = @import("zmath"); const zems = @import("zems"); const Gctx = zgpu.GraphicsContext; const wgpu = zgpu.wgpu; const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_STRUCTS = 10000; // A mishmash of Wgpu initialization functions and buffer helpers for an array of generic structs // Data Types pub const GraphicsObject = struct { render_pipeline: zgpu.RenderPipelineHandle, attribute_buffer: zgpu.BufferHandle, vertex_buffer: zgpu.BufferHandle, index_buffer: zgpu.BufferHandle, size_of_struct: u32, }; pub fn ObjectBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, list: std.ArrayList(T), mapping: MappingBuffer(T), }; } const callback_queue_len: usize = 10; fn MappingBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, insert_idx: usize = 0, remove_idx: usize = 0, requests: [callback_queue_len]struct { func: Callback(T), args: Callbacks.Args(T), } = undefined, staging: StagingBuffer(T), waiting: bool = false, num_structs: u32, }; } fn StagingBuffer(comptime T: type) type { return struct { slice: ?[]const T = null, buffer: wgpu.Buffer = undefined, num_structs: u32, }; } fn Callback(comptime T: type) type { return ?*const fn (args: Callbacks.Args(T)) void; } pub const RenderPipelineInfo = struct { pub const Attribute = struct { name: []const u8, type: type, }; vs: [:0]const u8, fs: [:0]const u8, inst_type: type, inst_attrs: []const Attribute, primitive_topology: wgpu.PrimitiveTopology = .triangle_list, }; pub const ComputePipelineInfo = struct { cs: [:0]const u8, entry_point: [:0]const u8, }; pub fn GenCallback(comptime T: type) wgpu.BufferMapCallback { return struct { fn callback(status: wgpu.BufferMapAsyncStatus, userdata: ?*anyopaque) callconv(.C) void { const usb = @as(*StagingBuffer(T), @ptrCast(@alignCast(userdata))); std.debug.assert(usb.slice == null); if (status == .success) { usb.slice = usb.buffer.getConstMappedRange(T, 0, usb.num_structs).?; } else { std.log.err("[zgpu] Failed to map buffer (code: {any})\n", .{status}); } } }.callback; } pub fn getAllAsync( comptime T: type, callback: Callback(T), args: Callbacks.Args(T), ) void { const map_ptr = &args.buf.mapping; map_ptr.staging.num_structs = map_ptr.num_structs; if (map_ptr.staging.num_structs <= 0) return; map_ptr.requests[map_ptr.insert_idx].func = callback; map_ptr.requests[map_ptr.insert_idx].args = args; map_ptr.insert_idx = (map_ptr.insert_idx + 1) % callback_queue_len; runMapIfReady(T, &args.buf.mapping); } pub fn runMapIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (!buf.waiting and buf.staging.slice == null and buf.insert_idx != buf.remove_idx) { buf.waiting = true; const gctx = buf.requests[buf.remove_idx].args.gctx; buf.staging.buffer = gctx.lookupResource(buf.buf).?; buf.staging.buffer.mapAsync( .{ .read = true }, 0, @sizeOf(T) * buf.staging.num_structs, GenCallback(T), @as(*anyopaque, @ptrCast(&buf.staging)), ); } } pub fn runCallbackIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (buf.waiting and buf.staging.slice != null) { const request = buf.requests[buf.remove_idx]; buf.remove_idx = (buf.remove_idx + 1) % callback_queue_len; request.func.?(request.args); buf.staging.buffer.unmap(); buf.staging.slice = null; buf.waiting = false; } } pub fn waitForCallback(comptime T: type, buf: *MappingBuffer(T)) void { while (buf.waiting) { runCallbackIfReady(T, buf); } } pub fn getMappedData(comptime T: type, buf: *MappingBuffer(T)) []T { return @constCast(buf.staging.slice.?[0..buf.staging.num_structs]); } pub fn agentParameters(comptime T: type) type { switch (T) { Consumer => return union(enum) { moving_rate: f32, demand_rate: u32, }, Producer => return union(enum) { production_rate: u32, inventory: i32, max_inventory: u32, }, u32 => return u32, else => unreachable, } } pub fn setArgs(comptime T: type) type { return struct { agents: ObjectBuffer, parameter: agentParameters(T), }; } // pub fn setAll(gctx: *zgpu.GraphicsContext, comptime T: type, args: setArgs(T)) void { // var agents = getAllAsync(T, Callbacks.clearConsumerHovers, .{ // .gctx = gctx, // .buf = args.agents, // }); // for (agents, 0..) |_, i| { // setAgentParameter(T, &agents[i], args.parameter); // } // writeBuffer(gctx, args.agents.data, T, agents); // } pub fn writeBuffer( gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle, comptime T: type, structs: []T, ) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, T, structs); } pub fn setAgentParameter( comptime T: type, agent: *T, parameter: agentParameters(T), ) void { switch (T) { Consumer => { switch (parameter) { .moving_rate => |v| agent.moving_rate = v, .demand_rate => |v| agent.demand_rate = v, } }, Producer => { switch (parameter) { .production_rate => |v| agent.production_rate = v, .inventory => |v| agent.inventory = v, .max_inventory => |v| agent.max_inventory = v, } }, else => unreachable, } } pub fn setGroupingArgs(comptime T: type) type { return struct { setArgs: setArgs(T), grouping_id: u32, }; } //pub fn setGroup(gctx: *zgpu.GraphicsContext, comptime T: type, args: setGroupingArgs(T)) void { // var agents = getAll(gctx, T, .{ // .structs = args.setArgs.agents, // .num_structs = args.setArgs.num_structs, // }) catch return; // for (agents, 0..) |agent, i| { // if (args.grouping_id == agent.grouping_id) { // setAgentParameter(T, &agents[i], args.setArgs.parameter); // } // } // writeBuffer(gctx, args.setArgs.agents.data, T, agents); //} pub fn updateCoords(gctx: *zgpu.GraphicsContext, comptime T: type, obj_buf: ObjectBuffer(T)) void { for (obj_buf.list.items, 0..) |obj, i| { const args: bufArgs(T, [4]f32) = .{ .obj_buf = obj_buf, .index = i, .value = Camera.getWorldPosition(gctx, obj.absolute_home), }; writeToObjectBuffer(gctx, T, [4]f32, "home", args); if (T == Consumer) { writeToObjectBuffer(gctx, T, [4]f32, "position", args); writeToObjectBuffer(gctx, T, [4]f32, "destination", args); } } } pub fn bufArgs(comptime T: type, comptime V: type) type { return struct { obj_buf: ObjectBuffer(T), index: usize, value: V, }; } pub fn writeToObjectBuffer( gctx: *Gctx, comptime T: type, comptime V: type, comptime field: []const u8, args: bufArgs(T, V), ) void { gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.mapping.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); } pub fn writeToMappedBuffer(gctx: *Gctx, buf: zgpu.BufferHandle, mapped: zgpu.BufferHandle) void { const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); pass: { const p = gctx.lookupResource(buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(buf) orelse break :pass; const pm = gctx.lookupResource(mapped) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); } pub const shrinkArgs = struct { new_size: u32, buf: zgpu.BufferHandle, }; pub fn shrinkBuffer(gctx: *Gctx, comptime T: type, args: shrinkArgs) void { const all_zero = [_]u8{0} ** 10000000; const buf = gctx.lookupResource(args.buf).?; const buf_info = gctx.lookupResourceInfo(args.buf).?; const size_to_keep = @sizeOf(T) * args.new_size; const size_to_clear = buf_info.size - size_to_keep; const usize_to_clear = @as(usize, @intCast(size_to_clear)); gctx.queue.writeBuffer( buf, size_to_keep, u8, all_zero[0..usize_to_clear], ); } pub fn appendArgs(comptime T: type) type { return struct { num_old_structs: u32, buf: zgpu.BufferHandle, structs: []T, }; } pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void { gctx.queue.writeBuffer( gctx.lookupResource(args.buf).?, args.num_old_structs * @sizeOf(T), T, args.structs, ); } pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 0, u8, all_zero[0..b_size], ); } pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.buf).?, 0, u8, all_zero[0..b_size], ); const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?; const m_size = @as(usize, @intCast(map_buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.mapping.buf).?, 0, u8, all_zero[0..m_size], ); obj_buf.list.clearAndFree(); obj_buf.mapping.insert_idx = 0; obj_buf.mapping.remove_idx = 0; obj_buf.mapping.waiting = false; obj_buf.mapping.staging.slice = null; obj_buf.mapping.num_structs = 0; obj_buf.mapping.staging.num_structs = 0; } // Blank Buffers pub fn createBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .copy_src = true, .vertex = true, .storage = true }, .size = num * @sizeOf(T), }); } pub fn createMappedBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .map_read = true }, .size = num * @sizeOf(T), }); } pub fn createObjectBuffer( allocator: std.mem.Allocator, gctx: *Gctx, comptime T: type, len: u32, num_structs: u32, ) ObjectBuffer(T) { return .{ .buf = createBuffer(gctx, T, len), .list = std.ArrayList(T).init(allocator), .mapping = .{ .buf = createMappedBuffer(gctx, T, len), .num_structs = num_structs, .staging = .{ .num_structs = num_structs, }, }, }; } // Depth Texture pub const Depth = struct { texture: zgpu.TextureHandle, view: zgpu.TextureViewHandle, }; pub fn createDepthTexture(gctx: *zgpu.GraphicsContext) Depth { const texture = gctx.createTexture(.{ .usage = .{ .render_attachment = true }, .dimension = .tdim_2d, .size = .{ .width = gctx.swapchain_descriptor.width, .height = gctx.swapchain_descriptor.height, .depth_or_array_layers = 1, }, .format = .depth32_float, .mip_level_count = 1, .sample_count = 1, }); const view = gctx.createTextureView(texture, .{}); return .{ .texture = texture, .view = view }; } // Bind Group Layouts pub fn createUniformBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .vertex = true }, .uniform, true, 0), }); } pub fn createComputeBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(1, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(2, .{ .compute = true }, .storage, false, 0), }); } // Bind Groups pub fn createUniformBindGroup(gctx: *Gctx) zgpu.BindGroupHandle { const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); return gctx.createBindGroup(bind_group_layout, &.{ .{ .binding = 0, .buffer_handle = gctx.uniforms.buffer, .offset = 0, .size = @sizeOf(zm.Mat) }, }); } pub const computeBindGroup = struct { consumer: zgpu.BufferHandle, producer: zgpu.BufferHandle, stats: zgpu.BufferHandle, }; pub fn createComputeBindGroup(gctx: *Gctx, args: computeBindGroup) zgpu.BindGroupHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const c_info = gctx.lookupResourceInfo(args.consumer) orelse unreachable; const p_info = gctx.lookupResourceInfo(args.producer) orelse unreachable; const s_info = gctx.lookupResourceInfo(args.stats) orelse unreachable; return gctx.createBindGroup(compute_bgl, &[_]zgpu.BindGroupEntryInfo{ .{ .binding = 0, .buffer_handle = args.consumer, .offset = 0, .size = c_info.size, }, .{ .binding = 1, .buffer_handle = args.producer, .offset = 0, .size = p_info.size, }, .{ .binding = 2, .buffer_handle = args.stats, .offset = 0, .size = s_info.size, }, }); } fn getWgpuType(comptime T: type) !wgpu.VertexFormat { return switch (T) { u32 => .uint32, f32 => .float32, [2]f32 => .float32x2, [3]f32 => .float32x3, [4]f32 => .float32x4, else => error.NoValidWgpuType, }; } pub fn createRenderPipeline( gctx: *zgpu.GraphicsContext, comptime args: RenderPipelineInfo, ) zgpu.RenderPipelineHandle { const vs_module = zgpu.createWgslShaderModule(gctx.device, args.vs, "vs"); defer vs_module.release(); const fs_module = zgpu.createWgslShaderModule(gctx.device, args.fs, "fs"); defer fs_module.release(); const color_targets = [_]wgpu.ColorTargetState{.{ .format = zgpu.GraphicsContext.swapchain_format, .blend = &.{ .color = .{}, .alpha = .{} }, }}; const vertex_attributes = [_]wgpu.VertexAttribute{ .{ .format = .float32x3, .offset = 0, .shader_location = 0 }, }; const instance_attributes = init: { var arr: [args.inst_attrs.len]wgpu.VertexAttribute = undefined; inline for (args.inst_attrs, 0..) |attr, i| { arr[i] = .{ .format = getWgpuType(attr.type) catch unreachable, .offset = @offsetOf(args.inst_type, attr.name), .shader_location = i + 1, }; } break :init arr; }; const vertex_buffers = [_]wgpu.VertexBufferLayout{ .{ .array_stride = @sizeOf(f32) * 3, .attribute_count = vertex_attributes.len, .attributes = &vertex_attributes, .step_mode = .vertex, }, .{ .array_stride = @sizeOf(args.inst_type), .attribute_count = instance_attributes.len, .attributes = &instance_attributes, .step_mode = .instance, }, }; const pipeline_descriptor = wgpu.RenderPipelineDescriptor{ .vertex = wgpu.VertexState{ .module = vs_module, .entry_point = "main", .buffer_count = vertex_buffers.len, .buffers = &vertex_buffers, }, .primitive = wgpu.PrimitiveState{ .front_face = .ccw, .cull_mode = .none, .topology = args.primitive_topology, }, .depth_stencil = &wgpu.DepthStencilState{ .format = .depth32_float, .depth_write_enabled = true, .depth_compare = .less_equal, }, .fragment = &wgpu.FragmentState{ .module = fs_module, .entry_point = "main", .target_count = color_targets.len, .targets = &color_targets, }, }; const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); const pipeline_layout = gctx.createPipelineLayout(&.{bind_group_layout}); return gctx.createRenderPipeline(pipeline_layout, pipeline_descriptor); } pub fn createComputePipeline(gctx: *zgpu.GraphicsContext, cpi: ComputePipelineInfo) zgpu.ComputePipelineHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const compute_pl = gctx.createPipelineLayout(&.{compute_bgl}); defer gctx.releaseResource(compute_pl); const cs_module = zgpu.createWgslShaderModule(gctx.device, cpi.cs, "cs"); defer cs_module.release(); const pipeline_descriptor = wgpu.ComputePipelineDescriptor{ .compute = wgpu.ProgrammableStageDescriptor{ .module = cs_module, .entry_point = cpi.entry_point, }, }; return gctx.createComputePipeline(compute_pl, pipeline_descriptor); }
0
repos/simulations/src
repos/simulations/src/editor/square.zig
const zgpu = @import("zgpu"); pub fn createVertexBuffer(gctx: *zgpu.GraphicsContext, width: f32) zgpu.BufferHandle { const producer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = 6 * @sizeOf(f32) * 3, }); const upper_left = [3]f32{ -width, width, 0.0 }; const lower_left = [3]f32{ -width, -width, 0.0 }; const upper_right = [3]f32{ width, width, 0.0 }; const lower_right = [3]f32{ width, -width, 0.0 }; const vertex_array = [6][3]f32{ upper_left, lower_left, lower_right, lower_right, upper_right, upper_left, }; gctx.queue.writeBuffer(gctx.lookupResource(producer_vertex_buffer).?, 0, [3]f32, vertex_array[0..]); return producer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/editor/windows.zig
const std = @import("std"); const zgui = @import("zgui"); const zgpu = @import("zgpu"); pub const window_flags = .{ .popen = null, .flags = zgui.WindowFlags.no_decoration, }; pub const ParametersWindow = PercentArgs{ .x = 0.0, .y = 0.0, .w = 0.25, .h = 0.75, .margin = 0.02, }; pub const StatsWindow = PercentArgs{ .x = 0.0, .y = 0.75, .w = 1.0, .h = 0.25, .margin = 0.02, .no_margin = .{ .top = true }, }; pub const PercentArgs = struct { x: f32, y: f32, w: f32, h: f32, margin: f32, no_margin: struct { top: bool = false, bottom: bool = false, left: bool = false, right: bool = false, } = .{}, // flags: zgui.WindowFlags = .{}, }; pub fn setNextWindowSize( gctx: *zgpu.GraphicsContext, p_width: f32, p_height: f32, ) void { std.debug.assert(0.0 <= p_width and p_width <= 1.0); std.debug.assert(0.0 <= p_height and p_height <= 1.0); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const height = @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)); zgui.setNextWindowSize(.{ .w = width * p_width, .h = height * p_height, }); } pub fn setNextWindow(gctx: *zgpu.GraphicsContext, args: PercentArgs) void { std.debug.assert(0.0 <= args.x and args.x <= 1.0); std.debug.assert(0.0 <= args.y and args.y <= 1.0); std.debug.assert(0.0 <= args.w and args.w <= 1.0); std.debug.assert(0.0 <= args.h and args.h <= 1.0); std.debug.assert(0.0 <= args.margin and args.margin <= 1.0); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const height = @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)); const margin_x = width * args.margin; const margin_y = height * args.margin; const margin_pixels = @min(margin_x, margin_y); var x = width * args.x + margin_pixels; var y = height * args.y + margin_pixels; var w = width * args.w - (margin_pixels * 2); var h = height * args.h - (margin_pixels * 2); if (args.no_margin.top) { y -= margin_pixels; h += margin_pixels; } if (args.no_margin.bottom) { h += margin_pixels; } if (args.no_margin.left) { x -= margin_pixels; w += margin_pixels; } if (args.no_margin.right) { w += margin_pixels; } zgui.setNextWindowPos(.{ .x = x, .y = y, }); zgui.setNextWindowSize(.{ .w = w, .h = h, }); }
0
repos/simulations/src
repos/simulations/src/editor/callbacks.zig
const std = @import("std"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Statistics = @import("statistics.zig"); pub fn Args(comptime T: type) type { return struct { gctx: *zgpu.GraphicsContext, buf: *Wgpu.ObjectBuffer(T), stats: *Statistics = undefined, }; } pub fn numTransactions(args: Args(u32)) void { const gpu_stats = Wgpu.getMappedData(u32, &args.buf.mapping); args.stats.num_transactions.append(gpu_stats[0]) catch unreachable; Statistics.clearNumTransactions(args.gctx, args.buf.buf); } pub fn totalInventory(args: Args(Producer)) void { const producers = Wgpu.getMappedData(Producer, &args.buf.mapping); var total_inventory: u32 = 0; for (producers) |p| { total_inventory += @as(u32, @intCast(p.inventory)); } args.stats.num_total_producer_inventory.append(total_inventory) catch unreachable; } pub fn emptyConsumers(args: Args(Consumer)) void { const consumers = Wgpu.getMappedData(Consumer, &args.buf.mapping); var empty_consumers: u32 = 0; for (consumers) |c| { if (c.inventory == 0) { empty_consumers += 1; } } args.stats.num_empty_consumers.append(empty_consumers) catch unreachable; }
0
repos/simulations/src/editor
repos/simulations/src/editor/shaders/shaders.zig
// zig fmt: off pub const vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) demand_rate: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(demand_rate); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (vertex_position[0] * scale); \\ var y = position[1] + (vertex_position[1] * scale); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const producer_vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) max_inventory: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(max_inventory); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (scale * vertex_position[0]); \\ var y = position[1] + (scale * vertex_position[1]); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const fs = \\ @stage(fragment) fn main( \\ @location(0) color: vec3<f32>, \\ ) -> @location(0) vec4<f32> { \\ return vec4(color, 1.0); \\ } ; pub const cs = \\ struct Consumer { \\ position: vec4<f32>, \\ home: vec4<f32>, \\ absolute_home: vec4<f32>, \\ destination: vec4<f32>, \\ step_size: vec4<f32>, \\ color: vec4<f32>, \\ moving_rate: f32, \\ demand_rate: u32, \\ inventory: u32, \\ radius: f32, \\ producer_id: i32, \\ } \\ struct Producer { \\ position: vec4<f32>, \\ absolute_pos: vec4<f32>, \\ color: vec4<f32>, \\ production_rate: u32, \\ inventory: atomic<u32>, \\ max_inventory: u32, \\ len: atomic<u32>, \\ queue: array<u32, 480>, \\ } \\ struct Stats { \\ transactions: u32, \\ } \\ \\ @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; \\ @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; \\ @group(0) @binding(2) var<storage, read_write> stats: Stats; \\ @compute @workgroup_size(64) \\ fn consumer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let nc = arrayLength(&consumers); \\ if(GlobalInvocationID.x >= nc) { \\ return; \\ } \\ let c = consumers[index]; \\ consumers[index].position += c.step_size; \\ let dist = abs(c.position - c.destination); \\ let at_destination = all(dist.xy <= vec2<f32>(0.1)); \\ \\ if (at_destination) { \\ var new_destination = vec4<f32>(0); \\ let at_home = all(c.destination == c.home); \\ if (at_home) { \\ consumers[index].position = c.home; \\ let consumption_rate = 1u; \\ if (c.inventory >= consumption_rate) { \\ consumers[index].inventory -= consumption_rate; \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ return; \\ } \\ consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); \\ var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); \\ var shortest_distance = 100000.0; \\ var array_len = i32(arrayLength(&producers)); \\ for(var i = 0; i < array_len; i++){ \\ let dist = distance(c.home, producers[i].position); \\ let inventory = atomicLoad(&producers[i].inventory); \\ if (dist < shortest_distance && inventory > c.demand_rate) { \\ shortest_distance = dist; \\ consumers[index].destination = producers[i].position; \\ consumers[index].step_size = step_sizes(c.position, producers[i].position, c.moving_rate); \\ consumers[index].producer_id = i; \\ } \\ } \\ if (shortest_distance == 100000.0) { \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ } \\ } else { \\ let position = c.destination; \\ let pid = c.producer_id; \\ consumers[index].position = position; \\ consumers[index].step_size = vec4<f32>(0); \\ let idx = atomicAdd(&producers[pid].len, 1); \\ producers[pid].queue[idx] = index + 1; \\ } \\ } \\ } \\ fn step_sizes(pos: vec4<f32>, dest: vec4<f32>, mr: f32) -> vec4<f32>{ \\ let x_num_steps = num_steps(pos.x, dest.x, mr); \\ let y_num_steps = num_steps(pos.y, dest.y, mr); \\ let num_steps = max(x_num_steps, y_num_steps); \\ let distance = dest - pos; \\ return distance / num_steps; \\ } \\ fn num_steps(x: f32, y: f32, rate: f32) -> f32 { \\ let distance = abs(x - y); \\ if (rate > distance) { return 1.0; } \\ return ceil(distance / rate); \\ } \\ @compute @workgroup_size(64) \\ fn producer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let np = arrayLength(&producers); \\ if(GlobalInvocationID.x >= np) { \\ return; \\ } \\ let max_inventory = producers[index].max_inventory; \\ let inventory = atomicLoad(&producers[index].inventory); \\ var production_rate = producers[index].production_rate; \\ if (max_inventory > inventory) { \\ let diff = max_inventory - inventory; \\ production_rate = min(diff, production_rate); \\ let old_val = atomicAdd(&producers[index].inventory, production_rate); \\ } else if (inventory < max_inventory) { \\ atomicStore(&producers[index].inventory, max_inventory); \\ } \\ \\ let idx = atomicLoad(&producers[index].len); \\ for (var i = 0u; i < idx; i++) { \\ let cid = producers[index].queue[i] - 1; \\ let c = consumers[cid]; \\ let inventory = atomicLoad(&producers[index].inventory); \\ if (inventory >= c.demand_rate) { \\ consumers[cid].destination = c.home; \\ consumers[cid].step_size = step_sizes(c.position, c.home, c.moving_rate); \\ consumers[cid].inventory += c.demand_rate; \\ let old_inv = atomicSub(&producers[index].inventory, c.demand_rate); \\ stats.transactions += 1; \\ consumers[cid].color = vec4(0.0, 1.0, 0.0, 0.0); \\ } \\ } \\ atomicStore(&producers[index].len, 0); \\} ; // zig fmt: on
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/fragment/fragment.wgsl
@fragment fn main( @location(0) color: vec3<f32>, ) -> @location(0) vec4<f32> { return vec4(color, 1.0); }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/vertex/hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/vertex/producer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) max_inventory: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(max_inventory); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (scale * vertex_position[0]); var y = position[1] + (scale * vertex_position[1]); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/vertex/consumer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) demand_rate: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(demand_rate); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (vertex_position[0] * scale); var y = position[1] + (vertex_position[1] * scale); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/vertex/consumer_hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) hover_color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = hover_color.xyz; return output; }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/compute/common.wgsl
struct Consumer { absolute_home: vec4<i32>, position: vec4<f32>, home: vec4<f32>, destination: vec4<f32>, color: vec4<f32>, step_size: vec2<f32>, moving_rate: f32, demand_rate: u32, inventory: u32, radius: f32, producer_id: i32, grouping_id: u32, } struct Producer { absolute_home: vec4<i32>, home: vec4<f32>, color: vec4<f32>, production_rate: u32, inventory: atomic<i32>, max_inventory: u32, } struct Stats { transactions: u32, num_consumers: u32, num_producers: u32, num_consumer_hovers: u32, random_color: vec4<f32>, } @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; @group(0) @binding(2) var<storage, read_write> stats: Stats; fn step_sizes(pos: vec2<f32>, dest: vec2<f32>, mr: f32) -> vec2<f32>{ let x_num_steps = num_steps(pos.x, dest.x, mr); let y_num_steps = num_steps(pos.y, dest.y, mr); let num_steps = max(x_num_steps, y_num_steps); let distance = dest - pos; return distance / num_steps; } fn num_steps(x: f32, y: f32, rate: f32) -> f32 { let distance = abs(x - y); if (rate > distance) { return 1.0; } return ceil(distance / rate); }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/compute/producer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_producers) { return; } let max_inventory = i32(producers[index].max_inventory); var production_rate = i32(producers[index].production_rate); let old_inventory = atomicAdd(&producers[index].inventory, production_rate); if (old_inventory + production_rate > max_inventory) { atomicStore(&producers[index].inventory, max_inventory); } }
0
repos/simulations/src/editor/shaders
repos/simulations/src/editor/shaders/compute/consumer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_consumers) { return; } // User removed producer this consumer was targeting if (consumers[index].producer_id >= i32(stats.num_producers)) { search_for_producer(index); } let c = consumers[index]; consumers[index].position[0] += c.step_size[0]; consumers[index].position[1] += c.step_size[1]; let dist = abs(c.position - c.destination); let at_destination = all(dist.xy <= vec2<f32>(0.1)); if (at_destination) { consumers[index].step_size = vec2<f32>(0); consumers[index].position = c.destination; let at_home = all(c.destination == c.home); if (at_home) { if (c.inventory >= 1) { consumers[index].inventory -= 1; return; } consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); search_for_producer(index); return; } // At Producer let pid = c.producer_id; let i_demand_rate = i32(c.demand_rate); let old_val = atomicSub(&producers[pid].inventory, i_demand_rate); // Went negative, revert inventory if (i_demand_rate > old_val) { atomicAdd(&producers[pid].inventory, i_demand_rate); return; } consumers[index].color = vec4(0.0, 1.0, 0.0, 0.0); consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); consumers[index].inventory += c.demand_rate; consumers[index].producer_id = -1; stats.transactions += 1; } } fn search_for_producer(index: u32){ let c = consumers[index]; var pid = find_nearest_stocked_producer(c); if (pid == -1) { consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); return; } let p_pos = producers[pid].home; consumers[index].destination = p_pos; consumers[index].step_size = step_sizes(c.position.xy, p_pos.xy, c.moving_rate); consumers[index].producer_id = pid; } // Returns the pid of nearest stocked producer, -1 for failure fn find_nearest_stocked_producer(c: Consumer) -> i32 { var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); var shortest_distance = 100000.0; var pid: i32 = -1; for(var i: u32 = 0; i < stats.num_producers; i++){ let dist = distance(c.home, producers[i].home); let inventory = u32(atomicLoad(&producers[i].inventory)); if (dist < shortest_distance && inventory > c.demand_rate) { shortest_distance = dist; pid = i32(i); } } return pid; }
0
repos/simulations/src
repos/simulations/src/random/camera.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zmath = @import("zmath"); // Camera Settings pub const POS: [3]f32 = .{ 0.0, 0.0, -3000.0 }; pub const FOCUS: [3]f32 = .{ 0.0, 0.0, 0.0 }; pub const UP: [4]f32 = .{ 0.0, 1.0, 0.0, 0.0 }; pub const FOV_Y: f32 = 0.22 * std.math.pi; pub const NEAR_PLANE: f32 = 0.01; pub const FAR_PLANE: f32 = 3000.0; // Grid limits for absolute positions (without aspect ratio) pub const MAX_X: i32 = 1000; pub const MIN_X: i32 = -1000; pub const MAX_Y: i32 = 1000; pub const MIN_Y: i32 = -1000; pub const TOTAL_X: i32 = 2000; pub const TOTAL_Y: i32 = 2000; // Viewport size relative to total window size pub const VP_X_SIZE: f32 = 0.75; pub const VP_Y_SIZE: f32 = 0.75; pub fn getViewportPixelSize(gctx: *zgpu.GraphicsContext) [2]f32 { return .{ @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)) * VP_X_SIZE, @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)) * VP_Y_SIZE, }; } pub fn getAspectRatio(gctx: *zgpu.GraphicsContext) f32 { const sd = gctx.swapchain_descriptor; return @as(f32, @floatFromInt(sd.width)) / @as(f32, @floatFromInt(sd.height)); } // Given a world position (grid position with aspect), return grid position pub fn getGridFromWorld(gctx: *zgpu.GraphicsContext, world_pos: [2]f32) [2]i32 { const aspect = getAspectRatio(gctx); return .{ @as(i32, @intFromFloat(world_pos[0] / aspect)), @as(i32, @intFromFloat(world_pos[1])), // world_pos[2], // world_pos[3], }; } // Given a grid position, return a world position pub fn getWorldPosition(gctx: *zgpu.GraphicsContext, grid_pos: [4]i32) [4]f32 { const aspect = getAspectRatio(gctx); return .{ @as(f32, @floatFromInt(grid_pos[0])) * aspect, @as(f32, @floatFromInt(grid_pos[1])), @as(f32, @floatFromInt(grid_pos[2])), @as(f32, @floatFromInt(grid_pos[3])), }; } // Given a grid position, return a pixel position pub fn getPixelPosition(gctx: *zgpu.GraphicsContext, g_pos: [2]i32) [2]f32 { const grid_pos = .{ g_pos[0], g_pos[1], 1, 1 }; const world_pos = zmath.loadArr4(getWorldPosition(gctx, grid_pos)); const camera_pos = zmath.mul(world_pos, getObjectToClipMat(gctx)); const rel_pos = [4]f32{ camera_pos[0] / -POS[2], camera_pos[1] / -POS[2], 0, 1 }; const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const cursor_in_vp_x = ((rel_pos[0] + 1) * viewport_size[0]) / 2; const cursor_in_vp_y = ((-rel_pos[1] + 1) * viewport_size[1]) / 2; return .{ cursor_in_vp_x + xOffset, cursor_in_vp_y }; } // Given a pixel position, return a grid position pub fn getGridPosition(gctx: *zgpu.GraphicsContext, p_pos: [2]f32) [2]i32 { const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const rel_pos_x = (((p_pos[0] - xOffset) * 2) / viewport_size[0]) - 1; const rel_pos_y = ((p_pos[1] * 2) / viewport_size[1]) - 1; const camera_pos = zmath.loadArr4(.{ rel_pos_x * -POS[2], rel_pos_y * POS[2], 1, 1, }); const inverse_mat = zmath.inverse(getObjectToClipMat(gctx)); const world_pos = zmath.mul(camera_pos, inverse_mat); return getGridFromWorld(gctx, .{ world_pos[0], world_pos[1] }); } pub fn getObjectToClipMat(gctx: *zgpu.GraphicsContext) zmath.Mat { const camWorldToView = zmath.lookAtLh( zmath.loadArr3(POS), zmath.loadArr3(FOCUS), zmath.loadArr4(UP), ); const camViewToClip = zmath.perspectiveFovLh( FOV_Y, getAspectRatio(gctx), NEAR_PLANE, FAR_PLANE, ); const camWorldToClip = zmath.mul(camWorldToView, camViewToClip); // return zmath.transpose(camWorldToClip); return camWorldToClip; }
0
repos/simulations/src
repos/simulations/src/random/main.zig
const std = @import("std"); const math = std.math; const zglfw = @import("zglfw"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const zgui = @import("zgui"); const zm = @import("zmath"); const zstbi = @import("zstbi"); const Statistics = @import("statistics.zig"); const gui = @import("gui.zig"); const Wgpu = @import("wgpu.zig"); const config = @import("config.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Square = @import("square.zig"); const Circle = @import("circle.zig"); pub const MAX_NUM_PRODUCERS = 100; pub const MAX_NUM_CONSUMERS = 10000; pub const NUM_CONSUMER_SIDES = 40; pub const PRODUCER_WIDTH = 40; pub const DemoState = struct { gctx: *zgpu.GraphicsContext, window: *zglfw.Window, allocator: std.mem.Allocator, running: bool = false, push_coord_update: bool = false, push_restart: bool = false, content_scale: f32, render_pipelines: struct { circle: zgpu.RenderPipelineHandle, square: zgpu.RenderPipelineHandle, }, compute_pipelines: struct { consumer: zgpu.ComputePipelineHandle, producer: zgpu.ComputePipelineHandle, }, bind_groups: struct { render: zgpu.BindGroupHandle, compute: zgpu.BindGroupHandle, }, buffers: struct { data: struct { consumers: Wgpu.ObjectBuffer(Consumer), producers: Wgpu.ObjectBuffer(Producer), stats: Wgpu.ObjectBuffer(u32), }, index: struct { circle: zgpu.BufferHandle, }, vertex: struct { circle: zgpu.BufferHandle, square: zgpu.BufferHandle, }, }, depth_texture: zgpu.TextureHandle, depth_texture_view: zgpu.TextureViewHandle, params: Parameters, stats: Statistics, }; pub const Parameters = struct { max_num_stats: u32 = 3, num_producers: struct { old: u32 = 6, new: u32 = 6, }, num_consumers: struct { old: u32 = 5000, new: u32 = 5000, }, production_rate: u32 = 300, demand_rate: u32 = 100, max_inventory: u32 = 10000, moving_rate: f32 = 5.0, consumer_radius: f32 = 20.0, num_consumer_sides: u32 = 20, aspect: f32, }; pub fn init( gctx: *zgpu.GraphicsContext, allocator: std.mem.Allocator, window: *zglfw.Window, ) !DemoState { const params = Parameters{ .aspect = Camera.getAspectRatio(gctx), .num_producers = .{}, .num_consumers = .{}, }; var consumer_object = Wgpu.createObjectBuffer( allocator, gctx, Consumer, MAX_NUM_CONSUMERS, 0, ); Consumer.generateBulk( gctx, &consumer_object, params, params.num_consumers.new, ); var producer_object = Wgpu.createObjectBuffer( allocator, gctx, Producer, MAX_NUM_PRODUCERS, 0, ); Producer.generateBulk( gctx, &producer_object, params, params.num_producers.new, ); const stats_object = Wgpu.createObjectBuffer( allocator, gctx, u32, Statistics.NUM_STATS, Statistics.NUM_STATS, ); Statistics.setNum(gctx, .{ .stat_obj = stats_object, .num = params.num_consumers.new, .param = .consumers, }); Statistics.setNum(gctx, .{ .stat_obj = stats_object, .num = params.num_producers.new, .param = .producers, }); const compute_bind_group = Wgpu.createComputeBindGroup(gctx, .{ .consumer = consumer_object.buf, .producer = producer_object.buf, .stats = stats_object.buf, }); const depth = Wgpu.createDepthTexture(gctx); return DemoState{ .gctx = gctx, .window = window, .content_scale = getContentScale(window), .render_pipelines = .{ .circle = Wgpu.createRenderPipeline(gctx, config.cpi), .square = Wgpu.createRenderPipeline(gctx, config.ppi), }, .compute_pipelines = .{ .producer = Wgpu.createComputePipeline(gctx, config.pcpi), .consumer = Wgpu.createComputePipeline(gctx, config.ccpi), }, .bind_groups = .{ .render = Wgpu.createUniformBindGroup(gctx), .compute = compute_bind_group, }, .buffers = .{ .data = .{ .consumers = consumer_object, .producers = producer_object, .stats = stats_object, }, .index = .{ .circle = Circle.createIndexBuffer(gctx, NUM_CONSUMER_SIDES), }, .vertex = .{ .circle = Circle.createVertexBuffer( gctx, NUM_CONSUMER_SIDES, params.consumer_radius, ), .square = Square.createVertexBuffer(gctx, PRODUCER_WIDTH), }, }, .depth_texture = depth.texture, .depth_texture_view = depth.view, .allocator = allocator, .params = params, .stats = Statistics.init(allocator), }; } pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { if (demo.push_restart) restartSimulation(demo); if (demo.push_coord_update) updateAspectRatio(demo); gui.update(demo, selection_gui); } pub fn draw(demo: *DemoState) void { const gctx = demo.gctx; const cam_world_to_clip = Camera.getObjectToClipMat(gctx); const back_buffer_view = gctx.swapchain.getCurrentTextureView(); defer back_buffer_view.release(); const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); const data = demo.buffers.data; const num_consumers = @as(u32, @intCast(data.consumers.list.items.len)); const num_producers = @as(u32, @intCast(data.producers.list.items.len)); // Compute shaders if (demo.running) { pass: { const pcp = gctx.lookupResource(demo.compute_pipelines.producer) orelse break :pass; const ccp = gctx.lookupResource(demo.compute_pipelines.consumer) orelse break :pass; const bg = gctx.lookupResource(demo.bind_groups.compute) orelse break :pass; const pass = encoder.beginComputePass(null); defer { pass.end(); pass.release(); } pass.setBindGroup(0, bg, &.{}); pass.setPipeline(pcp); pass.dispatchWorkgroups( @divFloor(num_producers, 64) + 1, 1, 1, ); pass.setPipeline(ccp); pass.dispatchWorkgroups( @divFloor(num_consumers, 64) + 1, 1, 1, ); } } // Copy data to mapped buffers so we can retrieve it on demand pass: { if (!demo.buffers.data.stats.mapping.waiting) { const s = gctx.lookupResource(data.stats.buf) orelse break :pass; const s_info = gctx.lookupResourceInfo(data.stats.buf) orelse break :pass; const sm = gctx.lookupResource(data.stats.mapping.buf) orelse break :pass; const s_size = @as(usize, @intCast(s_info.size)); encoder.copyBufferToBuffer(s, 0, sm, 0, s_size); } if (!demo.buffers.data.producers.mapping.waiting) { const p = gctx.lookupResource(data.producers.buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(data.producers.buf) orelse break :pass; const pm = gctx.lookupResource(data.producers.mapping.buf) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } if (!demo.buffers.data.consumers.mapping.waiting) { const c = gctx.lookupResource(data.consumers.buf) orelse break :pass; const c_info = gctx.lookupResourceInfo(data.consumers.buf) orelse break :pass; const cm = gctx.lookupResource(data.consumers.mapping.buf) orelse break :pass; const c_size = @as(usize, @intCast(c_info.size)); encoder.copyBufferToBuffer(c, 0, cm, 0, c_size); } } // Draw the circles and squares in our defined viewport pass: { const svb_info = gctx.lookupResourceInfo(demo.buffers.vertex.square) orelse break :pass; const pb_info = gctx.lookupResourceInfo(demo.buffers.data.producers.buf) orelse break :pass; const cvb_info = gctx.lookupResourceInfo(demo.buffers.vertex.circle) orelse break :pass; const cb_info = gctx.lookupResourceInfo(demo.buffers.data.consumers.buf) orelse break :pass; const cib_info = gctx.lookupResourceInfo(demo.buffers.index.circle) orelse break :pass; const square_rp = gctx.lookupResource(demo.render_pipelines.square) orelse break :pass; const circle_rp = gctx.lookupResource(demo.render_pipelines.circle) orelse break :pass; const render_bind_group = gctx.lookupResource(demo.bind_groups.render) orelse break :pass; const depth_view = gctx.lookupResource(demo.depth_texture_view) orelse break :pass; const color_attachments = [_]wgpu.RenderPassColorAttachment{.{ .view = back_buffer_view, .load_op = .clear, .store_op = .store, }}; const depth_attachment = wgpu.RenderPassDepthStencilAttachment{ .view = depth_view, .depth_load_op = .clear, .depth_store_op = .store, .depth_clear_value = 1.0, }; const render_pass_info = wgpu.RenderPassDescriptor{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, .depth_stencil_attachment = &depth_attachment, }; const pass = encoder.beginRenderPass(render_pass_info); defer { pass.end(); pass.release(); } const sd = gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const xOffset = width / 4; const height = @as(f32, @floatFromInt(sd.height)); const yOffset = height / 4; pass.setViewport(xOffset, 0, width - xOffset, height - yOffset, 0, 1); var mem = gctx.uniformsAllocate(zm.Mat, 1); mem.slice[0] = cam_world_to_clip; pass.setBindGroup(0, render_bind_group, &.{mem.offset}); const num_indices_circle = @as( u32, @intCast(cib_info.size / @sizeOf(f32)), ); pass.setPipeline(circle_rp); pass.setVertexBuffer(0, cvb_info.gpuobj.?, 0, cvb_info.size); pass.setVertexBuffer(1, cb_info.gpuobj.?, 0, cb_info.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, num_consumers, 0, 0, 0); pass.setPipeline(square_rp); pass.setVertexBuffer(0, svb_info.gpuobj.?, 0, svb_info.size); pass.setVertexBuffer(1, pb_info.gpuobj.?, 0, pb_info.size); pass.draw(6, num_producers, 0, 0); } // Draw ImGui { const pass = zgpu.beginRenderPassSimple( encoder, .load, back_buffer_view, null, null, null, ); defer zgpu.endReleasePass(pass); zgui.backend.draw(pass); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); if (demo.gctx.present() == .swap_chain_resized) { demo.content_scale = getContentScale(demo.window); setImguiContentScale(demo.content_scale); updateAspectRatio(demo); } } pub fn restartSimulation(demo: *DemoState) void { const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; const stats_waiting = demo.buffers.data.stats.mapping.waiting; if (consumer_waiting or producer_waiting or stats_waiting) { demo.push_restart = true; return; } Wgpu.clearObjBuffer(demo.gctx, Consumer, &demo.buffers.data.consumers); Wgpu.clearObjBuffer(demo.gctx, Producer, &demo.buffers.data.producers); Wgpu.clearObjBuffer(demo.gctx, u32, &demo.buffers.data.stats); demo.buffers.data.stats.mapping.num_structs = Statistics.NUM_STATS; Consumer.generateBulk( demo.gctx, &demo.buffers.data.consumers, demo.params, demo.params.num_consumers.old, ); Producer.generateBulk( demo.gctx, &demo.buffers.data.producers, demo.params, demo.params.num_producers.new, ); Statistics.setNum(demo.gctx, .{ .stat_obj = demo.buffers.data.stats, .num = demo.params.num_consumers.new, .param = .consumers, }); Statistics.setNum(demo.gctx, .{ .stat_obj = demo.buffers.data.stats, .num = demo.params.num_producers.new, .param = .producers, }); demo.stats.clear(); demo.push_restart = false; } pub fn updateDepthTexture(demo: *DemoState) void { // Release old depth texture. demo.gctx.releaseResource(demo.depth_texture_view); demo.gctx.destroyResource(demo.depth_texture); // Create a new depth texture to match the new window size. const depth = Wgpu.createDepthTexture(demo.gctx); demo.depth_texture = depth.texture; demo.depth_texture_view = depth.view; } pub fn updateAspectRatio(demo: *DemoState) void { updateDepthTexture(demo); const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; if (consumer_waiting or producer_waiting) { demo.push_coord_update = true; return; } Wgpu.updateCoords(demo.gctx, Consumer, demo.buffers.data.consumers); Wgpu.updateCoords(demo.gctx, Producer, demo.buffers.data.producers); demo.push_coord_update = false; demo.params.aspect = Camera.getAspectRatio(demo.gctx); } fn getContentScale(window: *zglfw.Window) f32 { const content_scale = window.getContentScale(); return @max(content_scale[0], content_scale[1]); } fn setImguiContentScale(scale: f32) void { zgui.getStyle().* = zgui.Style.init(); zgui.getStyle().scaleAllSizes(scale); } pub fn deinit(demo: *DemoState) void { demo.stats.deinit(); demo.buffers.data.consumers.list.deinit(); demo.buffers.data.producers.list.deinit(); demo.buffers.data.stats.list.deinit(); }
0
repos/simulations/src
repos/simulations/src/random/statistics.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); } pub fn generateAndFillRandomColor(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 4 * @sizeOf(u32), f32, &.{ random.float(f32), random.float(f32), random.float(f32) }, ); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const setArgs = struct { stat_obj: Wgpu.ObjectBuffer(u32), num: u32, param: enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }, }; pub fn setNum(gctx: *zgpu.GraphicsContext, args: setArgs) void { gctx.queue.writeBuffer( gctx.lookupResource(args.stat_obj.buf).?, @intFromEnum(args.param) * @sizeOf(u32), u32, &.{args.num}, ); }
0
repos/simulations/src
repos/simulations/src/random/config.zig
const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); pub const cpi = .{ .vs = @embedFile("shaders/vertex/consumer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Consumer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "position", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "demand_rate", .type = u32, }, }, }; pub const ppi = .{ .vs = @embedFile("shaders/vertex/producer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Producer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "home", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "max_inventory", .type = u32, }, }, }; const common = @embedFile("shaders/compute/common.wgsl"); pub const ccpi = .{ .cs = common ++ @embedFile("shaders/compute/consumer.wgsl"), .entry_point = "main", }; pub const pcpi = .{ .cs = common ++ @embedFile("shaders/compute/producer.wgsl"), .entry_point = "main", };
0
repos/simulations/src
repos/simulations/src/random/producer.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const Allocator = std.mem.Allocator; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const DemoState = @import("main.zig"); const Parameters = DemoState.Parameters; const Camera = @import("camera.zig"); const Self = @This(); absolute_home: [4]i32, home: [4]f32, color: [4]f32, production_rate: u32, inventory: i32, max_inventory: u32, _padding1: u32 = 0, pub const z_pos = 0; pub const Parameter = enum { production_rate, supply_shock, max_inventory, }; pub const DEFAULT_PRODUCTION_RATE: u32 = 300; pub const DEFAULT_MAX_INVENTORY: u32 = 10000; pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = .{ 1, 1, 1, 0 }, production_rate: u32 = DEFAULT_PRODUCTION_RATE, inventory: i32 = 0, max_inventory: u32 = DEFAULT_MAX_INVENTORY, }; pub fn generateBulk( gctx: *zgpu.GraphicsContext, obj_buf: *Wgpu.ObjectBuffer(Self), params: Parameters, num: u32, ) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); createAndAppend(gctx, .{ .obj_buf = obj_buf, .producer = .{ .absolute_home = .{ x, y }, .home = [2]f32{ @as(f32, @floatFromInt(x)) * params.aspect, @as(f32, @floatFromInt(y)) }, .production_rate = params.production_rate, .inventory = @as(i32, @intCast(params.max_inventory)), .max_inventory = params.max_inventory, }, }); i += 1; } // Wgpu.writeToMappedBuffer(gctx, obj_buf.buf, obj_buf.mapping.buf); } pub const AppendArgs = struct { producer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const home: [4]f32 = .{ args.producer.home[0], args.producer.home[1], z_pos, 1, }; const absolute_home: [4]i32 = .{ args.producer.absolute_home[0], args.producer.absolute_home[1], z_pos, 1, }; const producer = Self{ .absolute_home = absolute_home, .home = home, .color = args.producer.color, .production_rate = args.producer.production_rate, .inventory = args.producer.inventory, .max_inventory = args.producer.max_inventory, }; var producers: [1]Self = .{producer}; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.obj_buf.list.items.len)), .buf = args.obj_buf.buf, .structs = producers[0..], }); args.obj_buf.list.append(producers[0]) catch unreachable; args.obj_buf.mapping.num_structs += 1; } // pub const updateCoordsArgs = struct { // producers: Wgpu.ObjectBuffer, // stats: Wgpu.ObjectBuffer, // }; // pub fn updateCoords(gctx: *zgpu.GraphicsContext, args: updateCoordsArgs) void { // const producers = Wgpu.getAll(gctx, Self, .{ // .structs = args.producers, // .num_structs = Wgpu.getNumStructs(gctx, Self, args.stats), // }) catch return; // var new_producers: [DemoState.MAX_NUM_PRODUCERS]Self = undefined; // for (producers, 0..) |p, i| { // new_producers[i] = p; // new_producers[i].home = Camera.getWorldPosition(gctx, p.absolute_home); // } // gctx.queue.writeBuffer( // gctx.lookupResource(args.producers.data).?, // 0, // Self, // new_producers[0..producers.len], // ); // }
0
repos/simulations/src
repos/simulations/src/random/circle.zig
const std = @import("std"); const math = std.math; const zgpu = @import("zgpu"); const Self = @This(); position: [4]f32, color: [4]f32, radius: f32, pub fn createIndexBuffer(gctx: *zgpu.GraphicsContext, comptime num_vertices: u32) zgpu.BufferHandle { const num_triangles = num_vertices - 1; const consumer_index_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .index = true }, .size = num_triangles * 3 * @sizeOf(u32), }); const num_indices = num_triangles * 3; var indices: [num_indices]u32 = undefined; var i: usize = 0; while (i < num_triangles) { indices[i * 3] = 0; indices[i * 3 + 1] = @as(u32, @intCast(i)) + 1; indices[i * 3 + 2] = @as(u32, @intCast(i)) + 2; i += 1; } indices[num_indices - 1] = 1; gctx.queue.writeBuffer(gctx.lookupResource(consumer_index_buffer).?, 0, u32, indices[0..]); return consumer_index_buffer; } pub fn createVertexBuffer( gctx: *zgpu.GraphicsContext, comptime num_vertices: u32, radius: f32, ) zgpu.BufferHandle { const consumer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = num_vertices * @sizeOf(f32) * 3, }); var consumer_vertex_data: [num_vertices][3]f32 = undefined; const num_sides = @as(f32, num_vertices - 1); const angle = 2 * math.pi / num_sides; consumer_vertex_data[0] = [3]f32{ 0, 0, 0 }; var i: u32 = 1; while (i < num_vertices) { const current_angle = angle * @as(f32, @floatFromInt(i)); const x = @cos(current_angle) * radius; const y = @sin(current_angle) * radius; consumer_vertex_data[i] = [3]f32{ x, y, 0 }; i += 1; } gctx.queue.writeBuffer(gctx.lookupResource(consumer_vertex_buffer).?, 0, [3]f32, consumer_vertex_data[0..]); return consumer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/random/consumer.zig
const std = @import("std"); const math = std.math; const array = std.ArrayList; const Allocator = std.mem.Allocator; const random = std.crypto.random; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const DemoState = @import("main.zig"); const Parameters = DemoState.Parameters; const Wgpu = @import("wgpu.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Self = @This(); pub const defaults = DEFAULTS{}; const DEFAULTS = struct { color: [4]f32 = .{ 1, 0, 0, 0 }, moving_rate: f32 = 5.0, demand_rate: u32 = 100, radius: f32 = 20.0, }; absolute_home: [4]i32, position: [4]f32, home: [4]f32, destination: [4]f32, color: [4]f32 = defaults.color, step_size: [2]f32 = .{ 0, 0 }, moving_rate: f32, demand_rate: u32, inventory: u32 = 0, radius: f32, producer_id: i32 = -1, grouping_id: u32 = 0, pub const z_pos = 0; pub fn generateBulk( gctx: *zgpu.GraphicsContext, obj_buf: *Wgpu.ObjectBuffer(Self), params: Parameters, num: u32, ) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); const aspect_home = [2]f32{ @as(f32, @floatFromInt(x)) * params.aspect, @as(f32, @floatFromInt(y)), }; createAndAppend(gctx, .{ .consumer = .{ .absolute_home = .{ x, y }, .home = aspect_home, .moving_rate = params.moving_rate, .demand_rate = params.demand_rate, .radius = params.consumer_radius, }, .obj_buf = obj_buf, }); i += 1; } } pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = defaults.color, moving_rate: f32 = defaults.moving_rate, demand_rate: u32 = defaults.demand_rate, radius: f32 = defaults.radius, grouping_id: u32 = 0, }; pub const AppendArgs = struct { consumer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const home: [4]f32 = .{ args.consumer.home[0], args.consumer.home[1], z_pos, 1, }; const absolute_home: [4]i32 = .{ args.consumer.absolute_home[0], args.consumer.absolute_home[1], z_pos, 1, }; const consumer = Self{ .absolute_home = absolute_home, .position = home, .home = home, .destination = home, .color = args.consumer.color, .moving_rate = args.consumer.moving_rate, .demand_rate = args.consumer.demand_rate, .radius = args.consumer.radius, .grouping_id = args.consumer.grouping_id, }; var consumers: [1]Self = .{consumer}; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.obj_buf.list.items.len)), .buf = args.obj_buf.buf, .structs = consumers[0..], }); args.obj_buf.list.append(consumers[0]) catch unreachable; args.obj_buf.mapping.num_structs += 1; }
0
repos/simulations/src
repos/simulations/src/random/build.zig
const std = @import("std"); const Options = @import("../../../build.zig").Options; pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = "Simulations", .root_source_file = b.path("src/resources/random/main.zig"), .target = options.target, .optimize = options.optimize, }); @import("system_sdk").addLibraryPathsTo(exe); const zglfw = b.dependency("zglfw", .{ .target = options.target, }); exe.root_module.addImport("zglfw", zglfw.module("root")); exe.linkLibrary(zglfw.artifact("glfw")); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{ .target = options.target, }); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); const zmath = b.dependency("zmath", .{ .target = options.target, }); exe.root_module.addImport("zmath", zmath.module("root")); const zgui = b.dependency("zgui", .{ .target = options.target, .backend = .glfw_wgpu, }); exe.root_module.addImport("zgui", zgui.module("root")); exe.linkLibrary(zgui.artifact("imgui")); const zpool = b.dependency("zpool", .{ .target = options.target, }); exe.root_module.addImport("zpool", zpool.module("root")); const zstbi = b.dependency("zstbi", .{ .target = options.target, }); exe.root_module.addImport("zstbi", zstbi.module("root")); exe.linkLibrary(zstbi.artifact("zstbi")); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "" }, .install_subdir = "bin/content", }); exe.step.dependOn(&install_content_step.step); return exe; } inline fn thisDir() []const u8 { return comptime std.fs.path.dirname(@src().file) orelse "."; }
0
repos/simulations/src
repos/simulations/src/random/gui.zig
const std = @import("std"); const random = std.crypto.random; const zgpu = @import("zgpu"); const zgui = @import("zgui"); const wgpu = zgpu.wgpu; const Main = @import("main.zig"); const DemoState = Main.DemoState; const Statistics = @import("statistics.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); const Window = @import("windows.zig"); const Circle = @import("circle.zig"); const Callbacks = @import("callbacks.zig"); pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { const gctx = demo.gctx; Window.setNextWindow(gctx, Window.ParametersWindow); if (zgui.begin("Parameters", Window.window_flags)) { zgui.pushIntId(2); selection_gui(); parameters(demo, gctx); zgui.popId(); } zgui.end(); Window.setNextWindow(gctx, Window.StatsWindow); if (zgui.begin("Data", Window.window_flags)) { zgui.pushIntId(3); plots(demo); zgui.popId(); } zgui.end(); Wgpu.runCallbackIfReady(u32, &demo.buffers.data.stats.mapping); Wgpu.runCallbackIfReady(Producer, &demo.buffers.data.producers.mapping); Wgpu.runCallbackIfReady(Consumer, &demo.buffers.data.consumers.mapping); if (demo.running) { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.stats.buf).?, 3 * @sizeOf(u32), f32, &.{ random.float(f32), random.float(f32), random.float(f32) }, ); const current_time = @as(f32, @floatCast(gctx.stats.time)); const seconds_passed = current_time - demo.stats.second; if (seconds_passed >= 1) { demo.stats.second = current_time; Wgpu.getAllAsync(u32, Callbacks.numTransactions, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.stats, .stats = &demo.stats, }); Wgpu.getAllAsync(Consumer, Callbacks.emptyConsumers, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.consumers, .stats = &demo.stats, }); Wgpu.getAllAsync(Producer, Callbacks.totalInventory, .{ .gctx = demo.gctx, .buf = &demo.buffers.data.producers, .stats = &demo.stats, }); } } } fn plots(demo: *DemoState) void { const window_size = zgui.getWindowSize(); const margin = 15; const plot_width = window_size[0] - margin; const plot_height = window_size[1] - margin; if (zgui.plot.beginPlot("", .{ .w = plot_width, .h = plot_height, .flags = .{}, })) { zgui.plot.setupAxis(.x1, .{ .label = "", .flags = .{ .auto_fit = true }, }); zgui.plot.setupAxis(.y1, .{ .label = "", .flags = .{ .auto_fit = true }, }); zgui.plot.setupLegend(.{ .north = true, .west = true }, .{}); zgui.plot.plotLineValues("Transactions", u32, .{ .v = demo.stats.num_transactions.items[0..], }); zgui.plot.plotLineValues("Empty Consumers", u32, .{ .v = demo.stats.num_empty_consumers.items[0..], }); zgui.plot.plotLineValues("Total Producer Inventory", u32, .{ .v = demo.stats.num_total_producer_inventory.items[0..], }); zgui.plot.endPlot(); } } fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void { zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); zgui.text("Number Of Producers", .{}); const p_bufs = demo.buffers.data.producers; if (zgui.sliderScalar( "##np", u32, .{ .v = &demo.params.num_producers.new, .min = 1, .max = 100 }, )) { const num_producers = demo.params.num_producers; Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = num_producers.new, .param = .producers, }); if (num_producers.old >= num_producers.new) { Wgpu.shrinkBuffer(gctx, Producer, .{ .new_size = num_producers.new, .buf = p_bufs.buf, }); demo.buffers.data.producers.list.resize(num_producers.new) catch unreachable; demo.buffers.data.producers.mapping.num_structs = num_producers.new; } else { Producer.generateBulk( gctx, &demo.buffers.data.producers, demo.params, num_producers.new - num_producers.old, ); } demo.params.num_producers.old = demo.params.num_producers.new; } zgui.text("Production Rate", .{}); if (zgui.sliderScalar( "##pr", u32, .{ .v = &demo.params.production_rate, .min = 1, .max = 1000 }, )) { for (demo.buffers.data.producers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "production_rate"), u32, &.{demo.params.production_rate}, ); } } zgui.text("Demand Rate", .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted( "How much consumers take from producers on a trip.", ); zgui.endTooltip(); } if (zgui.sliderScalar( "##dr", u32, .{ .v = &demo.params.demand_rate, .min = 1, .max = 1000 }, )) { for (demo.buffers.data.consumers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.consumers.buf).?, i * @sizeOf(Consumer) + @offsetOf(Consumer, "demand_rate"), u32, &.{demo.params.demand_rate}, ); } } zgui.text("Max Producer Inventory", .{}); if (zgui.sliderScalar("##mi", u32, .{ .v = &demo.params.max_inventory, .min = 10, .max = 10000, })) { for (demo.buffers.data.producers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "max_inventory"), u32, &.{demo.params.max_inventory}, ); } } zgui.dummy(.{ .w = 1.0, .h = 40.0 }); const c_bufs = demo.buffers.data.consumers; zgui.text("Number of Consumers", .{}); if (zgui.sliderScalar("##nc", u32, .{ .v = &demo.params.num_consumers.new, .min = 1, .max = 10000, })) { const num_consumers = demo.params.num_consumers; Statistics.setNum(gctx, .{ .stat_obj = demo.buffers.data.stats, .num = num_consumers.new, .param = .consumers, }); if (num_consumers.old >= num_consumers.new) { Wgpu.shrinkBuffer(gctx, Consumer, .{ .new_size = num_consumers.new, .buf = c_bufs.buf, }); demo.buffers.data.consumers.list.resize(num_consumers.new) catch unreachable; demo.buffers.data.consumers.mapping.num_structs = num_consumers.new; } else { Consumer.generateBulk( gctx, &demo.buffers.data.consumers, demo.params, num_consumers.new - num_consumers.old, ); } demo.params.num_consumers.old = demo.params.num_consumers.new; } zgui.text("Moving Rate", .{}); if (zgui.sliderScalar("##mr", f32, .{ .v = &demo.params.moving_rate, .min = 1.0, .max = 20, })) { for (demo.buffers.data.consumers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.consumers.buf).?, i * @sizeOf(Consumer) + @offsetOf(Consumer, "moving_rate"), f32, &.{demo.params.moving_rate}, ); } } zgui.text("Consumer Size", .{}); if (zgui.sliderScalar("##cs", f32, .{ .v = &demo.params.consumer_radius, .min = 1, .max = 40, })) { demo.buffers.vertex.circle = Circle.createVertexBuffer( gctx, 40, demo.params.consumer_radius, ); } if (zgui.button("Start", .{})) { demo.running = true; } if (zgui.button("Stop", .{})) { demo.running = false; } if (zgui.button("Restart", .{})) { demo.running = true; Main.restartSimulation(demo); } if (zgui.button("Supply Shock", .{})) { for (demo.buffers.data.producers.list.items, 0..) |_, i| { gctx.queue.writeBuffer( gctx.lookupResource(demo.buffers.data.producers.buf).?, i * @sizeOf(Producer) + @offsetOf(Producer, "inventory"), i32, &.{0}, ); } } zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted("Set all producer inventory to 0."); zgui.endTooltip(); } }
0
repos/simulations/src
repos/simulations/src/random/wgpu.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zm = @import("zmath"); const zems = @import("zems"); const Gctx = zgpu.GraphicsContext; const wgpu = zgpu.wgpu; const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_STRUCTS = 10000; // A mishmash of Wgpu initialization functions and buffer helpers for an array of generic structs // Data Types pub const GraphicsObject = struct { render_pipeline: zgpu.RenderPipelineHandle, attribute_buffer: zgpu.BufferHandle, vertex_buffer: zgpu.BufferHandle, index_buffer: zgpu.BufferHandle, size_of_struct: u32, }; pub fn ObjectBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, list: std.ArrayList(T), mapping: MappingBuffer(T), }; } const callback_queue_len: usize = 10; fn MappingBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, insert_idx: usize = 0, remove_idx: usize = 0, requests: [callback_queue_len]struct { func: Callback(T), args: Callbacks.Args(T), } = undefined, staging: StagingBuffer(T), waiting: bool = false, num_structs: u32, }; } fn StagingBuffer(comptime T: type) type { return struct { slice: ?[]const T = null, buffer: wgpu.Buffer = undefined, num_structs: u32, }; } fn Callback(comptime T: type) type { return ?*const fn (args: Callbacks.Args(T)) void; } pub const RenderPipelineInfo = struct { pub const Attribute = struct { name: []const u8, type: type, }; vs: [:0]const u8, fs: [:0]const u8, inst_type: type, inst_attrs: []const Attribute, primitive_topology: wgpu.PrimitiveTopology = .triangle_list, }; pub const ComputePipelineInfo = struct { cs: [:0]const u8, entry_point: [:0]const u8, }; pub fn GenCallback(comptime T: type) wgpu.BufferMapCallback { return struct { fn callback(status: wgpu.BufferMapAsyncStatus, userdata: ?*anyopaque) callconv(.C) void { const usb = @as(*StagingBuffer(T), @ptrCast(@alignCast(userdata))); std.debug.assert(usb.slice == null); if (status == .success) { usb.slice = usb.buffer.getConstMappedRange(T, 0, usb.num_structs).?; } else { std.log.err("[zgpu] Failed to map buffer (code: {any})\n", .{status}); } } }.callback; } pub fn getAllAsync( comptime T: type, callback: Callback(T), args: Callbacks.Args(T), ) void { const map_ptr = &args.buf.mapping; map_ptr.staging.num_structs = map_ptr.num_structs; if (map_ptr.staging.num_structs <= 0) return; map_ptr.requests[map_ptr.insert_idx].func = callback; map_ptr.requests[map_ptr.insert_idx].args = args; map_ptr.insert_idx = (map_ptr.insert_idx + 1) % callback_queue_len; runMapIfReady(T, &args.buf.mapping); } pub fn runMapIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (!buf.waiting and buf.staging.slice == null and buf.insert_idx != buf.remove_idx) { buf.waiting = true; const gctx = buf.requests[buf.remove_idx].args.gctx; buf.staging.buffer = gctx.lookupResource(buf.buf).?; buf.staging.buffer.mapAsync( .{ .read = true }, 0, @sizeOf(T) * buf.staging.num_structs, GenCallback(T), @as(*anyopaque, @ptrCast(&buf.staging)), ); } } pub fn runCallbackIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (buf.waiting and buf.staging.slice != null) { const request = buf.requests[buf.remove_idx]; buf.remove_idx = (buf.remove_idx + 1) % callback_queue_len; request.func.?(request.args); buf.staging.buffer.unmap(); buf.staging.slice = null; buf.waiting = false; } } pub fn waitForCallback(comptime T: type, buf: *MappingBuffer(T)) void { while (buf.waiting) { runCallbackIfReady(T, buf); } } pub fn getMappedData(comptime T: type, buf: *MappingBuffer(T)) []T { return @constCast(buf.staging.slice.?[0..buf.staging.num_structs]); } pub fn agentParameters(comptime T: type) type { switch (T) { Consumer => return union(enum) { moving_rate: f32, demand_rate: u32, }, Producer => return union(enum) { production_rate: u32, inventory: i32, max_inventory: u32, }, u32 => return u32, else => unreachable, } } pub fn setArgs(comptime T: type) type { return struct { agents: ObjectBuffer, parameter: agentParameters(T), }; } // pub fn setAll(gctx: *zgpu.GraphicsContext, comptime T: type, args: setArgs(T)) void { // var agents = getAllAsync(T, Callbacks.clearConsumerHovers, .{ // .gctx = gctx, // .buf = args.agents, // }); // for (agents, 0..) |_, i| { // setAgentParameter(T, &agents[i], args.parameter); // } // writeBuffer(gctx, args.agents.data, T, agents); // } pub fn writeBuffer( gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle, comptime T: type, structs: []T, ) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, T, structs); } pub fn setAgentParameter( comptime T: type, agent: *T, parameter: agentParameters(T), ) void { switch (T) { Consumer => { switch (parameter) { .moving_rate => |v| agent.moving_rate = v, .demand_rate => |v| agent.demand_rate = v, } }, Producer => { switch (parameter) { .production_rate => |v| agent.production_rate = v, .inventory => |v| agent.inventory = v, .max_inventory => |v| agent.max_inventory = v, } }, else => unreachable, } } pub fn setGroupingArgs(comptime T: type) type { return struct { setArgs: setArgs(T), grouping_id: u32, }; } //pub fn setGroup(gctx: *zgpu.GraphicsContext, comptime T: type, args: setGroupingArgs(T)) void { // var agents = getAll(gctx, T, .{ // .structs = args.setArgs.agents, // .num_structs = args.setArgs.num_structs, // }) catch return; // for (agents, 0..) |agent, i| { // if (args.grouping_id == agent.grouping_id) { // setAgentParameter(T, &agents[i], args.setArgs.parameter); // } // } // writeBuffer(gctx, args.setArgs.agents.data, T, agents); //} pub fn updateCoords(gctx: *zgpu.GraphicsContext, comptime T: type, obj_buf: ObjectBuffer(T)) void { for (obj_buf.list.items, 0..) |obj, i| { const args: bufArgs(T, [4]f32) = .{ .obj_buf = obj_buf, .index = i, .value = Camera.getWorldPosition(gctx, obj.absolute_home), }; writeToObjectBuffer(gctx, T, [4]f32, "home", args); if (T == Consumer) { writeToObjectBuffer(gctx, T, [4]f32, "position", args); writeToObjectBuffer(gctx, T, [4]f32, "destination", args); } } } pub fn bufArgs(comptime T: type, comptime V: type) type { return struct { obj_buf: ObjectBuffer(T), index: usize, value: V, }; } pub fn writeToObjectBuffer( gctx: *Gctx, comptime T: type, comptime V: type, comptime field: []const u8, args: bufArgs(T, V), ) void { gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.mapping.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); } pub fn writeToMappedBuffer(gctx: *Gctx, buf: zgpu.BufferHandle, mapped: zgpu.BufferHandle) void { const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); pass: { const p = gctx.lookupResource(buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(buf) orelse break :pass; const pm = gctx.lookupResource(mapped) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); } pub const shrinkArgs = struct { new_size: u32, buf: zgpu.BufferHandle, }; pub fn shrinkBuffer(gctx: *Gctx, comptime T: type, args: shrinkArgs) void { const all_zero = [_]u8{0} ** 10000000; const buf = gctx.lookupResource(args.buf).?; const buf_info = gctx.lookupResourceInfo(args.buf).?; const size_to_keep = @sizeOf(T) * args.new_size; const size_to_clear = buf_info.size - size_to_keep; const usize_to_clear = @as(usize, @intCast(size_to_clear)); gctx.queue.writeBuffer( buf, size_to_keep, u8, all_zero[0..usize_to_clear], ); } pub fn appendArgs(comptime T: type) type { return struct { num_old_structs: u32, buf: zgpu.BufferHandle, structs: []T, }; } pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void { gctx.queue.writeBuffer( gctx.lookupResource(args.buf).?, args.num_old_structs * @sizeOf(T), T, args.structs, ); } pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 0, u8, all_zero[0..b_size], ); } pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.buf).?, 0, u8, all_zero[0..b_size], ); const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?; const m_size = @as(usize, @intCast(map_buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.mapping.buf).?, 0, u8, all_zero[0..m_size], ); obj_buf.list.clearAndFree(); obj_buf.mapping.insert_idx = 0; obj_buf.mapping.remove_idx = 0; obj_buf.mapping.waiting = false; obj_buf.mapping.staging.slice = null; obj_buf.mapping.num_structs = 0; obj_buf.mapping.staging.num_structs = 0; } // Blank Buffers pub fn createBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .copy_src = true, .vertex = true, .storage = true }, .size = num * @sizeOf(T), }); } pub fn createMappedBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .map_read = true }, .size = num * @sizeOf(T), }); } pub fn createObjectBuffer( allocator: std.mem.Allocator, gctx: *Gctx, comptime T: type, len: u32, num_structs: u32, ) ObjectBuffer(T) { return .{ .buf = createBuffer(gctx, T, len), .list = std.ArrayList(T).init(allocator), .mapping = .{ .buf = createMappedBuffer(gctx, T, len), .num_structs = num_structs, .staging = .{ .num_structs = num_structs, }, }, }; } // Depth Texture pub const Depth = struct { texture: zgpu.TextureHandle, view: zgpu.TextureViewHandle, }; pub fn createDepthTexture(gctx: *zgpu.GraphicsContext) Depth { const texture = gctx.createTexture(.{ .usage = .{ .render_attachment = true }, .dimension = .tdim_2d, .size = .{ .width = gctx.swapchain_descriptor.width, .height = gctx.swapchain_descriptor.height, .depth_or_array_layers = 1, }, .format = .depth32_float, .mip_level_count = 1, .sample_count = 1, }); const view = gctx.createTextureView(texture, .{}); return .{ .texture = texture, .view = view }; } // Bind Group Layouts pub fn createUniformBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .vertex = true }, .uniform, true, 0), }); } pub fn createComputeBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(1, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(2, .{ .compute = true }, .storage, false, 0), }); } // Bind Groups pub fn createUniformBindGroup(gctx: *Gctx) zgpu.BindGroupHandle { const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); return gctx.createBindGroup(bind_group_layout, &.{ .{ .binding = 0, .buffer_handle = gctx.uniforms.buffer, .offset = 0, .size = @sizeOf(zm.Mat) }, }); } pub const computeBindGroup = struct { consumer: zgpu.BufferHandle, producer: zgpu.BufferHandle, stats: zgpu.BufferHandle, }; pub fn createComputeBindGroup(gctx: *Gctx, args: computeBindGroup) zgpu.BindGroupHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const c_info = gctx.lookupResourceInfo(args.consumer) orelse unreachable; const p_info = gctx.lookupResourceInfo(args.producer) orelse unreachable; const s_info = gctx.lookupResourceInfo(args.stats) orelse unreachable; return gctx.createBindGroup(compute_bgl, &[_]zgpu.BindGroupEntryInfo{ .{ .binding = 0, .buffer_handle = args.consumer, .offset = 0, .size = c_info.size, }, .{ .binding = 1, .buffer_handle = args.producer, .offset = 0, .size = p_info.size, }, .{ .binding = 2, .buffer_handle = args.stats, .offset = 0, .size = s_info.size, }, }); } fn getWgpuType(comptime T: type) !wgpu.VertexFormat { return switch (T) { u32 => .uint32, f32 => .float32, [2]f32 => .float32x2, [3]f32 => .float32x3, [4]f32 => .float32x4, else => error.NoValidWgpuType, }; } pub fn createRenderPipeline( gctx: *zgpu.GraphicsContext, comptime args: RenderPipelineInfo, ) zgpu.RenderPipelineHandle { const vs_module = zgpu.createWgslShaderModule(gctx.device, args.vs, "vs"); defer vs_module.release(); const fs_module = zgpu.createWgslShaderModule(gctx.device, args.fs, "fs"); defer fs_module.release(); const color_targets = [_]wgpu.ColorTargetState{.{ .format = zgpu.GraphicsContext.swapchain_format, .blend = &.{ .color = .{}, .alpha = .{} }, }}; const vertex_attributes = [_]wgpu.VertexAttribute{ .{ .format = .float32x3, .offset = 0, .shader_location = 0 }, }; const instance_attributes = init: { var arr: [args.inst_attrs.len]wgpu.VertexAttribute = undefined; inline for (args.inst_attrs, 0..) |attr, i| { arr[i] = .{ .format = getWgpuType(attr.type) catch unreachable, .offset = @offsetOf(args.inst_type, attr.name), .shader_location = i + 1, }; } break :init arr; }; const vertex_buffers = [_]wgpu.VertexBufferLayout{ .{ .array_stride = @sizeOf(f32) * 3, .attribute_count = vertex_attributes.len, .attributes = &vertex_attributes, .step_mode = .vertex, }, .{ .array_stride = @sizeOf(args.inst_type), .attribute_count = instance_attributes.len, .attributes = &instance_attributes, .step_mode = .instance, }, }; const pipeline_descriptor = wgpu.RenderPipelineDescriptor{ .vertex = wgpu.VertexState{ .module = vs_module, .entry_point = "main", .buffer_count = vertex_buffers.len, .buffers = &vertex_buffers, }, .primitive = wgpu.PrimitiveState{ .front_face = .ccw, .cull_mode = .none, .topology = args.primitive_topology, }, .depth_stencil = &wgpu.DepthStencilState{ .format = .depth32_float, .depth_write_enabled = true, .depth_compare = .less_equal, }, .fragment = &wgpu.FragmentState{ .module = fs_module, .entry_point = "main", .target_count = color_targets.len, .targets = &color_targets, }, }; const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); const pipeline_layout = gctx.createPipelineLayout(&.{bind_group_layout}); return gctx.createRenderPipeline(pipeline_layout, pipeline_descriptor); } pub fn createComputePipeline(gctx: *zgpu.GraphicsContext, cpi: ComputePipelineInfo) zgpu.ComputePipelineHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const compute_pl = gctx.createPipelineLayout(&.{compute_bgl}); defer gctx.releaseResource(compute_pl); const cs_module = zgpu.createWgslShaderModule(gctx.device, cpi.cs, "cs"); defer cs_module.release(); const pipeline_descriptor = wgpu.ComputePipelineDescriptor{ .compute = wgpu.ProgrammableStageDescriptor{ .module = cs_module, .entry_point = cpi.entry_point, }, }; return gctx.createComputePipeline(compute_pl, pipeline_descriptor); }
0
repos/simulations/src
repos/simulations/src/random/square.zig
const zgpu = @import("zgpu"); pub fn createVertexBuffer(gctx: *zgpu.GraphicsContext, width: f32) zgpu.BufferHandle { const producer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = 6 * @sizeOf(f32) * 3, }); const upper_left = [3]f32{ -width, width, 0.0 }; const lower_left = [3]f32{ -width, -width, 0.0 }; const upper_right = [3]f32{ width, width, 0.0 }; const lower_right = [3]f32{ width, -width, 0.0 }; const vertex_array = [6][3]f32{ upper_left, lower_left, lower_right, lower_right, upper_right, upper_left, }; gctx.queue.writeBuffer(gctx.lookupResource(producer_vertex_buffer).?, 0, [3]f32, vertex_array[0..]); return producer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/random/windows.zig
const std = @import("std"); const zgui = @import("zgui"); const zgpu = @import("zgpu"); pub const window_flags = .{ .popen = null, .flags = zgui.WindowFlags.no_decoration, }; pub const ParametersWindow = PercentArgs{ .x = 0.0, .y = 0.0, .w = 0.25, .h = 0.75, .margin = 0.02, }; pub const StatsWindow = PercentArgs{ .x = 0.0, .y = 0.75, .w = 1.0, .h = 0.25, .margin = 0.02, .no_margin = .{ .top = true }, }; pub const PercentArgs = struct { x: f32, y: f32, w: f32, h: f32, margin: f32, no_margin: struct { top: bool = false, bottom: bool = false, left: bool = false, right: bool = false, } = .{}, // flags: zgui.WindowFlags = .{}, }; pub fn setNextWindowSize( gctx: *zgpu.GraphicsContext, p_width: f32, p_height: f32, ) void { std.debug.assert(0.0 <= p_width and p_width <= 1.0); std.debug.assert(0.0 <= p_height and p_height <= 1.0); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const height = @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)); zgui.setNextWindowSize(.{ .w = width * p_width, .h = height * p_height, }); } pub fn setNextWindow(gctx: *zgpu.GraphicsContext, args: PercentArgs) void { std.debug.assert(0.0 <= args.x and args.x <= 1.0); std.debug.assert(0.0 <= args.y and args.y <= 1.0); std.debug.assert(0.0 <= args.w and args.w <= 1.0); std.debug.assert(0.0 <= args.h and args.h <= 1.0); std.debug.assert(0.0 <= args.margin and args.margin <= 1.0); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const height = @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)); const margin_x = width * args.margin; const margin_y = height * args.margin; const margin_pixels = @min(margin_x, margin_y); var x = width * args.x + margin_pixels; var y = height * args.y + margin_pixels; var w = width * args.w - (margin_pixels * 2); var h = height * args.h - (margin_pixels * 2); if (args.no_margin.top) { y -= margin_pixels; h += margin_pixels; } if (args.no_margin.bottom) { h += margin_pixels; } if (args.no_margin.left) { x -= margin_pixels; w += margin_pixels; } if (args.no_margin.right) { w += margin_pixels; } zgui.setNextWindowPos(.{ .x = x, .y = y, }); zgui.setNextWindowSize(.{ .w = w, .h = h, }); }
0
repos/simulations/src
repos/simulations/src/random/callbacks.zig
const std = @import("std"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Statistics = @import("statistics.zig"); pub fn Args(comptime T: type) type { return struct { gctx: *zgpu.GraphicsContext, buf: *Wgpu.ObjectBuffer(T), stats: *Statistics = undefined, }; } pub fn numTransactions(args: Args(u32)) void { const gpu_stats = Wgpu.getMappedData(u32, &args.buf.mapping); args.stats.num_transactions.append(gpu_stats[0]) catch unreachable; Statistics.clearNumTransactions(args.gctx, args.buf.buf); } pub fn totalInventory(args: Args(Producer)) void { const producers = Wgpu.getMappedData(Producer, &args.buf.mapping); var total_inventory: u32 = 0; for (producers) |p| { total_inventory += @as(u32, @intCast(p.inventory)); } args.stats.num_total_producer_inventory.append(total_inventory) catch unreachable; } pub fn emptyConsumers(args: Args(Consumer)) void { const consumers = Wgpu.getMappedData(Consumer, &args.buf.mapping); var empty_consumers: u32 = 0; for (consumers) |c| { if (c.inventory == 0) { empty_consumers += 1; } } args.stats.num_empty_consumers.append(empty_consumers) catch unreachable; }
0
repos/simulations/src/random
repos/simulations/src/random/shaders/shaders.zig
// zig fmt: off pub const vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) demand_rate: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(demand_rate); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (vertex_position[0] * scale); \\ var y = position[1] + (vertex_position[1] * scale); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const producer_vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) max_inventory: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(max_inventory); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (scale * vertex_position[0]); \\ var y = position[1] + (scale * vertex_position[1]); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const fs = \\ @stage(fragment) fn main( \\ @location(0) color: vec3<f32>, \\ ) -> @location(0) vec4<f32> { \\ return vec4(color, 1.0); \\ } ; pub const cs = \\ struct Consumer { \\ position: vec4<f32>, \\ home: vec4<f32>, \\ absolute_home: vec4<f32>, \\ destination: vec4<f32>, \\ step_size: vec4<f32>, \\ color: vec4<f32>, \\ moving_rate: f32, \\ demand_rate: u32, \\ inventory: u32, \\ radius: f32, \\ producer_id: i32, \\ } \\ struct Producer { \\ position: vec4<f32>, \\ absolute_pos: vec4<f32>, \\ color: vec4<f32>, \\ production_rate: u32, \\ inventory: atomic<u32>, \\ max_inventory: u32, \\ len: atomic<u32>, \\ queue: array<u32, 480>, \\ } \\ struct Stats { \\ transactions: u32, \\ } \\ \\ @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; \\ @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; \\ @group(0) @binding(2) var<storage, read_write> stats: Stats; \\ @compute @workgroup_size(64) \\ fn consumer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let nc = arrayLength(&consumers); \\ if(GlobalInvocationID.x >= nc) { \\ return; \\ } \\ let c = consumers[index]; \\ consumers[index].position += c.step_size; \\ let dist = abs(c.position - c.destination); \\ let at_destination = all(dist.xy <= vec2<f32>(0.1)); \\ \\ if (at_destination) { \\ var new_destination = vec4<f32>(0); \\ let at_home = all(c.destination == c.home); \\ if (at_home) { \\ consumers[index].position = c.home; \\ let consumption_rate = 1u; \\ if (c.inventory >= consumption_rate) { \\ consumers[index].inventory -= consumption_rate; \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ return; \\ } \\ consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); \\ var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); \\ var shortest_distance = 100000.0; \\ var array_len = i32(arrayLength(&producers)); \\ for(var i = 0; i < array_len; i++){ \\ let dist = distance(c.home, producers[i].position); \\ let inventory = atomicLoad(&producers[i].inventory); \\ if (dist < shortest_distance && inventory > c.demand_rate) { \\ shortest_distance = dist; \\ consumers[index].destination = producers[i].position; \\ consumers[index].step_size = step_sizes(c.position, producers[i].position, c.moving_rate); \\ consumers[index].producer_id = i; \\ } \\ } \\ if (shortest_distance == 100000.0) { \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ } \\ } else { \\ let position = c.destination; \\ let pid = c.producer_id; \\ consumers[index].position = position; \\ consumers[index].step_size = vec4<f32>(0); \\ let idx = atomicAdd(&producers[pid].len, 1); \\ producers[pid].queue[idx] = index + 1; \\ } \\ } \\ } \\ fn step_sizes(pos: vec4<f32>, dest: vec4<f32>, mr: f32) -> vec4<f32>{ \\ let x_num_steps = num_steps(pos.x, dest.x, mr); \\ let y_num_steps = num_steps(pos.y, dest.y, mr); \\ let num_steps = max(x_num_steps, y_num_steps); \\ let distance = dest - pos; \\ return distance / num_steps; \\ } \\ fn num_steps(x: f32, y: f32, rate: f32) -> f32 { \\ let distance = abs(x - y); \\ if (rate > distance) { return 1.0; } \\ return ceil(distance / rate); \\ } \\ @compute @workgroup_size(64) \\ fn producer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let np = arrayLength(&producers); \\ if(GlobalInvocationID.x >= np) { \\ return; \\ } \\ let max_inventory = producers[index].max_inventory; \\ let inventory = atomicLoad(&producers[index].inventory); \\ var production_rate = producers[index].production_rate; \\ if (max_inventory > inventory) { \\ let diff = max_inventory - inventory; \\ production_rate = min(diff, production_rate); \\ let old_val = atomicAdd(&producers[index].inventory, production_rate); \\ } else if (inventory < max_inventory) { \\ atomicStore(&producers[index].inventory, max_inventory); \\ } \\ \\ let idx = atomicLoad(&producers[index].len); \\ for (var i = 0u; i < idx; i++) { \\ let cid = producers[index].queue[i] - 1; \\ let c = consumers[cid]; \\ let inventory = atomicLoad(&producers[index].inventory); \\ if (inventory >= c.demand_rate) { \\ consumers[cid].destination = c.home; \\ consumers[cid].step_size = step_sizes(c.position, c.home, c.moving_rate); \\ consumers[cid].inventory += c.demand_rate; \\ let old_inv = atomicSub(&producers[index].inventory, c.demand_rate); \\ stats.transactions += 1; \\ consumers[cid].color = vec4(0.0, 1.0, 0.0, 0.0); \\ } \\ } \\ atomicStore(&producers[index].len, 0); \\} ; // zig fmt: on
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/fragment/fragment.wgsl
@fragment fn main( @location(0) color: vec3<f32>, ) -> @location(0) vec4<f32> { return vec4(color, 1.0); }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/vertex/hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/vertex/producer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) max_inventory: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(max_inventory); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (scale * vertex_position[0]); var y = position[1] + (scale * vertex_position[1]); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/vertex/consumer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) demand_rate: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(demand_rate); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (vertex_position[0] * scale); var y = position[1] + (vertex_position[1] * scale); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/vertex/consumer_hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) hover_color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = hover_color.xyz; return output; }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/compute/common.wgsl
struct Consumer { absolute_home: vec4<i32>, position: vec4<f32>, home: vec4<f32>, destination: vec4<f32>, color: vec4<f32>, step_size: vec2<f32>, moving_rate: f32, demand_rate: u32, inventory: u32, radius: f32, producer_id: i32, grouping_id: u32, } struct Producer { absolute_home: vec4<i32>, home: vec4<f32>, color: vec4<f32>, production_rate: u32, inventory: atomic<i32>, max_inventory: u32, } struct Stats { transactions: u32, num_consumers: u32, num_producers: u32, num_consumer_hovers: u32, random_color: vec4<f32>, } @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; @group(0) @binding(2) var<storage, read_write> stats: Stats; fn step_sizes(pos: vec2<f32>, dest: vec2<f32>, mr: f32) -> vec2<f32>{ let x_num_steps = num_steps(pos.x, dest.x, mr); let y_num_steps = num_steps(pos.y, dest.y, mr); let num_steps = max(x_num_steps, y_num_steps); let distance = dest - pos; return distance / num_steps; } fn num_steps(x: f32, y: f32, rate: f32) -> f32 { let distance = abs(x - y); if (rate > distance) { return 1.0; } return ceil(distance / rate); }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/compute/producer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_producers) { return; } let max_inventory = i32(producers[index].max_inventory); var production_rate = i32(producers[index].production_rate); let old_inventory = atomicAdd(&producers[index].inventory, production_rate); if (old_inventory + production_rate > max_inventory) { atomicStore(&producers[index].inventory, max_inventory); } }
0
repos/simulations/src/random/shaders
repos/simulations/src/random/shaders/compute/consumer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_consumers) { return; } // User removed producer this consumer was targeting if (consumers[index].producer_id >= i32(stats.num_producers)) { search_for_producer(index); } let c = consumers[index]; consumers[index].position[0] += c.step_size[0]; consumers[index].position[1] += c.step_size[1]; let dist = abs(c.position - c.destination); let at_destination = all(dist.xy <= vec2<f32>(0.1)); if (at_destination) { consumers[index].step_size = vec2<f32>(0); consumers[index].position = c.destination; let at_home = all(c.destination == c.home); if (at_home) { if (c.inventory >= u32(1)) { consumers[index].inventory -= u32(1); return; } consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); search_for_producer(index); return; } // At Producer let pid = c.producer_id; let i_demand_rate = i32(c.demand_rate); let old_val = atomicSub(&producers[pid].inventory, i_demand_rate); // Went negative, revert inventory if (i_demand_rate > old_val) { atomicAdd(&producers[pid].inventory, i_demand_rate); return; } consumers[index].color = vec4(0.0, 1.0, 0.0, 0.0); consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); consumers[index].inventory += c.demand_rate; consumers[index].producer_id = -1; stats.transactions += u32(1); } } fn search_for_producer(index: u32){ let c = consumers[index]; var pid = find_nearest_stocked_producer(c); if (pid == -1) { consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); return; } let p_pos = producers[pid].home; consumers[index].destination = p_pos; consumers[index].step_size = step_sizes(c.position.xy, p_pos.xy, c.moving_rate); consumers[index].producer_id = pid; } // Returns the pid of nearest stocked producer, -1 for failure fn find_nearest_stocked_producer(c: Consumer) -> i32 { var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); var shortest_distance = 100000.0; var pid: i32 = -1; for(var i: u32 = 0; i < stats.num_producers; i++){ let dist = distance(c.home, producers[i].home); let inventory = u32(atomicLoad(&producers[i].inventory)); if (dist < shortest_distance && inventory > c.demand_rate) { shortest_distance = dist; pid = i32(i); } } return pid; }
0
repos/simulations/src
repos/simulations/src/income/camera.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zmath = @import("zmath"); // Camera Settings pub const POS: [3]f32 = .{ 0.0, 0.0, -3000.0 }; pub const FOCUS: [3]f32 = .{ 0.0, 0.0, 0.0 }; pub const UP: [4]f32 = .{ 0.0, 1.0, 0.0, 0.0 }; pub const FOV_Y: f32 = 0.22 * std.math.pi; pub const NEAR_PLANE: f32 = 0.01; pub const FAR_PLANE: f32 = 3100.0; // Grid limits for absolute positions (without aspect ratio) pub const MAX_X: i32 = 1000; pub const MIN_X: i32 = -1000; pub const MAX_Y: i32 = 1000; pub const MIN_Y: i32 = -1000; pub const TOTAL_X: i32 = 2000; pub const TOTAL_Y: i32 = 2000; // Viewport size relative to total window size pub const VP_X_SIZE: f32 = 0.75; pub const VP_Y_SIZE: f32 = 0.75; pub fn getViewportPixelSize(gctx: *zgpu.GraphicsContext) [2]f32 { return .{ @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)) * VP_X_SIZE, @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)) * VP_Y_SIZE, }; } pub fn getAspectRatio(gctx: *zgpu.GraphicsContext) f32 { const sd = gctx.swapchain_descriptor; return @as(f32, @floatFromInt(sd.width)) / @as(f32, @floatFromInt(sd.height)); } // Given a world position (grid position with aspect), return grid position pub fn getGridFromWorld(gctx: *zgpu.GraphicsContext, world_pos: [2]f32) [2]i32 { const aspect = getAspectRatio(gctx); return .{ @as(i32, @intFromFloat(world_pos[0] / aspect)), @as(i32, @intFromFloat(world_pos[1])), // world_pos[2], // world_pos[3], }; } // Given a grid position, return a world position pub fn getWorldPosition(gctx: *zgpu.GraphicsContext, grid_pos: [4]i32) [4]f32 { const aspect = getAspectRatio(gctx); return .{ @as(f32, @floatFromInt(grid_pos[0])) * aspect, @as(f32, @floatFromInt(grid_pos[1])), @as(f32, @floatFromInt(grid_pos[2])), @as(f32, @floatFromInt(grid_pos[3])), }; } // Given a grid position, return a pixel position pub fn getPixelPosition(gctx: *zgpu.GraphicsContext, g_pos: [2]i32) [2]f32 { const grid_pos = .{ g_pos[0], g_pos[1], 1, 1 }; const world_pos = zmath.loadArr4(getWorldPosition(gctx, grid_pos)); const camera_pos = zmath.mul(world_pos, getObjectToClipMat(gctx)); const rel_pos = [4]f32{ camera_pos[0] / -POS[2], camera_pos[1] / -POS[2], 0, 1 }; const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const cursor_in_vp_x = ((rel_pos[0] + 1) * viewport_size[0]) / 2; const cursor_in_vp_y = ((-rel_pos[1] + 1) * viewport_size[1]) / 2; return .{ cursor_in_vp_x + xOffset, cursor_in_vp_y }; } // Given a pixel position, return a grid position pub fn getGridPosition(gctx: *zgpu.GraphicsContext, p_pos: [2]f32) [2]i32 { const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const rel_pos_x = (((p_pos[0] - xOffset) * 2) / viewport_size[0]) - 1; const rel_pos_y = ((p_pos[1] * 2) / viewport_size[1]) - 1; const camera_pos = zmath.loadArr4(.{ rel_pos_x * -POS[2], rel_pos_y * POS[2], 1, 1, }); const inverse_mat = zmath.inverse(getObjectToClipMat(gctx)); const world_pos = zmath.mul(camera_pos, inverse_mat); return getGridFromWorld(gctx, .{ world_pos[0], world_pos[1] }); } pub fn getObjectToClipMat(gctx: *zgpu.GraphicsContext) zmath.Mat { const camWorldToView = zmath.lookAtLh( zmath.loadArr3(POS), zmath.loadArr3(FOCUS), zmath.loadArr4(UP), ); const camViewToClip = zmath.perspectiveFovLh( FOV_Y, getAspectRatio(gctx), NEAR_PLANE, FAR_PLANE, ); const camWorldToClip = zmath.mul(camWorldToView, camViewToClip); // return zmath.transpose(camWorldToClip); return camWorldToClip; }
0
repos/simulations/src
repos/simulations/src/income/distribution.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), avg_consumer_balance: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), .avg_consumer_balance = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); self.avg_consumer_balance.deinit(); } pub fn generateAndFillRandomColor(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 4 * @sizeOf(u32), f32, &.{ random.float(f32), random.float(f32), random.float(f32) }, ); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); self.avg_consumer_balance.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const setArgs = struct { stat_obj: Wgpu.ObjectBuffer(u32), num: u32, param: enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }, }; pub fn setNum(gctx: *zgpu.GraphicsContext, args: setArgs) void { gctx.queue.writeBuffer( gctx.lookupResource(args.stat_obj.buf).?, @intFromEnum(args.param) * @sizeOf(u32), u32, &.{args.num}, ); }
0
repos/simulations/src
repos/simulations/src/income/main.zig
const std = @import("std"); const math = std.math; const random = std.crypto.random; const zglfw = @import("zglfw"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const zgui = @import("zgui"); const zm = @import("zmath"); const zstbi = @import("zstbi"); const Statistics = @import("statistics.zig"); const gui = @import("gui.zig"); const Wgpu = @import("wgpu.zig"); const config = @import("config.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Square = @import("square.zig"); const Circle = @import("circle.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_PRODUCERS = 100; pub const MAX_NUM_CONSUMERS = 40000; pub const NUM_CONSUMER_SIDES = 40; pub const PRODUCER_WIDTH = 40; pub const DemoState = struct { gctx: *zgpu.GraphicsContext, window: *zglfw.Window, allocator: std.mem.Allocator, running: bool = false, push_coord_update: bool = false, push_restart: bool = false, content_scale: f32, params: Parameters, stats: Statistics, imgui_windows: [2]gui.FullPos, render_pipelines: struct { circle: zgpu.RenderPipelineHandle, square: zgpu.RenderPipelineHandle, }, compute_pipelines: struct { consumer: zgpu.ComputePipelineHandle, producer: zgpu.ComputePipelineHandle, }, bind_groups: struct { render: zgpu.BindGroupHandle, compute: zgpu.BindGroupHandle, }, buffers: struct { data: struct { consumers: Wgpu.ObjectBuffer(Consumer), producers: Wgpu.ObjectBuffer(Producer), stats: Wgpu.ObjectBuffer(u32), }, index: struct { circle: zgpu.BufferHandle, }, vertex: struct { circle: zgpu.BufferHandle, square: zgpu.BufferHandle, }, }, depth_texture: zgpu.TextureHandle, depth_texture_view: zgpu.TextureViewHandle, }; pub const Parameters = struct { aspect: f32, max_num_stats: u32 = 3, num_producers: u32 = 6, num_consumers: u32 = 5000, production_rate: u32 = 300, max_demand_rate: u32 = 100, max_inventory: u32 = 10000, moving_rate: f32 = 5.0, consumer_radius: f32 = 1.0, num_consumer_sides: u32 = 20, median_income: f32 = 0.01, plot_hovered: bool = false, price: u32 = 1, consumer_incomes: [4]doubleXY(f64) = [_]doubleXY(f64){ doubleXY(f64).init(f64, 100, 0), doubleXY(f64).init(f64, 200, 0), doubleXY(f64).init(f64, 300, 0), doubleXY(f64).init(f64, 400, 4000), }, pub fn doubleXY(comptime T: type) type { return struct { old: struct { income: T, num: T }, new: struct { income: T, num: T }, num: u32 = 0, pub fn init(comptime Ti: type, income: Ti, num: Ti) doubleXY(Ti) { return doubleXY(Ti){ .old = .{ .income = income, .num = num }, .new = .{ .income = income, .num = num }, }; } }; } pub fn clearConsumerIncomesNum(self: *Parameters) void { for (&self.consumer_incomes) |*ci| { ci.num = 0; } } }; pub fn init(allocator: std.mem.Allocator, window: *zglfw.Window) !DemoState { const gctx = try zgpu.GraphicsContext.create( allocator, .{ .window = window, .fn_getTime = @ptrCast(&zglfw.getTime), .fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize), .fn_getWin32Window = @ptrCast(&zglfw.getWin32Window), .fn_getX11Display = @ptrCast(&zglfw.getX11Display), .fn_getX11Window = @ptrCast(&zglfw.getX11Window), .fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay), .fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow), .fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow), }, .{}, ); const params = Parameters{ .aspect = Camera.getAspectRatio(gctx), }; const consumer_object = Wgpu.createObjectBuffer( allocator, gctx, Consumer, MAX_NUM_CONSUMERS, 0, ); const producer_object = Wgpu.createObjectBuffer( allocator, gctx, Producer, MAX_NUM_PRODUCERS, 0, ); const stats_object = Wgpu.createObjectBuffer( allocator, gctx, u32, Statistics.NUM_STATS, Statistics.NUM_STATS, ); const compute_bind_group = Wgpu.createComputeBindGroup(gctx, .{ .consumer = consumer_object.buf, .producer = producer_object.buf, .stats = stats_object.buf, }); const depth = Wgpu.createDepthTexture(gctx); return DemoState{ .gctx = gctx, .window = window, .content_scale = getContentScale(window), .imgui_windows = .{ .{ .pos = .{ .x = 0, .y = 0 }, .size = .{ .x = 0.25, .y = 0.75 }, .id = .{ .num = 0, .str = "0", }, }, .{ .pos = .{ .x = 0, .y = 0.75, .margin = .{ .top = false } }, .size = .{ .x = 1, .y = 0.25, .margin = .{ .top = false } }, .id = .{ .num = 1, .str = "1", }, }, }, .render_pipelines = .{ .circle = Wgpu.createRenderPipeline(gctx, config.cpi), .square = Wgpu.createRenderPipeline(gctx, config.ppi), }, .compute_pipelines = .{ .producer = Wgpu.createComputePipeline(gctx, config.pcpi), .consumer = Wgpu.createComputePipeline(gctx, config.ccpi), }, .bind_groups = .{ .render = Wgpu.createUniformBindGroup(gctx), .compute = compute_bind_group, }, .buffers = .{ .data = .{ .consumers = consumer_object, .producers = producer_object, .stats = stats_object, }, .index = .{ .circle = Circle.createIndexBuffer(gctx, NUM_CONSUMER_SIDES), }, .vertex = .{ .circle = Circle.createVertexBuffer( gctx, NUM_CONSUMER_SIDES, params.consumer_radius, ), .square = Square.createVertexBuffer(gctx, PRODUCER_WIDTH), }, }, .depth_texture = depth.texture, .depth_texture_view = depth.view, .allocator = allocator, .params = params, .stats = Statistics.init(allocator), }; } pub fn update(demo: *DemoState) void { const sd = demo.gctx.swapchain_descriptor; zgui.backend.newFrame(sd.width, sd.height); if (demo.push_restart) restartSimulation(demo); if (demo.push_coord_update) updateAspectRatio(demo); Wgpu.runCallbackIfReady(u32, &demo.buffers.data.stats.mapping); Wgpu.runCallbackIfReady(Producer, &demo.buffers.data.producers.mapping); Wgpu.runCallbackIfReady(Consumer, &demo.buffers.data.consumers.mapping); // zgui.plot.showDemoWindow(null); gui.update(demo); if (demo.running) { Statistics.generateAndFillRandomColor(demo); const current_time = @as(f32, @floatCast(demo.gctx.stats.time)); const seconds_passed = current_time - demo.stats.second; if (seconds_passed >= 1) { demo.stats.second = current_time; Wgpu.getAllAsync(demo, u32, Callbacks.numTransactions); Wgpu.getAllAsync(demo, Consumer, Callbacks.consumerStats); Wgpu.getAllAsync(demo, Producer, Callbacks.totalInventory); } } } pub fn draw(demo: *DemoState) void { const gctx = demo.gctx; const cam_world_to_clip = Camera.getObjectToClipMat(gctx); const back_buffer_view = gctx.swapchain.getCurrentTextureView(); defer back_buffer_view.release(); const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); const data = demo.buffers.data; const num_consumers = @as(u32, @intCast(data.consumers.list.items.len)); const num_producers = @as(u32, @intCast(data.producers.list.items.len)); // Compute shaders if (demo.running) { pass: { const pcp = gctx.lookupResource(demo.compute_pipelines.producer) orelse break :pass; const ccp = gctx.lookupResource(demo.compute_pipelines.consumer) orelse break :pass; const bg = gctx.lookupResource(demo.bind_groups.compute) orelse break :pass; const pass = encoder.beginComputePass(null); defer { pass.end(); pass.release(); } pass.setBindGroup(0, bg, &.{}); pass.setPipeline(pcp); pass.dispatchWorkgroups(@divFloor(num_producers, 64) + 1, 1, 1); pass.setPipeline(ccp); pass.dispatchWorkgroups(@divFloor(num_consumers, 64) + 1, 1, 1); } } // Copy data to mapped buffers so we can retrieve it on demand pass: { if (!demo.buffers.data.stats.mapping.waiting) { const s = gctx.lookupResource(data.stats.buf) orelse break :pass; const s_info = gctx.lookupResourceInfo(data.stats.buf) orelse break :pass; const sm = gctx.lookupResource(data.stats.mapping.buf) orelse break :pass; const s_size = @as(usize, @intCast(s_info.size)); encoder.copyBufferToBuffer(s, 0, sm, 0, s_size); } if (!demo.buffers.data.producers.mapping.waiting) { const p = gctx.lookupResource(data.producers.buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(data.producers.buf) orelse break :pass; const pm = gctx.lookupResource(data.producers.mapping.buf) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } if (!demo.buffers.data.consumers.mapping.waiting) { const c = gctx.lookupResource(data.consumers.buf) orelse break :pass; const c_info = gctx.lookupResourceInfo(data.consumers.buf) orelse break :pass; const cm = gctx.lookupResource(data.consumers.mapping.buf) orelse break :pass; const c_size = @as(usize, @intCast(c_info.size)); encoder.copyBufferToBuffer(c, 0, cm, 0, c_size); } } // Draw the circles and squares in our defined viewport pass: { const svb_info = gctx.lookupResourceInfo(demo.buffers.vertex.square) orelse break :pass; const pb_info = gctx.lookupResourceInfo(demo.buffers.data.producers.buf) orelse break :pass; const cvb_info = gctx.lookupResourceInfo(demo.buffers.vertex.circle) orelse break :pass; const cb_info = gctx.lookupResourceInfo(demo.buffers.data.consumers.buf) orelse break :pass; const cib_info = gctx.lookupResourceInfo(demo.buffers.index.circle) orelse break :pass; const square_rp = gctx.lookupResource(demo.render_pipelines.square) orelse break :pass; const circle_rp = gctx.lookupResource(demo.render_pipelines.circle) orelse break :pass; const render_bind_group = gctx.lookupResource(demo.bind_groups.render) orelse break :pass; const depth_view = gctx.lookupResource(demo.depth_texture_view) orelse break :pass; const color_attachments = [_]wgpu.RenderPassColorAttachment{.{ .view = back_buffer_view, .load_op = .clear, .store_op = .store, }}; const depth_attachment = wgpu.RenderPassDepthStencilAttachment{ .view = depth_view, .depth_load_op = .clear, .depth_store_op = .store, .depth_clear_value = 1.0, }; const render_pass_info = wgpu.RenderPassDescriptor{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, .depth_stencil_attachment = &depth_attachment, }; const pass = encoder.beginRenderPass(render_pass_info); defer { pass.end(); pass.release(); } const sd = gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const xOffset = width / 4; const height = @as(f32, @floatFromInt(sd.height)); const yOffset = height / 4; pass.setViewport(xOffset, 0, width - xOffset, height - yOffset, 0, 1); var mem = gctx.uniformsAllocate(zm.Mat, 1); mem.slice[0] = cam_world_to_clip; pass.setBindGroup(0, render_bind_group, &.{mem.offset}); const num_indices_circle = @as( u32, @intCast(cib_info.size / @sizeOf(f32)), ); pass.setPipeline(circle_rp); pass.setVertexBuffer(0, cvb_info.gpuobj.?, 0, cvb_info.size); pass.setVertexBuffer(1, cb_info.gpuobj.?, 0, cb_info.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, num_consumers, 0, 0, 0); pass.setPipeline(square_rp); pass.setVertexBuffer(0, svb_info.gpuobj.?, 0, svb_info.size); pass.setVertexBuffer(1, pb_info.gpuobj.?, 0, pb_info.size); pass.draw(6, num_producers, 0, 0); } // Draw ImGui { const pass = zgpu.beginRenderPassSimple( encoder, .load, back_buffer_view, null, null, null, ); defer zgpu.endReleasePass(pass); zgui.backend.draw(pass); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); if (demo.gctx.present() == .swap_chain_resized) { demo.content_scale = getContentScale(demo.window); setImguiContentScale(demo.content_scale); updateAspectRatio(demo); } } pub fn restartSimulation(demo: *DemoState) void { const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; const stats_waiting = demo.buffers.data.stats.mapping.waiting; if (consumer_waiting or producer_waiting or stats_waiting) { demo.push_restart = true; return; } Wgpu.clearObjBuffer(demo.gctx, Consumer, &demo.buffers.data.consumers); Wgpu.clearObjBuffer(demo.gctx, Producer, &demo.buffers.data.producers); Wgpu.clearObjBuffer(demo.gctx, u32, &demo.buffers.data.stats); demo.buffers.data.stats.mapping.num_structs = Statistics.NUM_STATS; demo.params.clearConsumerIncomesNum(); Statistics.setNum(demo, demo.params.num_consumers, .consumers); Statistics.setNum(demo, demo.params.num_producers, .producers); Consumer.generateFromParams(demo); Producer.generateBulk(demo, demo.params.num_producers); demo.stats.clear(); demo.push_restart = false; } pub fn updateDepthTexture(demo: *DemoState) void { // Release old depth texture. demo.gctx.releaseResource(demo.depth_texture_view); demo.gctx.destroyResource(demo.depth_texture); // Create a new depth texture to match the new window size. const depth = Wgpu.createDepthTexture(demo.gctx); demo.depth_texture = depth.texture; demo.depth_texture_view = depth.view; } pub fn updateAspectRatio(demo: *DemoState) void { updateDepthTexture(demo); const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; if (consumer_waiting or producer_waiting) { demo.push_coord_update = true; return; } Wgpu.updateCoords(demo.gctx, Consumer, demo.buffers.data.consumers); Wgpu.updateCoords(demo.gctx, Producer, demo.buffers.data.producers); demo.push_coord_update = false; demo.params.aspect = Camera.getAspectRatio(demo.gctx); } fn getContentScale(window: *zglfw.Window) f32 { const content_scale = window.getContentScale(); return @max(content_scale[0], content_scale[1]); } fn setImguiContentScale(scale: f32) void { zgui.getStyle().* = zgui.Style.init(); zgui.getStyle().scaleAllSizes(scale); } pub fn deinit(demo: *DemoState) void { demo.gctx.destroy(demo.allocator); demo.stats.deinit(); demo.buffers.data.consumers.list.deinit(); demo.buffers.data.producers.list.deinit(); demo.buffers.data.stats.list.deinit(); demo.* = undefined; } pub fn main() !void { try zglfw.init(); defer zglfw.terminate(); // Change current working directory to where the executable is located. var buffer: [1024]u8 = undefined; const path = std.fs.selfExeDirPath(buffer[0..]) catch "."; std.posix.chdir(path) catch {}; zglfw.windowHintTyped(.client_api, .no_api); const window = try zglfw.Window.create(1600, 1000, "Simulations", null); defer window.destroy(); window.setSizeLimits(400, 400, -1, -1); window.setPos(0, 0); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); zstbi.init(allocator); defer zstbi.deinit(); var demo = try init(allocator, window); defer deinit(&demo); Statistics.setNum(&demo, demo.params.num_consumers, .consumers); Statistics.setNum(&demo, demo.params.num_producers, .producers); Consumer.generateFromParams(&demo); Producer.generateBulk(&demo, demo.params.num_producers); zgui.init(allocator); defer zgui.deinit(); zgui.plot.init(); defer zgui.plot.deinit(); zgui.io.setIniFilename(null); _ = zgui.io.addFontFromFile( "content/fonts/Roboto-Medium.ttf", 26.0 * demo.content_scale, ); setImguiContentScale(demo.content_scale); zgui.backend.init( window, demo.gctx.device, @intFromEnum(zgpu.GraphicsContext.swapchain_format), @intFromEnum(wgpu.TextureFormat.undef), ); defer zgui.backend.deinit(); zgui.plot.getStyle().plot_padding = .{ 20, 20 }; while (!window.shouldClose() and window.getKey(.escape) != .press) { zglfw.pollEvents(); update(&demo); draw(&demo); } }
0
repos/simulations/src
repos/simulations/src/income/statistics.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const DemoState = @import("main.zig").DemoState; const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), avg_consumer_balance: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), .avg_consumer_balance = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); self.avg_consumer_balance.deinit(); } pub fn generateAndFillRandomColor(demo: *DemoState) void { const handle = demo.buffers.data.stats.buf; const resource = demo.gctx.lookupResource(handle).?; const color = [3]f32{ random.float(f32), random.float(f32), random.float(f32) }; demo.gctx.queue.writeBuffer(resource, 3 * @sizeOf(u32), [3]f32, &.{color}); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); self.avg_consumer_balance.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const Param = enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }; pub fn setNum(demo: *DemoState, num: u32, param: Param) void { const resource = demo.gctx.lookupResource(demo.buffers.data.stats.buf).?; const offset = @intFromEnum(param) * @sizeOf(u32); demo.gctx.queue.writeBuffer(resource, offset, u32, &.{num}); }
0
repos/simulations/src
repos/simulations/src/income/config.zig
const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); pub const cpi = .{ .vs = @embedFile("shaders/vertex/consumer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Consumer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "position", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, }, }; pub const ppi = .{ .vs = @embedFile("shaders/vertex/producer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Producer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "home", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "max_inventory", .type = u32, }, }, }; const common = @embedFile("shaders/compute/common.wgsl"); pub const ccpi = .{ .cs = common ++ @embedFile("shaders/compute/consumer.wgsl"), .entry_point = "main", }; pub const pcpi = .{ .cs = common ++ @embedFile("shaders/compute/producer.wgsl"), .entry_point = "main", };
0
repos/simulations/src
repos/simulations/src/income/producer.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const Allocator = std.mem.Allocator; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const Main = @import("main.zig"); const DemoState = Main.DemoState; const Parameters = Main.Parameters; const Camera = @import("camera.zig"); const Self = @This(); absolute_home: [4]i32 = .{ 0, 0, 0, 0 }, home: [4]f32 = .{ 0, 0, 0, 0 }, color: [4]f32 = .{ 0, 0, 0, 0 }, production_rate: u32 = 0, inventory: i32 = 0, max_inventory: u32 = 0, price: u32 = 1, pub const z_pos = 0; pub const Parameter = enum { production_rate, supply_shock, max_inventory, }; pub const DEFAULT_PRODUCTION_RATE: u32 = 300; pub const DEFAULT_MAX_INVENTORY: u32 = 10000; pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = .{ 1, 1, 1, 0 }, production_rate: u32 = DEFAULT_PRODUCTION_RATE, inventory: i32 = 0, max_inventory: u32 = DEFAULT_MAX_INVENTORY, price: u32 = 1, }; pub fn generateBulk(demo: *DemoState, num: u32) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); createAndAppend(demo.gctx, .{ .obj_buf = &demo.buffers.data.producers, .producer = .{ .absolute_home = .{ x, y }, .home = [2]f32{ @as(f32, @floatFromInt(x)) * demo.params.aspect, @as(f32, @floatFromInt(y)), }, .production_rate = demo.params.production_rate, .inventory = @as(i32, @intCast(demo.params.max_inventory)), .max_inventory = demo.params.max_inventory, .price = demo.params.price, }, }); i += 1; } } pub const AppendArgs = struct { producer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const abs_home = args.producer.absolute_home; const home = args.producer.home; var producers: [1]Self = .{ .{ .absolute_home = .{ abs_home[0], abs_home[1], z_pos, 1 }, .home = .{ home[0], home[1], z_pos, 1 }, .color = args.producer.color, .production_rate = args.producer.production_rate, .inventory = args.producer.inventory, .max_inventory = args.producer.max_inventory, .price = args.producer.price, }, }; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = @as(u32, @intCast(args.obj_buf.list.items.len)), .buf = args.obj_buf.buf, .structs = producers[0..], }); args.obj_buf.list.append(producers[0]) catch unreachable; args.obj_buf.mapping.num_structs += 1; } pub fn setParamAll( demo: *DemoState, comptime tag: []const u8, comptime T: type, num: T, ) void { const buf = demo.buffers.data.producers.buf; const resource = demo.gctx.lookupResource(buf).?; const field_enum = @field(std.meta.FieldEnum(Self), tag); const field_type = std.meta.FieldType(Self, field_enum); std.debug.assert(field_type == T); const struct_offset = @offsetOf(Self, tag); for (demo.buffers.data.producers.list.items, 0..) |_, i| { const offset = i * @sizeOf(Self) + struct_offset; demo.gctx.queue.writeBuffer(resource, offset, field_type, &.{num}); } }
0
repos/simulations/src
repos/simulations/src/income/circle.zig
const std = @import("std"); const math = std.math; const zgpu = @import("zgpu"); const Self = @This(); position: [4]f32, color: [4]f32, radius: f32, pub fn createIndexBuffer(gctx: *zgpu.GraphicsContext, comptime num_vertices: u32) zgpu.BufferHandle { const num_triangles = num_vertices - 1; const consumer_index_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .index = true }, .size = num_triangles * 3 * @sizeOf(u32), }); const num_indices = num_triangles * 3; var indices: [num_indices]u32 = undefined; var i: usize = 0; while (i < num_triangles) { indices[i * 3] = 0; indices[i * 3 + 1] = @as(u32, @intCast(i)) + 1; indices[i * 3 + 2] = @as(u32, @intCast(i)) + 2; i += 1; } indices[num_indices - 1] = 1; gctx.queue.writeBuffer(gctx.lookupResource(consumer_index_buffer).?, 0, u32, indices[0..]); return consumer_index_buffer; } pub fn createVertexBuffer( gctx: *zgpu.GraphicsContext, comptime num_vertices: u32, radius: f32, ) zgpu.BufferHandle { const consumer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = num_vertices * @sizeOf(f32) * 3, }); var consumer_vertex_data: [num_vertices][3]f32 = undefined; const num_sides = @as(f32, num_vertices - 1); const angle = 2 * math.pi / num_sides; consumer_vertex_data[0] = [3]f32{ 0, 0, 0 }; var i: u32 = 1; while (i < num_vertices) { const current_angle = angle * @as(f32, @floatFromInt(i)); const x = @cos(current_angle) * radius; const y = @sin(current_angle) * radius; consumer_vertex_data[i] = [3]f32{ x, y, 0 }; i += 1; } gctx.queue.writeBuffer(gctx.lookupResource(consumer_vertex_buffer).?, 0, [3]f32, consumer_vertex_data[0..]); return consumer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/income/consumer.zig
const std = @import("std"); const math = std.math; const array = std.ArrayList; const Allocator = std.mem.Allocator; const random = std.crypto.random; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Main = @import("main.zig"); const DemoState = Main.DemoState; const Parameters = Main.Parameters; const Wgpu = @import("wgpu.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Self = @This(); absolute_home: [4]i32 = .{ 0, 0, 0, 0 }, position: [4]f32 = .{ 0, 0, 0, 0 }, home: [4]f32 = .{ 0, 0, 0, 0 }, destination: [4]f32 = .{ 0, 0, 0, 0 }, color: [4]f32 = .{ 1, 0, 0, 0 }, step_size: [2]f32 = .{ 0, 0 }, moving_rate: f32 = 0, max_demand_rate: u32 = 0, income_quartile: u32 = 0, income: u32 = 0, radius: f32 = 20.0, inventory: u32 = 0, balance: u32 = 0, max_balance: u32 = 100000, producer_id: i32 = -1, grouping_id: u32 = 0, pub const z_pos = 0; pub fn generateFromParams(demo: *DemoState) void { const consumer_incomes = demo.params.consumer_incomes; for (consumer_incomes, 0..) |ci, i| { const num: u32 = @intFromFloat(ci.new.num); for (0..num) |_| { const c = createNewConsumer(demo, @intCast(i)); appendConsumer(demo, c); } } } pub fn createNewConsumer(demo: *DemoState, i: u32) Self { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); const f_x = @as(f32, @floatFromInt(x)) * demo.params.aspect; const f_y = @as(f32, @floatFromInt(y)); const home = [4]f32{ f_x, f_y, z_pos, 1 }; return .{ .absolute_home = .{ x, y, z_pos, 1 }, .position = home, .home = home, .destination = home, .income = @intFromFloat(demo.params.consumer_incomes[i].new.income), .income_quartile = i, .moving_rate = demo.params.moving_rate, .max_demand_rate = demo.params.max_demand_rate, }; } pub fn appendConsumer(demo: *DemoState, c: Self) void { const obj_buf = &demo.buffers.data.consumers; var consumers: [1]Self = .{c}; Wgpu.appendBuffer(demo.gctx, Self, .{ .num_old_structs = @as(u32, @intCast(obj_buf.list.items.len)), .buf = obj_buf.buf, .structs = consumers[0..], }); obj_buf.list.append(c) catch unreachable; obj_buf.mapping.num_structs += 1; demo.params.consumer_incomes[c.income_quartile].num += 1; } pub fn setParamAll( demo: *DemoState, comptime tag: []const u8, comptime T: type, num: T, ) void { const buf = demo.buffers.data.consumers.buf; const resource = demo.gctx.lookupResource(buf).?; const field_enum = @field(std.meta.FieldEnum(Self), tag); const field_type = std.meta.FieldType(Self, field_enum); std.debug.assert(field_type == T); const struct_offset = @offsetOf(Self, tag); for (demo.buffers.data.consumers.list.items, 0..) |_, i| { const offset = i * @sizeOf(Self) + struct_offset; demo.gctx.queue.writeBuffer(resource, offset, field_type, &.{num}); } } pub fn setQuartileIncome(demo: *DemoState, quartile: u32, income: u32) void { const buf = demo.buffers.data.consumers.buf; const resource = demo.gctx.lookupResource(buf).?; const struct_offset = @offsetOf(Self, "income"); for (demo.buffers.data.consumers.list.items, 0..) |c, i| { if (c.income_quartile == quartile) { const offset = i * @sizeOf(Self) + struct_offset; demo.gctx.queue.writeBuffer(resource, offset, u32, &.{income}); } } } pub fn expandQuartile(demo: *DemoState, quartile: u32, num: u32) void { const num_to_add: usize = @intCast(num); for (0..num_to_add) |_| { const c = createNewConsumer(demo, quartile); appendConsumer(demo, c); } } pub fn shrinkQuartile(demo: *DemoState, quartile: u32, num: u32) void { const num_to_remove: usize = @intCast(num); for (0..num_to_remove) |_| { removeOneFromQuartile(demo, quartile) catch unreachable; } } pub fn removeOneFromQuartile(demo: *DemoState, quartile: u32) !void { const buf = &demo.buffers.data.consumers; for (buf.list.items, 0..) |c, i| { if (c.income_quartile == quartile) { replaceIndexWithLastElement(demo, i); zeroOutLastElement(demo); _ = buf.list.swapRemove(i); buf.mapping.num_structs -= 1; demo.params.consumer_incomes[quartile].num -= 1; return; } } return error.CouldNotRemove; } pub fn replaceIndexWithLastElement(demo: *DemoState, i: usize) void { const buf = demo.buffers.data.consumers.buf; const buff = demo.gctx.lookupResource(buf).?; const offset = @sizeOf(Self) * i; const consumers = demo.buffers.data.consumers; const end_index = consumers.list.items.len - 1; const replace = consumers.list.items[end_index]; demo.gctx.queue.writeBuffer(buff, offset, Self, &.{replace}); } pub fn zeroOutLastElement(demo: *DemoState) void { const buf = demo.buffers.data.consumers.buf; const buff = demo.gctx.lookupResource(buf).?; const end_index = demo.buffers.data.consumers.list.items.len - 1; const end_offset = @sizeOf(Self) * end_index; const all_zero = [_]u8{0} ** @sizeOf(Self); const zeros = all_zero[0..]; demo.gctx.queue.writeBuffer(buff, end_offset, u8, zeros); }
0
repos/simulations/src
repos/simulations/src/income/build.zig
const std = @import("std"); const Options = @import("../../../build.zig").Options; pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = "Simulations", .root_source_file = b.path("src/resources/income/main.zig"), .target = options.target, .optimize = options.optimize, }); @import("system_sdk").addLibraryPathsTo(exe); const zglfw = b.dependency("zglfw", .{ .target = options.target, }); exe.root_module.addImport("zglfw", zglfw.module("root")); exe.linkLibrary(zglfw.artifact("glfw")); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{ .target = options.target, }); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); const zmath = b.dependency("zmath", .{ .target = options.target, }); exe.root_module.addImport("zmath", zmath.module("root")); const zgui = b.dependency("zgui", .{ .target = options.target, .backend = .glfw_wgpu, }); exe.root_module.addImport("zgui", zgui.module("root")); exe.linkLibrary(zgui.artifact("imgui")); const zpool = b.dependency("zpool", .{ .target = options.target, }); exe.root_module.addImport("zpool", zpool.module("root")); const zstbi = b.dependency("zstbi", .{ .target = options.target, }); exe.root_module.addImport("zstbi", zstbi.module("root")); exe.linkLibrary(zstbi.artifact("zstbi")); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "" }, .install_subdir = "bin/content", }); exe.step.dependOn(&install_content_step.step); return exe; } inline fn thisDir() []const u8 { return comptime std.fs.path.dirname(@src().file) orelse "."; }
0
repos/simulations/src
repos/simulations/src/income/gui.zig
const std = @import("std"); const random = std.crypto.random; const zgpu = @import("zgpu"); const zgui = @import("zgui"); const wgpu = zgpu.wgpu; const Main = @import("main.zig"); const DemoState = Main.DemoState; const Statistics = @import("statistics.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); const Circle = @import("circle.zig"); const Callbacks = @import("callbacks.zig"); pub const FullPos = struct { pos: Pos, size: Pos, id: GuiID, }; pub const GuiID = struct { num: i32, str: [:0]const u8, }; pub const Pos = struct { x: f32, y: f32, margin: struct { percent: f32 = 0.02, top: bool = true, bottom: bool = true, left: bool = true, right: bool = true, } = .{}, }; pub fn update(demo: *DemoState) void { setUpWindow(demo, parameters, demo.imgui_windows[0]); setUpWindow(demo, plots, demo.imgui_windows[1]); } const gui_fn = *const fn (demo: *DemoState) void; pub fn setUpWindow(demo: *DemoState, func: gui_fn, full_pos: FullPos) void { setupWindowPos(demo, full_pos.pos); setupWindowSize(demo, full_pos.size); runWindow(demo, func, full_pos.id); } pub fn setupWindowPos(demo: *DemoState, pos: Pos) void { const sd = demo.gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const pos_margin_pixels = getMarginPixels(sd, pos.margin.percent); var x = width * pos.x; if (pos.margin.left) { x += pos_margin_pixels; } var y = height * pos.y; if (pos.margin.top) { y += pos_margin_pixels; } zgui.setNextWindowPos(.{ .x = x, .y = y }); } pub fn setupWindowSize(demo: *DemoState, size: Pos) void { const sd = demo.gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const size_margin_pixels = getMarginPixels(sd, size.margin.percent); var w = width * size.x; if (size.margin.left) { w -= size_margin_pixels; } if (size.margin.right) { w -= size_margin_pixels; } var h = height * size.y; if (size.margin.top) { h -= size_margin_pixels; } if (size.margin.bottom) { h -= size_margin_pixels; } zgui.setNextWindowSize(.{ .w = w, .h = h }); } pub fn runWindow(demo: *DemoState, func: gui_fn, id: GuiID) void { const flags = zgui.WindowFlags.no_decoration; const start = zgui.begin(id.str, .{ .flags = flags }); defer zgui.end(); if (start) { zgui.pushIntId(id.num); defer zgui.popId(); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); func(demo); } } fn getMarginPixels(sd: wgpu.SwapChainDescriptor, margin_percent: f32) f32 { const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const margin_x = width * margin_percent; const margin_y = height * margin_percent; return @min(margin_x, margin_y); } fn parameters(demo: *DemoState) void { income_quartiles(demo); max_demand_rate(demo); moving_rate(demo); num_producers(demo); production_rate(demo); price(demo); max_producer_inventory(demo); consumer_size(demo); buttons(demo); } fn income_quartiles(demo: *DemoState) void { const width = zgui.getContentRegionAvail()[0]; const plot_flags = .{ .flags = zgui.plot.Flags.canvas_only, .h = width, }; if (zgui.plot.beginPlot("Consumer Income", plot_flags)) { const x_flags = .{ .label = "Income Rate", .flags = .{} }; const y_flags = .{ .label = "Number of Consumers", .flags = .{} }; const x_limit_flags = .{ .min = 0, .max = 500, .cond = .always }; const y_limit_flags = .{ .min = 0, .max = 5000, .cond = .always }; zgui.plot.setupAxis(.x1, x_flags); zgui.plot.setupAxis(.y1, y_flags); zgui.plot.setupAxisLimits(.x1, x_limit_flags); zgui.plot.setupAxisLimits(.y1, y_limit_flags); const income_ptr = &demo.params.consumer_incomes; inline for (income_ptr, 0..) |_, i| { const ptr = &income_ptr[i].new; zgui.plot.plotBars("Consumer Income Brackets", f64, .{ .xv = &.{ptr.income}, .yv = &.{ptr.num}, .bar_size = 50, }); const red = .{ 1, 0, 0, 1 }; const flags = .{ .x = &ptr.income, .y = &ptr.num, .col = red }; if (zgui.plot.dragPoint(i, flags)) { if (ptr.num <= 0) ptr.num = 0; if (ptr.income > 500) ptr.income = 500; if (ptr.income < 0) ptr.income = 0; const point = &demo.params.consumer_incomes[i]; const new: u32 = @intFromFloat(point.new.num); const old: u32 = @intFromFloat(point.old.num); if (new > old) { Consumer.expandQuartile(demo, i, new - old); } else if (new < old) { Consumer.shrinkQuartile(demo, i, old - new); } if (point.new.income != point.old.income) { const u_income: u32 = @intFromFloat(point.new.income); Consumer.setQuartileIncome(demo, i, u_income); } point.old.income = point.new.income; point.old.num = point.new.num; } } zgui.plot.endPlot(); } } fn max_demand_rate(demo: *DemoState) void { zgui.text("Max Demand Rate", .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted( "The maximum amount consumers will buy from producers if they have enough money.", ); zgui.endTooltip(); } const mdr = &demo.params.max_demand_rate; const flags = .{ .v = mdr, .min = 1, .max = 200 }; if (zgui.sliderScalar("##dr", u32, flags)) { Consumer.setParamAll(demo, "max_demand_rate", u32, mdr.*); } } fn moving_rate(demo: *DemoState) void { zgui.text("Moving Rate", .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted( "How fast consumers move to and from producers.", ); zgui.endTooltip(); } const mr = &demo.params.moving_rate; const flags = .{ .v = mr, .min = 1.0, .max = 20 }; if (zgui.sliderScalar("##mr", f32, flags)) { Consumer.setParamAll(demo, "moving_rate", f32, mr.*); } } fn consumer_size(demo: *DemoState) void { zgui.text("Consumer Size", .{}); const cr = &demo.params.consumer_radius; const flags = .{ .v = cr, .min = 1, .max = 3 }; if (zgui.sliderScalar("##cs", f32, flags)) { demo.buffers.vertex.circle = Circle.createVertexBuffer( demo.gctx, 40, cr.*, ); } } fn num_producers(demo: *DemoState) void { zgui.text("Number Of Producers", .{}); const new = &demo.params.num_producers; const old: u32 = @intCast(demo.buffers.data.producers.list.items.len); const flags = .{ .v = new, .min = 1, .max = 100 }; if (zgui.sliderScalar("##np", u32, flags)) { Statistics.setNum(demo, new.*, .producers); if (old >= new.*) { const buf = demo.buffers.data.producers.buf; Wgpu.shrinkBuffer(demo.gctx, buf, Producer, new.*); demo.buffers.data.producers.list.resize(new.*) catch unreachable; demo.buffers.data.producers.mapping.num_structs = new.*; } else { Producer.generateBulk(demo, new.* - old); } } } fn production_rate(demo: *DemoState) void { zgui.text("Production Rate", .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted( "How many resources a producer creates each cycle.", ); zgui.endTooltip(); } const pr = &demo.params.production_rate; const flags = .{ .v = pr, .min = 1, .max = 1000 }; if (zgui.sliderScalar("##pr", u32, flags)) { Producer.setParamAll(demo, "production_rate", u32, pr.*); } } fn price(demo: *DemoState) void { const title = "Resource Price"; const help = "The cost a consumer must pay to buy 1 resource item."; infoButton(title, help); const p = &demo.params.price; const flags = .{ .v = p, .min = 1, .max = 1000 }; if (zgui.sliderScalar("##p", u32, flags)) { Producer.setParamAll(demo, "price", u32, p.*); } } fn max_producer_inventory(demo: *DemoState) void { zgui.text("Max Producer Inventory", .{}); const mi = &demo.params.max_inventory; const flags = .{ .v = mi, .min = 10, .max = 10000 }; if (zgui.sliderScalar("##mi", u32, flags)) { Producer.setParamAll(demo, "max_inventory", u32, mi.*); } } fn infoButton(comptime text: []const u8, comptime info: []const u8) void { zgui.text(text, .{}); zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted(info); zgui.endTooltip(); } } fn buttons(demo: *DemoState) void { if (zgui.button("Start", .{})) { demo.running = true; } zgui.sameLine(.{}); if (zgui.button("Stop", .{})) { demo.running = false; } zgui.sameLine(.{}); if (zgui.button("Restart", .{})) { demo.running = true; Main.restartSimulation(demo); } if (zgui.button("Supply Shock", .{})) { Producer.setParamAll(demo, "inventory", i32, 0); } zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted("Set all producer inventory to 0."); zgui.endTooltip(); } } fn plots(demo: *DemoState) void { const size = zgui.getWindowSize(); const margin = 15; const plot_size = .{ .w = size[0] - margin, .h = size[1] - margin }; if (zgui.plot.beginPlot("", plot_size)) { defer zgui.plot.endPlot(); var y_flags: zgui.plot.AxisFlags = .{ .auto_fit = true }; if (demo.params.plot_hovered) { y_flags = .{ .lock_min = true }; } const x_axis = .{ .label = "", .flags = .{ .auto_fit = true } }; const y_axis = .{ .label = "", .flags = y_flags }; zgui.plot.setupAxis(.x1, x_axis); zgui.plot.setupAxis(.y1, y_axis); zgui.plot.setupLegend(.{ .north = true, .west = true }, .{}); demo.params.plot_hovered = zgui.plot.isPlotHovered(); const stats = demo.stats; const nt = .{ .v = stats.num_transactions.items[0..] }; const ec = .{ .v = stats.num_empty_consumers.items[0..] }; const tpi = .{ .v = stats.num_total_producer_inventory.items[0..] }; const acb = .{ .v = stats.avg_consumer_balance.items[0..] }; zgui.plot.plotLineValues("Transactions", u32, nt); zgui.plot.plotLineValues("Empty Consumers", u32, ec); zgui.plot.plotLineValues("Total Producer Inventory", u32, tpi); zgui.plot.plotLineValues("Average Consumer Balance", u32, acb); } }
0
repos/simulations/src
repos/simulations/src/income/wgpu.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zm = @import("zmath"); const zems = @import("zems"); const Gctx = zgpu.GraphicsContext; const wgpu = zgpu.wgpu; const DemoState = @import("main.zig").DemoState; const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_STRUCTS = 10000; // A mishmash of Wgpu initialization functions and buffer helpers for an array of generic structs // Data Types pub const GraphicsObject = struct { render_pipeline: zgpu.RenderPipelineHandle, attribute_buffer: zgpu.BufferHandle, vertex_buffer: zgpu.BufferHandle, index_buffer: zgpu.BufferHandle, size_of_struct: u32, }; pub fn ObjectBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, list: std.ArrayList(T), mapping: MappingBuffer(T), }; } const callback_queue_len: usize = 10; fn MappingBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, insert_idx: usize = 0, remove_idx: usize = 0, requests: [callback_queue_len]struct { func: Callback(T), args: Callbacks.Args(T), } = undefined, staging: StagingBuffer(T), waiting: bool = false, num_structs: u32, }; } fn StagingBuffer(comptime T: type) type { return struct { slice: ?[]const T = null, buffer: wgpu.Buffer = undefined, num_structs: u32, ready: bool = false, }; } fn Callback(comptime T: type) type { return ?*const fn (args: Callbacks.Args(T)) void; } pub const RenderPipelineInfo = struct { pub const Attribute = struct { name: []const u8, type: type, }; vs: [:0]const u8, fs: [:0]const u8, inst_type: type, inst_attrs: []const Attribute, primitive_topology: wgpu.PrimitiveTopology = .triangle_list, }; pub const ComputePipelineInfo = struct { cs: [:0]const u8, entry_point: [:0]const u8, }; pub fn GenCallback(comptime T: type) wgpu.BufferMapCallback { return struct { fn callback( status: wgpu.BufferMapAsyncStatus, userdata: ?*anyopaque, ) callconv(.C) void { const usb = @as(*StagingBuffer(T), @ptrCast(@alignCast(userdata))); std.debug.assert(usb.slice == null); if (status == .success) { const data = usb.buffer.getConstMappedRange( T, 0, usb.num_structs, ); if (data) |d| { usb.slice = d; } usb.ready = true; } else { std.log.err("[zgpu] Failed to map buffer (code: {any})\n", .{status}); } } }.callback; } pub fn getAllAsync( demo: *DemoState, comptime T: type, callback: Callback(T), ) void { const buf = switch (T) { u32 => &demo.buffers.data.stats, Consumer => &demo.buffers.data.consumers, Producer => &demo.buffers.data.producers, else => unreachable, }; const map_ptr = &buf.mapping; if (map_ptr.num_structs < 0) return; map_ptr.staging.num_structs = map_ptr.num_structs; map_ptr.requests[map_ptr.insert_idx].func = callback; map_ptr.requests[map_ptr.insert_idx].args = .{ .gctx = demo.gctx, .buf = buf, .stats = &demo.stats }; map_ptr.insert_idx = (map_ptr.insert_idx + 1) % callback_queue_len; runMapIfReady(T, map_ptr); } pub fn runMapIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (!buf.waiting and buf.staging.slice == null and buf.insert_idx != buf.remove_idx) { const gctx = buf.requests[buf.remove_idx].args.gctx; buf.staging.buffer = gctx.lookupResource(buf.buf).?; buf.staging.buffer.mapAsync( .{ .read = true }, 0, @sizeOf(T) * buf.staging.num_structs, GenCallback(T), @as(*anyopaque, @ptrCast(&buf.staging)), ); buf.waiting = true; } } pub fn runCallbackIfReady(comptime T: type, buf: *MappingBuffer(T)) void { const request = buf.requests[buf.remove_idx]; if (buf.waiting and buf.staging.ready) { buf.remove_idx = (buf.remove_idx + 1) % callback_queue_len; request.func.?(request.args); buf.staging.buffer.unmap(); buf.staging.slice = null; buf.waiting = false; buf.staging.ready = false; } } pub fn waitForCallback(comptime T: type, buf: *MappingBuffer(T)) void { while (buf.waiting) { runCallbackIfReady(T, buf); } } pub fn getMappedData(comptime T: type, buf: *MappingBuffer(T)) []T { return @constCast(buf.staging.slice.?[0..buf.staging.num_structs]); } pub fn agentParameters(comptime T: type) type { switch (T) { Consumer => return union(enum) { moving_rate: f32, demand_rate: u32, }, Producer => return union(enum) { production_rate: u32, inventory: i32, max_inventory: u32, }, u32 => return u32, else => unreachable, } } pub fn setArgs(comptime T: type) type { return struct { agents: ObjectBuffer, parameter: agentParameters(T), }; } // pub fn setAll(gctx: *zgpu.GraphicsContext, comptime T: type, args: setArgs(T)) void { // var agents = getAllAsync(T, Callbacks.clearConsumerHovers, .{ // .gctx = gctx, // .buf = args.agents, // }); // for (agents, 0..) |_, i| { // setAgentParameter(T, &agents[i], args.parameter); // } // writeBuffer(gctx, args.agents.data, T, agents); // } pub fn writeBuffer( gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle, comptime T: type, structs: []T, ) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, T, structs); } pub fn setAgentParameter( comptime T: type, agent: *T, parameter: agentParameters(T), ) void { switch (T) { Consumer => { switch (parameter) { .moving_rate => |v| agent.moving_rate = v, .demand_rate => |v| agent.demand_rate = v, } }, Producer => { switch (parameter) { .production_rate => |v| agent.production_rate = v, .inventory => |v| agent.inventory = v, .max_inventory => |v| agent.max_inventory = v, } }, else => unreachable, } } pub fn setGroupingArgs(comptime T: type) type { return struct { setArgs: setArgs(T), grouping_id: u32, }; } //pub fn setGroup(gctx: *zgpu.GraphicsContext, comptime T: type, args: setGroupingArgs(T)) void { // var agents = getAll(gctx, T, .{ // .structs = args.setArgs.agents, // .num_structs = args.setArgs.num_structs, // }) catch return; // for (agents, 0..) |agent, i| { // if (args.grouping_id == agent.grouping_id) { // setAgentParameter(T, &agents[i], args.setArgs.parameter); // } // } // writeBuffer(gctx, args.setArgs.agents.data, T, agents); //} pub fn updateCoords(gctx: *zgpu.GraphicsContext, comptime T: type, obj_buf: ObjectBuffer(T)) void { for (obj_buf.list.items, 0..) |obj, i| { const args: bufArgs(T, [4]f32) = .{ .obj_buf = obj_buf, .index = i, .value = Camera.getWorldPosition(gctx, obj.absolute_home), }; writeToObjectBuffer(gctx, T, [4]f32, "home", args); if (T == Consumer) { writeToObjectBuffer(gctx, T, [4]f32, "position", args); writeToObjectBuffer(gctx, T, [4]f32, "destination", args); } } } pub fn bufArgs(comptime T: type, comptime V: type) type { return struct { obj_buf: ObjectBuffer(T), index: usize, value: V, }; } pub fn writeToObjectBuffer( gctx: *Gctx, comptime T: type, comptime V: type, comptime field: []const u8, args: bufArgs(T, V), ) void { gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); gctx.queue.writeBuffer( gctx.lookupResource(args.obj_buf.mapping.buf).?, args.index * @sizeOf(T) + @offsetOf(T, field), V, &.{args.value}, ); } pub fn writeToMappedBuffer(gctx: *Gctx, buf: zgpu.BufferHandle, mapped: zgpu.BufferHandle) void { const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); pass: { const p = gctx.lookupResource(buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(buf) orelse break :pass; const pm = gctx.lookupResource(mapped) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); } pub fn shrinkBuffer( gctx: *Gctx, buf: zgpu.BufferHandle, comptime T: type, new_size: u32, ) void { const all_zero = [_]u8{0} ** 10000000; const buff = gctx.lookupResource(buf).?; const buf_info = gctx.lookupResourceInfo(buf).?; const size_to_keep = @sizeOf(T) * new_size; const size_to_clear = buf_info.size - size_to_keep; const usize_to_clear = @as(usize, @intCast(size_to_clear)); gctx.queue.writeBuffer( buff, size_to_keep, u8, all_zero[0..usize_to_clear], ); } pub fn appendArgs(comptime T: type) type { return struct { num_old_structs: u32, buf: zgpu.BufferHandle, structs: []T, }; } pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void { gctx.queue.writeBuffer( gctx.lookupResource(args.buf).?, args.num_old_structs * @sizeOf(T), T, args.structs, ); } pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 0, u8, all_zero[0..b_size], ); } pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.buf).?, 0, u8, all_zero[0..b_size], ); const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?; const m_size = @as(usize, @intCast(map_buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.mapping.buf).?, 0, u8, all_zero[0..m_size], ); obj_buf.list.clearAndFree(); obj_buf.mapping.insert_idx = 0; obj_buf.mapping.remove_idx = 0; obj_buf.mapping.waiting = false; obj_buf.mapping.staging.ready = false; obj_buf.mapping.staging.slice = null; obj_buf.mapping.num_structs = 0; obj_buf.mapping.staging.num_structs = 0; } // Blank Buffers pub fn createBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .copy_src = true, .vertex = true, .storage = true }, .size = num * @sizeOf(T), }); } pub fn createMappedBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .map_read = true }, .size = num * @sizeOf(T), }); } pub fn createObjectBuffer( allocator: std.mem.Allocator, gctx: *Gctx, comptime T: type, len: u32, num_structs: u32, ) ObjectBuffer(T) { return .{ .buf = createBuffer(gctx, T, len), .list = std.ArrayList(T).init(allocator), .mapping = .{ .buf = createMappedBuffer(gctx, T, len), .num_structs = num_structs, .staging = .{ .num_structs = num_structs, }, }, }; } // Depth Texture pub const Depth = struct { texture: zgpu.TextureHandle, view: zgpu.TextureViewHandle, }; pub fn createDepthTexture(gctx: *zgpu.GraphicsContext) Depth { const texture = gctx.createTexture(.{ .usage = .{ .render_attachment = true }, .dimension = .tdim_2d, .size = .{ .width = gctx.swapchain_descriptor.width, .height = gctx.swapchain_descriptor.height, .depth_or_array_layers = 1, }, .format = .depth32_float, .mip_level_count = 1, .sample_count = 1, }); const view = gctx.createTextureView(texture, .{}); return .{ .texture = texture, .view = view }; } // Bind Group Layouts pub fn createUniformBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .vertex = true }, .uniform, true, 0), }); } pub fn createComputeBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(1, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(2, .{ .compute = true }, .storage, false, 0), }); } // Bind Groups pub fn createUniformBindGroup(gctx: *Gctx) zgpu.BindGroupHandle { const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); return gctx.createBindGroup(bind_group_layout, &.{ .{ .binding = 0, .buffer_handle = gctx.uniforms.buffer, .offset = 0, .size = @sizeOf(zm.Mat) }, }); } pub const computeBindGroup = struct { consumer: zgpu.BufferHandle, producer: zgpu.BufferHandle, stats: zgpu.BufferHandle, }; pub fn createComputeBindGroup(gctx: *Gctx, args: computeBindGroup) zgpu.BindGroupHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const c_info = gctx.lookupResourceInfo(args.consumer) orelse unreachable; const p_info = gctx.lookupResourceInfo(args.producer) orelse unreachable; const s_info = gctx.lookupResourceInfo(args.stats) orelse unreachable; return gctx.createBindGroup(compute_bgl, &[_]zgpu.BindGroupEntryInfo{ .{ .binding = 0, .buffer_handle = args.consumer, .offset = 0, .size = c_info.size, }, .{ .binding = 1, .buffer_handle = args.producer, .offset = 0, .size = p_info.size, }, .{ .binding = 2, .buffer_handle = args.stats, .offset = 0, .size = s_info.size, }, }); } fn getWgpuType(comptime T: type) !wgpu.VertexFormat { return switch (T) { u32 => .uint32, f32 => .float32, [2]f32 => .float32x2, [3]f32 => .float32x3, [4]f32 => .float32x4, else => error.NoValidWgpuType, }; } pub fn createRenderPipeline( gctx: *zgpu.GraphicsContext, comptime args: RenderPipelineInfo, ) zgpu.RenderPipelineHandle { const vs_module = zgpu.createWgslShaderModule(gctx.device, args.vs, "vs"); defer vs_module.release(); const fs_module = zgpu.createWgslShaderModule(gctx.device, args.fs, "fs"); defer fs_module.release(); const color_targets = [_]wgpu.ColorTargetState{.{ .format = zgpu.GraphicsContext.swapchain_format, .blend = &.{ .color = .{}, .alpha = .{} }, }}; const vertex_attributes = [_]wgpu.VertexAttribute{ .{ .format = .float32x3, .offset = 0, .shader_location = 0 }, }; const instance_attributes = init: { var arr: [args.inst_attrs.len]wgpu.VertexAttribute = undefined; inline for (args.inst_attrs, 0..) |attr, i| { arr[i] = .{ .format = getWgpuType(attr.type) catch unreachable, .offset = @offsetOf(args.inst_type, attr.name), .shader_location = i + 1, }; } break :init arr; }; const vertex_buffers = [_]wgpu.VertexBufferLayout{ .{ .array_stride = @sizeOf(f32) * 3, .attribute_count = vertex_attributes.len, .attributes = &vertex_attributes, .step_mode = .vertex, }, .{ .array_stride = @sizeOf(args.inst_type), .attribute_count = instance_attributes.len, .attributes = &instance_attributes, .step_mode = .instance, }, }; const pipeline_descriptor = wgpu.RenderPipelineDescriptor{ .vertex = wgpu.VertexState{ .module = vs_module, .entry_point = "main", .buffer_count = vertex_buffers.len, .buffers = &vertex_buffers, }, .primitive = wgpu.PrimitiveState{ .front_face = .ccw, .cull_mode = .none, .topology = args.primitive_topology, }, .depth_stencil = &wgpu.DepthStencilState{ .format = .depth32_float, .depth_write_enabled = true, .depth_compare = .less_equal, }, .fragment = &wgpu.FragmentState{ .module = fs_module, .entry_point = "main", .target_count = color_targets.len, .targets = &color_targets, }, }; const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); const pipeline_layout = gctx.createPipelineLayout(&.{bind_group_layout}); return gctx.createRenderPipeline(pipeline_layout, pipeline_descriptor); } pub fn createComputePipeline(gctx: *zgpu.GraphicsContext, cpi: ComputePipelineInfo) zgpu.ComputePipelineHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const compute_pl = gctx.createPipelineLayout(&.{compute_bgl}); defer gctx.releaseResource(compute_pl); const cs_module = zgpu.createWgslShaderModule(gctx.device, cpi.cs, "cs"); defer cs_module.release(); const pipeline_descriptor = wgpu.ComputePipelineDescriptor{ .compute = wgpu.ProgrammableStageDescriptor{ .module = cs_module, .entry_point = cpi.entry_point, }, }; return gctx.createComputePipeline(compute_pl, pipeline_descriptor); }
0
repos/simulations/src
repos/simulations/src/income/square.zig
const zgpu = @import("zgpu"); pub fn createVertexBuffer(gctx: *zgpu.GraphicsContext, width: f32) zgpu.BufferHandle { const producer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = 6 * @sizeOf(f32) * 3, }); const upper_left = [3]f32{ -width, width, 0.0 }; const lower_left = [3]f32{ -width, -width, 0.0 }; const upper_right = [3]f32{ width, width, 0.0 }; const lower_right = [3]f32{ width, -width, 0.0 }; const vertex_array = [6][3]f32{ upper_left, lower_left, lower_right, lower_right, upper_right, upper_left, }; gctx.queue.writeBuffer(gctx.lookupResource(producer_vertex_buffer).?, 0, [3]f32, vertex_array[0..]); return producer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/income/callbacks.zig
const std = @import("std"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Statistics = @import("statistics.zig"); pub fn Args(comptime T: type) type { return struct { gctx: *zgpu.GraphicsContext, buf: *Wgpu.ObjectBuffer(T), stats: *Statistics = undefined, }; } pub fn numTransactions(args: Args(u32)) void { const slice = args.buf.mapping.staging.slice; var num: u32 = 0; if (slice) |stats| { num = stats[0]; } args.stats.num_transactions.append(num) catch unreachable; Statistics.clearNumTransactions(args.gctx, args.buf.buf); } pub fn totalInventory(args: Args(Producer)) void { const slice = args.buf.mapping.staging.slice; var total_inventory: u32 = 0; if (slice) |producers| { for (producers) |p| { total_inventory += @as(u32, @intCast(p.inventory)); } } args.stats.num_total_producer_inventory.append(total_inventory) catch unreachable; } pub fn consumerStats(args: Args(Consumer)) void { const slice = args.buf.mapping.staging.slice; var empty_consumers: u32 = 0; if (slice) |consumers| { for (consumers) |c| { if (c.inventory == 0) { empty_consumers += 1; } } } args.stats.num_empty_consumers.append(empty_consumers) catch unreachable; var total_balance: u32 = 0; if (slice) |consumers| { for (consumers) |c| { total_balance += c.balance; } } const len: u32 = @intCast(args.buf.list.items.len + 1); const avg_balance = total_balance / len; args.stats.avg_consumer_balance.append(avg_balance) catch unreachable; }
0
repos/simulations/src/income
repos/simulations/src/income/shaders/shaders.zig
// zig fmt: off pub const vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) demand_rate: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(demand_rate); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (vertex_position[0] * scale); \\ var y = position[1] + (vertex_position[1] * scale); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const producer_vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) max_inventory: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(max_inventory); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (scale * vertex_position[0]); \\ var y = position[1] + (scale * vertex_position[1]); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const fs = \\ @stage(fragment) fn main( \\ @location(0) color: vec3<f32>, \\ ) -> @location(0) vec4<f32> { \\ return vec4(color, 1.0); \\ } ; pub const cs = \\ struct Consumer { \\ position: vec4<f32>, \\ home: vec4<f32>, \\ absolute_home: vec4<f32>, \\ destination: vec4<f32>, \\ step_size: vec4<f32>, \\ color: vec4<f32>, \\ moving_rate: f32, \\ demand_rate: u32, \\ inventory: u32, \\ radius: f32, \\ producer_id: i32, \\ } \\ struct Producer { \\ position: vec4<f32>, \\ absolute_pos: vec4<f32>, \\ color: vec4<f32>, \\ production_rate: u32, \\ inventory: atomic<u32>, \\ max_inventory: u32, \\ len: atomic<u32>, \\ queue: array<u32, 480>, \\ } \\ struct Stats { \\ transactions: u32, \\ } \\ \\ @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; \\ @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; \\ @group(0) @binding(2) var<storage, read_write> stats: Stats; \\ @compute @workgroup_size(64) \\ fn consumer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let nc = arrayLength(&consumers); \\ if(GlobalInvocationID.x >= nc) { \\ return; \\ } \\ let c = consumers[index]; \\ consumers[index].position += c.step_size; \\ let dist = abs(c.position - c.destination); \\ let at_destination = all(dist.xy <= vec2<f32>(0.1)); \\ \\ if (at_destination) { \\ var new_destination = vec4<f32>(0); \\ let at_home = all(c.destination == c.home); \\ if (at_home) { \\ consumers[index].position = c.home; \\ let consumption_rate = 1u; \\ if (c.inventory >= consumption_rate) { \\ consumers[index].inventory -= consumption_rate; \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ return; \\ } \\ consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); \\ var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); \\ var shortest_distance = 100000.0; \\ var array_len = i32(arrayLength(&producers)); \\ for(var i = 0; i < array_len; i++){ \\ let dist = distance(c.home, producers[i].position); \\ let inventory = atomicLoad(&producers[i].inventory); \\ if (dist < shortest_distance && inventory > c.demand_rate) { \\ shortest_distance = dist; \\ consumers[index].destination = producers[i].position; \\ consumers[index].step_size = step_sizes(c.position, producers[i].position, c.moving_rate); \\ consumers[index].producer_id = i; \\ } \\ } \\ if (shortest_distance == 100000.0) { \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ } \\ } else { \\ let position = c.destination; \\ let pid = c.producer_id; \\ consumers[index].position = position; \\ consumers[index].step_size = vec4<f32>(0); \\ let idx = atomicAdd(&producers[pid].len, 1); \\ producers[pid].queue[idx] = index + 1; \\ } \\ } \\ } \\ fn step_sizes(pos: vec4<f32>, dest: vec4<f32>, mr: f32) -> vec4<f32>{ \\ let x_num_steps = num_steps(pos.x, dest.x, mr); \\ let y_num_steps = num_steps(pos.y, dest.y, mr); \\ let num_steps = max(x_num_steps, y_num_steps); \\ let distance = dest - pos; \\ return distance / num_steps; \\ } \\ fn num_steps(x: f32, y: f32, rate: f32) -> f32 { \\ let distance = abs(x - y); \\ if (rate > distance) { return 1.0; } \\ return ceil(distance / rate); \\ } \\ @compute @workgroup_size(64) \\ fn producer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let np = arrayLength(&producers); \\ if(GlobalInvocationID.x >= np) { \\ return; \\ } \\ let max_inventory = producers[index].max_inventory; \\ let inventory = atomicLoad(&producers[index].inventory); \\ var production_rate = producers[index].production_rate; \\ if (max_inventory > inventory) { \\ let diff = max_inventory - inventory; \\ production_rate = min(diff, production_rate); \\ let old_val = atomicAdd(&producers[index].inventory, production_rate); \\ } else if (inventory < max_inventory) { \\ atomicStore(&producers[index].inventory, max_inventory); \\ } \\ \\ let idx = atomicLoad(&producers[index].len); \\ for (var i = 0u; i < idx; i++) { \\ let cid = producers[index].queue[i] - 1; \\ let c = consumers[cid]; \\ let inventory = atomicLoad(&producers[index].inventory); \\ if (inventory >= c.demand_rate) { \\ consumers[cid].destination = c.home; \\ consumers[cid].step_size = step_sizes(c.position, c.home, c.moving_rate); \\ consumers[cid].inventory += c.demand_rate; \\ let old_inv = atomicSub(&producers[index].inventory, c.demand_rate); \\ stats.transactions += 1; \\ consumers[cid].color = vec4(0.0, 1.0, 0.0, 0.0); \\ } \\ } \\ atomicStore(&producers[index].len, 0); \\} ; // zig fmt: on
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/fragment/fragment.wgsl
@fragment fn main( @location(0) color: vec3<f32>, ) -> @location(0) vec4<f32> { return vec4(color, 1.0); }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/vertex/hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/vertex/producer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) max_inventory: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(max_inventory); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (scale * vertex_position[0]); var y = position[1] + (scale * vertex_position[1]); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/vertex/consumer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, ) -> VertexOut { var output: VertexOut; let scale = max(f32(inventory) / 5, 5.0); var x = position[0] + (vertex_position[0] * scale); var y = position[1] + (vertex_position[1] * scale); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/vertex/consumer_hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) hover_color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = hover_color.xyz; return output; }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/compute/common.wgsl
struct Consumer { absolute_home: vec4<i32>, position: vec4<f32>, home: vec4<f32>, destination: vec4<f32>, color: vec4<f32>, step_size: vec2<f32>, moving_rate: f32, max_demand_rate: u32, income_quartile: u32, income: u32, radius: f32, inventory: u32, balance: u32, max_balance: u32, producer_id: i32, grouping_id: u32, } struct Producer { absolute_home: vec4<i32>, home: vec4<f32>, color: vec4<f32>, production_rate: u32, inventory: atomic<i32>, max_inventory: u32, price: u32, } struct Stats { transactions: u32, num_consumers: u32, num_producers: u32, num_consumer_hovers: u32, random_color: vec4<f32>, } @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; @group(0) @binding(2) var<storage, read_write> stats: Stats; fn step_sizes(pos: vec2<f32>, dest: vec2<f32>, mr: f32) -> vec2<f32>{ let x_num_steps = num_steps(pos.x, dest.x, mr); let y_num_steps = num_steps(pos.y, dest.y, mr); let num_steps = max(x_num_steps, y_num_steps); let distance = dest - pos; return distance / num_steps; } fn num_steps(x: f32, y: f32, rate: f32) -> f32 { let distance = abs(x - y); if (rate > distance) { return 1.0; } return ceil(distance / rate); }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/compute/producer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_producers) { return; } let max_inventory = i32(producers[index].max_inventory); var production_rate = i32(producers[index].production_rate); let old_inventory = atomicAdd(&producers[index].inventory, production_rate); if (old_inventory + production_rate > max_inventory) { atomicStore(&producers[index].inventory, max_inventory); } }
0
repos/simulations/src/income/shaders
repos/simulations/src/income/shaders/compute/consumer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_consumers) { return; } // User removed producer this consumer was targeting if (consumers[index].producer_id >= i32(stats.num_producers)) { search_for_producer(index); } let c = consumers[index]; consumers[index].position[0] += c.step_size[0]; consumers[index].position[1] += c.step_size[1]; if (c.balance + c.income < c.max_balance) { consumers[index].balance += c.income; } let dist = abs(c.position - c.destination); let at_destination = all(dist.xy <= vec2<f32>(0.1)); if (at_destination) { consumers[index].step_size = vec2<f32>(0); consumers[index].position = c.destination; let at_home = all(c.destination == c.home); if (at_home) { if (c.inventory >= u32(1)) { consumers[index].inventory -= u32(1); return; } consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); let demand_rate = min(c.balance, c.max_demand_rate); if (demand_rate > 0) { search_for_producer(index); } return; } // At Producer let pid = c.producer_id; let max_consumer_can_buy = c.balance / producers[pid].price; let demand_rate = min(max_consumer_can_buy, c.max_demand_rate); let old_val = atomicSub(&producers[pid].inventory, i32(demand_rate)); // Went negative, revert inventory if (i32(demand_rate) > old_val) { atomicAdd(&producers[pid].inventory, i32(demand_rate)); return; } consumers[index].color = vec4(0.0, 1.0, 0.0, 0.0); consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); consumers[index].inventory += demand_rate; consumers[index].balance -= demand_rate * producers[pid].price; consumers[index].producer_id = -1; stats.transactions += u32(1); } } fn search_for_producer(index: u32){ let c = consumers[index]; var pid = find_nearest_stocked_producer(c, index); if (pid == -1) { consumers[index].destination = c.home; consumers[index].step_size = step_sizes(c.position.xy, c.home.xy, c.moving_rate); return; } let p_pos = producers[pid].home; consumers[index].destination = p_pos; consumers[index].step_size = step_sizes(c.position.xy, p_pos.xy, c.moving_rate); consumers[index].producer_id = pid; } // Returns the pid of nearest stocked producer, -1 for failure fn find_nearest_stocked_producer(c: Consumer, index: u32) -> i32 { var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); var shortest_distance = 100000.0; var pid: i32 = -1; for(var i: u32 = 0; i < stats.num_producers; i++){ let dist = distance(c.home, producers[i].home); let inventory = u32(atomicLoad(&producers[i].inventory)); let demand_rate = min(c.balance, c.max_demand_rate); if (dist < shortest_distance && inventory > demand_rate) { shortest_distance = dist; pid = i32(i); } } return pid; }
0
repos/simulations/libs
repos/simulations/libs/zpool/README.md
# zpool v0.10.0 - Generic pool & handle implementation Based on [Andre Weissflog's "Handles Are The Better Pointers"](https://floooh.github.io/2018/06/17/handles-vs-pointers.html) Exposing API resources using pools and handles is a common way to avoid exposing implementation details to calling code and providing some insulation against stale references in data structures maintained by the caller. When the caller is provided a handle instead of an opaque pointer, the API implementation is free to move resources around, replace them, and even discard them. ```zig Pool(index_bits: u8, cycle_bits: u8, TResource: type, TColumns: type) Handle(index_bits: u8, cycle_bits: u8, TResource: type) ``` The generic `Pool` type has configurable bit distribution for the `Handle`'s `index`/`cycle` fields, and supports multiple columns of data to be indexed by a handle, using `std.MultiArrayList` to store all of the pool data in a single memory allocation. The `TResource` parameter ensures the pool and handle types can be distinct types even when other parameters are the same. ## Getting started Copy `zpool` to a subdirectory of your project and add the following to your `build.zig.zon` .dependencies: ```zig .zpool = .{ .path = "libs/zpool" }, ``` Then in your `build.zig` add: ```zig pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ ... }); const zpool = b.dependency("zpool", .{}); exe.root_module.addImport("zpool", zpool.module("root")); } ``` Now in your code you may import and use `zpool`: ```zig const Pool = @import("zpool").Pool; const ImagePtr = graphics.Image; const ImageInfo = graphics.ImageInfo; pub const ImagePool = Pool(16, 16, ImagePtr, struct { ptr: ImagePtr, info: ImageInfo, }); pub const ImageHandle = ImagePool.Handle; ``` ```zig var imagePool = ImagePool.initMaxCapacity(allocator); defer pool.deinit(); ``` ```zig pub fn acquireImage(info: ImageInfo) !ImageHandle { const handle : ImageHandle = try imagePool.add(.{ .ptr = graphics.createImage(info), .info = info, }); return handle; } pub fn drawImage(handle: ImageHandle) !void { // get the stored ImagePtr const ptr : ImagePtr = try imagePool.getColumn(handle, .ptr); graphics.drawImage(ptr); } pub fn resizeImage(handle: ImageHandle, width: u16, height: u16) !void { // get a pointer to the stored ImageInfo const info : *ImageInfo = try imagePool.getColumnPtr(handle, .info); const old_width = info.width; const old_height = info.height; const old_pixels = // allocate memory to store old pixels // get the stored ImagePtr const ptr = try imagePool.getColumn(handle, .ptr); graphics.readPixels(ptr, old_pixels); const new_pixels = // allocate memory to store new pixels super_eagle.resizeImage( old_width, old_height, old_pixels, new_width, new_height, new_pixels); graphics.writePixels(ptr, new_width, new_height, new_pixels); // update the stored ImageInfo info.width = new_width; info.height = new_height; } ```
0
repos/simulations/libs
repos/simulations/libs/zpool/build.zig.zon
.{ .name = "zpool", .version = "0.10.0", .paths = .{ "build.zig", "build.zig.zon", "src", "README.md", }, }
0
repos/simulations/libs
repos/simulations/libs/zpool/build.zig
const std = @import("std"); pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{}); const target = b.standardTargetOptions(.{}); _ = b.addModule("root", .{ .root_source_file = b.path("src/main.zig"), }); const test_step = b.step("test", "Run zpool tests"); const tests = b.addTest(.{ .name = "zpool-tests", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); b.installArtifact(tests); test_step.dependOn(&b.addRunArtifact(tests).step); }
0
repos/simulations/libs/zpool
repos/simulations/libs/zpool/src/pool.zig
const std = @import("std"); pub const PoolError = error{PoolIsFull} || HandleError; pub const HandleError = error{ HandleIsUnacquired, HandleIsOutOfBounds, HandleIsReleased, }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns a struct that maintains a pool of data. Handles returned by /// `Pool.add()` can be used to get/set the data in zero or more columns. /// /// See `handles.zig` for more information on `index_bits` and `cycle_bits`, /// and handles in general. /// /// `TResource` identifies type of resource referenced by a handle, and /// provides a type-safe distinction between two otherwise equivalently /// configured `Handle` types, such as: /// * `const BufferHandle = Handle(16, 16, Buffer);` /// * `const TextureHandle = Handle(16, 16, Texture);` /// /// `TColumns` is a struct that defines the column names, and the element types /// of the column arrays. /// /// ```zig /// const Texture = gpu.Texture; /// /// const TexturePool = Pool(16, 16, Texture, struct { obj:Texture, desc:Texture.Descriptor }); /// const TextureHandle = TexturePool.Handle; /// /// const GPA = std.heap.GeneralPurposeAllocator; /// var gpa = GPA(.{}){}; /// var pool = try TexturePool.initMaxCapacity(gpa.allocator()); /// defer pool.deinit(); /// /// // creating a texture and adding it to the pool returns a handle /// const desc : Texture.Descriptor = .{ ... }; /// const obj = device.createTexture(desc); /// const handle : TextureHandle = pool.add(.{ .obj = obj, .desc = desc }); /// /// // elsewhere, use the handle to get `obj` or `desc` as needed /// const obj = pool.getColumn(handle, .obj); /// const desc = pool.getColumn(handle, .desc); /// /// // ... /// /// // once the texture is no longer needed, release it. /// _ = pool.removeIfLive(handle); /// ``` pub fn Pool( comptime index_bits: u8, comptime cycle_bits: u8, comptime TResource: type, comptime TColumns: type, ) type { // Handle performs compile time checks on index_bits & cycle_bits const ring_queue = @import("embedded_ring_queue.zig"); const handles = @import("handle.zig"); const utils = @import("utils.zig"); if (!utils.isStruct(TColumns)) @compileError("TColumns must be a struct"); const assert = std.debug.assert; const meta = std.meta; const Allocator = std.mem.Allocator; const MultiArrayList = std.MultiArrayList; const StructOfSlices = utils.StructOfSlices; const RingQueue = ring_queue.EmbeddedRingQueue; return struct { const Self = @This(); pub const Error = PoolError; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub const Resource = TResource; pub const Handle = handles.Handle(index_bits, cycle_bits, TResource); pub const AddressableHandle = Handle.AddressableHandle; pub const AddressableIndex = Handle.AddressableIndex; pub const AddressableCycle = Handle.AddressableCycle; pub const max_index: usize = Handle.max_index; pub const max_cycle: usize = Handle.max_cycle; pub const max_capacity: usize = Handle.max_count; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub const Columns = TColumns; pub const ColumnSlices = StructOfSlices(Columns); pub const Column = meta.FieldEnum(Columns); pub const column_fields = meta.fields(Columns); pub const column_count = column_fields.len; pub fn ColumnType(comptime column: Column) type { return meta.fieldInfo(Columns, column).type; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const private_fields = meta.fields(struct { @"Pool._free_queue": AddressableIndex, @"Pool._curr_cycle": AddressableCycle, }); const Storage = MultiArrayList(@Type(.{ .Struct = .{ .layout = .auto, .fields = private_fields ++ column_fields, .decls = &.{}, .is_tuple = false, } })); const FreeQueue = RingQueue(AddressableIndex); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - _allocator: Allocator = undefined, _storage: Storage = .{}, _free_queue: FreeQueue = .{}, _curr_cycle: []AddressableCycle = &.{}, columns: ColumnSlices = undefined, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns an initialized `Pool` that will use `allocator` for all /// allocations. The `Pool` stores all handles and columns in a single /// memory allocation backed by `std.MultiArrayList`. pub fn init(allocator: Allocator) Self { var self = Self{ ._allocator = allocator }; updateSlices(&self); return self; } /// Returns an initialized `Pool` that will use `allocator` for all /// allocations, with at least `min_capacity` preallocated. pub fn initCapacity(allocator: Allocator, min_capacity: usize) !Self { var self = Self{ ._allocator = allocator }; try self.reserve(min_capacity); return self; } /// Returns an initialized `Pool` that will use `allocator` for all /// allocations, with the `Pool.max_capacity` preallocated. pub fn initMaxCapacity(allocator: Allocator) !Self { return initCapacity(allocator, max_capacity); } /// Releases all resources assocated with an initialized pool. pub fn deinit(self: *Self) void { self.clear(); self._storage.deinit(self._allocator); self.* = .{}; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns the capacity of the pool, i.e. the maximum number of handles /// it can contain without allocating additional memory. pub fn capacity(self: Self) usize { return self._storage.capacity; } /// Requests the capacity of the pool be at least `min_capacity`. /// If the pool `capacity()` is already equal to or greater than /// `min_capacity`, `reserve()` has no effect. pub fn reserve(self: *Self, min_capacity: usize) !void { const old_capacity = self._storage.capacity; if (min_capacity <= old_capacity) return; if (min_capacity > max_capacity) return Error.PoolIsFull; try self._storage.setCapacity(self._allocator, min_capacity); updateSlices(self); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns the number of live handles. pub fn liveHandleCount(self: Self) usize { return self._storage.len - self._free_queue.len(); } /// Returns `true` if `handle` is live, otherwise `false`. pub fn isLiveHandle(self: Self, handle: Handle) bool { return self.isLiveAddressableHandle(handle.addressable()); } /// Checks whether `handle` is live. /// Unlike `std.debug.assert()`, this check is evaluated in all builds. pub fn requireLiveHandle(self: Self, handle: Handle) HandleError!void { try self.requireLiveAddressableHandle(handle.addressable()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns an iterator that can enumerate each live index. /// The iterator is invalidated by calls to `add()`. pub fn liveIndices(self: Self) LiveIndexIterator { return .{ .curr_cycle = self._curr_cycle }; } pub const LiveIndexIterator = struct { curr_cycle: []const AddressableCycle = &.{}, next_index: AddressableIndex = 0, ended: bool = false, pub fn next(self: *LiveIndexIterator) ?AddressableIndex { while (!self.ended and self.next_index < self.curr_cycle.len) { const curr_index = self.next_index; if (curr_index < max_index) { self.next_index += 1; } else { self.ended = true; } if (isLiveCycle(self.curr_cycle[curr_index])) return curr_index; } return null; } }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns an iterator that can enumerate each live handle. /// The iterator is invalidated by calls to `add()`. pub fn liveHandles(self: Self) LiveHandleIterator { return .{ .live_indices = liveIndices(self) }; } pub const LiveHandleIterator = struct { live_indices: LiveIndexIterator = .{}, pub fn next(self: *LiveHandleIterator) ?Handle { if (self.live_indices.next()) |index| { const ahandle = AddressableHandle{ .index = index, .cycle = self.live_indices.curr_cycle[index], }; return ahandle.handle(); } return null; } }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Releases all live `Handles` and calls `deinit()` on columns if /// defined. pub fn clear(self: *Self) void { var ahandle = AddressableHandle{ .index = 0 }; for (self._curr_cycle, 0..) |cycle, i| { if (isLiveCycle(cycle)) { ahandle.index = @as(AddressableIndex, @intCast(i)); ahandle.cycle = cycle; self.releaseAddressableHandleUnchecked(ahandle); } } } /// Adds `values` and returns a live `Handle` if possible, otherwise /// returns one of: /// * `Error.PoolIsFull` /// * `Allocator.Error.OutOfMemory` pub fn add(self: *Self, values: Columns) !Handle { const ahandle = try self.acquireAddressableHandle(); self.initColumnsAt(ahandle.index, values); return ahandle.handle(); } /// Adds `values` and returns a live `Handle` if possible, otherwise /// returns null. pub fn addIfNotFull(self: *Self, values: Columns) ?Handle { const ahandle = self.acquireAddressableHandle() catch { return null; }; self.initColumnsAt(ahandle.index, values); return ahandle.handle(); } /// Adds `values` and returns a live `Handle` if possible, otherwise /// calls `std.debug.assert(false)` and returns `Handle.nil`. pub fn addAssumeNotFull(self: *Self, values: Columns) Handle { const ahandle = self.acquireAddressableHandle() catch { assert(false); return Handle.nil; }; self.initColumnsAt(ahandle.index, values); return ahandle.handle(); } /// Removes (and invalidates) `handle` if live. pub fn remove(self: *Self, handle: Handle) HandleError!void { try self.releaseAddressableHandle(handle.addressable()); } /// Removes (and invalidates) `handle` if live. /// Returns `true` if removed, otherwise `false`. pub fn removeIfLive(self: *Self, handle: Handle) bool { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { self.releaseAddressableHandleUnchecked(ahandle); return true; } return false; } /// Attempts to remove (and invalidates) `handle` assuming it is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn removeAssumeLive(self: *Self, handle: Handle) void { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); self.releaseAddressableHandleUnchecked(ahandle); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Gets a column pointer if `handle` is live. pub fn getColumnPtr(self: Self, handle: Handle, comptime column: Column) HandleError!*ColumnType(column) { const ahandle = handle.addressable(); try self.requireLiveAddressableHandle(ahandle); return self.getColumnPtrUnchecked(ahandle, column); } /// Gets a column value if `handle` is live. pub fn getColumn(self: Self, handle: Handle, comptime column: Column) HandleError!ColumnType(column) { const ahandle = handle.addressable(); try self.requireLiveAddressableHandle(ahandle); return self.getColumnUnchecked(ahandle, column); } /// Gets column values if `handle` is live. pub fn getColumns(self: Self, handle: Handle) HandleError!Columns { const ahandle = handle.addressable(); try self.requireLiveAddressableHandle(ahandle); return self.getColumnsUnchecked(ahandle); } /// Sets a column value if `handle` is live. pub fn setColumn(self: Self, handle: Handle, comptime column: Column, value: ColumnType(column)) HandleError!void { const ahandle = handle.addressable(); try self.requireLiveAddressableHandle(ahandle); self.setColumnUnchecked(ahandle, column, value); } /// Sets column values if `handle` is live. pub fn setColumns(self: Self, handle: Handle, values: Columns) HandleError!void { const ahandle = handle.addressable(); try self.requireLiveAddressableHandle(ahandle); self.setColumnsUnchecked(ahandle, values); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Gets a column pointer if `handle` is live, otherwise `null`. pub fn getColumnPtrIfLive(self: Self, handle: Handle, comptime column: Column) ?*ColumnType(column) { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { return self.getColumnPtrUnchecked(ahandle, column); } return null; } /// Gets a column value if `handle` is live, otherwise `null`. pub fn getColumnIfLive(self: Self, handle: Handle, comptime column: Column) ?ColumnType(column) { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { return self.getColumnUnchecked(ahandle, column); } return null; } /// Gets column values if `handle` is live, otherwise `null`. pub fn getColumnsIfLive(self: Self, handle: Handle) ?Columns { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { return self.getColumnsUnchecked(ahandle); } return null; } /// Sets a column value if `handle` is live. /// Returns `true` if the column value was set, otherwise `false`. pub fn setColumnIfLive(self: Self, handle: Handle, comptime column: Column, value: ColumnType(column)) bool { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { self.setColumnUnchecked(ahandle, column, value); return true; } return false; } /// Sets column values if `handle` is live. /// Returns `true` if the column value was set, otherwise `false`. pub fn setColumnsIfLive(self: Self, handle: Handle, values: Columns) bool { const ahandle = handle.addressable(); if (self.isLiveAddressableHandle(ahandle)) { self.setColumnsUnchecked(ahandle, values); return true; } return false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Attempts to get a column pointer assuming `handle` is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn getColumnPtrAssumeLive(self: Self, handle: Handle, comptime column: Column) *ColumnType(column) { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); return self.getColumnPtrUnchecked(ahandle, column); } /// Attempts to get a column value assuming `handle` is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn getColumnAssumeLive(self: Self, handle: Handle, comptime column: Column) ColumnType(column) { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); return self.getColumnUnchecked(ahandle, column); } /// Attempts to get column values assuming `handle` is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn getColumnsAssumeLive(self: Self, handle: Handle) Columns { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); return self.getColumnsUnchecked(ahandle); } /// Attempts to set a column value assuming `handle` is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn setColumnAssumeLive(self: Self, handle: Handle, comptime column: Column, value: ColumnType(column)) void { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); self.setColumnUnchecked(ahandle, column, value); } /// Attempts to set column values assuming `handle` is live. /// Liveness of `handle` is checked by `std.debug.assert()`. pub fn setColumnsAssumeLive(self: Self, handle: Handle, values: Columns) void { const ahandle = handle.addressable(); assert(self.isLiveAddressableHandle(ahandle)); self.setColumnsUnchecked(ahandle, values); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Gets a column pointer. In most cases, `getColumnPtrAssumeLive` should be used instead. pub fn getColumnPtrUnchecked(self: Self, handle: AddressableHandle, comptime column: Column) *ColumnType(column) { const column_field = meta.fieldInfo(Columns, column); return &@field(self.columns, column_field.name)[handle.index]; } /// Gets a column value. In most cases, `getColumnAssumeLive` should be used instead. pub fn getColumnUnchecked(self: Self, handle: AddressableHandle, comptime column: Column) ColumnType(column) { return self.getColumnPtrUnchecked(handle, column).*; } /// Gets column values. In most cases, `getColumnsAssumeLive` should be used instead. pub fn getColumnsUnchecked(self: Self, handle: AddressableHandle) Columns { var values: Columns = undefined; inline for (column_fields) |column_field| { @field(values, column_field.name) = @field(self.columns, column_field.name)[handle.index]; } return values; } /// Sets a column value. In most cases, `setColumnAssumeLive` should be used instead. pub fn setColumnUnchecked(self: Self, handle: AddressableHandle, comptime column: Column, value: ColumnType(column)) void { const column_field = meta.fieldInfo(Columns, column); self.deinitColumnAt(handle.index, column_field); @field(self.columns, column_field.name)[handle.index] = value; } /// Sets column values. In most cases, `setColumnsAssumeLive` should be used instead. pub fn setColumnsUnchecked(self: Self, handle: AddressableHandle, values: Columns) void { self.deinitColumnsAt(handle.index); self.initColumnsAt(handle.index, values); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const StructField = std.builtin.Type.StructField; fn initColumnsAt(self: Self, index: AddressableIndex, values: Columns) void { inline for (column_fields) |column_field| { @field(self.columns, column_field.name)[index] = @field(values, column_field.name); } } /// Call `value.deinit()` if defined. fn deinitColumnAt(self: Self, index: AddressableIndex, comptime column_field: StructField) void { switch (@typeInfo(column_field.type)) { .Struct, .Enum, .Union, .Opaque => { if (@hasDecl(column_field.type, "deinit")) { @field(self.columns, column_field.name)[index].deinit(); } }, else => {}, } } /// Call `values.deinit()` if defined. fn deinitColumnsAt(self: Self, index: AddressableIndex) void { if (@hasDecl(Columns, "deinit")) { var values: Columns = undefined; inline for (column_fields) |column_field| { @field(values, column_field.name) = @field(self.columns, column_field.name)[index]; } values.deinit(); inline for (column_fields) |column_field| { @field(self.columns, column_field.name)[index] = @field(values, column_field.name); } } else { inline for (column_fields) |column_field| { self.deinitColumnAt(index, column_field); } } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fn updateSlices(self: *Self) void { var slice = self._storage.slice(); self._free_queue.storage = slice.items(.@"Pool._free_queue"); self._curr_cycle = slice.items(.@"Pool._curr_cycle"); inline for (column_fields, 0..) |column_field, i| { const F = column_field.type; const p = slice.ptrs[private_fields.len + i]; const f = @as([*]F, @ptrCast(@alignCast(p))); @field(self.columns, column_field.name) = f[0..slice.len]; } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fn isLiveAddressableHandle( self: Self, handle: AddressableHandle, ) bool { if (isFreeCycle(handle.cycle)) return false; if (handle.index >= self._curr_cycle.len) return false; if (handle.cycle != self._curr_cycle[handle.index]) return false; return true; } fn requireLiveAddressableHandle( self: Self, handle: AddressableHandle, ) HandleError!void { if (isFreeCycle(handle.cycle)) return Error.HandleIsUnacquired; if (handle.index >= self._curr_cycle.len) return Error.HandleIsOutOfBounds; if (handle.cycle != self._curr_cycle[handle.index]) return Error.HandleIsReleased; } fn acquireAddressableHandle(self: *Self) !AddressableHandle { if (self._storage.len == max_capacity) { return Error.PoolIsFull; } var handle = AddressableHandle{}; if (self.didGetNewHandleNoResize(&handle)) { assert(self.isLiveAddressableHandle(handle)); return handle; } if (self.didDequeueFreeIndex(&handle.index)) { handle.cycle = self.incrementAndReturnCycle(handle.index); assert(self.isLiveAddressableHandle(handle)); return handle; } try self.getNewHandleAfterResize(&handle); assert(self.isLiveAddressableHandle(handle)); return handle; } fn releaseAddressableHandle( self: *Self, handle: AddressableHandle, ) !void { try self.requireLiveAddressableHandle(handle); self.releaseAddressableHandleUnchecked(handle); } fn releaseAddressableHandleUnchecked( self: *Self, handle: AddressableHandle, ) void { self.deinitColumnsAt(handle.index); self.incrementCycle(handle.index); self.enqueueFreeIndex(handle.index); } fn tryReleaseAddressableHandle( self: *Self, handle: AddressableHandle, ) bool { if (self.isLiveAddressableHandle(handle)) { self.releaseAddressableHandleUnchecked(handle); return true; } return false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Even cycles (least significant bit is `0`) are "free". fn isFreeCycle(cycle: AddressableCycle) bool { return (cycle & @as(AddressableCycle, 1)) == @as(AddressableCycle, 0); } /// Odd cycles (least significant bit is `1`) are "live". fn isLiveCycle(cycle: AddressableCycle) bool { return (cycle & @as(AddressableCycle, 1)) == @as(AddressableCycle, 1); } fn incrementCycle(self: *Self, index: AddressableIndex) void { const new_cycle = self._curr_cycle[index] +% 1; self._curr_cycle[index] = new_cycle; } fn incrementAndReturnCycle( self: *Self, index: AddressableIndex, ) AddressableCycle { const new_cycle = self._curr_cycle[index] +% 1; self._curr_cycle[index] = new_cycle; return new_cycle; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fn enqueueFreeIndex(self: *Self, index: AddressableIndex) void { self._free_queue.enqueueAssumeNotFull(index); } fn didDequeueFreeIndex(self: *Self, index: *AddressableIndex) bool { return self._free_queue.dequeueIfNotEmpty(index); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fn didGetNewHandleNoResize(self: *Self, handle: *AddressableHandle) bool { if (self._storage.len < max_capacity and self._storage.len < self._storage.capacity) { const new_index = self._storage.addOneAssumeCapacity(); updateSlices(self); self._curr_cycle[new_index] = 1; handle.index = @as(AddressableIndex, @intCast(new_index)); handle.cycle = 1; return true; } return false; } fn getNewHandleAfterResize(self: *Self, handle: *AddressableHandle) !void { const new_index = try self._storage.addOne(self._allocator); updateSlices(self); self._curr_cycle[new_index] = 1; handle.index = @as(AddressableIndex, @intCast(new_index)); handle.cycle = 1; } }; } //------------------------------------------------------------------------------ const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; const DeinitCounter = struct { const Self = @This(); counter: *u32, fn init(_counter: *u32) Self { return Self{ .counter = _counter }; } fn deinit(self: *Self) void { self.counter.* += 1; } }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.init()" { const TestPool = Pool(8, 8, void, struct {}); var pool = TestPool.init(std.testing.allocator); defer pool.deinit(); } test "Pool with no columns" { const TestPool = Pool(8, 8, void, struct {}); try expectEqual(@as(usize, 0), TestPool.column_count); try expectEqual(@as(usize, 0), @sizeOf(TestPool.ColumnSlices)); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); const handle = try pool.add(.{}); defer _ = pool.removeIfLive(handle); try pool.requireLiveHandle(handle); try expect(pool.isLiveHandle(handle)); try expectEqual(@as(u8, 0), handle.addressable().index); try expectEqual(@as(u8, 1), handle.addressable().cycle); try expectEqual(@as(usize, 1), pool.liveHandleCount()); } test "Pool with one column" { const TestPool = Pool(8, 8, void, struct { a: u32 }); try expectEqual(@as(usize, 1), TestPool.column_count); try expectEqual(@sizeOf([]u32), @sizeOf(TestPool.ColumnSlices)); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); const handle = try pool.add(.{ .a = 123 }); defer _ = pool.removeIfLive(handle); try pool.requireLiveHandle(handle); try expect(pool.isLiveHandle(handle)); try expectEqual(@as(usize, 1), pool.liveHandleCount()); try expectEqual(@as(u8, 0), handle.addressable().index); try expectEqual(@as(u8, 1), handle.addressable().cycle); try expectEqual(@as(u32, 123), try pool.getColumn(handle, .a)); try pool.setColumn(handle, .a, 456); try expectEqual(@as(u32, 456), try pool.getColumn(handle, .a)); } test "Pool with two columns" { const TestPool = Pool(8, 8, void, struct { a: u32, b: u64 }); try expectEqual(@as(usize, 2), TestPool.column_count); try expectEqual(@sizeOf([]u32) * 2, @sizeOf(TestPool.ColumnSlices)); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); const handle = try pool.add(.{ .a = 123, .b = 456 }); defer _ = pool.removeIfLive(handle); try pool.requireLiveHandle(handle); try expect(pool.isLiveHandle(handle)); try expectEqual(@as(usize, 1), pool.liveHandleCount()); try expectEqual(@as(u8, 0), handle.addressable().index); try expectEqual(@as(u8, 1), handle.addressable().cycle); try expectEqual(@as(u32, 123), try pool.getColumn(handle, .a)); try pool.setColumn(handle, .a, 456); try expectEqual(@as(u32, 456), try pool.getColumn(handle, .a)); try expectEqual(@as(u64, 456), try pool.getColumn(handle, .b)); try pool.setColumn(handle, .b, 123); try expectEqual(@as(u64, 123), try pool.getColumn(handle, .b)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.liveHandleCount()" { const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); try expectEqual(@as(usize, 1), pool.liveHandleCount()); const handle1 = try pool.add(.{}); try expectEqual(@as(usize, 2), pool.liveHandleCount()); try pool.remove(handle0); try expectEqual(@as(usize, 1), pool.liveHandleCount()); try pool.remove(handle1); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle2 = try pool.add(.{}); try expectEqual(@as(usize, 1), pool.liveHandleCount()); try pool.remove(handle2); try expectEqual(@as(usize, 0), pool.liveHandleCount()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.isLiveHandle()" { const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const unacquiredHandle = TestPool.Handle.init(0, 0); try expect(!pool.isLiveHandle(unacquiredHandle)); const outOfBoundsHandle = TestPool.Handle.init(1, 1); try expect(!pool.isLiveHandle(outOfBoundsHandle)); const handle = try pool.add(.{}); try expect(pool.isLiveHandle(handle)); try pool.remove(handle); try expect(!pool.isLiveHandle(handle)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.requireLiveHandle()" { const TestPool = Pool(8, 8, void, struct {}); try expectEqual(@as(usize, 0), TestPool.column_count); try expectEqual(@as(usize, 0), @sizeOf(TestPool.ColumnSlices)); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const unacquiredHandle = TestPool.Handle.init(0, 0); try expectError(TestPool.Error.HandleIsUnacquired, pool.requireLiveHandle(unacquiredHandle)); const outOfBoundsHandle = TestPool.Handle.init(1, 1); try expectError(TestPool.Error.HandleIsOutOfBounds, pool.requireLiveHandle(outOfBoundsHandle)); const handle = try pool.add(.{}); try pool.requireLiveHandle(handle); try pool.remove(handle); try expectError(TestPool.Error.HandleIsReleased, pool.requireLiveHandle(handle)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.liveIndices()" { const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); try expectEqual(@as(usize, 3), pool.liveHandleCount()); var live_indices = pool.liveIndices(); try expectEqual(handle0.addressable().index, live_indices.next().?); try expectEqual(handle1.addressable().index, live_indices.next().?); try expectEqual(handle2.addressable().index, live_indices.next().?); try expect(null == live_indices.next()); } test "Pool.liveIndices() when full" { // Test that iterator's internal index doesn't overflow when pool is full. // (8,8 is the smallest size we can easily test because AddressableIndex is // at least a u8) const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); var i: usize = 0; while (i < 256) { _ = try pool.add(.{}); i += 1; } try expectEqual(@as(usize, 256), pool.liveHandleCount()); // Make sure it does correctly iterate all the way. var j: usize = 0; var live_indices = pool.liveIndices(); while (live_indices.next()) |_| { j += 1; } try expectEqual(@as(usize, 256), j); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.liveHandles()" { const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); try expectEqual(@as(usize, 3), pool.liveHandleCount()); var live_handles = pool.liveHandles(); try expectEqual(handle0.id, live_handles.next().?.id); try expectEqual(handle1.id, live_handles.next().?.id); try expectEqual(handle2.id, live_handles.next().?.id); try expect(null == live_handles.next()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.clear()" { const TestPool = Pool(8, 8, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); try expectEqual(@as(usize, 3), pool.liveHandleCount()); try expect(pool.isLiveHandle(handle0)); try expect(pool.isLiveHandle(handle1)); try expect(pool.isLiveHandle(handle2)); pool.clear(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); try expect(!pool.isLiveHandle(handle0)); try expect(!pool.isLiveHandle(handle1)); try expect(!pool.isLiveHandle(handle2)); } test "Pool.clear() calls Columns.deinit()" { const TestPool = Pool(2, 6, void, DeinitCounter); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; _ = try pool.add(DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 0), deinit_count); pool.clear(); try expectEqual(@as(u32, 1), deinit_count); } test "Pool.clear() calls ColumnType.deinit()" { const TestPool = Pool(2, 6, void, struct { a: DeinitCounter, b: DeinitCounter }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; _ = try pool.add(.{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 0), deinit_count); pool.clear(); try expectEqual(@as(u32, 2), deinit_count); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.add()" { const TestPool = Pool(2, 6, void, struct {}); try expectEqual(@sizeOf(u8), @sizeOf(TestPool.Handle)); try expectEqual(@as(usize, 4), TestPool.max_capacity); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); const handle3 = try pool.add(.{}); try expectEqual(@as(usize, 4), pool.liveHandleCount()); try expect(pool.isLiveHandle(handle0)); try expect(pool.isLiveHandle(handle1)); try expect(pool.isLiveHandle(handle2)); try expect(pool.isLiveHandle(handle3)); try expectError(TestPool.Error.PoolIsFull, pool.add(.{})); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.remove()" { const TestPool = Pool(2, 6, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); const handle3 = try pool.add(.{}); try pool.remove(handle0); try pool.remove(handle1); try pool.remove(handle2); try pool.remove(handle3); try expectError(TestPool.Error.HandleIsReleased, pool.remove(handle0)); try expectError(TestPool.Error.HandleIsReleased, pool.remove(handle1)); try expectError(TestPool.Error.HandleIsReleased, pool.remove(handle2)); try expectError(TestPool.Error.HandleIsReleased, pool.remove(handle3)); } test "Pool.remove() calls Columns.deinit()" { const TestPool = Pool(2, 6, void, DeinitCounter); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 0), deinit_count); try pool.remove(handle); try expectEqual(@as(u32, 1), deinit_count); } test "Pool.remove() calls ColumnType.deinit()" { const TestPool = Pool(2, 6, void, struct { a: DeinitCounter, b: DeinitCounter }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(.{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 0), deinit_count); try pool.remove(handle); try expectEqual(@as(u32, 2), deinit_count); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.removeIfLive()" { const TestPool = Pool(2, 6, void, struct {}); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{}); const handle1 = try pool.add(.{}); const handle2 = try pool.add(.{}); const handle3 = try pool.add(.{}); try expect(pool.isLiveHandle(handle0)); try expect(pool.isLiveHandle(handle1)); try expect(pool.isLiveHandle(handle2)); try expect(pool.isLiveHandle(handle3)); try expect(pool.removeIfLive(handle0)); try expect(pool.removeIfLive(handle1)); try expect(pool.removeIfLive(handle2)); try expect(pool.removeIfLive(handle3)); try expect(!pool.isLiveHandle(handle0)); try expect(!pool.isLiveHandle(handle1)); try expect(!pool.isLiveHandle(handle2)); try expect(!pool.isLiveHandle(handle3)); try expect(!pool.removeIfLive(handle0)); try expect(!pool.removeIfLive(handle1)); try expect(!pool.removeIfLive(handle2)); try expect(!pool.removeIfLive(handle3)); } test "Pool.removeIfLive() calls Columns.deinit()" { const TestPool = Pool(2, 6, void, DeinitCounter); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 0), deinit_count); try expect(pool.removeIfLive(handle)); try expectEqual(@as(u32, 1), deinit_count); } test "Pool.removeIfLive() calls ColumnType.deinit()" { const TestPool = Pool(2, 6, void, struct { a: DeinitCounter, b: DeinitCounter }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(.{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 0), deinit_count); try expect(pool.removeIfLive(handle)); try expectEqual(@as(u32, 2), deinit_count); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.getColumnPtr*()" { const TestPool = Pool(2, 6, void, struct { a: u32 }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{ .a = 0 }); const handle1 = try pool.add(.{ .a = 1 }); const handle2 = try pool.add(.{ .a = 2 }); const handle3 = try pool.add(.{ .a = 3 }); const a0ptr: *u32 = try pool.getColumnPtr(handle0, .a); const a1ptr: *u32 = try pool.getColumnPtr(handle1, .a); const a2ptr: *u32 = try pool.getColumnPtr(handle2, .a); const a3ptr: *u32 = try pool.getColumnPtr(handle3, .a); try expectEqual(@as(u32, 0), a0ptr.*); try expectEqual(@as(u32, 1), a1ptr.*); try expectEqual(@as(u32, 2), a2ptr.*); try expectEqual(@as(u32, 3), a3ptr.*); try expectEqual(a0ptr, pool.getColumnPtrIfLive(handle0, .a).?); try expectEqual(a1ptr, pool.getColumnPtrIfLive(handle1, .a).?); try expectEqual(a2ptr, pool.getColumnPtrIfLive(handle2, .a).?); try expectEqual(a3ptr, pool.getColumnPtrIfLive(handle3, .a).?); try expectEqual(a0ptr, pool.getColumnPtrAssumeLive(handle0, .a)); try expectEqual(a1ptr, pool.getColumnPtrAssumeLive(handle1, .a)); try expectEqual(a2ptr, pool.getColumnPtrAssumeLive(handle2, .a)); try expectEqual(a3ptr, pool.getColumnPtrAssumeLive(handle3, .a)); try pool.remove(handle0); try pool.remove(handle1); try pool.remove(handle2); try pool.remove(handle3); try expectError(TestPool.Error.HandleIsReleased, pool.getColumnPtr(handle0, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumnPtr(handle1, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumnPtr(handle2, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumnPtr(handle3, .a)); try expect(null == pool.getColumnPtrIfLive(handle0, .a)); try expect(null == pool.getColumnPtrIfLive(handle1, .a)); try expect(null == pool.getColumnPtrIfLive(handle2, .a)); try expect(null == pool.getColumnPtrIfLive(handle3, .a)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.getColumn*()" { const TestPool = Pool(2, 6, void, struct { a: u32 }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{ .a = 0 }); const handle1 = try pool.add(.{ .a = 1 }); const handle2 = try pool.add(.{ .a = 2 }); const handle3 = try pool.add(.{ .a = 3 }); try expectEqual(@as(u32, 0), try pool.getColumn(handle0, .a)); try expectEqual(@as(u32, 1), try pool.getColumn(handle1, .a)); try expectEqual(@as(u32, 2), try pool.getColumn(handle2, .a)); try expectEqual(@as(u32, 3), try pool.getColumn(handle3, .a)); try expectEqual(@as(u32, 0), pool.getColumnIfLive(handle0, .a).?); try expectEqual(@as(u32, 1), pool.getColumnIfLive(handle1, .a).?); try expectEqual(@as(u32, 2), pool.getColumnIfLive(handle2, .a).?); try expectEqual(@as(u32, 3), pool.getColumnIfLive(handle3, .a).?); try expectEqual(@as(u32, 0), pool.getColumnAssumeLive(handle0, .a)); try expectEqual(@as(u32, 1), pool.getColumnAssumeLive(handle1, .a)); try expectEqual(@as(u32, 2), pool.getColumnAssumeLive(handle2, .a)); try expectEqual(@as(u32, 3), pool.getColumnAssumeLive(handle3, .a)); try pool.remove(handle0); try pool.remove(handle1); try pool.remove(handle2); try pool.remove(handle3); try expectError(TestPool.Error.HandleIsReleased, pool.getColumn(handle0, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumn(handle1, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumn(handle2, .a)); try expectError(TestPool.Error.HandleIsReleased, pool.getColumn(handle3, .a)); try expect(null == pool.getColumnIfLive(handle0, .a)); try expect(null == pool.getColumnIfLive(handle1, .a)); try expect(null == pool.getColumnIfLive(handle2, .a)); try expect(null == pool.getColumnIfLive(handle3, .a)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.setColumn*()" { const TestPool = Pool(2, 6, void, struct { a: u32 }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{ .a = 0 }); const handle1 = try pool.add(.{ .a = 1 }); const handle2 = try pool.add(.{ .a = 2 }); const handle3 = try pool.add(.{ .a = 3 }); try expectEqual(@as(u32, 0), try pool.getColumn(handle0, .a)); try expectEqual(@as(u32, 1), try pool.getColumn(handle1, .a)); try expectEqual(@as(u32, 2), try pool.getColumn(handle2, .a)); try expectEqual(@as(u32, 3), try pool.getColumn(handle3, .a)); try expectEqual(@as(u32, 0), pool.getColumnIfLive(handle0, .a).?); try expectEqual(@as(u32, 1), pool.getColumnIfLive(handle1, .a).?); try expectEqual(@as(u32, 2), pool.getColumnIfLive(handle2, .a).?); try expectEqual(@as(u32, 3), pool.getColumnIfLive(handle3, .a).?); try expectEqual(@as(u32, 0), pool.getColumnAssumeLive(handle0, .a)); try expectEqual(@as(u32, 1), pool.getColumnAssumeLive(handle1, .a)); try expectEqual(@as(u32, 2), pool.getColumnAssumeLive(handle2, .a)); try expectEqual(@as(u32, 3), pool.getColumnAssumeLive(handle3, .a)); try pool.setColumn(handle0, .a, 10); try pool.setColumn(handle1, .a, 11); try pool.setColumn(handle2, .a, 12); try pool.setColumn(handle3, .a, 13); try expect(pool.setColumnIfLive(handle0, .a, 20)); try expect(pool.setColumnIfLive(handle1, .a, 21)); try expect(pool.setColumnIfLive(handle2, .a, 22)); try expect(pool.setColumnIfLive(handle3, .a, 23)); pool.setColumnAssumeLive(handle0, .a, 30); pool.setColumnAssumeLive(handle1, .a, 31); pool.setColumnAssumeLive(handle2, .a, 32); pool.setColumnAssumeLive(handle3, .a, 33); try expectEqual(@as(u32, 30), try pool.getColumn(handle0, .a)); try expectEqual(@as(u32, 31), try pool.getColumn(handle1, .a)); try expectEqual(@as(u32, 32), try pool.getColumn(handle2, .a)); try expectEqual(@as(u32, 33), try pool.getColumn(handle3, .a)); try expectEqual(@as(u32, 30), pool.getColumnIfLive(handle0, .a).?); try expectEqual(@as(u32, 31), pool.getColumnIfLive(handle1, .a).?); try expectEqual(@as(u32, 32), pool.getColumnIfLive(handle2, .a).?); try expectEqual(@as(u32, 33), pool.getColumnIfLive(handle3, .a).?); try expectEqual(@as(u32, 30), pool.getColumnAssumeLive(handle0, .a)); try expectEqual(@as(u32, 31), pool.getColumnAssumeLive(handle1, .a)); try expectEqual(@as(u32, 32), pool.getColumnAssumeLive(handle2, .a)); try expectEqual(@as(u32, 33), pool.getColumnAssumeLive(handle3, .a)); try pool.remove(handle0); try pool.remove(handle1); try pool.remove(handle2); try pool.remove(handle3); try expectError(TestPool.Error.HandleIsReleased, pool.setColumn(handle0, .a, 40)); try expectError(TestPool.Error.HandleIsReleased, pool.setColumn(handle1, .a, 41)); try expectError(TestPool.Error.HandleIsReleased, pool.setColumn(handle2, .a, 42)); try expectError(TestPool.Error.HandleIsReleased, pool.setColumn(handle3, .a, 43)); try expect(!pool.setColumnIfLive(handle0, .a, 50)); try expect(!pool.setColumnIfLive(handle1, .a, 51)); try expect(!pool.setColumnIfLive(handle2, .a, 52)); try expect(!pool.setColumnIfLive(handle3, .a, 53)); // setColumnAssumeLive() would fail an assert() } test "Pool.setColumn*() calls ColumnType.deinit()" { const TestPool = Pool(2, 6, void, struct { a: DeinitCounter, b: DeinitCounter }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(.{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 0), deinit_count); try pool.setColumn(handle, .a, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 1), deinit_count); try pool.setColumn(handle, .b, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 2), deinit_count); try expect(pool.setColumnIfLive(handle, .a, DeinitCounter.init(&deinit_count))); try expectEqual(@as(u32, 3), deinit_count); try expect(pool.setColumnIfLive(handle, .b, DeinitCounter.init(&deinit_count))); try expectEqual(@as(u32, 4), deinit_count); pool.setColumnAssumeLive(handle, .a, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 5), deinit_count); pool.setColumnAssumeLive(handle, .b, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 6), deinit_count); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "Pool.setColumns*()" { const TestPool = Pool(2, 6, void, struct { a: u32 }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); try expectEqual(@as(usize, 0), pool.liveHandleCount()); const handle0 = try pool.add(.{ .a = 0 }); const handle1 = try pool.add(.{ .a = 1 }); const handle2 = try pool.add(.{ .a = 2 }); const handle3 = try pool.add(.{ .a = 3 }); try expectEqual(@as(u32, 0), try pool.getColumn(handle0, .a)); try expectEqual(@as(u32, 1), try pool.getColumn(handle1, .a)); try expectEqual(@as(u32, 2), try pool.getColumn(handle2, .a)); try expectEqual(@as(u32, 3), try pool.getColumn(handle3, .a)); try expectEqual(@as(u32, 0), pool.getColumnIfLive(handle0, .a).?); try expectEqual(@as(u32, 1), pool.getColumnIfLive(handle1, .a).?); try expectEqual(@as(u32, 2), pool.getColumnIfLive(handle2, .a).?); try expectEqual(@as(u32, 3), pool.getColumnIfLive(handle3, .a).?); try expectEqual(@as(u32, 0), pool.getColumnAssumeLive(handle0, .a)); try expectEqual(@as(u32, 1), pool.getColumnAssumeLive(handle1, .a)); try expectEqual(@as(u32, 2), pool.getColumnAssumeLive(handle2, .a)); try expectEqual(@as(u32, 3), pool.getColumnAssumeLive(handle3, .a)); try pool.setColumns(handle0, .{ .a = 10 }); try pool.setColumns(handle1, .{ .a = 11 }); try pool.setColumns(handle2, .{ .a = 12 }); try pool.setColumns(handle3, .{ .a = 13 }); try expect(pool.setColumnsIfLive(handle0, .{ .a = 20 })); try expect(pool.setColumnsIfLive(handle1, .{ .a = 21 })); try expect(pool.setColumnsIfLive(handle2, .{ .a = 22 })); try expect(pool.setColumnsIfLive(handle3, .{ .a = 23 })); pool.setColumnsAssumeLive(handle0, .{ .a = 30 }); pool.setColumnsAssumeLive(handle1, .{ .a = 31 }); pool.setColumnsAssumeLive(handle2, .{ .a = 32 }); pool.setColumnsAssumeLive(handle3, .{ .a = 33 }); try expectEqual(@as(u32, 30), try pool.getColumn(handle0, .a)); try expectEqual(@as(u32, 31), try pool.getColumn(handle1, .a)); try expectEqual(@as(u32, 32), try pool.getColumn(handle2, .a)); try expectEqual(@as(u32, 33), try pool.getColumn(handle3, .a)); try expectEqual(@as(u32, 30), pool.getColumnIfLive(handle0, .a).?); try expectEqual(@as(u32, 31), pool.getColumnIfLive(handle1, .a).?); try expectEqual(@as(u32, 32), pool.getColumnIfLive(handle2, .a).?); try expectEqual(@as(u32, 33), pool.getColumnIfLive(handle3, .a).?); try expectEqual(@as(u32, 30), pool.getColumnAssumeLive(handle0, .a)); try expectEqual(@as(u32, 31), pool.getColumnAssumeLive(handle1, .a)); try expectEqual(@as(u32, 32), pool.getColumnAssumeLive(handle2, .a)); try expectEqual(@as(u32, 33), pool.getColumnAssumeLive(handle3, .a)); try pool.remove(handle0); try pool.remove(handle1); try pool.remove(handle2); try pool.remove(handle3); try expectError(TestPool.Error.HandleIsReleased, pool.setColumns(handle0, .{ .a = 40 })); try expectError(TestPool.Error.HandleIsReleased, pool.setColumns(handle1, .{ .a = 41 })); try expectError(TestPool.Error.HandleIsReleased, pool.setColumns(handle2, .{ .a = 42 })); try expectError(TestPool.Error.HandleIsReleased, pool.setColumns(handle3, .{ .a = 43 })); try expect(!pool.setColumnsIfLive(handle0, .{ .a = 50 })); try expect(!pool.setColumnsIfLive(handle1, .{ .a = 51 })); try expect(!pool.setColumnsIfLive(handle2, .{ .a = 52 })); try expect(!pool.setColumnsIfLive(handle3, .{ .a = 53 })); // setColumnsAssumeLive() would fail an assert() } test "Pool.setColumns() calls Columns.deinit()" { const TestPool = Pool(2, 6, void, DeinitCounter); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 0), deinit_count); try pool.setColumns(handle, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 1), deinit_count); try expect(pool.setColumnsIfLive(handle, DeinitCounter.init(&deinit_count))); try expectEqual(@as(u32, 2), deinit_count); pool.setColumnsAssumeLive(handle, DeinitCounter.init(&deinit_count)); try expectEqual(@as(u32, 3), deinit_count); } test "Pool.setColumns() calls ColumnType.deinit()" { const TestPool = Pool(2, 6, void, struct { a: DeinitCounter, b: DeinitCounter }); var pool = try TestPool.initMaxCapacity(std.testing.allocator); defer pool.deinit(); var deinit_count: u32 = 0; const handle = try pool.add(.{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 0), deinit_count); try pool.setColumns(handle, .{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 2), deinit_count); try expect(pool.setColumnsIfLive(handle, .{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), })); try expectEqual(@as(u32, 4), deinit_count); pool.setColumnsAssumeLive(handle, .{ .a = DeinitCounter.init(&deinit_count), .b = DeinitCounter.init(&deinit_count), }); try expectEqual(@as(u32, 6), deinit_count); } //------------------------------------------------------------------------------
0
repos/simulations/libs/zpool
repos/simulations/libs/zpool/src/utils.zig
const std = @import("std"); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn asTypeId(comptime typeInfo: std.builtin.Type) std.builtin.TypeId { return @as(std.builtin.TypeId, typeInfo); } pub fn typeIdOf(comptime T: type) std.builtin.TypeId { return asTypeId(@typeInfo(T)); } pub fn isStruct(comptime T: type) bool { return typeIdOf(T) == std.builtin.TypeId.Struct; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// UInt(bits) returns an unsigned integer type of the requested bit width. pub fn UInt(comptime bits: u8) type { const unsigned = std.builtin.Signedness.unsigned; return @Type(.{ .Int = .{ .signedness = unsigned, .bits = bits } }); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Returns an unsigned integer type with ***at least*** `min_bits`, /// that is also large enough to be addressable by a normal pointer. /// The returned type will always be one of the following: /// * `u8` /// * `u16` /// * `u32` /// * `u64` /// * `u128` /// * `u256` pub fn AddressableUInt(comptime min_bits: u8) type { return switch (min_bits) { 0...8 => u8, 9...16 => u16, 17...32 => u32, 33...64 => u64, 65...128 => u128, 129...255 => u256, }; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Given: `Struct = struct { foo: u32, bar: u64 }` /// Returns: `StructOfSlices = struct { foo: []u32, bar: []u64 }` pub fn StructOfSlices(comptime Struct: type) type { const StructField = std.builtin.Type.StructField; // same number of fields in the new struct const struct_fields = @typeInfo(Struct).Struct.fields; comptime var struct_of_slices_fields: []const StructField = &.{}; inline for (struct_fields) |struct_field| { // u32 -> []u32 const element_type = struct_field.type; const slice_type_info = std.builtin.Type{ .Pointer = .{ .child = element_type, .alignment = @alignOf(element_type), .size = .Slice, .is_const = false, .is_volatile = false, .address_space = .generic, .is_allowzero = false, .sentinel = null, }, }; const FieldType = @Type(slice_type_info); // Struct.foo: u32 -> StructOfSlices.foo : []u32 const slice_field = std.builtin.Type.StructField{ .name = struct_field.name, .type = FieldType, .default_value = null, .is_comptime = false, .alignment = @alignOf(FieldType), }; // Struct.foo: u32 -> StructOfSlices.foo : []u32 struct_of_slices_fields = struct_of_slices_fields ++ [1]StructField{slice_field}; } return @Type(.{ .Struct = .{ .layout = .auto, .fields = struct_of_slices_fields, .decls = &.{}, .is_tuple = false, } }); } test "StructOfSlices" { const expectEqual = std.testing.expectEqual; const Struct = struct { a: u16, b: u16, c: u16 }; try expectEqual(@sizeOf(u16) * 3, @sizeOf(Struct)); const SOS = StructOfSlices(Struct); try expectEqual(@sizeOf([]u16) * 3, @sizeOf(SOS)); }
0
repos/simulations/libs/zpool
repos/simulations/libs/zpool/src/main.zig
pub const Handle = @import("handle.zig").Handle; pub const Pool = @import("pool.zig").Pool; // ensure transitive closure of test coverage comptime { _ = Handle; _ = Pool; }
0
repos/simulations/libs/zpool
repos/simulations/libs/zpool/src/handle.zig
const std = @import("std"); /// Returns a struct consisting of an array `index` and a semi-unique `cycle`, /// which exists to distinguish handles with the same array `index`. /// /// The `cycle` value is only unique within the incremental period of an /// unsigned integer with `cycle_bits`, so a larger number of `cycle_bits` /// provides a larger scope of identifiable conflicts between handles for the /// same `index`. /// /// `Handle` is generic because while the `{ index, cycle }` pattern is widely /// applicable, a good distribution of bits between `index` and `cycle` and the /// overall size of a handle are highly dependent on the lifecycle of the /// resource being identified by a handle and the systems consuming handles. /// /// Reasonable values for `index_bits` depend on the maximum number of /// uniquely identifiable resources your API will to identify with handles. /// Generally this is directly tied to the length of the array(s) in which /// you will store data to be referenced by a handle's `index`. /// /// Reasonable values for `cycle_bits` depend on the frequency with which your /// API expects to be issuing handles, and how many cycles of your application /// are likely to elapse before an expired handle will likely no longer be /// retained by the API caller's data structures. /// /// For example, a `Handle(16, 16)` may be sufficient for a GPU resource like /// a texture or buffer, where 64k instances of that resource is a reasonable /// upper bound. /// /// A `Handle(22, 10)` may be more appropriate to identify an entity in a /// system where we can safely assume that 4 million entities, is a lot, and /// that API callers can discover and discard expired entity handles within /// 1024 frames of an entity being destroyed and its handle's `index` being /// reissued for use by a distinct entity. /// /// `TResource` identifies type of resource referenced by a handle, and /// provides a type-safe distinction between two otherwise equivalently /// configured `Handle` types, such as: /// * `const BufferHandle = Handle(16, 16, Buffer);` /// * `const TextureHandle = Handle(16, 16, Texture);` /// /// The total size of a handle will always be the size of an addressable /// unsigned integer of type `u8`, `u16`, `u32`, `u64`, `u128`, or `u256`. pub fn Handle( comptime index_bits: u8, comptime cycle_bits: u8, comptime TResource: type, ) type { if (index_bits == 0) @compileError("index_bits must be greater than 0"); if (cycle_bits == 0) @compileError("cycle_bits must be greater than 0"); const id_bits: u16 = @as(u16, index_bits) + @as(u16, cycle_bits); const Id = switch (id_bits) { 8 => u8, 16 => u16, 32 => u32, 64 => u64, 128 => u128, 256 => u256, else => @compileError("index_bits + cycle_bits must sum to exactly " ++ "8, 16, 32, 64, 128, or 256 bits"), }; const field_bits = @max(index_bits, cycle_bits); const utils = @import("utils.zig"); const UInt = utils.UInt; const AddressableUInt = utils.AddressableUInt; return extern struct { const Self = @This(); const HandleType = Self; const IndexType = UInt(index_bits); const CycleType = UInt(cycle_bits); const HandleUnion = extern union { id: Id, bits: packed struct { cycle: CycleType, // least significant bits index: IndexType, // most significant bits }, }; pub const Resource = TResource; pub const AddressableCycle = AddressableUInt(field_bits); pub const AddressableIndex = AddressableUInt(field_bits); pub const max_cycle = ~@as(CycleType, 0); pub const max_index = ~@as(IndexType, 0); pub const max_count = @as(Id, max_index - 1) + 2; id: Id = 0, pub const nil = Self{ .id = 0 }; pub fn init(i: IndexType, c: CycleType) Self { const u = HandleUnion{ .bits = .{ .cycle = c, .index = i, } }; return .{ .id = u.id }; } pub fn cycle(self: Self) CycleType { const u = HandleUnion{ .id = self.id }; return u.bits.cycle; } pub fn index(self: Self) IndexType { const u = HandleUnion{ .id = self.id }; return u.bits.index; } /// Unpacks the `index` and `cycle` bit fields that comprise /// `Handle.id` into an `AddressableHandle`, which stores /// the `index` and `cycle` values in pointer-addressable fields. pub fn addressable(self: Self) AddressableHandle { const u = HandleUnion{ .id = self.id }; return .{ .cycle = u.bits.cycle, .index = u.bits.index, }; } /// When you want to directly access the `index` and `cycle` of a /// handle, first convert it to an `AddressableHandle` by calling /// `Handle.addressable()`. /// An `AddressableHandle` can be converted back into a "compact" /// `Handle` by calling `AddressableHandle.compact()`. pub const AddressableHandle = struct { cycle: AddressableCycle = 0, index: AddressableIndex = 0, /// Returns the corresponding `Handle` pub fn handle(self: AddressableHandle) HandleType { const u = HandleUnion{ .bits = .{ .cycle = @as(CycleType, @intCast(self.cycle)), .index = @as(IndexType, @intCast(self.index)), } }; return .{ .id = u.id }; } }; pub fn format( self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void { _ = fmt; _ = options; const n = @typeName(Resource); const a = self.addressable(); return writer.print("{s}[{}#{}]", .{ n, a.index, a.cycle }); } }; } //////////////////////////////////////////////////////////////////////////////// test "Handle sizes and alignments" { const expectEqual = std.testing.expectEqual; { const H = Handle(4, 4, void); try expectEqual(@sizeOf(u8), @sizeOf(H)); try expectEqual(@alignOf(u8), @alignOf(H)); try expectEqual(4, @bitSizeOf(H.IndexType)); try expectEqual(4, @bitSizeOf(H.CycleType)); try expectEqual(8, @bitSizeOf(H.AddressableIndex)); try expectEqual(8, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u16), @sizeOf(A)); try expectEqual(@alignOf(u8), @alignOf(A)); } { const H = Handle(6, 2, void); try expectEqual(@sizeOf(u8), @sizeOf(H)); try expectEqual(@alignOf(u8), @alignOf(H)); try expectEqual(6, @bitSizeOf(H.IndexType)); try expectEqual(2, @bitSizeOf(H.CycleType)); try expectEqual(8, @bitSizeOf(H.AddressableIndex)); try expectEqual(8, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u16), @sizeOf(A)); try expectEqual(@alignOf(u8), @alignOf(A)); } { const H = Handle(8, 8, void); try expectEqual(@sizeOf(u16), @sizeOf(H)); try expectEqual(@alignOf(u16), @alignOf(H)); try expectEqual(8, @bitSizeOf(H.IndexType)); try expectEqual(8, @bitSizeOf(H.CycleType)); try expectEqual(8, @bitSizeOf(H.AddressableIndex)); try expectEqual(8, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u16), @sizeOf(A)); try expectEqual(@alignOf(u8), @alignOf(A)); } { const H = Handle(12, 4, void); try expectEqual(@sizeOf(u16), @sizeOf(H)); try expectEqual(@alignOf(u16), @alignOf(H)); try expectEqual(12, @bitSizeOf(H.IndexType)); try expectEqual(4, @bitSizeOf(H.CycleType)); try expectEqual(16, @bitSizeOf(H.AddressableIndex)); try expectEqual(16, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u32), @sizeOf(A)); try expectEqual(@alignOf(u16), @alignOf(A)); } { const H = Handle(16, 16, void); try expectEqual(@sizeOf(u32), @sizeOf(H)); try expectEqual(@alignOf(u32), @alignOf(H)); try expectEqual(16, @bitSizeOf(H.IndexType)); try expectEqual(16, @bitSizeOf(H.CycleType)); try expectEqual(16, @bitSizeOf(H.AddressableIndex)); try expectEqual(16, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u32), @sizeOf(A)); try expectEqual(@alignOf(u16), @alignOf(A)); } { const H = Handle(22, 10, void); try expectEqual(@sizeOf(u32), @sizeOf(H)); try expectEqual(@alignOf(u32), @alignOf(H)); try expectEqual(22, @bitSizeOf(H.IndexType)); try expectEqual(10, @bitSizeOf(H.CycleType)); try expectEqual(32, @bitSizeOf(H.AddressableIndex)); try expectEqual(32, @bitSizeOf(H.AddressableCycle)); const A = H.AddressableHandle; try expectEqual(@sizeOf(u64), @sizeOf(A)); try expectEqual(@alignOf(u32), @alignOf(A)); } } //////////////////////////////////////////////////////////////////////////////// test "Handle sort order" { const expect = std.testing.expect; const handle = Handle(4, 4, void).init; const a = handle(0, 3); const b = handle(1, 1); // id order is consistent with index order, even when cycle order is not try expect(a.id < b.id); try expect(a.index() < b.index()); try expect(a.cycle() > b.cycle()); } //////////////////////////////////////////////////////////////////////////////// test "Handle.format()" { const bufPrint = std.fmt.bufPrint; const expectEqualStrings = std.testing.expectEqualStrings; const Foo = struct {}; const H = Handle(12, 4, Foo); const h = H.init(0, 1); var buffer = [_]u8{0} ** 128; const s = try bufPrint(buffer[0..], "{}", .{h}); try expectEqualStrings("handle.test.Handle.format().Foo[0#1]", s); }
0
repos/simulations/libs/zpool
repos/simulations/libs/zpool/src/embedded_ring_queue.zig
const std = @import("std"); pub fn EmbeddedRingQueue(comptime TElement: type) type { const assert = std.debug.assert; return struct { const Self = @This(); pub const Error = error{ Empty, Full, }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub const Element = TElement; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - head: usize = 0, tail: usize = 0, storage: []Element = &.{}, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn init(buffer: []Element) Self { return .{ .storage = buffer }; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn capacity(self: Self) usize { return self.storage.len; } pub fn len(self: Self) usize { return self.tail -% self.head; } pub fn empty(self: Self) bool { return self.len() == 0; } pub fn full(self: Self) bool { return self.len() == self.capacity(); } pub fn clear(self: *Self) void { self.head = 0; self.tail = 0; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn enqueue(self: *Self, value: Element) Error!void { if (self.enqueueIfNotFull(value)) { return; } return Error.Full; } pub fn dequeue(self: *Self) Error!Element { var value: Element = undefined; if (self.dequeueIfNotEmpty(&value)) { return value; } return Error.Empty; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn enqueueIfNotFull(self: *Self, value: Element) bool { if (self.full()) { return false; } self.enqueueUnchecked(value); return true; } pub fn dequeueIfNotEmpty(self: *Self, value: *Element) bool { if (self.empty()) { return false; } self.dequeueUnchecked(value); return true; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn enqueueAssumeNotFull(self: *Self, value: Element) void { assert(!self.full()); self.enqueueUnchecked(value); } pub fn dequeueAssumeNotEmpty(self: *Self) Element { assert(!self.empty()); var value: Element = undefined; self.dequeueUnchecked(&value); return value; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pub fn enqueueUnchecked(self: *Self, value: Element) void { const tail_index = self.tail % self.storage.len; self.storage[tail_index] = value; self.tail +%= 1; } pub fn dequeueUnchecked(self: *Self, value: *Element) void { const head_index = self.head % self.storage.len; value.* = self.storage[head_index]; self.head +%= 1; } }; } //------------------------------------------------------------------------------ const expectEqual = std.testing.expectEqual; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test "EmbeddedRingQueue basics" { var buffer: [16]usize = undefined; var queue = EmbeddedRingQueue(usize).init(buffer[0..]); try expectEqual(buffer.len, queue.capacity()); try expectEqual(@as(usize, 0), queue.len()); try expectEqual(true, queue.empty()); try expectEqual(false, queue.full()); for (buffer, 0..) |_, i| { try expectEqual(i, queue.len()); try queue.enqueue(i); try expectEqual(i, buffer[i]); } try expectEqual(buffer.len, queue.capacity()); try expectEqual(buffer.len, queue.len()); try expectEqual(false, queue.empty()); try expectEqual(true, queue.full()); for (buffer, 0..) |_, i| { try expectEqual(buffer.len - i, queue.len()); const j = try queue.dequeue(); try expectEqual(i, j); } try expectEqual(buffer.len, queue.capacity()); try expectEqual(@as(usize, 0), queue.len()); try expectEqual(true, queue.empty()); try expectEqual(false, queue.full()); for (buffer, 0..) |_, i| { try expectEqual(i, queue.len()); try queue.enqueue(i); try expectEqual(i, buffer[i]); } try expectEqual(buffer.len, queue.capacity()); try expectEqual(buffer.len, queue.len()); try expectEqual(false, queue.empty()); try expectEqual(true, queue.full()); queue.clear(); try expectEqual(buffer.len, queue.capacity()); try expectEqual(@as(usize, 0), queue.len()); try expectEqual(true, queue.empty()); try expectEqual(false, queue.full()); } //------------------------------------------------------------------------------
0
repos/simulations/libs
repos/simulations/libs/zgpu/README.md
# zgpu v0.11.0 - Cross-platform graphics library Small helper library built on top of [Dawn](https://github.com/zig-gamedev/dawn) native WebGPU implementation Supports Windows 10+ (DirectX 12), macOS 12+ (Metal) and Linux (Vulkan). ## Features: - Zero-overhead wgpu API bindings ([source code](https://github.com/michal-z/zig-gamedev/blob/main/libs/zgpu/src/wgpu.zig)) - Uniform buffer pool for fast CPU->GPU transfers - Resource pools and handle-based GPU resources - Async shader compilation - GPU mipmap generator For more details please see below. ## Getting started Copy `zgpu`, `zpool` and `system-sdk` to a subdirectory of your project and add the following to your `build.zig.zon` dependencies: ```zig .{ .zgpu = .{ .path = "libs/zgpu" }, .zpool = .{ .path = "libs/zpool" }, .dawn_x86_64_windows_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-windows-gnu/archive/d3a68014e6b6b53fd330a0ccba99e4dcfffddae5.tar.gz", .hash = "1220f9448cde02ef3cd51bde2e0850d4489daa0541571d748154e89c6eb46c76a267", }, .dawn_x86_64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-linux-gnu/archive/7d70db023bf254546024629cbec5ee6113e12a42.tar.gz", .hash = "12204a3519efd49ea2d7cf63b544492a3a771d37eda320f86380813376801e4cfa73", }, .dawn_aarch64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-linux-gnu/archive/c1f55e740a62f6942ff046e709ecd509a005dbeb.tar.gz", .hash = "12205cd13f6849f94ef7688ee88c6b74c7918a5dfb514f8a403fcc2929a0aa342627", }, .dawn_aarch64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-macos/archive/d2360cdfff0cf4a780cb77aa47c57aca03cc6dfe.tar.gz", .hash = "12201fe677e9c7cfb8984a36446b329d5af23d03dc1e4f79a853399529e523a007fa", }, .dawn_x86_64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-macos/archive/901716b10b31ce3e0d3fe479326b41e91d59c661.tar.gz", .hash = "1220b1f02f2f7edd98a078c64e3100907d90311d94880a3cc5927e1ac009d002667a", }, } ``` then in your `build.zig`: ```zig pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ ... }); @import("system_sdk").addLibraryPathsTo(exe); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{}); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); } ``` --- ## Sample applications - [gui test (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/gui_test_wgpu) - [physically based rendering (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/physically_based_rendering_wgpu) - [bullet physics test (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/bullet_physics_test_wgpu) - [procedural mesh (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/procedural_mesh_wgpu) - [textured quad (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/textured_quad_wgpu) - [triangle (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/triangle_wgpu) ## Library overview Below you can find an overview of main `zgpu` features. ### Compile-time options You can override default options in your `build.zig`: ```zig pub fn build(b: *std.Build) void { ... const zgpu = @import("zgpu").package(b, target, optimize, .{ .options = .{ .uniforms_buffer_size = 4 * 1024 * 1024, .dawn_skip_validation = false, .buffer_pool_size = 256, .texture_pool_size = 256, .texture_view_pool_size = 256, .sampler_pool_size = 16, .render_pipeline_pool_size = 128, .compute_pipeline_pool_size = 128, .bind_group_pool_size = 32, .bind_group_layout_pool_size = 32, .pipeline_layout_pool_size = 32, }, }); zgpu.link(exe); ... } ``` ### Graphics Context Create a `GraphicsContext` using a `WindowProvider`. For example, using [zglfw](https://github.com/zig-gamedev): ```zig const gctx = try zgpu.GraphicsContext.create( allocator, .{ .window = window, .fn_getTime = @ptrCast(&zglfw.getTime), .fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize), // optional fields .fn_getWin32Window = @ptrCast(&zglfw.getWin32Window), .fn_getX11Display = @ptrCast(&zglfw.getX11Display), .fn_getX11Window = @ptrCast(&zglfw.getX11Window), .fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay), .fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow), .fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow), }, .{}, // default context creation options ); ``` ### Uniforms - Implemented as a uniform buffer pool - Easy to use - Efficient - only one copy operation per frame ```zig struct DrawUniforms = extern struct { object_to_world: zm.Mat, }; const mem = gctx.uniformsAllocate(DrawUniforms, 1); mem.slice[0] = .{ .object_to_world = zm.transpose(zm.translation(...)) }; pass.setBindGroup(0, bind_group, &.{mem.offset}); pass.drawIndexed(...); // When you are done encoding all commands for a frame: gctx.submit(...); // Injects *one* copy operation to transfer *all* allocated uniforms ``` ### Resource pools - Every GPU resource is identified by 32-bit integer handle - All resources are stored in one system - We keep basic info about each resource (size of the buffer, format of the texture, etc.) - You can always check if resource is valid (very useful for async operations) - System keeps basic info about resource dependencies, for example, `TextureViewHandle` knows about its parent texture and becomes invalid when parent texture becomes invalid; `BindGroupHandle` knows about all resources it binds so it becomes invalid if any of those resources become invalid ```zig const buffer_handle = gctx.createBuffer(...); if (gctx.isResourceValid(buffer_handle)) { const buffer = gctx.lookupResource(buffer_handle).?; // Returns `wgpu.Buffer` const buffer_info = gctx.lookupResourceInfo(buffer_handle).?; // Returns `zgpu.BufferInfo` std.debug.print("Buffer size is: {d}", .{buffer_info.size}); } // If you want to destroy a resource before shutting down graphics context: gctx.destroyResource(buffer_handle); ``` ### Async shader compilation - Thanks to resource pools and resources identified by handles we can easily async compile all our shaders ```zig const DemoState = struct { pipeline_handle: zgpu.PipelineLayoutHandle = .{}, ... }; const demo = try allocator.create(DemoState); // Below call schedules pipeline compilation and returns immediately. When compilation is complete // valid pipeline handle will be stored in `demo.pipeline_handle`. gctx.createRenderPipelineAsync(allocator, pipeline_layout, pipeline_descriptor, &demo.pipeline_handle); // Pass using our pipeline will be skipped until compilation is ready pass: { const pipeline = gctx.lookupResource(demo.pipeline_handle) orelse break :pass; ... pass.setPipeline(pipeline); pass.drawIndexed(...); } ``` ### Mipmap generation on the GPU - wgpu API does not provide mipmap generator - zgpu provides decent mipmap generator implemented in a compute shader - It supports 2D textures, array textures and cubemap textures of any format (`rgba8_unorm`, `rg16_float`, `rgba32_float`, etc.) - Currently it requires that: `texture_width == texture_height and isPowerOfTwo(texture_width)` - It takes ~260 microsec to generate all mips for 1024x1024 `rgba8_unorm` texture on GTX 1660 ```zig // Usage: gctx.generateMipmaps(arena, command_encoder, texture_handle); ```
0
repos/simulations/libs
repos/simulations/libs/zgpu/build.zig.zon
.{ .name = "zgpu", .version = "0.11.0", .paths = .{ "build.zig", "build.zig.zon", "libs", "src", "README.md", }, .dependencies = .{ .system_sdk = .{ .path = "../system-sdk" }, .zpool = .{ .path = "../zpool" }, .dawn_x86_64_windows_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-windows-gnu/archive/d3a68014e6b6b53fd330a0ccba99e4dcfffddae5.tar.gz", .hash = "1220f9448cde02ef3cd51bde2e0850d4489daa0541571d748154e89c6eb46c76a267", }, .dawn_x86_64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-linux-gnu/archive/7d70db023bf254546024629cbec5ee6113e12a42.tar.gz", .hash = "12204a3519efd49ea2d7cf63b544492a3a771d37eda320f86380813376801e4cfa73", }, .dawn_aarch64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-linux-gnu/archive/c1f55e740a62f6942ff046e709ecd509a005dbeb.tar.gz", .hash = "12205cd13f6849f94ef7688ee88c6b74c7918a5dfb514f8a403fcc2929a0aa342627", }, .dawn_aarch64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-macos/archive/d2360cdfff0cf4a780cb77aa47c57aca03cc6dfe.tar.gz", .hash = "12201fe677e9c7cfb8984a36446b329d5af23d03dc1e4f79a853399529e523a007fa", }, .dawn_x86_64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-macos/archive/901716b10b31ce3e0d3fe479326b41e91d59c661.tar.gz", .hash = "1220b1f02f2f7edd98a078c64e3100907d90311d94880a3cc5927e1ac009d002667a", }, }, }
0
repos/simulations/libs
repos/simulations/libs/zgpu/build.zig
const std = @import("std"); const log = std.log.scoped(.zgpu); const default_options = struct { const uniforms_buffer_size = 4 * 1024 * 1024; const dawn_skip_validation = false; const buffer_pool_size = 256; const texture_pool_size = 256; const texture_view_pool_size = 256; const sampler_pool_size = 16; const render_pipeline_pool_size = 128; const compute_pipeline_pool_size = 128; const bind_group_pool_size = 32; const bind_group_layout_pool_size = 32; const pipeline_layout_pool_size = 32; }; pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{}); const target = b.standardTargetOptions(.{}); const options = .{ .uniforms_buffer_size = b.option( u64, "uniforms_buffer_size", "Set uniforms buffer size", ) orelse default_options.uniforms_buffer_size, .dawn_skip_validation = b.option( bool, "dawn_skip_validation", "Disable Dawn validation", ) orelse default_options.dawn_skip_validation, .buffer_pool_size = b.option( u32, "buffer_pool_size", "Set buffer pool size", ) orelse default_options.buffer_pool_size, .texture_pool_size = b.option( u32, "texture_pool_size", "Set texture pool size", ) orelse default_options.texture_pool_size, .texture_view_pool_size = b.option( u32, "texture_view_pool_size", "Set texture view pool size", ) orelse default_options.texture_view_pool_size, .sampler_pool_size = b.option( u32, "sampler_pool_size", "Set sample pool size", ) orelse default_options.sampler_pool_size, .render_pipeline_pool_size = b.option( u32, "render_pipeline_pool_size", "Set render pipeline pool size", ) orelse default_options.render_pipeline_pool_size, .compute_pipeline_pool_size = b.option( u32, "compute_pipeline_pool_size", "Set compute pipeline pool size", ) orelse default_options.compute_pipeline_pool_size, .bind_group_pool_size = b.option( u32, "bind_group_pool_size", "Set bind group pool size", ) orelse default_options.bind_group_pool_size, .bind_group_layout_pool_size = b.option( u32, "bind_group_layout_pool_size", "Set bind group layout pool size", ) orelse default_options.bind_group_layout_pool_size, .pipeline_layout_pool_size = b.option( u32, "pipeline_layout_pool_size", "Set pipeline layout pool size", ) orelse default_options.pipeline_layout_pool_size, }; const options_step = b.addOptions(); inline for (std.meta.fields(@TypeOf(options))) |field| { options_step.addOption(field.type, field.name, @field(options, field.name)); } const options_module = options_step.createModule(); _ = b.addModule("root", .{ .root_source_file = b.path("src/zgpu.zig"), .imports = &.{ .{ .name = "zgpu_options", .module = options_module }, .{ .name = "zpool", .module = b.dependency("zpool", .{}).module("root") }, }, }); const zdawn = b.addStaticLibrary(.{ .name = "zdawn", .target = target, .optimize = optimize, }); b.installArtifact(zdawn); @import("system_sdk").addLibraryPathsTo(zdawn); addLibraryPathsTo(zdawn); linkSystemDeps(zdawn); zdawn.linkSystemLibrary("dawn"); zdawn.linkLibC(); zdawn.linkLibCpp(); zdawn.addIncludePath(b.path("libs/dawn/include")); zdawn.addIncludePath(b.path("src")); zdawn.addCSourceFile(.{ .file = b.path("src/dawn.cpp"), .flags = &.{ "-std=c++17", "-fno-sanitize=undefined" }, }); zdawn.addCSourceFile(.{ .file = b.path("src/dawn_proc.c"), .flags = &.{"-fno-sanitize=undefined"}, }); const test_step = b.step("test", "Run zgpu tests"); const tests = b.addTest(.{ .name = "zgpu-tests", .root_source_file = b.path("src/zgpu.zig"), .target = target, .optimize = optimize, }); @import("system_sdk").addLibraryPathsTo(tests); tests.addIncludePath(b.path("libs/dawn/include")); tests.linkLibrary(zdawn); addLibraryPathsTo(tests); linkSystemDeps(tests); b.installArtifact(tests); test_step.dependOn(&b.addRunArtifact(tests).step); } pub fn linkSystemDeps(compile_step: *std.Build.Step.Compile) void { switch (compile_step.rootModuleTarget().os.tag) { .windows => { compile_step.linkSystemLibrary("ole32"); compile_step.linkSystemLibrary("dxguid"); }, .macos => { compile_step.linkSystemLibrary("objc"); compile_step.linkFramework("Metal"); compile_step.linkFramework("CoreGraphics"); compile_step.linkFramework("Foundation"); compile_step.linkFramework("IOKit"); compile_step.linkFramework("IOSurface"); compile_step.linkFramework("QuartzCore"); }, else => {}, } } pub fn addLibraryPathsTo(compile_step: *std.Build.Step.Compile) void { const b = compile_step.step.owner; const target = compile_step.rootModuleTarget(); switch (target.os.tag) { .windows => { compile_step.addLibraryPath(b.dependency("dawn_x86_64_windows_gnu", .{}).path("")); }, .linux => { compile_step.addLibraryPath(b.dependency(if (target.cpu.arch.isX86()) "dawn_x86_64_linux_gnu" else "dawn_aarch64_linux_gnu", .{}).path("")); }, .macos => { compile_step.addLibraryPath(b.dependency(if (target.cpu.arch.isX86()) "dawn_x86_64_macos" else "dawn_aarch64_macos", .{}).path("")); }, else => {}, } } pub fn checkTargetSupported(target: std.Target) bool { const supported = switch (target.os.tag) { .windows => target.cpu.arch.isX86() and target.abi.isGnu(), .linux => (target.cpu.arch.isX86() or target.cpu.arch.isAARCH64()) and target.abi.isGnu(), .macos => blk: { if (!target.cpu.arch.isX86() and !target.cpu.arch.isAARCH64()) break :blk false; // If min. target macOS version is lesser than the min version we have available, then // our Dawn binary is incompatible with the target. if (target.os.version_range.semver.min.order( .{ .major = 12, .minor = 0, .patch = 0 }, ) == .lt) break :blk false; break :blk true; }, else => false, }; if (supported == false) { log.warn("\n" ++ \\--------------------------------------------------------------------------- \\ \\Dawn/WebGPU binary for this target is not available. \\ \\Following targets are supported: \\ \\x86_64-windows-gnu \\x86_64-linux-gnu \\x86_64-macos.12.0.0-none \\aarch64-linux-gnu \\aarch64-macos.12.0.0-none \\ \\--------------------------------------------------------------------------- \\ , .{}); } return supported; }
0
repos/simulations/libs/zgpu
repos/simulations/libs/zgpu/src/dawn.cpp
#include "dawn/native/DawnNative.h" #include <assert.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif typedef struct DawnNativeInstanceImpl* DawnNativeInstance; DawnNativeInstance dniCreate(void) { return reinterpret_cast<DawnNativeInstance>(new dawn::native::Instance()); } void dniDestroy(DawnNativeInstance dni) { assert(dni); delete reinterpret_cast<dawn::native::Instance*>(dni); } WGPUInstance dniGetWgpuInstance(DawnNativeInstance dni) { assert(dni); return reinterpret_cast<dawn::native::Instance*>(dni)->Get(); } void dniDiscoverDefaultAdapters(DawnNativeInstance dni) { assert(dni); dawn::native::Instance* instance = reinterpret_cast<dawn::native::Instance*>(dni); instance->DiscoverDefaultAdapters(); } const DawnProcTable* dnGetProcs(void) { return &dawn::native::GetProcs(); } #ifdef __cplusplus } #endif
0
repos/simulations/libs/zgpu
repos/simulations/libs/zgpu/src/zgpu.zig
//-------------------------------------------------------------------------------------------------- // zgpu is a small helper library built on top of native wgpu implementation (Dawn). // // It supports Windows 10+ (DirectX 12), macOS 12+ (Metal) and Linux (Vulkan). // // https://github.com/michal-z/zig-gamedev/tree/main/libs/zgpu //-------------------------------------------------------------------------------------------------- const std = @import("std"); const math = std.math; const assert = std.debug.assert; const wgsl = @import("common_wgsl.zig"); const zgpu_options = @import("zgpu_options"); pub const wgpu = @import("wgpu.zig"); test { _ = wgpu; } pub const WindowProvider = struct { window: *anyopaque, fn_getTime: *const fn () f64, fn_getFramebufferSize: *const fn (window: *const anyopaque) [2]u32, fn_getWin32Window: *const fn (window: *const anyopaque) ?*anyopaque = undefined, fn_getX11Display: *const fn () ?*anyopaque = undefined, fn_getX11Window: *const fn (window: *const anyopaque) u32 = undefined, fn_getWaylandDisplay: ?*const fn () ?*anyopaque = null, fn_getWaylandSurface: ?*const fn (window: *const anyopaque) ?*anyopaque = null, fn_getCocoaWindow: *const fn (window: *const anyopaque) ?*anyopaque = undefined, fn getTime(self: WindowProvider) f64 { return self.fn_getTime(); } fn getFramebufferSize(self: WindowProvider) [2]u32 { return self.fn_getFramebufferSize(self.window); } fn getWin32Window(self: WindowProvider) ?*anyopaque { return self.fn_getWin32Window(self.window); } fn getX11Display(self: WindowProvider) ?*anyopaque { return self.fn_getX11Display(); } fn getX11Window(self: WindowProvider) u32 { return self.fn_getX11Window(self.window); } fn getWaylandDisplay(self: WindowProvider) ?*anyopaque { if (self.fn_getWaylandDisplay) |f| { return f(); } else { return @as(?*anyopaque, null); } } fn getWaylandSurface(self: WindowProvider) ?*anyopaque { if (self.fn_getWaylandSurface) |f| { return f(self.window); } else { return @as(?*anyopaque, null); } } fn getCocoaWindow(self: WindowProvider) ?*anyopaque { return self.fn_getCocoaWindow(self.window); } }; pub const GraphicsContextOptions = struct { present_mode: wgpu.PresentMode = .fifo, required_features: []const wgpu.FeatureName = &.{}, }; pub const GraphicsContext = struct { pub const swapchain_format = wgpu.TextureFormat.bgra8_unorm; window_provider: WindowProvider, stats: FrameStats = .{}, native_instance: DawnNativeInstance, instance: wgpu.Instance, device: wgpu.Device, queue: wgpu.Queue, surface: wgpu.Surface, swapchain: wgpu.SwapChain, swapchain_descriptor: wgpu.SwapChainDescriptor, buffer_pool: BufferPool, texture_pool: TexturePool, texture_view_pool: TextureViewPool, sampler_pool: SamplerPool, render_pipeline_pool: RenderPipelinePool, compute_pipeline_pool: ComputePipelinePool, bind_group_pool: BindGroupPool, bind_group_layout_pool: BindGroupLayoutPool, pipeline_layout_pool: PipelineLayoutPool, mipgens: std.AutoHashMap(wgpu.TextureFormat, MipgenResources), uniforms: struct { offset: u32 = 0, buffer: BufferHandle = .{}, stage: struct { num: u32 = 0, current: u32 = 0, buffers: [uniforms_staging_pipeline_len]UniformsStagingBuffer = [_]UniformsStagingBuffer{.{}} ** uniforms_staging_pipeline_len, } = .{}, } = .{}, pub fn create( allocator: std.mem.Allocator, window_provider: WindowProvider, options: GraphicsContextOptions, ) !*GraphicsContext { dawnProcSetProcs(dnGetProcs()); const native_instance = dniCreate(); errdefer dniDestroy(native_instance); const instance = dniGetWgpuInstance(native_instance).?; const adapter = adapter: { const Response = struct { status: wgpu.RequestAdapterStatus = .unknown, adapter: wgpu.Adapter = undefined, }; const callback = (struct { fn callback( status: wgpu.RequestAdapterStatus, adapter: wgpu.Adapter, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void { _ = message; const response = @as(*Response, @ptrCast(@alignCast(userdata))); response.status = status; response.adapter = adapter; } }).callback; var response = Response{}; instance.requestAdapter( .{ .power_preference = .high_performance }, callback, @ptrCast(&response), ); if (response.status != .success) { std.log.err("Failed to request GPU adapter (status: {s}).", .{@tagName(response.status)}); return error.NoGraphicsAdapter; } break :adapter response.adapter; }; errdefer adapter.release(); var properties: wgpu.AdapterProperties = undefined; properties.next_in_chain = null; adapter.getProperties(&properties); 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}); std.log.info("[zgpu] Adapter type: {s}", .{@tagName(properties.adapter_type)}); std.log.info("[zgpu] Backend type: {s}", .{@tagName(properties.backend_type)}); const device = device: { const Response = struct { status: wgpu.RequestDeviceStatus = .unknown, device: wgpu.Device = undefined, }; const callback = (struct { fn callback( status: wgpu.RequestDeviceStatus, device: wgpu.Device, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void { _ = message; const response = @as(*Response, @ptrCast(@alignCast(userdata))); response.status = status; response.device = device; } }).callback; const toggles = [_][*:0]const u8{"skip_validation"}; const dawn_toggles = wgpu.DawnTogglesDescriptor{ .chain = .{ .next = null, .struct_type = .dawn_toggles_descriptor }, .enabled_toggles_count = toggles.len, .enabled_toggles = &toggles, }; var response = Response{}; adapter.requestDevice( wgpu.DeviceDescriptor{ .next_in_chain = if (zgpu_options.dawn_skip_validation) @ptrCast(&dawn_toggles) else null, .required_features_count = options.required_features.len, .required_features = options.required_features.ptr, }, callback, @ptrCast(&response), ); if (response.status != .success) { std.log.err("Failed to request GPU device (status: {s}).", .{@tagName(response.status)}); return error.NoGraphicsDevice; } break :device response.device; }; errdefer device.release(); device.setUncapturedErrorCallback(logUnhandledError, null); const surface = createSurfaceForWindow(instance, window_provider); errdefer surface.release(); const framebuffer_size = window_provider.getFramebufferSize(); const swapchain_descriptor = wgpu.SwapChainDescriptor{ .label = "zig-gamedev-gctx-swapchain", .usage = .{ .render_attachment = true }, .format = swapchain_format, .width = @intCast(framebuffer_size[0]), .height = @intCast(framebuffer_size[1]), .present_mode = options.present_mode, }; const swapchain = device.createSwapChain(surface, swapchain_descriptor); errdefer swapchain.release(); const gctx = try allocator.create(GraphicsContext); gctx.* = .{ .window_provider = window_provider, .native_instance = native_instance, .instance = instance, .device = device, .queue = device.getQueue(), .surface = surface, .swapchain = swapchain, .swapchain_descriptor = swapchain_descriptor, .buffer_pool = BufferPool.init(allocator, zgpu_options.buffer_pool_size), .texture_pool = TexturePool.init(allocator, zgpu_options.texture_pool_size), .texture_view_pool = TextureViewPool.init(allocator, zgpu_options.texture_view_pool_size), .sampler_pool = SamplerPool.init(allocator, zgpu_options.sampler_pool_size), .render_pipeline_pool = RenderPipelinePool.init(allocator, zgpu_options.render_pipeline_pool_size), .compute_pipeline_pool = ComputePipelinePool.init(allocator, zgpu_options.compute_pipeline_pool_size), .bind_group_pool = BindGroupPool.init(allocator, zgpu_options.bind_group_pool_size), .bind_group_layout_pool = BindGroupLayoutPool.init(allocator, zgpu_options.bind_group_layout_pool_size), .pipeline_layout_pool = PipelineLayoutPool.init(allocator, zgpu_options.pipeline_layout_pool_size), .mipgens = std.AutoHashMap(wgpu.TextureFormat, MipgenResources).init(allocator), }; uniformsInit(gctx); return gctx; } pub fn destroy(gctx: *GraphicsContext, allocator: std.mem.Allocator) void { // Wait for the GPU to finish all encoded commands. while (gctx.stats.cpu_frame_number != gctx.stats.gpu_frame_number) { gctx.device.tick(); } // Wait for all outstanding mapAsync() calls to complete. wait_loop: while (true) { gctx.device.tick(); var i: u32 = 0; while (i < gctx.uniforms.stage.num) : (i += 1) { if (gctx.uniforms.stage.buffers[i].slice == null) { continue :wait_loop; } } break; } gctx.mipgens.deinit(); gctx.pipeline_layout_pool.deinit(allocator); gctx.bind_group_pool.deinit(allocator); gctx.bind_group_layout_pool.deinit(allocator); gctx.buffer_pool.deinit(allocator); gctx.texture_view_pool.deinit(allocator); gctx.texture_pool.deinit(allocator); gctx.sampler_pool.deinit(allocator); gctx.render_pipeline_pool.deinit(allocator); gctx.compute_pipeline_pool.deinit(allocator); gctx.surface.release(); gctx.swapchain.release(); gctx.queue.release(); gctx.device.release(); dniDestroy(gctx.native_instance); allocator.destroy(gctx); } // // Uniform buffer pool // pub fn uniformsAllocate( gctx: *GraphicsContext, comptime T: type, num_elements: u32, ) struct { slice: []T, offset: u32 } { assert(num_elements > 0); const size = num_elements * @sizeOf(T); const offset = gctx.uniforms.offset; const aligned_size = (size + (uniforms_alloc_alignment - 1)) & ~(uniforms_alloc_alignment - 1); if ((offset + aligned_size) >= uniforms_buffer_size) { std.log.err("[zgpu] Uniforms buffer size is too small. " ++ "Consider increasing 'zgpu.BuildOptions.uniforms_buffer_size' constant.", .{}); return .{ .slice = @as([*]T, undefined)[0..0], .offset = 0 }; } const current = gctx.uniforms.stage.current; const slice = (gctx.uniforms.stage.buffers[current].slice.?.ptr + offset)[0..size]; gctx.uniforms.offset += aligned_size; return .{ .slice = std.mem.bytesAsSlice(T, @as([]align(@alignOf(T)) u8, @alignCast(slice))), .offset = offset, }; } const UniformsStagingBuffer = struct { slice: ?[]u8 = null, buffer: wgpu.Buffer = undefined, }; const uniforms_buffer_size = zgpu_options.uniforms_buffer_size; const uniforms_staging_pipeline_len = 8; const uniforms_alloc_alignment: u32 = 256; fn uniformsInit(gctx: *GraphicsContext) void { gctx.uniforms.buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .uniform = true }, .size = uniforms_buffer_size, }); gctx.uniformsNextStagingBuffer(); } fn uniformsMappedCallback(status: wgpu.BufferMapAsyncStatus, userdata: ?*anyopaque) callconv(.C) void { const usb = @as(*UniformsStagingBuffer, @ptrCast(@alignCast(userdata))); assert(usb.slice == null); if (status == .success) { usb.slice = usb.buffer.getMappedRange(u8, 0, uniforms_buffer_size).?; } else { std.log.err("[zgpu] Failed to map buffer (status: {s}).", .{@tagName(status)}); } } fn uniformsNextStagingBuffer(gctx: *GraphicsContext) void { if (gctx.stats.cpu_frame_number > 0) { // Map staging buffer which was used this frame. const current = gctx.uniforms.stage.current; assert(gctx.uniforms.stage.buffers[current].slice == null); gctx.uniforms.stage.buffers[current].buffer.mapAsync( .{ .write = true }, 0, uniforms_buffer_size, uniformsMappedCallback, @ptrCast(&gctx.uniforms.stage.buffers[current]), ); } gctx.uniforms.offset = 0; 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; } } if (gctx.uniforms.stage.num >= uniforms_staging_pipeline_len) { // Wait until one of the buffers is mapped and ready to use. while (true) { gctx.device.tick(); i = 0; while (i < gctx.uniforms.stage.num) : (i += 1) { if (gctx.uniforms.stage.buffers[i].slice != null) { gctx.uniforms.stage.current = i; return; } } } } assert(gctx.uniforms.stage.num < uniforms_staging_pipeline_len); const current = gctx.uniforms.stage.num; gctx.uniforms.stage.current = current; gctx.uniforms.stage.num += 1; // Create new staging buffer. const buffer_handle = gctx.createBuffer(.{ .usage = .{ .copy_src = true, .map_write = true }, .size = uniforms_buffer_size, .mapped_at_creation = true, }); // Add new (mapped) staging buffer to the buffer list. gctx.uniforms.stage.buffers[current] = .{ .slice = gctx.lookupResource(buffer_handle).?.getMappedRange(u8, 0, uniforms_buffer_size).?, .buffer = gctx.lookupResource(buffer_handle).?, }; } // // Submit/Present // pub fn submit(gctx: *GraphicsContext, commands: []const wgpu.CommandBuffer) void { const stage_commands = stage_commands: { const stage_encoder = gctx.device.createCommandEncoder(null); defer stage_encoder.release(); const current = gctx.uniforms.stage.current; assert(gctx.uniforms.stage.buffers[current].slice != null); gctx.uniforms.stage.buffers[current].slice = null; gctx.uniforms.stage.buffers[current].buffer.unmap(); if (gctx.uniforms.offset > 0) { stage_encoder.copyBufferToBuffer( gctx.uniforms.stage.buffers[current].buffer, 0, gctx.lookupResource(gctx.uniforms.buffer).?, 0, gctx.uniforms.offset, ); } break :stage_commands stage_encoder.finish(null); }; defer stage_commands.release(); // TODO: We support up to 32 command buffers for now. Make it more robust. var command_buffers = std.BoundedArray(wgpu.CommandBuffer, 32).init(0) catch unreachable; command_buffers.append(stage_commands) catch unreachable; command_buffers.appendSlice(commands) catch unreachable; gctx.queue.onSubmittedWorkDone(0, gpuWorkDone, @ptrCast(&gctx.stats.gpu_frame_number)); gctx.queue.submit(command_buffers.slice()); gctx.stats.tick(gctx.window_provider.getTime()); gctx.uniformsNextStagingBuffer(); } fn gpuWorkDone(status: wgpu.QueueWorkDoneStatus, userdata: ?*anyopaque) callconv(.C) void { const gpu_frame_number: *u64 = @ptrCast(@alignCast(userdata)); gpu_frame_number.* += 1; if (status != .success) { std.log.err("[zgpu] Failed to complete GPU work (status: {s}).", .{@tagName(status)}); } } pub fn present(gctx: *GraphicsContext) enum { normal_execution, swap_chain_resized, } { gctx.swapchain.present(); const fb_size = gctx.window_provider.getFramebufferSize(); if (gctx.swapchain_descriptor.width != fb_size[0] or gctx.swapchain_descriptor.height != fb_size[1]) { if (fb_size[0] != 0 and fb_size[1] != 0) { gctx.swapchain_descriptor.width = @intCast(fb_size[0]); gctx.swapchain_descriptor.height = @intCast(fb_size[1]); gctx.swapchain.release(); gctx.swapchain = gctx.device.createSwapChain(gctx.surface, gctx.swapchain_descriptor); std.log.info( "[zgpu] Window has been resized to: {d}x{d}.", .{ gctx.swapchain_descriptor.width, gctx.swapchain_descriptor.height }, ); return .swap_chain_resized; } } return .normal_execution; } // // Resources // pub fn createBuffer(gctx: *GraphicsContext, descriptor: wgpu.BufferDescriptor) BufferHandle { return gctx.buffer_pool.addResource(gctx.*, .{ .gpuobj = gctx.device.createBuffer(descriptor), .size = descriptor.size, .usage = descriptor.usage, }); } pub fn createTexture(gctx: *GraphicsContext, descriptor: wgpu.TextureDescriptor) TextureHandle { return gctx.texture_pool.addResource(gctx.*, .{ .gpuobj = gctx.device.createTexture(descriptor), .usage = descriptor.usage, .dimension = descriptor.dimension, .size = descriptor.size, .format = descriptor.format, .mip_level_count = descriptor.mip_level_count, .sample_count = descriptor.sample_count, }); } pub fn createTextureView( gctx: *GraphicsContext, texture_handle: TextureHandle, descriptor: wgpu.TextureViewDescriptor, ) TextureViewHandle { const texture = gctx.lookupResource(texture_handle).?; const info = gctx.lookupResourceInfo(texture_handle).?; var dim = descriptor.dimension; if (dim == .undef) { dim = switch (info.dimension) { .tdim_1d => .tvdim_1d, .tdim_2d => .tvdim_2d, .tdim_3d => .tvdim_3d, }; } return gctx.texture_view_pool.addResource(gctx.*, .{ .gpuobj = texture.createView(descriptor), .format = if (descriptor.format == .undef) info.format else descriptor.format, .dimension = dim, .base_mip_level = descriptor.base_mip_level, .mip_level_count = if (descriptor.mip_level_count == 0xffff_ffff) info.mip_level_count else descriptor.mip_level_count, .base_array_layer = descriptor.base_array_layer, .array_layer_count = if (descriptor.array_layer_count == 0xffff_ffff) info.size.depth_or_array_layers else descriptor.array_layer_count, .aspect = descriptor.aspect, .parent_texture_handle = texture_handle, }); } pub fn createSampler(gctx: *GraphicsContext, descriptor: wgpu.SamplerDescriptor) SamplerHandle { return gctx.sampler_pool.addResource(gctx.*, .{ .gpuobj = gctx.device.createSampler(descriptor), .address_mode_u = descriptor.address_mode_u, .address_mode_v = descriptor.address_mode_v, .address_mode_w = descriptor.address_mode_w, .mag_filter = descriptor.mag_filter, .min_filter = descriptor.min_filter, .mipmap_filter = descriptor.mipmap_filter, .lod_min_clamp = descriptor.lod_min_clamp, .lod_max_clamp = descriptor.lod_max_clamp, .compare = descriptor.compare, .max_anisotropy = descriptor.max_anisotropy, }); } pub fn createRenderPipeline( gctx: *GraphicsContext, pipeline_layout: PipelineLayoutHandle, descriptor: wgpu.RenderPipelineDescriptor, ) RenderPipelineHandle { var desc = descriptor; desc.layout = gctx.lookupResource(pipeline_layout) orelse null; return gctx.render_pipeline_pool.addResource(gctx.*, .{ .gpuobj = gctx.device.createRenderPipeline(desc), .pipeline_layout_handle = pipeline_layout, }); } const AsyncCreateOpRender = struct { gctx: *GraphicsContext, result: *RenderPipelineHandle, pipeline_layout: PipelineLayoutHandle, allocator: std.mem.Allocator, fn create( status: wgpu.CreatePipelineAsyncStatus, pipeline: wgpu.RenderPipeline, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void { const op = @as(*AsyncCreateOpRender, @ptrCast(@alignCast(userdata))); if (status == .success) { op.result.* = op.gctx.render_pipeline_pool.addResource( op.gctx.*, .{ .gpuobj = pipeline, .pipeline_layout_handle = op.pipeline_layout }, ); } else { std.log.err( "[zgpu] Failed to async create render pipeline (status: {s}, message: {?s}).", .{ @tagName(status), message }, ); } op.allocator.destroy(op); } }; pub fn createRenderPipelineAsync( gctx: *GraphicsContext, allocator: std.mem.Allocator, pipeline_layout: PipelineLayoutHandle, descriptor: wgpu.RenderPipelineDescriptor, result: *RenderPipelineHandle, ) void { var desc = descriptor; desc.layout = gctx.lookupResource(pipeline_layout) orelse null; const op = allocator.create(AsyncCreateOpRender) catch unreachable; op.* = .{ .gctx = gctx, .result = result, .pipeline_layout = pipeline_layout, .allocator = allocator, }; gctx.device.createRenderPipelineAsync(desc, AsyncCreateOpRender.create, @ptrCast(op)); } pub fn createComputePipeline( gctx: *GraphicsContext, pipeline_layout: PipelineLayoutHandle, descriptor: wgpu.ComputePipelineDescriptor, ) ComputePipelineHandle { var desc = descriptor; desc.layout = gctx.lookupResource(pipeline_layout) orelse null; return gctx.compute_pipeline_pool.addResource(gctx.*, .{ .gpuobj = gctx.device.createComputePipeline(desc), .pipeline_layout_handle = pipeline_layout, }); } const AsyncCreateOpCompute = struct { gctx: *GraphicsContext, result: *ComputePipelineHandle, pipeline_layout: PipelineLayoutHandle, allocator: std.mem.Allocator, fn create( status: wgpu.CreatePipelineAsyncStatus, pipeline: wgpu.ComputePipeline, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void { const op = @as(*AsyncCreateOpCompute, @ptrCast(@alignCast(userdata))); if (status == .success) { op.result.* = op.gctx.compute_pipeline_pool.addResource( op.gctx.*, .{ .gpuobj = pipeline, .pipeline_layout_handle = op.pipeline_layout }, ); } else { std.log.err( "[zgpu] Failed to async create compute pipeline (status: {s}, message: {?s}).", .{ @tagName(status), message }, ); } op.allocator.destroy(op); } }; pub fn createComputePipelineAsync( gctx: *GraphicsContext, allocator: std.mem.Allocator, pipeline_layout: PipelineLayoutHandle, descriptor: wgpu.ComputePipelineDescriptor, result: *ComputePipelineHandle, ) void { var desc = descriptor; desc.layout = gctx.lookupResource(pipeline_layout) orelse null; const op = allocator.create(AsyncCreateOpCompute) catch unreachable; op.* = .{ .gctx = gctx, .result = result, .pipeline_layout = pipeline_layout, .allocator = allocator, }; gctx.device.createComputePipelineAsync(desc, AsyncCreateOpCompute.create, @ptrCast(op)); } pub fn createBindGroup( gctx: *GraphicsContext, layout: BindGroupLayoutHandle, entries: []const BindGroupEntryInfo, ) BindGroupHandle { assert(entries.len > 0 and entries.len <= max_num_bindings_per_group); var bind_group_info = BindGroupInfo{ .num_entries = @intCast(entries.len) }; var gpu_bind_group_entries: [max_num_bindings_per_group]wgpu.BindGroupEntry = undefined; for (entries, 0..) |entry, i| { bind_group_info.entries[i] = entry; if (entries[i].buffer_handle) |handle| { gpu_bind_group_entries[i] = .{ .binding = entries[i].binding, .buffer = gctx.lookupResource(handle).?, .offset = entries[i].offset, .size = entries[i].size, .sampler = null, .texture_view = null, }; } else if (entries[i].sampler_handle) |handle| { gpu_bind_group_entries[i] = .{ .binding = entries[i].binding, .buffer = null, .offset = 0, .size = 0, .sampler = gctx.lookupResource(handle).?, .texture_view = null, }; } else if (entries[i].texture_view_handle) |handle| { gpu_bind_group_entries[i] = .{ .binding = entries[i].binding, .buffer = null, .offset = 0, .size = 0, .sampler = null, .texture_view = gctx.lookupResource(handle).?, }; } else unreachable; } bind_group_info.gpuobj = gctx.device.createBindGroup(.{ .layout = gctx.lookupResource(layout).?, .entry_count = @intCast(entries.len), .entries = &gpu_bind_group_entries, }); return gctx.bind_group_pool.addResource(gctx.*, bind_group_info); } pub fn createBindGroupLayout( gctx: *GraphicsContext, entries: []const wgpu.BindGroupLayoutEntry, ) BindGroupLayoutHandle { assert(entries.len > 0 and entries.len <= max_num_bindings_per_group); var bind_group_layout_info = BindGroupLayoutInfo{ .gpuobj = gctx.device.createBindGroupLayout(.{ .entry_count = @intCast(entries.len), .entries = entries.ptr, }), .num_entries = @intCast(entries.len), }; for (entries, 0..) |entry, i| { bind_group_layout_info.entries[i] = entry; bind_group_layout_info.entries[i].next_in_chain = null; bind_group_layout_info.entries[i].buffer.next_in_chain = null; bind_group_layout_info.entries[i].sampler.next_in_chain = null; bind_group_layout_info.entries[i].texture.next_in_chain = null; bind_group_layout_info.entries[i].storage_texture.next_in_chain = null; } return gctx.bind_group_layout_pool.addResource(gctx.*, bind_group_layout_info); } pub fn createBindGroupLayoutAuto( gctx: *GraphicsContext, pipeline: anytype, group_index: u32, ) BindGroupLayoutHandle { const bgl = gctx.lookupResource(pipeline).?.getBindGroupLayout(group_index); return gctx.bind_group_layout_pool.addResource(gctx.*, BindGroupLayoutInfo{ .gpuobj = bgl }); } pub fn createPipelineLayout( gctx: *GraphicsContext, bind_group_layouts: []const BindGroupLayoutHandle, ) PipelineLayoutHandle { assert(bind_group_layouts.len > 0); var info: PipelineLayoutInfo = .{ .num_bind_group_layouts = @as(u32, @intCast(bind_group_layouts.len)) }; var gpu_bind_group_layouts: [max_num_bind_groups_per_pipeline]wgpu.BindGroupLayout = undefined; for (bind_group_layouts, 0..) |bgl, i| { info.bind_group_layouts[i] = bgl; gpu_bind_group_layouts[i] = gctx.lookupResource(bgl).?; } info.gpuobj = gctx.device.createPipelineLayout(.{ .bind_group_layout_count = info.num_bind_group_layouts, .bind_group_layouts = &gpu_bind_group_layouts, }); return gctx.pipeline_layout_pool.addResource(gctx.*, info); } pub fn lookupResource(gctx: GraphicsContext, handle: anytype) ?handleToGpuResourceType(@TypeOf(handle)) { if (gctx.isResourceValid(handle)) { const T = @TypeOf(handle); return switch (T) { BufferHandle => gctx.buffer_pool.getGpuObj(handle).?, TextureHandle => gctx.texture_pool.getGpuObj(handle).?, TextureViewHandle => gctx.texture_view_pool.getGpuObj(handle).?, SamplerHandle => gctx.sampler_pool.getGpuObj(handle).?, RenderPipelineHandle => gctx.render_pipeline_pool.getGpuObj(handle).?, ComputePipelineHandle => gctx.compute_pipeline_pool.getGpuObj(handle).?, BindGroupHandle => gctx.bind_group_pool.getGpuObj(handle).?, BindGroupLayoutHandle => gctx.bind_group_layout_pool.getGpuObj(handle).?, PipelineLayoutHandle => gctx.pipeline_layout_pool.getGpuObj(handle).?, else => @compileError( "[zgpu] GraphicsContext.lookupResource() not implemented for " ++ @typeName(T), ), }; } return null; } pub fn lookupResourceInfo(gctx: GraphicsContext, handle: anytype) ?handleToResourceInfoType(@TypeOf(handle)) { if (gctx.isResourceValid(handle)) { const T = @TypeOf(handle); return switch (T) { BufferHandle => gctx.buffer_pool.getInfo(handle), TextureHandle => gctx.texture_pool.getInfo(handle), TextureViewHandle => gctx.texture_view_pool.getInfo(handle), SamplerHandle => gctx.sampler_pool.getInfo(handle), RenderPipelineHandle => gctx.render_pipeline_pool.getInfo(handle), ComputePipelineHandle => gctx.compute_pipeline_pool.getInfo(handle), BindGroupHandle => gctx.bind_group_pool.getInfo(handle), BindGroupLayoutHandle => gctx.bind_group_layout_pool.getInfo(handle), PipelineLayoutHandle => gctx.pipeline_layout_pool.getInfo(handle), else => @compileError( "[zgpu] GraphicsContext.lookupResourceInfo() not implemented for " ++ @typeName(T), ), }; } return null; } pub fn releaseResource(gctx: *GraphicsContext, handle: anytype) void { const T = @TypeOf(handle); switch (T) { BufferHandle => gctx.buffer_pool.destroyResource(handle, false), TextureHandle => gctx.texture_pool.destroyResource(handle, false), TextureViewHandle => gctx.texture_view_pool.destroyResource(handle, false), SamplerHandle => gctx.sampler_pool.destroyResource(handle, false), RenderPipelineHandle => gctx.render_pipeline_pool.destroyResource(handle, false), ComputePipelineHandle => gctx.compute_pipeline_pool.destroyResource(handle, false), BindGroupHandle => gctx.bind_group_pool.destroyResource(handle, false), BindGroupLayoutHandle => gctx.bind_group_layout_pool.destroyResource(handle, false), PipelineLayoutHandle => gctx.pipeline_layout_pool.destroyResource(handle, false), else => @compileError("[zgpu] GraphicsContext.releaseResource() not implemented for " ++ @typeName(T)), } } pub fn destroyResource(gctx: *GraphicsContext, handle: anytype) void { const T = @TypeOf(handle); switch (T) { BufferHandle => gctx.buffer_pool.destroyResource(handle, true), TextureHandle => gctx.texture_pool.destroyResource(handle, true), else => @compileError("[zgpu] GraphicsContext.destroyResource() not implemented for " ++ @typeName(T)), } } pub fn isResourceValid(gctx: GraphicsContext, handle: anytype) bool { const T = @TypeOf(handle); switch (T) { BufferHandle => return gctx.buffer_pool.isHandleValid(handle), TextureHandle => return gctx.texture_pool.isHandleValid(handle), TextureViewHandle => { if (gctx.texture_view_pool.isHandleValid(handle)) { const texture = gctx.texture_view_pool.getInfoPtr(handle).parent_texture_handle; return gctx.isResourceValid(texture); } return false; }, SamplerHandle => return gctx.sampler_pool.isHandleValid(handle), RenderPipelineHandle => return gctx.render_pipeline_pool.isHandleValid(handle), ComputePipelineHandle => return gctx.compute_pipeline_pool.isHandleValid(handle), BindGroupHandle => { if (gctx.bind_group_pool.isHandleValid(handle)) { const num_entries = gctx.bind_group_pool.getInfoPtr(handle).num_entries; const entries = &gctx.bind_group_pool.getInfoPtr(handle).entries; var i: u32 = 0; while (i < num_entries) : (i += 1) { if (entries[i].buffer_handle) |buffer| { if (!gctx.isResourceValid(buffer)) return false; } else if (entries[i].sampler_handle) |sampler| { if (!gctx.isResourceValid(sampler)) return false; } else if (entries[i].texture_view_handle) |texture_view| { if (!gctx.isResourceValid(texture_view)) return false; } else unreachable; } return true; } return false; }, BindGroupLayoutHandle => return gctx.bind_group_layout_pool.isHandleValid(handle), PipelineLayoutHandle => return gctx.pipeline_layout_pool.isHandleValid(handle), else => @compileError("[zgpu] GraphicsContext.isResourceValid() not implemented for " ++ @typeName(T)), } } // // Mipmaps // const MipgenResources = struct { pipeline: ComputePipelineHandle = .{}, scratch_texture: TextureHandle = .{}, scratch_texture_views: [max_levels_per_dispatch]TextureViewHandle = [_]TextureViewHandle{.{}} ** max_levels_per_dispatch, bind_group_layout: BindGroupLayoutHandle = .{}, const max_levels_per_dispatch = 4; }; pub fn generateMipmaps( gctx: *GraphicsContext, arena: std.mem.Allocator, encoder: wgpu.CommandEncoder, texture: TextureHandle, ) void { const texture_info = gctx.lookupResourceInfo(texture) orelse return; if (texture_info.mip_level_count == 1) return; const max_size = 2048; assert(texture_info.usage.copy_dst == true); assert(texture_info.dimension == .tdim_2d); assert(texture_info.size.width <= max_size and texture_info.size.height <= max_size); assert(texture_info.size.width == texture_info.size.height); assert(math.isPowerOfTwo(texture_info.size.width)); const format = texture_info.format; const entry = gctx.mipgens.getOrPut(format) catch unreachable; const mipgen = entry.value_ptr; if (!entry.found_existing) { mipgen.bind_group_layout = gctx.createBindGroupLayout(&.{ bufferEntry(0, .{ .compute = true }, .uniform, true, 0), textureEntry(1, .{ .compute = true }, .unfilterable_float, .tvdim_2d, false), storageTextureEntry(2, .{ .compute = true }, .write_only, format, .tvdim_2d), storageTextureEntry(3, .{ .compute = true }, .write_only, format, .tvdim_2d), storageTextureEntry(4, .{ .compute = true }, .write_only, format, .tvdim_2d), storageTextureEntry(5, .{ .compute = true }, .write_only, format, .tvdim_2d), }); const pipeline_layout = gctx.createPipelineLayout(&.{ mipgen.bind_group_layout, }); defer gctx.releaseResource(pipeline_layout); const wgsl_src = wgsl.csGenerateMipmaps(arena, formatToShaderFormat(format)); const cs_module = createWgslShaderModule(gctx.device, wgsl_src, "zgpu_cs_generate_mipmaps"); defer { arena.free(wgsl_src); cs_module.release(); } mipgen.pipeline = gctx.createComputePipeline(pipeline_layout, .{ .compute = .{ .module = cs_module, .entry_point = "main", }, }); mipgen.scratch_texture = gctx.createTexture(.{ .usage = .{ .copy_src = true, .storage_binding = true }, .dimension = .tdim_2d, .size = .{ .width = max_size / 2, .height = max_size / 2, .depth_or_array_layers = 1 }, .format = format, .mip_level_count = MipgenResources.max_levels_per_dispatch, .sample_count = 1, }); for (&mipgen.scratch_texture_views, 0..) |*view, i| { view.* = gctx.createTextureView(mipgen.scratch_texture, .{ .base_mip_level = @intCast(i), .mip_level_count = 1, .base_array_layer = 0, .array_layer_count = 1, }); } } var array_layer: u32 = 0; while (array_layer < texture_info.size.depth_or_array_layers) : (array_layer += 1) { const texture_view = gctx.createTextureView(texture, .{ .dimension = .tvdim_2d, .base_array_layer = array_layer, .array_layer_count = 1, }); defer gctx.releaseResource(texture_view); const bind_group = gctx.createBindGroup(mipgen.bind_group_layout, &.{ .{ .binding = 0, .buffer_handle = gctx.uniforms.buffer, .offset = 0, .size = 8 }, .{ .binding = 1, .texture_view_handle = texture_view }, .{ .binding = 2, .texture_view_handle = mipgen.scratch_texture_views[0] }, .{ .binding = 3, .texture_view_handle = mipgen.scratch_texture_views[1] }, .{ .binding = 4, .texture_view_handle = mipgen.scratch_texture_views[2] }, .{ .binding = 5, .texture_view_handle = mipgen.scratch_texture_views[3] }, }); defer gctx.releaseResource(bind_group); const MipgenUniforms = extern struct { src_mip_level: i32, num_mip_levels: u32, }; var total_num_mips: u32 = texture_info.mip_level_count - 1; var current_src_mip_level: u32 = 0; while (true) { const dispatch_num_mips = @min(MipgenResources.max_levels_per_dispatch, total_num_mips); { const pass = encoder.beginComputePass(null); defer { pass.end(); pass.release(); } pass.setPipeline(gctx.lookupResource(mipgen.pipeline).?); const mem = gctx.uniformsAllocate(MipgenUniforms, 1); mem.slice[0] = .{ .src_mip_level = @intCast(current_src_mip_level), .num_mip_levels = dispatch_num_mips, }; pass.setBindGroup(0, gctx.lookupResource(bind_group).?, &.{mem.offset}); pass.dispatchWorkgroups( @max(texture_info.size.width >> @intCast(3 + current_src_mip_level), 1), @max(texture_info.size.height >> @intCast(3 + current_src_mip_level), 1), 1, ); } var mip_index: u32 = 0; while (mip_index < dispatch_num_mips) : (mip_index += 1) { const src_origin = wgpu.Origin3D{ .x = 0, .y = 0, .z = 0 }; const dst_origin = wgpu.Origin3D{ .x = 0, .y = 0, .z = array_layer }; encoder.copyTextureToTexture( .{ .texture = gctx.lookupResource(mipgen.scratch_texture).?, .mip_level = mip_index, .origin = src_origin, }, .{ .texture = gctx.lookupResource(texture).?, .mip_level = mip_index + current_src_mip_level + 1, .origin = dst_origin, }, .{ .width = texture_info.size.width >> @intCast(mip_index + current_src_mip_level + 1), .height = texture_info.size.height >> @intCast(mip_index + current_src_mip_level + 1), }, ); } assert(total_num_mips >= dispatch_num_mips); total_num_mips -= dispatch_num_mips; if (total_num_mips == 0) { break; } current_src_mip_level += dispatch_num_mips; } } } }; // Defined in dawn.cpp const DawnNativeInstance = ?*opaque {}; const DawnProcsTable = ?*opaque {}; extern fn dniCreate() DawnNativeInstance; extern fn dniDestroy(dni: DawnNativeInstance) void; extern fn dniGetWgpuInstance(dni: DawnNativeInstance) ?wgpu.Instance; extern fn dnGetProcs() DawnProcsTable; // Defined in Dawn codebase extern fn dawnProcSetProcs(procs: DawnProcsTable) void; /// Helper to create a buffer BindGroupLayoutEntry. pub fn bufferEntry( binding: u32, visibility: wgpu.ShaderStage, binding_type: wgpu.BufferBindingType, has_dynamic_offset: bool, min_binding_size: u64, ) wgpu.BindGroupLayoutEntry { return .{ .binding = binding, .visibility = visibility, .buffer = .{ .binding_type = binding_type, .has_dynamic_offset = has_dynamic_offset, .min_binding_size = min_binding_size, }, }; } /// Helper to create a sampler BindGroupLayoutEntry. pub fn samplerEntry( binding: u32, visibility: wgpu.ShaderStage, binding_type: wgpu.SamplerBindingType, ) wgpu.BindGroupLayoutEntry { return .{ .binding = binding, .visibility = visibility, .sampler = .{ .binding_type = binding_type }, }; } /// Helper to create a texture BindGroupLayoutEntry. pub fn textureEntry( binding: u32, visibility: wgpu.ShaderStage, sample_type: wgpu.TextureSampleType, view_dimension: wgpu.TextureViewDimension, multisampled: bool, ) wgpu.BindGroupLayoutEntry { return .{ .binding = binding, .visibility = visibility, .texture = .{ .sample_type = sample_type, .view_dimension = view_dimension, .multisampled = multisampled, }, }; } /// Helper to create a storage texture BindGroupLayoutEntry. pub fn storageTextureEntry( binding: u32, visibility: wgpu.ShaderStage, access: wgpu.StorageTextureAccess, format: wgpu.TextureFormat, view_dimension: wgpu.TextureViewDimension, ) wgpu.BindGroupLayoutEntry { return .{ .binding = binding, .visibility = visibility, .storage_texture = .{ .access = access, .format = format, .view_dimension = view_dimension, }, }; } /// You may disable async shader compilation for debugging purposes. const enable_async_shader_compilation = true; /// Helper function for creating render pipelines. /// Supports: one vertex buffer, one non-blending render target, /// one vertex shader module and one fragment shader module. pub fn createRenderPipelineSimple( allocator: std.mem.Allocator, gctx: *GraphicsContext, bgls: []const BindGroupLayoutHandle, wgsl_vs: [:0]const u8, wgsl_fs: [:0]const u8, vertex_stride: ?u64, vertex_attribs: ?[]const wgpu.VertexAttribute, primitive_state: wgpu.PrimitiveState, rt_format: wgpu.TextureFormat, depth_state: ?wgpu.DepthStencilState, out_pipe: *RenderPipelineHandle, ) void { const pl = gctx.createPipelineLayout(bgls); defer gctx.releaseResource(pl); const vs_mod = createWgslShaderModule(gctx.device, wgsl_vs, null); defer vs_mod.release(); const fs_mod = createWgslShaderModule(gctx.device, wgsl_fs, null); defer fs_mod.release(); const color_targets = [_]wgpu.ColorTargetState{.{ .format = rt_format }}; const vertex_buffers = if (vertex_stride) |vs| [_]wgpu.VertexBufferLayout{.{ .array_stride = vs, .attribute_count = @intCast(vertex_attribs.?.len), .attributes = vertex_attribs.?.ptr, }} else null; const pipe_desc = wgpu.RenderPipelineDescriptor{ .vertex = wgpu.VertexState{ .module = vs_mod, .entry_point = "main", .buffer_count = if (vertex_buffers) |vbs| vbs.len else 0, .buffers = if (vertex_buffers) |vbs| &vbs else null, }, .fragment = &wgpu.FragmentState{ .module = fs_mod, .entry_point = "main", .target_count = color_targets.len, .targets = &color_targets, }, .depth_stencil = if (depth_state) |ds| &ds else null, .primitive = primitive_state, }; if (enable_async_shader_compilation) { gctx.createRenderPipelineAsync(allocator, pl, pipe_desc, out_pipe); } else { out_pipe.* = gctx.createRenderPipeline(pl, pipe_desc); } } /// Helper function for creating render passes. /// Supports: One color attachment and optional depth attachment. pub fn beginRenderPassSimple( encoder: wgpu.CommandEncoder, load_op: wgpu.LoadOp, color_texv: wgpu.TextureView, clear_color: ?wgpu.Color, depth_texv: ?wgpu.TextureView, clear_depth: ?f32, ) wgpu.RenderPassEncoder { if (depth_texv == null) { assert(clear_depth == null); } const color_attachments = [_]wgpu.RenderPassColorAttachment{.{ .view = color_texv, .load_op = load_op, .store_op = .store, .clear_value = if (clear_color) |cc| cc else .{ .r = 0, .g = 0, .b = 0, .a = 0 }, }}; if (depth_texv) |dtexv| { const depth_attachment = wgpu.RenderPassDepthStencilAttachment{ .view = dtexv, .depth_load_op = load_op, .depth_store_op = .store, .depth_clear_value = if (clear_depth) |cd| cd else 0.0, }; return encoder.beginRenderPass(.{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, .depth_stencil_attachment = &depth_attachment, }); } return encoder.beginRenderPass(.{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, }); } pub fn endReleasePass(pass: anytype) void { pass.end(); pass.release(); } pub fn createWgslShaderModule( device: wgpu.Device, source: [*:0]const u8, label: ?[*:0]const u8, ) wgpu.ShaderModule { const wgsl_desc = wgpu.ShaderModuleWGSLDescriptor{ .chain = .{ .next = null, .struct_type = .shader_module_wgsl_descriptor }, .code = source, }; const desc = wgpu.ShaderModuleDescriptor{ .next_in_chain = @ptrCast(&wgsl_desc), .label = if (label) |l| l else null, }; return device.createShaderModule(desc); } pub fn imageInfoToTextureFormat(num_components: u32, bytes_per_component: u32, is_hdr: bool) wgpu.TextureFormat { assert(num_components == 1 or num_components == 2 or num_components == 4); assert(bytes_per_component == 1 or bytes_per_component == 2); assert(if (is_hdr and bytes_per_component != 2) false else true); if (is_hdr) { if (num_components == 1) return .r16_float; if (num_components == 2) return .rg16_float; if (num_components == 4) return .rgba16_float; } else { if (bytes_per_component == 1) { if (num_components == 1) return .r8_unorm; if (num_components == 2) return .rg8_unorm; if (num_components == 4) return .rgba8_unorm; } else { // TODO: Looks like wgpu does not support 16 bit unorm formats. unreachable; } } unreachable; } pub const BufferInfo = struct { gpuobj: ?wgpu.Buffer = null, size: usize = 0, usage: wgpu.BufferUsage = .{}, }; pub const TextureInfo = struct { gpuobj: ?wgpu.Texture = null, usage: wgpu.TextureUsage = .{}, dimension: wgpu.TextureDimension = .tdim_1d, size: wgpu.Extent3D = .{ .width = 0 }, format: wgpu.TextureFormat = .undef, mip_level_count: u32 = 0, sample_count: u32 = 0, }; pub const TextureViewInfo = struct { gpuobj: ?wgpu.TextureView = null, format: wgpu.TextureFormat = .undef, dimension: wgpu.TextureViewDimension = .undef, base_mip_level: u32 = 0, mip_level_count: u32 = 0, base_array_layer: u32 = 0, array_layer_count: u32 = 0, aspect: wgpu.TextureAspect = .all, parent_texture_handle: TextureHandle = .{}, }; pub const SamplerInfo = struct { gpuobj: ?wgpu.Sampler = null, address_mode_u: wgpu.AddressMode = .repeat, address_mode_v: wgpu.AddressMode = .repeat, address_mode_w: wgpu.AddressMode = .repeat, mag_filter: wgpu.FilterMode = .nearest, min_filter: wgpu.FilterMode = .nearest, mipmap_filter: wgpu.MipmapFilterMode = .nearest, lod_min_clamp: f32 = 0.0, lod_max_clamp: f32 = 0.0, compare: wgpu.CompareFunction = .undef, max_anisotropy: u16 = 0, }; pub const RenderPipelineInfo = struct { gpuobj: ?wgpu.RenderPipeline = null, pipeline_layout_handle: PipelineLayoutHandle = .{}, }; pub const ComputePipelineInfo = struct { gpuobj: ?wgpu.ComputePipeline = null, pipeline_layout_handle: PipelineLayoutHandle = .{}, }; pub const BindGroupEntryInfo = struct { binding: u32 = 0, buffer_handle: ?BufferHandle = null, offset: u64 = 0, size: u64 = 0, sampler_handle: ?SamplerHandle = null, texture_view_handle: ?TextureViewHandle = null, }; const max_num_bindings_per_group = 10; pub const BindGroupInfo = struct { gpuobj: ?wgpu.BindGroup = null, num_entries: u32 = 0, entries: [max_num_bindings_per_group]BindGroupEntryInfo = [_]BindGroupEntryInfo{.{}} ** max_num_bindings_per_group, }; pub const BindGroupLayoutInfo = struct { gpuobj: ?wgpu.BindGroupLayout = null, num_entries: u32 = 0, entries: [max_num_bindings_per_group]wgpu.BindGroupLayoutEntry = [_]wgpu.BindGroupLayoutEntry{.{ .binding = 0, .visibility = .{} }} ** max_num_bindings_per_group, }; const max_num_bind_groups_per_pipeline = 4; pub const PipelineLayoutInfo = struct { gpuobj: ?wgpu.PipelineLayout = null, num_bind_group_layouts: u32 = 0, bind_group_layouts: [max_num_bind_groups_per_pipeline]BindGroupLayoutHandle = [_]BindGroupLayoutHandle{.{}} ** max_num_bind_groups_per_pipeline, }; pub const BufferHandle = BufferPool.Handle; pub const TextureHandle = TexturePool.Handle; pub const TextureViewHandle = TextureViewPool.Handle; pub const SamplerHandle = SamplerPool.Handle; pub const RenderPipelineHandle = RenderPipelinePool.Handle; pub const ComputePipelineHandle = ComputePipelinePool.Handle; pub const BindGroupHandle = BindGroupPool.Handle; pub const BindGroupLayoutHandle = BindGroupLayoutPool.Handle; pub const PipelineLayoutHandle = PipelineLayoutPool.Handle; const BufferPool = ResourcePool(BufferInfo, wgpu.Buffer); const TexturePool = ResourcePool(TextureInfo, wgpu.Texture); const TextureViewPool = ResourcePool(TextureViewInfo, wgpu.TextureView); const SamplerPool = ResourcePool(SamplerInfo, wgpu.Sampler); const RenderPipelinePool = ResourcePool(RenderPipelineInfo, wgpu.RenderPipeline); const ComputePipelinePool = ResourcePool(ComputePipelineInfo, wgpu.ComputePipeline); const BindGroupPool = ResourcePool(BindGroupInfo, wgpu.BindGroup); const BindGroupLayoutPool = ResourcePool(BindGroupLayoutInfo, wgpu.BindGroupLayout); const PipelineLayoutPool = ResourcePool(PipelineLayoutInfo, wgpu.PipelineLayout); fn ResourcePool(comptime Info: type, comptime Resource: type) type { const zpool = @import("zpool"); const Pool = zpool.Pool(16, 16, Resource, struct { info: Info }); return struct { const Self = @This(); pub const Handle = Pool.Handle; pool: Pool, fn init(allocator: std.mem.Allocator, capacity: u32) Self { const pool = Pool.initCapacity(allocator, capacity) catch unreachable; return .{ .pool = pool }; } fn deinit(self: *Self, allocator: std.mem.Allocator) void { _ = allocator; self.pool.deinit(); } fn addResource(self: *Self, gctx: GraphicsContext, info: Info) Handle { assert(info.gpuobj != null); if (self.pool.addIfNotFull(.{ .info = info })) |handle| { return handle; } // If pool is free, attempt to remove a resource that is now invalid // because of dependent resources which have become invalid. // For example, texture view becomes invalid when parent texture // is destroyed. // // TODO: We could instead store a linked list in Info to track // dependencies. The parent resource could "point" to the first // dependent resource, and each dependent resource could "point" to // the parent and the prev/next dependent resources of the same // type (perhaps using handles instead of pointers). // When a parent resource is destroyed, we could traverse that list // to destroy dependent resources, and when a dependent resource // is destroyed, we can remove it from the doubly-linked list. // // pub const TextureInfo = struct { // ... // // note generic name: // first_dependent_handle: TextureViewHandle = .{} // }; // // pub const TextureViewInfo = struct { // ... // // note generic names: // parent_handle: TextureHandle = .{}, // prev_dependent_handle: TextureViewHandle, // next_dependent_handle: TextureViewHandle, // }; if (self.removeResourceIfInvalid(gctx)) { if (self.pool.addIfNotFull(.{ .info = info })) |handle| { return handle; } } // TODO: For now we just assert if pool is full - make it more roboust. assert(false); return Handle.nil; } fn removeResourceIfInvalid(self: *Self, gctx: GraphicsContext) bool { var live_handles = self.pool.liveHandles(); while (live_handles.next()) |live_handle| { if (!gctx.isResourceValid(live_handle)) { self.destroyResource(live_handle, true); return true; } } return false; } fn destroyResource(self: *Self, handle: Handle, comptime call_destroy: bool) void { if (!self.isHandleValid(handle)) return; const resource_info = self.pool.getColumnPtrAssumeLive(handle, .info); const gpuobj = resource_info.gpuobj.?; if (call_destroy and (Handle == BufferHandle or Handle == TextureHandle)) { gpuobj.destroy(); } gpuobj.release(); resource_info.* = .{}; self.pool.removeAssumeLive(handle); } fn isHandleValid(self: Self, handle: Handle) bool { return self.pool.isLiveHandle(handle); } fn getInfoPtr(self: Self, handle: Handle) *Info { return self.pool.getColumnPtrAssumeLive(handle, .info); } fn getInfo(self: Self, handle: Handle) Info { return self.pool.getColumnAssumeLive(handle, .info); } fn getGpuObj(self: Self, handle: Handle) ?Resource { if (self.pool.getColumnPtrIfLive(handle, .info)) |info| { return info.gpuobj; } return null; } }; } const FrameStats = struct { time: f64 = 0.0, delta_time: f32 = 0.0, fps_counter: u32 = 0, fps: f64 = 0.0, average_cpu_time: f64 = 0.0, previous_time: f64 = 0.0, fps_refresh_time: f64 = 0.0, cpu_frame_number: u64 = 0, gpu_frame_number: u64 = 0, fn tick(stats: *FrameStats, now_secs: f64) void { stats.time = now_secs; stats.delta_time = @floatCast(stats.time - stats.previous_time); stats.previous_time = stats.time; if ((stats.time - stats.fps_refresh_time) >= 1.0) { const t = stats.time - stats.fps_refresh_time; const fps = @as(f64, @floatFromInt(stats.fps_counter)) / t; const ms = (1.0 / fps) * 1000.0; stats.fps = fps; stats.average_cpu_time = ms; stats.fps_refresh_time = stats.time; stats.fps_counter = 0; } stats.fps_counter += 1; stats.cpu_frame_number += 1; } }; const SurfaceDescriptorTag = enum { metal_layer, windows_hwnd, xlib, wayland, }; const SurfaceDescriptor = union(SurfaceDescriptorTag) { metal_layer: struct { label: ?[*:0]const u8 = null, layer: *anyopaque, }, windows_hwnd: struct { label: ?[*:0]const u8 = null, hinstance: *anyopaque, hwnd: *anyopaque, }, xlib: struct { label: ?[*:0]const u8 = null, display: *anyopaque, window: u32, }, wayland: struct { label: ?[*:0]const u8 = null, display: *anyopaque, surface: *anyopaque, }, }; fn isLinuxDesktopLike(tag: std.Target.Os.Tag) bool { return switch (tag) { .linux, .freebsd, .openbsd, .dragonfly, => true, else => false, }; } fn createSurfaceForWindow(instance: wgpu.Instance, window_provider: WindowProvider) wgpu.Surface { const os_tag = @import("builtin").target.os.tag; const descriptor = switch (os_tag) { .windows => SurfaceDescriptor{ .windows_hwnd = .{ .label = "basic surface", .hinstance = std.os.windows.kernel32.GetModuleHandleW(null).?, .hwnd = window_provider.getWin32Window().?, }, }, .macos => macos: { const ns_window = window_provider.getCocoaWindow().?; const ns_view = msgSend(ns_window, "contentView", .{}, *anyopaque); // [nsWindow contentView] // Create a CAMetalLayer that covers the whole window that will be passed to CreateSurface. msgSend(ns_view, "setWantsLayer:", .{true}, void); // [view setWantsLayer:YES] const layer = msgSend(objc.objc_getClass("CAMetalLayer"), "layer", .{}, ?*anyopaque); // [CAMetalLayer layer] if (layer == null) @panic("failed to create Metal layer"); msgSend(ns_view, "setLayer:", .{layer.?}, void); // [view setLayer:layer] // Use retina if the window was created with retina support. const scale_factor = msgSend(ns_window, "backingScaleFactor", .{}, f64); // [ns_window backingScaleFactor] msgSend(layer.?, "setContentsScale:", .{scale_factor}, void); // [layer setContentsScale:scale_factor] break :macos SurfaceDescriptor{ .metal_layer = .{ .label = "basic surface", .layer = layer.?, }, }; }, else => if (isLinuxDesktopLike(os_tag)) linux: { if (window_provider.getWaylandDisplay()) |wl_display| { break :linux SurfaceDescriptor{ .wayland = .{ .label = "basic surface", .display = wl_display, .surface = window_provider.getWaylandSurface().?, }, }; } else { break :linux SurfaceDescriptor{ .xlib = .{ .label = "basic surface", .display = window_provider.getX11Display().?, .window = window_provider.getX11Window(), }, }; } } else unreachable, }; return switch (descriptor) { .metal_layer => |src| blk: { var desc: wgpu.SurfaceDescriptorFromMetalLayer = undefined; desc.chain.next = null; desc.chain.struct_type = .surface_descriptor_from_metal_layer; desc.layer = src.layer; break :blk instance.createSurface(.{ .next_in_chain = @ptrCast(&desc), .label = if (src.label) |l| l else null, }); }, .windows_hwnd => |src| blk: { var desc: wgpu.SurfaceDescriptorFromWindowsHWND = undefined; desc.chain.next = null; desc.chain.struct_type = .surface_descriptor_from_windows_hwnd; desc.hinstance = src.hinstance; desc.hwnd = src.hwnd; break :blk instance.createSurface(.{ .next_in_chain = @ptrCast(&desc), .label = if (src.label) |l| l else null, }); }, .xlib => |src| blk: { var desc: wgpu.SurfaceDescriptorFromXlibWindow = undefined; desc.chain.next = null; desc.chain.struct_type = .surface_descriptor_from_xlib_window; desc.display = src.display; desc.window = src.window; break :blk instance.createSurface(.{ .next_in_chain = @ptrCast(&desc), .label = if (src.label) |l| l else null, }); }, .wayland => |src| blk: { var desc: wgpu.SurfaceDescriptorFromWaylandSurface = undefined; desc.chain.next = null; desc.chain.struct_type = .surface_descriptor_from_wayland_surface; desc.display = src.display; desc.surface = src.surface; break :blk instance.createSurface(.{ .next_in_chain = @ptrCast(&desc), .label = if (src.label) |l| l else null, }); }, }; } const objc = struct { const SEL = ?*opaque {}; const Class = ?*opaque {}; extern fn sel_getUid(str: [*:0]const u8) SEL; extern fn objc_getClass(name: [*:0]const u8) Class; extern fn objc_msgSend() void; }; fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType { const args_meta = @typeInfo(@TypeOf(args)).Struct.fields; const FnType = switch (args_meta.len) { 0 => *const fn (@TypeOf(obj), objc.SEL) callconv(.C) ReturnType, 1 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].type) callconv(.C) ReturnType, 2 => *const fn ( @TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type, ) callconv(.C) ReturnType, 3 => *const fn ( @TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type, ) callconv(.C) ReturnType, 4 => *const fn ( @TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type, args_meta[3].type, ) callconv(.C) ReturnType, else => @compileError("[zgpu] Unsupported number of args"), }; const func = @as(FnType, @ptrCast(&objc.objc_msgSend)); const sel = objc.sel_getUid(sel_name.ptr); return @call(.never_inline, func, .{ obj, sel } ++ args); } fn logUnhandledError( err_type: wgpu.ErrorType, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void { _ = userdata; switch (err_type) { .no_error => std.log.info("[zgpu] No error: {?s}", .{message}), .validation => std.log.err("[zgpu] Validation: {?s}", .{message}), .out_of_memory => std.log.err("[zgpu] Out of memory: {?s}", .{message}), .device_lost => std.log.err("[zgpu] Device lost: {?s}", .{message}), .internal => std.log.err("[zgpu] Internal error: {?s}", .{message}), .unknown => std.log.err("[zgpu] Unknown error: {?s}", .{message}), } // Exit the process for easier debugging. if (@import("builtin").mode == .Debug) std.process.exit(1); } fn handleToGpuResourceType(comptime T: type) type { return switch (T) { BufferHandle => wgpu.Buffer, TextureHandle => wgpu.Texture, TextureViewHandle => wgpu.TextureView, SamplerHandle => wgpu.Sampler, RenderPipelineHandle => wgpu.RenderPipeline, ComputePipelineHandle => wgpu.ComputePipeline, BindGroupHandle => wgpu.BindGroup, BindGroupLayoutHandle => wgpu.BindGroupLayout, PipelineLayoutHandle => wgpu.PipelineLayout, else => @compileError("[zgpu] handleToGpuResourceType() not implemented for " ++ @typeName(T)), }; } fn handleToResourceInfoType(comptime T: type) type { return switch (T) { BufferHandle => BufferInfo, TextureHandle => TextureInfo, TextureViewHandle => TextureViewInfo, SamplerHandle => SamplerInfo, RenderPipelineHandle => RenderPipelineInfo, ComputePipelineHandle => ComputePipelineInfo, BindGroupHandle => BindGroupInfo, BindGroupLayoutHandle => BindGroupLayoutInfo, PipelineLayoutHandle => PipelineLayoutInfo, else => @compileError("[zgpu] handleToResourceInfoType() not implemented for " ++ @typeName(T)), }; } fn formatToShaderFormat(format: wgpu.TextureFormat) []const u8 { // TODO: Add missing formats. return switch (format) { .rgba8_unorm => "rgba8unorm", .rgba8_snorm => "rgba8snorm", .rgba16_float => "rgba16float", .rgba32_float => "rgba32float", else => unreachable, }; }
0
repos/simulations/libs/zgpu
repos/simulations/libs/zgpu/src/common_wgsl.zig
const std = @import("std"); pub fn csGenerateMipmaps(allocator: std.mem.Allocator, format: []const u8) [:0]const u8 { const s0 = std.fmt.allocPrint( allocator, \\ @group(0) @binding(2) var dst_mipmap1: texture_storage_2d<{s}, write>; \\ @group(0) @binding(3) var dst_mipmap2: texture_storage_2d<{s}, write>; \\ @group(0) @binding(4) var dst_mipmap3: texture_storage_2d<{s}, write>; \\ @group(0) @binding(5) var dst_mipmap4: texture_storage_2d<{s}, write>; , .{ format, format, format, format }, ) catch unreachable; defer allocator.free(s0); return std.mem.joinZ(allocator, "\n\n", &.{ s0, cs_generate_mipmaps }) catch unreachable; } // zig fmt: off const cs_generate_mipmaps = \\ struct Uniforms { \\ src_mip_level: i32, \\ num_mip_levels: u32, \\ } \\ @group(0) @binding(0) var<uniform> uniforms: Uniforms; \\ @group(0) @binding(1) var src_image: texture_2d<f32>; \\ \\ var<workgroup> red: array<f32, 64>; \\ var<workgroup> green: array<f32, 64>; \\ var<workgroup> blue: array<f32, 64>; \\ var<workgroup> alpha: array<f32, 64>; \\ \\ fn storeColor(index: u32, color: vec4<f32>) { \\ red[index] = color.x; \\ green[index] = color.y; \\ blue[index] = color.z; \\ alpha[index] = color.w; \\ } \\ \\ fn loadColor(index: u32) -> vec4<f32> { \\ return vec4(red[index], green[index], blue[index], alpha[index]); \\ } \\ \\ @compute @workgroup_size(8, 8, 1) \\ fn main( \\ @builtin(global_invocation_id) global_invocation_id: vec3<u32>, \\ @builtin(local_invocation_index) local_invocation_index : u32, \\ ) { \\ let x = i32(global_invocation_id.x * 2u); \\ let y = i32(global_invocation_id.y * 2u); \\ \\ var s00 = textureLoad(src_image, vec2(x, y), uniforms.src_mip_level); \\ var s10 = textureLoad(src_image, vec2(x + 1, y), uniforms.src_mip_level); \\ var s01 = textureLoad(src_image, vec2(x, y + 1), uniforms.src_mip_level); \\ var s11 = textureLoad(src_image, vec2(x + 1, y + 1), uniforms.src_mip_level); \\ s00 = 0.25 * (s00 + s01 + s10 + s11); \\ \\ textureStore(dst_mipmap1, vec2<i32>(global_invocation_id.xy), s00); \\ storeColor(local_invocation_index, s00); \\ if (uniforms.num_mip_levels == 1u) { \\ return; \\ } \\ workgroupBarrier(); \\ \\ if ((local_invocation_index & 0x9u) == 0u) { \\ s10 = loadColor(local_invocation_index + 1u); \\ s01 = loadColor(local_invocation_index + 8u); \\ s11 = loadColor(local_invocation_index + 9u); \\ s00 = 0.25 * (s00 + s01 + s10 + s11); \\ textureStore(dst_mipmap2, vec2<i32>(global_invocation_id.xy / 2u), s00); \\ storeColor(local_invocation_index, s00); \\ } \\ if (uniforms.num_mip_levels == 2u) { \\ return; \\ } \\ workgroupBarrier(); \\ \\ if ((local_invocation_index & 0x1Bu) == 0u) { \\ s10 = loadColor(local_invocation_index + 2u); \\ s01 = loadColor(local_invocation_index + 16u); \\ s11 = loadColor(local_invocation_index + 18u); \\ s00 = 0.25 * (s00 + s01 + s10 + s11); \\ textureStore(dst_mipmap3, vec2<i32>(global_invocation_id.xy / 4u), s00); \\ storeColor(local_invocation_index, s00); \\ } \\ if (uniforms.num_mip_levels == 3u) { \\ return; \\ } \\ workgroupBarrier(); \\ \\ if (local_invocation_index == 0u) { \\ s10 = loadColor(local_invocation_index + 4u); \\ s01 = loadColor(local_invocation_index + 32u); \\ s11 = loadColor(local_invocation_index + 36u); \\ s00 = 0.25 * (s00 + s01 + s10 + s11); \\ textureStore(dst_mipmap4, vec2<i32>(global_invocation_id.xy / 8u), s00); \\ storeColor(local_invocation_index, s00); \\ } \\ } ; // zig fmt: on
0
repos/simulations/libs/zgpu
repos/simulations/libs/zgpu/src/dawn_proc.c
#include "dawn/dawn_proc.h" static DawnProcTable procs; static DawnProcTable nullProcs; void dawnProcSetProcs(const DawnProcTable* procs_) { if (procs_) { procs = *procs_; } else { procs = nullProcs; } } WGPUInstance wgpuCreateInstance(WGPUInstanceDescriptor const * descriptor) { return procs.createInstance(descriptor); } WGPUProc wgpuGetProcAddress(WGPUDevice device, char const * procName) { return procs.getProcAddress(device, procName); } WGPUDevice wgpuAdapterCreateDevice(WGPUAdapter adapter, WGPUDeviceDescriptor const * descriptor) { return procs.adapterCreateDevice(adapter, descriptor); } size_t wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName * features) { return procs.adapterEnumerateFeatures(adapter, features); } WGPUInstance wgpuAdapterGetInstance(WGPUAdapter adapter) { return procs.adapterGetInstance(adapter); } bool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits * limits) { return procs.adapterGetLimits(adapter, limits); } void wgpuAdapterGetProperties(WGPUAdapter adapter, WGPUAdapterProperties * properties) { procs.adapterGetProperties(adapter, properties); } bool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature) { return procs.adapterHasFeature(adapter, feature); } void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallback callback, void * userdata) { procs.adapterRequestDevice(adapter, descriptor, callback, userdata); } void wgpuAdapterReference(WGPUAdapter adapter) { procs.adapterReference(adapter); } void wgpuAdapterRelease(WGPUAdapter adapter) { procs.adapterRelease(adapter); } void wgpuBindGroupSetLabel(WGPUBindGroup bindGroup, char const * label) { procs.bindGroupSetLabel(bindGroup, label); } void wgpuBindGroupReference(WGPUBindGroup bindGroup) { procs.bindGroupReference(bindGroup); } void wgpuBindGroupRelease(WGPUBindGroup bindGroup) { procs.bindGroupRelease(bindGroup); } void wgpuBindGroupLayoutSetLabel(WGPUBindGroupLayout bindGroupLayout, char const * label) { procs.bindGroupLayoutSetLabel(bindGroupLayout, label); } void wgpuBindGroupLayoutReference(WGPUBindGroupLayout bindGroupLayout) { procs.bindGroupLayoutReference(bindGroupLayout); } void wgpuBindGroupLayoutRelease(WGPUBindGroupLayout bindGroupLayout) { procs.bindGroupLayoutRelease(bindGroupLayout); } void wgpuBufferDestroy(WGPUBuffer buffer) { procs.bufferDestroy(buffer); } void const * wgpuBufferGetConstMappedRange(WGPUBuffer buffer, size_t offset, size_t size) { return procs.bufferGetConstMappedRange(buffer, offset, size); } WGPUBufferMapState wgpuBufferGetMapState(WGPUBuffer buffer) { return procs.bufferGetMapState(buffer); } void * wgpuBufferGetMappedRange(WGPUBuffer buffer, size_t offset, size_t size) { return procs.bufferGetMappedRange(buffer, offset, size); } uint64_t wgpuBufferGetSize(WGPUBuffer buffer) { return procs.bufferGetSize(buffer); } WGPUBufferUsageFlags wgpuBufferGetUsage(WGPUBuffer buffer) { return procs.bufferGetUsage(buffer); } void wgpuBufferMapAsync(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, WGPUBufferMapCallback callback, void * userdata) { procs.bufferMapAsync(buffer, mode, offset, size, callback, userdata); } void wgpuBufferSetLabel(WGPUBuffer buffer, char const * label) { procs.bufferSetLabel(buffer, label); } void wgpuBufferUnmap(WGPUBuffer buffer) { procs.bufferUnmap(buffer); } void wgpuBufferReference(WGPUBuffer buffer) { procs.bufferReference(buffer); } void wgpuBufferRelease(WGPUBuffer buffer) { procs.bufferRelease(buffer); } void wgpuCommandBufferSetLabel(WGPUCommandBuffer commandBuffer, char const * label) { procs.commandBufferSetLabel(commandBuffer, label); } void wgpuCommandBufferReference(WGPUCommandBuffer commandBuffer) { procs.commandBufferReference(commandBuffer); } void wgpuCommandBufferRelease(WGPUCommandBuffer commandBuffer) { procs.commandBufferRelease(commandBuffer); } WGPUComputePassEncoder wgpuCommandEncoderBeginComputePass(WGPUCommandEncoder commandEncoder, WGPUComputePassDescriptor const * descriptor) { return procs.commandEncoderBeginComputePass(commandEncoder, descriptor); } WGPURenderPassEncoder wgpuCommandEncoderBeginRenderPass(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor) { return procs.commandEncoderBeginRenderPass(commandEncoder, descriptor); } void wgpuCommandEncoderClearBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size) { procs.commandEncoderClearBuffer(commandEncoder, buffer, offset, size); } void wgpuCommandEncoderCopyBufferToBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size) { procs.commandEncoderCopyBufferToBuffer(commandEncoder, source, sourceOffset, destination, destinationOffset, size); } void wgpuCommandEncoderCopyBufferToTexture(WGPUCommandEncoder commandEncoder, WGPUImageCopyBuffer const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) { procs.commandEncoderCopyBufferToTexture(commandEncoder, source, destination, copySize); } void wgpuCommandEncoderCopyTextureToBuffer(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyBuffer const * destination, WGPUExtent3D const * copySize) { procs.commandEncoderCopyTextureToBuffer(commandEncoder, source, destination, copySize); } void wgpuCommandEncoderCopyTextureToTexture(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) { procs.commandEncoderCopyTextureToTexture(commandEncoder, source, destination, copySize); } void wgpuCommandEncoderCopyTextureToTextureInternal(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) { procs.commandEncoderCopyTextureToTextureInternal(commandEncoder, source, destination, copySize); } WGPUCommandBuffer wgpuCommandEncoderFinish(WGPUCommandEncoder commandEncoder, WGPUCommandBufferDescriptor const * descriptor) { return procs.commandEncoderFinish(commandEncoder, descriptor); } void wgpuCommandEncoderInjectValidationError(WGPUCommandEncoder commandEncoder, char const * message) { procs.commandEncoderInjectValidationError(commandEncoder, message); } void wgpuCommandEncoderInsertDebugMarker(WGPUCommandEncoder commandEncoder, char const * markerLabel) { procs.commandEncoderInsertDebugMarker(commandEncoder, markerLabel); } void wgpuCommandEncoderPopDebugGroup(WGPUCommandEncoder commandEncoder) { procs.commandEncoderPopDebugGroup(commandEncoder); } void wgpuCommandEncoderPushDebugGroup(WGPUCommandEncoder commandEncoder, char const * groupLabel) { procs.commandEncoderPushDebugGroup(commandEncoder, groupLabel); } void wgpuCommandEncoderResolveQuerySet(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, WGPUBuffer destination, uint64_t destinationOffset) { procs.commandEncoderResolveQuerySet(commandEncoder, querySet, firstQuery, queryCount, destination, destinationOffset); } void wgpuCommandEncoderSetLabel(WGPUCommandEncoder commandEncoder, char const * label) { procs.commandEncoderSetLabel(commandEncoder, label); } void wgpuCommandEncoderWriteBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t bufferOffset, uint8_t const * data, uint64_t size) { procs.commandEncoderWriteBuffer(commandEncoder, buffer, bufferOffset, data, size); } void wgpuCommandEncoderWriteTimestamp(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t queryIndex) { procs.commandEncoderWriteTimestamp(commandEncoder, querySet, queryIndex); } void wgpuCommandEncoderReference(WGPUCommandEncoder commandEncoder) { procs.commandEncoderReference(commandEncoder); } void wgpuCommandEncoderRelease(WGPUCommandEncoder commandEncoder) { procs.commandEncoderRelease(commandEncoder); } void wgpuComputePassEncoderDispatchWorkgroups(WGPUComputePassEncoder computePassEncoder, uint32_t workgroupCountX, uint32_t workgroupCountY, uint32_t workgroupCountZ) { procs.computePassEncoderDispatchWorkgroups(computePassEncoder, workgroupCountX, workgroupCountY, workgroupCountZ); } void wgpuComputePassEncoderDispatchWorkgroupsIndirect(WGPUComputePassEncoder computePassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) { procs.computePassEncoderDispatchWorkgroupsIndirect(computePassEncoder, indirectBuffer, indirectOffset); } void wgpuComputePassEncoderEnd(WGPUComputePassEncoder computePassEncoder) { procs.computePassEncoderEnd(computePassEncoder); } void wgpuComputePassEncoderInsertDebugMarker(WGPUComputePassEncoder computePassEncoder, char const * markerLabel) { procs.computePassEncoderInsertDebugMarker(computePassEncoder, markerLabel); } void wgpuComputePassEncoderPopDebugGroup(WGPUComputePassEncoder computePassEncoder) { procs.computePassEncoderPopDebugGroup(computePassEncoder); } void wgpuComputePassEncoderPushDebugGroup(WGPUComputePassEncoder computePassEncoder, char const * groupLabel) { procs.computePassEncoderPushDebugGroup(computePassEncoder, groupLabel); } void wgpuComputePassEncoderSetBindGroup(WGPUComputePassEncoder computePassEncoder, uint32_t groupIndex, WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) { procs.computePassEncoderSetBindGroup(computePassEncoder, groupIndex, group, dynamicOffsetCount, dynamicOffsets); } void wgpuComputePassEncoderSetLabel(WGPUComputePassEncoder computePassEncoder, char const * label) { procs.computePassEncoderSetLabel(computePassEncoder, label); } void wgpuComputePassEncoderSetPipeline(WGPUComputePassEncoder computePassEncoder, WGPUComputePipeline pipeline) { procs.computePassEncoderSetPipeline(computePassEncoder, pipeline); } void wgpuComputePassEncoderWriteTimestamp(WGPUComputePassEncoder computePassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) { procs.computePassEncoderWriteTimestamp(computePassEncoder, querySet, queryIndex); } void wgpuComputePassEncoderReference(WGPUComputePassEncoder computePassEncoder) { procs.computePassEncoderReference(computePassEncoder); } void wgpuComputePassEncoderRelease(WGPUComputePassEncoder computePassEncoder) { procs.computePassEncoderRelease(computePassEncoder); } WGPUBindGroupLayout wgpuComputePipelineGetBindGroupLayout(WGPUComputePipeline computePipeline, uint32_t groupIndex) { return procs.computePipelineGetBindGroupLayout(computePipeline, groupIndex); } void wgpuComputePipelineSetLabel(WGPUComputePipeline computePipeline, char const * label) { procs.computePipelineSetLabel(computePipeline, label); } void wgpuComputePipelineReference(WGPUComputePipeline computePipeline) { procs.computePipelineReference(computePipeline); } void wgpuComputePipelineRelease(WGPUComputePipeline computePipeline) { procs.computePipelineRelease(computePipeline); } WGPUBindGroup wgpuDeviceCreateBindGroup(WGPUDevice device, WGPUBindGroupDescriptor const * descriptor) { return procs.deviceCreateBindGroup(device, descriptor); } WGPUBindGroupLayout wgpuDeviceCreateBindGroupLayout(WGPUDevice device, WGPUBindGroupLayoutDescriptor const * descriptor) { return procs.deviceCreateBindGroupLayout(device, descriptor); } WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const * descriptor) { return procs.deviceCreateBuffer(device, descriptor); } WGPUCommandEncoder wgpuDeviceCreateCommandEncoder(WGPUDevice device, WGPUCommandEncoderDescriptor const * descriptor) { return procs.deviceCreateCommandEncoder(device, descriptor); } WGPUComputePipeline wgpuDeviceCreateComputePipeline(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor) { return procs.deviceCreateComputePipeline(device, descriptor); } void wgpuDeviceCreateComputePipelineAsync(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor, WGPUCreateComputePipelineAsyncCallback callback, void * userdata) { procs.deviceCreateComputePipelineAsync(device, descriptor, callback, userdata); } WGPUBuffer wgpuDeviceCreateErrorBuffer(WGPUDevice device, WGPUBufferDescriptor const * descriptor) { return procs.deviceCreateErrorBuffer(device, descriptor); } WGPUExternalTexture wgpuDeviceCreateErrorExternalTexture(WGPUDevice device) { return procs.deviceCreateErrorExternalTexture(device); } WGPUShaderModule wgpuDeviceCreateErrorShaderModule(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor, char const * errorMessage) { return procs.deviceCreateErrorShaderModule(device, descriptor, errorMessage); } WGPUTexture wgpuDeviceCreateErrorTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor) { return procs.deviceCreateErrorTexture(device, descriptor); } WGPUExternalTexture wgpuDeviceCreateExternalTexture(WGPUDevice device, WGPUExternalTextureDescriptor const * externalTextureDescriptor) { return procs.deviceCreateExternalTexture(device, externalTextureDescriptor); } WGPUPipelineLayout wgpuDeviceCreatePipelineLayout(WGPUDevice device, WGPUPipelineLayoutDescriptor const * descriptor) { return procs.deviceCreatePipelineLayout(device, descriptor); } WGPUQuerySet wgpuDeviceCreateQuerySet(WGPUDevice device, WGPUQuerySetDescriptor const * descriptor) { return procs.deviceCreateQuerySet(device, descriptor); } WGPURenderBundleEncoder wgpuDeviceCreateRenderBundleEncoder(WGPUDevice device, WGPURenderBundleEncoderDescriptor const * descriptor) { return procs.deviceCreateRenderBundleEncoder(device, descriptor); } WGPURenderPipeline wgpuDeviceCreateRenderPipeline(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor) { return procs.deviceCreateRenderPipeline(device, descriptor); } void wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallback callback, void * userdata) { procs.deviceCreateRenderPipelineAsync(device, descriptor, callback, userdata); } WGPUSampler wgpuDeviceCreateSampler(WGPUDevice device, WGPUSamplerDescriptor const * descriptor) { return procs.deviceCreateSampler(device, descriptor); } WGPUShaderModule wgpuDeviceCreateShaderModule(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor) { return procs.deviceCreateShaderModule(device, descriptor); } WGPUSwapChain wgpuDeviceCreateSwapChain(WGPUDevice device, WGPUSurface surface, WGPUSwapChainDescriptor const * descriptor) { return procs.deviceCreateSwapChain(device, surface, descriptor); } WGPUTexture wgpuDeviceCreateTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor) { return procs.deviceCreateTexture(device, descriptor); } void wgpuDeviceDestroy(WGPUDevice device) { procs.deviceDestroy(device); } size_t wgpuDeviceEnumerateFeatures(WGPUDevice device, WGPUFeatureName * features) { return procs.deviceEnumerateFeatures(device, features); } void wgpuDeviceForceLoss(WGPUDevice device, WGPUDeviceLostReason type, char const * message) { procs.deviceForceLoss(device, type, message); } WGPUAdapter wgpuDeviceGetAdapter(WGPUDevice device) { return procs.deviceGetAdapter(device); } bool wgpuDeviceGetLimits(WGPUDevice device, WGPUSupportedLimits * limits) { return procs.deviceGetLimits(device, limits); } WGPUQueue wgpuDeviceGetQueue(WGPUDevice device) { return procs.deviceGetQueue(device); } WGPUTextureUsageFlags wgpuDeviceGetSupportedSurfaceUsage(WGPUDevice device, WGPUSurface surface) { return procs.deviceGetSupportedSurfaceUsage(device, surface); } bool wgpuDeviceHasFeature(WGPUDevice device, WGPUFeatureName feature) { return procs.deviceHasFeature(device, feature); } void wgpuDeviceInjectError(WGPUDevice device, WGPUErrorType type, char const * message) { procs.deviceInjectError(device, type, message); } void wgpuDevicePopErrorScope(WGPUDevice device, WGPUErrorCallback callback, void * userdata) { procs.devicePopErrorScope(device, callback, userdata); } void wgpuDevicePushErrorScope(WGPUDevice device, WGPUErrorFilter filter) { procs.devicePushErrorScope(device, filter); } void wgpuDeviceSetDeviceLostCallback(WGPUDevice device, WGPUDeviceLostCallback callback, void * userdata) { procs.deviceSetDeviceLostCallback(device, callback, userdata); } void wgpuDeviceSetLabel(WGPUDevice device, char const * label) { procs.deviceSetLabel(device, label); } void wgpuDeviceSetLoggingCallback(WGPUDevice device, WGPULoggingCallback callback, void * userdata) { procs.deviceSetLoggingCallback(device, callback, userdata); } void wgpuDeviceSetUncapturedErrorCallback(WGPUDevice device, WGPUErrorCallback callback, void * userdata) { procs.deviceSetUncapturedErrorCallback(device, callback, userdata); } void wgpuDeviceTick(WGPUDevice device) { procs.deviceTick(device); } void wgpuDeviceValidateTextureDescriptor(WGPUDevice device, WGPUTextureDescriptor const * descriptor) { procs.deviceValidateTextureDescriptor(device, descriptor); } void wgpuDeviceReference(WGPUDevice device) { procs.deviceReference(device); } void wgpuDeviceRelease(WGPUDevice device) { procs.deviceRelease(device); } void wgpuExternalTextureDestroy(WGPUExternalTexture externalTexture) { procs.externalTextureDestroy(externalTexture); } void wgpuExternalTextureExpire(WGPUExternalTexture externalTexture) { procs.externalTextureExpire(externalTexture); } void wgpuExternalTextureRefresh(WGPUExternalTexture externalTexture) { procs.externalTextureRefresh(externalTexture); } void wgpuExternalTextureSetLabel(WGPUExternalTexture externalTexture, char const * label) { procs.externalTextureSetLabel(externalTexture, label); } void wgpuExternalTextureReference(WGPUExternalTexture externalTexture) { procs.externalTextureReference(externalTexture); } void wgpuExternalTextureRelease(WGPUExternalTexture externalTexture) { procs.externalTextureRelease(externalTexture); } WGPUSurface wgpuInstanceCreateSurface(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor) { return procs.instanceCreateSurface(instance, descriptor); } void wgpuInstanceProcessEvents(WGPUInstance instance) { procs.instanceProcessEvents(instance); } void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata) { procs.instanceRequestAdapter(instance, options, callback, userdata); } void wgpuInstanceReference(WGPUInstance instance) { procs.instanceReference(instance); } void wgpuInstanceRelease(WGPUInstance instance) { procs.instanceRelease(instance); } void wgpuPipelineLayoutSetLabel(WGPUPipelineLayout pipelineLayout, char const * label) { procs.pipelineLayoutSetLabel(pipelineLayout, label); } void wgpuPipelineLayoutReference(WGPUPipelineLayout pipelineLayout) { procs.pipelineLayoutReference(pipelineLayout); } void wgpuPipelineLayoutRelease(WGPUPipelineLayout pipelineLayout) { procs.pipelineLayoutRelease(pipelineLayout); } void wgpuQuerySetDestroy(WGPUQuerySet querySet) { procs.querySetDestroy(querySet); } uint32_t wgpuQuerySetGetCount(WGPUQuerySet querySet) { return procs.querySetGetCount(querySet); } WGPUQueryType wgpuQuerySetGetType(WGPUQuerySet querySet) { return procs.querySetGetType(querySet); } void wgpuQuerySetSetLabel(WGPUQuerySet querySet, char const * label) { procs.querySetSetLabel(querySet, label); } void wgpuQuerySetReference(WGPUQuerySet querySet) { procs.querySetReference(querySet); } void wgpuQuerySetRelease(WGPUQuerySet querySet) { procs.querySetRelease(querySet); } void wgpuQueueCopyExternalTextureForBrowser(WGPUQueue queue, WGPUImageCopyExternalTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) { procs.queueCopyExternalTextureForBrowser(queue, source, destination, copySize, options); } void wgpuQueueCopyTextureForBrowser(WGPUQueue queue, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) { procs.queueCopyTextureForBrowser(queue, source, destination, copySize, options); } void wgpuQueueOnSubmittedWorkDone(WGPUQueue queue, uint64_t signalValue, WGPUQueueWorkDoneCallback callback, void * userdata) { procs.queueOnSubmittedWorkDone(queue, signalValue, callback, userdata); } void wgpuQueueSetLabel(WGPUQueue queue, char const * label) { procs.queueSetLabel(queue, label); } void wgpuQueueSubmit(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands) { procs.queueSubmit(queue, commandCount, commands); } void wgpuQueueWriteBuffer(WGPUQueue queue, WGPUBuffer buffer, uint64_t bufferOffset, void const * data, size_t size) { procs.queueWriteBuffer(queue, buffer, bufferOffset, data, size); } void wgpuQueueWriteTexture(WGPUQueue queue, WGPUImageCopyTexture const * destination, void const * data, size_t dataSize, WGPUTextureDataLayout const * dataLayout, WGPUExtent3D const * writeSize) { procs.queueWriteTexture(queue, destination, data, dataSize, dataLayout, writeSize); } void wgpuQueueReference(WGPUQueue queue) { procs.queueReference(queue); } void wgpuQueueRelease(WGPUQueue queue) { procs.queueRelease(queue); } void wgpuRenderBundleSetLabel(WGPURenderBundle renderBundle, char const * label) { procs.renderBundleSetLabel(renderBundle, label); } void wgpuRenderBundleReference(WGPURenderBundle renderBundle) { procs.renderBundleReference(renderBundle); } void wgpuRenderBundleRelease(WGPURenderBundle renderBundle) { procs.renderBundleRelease(renderBundle); } void wgpuRenderBundleEncoderDraw(WGPURenderBundleEncoder renderBundleEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) { procs.renderBundleEncoderDraw(renderBundleEncoder, vertexCount, instanceCount, firstVertex, firstInstance); } void wgpuRenderBundleEncoderDrawIndexed(WGPURenderBundleEncoder renderBundleEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) { procs.renderBundleEncoderDrawIndexed(renderBundleEncoder, indexCount, instanceCount, firstIndex, baseVertex, firstInstance); } void wgpuRenderBundleEncoderDrawIndexedIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) { procs.renderBundleEncoderDrawIndexedIndirect(renderBundleEncoder, indirectBuffer, indirectOffset); } void wgpuRenderBundleEncoderDrawIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) { procs.renderBundleEncoderDrawIndirect(renderBundleEncoder, indirectBuffer, indirectOffset); } WGPURenderBundle wgpuRenderBundleEncoderFinish(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderBundleDescriptor const * descriptor) { return procs.renderBundleEncoderFinish(renderBundleEncoder, descriptor); } void wgpuRenderBundleEncoderInsertDebugMarker(WGPURenderBundleEncoder renderBundleEncoder, char const * markerLabel) { procs.renderBundleEncoderInsertDebugMarker(renderBundleEncoder, markerLabel); } void wgpuRenderBundleEncoderPopDebugGroup(WGPURenderBundleEncoder renderBundleEncoder) { procs.renderBundleEncoderPopDebugGroup(renderBundleEncoder); } void wgpuRenderBundleEncoderPushDebugGroup(WGPURenderBundleEncoder renderBundleEncoder, char const * groupLabel) { procs.renderBundleEncoderPushDebugGroup(renderBundleEncoder, groupLabel); } void wgpuRenderBundleEncoderSetBindGroup(WGPURenderBundleEncoder renderBundleEncoder, uint32_t groupIndex, WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) { procs.renderBundleEncoderSetBindGroup(renderBundleEncoder, groupIndex, group, dynamicOffsetCount, dynamicOffsets); } void wgpuRenderBundleEncoderSetIndexBuffer(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) { procs.renderBundleEncoderSetIndexBuffer(renderBundleEncoder, buffer, format, offset, size); } void wgpuRenderBundleEncoderSetLabel(WGPURenderBundleEncoder renderBundleEncoder, char const * label) { procs.renderBundleEncoderSetLabel(renderBundleEncoder, label); } void wgpuRenderBundleEncoderSetPipeline(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderPipeline pipeline) { procs.renderBundleEncoderSetPipeline(renderBundleEncoder, pipeline); } void wgpuRenderBundleEncoderSetVertexBuffer(WGPURenderBundleEncoder renderBundleEncoder, uint32_t slot, WGPUBuffer buffer, uint64_t offset, uint64_t size) { procs.renderBundleEncoderSetVertexBuffer(renderBundleEncoder, slot, buffer, offset, size); } void wgpuRenderBundleEncoderReference(WGPURenderBundleEncoder renderBundleEncoder) { procs.renderBundleEncoderReference(renderBundleEncoder); } void wgpuRenderBundleEncoderRelease(WGPURenderBundleEncoder renderBundleEncoder) { procs.renderBundleEncoderRelease(renderBundleEncoder); } void wgpuRenderPassEncoderBeginOcclusionQuery(WGPURenderPassEncoder renderPassEncoder, uint32_t queryIndex) { procs.renderPassEncoderBeginOcclusionQuery(renderPassEncoder, queryIndex); } void wgpuRenderPassEncoderDraw(WGPURenderPassEncoder renderPassEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) { procs.renderPassEncoderDraw(renderPassEncoder, vertexCount, instanceCount, firstVertex, firstInstance); } void wgpuRenderPassEncoderDrawIndexed(WGPURenderPassEncoder renderPassEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) { procs.renderPassEncoderDrawIndexed(renderPassEncoder, indexCount, instanceCount, firstIndex, baseVertex, firstInstance); } void wgpuRenderPassEncoderDrawIndexedIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) { procs.renderPassEncoderDrawIndexedIndirect(renderPassEncoder, indirectBuffer, indirectOffset); } void wgpuRenderPassEncoderDrawIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) { procs.renderPassEncoderDrawIndirect(renderPassEncoder, indirectBuffer, indirectOffset); } void wgpuRenderPassEncoderEnd(WGPURenderPassEncoder renderPassEncoder) { procs.renderPassEncoderEnd(renderPassEncoder); } void wgpuRenderPassEncoderEndOcclusionQuery(WGPURenderPassEncoder renderPassEncoder) { procs.renderPassEncoderEndOcclusionQuery(renderPassEncoder); } void wgpuRenderPassEncoderExecuteBundles(WGPURenderPassEncoder renderPassEncoder, size_t bundleCount, WGPURenderBundle const * bundles) { procs.renderPassEncoderExecuteBundles(renderPassEncoder, bundleCount, bundles); } void wgpuRenderPassEncoderInsertDebugMarker(WGPURenderPassEncoder renderPassEncoder, char const * markerLabel) { procs.renderPassEncoderInsertDebugMarker(renderPassEncoder, markerLabel); } void wgpuRenderPassEncoderPopDebugGroup(WGPURenderPassEncoder renderPassEncoder) { procs.renderPassEncoderPopDebugGroup(renderPassEncoder); } void wgpuRenderPassEncoderPushDebugGroup(WGPURenderPassEncoder renderPassEncoder, char const * groupLabel) { procs.renderPassEncoderPushDebugGroup(renderPassEncoder, groupLabel); } void wgpuRenderPassEncoderSetBindGroup(WGPURenderPassEncoder renderPassEncoder, uint32_t groupIndex, WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) { procs.renderPassEncoderSetBindGroup(renderPassEncoder, groupIndex, group, dynamicOffsetCount, dynamicOffsets); } void wgpuRenderPassEncoderSetBlendConstant(WGPURenderPassEncoder renderPassEncoder, WGPUColor const * color) { procs.renderPassEncoderSetBlendConstant(renderPassEncoder, color); } void wgpuRenderPassEncoderSetIndexBuffer(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) { procs.renderPassEncoderSetIndexBuffer(renderPassEncoder, buffer, format, offset, size); } void wgpuRenderPassEncoderSetLabel(WGPURenderPassEncoder renderPassEncoder, char const * label) { procs.renderPassEncoderSetLabel(renderPassEncoder, label); } void wgpuRenderPassEncoderSetPipeline(WGPURenderPassEncoder renderPassEncoder, WGPURenderPipeline pipeline) { procs.renderPassEncoderSetPipeline(renderPassEncoder, pipeline); } void wgpuRenderPassEncoderSetScissorRect(WGPURenderPassEncoder renderPassEncoder, uint32_t x, uint32_t y, uint32_t width, uint32_t height) { procs.renderPassEncoderSetScissorRect(renderPassEncoder, x, y, width, height); } void wgpuRenderPassEncoderSetStencilReference(WGPURenderPassEncoder renderPassEncoder, uint32_t reference) { procs.renderPassEncoderSetStencilReference(renderPassEncoder, reference); } void wgpuRenderPassEncoderSetVertexBuffer(WGPURenderPassEncoder renderPassEncoder, uint32_t slot, WGPUBuffer buffer, uint64_t offset, uint64_t size) { procs.renderPassEncoderSetVertexBuffer(renderPassEncoder, slot, buffer, offset, size); } void wgpuRenderPassEncoderSetViewport(WGPURenderPassEncoder renderPassEncoder, float x, float y, float width, float height, float minDepth, float maxDepth) { procs.renderPassEncoderSetViewport(renderPassEncoder, x, y, width, height, minDepth, maxDepth); } void wgpuRenderPassEncoderWriteTimestamp(WGPURenderPassEncoder renderPassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) { procs.renderPassEncoderWriteTimestamp(renderPassEncoder, querySet, queryIndex); } void wgpuRenderPassEncoderReference(WGPURenderPassEncoder renderPassEncoder) { procs.renderPassEncoderReference(renderPassEncoder); } void wgpuRenderPassEncoderRelease(WGPURenderPassEncoder renderPassEncoder) { procs.renderPassEncoderRelease(renderPassEncoder); } WGPUBindGroupLayout wgpuRenderPipelineGetBindGroupLayout(WGPURenderPipeline renderPipeline, uint32_t groupIndex) { return procs.renderPipelineGetBindGroupLayout(renderPipeline, groupIndex); } void wgpuRenderPipelineSetLabel(WGPURenderPipeline renderPipeline, char const * label) { procs.renderPipelineSetLabel(renderPipeline, label); } void wgpuRenderPipelineReference(WGPURenderPipeline renderPipeline) { procs.renderPipelineReference(renderPipeline); } void wgpuRenderPipelineRelease(WGPURenderPipeline renderPipeline) { procs.renderPipelineRelease(renderPipeline); } void wgpuSamplerSetLabel(WGPUSampler sampler, char const * label) { procs.samplerSetLabel(sampler, label); } void wgpuSamplerReference(WGPUSampler sampler) { procs.samplerReference(sampler); } void wgpuSamplerRelease(WGPUSampler sampler) { procs.samplerRelease(sampler); } void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata) { procs.shaderModuleGetCompilationInfo(shaderModule, callback, userdata); } void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, char const * label) { procs.shaderModuleSetLabel(shaderModule, label); } void wgpuShaderModuleReference(WGPUShaderModule shaderModule) { procs.shaderModuleReference(shaderModule); } void wgpuShaderModuleRelease(WGPUShaderModule shaderModule) { procs.shaderModuleRelease(shaderModule); } void wgpuSurfaceReference(WGPUSurface surface) { procs.surfaceReference(surface); } void wgpuSurfaceRelease(WGPUSurface surface) { procs.surfaceRelease(surface); } WGPUTexture wgpuSwapChainGetCurrentTexture(WGPUSwapChain swapChain) { return procs.swapChainGetCurrentTexture(swapChain); } WGPUTextureView wgpuSwapChainGetCurrentTextureView(WGPUSwapChain swapChain) { return procs.swapChainGetCurrentTextureView(swapChain); } void wgpuSwapChainPresent(WGPUSwapChain swapChain) { procs.swapChainPresent(swapChain); } void wgpuSwapChainReference(WGPUSwapChain swapChain) { procs.swapChainReference(swapChain); } void wgpuSwapChainRelease(WGPUSwapChain swapChain) { procs.swapChainRelease(swapChain); } WGPUTextureView wgpuTextureCreateView(WGPUTexture texture, WGPUTextureViewDescriptor const * descriptor) { return procs.textureCreateView(texture, descriptor); } void wgpuTextureDestroy(WGPUTexture texture) { procs.textureDestroy(texture); } uint32_t wgpuTextureGetDepthOrArrayLayers(WGPUTexture texture) { return procs.textureGetDepthOrArrayLayers(texture); } WGPUTextureDimension wgpuTextureGetDimension(WGPUTexture texture) { return procs.textureGetDimension(texture); } WGPUTextureFormat wgpuTextureGetFormat(WGPUTexture texture) { return procs.textureGetFormat(texture); } uint32_t wgpuTextureGetHeight(WGPUTexture texture) { return procs.textureGetHeight(texture); } uint32_t wgpuTextureGetMipLevelCount(WGPUTexture texture) { return procs.textureGetMipLevelCount(texture); } uint32_t wgpuTextureGetSampleCount(WGPUTexture texture) { return procs.textureGetSampleCount(texture); } WGPUTextureUsageFlags wgpuTextureGetUsage(WGPUTexture texture) { return procs.textureGetUsage(texture); } uint32_t wgpuTextureGetWidth(WGPUTexture texture) { return procs.textureGetWidth(texture); } void wgpuTextureSetLabel(WGPUTexture texture, char const * label) { procs.textureSetLabel(texture, label); } void wgpuTextureReference(WGPUTexture texture) { procs.textureReference(texture); } void wgpuTextureRelease(WGPUTexture texture) { procs.textureRelease(texture); } void wgpuTextureViewSetLabel(WGPUTextureView textureView, char const * label) { procs.textureViewSetLabel(textureView, label); } void wgpuTextureViewReference(WGPUTextureView textureView) { procs.textureViewReference(textureView); } void wgpuTextureViewRelease(WGPUTextureView textureView) { procs.textureViewRelease(textureView); }
0
repos/simulations/libs/zgpu
repos/simulations/libs/zgpu/src/wgpu.zig
const std = @import("std"); test "extern struct ABI compatibility" { @setEvalBranchQuota(10_000); const wgpu = @cImport(@cInclude("dawn/webgpu.h")); inline for (comptime std.meta.declarations(@This())) |decl| { const ZigType = @field(@This(), decl.name); if (@TypeOf(ZigType) != type) { continue; } if (comptime std.meta.activeTag(@typeInfo(ZigType)) == .Struct and @typeInfo(ZigType).Struct.layout == .@"extern") { const wgpu_name = "WGPU" ++ decl.name; const CType = @field(wgpu, wgpu_name); std.testing.expectEqual(@sizeOf(CType), @sizeOf(ZigType)) catch |err| { std.log.err("@sizeOf({s}) != @sizeOf({s})", .{ wgpu_name, decl.name }); return err; }; comptime var i: usize = 0; inline for (comptime std.meta.fieldNames(CType)) |c_field_name| { std.testing.expectEqual( @offsetOf(CType, c_field_name), @offsetOf(ZigType, std.meta.fieldNames(ZigType)[i]), ) catch |err| { std.log.err( "@offsetOf({s}, {s}) != @offsetOf({s}, {s})", .{ wgpu_name, c_field_name, decl.name, std.meta.fieldNames(ZigType)[i] }, ); return err; }; i += 1; } } } } pub const AdapterType = enum(u32) { discrete_gpu, integrated_gpu, cpu, unknown, }; pub const AddressMode = enum(u32) { repeat = 0x00000000, mirror_repeat = 0x00000001, clamp_to_edge = 0x00000002, }; pub const AlphaMode = enum(u32) { premultiplied = 0x00000000, unpremultiplied = 0x00000001, opaq = 0x00000002, }; pub const BackendType = enum(u32) { undef, nul, webgpu, d3d11, d3d12, metal, vulkan, opengl, 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 BufferBindingType = enum(u32) { undef = 0x00000000, uniform = 0x00000001, storage = 0x00000002, read_only_storage = 0x00000003, }; pub const BufferMapAsyncStatus = enum(u32) { success = 0x00000000, validation_error = 0x00000001, unknown = 0x00000002, device_lost = 0x00000003, destroyed_before_callback = 0x00000004, unmapped_before_callback = 0x00000005, mappingAlreadyPending = 0x00000006, offset_out_of_range = 0x00000007, size_out_of_range = 0x00000008, }; pub const BufferMapState = enum(u32) { unmapped = 0x00000000, pending = 0x00000001, mapped = 0x00000002, }; pub const CompareFunction = enum(u32) { undef = 0x00000000, never = 0x00000001, less = 0x00000002, less_equal = 0x00000003, greater = 0x00000004, greater_equal = 0x00000005, equal = 0x00000006, not_equal = 0x00000007, always = 0x00000008, }; pub const CompilationInfoRequestStatus = enum(u32) { success = 0x00000000, err = 0x00000001, device_lost = 0x00000002, unknown = 0x00000003, }; pub const CompilationMessageType = enum(u32) { err = 0x00000000, warning = 0x00000001, info = 0x00000002, }; pub const ComputePassTimestampLocation = enum(u32) { beginning = 0x00000000, end = 0x00000001, }; pub const CreatePipelineAsyncStatus = enum(u32) { success = 0x00000000, validation_error = 0x00000001, internal_error = 0x00000002, device_lost = 0x00000003, device_destroyed = 0x00000004, unknown = 0x00000005, }; pub const ExternalTextureRotation = enum(u32) { rotate_0_degrees = 0x00000000, rotate_90_degrees = 0x00000001, rotate_180_degrees = 0x00000002, rotate_270_degrees = 0x00000003, }; pub const CullMode = enum(u32) { none = 0x00000000, front = 0x00000001, back = 0x00000002, }; pub const DeviceLostReason = enum(u32) { undef = 0x00000000, destroyed = 0x00000001, }; pub const ErrorFilter = enum(u32) { validation = 0x00000000, out_of_memory = 0x00000001, internal = 0x00000002, }; pub const ErrorType = enum(u32) { no_error = 0x00000000, validation = 0x00000001, out_of_memory = 0x00000002, internal = 0x00000003, unknown = 0x00000004, device_lost = 0x00000005, }; pub const FeatureName = enum(u32) { undef = 0x00000000, depth_clip_control = 0x00000001, depth32_float_stencil8 = 0x00000002, timestamp_query = 0x00000003, pipeline_statistics_query = 0x00000004, texture_compression_bc = 0x00000005, texture_compression_etc2 = 0x00000006, texture_compression_astc = 0x00000007, indirect_first_instance = 0x00000008, shader_f16 = 0x00000009, rg11_b10_ufloat_renderable = 0x0000000A, bgra8_unorm_storage = 0x0000000B, float32_filterable = 0x0000000C, depth_clamping = 0x000003E8, dawn_shader_float16 = 0x000003E9, dawn_internal_usages = 0x000003EA, dawn_multi_planar_formats = 0x000003EB, dawn_native = 0x000003EC, chromium_experimental_dp4a = 0x000003ED, timestamp_query_inside_passes = 0x000003EE, implicit_device_synchronization = 0x000003EF, surface_capabilities = 0x000003F0, transient_attachments = 0x000003F1, msaa_render_to_single_sampled = 0x000003F2, }; pub const FilterMode = enum(u32) { nearest = 0x00000000, linear = 0x00000001, }; pub const MipmapFilterMode = enum(u32) { nearest = 0x00000000, linear = 0x00000001, }; pub const FrontFace = enum(u32) { ccw = 0x00000000, cw = 0x00000001, }; pub const IndexFormat = enum(u32) { undef = 0x00000000, uint16 = 0x00000001, uint32 = 0x00000002, }; pub const LoadOp = enum(u32) { undef = 0x00000000, clear = 0x00000001, load = 0x00000002, }; pub const LoggingType = enum(u32) { verbose = 0x00000000, info = 0x00000001, warning = 0x00000002, err = 0x00000003, }; pub const PipelineStatisticName = enum(u32) { vertex_shader_invocations = 0x00000000, clipper_invocations = 0x00000001, clipper_primitives_out = 0x00000002, fragment_shader_invocations = 0x00000003, compute_shader_invocations = 0x00000004, }; pub const PowerPreference = enum(u32) { undef = 0x00000000, low_power = 0x00000001, high_performance = 0x00000002, }; pub const PresentMode = enum(u32) { immediate = 0x00000000, mailbox = 0x00000001, fifo = 0x00000002, }; pub const PrimitiveTopology = enum(u32) { point_list = 0x00000000, line_list = 0x00000001, line_strip = 0x00000002, triangle_list = 0x00000003, triangle_strip = 0x00000004, }; pub const QueryType = enum(u32) { occlusion = 0x00000000, pipeline_statistics = 0x00000001, timestamp = 0x00000002, }; pub const QueueWorkDoneStatus = enum(u32) { success = 0x00000000, err = 0x00000001, unknown = 0x00000002, device_lost = 0x00000003, }; pub const RenderPassTimestampLocation = enum(u32) { beginning = 0x00000000, end = 0x00000001, }; pub const RequestAdapterStatus = enum(u32) { success = 0x00000000, unavailable = 0x00000001, err = 0x00000002, unknown = 0x00000003, }; pub const RequestDeviceStatus = enum(u32) { success = 0x00000000, err = 0x00000001, unknown = 0x00000002, }; pub const SurfaceDescriptorFromMetalLayer = extern struct { chain: ChainedStruct, layer: *anyopaque, }; pub const SurfaceDescriptorFromWaylandSurface = extern struct { chain: ChainedStruct, display: *anyopaque, surface: *anyopaque, }; pub const SurfaceDescriptorFromWindowsHWND = extern struct { chain: ChainedStruct, hinstance: *anyopaque, hwnd: *anyopaque, }; pub const SurfaceDescriptorFromXlibWindow = extern struct { chain: ChainedStruct, display: *anyopaque, window: u32, }; pub const SurfaceDescriptorFromWindowsCoreWindow = extern struct { chain: ChainedStruct, core_window: *anyopaque, }; pub const SurfaceDescriptorFromWindowsSwapChainPanel = extern struct { chain: ChainedStruct, swap_chain_panel: *anyopaque, }; pub const SurfaceDescriptorFromCanvasHTMLSelector = extern struct { chain: ChainedStruct, selector: [*:0]const u8, }; pub const StructType = enum(u32) { invalid = 0x00000000, surface_descriptor_from_metal_layer = 0x00000001, surface_descriptor_from_windows_hwnd = 0x00000002, surface_descriptor_from_xlib_window = 0x00000003, surface_descriptor_from_canvas_html_selector = 0x00000004, shader_module_spirv_descriptor = 0x00000005, shader_module_wgsl_descriptor = 0x00000006, surface_descriptor_from_wayland_surface = 0x00000008, surface_descriptor_from_android_native_window = 0x00000009, surface_descriptor_from_windows_core_window = 0x0000000B, external_texture_binding_entry = 0x0000000C, external_texture_binding_layout = 0x0000000D, surface_descriptor_from_windows_swap_chain_panel = 0x0000000E, dawn_texture_internal_usage_descriptor = 0x000003E8, dawn_encoder_internal_usage_descriptor = 0x000003EB, dawn_instance_descriptor = 0x000003EC, dawn_cache_device_descriptor = 0x000003ED, dawn_adapter_properties_power_preference = 0x000003EE, dawn_buffer_descriptor_error_info_from_wire_client = 0x000003EF, dawn_toggles_descriptor = 0x000003F0, dawn_shader_module_spirv_options_descriptor = 0x000003F1, request_adapter_options_luid = 0x000003F2, request_adapter_options_get_gl_proc = 0x000003F3, dawn_multisample_state_render_to_single_sampled = 0x000003F4, dawn_render_pass_color_attachment_render_to_single_sampled = 0x000003F5, }; pub const SamplerBindingType = enum(u32) { undef = 0x00000000, filtering = 0x00000001, non_filtering = 0x00000002, comparison = 0x00000003, }; pub const StencilOperation = enum(u32) { keep = 0x00000000, zero = 0x00000001, replace = 0x00000002, invert = 0x00000003, increment_lamp = 0x00000004, decrement_clamp = 0x00000005, increment_wrap = 0x00000006, decrement_wrap = 0x00000007, }; pub const StorageTextureAccess = enum(u32) { undef = 0x00000000, write_only = 0x00000001, }; pub const StoreOp = enum(u32) { undef = 0x00000000, store = 0x00000001, discard = 0x00000002, }; pub const TextureAspect = enum(u32) { all = 0x00000000, stencil_only = 0x00000001, depth_only = 0x00000002, plane0_only = 0x00000003, plane1_only = 0x00000004, }; pub const TextureDimension = enum(u32) { tdim_1d = 0x00000000, tdim_2d = 0x00000001, tdim_3d = 0x00000002, }; pub const TextureFormat = enum(u32) { undef = 0x00000000, r8_unorm = 0x00000001, r8_snorm = 0x00000002, r8_uint = 0x00000003, r8_sint = 0x00000004, r16_uint = 0x00000005, r16_sint = 0x00000006, r16_float = 0x00000007, rg8_unorm = 0x00000008, rg8_snorm = 0x00000009, rg8_uint = 0x0000000a, rg8_sint = 0x0000000b, r32_float = 0x0000000c, r32_uint = 0x0000000d, r32_sint = 0x0000000e, rg16_uint = 0x0000000f, rg16_sint = 0x00000010, rg16_float = 0x00000011, rgba8_unorm = 0x00000012, rgba8_unorm_srgb = 0x00000013, rgba8_snorm = 0x00000014, rgba8_uint = 0x00000015, rgba8_sint = 0x00000016, bgra8_unorm = 0x00000017, bgra8_unorm_srgb = 0x00000018, rgb10_a2_unorm = 0x00000019, rg11_b10_ufloat = 0x0000001a, rgb9_e5_ufloat = 0x0000001b, rg32_float = 0x0000001c, rg32_uint = 0x0000001d, rg32_sint = 0x0000001e, rgba16_uint = 0x0000001f, rgba16_sint = 0x00000020, rgba16_float = 0x00000021, rgba32_float = 0x00000022, rgba32_uint = 0x00000023, rgba32_sint = 0x00000024, stencil8 = 0x00000025, depth16_unorm = 0x00000026, depth24_plus = 0x00000027, depth24_plus_stencil8 = 0x00000028, depth32_float = 0x00000029, depth32_float_stencil8 = 0x0000002a, bc1_rgba_unorm = 0x0000002b, bc1_rgba_unorm_srgb = 0x0000002c, bc2_rgba_unorm = 0x0000002d, bc2_rgba_unorm_srgb = 0x0000002e, bc3_rgba_unorm = 0x0000002f, bc3_rgba_unorm_srgb = 0x00000030, bc4_runorm = 0x00000031, bc4_rsnorm = 0x00000032, bc5_rg_unorm = 0x00000033, bc5_rg_snorm = 0x00000034, bc6_hrgb_ufloat = 0x00000035, bc6_hrgb_float = 0x00000036, bc7_rgba_unorm = 0x00000037, bc7_rgba_unorm_srgb = 0x00000038, etc2_rgb8_unorm = 0x00000039, etc2_rgb8_unorm_srgb = 0x0000003a, etc2_rgb8_a1_unorm = 0x0000003b, etc2_rgb8_a1_unorm_srgb = 0x0000003c, etc2_rgba8_unorm = 0x0000003d, etc2_rgba8_unorm_srgb = 0x0000003e, eacr11_unorm = 0x0000003f, eacr11_snorm = 0x00000040, eacrg11_unorm = 0x00000041, eacrg11_snorm = 0x00000042, astc4x4_unorm = 0x00000043, astc4x4_unorm_srgb = 0x00000044, astc5x4_unorm = 0x00000045, astc5x4_unorm_srgb = 0x00000046, astc5x5_unorm = 0x00000047, astc5x5_unorm_srgb = 0x00000048, astc6x5_unorm = 0x00000049, astc6x5_unorm_srgb = 0x0000004a, astc6x6_unorm = 0x0000004b, astc6x6_unorm_srgb = 0x0000004c, astc8x5_unorm = 0x0000004d, astc8x5_unorm_srgb = 0x0000004e, astc8x6_unorm = 0x0000004f, astc8x6_unorm_srgb = 0x00000050, astc8x8_unorm = 0x00000051, astc8x8_unorm_srgb = 0x00000052, astc10x5_unorm = 0x00000053, astc10x5_unorm_srgb = 0x00000054, astc10x6_unorm = 0x00000055, astc10x6_unorm_srgb = 0x00000056, astc10x8_unorm = 0x00000057, astc10x8_unorm_srgb = 0x00000058, astc10x10_unorm = 0x00000059, astc10x10_unorm_srgb = 0x0000005a, astc12x10_unorm = 0x0000005b, astc12x10_unorm_srgb = 0x0000005c, astc12x12_unorm = 0x0000005d, astc12x12_unorm_srgb = 0x0000005e, r8_bg8_biplanar420_unorm = 0x0000005f, }; pub const TextureSampleType = enum(u32) { undef = 0x00000000, float = 0x00000001, unfilterable_float = 0x00000002, depth = 0x00000003, sint = 0x00000004, uint = 0x00000005, }; pub const TextureViewDimension = enum(u32) { undef = 0x00000000, tvdim_1d = 0x00000001, tvdim_2d = 0x00000002, tvdim_2d_array = 0x00000003, tvdim_cube = 0x00000004, tvdim_cube_array = 0x00000005, tvdim_3d = 0x00000006, }; pub const VertexFormat = enum(u32) { undef = 0x00000000, uint8x2 = 0x00000001, uint8x4 = 0x00000002, sint8x2 = 0x00000003, sint8x4 = 0x00000004, unorm8x2 = 0x00000005, unorm8x4 = 0x00000006, snorm8x2 = 0x00000007, snorm8x4 = 0x00000008, uint16x2 = 0x00000009, uint16x4 = 0x0000000A, sint16x2 = 0x0000000B, sint16x4 = 0x0000000C, unorm16x2 = 0x0000000D, unorm16x4 = 0x0000000E, snorm16x2 = 0x0000000F, snorm16x4 = 0x00000010, float16x2 = 0x00000011, float16x4 = 0x00000012, float32 = 0x00000013, float32x2 = 0x00000014, float32x3 = 0x00000015, float32x4 = 0x00000016, uint32 = 0x00000017, uint32x2 = 0x00000018, uint32x3 = 0x00000019, uint32x4 = 0x0000001A, sint32 = 0x0000001B, sint32x2 = 0x0000001C, sint32x3 = 0x0000001D, sint32x4 = 0x0000001E, }; pub const VertexStepMode = enum(u32) { vertex = 0x00000000, instance = 0x00000001, vertex_buffer_not_used = 0x00000002, }; pub const BufferUsage = packed struct(u32) { 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, }; pub const ColorWriteMask = packed struct(u32) { red: bool = false, green: bool = false, blue: bool = false, alpha: bool = false, _padding: u28 = 0, pub const all = ColorWriteMask{ .red = true, .green = true, .blue = true, .alpha = true }; }; pub const MapMode = packed struct(u32) { read: bool = false, write: bool = false, _padding: u30 = 0, }; pub const ShaderStage = packed struct(u32) { vertex: bool = false, fragment: bool = false, compute: bool = false, _padding: u29 = 0, }; pub const TextureUsage = packed struct(u32) { copy_src: bool = false, copy_dst: bool = false, texture_binding: bool = false, storage_binding: bool = false, render_attachment: bool = false, transient_attachment: bool = false, _padding: u26 = 0, }; pub const ChainedStruct = extern struct { next: ?*const ChainedStruct, struct_type: StructType, }; pub const ChainedStructOut = extern struct { next: ?*ChainedStructOut, struct_type: StructType, }; pub const AdapterProperties = extern struct { next_in_chain: ?*ChainedStructOut = null, vendor_id: u32, vendor_name: [*:0]const u8, architecture: [*:0]const u8, device_id: u32, name: [*:0]const u8, driver_description: [*:0]const u8, adapter_type: AdapterType, backend_type: BackendType, compatibility_mode: bool, }; pub const BindGroupEntry = extern struct { next_in_chain: ?*const ChainedStruct = null, binding: u32, buffer: ?Buffer = null, offset: u64 = 0, size: u64, sampler: ?Sampler = null, texture_view: ?TextureView = null, }; pub const BindGroupDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, layout: BindGroupLayout, entry_count: usize, entries: ?[*]const BindGroupEntry, }; pub const BufferBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, binding_type: BufferBindingType = .uniform, has_dynamic_offset: bool = false, min_binding_size: u64 = 0, }; pub const SamplerBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, binding_type: SamplerBindingType = .filtering, }; pub const TextureBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, sample_type: TextureSampleType = .float, view_dimension: TextureViewDimension = .tvdim_2d, multisampled: bool = false, }; pub const StorageTextureBindingLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, access: StorageTextureAccess = .write_only, format: TextureFormat, view_dimension: TextureViewDimension = .tvdim_2d, }; pub const BindGroupLayoutEntry = extern struct { next_in_chain: ?*const ChainedStruct = null, binding: u32, visibility: ShaderStage, buffer: BufferBindingLayout = .{ .binding_type = .undef }, sampler: SamplerBindingLayout = .{ .binding_type = .undef }, texture: TextureBindingLayout = .{ .sample_type = .undef }, storage_texture: StorageTextureBindingLayout = .{ .access = .undef, .format = .undef }, }; pub const BindGroupLayoutDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, entry_count: usize, entries: ?[*]const BindGroupLayoutEntry, }; pub const BufferDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, usage: BufferUsage, size: u64, mapped_at_creation: bool = false, }; pub const CommandEncoderDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; pub const ConstantEntry = extern struct { next_in_chain: ?*const ChainedStruct = null, key: [*:0]const u8, value: f64, }; pub const ProgrammableStageDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, module: ShaderModule, entry_point: [*:0]const u8, constant_count: usize = 0, constants: ?[*]const ConstantEntry = null, }; pub const ComputePipelineDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, layout: ?PipelineLayout = null, compute: ProgrammableStageDescriptor, }; pub const ExternalTextureDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, plane0: TextureView, plane1: ?TextureView = null, visible_origin: Origin2D, visible_size: Extent2D, do_yuv_to_rgb_conversion_only: bool, yuv_to_rgb_conversion_matrix: ?[*]const f32, src_transfer_function_parameters: [*]const f32, dst_transfer_function_parameters: [*]const f32, gamut_conversion_matrix: [*]const f32, flip_y: bool, rotation: ExternalTextureRotation, }; pub const PipelineLayoutDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, bind_group_layout_count: usize, bind_group_layouts: ?[*]const BindGroupLayout, }; pub const QuerySetDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, query_type: QueryType, count: u32, pipeline_statistics: ?[*]const PipelineStatisticName, pipeline_statistics_count: usize, }; pub const RenderBundleEncoderDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, color_formats_count: usize, color_formats: ?[*]const TextureFormat, depth_stencil_format: TextureFormat, sample_count: u32, depth_read_only: bool, stencil_read_only: bool, }; pub const VertexAttribute = extern struct { format: VertexFormat, offset: u64, shader_location: u32, }; pub const VertexBufferLayout = extern struct { array_stride: u64, step_mode: VertexStepMode = .vertex, attribute_count: usize, attributes: [*]const VertexAttribute, }; pub const VertexState = extern struct { next_in_chain: ?*const ChainedStruct = null, module: ShaderModule, entry_point: [*:0]const u8, constant_count: usize = 0, constants: ?[*]const ConstantEntry = null, buffer_count: usize = 0, buffers: ?[*]const VertexBufferLayout = null, }; pub const BlendComponent = extern struct { operation: BlendOperation = .add, src_factor: BlendFactor = .one, dst_factor: BlendFactor = .zero, }; pub const BlendState = extern struct { color: BlendComponent, alpha: BlendComponent, }; pub const ColorTargetState = extern struct { next_in_chain: ?*const ChainedStruct = null, format: TextureFormat, blend: ?*const BlendState = null, write_mask: ColorWriteMask = ColorWriteMask.all, }; pub const FragmentState = extern struct { next_in_chain: ?*const ChainedStruct = null, module: ShaderModule, entry_point: [*:0]const u8, constant_count: usize = 0, constants: ?[*]const ConstantEntry = null, target_count: usize = 0, targets: ?[*]const ColorTargetState = null, }; pub const PrimitiveState = extern struct { next_in_chain: ?*const ChainedStruct = null, topology: PrimitiveTopology = .triangle_list, strip_index_format: IndexFormat = .undef, front_face: FrontFace = .ccw, cull_mode: CullMode = .none, }; pub const StencilFaceState = extern struct { compare: CompareFunction = .always, fail_op: StencilOperation = .keep, depth_fail_op: StencilOperation = .keep, pass_op: StencilOperation = .keep, }; pub const DepthStencilState = extern struct { next_in_chain: ?*const ChainedStruct = null, format: TextureFormat, depth_write_enabled: bool = false, depth_compare: CompareFunction = .always, stencil_front: StencilFaceState = .{}, stencil_back: StencilFaceState = .{}, stencil_read_mask: u32 = 0xffff_ffff, stencil_write_mask: u32 = 0xffff_ffff, depth_bias: i32 = 0, depth_bias_slope_scale: f32 = 0.0, depth_bias_clamp: f32 = 0.0, }; pub const MultisampleState = extern struct { next_in_chain: ?*const ChainedStruct = null, count: u32 = 1, mask: u32 = 0xffff_ffff, alpha_to_coverage_enabled: bool = false, }; pub const RenderPipelineDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, layout: ?PipelineLayout = null, vertex: VertexState, primitive: PrimitiveState = .{}, depth_stencil: ?*const DepthStencilState = null, multisample: MultisampleState = .{}, fragment: ?*const FragmentState = null, }; pub const SamplerDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, address_mode_u: AddressMode = .clamp_to_edge, address_mode_v: AddressMode = .clamp_to_edge, address_mode_w: AddressMode = .clamp_to_edge, mag_filter: FilterMode = .nearest, min_filter: FilterMode = .nearest, mipmap_filter: MipmapFilterMode = .nearest, lod_min_clamp: f32 = 0.0, lod_max_clamp: f32 = 32.0, compare: CompareFunction = .undef, max_anisotropy: u16 = 1, }; pub const ShaderModuleDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; pub const ShaderModuleWGSLDescriptor = extern struct { chain: ChainedStruct, code: [*:0]const u8, }; pub const SwapChainDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, usage: TextureUsage, format: TextureFormat, width: u32, height: u32, present_mode: PresentMode, }; pub const Extent2D = extern struct { width: u32, height: u32 = 1, }; pub const Extent3D = extern struct { width: u32, height: u32 = 1, depth_or_array_layers: u32 = 1, }; pub const TextureDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, usage: TextureUsage, dimension: TextureDimension = .tdim_2d, size: Extent3D, format: TextureFormat, mip_level_count: u32 = 1, sample_count: u32 = 1, view_format_count: usize = 0, view_formats: ?[*]const TextureFormat = null, }; pub const Limits = extern struct { max_texture_dimension_1d: u32, max_texture_dimension_2d: u32, max_texture_dimension_3d: u32, max_texture_array_layers: u32, max_bind_groups: u32, max_bind_groups_plus_vertex_buffers: u32, max_bindings_per_bind_group: u32, max_dynamic_uniform_buffers_per_pipeline_layout: u32, max_dynamic_storage_buffers_per_pipeline_layout: u32, max_sampled_textures_per_shader_stage: u32, max_samplers_per_shader_stage: u32, max_storage_buffers_per_shader_stage: u32, max_storage_textures_per_shader_stage: u32, max_uniform_buffers_per_shader_stage: u32, max_uniform_buffer_binding_size: u64, max_storage_buffer_binding_size: u64, min_uniform_buffer_offset_alignment: u32, min_storage_buffer_offset_alignment: u32, max_vertex_buffers: u32, max_buffer_size: u64, max_vertex_attributes: u32, max_vertex_buffer_array_stride: u32, max_inter_stage_shader_components: u32, max_inter_stage_shader_variables: u32, max_color_attachments: u32, max_color_attachment_bytes_per_sample: u32, max_compute_workgroup_storage_size: u32, max_compute_invocations_per_workgroup: u32, max_compute_workgroup_size_x: u32, max_compute_workgroup_size_y: u32, max_compute_workgroup_size_z: u32, max_compute_workgroups_per_dimension: u32, }; pub const RequiredLimits = extern struct { next_in_chain: ?*const ChainedStruct = null, limits: Limits, }; pub const SupportedLimits = extern struct { next_in_chain: ?*ChainedStructOut = null, limits: Limits, }; pub const QueueDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; // Can be chained in InstanceDescriptor // Can be chained in RequestAdapterOptions // Can be chained in DeviceDescriptor pub const DawnTogglesDescriptor = extern struct { chain: ChainedStruct, enabled_toggles_count: usize = 0, enabled_toggles: ?[*]const [*:0]const u8 = null, disabled_toggles_count: usize = 0, disabled_toggles: ?[*]const [*:0]const u8 = null, }; pub const DawnAdapterPropertiesPowerPreference = extern struct { chain: ChainedStructOut, power_preference: PowerPreference, }; pub const DeviceDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, required_features_count: usize = 0, required_features: ?[*]const FeatureName = null, required_limits: ?[*]const RequiredLimits = null, default_queue: QueueDescriptor = .{}, device_lost_callback: ?DeviceLostCallback = null, device_lost_user_data: ?*anyopaque = null, }; pub const SurfaceDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; pub const RequestAdapterOptions = extern struct { next_in_chain: ?*const ChainedStruct = null, compatible_surface: ?Surface = null, power_preference: PowerPreference, backend_type: BackendType = .undef, force_fallback_adapter: bool = false, compatibility_mode: bool = false, }; pub const ComputePassTimestampWrite = extern struct { query_set: QuerySet, query_index: u32, location: ComputePassTimestampLocation, }; pub const RenderPassTimestampWrite = extern struct { query_set: QuerySet, query_index: u32, location: RenderPassTimestampLocation, }; pub const ComputePassDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, timestamp_write_count: usize, timestamp_writes: ?[*]const ComputePassTimestampWrite, }; pub const Color = extern struct { r: f64, g: f64, b: f64, 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 RenderPassDepthStencilAttachment = extern struct { view: TextureView, depth_load_op: LoadOp = .undef, depth_store_op: StoreOp = .undef, depth_clear_value: f32 = 0.0, depth_read_only: bool = false, stencil_load_op: LoadOp = .undef, stencil_store_op: StoreOp = .undef, stencil_clear_value: u32 = 0, stencil_read_only: bool = false, }; pub const RenderPassDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, color_attachment_count: usize, color_attachments: ?[*]const RenderPassColorAttachment, depth_stencil_attachment: ?*const RenderPassDepthStencilAttachment = null, occlusion_query_set: ?QuerySet = null, timestamp_write_count: usize = 0, timestamp_writes: ?[*]const RenderPassTimestampWrite = null, }; pub const TextureDataLayout = extern struct { next_in_chain: ?*const ChainedStruct = null, offset: u64 = 0, bytes_per_row: u32, rows_per_image: u32, }; pub const Origin2D = extern struct { x: u32 = 0, y: u32 = 0, }; pub const Origin3D = extern struct { x: u32 = 0, y: u32 = 0, z: u32 = 0, }; pub const ImageCopyBuffer = extern struct { next_in_chain: ?*const ChainedStruct = null, layout: TextureDataLayout, buffer: Buffer, }; pub const ImageCopyTexture = extern struct { next_in_chain: ?*const ChainedStruct = null, texture: Texture, mip_level: u32 = 0, origin: Origin3D = .{}, aspect: TextureAspect = .all, }; pub const ImageCopyExternalTexture = extern struct { next_in_chain: ?*const ChainedStruct = null, external_texture: ExternalTexture, origin: Origin3D, natural_size: Extent2D, }; pub const CommandBufferDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; pub const CopyTextureForBrowserOptions = extern struct { next_in_chain: ?*const ChainedStruct = null, flip_y: bool, needs_color_space_conversion: bool, src_alpha_mode: AlphaMode, src_transfer_function_parameters: ?[*]const f32, conversion_matrix: ?[*]const f32, dst_transfer_function_parameters: ?[*]const f32, dst_alpha_mode: AlphaMode, internal_usage: bool, }; pub const TextureViewDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, format: TextureFormat = .undef, dimension: TextureViewDimension = .undef, base_mip_level: u32 = 0, mip_level_count: u32 = 0xffff_ffff, base_array_layer: u32 = 0, array_layer_count: u32 = 0xffff_ffff, aspect: TextureAspect = .all, }; pub const CompilationMessage = extern struct { next_in_chain: ?*const ChainedStruct = null, message: ?[*:0]const u8 = null, message_type: CompilationMessageType, line_num: u64, line_pos: u64, offset: u64, length: u64, utf16_line_pos: u64, utf16_offset: u64, utf16_length: u64, }; pub const CompilationInfo = extern struct { next_in_chain: ?*const ChainedStruct = null, message_count: usize, messages: ?[*]const CompilationMessage, }; pub const RenderBundleDescriptor = extern struct { next_in_chain: ?*const ChainedStruct = null, label: ?[*:0]const u8 = null, }; pub const CreateComputePipelineAsyncCallback = *const fn ( status: CreatePipelineAsyncStatus, pipeline: ComputePipeline, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const CreateRenderPipelineAsyncCallback = *const fn ( status: CreatePipelineAsyncStatus, pipeline: RenderPipeline, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const ErrorCallback = *const fn ( err_type: ErrorType, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const LoggingCallback = *const fn ( log_type: LoggingType, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const DeviceLostCallback = *const fn ( reason: DeviceLostReason, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const RequestAdapterCallback = *const fn ( status: RequestAdapterStatus, adapter: Adapter, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const RequestDeviceCallback = *const fn ( status: RequestDeviceStatus, device: Device, message: ?[*:0]const u8, userdata: ?*anyopaque, ) callconv(.C) void; pub const BufferMapCallback = *const fn ( status: BufferMapAsyncStatus, userdata: ?*anyopaque, ) callconv(.C) void; pub const QueueWorkDoneCallback = *const fn ( status: QueueWorkDoneStatus, userdata: ?*anyopaque, ) callconv(.C) void; pub const CompilationInfoCallback = *const fn ( status: CompilationInfoRequestStatus, userdata: ?*anyopaque, ) callconv(.C) void; pub const Adapter = *opaque { pub fn createDevice(adapter: Adapter, descriptor: DeviceDescriptor) Device { return wgpuAdapterCreateDevice(adapter, &descriptor); } extern fn wgpuAdapterCreateDevice(adapter: Adapter, descriptor: *const DeviceDescriptor) Device; pub fn enumerateFeatures(adapter: Adapter, features: ?[*]FeatureName) usize { return wgpuAdapterEnumerateFeatures(adapter, features); } extern fn wgpuAdapterEnumerateFeatures(adapter: Adapter, features: ?[*]FeatureName) usize; pub fn getLimits(adapter: Adapter, limits: *SupportedLimits) bool { return wgpuAdapterGetLimits(adapter, limits); } extern fn wgpuAdapterGetLimits(adapter: Adapter, limits: *SupportedLimits) bool; pub fn getProperties(adapter: Adapter, properties: *AdapterProperties) void { wgpuAdapterGetProperties(adapter, properties); } extern fn wgpuAdapterGetProperties(adapter: Adapter, properties: *AdapterProperties) void; pub fn hasFeature(adapter: Adapter, feature: FeatureName) bool { return wgpuAdapterHasFeature(adapter, feature); } extern fn wgpuAdapterHasFeature(adapter: Adapter, feature: FeatureName) bool; pub fn requestDevice( adapter: Adapter, descriptor: DeviceDescriptor, callback: RequestDeviceCallback, userdata: ?*anyopaque, ) void { wgpuAdapterRequestDevice(adapter, &descriptor, callback, userdata); } extern fn wgpuAdapterRequestDevice( adapter: Adapter, descriptor: *const DeviceDescriptor, callback: RequestDeviceCallback, userdata: ?*anyopaque, ) void; pub fn reference(adapter: Adapter) void { wgpuAdapterReference(adapter); } extern fn wgpuAdapterReference(adapter: Adapter) void; pub fn release(adapter: Adapter) void { wgpuAdapterRelease(adapter); } extern fn wgpuAdapterRelease(adapter: Adapter) void; }; pub const BindGroup = *opaque { pub fn setLabel(bind_group: BindGroup, label: ?[*:0]const u8) void { wgpuBindGroupSetLabel(bind_group, label); } extern fn wgpuBindGroupSetLabel(bind_group: BindGroup, label: ?[*:0]const u8) void; pub fn reference(bind_group: BindGroup) void { wgpuBindGroupReference(bind_group); } extern fn wgpuBindGroupReference(bind_group: BindGroup) void; pub fn release(bind_group: BindGroup) void { wgpuBindGroupRelease(bind_group); } extern fn wgpuBindGroupRelease(bind_group: BindGroup) void; }; pub const BindGroupLayout = *opaque { pub fn setLabel(bind_group_layout: BindGroupLayout, label: ?[*:0]const u8) void { wgpuBindGroupLayoutSetLabel(bind_group_layout, label); } extern fn wgpuBindGroupLayoutSetLabel(bind_group_layout: BindGroupLayout, label: ?[*:0]const u8) void; pub fn reference(bind_group_layout: BindGroupLayout) void { wgpuBindGroupLayoutReference(bind_group_layout); } extern fn wgpuBindGroupLayoutReference(bind_group_layout: BindGroupLayout) void; pub fn release(bind_group_layout: BindGroupLayout) void { wgpuBindGroupLayoutRelease(bind_group_layout); } extern fn wgpuBindGroupLayoutRelease(bind_group_layout: BindGroupLayout) void; }; pub const Buffer = *opaque { pub fn destroy(buffer: Buffer) void { wgpuBufferDestroy(buffer); } extern fn wgpuBufferDestroy(buffer: Buffer) void; // `offset` has to be a multiple of 8 (otherwise `null` will be returned). // `@sizeOf(T) * len` has to be a multiple of 4 (otherwise `null` will be returned). pub fn getConstMappedRange(buffer: Buffer, comptime T: type, offset: usize, len: usize) ?[]const T { if (len == 0) return null; const ptr = wgpuBufferGetConstMappedRange(buffer, offset, @sizeOf(T) * len); if (ptr == null) return null; return @as([*]const T, @ptrCast(@alignCast(ptr)))[0..len]; } extern fn wgpuBufferGetConstMappedRange(buffer: Buffer, offset: usize, size: usize) ?*const anyopaque; // `offset` has to be a multiple of 8 (otherwise `null` will be returned). // `@sizeOf(T) * len` has to be a multiple of 4 (otherwise `null` will be returned). pub fn getMappedRange(buffer: Buffer, comptime T: type, offset: usize, len: usize) ?[]T { if (len == 0) return null; const ptr = wgpuBufferGetMappedRange(buffer, offset, @sizeOf(T) * len); if (ptr == null) return null; return @as([*]T, @ptrCast(@alignCast(ptr)))[0..len]; } extern fn wgpuBufferGetMappedRange(buffer: Buffer, offset: usize, size: usize) ?*anyopaque; // `offset` has to be a multiple of 8 (Dawn's validation layer will warn). // `size` has to be a multiple of 4 (Dawn's validation layer will warn). // `size == 0` will map entire range (from 'offset' to the end of the buffer). pub fn mapAsync( buffer: Buffer, mode: MapMode, offset: usize, size: usize, callback: BufferMapCallback, userdata: ?*anyopaque, ) void { wgpuBufferMapAsync(buffer, mode, offset, size, callback, userdata); } extern fn wgpuBufferMapAsync( buffer: Buffer, mode: MapMode, offset: usize, size: usize, callback: BufferMapCallback, userdata: ?*anyopaque, ) void; pub fn setLabel(buffer: Buffer, label: ?[*:0]const u8) void { wgpuBufferSetLabel(buffer, label); } extern fn wgpuBufferSetLabel(buffer: Buffer, label: ?[*:0]const u8) void; pub fn unmap(buffer: Buffer) void { wgpuBufferUnmap(buffer); } extern fn wgpuBufferUnmap(buffer: Buffer) void; pub fn reference(buffer: Buffer) void { wgpuBufferReference(buffer); } extern fn wgpuBufferReference(buffer: Buffer) void; pub fn release(buffer: Buffer) void { wgpuBufferRelease(buffer); } extern fn wgpuBufferRelease(buffer: Buffer) void; }; pub const CommandBuffer = *opaque { pub fn setLabel(command_buffer: CommandBuffer, label: ?[*:0]const u8) void { wgpuCommandBufferSetLabel(command_buffer, label); } extern fn wgpuCommandBufferSetLabel(command_buffer: CommandBuffer, label: ?[*:0]const u8) void; pub fn reference(command_buffer: CommandBuffer) void { wgpuCommandBufferReference(command_buffer); } extern fn wgpuCommandBufferReference(command_buffer: CommandBuffer) void; pub fn release(command_buffer: CommandBuffer) void { wgpuCommandBufferRelease(command_buffer); } extern fn wgpuCommandBufferRelease(command_buffer: CommandBuffer) void; }; pub const CommandEncoder = *opaque { pub fn beginComputePass( command_encoder: CommandEncoder, descriptor: ?ComputePassDescriptor, ) ComputePassEncoder { return wgpuCommandEncoderBeginComputePass(command_encoder, if (descriptor) |d| &d else null); } extern fn wgpuCommandEncoderBeginComputePass( command_encoder: CommandEncoder, descriptor: ?*const ComputePassDescriptor, ) ComputePassEncoder; pub fn beginRenderPass( command_encoder: CommandEncoder, descriptor: RenderPassDescriptor, ) RenderPassEncoder { return wgpuCommandEncoderBeginRenderPass(command_encoder, &descriptor); } extern fn wgpuCommandEncoderBeginRenderPass( command_encoder: CommandEncoder, descriptor: *const RenderPassDescriptor, ) RenderPassEncoder; pub fn clearBuffer(command_encoder: CommandEncoder, buffer: Buffer, offset: usize, size: usize) void { wgpuCommandEncoderClearBuffer(command_encoder, buffer, offset, size); } extern fn wgpuCommandEncoderClearBuffer( command_encoder: CommandEncoder, buffer: Buffer, offset: usize, size: usize, ) void; pub fn copyBufferToBuffer( command_encoder: CommandEncoder, source: Buffer, source_offset: usize, destination: Buffer, destination_offset: usize, size: usize, ) void { wgpuCommandEncoderCopyBufferToBuffer( command_encoder, source, source_offset, destination, destination_offset, size, ); } extern fn wgpuCommandEncoderCopyBufferToBuffer( command_encoder: CommandEncoder, source: Buffer, source_offset: usize, destination: Buffer, destination_offset: usize, size: usize, ) void; pub fn copyBufferToTexture( command_encoder: CommandEncoder, source: ImageCopyBuffer, destination: ImageCopyTexture, copy_size: Extent3D, ) void { wgpuCommandEncoderCopyBufferToTexture(command_encoder, &source, &destination, &copy_size); } extern fn wgpuCommandEncoderCopyBufferToTexture( command_encoder: CommandEncoder, source: *const ImageCopyBuffer, destination: *const ImageCopyTexture, copy_size: *const Extent3D, ) void; pub fn copyTextureToBuffer( command_encoder: CommandEncoder, source: ImageCopyTexture, destination: ImageCopyBuffer, copy_size: Extent3D, ) void { wgpuCommandEncoderCopyTextureToBuffer(command_encoder, &source, &destination, &copy_size); } extern fn wgpuCommandEncoderCopyTextureToBuffer( command_encoder: CommandEncoder, source: *const ImageCopyTexture, destination: *const ImageCopyBuffer, copy_size: *const Extent3D, ) void; pub fn copyTextureToTexture( command_encoder: CommandEncoder, source: ImageCopyTexture, destination: ImageCopyTexture, copy_size: Extent3D, ) void { wgpuCommandEncoderCopyTextureToTexture(command_encoder, &source, &destination, &copy_size); } extern fn wgpuCommandEncoderCopyTextureToTexture( command_encoder: CommandEncoder, source: *const ImageCopyTexture, destination: *const ImageCopyTexture, copy_size: *const Extent3D, ) void; pub fn finish(command_encoder: CommandEncoder, descriptor: ?CommandBufferDescriptor) CommandBuffer { return wgpuCommandEncoderFinish(command_encoder, if (descriptor) |d| &d else null); } extern fn wgpuCommandEncoderFinish( command_encoder: CommandEncoder, descriptor: ?*const CommandBufferDescriptor, ) CommandBuffer; pub fn injectValidationError(command_encoder: CommandEncoder, message: [*:0]const u8) void { wgpuCommandEncoderInjectValidationError(command_encoder, message); } extern fn wgpuCommandEncoderInjectValidationError(command_encoder: CommandEncoder, message: [*:0]const u8) void; pub fn insertDebugMarker(command_encoder: CommandEncoder, marker_label: [*:0]const u8) void { wgpuCommandEncoderInsertDebugMarker(command_encoder, marker_label); } extern fn wgpuCommandEncoderInsertDebugMarker(command_encoder: CommandEncoder, marker_label: [*:0]const u8) void; pub fn popDebugGroup(command_encoder: CommandEncoder) void { wgpuCommandEncoderPopDebugGroup(command_encoder); } extern fn wgpuCommandEncoderPopDebugGroup(command_encoder: CommandEncoder) void; pub fn pushDebugGroup(command_encoder: CommandEncoder, group_label: [*:0]const u8) void { wgpuCommandEncoderPushDebugGroup(command_encoder, group_label); } extern fn wgpuCommandEncoderPushDebugGroup(command_encoder: CommandEncoder, group_label: [*:0]const u8) void; pub fn resolveQuerySet( command_encoder: CommandEncoder, query_set: QuerySet, first_query: u32, query_count: u32, destination: Buffer, destination_offset: u64, ) void { wgpuCommandEncoderResolveQuerySet( command_encoder, query_set, first_query, query_count, destination, destination_offset, ); } extern fn wgpuCommandEncoderResolveQuerySet( command_encoder: CommandEncoder, query_set: QuerySet, first_query: u32, query_count: u32, destination: Buffer, destination_offset: u64, ) void; pub fn setLabel(command_encoder: CommandEncoder, label: ?[*:0]const u8) void { wgpuCommandEncoderSetLabel(command_encoder, label); } extern fn wgpuCommandEncoderSetLabel(command_encoder: CommandEncoder, label: ?[*:0]const u8) void; pub fn writeBuffer( command_encoder: CommandEncoder, buffer: Buffer, buffer_offset: u64, comptime T: type, data: []const T, ) void { wgpuCommandEncoderWriteBuffer( command_encoder, buffer, buffer_offset, @as([*]const u8, @ptrCast(data.ptr)), @as(u64, @intCast(data.len)) * @sizeOf(T), ); } extern fn wgpuCommandEncoderWriteBuffer( command_encoder: CommandEncoder, buffer: Buffer, buffer_offset: u64, data: [*]const u8, size: u64, ) void; pub fn writeTimestamp(command_encoder: CommandEncoder, query_set: QuerySet, query_index: u32) void { wgpuCommandEncoderWriteTimestamp(command_encoder, query_set, query_index); } extern fn wgpuCommandEncoderWriteTimestamp( command_encoder: CommandEncoder, query_set: QuerySet, query_index: u32, ) void; pub fn reference(command_encoder: CommandEncoder) void { wgpuCommandEncoderReference(command_encoder); } extern fn wgpuCommandEncoderReference(command_encoder: CommandEncoder) void; pub fn release(command_encoder: CommandEncoder) void { wgpuCommandEncoderRelease(command_encoder); } extern fn wgpuCommandEncoderRelease(command_encoder: CommandEncoder) void; }; pub const ComputePassEncoder = *opaque { pub fn dispatchWorkgroups( compute_pass_encoder: ComputePassEncoder, workgroup_count_x: u32, workgroup_count_y: u32, workgroup_count_z: u32, ) void { wgpuComputePassEncoderDispatchWorkgroups( compute_pass_encoder, workgroup_count_x, workgroup_count_y, workgroup_count_z, ); } extern fn wgpuComputePassEncoderDispatchWorkgroups( compute_pass_encoder: ComputePassEncoder, workgroup_count_x: u32, workgroup_count_y: u32, workgroup_count_z: u32, ) void; pub fn dispatchWorkgroupsIndirect( compute_pass_encoder: ComputePassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void { wgpuComputePassEncoderDispatchWorkgroupsIndirect(compute_pass_encoder, indirect_buffer, indirect_offset); } extern fn wgpuComputePassEncoderDispatchWorkgroupsIndirect( compute_pass_encoder: ComputePassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void; pub fn end(compute_pass_encoder: ComputePassEncoder) void { wgpuComputePassEncoderEnd(compute_pass_encoder); } extern fn wgpuComputePassEncoderEnd(compute_pass_encoder: ComputePassEncoder) void; pub fn insertDebugMarker(compute_pass_encoder: ComputePassEncoder, marker_label: [*:0]const u8) void { wgpuComputePassEncoderInsertDebugMarker(compute_pass_encoder, marker_label); } extern fn wgpuComputePassEncoderInsertDebugMarker( compute_pass_encoder: ComputePassEncoder, marker_label: [*:0]const u8, ) void; pub fn popDebugGroup(compute_pass_encoder: ComputePassEncoder) void { wgpuComputePassEncoderPopDebugGroup(compute_pass_encoder); } extern fn wgpuComputePassEncoderPopDebugGroup(compute_pass_encoder: ComputePassEncoder) void; pub fn pushDebugGroup(compute_pass_encoder: ComputePassEncoder, group_label: [*:0]const u8) void { wgpuComputePassEncoderPushDebugGroup(compute_pass_encoder, group_label); } extern fn wgpuComputePassEncoderPushDebugGroup( compute_pass_encoder: ComputePassEncoder, group_label: [*:0]const u8, ) void; pub fn setBindGroup( compute_pass_encoder: ComputePassEncoder, group_index: u32, bind_group: BindGroup, dynamic_offsets: ?[]const u32, ) void { wgpuComputePassEncoderSetBindGroup( compute_pass_encoder, group_index, bind_group, if (dynamic_offsets) |dynoff| @as(u32, @intCast(dynoff.len)) else 0, if (dynamic_offsets) |dynoff| dynoff.ptr else null, ); } extern fn wgpuComputePassEncoderSetBindGroup( compute_pass_encoder: ComputePassEncoder, group_index: u32, bind_group: BindGroup, dynamic_offset_count: u32, dynamic_offsets: ?[*]const u32, ) void; pub fn setLabel(compute_pass_encoder: ComputePassEncoder, label: ?[*:0]const u8) void { wgpuComputePassEncoderSetLabel(compute_pass_encoder, label); } extern fn wgpuComputePassEncoderSetLabel(compute_pass_encoder: ComputePassEncoder, label: ?[*:0]const u8) void; pub fn setPipeline(compute_pass_encoder: ComputePassEncoder, pipeline: ComputePipeline) void { wgpuComputePassEncoderSetPipeline(compute_pass_encoder, pipeline); } extern fn wgpuComputePassEncoderSetPipeline( compute_pass_encoder: ComputePassEncoder, pipeline: ComputePipeline, ) void; pub fn writeTimestamp( compute_pass_encoder: ComputePassEncoder, query_set: QuerySet, query_index: u32, ) void { wgpuComputePassEncoderWriteTimestamp(compute_pass_encoder, query_set, query_index); } extern fn wgpuComputePassEncoderWriteTimestamp( compute_pass_encoder: ComputePassEncoder, query_set: QuerySet, query_index: u32, ) void; pub fn reference(compute_pass_encoder: ComputePassEncoder) void { wgpuComputePassEncoderReference(compute_pass_encoder); } extern fn wgpuComputePassEncoderReference(compute_pass_encoder: ComputePassEncoder) void; pub fn release(compute_pass_encoder: ComputePassEncoder) void { wgpuComputePassEncoderRelease(compute_pass_encoder); } extern fn wgpuComputePassEncoderRelease(compute_pass_encoder: ComputePassEncoder) void; }; pub const ComputePipeline = *opaque { pub fn getBindGroupLayout(compute_pipeline: ComputePipeline, group_index: u32) BindGroupLayout { return wgpuComputePipelineGetBindGroupLayout(compute_pipeline, group_index); } extern fn wgpuComputePipelineGetBindGroupLayout( compute_pipeline: ComputePipeline, group_index: u32, ) BindGroupLayout; pub fn setLabel(compute_pipeline: ComputePipeline, label: ?[*:0]const u8) void { wgpuComputePipelineSetLabel(compute_pipeline, label); } extern fn wgpuComputePipelineSetLabel(compute_pipeline: ComputePipeline, label: ?[*:0]const u8) void; pub fn reference(compute_pipeline: ComputePipeline) void { wgpuComputePipelineReference(compute_pipeline); } extern fn wgpuComputePipelineReference(compute_pipeline: ComputePipeline) void; pub fn release(compute_pipeline: ComputePipeline) void { wgpuComputePipelineRelease(compute_pipeline); } extern fn wgpuComputePipelineRelease(compute_pipeline: ComputePipeline) void; }; pub const Device = *opaque { pub fn createBindGroup(device: Device, descriptor: BindGroupDescriptor) BindGroup { return wgpuDeviceCreateBindGroup(device, &descriptor); } extern fn wgpuDeviceCreateBindGroup(device: Device, descriptor: *const BindGroupDescriptor) BindGroup; pub fn createBindGroupLayout(device: Device, descriptor: BindGroupLayoutDescriptor) BindGroupLayout { return wgpuDeviceCreateBindGroupLayout(device, &descriptor); } extern fn wgpuDeviceCreateBindGroupLayout( device: Device, descriptor: *const BindGroupLayoutDescriptor, ) BindGroupLayout; pub fn createBuffer(device: Device, descriptor: BufferDescriptor) Buffer { return wgpuDeviceCreateBuffer(device, &descriptor); } extern fn wgpuDeviceCreateBuffer(device: Device, descriptor: *const BufferDescriptor) Buffer; pub fn createCommandEncoder(device: Device, descriptor: ?CommandEncoderDescriptor) CommandEncoder { return wgpuDeviceCreateCommandEncoder(device, if (descriptor) |d| &d else null); } extern fn wgpuDeviceCreateCommandEncoder( device: Device, descriptor: ?*const CommandEncoderDescriptor, ) CommandEncoder; pub fn createComputePipeline(device: Device, descriptor: ComputePipelineDescriptor) ComputePipeline { return wgpuDeviceCreateComputePipeline(device, &descriptor); } extern fn wgpuDeviceCreateComputePipeline( device: Device, descriptor: *const ComputePipelineDescriptor, ) ComputePipeline; pub fn createComputePipelineAsync( device: Device, descriptor: ComputePipelineDescriptor, callback: CreateComputePipelineAsyncCallback, userdata: ?*anyopaque, ) void { wgpuDeviceCreateComputePipelineAsync(device, &descriptor, callback, userdata); } extern fn wgpuDeviceCreateComputePipelineAsync( device: Device, descriptor: *const ComputePipelineDescriptor, callback: CreateComputePipelineAsyncCallback, userdata: ?*anyopaque, ) void; pub fn createErrorBuffer(device: Device) Buffer { return wgpuDeviceCreateErrorBuffer(device); } extern fn wgpuDeviceCreateErrorBuffer(device: Device) Buffer; pub fn createExternalTexture(device: Device, descriptor: ExternalTextureDescriptor) ExternalTexture { return wgpuDeviceCreateExternalTexture(device, &descriptor); } extern fn wgpuDeviceCreateExternalTexture( device: Device, descriptor: *const ExternalTextureDescriptor, ) ExternalTexture; pub fn createPipelineLayout(device: Device, descriptor: PipelineLayoutDescriptor) PipelineLayout { return wgpuDeviceCreatePipelineLayout(device, &descriptor); } extern fn wgpuDeviceCreatePipelineLayout( device: Device, descriptor: *const PipelineLayoutDescriptor, ) PipelineLayout; pub fn createQuerySet(device: Device, descriptor: QuerySetDescriptor) QuerySet { return wgpuDeviceCreateQuerySet(device, &descriptor); } extern fn wgpuDeviceCreateQuerySet(device: Device, descriptor: *const QuerySetDescriptor) QuerySet; pub fn createRenderBundleEncoder( device: Device, descriptor: RenderBundleEncoderDescriptor, ) RenderBundleEncoder { return wgpuDeviceCreateRenderBundleEncoder(device, &descriptor); } extern fn wgpuDeviceCreateRenderBundleEncoder( device: Device, descriptor: *const RenderBundleEncoderDescriptor, ) RenderBundleEncoder; pub fn createRenderPipeline(device: Device, descriptor: RenderPipelineDescriptor) RenderPipeline { return wgpuDeviceCreateRenderPipeline(device, &descriptor); } extern fn wgpuDeviceCreateRenderPipeline( device: Device, descriptor: *const RenderPipelineDescriptor, ) RenderPipeline; pub fn createRenderPipelineAsync( device: Device, descriptor: RenderPipelineDescriptor, callback: CreateRenderPipelineAsyncCallback, userdata: ?*anyopaque, ) void { wgpuDeviceCreateRenderPipelineAsync(device, &descriptor, callback, userdata); } extern fn wgpuDeviceCreateRenderPipelineAsync( device: Device, descriptor: *const RenderPipelineDescriptor, callback: CreateRenderPipelineAsyncCallback, userdata: ?*anyopaque, ) void; pub fn createSampler(device: Device, descriptor: SamplerDescriptor) Sampler { return wgpuDeviceCreateSampler(device, &descriptor); } extern fn wgpuDeviceCreateSampler(device: Device, descriptor: *const SamplerDescriptor) Sampler; pub fn createShaderModule(device: Device, descriptor: ShaderModuleDescriptor) ShaderModule { return wgpuDeviceCreateShaderModule(device, &descriptor); } extern fn wgpuDeviceCreateShaderModule(device: Device, descriptor: *const ShaderModuleDescriptor) ShaderModule; pub fn createSwapChain(device: Device, surface: ?Surface, descriptor: SwapChainDescriptor) SwapChain { return wgpuDeviceCreateSwapChain(device, surface, &descriptor); } extern fn wgpuDeviceCreateSwapChain( device: Device, surface: ?Surface, descriptor: *const SwapChainDescriptor, ) SwapChain; pub fn createTexture(device: Device, descriptor: TextureDescriptor) Texture { return wgpuDeviceCreateTexture(device, &descriptor); } extern fn wgpuDeviceCreateTexture(device: Device, descriptor: *const TextureDescriptor) Texture; pub fn destroy(device: Device) void { wgpuDeviceDestroy(device); } extern fn wgpuDeviceDestroy(device: Device) void; pub fn enumerateFeatures(device: Device, features: ?[*]FeatureName) usize { return wgpuDeviceEnumerateFeatures(device, features); } extern fn wgpuDeviceEnumerateFeatures(device: Device, features: ?[*]FeatureName) usize; pub fn getLimits(device: Device, limits: *SupportedLimits) bool { return wgpuDeviceGetLimits(device, limits); } extern fn wgpuDeviceGetLimits(device: Device, limits: *SupportedLimits) bool; pub fn getQueue(device: Device) Queue { return wgpuDeviceGetQueue(device); } extern fn wgpuDeviceGetQueue(device: Device) Queue; pub fn hasFeature(device: Device, feature: FeatureName) bool { return wgpuDeviceHasFeature(device, feature); } extern fn wgpuDeviceHasFeature(device: Device, feature: FeatureName) bool; pub fn injectError(device: Device, err_type: ErrorType, message: ?[*:0]const u8) void { wgpuDeviceInjectError(device, err_type, message); } extern fn wgpuDeviceInjectError(device: Device, err_type: ErrorType, message: ?[*:0]const u8) void; pub fn forceLoss(device: Device, reason: DeviceLostReason, message: ?[*:0]const u8) void { wgpuDeviceForceLoss(device, reason, message); } extern fn wgpuDeviceForceLoss(device: Device, reason: DeviceLostReason, message: ?[*:0]const u8) void; pub fn getAdapter(device: Device) Adapter { return wgpuDeviceGetAdapter(device); } extern fn wgpuDeviceGetAdapter(device: Device) Adapter; pub fn popErrorScope(device: Device, callback: ErrorCallback, userdata: ?*anyopaque) bool { return wgpuDevicePopErrorScope(device, callback, userdata); } extern fn wgpuDevicePopErrorScope(device: Device, callback: ErrorCallback, userdata: ?*anyopaque) bool; pub fn pushErrorScope(device: Device, filter: ErrorFilter) void { wgpuDevicePushErrorScope(device, filter); } extern fn wgpuDevicePushErrorScope(device: Device, filter: ErrorFilter) void; pub fn setDeviceLostCallback( device: Device, callback: DeviceLostCallback, userdata: ?*anyopaque, ) void { wgpuDeviceSetDeviceLostCallback(device, callback, userdata); } extern fn wgpuDeviceSetDeviceLostCallback( device: Device, callback: DeviceLostCallback, userdata: ?*anyopaque, ) void; pub fn setLabel(device: Device, label: ?[*:0]const u8) void { wgpuDeviceSetLabel(device, label); } extern fn wgpuDeviceSetLabel(device: Device, label: ?[*:0]const u8) void; pub fn setLoggingCallback(device: Device, callback: LoggingCallback, userdata: ?*anyopaque) void { wgpuDeviceSetLoggingCallback(device, callback, userdata); } extern fn wgpuDeviceSetLoggingCallback(device: Device, callback: LoggingCallback, userdata: ?*anyopaque) void; pub fn setUncapturedErrorCallback(device: Device, callback: ErrorCallback, userdata: ?*anyopaque) void { wgpuDeviceSetUncapturedErrorCallback(device, callback, userdata); } extern fn wgpuDeviceSetUncapturedErrorCallback( device: Device, callback: ErrorCallback, userdata: ?*anyopaque, ) void; pub fn tick(device: Device) void { wgpuDeviceTick(device); } extern fn wgpuDeviceTick(device: Device) void; pub fn reference(device: Device) void { wgpuDeviceReference(device); } extern fn wgpuDeviceReference(device: Device) void; pub fn release(device: Device) void { wgpuDeviceRelease(device); } extern fn wgpuDeviceRelease(device: Device) void; }; pub const ExternalTexture = *opaque { pub fn destroy(external_texture: ExternalTexture) void { wgpuExternalTextureDestroy(external_texture); } extern fn wgpuExternalTextureDestroy(external_texture: ExternalTexture) void; pub fn setLabel(external_texture: ExternalTexture, label: ?[*:0]const u8) void { wgpuExternalTextureSetLabel(external_texture, label); } extern fn wgpuExternalTextureSetLabel(external_texture: ExternalTexture, label: ?[*:0]const u8) void; pub fn reference(external_texture: ExternalTexture) void { wgpuExternalTextureReference(external_texture); } extern fn wgpuExternalTextureReference(external_texture: ExternalTexture) void; pub fn release(external_texture: ExternalTexture) void { wgpuExternalTextureRelease(external_texture); } extern fn wgpuExternalTextureRelease(external_texture: ExternalTexture) void; }; pub const Instance = *opaque { pub fn createSurface(instance: Instance, descriptor: SurfaceDescriptor) Surface { return wgpuInstanceCreateSurface(instance, &descriptor); } extern fn wgpuInstanceCreateSurface(instance: Instance, descriptor: *const SurfaceDescriptor) Surface; pub fn requestAdapter( instance: Instance, options: RequestAdapterOptions, callback: RequestAdapterCallback, userdata: ?*anyopaque, ) void { wgpuInstanceRequestAdapter(instance, &options, callback, userdata); } extern fn wgpuInstanceRequestAdapter( instance: Instance, options: *const RequestAdapterOptions, callback: RequestAdapterCallback, userdata: ?*anyopaque, ) void; pub fn reference(instance: Instance) void { wgpuInstanceReference(instance); } extern fn wgpuInstanceReference(instance: Instance) void; pub fn release(instance: Instance) void { wgpuInstanceRelease(instance); } extern fn wgpuInstanceRelease(instance: Instance) void; }; pub const PipelineLayout = *opaque { pub fn setLabel(pipeline_layout: PipelineLayout, label: ?[*:0]const u8) void { wgpuPipelineLayoutSetLabel(pipeline_layout, label); } extern fn wgpuPipelineLayoutSetLabel(pipeline_layout: PipelineLayout, label: ?[*:0]const u8) void; pub fn reference(pipeline_layout: PipelineLayout) void { wgpuPipelineLayoutReference(pipeline_layout); } extern fn wgpuPipelineLayoutReference(pipeline_layout: PipelineLayout) void; pub fn release(pipeline_layout: PipelineLayout) void { wgpuPipelineLayoutRelease(pipeline_layout); } extern fn wgpuPipelineLayoutRelease(pipeline_layout: PipelineLayout) void; }; pub const QuerySet = *opaque { pub fn destroy(query_set: QuerySet) void { wgpuQuerySetDestroy(query_set); } extern fn wgpuQuerySetDestroy(query_set: QuerySet) void; pub fn setLabel(query_set: QuerySet, label: ?[*:0]const u8) void { wgpuQuerySetSetLabel(query_set, label); } extern fn wgpuQuerySetSetLabel(query_set: QuerySet, label: ?[*:0]const u8) void; pub fn reference(query_set: QuerySet) void { wgpuQuerySetReference(query_set); } extern fn wgpuQuerySetReference(query_set: QuerySet) void; pub fn release(query_set: QuerySet) void { wgpuQuerySetRelease(query_set); } extern fn wgpuQuerySetRelease(query_set: QuerySet) void; }; pub const Queue = *opaque { pub fn copyExternalTextureForBrowser( queue: Queue, source: ImageCopyExternalTexture, destination: ImageCopyTexture, copy_size: Extent3D, options: CopyTextureForBrowserOptions, ) void { wgpuQueueCopyExternalTextureForBrowser(queue, &source, &destination, &copy_size, &options); } extern fn wgpuQueueCopyExternalTextureForBrowser( queue: Queue, source: *const ImageCopyExternalTexture, destination: *const ImageCopyTexture, copy_size: *const Extent3D, options: *const CopyTextureForBrowserOptions, ) void; pub fn copyTextureForBrowser( queue: Queue, source: ImageCopyTexture, destination: ImageCopyTexture, copy_size: Extent3D, options: CopyTextureForBrowserOptions, ) void { wgpuQueueCopyTextureForBrowser(queue, &source, &destination, &copy_size, &options); } extern fn wgpuQueueCopyTextureForBrowser( queue: Queue, source: *const ImageCopyTexture, destination: *const ImageCopyTexture, copy_size: *const Extent3D, options: *const CopyTextureForBrowserOptions, ) void; pub fn onSubmittedWorkDone( queue: Queue, signal_value: u64, callback: QueueWorkDoneCallback, userdata: ?*anyopaque, ) void { wgpuQueueOnSubmittedWorkDone(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); } extern fn wgpuQueueSetLabel(queue: Queue, label: ?[*:0]const u8) void; pub fn submit(queue: Queue, commands: []const CommandBuffer) void { wgpuQueueSubmit(queue, @as(u32, @intCast(commands.len)), commands.ptr); } extern fn wgpuQueueSubmit(queue: Queue, command_count: u32, commands: [*]const CommandBuffer) void; pub fn writeBuffer( queue: Queue, buffer: Buffer, buffer_offset: u64, comptime T: type, data: []const T, ) void { wgpuQueueWriteBuffer( queue, buffer, buffer_offset, @as(*const anyopaque, @ptrCast(data.ptr)), @as(u64, @intCast(data.len)) * @sizeOf(T), ); } extern fn wgpuQueueWriteBuffer( queue: Queue, buffer: Buffer, buffer_offset: u64, data: *const anyopaque, size: u64, ) void; pub fn writeTexture( queue: Queue, destination: ImageCopyTexture, data_layout: TextureDataLayout, write_size: Extent3D, comptime T: type, data: []const T, ) void { wgpuQueueWriteTexture( queue, &destination, @as(*const anyopaque, @ptrCast(data.ptr)), @as(usize, @intCast(data.len)) * @sizeOf(T), &data_layout, &write_size, ); } extern fn wgpuQueueWriteTexture( queue: Queue, destination: *const ImageCopyTexture, data: *const anyopaque, data_size: u64, data_layout: *const TextureDataLayout, write_size: *const Extent3D, ) void; pub fn reference(queue: Queue) void { wgpuQueueReference(queue); } extern fn wgpuQueueReference(queue: Queue) void; pub fn release(queue: Queue) void { wgpuQueueRelease(queue); } extern fn wgpuQueueRelease(queue: Queue) void; }; pub const RenderBundle = *opaque { pub fn reference(render_bundle: RenderBundle) void { wgpuRenderBundleReference(render_bundle); } extern fn wgpuRenderBundleReference(render_bundle: RenderBundle) void; pub fn release(render_bundle: RenderBundle) void { wgpuRenderBundleRelease(render_bundle); } extern fn wgpuRenderBundleRelease(render_bundle: RenderBundle) void; }; pub const RenderBundleEncoder = *opaque { pub fn draw( render_bundle_encoder: RenderBundleEncoder, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32, ) void { wgpuRenderBundleEncoderDraw( render_bundle_encoder, vertex_count, instance_count, first_vertex, first_instance, ); } extern fn wgpuRenderBundleEncoderDraw( render_bundle_encoder: RenderBundleEncoder, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32, ) void; pub fn drawIndexed( render_bundle_encoder: RenderBundleEncoder, index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32, ) void { wgpuRenderBundleEncoderDrawIndexed( render_bundle_encoder, index_count, instance_count, first_index, base_vertex, first_instance, ); } extern fn wgpuRenderBundleEncoderDrawIndexed( render_bundle_encoder: RenderBundleEncoder, index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32, ) void; pub fn drawIndexedIndirect( render_bundle_encoder: RenderBundleEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void { wgpuRenderBundleEncoderDrawIndexedIndirect(render_bundle_encoder, indirect_buffer, indirect_offset); } extern fn wgpuRenderBundleEncoderDrawIndexedIndirect( render_bundle_encoder: RenderBundleEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void; pub fn drawIndirect( render_bundle_encoder: RenderBundleEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void { wgpuRenderBundleEncoderDrawIndirect(render_bundle_encoder, indirect_buffer, indirect_offset); } extern fn wgpuRenderBundleEncoderDrawIndirect( render_bundle_encoder: RenderBundleEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void; pub fn finish( render_bundle_encoder: RenderBundleEncoder, descriptor: RenderBundleDescriptor, ) RenderBundle { return wgpuRenderBundleEncoderFinish(render_bundle_encoder, &descriptor); } extern fn wgpuRenderBundleEncoderFinish( render_bundle_encoder: RenderBundleEncoder, descriptor: *const RenderBundleDescriptor, ) RenderBundle; pub fn insertDebugMarker( render_bundle_encoder: RenderBundleEncoder, marker_label: [*:0]const u8, ) void { wgpuRenderBundleEncoderInsertDebugMarker(render_bundle_encoder, marker_label); } extern fn wgpuRenderBundleEncoderInsertDebugMarker( render_bundle_encoder: RenderBundleEncoder, marker_label: [*:0]const u8, ) void; pub fn popDebugGroup(render_bundle_encoder: RenderBundleEncoder) void { wgpuRenderBundleEncoderPopDebugGroup(render_bundle_encoder); } extern fn wgpuRenderBundleEncoderPopDebugGroup(render_bundle_encoder: RenderBundleEncoder) void; pub fn pushDebugGroup(render_bundle_encoder: RenderBundleEncoder, group_label: [*:0]const u8) void { wgpuRenderBundleEncoderPushDebugGroup(render_bundle_encoder, group_label); } extern fn wgpuRenderBundleEncoderPushDebugGroup( render_bundle_encoder: RenderBundleEncoder, group_label: [*:0]const u8, ) void; pub fn setBindGroup( render_bundle_encoder: RenderBundleEncoder, group_index: u32, group: BindGroup, dynamic_offsets: ?[]const u32, ) void { wgpuRenderBundleEncoderSetBindGroup( render_bundle_encoder, group_index, group, if (dynamic_offsets) |dynoff| @as(u32, @intCast(dynoff.len)) else 0, if (dynamic_offsets) |dynoff| dynoff.ptr else null, ); } extern fn wgpuRenderBundleEncoderSetBindGroup( render_bundle_encoder: RenderBundleEncoder, group_index: u32, group: BindGroup, dynamic_offset_count: u32, dynamic_offsets: ?[*]const u32, ) void; pub fn setIndexBuffer( render_bundle_encoder: RenderBundleEncoder, buffer: Buffer, format: IndexFormat, offset: u64, size: u64, ) void { wgpuRenderBundleEncoderSetIndexBuffer(render_bundle_encoder, buffer, format, offset, size); } extern fn wgpuRenderBundleEncoderSetIndexBuffer( render_bundle_encoder: RenderBundleEncoder, buffer: Buffer, format: IndexFormat, offset: u64, size: u64, ) void; pub fn setLabel(render_bundle_encoder: RenderBundleEncoder, label: ?[*:0]const u8) void { wgpuRenderBundleEncoderSetLabel(render_bundle_encoder, label); } extern fn wgpuRenderBundleEncoderSetLabel( render_bundle_encoder: RenderBundleEncoder, label: ?[*:0]const u8, ) void; pub fn setPipeline(render_bundle_encoder: RenderBundleEncoder, pipeline: RenderPipeline) void { wgpuRenderBundleEncoderSetPipeline(render_bundle_encoder, pipeline); } extern fn wgpuRenderBundleEncoderSetPipeline( render_bundle_encoder: RenderBundleEncoder, pipeline: RenderPipeline, ) void; pub fn setVertexBuffer( render_bundle_encoder: RenderBundleEncoder, slot: u32, buffer: Buffer, offset: u64, size: u64, ) void { wgpuRenderBundleEncoderSetVertexBuffer(render_bundle_encoder, slot, buffer, offset, size); } extern fn wgpuRenderBundleEncoderSetVertexBuffer( render_bundle_encoder: RenderBundleEncoder, slot: u32, buffer: Buffer, offset: u64, size: u64, ) void; pub fn reference(render_bundle_encoder: RenderBundleEncoder) void { wgpuRenderBundleEncoderReference(render_bundle_encoder); } extern fn wgpuRenderBundleEncoderReference(render_bundle_encoder: RenderBundleEncoder) void; pub fn release(render_bundle_encoder: RenderBundleEncoder) void { wgpuRenderBundleEncoderRelease(render_bundle_encoder); } extern fn wgpuRenderBundleEncoderRelease(render_bundle_encoder: RenderBundleEncoder) void; }; pub const RenderPassEncoder = *opaque { pub fn beginOcclusionQuery(render_pass_encoder: RenderPassEncoder, query_index: u32) void { wgpuRenderPassEncoderBeginOcclusionQuery(render_pass_encoder, query_index); } extern fn wgpuRenderPassEncoderBeginOcclusionQuery( render_pass_encoder: RenderPassEncoder, query_index: u32, ) void; pub fn draw( render_pass_encoder: RenderPassEncoder, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32, ) void { wgpuRenderPassEncoderDraw(render_pass_encoder, vertex_count, instance_count, first_vertex, first_instance); } extern fn wgpuRenderPassEncoderDraw( render_pass_encoder: RenderPassEncoder, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32, ) void; pub fn drawIndexed( render_pass_encoder: RenderPassEncoder, index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32, ) void { wgpuRenderPassEncoderDrawIndexed( render_pass_encoder, index_count, instance_count, first_index, base_vertex, first_instance, ); } extern fn wgpuRenderPassEncoderDrawIndexed( render_pass_encoder: RenderPassEncoder, index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32, ) void; pub fn drawIndexedIndirect( render_pass_encoder: RenderPassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void { wgpuRenderPassEncoderDrawIndexedIndirect(render_pass_encoder, indirect_buffer, indirect_offset); } extern fn wgpuRenderPassEncoderDrawIndexedIndirect( render_pass_encoder: RenderPassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void; pub fn drawIndirect( render_pass_encoder: RenderPassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void { wgpuRenderPassEncoderDrawIndirect(render_pass_encoder, indirect_buffer, indirect_offset); } extern fn wgpuRenderPassEncoderDrawIndirect( render_pass_encoder: RenderPassEncoder, indirect_buffer: Buffer, indirect_offset: u64, ) void; pub fn end(render_pass_encoder: RenderPassEncoder) void { wgpuRenderPassEncoderEnd(render_pass_encoder); } extern fn wgpuRenderPassEncoderEnd(render_pass_encoder: RenderPassEncoder) void; pub fn endOcclusionQuery(render_pass_encoder: RenderPassEncoder) void { wgpuRenderPassEncoderEndOcclusionQuery(render_pass_encoder); } extern fn wgpuRenderPassEncoderEndOcclusionQuery(render_pass_encoder: RenderPassEncoder) void; pub fn executeBundles( render_pass_encoder: RenderPassEncoder, bundle_count: u32, bundles: [*]const RenderBundle, ) void { wgpuRenderPassEncoderExecuteBundles(render_pass_encoder, bundle_count, bundles); } extern fn wgpuRenderPassEncoderExecuteBundles( render_pass_encoder: RenderPassEncoder, bundle_count: u32, bundles: [*]const RenderBundle, ) void; pub fn insertDebugMarker(render_pass_encoder: RenderPassEncoder, marker_label: [*:0]const u8) void { wgpuRenderPassEncoderInsertDebugMarker(render_pass_encoder, marker_label); } extern fn wgpuRenderPassEncoderInsertDebugMarker( render_pass_encoder: RenderPassEncoder, marker_label: [*:0]const u8, ) void; pub fn popDebugGroup(render_pass_encoder: RenderPassEncoder) void { wgpuRenderPassEncoderPopDebugGroup(render_pass_encoder); } extern fn wgpuRenderPassEncoderPopDebugGroup(render_pass_encoder: RenderPassEncoder) void; pub fn pushDebugGroup(render_pass_encoder: RenderPassEncoder, group_label: [*:0]const u8) void { wgpuRenderPassEncoderPushDebugGroup(render_pass_encoder, group_label); } extern fn wgpuRenderPassEncoderPushDebugGroup( render_pass_encoder: RenderPassEncoder, group_label: [*:0]const u8, ) void; pub fn setBindGroup( render_pass_encoder: RenderPassEncoder, group_index: u32, group: BindGroup, dynamic_offsets: ?[]const u32, ) void { wgpuRenderPassEncoderSetBindGroup( render_pass_encoder, group_index, group, if (dynamic_offsets) |dynoff| @as(u32, @intCast(dynoff.len)) else 0, if (dynamic_offsets) |dynoff| dynoff.ptr else null, ); } extern fn wgpuRenderPassEncoderSetBindGroup( render_pass_encoder: RenderPassEncoder, group_index: u32, group: BindGroup, dynamic_offset_count: u32, dynamic_offsets: ?[*]const u32, ) void; pub fn setBlendConstant(render_pass_encoder: RenderPassEncoder, color: Color) void { wgpuRenderPassEncoderSetBlendConstant(render_pass_encoder, &color); } extern fn wgpuRenderPassEncoderSetBlendConstant( render_pass_encoder: RenderPassEncoder, color: *const Color, ) void; pub fn setIndexBuffer( render_pass_encoder: RenderPassEncoder, buffer: Buffer, format: IndexFormat, offset: u64, size: u64, ) void { wgpuRenderPassEncoderSetIndexBuffer(render_pass_encoder, buffer, format, offset, size); } extern fn wgpuRenderPassEncoderSetIndexBuffer( render_pass_encoder: RenderPassEncoder, buffer: Buffer, format: IndexFormat, offset: u64, size: u64, ) void; pub fn setLabel(render_pass_encoder: RenderPassEncoder, label: ?[*:0]const u8) void { wgpuRenderPassEncoderSetLabel(render_pass_encoder, label); } extern fn wgpuRenderPassEncoderSetLabel(render_pass_encoder: RenderPassEncoder, label: ?[*:0]const u8) void; pub fn setPipeline(render_pass_encoder: RenderPassEncoder, pipeline: RenderPipeline) void { wgpuRenderPassEncoderSetPipeline(render_pass_encoder, pipeline); } extern fn wgpuRenderPassEncoderSetPipeline( render_pass_encoder: RenderPassEncoder, pipeline: RenderPipeline, ) void; pub fn setScissorRect( render_pass_encoder: RenderPassEncoder, x: u32, y: u32, width: u32, height: u32, ) void { wgpuRenderPassEncoderSetScissorRect(render_pass_encoder, x, y, width, height); } extern fn wgpuRenderPassEncoderSetScissorRect( render_pass_encoder: RenderPassEncoder, x: u32, y: u32, width: u32, height: u32, ) void; pub fn setStencilReference(render_pass_encoder: RenderPassEncoder, ref: u32) void { wgpuRenderPassEncoderSetStencilReference(render_pass_encoder, ref); } extern fn wgpuRenderPassEncoderSetStencilReference(render_pass_encoder: RenderPassEncoder, ref: u32) void; pub fn setVertexBuffer( render_pass_encoder: RenderPassEncoder, slot: u32, buffer: Buffer, offset: u64, size: u64, ) void { wgpuRenderPassEncoderSetVertexBuffer(render_pass_encoder, slot, buffer, offset, size); } extern fn wgpuRenderPassEncoderSetVertexBuffer( render_pass_encoder: RenderPassEncoder, slot: u32, buffer: Buffer, offset: u64, size: u64, ) void; pub fn setViewport( render_pass_encoder: RenderPassEncoder, x: f32, y: f32, width: f32, height: f32, min_depth: f32, max_depth: f32, ) void { wgpuRenderPassEncoderSetViewport(render_pass_encoder, x, y, width, height, min_depth, max_depth); } extern fn wgpuRenderPassEncoderSetViewport( render_pass_encoder: RenderPassEncoder, x: f32, y: f32, width: f32, height: f32, min_depth: f32, max_depth: f32, ) void; pub fn writeTimestamp( render_pass_encoder: RenderPassEncoder, query_set: QuerySet, query_index: u32, ) void { wgpuRenderPassEncoderWriteTimestamp(render_pass_encoder, query_set, query_index); } extern fn wgpuRenderPassEncoderWriteTimestamp( render_pass_encoder: RenderPassEncoder, query_set: QuerySet, query_index: u32, ) void; pub fn reference(render_pass_encoder: RenderPassEncoder) void { wgpuRenderPassEncoderReference(render_pass_encoder); } extern fn wgpuRenderPassEncoderReference(render_pass_encoder: RenderPassEncoder) void; pub fn release(render_pass_encoder: RenderPassEncoder) void { wgpuRenderPassEncoderRelease(render_pass_encoder); } extern fn wgpuRenderPassEncoderRelease(render_pass_encoder: RenderPassEncoder) void; }; pub const RenderPipeline = *opaque { pub fn getBindGroupLayout(render_pipeline: RenderPipeline, group_index: u32) BindGroupLayout { return wgpuRenderPipelineGetBindGroupLayout(render_pipeline, group_index); } extern fn wgpuRenderPipelineGetBindGroupLayout( render_pipeline: RenderPipeline, group_index: u32, ) BindGroupLayout; pub fn setLabel(render_pipeline: RenderPipeline, label: ?[*:0]const u8) void { wgpuRenderPipelineSetLabel(render_pipeline, label); } extern fn wgpuRenderPipelineSetLabel(render_pipeline: RenderPipeline, label: ?[*:0]const u8) void; pub fn reference(render_pipeline: RenderPipeline) void { wgpuRenderPipelineReference(render_pipeline); } extern fn wgpuRenderPipelineReference(render_pipeline: RenderPipeline) void; pub fn release(render_pipeline: RenderPipeline) void { wgpuRenderPipelineRelease(render_pipeline); } extern fn wgpuRenderPipelineRelease(render_pipeline: RenderPipeline) void; }; pub const Sampler = *opaque { pub fn setLabel(sampler: Sampler, label: ?[*:0]const u8) void { wgpuSamplerSetLabel(sampler, label); } extern fn wgpuSamplerSetLabel(sampler: Sampler, label: ?[*:0]const u8) void; pub fn reference(sampler: Sampler) void { wgpuSamplerReference(sampler); } extern fn wgpuSamplerReference(sampler: Sampler) void; pub fn release(sampler: Sampler) void { wgpuSamplerRelease(sampler); } extern fn wgpuSamplerRelease(sampler: Sampler) void; }; pub const ShaderModule = *opaque { pub fn getCompilationInfo( shader_module: ShaderModule, callback: CompilationInfoCallback, userdata: ?*anyopaque, ) void { wgpuShaderModuleGetCompilationInfo(shader_module, callback, userdata); } extern fn wgpuShaderModuleGetCompilationInfo( shader_module: ShaderModule, callback: CompilationInfoCallback, userdata: ?*anyopaque, ) void; pub fn setLabel(shader_module: ShaderModule, label: ?[*:0]const u8) void { wgpuShaderModuleSetLabel(shader_module, label); } extern fn wgpuShaderModuleSetLabel(shader_module: ShaderModule, label: ?[*:0]const u8) void; pub fn reference(shader_module: ShaderModule) void { wgpuShaderModuleReference(shader_module); } extern fn wgpuShaderModuleReference(shader_module: ShaderModule) void; pub fn release(shader_module: ShaderModule) void { wgpuShaderModuleRelease(shader_module); } extern fn wgpuShaderModuleRelease(shader_module: ShaderModule) void; }; pub const Surface = *opaque { pub fn reference(surface: Surface) void { wgpuSurfaceReference(surface); } extern fn wgpuSurfaceReference(surface: Surface) void; pub fn release(surface: Surface) void { wgpuSurfaceRelease(surface); } extern fn wgpuSurfaceRelease(surface: Surface) void; }; pub const SwapChain = *opaque { pub fn configure( swap_chain: SwapChain, format: TextureFormat, allowed_usage: TextureUsage, width: u32, height: u32, ) void { wgpuSwapChainConfigure(swap_chain, format, allowed_usage, width, height); } extern fn wgpuSwapChainConfigure( swap_chain: SwapChain, format: TextureFormat, allowed_usage: TextureUsage, width: u32, height: u32, ) void; pub fn getCurrentTextureView(swap_chain: SwapChain) TextureView { return wgpuSwapChainGetCurrentTextureView(swap_chain); } extern fn wgpuSwapChainGetCurrentTextureView(swap_chain: SwapChain) TextureView; pub fn present(swap_chain: SwapChain) void { wgpuSwapChainPresent(swap_chain); } extern fn wgpuSwapChainPresent(swap_chain: SwapChain) void; pub fn reference(swap_chain: SwapChain) void { wgpuSwapChainReference(swap_chain); } extern fn wgpuSwapChainReference(swap_chain: SwapChain) void; pub fn release(swap_chain: SwapChain) void { wgpuSwapChainRelease(swap_chain); } extern fn wgpuSwapChainRelease(swap_chain: SwapChain) void; }; pub const Texture = *opaque { pub fn createView(texture: Texture, descriptor: TextureViewDescriptor) TextureView { return wgpuTextureCreateView(texture, &descriptor); } extern fn wgpuTextureCreateView(texture: Texture, descriptor: *const TextureViewDescriptor) TextureView; pub fn destroy(texture: Texture) void { wgpuTextureDestroy(texture); } extern fn wgpuTextureDestroy(texture: Texture) void; pub fn setLabel(texture: Texture, label: ?[*:0]const u8) void { wgpuTextureSetLabel(texture, label); } extern fn wgpuTextureSetLabel(texture: Texture, label: ?[*:0]const u8) void; pub fn reference(texture: Texture) void { wgpuTextureReference(texture); } extern fn wgpuTextureReference(texture: Texture) void; pub fn release(texture: Texture) void { wgpuTextureRelease(texture); } extern fn wgpuTextureRelease(texture: Texture) void; }; pub const TextureView = *opaque { pub fn setLabel(texture_view: TextureView, label: ?[*:0]const u8) void { wgpuTextureViewSetLabel(texture_view, label); } extern fn wgpuTextureViewSetLabel(texture_view: TextureView, label: ?[*:0]const u8) void; pub fn reference(texture_view: TextureView) void { wgpuTextureViewReference(texture_view); } extern fn wgpuTextureViewReference(texture_view: TextureView) void; pub fn release(texture_view: TextureView) void { wgpuTextureViewRelease(texture_view); } extern fn wgpuTextureViewRelease(texture_view: TextureView) void; };
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/webgpu/webgpu_glfw.h
// Copyright 2022 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_WEBGPU_WEBGPU_GLFW_H_ #define INCLUDE_WEBGPU_WEBGPU_GLFW_H_ #include <memory> #include "webgpu/webgpu_cpp.h" #if defined(WGPU_GLFW_SHARED_LIBRARY) #if defined(_WIN32) #if defined(WGPU_GLFW_IMPLEMENTATION) #define WGPU_GLFW_EXPORT __declspec(dllexport) #else #define WGPU_GLFW_EXPORT __declspec(dllimport) #endif #else // defined(_WIN32) #if defined(WGPU_GLFW_IMPLEMENTATION) #define WGPU_GLFW_EXPORT __attribute__((visibility("default"))) #else #define WGPU_GLFW_EXPORT #endif #endif // defined(_WIN32) #else // defined(WGPU_GLFW_SHARED_LIBRARY) #define WGPU_GLFW_EXPORT #endif // defined(WGPU_GLFW_SHARED_LIBRARY) struct GLFWwindow; namespace wgpu::glfw { // Does the necessary setup on the GLFWwindow to allow creating a wgpu::Surface with it and // calls `instance.CreateSurface` with the correct descriptor for this window. // Returns a null wgpu::Surface on failure. WGPU_GLFW_EXPORT wgpu::Surface CreateSurfaceForWindow(const wgpu::Instance& instance, GLFWwindow* window); // Use for testing only. Does everything that CreateSurfaceForWindow does except the call to // CreateSurface. Useful to be able to modify the descriptor for testing, or when trying to // avoid using the global proc table. WGPU_GLFW_EXPORT std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptor( GLFWwindow* window); } // namespace wgpu::glfw #endif // INCLUDE_WEBGPU_WEBGPU_GLFW_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/webgpu/webgpu_cpp.h
// Copyright 2022 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_WEBGPU_WEBGPU_CPP_H_ #define INCLUDE_WEBGPU_WEBGPU_CPP_H_ #include "dawn/webgpu_cpp.h" #endif // INCLUDE_WEBGPU_WEBGPU_CPP_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/webgpu/webgpu.h
// Copyright 2022 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_WEBGPU_WEBGPU_H_ #define INCLUDE_WEBGPU_WEBGPU_H_ #include "dawn/webgpu.h" #endif // INCLUDE_WEBGPU_WEBGPU_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/tint/tint.h
// Copyright 2020 The Tint Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_TINT_TINT_H_ #define INCLUDE_TINT_TINT_H_ // Guard for accidental includes to private headers #define CURRENTLY_IN_TINT_PUBLIC_HEADER // TODO(tint:88): When implementing support for an install target, all of these // headers will need to be moved to include/tint/. #include "src/tint/ast/transform/first_index_offset.h" #include "src/tint/ast/transform/renamer.h" #include "src/tint/ast/transform/single_entry_point.h" #include "src/tint/ast/transform/substitute_override.h" #include "src/tint/ast/transform/vertex_pulling.h" #include "src/tint/diagnostic/printer.h" #include "src/tint/inspector/inspector.h" #include "src/tint/reader/reader.h" #include "src/tint/transform/manager.h" #include "src/tint/type/manager.h" #include "src/tint/utils/unicode.h" #include "src/tint/writer/array_length_from_uniform_options.h" #include "src/tint/writer/binding_point.h" #include "src/tint/writer/binding_remapper_options.h" #include "src/tint/writer/external_texture_options.h" #include "src/tint/writer/flatten_bindings.h" #include "src/tint/writer/writer.h" #if TINT_BUILD_SPV_READER #include "src/tint/reader/spirv/parser.h" #endif // TINT_BUILD_SPV_READER #if TINT_BUILD_WGSL_READER #include "src/tint/reader/wgsl/parser.h" #endif // TINT_BUILD_WGSL_READER #if TINT_BUILD_SPV_WRITER #include "src/tint/writer/spirv/generator.h" #endif // TINT_BUILD_SPV_WRITER #if TINT_BUILD_WGSL_WRITER #include "src/tint/writer/wgsl/generator.h" #endif // TINT_BUILD_WGSL_WRITER #if TINT_BUILD_MSL_WRITER #include "src/tint/writer/msl/generator.h" #endif // TINT_BUILD_MSL_WRITER #if TINT_BUILD_HLSL_WRITER #include "src/tint/writer/hlsl/generator.h" #endif // TINT_BUILD_HLSL_WRITER #if TINT_BUILD_GLSL_WRITER #include "src/tint/writer/glsl/generator.h" #endif // TINT_BUILD_GLSL_WRITER namespace tint { /// Initialize initializes the Tint library. Call before using the Tint API. void Initialize(); /// Shutdown uninitializes the Tint library. Call after using the Tint API. void Shutdown(); } // namespace tint #undef CURRENTLY_IN_TINT_PUBLIC_HEADER #endif // INCLUDE_TINT_TINT_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/tint/override_id.h
// Copyright 2022 The Tint Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef SRC_TINT_OVERRIDE_ID_H_ #define SRC_TINT_OVERRIDE_ID_H_ #include <stdint.h> #include <functional> #include "src/tint/reflection.h" namespace tint { /// OverrideId is a numerical identifier for an override variable, unique per program. struct OverrideId { uint16_t value = 0; /// Reflect the fields of this struct so that it can be used by tint::ForeachField() TINT_REFLECT(value); }; /// Equality operator for OverrideId /// @param lhs the OverrideId on the left of the '=' operator /// @param rhs the OverrideId on the right of the '=' operator /// @returns true if `lhs` is equal to `rhs` inline bool operator==(OverrideId lhs, OverrideId rhs) { return lhs.value == rhs.value; } /// Less-than operator for OverrideId /// @param lhs the OverrideId on the left of the '<' operator /// @param rhs the OverrideId on the right of the '<' operator /// @returns true if `lhs` comes before `rhs` inline bool operator<(OverrideId lhs, OverrideId rhs) { return lhs.value < rhs.value; } } // namespace tint namespace std { /// Custom std::hash specialization for tint::OverrideId. template <> class hash<tint::OverrideId> { public: /// @param id the override identifier /// @return the hash of the override identifier inline std::size_t operator()(tint::OverrideId id) const { return std::hash<decltype(tint::OverrideId::value)>()(id.value); } }; } // namespace std #endif // SRC_TINT_OVERRIDE_ID_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/dawn_proc.h
// Copyright 2019 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_DAWN_DAWN_PROC_H_ #define INCLUDE_DAWN_DAWN_PROC_H_ #include "dawn/dawn_proc_table.h" #include "dawn/webgpu.h" #ifdef __cplusplus extern "C" { #endif // Sets the static proctable used by libdawn_proc to implement the Dawn entrypoints. Passing NULL // for `procs` sets up the null proctable that contains only null function pointers. It is the // default value of the proctable. Setting the proctable back to null is good practice when you // are done using libdawn_proc since further usage will cause a segfault instead of calling an // unexpected function. WGPU_EXPORT void dawnProcSetProcs(const DawnProcTable* procs); #ifdef __cplusplus } // extern "C" #endif #endif // INCLUDE_DAWN_DAWN_PROC_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/dawn_thread_dispatch_proc.h
// Copyright 2020 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_DAWN_DAWN_THREAD_DISPATCH_PROC_H_ #define INCLUDE_DAWN_DAWN_THREAD_DISPATCH_PROC_H_ #include "dawn/dawn_proc.h" #ifdef __cplusplus extern "C" { #endif // Call dawnProcSetProcs(&dawnThreadDispatchProcTable) and then use dawnProcSetPerThreadProcs // to set per-thread procs. WGPU_EXPORT extern DawnProcTable dawnThreadDispatchProcTable; WGPU_EXPORT void dawnProcSetPerThreadProcs(const DawnProcTable* procs); #ifdef __cplusplus } // extern "C" #endif #endif // INCLUDE_DAWN_DAWN_THREAD_DISPATCH_PROC_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/dawn_proc_table.h
#ifndef DAWN_DAWN_PROC_TABLE_H_ #define DAWN_DAWN_PROC_TABLE_H_ #include "dawn/webgpu.h" // Note: Often allocated as a static global. Do not add a complex constructor. typedef struct DawnProcTable { WGPUProcCreateInstance createInstance; WGPUProcGetProcAddress getProcAddress; WGPUProcAdapterCreateDevice adapterCreateDevice; WGPUProcAdapterEnumerateFeatures adapterEnumerateFeatures; WGPUProcAdapterGetInstance adapterGetInstance; WGPUProcAdapterGetLimits adapterGetLimits; WGPUProcAdapterGetProperties adapterGetProperties; WGPUProcAdapterHasFeature adapterHasFeature; WGPUProcAdapterRequestDevice adapterRequestDevice; WGPUProcAdapterReference adapterReference; WGPUProcAdapterRelease adapterRelease; WGPUProcBindGroupSetLabel bindGroupSetLabel; WGPUProcBindGroupReference bindGroupReference; WGPUProcBindGroupRelease bindGroupRelease; WGPUProcBindGroupLayoutSetLabel bindGroupLayoutSetLabel; WGPUProcBindGroupLayoutReference bindGroupLayoutReference; WGPUProcBindGroupLayoutRelease bindGroupLayoutRelease; WGPUProcBufferDestroy bufferDestroy; WGPUProcBufferGetConstMappedRange bufferGetConstMappedRange; WGPUProcBufferGetMapState bufferGetMapState; WGPUProcBufferGetMappedRange bufferGetMappedRange; WGPUProcBufferGetSize bufferGetSize; WGPUProcBufferGetUsage bufferGetUsage; WGPUProcBufferMapAsync bufferMapAsync; WGPUProcBufferSetLabel bufferSetLabel; WGPUProcBufferUnmap bufferUnmap; WGPUProcBufferReference bufferReference; WGPUProcBufferRelease bufferRelease; WGPUProcCommandBufferSetLabel commandBufferSetLabel; WGPUProcCommandBufferReference commandBufferReference; WGPUProcCommandBufferRelease commandBufferRelease; WGPUProcCommandEncoderBeginComputePass commandEncoderBeginComputePass; WGPUProcCommandEncoderBeginRenderPass commandEncoderBeginRenderPass; WGPUProcCommandEncoderClearBuffer commandEncoderClearBuffer; WGPUProcCommandEncoderCopyBufferToBuffer commandEncoderCopyBufferToBuffer; WGPUProcCommandEncoderCopyBufferToTexture commandEncoderCopyBufferToTexture; WGPUProcCommandEncoderCopyTextureToBuffer commandEncoderCopyTextureToBuffer; WGPUProcCommandEncoderCopyTextureToTexture commandEncoderCopyTextureToTexture; WGPUProcCommandEncoderCopyTextureToTextureInternal commandEncoderCopyTextureToTextureInternal; WGPUProcCommandEncoderFinish commandEncoderFinish; WGPUProcCommandEncoderInjectValidationError commandEncoderInjectValidationError; WGPUProcCommandEncoderInsertDebugMarker commandEncoderInsertDebugMarker; WGPUProcCommandEncoderPopDebugGroup commandEncoderPopDebugGroup; WGPUProcCommandEncoderPushDebugGroup commandEncoderPushDebugGroup; WGPUProcCommandEncoderResolveQuerySet commandEncoderResolveQuerySet; WGPUProcCommandEncoderSetLabel commandEncoderSetLabel; WGPUProcCommandEncoderWriteBuffer commandEncoderWriteBuffer; WGPUProcCommandEncoderWriteTimestamp commandEncoderWriteTimestamp; WGPUProcCommandEncoderReference commandEncoderReference; WGPUProcCommandEncoderRelease commandEncoderRelease; WGPUProcComputePassEncoderDispatchWorkgroups computePassEncoderDispatchWorkgroups; WGPUProcComputePassEncoderDispatchWorkgroupsIndirect computePassEncoderDispatchWorkgroupsIndirect; WGPUProcComputePassEncoderEnd computePassEncoderEnd; WGPUProcComputePassEncoderInsertDebugMarker computePassEncoderInsertDebugMarker; WGPUProcComputePassEncoderPopDebugGroup computePassEncoderPopDebugGroup; WGPUProcComputePassEncoderPushDebugGroup computePassEncoderPushDebugGroup; WGPUProcComputePassEncoderSetBindGroup computePassEncoderSetBindGroup; WGPUProcComputePassEncoderSetLabel computePassEncoderSetLabel; WGPUProcComputePassEncoderSetPipeline computePassEncoderSetPipeline; WGPUProcComputePassEncoderWriteTimestamp computePassEncoderWriteTimestamp; WGPUProcComputePassEncoderReference computePassEncoderReference; WGPUProcComputePassEncoderRelease computePassEncoderRelease; WGPUProcComputePipelineGetBindGroupLayout computePipelineGetBindGroupLayout; WGPUProcComputePipelineSetLabel computePipelineSetLabel; WGPUProcComputePipelineReference computePipelineReference; WGPUProcComputePipelineRelease computePipelineRelease; WGPUProcDeviceCreateBindGroup deviceCreateBindGroup; WGPUProcDeviceCreateBindGroupLayout deviceCreateBindGroupLayout; WGPUProcDeviceCreateBuffer deviceCreateBuffer; WGPUProcDeviceCreateCommandEncoder deviceCreateCommandEncoder; WGPUProcDeviceCreateComputePipeline deviceCreateComputePipeline; WGPUProcDeviceCreateComputePipelineAsync deviceCreateComputePipelineAsync; WGPUProcDeviceCreateErrorBuffer deviceCreateErrorBuffer; WGPUProcDeviceCreateErrorExternalTexture deviceCreateErrorExternalTexture; WGPUProcDeviceCreateErrorShaderModule deviceCreateErrorShaderModule; WGPUProcDeviceCreateErrorTexture deviceCreateErrorTexture; WGPUProcDeviceCreateExternalTexture deviceCreateExternalTexture; WGPUProcDeviceCreatePipelineLayout deviceCreatePipelineLayout; WGPUProcDeviceCreateQuerySet deviceCreateQuerySet; WGPUProcDeviceCreateRenderBundleEncoder deviceCreateRenderBundleEncoder; WGPUProcDeviceCreateRenderPipeline deviceCreateRenderPipeline; WGPUProcDeviceCreateRenderPipelineAsync deviceCreateRenderPipelineAsync; WGPUProcDeviceCreateSampler deviceCreateSampler; WGPUProcDeviceCreateShaderModule deviceCreateShaderModule; WGPUProcDeviceCreateSwapChain deviceCreateSwapChain; WGPUProcDeviceCreateTexture deviceCreateTexture; WGPUProcDeviceDestroy deviceDestroy; WGPUProcDeviceEnumerateFeatures deviceEnumerateFeatures; WGPUProcDeviceForceLoss deviceForceLoss; WGPUProcDeviceGetAdapter deviceGetAdapter; WGPUProcDeviceGetLimits deviceGetLimits; WGPUProcDeviceGetQueue deviceGetQueue; WGPUProcDeviceGetSupportedSurfaceUsage deviceGetSupportedSurfaceUsage; WGPUProcDeviceHasFeature deviceHasFeature; WGPUProcDeviceInjectError deviceInjectError; WGPUProcDevicePopErrorScope devicePopErrorScope; WGPUProcDevicePushErrorScope devicePushErrorScope; WGPUProcDeviceSetDeviceLostCallback deviceSetDeviceLostCallback; WGPUProcDeviceSetLabel deviceSetLabel; WGPUProcDeviceSetLoggingCallback deviceSetLoggingCallback; WGPUProcDeviceSetUncapturedErrorCallback deviceSetUncapturedErrorCallback; WGPUProcDeviceTick deviceTick; WGPUProcDeviceValidateTextureDescriptor deviceValidateTextureDescriptor; WGPUProcDeviceReference deviceReference; WGPUProcDeviceRelease deviceRelease; WGPUProcExternalTextureDestroy externalTextureDestroy; WGPUProcExternalTextureExpire externalTextureExpire; WGPUProcExternalTextureRefresh externalTextureRefresh; WGPUProcExternalTextureSetLabel externalTextureSetLabel; WGPUProcExternalTextureReference externalTextureReference; WGPUProcExternalTextureRelease externalTextureRelease; WGPUProcInstanceCreateSurface instanceCreateSurface; WGPUProcInstanceProcessEvents instanceProcessEvents; WGPUProcInstanceRequestAdapter instanceRequestAdapter; WGPUProcInstanceReference instanceReference; WGPUProcInstanceRelease instanceRelease; WGPUProcPipelineLayoutSetLabel pipelineLayoutSetLabel; WGPUProcPipelineLayoutReference pipelineLayoutReference; WGPUProcPipelineLayoutRelease pipelineLayoutRelease; WGPUProcQuerySetDestroy querySetDestroy; WGPUProcQuerySetGetCount querySetGetCount; WGPUProcQuerySetGetType querySetGetType; WGPUProcQuerySetSetLabel querySetSetLabel; WGPUProcQuerySetReference querySetReference; WGPUProcQuerySetRelease querySetRelease; WGPUProcQueueCopyExternalTextureForBrowser queueCopyExternalTextureForBrowser; WGPUProcQueueCopyTextureForBrowser queueCopyTextureForBrowser; WGPUProcQueueOnSubmittedWorkDone queueOnSubmittedWorkDone; WGPUProcQueueSetLabel queueSetLabel; WGPUProcQueueSubmit queueSubmit; WGPUProcQueueWriteBuffer queueWriteBuffer; WGPUProcQueueWriteTexture queueWriteTexture; WGPUProcQueueReference queueReference; WGPUProcQueueRelease queueRelease; WGPUProcRenderBundleSetLabel renderBundleSetLabel; WGPUProcRenderBundleReference renderBundleReference; WGPUProcRenderBundleRelease renderBundleRelease; WGPUProcRenderBundleEncoderDraw renderBundleEncoderDraw; WGPUProcRenderBundleEncoderDrawIndexed renderBundleEncoderDrawIndexed; WGPUProcRenderBundleEncoderDrawIndexedIndirect renderBundleEncoderDrawIndexedIndirect; WGPUProcRenderBundleEncoderDrawIndirect renderBundleEncoderDrawIndirect; WGPUProcRenderBundleEncoderFinish renderBundleEncoderFinish; WGPUProcRenderBundleEncoderInsertDebugMarker renderBundleEncoderInsertDebugMarker; WGPUProcRenderBundleEncoderPopDebugGroup renderBundleEncoderPopDebugGroup; WGPUProcRenderBundleEncoderPushDebugGroup renderBundleEncoderPushDebugGroup; WGPUProcRenderBundleEncoderSetBindGroup renderBundleEncoderSetBindGroup; WGPUProcRenderBundleEncoderSetIndexBuffer renderBundleEncoderSetIndexBuffer; WGPUProcRenderBundleEncoderSetLabel renderBundleEncoderSetLabel; WGPUProcRenderBundleEncoderSetPipeline renderBundleEncoderSetPipeline; WGPUProcRenderBundleEncoderSetVertexBuffer renderBundleEncoderSetVertexBuffer; WGPUProcRenderBundleEncoderReference renderBundleEncoderReference; WGPUProcRenderBundleEncoderRelease renderBundleEncoderRelease; WGPUProcRenderPassEncoderBeginOcclusionQuery renderPassEncoderBeginOcclusionQuery; WGPUProcRenderPassEncoderDraw renderPassEncoderDraw; WGPUProcRenderPassEncoderDrawIndexed renderPassEncoderDrawIndexed; WGPUProcRenderPassEncoderDrawIndexedIndirect renderPassEncoderDrawIndexedIndirect; WGPUProcRenderPassEncoderDrawIndirect renderPassEncoderDrawIndirect; WGPUProcRenderPassEncoderEnd renderPassEncoderEnd; WGPUProcRenderPassEncoderEndOcclusionQuery renderPassEncoderEndOcclusionQuery; WGPUProcRenderPassEncoderExecuteBundles renderPassEncoderExecuteBundles; WGPUProcRenderPassEncoderInsertDebugMarker renderPassEncoderInsertDebugMarker; WGPUProcRenderPassEncoderPopDebugGroup renderPassEncoderPopDebugGroup; WGPUProcRenderPassEncoderPushDebugGroup renderPassEncoderPushDebugGroup; WGPUProcRenderPassEncoderSetBindGroup renderPassEncoderSetBindGroup; WGPUProcRenderPassEncoderSetBlendConstant renderPassEncoderSetBlendConstant; WGPUProcRenderPassEncoderSetIndexBuffer renderPassEncoderSetIndexBuffer; WGPUProcRenderPassEncoderSetLabel renderPassEncoderSetLabel; WGPUProcRenderPassEncoderSetPipeline renderPassEncoderSetPipeline; WGPUProcRenderPassEncoderSetScissorRect renderPassEncoderSetScissorRect; WGPUProcRenderPassEncoderSetStencilReference renderPassEncoderSetStencilReference; WGPUProcRenderPassEncoderSetVertexBuffer renderPassEncoderSetVertexBuffer; WGPUProcRenderPassEncoderSetViewport renderPassEncoderSetViewport; WGPUProcRenderPassEncoderWriteTimestamp renderPassEncoderWriteTimestamp; WGPUProcRenderPassEncoderReference renderPassEncoderReference; WGPUProcRenderPassEncoderRelease renderPassEncoderRelease; WGPUProcRenderPipelineGetBindGroupLayout renderPipelineGetBindGroupLayout; WGPUProcRenderPipelineSetLabel renderPipelineSetLabel; WGPUProcRenderPipelineReference renderPipelineReference; WGPUProcRenderPipelineRelease renderPipelineRelease; WGPUProcSamplerSetLabel samplerSetLabel; WGPUProcSamplerReference samplerReference; WGPUProcSamplerRelease samplerRelease; WGPUProcShaderModuleGetCompilationInfo shaderModuleGetCompilationInfo; WGPUProcShaderModuleSetLabel shaderModuleSetLabel; WGPUProcShaderModuleReference shaderModuleReference; WGPUProcShaderModuleRelease shaderModuleRelease; WGPUProcSurfaceReference surfaceReference; WGPUProcSurfaceRelease surfaceRelease; WGPUProcSwapChainGetCurrentTexture swapChainGetCurrentTexture; WGPUProcSwapChainGetCurrentTextureView swapChainGetCurrentTextureView; WGPUProcSwapChainPresent swapChainPresent; WGPUProcSwapChainReference swapChainReference; WGPUProcSwapChainRelease swapChainRelease; WGPUProcTextureCreateView textureCreateView; WGPUProcTextureDestroy textureDestroy; WGPUProcTextureGetDepthOrArrayLayers textureGetDepthOrArrayLayers; WGPUProcTextureGetDimension textureGetDimension; WGPUProcTextureGetFormat textureGetFormat; WGPUProcTextureGetHeight textureGetHeight; WGPUProcTextureGetMipLevelCount textureGetMipLevelCount; WGPUProcTextureGetSampleCount textureGetSampleCount; WGPUProcTextureGetUsage textureGetUsage; WGPUProcTextureGetWidth textureGetWidth; WGPUProcTextureSetLabel textureSetLabel; WGPUProcTextureReference textureReference; WGPUProcTextureRelease textureRelease; WGPUProcTextureViewSetLabel textureViewSetLabel; WGPUProcTextureViewReference textureViewReference; WGPUProcTextureViewRelease textureViewRelease; } DawnProcTable; #endif // DAWN_DAWN_PROC_TABLE_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/webgpu_cpp.h
#ifdef __EMSCRIPTEN__ #error "Do not include this header. Emscripten already provides headers needed for WebGPU." #endif #ifndef WEBGPU_CPP_H_ #define WEBGPU_CPP_H_ #include "dawn/webgpu.h" #include "dawn/webgpu_cpp_chained_struct.h" #include "dawn/EnumClassBitmasks.h" #include <cmath> #include <cstddef> #include <cstdint> namespace wgpu { static constexpr uint32_t kArrayLayerCountUndefined = WGPU_ARRAY_LAYER_COUNT_UNDEFINED; static constexpr uint32_t kCopyStrideUndefined = WGPU_COPY_STRIDE_UNDEFINED; static constexpr uint32_t kLimitU32Undefined = WGPU_LIMIT_U32_UNDEFINED; static constexpr uint64_t kLimitU64Undefined = WGPU_LIMIT_U64_UNDEFINED; static constexpr uint32_t kMipLevelCountUndefined = WGPU_MIP_LEVEL_COUNT_UNDEFINED; static constexpr size_t kWholeMapSize = WGPU_WHOLE_MAP_SIZE; static constexpr uint64_t kWholeSize = WGPU_WHOLE_SIZE; enum class AdapterType : uint32_t { DiscreteGPU = 0x00000000, IntegratedGPU = 0x00000001, CPU = 0x00000002, Unknown = 0x00000003, }; enum class AddressMode : uint32_t { Repeat = 0x00000000, MirrorRepeat = 0x00000001, ClampToEdge = 0x00000002, }; enum class AlphaMode : uint32_t { Premultiplied = 0x00000000, Unpremultiplied = 0x00000001, Opaque = 0x00000002, }; enum class BackendType : uint32_t { Undefined = 0x00000000, Null = 0x00000001, WebGPU = 0x00000002, D3D11 = 0x00000003, D3D12 = 0x00000004, Metal = 0x00000005, Vulkan = 0x00000006, OpenGL = 0x00000007, OpenGLES = 0x00000008, }; enum class BlendFactor : uint32_t { Zero = 0x00000000, One = 0x00000001, Src = 0x00000002, OneMinusSrc = 0x00000003, SrcAlpha = 0x00000004, OneMinusSrcAlpha = 0x00000005, Dst = 0x00000006, OneMinusDst = 0x00000007, DstAlpha = 0x00000008, OneMinusDstAlpha = 0x00000009, SrcAlphaSaturated = 0x0000000A, Constant = 0x0000000B, OneMinusConstant = 0x0000000C, }; enum class BlendOperation : uint32_t { Add = 0x00000000, Subtract = 0x00000001, ReverseSubtract = 0x00000002, Min = 0x00000003, Max = 0x00000004, }; enum class BufferBindingType : uint32_t { Undefined = 0x00000000, Uniform = 0x00000001, Storage = 0x00000002, ReadOnlyStorage = 0x00000003, }; enum class BufferMapAsyncStatus : uint32_t { Success = 0x00000000, ValidationError = 0x00000001, Unknown = 0x00000002, DeviceLost = 0x00000003, DestroyedBeforeCallback = 0x00000004, UnmappedBeforeCallback = 0x00000005, MappingAlreadyPending = 0x00000006, OffsetOutOfRange = 0x00000007, SizeOutOfRange = 0x00000008, }; enum class BufferMapState : uint32_t { Unmapped = 0x00000000, Pending = 0x00000001, Mapped = 0x00000002, }; enum class CompareFunction : uint32_t { Undefined = 0x00000000, Never = 0x00000001, Less = 0x00000002, LessEqual = 0x00000003, Greater = 0x00000004, GreaterEqual = 0x00000005, Equal = 0x00000006, NotEqual = 0x00000007, Always = 0x00000008, }; enum class CompilationInfoRequestStatus : uint32_t { Success = 0x00000000, Error = 0x00000001, DeviceLost = 0x00000002, Unknown = 0x00000003, }; enum class CompilationMessageType : uint32_t { Error = 0x00000000, Warning = 0x00000001, Info = 0x00000002, }; enum class ComputePassTimestampLocation : uint32_t { Beginning = 0x00000000, End = 0x00000001, }; enum class CreatePipelineAsyncStatus : uint32_t { Success = 0x00000000, ValidationError = 0x00000001, InternalError = 0x00000002, DeviceLost = 0x00000003, DeviceDestroyed = 0x00000004, Unknown = 0x00000005, }; enum class CullMode : uint32_t { None = 0x00000000, Front = 0x00000001, Back = 0x00000002, }; enum class DeviceLostReason : uint32_t { Undefined = 0x00000000, Destroyed = 0x00000001, }; enum class ErrorFilter : uint32_t { Validation = 0x00000000, OutOfMemory = 0x00000001, Internal = 0x00000002, }; enum class ErrorType : uint32_t { NoError = 0x00000000, Validation = 0x00000001, OutOfMemory = 0x00000002, Internal = 0x00000003, Unknown = 0x00000004, DeviceLost = 0x00000005, }; enum class ExternalTextureRotation : uint32_t { Rotate0Degrees = 0x00000000, Rotate90Degrees = 0x00000001, Rotate180Degrees = 0x00000002, Rotate270Degrees = 0x00000003, }; enum class FeatureName : uint32_t { Undefined = 0x00000000, DepthClipControl = 0x00000001, Depth32FloatStencil8 = 0x00000002, TimestampQuery = 0x00000003, PipelineStatisticsQuery = 0x00000004, TextureCompressionBC = 0x00000005, TextureCompressionETC2 = 0x00000006, TextureCompressionASTC = 0x00000007, IndirectFirstInstance = 0x00000008, ShaderF16 = 0x00000009, RG11B10UfloatRenderable = 0x0000000A, BGRA8UnormStorage = 0x0000000B, Float32Filterable = 0x0000000C, DawnShaderFloat16 = 0x000003E9, DawnInternalUsages = 0x000003EA, DawnMultiPlanarFormats = 0x000003EB, DawnNative = 0x000003EC, ChromiumExperimentalDp4a = 0x000003ED, TimestampQueryInsidePasses = 0x000003EE, ImplicitDeviceSynchronization = 0x000003EF, SurfaceCapabilities = 0x000003F0, TransientAttachments = 0x000003F1, MSAARenderToSingleSampled = 0x000003F2, }; enum class FilterMode : uint32_t { Nearest = 0x00000000, Linear = 0x00000001, }; enum class FrontFace : uint32_t { CCW = 0x00000000, CW = 0x00000001, }; enum class IndexFormat : uint32_t { Undefined = 0x00000000, Uint16 = 0x00000001, Uint32 = 0x00000002, }; enum class LoadOp : uint32_t { Undefined = 0x00000000, Clear = 0x00000001, Load = 0x00000002, }; enum class LoggingType : uint32_t { Verbose = 0x00000000, Info = 0x00000001, Warning = 0x00000002, Error = 0x00000003, }; enum class MipmapFilterMode : uint32_t { Nearest = 0x00000000, Linear = 0x00000001, }; enum class PipelineStatisticName : uint32_t { VertexShaderInvocations = 0x00000000, ClipperInvocations = 0x00000001, ClipperPrimitivesOut = 0x00000002, FragmentShaderInvocations = 0x00000003, ComputeShaderInvocations = 0x00000004, }; enum class PowerPreference : uint32_t { Undefined = 0x00000000, LowPower = 0x00000001, HighPerformance = 0x00000002, }; enum class PresentMode : uint32_t { Immediate = 0x00000000, Mailbox = 0x00000001, Fifo = 0x00000002, }; enum class PrimitiveTopology : uint32_t { PointList = 0x00000000, LineList = 0x00000001, LineStrip = 0x00000002, TriangleList = 0x00000003, TriangleStrip = 0x00000004, }; enum class QueryType : uint32_t { Occlusion = 0x00000000, PipelineStatistics = 0x00000001, Timestamp = 0x00000002, }; enum class QueueWorkDoneStatus : uint32_t { Success = 0x00000000, Error = 0x00000001, Unknown = 0x00000002, DeviceLost = 0x00000003, }; enum class RenderPassTimestampLocation : uint32_t { Beginning = 0x00000000, End = 0x00000001, }; enum class RequestAdapterStatus : uint32_t { Success = 0x00000000, Unavailable = 0x00000001, Error = 0x00000002, Unknown = 0x00000003, }; enum class RequestDeviceStatus : uint32_t { Success = 0x00000000, Error = 0x00000001, Unknown = 0x00000002, }; enum class SType : uint32_t { Invalid = 0x00000000, SurfaceDescriptorFromMetalLayer = 0x00000001, SurfaceDescriptorFromWindowsHWND = 0x00000002, SurfaceDescriptorFromXlibWindow = 0x00000003, SurfaceDescriptorFromCanvasHTMLSelector = 0x00000004, ShaderModuleSPIRVDescriptor = 0x00000005, ShaderModuleWGSLDescriptor = 0x00000006, PrimitiveDepthClipControl = 0x00000007, SurfaceDescriptorFromWaylandSurface = 0x00000008, SurfaceDescriptorFromAndroidNativeWindow = 0x00000009, SurfaceDescriptorFromWindowsCoreWindow = 0x0000000B, ExternalTextureBindingEntry = 0x0000000C, ExternalTextureBindingLayout = 0x0000000D, SurfaceDescriptorFromWindowsSwapChainPanel = 0x0000000E, RenderPassDescriptorMaxDrawCount = 0x0000000F, DawnTextureInternalUsageDescriptor = 0x000003E8, DawnEncoderInternalUsageDescriptor = 0x000003EB, DawnInstanceDescriptor = 0x000003EC, DawnCacheDeviceDescriptor = 0x000003ED, DawnAdapterPropertiesPowerPreference = 0x000003EE, DawnBufferDescriptorErrorInfoFromWireClient = 0x000003EF, DawnTogglesDescriptor = 0x000003F0, DawnShaderModuleSPIRVOptionsDescriptor = 0x000003F1, RequestAdapterOptionsLUID = 0x000003F2, RequestAdapterOptionsGetGLProc = 0x000003F3, DawnMultisampleStateRenderToSingleSampled = 0x000003F4, DawnRenderPassColorAttachmentRenderToSingleSampled = 0x000003F5, }; enum class SamplerBindingType : uint32_t { Undefined = 0x00000000, Filtering = 0x00000001, NonFiltering = 0x00000002, Comparison = 0x00000003, }; enum class StencilOperation : uint32_t { Keep = 0x00000000, Zero = 0x00000001, Replace = 0x00000002, Invert = 0x00000003, IncrementClamp = 0x00000004, DecrementClamp = 0x00000005, IncrementWrap = 0x00000006, DecrementWrap = 0x00000007, }; enum class StorageTextureAccess : uint32_t { Undefined = 0x00000000, WriteOnly = 0x00000001, }; enum class StoreOp : uint32_t { Undefined = 0x00000000, Store = 0x00000001, Discard = 0x00000002, }; enum class TextureAspect : uint32_t { All = 0x00000000, StencilOnly = 0x00000001, DepthOnly = 0x00000002, Plane0Only = 0x00000003, Plane1Only = 0x00000004, }; enum class TextureDimension : uint32_t { e1D = 0x00000000, e2D = 0x00000001, e3D = 0x00000002, }; enum class TextureFormat : uint32_t { Undefined = 0x00000000, R8Unorm = 0x00000001, R8Snorm = 0x00000002, R8Uint = 0x00000003, R8Sint = 0x00000004, R16Uint = 0x00000005, R16Sint = 0x00000006, R16Float = 0x00000007, RG8Unorm = 0x00000008, RG8Snorm = 0x00000009, RG8Uint = 0x0000000A, RG8Sint = 0x0000000B, R32Float = 0x0000000C, R32Uint = 0x0000000D, R32Sint = 0x0000000E, RG16Uint = 0x0000000F, RG16Sint = 0x00000010, RG16Float = 0x00000011, RGBA8Unorm = 0x00000012, RGBA8UnormSrgb = 0x00000013, RGBA8Snorm = 0x00000014, RGBA8Uint = 0x00000015, RGBA8Sint = 0x00000016, BGRA8Unorm = 0x00000017, BGRA8UnormSrgb = 0x00000018, RGB10A2Unorm = 0x00000019, RG11B10Ufloat = 0x0000001A, RGB9E5Ufloat = 0x0000001B, RG32Float = 0x0000001C, RG32Uint = 0x0000001D, RG32Sint = 0x0000001E, RGBA16Uint = 0x0000001F, RGBA16Sint = 0x00000020, RGBA16Float = 0x00000021, RGBA32Float = 0x00000022, RGBA32Uint = 0x00000023, RGBA32Sint = 0x00000024, Stencil8 = 0x00000025, Depth16Unorm = 0x00000026, Depth24Plus = 0x00000027, Depth24PlusStencil8 = 0x00000028, Depth32Float = 0x00000029, Depth32FloatStencil8 = 0x0000002A, BC1RGBAUnorm = 0x0000002B, BC1RGBAUnormSrgb = 0x0000002C, BC2RGBAUnorm = 0x0000002D, BC2RGBAUnormSrgb = 0x0000002E, BC3RGBAUnorm = 0x0000002F, BC3RGBAUnormSrgb = 0x00000030, BC4RUnorm = 0x00000031, BC4RSnorm = 0x00000032, BC5RGUnorm = 0x00000033, BC5RGSnorm = 0x00000034, BC6HRGBUfloat = 0x00000035, BC6HRGBFloat = 0x00000036, BC7RGBAUnorm = 0x00000037, BC7RGBAUnormSrgb = 0x00000038, ETC2RGB8Unorm = 0x00000039, ETC2RGB8UnormSrgb = 0x0000003A, ETC2RGB8A1Unorm = 0x0000003B, ETC2RGB8A1UnormSrgb = 0x0000003C, ETC2RGBA8Unorm = 0x0000003D, ETC2RGBA8UnormSrgb = 0x0000003E, EACR11Unorm = 0x0000003F, EACR11Snorm = 0x00000040, EACRG11Unorm = 0x00000041, EACRG11Snorm = 0x00000042, ASTC4x4Unorm = 0x00000043, ASTC4x4UnormSrgb = 0x00000044, ASTC5x4Unorm = 0x00000045, ASTC5x4UnormSrgb = 0x00000046, ASTC5x5Unorm = 0x00000047, ASTC5x5UnormSrgb = 0x00000048, ASTC6x5Unorm = 0x00000049, ASTC6x5UnormSrgb = 0x0000004A, ASTC6x6Unorm = 0x0000004B, ASTC6x6UnormSrgb = 0x0000004C, ASTC8x5Unorm = 0x0000004D, ASTC8x5UnormSrgb = 0x0000004E, ASTC8x6Unorm = 0x0000004F, ASTC8x6UnormSrgb = 0x00000050, ASTC8x8Unorm = 0x00000051, ASTC8x8UnormSrgb = 0x00000052, ASTC10x5Unorm = 0x00000053, ASTC10x5UnormSrgb = 0x00000054, ASTC10x6Unorm = 0x00000055, ASTC10x6UnormSrgb = 0x00000056, ASTC10x8Unorm = 0x00000057, ASTC10x8UnormSrgb = 0x00000058, ASTC10x10Unorm = 0x00000059, ASTC10x10UnormSrgb = 0x0000005A, ASTC12x10Unorm = 0x0000005B, ASTC12x10UnormSrgb = 0x0000005C, ASTC12x12Unorm = 0x0000005D, ASTC12x12UnormSrgb = 0x0000005E, R8BG8Biplanar420Unorm = 0x0000005F, }; enum class TextureSampleType : uint32_t { Undefined = 0x00000000, Float = 0x00000001, UnfilterableFloat = 0x00000002, Depth = 0x00000003, Sint = 0x00000004, Uint = 0x00000005, }; enum class TextureViewDimension : uint32_t { Undefined = 0x00000000, e1D = 0x00000001, e2D = 0x00000002, e2DArray = 0x00000003, Cube = 0x00000004, CubeArray = 0x00000005, e3D = 0x00000006, }; enum class VertexFormat : uint32_t { Undefined = 0x00000000, Uint8x2 = 0x00000001, Uint8x4 = 0x00000002, Sint8x2 = 0x00000003, Sint8x4 = 0x00000004, Unorm8x2 = 0x00000005, Unorm8x4 = 0x00000006, Snorm8x2 = 0x00000007, Snorm8x4 = 0x00000008, Uint16x2 = 0x00000009, Uint16x4 = 0x0000000A, Sint16x2 = 0x0000000B, Sint16x4 = 0x0000000C, Unorm16x2 = 0x0000000D, Unorm16x4 = 0x0000000E, Snorm16x2 = 0x0000000F, Snorm16x4 = 0x00000010, Float16x2 = 0x00000011, Float16x4 = 0x00000012, Float32 = 0x00000013, Float32x2 = 0x00000014, Float32x3 = 0x00000015, Float32x4 = 0x00000016, Uint32 = 0x00000017, Uint32x2 = 0x00000018, Uint32x3 = 0x00000019, Uint32x4 = 0x0000001A, Sint32 = 0x0000001B, Sint32x2 = 0x0000001C, Sint32x3 = 0x0000001D, Sint32x4 = 0x0000001E, }; enum class VertexStepMode : uint32_t { Vertex = 0x00000000, Instance = 0x00000001, VertexBufferNotUsed = 0x00000002, }; enum class BufferUsage : uint32_t { None = 0x00000000, MapRead = 0x00000001, MapWrite = 0x00000002, CopySrc = 0x00000004, CopyDst = 0x00000008, Index = 0x00000010, Vertex = 0x00000020, Uniform = 0x00000040, Storage = 0x00000080, Indirect = 0x00000100, QueryResolve = 0x00000200, }; enum class ColorWriteMask : uint32_t { None = 0x00000000, Red = 0x00000001, Green = 0x00000002, Blue = 0x00000004, Alpha = 0x00000008, All = 0x0000000F, }; enum class MapMode : uint32_t { None = 0x00000000, Read = 0x00000001, Write = 0x00000002, }; enum class ShaderStage : uint32_t { None = 0x00000000, Vertex = 0x00000001, Fragment = 0x00000002, Compute = 0x00000004, }; enum class TextureUsage : uint32_t { None = 0x00000000, CopySrc = 0x00000001, CopyDst = 0x00000002, TextureBinding = 0x00000004, StorageBinding = 0x00000008, RenderAttachment = 0x00000010, TransientAttachment = 0x00000020, }; using BufferMapCallback = WGPUBufferMapCallback; using CompilationInfoCallback = WGPUCompilationInfoCallback; using CreateComputePipelineAsyncCallback = WGPUCreateComputePipelineAsyncCallback; using CreateRenderPipelineAsyncCallback = WGPUCreateRenderPipelineAsyncCallback; using DeviceLostCallback = WGPUDeviceLostCallback; using ErrorCallback = WGPUErrorCallback; using LoggingCallback = WGPULoggingCallback; using Proc = WGPUProc; using QueueWorkDoneCallback = WGPUQueueWorkDoneCallback; using RequestAdapterCallback = WGPURequestAdapterCallback; using RequestDeviceCallback = WGPURequestDeviceCallback; class Adapter; class BindGroup; class BindGroupLayout; class Buffer; class CommandBuffer; class CommandEncoder; class ComputePassEncoder; class ComputePipeline; class Device; class ExternalTexture; class Instance; class PipelineLayout; class QuerySet; class Queue; class RenderBundle; class RenderBundleEncoder; class RenderPassEncoder; class RenderPipeline; class Sampler; class ShaderModule; class Surface; class SwapChain; class Texture; class TextureView; struct AdapterProperties; struct BindGroupEntry; struct BlendComponent; struct BufferBindingLayout; struct BufferDescriptor; struct Color; struct CommandBufferDescriptor; struct CommandEncoderDescriptor; struct CompilationMessage; struct ComputePassTimestampWrite; struct ConstantEntry; struct CopyTextureForBrowserOptions; struct DawnAdapterPropertiesPowerPreference; struct DawnBufferDescriptorErrorInfoFromWireClient; struct DawnCacheDeviceDescriptor; struct DawnEncoderInternalUsageDescriptor; struct DawnMultisampleStateRenderToSingleSampled; struct DawnRenderPassColorAttachmentRenderToSingleSampled; struct DawnShaderModuleSPIRVOptionsDescriptor; struct DawnTextureInternalUsageDescriptor; struct DawnTogglesDescriptor; struct Extent2D; struct Extent3D; struct ExternalTextureBindingEntry; struct ExternalTextureBindingLayout; struct InstanceDescriptor; struct Limits; struct MultisampleState; struct Origin2D; struct Origin3D; struct PipelineLayoutDescriptor; struct PrimitiveDepthClipControl; struct PrimitiveState; struct QuerySetDescriptor; struct QueueDescriptor; struct RenderBundleDescriptor; struct RenderBundleEncoderDescriptor; struct RenderPassDepthStencilAttachment; struct RenderPassDescriptorMaxDrawCount; struct RenderPassTimestampWrite; struct RequestAdapterOptions; struct SamplerBindingLayout; struct SamplerDescriptor; struct ShaderModuleDescriptor; struct ShaderModuleSPIRVDescriptor; struct ShaderModuleWGSLDescriptor; struct StencilFaceState; struct StorageTextureBindingLayout; struct SurfaceDescriptor; struct SurfaceDescriptorFromAndroidNativeWindow; struct SurfaceDescriptorFromCanvasHTMLSelector; struct SurfaceDescriptorFromMetalLayer; struct SurfaceDescriptorFromWaylandSurface; struct SurfaceDescriptorFromWindowsCoreWindow; struct SurfaceDescriptorFromWindowsHWND; struct SurfaceDescriptorFromWindowsSwapChainPanel; struct SurfaceDescriptorFromXlibWindow; struct SwapChainDescriptor; struct TextureBindingLayout; struct TextureDataLayout; struct TextureViewDescriptor; struct VertexAttribute; struct BindGroupDescriptor; struct BindGroupLayoutEntry; struct BlendState; struct CompilationInfo; struct ComputePassDescriptor; struct DepthStencilState; struct ExternalTextureDescriptor; struct ImageCopyBuffer; struct ImageCopyExternalTexture; struct ImageCopyTexture; struct ProgrammableStageDescriptor; struct RenderPassColorAttachment; struct RequiredLimits; struct SupportedLimits; struct TextureDescriptor; struct VertexBufferLayout; struct BindGroupLayoutDescriptor; struct ColorTargetState; struct ComputePipelineDescriptor; struct DeviceDescriptor; struct RenderPassDescriptor; struct VertexState; struct FragmentState; struct RenderPipelineDescriptor; template<typename Derived, typename CType> class ObjectBase { public: ObjectBase() = default; ObjectBase(CType handle): mHandle(handle) { if (mHandle) Derived::WGPUReference(mHandle); } ~ObjectBase() { if (mHandle) Derived::WGPURelease(mHandle); } ObjectBase(ObjectBase const& other) : ObjectBase(other.Get()) { } Derived& operator=(ObjectBase const& other) { if (&other != this) { if (mHandle) Derived::WGPURelease(mHandle); mHandle = other.mHandle; if (mHandle) Derived::WGPUReference(mHandle); } return static_cast<Derived&>(*this); } ObjectBase(ObjectBase&& other) { mHandle = other.mHandle; other.mHandle = 0; } Derived& operator=(ObjectBase&& other) { if (&other != this) { if (mHandle) Derived::WGPURelease(mHandle); mHandle = other.mHandle; other.mHandle = 0; } return static_cast<Derived&>(*this); } ObjectBase(std::nullptr_t) {} Derived& operator=(std::nullptr_t) { if (mHandle != nullptr) { Derived::WGPURelease(mHandle); mHandle = nullptr; } return static_cast<Derived&>(*this); } bool operator==(std::nullptr_t) const { return mHandle == nullptr; } bool operator!=(std::nullptr_t) const { return mHandle != nullptr; } explicit operator bool() const { return mHandle != nullptr; } CType Get() const { return mHandle; } // TODO(dawn:1639) Deprecate Release after uses have been removed. CType Release() { CType result = mHandle; mHandle = 0; return result; } CType MoveToCHandle() { CType result = mHandle; mHandle = 0; return result; } static Derived Acquire(CType handle) { Derived result; result.mHandle = handle; return result; } protected: CType mHandle = nullptr; }; class Adapter : public ObjectBase<Adapter, WGPUAdapter> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; Device CreateDevice(DeviceDescriptor const * descriptor = nullptr) const; size_t EnumerateFeatures(FeatureName * features) const; Instance GetInstance() const; bool GetLimits(SupportedLimits * limits) const; void GetProperties(AdapterProperties * properties) const; bool HasFeature(FeatureName feature) const; void RequestDevice(DeviceDescriptor const * descriptor, RequestDeviceCallback callback, void * userdata) const; private: friend ObjectBase<Adapter, WGPUAdapter>; static void WGPUReference(WGPUAdapter handle); static void WGPURelease(WGPUAdapter handle); }; class BindGroup : public ObjectBase<BindGroup, WGPUBindGroup> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<BindGroup, WGPUBindGroup>; static void WGPUReference(WGPUBindGroup handle); static void WGPURelease(WGPUBindGroup handle); }; class BindGroupLayout : public ObjectBase<BindGroupLayout, WGPUBindGroupLayout> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<BindGroupLayout, WGPUBindGroupLayout>; static void WGPUReference(WGPUBindGroupLayout handle); static void WGPURelease(WGPUBindGroupLayout handle); }; class Buffer : public ObjectBase<Buffer, WGPUBuffer> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void Destroy() const; void const * GetConstMappedRange(size_t offset = 0, size_t size = WGPU_WHOLE_MAP_SIZE) const; BufferMapState GetMapState() const; void * GetMappedRange(size_t offset = 0, size_t size = WGPU_WHOLE_MAP_SIZE) const; uint64_t GetSize() const; BufferUsage GetUsage() const; void MapAsync(MapMode mode, size_t offset, size_t size, BufferMapCallback callback, void * userdata) const; void SetLabel(char const * label) const; void Unmap() const; private: friend ObjectBase<Buffer, WGPUBuffer>; static void WGPUReference(WGPUBuffer handle); static void WGPURelease(WGPUBuffer handle); }; class CommandBuffer : public ObjectBase<CommandBuffer, WGPUCommandBuffer> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<CommandBuffer, WGPUCommandBuffer>; static void WGPUReference(WGPUCommandBuffer handle); static void WGPURelease(WGPUCommandBuffer handle); }; class CommandEncoder : public ObjectBase<CommandEncoder, WGPUCommandEncoder> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; ComputePassEncoder BeginComputePass(ComputePassDescriptor const * descriptor = nullptr) const; RenderPassEncoder BeginRenderPass(RenderPassDescriptor const * descriptor) const; void ClearBuffer(Buffer const& buffer, uint64_t offset = 0, uint64_t size = WGPU_WHOLE_SIZE) const; void CopyBufferToBuffer(Buffer const& source, uint64_t sourceOffset, Buffer const& destination, uint64_t destinationOffset, uint64_t size) const; void CopyBufferToTexture(ImageCopyBuffer const * source, ImageCopyTexture const * destination, Extent3D const * copySize) const; void CopyTextureToBuffer(ImageCopyTexture const * source, ImageCopyBuffer const * destination, Extent3D const * copySize) const; void CopyTextureToTexture(ImageCopyTexture const * source, ImageCopyTexture const * destination, Extent3D const * copySize) const; void CopyTextureToTextureInternal(ImageCopyTexture const * source, ImageCopyTexture const * destination, Extent3D const * copySize) const; CommandBuffer Finish(CommandBufferDescriptor const * descriptor = nullptr) const; void InjectValidationError(char const * message) const; void InsertDebugMarker(char const * markerLabel) const; void PopDebugGroup() const; void PushDebugGroup(char const * groupLabel) const; void ResolveQuerySet(QuerySet const& querySet, uint32_t firstQuery, uint32_t queryCount, Buffer const& destination, uint64_t destinationOffset) const; void SetLabel(char const * label) const; void WriteBuffer(Buffer const& buffer, uint64_t bufferOffset, uint8_t const * data, uint64_t size) const; void WriteTimestamp(QuerySet const& querySet, uint32_t queryIndex) const; private: friend ObjectBase<CommandEncoder, WGPUCommandEncoder>; static void WGPUReference(WGPUCommandEncoder handle); static void WGPURelease(WGPUCommandEncoder handle); }; class ComputePassEncoder : public ObjectBase<ComputePassEncoder, WGPUComputePassEncoder> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void DispatchWorkgroups(uint32_t workgroupCountX, uint32_t workgroupCountY = 1, uint32_t workgroupCountZ = 1) const; void DispatchWorkgroupsIndirect(Buffer const& indirectBuffer, uint64_t indirectOffset) const; void End() const; void InsertDebugMarker(char const * markerLabel) const; void PopDebugGroup() const; void PushDebugGroup(char const * groupLabel) const; void SetBindGroup(uint32_t groupIndex, BindGroup const& group, size_t dynamicOffsetCount = 0, uint32_t const * dynamicOffsets = nullptr) const; void SetLabel(char const * label) const; void SetPipeline(ComputePipeline const& pipeline) const; void WriteTimestamp(QuerySet const& querySet, uint32_t queryIndex) const; private: friend ObjectBase<ComputePassEncoder, WGPUComputePassEncoder>; static void WGPUReference(WGPUComputePassEncoder handle); static void WGPURelease(WGPUComputePassEncoder handle); }; class ComputePipeline : public ObjectBase<ComputePipeline, WGPUComputePipeline> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; BindGroupLayout GetBindGroupLayout(uint32_t groupIndex) const; void SetLabel(char const * label) const; private: friend ObjectBase<ComputePipeline, WGPUComputePipeline>; static void WGPUReference(WGPUComputePipeline handle); static void WGPURelease(WGPUComputePipeline handle); }; class Device : public ObjectBase<Device, WGPUDevice> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; BindGroup CreateBindGroup(BindGroupDescriptor const * descriptor) const; BindGroupLayout CreateBindGroupLayout(BindGroupLayoutDescriptor const * descriptor) const; Buffer CreateBuffer(BufferDescriptor const * descriptor) const; CommandEncoder CreateCommandEncoder(CommandEncoderDescriptor const * descriptor = nullptr) const; ComputePipeline CreateComputePipeline(ComputePipelineDescriptor const * descriptor) const; void CreateComputePipelineAsync(ComputePipelineDescriptor const * descriptor, CreateComputePipelineAsyncCallback callback, void * userdata) const; Buffer CreateErrorBuffer(BufferDescriptor const * descriptor) const; ExternalTexture CreateErrorExternalTexture() const; ShaderModule CreateErrorShaderModule(ShaderModuleDescriptor const * descriptor, char const * errorMessage) const; Texture CreateErrorTexture(TextureDescriptor const * descriptor) const; ExternalTexture CreateExternalTexture(ExternalTextureDescriptor const * externalTextureDescriptor) const; PipelineLayout CreatePipelineLayout(PipelineLayoutDescriptor const * descriptor) const; QuerySet CreateQuerySet(QuerySetDescriptor const * descriptor) const; RenderBundleEncoder CreateRenderBundleEncoder(RenderBundleEncoderDescriptor const * descriptor) const; RenderPipeline CreateRenderPipeline(RenderPipelineDescriptor const * descriptor) const; void CreateRenderPipelineAsync(RenderPipelineDescriptor const * descriptor, CreateRenderPipelineAsyncCallback callback, void * userdata) const; Sampler CreateSampler(SamplerDescriptor const * descriptor = nullptr) const; ShaderModule CreateShaderModule(ShaderModuleDescriptor const * descriptor) const; SwapChain CreateSwapChain(Surface const& surface, SwapChainDescriptor const * descriptor) const; Texture CreateTexture(TextureDescriptor const * descriptor) const; void Destroy() const; size_t EnumerateFeatures(FeatureName * features) const; void ForceLoss(DeviceLostReason type, char const * message) const; Adapter GetAdapter() const; bool GetLimits(SupportedLimits * limits) const; Queue GetQueue() const; TextureUsage GetSupportedSurfaceUsage(Surface const& surface) const; bool HasFeature(FeatureName feature) const; void InjectError(ErrorType type, char const * message) const; void PopErrorScope(ErrorCallback callback, void * userdata) const; void PushErrorScope(ErrorFilter filter) const; void SetDeviceLostCallback(DeviceLostCallback callback, void * userdata) const; void SetLabel(char const * label) const; void SetLoggingCallback(LoggingCallback callback, void * userdata) const; void SetUncapturedErrorCallback(ErrorCallback callback, void * userdata) const; void Tick() const; void ValidateTextureDescriptor(TextureDescriptor const * descriptor) const; private: friend ObjectBase<Device, WGPUDevice>; static void WGPUReference(WGPUDevice handle); static void WGPURelease(WGPUDevice handle); }; class ExternalTexture : public ObjectBase<ExternalTexture, WGPUExternalTexture> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void Destroy() const; void Expire() const; void Refresh() const; void SetLabel(char const * label) const; private: friend ObjectBase<ExternalTexture, WGPUExternalTexture>; static void WGPUReference(WGPUExternalTexture handle); static void WGPURelease(WGPUExternalTexture handle); }; class Instance : public ObjectBase<Instance, WGPUInstance> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; Surface CreateSurface(SurfaceDescriptor const * descriptor) const; void ProcessEvents() const; void RequestAdapter(RequestAdapterOptions const * options, RequestAdapterCallback callback, void * userdata) const; private: friend ObjectBase<Instance, WGPUInstance>; static void WGPUReference(WGPUInstance handle); static void WGPURelease(WGPUInstance handle); }; class PipelineLayout : public ObjectBase<PipelineLayout, WGPUPipelineLayout> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<PipelineLayout, WGPUPipelineLayout>; static void WGPUReference(WGPUPipelineLayout handle); static void WGPURelease(WGPUPipelineLayout handle); }; class QuerySet : public ObjectBase<QuerySet, WGPUQuerySet> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void Destroy() const; uint32_t GetCount() const; QueryType GetType() const; void SetLabel(char const * label) const; private: friend ObjectBase<QuerySet, WGPUQuerySet>; static void WGPUReference(WGPUQuerySet handle); static void WGPURelease(WGPUQuerySet handle); }; class Queue : public ObjectBase<Queue, WGPUQueue> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void CopyExternalTextureForBrowser(ImageCopyExternalTexture const * source, ImageCopyTexture const * destination, Extent3D const * copySize, CopyTextureForBrowserOptions const * options) const; void CopyTextureForBrowser(ImageCopyTexture const * source, ImageCopyTexture const * destination, Extent3D const * copySize, CopyTextureForBrowserOptions const * options) const; void OnSubmittedWorkDone(uint64_t signalValue, QueueWorkDoneCallback callback, void * userdata) const; void SetLabel(char const * label) const; void Submit(size_t commandCount, CommandBuffer const * commands) const; void WriteBuffer(Buffer const& buffer, uint64_t bufferOffset, void const * data, size_t size) const; void WriteTexture(ImageCopyTexture const * destination, void const * data, size_t dataSize, TextureDataLayout const * dataLayout, Extent3D const * writeSize) const; private: friend ObjectBase<Queue, WGPUQueue>; static void WGPUReference(WGPUQueue handle); static void WGPURelease(WGPUQueue handle); }; class RenderBundle : public ObjectBase<RenderBundle, WGPURenderBundle> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<RenderBundle, WGPURenderBundle>; static void WGPUReference(WGPURenderBundle handle); static void WGPURelease(WGPURenderBundle handle); }; class RenderBundleEncoder : public ObjectBase<RenderBundleEncoder, WGPURenderBundleEncoder> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t firstVertex = 0, uint32_t firstInstance = 0) const; void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t firstIndex = 0, int32_t baseVertex = 0, uint32_t firstInstance = 0) const; void DrawIndexedIndirect(Buffer const& indirectBuffer, uint64_t indirectOffset) const; void DrawIndirect(Buffer const& indirectBuffer, uint64_t indirectOffset) const; RenderBundle Finish(RenderBundleDescriptor const * descriptor = nullptr) const; void InsertDebugMarker(char const * markerLabel) const; void PopDebugGroup() const; void PushDebugGroup(char const * groupLabel) const; void SetBindGroup(uint32_t groupIndex, BindGroup const& group, size_t dynamicOffsetCount = 0, uint32_t const * dynamicOffsets = nullptr) const; void SetIndexBuffer(Buffer const& buffer, IndexFormat format, uint64_t offset = 0, uint64_t size = WGPU_WHOLE_SIZE) const; void SetLabel(char const * label) const; void SetPipeline(RenderPipeline const& pipeline) const; void SetVertexBuffer(uint32_t slot, Buffer const& buffer, uint64_t offset = 0, uint64_t size = WGPU_WHOLE_SIZE) const; private: friend ObjectBase<RenderBundleEncoder, WGPURenderBundleEncoder>; static void WGPUReference(WGPURenderBundleEncoder handle); static void WGPURelease(WGPURenderBundleEncoder handle); }; class RenderPassEncoder : public ObjectBase<RenderPassEncoder, WGPURenderPassEncoder> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void BeginOcclusionQuery(uint32_t queryIndex) const; void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t firstVertex = 0, uint32_t firstInstance = 0) const; void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t firstIndex = 0, int32_t baseVertex = 0, uint32_t firstInstance = 0) const; void DrawIndexedIndirect(Buffer const& indirectBuffer, uint64_t indirectOffset) const; void DrawIndirect(Buffer const& indirectBuffer, uint64_t indirectOffset) const; void End() const; void EndOcclusionQuery() const; void ExecuteBundles(size_t bundleCount, RenderBundle const * bundles) const; void InsertDebugMarker(char const * markerLabel) const; void PopDebugGroup() const; void PushDebugGroup(char const * groupLabel) const; void SetBindGroup(uint32_t groupIndex, BindGroup const& group, size_t dynamicOffsetCount = 0, uint32_t const * dynamicOffsets = nullptr) const; void SetBlendConstant(Color const * color) const; void SetIndexBuffer(Buffer const& buffer, IndexFormat format, uint64_t offset = 0, uint64_t size = WGPU_WHOLE_SIZE) const; void SetLabel(char const * label) const; void SetPipeline(RenderPipeline const& pipeline) const; void SetScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height) const; void SetStencilReference(uint32_t reference) const; void SetVertexBuffer(uint32_t slot, Buffer const& buffer, uint64_t offset = 0, uint64_t size = WGPU_WHOLE_SIZE) const; void SetViewport(float x, float y, float width, float height, float minDepth, float maxDepth) const; void WriteTimestamp(QuerySet const& querySet, uint32_t queryIndex) const; private: friend ObjectBase<RenderPassEncoder, WGPURenderPassEncoder>; static void WGPUReference(WGPURenderPassEncoder handle); static void WGPURelease(WGPURenderPassEncoder handle); }; class RenderPipeline : public ObjectBase<RenderPipeline, WGPURenderPipeline> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; BindGroupLayout GetBindGroupLayout(uint32_t groupIndex) const; void SetLabel(char const * label) const; private: friend ObjectBase<RenderPipeline, WGPURenderPipeline>; static void WGPUReference(WGPURenderPipeline handle); static void WGPURelease(WGPURenderPipeline handle); }; class Sampler : public ObjectBase<Sampler, WGPUSampler> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<Sampler, WGPUSampler>; static void WGPUReference(WGPUSampler handle); static void WGPURelease(WGPUSampler handle); }; class ShaderModule : public ObjectBase<ShaderModule, WGPUShaderModule> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void GetCompilationInfo(CompilationInfoCallback callback, void * userdata) const; void SetLabel(char const * label) const; private: friend ObjectBase<ShaderModule, WGPUShaderModule>; static void WGPUReference(WGPUShaderModule handle); static void WGPURelease(WGPUShaderModule handle); }; class Surface : public ObjectBase<Surface, WGPUSurface> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; private: friend ObjectBase<Surface, WGPUSurface>; static void WGPUReference(WGPUSurface handle); static void WGPURelease(WGPUSurface handle); }; class SwapChain : public ObjectBase<SwapChain, WGPUSwapChain> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; Texture GetCurrentTexture() const; TextureView GetCurrentTextureView() const; void Present() const; private: friend ObjectBase<SwapChain, WGPUSwapChain>; static void WGPUReference(WGPUSwapChain handle); static void WGPURelease(WGPUSwapChain handle); }; class Texture : public ObjectBase<Texture, WGPUTexture> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; TextureView CreateView(TextureViewDescriptor const * descriptor = nullptr) const; void Destroy() const; uint32_t GetDepthOrArrayLayers() const; TextureDimension GetDimension() const; TextureFormat GetFormat() const; uint32_t GetHeight() const; uint32_t GetMipLevelCount() const; uint32_t GetSampleCount() const; TextureUsage GetUsage() const; uint32_t GetWidth() const; void SetLabel(char const * label) const; private: friend ObjectBase<Texture, WGPUTexture>; static void WGPUReference(WGPUTexture handle); static void WGPURelease(WGPUTexture handle); }; class TextureView : public ObjectBase<TextureView, WGPUTextureView> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; void SetLabel(char const * label) const; private: friend ObjectBase<TextureView, WGPUTextureView>; static void WGPUReference(WGPUTextureView handle); static void WGPURelease(WGPUTextureView handle); }; Instance CreateInstance(InstanceDescriptor const * descriptor = nullptr); Proc GetProcAddress(Device device, char const * procName); struct AdapterProperties { ChainedStructOut * nextInChain = nullptr; uint32_t vendorID; char const * vendorName; char const * architecture; uint32_t deviceID; char const * name; char const * driverDescription; AdapterType adapterType; BackendType backendType; bool compatibilityMode = false; }; struct BindGroupEntry { ChainedStruct const * nextInChain = nullptr; uint32_t binding; Buffer buffer = nullptr; uint64_t offset = 0; uint64_t size = WGPU_WHOLE_SIZE; Sampler sampler = nullptr; TextureView textureView = nullptr; }; struct BlendComponent { BlendOperation operation = BlendOperation::Add; BlendFactor srcFactor = BlendFactor::One; BlendFactor dstFactor = BlendFactor::Zero; }; struct BufferBindingLayout { ChainedStruct const * nextInChain = nullptr; BufferBindingType type = BufferBindingType::Undefined; bool hasDynamicOffset = false; uint64_t minBindingSize = 0; }; struct BufferDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; BufferUsage usage; uint64_t size; bool mappedAtCreation = false; }; struct Color { double r; double g; double b; double a; }; struct CommandBufferDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; struct CommandEncoderDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; struct CompilationMessage { ChainedStruct const * nextInChain = nullptr; char const * message = nullptr; CompilationMessageType type; uint64_t lineNum; uint64_t linePos; uint64_t offset; uint64_t length; uint64_t utf16LinePos; uint64_t utf16Offset; uint64_t utf16Length; }; struct ComputePassTimestampWrite { QuerySet querySet; uint32_t queryIndex; ComputePassTimestampLocation location; }; struct ConstantEntry { ChainedStruct const * nextInChain = nullptr; char const * key; double value; }; struct CopyTextureForBrowserOptions { ChainedStruct const * nextInChain = nullptr; bool flipY = false; bool needsColorSpaceConversion = false; AlphaMode srcAlphaMode = AlphaMode::Unpremultiplied; float const * srcTransferFunctionParameters = nullptr; float const * conversionMatrix = nullptr; float const * dstTransferFunctionParameters = nullptr; AlphaMode dstAlphaMode = AlphaMode::Unpremultiplied; bool internalUsage = false; }; // Can be chained in AdapterProperties struct DawnAdapterPropertiesPowerPreference : ChainedStructOut { DawnAdapterPropertiesPowerPreference() { sType = SType::DawnAdapterPropertiesPowerPreference; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(PowerPreference )); alignas(kFirstMemberAlignment) PowerPreference powerPreference = PowerPreference::Undefined; }; // Can be chained in BufferDescriptor struct DawnBufferDescriptorErrorInfoFromWireClient : ChainedStruct { DawnBufferDescriptorErrorInfoFromWireClient() { sType = SType::DawnBufferDescriptorErrorInfoFromWireClient; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(bool )); alignas(kFirstMemberAlignment) bool outOfMemory = false; }; // Can be chained in DeviceDescriptor struct DawnCacheDeviceDescriptor : ChainedStruct { DawnCacheDeviceDescriptor() { sType = SType::DawnCacheDeviceDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(char const * )); alignas(kFirstMemberAlignment) char const * isolationKey = ""; }; // Can be chained in CommandEncoderDescriptor struct DawnEncoderInternalUsageDescriptor : ChainedStruct { DawnEncoderInternalUsageDescriptor() { sType = SType::DawnEncoderInternalUsageDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(bool )); alignas(kFirstMemberAlignment) bool useInternalUsages = false; }; // Can be chained in MultisampleState struct DawnMultisampleStateRenderToSingleSampled : ChainedStruct { DawnMultisampleStateRenderToSingleSampled() { sType = SType::DawnMultisampleStateRenderToSingleSampled; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(bool )); alignas(kFirstMemberAlignment) bool enabled = false; }; // Can be chained in RenderPassColorAttachment struct DawnRenderPassColorAttachmentRenderToSingleSampled : ChainedStruct { DawnRenderPassColorAttachmentRenderToSingleSampled() { sType = SType::DawnRenderPassColorAttachmentRenderToSingleSampled; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(uint32_t )); alignas(kFirstMemberAlignment) uint32_t implicitSampleCount = 1; }; // Can be chained in ShaderModuleDescriptor struct DawnShaderModuleSPIRVOptionsDescriptor : ChainedStruct { DawnShaderModuleSPIRVOptionsDescriptor() { sType = SType::DawnShaderModuleSPIRVOptionsDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(bool )); alignas(kFirstMemberAlignment) bool allowNonUniformDerivatives = false; }; // Can be chained in TextureDescriptor struct DawnTextureInternalUsageDescriptor : ChainedStruct { DawnTextureInternalUsageDescriptor() { sType = SType::DawnTextureInternalUsageDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(TextureUsage )); alignas(kFirstMemberAlignment) TextureUsage internalUsage = TextureUsage::None; }; // Can be chained in InstanceDescriptor // Can be chained in RequestAdapterOptions // Can be chained in DeviceDescriptor struct DawnTogglesDescriptor : ChainedStruct { DawnTogglesDescriptor() { sType = SType::DawnTogglesDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(size_t )); alignas(kFirstMemberAlignment) size_t enabledTogglesCount = 0; const char* const * enabledToggles; size_t disabledTogglesCount = 0; const char* const * disabledToggles; }; struct Extent2D { uint32_t width = 0; uint32_t height = 1; }; struct Extent3D { uint32_t width; uint32_t height = 1; uint32_t depthOrArrayLayers = 1; }; // Can be chained in BindGroupEntry struct ExternalTextureBindingEntry : ChainedStruct { ExternalTextureBindingEntry() { sType = SType::ExternalTextureBindingEntry; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(ExternalTexture )); alignas(kFirstMemberAlignment) ExternalTexture externalTexture; }; // Can be chained in BindGroupLayoutEntry struct ExternalTextureBindingLayout : ChainedStruct { ExternalTextureBindingLayout() { sType = SType::ExternalTextureBindingLayout; } }; struct InstanceDescriptor { ChainedStruct const * nextInChain = nullptr; }; struct Limits { uint32_t maxTextureDimension1D = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxTextureDimension2D = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxTextureDimension3D = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxTextureArrayLayers = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxBindGroups = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxBindGroupsPlusVertexBuffers = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxBindingsPerBindGroup = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxDynamicUniformBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxDynamicStorageBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxSampledTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxSamplersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxStorageBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxStorageTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxUniformBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED; uint64_t maxUniformBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED; uint64_t maxStorageBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED; uint32_t minUniformBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED; uint32_t minStorageBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxVertexBuffers = WGPU_LIMIT_U32_UNDEFINED; uint64_t maxBufferSize = WGPU_LIMIT_U64_UNDEFINED; uint32_t maxVertexAttributes = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxVertexBufferArrayStride = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxInterStageShaderComponents = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxInterStageShaderVariables = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxColorAttachments = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxColorAttachmentBytesPerSample = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeWorkgroupStorageSize = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeInvocationsPerWorkgroup = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeWorkgroupSizeX = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeWorkgroupSizeY = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeWorkgroupSizeZ = WGPU_LIMIT_U32_UNDEFINED; uint32_t maxComputeWorkgroupsPerDimension = WGPU_LIMIT_U32_UNDEFINED; }; struct MultisampleState { ChainedStruct const * nextInChain = nullptr; uint32_t count = 1; uint32_t mask = 0xFFFFFFFF; bool alphaToCoverageEnabled = false; }; struct Origin2D { uint32_t x = 0; uint32_t y = 0; }; struct Origin3D { uint32_t x = 0; uint32_t y = 0; uint32_t z = 0; }; struct PipelineLayoutDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t bindGroupLayoutCount; BindGroupLayout const * bindGroupLayouts; }; // Can be chained in PrimitiveState struct PrimitiveDepthClipControl : ChainedStruct { PrimitiveDepthClipControl() { sType = SType::PrimitiveDepthClipControl; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(bool )); alignas(kFirstMemberAlignment) bool unclippedDepth = false; }; struct PrimitiveState { ChainedStruct const * nextInChain = nullptr; PrimitiveTopology topology = PrimitiveTopology::TriangleList; IndexFormat stripIndexFormat = IndexFormat::Undefined; FrontFace frontFace = FrontFace::CCW; CullMode cullMode = CullMode::None; }; struct QuerySetDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; QueryType type; uint32_t count; PipelineStatisticName const * pipelineStatistics; size_t pipelineStatisticsCount = 0; }; struct QueueDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; struct RenderBundleDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; struct RenderBundleEncoderDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t colorFormatsCount; TextureFormat const * colorFormats; TextureFormat depthStencilFormat = TextureFormat::Undefined; uint32_t sampleCount = 1; bool depthReadOnly = false; bool stencilReadOnly = false; }; struct RenderPassDepthStencilAttachment { TextureView view; LoadOp depthLoadOp = LoadOp::Undefined; StoreOp depthStoreOp = StoreOp::Undefined; float depthClearValue = NAN; bool depthReadOnly = false; LoadOp stencilLoadOp = LoadOp::Undefined; StoreOp stencilStoreOp = StoreOp::Undefined; uint32_t stencilClearValue = 0; bool stencilReadOnly = false; }; // Can be chained in RenderPassDescriptor struct RenderPassDescriptorMaxDrawCount : ChainedStruct { RenderPassDescriptorMaxDrawCount() { sType = SType::RenderPassDescriptorMaxDrawCount; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(uint64_t )); alignas(kFirstMemberAlignment) uint64_t maxDrawCount = 50000000; }; struct RenderPassTimestampWrite { QuerySet querySet; uint32_t queryIndex; RenderPassTimestampLocation location; }; struct RequestAdapterOptions { ChainedStruct const * nextInChain = nullptr; Surface compatibleSurface = nullptr; PowerPreference powerPreference = PowerPreference::Undefined; BackendType backendType = BackendType::Undefined; bool forceFallbackAdapter = false; bool compatibilityMode = false; }; struct SamplerBindingLayout { ChainedStruct const * nextInChain = nullptr; SamplerBindingType type = SamplerBindingType::Undefined; }; struct SamplerDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; AddressMode addressModeU = AddressMode::ClampToEdge; AddressMode addressModeV = AddressMode::ClampToEdge; AddressMode addressModeW = AddressMode::ClampToEdge; FilterMode magFilter = FilterMode::Nearest; FilterMode minFilter = FilterMode::Nearest; MipmapFilterMode mipmapFilter = MipmapFilterMode::Nearest; float lodMinClamp = 0.0f; float lodMaxClamp = 32.0f; CompareFunction compare = CompareFunction::Undefined; uint16_t maxAnisotropy = 1; }; struct ShaderModuleDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; // Can be chained in ShaderModuleDescriptor struct ShaderModuleSPIRVDescriptor : ChainedStruct { ShaderModuleSPIRVDescriptor() { sType = SType::ShaderModuleSPIRVDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(uint32_t )); alignas(kFirstMemberAlignment) uint32_t codeSize; uint32_t const * code; }; // Can be chained in ShaderModuleDescriptor struct ShaderModuleWGSLDescriptor : ChainedStruct { ShaderModuleWGSLDescriptor() { sType = SType::ShaderModuleWGSLDescriptor; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(char const * )); alignas(kFirstMemberAlignment) char const * code; }; struct StencilFaceState { CompareFunction compare = CompareFunction::Always; StencilOperation failOp = StencilOperation::Keep; StencilOperation depthFailOp = StencilOperation::Keep; StencilOperation passOp = StencilOperation::Keep; }; struct StorageTextureBindingLayout { ChainedStruct const * nextInChain = nullptr; StorageTextureAccess access = StorageTextureAccess::Undefined; TextureFormat format = TextureFormat::Undefined; TextureViewDimension viewDimension = TextureViewDimension::Undefined; }; struct SurfaceDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromAndroidNativeWindow : ChainedStruct { SurfaceDescriptorFromAndroidNativeWindow() { sType = SType::SurfaceDescriptorFromAndroidNativeWindow; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * window; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromCanvasHTMLSelector : ChainedStruct { SurfaceDescriptorFromCanvasHTMLSelector() { sType = SType::SurfaceDescriptorFromCanvasHTMLSelector; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(char const * )); alignas(kFirstMemberAlignment) char const * selector; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromMetalLayer : ChainedStruct { SurfaceDescriptorFromMetalLayer() { sType = SType::SurfaceDescriptorFromMetalLayer; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * layer; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromWaylandSurface : ChainedStruct { SurfaceDescriptorFromWaylandSurface() { sType = SType::SurfaceDescriptorFromWaylandSurface; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * display; void * surface; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromWindowsCoreWindow : ChainedStruct { SurfaceDescriptorFromWindowsCoreWindow() { sType = SType::SurfaceDescriptorFromWindowsCoreWindow; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * coreWindow; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromWindowsHWND : ChainedStruct { SurfaceDescriptorFromWindowsHWND() { sType = SType::SurfaceDescriptorFromWindowsHWND; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * hinstance; void * hwnd; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromWindowsSwapChainPanel : ChainedStruct { SurfaceDescriptorFromWindowsSwapChainPanel() { sType = SType::SurfaceDescriptorFromWindowsSwapChainPanel; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * swapChainPanel; }; // Can be chained in SurfaceDescriptor struct SurfaceDescriptorFromXlibWindow : ChainedStruct { SurfaceDescriptorFromXlibWindow() { sType = SType::SurfaceDescriptorFromXlibWindow; } static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct), alignof(void * )); alignas(kFirstMemberAlignment) void * display; uint32_t window; }; struct SwapChainDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; TextureUsage usage; TextureFormat format; uint32_t width; uint32_t height; PresentMode presentMode; }; struct TextureBindingLayout { ChainedStruct const * nextInChain = nullptr; TextureSampleType sampleType = TextureSampleType::Undefined; TextureViewDimension viewDimension = TextureViewDimension::Undefined; bool multisampled = false; }; struct TextureDataLayout { ChainedStruct const * nextInChain = nullptr; uint64_t offset = 0; uint32_t bytesPerRow = WGPU_COPY_STRIDE_UNDEFINED; uint32_t rowsPerImage = WGPU_COPY_STRIDE_UNDEFINED; }; struct TextureViewDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; TextureFormat format = TextureFormat::Undefined; TextureViewDimension dimension = TextureViewDimension::Undefined; uint32_t baseMipLevel = 0; uint32_t mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED; uint32_t baseArrayLayer = 0; uint32_t arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED; TextureAspect aspect = TextureAspect::All; }; struct VertexAttribute { VertexFormat format; uint64_t offset; uint32_t shaderLocation; }; struct BindGroupDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; BindGroupLayout layout; size_t entryCount; BindGroupEntry const * entries; }; struct BindGroupLayoutEntry { ChainedStruct const * nextInChain = nullptr; uint32_t binding; ShaderStage visibility; BufferBindingLayout buffer; SamplerBindingLayout sampler; TextureBindingLayout texture; StorageTextureBindingLayout storageTexture; }; struct BlendState { BlendComponent color; BlendComponent alpha; }; struct CompilationInfo { ChainedStruct const * nextInChain = nullptr; size_t messageCount; CompilationMessage const * messages; }; struct ComputePassDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t timestampWriteCount = 0; ComputePassTimestampWrite const * timestampWrites; }; struct DepthStencilState { ChainedStruct const * nextInChain = nullptr; TextureFormat format; bool depthWriteEnabled; CompareFunction depthCompare; StencilFaceState stencilFront; StencilFaceState stencilBack; uint32_t stencilReadMask = 0xFFFFFFFF; uint32_t stencilWriteMask = 0xFFFFFFFF; int32_t depthBias = 0; float depthBiasSlopeScale = 0.0f; float depthBiasClamp = 0.0f; }; struct ExternalTextureDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; TextureView plane0; TextureView plane1 = nullptr; Origin2D visibleOrigin; Extent2D visibleSize; bool doYuvToRgbConversionOnly = false; float const * yuvToRgbConversionMatrix = nullptr; float const * srcTransferFunctionParameters; float const * dstTransferFunctionParameters; float const * gamutConversionMatrix; bool flipY = false; ExternalTextureRotation rotation = ExternalTextureRotation::Rotate0Degrees; }; struct ImageCopyBuffer { ChainedStruct const * nextInChain = nullptr; TextureDataLayout layout; Buffer buffer; }; struct ImageCopyExternalTexture { ChainedStruct const * nextInChain = nullptr; ExternalTexture externalTexture; Origin3D origin; Extent2D naturalSize; }; struct ImageCopyTexture { ChainedStruct const * nextInChain = nullptr; Texture texture; uint32_t mipLevel = 0; Origin3D origin; TextureAspect aspect = TextureAspect::All; }; struct ProgrammableStageDescriptor { ChainedStruct const * nextInChain = nullptr; ShaderModule module; char const * entryPoint; size_t constantCount = 0; ConstantEntry const * constants; }; struct RenderPassColorAttachment { ChainedStruct const * nextInChain = nullptr; TextureView view = nullptr; TextureView resolveTarget = nullptr; LoadOp loadOp; StoreOp storeOp; Color clearValue; }; struct RequiredLimits { ChainedStruct const * nextInChain = nullptr; Limits limits; }; struct SupportedLimits { ChainedStructOut * nextInChain = nullptr; Limits limits; }; struct TextureDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; TextureUsage usage; TextureDimension dimension = TextureDimension::e2D; Extent3D size; TextureFormat format; uint32_t mipLevelCount = 1; uint32_t sampleCount = 1; size_t viewFormatCount = 0; TextureFormat const * viewFormats; }; struct VertexBufferLayout { uint64_t arrayStride; VertexStepMode stepMode = VertexStepMode::Vertex; size_t attributeCount; VertexAttribute const * attributes; }; struct BindGroupLayoutDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t entryCount; BindGroupLayoutEntry const * entries; }; struct ColorTargetState { ChainedStruct const * nextInChain = nullptr; TextureFormat format; BlendState const * blend = nullptr; ColorWriteMask writeMask = ColorWriteMask::All; }; struct ComputePipelineDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; PipelineLayout layout = nullptr; ProgrammableStageDescriptor compute; }; struct DeviceDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t requiredFeaturesCount = 0; FeatureName const * requiredFeatures = nullptr; RequiredLimits const * requiredLimits = nullptr; QueueDescriptor defaultQueue; DeviceLostCallback deviceLostCallback = nullptr; void * deviceLostUserdata = nullptr; }; struct RenderPassDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; size_t colorAttachmentCount; RenderPassColorAttachment const * colorAttachments; RenderPassDepthStencilAttachment const * depthStencilAttachment = nullptr; QuerySet occlusionQuerySet = nullptr; size_t timestampWriteCount = 0; RenderPassTimestampWrite const * timestampWrites; }; struct VertexState { ChainedStruct const * nextInChain = nullptr; ShaderModule module; char const * entryPoint; size_t constantCount = 0; ConstantEntry const * constants; size_t bufferCount = 0; VertexBufferLayout const * buffers; }; struct FragmentState { ChainedStruct const * nextInChain = nullptr; ShaderModule module; char const * entryPoint; size_t constantCount = 0; ConstantEntry const * constants; size_t targetCount; ColorTargetState const * targets; }; struct RenderPipelineDescriptor { ChainedStruct const * nextInChain = nullptr; char const * label = nullptr; PipelineLayout layout = nullptr; VertexState vertex; PrimitiveState primitive; DepthStencilState const * depthStencil = nullptr; MultisampleState multisample; FragmentState const * fragment = nullptr; }; // The operators of EnumClassBitmmasks in the dawn:: namespace need to be imported // in the wgpu namespace for Argument Dependent Lookup. DAWN_IMPORT_BITMASK_OPERATORS } // namespace wgpu namespace dawn { template<> struct IsDawnBitmask<wgpu::BufferUsage> { static constexpr bool enable = true; }; template<> struct IsDawnBitmask<wgpu::ColorWriteMask> { static constexpr bool enable = true; }; template<> struct IsDawnBitmask<wgpu::MapMode> { static constexpr bool enable = true; }; template<> struct IsDawnBitmask<wgpu::ShaderStage> { static constexpr bool enable = true; }; template<> struct IsDawnBitmask<wgpu::TextureUsage> { static constexpr bool enable = true; }; } // namespace dawn #endif // WEBGPU_CPP_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/webgpu_cpp_print.h
#ifndef WEBGPU_CPP_PRINT_H_ #define WEBGPU_CPP_PRINT_H_ #include "dawn/webgpu_cpp.h" #include <iomanip> #include <ios> #include <ostream> #include <type_traits> namespace wgpu { template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, AdapterType value) { switch (value) { case AdapterType::DiscreteGPU: o << "AdapterType::DiscreteGPU"; break; case AdapterType::IntegratedGPU: o << "AdapterType::IntegratedGPU"; break; case AdapterType::CPU: o << "AdapterType::CPU"; break; case AdapterType::Unknown: o << "AdapterType::Unknown"; break; default: o << "AdapterType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<AdapterType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, AddressMode value) { switch (value) { case AddressMode::Repeat: o << "AddressMode::Repeat"; break; case AddressMode::MirrorRepeat: o << "AddressMode::MirrorRepeat"; break; case AddressMode::ClampToEdge: o << "AddressMode::ClampToEdge"; break; default: o << "AddressMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<AddressMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, AlphaMode value) { switch (value) { case AlphaMode::Premultiplied: o << "AlphaMode::Premultiplied"; break; case AlphaMode::Unpremultiplied: o << "AlphaMode::Unpremultiplied"; break; case AlphaMode::Opaque: o << "AlphaMode::Opaque"; break; default: o << "AlphaMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<AlphaMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BackendType value) { switch (value) { case BackendType::Undefined: o << "BackendType::Undefined"; break; case BackendType::Null: o << "BackendType::Null"; break; case BackendType::WebGPU: o << "BackendType::WebGPU"; break; case BackendType::D3D11: o << "BackendType::D3D11"; break; case BackendType::D3D12: o << "BackendType::D3D12"; break; case BackendType::Metal: o << "BackendType::Metal"; break; case BackendType::Vulkan: o << "BackendType::Vulkan"; break; case BackendType::OpenGL: o << "BackendType::OpenGL"; break; case BackendType::OpenGLES: o << "BackendType::OpenGLES"; break; default: o << "BackendType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BackendType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BlendFactor value) { switch (value) { case BlendFactor::Zero: o << "BlendFactor::Zero"; break; case BlendFactor::One: o << "BlendFactor::One"; break; case BlendFactor::Src: o << "BlendFactor::Src"; break; case BlendFactor::OneMinusSrc: o << "BlendFactor::OneMinusSrc"; break; case BlendFactor::SrcAlpha: o << "BlendFactor::SrcAlpha"; break; case BlendFactor::OneMinusSrcAlpha: o << "BlendFactor::OneMinusSrcAlpha"; break; case BlendFactor::Dst: o << "BlendFactor::Dst"; break; case BlendFactor::OneMinusDst: o << "BlendFactor::OneMinusDst"; break; case BlendFactor::DstAlpha: o << "BlendFactor::DstAlpha"; break; case BlendFactor::OneMinusDstAlpha: o << "BlendFactor::OneMinusDstAlpha"; break; case BlendFactor::SrcAlphaSaturated: o << "BlendFactor::SrcAlphaSaturated"; break; case BlendFactor::Constant: o << "BlendFactor::Constant"; break; case BlendFactor::OneMinusConstant: o << "BlendFactor::OneMinusConstant"; break; default: o << "BlendFactor::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BlendFactor>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BlendOperation value) { switch (value) { case BlendOperation::Add: o << "BlendOperation::Add"; break; case BlendOperation::Subtract: o << "BlendOperation::Subtract"; break; case BlendOperation::ReverseSubtract: o << "BlendOperation::ReverseSubtract"; break; case BlendOperation::Min: o << "BlendOperation::Min"; break; case BlendOperation::Max: o << "BlendOperation::Max"; break; default: o << "BlendOperation::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BlendOperation>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BufferBindingType value) { switch (value) { case BufferBindingType::Undefined: o << "BufferBindingType::Undefined"; break; case BufferBindingType::Uniform: o << "BufferBindingType::Uniform"; break; case BufferBindingType::Storage: o << "BufferBindingType::Storage"; break; case BufferBindingType::ReadOnlyStorage: o << "BufferBindingType::ReadOnlyStorage"; break; default: o << "BufferBindingType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BufferBindingType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BufferMapAsyncStatus value) { switch (value) { case BufferMapAsyncStatus::Success: o << "BufferMapAsyncStatus::Success"; break; case BufferMapAsyncStatus::ValidationError: o << "BufferMapAsyncStatus::ValidationError"; break; case BufferMapAsyncStatus::Unknown: o << "BufferMapAsyncStatus::Unknown"; break; case BufferMapAsyncStatus::DeviceLost: o << "BufferMapAsyncStatus::DeviceLost"; break; case BufferMapAsyncStatus::DestroyedBeforeCallback: o << "BufferMapAsyncStatus::DestroyedBeforeCallback"; break; case BufferMapAsyncStatus::UnmappedBeforeCallback: o << "BufferMapAsyncStatus::UnmappedBeforeCallback"; break; case BufferMapAsyncStatus::MappingAlreadyPending: o << "BufferMapAsyncStatus::MappingAlreadyPending"; break; case BufferMapAsyncStatus::OffsetOutOfRange: o << "BufferMapAsyncStatus::OffsetOutOfRange"; break; case BufferMapAsyncStatus::SizeOutOfRange: o << "BufferMapAsyncStatus::SizeOutOfRange"; break; default: o << "BufferMapAsyncStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BufferMapAsyncStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BufferMapState value) { switch (value) { case BufferMapState::Unmapped: o << "BufferMapState::Unmapped"; break; case BufferMapState::Pending: o << "BufferMapState::Pending"; break; case BufferMapState::Mapped: o << "BufferMapState::Mapped"; break; default: o << "BufferMapState::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BufferMapState>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, CompareFunction value) { switch (value) { case CompareFunction::Undefined: o << "CompareFunction::Undefined"; break; case CompareFunction::Never: o << "CompareFunction::Never"; break; case CompareFunction::Less: o << "CompareFunction::Less"; break; case CompareFunction::LessEqual: o << "CompareFunction::LessEqual"; break; case CompareFunction::Greater: o << "CompareFunction::Greater"; break; case CompareFunction::GreaterEqual: o << "CompareFunction::GreaterEqual"; break; case CompareFunction::Equal: o << "CompareFunction::Equal"; break; case CompareFunction::NotEqual: o << "CompareFunction::NotEqual"; break; case CompareFunction::Always: o << "CompareFunction::Always"; break; default: o << "CompareFunction::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<CompareFunction>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, CompilationInfoRequestStatus value) { switch (value) { case CompilationInfoRequestStatus::Success: o << "CompilationInfoRequestStatus::Success"; break; case CompilationInfoRequestStatus::Error: o << "CompilationInfoRequestStatus::Error"; break; case CompilationInfoRequestStatus::DeviceLost: o << "CompilationInfoRequestStatus::DeviceLost"; break; case CompilationInfoRequestStatus::Unknown: o << "CompilationInfoRequestStatus::Unknown"; break; default: o << "CompilationInfoRequestStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<CompilationInfoRequestStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, CompilationMessageType value) { switch (value) { case CompilationMessageType::Error: o << "CompilationMessageType::Error"; break; case CompilationMessageType::Warning: o << "CompilationMessageType::Warning"; break; case CompilationMessageType::Info: o << "CompilationMessageType::Info"; break; default: o << "CompilationMessageType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<CompilationMessageType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ComputePassTimestampLocation value) { switch (value) { case ComputePassTimestampLocation::Beginning: o << "ComputePassTimestampLocation::Beginning"; break; case ComputePassTimestampLocation::End: o << "ComputePassTimestampLocation::End"; break; default: o << "ComputePassTimestampLocation::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ComputePassTimestampLocation>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, CreatePipelineAsyncStatus value) { switch (value) { case CreatePipelineAsyncStatus::Success: o << "CreatePipelineAsyncStatus::Success"; break; case CreatePipelineAsyncStatus::ValidationError: o << "CreatePipelineAsyncStatus::ValidationError"; break; case CreatePipelineAsyncStatus::InternalError: o << "CreatePipelineAsyncStatus::InternalError"; break; case CreatePipelineAsyncStatus::DeviceLost: o << "CreatePipelineAsyncStatus::DeviceLost"; break; case CreatePipelineAsyncStatus::DeviceDestroyed: o << "CreatePipelineAsyncStatus::DeviceDestroyed"; break; case CreatePipelineAsyncStatus::Unknown: o << "CreatePipelineAsyncStatus::Unknown"; break; default: o << "CreatePipelineAsyncStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<CreatePipelineAsyncStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, CullMode value) { switch (value) { case CullMode::None: o << "CullMode::None"; break; case CullMode::Front: o << "CullMode::Front"; break; case CullMode::Back: o << "CullMode::Back"; break; default: o << "CullMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<CullMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, DeviceLostReason value) { switch (value) { case DeviceLostReason::Undefined: o << "DeviceLostReason::Undefined"; break; case DeviceLostReason::Destroyed: o << "DeviceLostReason::Destroyed"; break; default: o << "DeviceLostReason::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<DeviceLostReason>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ErrorFilter value) { switch (value) { case ErrorFilter::Validation: o << "ErrorFilter::Validation"; break; case ErrorFilter::OutOfMemory: o << "ErrorFilter::OutOfMemory"; break; case ErrorFilter::Internal: o << "ErrorFilter::Internal"; break; default: o << "ErrorFilter::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ErrorFilter>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ErrorType value) { switch (value) { case ErrorType::NoError: o << "ErrorType::NoError"; break; case ErrorType::Validation: o << "ErrorType::Validation"; break; case ErrorType::OutOfMemory: o << "ErrorType::OutOfMemory"; break; case ErrorType::Internal: o << "ErrorType::Internal"; break; case ErrorType::Unknown: o << "ErrorType::Unknown"; break; case ErrorType::DeviceLost: o << "ErrorType::DeviceLost"; break; default: o << "ErrorType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ErrorType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ExternalTextureRotation value) { switch (value) { case ExternalTextureRotation::Rotate0Degrees: o << "ExternalTextureRotation::Rotate0Degrees"; break; case ExternalTextureRotation::Rotate90Degrees: o << "ExternalTextureRotation::Rotate90Degrees"; break; case ExternalTextureRotation::Rotate180Degrees: o << "ExternalTextureRotation::Rotate180Degrees"; break; case ExternalTextureRotation::Rotate270Degrees: o << "ExternalTextureRotation::Rotate270Degrees"; break; default: o << "ExternalTextureRotation::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ExternalTextureRotation>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, FeatureName value) { switch (value) { case FeatureName::Undefined: o << "FeatureName::Undefined"; break; case FeatureName::DepthClipControl: o << "FeatureName::DepthClipControl"; break; case FeatureName::Depth32FloatStencil8: o << "FeatureName::Depth32FloatStencil8"; break; case FeatureName::TimestampQuery: o << "FeatureName::TimestampQuery"; break; case FeatureName::PipelineStatisticsQuery: o << "FeatureName::PipelineStatisticsQuery"; break; case FeatureName::TextureCompressionBC: o << "FeatureName::TextureCompressionBC"; break; case FeatureName::TextureCompressionETC2: o << "FeatureName::TextureCompressionETC2"; break; case FeatureName::TextureCompressionASTC: o << "FeatureName::TextureCompressionASTC"; break; case FeatureName::IndirectFirstInstance: o << "FeatureName::IndirectFirstInstance"; break; case FeatureName::ShaderF16: o << "FeatureName::ShaderF16"; break; case FeatureName::RG11B10UfloatRenderable: o << "FeatureName::RG11B10UfloatRenderable"; break; case FeatureName::BGRA8UnormStorage: o << "FeatureName::BGRA8UnormStorage"; break; case FeatureName::Float32Filterable: o << "FeatureName::Float32Filterable"; break; case FeatureName::DawnShaderFloat16: o << "FeatureName::DawnShaderFloat16"; break; case FeatureName::DawnInternalUsages: o << "FeatureName::DawnInternalUsages"; break; case FeatureName::DawnMultiPlanarFormats: o << "FeatureName::DawnMultiPlanarFormats"; break; case FeatureName::DawnNative: o << "FeatureName::DawnNative"; break; case FeatureName::ChromiumExperimentalDp4a: o << "FeatureName::ChromiumExperimentalDp4a"; break; case FeatureName::TimestampQueryInsidePasses: o << "FeatureName::TimestampQueryInsidePasses"; break; case FeatureName::ImplicitDeviceSynchronization: o << "FeatureName::ImplicitDeviceSynchronization"; break; case FeatureName::SurfaceCapabilities: o << "FeatureName::SurfaceCapabilities"; break; case FeatureName::TransientAttachments: o << "FeatureName::TransientAttachments"; break; case FeatureName::MSAARenderToSingleSampled: o << "FeatureName::MSAARenderToSingleSampled"; break; default: o << "FeatureName::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<FeatureName>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, FilterMode value) { switch (value) { case FilterMode::Nearest: o << "FilterMode::Nearest"; break; case FilterMode::Linear: o << "FilterMode::Linear"; break; default: o << "FilterMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<FilterMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, FrontFace value) { switch (value) { case FrontFace::CCW: o << "FrontFace::CCW"; break; case FrontFace::CW: o << "FrontFace::CW"; break; default: o << "FrontFace::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<FrontFace>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, IndexFormat value) { switch (value) { case IndexFormat::Undefined: o << "IndexFormat::Undefined"; break; case IndexFormat::Uint16: o << "IndexFormat::Uint16"; break; case IndexFormat::Uint32: o << "IndexFormat::Uint32"; break; default: o << "IndexFormat::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<IndexFormat>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, LoadOp value) { switch (value) { case LoadOp::Undefined: o << "LoadOp::Undefined"; break; case LoadOp::Clear: o << "LoadOp::Clear"; break; case LoadOp::Load: o << "LoadOp::Load"; break; default: o << "LoadOp::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<LoadOp>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, LoggingType value) { switch (value) { case LoggingType::Verbose: o << "LoggingType::Verbose"; break; case LoggingType::Info: o << "LoggingType::Info"; break; case LoggingType::Warning: o << "LoggingType::Warning"; break; case LoggingType::Error: o << "LoggingType::Error"; break; default: o << "LoggingType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<LoggingType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, MipmapFilterMode value) { switch (value) { case MipmapFilterMode::Nearest: o << "MipmapFilterMode::Nearest"; break; case MipmapFilterMode::Linear: o << "MipmapFilterMode::Linear"; break; default: o << "MipmapFilterMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<MipmapFilterMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, PipelineStatisticName value) { switch (value) { case PipelineStatisticName::VertexShaderInvocations: o << "PipelineStatisticName::VertexShaderInvocations"; break; case PipelineStatisticName::ClipperInvocations: o << "PipelineStatisticName::ClipperInvocations"; break; case PipelineStatisticName::ClipperPrimitivesOut: o << "PipelineStatisticName::ClipperPrimitivesOut"; break; case PipelineStatisticName::FragmentShaderInvocations: o << "PipelineStatisticName::FragmentShaderInvocations"; break; case PipelineStatisticName::ComputeShaderInvocations: o << "PipelineStatisticName::ComputeShaderInvocations"; break; default: o << "PipelineStatisticName::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<PipelineStatisticName>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, PowerPreference value) { switch (value) { case PowerPreference::Undefined: o << "PowerPreference::Undefined"; break; case PowerPreference::LowPower: o << "PowerPreference::LowPower"; break; case PowerPreference::HighPerformance: o << "PowerPreference::HighPerformance"; break; default: o << "PowerPreference::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<PowerPreference>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, PresentMode value) { switch (value) { case PresentMode::Immediate: o << "PresentMode::Immediate"; break; case PresentMode::Mailbox: o << "PresentMode::Mailbox"; break; case PresentMode::Fifo: o << "PresentMode::Fifo"; break; default: o << "PresentMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<PresentMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, PrimitiveTopology value) { switch (value) { case PrimitiveTopology::PointList: o << "PrimitiveTopology::PointList"; break; case PrimitiveTopology::LineList: o << "PrimitiveTopology::LineList"; break; case PrimitiveTopology::LineStrip: o << "PrimitiveTopology::LineStrip"; break; case PrimitiveTopology::TriangleList: o << "PrimitiveTopology::TriangleList"; break; case PrimitiveTopology::TriangleStrip: o << "PrimitiveTopology::TriangleStrip"; break; default: o << "PrimitiveTopology::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<PrimitiveTopology>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, QueryType value) { switch (value) { case QueryType::Occlusion: o << "QueryType::Occlusion"; break; case QueryType::PipelineStatistics: o << "QueryType::PipelineStatistics"; break; case QueryType::Timestamp: o << "QueryType::Timestamp"; break; default: o << "QueryType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<QueryType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, QueueWorkDoneStatus value) { switch (value) { case QueueWorkDoneStatus::Success: o << "QueueWorkDoneStatus::Success"; break; case QueueWorkDoneStatus::Error: o << "QueueWorkDoneStatus::Error"; break; case QueueWorkDoneStatus::Unknown: o << "QueueWorkDoneStatus::Unknown"; break; case QueueWorkDoneStatus::DeviceLost: o << "QueueWorkDoneStatus::DeviceLost"; break; default: o << "QueueWorkDoneStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<QueueWorkDoneStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, RenderPassTimestampLocation value) { switch (value) { case RenderPassTimestampLocation::Beginning: o << "RenderPassTimestampLocation::Beginning"; break; case RenderPassTimestampLocation::End: o << "RenderPassTimestampLocation::End"; break; default: o << "RenderPassTimestampLocation::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<RenderPassTimestampLocation>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, RequestAdapterStatus value) { switch (value) { case RequestAdapterStatus::Success: o << "RequestAdapterStatus::Success"; break; case RequestAdapterStatus::Unavailable: o << "RequestAdapterStatus::Unavailable"; break; case RequestAdapterStatus::Error: o << "RequestAdapterStatus::Error"; break; case RequestAdapterStatus::Unknown: o << "RequestAdapterStatus::Unknown"; break; default: o << "RequestAdapterStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<RequestAdapterStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, RequestDeviceStatus value) { switch (value) { case RequestDeviceStatus::Success: o << "RequestDeviceStatus::Success"; break; case RequestDeviceStatus::Error: o << "RequestDeviceStatus::Error"; break; case RequestDeviceStatus::Unknown: o << "RequestDeviceStatus::Unknown"; break; default: o << "RequestDeviceStatus::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<RequestDeviceStatus>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, SType value) { switch (value) { case SType::Invalid: o << "SType::Invalid"; break; case SType::SurfaceDescriptorFromMetalLayer: o << "SType::SurfaceDescriptorFromMetalLayer"; break; case SType::SurfaceDescriptorFromWindowsHWND: o << "SType::SurfaceDescriptorFromWindowsHWND"; break; case SType::SurfaceDescriptorFromXlibWindow: o << "SType::SurfaceDescriptorFromXlibWindow"; break; case SType::SurfaceDescriptorFromCanvasHTMLSelector: o << "SType::SurfaceDescriptorFromCanvasHTMLSelector"; break; case SType::ShaderModuleSPIRVDescriptor: o << "SType::ShaderModuleSPIRVDescriptor"; break; case SType::ShaderModuleWGSLDescriptor: o << "SType::ShaderModuleWGSLDescriptor"; break; case SType::PrimitiveDepthClipControl: o << "SType::PrimitiveDepthClipControl"; break; case SType::SurfaceDescriptorFromWaylandSurface: o << "SType::SurfaceDescriptorFromWaylandSurface"; break; case SType::SurfaceDescriptorFromAndroidNativeWindow: o << "SType::SurfaceDescriptorFromAndroidNativeWindow"; break; case SType::SurfaceDescriptorFromWindowsCoreWindow: o << "SType::SurfaceDescriptorFromWindowsCoreWindow"; break; case SType::ExternalTextureBindingEntry: o << "SType::ExternalTextureBindingEntry"; break; case SType::ExternalTextureBindingLayout: o << "SType::ExternalTextureBindingLayout"; break; case SType::SurfaceDescriptorFromWindowsSwapChainPanel: o << "SType::SurfaceDescriptorFromWindowsSwapChainPanel"; break; case SType::RenderPassDescriptorMaxDrawCount: o << "SType::RenderPassDescriptorMaxDrawCount"; break; case SType::DawnTextureInternalUsageDescriptor: o << "SType::DawnTextureInternalUsageDescriptor"; break; case SType::DawnEncoderInternalUsageDescriptor: o << "SType::DawnEncoderInternalUsageDescriptor"; break; case SType::DawnInstanceDescriptor: o << "SType::DawnInstanceDescriptor"; break; case SType::DawnCacheDeviceDescriptor: o << "SType::DawnCacheDeviceDescriptor"; break; case SType::DawnAdapterPropertiesPowerPreference: o << "SType::DawnAdapterPropertiesPowerPreference"; break; case SType::DawnBufferDescriptorErrorInfoFromWireClient: o << "SType::DawnBufferDescriptorErrorInfoFromWireClient"; break; case SType::DawnTogglesDescriptor: o << "SType::DawnTogglesDescriptor"; break; case SType::DawnShaderModuleSPIRVOptionsDescriptor: o << "SType::DawnShaderModuleSPIRVOptionsDescriptor"; break; case SType::RequestAdapterOptionsLUID: o << "SType::RequestAdapterOptionsLUID"; break; case SType::RequestAdapterOptionsGetGLProc: o << "SType::RequestAdapterOptionsGetGLProc"; break; case SType::DawnMultisampleStateRenderToSingleSampled: o << "SType::DawnMultisampleStateRenderToSingleSampled"; break; case SType::DawnRenderPassColorAttachmentRenderToSingleSampled: o << "SType::DawnRenderPassColorAttachmentRenderToSingleSampled"; break; default: o << "SType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<SType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, SamplerBindingType value) { switch (value) { case SamplerBindingType::Undefined: o << "SamplerBindingType::Undefined"; break; case SamplerBindingType::Filtering: o << "SamplerBindingType::Filtering"; break; case SamplerBindingType::NonFiltering: o << "SamplerBindingType::NonFiltering"; break; case SamplerBindingType::Comparison: o << "SamplerBindingType::Comparison"; break; default: o << "SamplerBindingType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<SamplerBindingType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, StencilOperation value) { switch (value) { case StencilOperation::Keep: o << "StencilOperation::Keep"; break; case StencilOperation::Zero: o << "StencilOperation::Zero"; break; case StencilOperation::Replace: o << "StencilOperation::Replace"; break; case StencilOperation::Invert: o << "StencilOperation::Invert"; break; case StencilOperation::IncrementClamp: o << "StencilOperation::IncrementClamp"; break; case StencilOperation::DecrementClamp: o << "StencilOperation::DecrementClamp"; break; case StencilOperation::IncrementWrap: o << "StencilOperation::IncrementWrap"; break; case StencilOperation::DecrementWrap: o << "StencilOperation::DecrementWrap"; break; default: o << "StencilOperation::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<StencilOperation>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, StorageTextureAccess value) { switch (value) { case StorageTextureAccess::Undefined: o << "StorageTextureAccess::Undefined"; break; case StorageTextureAccess::WriteOnly: o << "StorageTextureAccess::WriteOnly"; break; default: o << "StorageTextureAccess::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<StorageTextureAccess>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, StoreOp value) { switch (value) { case StoreOp::Undefined: o << "StoreOp::Undefined"; break; case StoreOp::Store: o << "StoreOp::Store"; break; case StoreOp::Discard: o << "StoreOp::Discard"; break; default: o << "StoreOp::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<StoreOp>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureAspect value) { switch (value) { case TextureAspect::All: o << "TextureAspect::All"; break; case TextureAspect::StencilOnly: o << "TextureAspect::StencilOnly"; break; case TextureAspect::DepthOnly: o << "TextureAspect::DepthOnly"; break; case TextureAspect::Plane0Only: o << "TextureAspect::Plane0Only"; break; case TextureAspect::Plane1Only: o << "TextureAspect::Plane1Only"; break; default: o << "TextureAspect::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureAspect>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureDimension value) { switch (value) { case TextureDimension::e1D: o << "TextureDimension::e1D"; break; case TextureDimension::e2D: o << "TextureDimension::e2D"; break; case TextureDimension::e3D: o << "TextureDimension::e3D"; break; default: o << "TextureDimension::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureDimension>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureFormat value) { switch (value) { case TextureFormat::Undefined: o << "TextureFormat::Undefined"; break; case TextureFormat::R8Unorm: o << "TextureFormat::R8Unorm"; break; case TextureFormat::R8Snorm: o << "TextureFormat::R8Snorm"; break; case TextureFormat::R8Uint: o << "TextureFormat::R8Uint"; break; case TextureFormat::R8Sint: o << "TextureFormat::R8Sint"; break; case TextureFormat::R16Uint: o << "TextureFormat::R16Uint"; break; case TextureFormat::R16Sint: o << "TextureFormat::R16Sint"; break; case TextureFormat::R16Float: o << "TextureFormat::R16Float"; break; case TextureFormat::RG8Unorm: o << "TextureFormat::RG8Unorm"; break; case TextureFormat::RG8Snorm: o << "TextureFormat::RG8Snorm"; break; case TextureFormat::RG8Uint: o << "TextureFormat::RG8Uint"; break; case TextureFormat::RG8Sint: o << "TextureFormat::RG8Sint"; break; case TextureFormat::R32Float: o << "TextureFormat::R32Float"; break; case TextureFormat::R32Uint: o << "TextureFormat::R32Uint"; break; case TextureFormat::R32Sint: o << "TextureFormat::R32Sint"; break; case TextureFormat::RG16Uint: o << "TextureFormat::RG16Uint"; break; case TextureFormat::RG16Sint: o << "TextureFormat::RG16Sint"; break; case TextureFormat::RG16Float: o << "TextureFormat::RG16Float"; break; case TextureFormat::RGBA8Unorm: o << "TextureFormat::RGBA8Unorm"; break; case TextureFormat::RGBA8UnormSrgb: o << "TextureFormat::RGBA8UnormSrgb"; break; case TextureFormat::RGBA8Snorm: o << "TextureFormat::RGBA8Snorm"; break; case TextureFormat::RGBA8Uint: o << "TextureFormat::RGBA8Uint"; break; case TextureFormat::RGBA8Sint: o << "TextureFormat::RGBA8Sint"; break; case TextureFormat::BGRA8Unorm: o << "TextureFormat::BGRA8Unorm"; break; case TextureFormat::BGRA8UnormSrgb: o << "TextureFormat::BGRA8UnormSrgb"; break; case TextureFormat::RGB10A2Unorm: o << "TextureFormat::RGB10A2Unorm"; break; case TextureFormat::RG11B10Ufloat: o << "TextureFormat::RG11B10Ufloat"; break; case TextureFormat::RGB9E5Ufloat: o << "TextureFormat::RGB9E5Ufloat"; break; case TextureFormat::RG32Float: o << "TextureFormat::RG32Float"; break; case TextureFormat::RG32Uint: o << "TextureFormat::RG32Uint"; break; case TextureFormat::RG32Sint: o << "TextureFormat::RG32Sint"; break; case TextureFormat::RGBA16Uint: o << "TextureFormat::RGBA16Uint"; break; case TextureFormat::RGBA16Sint: o << "TextureFormat::RGBA16Sint"; break; case TextureFormat::RGBA16Float: o << "TextureFormat::RGBA16Float"; break; case TextureFormat::RGBA32Float: o << "TextureFormat::RGBA32Float"; break; case TextureFormat::RGBA32Uint: o << "TextureFormat::RGBA32Uint"; break; case TextureFormat::RGBA32Sint: o << "TextureFormat::RGBA32Sint"; break; case TextureFormat::Stencil8: o << "TextureFormat::Stencil8"; break; case TextureFormat::Depth16Unorm: o << "TextureFormat::Depth16Unorm"; break; case TextureFormat::Depth24Plus: o << "TextureFormat::Depth24Plus"; break; case TextureFormat::Depth24PlusStencil8: o << "TextureFormat::Depth24PlusStencil8"; break; case TextureFormat::Depth32Float: o << "TextureFormat::Depth32Float"; break; case TextureFormat::Depth32FloatStencil8: o << "TextureFormat::Depth32FloatStencil8"; break; case TextureFormat::BC1RGBAUnorm: o << "TextureFormat::BC1RGBAUnorm"; break; case TextureFormat::BC1RGBAUnormSrgb: o << "TextureFormat::BC1RGBAUnormSrgb"; break; case TextureFormat::BC2RGBAUnorm: o << "TextureFormat::BC2RGBAUnorm"; break; case TextureFormat::BC2RGBAUnormSrgb: o << "TextureFormat::BC2RGBAUnormSrgb"; break; case TextureFormat::BC3RGBAUnorm: o << "TextureFormat::BC3RGBAUnorm"; break; case TextureFormat::BC3RGBAUnormSrgb: o << "TextureFormat::BC3RGBAUnormSrgb"; break; case TextureFormat::BC4RUnorm: o << "TextureFormat::BC4RUnorm"; break; case TextureFormat::BC4RSnorm: o << "TextureFormat::BC4RSnorm"; break; case TextureFormat::BC5RGUnorm: o << "TextureFormat::BC5RGUnorm"; break; case TextureFormat::BC5RGSnorm: o << "TextureFormat::BC5RGSnorm"; break; case TextureFormat::BC6HRGBUfloat: o << "TextureFormat::BC6HRGBUfloat"; break; case TextureFormat::BC6HRGBFloat: o << "TextureFormat::BC6HRGBFloat"; break; case TextureFormat::BC7RGBAUnorm: o << "TextureFormat::BC7RGBAUnorm"; break; case TextureFormat::BC7RGBAUnormSrgb: o << "TextureFormat::BC7RGBAUnormSrgb"; break; case TextureFormat::ETC2RGB8Unorm: o << "TextureFormat::ETC2RGB8Unorm"; break; case TextureFormat::ETC2RGB8UnormSrgb: o << "TextureFormat::ETC2RGB8UnormSrgb"; break; case TextureFormat::ETC2RGB8A1Unorm: o << "TextureFormat::ETC2RGB8A1Unorm"; break; case TextureFormat::ETC2RGB8A1UnormSrgb: o << "TextureFormat::ETC2RGB8A1UnormSrgb"; break; case TextureFormat::ETC2RGBA8Unorm: o << "TextureFormat::ETC2RGBA8Unorm"; break; case TextureFormat::ETC2RGBA8UnormSrgb: o << "TextureFormat::ETC2RGBA8UnormSrgb"; break; case TextureFormat::EACR11Unorm: o << "TextureFormat::EACR11Unorm"; break; case TextureFormat::EACR11Snorm: o << "TextureFormat::EACR11Snorm"; break; case TextureFormat::EACRG11Unorm: o << "TextureFormat::EACRG11Unorm"; break; case TextureFormat::EACRG11Snorm: o << "TextureFormat::EACRG11Snorm"; break; case TextureFormat::ASTC4x4Unorm: o << "TextureFormat::ASTC4x4Unorm"; break; case TextureFormat::ASTC4x4UnormSrgb: o << "TextureFormat::ASTC4x4UnormSrgb"; break; case TextureFormat::ASTC5x4Unorm: o << "TextureFormat::ASTC5x4Unorm"; break; case TextureFormat::ASTC5x4UnormSrgb: o << "TextureFormat::ASTC5x4UnormSrgb"; break; case TextureFormat::ASTC5x5Unorm: o << "TextureFormat::ASTC5x5Unorm"; break; case TextureFormat::ASTC5x5UnormSrgb: o << "TextureFormat::ASTC5x5UnormSrgb"; break; case TextureFormat::ASTC6x5Unorm: o << "TextureFormat::ASTC6x5Unorm"; break; case TextureFormat::ASTC6x5UnormSrgb: o << "TextureFormat::ASTC6x5UnormSrgb"; break; case TextureFormat::ASTC6x6Unorm: o << "TextureFormat::ASTC6x6Unorm"; break; case TextureFormat::ASTC6x6UnormSrgb: o << "TextureFormat::ASTC6x6UnormSrgb"; break; case TextureFormat::ASTC8x5Unorm: o << "TextureFormat::ASTC8x5Unorm"; break; case TextureFormat::ASTC8x5UnormSrgb: o << "TextureFormat::ASTC8x5UnormSrgb"; break; case TextureFormat::ASTC8x6Unorm: o << "TextureFormat::ASTC8x6Unorm"; break; case TextureFormat::ASTC8x6UnormSrgb: o << "TextureFormat::ASTC8x6UnormSrgb"; break; case TextureFormat::ASTC8x8Unorm: o << "TextureFormat::ASTC8x8Unorm"; break; case TextureFormat::ASTC8x8UnormSrgb: o << "TextureFormat::ASTC8x8UnormSrgb"; break; case TextureFormat::ASTC10x5Unorm: o << "TextureFormat::ASTC10x5Unorm"; break; case TextureFormat::ASTC10x5UnormSrgb: o << "TextureFormat::ASTC10x5UnormSrgb"; break; case TextureFormat::ASTC10x6Unorm: o << "TextureFormat::ASTC10x6Unorm"; break; case TextureFormat::ASTC10x6UnormSrgb: o << "TextureFormat::ASTC10x6UnormSrgb"; break; case TextureFormat::ASTC10x8Unorm: o << "TextureFormat::ASTC10x8Unorm"; break; case TextureFormat::ASTC10x8UnormSrgb: o << "TextureFormat::ASTC10x8UnormSrgb"; break; case TextureFormat::ASTC10x10Unorm: o << "TextureFormat::ASTC10x10Unorm"; break; case TextureFormat::ASTC10x10UnormSrgb: o << "TextureFormat::ASTC10x10UnormSrgb"; break; case TextureFormat::ASTC12x10Unorm: o << "TextureFormat::ASTC12x10Unorm"; break; case TextureFormat::ASTC12x10UnormSrgb: o << "TextureFormat::ASTC12x10UnormSrgb"; break; case TextureFormat::ASTC12x12Unorm: o << "TextureFormat::ASTC12x12Unorm"; break; case TextureFormat::ASTC12x12UnormSrgb: o << "TextureFormat::ASTC12x12UnormSrgb"; break; case TextureFormat::R8BG8Biplanar420Unorm: o << "TextureFormat::R8BG8Biplanar420Unorm"; break; default: o << "TextureFormat::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureFormat>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureSampleType value) { switch (value) { case TextureSampleType::Undefined: o << "TextureSampleType::Undefined"; break; case TextureSampleType::Float: o << "TextureSampleType::Float"; break; case TextureSampleType::UnfilterableFloat: o << "TextureSampleType::UnfilterableFloat"; break; case TextureSampleType::Depth: o << "TextureSampleType::Depth"; break; case TextureSampleType::Sint: o << "TextureSampleType::Sint"; break; case TextureSampleType::Uint: o << "TextureSampleType::Uint"; break; default: o << "TextureSampleType::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureSampleType>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureViewDimension value) { switch (value) { case TextureViewDimension::Undefined: o << "TextureViewDimension::Undefined"; break; case TextureViewDimension::e1D: o << "TextureViewDimension::e1D"; break; case TextureViewDimension::e2D: o << "TextureViewDimension::e2D"; break; case TextureViewDimension::e2DArray: o << "TextureViewDimension::e2DArray"; break; case TextureViewDimension::Cube: o << "TextureViewDimension::Cube"; break; case TextureViewDimension::CubeArray: o << "TextureViewDimension::CubeArray"; break; case TextureViewDimension::e3D: o << "TextureViewDimension::e3D"; break; default: o << "TextureViewDimension::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureViewDimension>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, VertexFormat value) { switch (value) { case VertexFormat::Undefined: o << "VertexFormat::Undefined"; break; case VertexFormat::Uint8x2: o << "VertexFormat::Uint8x2"; break; case VertexFormat::Uint8x4: o << "VertexFormat::Uint8x4"; break; case VertexFormat::Sint8x2: o << "VertexFormat::Sint8x2"; break; case VertexFormat::Sint8x4: o << "VertexFormat::Sint8x4"; break; case VertexFormat::Unorm8x2: o << "VertexFormat::Unorm8x2"; break; case VertexFormat::Unorm8x4: o << "VertexFormat::Unorm8x4"; break; case VertexFormat::Snorm8x2: o << "VertexFormat::Snorm8x2"; break; case VertexFormat::Snorm8x4: o << "VertexFormat::Snorm8x4"; break; case VertexFormat::Uint16x2: o << "VertexFormat::Uint16x2"; break; case VertexFormat::Uint16x4: o << "VertexFormat::Uint16x4"; break; case VertexFormat::Sint16x2: o << "VertexFormat::Sint16x2"; break; case VertexFormat::Sint16x4: o << "VertexFormat::Sint16x4"; break; case VertexFormat::Unorm16x2: o << "VertexFormat::Unorm16x2"; break; case VertexFormat::Unorm16x4: o << "VertexFormat::Unorm16x4"; break; case VertexFormat::Snorm16x2: o << "VertexFormat::Snorm16x2"; break; case VertexFormat::Snorm16x4: o << "VertexFormat::Snorm16x4"; break; case VertexFormat::Float16x2: o << "VertexFormat::Float16x2"; break; case VertexFormat::Float16x4: o << "VertexFormat::Float16x4"; break; case VertexFormat::Float32: o << "VertexFormat::Float32"; break; case VertexFormat::Float32x2: o << "VertexFormat::Float32x2"; break; case VertexFormat::Float32x3: o << "VertexFormat::Float32x3"; break; case VertexFormat::Float32x4: o << "VertexFormat::Float32x4"; break; case VertexFormat::Uint32: o << "VertexFormat::Uint32"; break; case VertexFormat::Uint32x2: o << "VertexFormat::Uint32x2"; break; case VertexFormat::Uint32x3: o << "VertexFormat::Uint32x3"; break; case VertexFormat::Uint32x4: o << "VertexFormat::Uint32x4"; break; case VertexFormat::Sint32: o << "VertexFormat::Sint32"; break; case VertexFormat::Sint32x2: o << "VertexFormat::Sint32x2"; break; case VertexFormat::Sint32x3: o << "VertexFormat::Sint32x3"; break; case VertexFormat::Sint32x4: o << "VertexFormat::Sint32x4"; break; default: o << "VertexFormat::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<VertexFormat>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, VertexStepMode value) { switch (value) { case VertexStepMode::Vertex: o << "VertexStepMode::Vertex"; break; case VertexStepMode::Instance: o << "VertexStepMode::Instance"; break; case VertexStepMode::VertexBufferNotUsed: o << "VertexStepMode::VertexBufferNotUsed"; break; default: o << "VertexStepMode::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<VertexStepMode>::type>(value); } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, BufferUsage value) { o << "BufferUsage::"; if (!static_cast<bool>(value)) { // 0 is often explicitly declared as None. o << "None"; return o; } bool moreThanOneBit = !HasZeroOrOneBits(value); if (moreThanOneBit) { o << "("; } bool first = true; if (value & BufferUsage::MapRead) { if (!first) { o << "|"; } first = false; o << "MapRead"; value &= ~BufferUsage::MapRead; } if (value & BufferUsage::MapWrite) { if (!first) { o << "|"; } first = false; o << "MapWrite"; value &= ~BufferUsage::MapWrite; } if (value & BufferUsage::CopySrc) { if (!first) { o << "|"; } first = false; o << "CopySrc"; value &= ~BufferUsage::CopySrc; } if (value & BufferUsage::CopyDst) { if (!first) { o << "|"; } first = false; o << "CopyDst"; value &= ~BufferUsage::CopyDst; } if (value & BufferUsage::Index) { if (!first) { o << "|"; } first = false; o << "Index"; value &= ~BufferUsage::Index; } if (value & BufferUsage::Vertex) { if (!first) { o << "|"; } first = false; o << "Vertex"; value &= ~BufferUsage::Vertex; } if (value & BufferUsage::Uniform) { if (!first) { o << "|"; } first = false; o << "Uniform"; value &= ~BufferUsage::Uniform; } if (value & BufferUsage::Storage) { if (!first) { o << "|"; } first = false; o << "Storage"; value &= ~BufferUsage::Storage; } if (value & BufferUsage::Indirect) { if (!first) { o << "|"; } first = false; o << "Indirect"; value &= ~BufferUsage::Indirect; } if (value & BufferUsage::QueryResolve) { if (!first) { o << "|"; } first = false; o << "QueryResolve"; value &= ~BufferUsage::QueryResolve; } if (static_cast<bool>(value)) { if (!first) { o << "|"; } o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<BufferUsage>::type>(value); } if (moreThanOneBit) { o << ")"; } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ColorWriteMask value) { o << "ColorWriteMask::"; if (!static_cast<bool>(value)) { // 0 is often explicitly declared as None. o << "None"; return o; } bool moreThanOneBit = !HasZeroOrOneBits(value); if (moreThanOneBit) { o << "("; } bool first = true; if (value & ColorWriteMask::Red) { if (!first) { o << "|"; } first = false; o << "Red"; value &= ~ColorWriteMask::Red; } if (value & ColorWriteMask::Green) { if (!first) { o << "|"; } first = false; o << "Green"; value &= ~ColorWriteMask::Green; } if (value & ColorWriteMask::Blue) { if (!first) { o << "|"; } first = false; o << "Blue"; value &= ~ColorWriteMask::Blue; } if (value & ColorWriteMask::Alpha) { if (!first) { o << "|"; } first = false; o << "Alpha"; value &= ~ColorWriteMask::Alpha; } if (value & ColorWriteMask::All) { if (!first) { o << "|"; } first = false; o << "All"; value &= ~ColorWriteMask::All; } if (static_cast<bool>(value)) { if (!first) { o << "|"; } o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ColorWriteMask>::type>(value); } if (moreThanOneBit) { o << ")"; } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, MapMode value) { o << "MapMode::"; if (!static_cast<bool>(value)) { // 0 is often explicitly declared as None. o << "None"; return o; } bool moreThanOneBit = !HasZeroOrOneBits(value); if (moreThanOneBit) { o << "("; } bool first = true; if (value & MapMode::Read) { if (!first) { o << "|"; } first = false; o << "Read"; value &= ~MapMode::Read; } if (value & MapMode::Write) { if (!first) { o << "|"; } first = false; o << "Write"; value &= ~MapMode::Write; } if (static_cast<bool>(value)) { if (!first) { o << "|"; } o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<MapMode>::type>(value); } if (moreThanOneBit) { o << ")"; } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, ShaderStage value) { o << "ShaderStage::"; if (!static_cast<bool>(value)) { // 0 is often explicitly declared as None. o << "None"; return o; } bool moreThanOneBit = !HasZeroOrOneBits(value); if (moreThanOneBit) { o << "("; } bool first = true; if (value & ShaderStage::Vertex) { if (!first) { o << "|"; } first = false; o << "Vertex"; value &= ~ShaderStage::Vertex; } if (value & ShaderStage::Fragment) { if (!first) { o << "|"; } first = false; o << "Fragment"; value &= ~ShaderStage::Fragment; } if (value & ShaderStage::Compute) { if (!first) { o << "|"; } first = false; o << "Compute"; value &= ~ShaderStage::Compute; } if (static_cast<bool>(value)) { if (!first) { o << "|"; } o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<ShaderStage>::type>(value); } if (moreThanOneBit) { o << ")"; } return o; } template <typename CharT, typename Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, TextureUsage value) { o << "TextureUsage::"; if (!static_cast<bool>(value)) { // 0 is often explicitly declared as None. o << "None"; return o; } bool moreThanOneBit = !HasZeroOrOneBits(value); if (moreThanOneBit) { o << "("; } bool first = true; if (value & TextureUsage::CopySrc) { if (!first) { o << "|"; } first = false; o << "CopySrc"; value &= ~TextureUsage::CopySrc; } if (value & TextureUsage::CopyDst) { if (!first) { o << "|"; } first = false; o << "CopyDst"; value &= ~TextureUsage::CopyDst; } if (value & TextureUsage::TextureBinding) { if (!first) { o << "|"; } first = false; o << "TextureBinding"; value &= ~TextureUsage::TextureBinding; } if (value & TextureUsage::StorageBinding) { if (!first) { o << "|"; } first = false; o << "StorageBinding"; value &= ~TextureUsage::StorageBinding; } if (value & TextureUsage::RenderAttachment) { if (!first) { o << "|"; } first = false; o << "RenderAttachment"; value &= ~TextureUsage::RenderAttachment; } if (value & TextureUsage::TransientAttachment) { if (!first) { o << "|"; } first = false; o << "TransientAttachment"; value &= ~TextureUsage::TransientAttachment; } if (static_cast<bool>(value)) { if (!first) { o << "|"; } o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<TextureUsage>::type>(value); } if (moreThanOneBit) { o << ")"; } return o; } } // namespace wgpu #endif // WEBGPU_CPP_PRINT_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/webgpu_cpp_chained_struct.h
#ifdef __EMSCRIPTEN__ #error "Do not include this header. Emscripten already provides headers needed for WebGPU." #endif #ifndef WEBGPU_CPP_CHAINED_STRUCT_H_ #define WEBGPU_CPP_CHAINED_STRUCT_H_ #include <cstddef> #include <cstdint> // This header file declares the ChainedStruct structures separately from the WebGPU // headers so that dependencies can directly extend structures without including the larger header // which exposes capabilities that may require correctly set proc tables. namespace wgpu { namespace detail { constexpr size_t ConstexprMax(size_t a, size_t b) { return a > b ? a : b; } } // namespace detail enum class SType : uint32_t; struct ChainedStruct { ChainedStruct const * nextInChain = nullptr; SType sType = SType(0u); }; struct ChainedStructOut { ChainedStructOut * nextInChain = nullptr; SType sType = SType(0u); }; } // namespace wgpu} #endif // WEBGPU_CPP_CHAINED_STRUCT_H_
0
repos/simulations/libs/zgpu/libs/dawn/include
repos/simulations/libs/zgpu/libs/dawn/include/dawn/webgpu.h
// BSD 3-Clause License // // Copyright (c) 2019, "WebGPU native" developers // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef __EMSCRIPTEN__ #error "Do not include this header. Emscripten already provides headers needed for WebGPU." #endif #ifndef WEBGPU_H_ #define WEBGPU_H_ #if defined(WGPU_SHARED_LIBRARY) # if defined(_WIN32) # if defined(WGPU_IMPLEMENTATION) # define WGPU_EXPORT __declspec(dllexport) # else # define WGPU_EXPORT __declspec(dllimport) # endif # else // defined(_WIN32) # if defined(WGPU_IMPLEMENTATION) # define WGPU_EXPORT __attribute__((visibility("default"))) # else # define WGPU_EXPORT # endif # endif // defined(_WIN32) #else // defined(WGPU_SHARED_LIBRARY) # define WGPU_EXPORT #endif // defined(WGPU_SHARED_LIBRARY) #if !defined(WGPU_OBJECT_ATTRIBUTE) #define WGPU_OBJECT_ATTRIBUTE #endif #if !defined(WGPU_ENUM_ATTRIBUTE) #define WGPU_ENUM_ATTRIBUTE #endif #if !defined(WGPU_STRUCTURE_ATTRIBUTE) #define WGPU_STRUCTURE_ATTRIBUTE #endif #if !defined(WGPU_FUNCTION_ATTRIBUTE) #define WGPU_FUNCTION_ATTRIBUTE #endif #if !defined(WGPU_NULLABLE) #define WGPU_NULLABLE #endif #include <stdint.h> #include <stddef.h> #include <stdbool.h> #define WGPU_ARRAY_LAYER_COUNT_UNDEFINED (0xffffffffUL) #define WGPU_COPY_STRIDE_UNDEFINED (0xffffffffUL) #define WGPU_LIMIT_U32_UNDEFINED (0xffffffffUL) #define WGPU_LIMIT_U64_UNDEFINED (0xffffffffffffffffULL) #define WGPU_MIP_LEVEL_COUNT_UNDEFINED (0xffffffffUL) #define WGPU_WHOLE_MAP_SIZE SIZE_MAX #define WGPU_WHOLE_SIZE (0xffffffffffffffffULL) typedef uint32_t WGPUFlags; typedef struct WGPUAdapterImpl* WGPUAdapter WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUBindGroupImpl* WGPUBindGroup WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUBindGroupLayoutImpl* WGPUBindGroupLayout WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUBufferImpl* WGPUBuffer WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUCommandBufferImpl* WGPUCommandBuffer WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUCommandEncoderImpl* WGPUCommandEncoder WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUComputePassEncoderImpl* WGPUComputePassEncoder WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUComputePipelineImpl* WGPUComputePipeline WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUDeviceImpl* WGPUDevice WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUExternalTextureImpl* WGPUExternalTexture WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUInstanceImpl* WGPUInstance WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUPipelineLayoutImpl* WGPUPipelineLayout WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUQuerySetImpl* WGPUQuerySet WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUQueueImpl* WGPUQueue WGPU_OBJECT_ATTRIBUTE; typedef struct WGPURenderBundleImpl* WGPURenderBundle WGPU_OBJECT_ATTRIBUTE; typedef struct WGPURenderBundleEncoderImpl* WGPURenderBundleEncoder WGPU_OBJECT_ATTRIBUTE; typedef struct WGPURenderPassEncoderImpl* WGPURenderPassEncoder WGPU_OBJECT_ATTRIBUTE; typedef struct WGPURenderPipelineImpl* WGPURenderPipeline WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUSamplerImpl* WGPUSampler WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUShaderModuleImpl* WGPUShaderModule WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUSurfaceImpl* WGPUSurface WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUSwapChainImpl* WGPUSwapChain WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUTextureImpl* WGPUTexture WGPU_OBJECT_ATTRIBUTE; typedef struct WGPUTextureViewImpl* WGPUTextureView WGPU_OBJECT_ATTRIBUTE; // Structure forward declarations struct WGPUAdapterProperties; struct WGPUBindGroupEntry; struct WGPUBlendComponent; struct WGPUBufferBindingLayout; struct WGPUBufferDescriptor; struct WGPUColor; struct WGPUCommandBufferDescriptor; struct WGPUCommandEncoderDescriptor; struct WGPUCompilationMessage; struct WGPUComputePassTimestampWrite; struct WGPUConstantEntry; struct WGPUCopyTextureForBrowserOptions; struct WGPUDawnAdapterPropertiesPowerPreference; struct WGPUDawnBufferDescriptorErrorInfoFromWireClient; struct WGPUDawnCacheDeviceDescriptor; struct WGPUDawnEncoderInternalUsageDescriptor; struct WGPUDawnMultisampleStateRenderToSingleSampled; struct WGPUDawnRenderPassColorAttachmentRenderToSingleSampled; struct WGPUDawnShaderModuleSPIRVOptionsDescriptor; struct WGPUDawnTextureInternalUsageDescriptor; struct WGPUDawnTogglesDescriptor; struct WGPUExtent2D; struct WGPUExtent3D; struct WGPUExternalTextureBindingEntry; struct WGPUExternalTextureBindingLayout; struct WGPUInstanceDescriptor; struct WGPULimits; struct WGPUMultisampleState; struct WGPUOrigin2D; struct WGPUOrigin3D; struct WGPUPipelineLayoutDescriptor; struct WGPUPrimitiveDepthClipControl; struct WGPUPrimitiveState; struct WGPUQuerySetDescriptor; struct WGPUQueueDescriptor; struct WGPURenderBundleDescriptor; struct WGPURenderBundleEncoderDescriptor; struct WGPURenderPassDepthStencilAttachment; struct WGPURenderPassDescriptorMaxDrawCount; struct WGPURenderPassTimestampWrite; struct WGPURequestAdapterOptions; struct WGPUSamplerBindingLayout; struct WGPUSamplerDescriptor; struct WGPUShaderModuleDescriptor; struct WGPUShaderModuleSPIRVDescriptor; struct WGPUShaderModuleWGSLDescriptor; struct WGPUStencilFaceState; struct WGPUStorageTextureBindingLayout; struct WGPUSurfaceDescriptor; struct WGPUSurfaceDescriptorFromAndroidNativeWindow; struct WGPUSurfaceDescriptorFromCanvasHTMLSelector; struct WGPUSurfaceDescriptorFromMetalLayer; struct WGPUSurfaceDescriptorFromWaylandSurface; struct WGPUSurfaceDescriptorFromWindowsCoreWindow; struct WGPUSurfaceDescriptorFromWindowsHWND; struct WGPUSurfaceDescriptorFromWindowsSwapChainPanel; struct WGPUSurfaceDescriptorFromXlibWindow; struct WGPUSwapChainDescriptor; struct WGPUTextureBindingLayout; struct WGPUTextureDataLayout; struct WGPUTextureViewDescriptor; struct WGPUVertexAttribute; struct WGPUBindGroupDescriptor; struct WGPUBindGroupLayoutEntry; struct WGPUBlendState; struct WGPUCompilationInfo; struct WGPUComputePassDescriptor; struct WGPUDepthStencilState; struct WGPUExternalTextureDescriptor; struct WGPUImageCopyBuffer; struct WGPUImageCopyExternalTexture; struct WGPUImageCopyTexture; struct WGPUProgrammableStageDescriptor; struct WGPURenderPassColorAttachment; struct WGPURequiredLimits; struct WGPUSupportedLimits; struct WGPUTextureDescriptor; struct WGPUVertexBufferLayout; struct WGPUBindGroupLayoutDescriptor; struct WGPUColorTargetState; struct WGPUComputePipelineDescriptor; struct WGPUDeviceDescriptor; struct WGPURenderPassDescriptor; struct WGPUVertexState; struct WGPUFragmentState; struct WGPURenderPipelineDescriptor; typedef enum WGPUAdapterType { WGPUAdapterType_DiscreteGPU = 0x00000000, WGPUAdapterType_IntegratedGPU = 0x00000001, WGPUAdapterType_CPU = 0x00000002, WGPUAdapterType_Unknown = 0x00000003, WGPUAdapterType_Force32 = 0x7FFFFFFF } WGPUAdapterType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUAddressMode { WGPUAddressMode_Repeat = 0x00000000, WGPUAddressMode_MirrorRepeat = 0x00000001, WGPUAddressMode_ClampToEdge = 0x00000002, WGPUAddressMode_Force32 = 0x7FFFFFFF } WGPUAddressMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUAlphaMode { WGPUAlphaMode_Premultiplied = 0x00000000, WGPUAlphaMode_Unpremultiplied = 0x00000001, WGPUAlphaMode_Opaque = 0x00000002, WGPUAlphaMode_Force32 = 0x7FFFFFFF } WGPUAlphaMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBackendType { WGPUBackendType_Undefined = 0x00000000, WGPUBackendType_Null = 0x00000001, WGPUBackendType_WebGPU = 0x00000002, WGPUBackendType_D3D11 = 0x00000003, WGPUBackendType_D3D12 = 0x00000004, WGPUBackendType_Metal = 0x00000005, WGPUBackendType_Vulkan = 0x00000006, WGPUBackendType_OpenGL = 0x00000007, WGPUBackendType_OpenGLES = 0x00000008, WGPUBackendType_Force32 = 0x7FFFFFFF } WGPUBackendType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBlendFactor { WGPUBlendFactor_Zero = 0x00000000, WGPUBlendFactor_One = 0x00000001, WGPUBlendFactor_Src = 0x00000002, WGPUBlendFactor_OneMinusSrc = 0x00000003, WGPUBlendFactor_SrcAlpha = 0x00000004, WGPUBlendFactor_OneMinusSrcAlpha = 0x00000005, WGPUBlendFactor_Dst = 0x00000006, WGPUBlendFactor_OneMinusDst = 0x00000007, WGPUBlendFactor_DstAlpha = 0x00000008, WGPUBlendFactor_OneMinusDstAlpha = 0x00000009, WGPUBlendFactor_SrcAlphaSaturated = 0x0000000A, WGPUBlendFactor_Constant = 0x0000000B, WGPUBlendFactor_OneMinusConstant = 0x0000000C, WGPUBlendFactor_Force32 = 0x7FFFFFFF } WGPUBlendFactor WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBlendOperation { WGPUBlendOperation_Add = 0x00000000, WGPUBlendOperation_Subtract = 0x00000001, WGPUBlendOperation_ReverseSubtract = 0x00000002, WGPUBlendOperation_Min = 0x00000003, WGPUBlendOperation_Max = 0x00000004, WGPUBlendOperation_Force32 = 0x7FFFFFFF } WGPUBlendOperation WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBufferBindingType { WGPUBufferBindingType_Undefined = 0x00000000, WGPUBufferBindingType_Uniform = 0x00000001, WGPUBufferBindingType_Storage = 0x00000002, WGPUBufferBindingType_ReadOnlyStorage = 0x00000003, WGPUBufferBindingType_Force32 = 0x7FFFFFFF } WGPUBufferBindingType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBufferMapAsyncStatus { WGPUBufferMapAsyncStatus_Success = 0x00000000, WGPUBufferMapAsyncStatus_ValidationError = 0x00000001, WGPUBufferMapAsyncStatus_Unknown = 0x00000002, WGPUBufferMapAsyncStatus_DeviceLost = 0x00000003, WGPUBufferMapAsyncStatus_DestroyedBeforeCallback = 0x00000004, WGPUBufferMapAsyncStatus_UnmappedBeforeCallback = 0x00000005, WGPUBufferMapAsyncStatus_MappingAlreadyPending = 0x00000006, WGPUBufferMapAsyncStatus_OffsetOutOfRange = 0x00000007, WGPUBufferMapAsyncStatus_SizeOutOfRange = 0x00000008, WGPUBufferMapAsyncStatus_Force32 = 0x7FFFFFFF } WGPUBufferMapAsyncStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBufferMapState { WGPUBufferMapState_Unmapped = 0x00000000, WGPUBufferMapState_Pending = 0x00000001, WGPUBufferMapState_Mapped = 0x00000002, WGPUBufferMapState_Force32 = 0x7FFFFFFF } WGPUBufferMapState WGPU_ENUM_ATTRIBUTE; typedef enum WGPUCompareFunction { WGPUCompareFunction_Undefined = 0x00000000, WGPUCompareFunction_Never = 0x00000001, WGPUCompareFunction_Less = 0x00000002, WGPUCompareFunction_LessEqual = 0x00000003, WGPUCompareFunction_Greater = 0x00000004, WGPUCompareFunction_GreaterEqual = 0x00000005, WGPUCompareFunction_Equal = 0x00000006, WGPUCompareFunction_NotEqual = 0x00000007, WGPUCompareFunction_Always = 0x00000008, WGPUCompareFunction_Force32 = 0x7FFFFFFF } WGPUCompareFunction WGPU_ENUM_ATTRIBUTE; typedef enum WGPUCompilationInfoRequestStatus { WGPUCompilationInfoRequestStatus_Success = 0x00000000, WGPUCompilationInfoRequestStatus_Error = 0x00000001, WGPUCompilationInfoRequestStatus_DeviceLost = 0x00000002, WGPUCompilationInfoRequestStatus_Unknown = 0x00000003, WGPUCompilationInfoRequestStatus_Force32 = 0x7FFFFFFF } WGPUCompilationInfoRequestStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPUCompilationMessageType { WGPUCompilationMessageType_Error = 0x00000000, WGPUCompilationMessageType_Warning = 0x00000001, WGPUCompilationMessageType_Info = 0x00000002, WGPUCompilationMessageType_Force32 = 0x7FFFFFFF } WGPUCompilationMessageType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUComputePassTimestampLocation { WGPUComputePassTimestampLocation_Beginning = 0x00000000, WGPUComputePassTimestampLocation_End = 0x00000001, WGPUComputePassTimestampLocation_Force32 = 0x7FFFFFFF } WGPUComputePassTimestampLocation WGPU_ENUM_ATTRIBUTE; typedef enum WGPUCreatePipelineAsyncStatus { WGPUCreatePipelineAsyncStatus_Success = 0x00000000, WGPUCreatePipelineAsyncStatus_ValidationError = 0x00000001, WGPUCreatePipelineAsyncStatus_InternalError = 0x00000002, WGPUCreatePipelineAsyncStatus_DeviceLost = 0x00000003, WGPUCreatePipelineAsyncStatus_DeviceDestroyed = 0x00000004, WGPUCreatePipelineAsyncStatus_Unknown = 0x00000005, WGPUCreatePipelineAsyncStatus_Force32 = 0x7FFFFFFF } WGPUCreatePipelineAsyncStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPUCullMode { WGPUCullMode_None = 0x00000000, WGPUCullMode_Front = 0x00000001, WGPUCullMode_Back = 0x00000002, WGPUCullMode_Force32 = 0x7FFFFFFF } WGPUCullMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUDeviceLostReason { WGPUDeviceLostReason_Undefined = 0x00000000, WGPUDeviceLostReason_Destroyed = 0x00000001, WGPUDeviceLostReason_Force32 = 0x7FFFFFFF } WGPUDeviceLostReason WGPU_ENUM_ATTRIBUTE; typedef enum WGPUErrorFilter { WGPUErrorFilter_Validation = 0x00000000, WGPUErrorFilter_OutOfMemory = 0x00000001, WGPUErrorFilter_Internal = 0x00000002, WGPUErrorFilter_Force32 = 0x7FFFFFFF } WGPUErrorFilter WGPU_ENUM_ATTRIBUTE; typedef enum WGPUErrorType { WGPUErrorType_NoError = 0x00000000, WGPUErrorType_Validation = 0x00000001, WGPUErrorType_OutOfMemory = 0x00000002, WGPUErrorType_Internal = 0x00000003, WGPUErrorType_Unknown = 0x00000004, WGPUErrorType_DeviceLost = 0x00000005, WGPUErrorType_Force32 = 0x7FFFFFFF } WGPUErrorType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUExternalTextureRotation { WGPUExternalTextureRotation_Rotate0Degrees = 0x00000000, WGPUExternalTextureRotation_Rotate90Degrees = 0x00000001, WGPUExternalTextureRotation_Rotate180Degrees = 0x00000002, WGPUExternalTextureRotation_Rotate270Degrees = 0x00000003, WGPUExternalTextureRotation_Force32 = 0x7FFFFFFF } WGPUExternalTextureRotation WGPU_ENUM_ATTRIBUTE; typedef enum WGPUFeatureName { WGPUFeatureName_Undefined = 0x00000000, WGPUFeatureName_DepthClipControl = 0x00000001, WGPUFeatureName_Depth32FloatStencil8 = 0x00000002, WGPUFeatureName_TimestampQuery = 0x00000003, WGPUFeatureName_PipelineStatisticsQuery = 0x00000004, WGPUFeatureName_TextureCompressionBC = 0x00000005, WGPUFeatureName_TextureCompressionETC2 = 0x00000006, WGPUFeatureName_TextureCompressionASTC = 0x00000007, WGPUFeatureName_IndirectFirstInstance = 0x00000008, WGPUFeatureName_ShaderF16 = 0x00000009, WGPUFeatureName_RG11B10UfloatRenderable = 0x0000000A, WGPUFeatureName_BGRA8UnormStorage = 0x0000000B, WGPUFeatureName_Float32Filterable = 0x0000000C, WGPUFeatureName_DawnShaderFloat16 = 0x000003E9, WGPUFeatureName_DawnInternalUsages = 0x000003EA, WGPUFeatureName_DawnMultiPlanarFormats = 0x000003EB, WGPUFeatureName_DawnNative = 0x000003EC, WGPUFeatureName_ChromiumExperimentalDp4a = 0x000003ED, WGPUFeatureName_TimestampQueryInsidePasses = 0x000003EE, WGPUFeatureName_ImplicitDeviceSynchronization = 0x000003EF, WGPUFeatureName_SurfaceCapabilities = 0x000003F0, WGPUFeatureName_TransientAttachments = 0x000003F1, WGPUFeatureName_MSAARenderToSingleSampled = 0x000003F2, WGPUFeatureName_Force32 = 0x7FFFFFFF } WGPUFeatureName WGPU_ENUM_ATTRIBUTE; typedef enum WGPUFilterMode { WGPUFilterMode_Nearest = 0x00000000, WGPUFilterMode_Linear = 0x00000001, WGPUFilterMode_Force32 = 0x7FFFFFFF } WGPUFilterMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUFrontFace { WGPUFrontFace_CCW = 0x00000000, WGPUFrontFace_CW = 0x00000001, WGPUFrontFace_Force32 = 0x7FFFFFFF } WGPUFrontFace WGPU_ENUM_ATTRIBUTE; typedef enum WGPUIndexFormat { WGPUIndexFormat_Undefined = 0x00000000, WGPUIndexFormat_Uint16 = 0x00000001, WGPUIndexFormat_Uint32 = 0x00000002, WGPUIndexFormat_Force32 = 0x7FFFFFFF } WGPUIndexFormat WGPU_ENUM_ATTRIBUTE; typedef enum WGPULoadOp { WGPULoadOp_Undefined = 0x00000000, WGPULoadOp_Clear = 0x00000001, WGPULoadOp_Load = 0x00000002, WGPULoadOp_Force32 = 0x7FFFFFFF } WGPULoadOp WGPU_ENUM_ATTRIBUTE; typedef enum WGPULoggingType { WGPULoggingType_Verbose = 0x00000000, WGPULoggingType_Info = 0x00000001, WGPULoggingType_Warning = 0x00000002, WGPULoggingType_Error = 0x00000003, WGPULoggingType_Force32 = 0x7FFFFFFF } WGPULoggingType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUMipmapFilterMode { WGPUMipmapFilterMode_Nearest = 0x00000000, WGPUMipmapFilterMode_Linear = 0x00000001, WGPUMipmapFilterMode_Force32 = 0x7FFFFFFF } WGPUMipmapFilterMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUPipelineStatisticName { WGPUPipelineStatisticName_VertexShaderInvocations = 0x00000000, WGPUPipelineStatisticName_ClipperInvocations = 0x00000001, WGPUPipelineStatisticName_ClipperPrimitivesOut = 0x00000002, WGPUPipelineStatisticName_FragmentShaderInvocations = 0x00000003, WGPUPipelineStatisticName_ComputeShaderInvocations = 0x00000004, WGPUPipelineStatisticName_Force32 = 0x7FFFFFFF } WGPUPipelineStatisticName WGPU_ENUM_ATTRIBUTE; typedef enum WGPUPowerPreference { WGPUPowerPreference_Undefined = 0x00000000, WGPUPowerPreference_LowPower = 0x00000001, WGPUPowerPreference_HighPerformance = 0x00000002, WGPUPowerPreference_Force32 = 0x7FFFFFFF } WGPUPowerPreference WGPU_ENUM_ATTRIBUTE; typedef enum WGPUPresentMode { WGPUPresentMode_Immediate = 0x00000000, WGPUPresentMode_Mailbox = 0x00000001, WGPUPresentMode_Fifo = 0x00000002, WGPUPresentMode_Force32 = 0x7FFFFFFF } WGPUPresentMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUPrimitiveTopology { WGPUPrimitiveTopology_PointList = 0x00000000, WGPUPrimitiveTopology_LineList = 0x00000001, WGPUPrimitiveTopology_LineStrip = 0x00000002, WGPUPrimitiveTopology_TriangleList = 0x00000003, WGPUPrimitiveTopology_TriangleStrip = 0x00000004, WGPUPrimitiveTopology_Force32 = 0x7FFFFFFF } WGPUPrimitiveTopology WGPU_ENUM_ATTRIBUTE; typedef enum WGPUQueryType { WGPUQueryType_Occlusion = 0x00000000, WGPUQueryType_PipelineStatistics = 0x00000001, WGPUQueryType_Timestamp = 0x00000002, WGPUQueryType_Force32 = 0x7FFFFFFF } WGPUQueryType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUQueueWorkDoneStatus { WGPUQueueWorkDoneStatus_Success = 0x00000000, WGPUQueueWorkDoneStatus_Error = 0x00000001, WGPUQueueWorkDoneStatus_Unknown = 0x00000002, WGPUQueueWorkDoneStatus_DeviceLost = 0x00000003, WGPUQueueWorkDoneStatus_Force32 = 0x7FFFFFFF } WGPUQueueWorkDoneStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPURenderPassTimestampLocation { WGPURenderPassTimestampLocation_Beginning = 0x00000000, WGPURenderPassTimestampLocation_End = 0x00000001, WGPURenderPassTimestampLocation_Force32 = 0x7FFFFFFF } WGPURenderPassTimestampLocation WGPU_ENUM_ATTRIBUTE; typedef enum WGPURequestAdapterStatus { WGPURequestAdapterStatus_Success = 0x00000000, WGPURequestAdapterStatus_Unavailable = 0x00000001, WGPURequestAdapterStatus_Error = 0x00000002, WGPURequestAdapterStatus_Unknown = 0x00000003, WGPURequestAdapterStatus_Force32 = 0x7FFFFFFF } WGPURequestAdapterStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPURequestDeviceStatus { WGPURequestDeviceStatus_Success = 0x00000000, WGPURequestDeviceStatus_Error = 0x00000001, WGPURequestDeviceStatus_Unknown = 0x00000002, WGPURequestDeviceStatus_Force32 = 0x7FFFFFFF } WGPURequestDeviceStatus WGPU_ENUM_ATTRIBUTE; typedef enum WGPUSType { WGPUSType_Invalid = 0x00000000, WGPUSType_SurfaceDescriptorFromMetalLayer = 0x00000001, WGPUSType_SurfaceDescriptorFromWindowsHWND = 0x00000002, WGPUSType_SurfaceDescriptorFromXlibWindow = 0x00000003, WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector = 0x00000004, WGPUSType_ShaderModuleSPIRVDescriptor = 0x00000005, WGPUSType_ShaderModuleWGSLDescriptor = 0x00000006, WGPUSType_PrimitiveDepthClipControl = 0x00000007, WGPUSType_SurfaceDescriptorFromWaylandSurface = 0x00000008, WGPUSType_SurfaceDescriptorFromAndroidNativeWindow = 0x00000009, WGPUSType_SurfaceDescriptorFromWindowsCoreWindow = 0x0000000B, WGPUSType_ExternalTextureBindingEntry = 0x0000000C, WGPUSType_ExternalTextureBindingLayout = 0x0000000D, WGPUSType_SurfaceDescriptorFromWindowsSwapChainPanel = 0x0000000E, WGPUSType_RenderPassDescriptorMaxDrawCount = 0x0000000F, WGPUSType_DawnTextureInternalUsageDescriptor = 0x000003E8, WGPUSType_DawnEncoderInternalUsageDescriptor = 0x000003EB, WGPUSType_DawnInstanceDescriptor = 0x000003EC, WGPUSType_DawnCacheDeviceDescriptor = 0x000003ED, WGPUSType_DawnAdapterPropertiesPowerPreference = 0x000003EE, WGPUSType_DawnBufferDescriptorErrorInfoFromWireClient = 0x000003EF, WGPUSType_DawnTogglesDescriptor = 0x000003F0, WGPUSType_DawnShaderModuleSPIRVOptionsDescriptor = 0x000003F1, WGPUSType_RequestAdapterOptionsLUID = 0x000003F2, WGPUSType_RequestAdapterOptionsGetGLProc = 0x000003F3, WGPUSType_DawnMultisampleStateRenderToSingleSampled = 0x000003F4, WGPUSType_DawnRenderPassColorAttachmentRenderToSingleSampled = 0x000003F5, WGPUSType_Force32 = 0x7FFFFFFF } WGPUSType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUSamplerBindingType { WGPUSamplerBindingType_Undefined = 0x00000000, WGPUSamplerBindingType_Filtering = 0x00000001, WGPUSamplerBindingType_NonFiltering = 0x00000002, WGPUSamplerBindingType_Comparison = 0x00000003, WGPUSamplerBindingType_Force32 = 0x7FFFFFFF } WGPUSamplerBindingType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUStencilOperation { WGPUStencilOperation_Keep = 0x00000000, WGPUStencilOperation_Zero = 0x00000001, WGPUStencilOperation_Replace = 0x00000002, WGPUStencilOperation_Invert = 0x00000003, WGPUStencilOperation_IncrementClamp = 0x00000004, WGPUStencilOperation_DecrementClamp = 0x00000005, WGPUStencilOperation_IncrementWrap = 0x00000006, WGPUStencilOperation_DecrementWrap = 0x00000007, WGPUStencilOperation_Force32 = 0x7FFFFFFF } WGPUStencilOperation WGPU_ENUM_ATTRIBUTE; typedef enum WGPUStorageTextureAccess { WGPUStorageTextureAccess_Undefined = 0x00000000, WGPUStorageTextureAccess_WriteOnly = 0x00000001, WGPUStorageTextureAccess_Force32 = 0x7FFFFFFF } WGPUStorageTextureAccess WGPU_ENUM_ATTRIBUTE; typedef enum WGPUStoreOp { WGPUStoreOp_Undefined = 0x00000000, WGPUStoreOp_Store = 0x00000001, WGPUStoreOp_Discard = 0x00000002, WGPUStoreOp_Force32 = 0x7FFFFFFF } WGPUStoreOp WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureAspect { WGPUTextureAspect_All = 0x00000000, WGPUTextureAspect_StencilOnly = 0x00000001, WGPUTextureAspect_DepthOnly = 0x00000002, WGPUTextureAspect_Plane0Only = 0x00000003, WGPUTextureAspect_Plane1Only = 0x00000004, WGPUTextureAspect_Force32 = 0x7FFFFFFF } WGPUTextureAspect WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureDimension { WGPUTextureDimension_1D = 0x00000000, WGPUTextureDimension_2D = 0x00000001, WGPUTextureDimension_3D = 0x00000002, WGPUTextureDimension_Force32 = 0x7FFFFFFF } WGPUTextureDimension WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureFormat { WGPUTextureFormat_Undefined = 0x00000000, WGPUTextureFormat_R8Unorm = 0x00000001, WGPUTextureFormat_R8Snorm = 0x00000002, WGPUTextureFormat_R8Uint = 0x00000003, WGPUTextureFormat_R8Sint = 0x00000004, WGPUTextureFormat_R16Uint = 0x00000005, WGPUTextureFormat_R16Sint = 0x00000006, WGPUTextureFormat_R16Float = 0x00000007, WGPUTextureFormat_RG8Unorm = 0x00000008, WGPUTextureFormat_RG8Snorm = 0x00000009, WGPUTextureFormat_RG8Uint = 0x0000000A, WGPUTextureFormat_RG8Sint = 0x0000000B, WGPUTextureFormat_R32Float = 0x0000000C, WGPUTextureFormat_R32Uint = 0x0000000D, WGPUTextureFormat_R32Sint = 0x0000000E, WGPUTextureFormat_RG16Uint = 0x0000000F, WGPUTextureFormat_RG16Sint = 0x00000010, WGPUTextureFormat_RG16Float = 0x00000011, WGPUTextureFormat_RGBA8Unorm = 0x00000012, WGPUTextureFormat_RGBA8UnormSrgb = 0x00000013, WGPUTextureFormat_RGBA8Snorm = 0x00000014, WGPUTextureFormat_RGBA8Uint = 0x00000015, WGPUTextureFormat_RGBA8Sint = 0x00000016, WGPUTextureFormat_BGRA8Unorm = 0x00000017, WGPUTextureFormat_BGRA8UnormSrgb = 0x00000018, WGPUTextureFormat_RGB10A2Unorm = 0x00000019, WGPUTextureFormat_RG11B10Ufloat = 0x0000001A, WGPUTextureFormat_RGB9E5Ufloat = 0x0000001B, WGPUTextureFormat_RG32Float = 0x0000001C, WGPUTextureFormat_RG32Uint = 0x0000001D, WGPUTextureFormat_RG32Sint = 0x0000001E, WGPUTextureFormat_RGBA16Uint = 0x0000001F, WGPUTextureFormat_RGBA16Sint = 0x00000020, WGPUTextureFormat_RGBA16Float = 0x00000021, WGPUTextureFormat_RGBA32Float = 0x00000022, WGPUTextureFormat_RGBA32Uint = 0x00000023, WGPUTextureFormat_RGBA32Sint = 0x00000024, WGPUTextureFormat_Stencil8 = 0x00000025, WGPUTextureFormat_Depth16Unorm = 0x00000026, WGPUTextureFormat_Depth24Plus = 0x00000027, WGPUTextureFormat_Depth24PlusStencil8 = 0x00000028, WGPUTextureFormat_Depth32Float = 0x00000029, WGPUTextureFormat_Depth32FloatStencil8 = 0x0000002A, WGPUTextureFormat_BC1RGBAUnorm = 0x0000002B, WGPUTextureFormat_BC1RGBAUnormSrgb = 0x0000002C, WGPUTextureFormat_BC2RGBAUnorm = 0x0000002D, WGPUTextureFormat_BC2RGBAUnormSrgb = 0x0000002E, WGPUTextureFormat_BC3RGBAUnorm = 0x0000002F, WGPUTextureFormat_BC3RGBAUnormSrgb = 0x00000030, WGPUTextureFormat_BC4RUnorm = 0x00000031, WGPUTextureFormat_BC4RSnorm = 0x00000032, WGPUTextureFormat_BC5RGUnorm = 0x00000033, WGPUTextureFormat_BC5RGSnorm = 0x00000034, WGPUTextureFormat_BC6HRGBUfloat = 0x00000035, WGPUTextureFormat_BC6HRGBFloat = 0x00000036, WGPUTextureFormat_BC7RGBAUnorm = 0x00000037, WGPUTextureFormat_BC7RGBAUnormSrgb = 0x00000038, WGPUTextureFormat_ETC2RGB8Unorm = 0x00000039, WGPUTextureFormat_ETC2RGB8UnormSrgb = 0x0000003A, WGPUTextureFormat_ETC2RGB8A1Unorm = 0x0000003B, WGPUTextureFormat_ETC2RGB8A1UnormSrgb = 0x0000003C, WGPUTextureFormat_ETC2RGBA8Unorm = 0x0000003D, WGPUTextureFormat_ETC2RGBA8UnormSrgb = 0x0000003E, WGPUTextureFormat_EACR11Unorm = 0x0000003F, WGPUTextureFormat_EACR11Snorm = 0x00000040, WGPUTextureFormat_EACRG11Unorm = 0x00000041, WGPUTextureFormat_EACRG11Snorm = 0x00000042, WGPUTextureFormat_ASTC4x4Unorm = 0x00000043, WGPUTextureFormat_ASTC4x4UnormSrgb = 0x00000044, WGPUTextureFormat_ASTC5x4Unorm = 0x00000045, WGPUTextureFormat_ASTC5x4UnormSrgb = 0x00000046, WGPUTextureFormat_ASTC5x5Unorm = 0x00000047, WGPUTextureFormat_ASTC5x5UnormSrgb = 0x00000048, WGPUTextureFormat_ASTC6x5Unorm = 0x00000049, WGPUTextureFormat_ASTC6x5UnormSrgb = 0x0000004A, WGPUTextureFormat_ASTC6x6Unorm = 0x0000004B, WGPUTextureFormat_ASTC6x6UnormSrgb = 0x0000004C, WGPUTextureFormat_ASTC8x5Unorm = 0x0000004D, WGPUTextureFormat_ASTC8x5UnormSrgb = 0x0000004E, WGPUTextureFormat_ASTC8x6Unorm = 0x0000004F, WGPUTextureFormat_ASTC8x6UnormSrgb = 0x00000050, WGPUTextureFormat_ASTC8x8Unorm = 0x00000051, WGPUTextureFormat_ASTC8x8UnormSrgb = 0x00000052, WGPUTextureFormat_ASTC10x5Unorm = 0x00000053, WGPUTextureFormat_ASTC10x5UnormSrgb = 0x00000054, WGPUTextureFormat_ASTC10x6Unorm = 0x00000055, WGPUTextureFormat_ASTC10x6UnormSrgb = 0x00000056, WGPUTextureFormat_ASTC10x8Unorm = 0x00000057, WGPUTextureFormat_ASTC10x8UnormSrgb = 0x00000058, WGPUTextureFormat_ASTC10x10Unorm = 0x00000059, WGPUTextureFormat_ASTC10x10UnormSrgb = 0x0000005A, WGPUTextureFormat_ASTC12x10Unorm = 0x0000005B, WGPUTextureFormat_ASTC12x10UnormSrgb = 0x0000005C, WGPUTextureFormat_ASTC12x12Unorm = 0x0000005D, WGPUTextureFormat_ASTC12x12UnormSrgb = 0x0000005E, WGPUTextureFormat_R8BG8Biplanar420Unorm = 0x0000005F, WGPUTextureFormat_Force32 = 0x7FFFFFFF } WGPUTextureFormat WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureSampleType { WGPUTextureSampleType_Undefined = 0x00000000, WGPUTextureSampleType_Float = 0x00000001, WGPUTextureSampleType_UnfilterableFloat = 0x00000002, WGPUTextureSampleType_Depth = 0x00000003, WGPUTextureSampleType_Sint = 0x00000004, WGPUTextureSampleType_Uint = 0x00000005, WGPUTextureSampleType_Force32 = 0x7FFFFFFF } WGPUTextureSampleType WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureViewDimension { WGPUTextureViewDimension_Undefined = 0x00000000, WGPUTextureViewDimension_1D = 0x00000001, WGPUTextureViewDimension_2D = 0x00000002, WGPUTextureViewDimension_2DArray = 0x00000003, WGPUTextureViewDimension_Cube = 0x00000004, WGPUTextureViewDimension_CubeArray = 0x00000005, WGPUTextureViewDimension_3D = 0x00000006, WGPUTextureViewDimension_Force32 = 0x7FFFFFFF } WGPUTextureViewDimension WGPU_ENUM_ATTRIBUTE; typedef enum WGPUVertexFormat { WGPUVertexFormat_Undefined = 0x00000000, WGPUVertexFormat_Uint8x2 = 0x00000001, WGPUVertexFormat_Uint8x4 = 0x00000002, WGPUVertexFormat_Sint8x2 = 0x00000003, WGPUVertexFormat_Sint8x4 = 0x00000004, WGPUVertexFormat_Unorm8x2 = 0x00000005, WGPUVertexFormat_Unorm8x4 = 0x00000006, WGPUVertexFormat_Snorm8x2 = 0x00000007, WGPUVertexFormat_Snorm8x4 = 0x00000008, WGPUVertexFormat_Uint16x2 = 0x00000009, WGPUVertexFormat_Uint16x4 = 0x0000000A, WGPUVertexFormat_Sint16x2 = 0x0000000B, WGPUVertexFormat_Sint16x4 = 0x0000000C, WGPUVertexFormat_Unorm16x2 = 0x0000000D, WGPUVertexFormat_Unorm16x4 = 0x0000000E, WGPUVertexFormat_Snorm16x2 = 0x0000000F, WGPUVertexFormat_Snorm16x4 = 0x00000010, WGPUVertexFormat_Float16x2 = 0x00000011, WGPUVertexFormat_Float16x4 = 0x00000012, WGPUVertexFormat_Float32 = 0x00000013, WGPUVertexFormat_Float32x2 = 0x00000014, WGPUVertexFormat_Float32x3 = 0x00000015, WGPUVertexFormat_Float32x4 = 0x00000016, WGPUVertexFormat_Uint32 = 0x00000017, WGPUVertexFormat_Uint32x2 = 0x00000018, WGPUVertexFormat_Uint32x3 = 0x00000019, WGPUVertexFormat_Uint32x4 = 0x0000001A, WGPUVertexFormat_Sint32 = 0x0000001B, WGPUVertexFormat_Sint32x2 = 0x0000001C, WGPUVertexFormat_Sint32x3 = 0x0000001D, WGPUVertexFormat_Sint32x4 = 0x0000001E, WGPUVertexFormat_Force32 = 0x7FFFFFFF } WGPUVertexFormat WGPU_ENUM_ATTRIBUTE; typedef enum WGPUVertexStepMode { WGPUVertexStepMode_Vertex = 0x00000000, WGPUVertexStepMode_Instance = 0x00000001, WGPUVertexStepMode_VertexBufferNotUsed = 0x00000002, WGPUVertexStepMode_Force32 = 0x7FFFFFFF } WGPUVertexStepMode WGPU_ENUM_ATTRIBUTE; typedef enum WGPUBufferUsage { WGPUBufferUsage_None = 0x00000000, WGPUBufferUsage_MapRead = 0x00000001, WGPUBufferUsage_MapWrite = 0x00000002, WGPUBufferUsage_CopySrc = 0x00000004, WGPUBufferUsage_CopyDst = 0x00000008, WGPUBufferUsage_Index = 0x00000010, WGPUBufferUsage_Vertex = 0x00000020, WGPUBufferUsage_Uniform = 0x00000040, WGPUBufferUsage_Storage = 0x00000080, WGPUBufferUsage_Indirect = 0x00000100, WGPUBufferUsage_QueryResolve = 0x00000200, WGPUBufferUsage_Force32 = 0x7FFFFFFF } WGPUBufferUsage WGPU_ENUM_ATTRIBUTE; typedef WGPUFlags WGPUBufferUsageFlags WGPU_ENUM_ATTRIBUTE; typedef enum WGPUColorWriteMask { WGPUColorWriteMask_None = 0x00000000, WGPUColorWriteMask_Red = 0x00000001, WGPUColorWriteMask_Green = 0x00000002, WGPUColorWriteMask_Blue = 0x00000004, WGPUColorWriteMask_Alpha = 0x00000008, WGPUColorWriteMask_All = 0x0000000F, WGPUColorWriteMask_Force32 = 0x7FFFFFFF } WGPUColorWriteMask WGPU_ENUM_ATTRIBUTE; typedef WGPUFlags WGPUColorWriteMaskFlags WGPU_ENUM_ATTRIBUTE; typedef enum WGPUMapMode { WGPUMapMode_None = 0x00000000, WGPUMapMode_Read = 0x00000001, WGPUMapMode_Write = 0x00000002, WGPUMapMode_Force32 = 0x7FFFFFFF } WGPUMapMode WGPU_ENUM_ATTRIBUTE; typedef WGPUFlags WGPUMapModeFlags WGPU_ENUM_ATTRIBUTE; typedef enum WGPUShaderStage { WGPUShaderStage_None = 0x00000000, WGPUShaderStage_Vertex = 0x00000001, WGPUShaderStage_Fragment = 0x00000002, WGPUShaderStage_Compute = 0x00000004, WGPUShaderStage_Force32 = 0x7FFFFFFF } WGPUShaderStage WGPU_ENUM_ATTRIBUTE; typedef WGPUFlags WGPUShaderStageFlags WGPU_ENUM_ATTRIBUTE; typedef enum WGPUTextureUsage { WGPUTextureUsage_None = 0x00000000, WGPUTextureUsage_CopySrc = 0x00000001, WGPUTextureUsage_CopyDst = 0x00000002, WGPUTextureUsage_TextureBinding = 0x00000004, WGPUTextureUsage_StorageBinding = 0x00000008, WGPUTextureUsage_RenderAttachment = 0x00000010, WGPUTextureUsage_TransientAttachment = 0x00000020, WGPUTextureUsage_Force32 = 0x7FFFFFFF } WGPUTextureUsage WGPU_ENUM_ATTRIBUTE; typedef WGPUFlags WGPUTextureUsageFlags WGPU_ENUM_ATTRIBUTE; typedef void (*WGPUBufferMapCallback)(WGPUBufferMapAsyncStatus status, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUCompilationInfoCallback)(WGPUCompilationInfoRequestStatus status, struct WGPUCompilationInfo const * compilationInfo, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUCreateComputePipelineAsyncCallback)(WGPUCreatePipelineAsyncStatus status, WGPUComputePipeline pipeline, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUCreateRenderPipelineAsyncCallback)(WGPUCreatePipelineAsyncStatus status, WGPURenderPipeline pipeline, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUDeviceLostCallback)(WGPUDeviceLostReason reason, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUErrorCallback)(WGPUErrorType type, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPULoggingCallback)(WGPULoggingType type, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProc)(void) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUQueueWorkDoneCallback)(WGPUQueueWorkDoneStatus status, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPURequestAdapterCallback)(WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPURequestDeviceCallback)(WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef struct WGPUChainedStruct { struct WGPUChainedStruct const * next; WGPUSType sType; } WGPUChainedStruct WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUChainedStructOut { struct WGPUChainedStructOut * next; WGPUSType sType; } WGPUChainedStructOut WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUAdapterProperties { WGPUChainedStructOut * nextInChain; uint32_t vendorID; char const * vendorName; char const * architecture; uint32_t deviceID; char const * name; char const * driverDescription; WGPUAdapterType adapterType; WGPUBackendType backendType; bool compatibilityMode; } WGPUAdapterProperties WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBindGroupEntry { WGPUChainedStruct const * nextInChain; uint32_t binding; WGPU_NULLABLE WGPUBuffer buffer; uint64_t offset; uint64_t size; WGPU_NULLABLE WGPUSampler sampler; WGPU_NULLABLE WGPUTextureView textureView; } WGPUBindGroupEntry WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBlendComponent { WGPUBlendOperation operation; WGPUBlendFactor srcFactor; WGPUBlendFactor dstFactor; } WGPUBlendComponent WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBufferBindingLayout { WGPUChainedStruct const * nextInChain; WGPUBufferBindingType type; bool hasDynamicOffset; uint64_t minBindingSize; } WGPUBufferBindingLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBufferDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUBufferUsageFlags usage; uint64_t size; bool mappedAtCreation; } WGPUBufferDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUColor { double r; double g; double b; double a; } WGPUColor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUCommandBufferDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPUCommandBufferDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUCommandEncoderDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPUCommandEncoderDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUCompilationMessage { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * message; WGPUCompilationMessageType type; uint64_t lineNum; uint64_t linePos; uint64_t offset; uint64_t length; uint64_t utf16LinePos; uint64_t utf16Offset; uint64_t utf16Length; } WGPUCompilationMessage WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUComputePassTimestampWrite { WGPUQuerySet querySet; uint32_t queryIndex; WGPUComputePassTimestampLocation location; } WGPUComputePassTimestampWrite WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUConstantEntry { WGPUChainedStruct const * nextInChain; char const * key; double value; } WGPUConstantEntry WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUCopyTextureForBrowserOptions { WGPUChainedStruct const * nextInChain; bool flipY; bool needsColorSpaceConversion; WGPUAlphaMode srcAlphaMode; WGPU_NULLABLE float const * srcTransferFunctionParameters; WGPU_NULLABLE float const * conversionMatrix; WGPU_NULLABLE float const * dstTransferFunctionParameters; WGPUAlphaMode dstAlphaMode; bool internalUsage; } WGPUCopyTextureForBrowserOptions WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUAdapterProperties typedef struct WGPUDawnAdapterPropertiesPowerPreference { WGPUChainedStructOut chain; WGPUPowerPreference powerPreference; } WGPUDawnAdapterPropertiesPowerPreference WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUBufferDescriptor typedef struct WGPUDawnBufferDescriptorErrorInfoFromWireClient { WGPUChainedStruct chain; bool outOfMemory; } WGPUDawnBufferDescriptorErrorInfoFromWireClient WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUDeviceDescriptor typedef struct WGPUDawnCacheDeviceDescriptor { WGPUChainedStruct chain; char const * isolationKey; } WGPUDawnCacheDeviceDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUCommandEncoderDescriptor typedef struct WGPUDawnEncoderInternalUsageDescriptor { WGPUChainedStruct chain; bool useInternalUsages; } WGPUDawnEncoderInternalUsageDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUMultisampleState typedef struct WGPUDawnMultisampleStateRenderToSingleSampled { WGPUChainedStruct chain; bool enabled; } WGPUDawnMultisampleStateRenderToSingleSampled WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPURenderPassColorAttachment typedef struct WGPUDawnRenderPassColorAttachmentRenderToSingleSampled { WGPUChainedStruct chain; uint32_t implicitSampleCount; } WGPUDawnRenderPassColorAttachmentRenderToSingleSampled WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUShaderModuleDescriptor typedef struct WGPUDawnShaderModuleSPIRVOptionsDescriptor { WGPUChainedStruct chain; bool allowNonUniformDerivatives; } WGPUDawnShaderModuleSPIRVOptionsDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUTextureDescriptor typedef struct WGPUDawnTextureInternalUsageDescriptor { WGPUChainedStruct chain; WGPUTextureUsageFlags internalUsage; } WGPUDawnTextureInternalUsageDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUInstanceDescriptor // Can be chained in WGPURequestAdapterOptions // Can be chained in WGPUDeviceDescriptor typedef struct WGPUDawnTogglesDescriptor { WGPUChainedStruct chain; size_t enabledTogglesCount; const char* const * enabledToggles; size_t disabledTogglesCount; const char* const * disabledToggles; } WGPUDawnTogglesDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUExtent2D { uint32_t width; uint32_t height; } WGPUExtent2D WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUExtent3D { uint32_t width; uint32_t height; uint32_t depthOrArrayLayers; } WGPUExtent3D WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUBindGroupEntry typedef struct WGPUExternalTextureBindingEntry { WGPUChainedStruct chain; WGPUExternalTexture externalTexture; } WGPUExternalTextureBindingEntry WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUBindGroupLayoutEntry typedef struct WGPUExternalTextureBindingLayout { WGPUChainedStruct chain; } WGPUExternalTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUInstanceDescriptor { WGPUChainedStruct const * nextInChain; } WGPUInstanceDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPULimits { uint32_t maxTextureDimension1D; uint32_t maxTextureDimension2D; uint32_t maxTextureDimension3D; uint32_t maxTextureArrayLayers; uint32_t maxBindGroups; uint32_t maxBindGroupsPlusVertexBuffers; uint32_t maxBindingsPerBindGroup; uint32_t maxDynamicUniformBuffersPerPipelineLayout; uint32_t maxDynamicStorageBuffersPerPipelineLayout; uint32_t maxSampledTexturesPerShaderStage; uint32_t maxSamplersPerShaderStage; uint32_t maxStorageBuffersPerShaderStage; uint32_t maxStorageTexturesPerShaderStage; uint32_t maxUniformBuffersPerShaderStage; uint64_t maxUniformBufferBindingSize; uint64_t maxStorageBufferBindingSize; uint32_t minUniformBufferOffsetAlignment; uint32_t minStorageBufferOffsetAlignment; uint32_t maxVertexBuffers; uint64_t maxBufferSize; uint32_t maxVertexAttributes; uint32_t maxVertexBufferArrayStride; uint32_t maxInterStageShaderComponents; uint32_t maxInterStageShaderVariables; uint32_t maxColorAttachments; uint32_t maxColorAttachmentBytesPerSample; uint32_t maxComputeWorkgroupStorageSize; uint32_t maxComputeInvocationsPerWorkgroup; uint32_t maxComputeWorkgroupSizeX; uint32_t maxComputeWorkgroupSizeY; uint32_t maxComputeWorkgroupSizeZ; uint32_t maxComputeWorkgroupsPerDimension; } WGPULimits WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUMultisampleState { WGPUChainedStruct const * nextInChain; uint32_t count; uint32_t mask; bool alphaToCoverageEnabled; } WGPUMultisampleState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUOrigin2D { uint32_t x; uint32_t y; } WGPUOrigin2D WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUOrigin3D { uint32_t x; uint32_t y; uint32_t z; } WGPUOrigin3D WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUPipelineLayoutDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t bindGroupLayoutCount; WGPUBindGroupLayout const * bindGroupLayouts; } WGPUPipelineLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUPrimitiveState typedef struct WGPUPrimitiveDepthClipControl { WGPUChainedStruct chain; bool unclippedDepth; } WGPUPrimitiveDepthClipControl WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUPrimitiveState { WGPUChainedStruct const * nextInChain; WGPUPrimitiveTopology topology; WGPUIndexFormat stripIndexFormat; WGPUFrontFace frontFace; WGPUCullMode cullMode; } WGPUPrimitiveState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUQuerySetDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUQueryType type; uint32_t count; WGPUPipelineStatisticName const * pipelineStatistics; size_t pipelineStatisticsCount; } WGPUQuerySetDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUQueueDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPUQueueDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderBundleDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPURenderBundleDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderBundleEncoderDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t colorFormatsCount; WGPUTextureFormat const * colorFormats; WGPUTextureFormat depthStencilFormat; uint32_t sampleCount; bool depthReadOnly; bool stencilReadOnly; } WGPURenderBundleEncoderDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderPassDepthStencilAttachment { WGPUTextureView view; WGPULoadOp depthLoadOp; WGPUStoreOp depthStoreOp; float depthClearValue; bool depthReadOnly; WGPULoadOp stencilLoadOp; WGPUStoreOp stencilStoreOp; uint32_t stencilClearValue; bool stencilReadOnly; } WGPURenderPassDepthStencilAttachment WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPURenderPassDescriptor typedef struct WGPURenderPassDescriptorMaxDrawCount { WGPUChainedStruct chain; uint64_t maxDrawCount; } WGPURenderPassDescriptorMaxDrawCount WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderPassTimestampWrite { WGPUQuerySet querySet; uint32_t queryIndex; WGPURenderPassTimestampLocation location; } WGPURenderPassTimestampWrite WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURequestAdapterOptions { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE WGPUSurface compatibleSurface; WGPUPowerPreference powerPreference; WGPUBackendType backendType; bool forceFallbackAdapter; bool compatibilityMode; } WGPURequestAdapterOptions WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUSamplerBindingLayout { WGPUChainedStruct const * nextInChain; WGPUSamplerBindingType type; } WGPUSamplerBindingLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUSamplerDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUAddressMode addressModeU; WGPUAddressMode addressModeV; WGPUAddressMode addressModeW; WGPUFilterMode magFilter; WGPUFilterMode minFilter; WGPUMipmapFilterMode mipmapFilter; float lodMinClamp; float lodMaxClamp; WGPUCompareFunction compare; uint16_t maxAnisotropy; } WGPUSamplerDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUShaderModuleDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPUShaderModuleDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUShaderModuleDescriptor typedef struct WGPUShaderModuleSPIRVDescriptor { WGPUChainedStruct chain; uint32_t codeSize; uint32_t const * code; } WGPUShaderModuleSPIRVDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUShaderModuleDescriptor typedef struct WGPUShaderModuleWGSLDescriptor { WGPUChainedStruct chain; char const * code; } WGPUShaderModuleWGSLDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUStencilFaceState { WGPUCompareFunction compare; WGPUStencilOperation failOp; WGPUStencilOperation depthFailOp; WGPUStencilOperation passOp; } WGPUStencilFaceState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUStorageTextureBindingLayout { WGPUChainedStruct const * nextInChain; WGPUStorageTextureAccess access; WGPUTextureFormat format; WGPUTextureViewDimension viewDimension; } WGPUStorageTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUSurfaceDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; } WGPUSurfaceDescriptor WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromAndroidNativeWindow { WGPUChainedStruct chain; void * window; } WGPUSurfaceDescriptorFromAndroidNativeWindow WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromCanvasHTMLSelector { WGPUChainedStruct chain; char const * selector; } WGPUSurfaceDescriptorFromCanvasHTMLSelector WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromMetalLayer { WGPUChainedStruct chain; void * layer; } WGPUSurfaceDescriptorFromMetalLayer WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromWaylandSurface { WGPUChainedStruct chain; void * display; void * surface; } WGPUSurfaceDescriptorFromWaylandSurface WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromWindowsCoreWindow { WGPUChainedStruct chain; void * coreWindow; } WGPUSurfaceDescriptorFromWindowsCoreWindow WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromWindowsHWND { WGPUChainedStruct chain; void * hinstance; void * hwnd; } WGPUSurfaceDescriptorFromWindowsHWND WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromWindowsSwapChainPanel { WGPUChainedStruct chain; void * swapChainPanel; } WGPUSurfaceDescriptorFromWindowsSwapChainPanel WGPU_STRUCTURE_ATTRIBUTE; // Can be chained in WGPUSurfaceDescriptor typedef struct WGPUSurfaceDescriptorFromXlibWindow { WGPUChainedStruct chain; void * display; uint32_t window; } WGPUSurfaceDescriptorFromXlibWindow WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUSwapChainDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUTextureUsageFlags usage; WGPUTextureFormat format; uint32_t width; uint32_t height; WGPUPresentMode presentMode; } WGPUSwapChainDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUTextureBindingLayout { WGPUChainedStruct const * nextInChain; WGPUTextureSampleType sampleType; WGPUTextureViewDimension viewDimension; bool multisampled; } WGPUTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUTextureDataLayout { WGPUChainedStruct const * nextInChain; uint64_t offset; uint32_t bytesPerRow; uint32_t rowsPerImage; } WGPUTextureDataLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUTextureViewDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUTextureFormat format; WGPUTextureViewDimension dimension; uint32_t baseMipLevel; uint32_t mipLevelCount; uint32_t baseArrayLayer; uint32_t arrayLayerCount; WGPUTextureAspect aspect; } WGPUTextureViewDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUVertexAttribute { WGPUVertexFormat format; uint64_t offset; uint32_t shaderLocation; } WGPUVertexAttribute WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBindGroupDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUBindGroupLayout layout; size_t entryCount; WGPUBindGroupEntry const * entries; } WGPUBindGroupDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBindGroupLayoutEntry { WGPUChainedStruct const * nextInChain; uint32_t binding; WGPUShaderStageFlags visibility; WGPUBufferBindingLayout buffer; WGPUSamplerBindingLayout sampler; WGPUTextureBindingLayout texture; WGPUStorageTextureBindingLayout storageTexture; } WGPUBindGroupLayoutEntry WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBlendState { WGPUBlendComponent color; WGPUBlendComponent alpha; } WGPUBlendState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUCompilationInfo { WGPUChainedStruct const * nextInChain; size_t messageCount; WGPUCompilationMessage const * messages; } WGPUCompilationInfo WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUComputePassDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t timestampWriteCount; WGPUComputePassTimestampWrite const * timestampWrites; } WGPUComputePassDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUDepthStencilState { WGPUChainedStruct const * nextInChain; WGPUTextureFormat format; bool depthWriteEnabled; WGPUCompareFunction depthCompare; WGPUStencilFaceState stencilFront; WGPUStencilFaceState stencilBack; uint32_t stencilReadMask; uint32_t stencilWriteMask; int32_t depthBias; float depthBiasSlopeScale; float depthBiasClamp; } WGPUDepthStencilState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUExternalTextureDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUTextureView plane0; WGPU_NULLABLE WGPUTextureView plane1; WGPUOrigin2D visibleOrigin; WGPUExtent2D visibleSize; bool doYuvToRgbConversionOnly; WGPU_NULLABLE float const * yuvToRgbConversionMatrix; float const * srcTransferFunctionParameters; float const * dstTransferFunctionParameters; float const * gamutConversionMatrix; bool flipY; WGPUExternalTextureRotation rotation; } WGPUExternalTextureDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUImageCopyBuffer { WGPUChainedStruct const * nextInChain; WGPUTextureDataLayout layout; WGPUBuffer buffer; } WGPUImageCopyBuffer WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUImageCopyExternalTexture { WGPUChainedStruct const * nextInChain; WGPUExternalTexture externalTexture; WGPUOrigin3D origin; WGPUExtent2D naturalSize; } WGPUImageCopyExternalTexture WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUImageCopyTexture { WGPUChainedStruct const * nextInChain; WGPUTexture texture; uint32_t mipLevel; WGPUOrigin3D origin; WGPUTextureAspect aspect; } WGPUImageCopyTexture WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUProgrammableStageDescriptor { WGPUChainedStruct const * nextInChain; WGPUShaderModule module; char const * entryPoint; size_t constantCount; WGPUConstantEntry const * constants; } WGPUProgrammableStageDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderPassColorAttachment { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE WGPUTextureView view; WGPU_NULLABLE WGPUTextureView resolveTarget; WGPULoadOp loadOp; WGPUStoreOp storeOp; WGPUColor clearValue; } WGPURenderPassColorAttachment WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURequiredLimits { WGPUChainedStruct const * nextInChain; WGPULimits limits; } WGPURequiredLimits WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUSupportedLimits { WGPUChainedStructOut * nextInChain; WGPULimits limits; } WGPUSupportedLimits WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUTextureDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPUTextureUsageFlags usage; WGPUTextureDimension dimension; WGPUExtent3D size; WGPUTextureFormat format; uint32_t mipLevelCount; uint32_t sampleCount; size_t viewFormatCount; WGPUTextureFormat const * viewFormats; } WGPUTextureDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUVertexBufferLayout { uint64_t arrayStride; WGPUVertexStepMode stepMode; size_t attributeCount; WGPUVertexAttribute const * attributes; } WGPUVertexBufferLayout WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUBindGroupLayoutDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t entryCount; WGPUBindGroupLayoutEntry const * entries; } WGPUBindGroupLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUColorTargetState { WGPUChainedStruct const * nextInChain; WGPUTextureFormat format; WGPU_NULLABLE WGPUBlendState const * blend; WGPUColorWriteMaskFlags writeMask; } WGPUColorTargetState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUComputePipelineDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPU_NULLABLE WGPUPipelineLayout layout; WGPUProgrammableStageDescriptor compute; } WGPUComputePipelineDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUDeviceDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t requiredFeaturesCount; WGPUFeatureName const * requiredFeatures; WGPU_NULLABLE WGPURequiredLimits const * requiredLimits; WGPUQueueDescriptor defaultQueue; WGPUDeviceLostCallback deviceLostCallback; void * deviceLostUserdata; } WGPUDeviceDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderPassDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; size_t colorAttachmentCount; WGPURenderPassColorAttachment const * colorAttachments; WGPU_NULLABLE WGPURenderPassDepthStencilAttachment const * depthStencilAttachment; WGPU_NULLABLE WGPUQuerySet occlusionQuerySet; size_t timestampWriteCount; WGPURenderPassTimestampWrite const * timestampWrites; } WGPURenderPassDescriptor WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUVertexState { WGPUChainedStruct const * nextInChain; WGPUShaderModule module; char const * entryPoint; size_t constantCount; WGPUConstantEntry const * constants; size_t bufferCount; WGPUVertexBufferLayout const * buffers; } WGPUVertexState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPUFragmentState { WGPUChainedStruct const * nextInChain; WGPUShaderModule module; char const * entryPoint; size_t constantCount; WGPUConstantEntry const * constants; size_t targetCount; WGPUColorTargetState const * targets; } WGPUFragmentState WGPU_STRUCTURE_ATTRIBUTE; typedef struct WGPURenderPipelineDescriptor { WGPUChainedStruct const * nextInChain; WGPU_NULLABLE char const * label; WGPU_NULLABLE WGPUPipelineLayout layout; WGPUVertexState vertex; WGPUPrimitiveState primitive; WGPU_NULLABLE WGPUDepthStencilState const * depthStencil; WGPUMultisampleState multisample; WGPU_NULLABLE WGPUFragmentState const * fragment; } WGPURenderPipelineDescriptor WGPU_STRUCTURE_ATTRIBUTE; #ifdef __cplusplus extern "C" { #endif #if !defined(WGPU_SKIP_PROCS) typedef WGPUInstance (*WGPUProcCreateInstance)(WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUProc (*WGPUProcGetProcAddress)(WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE; // Procs of Adapter typedef WGPUDevice (*WGPUProcAdapterCreateDevice)(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef size_t (*WGPUProcAdapterEnumerateFeatures)(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUInstance (*WGPUProcAdapterGetInstance)(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; typedef bool (*WGPUProcAdapterGetLimits)(WGPUAdapter adapter, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcAdapterGetProperties)(WGPUAdapter adapter, WGPUAdapterProperties * properties) WGPU_FUNCTION_ATTRIBUTE; typedef bool (*WGPUProcAdapterHasFeature)(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcAdapterRequestDevice)(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcAdapterReference)(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcAdapterRelease)(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; // Procs of BindGroup typedef void (*WGPUProcBindGroupSetLabel)(WGPUBindGroup bindGroup, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBindGroupReference)(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBindGroupRelease)(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; // Procs of BindGroupLayout typedef void (*WGPUProcBindGroupLayoutSetLabel)(WGPUBindGroupLayout bindGroupLayout, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBindGroupLayoutReference)(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBindGroupLayoutRelease)(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; // Procs of Buffer typedef void (*WGPUProcBufferDestroy)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef void const * (*WGPUProcBufferGetConstMappedRange)(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUBufferMapState (*WGPUProcBufferGetMapState)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef void * (*WGPUProcBufferGetMappedRange)(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; typedef uint64_t (*WGPUProcBufferGetSize)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUBufferUsageFlags (*WGPUProcBufferGetUsage)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBufferMapAsync)(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, WGPUBufferMapCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBufferSetLabel)(WGPUBuffer buffer, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBufferUnmap)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBufferReference)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcBufferRelease)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; // Procs of CommandBuffer typedef void (*WGPUProcCommandBufferSetLabel)(WGPUCommandBuffer commandBuffer, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandBufferReference)(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandBufferRelease)(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; // Procs of CommandEncoder typedef WGPUComputePassEncoder (*WGPUProcCommandEncoderBeginComputePass)(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUComputePassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPURenderPassEncoder (*WGPUProcCommandEncoderBeginRenderPass)(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderClearBuffer)(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderCopyBufferToBuffer)(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderCopyBufferToTexture)(WGPUCommandEncoder commandEncoder, WGPUImageCopyBuffer const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderCopyTextureToBuffer)(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyBuffer const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderCopyTextureToTexture)(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderCopyTextureToTextureInternal)(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUCommandBuffer (*WGPUProcCommandEncoderFinish)(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUCommandBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderInjectValidationError)(WGPUCommandEncoder commandEncoder, char const * message) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderInsertDebugMarker)(WGPUCommandEncoder commandEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderPopDebugGroup)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderPushDebugGroup)(WGPUCommandEncoder commandEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderResolveQuerySet)(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, WGPUBuffer destination, uint64_t destinationOffset) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderSetLabel)(WGPUCommandEncoder commandEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderWriteBuffer)(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t bufferOffset, uint8_t const * data, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderWriteTimestamp)(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderReference)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcCommandEncoderRelease)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; // Procs of ComputePassEncoder typedef void (*WGPUProcComputePassEncoderDispatchWorkgroups)(WGPUComputePassEncoder computePassEncoder, uint32_t workgroupCountX, uint32_t workgroupCountY, uint32_t workgroupCountZ) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderDispatchWorkgroupsIndirect)(WGPUComputePassEncoder computePassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderEnd)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderInsertDebugMarker)(WGPUComputePassEncoder computePassEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderPopDebugGroup)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderPushDebugGroup)(WGPUComputePassEncoder computePassEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderSetBindGroup)(WGPUComputePassEncoder computePassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderSetLabel)(WGPUComputePassEncoder computePassEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderSetPipeline)(WGPUComputePassEncoder computePassEncoder, WGPUComputePipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderWriteTimestamp)(WGPUComputePassEncoder computePassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderReference)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePassEncoderRelease)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; // Procs of ComputePipeline typedef WGPUBindGroupLayout (*WGPUProcComputePipelineGetBindGroupLayout)(WGPUComputePipeline computePipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePipelineSetLabel)(WGPUComputePipeline computePipeline, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePipelineReference)(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcComputePipelineRelease)(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; // Procs of Device typedef WGPUBindGroup (*WGPUProcDeviceCreateBindGroup)(WGPUDevice device, WGPUBindGroupDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUBindGroupLayout (*WGPUProcDeviceCreateBindGroupLayout)(WGPUDevice device, WGPUBindGroupLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUBuffer (*WGPUProcDeviceCreateBuffer)(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUCommandEncoder (*WGPUProcDeviceCreateCommandEncoder)(WGPUDevice device, WGPU_NULLABLE WGPUCommandEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUComputePipeline (*WGPUProcDeviceCreateComputePipeline)(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceCreateComputePipelineAsync)(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor, WGPUCreateComputePipelineAsyncCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUBuffer (*WGPUProcDeviceCreateErrorBuffer)(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUExternalTexture (*WGPUProcDeviceCreateErrorExternalTexture)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUShaderModule (*WGPUProcDeviceCreateErrorShaderModule)(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor, char const * errorMessage) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTexture (*WGPUProcDeviceCreateErrorTexture)(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUExternalTexture (*WGPUProcDeviceCreateExternalTexture)(WGPUDevice device, WGPUExternalTextureDescriptor const * externalTextureDescriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUPipelineLayout (*WGPUProcDeviceCreatePipelineLayout)(WGPUDevice device, WGPUPipelineLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUQuerySet (*WGPUProcDeviceCreateQuerySet)(WGPUDevice device, WGPUQuerySetDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPURenderBundleEncoder (*WGPUProcDeviceCreateRenderBundleEncoder)(WGPUDevice device, WGPURenderBundleEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPURenderPipeline (*WGPUProcDeviceCreateRenderPipeline)(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceCreateRenderPipelineAsync)(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUSampler (*WGPUProcDeviceCreateSampler)(WGPUDevice device, WGPU_NULLABLE WGPUSamplerDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUShaderModule (*WGPUProcDeviceCreateShaderModule)(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUSwapChain (*WGPUProcDeviceCreateSwapChain)(WGPUDevice device, WGPUSurface surface, WGPUSwapChainDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTexture (*WGPUProcDeviceCreateTexture)(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceDestroy)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef size_t (*WGPUProcDeviceEnumerateFeatures)(WGPUDevice device, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceForceLoss)(WGPUDevice device, WGPUDeviceLostReason type, char const * message) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUAdapter (*WGPUProcDeviceGetAdapter)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef bool (*WGPUProcDeviceGetLimits)(WGPUDevice device, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUQueue (*WGPUProcDeviceGetQueue)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTextureUsageFlags (*WGPUProcDeviceGetSupportedSurfaceUsage)(WGPUDevice device, WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; typedef bool (*WGPUProcDeviceHasFeature)(WGPUDevice device, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceInjectError)(WGPUDevice device, WGPUErrorType type, char const * message) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDevicePopErrorScope)(WGPUDevice device, WGPUErrorCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDevicePushErrorScope)(WGPUDevice device, WGPUErrorFilter filter) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceSetDeviceLostCallback)(WGPUDevice device, WGPUDeviceLostCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceSetLabel)(WGPUDevice device, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceSetLoggingCallback)(WGPUDevice device, WGPULoggingCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceSetUncapturedErrorCallback)(WGPUDevice device, WGPUErrorCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceTick)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceValidateTextureDescriptor)(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceReference)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcDeviceRelease)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; // Procs of ExternalTexture typedef void (*WGPUProcExternalTextureDestroy)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcExternalTextureExpire)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcExternalTextureRefresh)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcExternalTextureSetLabel)(WGPUExternalTexture externalTexture, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcExternalTextureReference)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcExternalTextureRelease)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; // Procs of Instance typedef WGPUSurface (*WGPUProcInstanceCreateSurface)(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcInstanceProcessEvents)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcInstanceRequestAdapter)(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcInstanceReference)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcInstanceRelease)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; // Procs of PipelineLayout typedef void (*WGPUProcPipelineLayoutSetLabel)(WGPUPipelineLayout pipelineLayout, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcPipelineLayoutReference)(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcPipelineLayoutRelease)(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; // Procs of QuerySet typedef void (*WGPUProcQuerySetDestroy)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcQuerySetGetCount)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUQueryType (*WGPUProcQuerySetGetType)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQuerySetSetLabel)(WGPUQuerySet querySet, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQuerySetReference)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQuerySetRelease)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; // Procs of Queue typedef void (*WGPUProcQueueCopyExternalTextureForBrowser)(WGPUQueue queue, WGPUImageCopyExternalTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueCopyTextureForBrowser)(WGPUQueue queue, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueOnSubmittedWorkDone)(WGPUQueue queue, uint64_t signalValue, WGPUQueueWorkDoneCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueSetLabel)(WGPUQueue queue, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueSubmit)(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueWriteBuffer)(WGPUQueue queue, WGPUBuffer buffer, uint64_t bufferOffset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueWriteTexture)(WGPUQueue queue, WGPUImageCopyTexture const * destination, void const * data, size_t dataSize, WGPUTextureDataLayout const * dataLayout, WGPUExtent3D const * writeSize) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueReference)(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcQueueRelease)(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; // Procs of RenderBundle typedef void (*WGPUProcRenderBundleSetLabel)(WGPURenderBundle renderBundle, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleReference)(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleRelease)(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; // Procs of RenderBundleEncoder typedef void (*WGPUProcRenderBundleEncoderDraw)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderDrawIndexed)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderDrawIndexedIndirect)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderDrawIndirect)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; typedef WGPURenderBundle (*WGPUProcRenderBundleEncoderFinish)(WGPURenderBundleEncoder renderBundleEncoder, WGPU_NULLABLE WGPURenderBundleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderInsertDebugMarker)(WGPURenderBundleEncoder renderBundleEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderPopDebugGroup)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderPushDebugGroup)(WGPURenderBundleEncoder renderBundleEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderSetBindGroup)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderSetIndexBuffer)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderSetLabel)(WGPURenderBundleEncoder renderBundleEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderSetPipeline)(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderSetVertexBuffer)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderReference)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderBundleEncoderRelease)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; // Procs of RenderPassEncoder typedef void (*WGPUProcRenderPassEncoderBeginOcclusionQuery)(WGPURenderPassEncoder renderPassEncoder, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderDraw)(WGPURenderPassEncoder renderPassEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderDrawIndexed)(WGPURenderPassEncoder renderPassEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderDrawIndexedIndirect)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderDrawIndirect)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderEnd)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderEndOcclusionQuery)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderExecuteBundles)(WGPURenderPassEncoder renderPassEncoder, size_t bundleCount, WGPURenderBundle const * bundles) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderInsertDebugMarker)(WGPURenderPassEncoder renderPassEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderPopDebugGroup)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderPushDebugGroup)(WGPURenderPassEncoder renderPassEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetBindGroup)(WGPURenderPassEncoder renderPassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetBlendConstant)(WGPURenderPassEncoder renderPassEncoder, WGPUColor const * color) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetIndexBuffer)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetLabel)(WGPURenderPassEncoder renderPassEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetPipeline)(WGPURenderPassEncoder renderPassEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetScissorRect)(WGPURenderPassEncoder renderPassEncoder, uint32_t x, uint32_t y, uint32_t width, uint32_t height) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetStencilReference)(WGPURenderPassEncoder renderPassEncoder, uint32_t reference) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetVertexBuffer)(WGPURenderPassEncoder renderPassEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderSetViewport)(WGPURenderPassEncoder renderPassEncoder, float x, float y, float width, float height, float minDepth, float maxDepth) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderWriteTimestamp)(WGPURenderPassEncoder renderPassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderReference)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPassEncoderRelease)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; // Procs of RenderPipeline typedef WGPUBindGroupLayout (*WGPUProcRenderPipelineGetBindGroupLayout)(WGPURenderPipeline renderPipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPipelineSetLabel)(WGPURenderPipeline renderPipeline, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPipelineReference)(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcRenderPipelineRelease)(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; // Procs of Sampler typedef void (*WGPUProcSamplerSetLabel)(WGPUSampler sampler, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSamplerReference)(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSamplerRelease)(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; // Procs of ShaderModule typedef void (*WGPUProcShaderModuleGetCompilationInfo)(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcShaderModuleSetLabel)(WGPUShaderModule shaderModule, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcShaderModuleReference)(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcShaderModuleRelease)(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; // Procs of Surface typedef void (*WGPUProcSurfaceReference)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSurfaceRelease)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; // Procs of SwapChain typedef WGPUTexture (*WGPUProcSwapChainGetCurrentTexture)(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTextureView (*WGPUProcSwapChainGetCurrentTextureView)(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSwapChainPresent)(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSwapChainReference)(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcSwapChainRelease)(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; // Procs of Texture typedef WGPUTextureView (*WGPUProcTextureCreateView)(WGPUTexture texture, WGPU_NULLABLE WGPUTextureViewDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureDestroy)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcTextureGetDepthOrArrayLayers)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTextureDimension (*WGPUProcTextureGetDimension)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTextureFormat (*WGPUProcTextureGetFormat)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcTextureGetHeight)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcTextureGetMipLevelCount)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcTextureGetSampleCount)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef WGPUTextureUsageFlags (*WGPUProcTextureGetUsage)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef uint32_t (*WGPUProcTextureGetWidth)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureSetLabel)(WGPUTexture texture, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureReference)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureRelease)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; // Procs of TextureView typedef void (*WGPUProcTextureViewSetLabel)(WGPUTextureView textureView, char const * label) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureViewReference)(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; typedef void (*WGPUProcTextureViewRelease)(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; #endif // !defined(WGPU_SKIP_PROCS) #if !defined(WGPU_SKIP_DECLARATIONS) WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUProc wgpuGetProcAddress(WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE; // Methods of Adapter WGPU_EXPORT WGPUDevice wgpuAdapterCreateDevice(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT size_t wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUInstance wgpuAdapterGetInstance(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT bool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuAdapterGetProperties(WGPUAdapter adapter, WGPUAdapterProperties * properties) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT bool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuAdapterReference(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuAdapterRelease(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; // Methods of BindGroup WGPU_EXPORT void wgpuBindGroupSetLabel(WGPUBindGroup bindGroup, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBindGroupReference(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBindGroupRelease(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; // Methods of BindGroupLayout WGPU_EXPORT void wgpuBindGroupLayoutSetLabel(WGPUBindGroupLayout bindGroupLayout, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBindGroupLayoutReference(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBindGroupLayoutRelease(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; // Methods of Buffer WGPU_EXPORT void wgpuBufferDestroy(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void const * wgpuBufferGetConstMappedRange(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUBufferMapState wgpuBufferGetMapState(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void * wgpuBufferGetMappedRange(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint64_t wgpuBufferGetSize(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUBufferUsageFlags wgpuBufferGetUsage(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBufferMapAsync(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, WGPUBufferMapCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBufferSetLabel(WGPUBuffer buffer, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBufferUnmap(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBufferReference(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuBufferRelease(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; // Methods of CommandBuffer WGPU_EXPORT void wgpuCommandBufferSetLabel(WGPUCommandBuffer commandBuffer, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandBufferReference(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandBufferRelease(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; // Methods of CommandEncoder WGPU_EXPORT WGPUComputePassEncoder wgpuCommandEncoderBeginComputePass(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUComputePassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPURenderPassEncoder wgpuCommandEncoderBeginRenderPass(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderClearBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderCopyBufferToBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderCopyBufferToTexture(WGPUCommandEncoder commandEncoder, WGPUImageCopyBuffer const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderCopyTextureToBuffer(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyBuffer const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderCopyTextureToTexture(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderCopyTextureToTextureInternal(WGPUCommandEncoder commandEncoder, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUCommandBuffer wgpuCommandEncoderFinish(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUCommandBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderInjectValidationError(WGPUCommandEncoder commandEncoder, char const * message) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderInsertDebugMarker(WGPUCommandEncoder commandEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderPopDebugGroup(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderPushDebugGroup(WGPUCommandEncoder commandEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderResolveQuerySet(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, WGPUBuffer destination, uint64_t destinationOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderSetLabel(WGPUCommandEncoder commandEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderWriteBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t bufferOffset, uint8_t const * data, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderWriteTimestamp(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderReference(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuCommandEncoderRelease(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; // Methods of ComputePassEncoder WGPU_EXPORT void wgpuComputePassEncoderDispatchWorkgroups(WGPUComputePassEncoder computePassEncoder, uint32_t workgroupCountX, uint32_t workgroupCountY, uint32_t workgroupCountZ) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderDispatchWorkgroupsIndirect(WGPUComputePassEncoder computePassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderEnd(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderInsertDebugMarker(WGPUComputePassEncoder computePassEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderPopDebugGroup(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderPushDebugGroup(WGPUComputePassEncoder computePassEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderSetBindGroup(WGPUComputePassEncoder computePassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderSetLabel(WGPUComputePassEncoder computePassEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderSetPipeline(WGPUComputePassEncoder computePassEncoder, WGPUComputePipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderWriteTimestamp(WGPUComputePassEncoder computePassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderReference(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePassEncoderRelease(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; // Methods of ComputePipeline WGPU_EXPORT WGPUBindGroupLayout wgpuComputePipelineGetBindGroupLayout(WGPUComputePipeline computePipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePipelineSetLabel(WGPUComputePipeline computePipeline, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePipelineReference(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuComputePipelineRelease(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; // Methods of Device WGPU_EXPORT WGPUBindGroup wgpuDeviceCreateBindGroup(WGPUDevice device, WGPUBindGroupDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUBindGroupLayout wgpuDeviceCreateBindGroupLayout(WGPUDevice device, WGPUBindGroupLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUCommandEncoder wgpuDeviceCreateCommandEncoder(WGPUDevice device, WGPU_NULLABLE WGPUCommandEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUComputePipeline wgpuDeviceCreateComputePipeline(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceCreateComputePipelineAsync(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor, WGPUCreateComputePipelineAsyncCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUBuffer wgpuDeviceCreateErrorBuffer(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUExternalTexture wgpuDeviceCreateErrorExternalTexture(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUShaderModule wgpuDeviceCreateErrorShaderModule(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor, char const * errorMessage) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTexture wgpuDeviceCreateErrorTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUExternalTexture wgpuDeviceCreateExternalTexture(WGPUDevice device, WGPUExternalTextureDescriptor const * externalTextureDescriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUPipelineLayout wgpuDeviceCreatePipelineLayout(WGPUDevice device, WGPUPipelineLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUQuerySet wgpuDeviceCreateQuerySet(WGPUDevice device, WGPUQuerySetDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPURenderBundleEncoder wgpuDeviceCreateRenderBundleEncoder(WGPUDevice device, WGPURenderBundleEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPURenderPipeline wgpuDeviceCreateRenderPipeline(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUSampler wgpuDeviceCreateSampler(WGPUDevice device, WGPU_NULLABLE WGPUSamplerDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUShaderModule wgpuDeviceCreateShaderModule(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUSwapChain wgpuDeviceCreateSwapChain(WGPUDevice device, WGPUSurface surface, WGPUSwapChainDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTexture wgpuDeviceCreateTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceDestroy(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT size_t wgpuDeviceEnumerateFeatures(WGPUDevice device, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceForceLoss(WGPUDevice device, WGPUDeviceLostReason type, char const * message) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUAdapter wgpuDeviceGetAdapter(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT bool wgpuDeviceGetLimits(WGPUDevice device, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUQueue wgpuDeviceGetQueue(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTextureUsageFlags wgpuDeviceGetSupportedSurfaceUsage(WGPUDevice device, WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT bool wgpuDeviceHasFeature(WGPUDevice device, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceInjectError(WGPUDevice device, WGPUErrorType type, char const * message) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDevicePopErrorScope(WGPUDevice device, WGPUErrorCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDevicePushErrorScope(WGPUDevice device, WGPUErrorFilter filter) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceSetDeviceLostCallback(WGPUDevice device, WGPUDeviceLostCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceSetLabel(WGPUDevice device, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceSetLoggingCallback(WGPUDevice device, WGPULoggingCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceSetUncapturedErrorCallback(WGPUDevice device, WGPUErrorCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceTick(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceValidateTextureDescriptor(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceReference(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuDeviceRelease(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; // Methods of ExternalTexture WGPU_EXPORT void wgpuExternalTextureDestroy(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuExternalTextureExpire(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuExternalTextureRefresh(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuExternalTextureSetLabel(WGPUExternalTexture externalTexture, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuExternalTextureReference(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuExternalTextureRelease(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; // Methods of Instance WGPU_EXPORT WGPUSurface wgpuInstanceCreateSurface(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuInstanceProcessEvents(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuInstanceReference(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; // Methods of PipelineLayout WGPU_EXPORT void wgpuPipelineLayoutSetLabel(WGPUPipelineLayout pipelineLayout, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuPipelineLayoutReference(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuPipelineLayoutRelease(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; // Methods of QuerySet WGPU_EXPORT void wgpuQuerySetDestroy(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuQuerySetGetCount(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUQueryType wgpuQuerySetGetType(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQuerySetSetLabel(WGPUQuerySet querySet, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQuerySetReference(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQuerySetRelease(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; // Methods of Queue WGPU_EXPORT void wgpuQueueCopyExternalTextureForBrowser(WGPUQueue queue, WGPUImageCopyExternalTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueCopyTextureForBrowser(WGPUQueue queue, WGPUImageCopyTexture const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize, WGPUCopyTextureForBrowserOptions const * options) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueOnSubmittedWorkDone(WGPUQueue queue, uint64_t signalValue, WGPUQueueWorkDoneCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueSetLabel(WGPUQueue queue, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueSubmit(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueWriteBuffer(WGPUQueue queue, WGPUBuffer buffer, uint64_t bufferOffset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueWriteTexture(WGPUQueue queue, WGPUImageCopyTexture const * destination, void const * data, size_t dataSize, WGPUTextureDataLayout const * dataLayout, WGPUExtent3D const * writeSize) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueReference(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuQueueRelease(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; // Methods of RenderBundle WGPU_EXPORT void wgpuRenderBundleSetLabel(WGPURenderBundle renderBundle, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleReference(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleRelease(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; // Methods of RenderBundleEncoder WGPU_EXPORT void wgpuRenderBundleEncoderDraw(WGPURenderBundleEncoder renderBundleEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndexed(WGPURenderBundleEncoder renderBundleEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndexedIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPURenderBundle wgpuRenderBundleEncoderFinish(WGPURenderBundleEncoder renderBundleEncoder, WGPU_NULLABLE WGPURenderBundleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderInsertDebugMarker(WGPURenderBundleEncoder renderBundleEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderPopDebugGroup(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderPushDebugGroup(WGPURenderBundleEncoder renderBundleEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderSetBindGroup(WGPURenderBundleEncoder renderBundleEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderSetIndexBuffer(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderSetLabel(WGPURenderBundleEncoder renderBundleEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderSetPipeline(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderSetVertexBuffer(WGPURenderBundleEncoder renderBundleEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderReference(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderBundleEncoderRelease(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; // Methods of RenderPassEncoder WGPU_EXPORT void wgpuRenderPassEncoderBeginOcclusionQuery(WGPURenderPassEncoder renderPassEncoder, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderDraw(WGPURenderPassEncoder renderPassEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderDrawIndexed(WGPURenderPassEncoder renderPassEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderDrawIndexedIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderDrawIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderEnd(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderEndOcclusionQuery(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderExecuteBundles(WGPURenderPassEncoder renderPassEncoder, size_t bundleCount, WGPURenderBundle const * bundles) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderInsertDebugMarker(WGPURenderPassEncoder renderPassEncoder, char const * markerLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderPopDebugGroup(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderPushDebugGroup(WGPURenderPassEncoder renderPassEncoder, char const * groupLabel) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetBindGroup(WGPURenderPassEncoder renderPassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetBlendConstant(WGPURenderPassEncoder renderPassEncoder, WGPUColor const * color) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetIndexBuffer(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetLabel(WGPURenderPassEncoder renderPassEncoder, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetPipeline(WGPURenderPassEncoder renderPassEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetScissorRect(WGPURenderPassEncoder renderPassEncoder, uint32_t x, uint32_t y, uint32_t width, uint32_t height) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetStencilReference(WGPURenderPassEncoder renderPassEncoder, uint32_t reference) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetVertexBuffer(WGPURenderPassEncoder renderPassEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderSetViewport(WGPURenderPassEncoder renderPassEncoder, float x, float y, float width, float height, float minDepth, float maxDepth) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderWriteTimestamp(WGPURenderPassEncoder renderPassEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderReference(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPassEncoderRelease(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; // Methods of RenderPipeline WGPU_EXPORT WGPUBindGroupLayout wgpuRenderPipelineGetBindGroupLayout(WGPURenderPipeline renderPipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPipelineSetLabel(WGPURenderPipeline renderPipeline, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPipelineReference(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuRenderPipelineRelease(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; // Methods of Sampler WGPU_EXPORT void wgpuSamplerSetLabel(WGPUSampler sampler, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSamplerReference(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSamplerRelease(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; // Methods of ShaderModule WGPU_EXPORT void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuShaderModuleReference(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuShaderModuleRelease(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; // Methods of Surface WGPU_EXPORT void wgpuSurfaceReference(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSurfaceRelease(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; // Methods of SwapChain WGPU_EXPORT WGPUTexture wgpuSwapChainGetCurrentTexture(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTextureView wgpuSwapChainGetCurrentTextureView(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSwapChainPresent(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSwapChainReference(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuSwapChainRelease(WGPUSwapChain swapChain) WGPU_FUNCTION_ATTRIBUTE; // Methods of Texture WGPU_EXPORT WGPUTextureView wgpuTextureCreateView(WGPUTexture texture, WGPU_NULLABLE WGPUTextureViewDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureDestroy(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuTextureGetDepthOrArrayLayers(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTextureDimension wgpuTextureGetDimension(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTextureFormat wgpuTextureGetFormat(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuTextureGetHeight(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuTextureGetMipLevelCount(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuTextureGetSampleCount(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT WGPUTextureUsageFlags wgpuTextureGetUsage(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT uint32_t wgpuTextureGetWidth(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureSetLabel(WGPUTexture texture, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureReference(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureRelease(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; // Methods of TextureView WGPU_EXPORT void wgpuTextureViewSetLabel(WGPUTextureView textureView, char const * label) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureViewReference(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; WGPU_EXPORT void wgpuTextureViewRelease(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; #endif // !defined(WGPU_SKIP_DECLARATIONS) #ifdef __cplusplus } // extern "C" #endif #endif // WEBGPU_H_