Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.
Rust differential-testinggame-enginegamedevlinuxminecraftminecraft-javareimplementationrustvoxelvulkanwgpu
Renderer: keep shader modules, layouts and pipelines for the next renderer on the device
Ludovic Marief57550ec8c36
diff --git a/crates/horizon-client-render/src/gfx.rs b/crates/horizon-client-render/src/gfx.rsindex 5b3ee3b82..93e0bf3a7 100644--- a/crates/horizon-client-render/src/gfx.rs+++ b/crates/horizon-client-render/src/gfx.rs@@ -145,6 +145,7 @@ mod page_runs;pub mod panorama;mod readback;mod resident;+mod reuse;mod stars;pub mod submit;mod sunrise_fan;@@ -753,6 +754,9 @@ pub struct Renderer {display_fresh: bool,window: Arc<Window>,pub device: wgpu::Device,+ /// The shader modules, layouts and pipelines kept for the next renderer on+ /// this device ([`reuse`]).+ reuse: Arc<reuse::Reuse>,/// Frames are finished and submitted on its submit thread ([`submit`]).pub queue: submit::Queue,/// The last frame went to the submit thread; whether it had no window surface.@@ -1940,10 +1944,15 @@ impl Renderer {if previous.is_none() {startup_step(format_args!("creating the pipelines"));}+ // A renderer on the same device takes the previous one's shader+ // modules, layouts and pipelines where they come out the same.+ let reuse = previous.map_or_else(Default::default, |r| r.reuse.clone());+ let generation = reuse.begin();+ let kept = reuse.on(&device);let pipelines_step = crate::spans::startup::step("renderer.pipelines_textures");let terrain_step = crate::spans::startup::step("renderer.terrain_pipelines");let gpu_timer = timing::GpuTimer::new(&device, &queue);- let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {+ let shader = kept.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon terrain"),source: wgpu::ShaderSource::Wgsl(include_str!("terrain.wgsl").into()),});@@ -1967,7 +1976,7 @@ impl Renderer {("SPRITE_ARRAYS", f64::from(sprite_arrays)),("SPRITE_SPLIT", f64::from(split)),];- let bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {+ let bindings = kept.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("Horizon terrain bindings"),entries: &[entry(@@ -2025,7 +2034,7 @@ impl Renderer {),],});- let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ let layout = kept.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {label: Some("Horizon terrain layout"),bind_group_layouts: &[Some(&bindings)],..Default::default()@@ -2061,7 +2070,7 @@ impl Renderer {depth_write: bool,depth_test: bool,blend: Option<wgpu::BlendState>| {- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some(label),layout: Some(layout),vertex: wgpu::VertexState {@@ -2209,7 +2218,7 @@ impl Renderer {false,Some(alpha_blend),);- let depth_reset = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ let depth_reset = kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("Hand depth reset"),layout: Some(&layout),vertex: wgpu::VertexState {@@ -2601,11 +2610,11 @@ impl Renderer {)?;hud.atlas_texture = Some(atlas.clone());let entity_step = crate::spans::startup::step("renderer.entity_pipelines");- let entity_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {+ let entity_shader = kept.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon entities"),source: wgpu::ShaderSource::Wgsl(entity_shader().into()),});- let entity_bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {+ let entity_bindings = kept.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("Entity bindings"),entries: &[entry(@@ -2756,12 +2765,12 @@ impl Renderer {},],});- let entity_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ let entity_layout = kept.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {label: Some("Entity layout"),bind_group_layouts: &[Some(&entity_bindings)],..Default::default()});- let cloud_bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {+ let cloud_bindings = kept.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("Cloud bindings"),entries: &[entry(0,@@ -2787,7 +2796,7 @@ impl Renderer {resource: cloud_uniform.as_entire_binding(),}],});- let cloud_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ let cloud_layout = kept.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {label: Some("Cloud layout"),bind_group_layouts: &[Some(&entity_bindings), Some(&cloud_bindings)],..Default::default()@@ -2814,7 +2823,7 @@ impl Renderer {clouds,fragment: &str,write_mask| {- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some(label),layout: Some(if clouds {&cloud_layout@@ -3076,10 +3085,10 @@ impl Renderer {multiview_mask: None,cache: None,};- let additive_pipeline = device.create_render_pipeline(&additive_descriptor);+ let additive_pipeline = kept.create_render_pipeline(&additive_descriptor);// `RenderPipelines.DRAGON_RAYS` uses DepthStencilState.DEFAULT:// unlike eyes, a ray hides the faces and rays behind it.- let dragon_rays_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ let dragon_rays_pipeline = kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("Dragon death rays"),primitive: wgpu::PrimitiveState {cull_mode: Some(wgpu::Face::Back),@@ -3099,7 +3108,7 @@ impl Renderer {write_mask: wgpu::ColorWrites::ALPHA,})];let hdr_mark_lightning_pipeline =- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("HDR lightning mark"),vertex: wgpu::VertexState {entry_point: Some("vs_lightning"),@@ -3114,7 +3123,7 @@ impl Renderer {}),..additive_descriptor.clone()});- let lightning_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ let lightning_pipeline = kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("Lightning"),vertex: wgpu::VertexState {entry_point: Some("vs_lightning"),@@ -3123,7 +3132,7 @@ impl Renderer {},..additive_descriptor});- let celestial_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ let celestial_pipeline = kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("Celestial bodies"),layout: Some(&entity_layout),vertex: wgpu::VertexState {@@ -3218,7 +3227,7 @@ impl Renderer {usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,mapped_at_creation: false,});- let border_bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {+ let border_bindings = kept.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("World border bindings"),entries: &[entry(@@ -3277,19 +3286,19 @@ impl Renderer {},],});- let border_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ let border_layout = kept.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {label: Some("World border layout"),bind_group_layouts: &[Some(&border_bindings)],..Default::default()});- let border_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {+ let border_shader = kept.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon world border"),source: wgpu::ShaderSource::Wgsl(include_str!("border.wgsl").into()),});let border_attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2];// RenderPipelines.WORLD_BORDER: BlendFunction.OVERLAY, no culling,// depth writes with a polygon offset of 3 towards the camera.- let border_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ let border_pipeline = kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("World border"),layout: Some(&border_layout),vertex: wgpu::VertexState {@@ -3342,7 +3351,7 @@ impl Renderer {multiview_mask: None,cache: None,});- let screen_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {+ let screen_shader = kept.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon screen effects"),source: wgpu::ShaderSource::Wgsl(include_str!("screen.wgsl").into()),});@@ -3381,7 +3390,7 @@ impl Renderer {constants: &constants,..Default::default()};- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ kept.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some("Screen effects"),layout: Some(&entity_layout),vertex: wgpu::VertexState {@@ -3551,6 +3560,13 @@ impl Renderer {let panorama =panorama::Panorama::new(&device, config.format.remove_srgb_suffix(), panorama_faces);drop(pipelines_step);+ reuse.finish(generation);+ if previous.is_some() {+ log::info!(+ "Horizon renderer: rebuilt on the same device ({} shader modules, layouts and pipelines kept)",+ reuse.len()+ );+ }if previous.is_none() {startup_step(format_args!("pipelines ready (multi-draw {}, occlusion culling {}, GPU timer {})",@@ -3581,6 +3597,7 @@ impl Renderer {last_encode: std::time::Duration::ZERO,window,device,+ reuse,queue: submit::Queue::new(queue, submit::wanted()),deferred: None,device_lost,diff --git a/crates/horizon-client-render/src/gfx/reuse.rs b/crates/horizon-client-render/src/gfx/reuse.rsnew file mode 100644index 000000000..ae5a8f32d--- /dev/null+++ b/crates/horizon-client-render/src/gfx/reuse.rs@@ -0,0 +1,181 @@+//! Pipelines kept between the renderers built on one device. The start builds+//! three (the boot screen's, the title artwork's, the world's) and a resource+//! reload or a graphics option builds another; each compiled every shader+//! module and pipeline again, though most come out identical (the boot and+//! title renderers' entirely, the world's entity pipelines). On a driver that+//! compiles slowly (a cold shader cache, llvmpipe, a weak CPU) that was seconds+//! of the main thread before the title showed.+//!+//! An object is kept under its descriptor's `Debug` text: wgpu prints the+//! labels, sources, constants, formats and states, and the handles a descriptor+//! names (shader modules, layouts) by their ids, so a descriptor that names a+//! kept handle matches only the same handle. Kept objects stay alive, so their+//! ids are never reused while they are keys.+use std::{+ collections::HashMap,+ sync::{Mutex, PoisonError},+};++/// One map of kept objects with the generation each was last asked for in.+struct Kept<T> {+ map: Mutex<HashMap<String, (T, u32)>>,+}+impl<T> Default for Kept<T> {+ fn default() -> Self {+ Self {+ map: Mutex::new(HashMap::new()),+ }+ }+}+impl<T: Clone> Kept<T> {+ /// The object kept under `key`, else `make`'s, kept. The lock is not+ /// held while `make` compiles.+ fn get(&self, key: String, generation: u32, make: impl FnOnce() -> T) -> T {+ if let Some((object, used)) = self+ .map+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .get_mut(&key)+ {+ *used = generation;+ return object.clone();+ }+ let object = make();+ self.map+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .insert(key, (object.clone(), generation));+ object+ }+ fn keep_only(&self, generation: u32) {+ self.map+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .retain(|_, (_, used)| *used == generation);+ }+ fn len(&self) -> usize {+ self.map+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .len()+ }+}++/// The shader modules, layouts and pipelines of the device's renderers.+#[derive(Default)]+pub struct Reuse {+ generation: std::sync::atomic::AtomicU32,+ shaders: Kept<wgpu::ShaderModule>,+ bind_group_layouts: Kept<wgpu::BindGroupLayout>,+ pipeline_layouts: Kept<wgpu::PipelineLayout>,+ render: Kept<wgpu::RenderPipeline>,+}++impl Reuse {+ /// A renderer starts building: what it asks for is kept, the rest is let+ /// go by [`Reuse::finish`].+ pub fn begin(&self) -> u32 {+ self.generation+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed)+ .wrapping_add(1)+ }+ /// The renderer of `generation` is built: objects only older renderers+ /// used are dropped here (the renderers still alive hold their own).+ pub fn finish(&self, generation: u32) {+ self.shaders.keep_only(generation);+ self.bind_group_layouts.keep_only(generation);+ self.pipeline_layouts.keep_only(generation);+ self.render.keep_only(generation);+ }+ /// How many objects are kept.+ pub fn len(&self) -> usize {+ self.shaders.len()+ + self.bind_group_layouts.len()+ + self.pipeline_layouts.len()+ + self.render.len()+ }+ fn current(&self) -> u32 {+ self.generation.load(std::sync::atomic::Ordering::Relaxed)+ }+ /// `device`'s `create_*` calls through this cache.+ pub fn on<'a>(&'a self, device: &'a wgpu::Device) -> On<'a> {+ On {+ reuse: self,+ device,+ generation: self.current(),+ }+ }+}++/// A device whose shader modules, layouts and render pipelines come from a+/// [`Reuse`] when one with the same descriptor is kept.+pub struct On<'a> {+ reuse: &'a Reuse,+ device: &'a wgpu::Device,+ generation: u32,+}+impl On<'_> {+ pub fn create_shader_module(+ &self,+ descriptor: wgpu::ShaderModuleDescriptor<'_>,+ ) -> wgpu::ShaderModule {+ let key = format!("{descriptor:?}");+ self.reuse.shaders.get(key, self.generation, || {+ self.device.create_shader_module(descriptor)+ })+ }+ pub fn create_bind_group_layout(+ &self,+ descriptor: &wgpu::BindGroupLayoutDescriptor<'_>,+ ) -> wgpu::BindGroupLayout {+ let key = format!("{descriptor:?}");+ self.reuse.bind_group_layouts.get(key, self.generation, || {+ self.device.create_bind_group_layout(descriptor)+ })+ }+ pub fn create_pipeline_layout(+ &self,+ descriptor: &wgpu::PipelineLayoutDescriptor<'_>,+ ) -> wgpu::PipelineLayout {+ let key = format!("{descriptor:?}");+ self.reuse.pipeline_layouts.get(key, self.generation, || {+ self.device.create_pipeline_layout(descriptor)+ })+ }+ pub fn create_render_pipeline(+ &self,+ descriptor: &wgpu::RenderPipelineDescriptor<'_>,+ ) -> wgpu::RenderPipeline {+ let key = format!("{descriptor:?}");+ self.reuse.render.get(key, self.generation, || {+ self.device.create_render_pipeline(descriptor)+ })+ }+}++#[cfg(test)]+mod tests {+ use super::Kept;++ /// A key asked for again gets the kept object without making one; what+ /// the latest generation did not ask for is let go when it finishes.+ #[test]+ fn kept_objects_are_shared_until_a_build_stops_asking() {+ let kept = Kept::<u32>::default();+ let made = std::cell::Cell::new(0);+ let make = |value| {+ made.set(made.get() + 1);+ value+ };+ assert_eq!(kept.get("a".into(), 1, || make(10)), 10);+ assert_eq!(kept.get("b".into(), 1, || make(20)), 20);+ kept.keep_only(1);+ assert_eq!(kept.len(), 2);+ // The next build asks for "a" only.+ assert_eq!(kept.get("a".into(), 2, || make(11)), 10);+ kept.keep_only(2);+ assert_eq!(kept.len(), 1);+ assert_eq!(kept.get("b".into(), 3, || make(21)), 21);+ assert_eq!(made.get(), 3);+ }+}