Cache framebuffers by the Device

This commit is contained in:
Dzmitry Malyshau 2018-10-30 21:23:22 -04:00
parent d206de2049
commit 4f07ce8248
2 changed files with 34 additions and 11 deletions

View File

@ -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,

View File

@ -24,6 +24,12 @@ pub(crate) struct RenderPassKey {
}
impl Eq for RenderPassKey {}
#[derive(Hash, PartialEq)]
pub(crate) struct FramebufferKey {
pub attachments: Vec<Stored<TextureViewId>>,
}
impl Eq for FramebufferKey {}
pub struct Device<B: hal::Backend> {
pub(crate) raw: B::Device,
@ -34,6 +40,7 @@ pub struct Device<B: hal::Backend> {
texture_tracker: Mutex<TextureTracker>,
mem_props: hal::MemoryProperties,
pub(crate) render_passes: Mutex<HashMap<RenderPassKey, B::RenderPass>>,
pub(crate) framebuffers: Mutex<HashMap<FramebufferKey, B::Framebuffer>>,
}
impl<B: hal::Backend> Device<B> {
@ -72,6 +79,7 @@ impl<B: hal::Backend> Device<B> {
texture_tracker: Mutex::new(TextureTracker::new()),
mem_props,
render_passes: Mutex::new(HashMap::new()),
framebuffers: Mutex::new(HashMap::new()),
}
}
}