TextureBuffer.cs 4.7 KB

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