diff --git a/src/Backend.zig b/src/Backend.zig index cc3d8319..73975a44 100644 --- a/src/Backend.zig +++ b/src/Backend.zig @@ -130,6 +130,33 @@ pub fn textureCreateTarget(self: Backend, options: dvui.Texture.CreateOptions) T return self.renderer().textureCreateTarget(options); } +/// Whether the backend can read back a rectangle of the current render target — see +/// `readPixels`. Optional: a backend that cannot simply lacks the decl. +pub const support_read_pixels = @hasDecl(Implementation, "readPixels"); + +/// Read back `rect` of the current render target (straight RGBA, `rect.w * rect.h * 4` bytes) — +/// what has been drawn there so far this frame. Only when `support_read_pixels`. +pub fn readPixels(self: Backend, rect: dvui.Rect.Physical, pixels_out: [*]u8) TextureError!void { + if (!support_read_pixels) return TextureError.TextureRead; + return self.renderer().readPixels(rect, pixels_out); +} + +/// How a texture's texels combine with what is already in the target when drawn. `over` is +/// the premultiplied source-over every texture is created with. `add` sums texel and target +/// (colour and alpha alike) — what a weighted-tap filter needs to average translucent +/// sources, where source-over with running weights overshoots. `copy` writes the texel +/// (alpha included) over the target — a frost that replaces what it covers. +pub const TextureBlend = enum { over, add, copy }; + +/// Whether `textureBlend` is available. Optional: a backend that cannot simply lacks the decl. +pub const support_texture_blend = @hasDecl(Implementation, "textureBlend"); + +/// Set how `texture` blends when drawn, until set again. Only when `support_texture_blend`. +pub fn textureBlend(self: Backend, texture: dvui.Texture, blend: TextureBlend) TextureError!void { + if (!support_texture_blend) return TextureError.NotImplemented; + return self.renderer().textureBlend(texture, blend); +} + /// Read pixel data (RGBA) from `texture` into `pixels_out`. pub fn textureReadTarget(self: Backend, texture: dvui.TextureTarget, pixels_out: [*]u8) TextureError!void { return self.renderer().textureReadTarget(texture, pixels_out); diff --git a/src/backends/proxy.zig b/src/backends/proxy.zig index 7c48bcbc..0287fca4 100644 --- a/src/backends/proxy.zig +++ b/src/backends/proxy.zig @@ -119,6 +119,12 @@ pub fn textureCreateTarget(_: *ProxyBackend, options: dvui.Texture.CreateOptions return proxy_bridge.targetFromDesc(desc) catch error.TextureCreate; } +pub fn textureBlend(_: *ProxyBackend, texture: dvui.Texture, blend: dvui.Backend.TextureBlend) !void { + const b = proxy_bridge.bridge orelse return error.NotImplemented; + var desc = proxy_bridge.textureDescFrom(texture); + if (b.texture_blend(bridgeCtx(b), &desc, @intFromEnum(blend)) == 0) return error.NotImplemented; +} + pub fn textureClearTarget(_: *ProxyBackend, target: dvui.TextureTarget) void { const b = bridgeGeneric() catch return; var desc = proxy_bridge.textureDescFromTarget(target); diff --git a/src/backends/proxy_bridge.zig b/src/backends/proxy_bridge.zig index aaa047d6..71c39460 100644 --- a/src/backends/proxy_bridge.zig +++ b/src/backends/proxy_bridge.zig @@ -79,6 +79,8 @@ pub const RenderBridge = extern struct { texture_destroy: *const fn (ctx: ?*anyopaque, texture: *const TextureDesc) callconv(.c) void, texture_create_target: *const fn (ctx: ?*anyopaque, options: CreateOptions) callconv(.c) TextureDesc, + /// `Backend.textureBlend`; `blend` is `@intFromEnum(Backend.TextureBlend)`. Returns 0 on failure. + texture_blend: *const fn (ctx: ?*anyopaque, texture: *const TextureDesc, blend: u8) callconv(.c) u8, texture_read_target: *const fn (ctx: ?*anyopaque, target: *const TextureDesc, pixels_out: [*]u8) callconv(.c) u8, texture_destroy_target: *const fn (ctx: ?*anyopaque, target: *const TextureDesc) callconv(.c) void, texture_clear_target: *const fn (ctx: ?*anyopaque, target: *const TextureDesc) callconv(.c) void, diff --git a/src/backends/sdl.zig b/src/backends/sdl.zig index e218b211..30aca2f4 100644 --- a/src/backends/sdl.zig +++ b/src/backends/sdl.zig @@ -1302,6 +1302,44 @@ pub fn textureClearTarget(self: *SDLBackend, texture: dvui.TextureTarget) void { ) catch return; } +/// See `dvui.Backend.textureBlend`. +pub fn textureBlend(_: *SDLBackend, texture: dvui.Texture, blend: dvui.Backend.TextureBlend) !void { + const mode = switch (blend) { + .over => c.SDL_ComposeCustomBlendMode(c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, c.SDL_BLENDOPERATION_ADD, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, c.SDL_BLENDOPERATION_ADD), + .add => c.SDL_ComposeCustomBlendMode(c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDOPERATION_ADD, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDOPERATION_ADD), + .copy => c.SDL_BLENDMODE_NONE, + }; + try toErr(c.SDL_SetTextureBlendMode(@ptrCast(@alignCast(texture.ptr)), mode), "SDL_SetTextureBlendMode in textureBlend"); +} + +/// Read back a rectangle of the *current* render target (the window, unless a target is bound) +/// as straight RGBA into `pixels_out` (`rect.w * rect.h * 4` bytes). What has been drawn so far +/// this frame — a backdrop that wants "what is under me" without replaying the command queue. +/// SDL3 only. +pub fn readPixels(self: *SDLBackend, rect: dvui.Rect.Physical, pixels_out: [*]u8) !void { + if (!sdl3) return dvui.Backend.TextureError.TextureRead; + const r: c.SDL_Rect = .{ .x = @intFromFloat(rect.x), .y = @intFromFloat(rect.y), .w = @intFromFloat(rect.w), .h = @intFromFloat(rect.h) }; + var surface: *c.SDL_Surface = c.SDL_RenderReadPixels(self.renderer, &r) orelse + logErr("SDL_RenderReadPixels in readPixels") catch + return dvui.Backend.TextureError.TextureRead; + defer c.SDL_DestroySurface(surface); + if (surface.*.w != r.w or surface.*.h != r.h) return dvui.Backend.TextureError.TextureRead; + if (surface.*.format != c.SDL_PIXELFORMAT_ABGR8888) { + const converted = c.SDL_ConvertSurface(surface, c.SDL_PIXELFORMAT_ABGR8888) orelse + logErr("SDL_ConvertSurface in readPixels") catch + return dvui.Backend.TextureError.TextureRead; + c.SDL_DestroySurface(surface); + surface = converted; + } + const w: usize = @intCast(r.w); + const h: usize = @intCast(r.h); + const pitch: usize = @intCast(surface.*.pitch); + const src: [*]const u8 = @ptrCast(surface.*.pixels); + for (0..h) |y| { + @memcpy(pixels_out[y * w * 4 .. (y + 1) * w * 4], src[y * pitch .. y * pitch + w * 4]); + } +} + pub fn textureReadTarget(self: *SDLBackend, texture: dvui.TextureTarget, pixels_out: [*]u8) !void { if (sdl3) { // null is the default target