TextureBuffer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Buffers;
  5. using System.Collections.Generic;
  6. using Format = Ryujinx.Graphics.GAL.Format;
  7. using VkFormat = Silk.NET.Vulkan.Format;
  8. namespace Ryujinx.Graphics.Vulkan
  9. {
  10. class TextureBuffer : ITexture
  11. {
  12. private readonly VulkanRenderer _gd;
  13. private BufferHandle _bufferHandle;
  14. private int _offset;
  15. private int _size;
  16. private Auto<DisposableBufferView> _bufferView;
  17. private int _bufferCount;
  18. public int Width { get; }
  19. public int Height { get; }
  20. public VkFormat VkFormat { get; }
  21. public TextureBuffer(VulkanRenderer gd, TextureCreateInfo info)
  22. {
  23. _gd = gd;
  24. Width = info.Width;
  25. Height = info.Height;
  26. VkFormat = FormatTable.GetFormat(info.Format);
  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 PinnedSpan<byte> GetData()
  46. {
  47. return _gd.GetBufferData(_bufferHandle, _offset, _size);
  48. }
  49. public PinnedSpan<byte> GetData(int layer, int level)
  50. {
  51. return GetData();
  52. }
  53. public void CopyTo(BufferRange range, int layer, int level, int stride)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. public void Release()
  58. {
  59. if (_gd.Textures.Remove(this))
  60. {
  61. ReleaseImpl();
  62. }
  63. }
  64. private void ReleaseImpl()
  65. {
  66. _bufferView?.Dispose();
  67. _bufferView = null;
  68. }
  69. /// <inheritdoc/>
  70. public void SetData(IMemoryOwner<byte> data)
  71. {
  72. _gd.SetBufferData(_bufferHandle, _offset, data.Memory.Span);
  73. data.Dispose();
  74. }
  75. /// <inheritdoc/>
  76. public void SetData(IMemoryOwner<byte> data, int layer, int level)
  77. {
  78. throw new NotSupportedException();
  79. }
  80. /// <inheritdoc/>
  81. public void SetData(IMemoryOwner<byte> data, int layer, int level, Rectangle<int> region)
  82. {
  83. throw new NotSupportedException();
  84. }
  85. public void SetStorage(BufferRange buffer)
  86. {
  87. if (_bufferHandle == buffer.Handle &&
  88. _offset == buffer.Offset &&
  89. _size == buffer.Size &&
  90. _bufferCount == _gd.BufferManager.BufferCount)
  91. {
  92. return;
  93. }
  94. _bufferHandle = buffer.Handle;
  95. _offset = buffer.Offset;
  96. _size = buffer.Size;
  97. _bufferCount = _gd.BufferManager.BufferCount;
  98. ReleaseImpl();
  99. }
  100. public BufferView GetBufferView(CommandBufferScoped cbs, bool write)
  101. {
  102. _bufferView ??= _gd.BufferManager.CreateView(_bufferHandle, VkFormat, _offset, _size, ReleaseImpl);
  103. return _bufferView?.Get(cbs, _offset, _size, write).Value ?? default;
  104. }
  105. }
  106. }