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

Fix texture clamping and color gradient selection being displayed incorrectly #2394

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Changes from 1 commit
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
66 changes: 45 additions & 21 deletions crates/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,53 +40,77 @@ fn decode_color(sampled_value: Vec4) -> Vec4 {
return rgba;
}

/// Takes a floating point texel coordinate and outputs a neighrest neighbor
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
fn to_clamp_to_border_sample(coord: Vec2, texture_dimension: Vec2) -> IVec2 {
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
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 border.
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
fn clamped_bilinear_sample_positions(coord: Vec2, texture_dimension: Vec2) -> array<IVec2, 4> {
let v00 = to_clamp_to_border_sample(coord + vec2(-0.5, -0.5), texture_dimension);
let v01 = to_clamp_to_border_sample(coord + vec2(-0.5, 0.5), texture_dimension);
let v10 = to_clamp_to_border_sample(coord + vec2( 0.5, -0.5), texture_dimension);
let v11 = to_clamp_to_border_sample(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));
return mix(top, bottom, fract(coord.y - 0.5));
let top = mix(v00, v10, fract(coord.x - 0.5 + 10.0));
let bottom = mix(v01, v11, fract(coord.x - 0.5 + 10.0));
return mix(top, bottom, fract(coord.y - 0.5 + 10.0));
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
}

@fragment
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 coord = in.texcoord * Vec2(textureDimensions(texture_float).xy);
let texture_dimensions = Vec2(textureDimensions(texture_float).xy);
let coord = in.texcoord * texture_dimensions;
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(textureLoad(texture_float, IVec2(coord), 0));
normalized_value = decode_color(textureLoad(texture_float,
to_clamp_to_border_sample(coord, texture_dimensions), 0));
} else {
// bilinear
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));
let sample_positions = clamped_bilinear_sample_positions(coord, Vec2(textureDimensions(texture_float).xy));
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));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_SINT {
let coord = in.texcoord * Vec2(textureDimensions(texture_sint).xy);
let texture_dimensions = Vec2(textureDimensions(texture_sint).xy);
let coord = in.texcoord * texture_dimensions;
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(Vec4(textureLoad(texture_sint, IVec2(coord), 0)));
normalized_value = decode_color(Vec4(textureLoad(texture_sint,
to_clamp_to_border_sample(coord, texture_dimensions), 0)));
} else {
// bilinear
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)));
let sample_positions = clamped_bilinear_sample_positions(coord, Vec2(textureDimensions(texture_float).xy));
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)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_UINT {
let coord = in.texcoord * Vec2(textureDimensions(texture_uint).xy);
let texture_dimensions = Vec2(textureDimensions(texture_uint).xy);
let coord = in.texcoord * texture_dimensions;
if tex_filter(coord) == FILTER_NEAREST {
// nearest
normalized_value = decode_color(Vec4(textureLoad(texture_uint, IVec2(coord), 0)));
normalized_value = decode_color(Vec4(textureLoad(texture_uint,
to_clamp_to_border_sample(coord, texture_dimensions), 0)));
} else {
// bilinear
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)));
let sample_positions = clamped_bilinear_sample_positions(coord, Vec2(textureDimensions(texture_float).xy));
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)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else {
Expand Down