1
0

VulkanDemoControl.cs 2.9 KB

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