1
//! FXAA (Fast Approximate Anti-Aliasing) shader implementation.
2
//!
3
//! Post-processing AA that detects edges via luminance and selectively blurs them.
4
//! Faster than supersampling and works without hardware MSAA support.
5
//!
6
//! Shader compilation: `GlContextPtrInner.fxaa_shader` (see `core/src/gl.rs`).
7
//! FXAA pass: `apply_fxaa` / `apply_fxaa_with_config` (see `layout/src/xml/svg.rs`).
8
//!
9
//! Presets: `FxaaConfig::enabled()`, `::high_quality()`, `::balanced()`, `::performance()`
10

            
11
/// FXAA shader configuration
12
#[derive(Debug, Clone, Copy)]
13
pub struct FxaaConfig {
14
    /// Enable/disable FXAA
15
    pub enabled: bool,
16
    /// Edge detection threshold (0.063 - 0.333, default: 0.125)
17
    /// Lower = more edges detected = more AA but potential blur
18
    pub edge_threshold: f32,
19
    /// Minimum edge threshold (0.0312 - 0.0833, default: 0.0312)
20
    /// Prevents AA on very low contrast edges
21
    pub edge_threshold_min: f32,
22
}
23

            
24
impl Default for FxaaConfig {
25
282
    fn default() -> Self {
26
282
        Self {
27
282
            enabled: false, // Disabled by default for performance
28
282
            edge_threshold: 0.125,
29
282
            edge_threshold_min: 0.0312,
30
282
        }
31
282
    }
32
}
33

            
34
impl FxaaConfig {
35
    /// Create config with FXAA enabled and default quality settings
36
    #[must_use]
37
141
    pub fn enabled() -> Self {
38
141
        Self {
39
141
            enabled: true,
40
141
            ..Default::default()
41
141
        }
42
141
    }
43

            
44
    /// High quality preset - more aggressive edge detection
45
    #[must_use]
46
142
    pub const fn high_quality() -> Self {
47
142
        Self {
48
142
            enabled: true,
49
142
            edge_threshold: 0.063,
50
142
            edge_threshold_min: 0.0312,
51
142
        }
52
142
    }
53

            
54
    /// Balanced preset - default settings
55
    #[must_use]
56
142
    pub const fn balanced() -> Self {
57
142
        Self {
58
142
            enabled: true,
59
142
            edge_threshold: 0.125,
60
142
            edge_threshold_min: 0.0312,
61
142
        }
62
142
    }
63

            
64
    /// Performance preset - less aggressive, faster
65
    #[must_use]
66
141
    pub const fn performance() -> Self {
67
141
        Self {
68
141
            enabled: true,
69
141
            edge_threshold: 0.25,
70
141
            edge_threshold_min: 0.0625,
71
141
        }
72
141
    }
73
}
74

            
75
/// FXAA vertex shader - simple fullscreen quad pass-through
76
pub static FXAA_VERTEX_SHADER: &[u8] = b"#version 150
77

            
78
#if __VERSION__ != 100
79
    #define varying out
80
    #define attribute in
81
#endif
82

            
83
attribute vec2 vAttrXY;
84
varying vec2 vTexCoord;
85

            
86
void main() {
87
    vTexCoord = vAttrXY * 0.5 + 0.5; // Convert from [-1,1] to [0,1]
88
    gl_Position = vec4(vAttrXY, 0.0, 1.0);
89
}
90
";
91

            
92
/// FXAA fragment shader - implements edge-based anti-aliasing
93
pub static FXAA_FRAGMENT_SHADER: &[u8] = b"#version 150
94

            
95
precision highp float;
96

            
97
#if __VERSION__ == 100
98
    #define oFragColor gl_FragColor
99
    #define texture texture2D
100
#else
101
    out vec4 oFragColor;
102
#endif
103

            
104
#if __VERSION__ != 100
105
    #define varying in
106
#endif
107

            
108
uniform sampler2D uTexture;
109
uniform vec2 uTexelSize; // 1.0 / texture dimensions
110
uniform float uEdgeThreshold;
111
uniform float uEdgeThresholdMin;
112

            
113
varying vec2 vTexCoord;
114

            
115
// Luminance conversion (Rec. 709)
116
float luminance(vec3 color) {
117
    return dot(color, vec3(0.2126, 0.7152, 0.0722));
118
}
119

            
120
void main() {
121
    // Sample center and 4-neighborhood
122
    vec3 colorCenter = texture(uTexture, vTexCoord).rgb;
123
    vec3 colorN = texture(uTexture, vTexCoord + vec2(0.0, -uTexelSize.y)).rgb;
124
    vec3 colorS = texture(uTexture, vTexCoord + vec2(0.0, uTexelSize.y)).rgb;
125
    vec3 colorE = texture(uTexture, vTexCoord + vec2(uTexelSize.x, 0.0)).rgb;
126
    vec3 colorW = texture(uTexture, vTexCoord + vec2(-uTexelSize.x, 0.0)).rgb;
127

            
128
    // Calculate luminance
129
    float lumCenter = luminance(colorCenter);
130
    float lumN = luminance(colorN);
131
    float lumS = luminance(colorS);
132
    float lumE = luminance(colorE);
133
    float lumW = luminance(colorW);
134

            
135
    // Find min/max luminance in neighborhood
136
    float lumMin = min(lumCenter, min(min(lumN, lumS), min(lumE, lumW)));
137
    float lumMax = max(lumCenter, max(max(lumN, lumS), max(lumE, lumW)));
138
    float lumRange = lumMax - lumMin;
139

            
140
    // Early exit if no edge detected
141
    if (lumRange < max(uEdgeThresholdMin, lumMax * uEdgeThreshold)) {
142
        oFragColor = vec4(colorCenter, 1.0);
143
        return;
144
    }
145

            
146
    // Calculate edge direction
147
    float lumNS = lumN + lumS;
148
    float lumEW = lumE + lumW;
149

            
150
    vec2 dir;
151
    dir.x = lumNS - lumEW;
152
    dir.y = lumN - lumS;
153

            
154
    // Normalize edge direction
155
    float dirReduce = max((lumN + lumS + lumE + lumW) * 0.25 * 0.25, 0.0078125);
156
    float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
157
    dir = min(vec2(8.0), max(vec2(-8.0), dir * rcpDirMin)) * uTexelSize;
158

            
159
    // Sample along edge direction
160
    vec3 color1 = 0.5 * (
161
        texture(uTexture, vTexCoord + dir * (1.0/3.0 - 0.5)).rgb +
162
        texture(uTexture, vTexCoord + dir * (2.0/3.0 - 0.5)).rgb
163
    );
164

            
165
    vec3 color2 = color1 * 0.5 + 0.25 * (
166
        texture(uTexture, vTexCoord + dir * -0.5).rgb +
167
        texture(uTexture, vTexCoord + dir * 0.5).rgb
168
    );
169

            
170
    float lum2 = luminance(color2);
171

            
172
    // Choose appropriate sample based on luminance range
173
    if (lum2 < lumMin || lum2 > lumMax) {
174
        oFragColor = vec4(color1, 1.0);
175
    } else {
176
        oFragColor = vec4(color2, 1.0);
177
    }
178
}
179
";
180

            
181
#[cfg(test)]
182
#[path = "gl_fxaa_test.rs"]
183
mod gl_fxaa_test;