From 6953710c74fccbde4c7d928c3a577d11e3501610 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 09:32:16 +0200 Subject: [PATCH] Blur the panorama behind the title menus' screens, as vanilla does (#282) Screen.extractBlurredBackground: every classic screen reached from the title (world selection and creation, multiplayer, options) blurs the panorama with vanilla's minecraft:blur post chain, three horizontal and vertical box blur passes of menuBackgroundBlurriness pixels, then draws menu_background over it instead of a flat dark fill. The title screen itself stays sharp. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 13 ++ crates/horizon-client/src/gfx.rs | 23 +++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/menu_blur.rs | 249 +++++++++++++++++++++++ crates/horizon-client/src/menu_blur.wgsl | 32 +++ crates/horizon-client/src/multiplayer.rs | 2 +- crates/horizon-client/src/title.rs | 12 +- 7 files changed, 325 insertions(+), 7 deletions(-) create mode 100644 crates/horizon-client/src/menu_blur.rs create mode 100644 crates/horizon-client/src/menu_blur.wgsl diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 45068c9d6..fd4557137 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8226,6 +8226,7 @@ impl Game<'_> { } else { None }, + menu_blur: self.menu_blur(), }; let result = match self.renderer.as_mut() { Some(renderer) => horizon_client::spans::time("renderer", || renderer.render(&frame)), @@ -8235,6 +8236,18 @@ impl Game<'_> { self.entity_vertices = entities; result } + /// `Screen.extractBlurredBackground` over the panorama: every classic + /// screen of the title menus but the title itself (`TitleScreen` + /// doesn't blur), when `menuBackgroundBlurriness` is at least 1. + fn menu_blur(&self) -> Option { + let blurred = !horizon_client::ui_theme::horizon() + && self.title.as_ref().is_some_and(|t| { + self.screen == Screen::Options + || !matches!(t.mode, horizon_client::title::Mode::Main) + }); + let radius = self.settings.menu_blur as f32; + (blurred && radius >= 1.0).then_some(radius) + } /// `LevelExtractor` / `LevelRenderer`: the world border wall this frame. fn border_draw( &mut self, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index f68ba554b..b419cbcd1 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -169,6 +169,9 @@ pub struct Frame<'a> { pub block_flicker: f32, /// Blur and grade the frame under a Horizon-style menu (`post`). pub post: Option, + /// `Screen.extractBlurredBackground`: blur the frame by this radius + /// (`menuBackgroundBlurriness`) before the screen is drawn over it. + pub menu_blur: Option, } /// One frame of the world border wall (`WorldBorderRenderer.render`). #[derive(Clone, Debug)] @@ -287,6 +290,8 @@ pub struct Renderer { pub hud: HudRenderer, /// The menus' backdrop blur, made on first use. post: Option, + /// Vanilla's menu background blur, made on first use. + menu_blur: Option, pub info: wgpu::AdapterInfo, pub suspended: bool, pub drawn_sections: usize, @@ -1432,6 +1437,7 @@ impl Renderer { world_post, hud, post: None, + menu_blur: None, info, suspended: size.width == 0 || size.height == 0, drawn_sections: 0, @@ -2397,6 +2403,23 @@ impl Renderer { pass.draw_indexed(0..6, (i * 4) as i32, 0..1); } } + if let Some(radius) = frame.menu_blur + && self.config.usage.contains(wgpu::TextureUsages::COPY_SRC) + { + let format = self.config.format; + let device = &self.device; + self.menu_blur + .get_or_insert_with(|| crate::menu_blur::MenuBlur::new(device, format)) + .apply( + &self.device, + &self.queue, + &mut encoder, + &target_texture, + &view_target, + [self.config.width, self.config.height], + radius, + ); + } if let Some(grade) = &frame.post && self.config.usage.contains(wgpu::TextureUsages::COPY_SRC) { diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index f8dd9d503..8d5d91f87 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -92,6 +92,7 @@ pub mod lang; pub mod light; pub mod logging; pub mod menu; +pub mod menu_blur; pub mod merchant; pub mod mesher; pub mod minimap; diff --git a/crates/horizon-client/src/menu_blur.rs b/crates/horizon-client/src/menu_blur.rs new file mode 100644 index 000000000..65251f678 --- /dev/null +++ b/crates/horizon-client/src/menu_blur.rs @@ -0,0 +1,249 @@ +//! Vanilla's menu background blur (`GameRenderer.processBlurEffect`, the +//! `minecraft:blur` post chain): three rounds of a horizontal then a vertical +//! `post/box_blur` pass over the frame drawn so far, `menuBackgroundBlurriness` +//! framebuffer pixels wide, before the screen is drawn over it. Works in the +//! frame's own (gamma) values, as vanilla's main target does, and needs a +//! swapchain that can be copied from. + +/// `blur.json`: the passes' directions, ping-ponging between the copied +/// frame and a swap texture; the last one writes the frame. +const PASSES: [[f32; 2]; 6] = [ + [1.0, 0.0], + [0.0, 1.0], + [1.0, 0.0], + [0.0, 1.0], + [1.0, 0.0], + [0.0, 1.0], +]; + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Uniform { + size_dir: [f32; 4], + radius: [f32; 4], +} + +pub struct MenuBlur { + layout: wgpu::BindGroupLayout, + pipeline: wgpu::RenderPipeline, + sampler: wgpu::Sampler, + uniforms: Vec, + /// The copied frame (`minecraft:main`) and `swap`. + targets: Vec<(wgpu::Texture, wgpu::TextureView)>, + size: [u32; 2], + format: wgpu::TextureFormat, +} +impl MenuBlur { + /// `format` is the swapchain's; the blur works in its non-sRGB view. + pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self { + let format = format.remove_srgb_suffix(); + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Menu blur"), + source: wgpu::ShaderSource::Wgsl(include_str!("menu_blur.wgsl").into()), + }); + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Menu blur bindings"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 2, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + ], + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Menu blur"), + bind_group_layouts: &[Some(&layout)], + ..Default::default() + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Menu blur pass"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: Some("vs_main"), + buffers: &[], + compilation_options: Default::default(), + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: Some("fs_main"), + targets: &[Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + compilation_options: Default::default(), + }), + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + multiview_mask: None, + cache: None, + }); + // The post chain's "bilinear": true inputs, clamped like render targets. + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("Menu blur"), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + ..Default::default() + }); + let uniforms = PASSES + .iter() + .map(|_| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Menu blur pass"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) + }) + .collect(); + Self { + layout, + pipeline, + sampler, + uniforms, + targets: Vec::new(), + size: [0, 0], + format, + } + } + fn ensure(&mut self, device: &wgpu::Device, size: [u32; 2]) { + if self.size == size && !self.targets.is_empty() { + return; + } + self.size = size; + self.targets = (0..2) + .map(|_| { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Menu blur target"), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: self.format, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + (texture, view) + }) + .collect(); + } + /// Blur `frame` (the swapchain texture) in place through `target`, its + /// non-sRGB view, by `radius` pixels (`menuBackgroundBlurriness`). + #[allow(clippy::too_many_arguments)] + pub fn apply( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + frame: &wgpu::Texture, + target: &wgpu::TextureView, + size: [u32; 2], + radius: f32, + ) { + self.ensure(device, size); + encoder.copy_texture_to_texture( + frame.as_image_copy(), + self.targets[0].0.as_image_copy(), + wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + ); + for (i, dir) in PASSES.iter().enumerate() { + let data = Uniform { + size_dir: [size[0] as f32, size[1] as f32, dir[0], dir[1]], + radius: [radius, 0.0, 0.0, 0.0], + }; + queue.write_buffer(&self.uniforms[i], 0, bytemuck::bytes_of(&data)); + let source = &self.targets[i % 2].1; + let output = if i + 1 == PASSES.len() { + target + } else { + &self.targets[(i + 1) % 2].1 + }; + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Menu blur pass"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(source), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::Sampler(&self.sampler), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: self.uniforms[i].as_entire_binding(), + }, + ], + }); + let attachments = [Some(wgpu::RenderPassColorAttachment { + view: output, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + })]; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Menu blur pass"), + color_attachments: &attachments, + ..Default::default() + }); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &group, &[]); + pass.draw(0..3, 0..1); + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn box_blur_shader_validates() { + let module = naga::front::wgsl::parse_str(include_str!("menu_blur.wgsl")).unwrap(); + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .unwrap(); + } +} diff --git a/crates/horizon-client/src/menu_blur.wgsl b/crates/horizon-client/src/menu_blur.wgsl new file mode 100644 index 000000000..d8b15fa94 --- /dev/null +++ b/crates/horizon-client/src/menu_blur.wgsl @@ -0,0 +1,32 @@ +// Vanilla's post/box_blur.fsh: one direction of a box blur of 2·Radius + 1 +// pixels, reading two pixels per linear sample. +struct Blur { + // InSize (pixels), BlurDir. + size_dir: vec4, + // Radius, unused, unused, unused. + radius: vec4, +} +@group(0) @binding(0) var src: texture_2d; +@group(0) @binding(1) var samp: sampler; +@group(0) @binding(2) var blur: Blur; +struct Out { + @builtin(position) clip: vec4, + @location(0) uv: vec2, +} +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> Out { + let p = vec2(f32((i << 1u) & 2u), f32(i & 2u)); + var out: Out; + out.clip = vec4(p * 2.0 - 1.0, 0.0, 1.0); + out.uv = vec2(p.x, 1.0 - p.y); + return out; +} +@fragment fn fs_main(in: Out) -> @location(0) vec4 { + let step = blur.size_dir.zw / blur.size_dir.xy; + let radius = round(blur.radius.x); + var blurred = vec4(0.0); + for (var a = -radius + 0.5; a <= radius; a += 2.0) { + blurred += textureSampleLevel(src, samp, in.uv + step * a, 0.0); + } + blurred += textureSampleLevel(src, samp, in.uv + step * radius, 0.0) / 2.0; + return blurred / (radius + 0.5); +} diff --git a/crates/horizon-client/src/multiplayer.rs b/crates/horizon-client/src/multiplayer.rs index 9e7023391..1936c8ace 100644 --- a/crates/horizon-client/src/multiplayer.rs +++ b/crates/horizon-client/src/multiplayer.rs @@ -314,7 +314,7 @@ impl Screen { } pub fn draw(&self, atlas: &HudAtlas, batch: &mut HudBatch, blink: bool) { let (w, h) = (batch.width, batch.height); - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); let centered = |batch: &mut HudBatch, text: &str, y: f32, color: u32| { let tw = HudBatch::text_width(atlas, text); batch.text(atlas, text, (w - tw) / 2.0, y, color, true); diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index d680b32ab..1cd57f00d 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -765,7 +765,7 @@ impl Title { batch.text(atlas, notice, w - tw - 2.0, h - 10.0, u32::MAX, true); } Mode::Select { worlds, selected } => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); centered(atlas, batch, "Select World", 8.0, u32::MAX); text_field( atlas, @@ -908,7 +908,7 @@ impl Title { Mode::Create { name, seed, focus, .. } => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); let title = "Create New World"; let tw = HudBatch::text_width(atlas, title); batch.text(atlas, title, (w - tw) / 2.0, 8.0, u32::MAX, true); @@ -972,7 +972,7 @@ impl Title { } } Mode::Edit { name, focused, .. } => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); centered( atlas, batch, @@ -999,7 +999,7 @@ impl Title { ); } Mode::Delete { world } => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); let message = translate( "selectWorld.deleteWarning", "'%s' will be lost forever! (A long time!)", @@ -1024,7 +1024,7 @@ impl Title { } Mode::Backup { .. } => { self.backup_shown.set(true); - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); centered( atlas, batch, @@ -1050,7 +1050,7 @@ impl Title { } } Mode::Error { message, .. } => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(0, 0, 0, 120)); + crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false); let lines = wrapped(atlas, message, w - 50.0); self.message_lines.set(lines.len()); let top = confirm_top(h, lines.len());