diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index 8169d07cdb1..910ed109f3c 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -16,7 +16,7 @@ default = [] metal = ["naga/msl-out", "block", "foreign-types"] vulkan = ["naga/spv-out", "ash", "gpu-alloc", "gpu-descriptor", "libloading", "inplace_it"] gles = ["naga/glsl-out", "glow", "egl", "libloading"] -dx11 = ["naga/hlsl-out", "winapi/d3d11", "winapi/d3d11_1", "winapi/d3d11sdklayers", "winapi/dxgi1_6"] +dx11 = ["naga/hlsl-out", "native", "winapi/d3d11", "winapi/d3d11_1", "winapi/d3d11sdklayers", "winapi/dxgi1_6"] dx12 = ["naga/hlsl-out", "native", "bit-set", "range-alloc", "winapi/d3d12", "winapi/d3d12shader", "winapi/d3d12sdklayers", "winapi/dxgi1_6"] renderdoc = ["libloading", "renderdoc-sys"] emscripten = ["gles"] diff --git a/wgpu-hal/src/auxil/dxgi/conv.rs b/wgpu-hal/src/auxil/dxgi/conv.rs new file mode 100644 index 00000000000..300024824c9 --- /dev/null +++ b/wgpu-hal/src/auxil/dxgi/conv.rs @@ -0,0 +1,199 @@ +use winapi::shared::{dxgi1_2, dxgiformat}; + +pub fn map_texture_format(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { + use wgt::TextureFormat as Tf; + use winapi::shared::dxgiformat::*; + + match format { + Tf::R8Unorm => DXGI_FORMAT_R8_UNORM, + Tf::R8Snorm => DXGI_FORMAT_R8_SNORM, + Tf::R8Uint => DXGI_FORMAT_R8_UINT, + Tf::R8Sint => DXGI_FORMAT_R8_SINT, + Tf::R16Uint => DXGI_FORMAT_R16_UINT, + Tf::R16Sint => DXGI_FORMAT_R16_SINT, + Tf::R16Unorm => DXGI_FORMAT_R16_UNORM, + Tf::R16Snorm => DXGI_FORMAT_R16_SNORM, + Tf::R16Float => DXGI_FORMAT_R16_FLOAT, + Tf::Rg8Unorm => DXGI_FORMAT_R8G8_UNORM, + Tf::Rg8Snorm => DXGI_FORMAT_R8G8_SNORM, + Tf::Rg8Uint => DXGI_FORMAT_R8G8_UINT, + Tf::Rg8Sint => DXGI_FORMAT_R8G8_SINT, + Tf::Rg16Unorm => DXGI_FORMAT_R16G16_UNORM, + Tf::Rg16Snorm => DXGI_FORMAT_R16G16_SNORM, + Tf::R32Uint => DXGI_FORMAT_R32_UINT, + Tf::R32Sint => DXGI_FORMAT_R32_SINT, + Tf::R32Float => DXGI_FORMAT_R32_FLOAT, + Tf::Rg16Uint => DXGI_FORMAT_R16G16_UINT, + Tf::Rg16Sint => DXGI_FORMAT_R16G16_SINT, + Tf::Rg16Float => DXGI_FORMAT_R16G16_FLOAT, + Tf::Rgba8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM, + Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, + Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, + Tf::Rgba8Snorm => DXGI_FORMAT_R8G8B8A8_SNORM, + Tf::Bgra8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM, + Tf::Rgba8Uint => DXGI_FORMAT_R8G8B8A8_UINT, + Tf::Rgba8Sint => DXGI_FORMAT_R8G8B8A8_SINT, + Tf::Rgb10a2Unorm => DXGI_FORMAT_R10G10B10A2_UNORM, + Tf::Rg11b10Float => DXGI_FORMAT_R11G11B10_FLOAT, + Tf::Rg32Uint => DXGI_FORMAT_R32G32_UINT, + Tf::Rg32Sint => DXGI_FORMAT_R32G32_SINT, + Tf::Rg32Float => DXGI_FORMAT_R32G32_FLOAT, + Tf::Rgba16Uint => DXGI_FORMAT_R16G16B16A16_UINT, + Tf::Rgba16Sint => DXGI_FORMAT_R16G16B16A16_SINT, + Tf::Rgba16Unorm => DXGI_FORMAT_R16G16B16A16_UNORM, + Tf::Rgba16Snorm => DXGI_FORMAT_R16G16B16A16_SNORM, + Tf::Rgba16Float => DXGI_FORMAT_R16G16B16A16_FLOAT, + Tf::Rgba32Uint => DXGI_FORMAT_R32G32B32A32_UINT, + Tf::Rgba32Sint => DXGI_FORMAT_R32G32B32A32_SINT, + Tf::Rgba32Float => DXGI_FORMAT_R32G32B32A32_FLOAT, + Tf::Depth32Float => DXGI_FORMAT_D32_FLOAT, + Tf::Depth24Plus => DXGI_FORMAT_D24_UNORM_S8_UINT, + Tf::Depth24PlusStencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT, + Tf::Rgb9e5Ufloat => DXGI_FORMAT_R9G9B9E5_SHAREDEXP, + Tf::Bc1RgbaUnorm => DXGI_FORMAT_BC1_UNORM, + Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_UNORM_SRGB, + Tf::Bc2RgbaUnorm => DXGI_FORMAT_BC2_UNORM, + Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_UNORM_SRGB, + Tf::Bc3RgbaUnorm => DXGI_FORMAT_BC3_UNORM, + Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_UNORM_SRGB, + Tf::Bc4RUnorm => DXGI_FORMAT_BC4_UNORM, + Tf::Bc4RSnorm => DXGI_FORMAT_BC4_SNORM, + Tf::Bc5RgUnorm => DXGI_FORMAT_BC5_UNORM, + Tf::Bc5RgSnorm => DXGI_FORMAT_BC5_SNORM, + Tf::Bc6hRgbUfloat => DXGI_FORMAT_BC6H_UF16, + Tf::Bc6hRgbSfloat => DXGI_FORMAT_BC6H_SF16, + Tf::Bc7RgbaUnorm => DXGI_FORMAT_BC7_UNORM, + Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_UNORM_SRGB, + Tf::Etc2Rgb8Unorm + | Tf::Etc2Rgb8UnormSrgb + | Tf::Etc2Rgb8A1Unorm + | Tf::Etc2Rgb8A1UnormSrgb + | Tf::Etc2Rgba8Unorm + | Tf::Etc2Rgba8UnormSrgb + | Tf::EacR11Unorm + | Tf::EacR11Snorm + | Tf::EacRg11Unorm + | Tf::EacRg11Snorm + | Tf::Astc4x4RgbaUnorm + | Tf::Astc4x4RgbaUnormSrgb + | Tf::Astc5x4RgbaUnorm + | Tf::Astc5x4RgbaUnormSrgb + | Tf::Astc5x5RgbaUnorm + | Tf::Astc5x5RgbaUnormSrgb + | Tf::Astc6x5RgbaUnorm + | Tf::Astc6x5RgbaUnormSrgb + | Tf::Astc6x6RgbaUnorm + | Tf::Astc6x6RgbaUnormSrgb + | Tf::Astc8x5RgbaUnorm + | Tf::Astc8x5RgbaUnormSrgb + | Tf::Astc8x6RgbaUnorm + | Tf::Astc8x6RgbaUnormSrgb + | Tf::Astc10x5RgbaUnorm + | Tf::Astc10x5RgbaUnormSrgb + | Tf::Astc10x6RgbaUnorm + | Tf::Astc10x6RgbaUnormSrgb + | Tf::Astc8x8RgbaUnorm + | Tf::Astc8x8RgbaUnormSrgb + | Tf::Astc10x8RgbaUnorm + | Tf::Astc10x8RgbaUnormSrgb + | Tf::Astc10x10RgbaUnorm + | Tf::Astc10x10RgbaUnormSrgb + | Tf::Astc12x10RgbaUnorm + | Tf::Astc12x10RgbaUnormSrgb + | Tf::Astc12x12RgbaUnorm + | Tf::Astc12x12RgbaUnormSrgb => unreachable!(), + } +} + +// Note: DXGI doesn't allow sRGB format on the swapchain, +// but creating RTV of swapchain buffers with sRGB works. +pub fn map_texture_format_nosrgb(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { + match format { + wgt::TextureFormat::Bgra8UnormSrgb => dxgiformat::DXGI_FORMAT_B8G8R8A8_UNORM, + wgt::TextureFormat::Rgba8UnormSrgb => dxgiformat::DXGI_FORMAT_R8G8B8A8_UNORM, + _ => map_texture_format(format), + } +} + +//Note: SRV and UAV can't use the depth formats directly +//TODO: stencil views? +pub fn map_texture_format_nodepth(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { + match format { + wgt::TextureFormat::Depth32Float => dxgiformat::DXGI_FORMAT_R32_FLOAT, + wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8 => { + dxgiformat::DXGI_FORMAT_R24_UNORM_X8_TYPELESS + } + _ => { + assert_eq!( + crate::FormatAspects::from(format), + crate::FormatAspects::COLOR + ); + map_texture_format(format) + } + } +} + +pub fn map_texture_format_depth_typeless(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { + match format { + wgt::TextureFormat::Depth32Float => dxgiformat::DXGI_FORMAT_R32_TYPELESS, + wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8 => { + dxgiformat::DXGI_FORMAT_R24G8_TYPELESS + } + _ => unreachable!(), + } +} + +pub fn map_index_format(format: wgt::IndexFormat) -> dxgiformat::DXGI_FORMAT { + match format { + wgt::IndexFormat::Uint16 => dxgiformat::DXGI_FORMAT_R16_UINT, + wgt::IndexFormat::Uint32 => dxgiformat::DXGI_FORMAT_R32_UINT, + } +} + +pub fn map_vertex_format(format: wgt::VertexFormat) -> dxgiformat::DXGI_FORMAT { + use wgt::VertexFormat as Vf; + use winapi::shared::dxgiformat::*; + + match format { + Vf::Unorm8x2 => DXGI_FORMAT_R8G8_UNORM, + Vf::Snorm8x2 => DXGI_FORMAT_R8G8_SNORM, + Vf::Uint8x2 => DXGI_FORMAT_R8G8_UINT, + Vf::Sint8x2 => DXGI_FORMAT_R8G8_SINT, + Vf::Unorm8x4 => DXGI_FORMAT_R8G8B8A8_UNORM, + Vf::Snorm8x4 => DXGI_FORMAT_R8G8B8A8_SNORM, + Vf::Uint8x4 => DXGI_FORMAT_R8G8B8A8_UINT, + Vf::Sint8x4 => DXGI_FORMAT_R8G8B8A8_SINT, + Vf::Unorm16x2 => DXGI_FORMAT_R16G16_UNORM, + Vf::Snorm16x2 => DXGI_FORMAT_R16G16_SNORM, + Vf::Uint16x2 => DXGI_FORMAT_R16G16_UINT, + Vf::Sint16x2 => DXGI_FORMAT_R16G16_SINT, + Vf::Float16x2 => DXGI_FORMAT_R16G16_FLOAT, + Vf::Unorm16x4 => DXGI_FORMAT_R16G16B16A16_UNORM, + Vf::Snorm16x4 => DXGI_FORMAT_R16G16B16A16_SNORM, + Vf::Uint16x4 => DXGI_FORMAT_R16G16B16A16_UINT, + Vf::Sint16x4 => DXGI_FORMAT_R16G16B16A16_SINT, + Vf::Float16x4 => DXGI_FORMAT_R16G16B16A16_FLOAT, + Vf::Uint32 => DXGI_FORMAT_R32_UINT, + Vf::Sint32 => DXGI_FORMAT_R32_SINT, + Vf::Float32 => DXGI_FORMAT_R32_FLOAT, + Vf::Uint32x2 => DXGI_FORMAT_R32G32_UINT, + Vf::Sint32x2 => DXGI_FORMAT_R32G32_SINT, + Vf::Float32x2 => DXGI_FORMAT_R32G32_FLOAT, + Vf::Uint32x3 => DXGI_FORMAT_R32G32B32_UINT, + Vf::Sint32x3 => DXGI_FORMAT_R32G32B32_SINT, + Vf::Float32x3 => DXGI_FORMAT_R32G32B32_FLOAT, + Vf::Uint32x4 => DXGI_FORMAT_R32G32B32A32_UINT, + Vf::Sint32x4 => DXGI_FORMAT_R32G32B32A32_SINT, + Vf::Float32x4 => DXGI_FORMAT_R32G32B32A32_FLOAT, + Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), + } +} + +pub fn map_acomposite_alpha_mode(mode: crate::CompositeAlphaMode) -> dxgi1_2::DXGI_ALPHA_MODE { + 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, + } +} diff --git a/wgpu-hal/src/auxil/dxgi/mod.rs b/wgpu-hal/src/auxil/dxgi/mod.rs new file mode 100644 index 00000000000..e906693400c --- /dev/null +++ b/wgpu-hal/src/auxil/dxgi/mod.rs @@ -0,0 +1 @@ +pub mod conv; diff --git a/wgpu-hal/src/auxil/mod.rs b/wgpu-hal/src/auxil/mod.rs index 4d8cd0d2fa9..f92fa2863e9 100644 --- a/wgpu-hal/src/auxil/mod.rs +++ b/wgpu-hal/src/auxil/mod.rs @@ -1,3 +1,6 @@ +#[cfg(any(feature = "dx11", feature = "dx12"))] +pub(super) mod dxgi; + #[cfg(feature = "renderdoc")] pub(super) mod renderdoc; diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 51d40c8671c..f6030e9cf84 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -1,4 +1,4 @@ -use super::{conv, HResult as _}; +use crate::{auxil, dx12::HResult as _}; use std::{mem, sync::Arc, thread}; use winapi::{ shared::{dxgi, dxgi1_2, dxgi1_5, minwindef, windef, winerror}, @@ -317,7 +317,7 @@ impl crate::Adapter for super::Adapter { ) -> crate::TextureFormatCapabilities { use crate::TextureFormatCapabilities as Tfc; - let raw_format = conv::map_texture_format(format); + let raw_format = auxil::dxgi::conv::map_texture_format(format); let mut data = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT { Format: raw_format, Support1: mem::zeroed(), diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 673ccccdf1e..5ed065da4a2 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1,3 +1,5 @@ +use crate::auxil; + use super::{conv, HResult as _}; use std::{mem, ops::Range, ptr}; use winapi::um::d3d12; @@ -22,7 +24,7 @@ impl crate::BufferTextureCopy { d3d12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT { Offset: self.buffer_layout.offset, Footprint: d3d12::D3D12_SUBRESOURCE_FOOTPRINT { - Format: conv::map_texture_format(format), + Format: auxil::dxgi::conv::map_texture_format(format), Width: self.size.width, Height: self .buffer_layout @@ -834,7 +836,7 @@ impl crate::CommandEncoder for super::CommandEncoder { self.list.unwrap().set_index_buffer( binding.resolve_address(), binding.resolve_size() as u32, - conv::map_index_format(format), + auxil::dxgi::conv::map_index_format(format), ); } unsafe fn set_vertex_buffer<'a>( diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index 45c648a6ab4..38cca33ae44 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -1,207 +1,8 @@ use std::iter; use winapi::{ - shared::{dxgi1_2, dxgiformat}, um::{d3d12, d3dcommon}, }; -pub(super) fn map_texture_format(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { - use wgt::TextureFormat as Tf; - use winapi::shared::dxgiformat::*; - - match format { - Tf::R8Unorm => DXGI_FORMAT_R8_UNORM, - Tf::R8Snorm => DXGI_FORMAT_R8_SNORM, - Tf::R8Uint => DXGI_FORMAT_R8_UINT, - Tf::R8Sint => DXGI_FORMAT_R8_SINT, - Tf::R16Uint => DXGI_FORMAT_R16_UINT, - Tf::R16Sint => DXGI_FORMAT_R16_SINT, - Tf::R16Unorm => DXGI_FORMAT_R16_UNORM, - Tf::R16Snorm => DXGI_FORMAT_R16_SNORM, - Tf::R16Float => DXGI_FORMAT_R16_FLOAT, - Tf::Rg8Unorm => DXGI_FORMAT_R8G8_UNORM, - Tf::Rg8Snorm => DXGI_FORMAT_R8G8_SNORM, - Tf::Rg8Uint => DXGI_FORMAT_R8G8_UINT, - Tf::Rg8Sint => DXGI_FORMAT_R8G8_SINT, - Tf::Rg16Unorm => DXGI_FORMAT_R16G16_UNORM, - Tf::Rg16Snorm => DXGI_FORMAT_R16G16_SNORM, - Tf::R32Uint => DXGI_FORMAT_R32_UINT, - Tf::R32Sint => DXGI_FORMAT_R32_SINT, - Tf::R32Float => DXGI_FORMAT_R32_FLOAT, - Tf::Rg16Uint => DXGI_FORMAT_R16G16_UINT, - Tf::Rg16Sint => DXGI_FORMAT_R16G16_SINT, - Tf::Rg16Float => DXGI_FORMAT_R16G16_FLOAT, - Tf::Rgba8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM, - Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, - Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, - Tf::Rgba8Snorm => DXGI_FORMAT_R8G8B8A8_SNORM, - Tf::Bgra8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM, - Tf::Rgba8Uint => DXGI_FORMAT_R8G8B8A8_UINT, - Tf::Rgba8Sint => DXGI_FORMAT_R8G8B8A8_SINT, - Tf::Rgb10a2Unorm => DXGI_FORMAT_R10G10B10A2_UNORM, - Tf::Rg11b10Float => DXGI_FORMAT_R11G11B10_FLOAT, - Tf::Rg32Uint => DXGI_FORMAT_R32G32_UINT, - Tf::Rg32Sint => DXGI_FORMAT_R32G32_SINT, - Tf::Rg32Float => DXGI_FORMAT_R32G32_FLOAT, - Tf::Rgba16Uint => DXGI_FORMAT_R16G16B16A16_UINT, - Tf::Rgba16Sint => DXGI_FORMAT_R16G16B16A16_SINT, - Tf::Rgba16Unorm => DXGI_FORMAT_R16G16B16A16_UNORM, - Tf::Rgba16Snorm => DXGI_FORMAT_R16G16B16A16_SNORM, - Tf::Rgba16Float => DXGI_FORMAT_R16G16B16A16_FLOAT, - Tf::Rgba32Uint => DXGI_FORMAT_R32G32B32A32_UINT, - Tf::Rgba32Sint => DXGI_FORMAT_R32G32B32A32_SINT, - Tf::Rgba32Float => DXGI_FORMAT_R32G32B32A32_FLOAT, - Tf::Depth32Float => DXGI_FORMAT_D32_FLOAT, - Tf::Depth24Plus => DXGI_FORMAT_D24_UNORM_S8_UINT, - Tf::Depth24PlusStencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT, - Tf::Rgb9e5Ufloat => DXGI_FORMAT_R9G9B9E5_SHAREDEXP, - Tf::Bc1RgbaUnorm => DXGI_FORMAT_BC1_UNORM, - Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_UNORM_SRGB, - Tf::Bc2RgbaUnorm => DXGI_FORMAT_BC2_UNORM, - Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_UNORM_SRGB, - Tf::Bc3RgbaUnorm => DXGI_FORMAT_BC3_UNORM, - Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_UNORM_SRGB, - Tf::Bc4RUnorm => DXGI_FORMAT_BC4_UNORM, - Tf::Bc4RSnorm => DXGI_FORMAT_BC4_SNORM, - Tf::Bc5RgUnorm => DXGI_FORMAT_BC5_UNORM, - Tf::Bc5RgSnorm => DXGI_FORMAT_BC5_SNORM, - Tf::Bc6hRgbUfloat => DXGI_FORMAT_BC6H_UF16, - Tf::Bc6hRgbSfloat => DXGI_FORMAT_BC6H_SF16, - Tf::Bc7RgbaUnorm => DXGI_FORMAT_BC7_UNORM, - Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_UNORM_SRGB, - Tf::Etc2Rgb8Unorm - | Tf::Etc2Rgb8UnormSrgb - | Tf::Etc2Rgb8A1Unorm - | Tf::Etc2Rgb8A1UnormSrgb - | Tf::Etc2Rgba8Unorm - | Tf::Etc2Rgba8UnormSrgb - | Tf::EacR11Unorm - | Tf::EacR11Snorm - | Tf::EacRg11Unorm - | Tf::EacRg11Snorm - | Tf::Astc4x4RgbaUnorm - | Tf::Astc4x4RgbaUnormSrgb - | Tf::Astc5x4RgbaUnorm - | Tf::Astc5x4RgbaUnormSrgb - | Tf::Astc5x5RgbaUnorm - | Tf::Astc5x5RgbaUnormSrgb - | Tf::Astc6x5RgbaUnorm - | Tf::Astc6x5RgbaUnormSrgb - | Tf::Astc6x6RgbaUnorm - | Tf::Astc6x6RgbaUnormSrgb - | Tf::Astc8x5RgbaUnorm - | Tf::Astc8x5RgbaUnormSrgb - | Tf::Astc8x6RgbaUnorm - | Tf::Astc8x6RgbaUnormSrgb - | Tf::Astc10x5RgbaUnorm - | Tf::Astc10x5RgbaUnormSrgb - | Tf::Astc10x6RgbaUnorm - | Tf::Astc10x6RgbaUnormSrgb - | Tf::Astc8x8RgbaUnorm - | Tf::Astc8x8RgbaUnormSrgb - | Tf::Astc10x8RgbaUnorm - | Tf::Astc10x8RgbaUnormSrgb - | Tf::Astc10x10RgbaUnorm - | Tf::Astc10x10RgbaUnormSrgb - | Tf::Astc12x10RgbaUnorm - | Tf::Astc12x10RgbaUnormSrgb - | Tf::Astc12x12RgbaUnorm - | Tf::Astc12x12RgbaUnormSrgb => unreachable!(), - } -} - -//Note: DXGI doesn't allow sRGB format on the swapchain, -// but creating RTV of swapchain buffers with sRGB works. -pub fn map_texture_format_nosrgb(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { - match format { - wgt::TextureFormat::Bgra8UnormSrgb => dxgiformat::DXGI_FORMAT_B8G8R8A8_UNORM, - wgt::TextureFormat::Rgba8UnormSrgb => dxgiformat::DXGI_FORMAT_R8G8B8A8_UNORM, - _ => map_texture_format(format), - } -} - -//Note: SRV and UAV can't use the depth formats directly -//TODO: stencil views? -pub fn map_texture_format_nodepth(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { - match format { - wgt::TextureFormat::Depth32Float => dxgiformat::DXGI_FORMAT_R32_FLOAT, - wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8 => { - dxgiformat::DXGI_FORMAT_R24_UNORM_X8_TYPELESS - } - _ => { - assert_eq!( - crate::FormatAspects::from(format), - crate::FormatAspects::COLOR - ); - map_texture_format(format) - } - } -} - -pub fn map_texture_format_depth_typeless(format: wgt::TextureFormat) -> dxgiformat::DXGI_FORMAT { - match format { - wgt::TextureFormat::Depth32Float => dxgiformat::DXGI_FORMAT_R32_TYPELESS, - wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8 => { - dxgiformat::DXGI_FORMAT_R24G8_TYPELESS - } - _ => unreachable!(), - } -} - -pub fn map_index_format(format: wgt::IndexFormat) -> dxgiformat::DXGI_FORMAT { - match format { - wgt::IndexFormat::Uint16 => dxgiformat::DXGI_FORMAT_R16_UINT, - wgt::IndexFormat::Uint32 => dxgiformat::DXGI_FORMAT_R32_UINT, - } -} - -pub fn map_vertex_format(format: wgt::VertexFormat) -> dxgiformat::DXGI_FORMAT { - use wgt::VertexFormat as Vf; - use winapi::shared::dxgiformat::*; - - match format { - Vf::Unorm8x2 => DXGI_FORMAT_R8G8_UNORM, - Vf::Snorm8x2 => DXGI_FORMAT_R8G8_SNORM, - Vf::Uint8x2 => DXGI_FORMAT_R8G8_UINT, - Vf::Sint8x2 => DXGI_FORMAT_R8G8_SINT, - Vf::Unorm8x4 => DXGI_FORMAT_R8G8B8A8_UNORM, - Vf::Snorm8x4 => DXGI_FORMAT_R8G8B8A8_SNORM, - Vf::Uint8x4 => DXGI_FORMAT_R8G8B8A8_UINT, - Vf::Sint8x4 => DXGI_FORMAT_R8G8B8A8_SINT, - Vf::Unorm16x2 => DXGI_FORMAT_R16G16_UNORM, - Vf::Snorm16x2 => DXGI_FORMAT_R16G16_SNORM, - Vf::Uint16x2 => DXGI_FORMAT_R16G16_UINT, - Vf::Sint16x2 => DXGI_FORMAT_R16G16_SINT, - Vf::Float16x2 => DXGI_FORMAT_R16G16_FLOAT, - Vf::Unorm16x4 => DXGI_FORMAT_R16G16B16A16_UNORM, - Vf::Snorm16x4 => DXGI_FORMAT_R16G16B16A16_SNORM, - Vf::Uint16x4 => DXGI_FORMAT_R16G16B16A16_UINT, - Vf::Sint16x4 => DXGI_FORMAT_R16G16B16A16_SINT, - Vf::Float16x4 => DXGI_FORMAT_R16G16B16A16_FLOAT, - Vf::Uint32 => DXGI_FORMAT_R32_UINT, - Vf::Sint32 => DXGI_FORMAT_R32_SINT, - Vf::Float32 => DXGI_FORMAT_R32_FLOAT, - Vf::Uint32x2 => DXGI_FORMAT_R32G32_UINT, - Vf::Sint32x2 => DXGI_FORMAT_R32G32_SINT, - Vf::Float32x2 => DXGI_FORMAT_R32G32_FLOAT, - Vf::Uint32x3 => DXGI_FORMAT_R32G32B32_UINT, - Vf::Sint32x3 => DXGI_FORMAT_R32G32B32_SINT, - Vf::Float32x3 => DXGI_FORMAT_R32G32B32_FLOAT, - Vf::Uint32x4 => DXGI_FORMAT_R32G32B32A32_UINT, - Vf::Sint32x4 => DXGI_FORMAT_R32G32B32A32_SINT, - Vf::Float32x4 => DXGI_FORMAT_R32G32B32A32_FLOAT, - Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), - } -} - -pub fn map_acomposite_alpha_mode(mode: crate::CompositeAlphaMode) -> dxgi1_2::DXGI_ALPHA_MODE { - 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, - } -} - pub fn map_buffer_usage_to_resource_flags(usage: crate::BufferUses) -> d3d12::D3D12_RESOURCE_FLAGS { let mut flags = 0; if usage.contains(crate::BufferUses::STORAGE_WRITE) { diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index a29eeed45b8..a3d10ffbd85 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1,4 +1,4 @@ -use crate::FormatAspects; +use crate::{FormatAspects, auxil}; use super::{conv, descriptor, view, HResult as _}; use parking_lot::Mutex; @@ -432,13 +432,13 @@ impl crate::Device for super::Device { | crate::TextureUses::STORAGE_READ | crate::TextureUses::STORAGE_WRITE, ) { - conv::map_texture_format(desc.format) + auxil::dxgi::conv::map_texture_format(desc.format) } else { // This branch is needed if it's a depth texture, and it's ever needed to be viewed as SRV or UAV, // because then we'd create a non-depth format view of it. // Note: we can skip this branch if // `D3D12_FEATURE_D3D12_OPTIONS3::CastingFullyTypedFormatSupported` - conv::map_texture_format_depth_typeless(desc.format) + auxil::dxgi::conv::map_texture_format_depth_typeless(desc.format) }, SampleDesc: dxgitype::DXGI_SAMPLE_DESC { Count: desc.sample_count, @@ -1249,7 +1249,7 @@ impl crate::Device for super::Device { input_element_descs.push(d3d12::D3D12_INPUT_ELEMENT_DESC { SemanticName: NAGA_LOCATION_SEMANTIC.as_ptr() as *const _, SemanticIndex: attribute.shader_location, - Format: conv::map_vertex_format(attribute.format), + Format: auxil::dxgi::conv::map_vertex_format(attribute.format), InputSlot: i as u32, AlignedByteOffset: attribute.offset as u32, InputSlotClass: slot_class, @@ -1261,7 +1261,7 @@ impl crate::Device for super::Device { let mut rtv_formats = [dxgiformat::DXGI_FORMAT_UNKNOWN; d3d12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize]; for (rtv_format, ct) in rtv_formats.iter_mut().zip(desc.color_targets) { - *rtv_format = conv::map_texture_format(ct.format); + *rtv_format = auxil::dxgi::conv::map_texture_format(ct.format); } let bias = desc @@ -1350,7 +1350,7 @@ impl crate::Device for super::Device { .depth_stencil .as_ref() .map_or(dxgiformat::DXGI_FORMAT_UNKNOWN, |ds| { - conv::map_texture_format(ds.format) + auxil::dxgi::conv::map_texture_format(ds.format) }), SampleDesc: dxgitype::DXGI_SAMPLE_DESC { Count: desc.multisample.count, diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index b4dd2950590..99dde7422b9 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -41,6 +41,8 @@ mod device; mod instance; mod view; +use crate::auxil; + use arrayvec::ArrayVec; use parking_lot::Mutex; use std::{borrow::Cow, ffi, mem, num::NonZeroU32, ptr, sync::Arc}; @@ -574,7 +576,7 @@ impl crate::Surface for Surface { _ => {} } - let non_srgb_format = conv::map_texture_format_nosrgb(config.format); + let non_srgb_format = auxil::dxgi::conv::map_texture_format_nosrgb(config.format); let swap_chain = match self.swap_chain.take() { //Note: this path doesn't properly re-initialize all of the things @@ -600,7 +602,9 @@ impl crate::Surface for Surface { let mut swap_chain1 = native::WeakPtr::::null(); let raw_desc = dxgi1_2::DXGI_SWAP_CHAIN_DESC1 { - AlphaMode: conv::map_acomposite_alpha_mode(config.composite_alpha_mode), + 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, diff --git a/wgpu-hal/src/dx12/view.rs b/wgpu-hal/src/dx12/view.rs index 52b3e2f77fc..81e187aaf15 100644 --- a/wgpu-hal/src/dx12/view.rs +++ b/wgpu-hal/src/dx12/view.rs @@ -1,4 +1,4 @@ -use super::conv; +use crate::auxil; use std::mem; use winapi::um::d3d12; @@ -19,8 +19,8 @@ impl crate::TextureViewDescriptor<'_> { pub(super) fn to_internal(&self, texture: &super::Texture) -> ViewDescriptor { ViewDescriptor { dimension: self.dimension, - format: conv::map_texture_format(self.format), - format_nodepth: conv::map_texture_format_nodepth(self.format), + format: auxil::dxgi::conv::map_texture_format(self.format), + format_nodepth: auxil::dxgi::conv::map_texture_format_nodepth(self.format), multisampled: texture.sample_count > 1, mip_level_base: self.range.base_mip_level, mip_level_count: match self.range.mip_level_count {