diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 537271f1..51ec6bbf 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1,3 +1,5 @@ +const SketchGpu = @import("sketch_gpu"); +const GpuScene = @import("gpu_scene"); const filesystem = @import("../fs.zig"); const std = @import("std"); const builtin = @import("builtin"); @@ -286,6 +288,7 @@ const ImageInstance = extern struct { const opaque_bg_bit: u32 = 0x2000_0000; const PreparedImage = struct { + premultiplied: bool = false, place: pardes.Placement, texture: *c.SDL_GPUTexture, }; @@ -517,6 +520,11 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } const Gui = struct { + addon_gpu: ?SketchGpu = null, + addon_hash: [pardes.MAX_PANES]u64 = @splat(0), + addon_serial: [pardes.MAX_PANES]u32 = @splat(0), + addon_failed: [pardes.MAX_PANES]u64 = @splat(0), + addon_pipeline: ?*c.SDL_GPUGraphicsPipeline = null, window: *c.SDL_Window, device: *c.SDL_GPUDevice, swapchain_format: c.SDL_GPUTextureFormat, @@ -1229,6 +1237,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di defer g.glyphs.deinit(); defer g.words.deinit(gpa); defer { + if (g.addon_gpu) |*gpu| gpu.deinit(); + if (g.addon_pipeline) |pipeline_| c.SDL_ReleaseGPUGraphicsPipeline(device, pipeline_); clearNativeImages(&g); g.native_images.deinit(gpa); g.prepared_images.deinit(gpa); @@ -4378,6 +4388,7 @@ fn prepareNativeImages( // The pictures are the page's: the other groups float over them. for (surface.placements.items) |place| if (texturePlacement(place)) appendPreparedImage(g, gpa, place); + prepareAddonGpu(g, gpa, cmd, surface); groups.items[0].image_start = 0; groups.items[0].image_count = std.math.cast(u32, g.prepared_images.items.len) orelse return false; @@ -4425,6 +4436,7 @@ fn drawNativeImagesGpu( var idx = batch.image_start; const end = batch.image_start + batch.image_count; for (g.prepared_images.items[batch.image_start..end]) |prepared| { + c.SDL_BindGPUGraphicsPipeline(pass, if (prepared.premultiplied) g.addon_pipeline.? else g.image_pipeline); const clip = imageScissor(g, prepared.place); if (clip.w > 0 and clip.h > 0) { c.SDL_SetGPUScissor(pass, &clip); @@ -9135,3 +9147,62 @@ test "detached trackpad scrolling keeps fractions local and queues whole rows fo core.update(client.pending.items[0]); try std.testing.expectEqual(@as(f32, 1.125), @as(f32, @floatFromInt(core.panes[id].?.scroll() - before)) + g.scroll_lag); } + +/// Render addon scenes to textures on this device, before the page's pass. +/// The fallback cells remain visible if decoding or pipeline creation fails. +fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, surface: *const pardes.Surface) void { + for (surface.gpu_frames, 0..) |maybe, id| { + const scratch: GpuScene.abi.Image = @fromBackingInt(@intCast(pardes.MAX_PANES + id + 1)); + const frame = maybe orelse { + if (g.addon_gpu) |*gpu| { + gpu.forget(@fromBackingInt(@intCast(id + 1))); + gpu.forget(scratch); + } + g.addon_hash[id] = 0; + g.addon_serial[id] = 0; + g.addon_failed[id] = 0; + continue; + }; + const hash = std.hash.Wyhash.hash(frame.serial, frame.bytes); + if (g.addon_failed[id] == hash) continue; + if (g.addon_gpu == null) g.addon_gpu = .init(gpa, g.device); + const gpu = &g.addon_gpu.?; + const target: GpuScene.abi.Image = @fromBackingInt(@intCast(id + 1)); + if (g.addon_hash[id] != hash or g.addon_serial[id] != frame.serial) { + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var scene = GpuScene.decodeFrame(arena.allocator(), frame.bytes) catch |err| { + log.warn("addon GPU scene: {t}", .{err}); + g.addon_failed[id] = hash; + continue; + }; + scene.pass.image = if (scene.post != null) scratch else target; + gpu.record(cmd, scene.pass) catch |err| { + log.warn("addon GPU render: {t}", .{err}); + g.addon_failed[id] = hash; + continue; + }; + if (scene.post) |post| { + gpu.postProcess(cmd, scratch, target, post) catch |err| { + log.warn("addon GPU postprocess: {t}", .{err}); + g.addon_failed[id] = hash; + continue; + }; + } else gpu.forget(scratch); + g.addon_hash[id] = hash; + g.addon_serial[id] = frame.serial; + } + const out = gpu.output(target) orelse continue; + if (g.addon_pipeline == null) { + var blend = backgroundLayerBlend(); + blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; + g.addon_pipeline = makePipeline(g.device, g.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend) catch continue; + } + g.prepared_images.append(gpa, .{ + .premultiplied = true, + .texture = out.texture, + .place = .{ .pane = @intCast(id), .raster = .{ .ptr = undefined, .w = out.width, .h = out.height, .stride = out.width * 4 }, .src = .{ .x = 0, .y = 0, .w = out.width, .h = out.height }, .dst = frame.dst, .key = hash }, + }) catch {}; + } + if (g.addon_gpu) |*gpu| gpu.endFrame(); +} |
