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,
num::NonZeroU64,
ops::Deref,
sync::{Arc, RwLock},
sync::{Arc, Mutex},
};
use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry,
@ -29,12 +29,12 @@ struct Inner {
atlas_layout: BindGroupLayout,
uniforms_layout: BindGroupLayout,
pipeline_layout: PipelineLayout,
cache: RwLock<
cache: Mutex<
Vec<(
TextureFormat,
MultisampleState,
Option<DepthStencilState>,
Arc<RenderPipeline>,
RenderPipeline,
)>,
>,
}
@ -153,7 +153,7 @@ impl Cache {
uniforms_layout,
atlas_layout,
pipeline_layout,
cache: RwLock::new(Vec::new()),
cache: Mutex::new(Vec::new()),
}))
}
@ -200,7 +200,7 @@ impl Cache {
format: TextureFormat,
multisample: MultisampleState,
depth_stencil: Option<DepthStencilState>,
) -> Arc<RenderPipeline> {
) -> RenderPipeline {
let Inner {
cache,
pipeline_layout,
@ -209,14 +209,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 {
@ -243,7 +243,7 @@ impl Cache {
multisample,
multiview: None,
cache: None,
}));
});
cache.push((format, multisample, depth_stencil, pipeline.clone()));

View file

@ -1,15 +1,16 @@
use crate::{
text_render::GlyphonCacheKey, Cache, ContentType, RasterizeCustomGlyphRequest, FontSystem,
GlyphDetails, GpuCacheStatus, RasterizedCustomGlyph, SwashCache,
text_render::GlyphonCacheKey, Cache, ContentType, FontSystem, GlyphDetails, GpuCacheStatus,
RasterizeCustomGlyphRequest, RasterizedCustomGlyph, SwashCache,
};
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, TexelCopyTextureInfo, TexelCopyBufferLayout,
MultisampleState, Origin3d, Queue, RenderPipeline, Texture, TextureAspect, TextureDescriptor,
TextureDimension, TextureFormat, TextureUsages, TextureView, TextureViewDescriptor,
BindGroup, DepthStencilState, Device, Extent3d, MultisampleState, Origin3d, Queue,
RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture, TextureAspect,
TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureView,
TextureViewDescriptor,
};
type Hasher = BuildHasherDefault<FxHasher>;
@ -344,9 +345,7 @@ impl TextAtlas {
cache: &mut SwashCache,
content_type: ContentType,
scale_factor: f32,
rasterize_custom_glyph: impl FnMut(
RasterizeCustomGlyphRequest,
) -> Option<RasterizedCustomGlyph>,
rasterize_custom_glyph: impl FnMut(RasterizeCustomGlyphRequest) -> Option<RasterizedCustomGlyph>,
) -> bool {
let did_grow = match content_type {
ContentType::Mask => self.mask_atlas.grow(
@ -386,7 +385,7 @@ impl TextAtlas {
device: &Device,
multisample: MultisampleState,
depth_stencil: Option<DepthStencilState>,
) -> Arc<RenderPipeline> {
) -> RenderPipeline {
self.cache
.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,
};
use cosmic_text::{Color, SubpixelBin};
use std::{slice, sync::Arc};
use std::slice;
use wgpu::{
Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, TexelCopyTextureInfo,
TexelCopyBufferLayout, MultisampleState, Origin3d, Queue, RenderPass, RenderPipeline, TextureAspect,
COPY_BUFFER_ALIGNMENT,
Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, MultisampleState,
Origin3d, Queue, RenderPass, RenderPipeline, TexelCopyBufferLayout, TexelCopyTextureInfo,
TextureAspect, COPY_BUFFER_ALIGNMENT,
};
/// A text renderer that uses cached glyphs to render text into an existing render pass.
pub struct TextRenderer {
vertex_buffer: Buffer,
vertex_buffer_size: u64,
pipeline: Arc<RenderPipeline>,
pipeline: RenderPipeline,
glyph_vertices: Vec<GlyphToRender>,
}