Skip to content

Commit ff14615

Browse files
committed
optimize downsample shader. readd noise for blur. fix max blur pass panic.
1 parent 83e8c24 commit ff14615

5 files changed

Lines changed: 38 additions & 32 deletions

File tree

src/render_helpers/blur/element.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn draw_true_blur(
280280
],
281281
),
282282
// Uniform::new("alpha", alpha),
283-
// Uniform::new("noise", config.noise.0 as f32),
283+
Uniform::new("noise", config.noise.0 as f32),
284284
Uniform::new("corner_radius", corner_radius),
285285
// Uniform::new(
286286
// "output_size",
@@ -362,7 +362,7 @@ impl RenderElement<GlesRenderer> for BlurRenderElement {
362362
],
363363
),
364364
Uniform::new("corner_radius", *corner_radius),
365-
// Uniform::new("noise", *noise),
365+
Uniform::new("noise", *noise),
366366
// Uniform::new("alpha", self.alpha()),
367367
Uniform::new("ignore_alpha", 0.),
368368
],

src/render_helpers/blur/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ impl EffectsFramebuffers {
130130
size.to_logical(1).to_buffer(1, Transform::Normal),
131131
)
132132
}
133-
133+
134134
let mut sample_effects: Vec<GlesTexture> = Vec::new();
135135
let mut sample_fbos: Vec<u32> = Vec::new();
136-
for i in 0..8 {
136+
for i in 0..9 {
137137
let size = if i == 0 {
138138
texture_size
139139
} else {
@@ -210,7 +210,7 @@ impl EffectsFramebuffers {
210210

211211
let mut sample_effects: Vec<GlesTexture> = Vec::new();
212212
let mut sample_fbos: Vec<u32> = Vec::new();
213-
for i in 0..8 {
213+
for i in 0..9 {
214214
let size = if i == 0 {
215215
texture_size
216216
} else {
@@ -441,10 +441,10 @@ pub(super) unsafe fn get_main_buffer_blur(
441441

442442
let dst_expanded = {
443443
let mut dst = dst;
444-
// let size =
445-
// (2f32.powi(blur_config.passes as i32 + 1) * blur_config.radius.0 as f32).ceil() as i32;
444+
let p = blur_config.passes.saturating_sub(1);
445+
let size = (1 << p) * blur_config.radius.0 as i32;
446446
// let size= blur_config.radius.0 as i32;
447-
let size = blur_config.radius.0 as i32 * blur_config.passes as i32;
447+
// let size = blur_config.radius.0 as i32 * blur_config.passes as i32;
448448
dst.loc -= Point::from((size, size));
449449
dst.size += Size::from((size, size)).upscale(2);
450450
dst
@@ -909,10 +909,7 @@ unsafe fn render_blur_pass_with_gl(
909909
if i == 0 { 0.0 } else { config.radius.0 as f32 },
910910
);
911911
gl.Uniform2f(program.uniform_half_pixel, half_pixel[0], half_pixel[1]);
912-
gl.Uniform1f(
913-
program.uniform_scale,
914-
scale as f32,
915-
);
912+
gl.Uniform1f(program.uniform_scale, scale as f32);
916913

917914
gl.EnableVertexAttribArray(program.attrib_vert as u32);
918915
gl.BindBuffer(ffi::ARRAY_BUFFER, vbos[0]);

src/render_helpers/shaders/blur_down.frag

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ void main() {
2121
gl_FragColor = texture2D(tex, uv);
2222
return;
2323
}
24-
vec2 offset = half_pixel * radius;
25-
vec4 sum = texture2D(tex, uv) * 4.0
26-
+ texture2D(tex, uv + vec2(offset.x, 0.0))
27-
+ texture2D(tex, uv - vec2(offset.x, 0.0))
28-
+ texture2D(tex, uv + vec2(0.0, offset.y))
29-
+ texture2D(tex, uv - vec2(0.0, offset.y));
24+
// Kawase风格:对角线采样
25+
vec2 offset = half_pixel * (radius * 0.707 + 0.5); // 调整到对角线
3026

31-
gl_FragColor = sum * 0.125;
27+
vec4 sum = texture2D(tex, uv + vec2(-offset.x, -offset.y))
28+
+ texture2D(tex, uv + vec2( offset.x, -offset.y))
29+
+ texture2D(tex, uv + vec2(-offset.x, offset.y))
30+
+ texture2D(tex, uv + vec2( offset.x, offset.y));
31+
32+
gl_FragColor = sum * 0.25;
3233
// gl_FragColor = texture2D(tex, uv);
3334
}

src/render_helpers/shaders/blur_finish.frag

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ varying vec2 v_coords;
2929
uniform vec4 geo;
3030
// uniform vec2 output_size;
3131
uniform float corner_radius;
32-
// uniform float noise;
32+
uniform float noise;
3333
uniform float ignore_alpha;
3434

3535
// float rounding_alpha(vec2 coords, vec2 size, float radius) {
@@ -58,13 +58,22 @@ float fast_rounding_alpha(vec2 coords, vec2 size, float radius) {
5858
return 1.0 - smoothstep(radius - 0.5, radius + 0.5, sdf);
5959
}
6060

61-
// // Noise function copied from hyprland.
62-
// // I like the effect it gave, can be tweaked further
63-
// float hash(vec2 p) {
64-
// vec3 p3 = fract(vec3(p.xyx) * 727.727); // wysi :wink: :wink:
65-
// p3 += dot(p3, p3.xyz + 33.33);
66-
// return fract((p3.x + p3.y) * p3.z);
67-
// }
61+
// 优化3:蓝噪声(Blue Noise)近似
62+
float blue_noise(vec2 uv) {
63+
// 使用旋转的三角波
64+
float x = uv.x * 1.61803398875; // 黄金比例
65+
float y = uv.y * 1.61803398875;
66+
67+
float noise = sin(x * 12.9898 + y * 78.233) * 43758.5453;
68+
noise = fract(noise);
69+
70+
// 添加高频分量
71+
noise += sin(x * 26.9898 + y * 92.233) * 43758.5453;
72+
noise = fract(noise) * 0.5 + 0.25; // 限制在[0.25, 0.75]
73+
74+
return noise - 0.5; // 居中在0
75+
}
76+
6877

6978
void main() {
7079
vec4 color = texture2D(tex, v_coords);
@@ -86,10 +95,9 @@ void main() {
8695
vec2 size = geo.zw;
8796
vec2 loc = gl_FragCoord.xy - geo.xy;
8897

89-
// // 噪声(只在alpha测试通过时添加)
90-
// float noiseHash = hash(loc / size);
91-
// float noise_contrib = (noiseHash - 0.5) * noise;
92-
// color.rgb += noise_contrib * alphaMask; // 用alphaMask控制
98+
// 噪声(只在alpha测试通过时添加)
99+
float noise_contrib = blue_noise(loc / size) * noise * 0.08;
100+
color.rgb = mix(color.rgb, color.rgb + noise_contrib, alphaMask);
93101

94102
float round_alpha = fast_rounding_alpha(loc, size, corner_radius);
95103

src/render_helpers/shaders/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Shaders {
111111
// UniformName::new("output_size", UniformType::_2f),
112112
UniformName::new("corner_radius", UniformType::_1f),
113113
// UniformName::new("alpha", UniformType::_1f),
114-
// UniformName::new("noise", UniformType::_1f),
114+
UniformName::new("noise", UniformType::_1f),
115115
UniformName::new("geo", UniformType::_4f),
116116
UniformName::new("ignore_alpha", UniformType::_1f),
117117
UniformName::new("alpha_tex", UniformType::_1i),

0 commit comments

Comments
 (0)