TextureBuffer.cs 4.5 KB

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