VulkanMemoryHelper.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Silk.NET.Vulkan;
  2. namespace Ryujinx.Ava.Ui.Vulkan
  3. {
  4. internal static class VulkanMemoryHelper
  5. {
  6. internal static int FindSuitableMemoryTypeIndex(VulkanPhysicalDevice physicalDevice, uint memoryTypeBits,
  7. MemoryPropertyFlags flags)
  8. {
  9. physicalDevice.Api.GetPhysicalDeviceMemoryProperties(physicalDevice.InternalHandle, out var properties);
  10. for (var i = 0; i < properties.MemoryTypeCount; i++)
  11. {
  12. var type = properties.MemoryTypes[i];
  13. if ((memoryTypeBits & (1 << i)) != 0 && type.PropertyFlags.HasFlag(flags)) return i;
  14. }
  15. return -1;
  16. }
  17. internal static unsafe void TransitionLayout(VulkanDevice device,
  18. CommandBuffer commandBuffer,
  19. Image image,
  20. ImageLayout sourceLayout,
  21. AccessFlags sourceAccessMask,
  22. ImageLayout destinationLayout,
  23. AccessFlags destinationAccessMask,
  24. uint mipLevels)
  25. {
  26. var subresourceRange = new ImageSubresourceRange(ImageAspectFlags.ImageAspectColorBit, 0, mipLevels, 0, 1);
  27. var barrier = new ImageMemoryBarrier
  28. {
  29. SType = StructureType.ImageMemoryBarrier,
  30. SrcAccessMask = sourceAccessMask,
  31. DstAccessMask = destinationAccessMask,
  32. OldLayout = sourceLayout,
  33. NewLayout = destinationLayout,
  34. SrcQueueFamilyIndex = Vk.QueueFamilyIgnored,
  35. DstQueueFamilyIndex = Vk.QueueFamilyIgnored,
  36. Image = image,
  37. SubresourceRange = subresourceRange
  38. };
  39. device.Api.CmdPipelineBarrier(
  40. commandBuffer,
  41. PipelineStageFlags.PipelineStageAllCommandsBit,
  42. PipelineStageFlags.PipelineStageAllCommandsBit,
  43. 0,
  44. 0,
  45. null,
  46. 0,
  47. null,
  48. 1,
  49. barrier);
  50. }
  51. }
  52. }