Cleanup: Overlay: Share line width macros

Pull Request: https://projects.blender.org/blender/blender/pulls/137953
This commit is contained in:
Clément Foucault
2025-04-17 23:24:40 +02:00
committed by Gitea
parent 77ed9359dc
commit b8f591c348
8 changed files with 27 additions and 65 deletions

View File

@@ -386,3 +386,20 @@ struct BoneStickData {
#endif
};
BLI_STATIC_ASSERT_ALIGN(BoneStickData, 16)
/**
* We want to know how much of a pixel is covered by a line.
* Here, we imagine the square pixel is a circle with the same area and try to find the
* intersection area. The overlap area is a circular segment.
* https://en.wikipedia.org/wiki/Circular_segment The formula for the area uses inverse trig
* function and is quite complex. Instead, we approximate it by using the smoothstep function and
* a 1.05f factor to the disc radius.
*
* For an alternate approach, see:
* https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-22-fast-prefiltered-lines
*/
#define M_1_SQRTPI 0.5641895835477563f /* `1/sqrt(pi)`. */
#define DISC_RADIUS (M_1_SQRTPI * 1.05f)
#define LINE_SMOOTH_START (0.5f + DISC_RADIUS)
#define LINE_SMOOTH_END (0.5f - DISC_RADIUS)
#define LINE_STEP(dist) smoothstep(LINE_SMOOTH_START, LINE_SMOOTH_END, dist)

View File

@@ -7,6 +7,8 @@
# include "gpu_glsl_cpp_stubs.hh"
# include "gpu_shader_fullscreen_info.hh"
# include "overlay_shader_shared.hh"
#endif
#include "overlay_common_info.hh"
@@ -18,6 +20,7 @@ SAMPLER(1, FLOAT_2D, color_tx)
SAMPLER(2, FLOAT_2D, line_tx)
PUSH_CONSTANT(bool, do_smooth_lines)
FRAGMENT_OUT(0, float4, frag_color)
TYPEDEF_SOURCE("overlay_shader_shared.hh")
FRAGMENT_SOURCE("overlay_antialiasing_frag.glsl")
ADDITIONAL_INFO(gpu_fullscreen)
ADDITIONAL_INFO(draw_globals)

View File

@@ -117,6 +117,7 @@ PUSH_CONSTANT(bool, use_vertex_selection)
VERTEX_OUT(overlay_edit_mesh_edge_geom_iface)
VERTEX_OUT(overlay_edit_mesh_edge_geom_flat_iface)
VERTEX_OUT(overlay_edit_mesh_edge_geom_noperspective_iface)
TYPEDEF_SOURCE("overlay_shader_shared.hh")
VERTEX_SOURCE("overlay_edit_mesh_edge_vert.glsl")
FRAGMENT_SOURCE("overlay_edit_mesh_frag.glsl")
ADDITIONAL_INFO(draw_view)

View File

@@ -6,20 +6,6 @@
FRAGMENT_SHADER_CREATE_INFO(overlay_antialiasing)
/**
* We want to know how much a pixel is covered by a line.
* We replace the square pixel with a circle of the same area and try to find the intersection
* area. The area we search is the circular segment. https://en.wikipedia.org/wiki/Circular_segment
* The formula for the area uses inverse trig function and is quite complex. Instead,
* we approximate it by using the smooth-step function and a 1.05 factor to the disc radius.
*/
#define M_1_SQRTPI 0.5641895835477563f /* `1/sqrt(pi)`. */
#define DISC_RADIUS (M_1_SQRTPI * 1.05f)
#define LINE_SMOOTH_START (0.5f - DISC_RADIUS)
#define LINE_SMOOTH_END (0.5f + DISC_RADIUS)
/**
* Returns coverage of a line onto a sample that is distance_to_line (in pixels) far from the line.
* line_kernel_size is the inner size of the line with 100% coverage.

View File

@@ -9,24 +9,10 @@ FRAGMENT_SHADER_CREATE_INFO(overlay_armature_shape_wire)
#include "gpu_shader_utildefines_lib.glsl"
#include "select_lib.glsl"
/**
* We want to know how much a pixel is covered by a line.
* We replace the square pixel with a circle of the same area and try to find the intersection
* area. The area we search is the circular segment. https://en.wikipedia.org/wiki/Circular_segment
* The formula for the area uses inverse trig function and is quite complex. Instead,
* we approximate it by using the smooth-step function and a 1.05 factor to the disc radius.
*/
#define M_1_SQRTPI 0.5641895835477563f /* `1/sqrt(pi)`. */
#define DISC_RADIUS (M_1_SQRTPI * 1.05f)
#define GRID_LINE_SMOOTH_START (0.5f - DISC_RADIUS)
#define GRID_LINE_SMOOTH_END (0.5f + DISC_RADIUS)
float edge_step(float dist)
{
if (do_smooth_wire) {
return smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist);
return smoothstep(LINE_SMOOTH_START, LINE_SMOOTH_END, dist);
}
else {
return step(0.5f, dist);

View File

@@ -6,20 +6,6 @@
FRAGMENT_SHADER_CREATE_INFO(overlay_edit_mesh_edge)
/**
* We want to know how much a pixel is covered by a line.
* We replace the square pixel with a circle of the same area and try to find the intersection
* area. The area we search is the circular segment. https://en.wikipedia.org/wiki/Circular_segment
* The formula for the area uses inverse trig function and is quite complex. Instead,
* we approximate it by using the smooth-step function and a 1.05 factor to the disc radius.
*/
#define M_1_SQRTPI 0.5641895835477563f /* `1/sqrt(pi)`. */
#define DISC_RADIUS (M_1_SQRTPI * 1.05f)
#define GRID_LINE_SMOOTH_START (0.5f - DISC_RADIUS)
#define GRID_LINE_SMOOTH_END (0.5f + DISC_RADIUS)
bool test_occlusion()
{
return gl_FragCoord.z > texelFetch(depth_tx, int2(gl_FragCoord.xy), 0).r;
@@ -28,7 +14,7 @@ bool test_occlusion()
float edge_step(float dist)
{
if (do_smooth_wire) {
return smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist);
return smoothstep(LINE_SMOOTH_START, LINE_SMOOTH_END, dist);
}
else {
return step(0.5f, dist);

View File

@@ -53,8 +53,8 @@ void main()
float mix_w_outer;
if (do_smooth_wire) {
mix_w = smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist);
mix_w_outer = smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist_outer);
mix_w = smoothstep(LINE_SMOOTH_START, LINE_SMOOTH_END, dist);
mix_w_outer = smoothstep(LINE_SMOOTH_START, LINE_SMOOTH_END, dist_outer);
}
else {
mix_w = step(0.5f, dist);

View File

@@ -13,23 +13,6 @@ FRAGMENT_SHADER_CREATE_INFO(overlay_grid_next)
* interpolation.
*/
/**
* We want to know how much of a pixel is covered by a line.
* Here, we imagine the square pixel is a circle with the same area and try to find the
* intersection area. The overlap area is a circular segment.
* https://en.wikipedia.org/wiki/Circular_segment The formula for the area uses inverse trig
* function and is quite complex. Instead, we approximate it by using the smoothstep function and
* a 1.05f factor to the disc radius.
*
* For an alternate approach, see:
* https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-22-fast-prefiltered-lines
*/
#define M_1_SQRTPI 0.5641895835477563f /* `1/sqrt(pi)`. */
#define DISC_RADIUS (M_1_SQRTPI * 1.05f)
#define GRID_LINE_SMOOTH_START (0.5f + DISC_RADIUS)
#define GRID_LINE_SMOOTH_END (0.5f - DISC_RADIUS)
#define GRID_LINE_STEP(dist) smoothstep(GRID_LINE_SMOOTH_START, GRID_LINE_SMOOTH_END, dist)
#include "draw_view_lib.glsl"
#include "gpu_shader_utildefines_lib.glsl"
@@ -43,7 +26,7 @@ float get_grid(float2 co, float2 fwidthCos, float2 grid_scale)
grid_domain /= fwidthCos;
/* Collapse waves. */
float line_dist = min(grid_domain.x, grid_domain.y);
return GRID_LINE_STEP(line_dist - grid_buf.line_size);
return LINE_STEP(line_dist - grid_buf.line_size);
}
float3 get_axes(float3 co, float3 fwidthCos, float line_size)
@@ -52,7 +35,7 @@ float3 get_axes(float3 co, float3 fwidthCos, float line_size)
/* Modulate by the absolute rate of change of the coordinates
* (make line have the same width under perspective). */
axes_domain /= fwidthCos;
return GRID_LINE_STEP(axes_domain - (line_size + grid_buf.line_size));
return LINE_STEP(axes_domain - (line_size + grid_buf.line_size));
}
#define linearstep(p0, p1, v) (clamp(((v) - (p0)) / abs((p1) - (p0)), 0.0f, 1.0f))