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 WebGL crash due to rectangle shader returning arrays #2413

Merged
merged 1 commit into from
Jun 13, 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
37 changes: 12 additions & 25 deletions crates/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ 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 @@ -75,11 +65,10 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 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, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, -0.5), texture_dimensions), 0));
let v01 = decode_color(textureLoad(texture_float, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, 0.5), texture_dimensions), 0));
let v10 = decode_color(textureLoad(texture_float, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, -0.5), texture_dimensions), 0));
let v11 = decode_color(textureLoad(texture_float, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, 0.5), texture_dimensions), 0));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_SINT {
Expand All @@ -91,11 +80,10 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 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, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, -0.5), texture_dimensions), 0)));
let v01 = decode_color(Vec4(textureLoad(texture_sint, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, 0.5), texture_dimensions), 0)));
let v10 = decode_color(Vec4(textureLoad(texture_sint, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, -0.5), texture_dimensions), 0)));
let v11 = decode_color(Vec4(textureLoad(texture_sint, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, 0.5), texture_dimensions), 0)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else if rect_info.sample_type == SAMPLE_TYPE_UINT {
Expand All @@ -107,11 +95,10 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
clamp_to_edge_nearest_neighbor(coord, texture_dimensions), 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, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, -0.5), texture_dimensions), 0)));
let v01 = decode_color(Vec4(textureLoad(texture_uint, clamp_to_edge_nearest_neighbor(coord + vec2(-0.5, 0.5), texture_dimensions), 0)));
let v10 = decode_color(Vec4(textureLoad(texture_uint, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, -0.5), texture_dimensions), 0)));
let v11 = decode_color(Vec4(textureLoad(texture_uint, clamp_to_edge_nearest_neighbor(coord + vec2( 0.5, 0.5), texture_dimensions), 0)));
normalized_value = filter_bilinear(coord, v00, v01, v10, v11);
}
} else {
Expand Down