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 Feb 2, 2022
1 parent b229c1e commit bc292e8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 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 = "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" }
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 @@ -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,
}
}
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
69 changes: 34 additions & 35 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, synchapi, winbase, winnt},
Expand Down Expand Up @@ -599,43 +599,42 @@ 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(
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::<dxgi1_4::IDXGISwapChain3>().into_result() {
Ok(swap_chain3) => {
Expand Down

0 comments on commit bc292e8

Please sign in to comment.