VulkanPlatformInterface.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Avalonia;
  2. using Ryujinx.Ava.Ui.Vulkan.Surfaces;
  3. using Ryujinx.Graphics.Vulkan;
  4. using Silk.NET.Vulkan;
  5. using System;
  6. namespace Ryujinx.Ava.Ui.Vulkan
  7. {
  8. internal class VulkanPlatformInterface : IDisposable
  9. {
  10. private static VulkanOptions _options;
  11. private VulkanPlatformInterface(VulkanInstance instance)
  12. {
  13. Instance = instance;
  14. Api = instance.Api;
  15. }
  16. public VulkanPhysicalDevice PhysicalDevice { get; private set; }
  17. public VulkanInstance Instance { get; }
  18. public Vk Api { get; private set; }
  19. public VulkanSurfaceRenderTarget MainSurface { get; set; }
  20. public void Dispose()
  21. {
  22. Instance?.Dispose();
  23. Api?.Dispose();
  24. }
  25. private static VulkanPlatformInterface TryCreate()
  26. {
  27. _options = AvaloniaLocator.Current.GetService<VulkanOptions>() ?? new VulkanOptions();
  28. var instance = VulkanInstance.Create(_options);
  29. return new VulkanPlatformInterface(instance);
  30. }
  31. public static bool TryInitialize()
  32. {
  33. var feature = TryCreate();
  34. if (feature != null)
  35. {
  36. AvaloniaLocator.CurrentMutable.Bind<VulkanPlatformInterface>().ToConstant(feature);
  37. return true;
  38. }
  39. return false;
  40. }
  41. public VulkanSurfaceRenderTarget CreateRenderTarget(IVulkanPlatformSurface platformSurface)
  42. {
  43. var surface = VulkanSurface.CreateSurface(Instance, platformSurface);
  44. if (PhysicalDevice == null)
  45. {
  46. PhysicalDevice = VulkanPhysicalDevice.FindSuitablePhysicalDevice(Instance, surface, _options.PreferDiscreteGpu, _options.PreferredDevice);
  47. }
  48. var renderTarget = new VulkanSurfaceRenderTarget(this, surface);
  49. if (MainSurface == null && surface != null)
  50. {
  51. MainSurface = renderTarget;
  52. }
  53. return renderTarget;
  54. }
  55. }
  56. }