VulkanMemoryHelper.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Silk.NET.Vulkan;
  2. namespace GpuInterop.VulkanDemo;
  3. internal static class VulkanMemoryHelper
  4. {
  5. internal static int FindSuitableMemoryTypeIndex(Vk api, PhysicalDevice physicalDevice, uint memoryTypeBits,
  6. MemoryPropertyFlags flags)
  7. {
  8. api.GetPhysicalDeviceMemoryProperties(physicalDevice, out var properties);
  9. for (var i = 0; i < properties.MemoryTypeCount; i++)
  10. {
  11. var type = properties.MemoryTypes[i];
  12. if ((memoryTypeBits & (1 << i)) != 0 && type.PropertyFlags.HasFlag(flags)) return i;
  13. }
  14. return -1;
  15. }
  16. internal static unsafe void TransitionLayout(
  17. Vk api,
  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. 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. }