TextureBuffer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Release()
  56. {
  57. if (_gd.Textures.Remove(this))
  58. {
  59. ReleaseImpl();
  60. }
  61. }
  62. private void ReleaseImpl()
  63. {
  64. if (_selfManagedViews != null)
  65. {
  66. foreach (var bufferView in _selfManagedViews.Values)
  67. {
  68. bufferView.Dispose();
  69. }
  70. _selfManagedViews = null;
  71. }
  72. _bufferView?.Dispose();
  73. _bufferView = null;
  74. }
  75. public void SetData(SpanOrArray<byte> data)
  76. {
  77. _gd.SetBufferData(_bufferHandle, _offset, data);
  78. }
  79. public void SetData(SpanOrArray<byte> data, int layer, int level)
  80. {
  81. throw new NotSupportedException();
  82. }
  83. public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
  84. {
  85. throw new NotSupportedException();
  86. }
  87. public void SetStorage(BufferRange buffer)
  88. {
  89. if (_bufferHandle == buffer.Handle &&
  90. _offset == buffer.Offset &&
  91. _size == buffer.Size &&
  92. _bufferCount == _gd.BufferManager.BufferCount)
  93. {
  94. return;
  95. }
  96. _bufferHandle = buffer.Handle;
  97. _offset = buffer.Offset;
  98. _size = buffer.Size;
  99. _bufferCount = _gd.BufferManager.BufferCount;
  100. ReleaseImpl();
  101. }
  102. public BufferView GetBufferView(CommandBufferScoped cbs)
  103. {
  104. if (_bufferView == null)
  105. {
  106. _bufferView = _gd.BufferManager.CreateView(_bufferHandle, VkFormat, _offset, _size, ReleaseImpl);
  107. }
  108. return _bufferView?.Get(cbs, _offset, _size).Value ?? default;
  109. }
  110. public BufferView GetBufferView(CommandBufferScoped cbs, GAL.Format format)
  111. {
  112. var vkFormat = FormatTable.GetFormat(format);
  113. if (vkFormat == VkFormat)
  114. {
  115. return GetBufferView(cbs);
  116. }
  117. if (_selfManagedViews != null && _selfManagedViews.TryGetValue(format, out var bufferView))
  118. {
  119. return bufferView.Get(cbs, _offset, _size).Value;
  120. }
  121. bufferView = _gd.BufferManager.CreateView(_bufferHandle, vkFormat, _offset, _size, ReleaseImpl);
  122. if (bufferView != null)
  123. {
  124. (_selfManagedViews ??= new Dictionary<GAL.Format, Auto<DisposableBufferView>>()).Add(format, bufferView);
  125. }
  126. return bufferView?.Get(cbs, _offset, _size).Value ?? default;
  127. }
  128. }
  129. }