2019-12-28 18:45:33 -05:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using System;
|
|
|
|
|
2020-05-23 05:46:09 -04:00
|
|
|
namespace Ryujinx.Graphics.OpenGL.Image
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
class TextureView : TextureBase, ITexture
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-29 12:41:50 -05:00
|
|
|
private readonly Renderer _renderer;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
private readonly TextureStorage _parent;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
private TextureView _emulatedViewParent;
|
|
|
|
|
2020-03-29 08:48:39 -04:00
|
|
|
private TextureView _incompatibleFormatView;
|
|
|
|
|
|
|
|
public int FirstLayer { get; private set; }
|
|
|
|
public int FirstLevel { get; private set; }
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
public TextureView(
|
|
|
|
Renderer renderer,
|
|
|
|
TextureStorage parent,
|
|
|
|
TextureCreateInfo info,
|
|
|
|
int firstLayer,
|
2020-07-06 22:41:07 -04:00
|
|
|
int firstLevel) : base(info, parent.ScaleFactor)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
_renderer = renderer;
|
|
|
|
_parent = parent;
|
|
|
|
|
2020-03-29 08:48:39 -04:00
|
|
|
FirstLayer = firstLayer;
|
|
|
|
FirstLevel = firstLevel;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
CreateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CreateView()
|
|
|
|
{
|
|
|
|
TextureTarget target = Target.Convert();
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
PixelInternalFormat pixelInternalFormat;
|
|
|
|
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
|
|
|
pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixelInternalFormat = format.PixelInternalFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.TextureView(
|
|
|
|
Handle,
|
|
|
|
target,
|
|
|
|
_parent.Handle,
|
|
|
|
pixelInternalFormat,
|
2020-03-29 08:48:39 -04:00
|
|
|
FirstLevel,
|
2020-04-25 09:02:18 -04:00
|
|
|
Info.Levels,
|
2020-03-29 08:48:39 -04:00
|
|
|
FirstLayer,
|
2020-04-25 09:02:18 -04:00
|
|
|
Info.GetLayers());
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0);
|
|
|
|
|
|
|
|
GL.BindTexture(target, Handle);
|
|
|
|
|
|
|
|
int[] swizzleRgba = new int[]
|
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
(int)Info.SwizzleR.Convert(),
|
|
|
|
(int)Info.SwizzleG.Convert(),
|
|
|
|
(int)Info.SwizzleB.Convert(),
|
|
|
|
(int)Info.SwizzleA.Convert()
|
2019-10-13 02:02:07 -04:00
|
|
|
};
|
|
|
|
|
2020-07-25 23:03:40 -04:00
|
|
|
if (Info.Format.IsBgra8())
|
|
|
|
{
|
|
|
|
// Swap B <-> R for BGRA formats, as OpenGL has no support for them
|
|
|
|
// and we need to manually swap the components on read/write on the GPU.
|
|
|
|
int temp = swizzleRgba[0];
|
|
|
|
swizzleRgba[0] = swizzleRgba[2];
|
|
|
|
swizzleRgba[2] = temp;
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
int maxLevel = Info.Levels - 1;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (maxLevel < 0)
|
|
|
|
{
|
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
|
2020-04-25 09:02:18 -04:00
|
|
|
GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)Info.DepthStencilMode.Convert());
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
|
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
if (Info.IsCompressed == info.IsCompressed)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-03-29 08:48:39 -04:00
|
|
|
firstLayer += FirstLayer;
|
|
|
|
firstLevel += FirstLevel;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
return _parent.CreateView(info, firstLayer, firstLevel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-01 10:39:09 -05:00
|
|
|
// TODO: Most graphics APIs doesn't support creating a texture view from a compressed format
|
|
|
|
// with a non-compressed format (or vice-versa), however NVN seems to support it.
|
|
|
|
// So we emulate that here with a texture copy (see the first CopyTo overload).
|
|
|
|
// However right now it only does a single copy right after the view is created,
|
|
|
|
// so it doesn't work for all cases.
|
2020-07-06 22:41:07 -04:00
|
|
|
TextureView emulatedView = (TextureView)_renderer.CreateTexture(info, ScaleFactor);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
emulatedView._emulatedViewParent = this;
|
|
|
|
|
2020-03-29 08:48:39 -04:00
|
|
|
emulatedView.FirstLayer = firstLayer;
|
|
|
|
emulatedView.FirstLevel = firstLevel;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
return emulatedView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 08:48:39 -04:00
|
|
|
public int GetIncompatibleFormatViewHandle()
|
|
|
|
{
|
|
|
|
// AMD and Intel has a bug where the view format is always ignored,
|
|
|
|
// it uses the parent format instead.
|
|
|
|
// As workaround we create a new texture with the correct
|
|
|
|
// format, and then do a copy after the draw.
|
|
|
|
if (_parent.Info.Format != Format)
|
|
|
|
{
|
|
|
|
if (_incompatibleFormatView == null)
|
|
|
|
{
|
2020-07-06 22:41:07 -04:00
|
|
|
_incompatibleFormatView = (TextureView)_renderer.CreateTexture(Info, ScaleFactor);
|
2020-03-29 08:48:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:41:07 -04:00
|
|
|
TextureCopyUnscaled.Copy(_parent.Info, _incompatibleFormatView.Info, _parent.Handle, _incompatibleFormatView.Handle, FirstLayer, 0, FirstLevel, 0, ScaleFactor);
|
2020-03-29 08:48:39 -04:00
|
|
|
|
|
|
|
return _incompatibleFormatView.Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SignalModified()
|
|
|
|
{
|
|
|
|
if (_incompatibleFormatView != null)
|
|
|
|
{
|
2020-07-06 22:41:07 -04:00
|
|
|
TextureCopyUnscaled.Copy(_incompatibleFormatView.Info, _parent.Info, _incompatibleFormatView.Handle, _parent.Handle, 0, FirstLayer, 0, FirstLevel, ScaleFactor);
|
2020-03-29 08:48:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
TextureView destinationView = (TextureView)destination;
|
|
|
|
|
2020-07-06 22:41:07 -04:00
|
|
|
TextureCopyUnscaled.Copy(Info, destinationView.Info, Handle, destinationView.Handle, 0, firstLayer, 0, firstLevel, ScaleFactor);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (destinationView._emulatedViewParent != null)
|
|
|
|
{
|
|
|
|
TextureCopyUnscaled.Copy(
|
2020-04-25 09:02:18 -04:00
|
|
|
Info,
|
|
|
|
destinationView._emulatedViewParent.Info,
|
2020-03-29 08:48:39 -04:00
|
|
|
Handle,
|
|
|
|
destinationView._emulatedViewParent.Handle,
|
|
|
|
0,
|
|
|
|
destinationView.FirstLayer,
|
|
|
|
0,
|
2020-07-06 22:41:07 -04:00
|
|
|
destinationView.FirstLevel,
|
|
|
|
ScaleFactor);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
|
|
|
|
{
|
|
|
|
_renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
public byte[] GetData()
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
for (int level = 0; level < Info.Levels; level++)
|
2019-12-05 15:34:47 -05:00
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
size += Info.GetMipSize(level);
|
2019-12-05 15:34:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
byte[] data = new byte[size];
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
fixed (byte* ptr = data)
|
|
|
|
{
|
|
|
|
WriteTo((IntPtr)ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-07-25 23:03:40 -04:00
|
|
|
public void WriteToPbo(int offset, bool forceBgra)
|
|
|
|
{
|
|
|
|
WriteTo(IntPtr.Zero + offset, forceBgra);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteTo(IntPtr data, bool forceBgra = false)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
TextureTarget target = Target.Convert();
|
|
|
|
|
|
|
|
Bind(target, 0);
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-07-25 23:03:40 -04:00
|
|
|
PixelFormat pixelFormat = format.PixelFormat;
|
|
|
|
PixelType pixelType = format.PixelType;
|
|
|
|
|
|
|
|
if (forceBgra)
|
|
|
|
{
|
|
|
|
pixelFormat = PixelFormat.Bgra;
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
int faces = 1;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (target == TextureTarget.TextureCubeMap)
|
|
|
|
{
|
2019-12-05 15:34:47 -05:00
|
|
|
target = TextureTarget.TextureCubeMapPositiveX;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
faces = 6;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
for (int level = 0; level < Info.Levels; level++)
|
2019-12-05 15:34:47 -05:00
|
|
|
{
|
|
|
|
for (int face = 0; face < faces; face++)
|
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
int faceOffset = face * Info.GetMipSize2D(level);
|
2019-12-05 15:34:47 -05:00
|
|
|
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
2020-07-25 23:03:40 -04:00
|
|
|
GL.GetCompressedTexImage(target + face, level, data + faceOffset);
|
2019-12-05 15:34:47 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-25 23:03:40 -04:00
|
|
|
GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
|
2019-12-05 15:34:47 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-07-25 23:03:40 -04:00
|
|
|
data += Info.GetMipSize(level);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:27:50 -05:00
|
|
|
public void SetData(ReadOnlySpan<byte> data)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
fixed (byte* ptr = data)
|
|
|
|
{
|
2020-07-25 23:03:40 -04:00
|
|
|
ReadFrom((IntPtr)ptr, data.Length);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 23:03:40 -04:00
|
|
|
public void ReadFromPbo(int offset, int size)
|
|
|
|
{
|
|
|
|
ReadFrom(IntPtr.Zero + offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ReadFrom(IntPtr data, int size)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
TextureTarget target = Target.Convert();
|
|
|
|
|
|
|
|
Bind(target, 0);
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
int width = Info.Width;
|
|
|
|
int height = Info.Height;
|
|
|
|
int depth = Info.Depth;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
for (int level = 0; level < Info.Levels; level++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
int mipSize = Info.GetMipSize(level);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int endOffset = offset + mipSize;
|
|
|
|
|
|
|
|
if ((uint)endOffset > (uint)size)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
switch (Info.Target)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
case Target.Texture1D:
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
|
|
|
GL.CompressedTexSubImage1D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
format.PixelFormat,
|
|
|
|
mipSize,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.TexSubImage1D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
format.PixelFormat,
|
|
|
|
format.PixelType,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Target.Texture1DArray:
|
|
|
|
case Target.Texture2D:
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
|
|
|
GL.CompressedTexSubImage2D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format.PixelFormat,
|
|
|
|
mipSize,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.TexSubImage2D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format.PixelFormat,
|
|
|
|
format.PixelType,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Target.Texture2DArray:
|
|
|
|
case Target.Texture3D:
|
|
|
|
case Target.CubemapArray:
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
|
|
|
GL.CompressedTexSubImage3D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
depth,
|
|
|
|
format.PixelFormat,
|
|
|
|
mipSize,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.TexSubImage3D(
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
depth,
|
|
|
|
format.PixelFormat,
|
|
|
|
format.PixelType,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Target.Cubemap:
|
|
|
|
int faceOffset = 0;
|
|
|
|
|
|
|
|
for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
|
|
|
|
{
|
|
|
|
if (format.IsCompressed)
|
|
|
|
{
|
|
|
|
GL.CompressedTexSubImage2D(
|
|
|
|
TextureTarget.TextureCubeMapPositiveX + face,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format.PixelFormat,
|
|
|
|
mipSize / 6,
|
|
|
|
data + faceOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.TexSubImage2D(
|
|
|
|
TextureTarget.TextureCubeMapPositiveX + face,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format.PixelFormat,
|
|
|
|
format.PixelType,
|
|
|
|
data + faceOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
data += mipSize;
|
|
|
|
offset += mipSize;
|
|
|
|
|
|
|
|
width = Math.Max(1, width >> 1);
|
|
|
|
height = Math.Max(1, height >> 1);
|
|
|
|
|
|
|
|
if (Target == Target.Texture3D)
|
|
|
|
{
|
|
|
|
depth = Math.Max(1, depth >> 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:02:18 -04:00
|
|
|
public void SetStorage(BufferRange buffer)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:02:18 -04:00
|
|
|
throw new NotSupportedException();
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-03-29 08:48:39 -04:00
|
|
|
if (_incompatibleFormatView != null)
|
|
|
|
{
|
|
|
|
_incompatibleFormatView.Dispose();
|
|
|
|
|
|
|
|
_incompatibleFormatView = null;
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
if (Handle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteTexture(Handle);
|
|
|
|
|
|
|
|
_parent.DecrementViewsCount();
|
|
|
|
|
|
|
|
Handle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|