Ryujinx/Ryujinx.Graphics.Gpu/Image/Texture.cs

762 lines
23 KiB
C#
Raw Normal View History

2019-10-13 08:02:07 +02:00
using Ryujinx.Common;
2019-12-29 00:45:33 +01:00
using Ryujinx.Common.Logging;
2019-10-13 08:02:07 +02:00
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Texture;
using Ryujinx.Graphics.Texture.Astc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
2019-10-13 08:02:07 +02:00
namespace Ryujinx.Graphics.Gpu.Image
{
class Texture : IRange<Texture>
{
private GpuContext _context;
private SizeInfo _sizeInfo;
2019-12-29 18:41:50 +01:00
public Format Format => Info.FormatInfo.Format;
2019-10-13 08:02:07 +02:00
2019-12-29 18:41:50 +01:00
public TextureInfo Info { get; private set; }
2019-10-13 08:02:07 +02:00
private int _depth;
private int _layers;
2019-12-29 18:41:50 +01:00
private readonly int _firstLayer;
private readonly int _firstLevel;
2019-10-13 08:02:07 +02:00
private bool _hasData;
private ITexture _arrayViewTexture;
private Target _arrayViewTarget;
private Texture _viewStorage;
private List<Texture> _views;
public ITexture HostTexture { get; private set; }
public LinkedListNode<Texture> CacheNode { get; set; }
public bool Modified { get; set; }
2019-12-29 18:41:50 +01:00
public ulong Address => Info.Address;
public ulong EndAddress => Info.Address + Size;
2019-10-13 08:02:07 +02:00
public ulong Size => (ulong)_sizeInfo.TotalSize;
private int _referenceCount;
private int _sequenceNumber;
private Texture(
GpuContext context,
TextureInfo info,
SizeInfo sizeInfo,
int firstLayer,
int firstLevel)
{
InitializeTexture(context, info, sizeInfo);
_firstLayer = firstLayer;
_firstLevel = firstLevel;
_hasData = true;
}
public Texture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
{
InitializeTexture(context, info, sizeInfo);
TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, context.Capabilities);
HostTexture = _context.Renderer.CreateTexture(createInfo);
}
private void InitializeTexture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
{
_context = context;
_sizeInfo = sizeInfo;
SetInfo(info);
_viewStorage = this;
_views = new List<Texture>();
}
public Texture CreateView(TextureInfo info, SizeInfo sizeInfo, int firstLayer, int firstLevel)
{
Texture texture = new Texture(
_context,
info,
sizeInfo,
_firstLayer + firstLayer,
_firstLevel + firstLevel);
TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, _context.Capabilities);
texture.HostTexture = HostTexture.CreateView(createInfo, firstLayer, firstLevel);
_viewStorage.AddView(texture);
return texture;
}
private void AddView(Texture texture)
{
_views.Add(texture);
texture._viewStorage = this;
}
private void RemoveView(Texture texture)
{
_views.Remove(texture);
texture._viewStorage = null;
DeleteIfNotUsed();
2019-10-13 08:02:07 +02:00
}
public void ChangeSize(int width, int height, int depthOrLayers)
{
width <<= _firstLevel;
height <<= _firstLevel;
2019-12-29 18:41:50 +01:00
if (Info.Target == Target.Texture3D)
2019-10-13 08:02:07 +02:00
{
depthOrLayers <<= _firstLevel;
}
else
{
2019-12-29 18:41:50 +01:00
depthOrLayers = _viewStorage.Info.DepthOrLayers;
2019-10-13 08:02:07 +02:00
}
_viewStorage.RecreateStorageOrView(width, height, depthOrLayers);
foreach (Texture view in _viewStorage._views)
{
int viewWidth = Math.Max(1, width >> view._firstLevel);
int viewHeight = Math.Max(1, height >> view._firstLevel);
int viewDepthOrLayers;
2019-12-29 18:41:50 +01:00
if (view.Info.Target == Target.Texture3D)
2019-10-13 08:02:07 +02:00
{
viewDepthOrLayers = Math.Max(1, depthOrLayers >> view._firstLevel);
}
else
{
2019-12-29 18:41:50 +01:00
viewDepthOrLayers = view.Info.DepthOrLayers;
2019-10-13 08:02:07 +02:00
}
view.RecreateStorageOrView(viewWidth, viewHeight, viewDepthOrLayers);
}
}
private void RecreateStorageOrView(int width, int height, int depthOrLayers)
{
SetInfo(new TextureInfo(
2019-12-29 18:41:50 +01:00
Info.Address,
2019-10-13 08:02:07 +02:00
width,
height,
depthOrLayers,
2019-12-29 18:41:50 +01:00
Info.Levels,
Info.SamplesInX,
Info.SamplesInY,
Info.Stride,
Info.IsLinear,
Info.GobBlocksInY,
Info.GobBlocksInZ,
Info.GobBlocksInTileX,
Info.Target,
Info.FormatInfo,
Info.DepthStencilMode,
Info.SwizzleR,
Info.SwizzleG,
Info.SwizzleB,
Info.SwizzleA));
TextureCreateInfo createInfo = TextureManager.GetCreateInfo(Info, _context.Capabilities);
2019-10-13 08:02:07 +02:00
if (_viewStorage != this)
{
ReplaceStorage(_viewStorage.HostTexture.CreateView(createInfo, _firstLayer, _firstLevel));
}
else
{
ITexture newStorage = _context.Renderer.CreateTexture(createInfo);
HostTexture.CopyTo(newStorage, 0, 0);
2019-10-13 08:02:07 +02:00
ReplaceStorage(newStorage);
}
}
public void SynchronizeMemory()
{
if (_sequenceNumber == _context.SequenceNumber && _hasData)
{
return;
}
_sequenceNumber = _context.SequenceNumber;
bool modified = _context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.Texture).Length != 0;
2019-10-13 08:02:07 +02:00
if (!modified && _hasData)
{
return;
}
Span<byte> data = _context.PhysicalMemory.Read(Address, Size);
2019-12-29 18:41:50 +01:00
if (Info.IsLinear)
2019-10-13 08:02:07 +02:00
{
data = LayoutConverter.ConvertLinearStridedToLinear(
2019-12-29 18:41:50 +01:00
Info.Width,
Info.Height,
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.Stride,
Info.FormatInfo.BytesPerPixel,
2019-10-13 08:02:07 +02:00
data);
}
else
{
data = LayoutConverter.ConvertBlockLinearToLinear(
2019-12-29 18:41:50 +01:00
Info.Width,
Info.Height,
2019-10-13 08:02:07 +02:00
_depth,
2019-12-29 18:41:50 +01:00
Info.Levels,
2019-10-13 08:02:07 +02:00
_layers,
2019-12-29 18:41:50 +01:00
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.FormatInfo.BytesPerPixel,
Info.GobBlocksInY,
Info.GobBlocksInZ,
Info.GobBlocksInTileX,
2019-10-13 08:02:07 +02:00
_sizeInfo,
data);
}
2019-12-29 18:41:50 +01:00
if (!_context.Capabilities.SupportsAstcCompression && Info.FormatInfo.Format.IsAstc())
2019-10-13 08:02:07 +02:00
{
if (!AstcDecoder.TryDecodeToRgba8(
data.ToArray(),
2019-12-29 18:41:50 +01:00
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.Width,
Info.Height,
_depth,
2019-12-29 18:41:50 +01:00
Info.Levels,
out Span<byte> decoded))
{
2019-12-29 18:41:50 +01:00
string texInfo = $"{Info.Target} {Info.FormatInfo.Format} {Info.Width}x{Info.Height}x{Info.DepthOrLayers} levels {Info.Levels}";
2019-12-29 00:45:33 +01:00
2019-12-29 18:41:50 +01:00
Logger.PrintError(LogClass.Gpu, $"Invalid ASTC texture at 0x{Info.Address:X} ({texInfo}).");
}
data = decoded;
2019-10-13 08:02:07 +02:00
}
HostTexture.SetData(data);
_hasData = true;
}
public void Flush()
{
Span<byte> data = HostTexture.GetData();
2019-12-29 18:41:50 +01:00
if (Info.IsLinear)
{
data = LayoutConverter.ConvertLinearToLinearStrided(
2019-12-29 18:41:50 +01:00
Info.Width,
Info.Height,
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.Stride,
Info.FormatInfo.BytesPerPixel,
data);
}
else
{
data = LayoutConverter.ConvertLinearToBlockLinear(
2019-12-29 18:41:50 +01:00
Info.Width,
Info.Height,
_depth,
2019-12-29 18:41:50 +01:00
Info.Levels,
_layers,
2019-12-29 18:41:50 +01:00
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.FormatInfo.BytesPerPixel,
Info.GobBlocksInY,
Info.GobBlocksInZ,
Info.GobBlocksInTileX,
_sizeInfo,
data);
}
2019-10-13 08:02:07 +02:00
_context.PhysicalMemory.Write(Address, data);
}
public bool IsPerfectMatch(TextureInfo info, TextureSearchFlags flags)
{
if (!FormatMatches(info, (flags & TextureSearchFlags.Strict) != 0))
{
return false;
}
if (!LayoutMatches(info))
{
return false;
}
if (!SizeMatches(info, (flags & TextureSearchFlags.Strict) == 0))
{
return false;
}
if ((flags & TextureSearchFlags.Sampler) != 0)
{
if (!SamplerParamsMatches(info))
{
return false;
}
}
if ((flags & TextureSearchFlags.IgnoreMs) != 0)
{
2019-12-29 18:41:50 +01:00
bool msTargetCompatible = Info.Target == Target.Texture2DMultisample && info.Target == Target.Texture2D;
2019-10-13 08:02:07 +02:00
if (!msTargetCompatible && !TargetAndSamplesCompatible(info))
{
return false;
}
}
else if (!TargetAndSamplesCompatible(info))
{
return false;
}
2019-12-29 18:41:50 +01:00
return Info.Address == info.Address && Info.Levels == info.Levels;
2019-10-13 08:02:07 +02:00
}
private bool FormatMatches(TextureInfo info, bool strict)
{
// D32F and R32F texture have the same representation internally,
// however the R32F format is used to sample from depth textures.
2019-12-29 18:41:50 +01:00
if (Info.FormatInfo.Format == Format.D32Float && info.FormatInfo.Format == Format.R32Float && !strict)
2019-10-13 08:02:07 +02:00
{
return true;
}
2019-12-29 18:41:50 +01:00
return Info.FormatInfo.Format == info.FormatInfo.Format;
2019-10-13 08:02:07 +02:00
}
private bool LayoutMatches(TextureInfo info)
{
2019-12-29 18:41:50 +01:00
if (Info.IsLinear != info.IsLinear)
2019-10-13 08:02:07 +02:00
{
return false;
}
// For linear textures, gob block sizes are ignored.
// For block linear textures, the stride is ignored.
if (info.IsLinear)
{
2019-12-29 18:41:50 +01:00
return Info.Stride == info.Stride;
2019-10-13 08:02:07 +02:00
}
else
{
2019-12-29 18:41:50 +01:00
return Info.GobBlocksInY == info.GobBlocksInY &&
Info.GobBlocksInZ == info.GobBlocksInZ;
2019-10-13 08:02:07 +02:00
}
}
public bool SizeMatches(TextureInfo info)
{
return SizeMatches(info, alignSizes: false);
}
public bool SizeMatches(TextureInfo info, int level)
{
2019-12-29 18:41:50 +01:00
return Math.Max(1, Info.Width >> level) == info.Width &&
Math.Max(1, Info.Height >> level) == info.Height &&
Math.Max(1, Info.GetDepth() >> level) == info.GetDepth();
2019-10-13 08:02:07 +02:00
}
private bool SizeMatches(TextureInfo info, bool alignSizes)
{
2019-12-29 18:41:50 +01:00
if (Info.GetLayers() != info.GetLayers())
2019-10-13 08:02:07 +02:00
{
return false;
}
if (alignSizes)
{
2019-12-29 18:41:50 +01:00
Size size0 = GetAlignedSize(Info);
2019-10-13 08:02:07 +02:00
Size size1 = GetAlignedSize(info);
return size0.Width == size1.Width &&
size0.Height == size1.Height &&
size0.Depth == size1.Depth;
}
else
{
2019-12-29 18:41:50 +01:00
return Info.Width == info.Width &&
Info.Height == info.Height &&
Info.GetDepth() == info.GetDepth();
2019-10-13 08:02:07 +02:00
}
}
private bool SamplerParamsMatches(TextureInfo info)
{
2019-12-29 18:41:50 +01:00
return Info.DepthStencilMode == info.DepthStencilMode &&
Info.SwizzleR == info.SwizzleR &&
Info.SwizzleG == info.SwizzleG &&
Info.SwizzleB == info.SwizzleB &&
Info.SwizzleA == info.SwizzleA;
2019-10-13 08:02:07 +02:00
}
private bool TargetAndSamplesCompatible(TextureInfo info)
{
2019-12-29 18:41:50 +01:00
return Info.Target == info.Target &&
Info.SamplesInX == info.SamplesInX &&
Info.SamplesInY == info.SamplesInY;
2019-10-13 08:02:07 +02:00
}
public bool IsViewCompatible(
TextureInfo info,
ulong size,
out int firstLayer,
out int firstLevel)
{
return IsViewCompatible(info, size, isCopy: false, out firstLayer, out firstLevel);
}
public bool IsViewCompatible(
TextureInfo info,
ulong size,
bool isCopy,
out int firstLayer,
out int firstLevel)
2019-10-13 08:02:07 +02:00
{
// Out of range.
if (info.Address < Address || info.Address + size > EndAddress)
{
firstLayer = 0;
firstLevel = 0;
return false;
}
int offset = (int)(info.Address - Address);
if (!_sizeInfo.FindView(offset, (int)size, out firstLayer, out firstLevel))
{
return false;
}
if (!ViewLayoutCompatible(info, firstLevel))
{
return false;
}
if (!ViewFormatCompatible(info))
{
return false;
}
if (!ViewSizeMatches(info, firstLevel, isCopy))
2019-10-13 08:02:07 +02:00
{
return false;
}
if (!ViewTargetCompatible(info, isCopy))
2019-10-13 08:02:07 +02:00
{
return false;
}
2019-12-29 18:41:50 +01:00
return Info.SamplesInX == info.SamplesInX &&
Info.SamplesInY == info.SamplesInY;
2019-10-13 08:02:07 +02:00
}
private bool ViewLayoutCompatible(TextureInfo info, int level)
{
2019-12-29 18:41:50 +01:00
if (Info.IsLinear != info.IsLinear)
2019-10-13 08:02:07 +02:00
{
return false;
}
// For linear textures, gob block sizes are ignored.
// For block linear textures, the stride is ignored.
if (info.IsLinear)
{
2019-12-29 18:41:50 +01:00
int width = Math.Max(1, Info.Width >> level);
2019-10-13 08:02:07 +02:00
2019-12-29 18:41:50 +01:00
int stride = width * Info.FormatInfo.BytesPerPixel;
2019-10-13 08:02:07 +02:00
stride = BitUtils.AlignUp(stride, 32);
return stride == info.Stride;
}
else
{
2019-12-29 18:41:50 +01:00
int height = Math.Max(1, Info.Height >> level);
int depth = Math.Max(1, Info.GetDepth() >> level);
2019-10-13 08:02:07 +02:00
(int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
height,
depth,
2019-12-29 18:41:50 +01:00
Info.FormatInfo.BlockHeight,
Info.GobBlocksInY,
Info.GobBlocksInZ);
2019-10-13 08:02:07 +02:00
return gobBlocksInY == info.GobBlocksInY &&
gobBlocksInZ == info.GobBlocksInZ;
}
}
private bool ViewFormatCompatible(TextureInfo info)
{
2019-12-29 18:41:50 +01:00
return TextureCompatibility.FormatCompatible(Info.FormatInfo, info.FormatInfo);
2019-10-13 08:02:07 +02:00
}
private bool ViewSizeMatches(TextureInfo info, int level, bool isCopy)
2019-10-13 08:02:07 +02:00
{
2019-12-29 18:41:50 +01:00
Size size = GetAlignedSize(Info, level);
2019-10-13 08:02:07 +02:00
Size otherSize = GetAlignedSize(info);
// For copies, we can copy a subset of the 3D texture slices,
// so the depth may be different in this case.
if (!isCopy && info.Target == Target.Texture3D && size.Depth != otherSize.Depth)
{
return false;
}
return size.Width == otherSize.Width &&
size.Height == otherSize.Height;
2019-10-13 08:02:07 +02:00
}
private bool ViewTargetCompatible(TextureInfo info, bool isCopy)
2019-10-13 08:02:07 +02:00
{
2019-12-29 18:41:50 +01:00
switch (Info.Target)
2019-10-13 08:02:07 +02:00
{
case Target.Texture1D:
case Target.Texture1DArray:
return info.Target == Target.Texture1D ||
info.Target == Target.Texture1DArray;
case Target.Texture2D:
return info.Target == Target.Texture2D ||
info.Target == Target.Texture2DArray;
case Target.Texture2DArray:
case Target.Cubemap:
case Target.CubemapArray:
return info.Target == Target.Texture2D ||
info.Target == Target.Texture2DArray ||
info.Target == Target.Cubemap ||
info.Target == Target.CubemapArray;
case Target.Texture2DMultisample:
case Target.Texture2DMultisampleArray:
return info.Target == Target.Texture2DMultisample ||
info.Target == Target.Texture2DMultisampleArray;
case Target.Texture3D:
return info.Target == Target.Texture3D ||
(info.Target == Target.Texture2D && isCopy);
2019-10-13 08:02:07 +02:00
}
return false;
}
private static Size GetAlignedSize(TextureInfo info, int level = 0)
{
int width = Math.Max(1, info.Width >> level);
int height = Math.Max(1, info.Height >> level);
if (info.IsLinear)
{
return SizeCalculator.GetLinearAlignedSize(
width,
height,
info.FormatInfo.BlockWidth,
info.FormatInfo.BlockHeight,
info.FormatInfo.BytesPerPixel);
}
else
{
int depth = Math.Max(1, info.GetDepth() >> level);
(int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
height,
depth,
info.FormatInfo.BlockHeight,
info.GobBlocksInY,
info.GobBlocksInZ);
return SizeCalculator.GetBlockLinearAlignedSize(
width,
height,
depth,
info.FormatInfo.BlockWidth,
info.FormatInfo.BlockHeight,
info.FormatInfo.BytesPerPixel,
gobBlocksInY,
gobBlocksInZ,
info.GobBlocksInTileX);
}
}
public ITexture GetTargetTexture(Target target)
{
2019-12-29 18:41:50 +01:00
if (target == Info.Target)
2019-10-13 08:02:07 +02:00
{
return HostTexture;
}
if (_arrayViewTexture == null && IsSameDimensionsTarget(target))
{
TextureCreateInfo createInfo = new TextureCreateInfo(
2019-12-29 18:41:50 +01:00
Info.Width,
Info.Height,
2019-10-13 08:02:07 +02:00
target == Target.CubemapArray ? 6 : 1,
2019-12-29 18:41:50 +01:00
Info.Levels,
Info.Samples,
Info.FormatInfo.BlockWidth,
Info.FormatInfo.BlockHeight,
Info.FormatInfo.BytesPerPixel,
Info.FormatInfo.Format,
Info.DepthStencilMode,
2019-10-13 08:02:07 +02:00
target,
2019-12-29 18:41:50 +01:00
Info.SwizzleR,
Info.SwizzleG,
Info.SwizzleB,
Info.SwizzleA);
2019-10-13 08:02:07 +02:00
ITexture viewTexture = HostTexture.CreateView(createInfo, 0, 0);
_arrayViewTexture = viewTexture;
_arrayViewTarget = target;
return viewTexture;
}
else if (_arrayViewTarget == target)
{
return _arrayViewTexture;
}
return null;
}
private bool IsSameDimensionsTarget(Target target)
{
2019-12-29 18:41:50 +01:00
switch (Info.Target)
2019-10-13 08:02:07 +02:00
{
case Target.Texture1D:
case Target.Texture1DArray:
return target == Target.Texture1D ||
target == Target.Texture1DArray;
case Target.Texture2D:
case Target.Texture2DArray:
return target == Target.Texture2D ||
target == Target.Texture2DArray;
case Target.Cubemap:
case Target.CubemapArray:
return target == Target.Cubemap ||
target == Target.CubemapArray;
case Target.Texture2DMultisample:
case Target.Texture2DMultisampleArray:
return target == Target.Texture2DMultisample ||
target == Target.Texture2DMultisampleArray;
case Target.Texture3D:
return target == Target.Texture3D;
}
return false;
}
public void ReplaceView(Texture parent, TextureInfo info, ITexture hostTexture)
{
ReplaceStorage(hostTexture);
parent._viewStorage.AddView(this);
SetInfo(info);
}
private void SetInfo(TextureInfo info)
{
2019-12-29 18:41:50 +01:00
Info = info;
2019-10-13 08:02:07 +02:00
_depth = info.GetDepth();
_layers = info.GetLayers();
}
private void ReplaceStorage(ITexture hostTexture)
{
DisposeTextures();
HostTexture = hostTexture;
}
public bool OverlapsWith(ulong address, ulong size)
{
return Address < address + size && address < EndAddress;
}
public void IncrementReferenceCount()
{
_referenceCount++;
}
public void DecrementReferenceCount()
{
int newRefCount = --_referenceCount;
if (newRefCount == 0)
2019-10-13 08:02:07 +02:00
{
if (_viewStorage != this)
{
_viewStorage.RemoveView(this);
}
_context.Methods.TextureManager.RemoveTextureFromCache(this);
}
Debug.Assert(newRefCount >= 0);
2019-10-13 08:02:07 +02:00
DeleteIfNotUsed();
}
private void DeleteIfNotUsed()
{
// We can delete the texture as long it is not being used
// in any cache (the reference count is 0 in this case), and
// also all views that may be created from this texture were
// already deleted (views count is 0).
if (_referenceCount == 0 && _views.Count == 0)
{
2019-10-13 08:02:07 +02:00
DisposeTextures();
}
}
private void DisposeTextures()
{
HostTexture.Dispose();
_arrayViewTexture?.Dispose();
_arrayViewTexture = null;
}
}
}