Skip to content

Commit ea83b60

Browse files
committed
perf(compositor): reuse the kept background's texture on Windows and macOS
A cache miss allocated a new full-size texture each time, so a key that changes every frame (a blur or colour dragged in the inspector) allocated one per frame. Take the previous one back, as Linux does; resized() empties the cache, so its size always matches.
1 parent 6b29eb7 commit ea83b60

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎crates/compositor/src/compositor_macos.rs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,14 +2409,18 @@ impl Compositor {
24092409
.then(|| crate::frame_geometry::BackgroundKey::of(bg_desc, bg_blur, lp.bg_color, [rw, rh]))
24102410
.flatten();
24112411
if let Some(key) = key {
2412-
let tex = make_texture(
2413-
&self.gpu.device,
2414-
metal::MTLPixelFormat::RGBA8Unorm,
2415-
self.render_w,
2416-
self.render_h,
2417-
metal::MTLStorageMode::Private,
2418-
metal::MTLTextureUsage::ShaderRead,
2419-
);
2412+
// L'ancienne texture est reprise : `resized` vide le cache, sa taille est la bonne.
2413+
let old = self.bg_cache.borrow_mut().take().map(|c| c.tex);
2414+
let tex = old.unwrap_or_else(|| {
2415+
make_texture(
2416+
&self.gpu.device,
2417+
metal::MTLPixelFormat::RGBA8Unorm,
2418+
self.render_w,
2419+
self.render_h,
2420+
metal::MTLStorageMode::Private,
2421+
metal::MTLTextureUsage::ShaderRead,
2422+
)
2423+
});
24202424
self.blit_background(cmd_buf, &tex, false);
24212425
*self.bg_cache.borrow_mut() = Some(BgCache { key, tex });
24222426
}

‎crates/compositor/src/compositor_windows.rs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,19 @@ impl Compositor {
973973

974974
/// Garde le fond que le RT porte à cet instant (`bg_cache`), sous `key`.
975975
unsafe fn keep_background(&self, key: crate::frame_geometry::BackgroundKey) -> Result<()> {
976-
let mut d = D3D11_TEXTURE2D_DESC::default();
977-
self.rt.GetDesc(&mut d);
978-
d.BindFlags = 0;
979-
let mut tex: Option<ID3D11Texture2D> = None;
980-
self.dev.CreateTexture2D(&d, None, Some(&mut tex))?;
981-
let tex = tex.ok_or_else(|| anyhow::anyhow!("CreateTexture2D (fond gardé)"))?;
976+
// L'ancienne texture est reprise : `resized` vide le cache, sa taille est la bonne.
977+
let old = self.bg_cache.borrow_mut().take().map(|c| c.tex);
978+
let tex = match old {
979+
Some(tex) => tex,
980+
None => {
981+
let mut d = D3D11_TEXTURE2D_DESC::default();
982+
self.rt.GetDesc(&mut d);
983+
d.BindFlags = 0;
984+
let mut tex: Option<ID3D11Texture2D> = None;
985+
self.dev.CreateTexture2D(&d, None, Some(&mut tex))?;
986+
tex.ok_or_else(|| anyhow::anyhow!("CreateTexture2D (fond gardé)"))?
987+
}
988+
};
982989
self.ctx.CopyResource(&tex, &self.rt);
983990
*self.bg_cache.borrow_mut() = Some(BgCache { key, tex });
984991
Ok(())

0 commit comments

Comments
 (0)