render: fix "next swap chain texture" bug

This commit is contained in:
Carter Anderson 2020-07-19 16:15:19 -07:00
parent 946d5d1024
commit cadbb4c1b0
4 changed files with 24 additions and 15 deletions

View File

@ -68,7 +68,7 @@ impl Node for WindowSwapChainNode {
render_resource_context.create_swap_chain(window); render_resource_context.create_swap_chain(window);
} }
let swap_chain_texture = render_resource_context.next_swap_chain_texture(window.id); let swap_chain_texture = render_resource_context.next_swap_chain_texture(&window);
output.set( output.set(
WINDOW_TEXTURE, WINDOW_TEXTURE,
RenderResourceId::Texture(swap_chain_texture), RenderResourceId::Texture(swap_chain_texture),

View File

@ -6,7 +6,7 @@ use crate::{
texture::{SamplerDescriptor, TextureDescriptor}, texture::{SamplerDescriptor, TextureDescriptor},
}; };
use bevy_asset::{Assets, Handle, HandleUntyped}; use bevy_asset::{Assets, Handle, HandleUntyped};
use bevy_window::{Window, WindowId}; use bevy_window::Window;
use std::{ use std::{
collections::HashMap, collections::HashMap,
ops::Range, ops::Range,
@ -35,7 +35,7 @@ impl HeadlessRenderResourceContext {
impl RenderResourceContext for HeadlessRenderResourceContext { impl RenderResourceContext for HeadlessRenderResourceContext {
fn create_swap_chain(&self, _window: &Window) {} fn create_swap_chain(&self, _window: &Window) {}
fn next_swap_chain_texture(&self, _window_id: WindowId) -> TextureId { fn next_swap_chain_texture(&self, _window: &Window) -> TextureId {
TextureId::new() TextureId::new()
} }
fn drop_swap_chain_texture(&self, _render_resource: TextureId) {} fn drop_swap_chain_texture(&self, _render_resource: TextureId) {}

View File

@ -5,13 +5,13 @@ use crate::{
texture::{SamplerDescriptor, TextureDescriptor}, texture::{SamplerDescriptor, TextureDescriptor},
}; };
use bevy_asset::{Assets, Handle, HandleUntyped}; use bevy_asset::{Assets, Handle, HandleUntyped};
use bevy_window::{Window, WindowId}; use bevy_window::Window;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
use std::ops::Range; use std::ops::Range;
pub trait RenderResourceContext: Downcast + Send + Sync + 'static { pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
fn create_swap_chain(&self, window: &Window); fn create_swap_chain(&self, window: &Window);
fn next_swap_chain_texture(&self, window_id: WindowId) -> TextureId; fn next_swap_chain_texture(&self, window: &Window) -> TextureId;
fn drop_swap_chain_texture(&self, resource: TextureId); fn drop_swap_chain_texture(&self, resource: TextureId);
fn drop_all_swap_chain_textures(&self); fn drop_all_swap_chain_textures(&self);
fn create_sampler(&self, sampler_descriptor: &SamplerDescriptor) -> SamplerId; fn create_sampler(&self, sampler_descriptor: &SamplerDescriptor) -> SamplerId;

View File

@ -127,6 +127,17 @@ impl WgpuRenderResourceContext {
let bind_group_layout = self.device.create_bind_group_layout(&wgpu_descriptor); let bind_group_layout = self.device.create_bind_group_layout(&wgpu_descriptor);
bind_group_layouts.insert(descriptor.id, bind_group_layout); bind_group_layouts.insert(descriptor.id, bind_group_layout);
} }
fn try_next_swap_chain_texture(&self, window_id: bevy_window::WindowId) -> Option<TextureId> {
let mut window_swap_chains = self.resources.window_swap_chains.write().unwrap();
let mut swap_chain_outputs = self.resources.swap_chain_frames.write().unwrap();
let window_swap_chain = window_swap_chains.get_mut(&window_id).unwrap();
let next_texture = window_swap_chain.get_next_frame().ok()?;
let id = TextureId::new();
swap_chain_outputs.insert(id, next_texture);
Some(id)
}
} }
impl RenderResourceContext for WgpuRenderResourceContext { impl RenderResourceContext for WgpuRenderResourceContext {
@ -252,16 +263,14 @@ impl RenderResourceContext for WgpuRenderResourceContext {
window_swap_chains.insert(window.id, swap_chain); window_swap_chains.insert(window.id, swap_chain);
} }
fn next_swap_chain_texture(&self, window_id: bevy_window::WindowId) -> TextureId { fn next_swap_chain_texture(&self, window: &bevy_window::Window) -> TextureId {
let mut window_swap_chains = self.resources.window_swap_chains.write().unwrap(); if let Some(texture_id) = self.try_next_swap_chain_texture(window.id) {
let mut swap_chain_outputs = self.resources.swap_chain_frames.write().unwrap(); texture_id
} else {
let window_swap_chain = window_swap_chains.get_mut(&window_id).unwrap(); self.resources.window_swap_chains.write().unwrap().remove(&window.id);
let next_texture = window_swap_chain.get_next_frame().unwrap(); self.create_swap_chain(window);
self.try_next_swap_chain_texture(window.id).expect("Failed to acquire next swap chain texture!")
let id = TextureId::new(); }
swap_chain_outputs.insert(id, next_texture);
id
} }
fn drop_swap_chain_texture(&self, texture: TextureId) { fn drop_swap_chain_texture(&self, texture: TextureId) {