Skip to content

Commit d823888

Browse files
leksisoalmarklein
andauthored
Expose WinUI SwapChainPanel as a surface source type (#480)
* Expose WinUI SwapChainPanel as a surface source type * Include SwapChainPanel only on windows * fix formatting --------- Co-authored-by: Almar Klein <[email protected]>
1 parent af9074e commit d823888

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

ffi/wgpu.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef enum WGPUNativeSType {
1414
WGPUSType_BindGroupLayoutEntryExtras = 0x00030008,
1515
WGPUSType_QuerySetDescriptorExtras = 0x00030009,
1616
WGPUSType_SurfaceConfigurationExtras = 0x0003000A,
17+
WGPUSType_SurfaceSourceSwapChainPanel = 0x0003000B,
1718
WGPUNativeSType_Force32 = 0x7FFFFFFF
1819
} WGPUNativeSType;
1920

@@ -256,6 +257,18 @@ typedef struct WGPUSurfaceConfigurationExtras {
256257
uint32_t desiredMaximumFrameLatency;
257258
} WGPUSurfaceConfigurationExtras WGPU_STRUCTURE_ATTRIBUTE;
258259

260+
/**
261+
* Chained in @ref WGPUSurfaceDescriptor to make a @ref WGPUSurface wrapping a WinUI [`SwapChainPanel`](https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.swapchainpanel).
262+
*/
263+
typedef struct WGPUSurfaceSourceSwapChainPanel {
264+
WGPUChainedStruct chain;
265+
/**
266+
* A pointer to the [`ISwapChainPanelNative`](https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/win32/microsoft.ui.xaml.media.dxinterop/nn-microsoft-ui-xaml-media-dxinterop-iswapchainpanelnative)
267+
* interface of the SwapChainPanel that will be wrapped by the @ref WGPUSurface.
268+
*/
269+
void * panelNative;
270+
} WGPUSurfaceSourceSwapChainPanel WGPU_STRUCTURE_ATTRIBUTE;
271+
259272
typedef void (*WGPULogCallback)(WGPULogLevel level, WGPUStringView message, void * userdata);
260273

261274
typedef enum WGPUNativeTextureFormat {

src/conv.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,8 @@ pub enum CreateSurfaceParams {
16031603
),
16041604
#[cfg(all(any(target_os = "ios", target_os = "macos"), feature = "metal"))]
16051605
Metal(*mut std::ffi::c_void),
1606+
#[cfg(all(target_os = "windows", feature = "dx12"))]
1607+
SwapChainPanel(*mut std::ffi::c_void),
16061608
}
16071609

16081610
pub unsafe fn map_surface(
@@ -1613,6 +1615,7 @@ pub unsafe fn map_surface(
16131615
wl: Option<&native::WGPUSurfaceSourceWaylandSurface>,
16141616
_metal: Option<&native::WGPUSurfaceSourceMetalLayer>,
16151617
android: Option<&native::WGPUSurfaceSourceAndroidNativeWindow>,
1618+
_swap_chain_panel: Option<&native::WGPUSurfaceSourceSwapChainPanel>,
16161619
) -> CreateSurfaceParams {
16171620
if let Some(win) = win {
16181621
let display_handle = raw_window_handle::WindowsDisplayHandle::new();
@@ -1677,6 +1680,11 @@ pub unsafe fn map_surface(
16771680
));
16781681
}
16791682

1683+
#[cfg(all(target_os = "windows", feature = "dx12"))]
1684+
if let Some(swap_chain_panel) = _swap_chain_panel {
1685+
return CreateSurfaceParams::SwapChainPanel(swap_chain_panel.panelNative);
1686+
}
1687+
16801688
panic!("Error: Unsupported Surface");
16811689
}
16821690

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,7 +2657,8 @@ pub unsafe extern "C" fn wgpuInstanceCreateSurface(
26572657
WGPUSType_SurfaceSourceXlibWindow => native::WGPUSurfaceSourceXlibWindow,
26582658
WGPUSType_SurfaceSourceWaylandSurface => native::WGPUSurfaceSourceWaylandSurface,
26592659
WGPUSType_SurfaceSourceMetalLayer => native::WGPUSurfaceSourceMetalLayer,
2660-
WGPUSType_SurfaceSourceAndroidNativeWindow => native::WGPUSurfaceSourceAndroidNativeWindow)
2660+
WGPUSType_SurfaceSourceAndroidNativeWindow => native::WGPUSurfaceSourceAndroidNativeWindow,
2661+
WGPUSType_SurfaceSourceSwapChainPanel => native::WGPUSurfaceSourceSwapChainPanel)
26612662
);
26622663

26632664
let surface_id = match create_surface_params {
@@ -2674,6 +2675,13 @@ pub unsafe extern "C" fn wgpuInstanceCreateSurface(
26742675
Err(cause) => handle_error_fatal(cause, "wgpuInstanceCreateSurface"),
26752676
}
26762677
}
2678+
#[cfg(all(target_os = "windows", feature = "dx12"))]
2679+
CreateSurfaceParams::SwapChainPanel(panel) => {
2680+
match context.instance_create_surface_from_swap_chain_panel(panel, None) {
2681+
Ok(surface_id) => surface_id,
2682+
Err(cause) => handle_error_fatal(cause, "wgpuInstanceCreateSurface"),
2683+
}
2684+
}
26772685
};
26782686

26792687
Arc::into_raw(Arc::new(WGPUSurfaceImpl {

0 commit comments

Comments
 (0)