GAL: Tune SMAA to have a conservative and aggressive mode

Fixes https://gitlab.com/kicad/code/kicad/-/issues/8522
Conservative makes SMAA usable in Eeschema through weaker AA.
Aggressive reduces some artifacts compared to the High preset.
Restores the unused Ultra preset in the shader source to its original state.
This commit is contained in:
david-beinder 2021-06-04 02:14:30 +02:00 committed by Seth Hillbrand
parent 8da77422d0
commit 2b4564571c
8 changed files with 59 additions and 24 deletions

View File

@ -74,7 +74,7 @@ PANEL_COMMON_SETTINGS_BASE::PANEL_COMMON_SETTINGS_BASE( wxWindow* parent, wxWind
antialiasingLabel->Wrap( -1 );
gbSizer1->Add( antialiasingLabel, wxGBPosition( 4, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
wxString m_antialiasingChoices[] = { _("No Antialiasing"), _("Subpixel Antialiasing (High Quality)"), _("Subpixel Antialiasing (Ultra Quality)"), _("Supersampling (2x)"), _("Supersampling (4x)") };
wxString m_antialiasingChoices[] = { _("No Antialiasing"), _("Smoothing (1x, Conservative)"), _("Smoothing (1x, Aggressive)"), _("Supersampling (2x)"), _("Supersampling (4x)") };
int m_antialiasingNChoices = sizeof( m_antialiasingChoices ) / sizeof( wxString );
m_antialiasing = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_antialiasingNChoices, m_antialiasingChoices, 0 );
m_antialiasing->SetSelection( 0 );

View File

@ -694,7 +694,7 @@
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="choices">&quot;No Antialiasing&quot; &quot;Subpixel Antialiasing (High Quality)&quot; &quot;Subpixel Antialiasing (Ultra Quality)&quot; &quot;Supersampling (2x)&quot; &quot;Supersampling (4x)&quot;</property>
<property name="choices">&quot;No Antialiasing&quot; &quot;Smoothing (1x, Conservative)&quot; &quot;Smoothing (1x, Aggressive)&quot; &quot;Supersampling (2x)&quot; &quot;Supersampling (4x)&quot;</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>

View File

@ -288,15 +288,39 @@ void ANTIALIASING_SMAA::loadShaders()
GL_UNSIGNED_BYTE, searchTexBytes );
checkGlError( "loading smaa search tex", __FILE__, __LINE__ );
// Quality settings:
// THRESHOLD: intended to exclude spurious edges in photorealistic game graphics
// but in a high-contrast CAD application, all edges are intentional
// should be set fairly low, so user color choices do not affect antialiasing
// MAX_SEARCH_STEPS: steps of 2px, searched in H/V direction to discover true angle of edges
// improves AA for lines close H/V but creates fuzzyness at junctions
// MAX_SEARCH_STEPS_DIAG: steps of 2px, searched in diagonal direction
// improves lines close to 45deg but turns small circles into octagons
// CORNER_ROUNDING: SMAA can distinguish actual corners from aliasing jaggies,
// we want to preserve those as much as possible
// Edge Detection: In Eeschema, when a single pixel line changes color, edge detection using
// color is too aggressive and leads to a white spot at the transition point
std::string quality_string;
const char* edge_detect_shader;
if( quality == SMAA_QUALITY::HIGH )
if( quality == SMAA_QUALITY::CONSERVATIVE )
{
quality_string = "#define SMAA_PRESET_HIGH\n";
// trades imperfect AA of shallow angles for a near artifact-free reproduction of fine features
// jaggies are smoothed over max 5px (original step + 2px in both directions)
quality_string = "#define SMAA_THRESHOLD 0.05\n"
"#define SMAA_MAX_SEARCH_STEPS 1\n"
"#define SMAA_MAX_SEARCH_STEPS_DIAG 2\n"
"#define SMAA_CORNER_ROUNDING 0\n";
edge_detect_shader = BUILTIN_SHADERS::smaa_pass_1_fragment_shader_luma;
}
else
{
quality_string = "#define SMAA_PRESET_ULTRA\n";
// jaggies are smoothed over max 17px (original step + 8px in both directions)
quality_string = "#define SMAA_THRESHOLD 0.05\n"
"#define SMAA_MAX_SEARCH_STEPS 4\n"
"#define SMAA_MAX_SEARCH_STEPS_DIAG 2\n"
"#define SMAA_CORNER_ROUNDING 0\n";
edge_detect_shader = BUILTIN_SHADERS::smaa_pass_1_fragment_shader_color;
}
@ -329,8 +353,7 @@ uniform vec4 SMAA_RT_METRICS;
pass_1_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_VERTEX, vert_preamble, quality_string,
smaa_source, BUILTIN_SHADERS::smaa_pass_1_vertex_shader );
pass_1_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_FRAGMENT, frag_preamble,
quality_string, smaa_source,
BUILTIN_SHADERS::smaa_pass_1_fragment_shader );
quality_string, smaa_source, edge_detect_shader );
pass_1_shader->Link();
checkGlError( "linking pass 1 shader", __FILE__, __LINE__ );

View File

@ -102,9 +102,10 @@ namespace KIGFX {
std::unique_ptr<SHADER> x4_shader;
};
enum class SMAA_QUALITY {
HIGH,
ULTRA
enum class SMAA_QUALITY
{
CONSERVATIVE,
AGGRESSIVE
};
class ANTIALIASING_SMAA : public OPENGL_PRESENTOR

View File

@ -755,12 +755,9 @@ const char smaa_base_shader_p1[] = R"SHADER_SOURCE(
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
#define SMAA_CORNER_ROUNDING 25
#elif defined(SMAA_PRESET_ULTRA)
#define SMAA_THRESHOLD 0.005
//0.05
#define SMAA_MAX_SEARCH_STEPS 64
//32
#define SMAA_MAX_SEARCH_STEPS_DIAG 32
//16
#define SMAA_THRESHOLD 0.05
#define SMAA_MAX_SEARCH_STEPS 32
#define SMAA_MAX_SEARCH_STEPS_DIAG 16
#define SMAA_CORNER_ROUNDING 25
#endif
@ -1833,7 +1830,7 @@ void main()
)SHADER_SOURCE";
const char smaa_pass_1_fragment_shader[] = R"SHADER_SOURCE(
const char smaa_pass_1_fragment_shader_luma[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec4 offset[3];
@ -1846,6 +1843,19 @@ void main()
)SHADER_SOURCE";
const char smaa_pass_1_fragment_shader_color[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec4 offset[3];
uniform sampler2D colorTex;
void main()
{
gl_FragColor.xy = SMAAColorEdgeDetectionPS(texcoord, offset, colorTex).xy;
}
)SHADER_SOURCE";
const char smaa_pass_2_vertex_shader[] = R"SHADER_SOURCE(
varying vec4 offset[3];

View File

@ -40,7 +40,8 @@ namespace KIGFX {
extern const char smaa_base_shader_p4[];
extern const char smaa_pass_1_vertex_shader[];
extern const char smaa_pass_1_fragment_shader[];
extern const char smaa_pass_1_fragment_shader_luma[];
extern const char smaa_pass_1_fragment_shader_color[];
extern const char smaa_pass_2_vertex_shader[];
extern const char smaa_pass_2_fragment_shader[];
extern const char smaa_pass_3_vertex_shader[];

View File

@ -96,11 +96,11 @@ void OPENGL_COMPOSITOR::Initialize()
case OPENGL_ANTIALIASING_MODE::NONE:
m_antialiasing = std::make_unique<ANTIALIASING_NONE>( this );
break;
case OPENGL_ANTIALIASING_MODE::SUBSAMPLE_HIGH:
m_antialiasing = std::make_unique<ANTIALIASING_SMAA>( this, SMAA_QUALITY::HIGH );
case OPENGL_ANTIALIASING_MODE::SUBSAMPLE_CONSERVATIVE:
m_antialiasing = std::make_unique<ANTIALIASING_SMAA>( this, SMAA_QUALITY::CONSERVATIVE );
break;
case OPENGL_ANTIALIASING_MODE::SUBSAMPLE_ULTRA:
m_antialiasing = std::make_unique<ANTIALIASING_SMAA>( this, SMAA_QUALITY::ULTRA );
case OPENGL_ANTIALIASING_MODE::SUBSAMPLE_AGGRESSIVE:
m_antialiasing = std::make_unique<ANTIALIASING_SMAA>( this, SMAA_QUALITY::AGGRESSIVE );
break;
case OPENGL_ANTIALIASING_MODE::SUPERSAMPLING_X2:
m_antialiasing =

View File

@ -48,8 +48,8 @@ namespace KIGFX
enum class OPENGL_ANTIALIASING_MODE
{
NONE,
SUBSAMPLE_HIGH,
SUBSAMPLE_ULTRA,
SUBSAMPLE_CONSERVATIVE,
SUBSAMPLE_AGGRESSIVE,
SUPERSAMPLING_X2,
SUPERSAMPLING_X4,
};