forge
Explore
Log in

Don't you dare go Hollow

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 commit

301ebce0bb3291a44812a75ccce8f523834957aeAhmet Kaan GümüşParent: 1b795c6
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> {
341341 Ok(ec_vk::dup_raw_fd(fd)?)
342342 }
343343
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+
344378 /// One picture for [`HwEncoder::submit`].
345379 #[derive(Debug, Clone, Copy)]
346380 pub enum EncodeInput<'a> {
@@ -356,6 +390,9 @@ pub enum EncodeInput<'a> {
356390 /// DMA-BUF of NV12 or P010 at the encoder size, Vulkan only.
357391 #[cfg(target_os = "linux")]
358392 DmaBufYuv(&'a DmaBufYuvFrame<'a>),
393+ /// Shared D3D11 texture of packed RGB, Vulkan only.
394+ #[cfg(windows)]
395+ D3d11(&'a D3d11Frame<'a>),
359396 }
360397
361398 #[cfg(target_os = "linux")]
@@ -483,6 +520,15 @@ impl VkEncoder {
483520 };
484521 self.submit_dma(&src, force_keyframe)
485522 }
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+ }
486532 }
487533 }
488534
@@ -859,6 +905,23 @@ impl HwEncoder {
859905 self.encode_dma(&src, meta)
860906 }
861907
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+
862925 #[cfg(target_os = "linux")]
863926 fn encode_dma(&mut self, src: &DmaBuf, meta: FrameMetadata) -> Result<CodedFrame> {
864927 let meta = self.with_recovery(meta);
crates/ec-hw/src/lib.rsmodified+2 -0
@@ -92,6 +92,8 @@ pub use error::{Error, Result};
9292 pub use ec_vk::{DmaBufFormat, DmaBufPlane};
9393 #[cfg(target_os = "linux")]
9494 pub use hw::{DmaBufFrame, DmaBufYuvFrame, dmabuf_modifiers, dup_fd};
95+#[cfg(windows)]
96+pub use hw::{D3d11Frame, d3d11_supported};
9597 pub use hw::{
9698 Backend, Capability, CodecProfile, Direction, EncodeInput, HwDecoder, HwEncoder, RgbFrame,
9799 RgbOrder, RgbOutput, capabilities,
crates/ec-vk/src/av1.rsmodified+26 -0
@@ -1,5 +1,7 @@
11 use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref};
22
3+#[cfg(windows)]
4+use crate::D3d11Texture;
35 #[cfg(target_os = "linux")]
46 use crate::DmaBuf;
57 use crate::history::History;
@@ -191,6 +193,30 @@ impl Av1Encoder {
191193 last_frame(first, rest)
192194 }
193195
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+
194220 /// Submits one DMA-BUF frame of the source size without waiting.
195221 #[cfg(target_os = "linux")]
196222 pub fn submit_dmabuf(
crates/ec-vk/src/h265.rsmodified+26 -0
@@ -1,5 +1,7 @@
11 use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref};
22
3+#[cfg(windows)]
4+use crate::D3d11Texture;
35 #[cfg(target_os = "linux")]
46 use crate::DmaBuf;
57 use crate::history::History;
@@ -189,6 +191,30 @@ impl H265Encoder {
189191 last_frame(first, rest)
190192 }
191193
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+
192218 /// Submits one DMA-BUF frame of the source size without waiting.
193219 #[cfg(target_os = "linux")]
194220 pub fn submit_dmabuf(
crates/ec-vk/src/lib.rsmodified+26 -0
@@ -13,6 +13,8 @@ mod history;
1313
1414 pub use av1::{Av1Encoder, Av1EncoderConfig};
1515 pub use h265::{H265Encoder, H265EncoderConfig};
16+#[cfg(windows)]
17+pub use vk_video_ffi::{D3d11Texture, d3d11_importable};
1618 #[cfg(target_os = "linux")]
1719 pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers};
1820 pub use vk_video_ffi::{
@@ -223,6 +225,30 @@ impl H264Encoder {
223225 last_frame(first, rest)
224226 }
225227
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+
226252 /// Submits one DMA-BUF frame of the source size without waiting.
227253 #[cfg(target_os = "linux")]
228254 pub fn submit_dmabuf(
shims/vk-video-ffi/src/av1_encode.rsmodified+21 -0
@@ -294,6 +294,27 @@ impl Av1EncodeSession {
294294 self.encode_uploaded(pic)
295295 }
296296
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+
297318 /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once.
298319 #[cfg(target_os = "linux")]
299320 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 {
391391 self.encode_uploaded(pic)
392392 }
393393
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+
394415 /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once.
395416 #[cfg(target_os = "linux")]
396417 pub fn dmabuf_imports(&self) -> usize {
shims/vk-video-ffi/src/h265_encode.rsmodified+21 -0
@@ -336,6 +336,27 @@ impl H265EncodeSession {
336336 self.encode_uploaded(pic)
337337 }
338338
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+
339360 /// Number of DMA-BUF imports made so far; a buffer re-used by fd is imported once.
340361 #[cfg(target_os = "linux")]
341362 pub fn dmabuf_imports(&self) -> usize {
shims/vk-video-ffi/src/lib.rsmodified+6 -1
@@ -8,6 +8,8 @@ use std::sync::Arc;
88
99 mod av1_decode;
1010 mod av1_encode;
11+#[cfg(windows)]
12+mod d3d11;
1113 #[cfg(target_os = "linux")]
1214 mod dmabuf;
1315 mod h264_decode;
@@ -27,6 +29,8 @@ pub use av1_decode::{
2729 };
2830 pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref};
2931 pub use session::DPB_SLOTS;
32+#[cfg(windows)]
33+pub use d3d11::{D3d11Texture, d3d11_importable};
3034 #[cfg(target_os = "linux")]
3135 pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers};
3236 pub use vp9_decode::{
@@ -65,7 +69,7 @@ use vk::KhrVideoQueueExtensionInstanceCommands;
6569 use vulkanalia::loader::LibloadingLoader;
6670 use vulkanalia::prelude::v1_3::*;
6771
68-const WANTED: [vk::Extension; 15] = [
72+const WANTED: [vk::Extension; 16] = [
6973 vk::KHR_VIDEO_QUEUE_EXTENSION,
7074 vk::KHR_VIDEO_ENCODE_QUEUE_EXTENSION,
7175 vk::KHR_VIDEO_DECODE_QUEUE_EXTENSION,
@@ -81,6 +85,7 @@ const WANTED: [vk::Extension; 15] = [
8185 vk::KHR_EXTERNAL_MEMORY_FD_EXTENSION,
8286 vk::EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION,
8387 vk::EXT_QUEUE_FAMILY_FOREIGN_EXTENSION,
88+ vk::KHR_EXTERNAL_MEMORY_WIN32_EXTENSION,
8489 ];
8590
8691 /// 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> {
2222 stride: usize,
2323 bgr: bool,
2424 },
25- #[cfg(target_os = "linux")]
26- DmaBuf {
25+ #[cfg(any(target_os = "linux", windows))]
26+ Imported {
2727 image: vk::Image,
2828 width: usize,
2929 height: usize,
@@ -36,16 +36,16 @@ impl Input<'_> {
3636 match *self {
3737 Input::Nv12 { .. } | Input::P010 { .. } => None,
3838 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),
4141 }
4242 }
4343
4444 pub(crate) fn source_image(&self) -> Option<vk::Image> {
4545 match *self {
4646 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),
4949 }
5050 }
5151 }
@@ -299,7 +299,11 @@ impl RgbConverter {
299299 .dst_access_mask(vk::AccessFlags2::TRANSFER_READ)
300300 .old_layout(vk::ImageLayout::GENERAL)
301301 .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+ })
303307 .dst_queue_family_index(family)
304308 .image(image)
305309 .subresource_range(color_range())
shims/vk-video-ffi/src/session.rsmodified+30 -10
@@ -8,7 +8,7 @@ use vulkanalia::vk::{
88
99 #[cfg(target_os = "linux")]
1010 use crate::dmabuf::{DmaBuf, DmaBufFormat, ImportCache, acquire, release};
11-#[cfg(target_os = "linux")]
11+#[cfg(any(target_os = "linux", windows))]
1212 use crate::rgb_convert::check_size;
1313 use crate::rgb_convert::{Input, RgbConverter};
1414 use crate::rgb_decode::{Params, RgbPass};
@@ -863,7 +863,7 @@ struct Slot {
863863 upload: Lane,
864864 ready: vk::Semaphore,
865865 encoded: vk::Semaphore,
866- #[cfg(target_os = "linux")]
866+ #[cfg(any(target_os = "linux", windows))]
867867 import: Option<u64>,
868868 }
869869
@@ -917,13 +917,13 @@ impl Slot {
917917 upload: Lane::new(&gpu, upload_family)?,
918918 ready: semaphore(d)?,
919919 encoded: semaphore(d)?,
920- #[cfg(target_os = "linux")]
920+ #[cfg(any(target_os = "linux", windows))]
921921 import: None,
922922 })
923923 }
924924
925925 fn retire(&mut self) {
926- #[cfg(target_os = "linux")]
926+ #[cfg(any(target_os = "linux", windows))]
927927 {
928928 self.import = None;
929929 }
@@ -944,6 +944,8 @@ pub(crate) struct Encoder {
944944 pub(crate) dpb: [Image; DPB_SLOTS],
945945 #[cfg(target_os = "linux")]
946946 pub(crate) imports: ImportCache,
947+ #[cfg(windows)]
948+ pub(crate) imports: crate::d3d11::ImportCache,
947949 slots: [Slot; FLIGHT],
948950 cur: usize,
949951 next: usize,
@@ -1010,7 +1012,7 @@ impl Encoder {
10101012 coded: false,
10111013 bitstream_len,
10121014 dpb,
1013- #[cfg(target_os = "linux")]
1015+ #[cfg(any(target_os = "linux", windows))]
10141016 imports: Default::default(),
10151017 slots,
10161018 cur: 0,
@@ -1096,8 +1098,8 @@ impl Encoder {
10961098 rgb.reserve(&gpu, width, height)?;
10971099 rgb.load(data, stride);
10981100 }
1099- #[cfg(target_os = "linux")]
1100- Input::DmaBuf { width, height, .. } => {
1101+ #[cfg(any(target_os = "linux", windows))]
1102+ Input::Imported { width, height, .. } => {
11011103 self.slots[k].rgb.reserve(&gpu, width, height)?;
11021104 }
11031105 }
@@ -1172,7 +1174,7 @@ impl Encoder {
11721174 let (image, planes, id) = self.imports.get(&gpu, src, &busy)?;
11731175 let k = self.next;
11741176 let result = match (src.format, planes) {
1175- (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::DmaBuf {
1177+ (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::Imported {
11761178 image,
11771179 width,
11781180 height,
@@ -1189,6 +1191,24 @@ impl Encoder {
11891191 result
11901192 }
11911193
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+
11921212 #[cfg(target_os = "linux")]
11931213 fn check_planar(&self, src: &DmaBuf) -> Result<(), Error> {
11941214 let ten_bit = src.format == DmaBufFormat::P010;
@@ -1336,7 +1356,7 @@ impl Encoder {
13361356 slot.upload.submit_with(&[], &[slot.ready])
13371357 }
13381358
1339- #[cfg(target_os = "linux")]
1359+ #[cfg(any(target_os = "linux", windows))]
13401360 pub(crate) fn import_count(&self) -> usize {
13411361 self.imports.count
13421362 }
@@ -1485,7 +1505,7 @@ impl Drop for Encoder {
14851505 for img in &self.dpb {
14861506 img.destroy(d);
14871507 }
1488- #[cfg(target_os = "linux")]
1508+ #[cfg(any(target_os = "linux", windows))]
14891509 self.imports.destroy(d);
14901510 for slot in self.slots.iter_mut() {
14911511 unsafe {