2019-02-28 01:12:24 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-09-18 05:30:35 +01:00
|
|
|
using Ryujinx.Graphics.Gal;
|
|
|
|
using Ryujinx.Graphics.Memory;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
2019-02-28 01:12:24 +00:00
|
|
|
using System;
|
2018-09-18 05:30:35 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics
|
|
|
|
{
|
|
|
|
public class GpuResourceManager
|
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
private enum ImageType
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Texture,
|
2019-02-28 01:12:24 +00:00
|
|
|
TextureArrayLayer,
|
2018-09-19 22:26:49 +01:00
|
|
|
ColorBuffer,
|
|
|
|
ZetaBuffer
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
private NvGpu Gpu;
|
|
|
|
|
|
|
|
private HashSet<long>[] UploadedKeys;
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
private Dictionary<long, ImageType> ImageTypes;
|
2019-02-28 01:12:24 +00:00
|
|
|
private Dictionary<long, int> MirroredTextures;
|
2018-09-19 22:26:49 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
public GpuResourceManager(NvGpu Gpu)
|
|
|
|
{
|
|
|
|
this.Gpu = Gpu;
|
|
|
|
|
|
|
|
UploadedKeys = new HashSet<long>[(int)NvGpuBufferType.Count];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < UploadedKeys.Length; Index++)
|
|
|
|
{
|
|
|
|
UploadedKeys[Index] = new HashSet<long>();
|
|
|
|
}
|
2018-09-19 22:26:49 +01:00
|
|
|
|
|
|
|
ImageTypes = new Dictionary<long, ImageType>();
|
2019-02-28 01:12:24 +00:00
|
|
|
MirroredTextures = new Dictionary<long, int>();
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SendColorBuffer(NvGpuVmm Vmm, long Position, int Attachment, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = (uint)ImageUtils.GetSize(NewImage);
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
ImageTypes[Position] = ImageType.ColorBuffer;
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
if (!TryReuse(Vmm, Position, NewImage))
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
Gpu.Renderer.Texture.Create(Position, (int)Size, NewImage);
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
Gpu.Renderer.RenderTarget.BindColor(Position, Attachment);
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SendZetaBuffer(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = (uint)ImageUtils.GetSize(NewImage);
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
ImageTypes[Position] = ImageType.ZetaBuffer;
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
if (!TryReuse(Vmm, Position, NewImage))
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
Gpu.Renderer.Texture.Create(Position, (int)Size, NewImage);
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
Gpu.Renderer.RenderTarget.BindZeta(Position);
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
public void SendTexture(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
PrepareSendTexture(Vmm, Position, NewImage);
|
|
|
|
|
|
|
|
ImageTypes[Position] = ImageType.Texture;
|
|
|
|
}
|
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
public bool TryGetTextureLayer(long Position, out int LayerIndex)
|
|
|
|
{
|
|
|
|
if (MirroredTextures.TryGetValue(Position, out LayerIndex))
|
|
|
|
{
|
|
|
|
ImageType Type = ImageTypes[Position];
|
|
|
|
|
|
|
|
// FIXME(thog): I'm actually unsure if we should deny all other image type, gpu testing needs to be done here.
|
|
|
|
if (Type != ImageType.Texture && Type != ImageType.TextureArrayLayer)
|
|
|
|
{
|
|
|
|
LayerIndex = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerIndex = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTextureArrayLayer(long Position, int LayerIndex)
|
|
|
|
{
|
|
|
|
ImageTypes[Position] = ImageType.TextureArrayLayer;
|
|
|
|
MirroredTextures[Position] = LayerIndex;
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
private void PrepareSendTexture(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = ImageUtils.GetSize(NewImage);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
bool SkipCheck = false;
|
|
|
|
|
|
|
|
if (ImageTypes.TryGetValue(Position, out ImageType OldType))
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
if (OldType == ImageType.ColorBuffer || OldType == ImageType.ZetaBuffer)
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
//Avoid data destruction
|
|
|
|
MemoryRegionModified(Vmm, Position, Size, NvGpuBufferType.Texture);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
SkipCheck = true;
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
if (SkipCheck || !MemoryRegionModified(Vmm, Position, Size, NvGpuBufferType.Texture))
|
|
|
|
{
|
|
|
|
if (TryReuse(Vmm, Position, NewImage))
|
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] Data = ImageUtils.ReadTexture(Vmm, NewImage, Position);
|
|
|
|
|
|
|
|
Gpu.Renderer.Texture.Create(Position, Data, NewImage);
|
2018-09-19 22:26:49 +01:00
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
private bool TryReuse(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
2019-02-28 01:12:24 +00:00
|
|
|
if (Gpu.Renderer.Texture.TryGetImage(Position, out GalImage CachedImage) && CachedImage.TextureTarget == NewImage.TextureTarget && CachedImage.SizeMatches(NewImage))
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
Gpu.Renderer.RenderTarget.Reinterpret(Position, NewImage);
|
|
|
|
|
|
|
|
return true;
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
return false;
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
public bool MemoryRegionModified(NvGpuVmm Vmm, long Position, long Size, NvGpuBufferType Type)
|
2018-09-18 05:30:35 +01:00
|
|
|
{
|
|
|
|
HashSet<long> Uploaded = UploadedKeys[(int)Type];
|
|
|
|
|
|
|
|
if (!Uploaded.Add(Position))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Vmm.IsRegionModified(Position, Size, Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearPbCache()
|
|
|
|
{
|
|
|
|
for (int Index = 0; Index < UploadedKeys.Length; Index++)
|
|
|
|
{
|
|
|
|
UploadedKeys[Index].Clear();
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
|
|
public void ClearPbCache(NvGpuBufferType Type)
|
|
|
|
{
|
|
|
|
UploadedKeys[(int)Type].Clear();
|
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
}
|