Skip to content

Commit

Permalink
Revert "Fix texture clamping and color gradient selection being displ…
Browse files Browse the repository at this point in the history
…ayed incorrectly (#2394)"

This reverts commit e155fe8.
  • Loading branch information
emilk committed Jun 13, 2023
1 parent 2cf3d39 commit 4d52318
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions crates/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ fn decode_color(sampled_value: Vec4) -> Vec4 {
return rgba;
}

/// Takes a floating point texel coordinate and outputs a integer texel coordinate
/// on the neighrest neighbor, clamped to the texture edge.
fn clamp_to_edge_nearest_neighbor(coord: Vec2, texture_dimension: Vec2) -> IVec2 {
return IVec2(clamp(floor(coord), Vec2(0.0), texture_dimension - Vec2(1.0)));
}

/// Takes a floating point texel coordinate and outputs the four integer texel coordinates that are used for bilinear filtering.
/// All four samples are clamped to the texture edge.
fn clamp_to_edge_bilinear_samples(coord: Vec2, texture_dimension: Vec2) -> array<IVec2, 4> {
let v00 = clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, -0.5), texture_dimension);
let v01 = clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, 0.5), texture_dimension);
let v10 = clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, -0.5), texture_dimension);
let v11 = clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, 0.5), texture_dimension);
return array<IVec2, 4>(IVec2(v00), IVec2(v01), IVec2(v10), IVec2(v11));
}

fn filter_bilinear(coord: Vec2, v00: Vec4, v01: Vec4, v10: Vec4, v11: Vec4) -> Vec4 {
let top = mix(v00, v10, fract(coord.x - 0.5));
let bottom = mix(v01, v11, fract(coord.x - 0.5));
Expand All @@ -67,51 +51,42 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
// Sample the main texture:
var normalized_value: Vec4;
if rect_info.sample_type == SAMPLE_TYPE_FLOAT {
let texture_dimensions = Vec2(textureDimensions(texture_float).xy);
let coord = in.texcoord * texture_dimensions;
let coord = in.texcoord * Vec2(textureDimensions(texture_float).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(textureLoad(texture_float,
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 0));
normalized_value = decode_color(textureLoad(texture_float, IVec2(coord), 0));
} else {
// bilinear
let sample_positions = clamp_to_edge_bilinear_samples(coord, texture_dimensions);
let v00 = decode_color(textureLoad(texture_float, sample_positions[0], 0));
let v01 = decode_color(textureLoad(texture_float, sample_positions[1], 0));
let v10 = decode_color(textureLoad(texture_float, sample_positions[2], 0));
let v11 = decode_color(textureLoad(texture_float, sample_positions[3], 0));
let v00 = decode_color(textureLoad(texture_float, IVec2(coord + vec2(-0.5, -0.5)), 0));
let v01 = decode_color(textureLoad(texture_float, IVec2(coord + vec2(-0.5, 0.5)), 0));
let v10 = decode_color(textureLoad(texture_float, IVec2(coord + vec2( 0.5, -0.5)), 0));
let v11 = decode_color(textureLoad(texture_float, IVec2(coord + vec2( 0.5, 0.5)), 0));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_SINT {
let texture_dimensions = Vec2(textureDimensions(texture_sint).xy);
let coord = in.texcoord * texture_dimensions;
let coord = in.texcoord * Vec2(textureDimensions(texture_sint).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(Vec4(textureLoad(texture_sint,
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 0)));
normalized_value = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord), 0)));
} else {
// bilinear
let sample_positions = clamp_to_edge_bilinear_samples(coord, texture_dimensions);
let v00 = decode_color(Vec4(textureLoad(texture_sint, sample_positions[0], 0)));
let v01 = decode_color(Vec4(textureLoad(texture_sint, sample_positions[1], 0)));
let v10 = decode_color(Vec4(textureLoad(texture_sint, sample_positions[2], 0)));
let v11 = decode_color(Vec4(textureLoad(texture_sint, sample_positions[3], 0)));
let v00 = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord + vec2(-0.5, -0.5)), 0)));
let v01 = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord + vec2(-0.5, 0.5)), 0)));
let v10 = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord + vec2( 0.5, -0.5)), 0)));
let v11 = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord + vec2( 0.5, 0.5)), 0)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_UINT {
let texture_dimensions = Vec2(textureDimensions(texture_uint).xy);
let coord = in.texcoord * texture_dimensions;
let coord = in.texcoord * Vec2(textureDimensions(texture_uint).xy);
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(Vec4(textureLoad(texture_uint,
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 0)));
normalized_value = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord), 0)));
} else {
// bilinear
let sample_positions = clamp_to_edge_bilinear_samples(coord, texture_dimensions);
let v00 = decode_color(Vec4(textureLoad(texture_uint, sample_positions[0], 0)));
let v01 = decode_color(Vec4(textureLoad(texture_uint, sample_positions[1], 0)));
let v10 = decode_color(Vec4(textureLoad(texture_uint, sample_positions[2], 0)));
let v11 = decode_color(Vec4(textureLoad(texture_uint, sample_positions[3], 0)));
let v00 = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord + vec2(-0.5, -0.5)), 0)));
let v01 = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord + vec2(-0.5, 0.5)), 0)));
let v10 = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord + vec2( 0.5, -0.5)), 0)));
let v11 = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord + vec2( 0.5, 0.5)), 0)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else {
Expand Down

0 comments on commit 4d52318

Please sign in to comment.