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.
This commit is contained in:
Alphyr 2025-02-05 03:04:11 +01:00 committed by Héctor Ramón Jiménez
parent 114cfa3e02
commit 505f12f6ce
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 13 additions and 14 deletions

View file

@ -1,5 +1,4 @@
use crate::{GlyphToRender, Params}; use crate::{GlyphToRender, Params};
use wgpu::{ use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry,
BindingResource, BindingType, BlendState, Buffer, BufferBindingType, ColorTargetState, BindingResource, BindingType, BlendState, Buffer, BufferBindingType, ColorTargetState,
@ -14,7 +13,7 @@ use std::borrow::Cow;
use std::mem; use std::mem;
use std::num::NonZeroU64; use std::num::NonZeroU64;
use std::ops::Deref; use std::ops::Deref;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Cache(Arc<Inner>); pub struct Cache(Arc<Inner>);
@ -27,12 +26,12 @@ struct Inner {
atlas_layout: BindGroupLayout, atlas_layout: BindGroupLayout,
uniforms_layout: BindGroupLayout, uniforms_layout: BindGroupLayout,
pipeline_layout: PipelineLayout, pipeline_layout: PipelineLayout,
cache: RwLock< cache: Mutex<
Vec<( Vec<(
TextureFormat, TextureFormat,
MultisampleState, MultisampleState,
Option<DepthStencilState>, Option<DepthStencilState>,
Arc<RenderPipeline>, RenderPipeline,
)>, )>,
>, >,
} }
@ -150,7 +149,7 @@ impl Cache {
uniforms_layout, uniforms_layout,
atlas_layout, atlas_layout,
pipeline_layout, pipeline_layout,
cache: RwLock::new(Vec::new()), cache: Mutex::new(Vec::new()),
})) }))
} }
@ -197,7 +196,7 @@ impl Cache {
format: TextureFormat, format: TextureFormat,
multisample: MultisampleState, multisample: MultisampleState,
depth_stencil: Option<DepthStencilState>, depth_stencil: Option<DepthStencilState>,
) -> Arc<RenderPipeline> { ) -> RenderPipeline {
let Inner { let Inner {
cache, cache,
pipeline_layout, pipeline_layout,
@ -206,14 +205,14 @@ impl Cache {
.. ..
} = self.0.deref(); } = self.0.deref();
let mut cache = cache.write().expect("Write pipeline cache"); let mut cache = cache.lock().expect("Write pipeline cache");
cache cache
.iter() .iter()
.find(|(fmt, ms, ds, _)| fmt == &format && ms == &multisample && ds == &depth_stencil) .find(|(fmt, ms, ds, _)| fmt == &format && ms == &multisample && ds == &depth_stencil)
.map(|(_, _, _, p)| Arc::clone(p)) .map(|(_, _, _, p)| p.clone())
.unwrap_or_else(|| { .unwrap_or_else(|| {
let pipeline = Arc::new(device.create_render_pipeline(&RenderPipelineDescriptor { let pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
label: Some("glyphon pipeline"), label: Some("glyphon pipeline"),
layout: Some(pipeline_layout), layout: Some(pipeline_layout),
vertex: VertexState { vertex: VertexState {
@ -240,7 +239,7 @@ impl Cache {
multisample, multisample,
multiview: None, multiview: None,
cache: None, cache: None,
})); });
cache.push((format, multisample, depth_stencil, pipeline.clone())); cache.push((format, multisample, depth_stencil, pipeline.clone()));

View file

@ -4,7 +4,7 @@ use crate::{
use etagere::{size2, Allocation, BucketedAtlasAllocator}; use etagere::{size2, Allocation, BucketedAtlasAllocator};
use lru::LruCache; use lru::LruCache;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use std::{collections::HashSet, hash::BuildHasherDefault, sync::Arc}; use std::{collections::HashSet, hash::BuildHasherDefault};
use wgpu::{ use wgpu::{
BindGroup, DepthStencilState, Device, Extent3d, MultisampleState, Origin3d, Queue, BindGroup, DepthStencilState, Device, Extent3d, MultisampleState, Origin3d, Queue,
RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture, TextureAspect, RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture, TextureAspect,
@ -329,7 +329,7 @@ impl TextAtlas {
device: &Device, device: &Device,
multisample: MultisampleState, multisample: MultisampleState,
depth_stencil: Option<DepthStencilState>, depth_stencil: Option<DepthStencilState>,
) -> Arc<RenderPipeline> { ) -> RenderPipeline {
self.cache self.cache
.get_or_create_pipeline(device, self.format, multisample, depth_stencil) .get_or_create_pipeline(device, self.format, multisample, depth_stencil)
} }

View file

@ -2,7 +2,7 @@ use crate::{
ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, PrepareError, RenderError, ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, PrepareError, RenderError,
SwashCache, SwashContent, TextArea, TextAtlas, Viewport, SwashCache, SwashContent, TextArea, TextAtlas, Viewport,
}; };
use std::{num::NonZeroU64, slice, sync::Arc}; use std::{num::NonZeroU64, slice};
use wgpu::util::StagingBelt; use wgpu::util::StagingBelt;
use wgpu::{ use wgpu::{
Buffer, BufferDescriptor, BufferUsages, CommandEncoder, DepthStencilState, Device, Extent3d, Buffer, BufferDescriptor, BufferUsages, CommandEncoder, DepthStencilState, Device, Extent3d,
@ -15,7 +15,7 @@ pub struct TextRenderer {
staging_belt: StagingBelt, staging_belt: StagingBelt,
vertex_buffer: Buffer, vertex_buffer: Buffer,
vertex_buffer_size: u64, vertex_buffer_size: u64,
pipeline: Arc<RenderPipeline>, pipeline: RenderPipeline,
glyph_vertices: Vec<GlyphToRender>, glyph_vertices: Vec<GlyphToRender>,
glyphs_to_render: u32, glyphs_to_render: u32,
} }