From ae223b07d8a88897923ae0570bf7706637d28f0f Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Wed, 2 Feb 2022 09:53:45 -0500 Subject: [PATCH] dx12: use create_swapchain_for_hwnd helper --- Cargo.lock | 2 - Cargo.toml | 2 + wgpu-hal/src/auxil/dxgi/conv.rs | 10 ++--- wgpu-hal/src/dx11/mod.rs | 2 + wgpu-hal/src/dx12/mod.rs | 79 ++++++++++++++++----------------- 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80bc29c0f85..7a7c3c821bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -430,8 +430,6 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" [[package]] name = "d3d12" version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2daefd788d1e96e0a9d66dee4b828b883509bc3ea9ce30665f04c3246372690c" dependencies = [ "bitflags", "libloading", diff --git a/Cargo.toml b/Cargo.toml index 0c7770ed44c..22bcff528b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ default-members = ["wgpu", "wgpu-hal", "wgpu-info"] [patch.crates-io] #naga = { path = "../naga" } #glow = { path = "../glow" } +# d3d12 = { git = "https://github.com/cwfitzgerald/d3d12-rs.git", rev = "2e52719e55f47036a9efaea3659a7216ba955a2d" } +d3d12 = { path = "../d3d12-rs" } #metal = { path = "../metal-rs" } #web-sys = { path = "../wasm-bindgen/crates/web-sys" } #js-sys = { path = "../wasm-bindgen/crates/js-sys" } diff --git a/wgpu-hal/src/auxil/dxgi/conv.rs b/wgpu-hal/src/auxil/dxgi/conv.rs index e0c793ac296..a08d2dd5a53 100644 --- a/wgpu-hal/src/auxil/dxgi/conv.rs +++ b/wgpu-hal/src/auxil/dxgi/conv.rs @@ -1,4 +1,4 @@ -use winapi::shared::{dxgi1_2, dxgiformat}; +use winapi::shared::{dxgiformat}; pub fn map_texture_format(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { use wgt::TextureFormat as Tf; @@ -165,11 +165,11 @@ pub fn map_vertex_format(format: wgt::VertexFormat) -> dxgiformat::DXGI_FORMAT { } } -pub fn map_acomposite_alpha_mode(mode: crate::CompositeAlphaMode) -> dxgi1_2::DXGI_ALPHA_MODE { +pub fn map_acomposite_alpha_mode(mode: crate::CompositeAlphaMode) -> native::AlphaMode { use crate::CompositeAlphaMode as Cam; match mode { - Cam::Opaque => dxgi1_2::DXGI_ALPHA_MODE_IGNORE, - Cam::PreMultiplied => dxgi1_2::DXGI_ALPHA_MODE_PREMULTIPLIED, - Cam::PostMultiplied => dxgi1_2::DXGI_ALPHA_MODE_STRAIGHT, + Cam::Opaque => native::AlphaMode::Ignore, + Cam::PreMultiplied => native::AlphaMode::Premultiplied, + Cam::PostMultiplied => native::AlphaMode::Straight, } } diff --git a/wgpu-hal/src/dx11/mod.rs b/wgpu-hal/src/dx11/mod.rs index 35db8ba8deb..41c488b8ff2 100644 --- a/wgpu-hal/src/dx11/mod.rs +++ b/wgpu-hal/src/dx11/mod.rs @@ -1,3 +1,5 @@ +#![allow(unused_variables)] + mod adapter; mod command; mod device; diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index a9f65ce5902..06da9caf77e 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -45,7 +45,7 @@ use crate::auxil; use arrayvec::ArrayVec; use parking_lot::Mutex; -use std::{borrow::Cow, ffi, mem, num::NonZeroU32, ptr, sync::Arc}; +use std::{borrow::Cow, ffi, mem, num::NonZeroU32, sync::Arc}; use winapi::{ shared::{dxgi, dxgi1_2, dxgi1_4, dxgitype, windef, winerror}, um::{d3d12, dcomp, synchapi, winbase, winnt}, @@ -617,56 +617,55 @@ impl crate::Surface for Surface { raw } None => { - let mut swap_chain1 = native::WeakPtr::::null(); - - let raw_desc = dxgi1_2::DXGI_SWAP_CHAIN_DESC1 { - AlphaMode: auxil::dxgi::conv::map_acomposite_alpha_mode( + let desc = native::SwapchainDesc { + alpha_mode: auxil::dxgi::conv::map_acomposite_alpha_mode( config.composite_alpha_mode, ), - BufferCount: config.swap_chain_size, - Width: config.extent.width, - Height: config.extent.height, - Format: non_srgb_format, - Flags: flags, - BufferUsage: dxgitype::DXGI_USAGE_RENDER_TARGET_OUTPUT, - SampleDesc: dxgitype::DXGI_SAMPLE_DESC { - Count: 1, - Quality: 0, + width: config.extent.width, + height: config.extent.height, + format: non_srgb_format, + stereo: false, + sample: native::SampleDesc { + count: 1, + quality: 0, }, - Scaling: dxgi1_2::DXGI_SCALING_STRETCH, - Stereo: 0, - SwapEffect: dxgi::DXGI_SWAP_EFFECT_FLIP_DISCARD, + buffer_usage: dxgitype::DXGI_USAGE_RENDER_TARGET_OUTPUT, + buffer_count: config.swap_chain_size, + scaling: native::Scaling::Stretch, + swap_effect: native::SwapEffect::FlipDiscard, + flags, }; - - let hr = { - match self.target { - SurfaceTarget::WndHandle(wnd_handle) => { - profiling::scope!("IDXGIFactory4::CreateSwapChainForHwnd"); - self.factory.CreateSwapChainForHwnd( + let swap_chain1 = match self.target { + SurfaceTarget::Visual(_) => { + profiling::scope!("IDXGIFactory4::CreateSwapChainForComposition"); + self.factory + .as_factory2() + .create_swapchain_for_composition( device.present_queue.as_mut_ptr() as *mut _, - wnd_handle, - &raw_desc, - ptr::null(), - ptr::null_mut(), - swap_chain1.mut_void() as *mut *mut _, + &desc, ) - } - SurfaceTarget::Visual(_) => { - profiling::scope!("IDXGIFactory4::CreateSwapChainForComposition"); - self.factory.CreateSwapChainForComposition( + .into_result() + } + SurfaceTarget::WndHandle(hwnd) => { + profiling::scope!("IDXGIFactory4::CreateSwapChainForHwnd"); + self.factory + .as_factory2() + .create_swapchain_for_hwnd( device.present_queue.as_mut_ptr() as *mut _, - &raw_desc, - ptr::null_mut(), - swap_chain1.mut_void() as *mut *mut _, + hwnd, + &desc, ) - } + .into_result() } }; - if let Err(err) = hr.into_result() { - log::error!("SwapChain creation error: {}", err); - return Err(crate::SurfaceError::Other("swap chain creation")); - } + let swap_chain1 = match swap_chain1 { + Ok(s) => s, + Err(err) => { + log::error!("SwapChain creation error: {}", err); + return Err(crate::SurfaceError::Other("swap chain creation")); + } + }; match self.target { SurfaceTarget::WndHandle(_) => {}