Use premultiplied colors in triangle pipelines

This commit is contained in:
Héctor Ramón Jiménez 2025-05-12 18:35:27 +02:00
parent df37d6f9bc
commit 098bd1b8a4
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 37 additions and 33 deletions

View file

@ -97,6 +97,8 @@ impl Pipeline {
"../shader/quad/gradient.wgsl"
),
"\n",
include_str!("../shader/color.wgsl"),
"\n",
include_str!("../shader/color/oklab.wgsl")
)
} else {
@ -109,6 +111,8 @@ impl Pipeline {
"../shader/quad/gradient.wgsl"
),
"\n",
include_str!("../shader/color.wgsl"),
"\n",
include_str!(
"../shader/color/linear_rgb.wgsl"
)

View file

@ -74,6 +74,8 @@ impl Pipeline {
label: Some("iced_wgpu.quad.solid.shader"),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
concat!(
include_str!("../shader/color.wgsl"),
"\n",
include_str!("../shader/quad.wgsl"),
"\n",
include_str!("../shader/vertex.wgsl"),

View file

@ -0,0 +1,14 @@
fn premultiply(color: vec4<f32>) -> vec4<f32> {
return vec4(color.xyz * color.a, color.a);
}
fn unpack_color(data: vec2<u32>) -> vec4<f32> {
return premultiply(unpack_u32(data));
}
fn unpack_u32(data: vec2<u32>) -> vec4<f32> {
let rg: vec2<f32> = unpack2x16float(data.x);
let ba: vec2<f32> = unpack2x16float(data.y);
return vec4<f32>(rg.y, rg.x, ba.y, ba.x);
}

View file

@ -20,7 +20,7 @@ fn interpolate_color(from_: vec4<f32>, to_: vec4<f32>, factor: f32) -> vec4<f32>
var color = to_rgb * (mixed * mixed * mixed);
// Alpha interpolation
color = to_ * factor + from_ * (1.0 - factor);
color.a = mix(from_.a, to_.a, factor);
return color;
}

View file

@ -5,10 +5,6 @@ struct Globals {
@group(0) @binding(0) var<uniform> globals: Globals;
fn premultiply(color: vec4<f32>) -> vec4<f32> {
return vec4(color.xyz * color.a, color.a);
}
fn distance_alg(
frag_coord: vec2<f32>,
position: vec2<f32>,

View file

@ -196,14 +196,3 @@ fn gradient_fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
return mixed_color * radius_alpha;
}
fn unpack_color(data: vec2<u32>) -> vec4<f32> {
return premultiply(unpack_u32(data));
}
fn unpack_u32(data: vec2<u32>) -> vec4<f32> {
let rg: vec2<f32> = unpack2x16float(data.x);
let ba: vec2<f32> = unpack2x16float(data.y);
return vec4<f32>(rg.y, rg.x, ba.y, ba.x);
}

View file

@ -87,14 +87,14 @@ fn gradient(
@fragment
fn gradient_fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
let colors = array<vec4<f32>, 8>(
unpack_u32(input.colors_1.xy),
unpack_u32(input.colors_1.zw),
unpack_u32(input.colors_2.xy),
unpack_u32(input.colors_2.zw),
unpack_u32(input.colors_3.xy),
unpack_u32(input.colors_3.zw),
unpack_u32(input.colors_4.xy),
unpack_u32(input.colors_4.zw),
unpack_color(input.colors_1.xy),
unpack_color(input.colors_1.zw),
unpack_color(input.colors_2.xy),
unpack_color(input.colors_2.zw),
unpack_color(input.colors_3.xy),
unpack_color(input.colors_3.zw),
unpack_color(input.colors_4.xy),
unpack_color(input.colors_4.zw),
);
let offsets_1: vec4<f32> = unpack_u32(input.offsets.xy);
@ -122,13 +122,6 @@ fn gradient_fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
return gradient(input.raw_position, input.direction, colors, offsets, last_index);
}
fn unpack_u32(color: vec2<u32>) -> vec4<f32> {
let rg: vec2<f32> = unpack2x16float(color.x);
let ba: vec2<f32> = unpack2x16float(color.y);
return vec4<f32>(rg.y, rg.x, ba.y, ba.x);
}
fn random(coords: vec2<f32>) -> f32 {
return fract(sin(dot(coords, vec2(12.9898,78.233))) * 43758.5453);
}

View file

@ -12,7 +12,7 @@ struct SolidVertexOutput {
fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
var out: SolidVertexOutput;
out.color = input.color;
out.color = premultiply(input.color);
out.position = globals.transform * vec4<f32>(input.position, 0.0, 1.0);
return out;

View file

@ -595,7 +595,7 @@ fn fragment_target(
) -> wgpu::ColorTargetState {
wgpu::ColorTargetState {
format: texture_format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}
}
@ -754,6 +754,8 @@ mod solid {
include_str!("shader/triangle.wgsl"),
"\n",
include_str!("shader/triangle/solid.wgsl"),
"\n",
include_str!("shader/color.wgsl"),
)),
),
});
@ -913,6 +915,8 @@ mod gradient {
"shader/triangle/gradient.wgsl"
),
"\n",
include_str!("shader/color.wgsl"),
"\n",
include_str!("shader/color/oklab.wgsl")
)
} else {
@ -923,6 +927,8 @@ mod gradient {
"shader/triangle/gradient.wgsl"
),
"\n",
include_str!("shader/color.wgsl"),
"\n",
include_str!(
"shader/color/linear_rgb.wgsl"
)