Skip to content

Commit

Permalink
fix nan handling in WGLMakie
Browse files Browse the repository at this point in the history
  • Loading branch information
EdsterG committed Sep 3, 2024
1 parent 0368dea commit 9286f60
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
14 changes: 8 additions & 6 deletions WGLMakie/src/Lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
flat out vec2 f_extrusion; // invalid / not needed
flat out float f_linewidth;
flat out vec4 f_pattern_overwrite; // invalid / not needed
flat out uint f_instance_id;
flat out int f_instance_id;
flat out ${color} f_color1;
flat out ${color} f_color2;
flat out float f_alpha_weight;
Expand Down Expand Up @@ -188,7 +188,8 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
// used to compute width sdf
f_linewidth = halfwidth;
f_instance_id = uint(2 * gl_InstanceID);
// TODO: this line crashes when lineindex_start is of type uint, why?
f_instance_id = lineindex_start; // NOTE: this is correct, no need to multiple by 2
// we restart patterns for each segment
f_cumulative_length = 0.0;
Expand Down Expand Up @@ -252,7 +253,7 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
flat out vec2 f_extrusion;
flat out float f_linewidth;
flat out vec4 f_pattern_overwrite;
flat out uint f_instance_id;
flat out int f_instance_id;
flat out ${color} f_color1;
flat out ${color} f_color2;
flat out float f_alpha_weight;
Expand Down Expand Up @@ -640,7 +641,8 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
// used to compute width sdf
f_linewidth = halfwidth;
f_instance_id = uint(gl_InstanceID);
// TODO: this line crashes when lineindex_start is of type uint, why?
f_instance_id = lineindex_start;
f_cumulative_length = lastlen_start;
Expand Down Expand Up @@ -749,7 +751,7 @@ function lines_fragment_shader(uniforms, attributes) {
flat in ${color} f_color1;
flat in ${color} f_color2;
flat in float f_alpha_weight;
flat in uint f_instance_id;
flat in int f_instance_id;
flat in float f_cumulative_length;
flat in ivec2 f_capmode;
flat in vec4 f_linepoints;
Expand Down Expand Up @@ -996,7 +998,7 @@ function lines_fragment_shader(uniforms, attributes) {
if (picking) {
if (color.a > 0.1) {
fragment_color = pack_int(object_id, f_instance_id);
fragment_color = pack_int(object_id, uint(f_instance_id));
}
return;
}
Expand Down
10 changes: 8 additions & 2 deletions WGLMakie/src/Shaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ export function attributes_to_type_declaration(attributes_dict) {
let result = "";
for (const name in attributes_dict) {
const attribute = attributes_dict[name];
const type = attribute_type(attribute);
result += `in ${type} ${name};\n`;
// TODO: what's the right way to do this?
if (name.startsWith("lineindex")) {
// TODO: uint seems to crash, probably due to poor browser support for uint?
result += `in int ${name};\n`;
} else {
const type = attribute_type(attribute);
result += `in ${type} ${name};\n`;
}
}
return result;
}
7 changes: 5 additions & 2 deletions WGLMakie/src/lines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function serialize_three(scene::Scene, plot::Union{Lines, LineSegments})
# involved point are not NaN, i.e. p1 -- p2 is only drawn if all of
# (p0, p1, p2, p3) are not NaN. So if p3 is NaN we need to dublicate p2 to
# make the p1 -- p2 segment draw, which is what indices does.
indices = Observable(Int[])
indices = Observable(Int32[])
points_transformed = lift(
plot, f32c, transform_func_obs(plot), plot.model, plot[1], plot.space
) do f32c, tf, model, ps, space
Expand Down Expand Up @@ -118,7 +118,10 @@ function serialize_three(scene::Scene, plot::Union{Lines, LineSegments})
end
end
positions = lift(serialize_buffer_attribute, plot, points_transformed)
attributes = Dict{Symbol, Any}(:linepoint => positions)
attributes = Dict{Symbol, Any}(
:linepoint => positions,
:lineindex => lift(_ -> serialize_buffer_attribute(indices[]), plot, points_transformed),
)

# TODO: in Javascript
# NOTE: clip.w needs to be available in shaders to avoid line inversion problems
Expand Down
22 changes: 14 additions & 8 deletions WGLMakie/src/wglmakie.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -20253,8 +20253,12 @@ function attributes_to_type_declaration(attributes_dict) {
let result = "";
for(const name in attributes_dict){
const attribute = attributes_dict[name];
const type = attribute_type(attribute);
result += `in ${type} ${name};\n`;
if (name.startsWith("lineindex")) {
result += `in int ${name};\n`;
} else {
const type = attribute_type(attribute);
result += `in ${type} ${name};\n`;
}
}
return result;
}
Expand Down Expand Up @@ -21337,7 +21341,7 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
flat out vec2 f_extrusion; // invalid / not needed
flat out float f_linewidth;
flat out vec4 f_pattern_overwrite; // invalid / not needed
flat out uint f_instance_id;
flat out int f_instance_id;
flat out ${color} f_color1;
flat out ${color} f_color2;
flat out float f_alpha_weight;
Expand Down Expand Up @@ -21473,7 +21477,8 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
// used to compute width sdf
f_linewidth = halfwidth;

f_instance_id = uint(2 * gl_InstanceID);
// TODO: this line crashes when lineindex_start is of type uint, why?
f_instance_id = lineindex_start; // NOTE: this is correct, not need to multiple by 2

// we restart patterns for each segment
f_cumulative_length = 0.0;
Expand Down Expand Up @@ -21532,7 +21537,7 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
flat out vec2 f_extrusion;
flat out float f_linewidth;
flat out vec4 f_pattern_overwrite;
flat out uint f_instance_id;
flat out int f_instance_id;
flat out ${color} f_color1;
flat out ${color} f_color2;
flat out float f_alpha_weight;
Expand Down Expand Up @@ -21920,7 +21925,8 @@ function lines_vertex_shader(uniforms, attributes, is_linesegments) {
// used to compute width sdf
f_linewidth = halfwidth;

f_instance_id = uint(gl_InstanceID);
// TODO: this line crashes when lineindex_start is of type uint, why?
f_instance_id = lineindex_start;

f_cumulative_length = lastlen_start;

Expand Down Expand Up @@ -22031,7 +22037,7 @@ function lines_fragment_shader(uniforms, attributes) {
flat in ${color} f_color1;
flat in ${color} f_color2;
flat in float f_alpha_weight;
flat in uint f_instance_id;
flat in int f_instance_id;
flat in float f_cumulative_length;
flat in ivec2 f_capmode;
flat in vec4 f_linepoints;
Expand Down Expand Up @@ -22278,7 +22284,7 @@ function lines_fragment_shader(uniforms, attributes) {

if (picking) {
if (color.a > 0.1) {
fragment_color = pack_int(object_id, f_instance_id);
fragment_color = pack_int(object_id, uint(f_instance_id));
}
return;
}
Expand Down

0 comments on commit 9286f60

Please sign in to comment.