// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! One frame drawn off-screen and written to a PNG, with no window: what //! the game looks like, for checking a change to how it draws without //! looking at a screen, and for tests that cannot have one. //! //! It is drawn by the same renderer the window uses, into its own image, //! and read back. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const vk = @import("vk"); const rules = @import("rules"); const view = @import("view"); const Device = @import("vulkan/Device.zig"); const Renderer = @import("vulkan/Renderer.zig"); const png = @import("png.zig"); pub const Options = struct { path: []const u8, width: u32 = 560, height: u32 = 640, level: rules.level.Number = 1, /// Dots tapped, in order, at the start. taps: []const rules.Board.Dot = &.{}, /// How long after the taps the frame is, in milliseconds. at: i64 = 0, validation: bool = false, }; pub fn run(gpa: Allocator, io: Io, options: Options) !void { const device = try Device.init(gpa, .{ .validation = options.validation }); defer device.deinit(); var renderer = try Renderer.init(device, Renderer.default_format); defer renderer.deinit(); var play = try view.Play.init(gpa, options.level); defer play.deinit(); const theme: view.draw.Theme = .{}; const layout = play.layout(@floatFromInt(options.width), @floatFromInt(options.height)); for (options.taps) |dot| play.tap(layout, layout.center(dot), 0); const now = options.at * std.time.ns_per_ms; var list: view.draw.List = .empty; defer list.deinit(gpa); try play.render(layout, theme, now, &list); var readback: Readback = .{ .device = device }; defer readback.buffer.destroy(device); try renderer.draw(options.width, options.height, theme.background, list.items, readback.target()); // The image is B, G, R, A in memory; PNG wants R, G, B, A. const pixels = readback.buffer.mapped.?[0 .. @as(usize, options.width) * options.height * 4]; var i: usize = 0; while (i < pixels.len) : (i += 4) std.mem.swap(u8, &pixels[i], &pixels[i + 2]); var file = try Io.Dir.cwd().createFile(io, options.path, .{}); defer file.close(io); var buf: [64 * 1024]u8 = undefined; var writer = file.writer(io, &buf); try png.write(&writer.interface, options.width, options.height, @as(usize, options.width) * 4, pixels); try writer.interface.flush(); } /// A target that copies the frame into a host-visible buffer. const Readback = struct { device: *Device, buffer: Device.Buffer = .{}, fn target(r: *Readback) Renderer.Target { return .{ .context = r, .acquire = acquire, .record = record, .finish = finish }; } fn acquire(_: *anyopaque, _: u32, _: u32, _: vk.RenderPass) anyerror!Renderer.Acquired { return .{}; } fn record(context: *anyopaque, cmd: vk.CommandBuffer, image: vk.Image, width: u32, height: u32) anyerror!void { const r: *Readback = @ptrCast(@alignCast(context)); r.buffer = try r.device.hostBuffer(@as(usize, width) * height * 4, .{ .transfer_dst = true }); r.device.device.cmdCopyImageToBuffer(cmd, image, .transfer_src_optimal, r.buffer.buffer, &.{.{ .buffer_offset = 0, .buffer_row_length = 0, .buffer_image_height = 0, .image_subresource = .{ .aspect_mask = .{ .color = true }, .mip_level = 0, .base_array_layer = 0, .layer_count = 1 }, .image_offset = .{ .x = 0, .y = 0, .z = 0 }, .image_extent = .{ .width = width, .height = height, .depth = 1 }, }}); const barrier = vk.BufferMemoryBarrier{ .src_access_mask = .{ .transfer_write = true }, .dst_access_mask = .{ .host_read = true }, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .buffer = r.buffer.buffer, .offset = 0, .size = vk.WHOLE_SIZE, }; r.device.device.cmdPipelineBarrier(cmd, .{ .transfer = true }, .{ .host = true }, .{}, null, &.{barrier}, null); } fn finish(context: *anyopaque, fence: vk.Fence) anyerror!void { const r: *Readback = @ptrCast(@alignCast(context)); _ = try r.device.device.waitForFences(&.{fence}, .true, std.math.maxInt(u64)); } };