VulkanImage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using Avalonia;
  5. using Avalonia.Platform;
  6. using Avalonia.Vulkan;
  7. using SharpDX.DXGI;
  8. using Silk.NET.Vulkan;
  9. using Silk.NET.Vulkan.Extensions.KHR;
  10. using SilkNetDemo;
  11. using SkiaSharp;
  12. using Device = Silk.NET.Vulkan.Device;
  13. using Format = Silk.NET.Vulkan.Format;
  14. namespace GpuInterop.VulkanDemo;
  15. public unsafe class VulkanImage : IDisposable
  16. {
  17. private readonly VulkanContext _vk;
  18. private readonly Instance _instance;
  19. private readonly Device _device;
  20. private readonly PhysicalDevice _physicalDevice;
  21. private readonly VulkanCommandBufferPool _commandBufferPool;
  22. private ImageLayout _currentLayout;
  23. private AccessFlags _currentAccessFlags;
  24. private ImageUsageFlags _imageUsageFlags { get; }
  25. private ImageView? _imageView { get; set; }
  26. private DeviceMemory _imageMemory { get; set; }
  27. private SharpDX.Direct3D11.Texture2D? _d3dTexture2D;
  28. internal Image? InternalHandle { get; private set; }
  29. internal Format Format { get; }
  30. internal ImageAspectFlags AspectFlags { get; private set; }
  31. public ulong Handle => InternalHandle?.Handle ?? 0;
  32. public ulong ViewHandle => _imageView?.Handle ?? 0;
  33. public uint UsageFlags => (uint) _imageUsageFlags;
  34. public ulong MemoryHandle => _imageMemory.Handle;
  35. public DeviceMemory DeviceMemory => _imageMemory;
  36. public uint MipLevels { get; private set; }
  37. public Vk Api { get; }
  38. public PixelSize Size { get; }
  39. public ulong MemorySize { get; private set; }
  40. public uint CurrentLayout => (uint) _currentLayout;
  41. public VulkanImage(VulkanContext vk, uint format, PixelSize size,
  42. bool exportable, uint mipLevels = 0)
  43. {
  44. _vk = vk;
  45. _instance = vk.Instance;
  46. _device = vk.Device;
  47. _physicalDevice = vk.PhysicalDevice;
  48. _commandBufferPool = vk.Pool;
  49. Format = (Format)format;
  50. Api = vk.Api;
  51. Size = size;
  52. MipLevels = 1;//mipLevels;
  53. _imageUsageFlags =
  54. ImageUsageFlags.ColorAttachmentBit | ImageUsageFlags.TransferDstBit |
  55. ImageUsageFlags.TransferSrcBit | ImageUsageFlags.SampledBit;
  56. //MipLevels = MipLevels != 0 ? MipLevels : (uint)Math.Floor(Math.Log(Math.Max(Size.Width, Size.Height), 2));
  57. var handleType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
  58. ExternalMemoryHandleTypeFlags.D3D11TextureBit :
  59. ExternalMemoryHandleTypeFlags.OpaqueFDBit;
  60. var externalMemoryCreateInfo = new ExternalMemoryImageCreateInfo
  61. {
  62. SType = StructureType.ExternalMemoryImageCreateInfo,
  63. HandleTypes = handleType
  64. };
  65. var imageCreateInfo = new ImageCreateInfo
  66. {
  67. PNext = exportable ? &externalMemoryCreateInfo : null,
  68. SType = StructureType.ImageCreateInfo,
  69. ImageType = ImageType.Type2D,
  70. Format = Format,
  71. Extent =
  72. new Extent3D((uint?)Size.Width,
  73. (uint?)Size.Height, 1),
  74. MipLevels = MipLevels,
  75. ArrayLayers = 1,
  76. Samples = SampleCountFlags.Count1Bit,
  77. Tiling = Tiling,
  78. Usage = _imageUsageFlags,
  79. SharingMode = SharingMode.Exclusive,
  80. InitialLayout = ImageLayout.Undefined,
  81. Flags = ImageCreateFlags.CreateMutableFormatBit
  82. };
  83. Api
  84. .CreateImage(_device, imageCreateInfo, null, out var image).ThrowOnError();
  85. InternalHandle = image;
  86. Api.GetImageMemoryRequirements(_device, InternalHandle.Value,
  87. out var memoryRequirements);
  88. var fdExport = new ExportMemoryAllocateInfo
  89. {
  90. HandleTypes = handleType, SType = StructureType.ExportMemoryAllocateInfo
  91. };
  92. var dedicatedAllocation = new MemoryDedicatedAllocateInfoKHR
  93. {
  94. SType = StructureType.MemoryDedicatedAllocateInfoKhr,
  95. Image = image
  96. };
  97. ImportMemoryWin32HandleInfoKHR handleImport = default;
  98. if (exportable && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  99. {
  100. _d3dTexture2D = D3DMemoryHelper.CreateMemoryHandle(vk.D3DDevice, size, Format);
  101. using var dxgi = _d3dTexture2D.QueryInterface<SharpDX.DXGI.Resource1>();
  102. handleImport = new ImportMemoryWin32HandleInfoKHR
  103. {
  104. PNext = &dedicatedAllocation,
  105. SType = StructureType.ImportMemoryWin32HandleInfoKhr,
  106. HandleType = ExternalMemoryHandleTypeFlags.D3D11TextureBit,
  107. Handle = dxgi.CreateSharedHandle(null, SharedResourceFlags.Read | SharedResourceFlags.Write),
  108. };
  109. }
  110. var memoryAllocateInfo = new MemoryAllocateInfo
  111. {
  112. PNext =
  113. exportable ? RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? &handleImport : &fdExport : null,
  114. SType = StructureType.MemoryAllocateInfo,
  115. AllocationSize = memoryRequirements.Size,
  116. MemoryTypeIndex = (uint)VulkanMemoryHelper.FindSuitableMemoryTypeIndex(
  117. Api,
  118. _physicalDevice,
  119. memoryRequirements.MemoryTypeBits, MemoryPropertyFlags.DeviceLocalBit)
  120. };
  121. Api.AllocateMemory(_device, memoryAllocateInfo, null,
  122. out var imageMemory).ThrowOnError();
  123. _imageMemory = imageMemory;
  124. MemorySize = memoryRequirements.Size;
  125. Api.BindImageMemory(_device, InternalHandle.Value, _imageMemory, 0).ThrowOnError();
  126. var componentMapping = new ComponentMapping(
  127. ComponentSwizzle.Identity,
  128. ComponentSwizzle.Identity,
  129. ComponentSwizzle.Identity,
  130. ComponentSwizzle.Identity);
  131. AspectFlags = ImageAspectFlags.ColorBit;
  132. var subresourceRange = new ImageSubresourceRange(AspectFlags, 0, MipLevels, 0, 1);
  133. var imageViewCreateInfo = new ImageViewCreateInfo
  134. {
  135. SType = StructureType.ImageViewCreateInfo,
  136. Image = InternalHandle.Value,
  137. ViewType = ImageViewType.Type2D,
  138. Format = Format,
  139. Components = componentMapping,
  140. SubresourceRange = subresourceRange
  141. };
  142. Api
  143. .CreateImageView(_device, imageViewCreateInfo, null, out var imageView)
  144. .ThrowOnError();
  145. _imageView = imageView;
  146. _currentLayout = ImageLayout.Undefined;
  147. TransitionLayout(ImageLayout.ColorAttachmentOptimal, AccessFlags.NoneKhr);
  148. }
  149. public int ExportFd()
  150. {
  151. if (!Api.TryGetDeviceExtension<KhrExternalMemoryFd>(_instance, _device, out var ext))
  152. throw new InvalidOperationException();
  153. var info = new MemoryGetFdInfoKHR
  154. {
  155. Memory = _imageMemory,
  156. SType = StructureType.MemoryGetFDInfoKhr,
  157. HandleType = ExternalMemoryHandleTypeFlags.OpaqueFDBit
  158. };
  159. ext.GetMemoryF(_device, info, out var fd).ThrowOnError();
  160. return fd;
  161. }
  162. public IPlatformHandle Export()
  163. {
  164. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  165. {
  166. using var dxgi = _d3dTexture2D!.QueryInterface<Resource1>();
  167. return new PlatformHandle(
  168. dxgi.CreateSharedHandle(null, SharedResourceFlags.Read | SharedResourceFlags.Write),
  169. KnownPlatformGraphicsExternalImageHandleTypes.D3D11TextureNtHandle);
  170. }
  171. else
  172. return new PlatformHandle(new IntPtr(ExportFd()),
  173. KnownPlatformGraphicsExternalImageHandleTypes.VulkanOpaquePosixFileDescriptor);
  174. }
  175. public ImageTiling Tiling => ImageTiling.Optimal;
  176. internal void TransitionLayout(CommandBuffer commandBuffer,
  177. ImageLayout fromLayout, AccessFlags fromAccessFlags,
  178. ImageLayout destinationLayout, AccessFlags destinationAccessFlags)
  179. {
  180. VulkanMemoryHelper.TransitionLayout(Api, commandBuffer, InternalHandle.Value,
  181. fromLayout,
  182. fromAccessFlags,
  183. destinationLayout, destinationAccessFlags,
  184. MipLevels);
  185. _currentLayout = destinationLayout;
  186. _currentAccessFlags = destinationAccessFlags;
  187. }
  188. internal void TransitionLayout(CommandBuffer commandBuffer,
  189. ImageLayout destinationLayout, AccessFlags destinationAccessFlags)
  190. => TransitionLayout(commandBuffer, _currentLayout, _currentAccessFlags, destinationLayout,
  191. destinationAccessFlags);
  192. internal void TransitionLayout(ImageLayout destinationLayout, AccessFlags destinationAccessFlags)
  193. {
  194. var commandBuffer = _commandBufferPool.CreateCommandBuffer();
  195. commandBuffer.BeginRecording();
  196. TransitionLayout(commandBuffer.InternalHandle, destinationLayout, destinationAccessFlags);
  197. commandBuffer.EndRecording();
  198. commandBuffer.Submit();
  199. }
  200. public void TransitionLayout(uint destinationLayout, uint destinationAccessFlags)
  201. {
  202. TransitionLayout((ImageLayout)destinationLayout, (AccessFlags)destinationAccessFlags);
  203. }
  204. public unsafe void Dispose()
  205. {
  206. Api.DestroyImageView(_device, _imageView.Value, null);
  207. Api.DestroyImage(_device, InternalHandle.Value, null);
  208. Api.FreeMemory(_device, _imageMemory, null);
  209. _imageView = default;
  210. InternalHandle = default;
  211. _imageMemory = default;
  212. }
  213. public void SaveTexture(string path)
  214. {
  215. _vk.GrContext.ResetContext();
  216. var _image = this;
  217. var imageInfo = new GRVkImageInfo()
  218. {
  219. CurrentQueueFamily = _vk.QueueFamilyIndex,
  220. Format = (uint)_image.Format,
  221. Image = _image.Handle,
  222. ImageLayout = (uint)_image.CurrentLayout,
  223. ImageTiling = (uint)_image.Tiling,
  224. ImageUsageFlags = (uint)_image.UsageFlags,
  225. LevelCount = _image.MipLevels,
  226. SampleCount = 1,
  227. Protected = false,
  228. Alloc = new GRVkAlloc()
  229. {
  230. Memory = _image.MemoryHandle, Flags = 0, Offset = 0, Size = _image.MemorySize
  231. }
  232. };
  233. using (var backendTexture = new GRBackendRenderTarget(_image.Size.Width, _image.Size.Height, 1,
  234. imageInfo))
  235. using (var surface = SKSurface.Create(_vk.GrContext, backendTexture,
  236. GRSurfaceOrigin.TopLeft,
  237. SKColorType.Rgba8888, SKColorSpace.CreateSrgb()))
  238. {
  239. using var snap = surface.Snapshot();
  240. using var encoded = snap.Encode();
  241. using (var s = File.Create(path))
  242. encoded.SaveTo(s);
  243. }
  244. }
  245. }