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