Files
clipforge/ClipForge/Direct3D11Helper.cs
Blade / SCOPEDD c1a425a7ad Add project files.
2026-02-22 11:31:35 -05:00

71 lines
2.4 KiB
C#

using System;
using System.Runtime.InteropServices;
using Windows.Graphics.DirectX.Direct3D11;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
namespace ClipForge
{
public static class Direct3D11Helper
{
[ComImport]
[Guid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
interface IDirect3DDxgiInterfaceAccess
{
IntPtr GetInterface([In] ref Guid iid);
}
[DllImport("d3d11.dll", EntryPoint = "D3D11CreateDevice",
SetLastError = true,
CharSet = CharSet.Unicode,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int D3D11CreateDevice(
IntPtr pAdapter,
uint driverType,
IntPtr software,
uint flags,
IntPtr pFeatureLevels,
uint featureLevels,
uint sdkVersion,
out IntPtr ppDevice,
out uint pFeatureLevel,
out IntPtr ppImmediateContext);
public static IDirect3DDevice CreateDevice()
{
// Create a SharpDX D3D11 device
var d3dDevice = new SharpDX.Direct3D11.Device(
SharpDX.Direct3D.DriverType.Hardware,
DeviceCreationFlags.BgraSupport);
// Get the DXGI device interface from it
var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
// Create a WinRT IDirect3DDevice from the DXGI device
var result = CreateDirect3DDeviceFromDXGIDevice(dxgiDevice.NativePointer);
var winrtDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(result);
Marshal.Release(result);
return winrtDevice;
}
[DllImport("d3d11.dll",
EntryPoint = "CreateDirect3D11DeviceFromDXGIDevice",
SetLastError = true,
CharSet = CharSet.Unicode,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int CreateDirect3D11DeviceFromDXGIDevice(
IntPtr dxgiDevice,
out IntPtr graphicsDevice);
private static IntPtr CreateDirect3DDeviceFromDXGIDevice(IntPtr dxgiDevice)
{
CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice, out IntPtr graphicsDevice);
return graphicsDevice;
}
}
}