From 505f12f6cec6c00f893971c999c96957b06683a0 Mon Sep 17 00:00:00 2001 From: Alphyr <47725341+a1phyr@users.noreply.github.com> Date: Wed, 5 Feb 2025 03:04:11 +0100 Subject: [PATCH] Small improvements to cache (#130) * Remove `Arc` around `RenderPipeline` It is not necessary anymore in last `wgpu` version. * Replace `RwLock` with `Mutex` It was only used for writing. --- src/cache.rs | 19 +++++++++---------- src/text_atlas.rs | 4 ++-- src/text_render.rs | 4 ++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index c567341..61d2e4d 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,5 +1,4 @@ use crate::{GlyphToRender, Params}; - use wgpu::{ BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry, BindingResource, BindingType, BlendState, Buffer, BufferBindingType, ColorTargetState, @@ -14,7 +13,7 @@ use std::borrow::Cow; use std::mem; use std::num::NonZeroU64; use std::ops::Deref; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, Mutex}; #[derive(Debug, Clone)] pub struct Cache(Arc); @@ -27,12 +26,12 @@ struct Inner { atlas_layout: BindGroupLayout, uniforms_layout: BindGroupLayout, pipeline_layout: PipelineLayout, - cache: RwLock< + cache: Mutex< Vec<( TextureFormat, MultisampleState, Option, - Arc, + RenderPipeline, )>, >, } @@ -150,7 +149,7 @@ impl Cache { uniforms_layout, atlas_layout, pipeline_layout, - cache: RwLock::new(Vec::new()), + cache: Mutex::new(Vec::new()), })) } @@ -197,7 +196,7 @@ impl Cache { format: TextureFormat, multisample: MultisampleState, depth_stencil: Option, - ) -> Arc { + ) -> RenderPipeline { let Inner { cache, pipeline_layout, @@ -206,14 +205,14 @@ impl Cache { .. } = self.0.deref(); - let mut cache = cache.write().expect("Write pipeline cache"); + let mut cache = cache.lock().expect("Write pipeline cache"); cache .iter() .find(|(fmt, ms, ds, _)| fmt == &format && ms == &multisample && ds == &depth_stencil) - .map(|(_, _, _, p)| Arc::clone(p)) + .map(|(_, _, _, p)| p.clone()) .unwrap_or_else(|| { - let pipeline = Arc::new(device.create_render_pipeline(&RenderPipelineDescriptor { + let pipeline = device.create_render_pipeline(&RenderPipelineDescriptor { label: Some("glyphon pipeline"), layout: Some(pipeline_layout), vertex: VertexState { @@ -240,7 +239,7 @@ impl Cache { multisample, multiview: None, cache: None, - })); + }); cache.push((format, multisample, depth_stencil, pipeline.clone())); diff --git a/src/text_atlas.rs b/src/text_atlas.rs index 0b90a0f..8313672 100644 --- a/src/text_atlas.rs +++ b/src/text_atlas.rs @@ -4,7 +4,7 @@ use crate::{ use etagere::{size2, Allocation, BucketedAtlasAllocator}; use lru::LruCache; use rustc_hash::FxHasher; -use std::{collections::HashSet, hash::BuildHasherDefault, sync::Arc}; +use std::{collections::HashSet, hash::BuildHasherDefault}; use wgpu::{ BindGroup, DepthStencilState, Device, Extent3d, MultisampleState, Origin3d, Queue, RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture, TextureAspect, @@ -329,7 +329,7 @@ impl TextAtlas { device: &Device, multisample: MultisampleState, depth_stencil: Option, - ) -> Arc { + ) -> RenderPipeline { self.cache .get_or_create_pipeline(device, self.format, multisample, depth_stencil) } diff --git a/src/text_render.rs b/src/text_render.rs index 7bd60bb..d1b4150 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -2,7 +2,7 @@ use crate::{ ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, PrepareError, RenderError, SwashCache, SwashContent, TextArea, TextAtlas, Viewport, }; -use std::{num::NonZeroU64, slice, sync::Arc}; +use std::{num::NonZeroU64, slice}; use wgpu::util::StagingBelt; use wgpu::{ Buffer, BufferDescriptor, BufferUsages, CommandEncoder, DepthStencilState, Device, Extent3d, @@ -15,7 +15,7 @@ pub struct TextRenderer { staging_belt: StagingBelt, vertex_buffer: Buffer, vertex_buffer_size: u64, - pipeline: Arc, + pipeline: RenderPipeline, glyph_vertices: Vec, glyphs_to_render: u32, }