2018-09-18 06:30:35 +02:00
|
|
|
using Ryujinx.Graphics.Gal;
|
|
|
|
using Ryujinx.Graphics.Memory;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics
|
|
|
|
{
|
|
|
|
public class GpuResourceManager
|
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
private enum ImageType
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Texture,
|
|
|
|
ColorBuffer,
|
|
|
|
ZetaBuffer
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
private NvGpu Gpu;
|
|
|
|
|
|
|
|
private HashSet<long>[] UploadedKeys;
|
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
private Dictionary<long, ImageType> ImageTypes;
|
|
|
|
|
2018-09-18 06:30:35 +02: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 23:26:49 +02:00
|
|
|
|
|
|
|
ImageTypes = new Dictionary<long, ImageType>();
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SendColorBuffer(NvGpuVmm Vmm, long Position, int Attachment, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = (uint)ImageUtils.GetSize(NewImage);
|
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
ImageTypes[Position] = ImageType.ColorBuffer;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
if (!TryReuse(Vmm, Position, NewImage))
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
Gpu.Renderer.Texture.Create(Position, (int)Size, NewImage);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:55:30 +02:00
|
|
|
Gpu.Renderer.RenderTarget.BindColor(Position, Attachment);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SendZetaBuffer(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = (uint)ImageUtils.GetSize(NewImage);
|
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
ImageTypes[Position] = ImageType.ZetaBuffer;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
if (!TryReuse(Vmm, Position, NewImage))
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
Gpu.Renderer.Texture.Create(Position, (int)Size, NewImage);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:55:30 +02:00
|
|
|
Gpu.Renderer.RenderTarget.BindZeta(Position);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:47 +01:00
|
|
|
public void SendTexture(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
PrepareSendTexture(Vmm, Position, NewImage);
|
|
|
|
|
|
|
|
ImageTypes[Position] = ImageType.Texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PrepareSendTexture(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
|
|
|
long Size = ImageUtils.GetSize(NewImage);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
bool SkipCheck = false;
|
|
|
|
|
|
|
|
if (ImageTypes.TryGetValue(Position, out ImageType OldType))
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
if (OldType == ImageType.ColorBuffer || OldType == ImageType.ZetaBuffer)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
//Avoid data destruction
|
|
|
|
MemoryRegionModified(Vmm, Position, Size, NvGpuBufferType.Texture);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
SkipCheck = true;
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
if (SkipCheck || !MemoryRegionModified(Vmm, Position, Size, NvGpuBufferType.Texture))
|
|
|
|
{
|
|
|
|
if (TryReuse(Vmm, Position, NewImage))
|
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] Data = ImageUtils.ReadTexture(Vmm, NewImage, Position);
|
|
|
|
|
|
|
|
Gpu.Renderer.Texture.Create(Position, Data, NewImage);
|
2018-09-19 23:26:49 +02:00
|
|
|
}
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
private bool TryReuse(NvGpuVmm Vmm, long Position, GalImage NewImage)
|
|
|
|
{
|
|
|
|
if (Gpu.Renderer.Texture.TryGetImage(Position, out GalImage CachedImage) && CachedImage.SizeMatches(NewImage))
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-09-19 23:26:49 +02:00
|
|
|
Gpu.Renderer.RenderTarget.Reinterpret(Position, NewImage);
|
|
|
|
|
|
|
|
return true;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 23:26:49 +02:00
|
|
|
return false;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-17 05:01:31 +01:00
|
|
|
public bool MemoryRegionModified(NvGpuVmm Vmm, long Position, long Size, NvGpuBufferType Type)
|
2018-09-18 06:30:35 +02: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 05:01:31 +01:00
|
|
|
|
|
|
|
public void ClearPbCache(NvGpuBufferType Type)
|
|
|
|
{
|
|
|
|
UploadedKeys[(int)Type].Clear();
|
|
|
|
}
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|