From 4f07ce8248c7b6a77e23379fb8ab0e830f7abc54 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 30 Oct 2018 21:23:22 -0400 Subject: [PATCH] Cache framebuffers by the Device --- wgpu-native/src/command/mod.rs | 37 ++++++++++++++++++++++++---------- wgpu-native/src/device.rs | 8 ++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 967f0a391b..ec348ee93c 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -14,7 +14,7 @@ use { BufferId, CommandBufferId, ComputePassId, DeviceId, RenderPassId, TextureId, TextureViewId, }; use conv; -use device::RenderPassKey; +use device::{FramebufferKey, RenderPassKey}; use registry::{HUB, Items, Registry}; use track::{BufferTracker, TextureTracker}; @@ -230,15 +230,30 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } }; - let framebuffer = { - let attachments = desc.color_attachments + let mut framebuffer_cache = device.framebuffers.lock().unwrap(); + let fb_key = FramebufferKey { + attachments: desc.color_attachments .iter() - .map(|at| at.attachment) - .chain(desc.depth_stencil_attachment.as_ref().map(|at| at.attachment)) - .map(|id| &view_guard.get(id).raw); - device.raw - .create_framebuffer(&render_pass, attachments, extent.unwrap()) - .unwrap() + .map(|at| Stored(at.attachment)) + .chain(desc.depth_stencil_attachment.as_ref().map(|at| Stored(at.attachment))) + .collect(), + }; + let framebuffer = match framebuffer_cache.entry(fb_key) { + Entry::Occupied(e) => e.into_mut(), + Entry::Vacant(e) => { + let fb = { + let attachments = e + .key() + .attachments + .iter() + .map(|&Stored(id)| &view_guard.get(id).raw); + + device.raw + .create_framebuffer(&render_pass, attachments, extent.unwrap()) + .unwrap() + }; + e.insert(fb) + } }; let rect = { @@ -263,8 +278,8 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( })); current_comb.begin_render_pass( - &render_pass, - &framebuffer, + render_pass, + framebuffer, rect, clear_values, hal::command::SubpassContents::Inline, diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 8ab1661dfd..ee53e951cd 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -24,6 +24,12 @@ pub(crate) struct RenderPassKey { } impl Eq for RenderPassKey {} +#[derive(Hash, PartialEq)] +pub(crate) struct FramebufferKey { + pub attachments: Vec>, +} +impl Eq for FramebufferKey {} + pub struct Device { pub(crate) raw: B::Device, @@ -34,6 +40,7 @@ pub struct Device { texture_tracker: Mutex, mem_props: hal::MemoryProperties, pub(crate) render_passes: Mutex>, + pub(crate) framebuffers: Mutex>, } impl Device { @@ -72,6 +79,7 @@ impl Device { texture_tracker: Mutex::new(TextureTracker::new()), mem_props, render_passes: Mutex::new(HashMap::new()), + framebuffers: Mutex::new(HashMap::new()), } } }