Skip to content

Commit

Permalink
dx12: use create_swapchain_for_hwnd helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Mar 1, 2022
1 parent 03ce0ab commit b3c2e2c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "799c61f85c5fb8b1c6c8578973a384fbfe063af1" }
# d3d12 = { path = "../d3d12-rs" }
#metal = { path = "../metal-rs" }
#web-sys = { path = "../wasm-bindgen/crates/web-sys" }
#js-sys = { path = "../wasm-bindgen/crates/js-sys" }
Expand Down
10 changes: 5 additions & 5 deletions wgpu-hal/src/auxil/dxgi/conv.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
}
}
2 changes: 2 additions & 0 deletions wgpu-hal/src/dx11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_variables)]

mod adapter;
mod command;
mod device;
Expand Down
79 changes: 39 additions & 40 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -617,56 +617,55 @@ impl crate::Surface<Api> for Surface {
raw
}
None => {
let mut swap_chain1 = native::WeakPtr::<dxgi1_2::IDXGISwapChain1>::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(_) => {}
Expand Down

0 comments on commit b3c2e2c

Please sign in to comment.