VulkanDemoControl.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. using Avalonia;
  8. using Avalonia.Platform;
  9. using Avalonia.Rendering.Composition;
  10. using Silk.NET.Core;
  11. using Silk.NET.Vulkan;
  12. using Silk.NET.Vulkan.Extensions.KHR;
  13. using SilkNetDemo;
  14. namespace GpuInterop.VulkanDemo;
  15. public class VulkanDemoControl : DrawingSurfaceDemoBase
  16. {
  17. private Instance _vkInstance;
  18. private Vk _api;
  19. class VulkanResources : IAsyncDisposable
  20. {
  21. public VulkanContext Context { get; }
  22. public VulkanSwapchain Swapchain { get; }
  23. public VulkanContent Content { get; }
  24. public VulkanResources(VulkanContext context, VulkanSwapchain swapchain, VulkanContent content)
  25. {
  26. Context = context;
  27. Swapchain = swapchain;
  28. Content = content;
  29. }
  30. public async ValueTask DisposeAsync()
  31. {
  32. Context.Pool.FreeUsedCommandBuffers();
  33. Content.Dispose();
  34. await Swapchain.DisposeAsync();
  35. Context.Dispose();
  36. }
  37. }
  38. protected override bool SupportsDisco => true;
  39. private VulkanResources? _resources;
  40. protected override (bool success, string info) InitializeGraphicsResources(Compositor compositor,
  41. CompositionDrawingSurface compositionDrawingSurface, ICompositionGpuInterop gpuInterop)
  42. {
  43. var (context, info) = VulkanContext.TryCreate(gpuInterop);
  44. if (context == null)
  45. return (false, info);
  46. try
  47. {
  48. var content = new VulkanContent(context);
  49. _resources = new VulkanResources(context,
  50. new VulkanSwapchain(context, gpuInterop, compositionDrawingSurface), content);
  51. return (true, info);
  52. }
  53. catch(Exception e)
  54. {
  55. return (false, e.ToString());
  56. }
  57. }
  58. protected override void FreeGraphicsResources()
  59. {
  60. _resources?.DisposeAsync();
  61. _resources = null;
  62. }
  63. protected override unsafe void RenderFrame(PixelSize pixelSize)
  64. {
  65. if (_resources == null)
  66. return;
  67. using (_resources.Swapchain.BeginDraw(pixelSize, out var image))
  68. {
  69. /*
  70. var commandBuffer = _resources.Context.Pool.CreateCommandBuffer();
  71. commandBuffer.BeginRecording();
  72. image.TransitionLayout(commandBuffer.InternalHandle, ImageLayout.TransferDstOptimal, AccessFlags.None);
  73. var range = new ImageSubresourceRange
  74. {
  75. AspectMask = ImageAspectFlags.ColorBit,
  76. LayerCount = 1,
  77. LevelCount = 1,
  78. BaseArrayLayer = 0,
  79. BaseMipLevel = 0
  80. };
  81. var color = new ClearColorValue
  82. {
  83. Float32_0 = 1, Float32_1 = 0, Float32_2 = 0, Float32_3 = 1
  84. };
  85. _resources.Context.Api.CmdClearColorImage(commandBuffer.InternalHandle, image.InternalHandle.Value, ImageLayout.TransferDstOptimal,
  86. &color, 1, &range);
  87. commandBuffer.Submit();*/
  88. _resources.Content.Render(image, Yaw, Pitch, Roll, Disco);
  89. }
  90. }
  91. }