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