| 1 |
use alloc::string::String; |
| 2 |
use std::{ffi::OsString, os::windows::ffi::OsStringExt}; |
| 3 |
|
| 4 |
use windows::Win32::Graphics::Dxgi; |
| 5 |
|
| 6 |
// Helper to convert DXGI adapter name to a normal string |
| 7 |
pub fn map_adapter_name(name: [u16; 128]) -> String { |
| 8 |
let len = name.iter().take_while(|&&c| c != 0).count(); |
| 9 |
let name = OsString::from_wide(&name[..len]); |
| 10 |
name.to_string_lossy().into_owned() |
| 11 |
} |
| 12 |
|
| 13 |
pub fn map_texture_format_failable( |
| 14 |
format: wgt::TextureFormat, |
| 15 |
) -> Option<Dxgi::Common::DXGI_FORMAT> { |
| 16 |
use wgt::TextureFormat as Tf; |
| 17 |
use Dxgi::Common::*; |
| 18 |
|
| 19 |
Some(match format { |
| 20 |
Tf::R8Unorm => DXGI_FORMAT_R8_UNORM, |
| 21 |
Tf::R8Snorm => DXGI_FORMAT_R8_SNORM, |
| 22 |
Tf::R8Uint => DXGI_FORMAT_R8_UINT, |
| 23 |
Tf::R8Sint => DXGI_FORMAT_R8_SINT, |
| 24 |
Tf::R16Uint => DXGI_FORMAT_R16_UINT, |
| 25 |
Tf::R16Sint => DXGI_FORMAT_R16_SINT, |
| 26 |
Tf::R16Unorm => DXGI_FORMAT_R16_UNORM, |
| 27 |
Tf::R16Snorm => DXGI_FORMAT_R16_SNORM, |
| 28 |
Tf::R16Float => DXGI_FORMAT_R16_FLOAT, |
| 29 |
Tf::Rg8Unorm => DXGI_FORMAT_R8G8_UNORM, |
| 30 |
Tf::Rg8Snorm => DXGI_FORMAT_R8G8_SNORM, |
| 31 |
Tf::Rg8Uint => DXGI_FORMAT_R8G8_UINT, |
| 32 |
Tf::Rg8Sint => DXGI_FORMAT_R8G8_SINT, |
| 33 |
Tf::Rg16Unorm => DXGI_FORMAT_R16G16_UNORM, |
| 34 |
Tf::Rg16Snorm => DXGI_FORMAT_R16G16_SNORM, |
| 35 |
Tf::R32Uint => DXGI_FORMAT_R32_UINT, |
| 36 |
Tf::R32Sint => DXGI_FORMAT_R32_SINT, |
| 37 |
Tf::R32Float => DXGI_FORMAT_R32_FLOAT, |
| 38 |
Tf::Rg16Uint => DXGI_FORMAT_R16G16_UINT, |
| 39 |
Tf::Rg16Sint => DXGI_FORMAT_R16G16_SINT, |
| 40 |
Tf::Rg16Float => DXGI_FORMAT_R16G16_FLOAT, |
| 41 |
Tf::Rgba8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM, |
| 42 |
Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, |
| 43 |
Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, |
| 44 |
Tf::Rgba8Snorm => DXGI_FORMAT_R8G8B8A8_SNORM, |
| 45 |
Tf::Bgra8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM, |
| 46 |
Tf::Rgba8Uint => DXGI_FORMAT_R8G8B8A8_UINT, |
| 47 |
Tf::Rgba8Sint => DXGI_FORMAT_R8G8B8A8_SINT, |
| 48 |
Tf::Rgb9e5Ufloat => DXGI_FORMAT_R9G9B9E5_SHAREDEXP, |
| 49 |
Tf::Rgb10a2Uint => DXGI_FORMAT_R10G10B10A2_UINT, |
| 50 |
Tf::Rgb10a2Unorm => DXGI_FORMAT_R10G10B10A2_UNORM, |
| 51 |
Tf::Rg11b10Ufloat => DXGI_FORMAT_R11G11B10_FLOAT, |
| 52 |
Tf::R64Uint => DXGI_FORMAT_R32G32_UINT, // R64 emulated by R32G32 |
| 53 |
Tf::Rg32Uint => DXGI_FORMAT_R32G32_UINT, |
| 54 |
Tf::Rg32Sint => DXGI_FORMAT_R32G32_SINT, |
| 55 |
Tf::Rg32Float => DXGI_FORMAT_R32G32_FLOAT, |
| 56 |
Tf::Rgba16Uint => DXGI_FORMAT_R16G16B16A16_UINT, |
| 57 |
Tf::Rgba16Sint => DXGI_FORMAT_R16G16B16A16_SINT, |
| 58 |
Tf::Rgba16Unorm => DXGI_FORMAT_R16G16B16A16_UNORM, |
| 59 |
Tf::Rgba16Snorm => DXGI_FORMAT_R16G16B16A16_SNORM, |
| 60 |
Tf::Rgba16Float => DXGI_FORMAT_R16G16B16A16_FLOAT, |
| 61 |
Tf::Rgba32Uint => DXGI_FORMAT_R32G32B32A32_UINT, |
| 62 |
Tf::Rgba32Sint => DXGI_FORMAT_R32G32B32A32_SINT, |
| 63 |
Tf::Rgba32Float => DXGI_FORMAT_R32G32B32A32_FLOAT, |
| 64 |
Tf::Stencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT, |
| 65 |
Tf::Depth16Unorm => DXGI_FORMAT_D16_UNORM, |
| 66 |
Tf::Depth24Plus => DXGI_FORMAT_D24_UNORM_S8_UINT, |
| 67 |
Tf::Depth24PlusStencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT, |
| 68 |
Tf::Depth32Float => DXGI_FORMAT_D32_FLOAT, |
| 69 |
Tf::Depth32FloatStencil8 => DXGI_FORMAT_D32_FLOAT_S8X24_UINT, |
| 70 |
Tf::NV12 => DXGI_FORMAT_NV12, |
| 71 |
Tf::Bc1RgbaUnorm => DXGI_FORMAT_BC1_UNORM, |
| 72 |
Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_UNORM_SRGB, |
| 73 |
Tf::Bc2RgbaUnorm => DXGI_FORMAT_BC2_UNORM, |
| 74 |
Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_UNORM_SRGB, |
| 75 |
Tf::Bc3RgbaUnorm => DXGI_FORMAT_BC3_UNORM, |
| 76 |
Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_UNORM_SRGB, |
| 77 |
Tf::Bc4RUnorm => DXGI_FORMAT_BC4_UNORM, |
| 78 |
Tf::Bc4RSnorm => DXGI_FORMAT_BC4_SNORM, |
| 79 |
Tf::Bc5RgUnorm => DXGI_FORMAT_BC5_UNORM, |
| 80 |
Tf::Bc5RgSnorm => DXGI_FORMAT_BC5_SNORM, |
| 81 |
Tf::Bc6hRgbUfloat => DXGI_FORMAT_BC6H_UF16, |
| 82 |
Tf::Bc6hRgbFloat => DXGI_FORMAT_BC6H_SF16, |
| 83 |
Tf::Bc7RgbaUnorm => DXGI_FORMAT_BC7_UNORM, |
| 84 |
Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_UNORM_SRGB, |
| 85 |
Tf::Etc2Rgb8Unorm |
| 86 |
| Tf::Etc2Rgb8UnormSrgb |
| 87 |
| Tf::Etc2Rgb8A1Unorm |
| 88 |
| Tf::Etc2Rgb8A1UnormSrgb |
| 89 |
| Tf::Etc2Rgba8Unorm |
| 90 |
| Tf::Etc2Rgba8UnormSrgb |
| 91 |
| Tf::EacR11Unorm |
| 92 |
| Tf::EacR11Snorm |
| 93 |
| Tf::EacRg11Unorm |
| 94 |
| Tf::EacRg11Snorm |
| 95 |
| Tf::Astc { |
| 96 |
block: _, |
| 97 |
channel: _, |
| 98 |
} => return None, |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
pub fn map_texture_format(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT { |
| 103 |
match map_texture_format_failable(format) { |
| 104 |
Some(f) => f, |
| 105 |
None => unreachable!(), |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Note: DXGI doesn't allow sRGB format on the swapchain, |
| 110 |
// but creating RTV of swapchain buffers with sRGB works. |
| 111 |
pub fn map_texture_format_nosrgb(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT { |
| 112 |
match format { |
| 113 |
wgt::TextureFormat::Bgra8UnormSrgb => Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM, |
| 114 |
wgt::TextureFormat::Rgba8UnormSrgb => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, |
| 115 |
_ => map_texture_format(format), |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// SRV and UAV can't use the depth or typeless formats |
| 120 |
// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#view-creation |
| 121 |
pub fn map_texture_format_for_srv_uav( |
| 122 |
format: wgt::TextureFormat, |
| 123 |
aspect: crate::FormatAspects, |
| 124 |
) -> Option<Dxgi::Common::DXGI_FORMAT> { |
| 125 |
Some(match (format, aspect) { |
| 126 |
(wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => { |
| 127 |
Dxgi::Common::DXGI_FORMAT_R16_UNORM |
| 128 |
} |
| 129 |
(wgt::TextureFormat::Depth32Float, crate::FormatAspects::DEPTH) => { |
| 130 |
Dxgi::Common::DXGI_FORMAT_R32_FLOAT |
| 131 |
} |
| 132 |
(wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::DEPTH) => { |
| 133 |
Dxgi::Common::DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS |
| 134 |
} |
| 135 |
( |
| 136 |
wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8, |
| 137 |
crate::FormatAspects::DEPTH, |
| 138 |
) => Dxgi::Common::DXGI_FORMAT_R24_UNORM_X8_TYPELESS, |
| 139 |
|
| 140 |
(wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::STENCIL) => { |
| 141 |
Dxgi::Common::DXGI_FORMAT_X32_TYPELESS_G8X24_UINT |
| 142 |
} |
| 143 |
( |
| 144 |
wgt::TextureFormat::Stencil8 | wgt::TextureFormat::Depth24PlusStencil8, |
| 145 |
crate::FormatAspects::STENCIL, |
| 146 |
) => Dxgi::Common::DXGI_FORMAT_X24_TYPELESS_G8_UINT, |
| 147 |
|
| 148 |
(_, crate::FormatAspects::DEPTH) |
| 149 |
| (_, crate::FormatAspects::STENCIL) |
| 150 |
| (_, crate::FormatAspects::DEPTH_STENCIL) => return None, |
| 151 |
|
| 152 |
_ => map_texture_format(format), |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#planar-layout-for-staging-from-buffer |
| 157 |
pub fn map_texture_format_for_copy( |
| 158 |
format: wgt::TextureFormat, |
| 159 |
aspect: crate::FormatAspects, |
| 160 |
) -> Option<Dxgi::Common::DXGI_FORMAT> { |
| 161 |
Some(match (format, aspect) { |
| 162 |
(wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => { |
| 163 |
Dxgi::Common::DXGI_FORMAT_R16_UNORM |
| 164 |
} |
| 165 |
( |
| 166 |
wgt::TextureFormat::Depth32Float | wgt::TextureFormat::Depth32FloatStencil8, |
| 167 |
crate::FormatAspects::DEPTH, |
| 168 |
) => Dxgi::Common::DXGI_FORMAT_R32_FLOAT, |
| 169 |
|
| 170 |
( |
| 171 |
wgt::TextureFormat::Stencil8 |
| 172 |
| wgt::TextureFormat::Depth24PlusStencil8 |
| 173 |
| wgt::TextureFormat::Depth32FloatStencil8, |
| 174 |
crate::FormatAspects::STENCIL, |
| 175 |
) => Dxgi::Common::DXGI_FORMAT_R8_UINT, |
| 176 |
|
| 177 |
(format, crate::FormatAspects::COLOR) => map_texture_format(format), |
| 178 |
|
| 179 |
_ => return None, |
| 180 |
}) |
| 181 |
} |
| 182 |
|
| 183 |
pub fn map_texture_format_for_resource( |
| 184 |
format: wgt::TextureFormat, |
| 185 |
usage: wgt::TextureUses, |
| 186 |
has_view_formats: bool, |
| 187 |
casting_fully_typed_format_supported: bool, |
| 188 |
) -> Dxgi::Common::DXGI_FORMAT { |
| 189 |
use wgt::TextureFormat as Tf; |
| 190 |
use Dxgi::Common::*; |
| 191 |
|
| 192 |
if casting_fully_typed_format_supported { |
| 193 |
map_texture_format(format) |
| 194 |
|
| 195 |
// We might view this resource as srgb or non-srgb |
| 196 |
} else if has_view_formats { |
| 197 |
match format { |
| 198 |
Tf::Rgba8Unorm | Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_TYPELESS, |
| 199 |
Tf::Bgra8Unorm | Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_TYPELESS, |
| 200 |
Tf::Bc1RgbaUnorm | Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_TYPELESS, |
| 201 |
Tf::Bc2RgbaUnorm | Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_TYPELESS, |
| 202 |
Tf::Bc3RgbaUnorm | Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_TYPELESS, |
| 203 |
Tf::Bc7RgbaUnorm | Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_TYPELESS, |
| 204 |
format => map_texture_format(format), |
| 205 |
} |
| 206 |
|
| 207 |
// We might view this resource as SRV/UAV but also as DSV |
| 208 |
} else if format.is_depth_stencil_format() |
| 209 |
&& usage.intersects( |
| 210 |
wgt::TextureUses::RESOURCE |
| 211 |
| wgt::TextureUses::STORAGE_READ_ONLY |
| 212 |
| wgt::TextureUses::STORAGE_WRITE_ONLY |
| 213 |
| wgt::TextureUses::STORAGE_READ_WRITE, |
| 214 |
) |
| 215 |
{ |
| 216 |
match format { |
| 217 |
Tf::Depth16Unorm => DXGI_FORMAT_R16_TYPELESS, |
| 218 |
Tf::Depth32Float => DXGI_FORMAT_R32_TYPELESS, |
| 219 |
Tf::Depth32FloatStencil8 => DXGI_FORMAT_R32G8X24_TYPELESS, |
| 220 |
Tf::Stencil8 | Tf::Depth24Plus | Tf::Depth24PlusStencil8 => DXGI_FORMAT_R24G8_TYPELESS, |
| 221 |
_ => unreachable!(), |
| 222 |
} |
| 223 |
} else { |
| 224 |
map_texture_format(format) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
pub fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_FORMAT { |
| 229 |
match format { |
| 230 |
wgt::IndexFormat::Uint16 => Dxgi::Common::DXGI_FORMAT_R16_UINT, |
| 231 |
wgt::IndexFormat::Uint32 => Dxgi::Common::DXGI_FORMAT_R32_UINT, |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
pub fn map_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT { |
| 236 |
use wgt::VertexFormat as Vf; |
| 237 |
use Dxgi::Common::*; |
| 238 |
|
| 239 |
match format { |
| 240 |
Vf::Unorm8 => DXGI_FORMAT_R8_UNORM, |
| 241 |
Vf::Snorm8 => DXGI_FORMAT_R8_SNORM, |
| 242 |
Vf::Uint8 => DXGI_FORMAT_R8_UINT, |
| 243 |
Vf::Sint8 => DXGI_FORMAT_R8_SINT, |
| 244 |
Vf::Unorm8x2 => DXGI_FORMAT_R8G8_UNORM, |
| 245 |
Vf::Snorm8x2 => DXGI_FORMAT_R8G8_SNORM, |
| 246 |
Vf::Uint8x2 => DXGI_FORMAT_R8G8_UINT, |
| 247 |
Vf::Sint8x2 => DXGI_FORMAT_R8G8_SINT, |
| 248 |
Vf::Unorm8x4 => DXGI_FORMAT_R8G8B8A8_UNORM, |
| 249 |
Vf::Snorm8x4 => DXGI_FORMAT_R8G8B8A8_SNORM, |
| 250 |
Vf::Uint8x4 => DXGI_FORMAT_R8G8B8A8_UINT, |
| 251 |
Vf::Sint8x4 => DXGI_FORMAT_R8G8B8A8_SINT, |
| 252 |
Vf::Unorm16 => DXGI_FORMAT_R16_UNORM, |
| 253 |
Vf::Snorm16 => DXGI_FORMAT_R16_SNORM, |
| 254 |
Vf::Uint16 => DXGI_FORMAT_R16_UINT, |
| 255 |
Vf::Sint16 => DXGI_FORMAT_R16_SINT, |
| 256 |
Vf::Float16 => DXGI_FORMAT_R16_FLOAT, |
| 257 |
Vf::Unorm16x2 => DXGI_FORMAT_R16G16_UNORM, |
| 258 |
Vf::Snorm16x2 => DXGI_FORMAT_R16G16_SNORM, |
| 259 |
Vf::Uint16x2 => DXGI_FORMAT_R16G16_UINT, |
| 260 |
Vf::Sint16x2 => DXGI_FORMAT_R16G16_SINT, |
| 261 |
Vf::Float16x2 => DXGI_FORMAT_R16G16_FLOAT, |
| 262 |
Vf::Unorm16x4 => DXGI_FORMAT_R16G16B16A16_UNORM, |
| 263 |
Vf::Snorm16x4 => DXGI_FORMAT_R16G16B16A16_SNORM, |
| 264 |
Vf::Uint16x4 => DXGI_FORMAT_R16G16B16A16_UINT, |
| 265 |
Vf::Sint16x4 => DXGI_FORMAT_R16G16B16A16_SINT, |
| 266 |
Vf::Float16x4 => DXGI_FORMAT_R16G16B16A16_FLOAT, |
| 267 |
Vf::Uint32 => DXGI_FORMAT_R32_UINT, |
| 268 |
Vf::Sint32 => DXGI_FORMAT_R32_SINT, |
| 269 |
Vf::Float32 => DXGI_FORMAT_R32_FLOAT, |
| 270 |
Vf::Uint32x2 => DXGI_FORMAT_R32G32_UINT, |
| 271 |
Vf::Sint32x2 => DXGI_FORMAT_R32G32_SINT, |
| 272 |
Vf::Float32x2 => DXGI_FORMAT_R32G32_FLOAT, |
| 273 |
Vf::Uint32x3 => DXGI_FORMAT_R32G32B32_UINT, |
| 274 |
Vf::Sint32x3 => DXGI_FORMAT_R32G32B32_SINT, |
| 275 |
Vf::Float32x3 => DXGI_FORMAT_R32G32B32_FLOAT, |
| 276 |
Vf::Uint32x4 => DXGI_FORMAT_R32G32B32A32_UINT, |
| 277 |
Vf::Sint32x4 => DXGI_FORMAT_R32G32B32A32_SINT, |
| 278 |
Vf::Float32x4 => DXGI_FORMAT_R32G32B32A32_FLOAT, |
| 279 |
Vf::Unorm10_10_10_2 => DXGI_FORMAT_R10G10B10A2_UNORM, |
| 280 |
Vf::Unorm8x4Bgra => DXGI_FORMAT_B8G8R8A8_UNORM, |
| 281 |
Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
pub fn map_acomposite_alpha_mode(mode: wgt::CompositeAlphaMode) -> Dxgi::Common::DXGI_ALPHA_MODE { |
| 286 |
match mode { |
| 287 |
wgt::CompositeAlphaMode::PreMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_PREMULTIPLIED, |
| 288 |
wgt::CompositeAlphaMode::PostMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_STRAIGHT, |
| 289 |
wgt::CompositeAlphaMode::Opaque => Dxgi::Common::DXGI_ALPHA_MODE_IGNORE, |
| 290 |
wgt::CompositeAlphaMode::Auto | wgt::CompositeAlphaMode::Inherit => { |
| 291 |
Dxgi::Common::DXGI_ALPHA_MODE_UNSPECIFIED |
| 292 |
} |
| 293 |
} |
| 294 |
} |