Skip to content

Commit

Permalink
Implement billinear filtering of textures (#1850)
Browse files Browse the repository at this point in the history
* Implement opt-in billinear filtering of textures

* bilinear
  • Loading branch information
emilk authored Apr 14, 2023
1 parent beffbc3 commit d33dab6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 32 deletions.
66 changes: 60 additions & 6 deletions crates/re_renderer/shader/rectangle.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const COLOR_MAPPER_OFF = 1u;
const COLOR_MAPPER_FUNCTION = 2u;
const COLOR_MAPPER_TEXTURE = 3u;

const FILTER_NEAREST = 1u;
const FILTER_BILINEAR = 2u;

struct UniformBuffer {
/// Top left corner position in world space.
top_left_corner_position: Vec3,
Expand Down Expand Up @@ -48,6 +51,9 @@ struct UniformBuffer {
/// Exponent to raise the normalized texture value.
/// Inverse brightness.
gamma: f32,

minification_filter: u32,
magnification_filter: u32,
};

@group(1) @binding(0)
Expand Down Expand Up @@ -90,6 +96,18 @@ fn vs_main(@builtin(vertex_index) v_idx: u32) -> VertexOut {
return out;
}

fn is_magnifying(pixel_coord: Vec2) -> bool {
return fwidth(pixel_coord.x) < 1.0;
}

fn tex_filter(pixel_coord: Vec2) -> u32 {
if is_magnifying(pixel_coord) {
return rect_info.magnification_filter;
} else {
return rect_info.minification_filter;
}
}

@fragment
fn fs_main(in: VertexOut) -> @location(0) Vec4 {
// Sample the main texture:
Expand All @@ -98,14 +116,50 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
// TODO(emilk): support mipmaps
sampled_value = textureSampleLevel(texture_float_filterable, texture_sampler, in.texcoord, 0.0);
} else if rect_info.sample_type == SAMPLE_TYPE_FLOAT_NOFILTER {
let icoords = IVec2(in.texcoord * Vec2(textureDimensions(texture_float).xy));
sampled_value = Vec4(textureLoad(texture_float, icoords, 0));
let coord = in.texcoord * Vec2(textureDimensions(texture_float).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
sampled_value = textureLoad(texture_float, IVec2(coord + vec2(0.5)), 0);
} else {
// bilinear
let v00 = textureLoad(texture_float, IVec2(coord) + IVec2(0, 0), 0);
let v01 = textureLoad(texture_float, IVec2(coord) + IVec2(0, 1), 0);
let v10 = textureLoad(texture_float, IVec2(coord) + IVec2(1, 0), 0);
let v11 = textureLoad(texture_float, IVec2(coord) + IVec2(1, 1), 0);
let top = mix(v00, v10, fract(coord.x));
let bottom = mix(v01, v11, fract(coord.x));
sampled_value = mix(top, bottom, fract(coord.y));
}
} else if rect_info.sample_type == SAMPLE_TYPE_SINT_NOFILTER {
let icoords = IVec2(in.texcoord * Vec2(textureDimensions(texture_sint).xy));
sampled_value = Vec4(textureLoad(texture_sint, icoords, 0));
let coord = in.texcoord * Vec2(textureDimensions(texture_sint).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
sampled_value = Vec4(textureLoad(texture_sint, IVec2(coord + vec2(0.5)), 0));
} else {
// bilinear
let v00 = Vec4(textureLoad(texture_sint, IVec2(coord) + IVec2(0, 0), 0));
let v01 = Vec4(textureLoad(texture_sint, IVec2(coord) + IVec2(0, 1), 0));
let v10 = Vec4(textureLoad(texture_sint, IVec2(coord) + IVec2(1, 0), 0));
let v11 = Vec4(textureLoad(texture_sint, IVec2(coord) + IVec2(1, 1), 0));
let top = mix(v00, v10, fract(coord.x));
let bottom = mix(v01, v11, fract(coord.x));
sampled_value = mix(top, bottom, fract(coord.y));
}
} else if rect_info.sample_type == SAMPLE_TYPE_UINT_NOFILTER {
let icoords = IVec2(in.texcoord * Vec2(textureDimensions(texture_uint).xy));
sampled_value = Vec4(textureLoad(texture_uint, icoords, 0));
let coord = in.texcoord * Vec2(textureDimensions(texture_uint).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
sampled_value = Vec4(textureLoad(texture_uint, IVec2(coord + vec2(0.5)), 0));
} else {
// bilinear
let v00 = Vec4(textureLoad(texture_uint, IVec2(coord) + IVec2(0, 0), 0));
let v01 = Vec4(textureLoad(texture_uint, IVec2(coord) + IVec2(0, 1), 0));
let v10 = Vec4(textureLoad(texture_uint, IVec2(coord) + IVec2(1, 0), 0));
let v11 = Vec4(textureLoad(texture_uint, IVec2(coord) + IVec2(1, 1), 0));
let top = mix(v00, v10, fract(coord.x));
let bottom = mix(v01, v11, fract(coord.x));
sampled_value = mix(top, bottom, fract(coord.y));
}
} else {
return ERROR_RGBA; // unknown sample type
}
Expand Down
18 changes: 16 additions & 2 deletions crates/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ mod gpu_data {
const COLOR_MAPPER_FUNCTION: u32 = 2;
const COLOR_MAPPER_TEXTURE: u32 = 3;

const FILTER_NEAREST: u32 = 1;
const FILTER_BILINEAR: u32 = 2;

#[repr(C, align(256))]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UniformBuffer {
Expand All @@ -213,7 +216,8 @@ mod gpu_data {

color_mapper: u32,
gamma: f32,
_row_padding: [u32; 2],
minification_filter: u32,
magnification_filter: u32,

_end_padding: [wgpu_buffer_types::PaddingRow; 16 - 6],
}
Expand Down Expand Up @@ -275,6 +279,15 @@ mod gpu_data {
}
}

let minification_filter = match rectangle.texture_filter_minification {
super::TextureFilterMin::Linear => FILTER_BILINEAR,
super::TextureFilterMin::Nearest => FILTER_NEAREST,
};
let magnification_filter = match rectangle.texture_filter_magnification {
super::TextureFilterMag::Linear => FILTER_BILINEAR,
super::TextureFilterMag::Nearest => FILTER_NEAREST,
};

Ok(Self {
top_left_corner_position: rectangle.top_left_corner_position.into(),
colormap_function,
Expand All @@ -287,7 +300,8 @@ mod gpu_data {
range_min_max: (*range).into(),
color_mapper: color_mapper_int,
gamma: *gamma,
_row_padding: Default::default(),
minification_filter,
magnification_filter,
_end_padding: Default::default(),
})
}
Expand Down
45 changes: 21 additions & 24 deletions crates/re_viewer/src/ui/view_tensor/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,32 +426,29 @@ impl TextureSettings {
});
ui.end_row();

// TODO(#1612): support texture filtering again
if false {
re_ui
.grid_left_hand_label(ui, "Filtering")
.on_hover_text("Filtering to use when magnifying");

fn tf_to_string(tf: egui::TextureFilter) -> &'static str {
match tf {
egui::TextureFilter::Nearest => "Nearest",
egui::TextureFilter::Linear => "Linear",
}
re_ui
.grid_left_hand_label(ui, "Filtering")
.on_hover_text("Filtering to use when magnifying");

fn tf_to_string(tf: egui::TextureFilter) -> &'static str {
match tf {
egui::TextureFilter::Nearest => "Nearest",
egui::TextureFilter::Linear => "Linear",
}
egui::ComboBox::from_id_source("texture_filter")
.selected_text(tf_to_string(options.magnification))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

let mut selectable_value = |ui: &mut egui::Ui, e| {
ui.selectable_value(&mut options.magnification, e, tf_to_string(e))
};
selectable_value(ui, egui::TextureFilter::Linear);
selectable_value(ui, egui::TextureFilter::Nearest);
});
ui.end_row();
}
egui::ComboBox::from_id_source("texture_filter")
.selected_text(tf_to_string(options.magnification))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

let mut selectable_value = |ui: &mut egui::Ui, e| {
ui.selectable_value(&mut options.magnification, e, tf_to_string(e))
};
selectable_value(ui, egui::TextureFilter::Nearest);
selectable_value(ui, egui::TextureFilter::Linear);
});
ui.end_row();
}
}

Expand Down

1 comment on commit d33dab6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: d33dab6 Previous: beffbc3 Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/insert/default 3010738 ns/iter (± 65920) 2877636 ns/iter (± 62881) 1.05
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 371 ns/iter (± 2) 372 ns/iter (± 5) 1.00
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 261 ns/iter (± 1) 262 ns/iter (± 0) 1.00
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 422 ns/iter (± 0) 422 ns/iter (± 0) 1
datastore/num_rows=1000/num_instances=1000/packed=false/range/default 3330134 ns/iter (± 112525) 3055292 ns/iter (± 80869) 1.09
datastore/num_rows=1000/num_instances=1000/gc/default 2424916 ns/iter (± 4701) 2426490 ns/iter (± 5620) 1.00
mono_points_arrow/generate_message_bundles 28432068 ns/iter (± 297731) 28560983 ns/iter (± 397414) 1.00
mono_points_arrow/generate_messages 114112970 ns/iter (± 886245) 114092789 ns/iter (± 866742) 1.00
mono_points_arrow/encode_log_msg 145899870 ns/iter (± 1004942) 146171056 ns/iter (± 1264660) 1.00
mono_points_arrow/encode_total 289080503 ns/iter (± 1272415) 287725342 ns/iter (± 1634449) 1.00
mono_points_arrow/decode_log_msg 179424574 ns/iter (± 722632) 178510582 ns/iter (± 706333) 1.01
mono_points_arrow/decode_message_bundles 62185032 ns/iter (± 620422) 60513277 ns/iter (± 545427) 1.03
mono_points_arrow/decode_total 240383305 ns/iter (± 1265142) 237899745 ns/iter (± 1140028) 1.01
mono_points_arrow_batched/generate_message_bundles 23470439 ns/iter (± 1057275) 23595041 ns/iter (± 1285668) 0.99
mono_points_arrow_batched/generate_messages 4586860 ns/iter (± 170445) 4529912 ns/iter (± 293322) 1.01
mono_points_arrow_batched/encode_log_msg 1375933 ns/iter (± 3621) 1380953 ns/iter (± 3357) 1.00
mono_points_arrow_batched/encode_total 30564909 ns/iter (± 904055) 30155636 ns/iter (± 1228395) 1.01
mono_points_arrow_batched/decode_log_msg 785594 ns/iter (± 2193) 782504 ns/iter (± 3329) 1.00
mono_points_arrow_batched/decode_message_bundles 7990733 ns/iter (± 219325) 7674332 ns/iter (± 314277) 1.04
mono_points_arrow_batched/decode_total 9926223 ns/iter (± 391846) 8905489 ns/iter (± 340569) 1.11
batch_points_arrow/generate_message_bundles 195680 ns/iter (± 257) 193693 ns/iter (± 413) 1.01
batch_points_arrow/generate_messages 5108 ns/iter (± 12) 5098 ns/iter (± 9) 1.00
batch_points_arrow/encode_log_msg 265634 ns/iter (± 1424) 259276 ns/iter (± 1454) 1.02
batch_points_arrow/encode_total 491914 ns/iter (± 2123) 487673 ns/iter (± 1840) 1.01
batch_points_arrow/decode_log_msg 212014 ns/iter (± 944) 211534 ns/iter (± 994) 1.00
batch_points_arrow/decode_message_bundles 1907 ns/iter (± 5) 1900 ns/iter (± 3) 1.00
batch_points_arrow/decode_total 224269 ns/iter (± 2163) 219596 ns/iter (± 907) 1.02
arrow_mono_points/insert 2316354742 ns/iter (± 4325193) 2306524156 ns/iter (± 6595048) 1.00
arrow_mono_points/query 1213093 ns/iter (± 15728) 1193517 ns/iter (± 10357) 1.02
arrow_batch_points/insert 1154639 ns/iter (± 3975) 1147408 ns/iter (± 6110) 1.01
arrow_batch_points/query 14532 ns/iter (± 93) 14360 ns/iter (± 113) 1.01
arrow_batch_vecs/insert 26419 ns/iter (± 45) 26264 ns/iter (± 341) 1.01
arrow_batch_vecs/query 325444 ns/iter (± 452) 326125 ns/iter (± 785) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.