From bc292e848620cfe24191cccc713731ecad015202 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 | 3 +- Cargo.toml | 2 + wgpu-hal/src/auxil/dxgi/conv.rs | 10 ++--- wgpu-hal/src/dx11/mod.rs | 2 + wgpu-hal/src/dx12/mod.rs | 69 ++++++++++++++++----------------- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 807a7fafade..0965d8c34e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -430,8 +430,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" [[package]] name = "d3d12" version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2daefd788d1e96e0a9d66dee4b828b883509bc3ea9ce30665f04c3246372690c" +source = "git+https://github.com/cwfitzgerald/d3d12-rs.git?rev=2e52719e55f47036a9efaea3659a7216ba955a2d#2e52719e55f47036a9efaea3659a7216ba955a2d" dependencies = [ "bitflags", "libloading", diff --git a/Cargo.toml b/Cargo.toml index 0c7770ed44c..31e8b5c39b8 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 300024824c9..32f06d0a0b1 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; @@ -189,11 +189,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 99dde7422b9..581f424ee93 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, synchapi, winbase, winnt}, @@ -599,43 +599,42 @@ 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( - 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, - }, - Scaling: dxgi1_2::DXGI_SCALING_STRETCH, - Stereo: 0, - SwapEffect: dxgi::DXGI_SWAP_EFFECT_FLIP_DISCARD, - }; - - let hr = { + let swap_chain1 = { profiling::scope!("IDXGIFactory4::CreateSwapChainForHwnd"); - self.factory.CreateSwapChainForHwnd( - device.present_queue.as_mut_ptr() as *mut _, - self.wnd_handle, - &raw_desc, - ptr::null(), - ptr::null_mut(), - swap_chain1.mut_void() as *mut *mut _, - ) + self.factory + .as_factory2() + .create_swapchain_for_hwnd( + device.present_queue.as_mut_ptr() as *mut _, + self.wnd_handle, + &native::SwapchainDesc { + alpha_mode: auxil::dxgi::conv::map_acomposite_alpha_mode( + config.composite_alpha_mode, + ), + width: config.extent.width, + height: config.extent.height, + format: non_srgb_format, + stereo: false, + sample: native::SampleDesc { + count: 1, + quality: 0, + }, + buffer_usage: dxgitype::DXGI_USAGE_RENDER_TARGET_OUTPUT, + buffer_count: config.swap_chain_size, + scaling: native::Scaling::Stretch, + swap_effect: native::SwapEffect::FlipDiscard, + flags, + }, + ) + .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 swap_chain1.cast::().into_result() { Ok(swap_chain3) => {