Commit: 2da005902f434b6d4301516e280c899129384b95 Author: Miguel Pozo Date: Fri Jan 13 17:51:54 2023 +0100 Branches: tmp-worbench-rewrite2-optimizations https://developer.blender.org/rB2da005902f434b6d4301516e280c899129384b95
Draw: Cleanup the GLSL intersection code =================================================================== M source/blender/draw/intern/shaders/common_intersect_lib.glsl M source/blender/draw/intern/shaders/common_math_geom_lib.glsl =================================================================== diff --git a/source/blender/draw/intern/shaders/common_intersect_lib.glsl b/source/blender/draw/intern/shaders/common_intersect_lib.glsl index d1416e220a4..080771ca49b 100644 --- a/source/blender/draw/intern/shaders/common_intersect_lib.glsl +++ b/source/blender/draw/intern/shaders/common_intersect_lib.glsl @@ -124,311 +124,137 @@ IsectFrustum isect_data_setup(Frustum shape) /** \} */ /* ---------------------------------------------------------------------- */ -/** \name View Intersection functions. +/** \name Shape vs. Shape Intersection functions. * \{ */ -bool intersect_view(Pyramid pyramid) +bool intersect(IsectPyramid i_pyramid, IsectBox i_box) { - bool intersects = true; - - /* Do Pyramid vertices vs Frustum planes. */ - for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 5; ++v) { - float test = dot(drw_view_culling.planes[p], vec4(pyramid.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - - if (!intersects) { - return intersects; - } - - /* Now do Frustum vertices vs Pyramid planes. */ - IsectPyramid i_pyramid = isect_data_setup(pyramid); + /* Do Box vertices vs Pyramid planes. */ for (int p = 0; p < 5; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 8; ++v) { - float test = dot(i_pyramid.planes[p], vec4(drw_view_culling.corners[v].xyz, 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - return intersects; -} - -bool intersect_view(Box box) -{ - bool intersects = true; - - /* Do Box vertices vs Frustum planes. */ - for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 8; ++v) { - float test = dot(drw_view_culling.planes[p], vec4(box.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - - if (!intersects) { - return intersects; - } - - /* Now do Frustum vertices vs Box planes. */ - IsectBox i_box = isect_data_setup(box); - for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 8; ++v) { - float test = dot(i_box.planes[p], vec4(drw_view_culling.corners[v].xyz, 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - - return intersects; -} - -bool intersect_view(IsectBox i_box) -{ - bool intersects = true; - - /* Do Box vertices vs Frustum planes. */ - for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; + bool separating_axis = true; for (int v = 0; v < 8; ++v) { - float test = dot(drw_view_culling.planes[p], vec4(i_box.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + float signed_distance = point_plane_projection_dist(i_box.corners[v], i_pyramid.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; + if (separating_axis) { + return false; } } - if (!intersects) { - return intersects; - } - + /* Now do Pyramid vertices vs Box planes. */ for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 8; ++v) { - float test = dot(i_box.planes[p], vec4(drw_view_culling.corners[v].xyz, 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + bool separating_axis = true; + for (int v = 0; v < 5; ++v) { + float signed_distance = point_plane_projection_dist(i_pyramid.corners[v], i_box.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; + if (separating_axis) { + return false; } } - return intersects; -} - -bool intersect_view(Sphere sphere) -{ - bool intersects = true; - - for (int p = 0; p < 6 && intersects; ++p) { - float dist_to_plane = dot(drw_view_culling.planes[p], vec4(sphere.center, 1.0)); - if (dist_to_plane < -sphere.radius) { - intersects = false; - } - } - /* TODO reject false positive. */ - return intersects; + return true; } -/** \} */ - -/* ---------------------------------------------------------------------- */ -/** \name Shape vs. Shape Intersection functions. - * \{ */ - bool intersect(IsectPyramid i_pyramid, Box box) { - bool intersects = true; - - /* Do Box vertices vs Pyramid planes. */ - for (int p = 0; p < 5; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 8; ++v) { - float test = dot(i_pyramid.planes[p], vec4(box.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - - if (!intersects) { - return intersects; - } - - /* Now do Pyramid vertices vs Box planes. */ - IsectBox i_box = isect_data_setup(box); - for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; - for (int v = 0; v < 5; ++v) { - float test = dot(i_box.planes[p], vec4(i_pyramid.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; - break; - } - } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; - } - } - return intersects; + return intersect(i_pyramid, isect_data_setup(box)); } bool intersect(IsectFrustum i_frustum, Pyramid pyramid) { - bool intersects = true; - /* Do Pyramid vertices vs Frustum planes. */ for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; + bool separating_axis = true; for (int v = 0; v < 5; ++v) { - float test = dot(i_frustum.planes[p], vec4(pyramid.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + float signed_distance = point_plane_projection_dist(pyramid.corners[v], i_frustum.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; + if (separating_axis) { + return false; } } - if (!intersects) { - return intersects; - } - /* Now do Frustum vertices vs Pyramid planes. */ IsectPyramid i_pyramid = isect_data_setup(pyramid); for (int p = 0; p < 5; ++p) { - bool is_any_vertex_on_positive_side = false; + bool separating_axis = true; for (int v = 0; v < 8; ++v) { - float test = dot(i_pyramid.planes[p], vec4(i_frustum.corners[v].xyz, 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + float signed_distance = point_plane_projection_dist(i_frustum.corners[v].xyz, + i_pyramid.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; + if (separating_axis) { + return false; } } - return intersects; + + return true; } -bool intersect(IsectFrustum i_frustum, Box box) +bool intersect(IsectFrustum i_frustum, IsectBox i_box) { - bool intersects = true; - /* Do Box vertices vs Frustum planes. */ for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; + bool separating_axis = true; for (int v = 0; v < 8; ++v) { - float test = dot(i_frustum.planes[p], vec4(box.corners[v], 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + float signed_distance = point_plane_projection_dist(i_box.corners[v], i_frustum.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_vertex_on_negative_side) { - intersects = false; - break; + if (separating_axis) { + return false; } } - if (!intersects) { - return intersects; - } - /* Now do Frustum vertices vs Box planes. */ - IsectBox i_box = isect_data_setup(box); for (int p = 0; p < 6; ++p) { - bool is_any_vertex_on_positive_side = false; + bool separating_axis = true; for (int v = 0; v < 8; ++v) { - float test = dot(i_box.planes[p], vec4(i_frustum.corners[v].xyz, 1.0)); - if (test > 0.0) { - is_any_vertex_on_positive_side = true; + float signed_distance = point_plane_projection_dist(i_frustum.corners[v].xyz, + i_box.planes[p]); + if (signed_distance <= 0.0) { + separating_axis = false; break; } } - bool all_vertex_on_negative_side = !is_any_vertex_on_positive_side; - if (all_ve @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs