Windows zero-copy encode: HwEncoder::encode_d3d11 / EncodeInput::D3d11 take a shared D3D11 BGRA/RGBA texture (NT handle, VK_KHR_external_memory_win32 D3D11_TEXTURE import, dedicated allocation, cached per handle like DMA-BUF imports) into the same GPU RGB->NV12 path; d3d11_supported(order) reports importability. Compile-checked for x86_64-pc-windows-msvc only: no Windows GPU to run it on
Browse files at this commit301ebce0bb3291a44812a75ccce8f523834957aeParent: 1b795c612 changed files +480 -18
- crates/ec-hw/src/hw.rs +63 -0
- crates/ec-hw/src/lib.rs +2 -0
- crates/ec-vk/src/av1.rs +26 -0
- crates/ec-vk/src/h265.rs +26 -0
- crates/ec-vk/src/lib.rs +26 -0
- shims/vk-video-ffi/src/av1_encode.rs +21 -0
- shims/vk-video-ffi/src/d3d11.rs +227 -0
- shims/vk-video-ffi/src/h264_encode.rs +21 -0
- shims/vk-video-ffi/src/h265_encode.rs +21 -0
- shims/vk-video-ffi/src/lib.rs +6 -1
- shims/vk-video-ffi/src/rgb_convert.rs +11 -7
- shims/vk-video-ffi/src/session.rs +30 -10
crates/ec-hw/src/hw.rsmodified+63 -0
| @@ -341,6 +341,40 @@ pub fn dup_fd(fd: std::os::fd::RawFd) -> Result<std::os::fd::OwnedFd> { | ||
| 341 | 341 | Ok(ec_vk::dup_raw_fd(fd)?) |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | +/// A D3D11 texture of packed 32-bit RGB pixels (DXGI_FORMAT_B8G8R8A8_UNORM or R8G8B8A8_UNORM, one mip, one layer) shared as an NT handle from `IDXGIResource1::CreateSharedHandle`. | |
| 345 | +/// | |
| 346 | +/// The encoder's import cache keys on the handle value: keep one handle open per texture for as long as frames come from it. The D3D11 writes must be finished before the call, and the texture left unwritten until its coded picture is returned. | |
| 347 | +#[cfg(windows)] | |
| 348 | +#[derive(Debug, Clone, Copy)] | |
| 349 | +pub struct D3d11Frame<'a> { | |
| 350 | + /// Shared NT handle; borrowed, not closed. | |
| 351 | + pub handle: std::os::windows::io::BorrowedHandle<'a>, | |
| 352 | + /// Width in pixels, even; at least the encoder width. | |
| 353 | + pub width: u32, | |
| 354 | + /// Height in pixels, even; at least the encoder height. | |
| 355 | + pub height: u32, | |
| 356 | + /// Byte order of each pixel. | |
| 357 | + pub order: RgbOrder, | |
| 358 | +} | |
| 359 | + | |
| 360 | +#[cfg(windows)] | |
| 361 | +impl D3d11Frame<'_> { | |
| 362 | + fn texture(&self) -> ec_vk::D3d11Texture<'_> { | |
| 363 | + ec_vk::D3d11Texture { | |
| 364 | + handle: self.handle, | |
| 365 | + width: self.width, | |
| 366 | + height: self.height, | |
| 367 | + bgr: self.order == RgbOrder::Bgra, | |
| 368 | + } | |
| 369 | + } | |
| 370 | +} | |
| 371 | + | |
| 372 | +/// Whether [`HwEncoder::encode_d3d11`] can import textures of `order` on this machine's Vulkan device. | |
| 373 | +#[cfg(windows)] | |
| 374 | +pub fn d3d11_supported(order: RgbOrder) -> bool { | |
| 375 | + vk_gpu().is_some_and(|gpu| ec_vk::d3d11_importable(&gpu, order == RgbOrder::Bgra)) | |
| 376 | +} | |
| 377 | + | |
| 344 | 378 | /// One picture for [`HwEncoder::submit`]. |
| 345 | 379 | #[derive(Debug, Clone, Copy)] |
| 346 | 380 | pub enum EncodeInput<'a> { |
| @@ -356,6 +390,9 @@ pub enum EncodeInput<'a> { | ||
| 356 | 390 | /// DMA-BUF of NV12 or P010 at the encoder size, Vulkan only. |
| 357 | 391 | #[cfg(target_os = "linux")] |
| 358 | 392 | DmaBufYuv(&'a DmaBufYuvFrame<'a>), |
| 393 | + /// Shared D3D11 texture of packed RGB, Vulkan only. | |
| 394 | + #[cfg(windows)] | |
| 395 | + D3d11(&'a D3d11Frame<'a>), | |
| 359 | 396 | } |
| 360 | 397 | |
| 361 | 398 | #[cfg(target_os = "linux")] |
| @@ -483,6 +520,15 @@ impl VkEncoder { | ||
| 483 | 520 | }; |
| 484 | 521 | self.submit_dma(&src, force_keyframe) |
| 485 | 522 | } |
| 523 | + #[cfg(windows)] | |
| 524 | + EncodeInput::D3d11(frame) => { | |
| 525 | + let src = frame.texture(); | |
| 526 | + match self { | |
| 527 | + VkEncoder::H264(e) => e.submit_d3d11(&src, force_keyframe), | |
| 528 | + VkEncoder::H265(e) => e.submit_d3d11(&src, force_keyframe), | |
| 529 | + VkEncoder::Av1(e) => e.submit_d3d11(&src, force_keyframe), | |
| 530 | + } | |
| 531 | + } | |
| 486 | 532 | } |
| 487 | 533 | } |
| 488 | 534 | |
| @@ -859,6 +905,23 @@ impl HwEncoder { | ||
| 859 | 905 | self.encode_dma(&src, meta) |
| 860 | 906 | } |
| 861 | 907 | |
| 908 | + /// Encode one shared D3D11 texture of the source size, downscaled to the encoder size on the GPU. | |
| 909 | + #[cfg(windows)] | |
| 910 | + pub fn encode_d3d11(&mut self, frame: &D3d11Frame, meta: FrameMetadata) -> Result<CodedFrame> { | |
| 911 | + let meta = self.with_recovery(meta); | |
| 912 | + self.idle()?; | |
| 913 | + let EncInner::Vk(enc) = &mut self.inner; | |
| 914 | + vk_session_alive(self.born_epoch)?; | |
| 915 | + let src = frame.texture(); | |
| 916 | + let coded = match enc { | |
| 917 | + VkEncoder::H264(e) => e.encode_d3d11(&src, meta.force_keyframe), | |
| 918 | + VkEncoder::H265(e) => e.encode_d3d11(&src, meta.force_keyframe), | |
| 919 | + VkEncoder::Av1(e) => e.encode_d3d11(&src, meta.force_keyframe), | |
| 920 | + }?; | |
| 921 | + self.steer(coded.data.len())?; | |
| 922 | + Ok(self.vk_frame(coded, meta.timestamp)) | |
| 923 | + } | |
| 924 | + | |
| 862 | 925 | #[cfg(target_os = "linux")] |
| 863 | 926 | fn encode_dma(&mut self, src: &DmaBuf, meta: FrameMetadata) -> Result<CodedFrame> { |
| 864 | 927 | let meta = self.with_recovery(meta); |
crates/ec-hw/src/lib.rsmodified+2 -0
| @@ -92,6 +92,8 @@ pub use error::{Error, Result}; | ||
| 92 | 92 | pub use ec_vk::{DmaBufFormat, DmaBufPlane}; |
| 93 | 93 | #[cfg(target_os = "linux")] |
| 94 | 94 | pub use hw::{DmaBufFrame, DmaBufYuvFrame, dmabuf_modifiers, dup_fd}; |
| 95 | +#[cfg(windows)] | |
| 96 | +pub use hw::{D3d11Frame, d3d11_supported}; | |
| 95 | 97 | pub use hw::{ |
| 96 | 98 | Backend, Capability, CodecProfile, Direction, EncodeInput, HwDecoder, HwEncoder, RgbFrame, |
| 97 | 99 | RgbOrder, RgbOutput, capabilities, |
crates/ec-vk/src/av1.rsmodified+26 -0
| @@ -1,5 +1,7 @@ | ||
| 1 | 1 | use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 2 | 2 | |
| 3 | +#[cfg(windows)] | |
| 4 | +use crate::D3d11Texture; | |
| 3 | 5 | #[cfg(target_os = "linux")] |
| 4 | 6 | use crate::DmaBuf; |
| 5 | 7 | use crate::history::History; |
| @@ -191,6 +193,30 @@ impl Av1Encoder { | ||
| 191 | 193 | last_frame(first, rest) |
| 192 | 194 | } |
| 193 | 195 | |
| 196 | + /// Submits one shared D3D11 texture of the source size without waiting. | |
| 197 | + #[cfg(windows)] | |
| 198 | + pub fn submit_d3d11( | |
| 199 | + &mut self, | |
| 200 | + src: &D3d11Texture<'_>, | |
| 201 | + force_keyframe: bool, | |
| 202 | + ) -> Result<Option<CodedFrame>, Error> { | |
| 203 | + let pic = self.next_picture(force_keyframe); | |
| 204 | + let out = self.session.submit_d3d11(src, &pic)?; | |
| 205 | + Ok(out.map(|bytes| self.coded(bytes))) | |
| 206 | + } | |
| 207 | + | |
| 208 | + /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | |
| 209 | + #[cfg(windows)] | |
| 210 | + pub fn encode_d3d11( | |
| 211 | + &mut self, | |
| 212 | + src: &D3d11Texture<'_>, | |
| 213 | + force_keyframe: bool, | |
| 214 | + ) -> Result<CodedFrame, Error> { | |
| 215 | + let first = self.submit_d3d11(src, force_keyframe)?; | |
| 216 | + let rest = self.flush()?; | |
| 217 | + last_frame(first, rest) | |
| 218 | + } | |
| 219 | + | |
| 194 | 220 | /// Submits one DMA-BUF frame of the source size without waiting. |
| 195 | 221 | #[cfg(target_os = "linux")] |
| 196 | 222 | pub fn submit_dmabuf( |
crates/ec-vk/src/h265.rsmodified+26 -0
| @@ -1,5 +1,7 @@ | ||
| 1 | 1 | use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; |
| 2 | 2 | |
| 3 | +#[cfg(windows)] | |
| 4 | +use crate::D3d11Texture; | |
| 3 | 5 | #[cfg(target_os = "linux")] |
| 4 | 6 | use crate::DmaBuf; |
| 5 | 7 | use crate::history::History; |
| @@ -189,6 +191,30 @@ impl H265Encoder { | ||
| 189 | 191 | last_frame(first, rest) |
| 190 | 192 | } |
| 191 | 193 | |
| 194 | + /// Submits one shared D3D11 texture of the source size without waiting. | |
| 195 | + #[cfg(windows)] | |
| 196 | + pub fn submit_d3d11( | |
| 197 | + &mut self, | |
| 198 | + src: &D3d11Texture<'_>, | |
| 199 | + force_keyframe: bool, | |
| 200 | + ) -> Result<Option<CodedFrame>, Error> { | |
| 201 | + let pic = self.next_picture(force_keyframe); | |
| 202 | + let out = self.session.submit_d3d11(src, &pic)?; | |
| 203 | + Ok(out.map(|bytes| self.coded(bytes))) | |
| 204 | + } | |
| 205 | + | |
| 206 | + /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | |
| 207 | + #[cfg(windows)] | |
| 208 | + pub fn encode_d3d11( | |
| 209 | + &mut self, | |
| 210 | + src: &D3d11Texture<'_>, | |
| 211 | + force_keyframe: bool, | |
| 212 | + ) -> Result<CodedFrame, Error> { | |
| 213 | + let first = self.submit_d3d11(src, force_keyframe)?; | |
| 214 | + let rest = self.flush()?; | |
| 215 | + last_frame(first, rest) | |
| 216 | + } | |
| 217 | + | |
| 192 | 218 | /// Submits one DMA-BUF frame of the source size without waiting. |
| 193 | 219 | #[cfg(target_os = "linux")] |
| 194 | 220 | pub fn submit_dmabuf( |
crates/ec-vk/src/lib.rsmodified+26 -0
| @@ -13,6 +13,8 @@ mod history; | ||
| 13 | 13 | |
| 14 | 14 | pub use av1::{Av1Encoder, Av1EncoderConfig}; |
| 15 | 15 | pub use h265::{H265Encoder, H265EncoderConfig}; |
| 16 | +#[cfg(windows)] | |
| 17 | +pub use vk_video_ffi::{D3d11Texture, d3d11_importable}; | |
| 16 | 18 | #[cfg(target_os = "linux")] |
| 17 | 19 | pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; |
| 18 | 20 | pub use vk_video_ffi::{ |
| @@ -223,6 +225,30 @@ impl H264Encoder { | ||
| 223 | 225 | last_frame(first, rest) |
| 224 | 226 | } |
| 225 | 227 | |
| 228 | + /// Submits one shared D3D11 texture of the source size without waiting. | |
| 229 | + #[cfg(windows)] | |
| 230 | + pub fn submit_d3d11( | |
| 231 | + &mut self, | |
| 232 | + src: &D3d11Texture<'_>, | |
| 233 | + force_keyframe: bool, | |
| 234 | + ) -> Result<Option<CodedFrame>, Error> { | |
| 235 | + let pic = self.next_picture(force_keyframe); | |
| 236 | + let out = self.session.submit_d3d11(src, &pic)?; | |
| 237 | + Ok(out.map(|bytes| self.coded(bytes))) | |
| 238 | + } | |
| 239 | + | |
| 240 | + /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | |
| 241 | + #[cfg(windows)] | |
| 242 | + pub fn encode_d3d11( | |
| 243 | + &mut self, | |
| 244 | + src: &D3d11Texture<'_>, | |
| 245 | + force_keyframe: bool, | |
| 246 | + ) -> Result<CodedFrame, Error> { | |
| 247 | + let first = self.submit_d3d11(src, force_keyframe)?; | |
| 248 | + let rest = self.flush()?; | |
| 249 | + last_frame(first, rest) | |
| 250 | + } | |
| 251 | + | |
| 226 | 252 | /// Submits one DMA-BUF frame of the source size without waiting. |
| 227 | 253 | #[cfg(target_os = "linux")] |
| 228 | 254 | pub fn submit_dmabuf( |
shims/vk-video-ffi/src/av1_encode.rsmodified+21 -0
| @@ -294,6 +294,27 @@ impl Av1EncodeSession { | ||
| 294 | 294 | self.encode_uploaded(pic) |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | + /// Encodes one picture from a shared D3D11 texture of BGRA or RGBA pixels, downscaled to the encoder size. | |
| 298 | + #[cfg(windows)] | |
| 299 | + pub fn submit_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &Av1Picture) -> Result<Option<Vec<u8>>, Error> { | |
| 300 | + self.check(pic)?; | |
| 301 | + self.enc.upload_d3d11(src)?; | |
| 302 | + self.encode_uploaded(pic) | |
| 303 | + } | |
| 304 | + | |
| 305 | + /// Encodes one picture from a shared D3D11 texture synchronously and returns its coded bytes. | |
| 306 | + #[cfg(windows)] | |
| 307 | + pub fn encode_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &Av1Picture) -> Result<Vec<u8>, Error> { | |
| 308 | + self.submit_d3d11(src, pic)?; | |
| 309 | + self.enc.flush_last() | |
| 310 | + } | |
| 311 | + | |
| 312 | + /// Number of D3D11 texture imports made so far; a texture re-used by handle is imported once. | |
| 313 | + #[cfg(windows)] | |
| 314 | + pub fn d3d11_imports(&self) -> usize { | |
| 315 | + self.enc.import_count() | |
| 316 | + } | |
| 317 | + | |
| 297 | 318 | /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once. |
| 298 | 319 | #[cfg(target_os = "linux")] |
| 299 | 320 | pub fn dmabuf_imports(&self) -> usize { |
shims/vk-video-ffi/src/d3d11.rsadded+227 -0
| @@ -0,0 +1,227 @@ | ||
| 1 | +use std::os::windows::io::{AsRawHandle, BorrowedHandle}; | |
| 2 | + | |
| 3 | +use vulkanalia::prelude::v1_3::*; | |
| 4 | +use vulkanalia::vk::KhrExternalMemoryWin32ExtensionDeviceCommands; | |
| 5 | + | |
| 6 | +use crate::session::{memory_type, vk}; | |
| 7 | +use crate::{Error, Gpu}; | |
| 8 | + | |
| 9 | +const CACHE_SIZE: usize = 16; | |
| 10 | +const HANDLE_TYPE: vk::ExternalMemoryHandleTypeFlags = | |
| 11 | + vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE; | |
| 12 | + | |
| 13 | +/// A D3D11 texture of DXGI_FORMAT_B8G8R8A8_UNORM or R8G8B8A8_UNORM pixels shared as an NT handle (`IDXGIResource1::CreateSharedHandle`), one mip and one array layer. | |
| 14 | +/// | |
| 15 | +/// The import cache keys on the handle value: keep one handle open per texture while sending frames from it. The D3D11 writes must be complete (flushed and finished) before the call, and the texture must not be written again until the coded picture comes back. | |
| 16 | +#[derive(Debug, Clone, Copy)] | |
| 17 | +pub struct D3d11Texture<'a> { | |
| 18 | + /// Shared NT handle of the texture; borrowed, not closed. | |
| 19 | + pub handle: BorrowedHandle<'a>, | |
| 20 | + /// Width in pixels. | |
| 21 | + pub width: u32, | |
| 22 | + /// Height in pixels. | |
| 23 | + pub height: u32, | |
| 24 | + /// B8G8R8A8 when true, R8G8B8A8 otherwise. | |
| 25 | + pub bgr: bool, | |
| 26 | +} | |
| 27 | + | |
| 28 | +fn format(bgr: bool) -> vk::Format { | |
| 29 | + if bgr { | |
| 30 | + vk::Format::B8G8R8A8_UNORM | |
| 31 | + } else { | |
| 32 | + vk::Format::R8G8B8A8_UNORM | |
| 33 | + } | |
| 34 | +} | |
| 35 | + | |
| 36 | +/// Whether a [`D3d11Texture`] of this byte order can be imported on `gpu`. | |
| 37 | +pub fn d3d11_importable(gpu: &Gpu, bgr: bool) -> bool { | |
| 38 | + if !gpu | |
| 39 | + .device() | |
| 40 | + .extensions() | |
| 41 | + .contains(&vk::KHR_EXTERNAL_MEMORY_WIN32_EXTENSION.name) | |
| 42 | + { | |
| 43 | + return false; | |
| 44 | + } | |
| 45 | + let mut external_info = vk::PhysicalDeviceExternalImageFormatInfo::builder() | |
| 46 | + .handle_type(HANDLE_TYPE) | |
| 47 | + .build(); | |
| 48 | + let info = vk::PhysicalDeviceImageFormatInfo2::builder() | |
| 49 | + .format(format(bgr)) | |
| 50 | + .type_(vk::ImageType::_2D) | |
| 51 | + .tiling(vk::ImageTiling::OPTIMAL) | |
| 52 | + .usage(vk::ImageUsageFlags::TRANSFER_SRC) | |
| 53 | + .push_next(&mut external_info) | |
| 54 | + .build(); | |
| 55 | + let mut external = vk::ExternalImageFormatProperties::builder().build(); | |
| 56 | + let mut props = vk::ImageFormatProperties2::builder() | |
| 57 | + .push_next(&mut external) | |
| 58 | + .build(); | |
| 59 | + let ok = unsafe { | |
| 60 | + gpu.instance() | |
| 61 | + .get_physical_device_image_format_properties2(gpu.physical(), &info, &mut props) | |
| 62 | + .is_ok() | |
| 63 | + }; | |
| 64 | + ok && external | |
| 65 | + .external_memory_properties | |
| 66 | + .external_memory_features | |
| 67 | + .contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE) | |
| 68 | +} | |
| 69 | + | |
| 70 | +#[derive(PartialEq, Eq)] | |
| 71 | +struct Key { | |
| 72 | + handle: usize, | |
| 73 | + width: u32, | |
| 74 | + height: u32, | |
| 75 | + bgr: bool, | |
| 76 | +} | |
| 77 | + | |
| 78 | +impl Key { | |
| 79 | + fn new(src: &D3d11Texture) -> Self { | |
| 80 | + Key { | |
| 81 | + handle: src.handle.as_raw_handle() as usize, | |
| 82 | + width: src.width, | |
| 83 | + height: src.height, | |
| 84 | + bgr: src.bgr, | |
| 85 | + } | |
| 86 | + } | |
| 87 | +} | |
| 88 | + | |
| 89 | +struct Import { | |
| 90 | + image: vk::Image, | |
| 91 | + memory: vk::DeviceMemory, | |
| 92 | + id: u64, | |
| 93 | +} | |
| 94 | + | |
| 95 | +impl Import { | |
| 96 | + fn new(gpu: &Gpu, src: &D3d11Texture, id: u64) -> Result<Self, Error> { | |
| 97 | + if !d3d11_importable(gpu, src.bgr) { | |
| 98 | + return Err(Error::Unsupported("D3D11 texture import".into())); | |
| 99 | + } | |
| 100 | + if src.width == 0 || src.height == 0 { | |
| 101 | + return Err(Error::Invalid("D3D11 texture size".into())); | |
| 102 | + } | |
| 103 | + let d = gpu.device(); | |
| 104 | + let mut external = vk::ExternalMemoryImageCreateInfo::builder() | |
| 105 | + .handle_types(HANDLE_TYPE) | |
| 106 | + .build(); | |
| 107 | + let info = vk::ImageCreateInfo::builder() | |
| 108 | + .image_type(vk::ImageType::_2D) | |
| 109 | + .format(format(src.bgr)) | |
| 110 | + .extent(vk::Extent3D { | |
| 111 | + width: src.width, | |
| 112 | + height: src.height, | |
| 113 | + depth: 1, | |
| 114 | + }) | |
| 115 | + .mip_levels(1) | |
| 116 | + .array_layers(1) | |
| 117 | + .samples(vk::SampleCountFlags::_1) | |
| 118 | + .tiling(vk::ImageTiling::OPTIMAL) | |
| 119 | + .usage(vk::ImageUsageFlags::TRANSFER_SRC) | |
| 120 | + .sharing_mode(vk::SharingMode::EXCLUSIVE) | |
| 121 | + .initial_layout(vk::ImageLayout::UNDEFINED) | |
| 122 | + .push_next(&mut external) | |
| 123 | + .build(); | |
| 124 | + let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? }; | |
| 125 | + match import_memory(gpu, image, src.handle) { | |
| 126 | + Ok(memory) => Ok(Import { image, memory, id }), | |
| 127 | + Err(e) => { | |
| 128 | + unsafe { d.destroy_image(image, None) }; | |
| 129 | + Err(e) | |
| 130 | + } | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + fn destroy(&self, d: &Device) { | |
| 135 | + unsafe { | |
| 136 | + d.destroy_image(self.image, None); | |
| 137 | + d.free_memory(self.memory, None); | |
| 138 | + } | |
| 139 | + } | |
| 140 | +} | |
| 141 | + | |
| 142 | +fn import_memory( | |
| 143 | + gpu: &Gpu, | |
| 144 | + image: vk::Image, | |
| 145 | + handle: BorrowedHandle, | |
| 146 | +) -> Result<vk::DeviceMemory, Error> { | |
| 147 | + let d = gpu.device(); | |
| 148 | + let req = unsafe { d.get_image_memory_requirements(image) }; | |
| 149 | + let raw = handle.as_raw_handle(); | |
| 150 | + let mut props = vk::MemoryWin32HandlePropertiesKHR::default(); | |
| 151 | + let bits = | |
| 152 | + match unsafe { d.get_memory_win32_handle_properties_khr(HANDLE_TYPE, raw, &mut props) } { | |
| 153 | + Ok(()) => req.memory_type_bits & props.memory_type_bits, | |
| 154 | + Err(_) => req.memory_type_bits, | |
| 155 | + }; | |
| 156 | + let index = memory_type(gpu, bits, vk::MemoryPropertyFlags::empty())?; | |
| 157 | + let mut import = vk::ImportMemoryWin32HandleInfoKHR::builder() | |
| 158 | + .handle_type(HANDLE_TYPE) | |
| 159 | + .handle(raw) | |
| 160 | + .build(); | |
| 161 | + let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder() | |
| 162 | + .image(image) | |
| 163 | + .build(); | |
| 164 | + let alloc = vk::MemoryAllocateInfo::builder() | |
| 165 | + .allocation_size(req.size) | |
| 166 | + .memory_type_index(index) | |
| 167 | + .push_next(&mut dedicated) | |
| 168 | + .push_next(&mut import) | |
| 169 | + .build(); | |
| 170 | + let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? }; | |
| 171 | + if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } { | |
| 172 | + unsafe { d.free_memory(memory, None) }; | |
| 173 | + return Err(e); | |
| 174 | + } | |
| 175 | + Ok(memory) | |
| 176 | +} | |
| 177 | + | |
| 178 | +#[derive(Default)] | |
| 179 | +pub(crate) struct ImportCache { | |
| 180 | + entries: Vec<(Key, Import)>, | |
| 181 | + next_id: u64, | |
| 182 | + pub(crate) count: usize, | |
| 183 | +} | |
| 184 | + | |
| 185 | +impl ImportCache { | |
| 186 | + pub(crate) fn get( | |
| 187 | + &mut self, | |
| 188 | + gpu: &Gpu, | |
| 189 | + src: &D3d11Texture, | |
| 190 | + busy: &[u64], | |
| 191 | + ) -> Result<(vk::Image, u64), Error> { | |
| 192 | + let key = Key::new(src); | |
| 193 | + match self.entries.iter().position(|(k, _)| *k == key) { | |
| 194 | + Some(i) => { | |
| 195 | + let entry = self.entries.remove(i); | |
| 196 | + self.entries.push(entry); | |
| 197 | + } | |
| 198 | + None => { | |
| 199 | + if self.entries.len() == CACHE_SIZE { | |
| 200 | + let i = self | |
| 201 | + .entries | |
| 202 | + .iter() | |
| 203 | + .position(|(_, import)| !busy.contains(&import.id)) | |
| 204 | + .ok_or_else(|| { | |
| 205 | + Error::Invalid("D3D11 import cache is full of in-flight imports".into()) | |
| 206 | + })?; | |
| 207 | + let (_, old) = self.entries.remove(i); | |
| 208 | + old.destroy(gpu.device()); | |
| 209 | + } | |
| 210 | + self.next_id += 1; | |
| 211 | + let import = Import::new(gpu, src, self.next_id)?; | |
| 212 | + self.count += 1; | |
| 213 | + self.entries.push((key, import)); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + self.entries | |
| 217 | + .last() | |
| 218 | + .map(|(_, import)| (import.image, import.id)) | |
| 219 | + .ok_or_else(|| Error::Invalid("D3D11 import cache".into())) | |
| 220 | + } | |
| 221 | + | |
| 222 | + pub(crate) fn destroy(&self, d: &Device) { | |
| 223 | + for (_, import) in &self.entries { | |
| 224 | + import.destroy(d); | |
| 225 | + } | |
| 226 | + } | |
| 227 | +} |
shims/vk-video-ffi/src/h264_encode.rsmodified+21 -0
| @@ -391,6 +391,27 @@ impl H264EncodeSession { | ||
| 391 | 391 | self.encode_uploaded(pic) |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | + /// Encodes one picture from a shared D3D11 texture of BGRA or RGBA pixels, downscaled to the encoder size. | |
| 395 | + #[cfg(windows)] | |
| 396 | + pub fn submit_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &H264Picture) -> Result<Option<Vec<u8>>, Error> { | |
| 397 | + self.check(pic)?; | |
| 398 | + self.enc.upload_d3d11(src)?; | |
| 399 | + self.encode_uploaded(pic) | |
| 400 | + } | |
| 401 | + | |
| 402 | + /// Encodes one picture from a shared D3D11 texture synchronously and returns its coded bytes. | |
| 403 | + #[cfg(windows)] | |
| 404 | + pub fn encode_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &H264Picture) -> Result<Vec<u8>, Error> { | |
| 405 | + self.submit_d3d11(src, pic)?; | |
| 406 | + self.enc.flush_last() | |
| 407 | + } | |
| 408 | + | |
| 409 | + /// Number of D3D11 texture imports made so far; a texture re-used by handle is imported once. | |
| 410 | + #[cfg(windows)] | |
| 411 | + pub fn d3d11_imports(&self) -> usize { | |
| 412 | + self.enc.import_count() | |
| 413 | + } | |
| 414 | + | |
| 394 | 415 | /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once. |
| 395 | 416 | #[cfg(target_os = "linux")] |
| 396 | 417 | pub fn dmabuf_imports(&self) -> usize { |
shims/vk-video-ffi/src/h265_encode.rsmodified+21 -0
| @@ -336,6 +336,27 @@ impl H265EncodeSession { | ||
| 336 | 336 | self.encode_uploaded(pic) |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | + /// Encodes one picture from a shared D3D11 texture of BGRA or RGBA pixels, downscaled to the encoder size. | |
| 340 | + #[cfg(windows)] | |
| 341 | + pub fn submit_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &H265Picture) -> Result<Option<Vec<u8>>, Error> { | |
| 342 | + self.check(pic)?; | |
| 343 | + self.enc.upload_d3d11(src)?; | |
| 344 | + self.encode_uploaded(pic) | |
| 345 | + } | |
| 346 | + | |
| 347 | + /// Encodes one picture from a shared D3D11 texture synchronously and returns its coded bytes. | |
| 348 | + #[cfg(windows)] | |
| 349 | + pub fn encode_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &H265Picture) -> Result<Vec<u8>, Error> { | |
| 350 | + self.submit_d3d11(src, pic)?; | |
| 351 | + self.enc.flush_last() | |
| 352 | + } | |
| 353 | + | |
| 354 | + /// Number of D3D11 texture imports made so far; a texture re-used by handle is imported once. | |
| 355 | + #[cfg(windows)] | |
| 356 | + pub fn d3d11_imports(&self) -> usize { | |
| 357 | + self.enc.import_count() | |
| 358 | + } | |
| 359 | + | |
| 339 | 360 | /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once. |
| 340 | 361 | #[cfg(target_os = "linux")] |
| 341 | 362 | pub fn dmabuf_imports(&self) -> usize { |
shims/vk-video-ffi/src/lib.rsmodified+6 -1
| @@ -8,6 +8,8 @@ use std::sync::Arc; | ||
| 8 | 8 | |
| 9 | 9 | mod av1_decode; |
| 10 | 10 | mod av1_encode; |
| 11 | +#[cfg(windows)] | |
| 12 | +mod d3d11; | |
| 11 | 13 | #[cfg(target_os = "linux")] |
| 12 | 14 | mod dmabuf; |
| 13 | 15 | mod h264_decode; |
| @@ -27,6 +29,8 @@ pub use av1_decode::{ | ||
| 27 | 29 | }; |
| 28 | 30 | pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 29 | 31 | pub use session::DPB_SLOTS; |
| 32 | +#[cfg(windows)] | |
| 33 | +pub use d3d11::{D3d11Texture, d3d11_importable}; | |
| 30 | 34 | #[cfg(target_os = "linux")] |
| 31 | 35 | pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; |
| 32 | 36 | pub use vp9_decode::{ |
| @@ -65,7 +69,7 @@ use vk::KhrVideoQueueExtensionInstanceCommands; | ||
| 65 | 69 | use vulkanalia::loader::LibloadingLoader; |
| 66 | 70 | use vulkanalia::prelude::v1_3::*; |
| 67 | 71 | |
| 68 | -const WANTED: [vk::Extension; 15] = [ | |
| 72 | +const WANTED: [vk::Extension; 16] = [ | |
| 69 | 73 | vk::KHR_VIDEO_QUEUE_EXTENSION, |
| 70 | 74 | vk::KHR_VIDEO_ENCODE_QUEUE_EXTENSION, |
| 71 | 75 | vk::KHR_VIDEO_DECODE_QUEUE_EXTENSION, |
| @@ -81,6 +85,7 @@ const WANTED: [vk::Extension; 15] = [ | ||
| 81 | 85 | vk::KHR_EXTERNAL_MEMORY_FD_EXTENSION, |
| 82 | 86 | vk::EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION, |
| 83 | 87 | vk::EXT_QUEUE_FAMILY_FOREIGN_EXTENSION, |
| 88 | + vk::KHR_EXTERNAL_MEMORY_WIN32_EXTENSION, | |
| 84 | 89 | ]; |
| 85 | 90 | |
| 86 | 91 | /// Errors from opening a GPU or probing it. |
shims/vk-video-ffi/src/rgb_convert.rsmodified+11 -7
| @@ -22,8 +22,8 @@ pub(crate) enum Input<'a> { | ||
| 22 | 22 | stride: usize, |
| 23 | 23 | bgr: bool, |
| 24 | 24 | }, |
| 25 | - #[cfg(target_os = "linux")] | |
| 26 | - DmaBuf { | |
| 25 | + #[cfg(any(target_os = "linux", windows))] | |
| 26 | + Imported { | |
| 27 | 27 | image: vk::Image, |
| 28 | 28 | width: usize, |
| 29 | 29 | height: usize, |
| @@ -36,16 +36,16 @@ impl Input<'_> { | ||
| 36 | 36 | match *self { |
| 37 | 37 | Input::Nv12 { .. } | Input::P010 { .. } => None, |
| 38 | 38 | Input::Rgb { bgr, .. } => Some(bgr), |
| 39 | - #[cfg(target_os = "linux")] | |
| 40 | - Input::DmaBuf { bgr, .. } => Some(bgr), | |
| 39 | + #[cfg(any(target_os = "linux", windows))] | |
| 40 | + Input::Imported { bgr, .. } => Some(bgr), | |
| 41 | 41 | } |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | pub(crate) fn source_image(&self) -> Option<vk::Image> { |
| 45 | 45 | match *self { |
| 46 | 46 | Input::Nv12 { .. } | Input::P010 { .. } | Input::Rgb { .. } => None, |
| 47 | - #[cfg(target_os = "linux")] | |
| 48 | - Input::DmaBuf { image, .. } => Some(image), | |
| 47 | + #[cfg(any(target_os = "linux", windows))] | |
| 48 | + Input::Imported { image, .. } => Some(image), | |
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | } |
| @@ -299,7 +299,11 @@ impl RgbConverter { | ||
| 299 | 299 | .dst_access_mask(vk::AccessFlags2::TRANSFER_READ) |
| 300 | 300 | .old_layout(vk::ImageLayout::GENERAL) |
| 301 | 301 | .new_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL) |
| 302 | - .src_queue_family_index(vk::QUEUE_FAMILY_FOREIGN_EXT) | |
| 302 | + .src_queue_family_index(if cfg!(windows) { | |
| 303 | + vk::QUEUE_FAMILY_EXTERNAL | |
| 304 | + } else { | |
| 305 | + vk::QUEUE_FAMILY_FOREIGN_EXT | |
| 306 | + }) | |
| 303 | 307 | .dst_queue_family_index(family) |
| 304 | 308 | .image(image) |
| 305 | 309 | .subresource_range(color_range()) |
shims/vk-video-ffi/src/session.rsmodified+30 -10
| @@ -8,7 +8,7 @@ use vulkanalia::vk::{ | ||
| 8 | 8 | |
| 9 | 9 | #[cfg(target_os = "linux")] |
| 10 | 10 | use crate::dmabuf::{DmaBuf, DmaBufFormat, ImportCache, acquire, release}; |
| 11 | -#[cfg(target_os = "linux")] | |
| 11 | +#[cfg(any(target_os = "linux", windows))] | |
| 12 | 12 | use crate::rgb_convert::check_size; |
| 13 | 13 | use crate::rgb_convert::{Input, RgbConverter}; |
| 14 | 14 | use crate::rgb_decode::{Params, RgbPass}; |
| @@ -863,7 +863,7 @@ struct Slot { | ||
| 863 | 863 | upload: Lane, |
| 864 | 864 | ready: vk::Semaphore, |
| 865 | 865 | encoded: vk::Semaphore, |
| 866 | - #[cfg(target_os = "linux")] | |
| 866 | + #[cfg(any(target_os = "linux", windows))] | |
| 867 | 867 | import: Option<u64>, |
| 868 | 868 | } |
| 869 | 869 | |
| @@ -917,13 +917,13 @@ impl Slot { | ||
| 917 | 917 | upload: Lane::new(&gpu, upload_family)?, |
| 918 | 918 | ready: semaphore(d)?, |
| 919 | 919 | encoded: semaphore(d)?, |
| 920 | - #[cfg(target_os = "linux")] | |
| 920 | + #[cfg(any(target_os = "linux", windows))] | |
| 921 | 921 | import: None, |
| 922 | 922 | }) |
| 923 | 923 | } |
| 924 | 924 | |
| 925 | 925 | fn retire(&mut self) { |
| 926 | - #[cfg(target_os = "linux")] | |
| 926 | + #[cfg(any(target_os = "linux", windows))] | |
| 927 | 927 | { |
| 928 | 928 | self.import = None; |
| 929 | 929 | } |
| @@ -944,6 +944,8 @@ pub(crate) struct Encoder { | ||
| 944 | 944 | pub(crate) dpb: [Image; DPB_SLOTS], |
| 945 | 945 | #[cfg(target_os = "linux")] |
| 946 | 946 | pub(crate) imports: ImportCache, |
| 947 | + #[cfg(windows)] | |
| 948 | + pub(crate) imports: crate::d3d11::ImportCache, | |
| 947 | 949 | slots: [Slot; FLIGHT], |
| 948 | 950 | cur: usize, |
| 949 | 951 | next: usize, |
| @@ -1010,7 +1012,7 @@ impl Encoder { | ||
| 1010 | 1012 | coded: false, |
| 1011 | 1013 | bitstream_len, |
| 1012 | 1014 | dpb, |
| 1013 | - #[cfg(target_os = "linux")] | |
| 1015 | + #[cfg(any(target_os = "linux", windows))] | |
| 1014 | 1016 | imports: Default::default(), |
| 1015 | 1017 | slots, |
| 1016 | 1018 | cur: 0, |
| @@ -1096,8 +1098,8 @@ impl Encoder { | ||
| 1096 | 1098 | rgb.reserve(&gpu, width, height)?; |
| 1097 | 1099 | rgb.load(data, stride); |
| 1098 | 1100 | } |
| 1099 | - #[cfg(target_os = "linux")] | |
| 1100 | - Input::DmaBuf { width, height, .. } => { | |
| 1101 | + #[cfg(any(target_os = "linux", windows))] | |
| 1102 | + Input::Imported { width, height, .. } => { | |
| 1101 | 1103 | self.slots[k].rgb.reserve(&gpu, width, height)?; |
| 1102 | 1104 | } |
| 1103 | 1105 | } |
| @@ -1172,7 +1174,7 @@ impl Encoder { | ||
| 1172 | 1174 | let (image, planes, id) = self.imports.get(&gpu, src, &busy)?; |
| 1173 | 1175 | let k = self.next; |
| 1174 | 1176 | let result = match (src.format, planes) { |
| 1175 | - (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::DmaBuf { | |
| 1177 | + (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::Imported { | |
| 1176 | 1178 | image, |
| 1177 | 1179 | width, |
| 1178 | 1180 | height, |
| @@ -1189,6 +1191,24 @@ impl Encoder { | ||
| 1189 | 1191 | result |
| 1190 | 1192 | } |
| 1191 | 1193 | |
| 1194 | + #[cfg(windows)] | |
| 1195 | + pub(crate) fn upload_d3d11(&mut self, src: &crate::d3d11::D3d11Texture) -> Result<(), Error> { | |
| 1196 | + let gpu = self.vs.gpu.clone(); | |
| 1197 | + let (width, height) = (src.width as usize, src.height as usize); | |
| 1198 | + check_size(self.input, (width, height))?; | |
| 1199 | + let busy: Vec<u64> = self.slots.iter().filter_map(|s| s.import).collect(); | |
| 1200 | + let (image, id) = self.imports.get(&gpu, src, &busy)?; | |
| 1201 | + let k = self.next; | |
| 1202 | + self.upload(Input::Imported { | |
| 1203 | + image, | |
| 1204 | + width, | |
| 1205 | + height, | |
| 1206 | + bgr: src.bgr, | |
| 1207 | + })?; | |
| 1208 | + self.slots[k].import = Some(id); | |
| 1209 | + Ok(()) | |
| 1210 | + } | |
| 1211 | + | |
| 1192 | 1212 | #[cfg(target_os = "linux")] |
| 1193 | 1213 | fn check_planar(&self, src: &DmaBuf) -> Result<(), Error> { |
| 1194 | 1214 | let ten_bit = src.format == DmaBufFormat::P010; |
| @@ -1336,7 +1356,7 @@ impl Encoder { | ||
| 1336 | 1356 | slot.upload.submit_with(&[], &[slot.ready]) |
| 1337 | 1357 | } |
| 1338 | 1358 | |
| 1339 | - #[cfg(target_os = "linux")] | |
| 1359 | + #[cfg(any(target_os = "linux", windows))] | |
| 1340 | 1360 | pub(crate) fn import_count(&self) -> usize { |
| 1341 | 1361 | self.imports.count |
| 1342 | 1362 | } |
| @@ -1485,7 +1505,7 @@ impl Drop for Encoder { | ||
| 1485 | 1505 | for img in &self.dpb { |
| 1486 | 1506 | img.destroy(d); |
| 1487 | 1507 | } |
| 1488 | - #[cfg(target_os = "linux")] | |
| 1508 | + #[cfg(any(target_os = "linux", windows))] | |
| 1489 | 1509 | self.imports.destroy(d); |
| 1490 | 1510 | for slot in self.slots.iter_mut() { |
| 1491 | 1511 | unsafe { |