From 11b92699a06a47e64ae4699c47fb017fc26a3c22 Mon Sep 17 00:00:00 2001 From: Madeline Sparkles Date: Wed, 17 Jul 2024 20:42:14 +0800 Subject: [PATCH] fix coloured text wrongly premultiplying alpha twice --- crates/yakui-core/src/paint/texture.rs | 4 ++++ crates/yakui-vulkan/src/vulkan_texture.rs | 1 + crates/yakui-wgpu/src/texture.rs | 7 +++++++ crates/yakui-widgets/src/text_renderer.rs | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/yakui-core/src/paint/texture.rs b/crates/yakui-core/src/paint/texture.rs index f9f29245..25c5c6c0 100644 --- a/crates/yakui-core/src/paint/texture.rs +++ b/crates/yakui-core/src/paint/texture.rs @@ -37,6 +37,10 @@ pub enum TextureFormat { /// color channels are sRGB-encoded. Rgba8Srgb, + /// Red, green, blue, and alpha channels, each represented as a `u8`. The + /// color channels are sRGB-encoded and premultiplied by the alpha. + Rgba8SrgbPremultiplied, + /// A single color channel represented as a `u8`. R8, } diff --git a/crates/yakui-vulkan/src/vulkan_texture.rs b/crates/yakui-vulkan/src/vulkan_texture.rs index 712bea18..c746db20 100644 --- a/crates/yakui-vulkan/src/vulkan_texture.rs +++ b/crates/yakui-vulkan/src/vulkan_texture.rs @@ -180,6 +180,7 @@ impl VulkanTexture { fn get_format(yakui_format: yakui::paint::TextureFormat) -> vk::Format { match yakui_format { yakui::paint::TextureFormat::Rgba8Srgb => vk::Format::R8G8B8A8_SRGB, + yakui::paint::TextureFormat::Rgba8SrgbPremultiplied => vk::Format::R8G8B8A8_SRGB, yakui::paint::TextureFormat::R8 => vk::Format::R8_UNORM, _ => panic!("Unsupported texture format: {yakui_format:?}"), } diff --git a/crates/yakui-wgpu/src/texture.rs b/crates/yakui-wgpu/src/texture.rs index 3ebc0813..38daecfe 100644 --- a/crates/yakui-wgpu/src/texture.rs +++ b/crates/yakui-wgpu/src/texture.rs @@ -109,6 +109,11 @@ fn data_layout(format: TextureFormat, size: UVec2) -> wgpu::ImageDataLayout { bytes_per_row: Some(4 * size.x), rows_per_image: Some(size.y), }, + TextureFormat::Rgba8SrgbPremultiplied => wgpu::ImageDataLayout { + offset: 0, + bytes_per_row: Some(4 * size.x), + rows_per_image: Some(size.y), + }, TextureFormat::R8 => wgpu::ImageDataLayout { offset: 0, bytes_per_row: Some(size.x), @@ -121,6 +126,7 @@ fn data_layout(format: TextureFormat, size: UVec2) -> wgpu::ImageDataLayout { fn wgpu_format(format: TextureFormat) -> wgpu::TextureFormat { match format { TextureFormat::Rgba8Srgb => wgpu::TextureFormat::Rgba8UnormSrgb, + TextureFormat::Rgba8SrgbPremultiplied => wgpu::TextureFormat::Rgba8UnormSrgb, TextureFormat::R8 => wgpu::TextureFormat::R8Unorm, _ => panic!("Unsupported texture format {format:?}"), } @@ -157,6 +163,7 @@ fn premultiply_alpha(texture: &Texture) -> Cow<'_, Texture> { Cow::Owned(texture) } + TextureFormat::Rgba8SrgbPremultiplied => Cow::Borrowed(texture), TextureFormat::R8 => Cow::Borrowed(texture), _ => Cow::Borrowed(texture), } diff --git a/crates/yakui-widgets/src/text_renderer.rs b/crates/yakui-widgets/src/text_renderer.rs index bc050405..02fcec18 100644 --- a/crates/yakui-widgets/src/text_renderer.rs +++ b/crates/yakui-widgets/src/text_renderer.rs @@ -23,7 +23,7 @@ impl Kind { fn texture_format(self) -> TextureFormat { match self { Kind::Mask => TextureFormat::R8, - Kind::Color => TextureFormat::Rgba8Srgb, + Kind::Color => TextureFormat::Rgba8SrgbPremultiplied, } } }