Skip to content

Commit

Permalink
fix coloured text wrongly premultiplying alpha twice
Browse files Browse the repository at this point in the history
  • Loading branch information
msparkles committed Jul 17, 2024
1 parent 46c81e6 commit 11b9269
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/yakui-core/src/paint/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions crates/yakui-vulkan/src/vulkan_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"),
}
Expand Down
7 changes: 7 additions & 0 deletions crates/yakui-wgpu/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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:?}"),
}
Expand Down Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/yakui-widgets/src/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down

0 comments on commit 11b9269

Please sign in to comment.