/ src / Ryujinx.Graphics.OpenGL / Image / TextureBuffer.cs
TextureBuffer.cs
  1  using OpenTK.Graphics.OpenGL;
  2  using Ryujinx.Common.Memory;
  3  using Ryujinx.Graphics.GAL;
  4  using System;
  5  
  6  namespace Ryujinx.Graphics.OpenGL.Image
  7  {
  8      class TextureBuffer : TextureBase, ITexture
  9      {
 10          private readonly OpenGLRenderer _renderer;
 11          private int _bufferOffset;
 12          private int _bufferSize;
 13          private int _bufferCount;
 14  
 15          private BufferHandle _buffer;
 16  
 17          public TextureBuffer(OpenGLRenderer renderer, TextureCreateInfo info) : base(info)
 18          {
 19              _renderer = renderer;
 20          }
 21  
 22          public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
 23          {
 24              throw new NotSupportedException();
 25          }
 26  
 27          public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
 28          {
 29              throw new NotSupportedException();
 30          }
 31  
 32          public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
 33          {
 34              throw new NotSupportedException();
 35          }
 36  
 37          public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
 38          {
 39              throw new NotSupportedException();
 40          }
 41  
 42          public PinnedSpan<byte> GetData()
 43          {
 44              return Buffer.GetData(_renderer, _buffer, _bufferOffset, _bufferSize);
 45          }
 46  
 47          public PinnedSpan<byte> GetData(int layer, int level)
 48          {
 49              return GetData();
 50          }
 51  
 52          public void CopyTo(BufferRange range, int layer, int level, int stride)
 53          {
 54              throw new NotImplementedException();
 55          }
 56  
 57          /// <inheritdoc/>
 58          public void SetData(MemoryOwner<byte> data)
 59          {
 60              var dataSpan = data.Span;
 61  
 62              Buffer.SetData(_buffer, _bufferOffset, dataSpan[..Math.Min(dataSpan.Length, _bufferSize)]);
 63  
 64              data.Dispose();
 65          }
 66  
 67          /// <inheritdoc/>
 68          public void SetData(MemoryOwner<byte> data, int layer, int level)
 69          {
 70              throw new NotSupportedException();
 71          }
 72  
 73          /// <inheritdoc/>
 74          public void SetData(MemoryOwner<byte> data, int layer, int level, Rectangle<int> region)
 75          {
 76              throw new NotSupportedException();
 77          }
 78  
 79          public void SetStorage(BufferRange buffer)
 80          {
 81              if (_buffer != BufferHandle.Null &&
 82                  _buffer == buffer.Handle &&
 83                  buffer.Offset == _bufferOffset &&
 84                  buffer.Size == _bufferSize &&
 85                  _renderer.BufferCount == _bufferCount)
 86              {
 87                  // Only rebind the buffer when more have been created.
 88                  return;
 89              }
 90  
 91              _buffer = buffer.Handle;
 92              _bufferOffset = buffer.Offset;
 93              _bufferSize = buffer.Size;
 94              _bufferCount = _renderer.BufferCount;
 95  
 96              Bind(0);
 97  
 98              SizedInternalFormat format = (SizedInternalFormat)FormatTable.GetFormatInfo(Info.Format).PixelInternalFormat;
 99  
100              GL.TexBufferRange(TextureBufferTarget.TextureBuffer, format, _buffer.ToInt32(), (IntPtr)buffer.Offset, buffer.Size);
101          }
102  
103          public void Dispose()
104          {
105              if (Handle != 0)
106              {
107                  GL.DeleteTexture(Handle);
108  
109                  Handle = 0;
110              }
111          }
112  
113          public void Release()
114          {
115              Dispose();
116          }
117      }
118  }