2018-09-18 06:30:35 +02:00
|
|
|
using Ryujinx.Graphics.Memory;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
|
|
|
using System.Collections.Generic;
|
2019-06-01 02:13:57 +02:00
|
|
|
using Ryujinx.Profiler;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2018-12-03 03:38:47 +01:00
|
|
|
namespace Ryujinx.Graphics.Graphics3d
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2018-11-17 05:01:31 +01:00
|
|
|
class NvGpuEngineM2mf : INvGpuEngine
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
|
|
|
public int[] Registers { get; private set; }
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private NvGpu _gpu;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private Dictionary<int, NvGpuMethod> _methods;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public NvGpuEngineM2mf(NvGpu gpu)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_gpu = gpu;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
|
|
|
Registers = new int[0x1d6];
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_methods = new Dictionary<int, NvGpuMethod>();
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
void AddMethod(int meth, int count, int stride, NvGpuMethod method)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
while (count-- > 0)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_methods.Add(meth, method);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
meth += stride;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddMethod(0xc0, 1, 1, Execute);
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_methods.TryGetValue(methCall.Method, out NvGpuMethod method))
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-06-01 02:13:57 +02:00
|
|
|
ProfileConfig profile = Profiles.GPU.EngineM2mf.CallMethod;
|
|
|
|
|
|
|
|
profile.SessionItem = method.Method.Name;
|
|
|
|
|
|
|
|
Profile.Begin(profile);
|
2019-03-04 02:45:25 +01:00
|
|
|
method(vmm, methCall);
|
2019-06-01 02:13:57 +02:00
|
|
|
Profile.End(profile);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
WriteRegister(methCall);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private void Execute(NvGpuVmm vmm, GpuMethodCall methCall)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-06-01 02:13:57 +02:00
|
|
|
Profile.Begin(Profiles.GPU.EngineM2mf.Execute);
|
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// TODO: Some registers and copy modes are still not implemented.
|
2019-03-04 02:45:25 +01:00
|
|
|
int control = methCall.Argument;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
bool srcLinear = ((control >> 7) & 1) != 0;
|
|
|
|
bool dstLinear = ((control >> 8) & 1) != 0;
|
|
|
|
bool copy2D = ((control >> 9) & 1) != 0;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
long srcAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.SrcAddress);
|
|
|
|
long dstAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.DstAddress);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcPitch = ReadRegister(NvGpuEngineM2mfReg.SrcPitch);
|
|
|
|
int dstPitch = ReadRegister(NvGpuEngineM2mfReg.DstPitch);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int xCount = ReadRegister(NvGpuEngineM2mfReg.XCount);
|
|
|
|
int yCount = ReadRegister(NvGpuEngineM2mfReg.YCount);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int swizzle = ReadRegister(NvGpuEngineM2mfReg.Swizzle);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int dstBlkDim = ReadRegister(NvGpuEngineM2mfReg.DstBlkDim);
|
|
|
|
int dstSizeX = ReadRegister(NvGpuEngineM2mfReg.DstSizeX);
|
|
|
|
int dstSizeY = ReadRegister(NvGpuEngineM2mfReg.DstSizeY);
|
|
|
|
int dstSizeZ = ReadRegister(NvGpuEngineM2mfReg.DstSizeZ);
|
|
|
|
int dstPosXY = ReadRegister(NvGpuEngineM2mfReg.DstPosXY);
|
|
|
|
int dstPosZ = ReadRegister(NvGpuEngineM2mfReg.DstPosZ);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcBlkDim = ReadRegister(NvGpuEngineM2mfReg.SrcBlkDim);
|
|
|
|
int srcSizeX = ReadRegister(NvGpuEngineM2mfReg.SrcSizeX);
|
|
|
|
int srcSizeY = ReadRegister(NvGpuEngineM2mfReg.SrcSizeY);
|
|
|
|
int srcSizeZ = ReadRegister(NvGpuEngineM2mfReg.SrcSizeZ);
|
|
|
|
int srcPosXY = ReadRegister(NvGpuEngineM2mfReg.SrcPosXY);
|
|
|
|
int srcPosZ = ReadRegister(NvGpuEngineM2mfReg.SrcPosZ);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcCpp = ((swizzle >> 20) & 7) + 1;
|
|
|
|
int dstCpp = ((swizzle >> 24) & 7) + 1;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int dstPosX = (dstPosXY >> 0) & 0xffff;
|
|
|
|
int dstPosY = (dstPosXY >> 16) & 0xffff;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcPosX = (srcPosXY >> 0) & 0xffff;
|
|
|
|
int srcPosY = (srcPosXY >> 16) & 0xffff;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcBlockHeight = 1 << ((srcBlkDim >> 4) & 0xf);
|
|
|
|
int dstBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
long srcPa = vmm.GetPhysicalAddress(srcAddress);
|
|
|
|
long dstPa = vmm.GetPhysicalAddress(dstAddress);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (copy2D)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (srcLinear)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
srcPosX = srcPosY = srcPosZ = 0;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (dstLinear)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
dstPosX = dstPosY = dstPosZ = 0;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (srcLinear && dstLinear)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
for (int y = 0; y < yCount; y++)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcOffset = (srcPosY + y) * srcPitch + srcPosX * srcCpp;
|
|
|
|
int dstOffset = (dstPosY + y) * dstPitch + dstPosX * dstCpp;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
long src = srcPa + (uint)srcOffset;
|
|
|
|
long dst = dstPa + (uint)dstOffset;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
vmm.Memory.CopyBytes(src, dst, xCount * srcCpp);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
ISwizzle srcSwizzle;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (srcLinear)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
srcSwizzle = new LinearSwizzle(srcPitch, srcCpp, srcSizeX, srcSizeY);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
srcSwizzle = new BlockLinearSwizzle(
|
|
|
|
srcSizeX,
|
|
|
|
srcSizeY, 1,
|
|
|
|
srcBlockHeight, 1,
|
|
|
|
srcCpp);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
ISwizzle dstSwizzle;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (dstLinear)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
dstSwizzle = new LinearSwizzle(dstPitch, dstCpp, srcSizeX, srcSizeY);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
dstSwizzle = new BlockLinearSwizzle(
|
|
|
|
dstSizeX,
|
|
|
|
dstSizeY, 1,
|
|
|
|
dstBlockHeight, 1,
|
|
|
|
dstCpp);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 05:59:12 +01:00
|
|
|
// Calculate the bits per pixel
|
|
|
|
int bpp = srcPitch / xCount;
|
|
|
|
|
|
|
|
// Copying all the bits at the same time corrupts the texture, unknown why but probably because the texture isn't linear
|
|
|
|
// To avoid this we will simply loop more times to cover all the bits,
|
|
|
|
// this allows up to recalculate the memory locations for each iteration around the loop
|
|
|
|
xCount *= bpp / srcCpp;
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
for (int y = 0; y < yCount; y++)
|
|
|
|
for (int x = 0; x < xCount; x++)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int srcOffset = srcSwizzle.GetSwizzleOffset(srcPosX + x, srcPosY + y, 0);
|
|
|
|
int dstOffset = dstSwizzle.GetSwizzleOffset(dstPosX + x, dstPosY + y, 0);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
long src = srcPa + (uint)srcOffset;
|
|
|
|
long dst = dstPa + (uint)dstOffset;
|
2018-09-18 06:30:35 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
vmm.Memory.CopyBytes(src, dst, srcCpp);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
vmm.Memory.CopyBytes(srcPa, dstPa, xCount);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
2019-06-01 02:13:57 +02:00
|
|
|
|
|
|
|
Profile.End(Profiles.GPU.EngineM2mf.Execute);
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private long MakeInt64From2xInt32(NvGpuEngineM2mfReg reg)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
|
|
|
return
|
2019-03-04 02:45:25 +01:00
|
|
|
(long)Registers[(int)reg + 0] << 32 |
|
|
|
|
(uint)Registers[(int)reg + 1];
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private void WriteRegister(GpuMethodCall methCall)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
Registers[methCall.Method] = methCall.Argument;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private int ReadRegister(NvGpuEngineM2mfReg reg)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
return Registers[(int)reg];
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private void WriteRegister(NvGpuEngineM2mfReg reg, int value)
|
2018-09-18 06:30:35 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
Registers[(int)reg] = value;
|
2018-09-18 06:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 02:13:57 +02:00
|
|
|
}
|