-
Notifications
You must be signed in to change notification settings - Fork 45
/
Direct3DInterop.cs
79 lines (62 loc) · 3.29 KB
/
Direct3DInterop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Vortice.DXGI;
using System;
using System.Runtime.InteropServices;
using Windows.Graphics.DirectX.Direct3D11;
using SharpGen.Runtime;
namespace DirectX12GameEngine.Graphics
{
internal static class Direct3DInterop
{
private static readonly Guid ID3D11Resource = new Guid("DC8E63F3-D12B-4952-B47B-5E45026A862D");
[ComImport]
[Guid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
internal interface IDirect3DDxgiInterfaceAccess : IDisposable
{
IntPtr GetInterface([In] ref Guid iid);
}
[ComImport]
[Guid("F92F19D2-3ADE-45A6-A20C-F6F1EA90554B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
internal interface ISwapChainPanelNative : IDisposable
{
Result SetSwapChain([In] IntPtr swapChain);
}
internal static IDirect3DDevice CreateDirect3DDevice(IDXGIDevice dxgiDevice)
{
Result result = CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.NativePointer, out IntPtr graphicsDevice);
if (result.Failure) throw new InvalidOperationException(result.Code.ToString());
IDirect3DDevice d3DInteropDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(graphicsDevice);
Marshal.Release(graphicsDevice);
return d3DInteropDevice;
}
internal static IDirect3DSurface CreateDirect3DSurface(IDXGISurface dxgiSurface)
{
Result result = CreateDirect3D11SurfaceFromDXGISurface(dxgiSurface.NativePointer, out IntPtr graphicsSurface);
if (result.Failure) throw new InvalidOperationException(result.Code.ToString());
IDirect3DSurface d3DSurface = (IDirect3DSurface)Marshal.GetObjectForIUnknown(graphicsSurface);
Marshal.Release(graphicsSurface);
return d3DSurface;
}
internal static IDXGIDevice CreateDXGIDevice(IDirect3DDevice direct3DDevice)
{
IDirect3DDxgiInterfaceAccess dxgiDeviceInterfaceAccess = (IDirect3DDxgiInterfaceAccess)direct3DDevice;
IntPtr device = dxgiDeviceInterfaceAccess.GetInterface(ID3D11Resource);
return new IDXGIDevice(device);
}
internal static IDXGISurface CreateDXGISurface(IDirect3DSurface direct3DSurface)
{
IDirect3DDxgiInterfaceAccess dxgiSurfaceInterfaceAccess = (IDirect3DDxgiInterfaceAccess)direct3DSurface;
IntPtr surface = dxgiSurfaceInterfaceAccess.GetInterface(ID3D11Resource);
return new IDXGISurface(surface);
}
[DllImport("d3d11.dll", EntryPoint = "CreateDirect3D11DeviceFromDXGIDevice",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern Result CreateDirect3D11DeviceFromDXGIDevice(IntPtr dxgiDevice, out IntPtr graphicsDevice);
[DllImport("d3d11.dll", EntryPoint = "CreateDirect3D11SurfaceFromDXGISurface",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern Result CreateDirect3D11SurfaceFromDXGISurface(IntPtr dxgiSurface, out IntPtr graphicsSurface);
}
}