From ba8828eb5bd2b23d1bd9192dd8d37dd70bcd7607 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 15 Jan 2019 09:09:29 -0500 Subject: [PATCH] Refactor includes, collect swapchain links on RP begin --- wgpu-native/src/binding_model.rs | 1 + wgpu-native/src/command/compute.rs | 1 + wgpu-native/src/command/mod.rs | 31 +++++++++++++++++++++++------- wgpu-native/src/command/render.rs | 1 + wgpu-native/src/device.rs | 2 +- wgpu-native/src/instance.rs | 7 ++----- wgpu-native/src/lib.rs | 2 +- wgpu-native/src/pipeline.rs | 2 +- wgpu-native/src/registry/mod.rs | 10 +++++----- wgpu-native/src/resource.rs | 12 ++++++------ wgpu-native/src/swap_chain.rs | 14 ++++++-------- wgpu-native/src/track.rs | 1 + 12 files changed, 50 insertions(+), 34 deletions(-) diff --git a/wgpu-native/src/binding_model.rs b/wgpu-native/src/binding_model.rs index 47196517b..a57279f05 100644 --- a/wgpu-native/src/binding_model.rs +++ b/wgpu-native/src/binding_model.rs @@ -2,6 +2,7 @@ use crate::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId}; use bitflags::bitflags; + bitflags! { #[repr(transparent)] pub struct ShaderStageFlags: u32 { diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 477e29f31..1ac92b212 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -5,6 +5,7 @@ use hal::command::RawCommandBuffer; use std::iter; + pub struct ComputePass { raw: B::CommandBuffer, cmb_id: Stored, diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 5eac089af..1c4a6909d 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -6,12 +6,9 @@ pub(crate) use self::allocator::CommandAllocator; pub use self::compute::*; pub use self::render::*; -use hal::command::RawCommandBuffer; -use hal::Device; - use crate::device::{FramebufferKey, RenderPassKey}; use crate::registry::{Items, HUB}; -use crate::swap_chain::SwapChainLink; +use crate::swap_chain::{SwapChainLink, SwapImageEpoch}; use crate::track::{BufferTracker, TextureTracker}; use crate::{conv, resource}; use crate::{ @@ -19,11 +16,14 @@ use crate::{ Origin3d, RenderPassId, Stored, TextureId, TextureUsageFlags, TextureViewId, WeaklyStored, B, }; +use hal::command::RawCommandBuffer; +use hal::Device; +use log::trace; + use std::collections::hash_map::Entry; use std::ops::Range; use std::thread::ThreadId; -use log::trace; #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] @@ -88,7 +88,7 @@ pub struct CommandBuffer { life_guard: LifeGuard, pub(crate) buffer_tracker: BufferTracker, pub(crate) texture_tracker: TextureTracker, - swap_chain_links: Vec, + swap_chain_links: Vec>, } impl CommandBuffer { @@ -164,7 +164,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( let rp_key = { let tracker = &mut cmb.texture_tracker; - //let swap_chain_links = &mut cmb.swap_chain_links; + let swap_chain_links = &mut cmb.swap_chain_links; let depth_stencil_key = match desc.depth_stencil_attachment { Some(ref at) => { @@ -192,6 +192,23 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( let color_keys = desc.color_attachments.iter().map(|at| { let view = view_guard.get(at.attachment); + + if view.is_owned_by_swap_chain { + let link = match HUB.textures + .read() + .get(view.texture_id.value) + .swap_chain_link + { + Some(ref link) => SwapChainLink { + swap_chain_id: link.swap_chain_id.clone(), + epoch: *link.epoch.lock(), + image_index: link.image_index, + }, + None => unreachable!() + }; + swap_chain_links.push(link); + } + if let Some(ex) = extent { assert_eq!(ex, view.extent); } else { diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 4c9d8159c..32b8e97a0 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -4,6 +4,7 @@ use crate::{CommandBuffer, CommandBufferId, RenderPassId, Stored}; use hal::command::RawCommandBuffer; + pub struct RenderPass { raw: B::CommandBuffer, cmb_id: Stored, diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 866b4125a..749baa68b 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -20,6 +20,7 @@ use std::{ffi, slice}; use std::collections::hash_map::{Entry, HashMap}; use std::sync::atomic::Ordering; + #[derive(Hash, PartialEq)] pub(crate) struct RenderPassKey { pub attachments: Vec, @@ -902,7 +903,6 @@ pub extern "C" fn wgpu_device_create_swap_chain( acquired: Vec::with_capacity(num_frames as usize), sem_available: device.raw.create_semaphore().unwrap(), command_pool, - epoch: 1, }); let swap_chain = swap_chain_guard.get_mut(swap_chain_id); diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 993623056..38b84e355 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -1,13 +1,10 @@ -#[cfg(not(feature = "winit"))] -extern crate winit; - -use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; - use crate::registry::{HUB, Items}; use crate::{WeaklyStored, Device, Surface, AdapterId, DeviceId, InstanceId, SurfaceId, }; +use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; + #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 4ceaba19f..c957c1dbe 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -99,7 +99,7 @@ struct Stored { unsafe impl Send for Stored {} unsafe impl Sync for Stored {} -#[derive(Debug, Hash, PartialEq, Eq)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] struct WeaklyStored(T); unsafe impl Send for WeaklyStored {} diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index eb918fd40..3d5b9b3c8 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -1,9 +1,9 @@ use crate::resource; - use crate::{BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId, ShaderModuleId}; use bitflags::bitflags; + #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum BlendFactor { diff --git a/wgpu-native/src/registry/mod.rs b/wgpu-native/src/registry/mod.rs index 9c9af2c97..c43c46a29 100644 --- a/wgpu-native/src/registry/mod.rs +++ b/wgpu-native/src/registry/mod.rs @@ -1,8 +1,3 @@ -use std::sync::Arc; - -use lazy_static::lazy_static; -use parking_lot::RwLock; - use crate::{ AdapterHandle, BindGroupLayoutHandle, BindGroupHandle, BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, @@ -12,6 +7,11 @@ use crate::{ SurfaceHandle, SwapChainHandle, }; +use lazy_static::lazy_static; +use parking_lot::RwLock; + +use std::sync::Arc; + #[cfg(not(feature = "remote"))] mod local; diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index 7f40bbb30..42e4f071f 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -1,12 +1,12 @@ -use bitflags::bitflags; - -use hal; - use crate::{ Extent3d, LifeGuard, Stored, DeviceId, TextureId, }; -use crate::swap_chain::SwapChainLink; +use crate::swap_chain::{SwapChainLink, SwapImageEpoch}; + +use bitflags::bitflags; +use hal; +use parking_lot::Mutex; bitflags! { @@ -85,7 +85,7 @@ pub(crate) struct Texture { pub kind: hal::image::Kind, pub format: TextureFormat, pub full_range: hal::image::SubresourceRange, - pub swap_chain_link: Option, + pub swap_chain_link: Option>>, pub life_guard: LifeGuard, } diff --git a/wgpu-native/src/swap_chain.rs b/wgpu-native/src/swap_chain.rs index e844d49e2..bae5ceb88 100644 --- a/wgpu-native/src/swap_chain.rs +++ b/wgpu-native/src/swap_chain.rs @@ -7,17 +7,16 @@ use crate::track::{Tracktion, TrackPermit}; use hal; use hal::{Device as _Device, Swapchain as _Swapchain}; - -use parking_lot::Mutex; +use log::trace; use std::{iter, mem}; -pub type Epoch = u16; +pub type SwapImageEpoch = u16; -pub(crate) struct SwapChainLink { +pub(crate) struct SwapChainLink { pub swap_chain_id: WeaklyStored, //TODO: strongly - pub epoch: Mutex, + pub epoch: E, pub image_index: hal::SwapImageIndex, } @@ -41,7 +40,6 @@ pub(crate) struct SwapChain { pub acquired: Vec, pub sem_available: B::Semaphore, pub command_pool: hal::CommandPool, - pub epoch: Epoch, } #[repr(C)] @@ -77,7 +75,7 @@ pub extern "C" fn wgpu_swap_chain_get_next_texture( let texture_guard = HUB.textures.read(); let texture = texture_guard.get(frame.texture_id.value); match texture.swap_chain_link { - Some(ref link) => *link.epoch.lock() = swap_chain.epoch, + Some(ref link) => *link.epoch.lock() += 1, None => unreachable!(), } @@ -103,7 +101,7 @@ pub extern "C" fn wgpu_swap_chain_present( None => unreachable!(), } - //trace!("transit {:?} to present", frame.texture_id.value); + trace!("transit {:?} to present", frame.texture_id.value); let tracktion = device.texture_tracker .lock() .transit( diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index 9327509c9..767fa3978 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -8,6 +8,7 @@ use std::ops::{BitOr, Range}; use bitflags::bitflags; + #[derive(Clone, Debug, PartialEq)] #[allow(unused)] pub enum Tracktion {