From 02872833b6da02a20e331305caf05f722e6c8e68 Mon Sep 17 00:00:00 2001 From: riperiperi Date: Tue, 10 Nov 2020 00:41:13 +0000 Subject: Size hints for copy regions and viewport dimensions to avoid data loss (#1686) * Size hints for copy regions and viewport dimensions to avoid data loss * Reword comment. * Use info for the rule rather than calculating aligned size. * Reorder min/max, remove spaces --- Ryujinx.Graphics.Gpu/Image/Texture.cs | 8 +++ Ryujinx.Graphics.Gpu/Image/TextureManager.cs | 80 +++++++++++++++++++--------- Ryujinx.Graphics.Gpu/Image/TexturePool.cs | 16 ++++++ 3 files changed, 80 insertions(+), 24 deletions(-) (limited to 'Ryujinx.Graphics.Gpu/Image') diff --git a/Ryujinx.Graphics.Gpu/Image/Texture.cs b/Ryujinx.Graphics.Gpu/Image/Texture.cs index b1d6af9b..5b3a01c5 100644 --- a/Ryujinx.Graphics.Gpu/Image/Texture.cs +++ b/Ryujinx.Graphics.Gpu/Image/Texture.cs @@ -50,6 +50,12 @@ namespace Ryujinx.Graphics.Gpu.Image /// public bool IsModified { get; internal set; } + /// + /// Set when a texture has been changed size. This indicates that it may need to be + /// changed again when obtained as a sampler. + /// + public bool ChangedSize { get; internal set; } + private int _depth; private int _layers; private int _firstLayer; @@ -353,6 +359,8 @@ namespace Ryujinx.Graphics.Gpu.Image /// The new texture depth (for 3D textures) or layers (for layered textures) private void RecreateStorageOrView(int width, int height, int depthOrLayers) { + ChangedSize = true; + SetInfo(new TextureInfo( Info.Address, width, diff --git a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs index 0694f3e3..f1d31f4f 100644 --- a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs +++ b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs @@ -453,8 +453,9 @@ namespace Ryujinx.Graphics.Gpu.Image /// Copy texture to find or create /// Format information of the copy texture /// Indicates if the texture should be scaled from the start + /// A hint indicating the minimum used size for the texture /// The texture - public Texture FindOrCreateTexture(CopyTexture copyTexture, FormatInfo formatInfo, bool preferScaling = true) + public Texture FindOrCreateTexture(CopyTexture copyTexture, FormatInfo formatInfo, bool preferScaling = true, Size? sizeHint = null) { ulong address = _context.MemoryManager.Translate(copyTexture.Address.Pack()); @@ -500,7 +501,7 @@ namespace Ryujinx.Graphics.Gpu.Image flags |= TextureSearchFlags.WithUpscale; } - Texture texture = FindOrCreateTexture(info, flags); + Texture texture = FindOrCreateTexture(info, flags, sizeHint); texture.SynchronizeMemory(); @@ -513,8 +514,9 @@ namespace Ryujinx.Graphics.Gpu.Image /// Color buffer texture to find or create /// Number of samples in the X direction, for MSAA /// Number of samples in the Y direction, for MSAA + /// A hint indicating the minimum used size for the texture /// The texture - public Texture FindOrCreateTexture(RtColorState colorState, int samplesInX, int samplesInY) + public Texture FindOrCreateTexture(RtColorState colorState, int samplesInX, int samplesInY, Size sizeHint) { ulong address = _context.MemoryManager.Translate(colorState.Address.Pack()); @@ -583,7 +585,7 @@ namespace Ryujinx.Graphics.Gpu.Image target, formatInfo); - Texture texture = FindOrCreateTexture(info, TextureSearchFlags.WithUpscale); + Texture texture = FindOrCreateTexture(info, TextureSearchFlags.WithUpscale, sizeHint); texture.SynchronizeMemory(); @@ -597,8 +599,9 @@ namespace Ryujinx.Graphics.Gpu.Image /// Size of the depth-stencil texture /// Number of samples in the X direction, for MSAA /// Number of samples in the Y direction, for MSAA + /// A hint indicating the minimum used size for the texture /// The texture - public Texture FindOrCreateTexture(RtDepthStencilState dsState, Size3D size, int samplesInX, int samplesInY) + public Texture FindOrCreateTexture(RtDepthStencilState dsState, Size3D size, int samplesInX, int samplesInY, Size sizeHint) { ulong address = _context.MemoryManager.Translate(dsState.Address.Pack()); @@ -632,7 +635,7 @@ namespace Ryujinx.Graphics.Gpu.Image target, formatInfo); - Texture texture = FindOrCreateTexture(info, TextureSearchFlags.WithUpscale); + Texture texture = FindOrCreateTexture(info, TextureSearchFlags.WithUpscale, sizeHint); texture.SynchronizeMemory(); @@ -644,8 +647,9 @@ namespace Ryujinx.Graphics.Gpu.Image /// /// Texture information of the texture to be found or created /// The texture search flags, defines texture comparison rules + /// A hint indicating the minimum used size for the texture /// The texture - public Texture FindOrCreateTexture(TextureInfo info, TextureSearchFlags flags = TextureSearchFlags.None) + public Texture FindOrCreateTexture(TextureInfo info, TextureSearchFlags flags = TextureSearchFlags.None, Size? sizeHint = null) { bool isSamplerTexture = (flags & TextureSearchFlags.ForSampler) != 0; @@ -678,14 +682,8 @@ namespace Ryujinx.Graphics.Gpu.Image // deletion. _cache.Lift(overlap); } - else if (!TextureCompatibility.SizeMatches(overlap.Info, info)) - { - // If this is used for sampling, the size must match, - // otherwise the shader would sample garbage data. - // To fix that, we create a new texture with the correct - // size, and copy the data from the old one to the new one. - overlap.ChangeSize(info.Width, info.Height, info.DepthOrLayers); - } + + ChangeSizeIfNeeded(info, overlap, isSamplerTexture, sizeHint); overlap.SynchronizeMemory(); @@ -741,25 +739,21 @@ namespace Ryujinx.Graphics.Gpu.Image if (overlapCompatibility == TextureViewCompatibility.Full) { + TextureInfo oInfo = AdjustSizes(overlap, info, firstLevel); + if (!isSamplerTexture) { - info = AdjustSizes(overlap, info, firstLevel); + info = oInfo; } - texture = overlap.CreateView(info, sizeInfo, firstLayer, firstLevel); + texture = overlap.CreateView(oInfo, sizeInfo, firstLayer, firstLevel); if (overlap.IsModified) { texture.SignalModified(); } - // The size only matters (and is only really reliable) when the - // texture is used on a sampler, because otherwise the size will be - // aligned. - if (!TextureCompatibility.SizeMatches(overlap.Info, info, firstLevel) && isSamplerTexture) - { - texture.ChangeSize(info.Width, info.Height, info.DepthOrLayers); - } + ChangeSizeIfNeeded(info, texture, isSamplerTexture, sizeHint); break; } @@ -911,6 +905,44 @@ namespace Ryujinx.Graphics.Gpu.Image return texture; } + /// + /// Changes a texture's size to match the desired size for samplers, + /// or increases a texture's size to fit the region indicated by a size hint. + /// + /// The desired texture info + /// The texture to resize + /// True if the texture will be used for a sampler, false otherwise + /// A hint indicating the minimum used size for the texture + private void ChangeSizeIfNeeded(TextureInfo info, Texture texture, bool isSamplerTexture, Size? sizeHint) + { + if (isSamplerTexture) + { + // If this is used for sampling, the size must match, + // otherwise the shader would sample garbage data. + // To fix that, we create a new texture with the correct + // size, and copy the data from the old one to the new one. + + if (!TextureCompatibility.SizeMatches(texture.Info, info)) + { + texture.ChangeSize(info.Width, info.Height, info.DepthOrLayers); + } + } + else if (sizeHint != null) + { + // A size hint indicates that data will be used within that range, at least. + // If the texture is smaller than the size hint, it must be enlarged to meet it. + // The maximum size is provided by the requested info, which generally has an aligned size. + + int width = Math.Max(texture.Info.Width, Math.Min(sizeHint.Value.Width, info.Width)); + int height = Math.Max(texture.Info.Height, Math.Min(sizeHint.Value.Height, info.Height)); + + if (texture.Info.Width != width || texture.Info.Height != height) + { + texture.ChangeSize(width, height, info.DepthOrLayers); + } + } + } + /// /// Tries to find an existing texture matching the given buffer copy destination. If none is found, returns null. /// diff --git a/Ryujinx.Graphics.Gpu/Image/TexturePool.cs b/Ryujinx.Graphics.Gpu/Image/TexturePool.cs index 59f2e901..e26dc501 100644 --- a/Ryujinx.Graphics.Gpu/Image/TexturePool.cs +++ b/Ryujinx.Graphics.Gpu/Image/TexturePool.cs @@ -67,6 +67,22 @@ namespace Ryujinx.Graphics.Gpu.Image } else { + if (texture.ChangedSize) + { + // Texture changed size at one point - it may be a different size than the sampler expects. + // This can be triggered when the size is changed by a size hint on copy or draw, but the texture has been sampled before. + + TextureDescriptor descriptor = GetDescriptor(id); + + int width = descriptor.UnpackWidth(); + int height = descriptor.UnpackHeight(); + + if (texture.Info.Width != width || texture.Info.Height != height) + { + texture.ChangeSize(width, height, texture.Info.DepthOrLayers); + } + } + // Memory is automatically synchronized on texture creation. texture.SynchronizeMemory(); } -- cgit v1.2.3