From 36fb0b62ec856ff40d5976ed5006b2fcb6e09afe Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 20 Nov 2024 17:07:51 -0500 Subject: [PATCH] Add premultiplied RGBA8 sRGB texture format Part of #194 --- crates/yakui-core/src/paint/texture.rs | 4 ++++ crates/yakui-vulkan/src/vulkan_texture.rs | 1 + crates/yakui-wgpu/src/texture.rs | 7 +++++++ 3 files changed, 12 insertions(+) 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 4698762d..15ce5ec3 100644 --- a/crates/yakui-vulkan/src/vulkan_texture.rs +++ b/crates/yakui-vulkan/src/vulkan_texture.rs @@ -181,6 +181,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 71d275a6..93acc773 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), }