Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Premultiply alpha of RGBA u8 images #2095

Merged
merged 2 commits into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/re_viewer_context/src/gpu_bridge/tensor_to_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ fn color_tensor_to_gpu(
TextureFormat::Rgba8UnormSrgb,
),
(4, TensorData::U8(buf)) => (
// TODO(emilk): premultiply alpha
cast_slice_to_cow(buf.as_slice()),
// We pre-multiply on the CPU because we currently use hardware texture filtering
// in `rectangle_fs.wgsl`, and pre-multiplication needs to happen before filtering.
// If we switched our shader to always do software filtering we could do the pre-multiplication
// on the GPU instead, saving us some time here!
premultiply_alpha(buf.as_slice()).into(),
TextureFormat::Rgba8UnormSrgb,
),

Expand Down Expand Up @@ -466,6 +469,15 @@ fn pad_and_narrow_and_cast<T: Copy + Pod>(
pod_collect_to_vec(&floats).into()
}

fn premultiply_alpha(rgba: &[u8]) -> Vec<u8> {
crate::profile_function!();
rgba.chunks_exact(4)
.flat_map(|rgba| {
egui::Color32::from_rgba_unmultiplied(rgba[0], rgba[1], rgba[2], rgba[3]).to_array()
})
.collect()
}

// ----------------------------------------------------------------------------;

fn height_width_depth(tensor: &Tensor) -> anyhow::Result<[u32; 3]> {
Expand Down
Loading