wgpu: Output premultiplied alpha

This commit is contained in:
Mike Welsh 2022-04-12 22:28:02 -07:00
parent 70b7222d20
commit 4a38d36cd7
4 changed files with 28 additions and 18 deletions

View File

@ -13,5 +13,6 @@ fn main_vertex(in: VertexInput) -> VertexOutput {
[[stage(fragment)]]
fn main_fragment(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return in.color * transforms.mult_color + transforms.add_color;
let out = in.color * transforms.mult_color + transforms.add_color;
return vec4<f32>(out.rgb * out.a, out.a);
}

View File

@ -43,16 +43,24 @@ var<uniform> transforms: Transforms;
/// Converts a color from linear to sRGB color space.
fn linear_to_srgb(linear: vec4<f32>) -> vec4<f32> {
let a = 12.92 * linear.rgb;
let b = 1.055 * pow(linear.rgb, vec3<f32>(1.0 / 2.4)) - 0.055;
let c = step(vec3<f32>(0.0031308), linear.rgb);
return vec4<f32>(mix(a, b, c), linear.a);
var rgb: vec3<f32> = linear.rgb;
if( linear.a > 0.0 ) {
rgb = rgb / linear.a;
}
let a = 12.92 * rgb;
let b = 1.055 * pow(rgb, vec3<f32>(1.0 / 2.4)) - 0.055;
let c = step(vec3<f32>(0.0031308), rgb);
return vec4<f32>(mix(a, b, c) * linear.a, linear.a);
}
/// Converts a color from sRGB to linear color space.
fn srgb_to_linear(srgb: vec4<f32>) -> vec4<f32> {
let a = srgb.rgb / 12.92;
let b = pow((srgb.rgb + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4));
let c = step(vec3<f32>(0.04045), srgb.rgb);
return vec4<f32>(mix(a, b, c), srgb.a);
var rgb: vec3<f32> = srgb.rgb;
if( srgb.a > 0.0 ) {
rgb = rgb / srgb.a;
}
let a = rgb / 12.92;
let b = pow((rgb + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4));
let c = step(vec3<f32>(0.04045), rgb);
return vec4<f32>(mix(a, b, c) * srgb.a, srgb.a);
}

View File

@ -101,5 +101,6 @@ fn main_fragment(in: VertexOutput) -> [[location(0)]] vec4<f32> {
if( gradient.interpolation != 0 ) {
color = linear_to_srgb(color);
}
return color * transforms.mult_color + transforms.add_color;
let out = color * transforms.mult_color + transforms.add_color;
return vec4<f32>(out.rgb * out.a, out.a);
}

View File

@ -306,7 +306,7 @@ fn create_color_pipelines(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_description,
@ -330,7 +330,7 @@ fn create_color_pipelines(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_description,
@ -354,7 +354,7 @@ fn create_color_pipelines(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_description,
@ -378,7 +378,7 @@ fn create_color_pipelines(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_description,
@ -554,7 +554,7 @@ fn create_gradient_pipeline(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_layout,
@ -578,7 +578,7 @@ fn create_gradient_pipeline(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_layout,
@ -603,7 +603,7 @@ fn create_gradient_pipeline(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_layout,
@ -627,7 +627,7 @@ fn create_gradient_pipeline(
}),
&[wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
write_mask,
}],
vertex_buffers_layout,