D3DMemoryHelper.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using Avalonia;
  5. using SharpDX.Direct3D;
  6. using SharpDX.Direct3D11;
  7. using SharpDX.DXGI;
  8. using D3DDevice = SharpDX.Direct3D11.Device;
  9. using DxgiFactory1 = SharpDX.DXGI.Factory1;
  10. namespace GpuInterop.VulkanDemo;
  11. public class D3DMemoryHelper
  12. {
  13. public static D3DDevice CreateDeviceByLuid(Span<byte> luid)
  14. {
  15. var factory = new DxgiFactory1();
  16. var longLuid = MemoryMarshal.Cast<byte, long>(luid)[0];
  17. for (var c = 0; c < factory.GetAdapterCount1(); c++)
  18. {
  19. using var adapter = factory.GetAdapter1(c);
  20. if (adapter.Description1.Luid != longLuid)
  21. continue;
  22. return new D3DDevice(adapter, DeviceCreationFlags.None,
  23. new[]
  24. {
  25. FeatureLevel.Level_12_1, FeatureLevel.Level_12_0, FeatureLevel.Level_11_1,
  26. FeatureLevel.Level_11_0, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3,
  27. FeatureLevel.Level_9_2, FeatureLevel.Level_9_1,
  28. });
  29. }
  30. throw new ArgumentException("Device with the corresponding LUID not found");
  31. }
  32. public static Texture2D CreateMemoryHandle(D3DDevice device, PixelSize size, Silk.NET.Vulkan.Format format)
  33. {
  34. if (format != Silk.NET.Vulkan.Format.R8G8B8A8Unorm)
  35. throw new ArgumentException("Not supported format");
  36. return new Texture2D(device,
  37. new Texture2DDescription
  38. {
  39. Format = Format.R8G8B8A8_UNorm,
  40. Width = size.Width,
  41. Height = size.Height,
  42. ArraySize = 1,
  43. MipLevels = 1,
  44. SampleDescription = new SampleDescription { Count = 1, Quality = 0 },
  45. CpuAccessFlags = default,
  46. OptionFlags = ResourceOptionFlags.SharedKeyedmutex|ResourceOptionFlags.SharedNthandle,
  47. BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource
  48. });
  49. }
  50. }