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 GitHub
parent 651f2543a1
commit 128841ee7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 24 deletions

View file

@ -4,7 +4,7 @@ use std::{
mem, mem,
num::NonZeroU64, num::NonZeroU64,
ops::Deref, ops::Deref,
sync::{Arc, RwLock}, sync::{Arc, Mutex},
}; };
use wgpu::{ use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry,
@ -29,12 +29,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,
)>, )>,
>, >,
} }
@ -153,7 +153,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()),
})) }))
} }
@ -200,7 +200,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,
@ -209,14 +209,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 {
@ -243,7 +243,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

@ -1,15 +1,16 @@
use crate::{ use crate::{
text_render::GlyphonCacheKey, Cache, ContentType, RasterizeCustomGlyphRequest, FontSystem, text_render::GlyphonCacheKey, Cache, ContentType, FontSystem, GlyphDetails, GpuCacheStatus,
GlyphDetails, GpuCacheStatus, RasterizedCustomGlyph, SwashCache, RasterizeCustomGlyphRequest, RasterizedCustomGlyph, SwashCache,
}; };
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, TexelCopyTextureInfo, TexelCopyBufferLayout, BindGroup, DepthStencilState, Device, Extent3d, MultisampleState, Origin3d, Queue,
MultisampleState, Origin3d, Queue, RenderPipeline, Texture, TextureAspect, TextureDescriptor, RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture, TextureAspect,
TextureDimension, TextureFormat, TextureUsages, TextureView, TextureViewDescriptor, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureView,
TextureViewDescriptor,
}; };
type Hasher = BuildHasherDefault<FxHasher>; type Hasher = BuildHasherDefault<FxHasher>;
@ -344,9 +345,7 @@ impl TextAtlas {
cache: &mut SwashCache, cache: &mut SwashCache,
content_type: ContentType, content_type: ContentType,
scale_factor: f32, scale_factor: f32,
rasterize_custom_glyph: impl FnMut( rasterize_custom_glyph: impl FnMut(RasterizeCustomGlyphRequest) -> Option<RasterizedCustomGlyph>,
RasterizeCustomGlyphRequest,
) -> Option<RasterizedCustomGlyph>,
) -> bool { ) -> bool {
let did_grow = match content_type { let did_grow = match content_type {
ContentType::Mask => self.mask_atlas.grow( ContentType::Mask => self.mask_atlas.grow(
@ -386,7 +385,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

@ -4,18 +4,18 @@ use crate::{
RasterizedCustomGlyph, RenderError, SwashCache, SwashContent, TextArea, TextAtlas, Viewport, RasterizedCustomGlyph, RenderError, SwashCache, SwashContent, TextArea, TextAtlas, Viewport,
}; };
use cosmic_text::{Color, SubpixelBin}; use cosmic_text::{Color, SubpixelBin};
use std::{slice, sync::Arc}; use std::slice;
use wgpu::{ use wgpu::{
Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, TexelCopyTextureInfo, Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, MultisampleState,
TexelCopyBufferLayout, MultisampleState, Origin3d, Queue, RenderPass, RenderPipeline, TextureAspect, Origin3d, Queue, RenderPass, RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo,
COPY_BUFFER_ALIGNMENT, TextureAspect, COPY_BUFFER_ALIGNMENT,
}; };
/// A text renderer that uses cached glyphs to render text into an existing render pass. /// A text renderer that uses cached glyphs to render text into an existing render pass.
pub struct TextRenderer { pub struct TextRenderer {
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>,
} }