TextureBuffer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Collections.Generic;
  5. using VkFormat = Silk.NET.Vulkan.Format;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class TextureBuffer : ITexture
  9. {
  10. private readonly VulkanRenderer _gd;
  11. private BufferHandle _bufferHandle;
  12. private int _offset;
  13. private int _size;
  14. private Auto<DisposableBufferView> _bufferView;
  15. private Dictionary<GAL.Format, Auto<DisposableBufferView>> _selfManagedViews;
  16. public int Width { get; }
  17. public int Height { get; }
  18. public VkFormat VkFormat { get; }
  19. public float ScaleFactor { get; }
  20. public TextureBuffer(VulkanRenderer gd, TextureCreateInfo info, float scale)
  21. {
  22. _gd = gd;
  23. Width = info.Width;
  24. Height = info.Height;
  25. VkFormat = FormatTable.GetFormat(info.Format);
  26. ScaleFactor = scale;
  27. gd.Textures.Add(this);
  28. }
  29. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  30. {
  31. throw new NotSupportedException();
  32. }
  33. public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
  34. {
  35. throw new NotSupportedException();
  36. }
  37. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  38. {
  39. throw new NotSupportedException();
  40. }
  41. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  42. {
  43. throw new NotSupportedException();
  44. }
  45. public ReadOnlySpan<byte> GetData()
  46. {
  47. return _gd.GetBufferData(_bufferHandle, _offset, _size);
  48. }
  49. public ReadOnlySpan<byte> GetData(int layer, int level)
  50. {
  51. return GetData();
  52. }
  53. public void Release()
  54. {
  55. if (_gd.Textures.Remove(this))
  56. {
  57. ReleaseImpl();
  58. }
  59. }
  60. private void ReleaseImpl()
  61. {
  62. if (_selfManagedViews != null)
  63. {
  64. foreach (var bufferView in _selfManagedViews.Values)
  65. {
  66. bufferView.Dispose();
  67. }
  68. _selfManagedViews = null;
  69. }
  70. _bufferView?.Dispose();
  71. _bufferView = null;
  72. }
  73. public void SetData(ReadOnlySpan<byte> data)
  74. {
  75. _gd.SetBufferData(_bufferHandle, _offset, data);
  76. }
  77. public void SetData(ReadOnlySpan<byte> data, int layer, int level)
  78. {
  79. throw new NotSupportedException();
  80. }
  81. public void SetStorage(BufferRange buffer)
  82. {
  83. if (_bufferHandle == buffer.Handle &&
  84. _offset == buffer.Offset &&
  85. _size == buffer.Size)
  86. {
  87. return;
  88. }
  89. _bufferHandle = buffer.Handle;
  90. _offset = buffer.Offset;
  91. _size = buffer.Size;
  92. ReleaseImpl();
  93. }
  94. public BufferView GetBufferView(CommandBufferScoped cbs)
  95. {
  96. if (_bufferView == null)
  97. {
  98. _bufferView = _gd.BufferManager.CreateView(_bufferHandle, VkFormat, _offset, _size);
  99. }
  100. return _bufferView?.Get(cbs, _offset, _size).Value ?? default;
  101. }
  102. public BufferView GetBufferView(CommandBufferScoped cbs, GAL.Format format)
  103. {
  104. var vkFormat = FormatTable.GetFormat(format);
  105. if (vkFormat == VkFormat)
  106. {
  107. return GetBufferView(cbs);
  108. }
  109. if (_selfManagedViews != null && _selfManagedViews.TryGetValue(format, out var bufferView))
  110. {
  111. return bufferView.Get(cbs, _offset, _size).Value;
  112. }
  113. bufferView = _gd.BufferManager.CreateView(_bufferHandle, vkFormat, _offset, _size);
  114. if (bufferView != null)
  115. {
  116. (_selfManagedViews ??= new Dictionary<GAL.Format, Auto<DisposableBufferView>>()).Add(format, bufferView);
  117. }
  118. return bufferView?.Get(cbs, _offset, _size).Value ?? default;
  119. }
  120. }
  121. }