2018-10-28 22:31:13 +00:00
|
|
|
using Ryujinx.Common;
|
2018-04-08 19:17:35 +00:00
|
|
|
using Ryujinx.Graphics.Gal;
|
2018-09-08 17:51:50 +00:00
|
|
|
using Ryujinx.Graphics.Memory;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-04-08 19:17:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
namespace Ryujinx.Graphics.Graphics3d
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
class NvGpuEngine3d : INvGpuEngine
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
public int[] Registers { get; private set; }
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
private NvGpu Gpu;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
|
|
|
private Dictionary<int, NvGpuMethod> Methods;
|
|
|
|
|
|
|
|
private struct ConstBuffer
|
|
|
|
{
|
|
|
|
public bool Enabled;
|
|
|
|
public long Position;
|
|
|
|
public int Size;
|
|
|
|
}
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
private ConstBuffer[][] ConstBuffers;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-25 04:16:58 +00:00
|
|
|
private int CurrentInstance = 0;
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
public NvGpuEngine3d(NvGpu Gpu)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
this.Gpu = Gpu;
|
|
|
|
|
|
|
|
Registers = new int[0xe00];
|
|
|
|
|
|
|
|
Methods = new Dictionary<int, NvGpuMethod>();
|
|
|
|
|
|
|
|
void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
|
|
|
|
{
|
|
|
|
while (Count-- > 0)
|
|
|
|
{
|
|
|
|
Methods.Add(Meth, Method);
|
|
|
|
|
|
|
|
Meth += Stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddMethod(0x585, 1, 1, VertexEndGl);
|
|
|
|
AddMethod(0x674, 1, 1, ClearBuffers);
|
|
|
|
AddMethod(0x6c3, 1, 1, QueryControl);
|
|
|
|
AddMethod(0x8e4, 16, 1, CbData);
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
AddMethod(0x904, 5, 8, CbBind);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
ConstBuffers = new ConstBuffer[6][];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < ConstBuffers.Length; Index++)
|
|
|
|
{
|
|
|
|
ConstBuffers[Index] = new ConstBuffer[18];
|
|
|
|
}
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-10-25 21:30:09 +00:00
|
|
|
//Ensure that all components are enabled by default.
|
|
|
|
//FIXME: Is this correct?
|
|
|
|
WriteRegister(NvGpuEngine3dReg.ColorMaskN, 0x1111);
|
2018-11-14 21:50:31 +00:00
|
|
|
|
2018-11-17 04:10:51 +00:00
|
|
|
WriteRegister(NvGpuEngine3dReg.FrameBufferSrgb, 1);
|
|
|
|
|
2018-11-14 21:50:31 +00:00
|
|
|
for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
|
|
|
|
{
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8, (int)GalBlendEquation.FuncAdd);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8, (int)GalBlendFactor.One);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8, (int)GalBlendFactor.Zero);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8, (int)GalBlendEquation.FuncAdd);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8, (int)GalBlendFactor.One);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8, (int)GalBlendFactor.Zero);
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
Method(Vmm, MethCall);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
WriteRegister(MethCall);
|
2018-08-15 01:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void VertexEndGl(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-07-10 02:01:59 +00:00
|
|
|
LockCaches();
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
GalPipelineState State = new GalPipelineState();
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
SetFrameBuffer(State);
|
2018-08-10 04:09:40 +00:00
|
|
|
SetFrontFace(State);
|
|
|
|
SetCullFace(State);
|
|
|
|
SetDepth(State);
|
|
|
|
SetStencil(State);
|
2018-10-14 02:54:14 +00:00
|
|
|
SetBlending(State);
|
|
|
|
SetColorMask(State);
|
2018-08-10 04:09:40 +00:00
|
|
|
SetPrimitiveRestart(State);
|
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
for (int FbIndex = 0; FbIndex < 8; FbIndex++)
|
|
|
|
{
|
|
|
|
SetFrameBuffer(Vmm, FbIndex);
|
|
|
|
}
|
2018-09-01 14:54:56 +00:00
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
SetZeta(Vmm);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-23 05:07:23 +00:00
|
|
|
SetRenderTargets();
|
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
long[] Keys = UploadShaders(Vmm);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
Gpu.Renderer.Shader.BindProgram();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
UploadTextures(Vmm, State, Keys);
|
2018-08-15 18:59:51 +00:00
|
|
|
UploadConstBuffers(Vmm, State, Keys);
|
2018-08-10 04:09:40 +00:00
|
|
|
UploadVertexArrays(Vmm, State);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
DispatchRender(Vmm, State);
|
2018-07-10 02:01:59 +00:00
|
|
|
|
|
|
|
UnlockCaches();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LockCaches()
|
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
Gpu.Renderer.Buffer.LockCache();
|
2018-07-10 02:01:59 +00:00
|
|
|
Gpu.Renderer.Rasterizer.LockCaches();
|
|
|
|
Gpu.Renderer.Texture.LockCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UnlockCaches()
|
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
Gpu.Renderer.Buffer.UnlockCache();
|
2018-07-10 02:01:59 +00:00
|
|
|
Gpu.Renderer.Rasterizer.UnlockCaches();
|
|
|
|
Gpu.Renderer.Texture.UnlockCache();
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void ClearBuffers(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
int Attachment = (MethCall.Argument >> 6) & 0xf;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
GalClearBufferFlags Flags = (GalClearBufferFlags)(MethCall.Argument & 0x3f);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
float Red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0);
|
|
|
|
float Green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1);
|
|
|
|
float Blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2);
|
|
|
|
float Alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3);
|
|
|
|
|
|
|
|
float Depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth);
|
|
|
|
|
|
|
|
int Stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil);
|
|
|
|
|
2018-10-17 21:02:23 +00:00
|
|
|
SetFrameBuffer(Vmm, Attachment);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
SetZeta(Vmm);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
SetRenderTargets();
|
|
|
|
|
|
|
|
Gpu.Renderer.RenderTarget.Bind();
|
|
|
|
|
2018-10-17 21:02:23 +00:00
|
|
|
Gpu.Renderer.Rasterizer.ClearBuffers(Flags, Attachment, Red, Green, Blue, Alpha, Depth, Stencil);
|
|
|
|
|
|
|
|
Gpu.Renderer.Pipeline.ResetDepthMask();
|
|
|
|
Gpu.Renderer.Pipeline.ResetColorMask(Attachment);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
int SurfFormat = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + FbIndex * 0x10);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
if (VA == 0 || SurfFormat == 0)
|
2018-08-20 01:25:26 +00:00
|
|
|
{
|
2018-09-08 17:51:50 +00:00
|
|
|
Gpu.Renderer.RenderTarget.UnbindColor(FbIndex);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:11:49 +00:00
|
|
|
long Key = Vmm.GetPhysicalAddress(VA);
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
|
2018-04-13 18:12:58 +00:00
|
|
|
int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
|
|
|
|
int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
int BlockDim = ReadRegister(NvGpuEngine3dReg.FrameBufferNBlockDim + FbIndex * 0x10);
|
|
|
|
|
|
|
|
int GobBlockHeight = 1 << ((BlockDim >> 4) & 7);
|
|
|
|
|
|
|
|
GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1);
|
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 8);
|
|
|
|
float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 8);
|
2018-07-19 05:30:21 +00:00
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 8);
|
|
|
|
float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 8);
|
2018-07-19 05:30:21 +00:00
|
|
|
|
|
|
|
int VpX = (int)MathF.Max(0, TX - MathF.Abs(SX));
|
|
|
|
int VpY = (int)MathF.Max(0, TY - MathF.Abs(SY));
|
|
|
|
|
|
|
|
int VpW = (int)(TX + MathF.Abs(SX)) - VpX;
|
|
|
|
int VpH = (int)(TY + MathF.Abs(SY)) - VpY;
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalImageFormat Format = ImageUtils.ConvertSurface((GalSurfaceFormat)SurfFormat);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalImage Image = new GalImage(Width, Height, 1, GobBlockHeight, Layout, Format);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
Gpu.ResourceManager.SendColorBuffer(Vmm, Key, FbIndex, Image);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
Gpu.Renderer.RenderTarget.SetViewport(FbIndex, VpX, VpY, VpW, VpH);
|
2018-09-18 04:30:35 +00:00
|
|
|
}
|
2018-07-19 05:30:21 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
private void SetFrameBuffer(GalPipelineState State)
|
|
|
|
{
|
2018-09-20 01:02:11 +00:00
|
|
|
State.FramebufferSrgb = ReadRegisterBool(NvGpuEngine3dReg.FrameBufferSrgb);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
State.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
|
|
|
|
State.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
|
|
int ScreenYControl = ReadRegister(NvGpuEngine3dReg.ScreenYControl);
|
|
|
|
|
|
|
|
bool NegateY = (ScreenYControl & 1) != 0;
|
|
|
|
|
|
|
|
if (NegateY)
|
|
|
|
{
|
|
|
|
State.FlipY = -State.FlipY;
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
private void SetZeta(NvGpuVmm Vmm)
|
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress);
|
|
|
|
|
|
|
|
int ZetaFormat = ReadRegister(NvGpuEngine3dReg.ZetaFormat);
|
|
|
|
|
|
|
|
int BlockDim = ReadRegister(NvGpuEngine3dReg.ZetaBlockDimensions);
|
|
|
|
|
|
|
|
int GobBlockHeight = 1 << ((BlockDim >> 4) & 7);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1); //?
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-20 01:02:11 +00:00
|
|
|
bool ZetaEnable = ReadRegisterBool(NvGpuEngine3dReg.ZetaEnable);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
if (VA == 0 || ZetaFormat == 0 || !ZetaEnable)
|
2018-08-20 01:25:26 +00:00
|
|
|
{
|
2018-09-08 17:51:50 +00:00
|
|
|
Gpu.Renderer.RenderTarget.UnbindZeta();
|
2018-08-20 01:25:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
long Key = Vmm.GetPhysicalAddress(VA);
|
2018-09-01 14:54:56 +00:00
|
|
|
|
2018-08-20 01:25:26 +00:00
|
|
|
int Width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz);
|
|
|
|
int Height = ReadRegister(NvGpuEngine3dReg.ZetaVert);
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalImageFormat Format = ImageUtils.ConvertZeta((GalZetaFormat)ZetaFormat);
|
2018-08-20 01:25:26 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalImage Image = new GalImage(Width, Height, 1, GobBlockHeight, Layout, Format);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
Gpu.ResourceManager.SendZetaBuffer(Vmm, Key, Image);
|
2018-08-20 01:25:26 +00:00
|
|
|
}
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
private long[] UploadShaders(NvGpuVmm Vmm)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
long[] Keys = new long[5];
|
2018-04-08 19:17:35 +00:00
|
|
|
|
|
|
|
long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
|
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
int Index = 1;
|
|
|
|
|
|
|
|
int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl);
|
|
|
|
|
|
|
|
bool VpAEnable = (VpAControl & 1) != 0;
|
|
|
|
|
|
|
|
if (VpAEnable)
|
|
|
|
{
|
|
|
|
//Note: The maxwell supports 2 vertex programs, usually
|
|
|
|
//only VP B is used, but in some cases VP A is also used.
|
|
|
|
//In this case, it seems to function as an extra vertex
|
|
|
|
//shader stage.
|
|
|
|
//The graphics abstraction layer has a special overload for this
|
|
|
|
//case, which should merge the two shaders into one vertex shader.
|
|
|
|
int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset);
|
|
|
|
int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10);
|
|
|
|
|
|
|
|
long VpAPos = BasePosition + (uint)VpAOffset;
|
|
|
|
long VpBPos = BasePosition + (uint)VpBOffset;
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
Keys[(int)GalShaderType.Vertex] = VpBPos;
|
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex);
|
|
|
|
Gpu.Renderer.Shader.Bind(VpBPos);
|
|
|
|
|
|
|
|
Index = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; Index < 6; Index++)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-07-14 16:08:39 +00:00
|
|
|
GalShaderType Type = GetTypeFromProgram(Index);
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
|
|
|
|
int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
|
|
|
|
|
|
|
|
//Note: Vertex Program (B) is always enabled.
|
|
|
|
bool Enable = (Control & 1) != 0 || Index == 1;
|
|
|
|
|
|
|
|
if (!Enable)
|
|
|
|
{
|
2018-07-14 16:08:39 +00:00
|
|
|
Gpu.Renderer.Shader.Unbind(Type);
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
long Key = BasePosition + (uint)Offset;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-07-14 16:08:39 +00:00
|
|
|
Keys[(int)Type] = Key;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-07-14 16:08:39 +00:00
|
|
|
Gpu.Renderer.Shader.Create(Vmm, Key, Type);
|
2018-06-24 00:39:25 +00:00
|
|
|
Gpu.Renderer.Shader.Bind(Key);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
return Keys;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static GalShaderType GetTypeFromProgram(int Program)
|
|
|
|
{
|
|
|
|
switch (Program)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1: return GalShaderType.Vertex;
|
|
|
|
case 2: return GalShaderType.TessControl;
|
|
|
|
case 3: return GalShaderType.TessEvaluation;
|
|
|
|
case 4: return GalShaderType.Geometry;
|
|
|
|
case 5: return GalShaderType.Fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(Program));
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void SetFrontFace(GalPipelineState State)
|
2018-07-05 18:47:29 +00:00
|
|
|
{
|
2018-07-19 05:30:21 +00:00
|
|
|
float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
|
|
|
|
float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
|
2018-07-05 18:47:29 +00:00
|
|
|
|
|
|
|
GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace);
|
|
|
|
|
|
|
|
//Flipping breaks facing. Flipping front facing too fixes it
|
|
|
|
if (SignX != SignY)
|
|
|
|
{
|
|
|
|
switch (FrontFace)
|
|
|
|
{
|
2018-10-17 21:02:23 +00:00
|
|
|
case GalFrontFace.CW: FrontFace = GalFrontFace.CCW; break;
|
|
|
|
case GalFrontFace.CCW: FrontFace = GalFrontFace.CW; break;
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
State.FrontFace = FrontFace;
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void SetCullFace(GalPipelineState State)
|
2018-06-27 04:32:28 +00:00
|
|
|
{
|
2018-09-20 01:02:11 +00:00
|
|
|
State.CullFaceEnabled = ReadRegisterBool(NvGpuEngine3dReg.CullFaceEnable);
|
2018-07-05 18:47:29 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
if (State.CullFaceEnabled)
|
2018-07-05 18:47:29 +00:00
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
State.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace);
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
2018-06-27 04:32:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void SetDepth(GalPipelineState State)
|
2018-06-27 04:32:28 +00:00
|
|
|
{
|
2018-09-20 01:02:11 +00:00
|
|
|
State.DepthTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthTestEnable);
|
|
|
|
|
|
|
|
State.DepthWriteEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthWriteEnable);
|
2018-07-05 18:47:29 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
if (State.DepthTestEnabled)
|
2018-07-05 18:47:29 +00:00
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction);
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
2018-10-23 19:04:08 +00:00
|
|
|
|
|
|
|
State.DepthRangeNear = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNNear);
|
|
|
|
State.DepthRangeFar = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNFar);
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void SetStencil(GalPipelineState State)
|
2018-07-05 18:47:29 +00:00
|
|
|
{
|
2018-09-20 01:02:11 +00:00
|
|
|
State.StencilTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.StencilEnable);
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
if (State.StencilTestEnabled)
|
|
|
|
{
|
|
|
|
State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc);
|
|
|
|
State.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef);
|
|
|
|
State.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask);
|
|
|
|
State.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail);
|
|
|
|
State.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail);
|
|
|
|
State.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass);
|
|
|
|
State.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask);
|
|
|
|
|
|
|
|
State.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc);
|
|
|
|
State.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef);
|
|
|
|
State.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask);
|
|
|
|
State.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail);
|
|
|
|
State.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail);
|
|
|
|
State.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass);
|
|
|
|
State.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask);
|
2018-07-05 18:47:29 +00:00
|
|
|
}
|
2018-06-27 04:32:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 02:54:14 +00:00
|
|
|
private void SetBlending(GalPipelineState State)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-01 04:22:24 +00:00
|
|
|
bool BlendIndependent = ReadRegisterBool(NvGpuEngine3dReg.BlendIndependent);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-11-01 04:22:24 +00:00
|
|
|
State.BlendIndependent = BlendIndependent;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
|
2018-06-02 03:50:56 +00:00
|
|
|
{
|
2018-11-01 04:22:24 +00:00
|
|
|
if (BlendIndependent)
|
|
|
|
{
|
|
|
|
State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable + Index);
|
|
|
|
|
|
|
|
if (State.Blends[Index].Enabled)
|
|
|
|
{
|
|
|
|
State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.IBlendNSeparateAlpha + Index * 8);
|
|
|
|
|
|
|
|
State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8);
|
|
|
|
State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8);
|
|
|
|
State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8);
|
|
|
|
State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8);
|
|
|
|
State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8);
|
|
|
|
State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//It seems that even when independent blend is disabled, the first IBlend enable
|
|
|
|
//register is still set to indicate whenever blend is enabled or not (?).
|
|
|
|
State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable);
|
|
|
|
|
|
|
|
if (State.Blends[Index].Enabled)
|
|
|
|
{
|
|
|
|
State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.BlendSeparateAlpha);
|
|
|
|
|
|
|
|
State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationRgb);
|
|
|
|
State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcRgb);
|
|
|
|
State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstRgb);
|
|
|
|
State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationAlpha);
|
|
|
|
State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcAlpha);
|
|
|
|
State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstAlpha);
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:22:24 +00:00
|
|
|
private GalBlendEquation ReadBlendEquation(NvGpuEngine3dReg Register)
|
|
|
|
{
|
|
|
|
return (GalBlendEquation)ReadRegister(Register);
|
|
|
|
}
|
|
|
|
|
|
|
|
private GalBlendFactor ReadBlendFactor(NvGpuEngine3dReg Register)
|
|
|
|
{
|
|
|
|
return (GalBlendFactor)ReadRegister(Register);
|
|
|
|
}
|
|
|
|
|
2018-10-14 02:54:14 +00:00
|
|
|
private void SetColorMask(GalPipelineState State)
|
|
|
|
{
|
2018-10-25 21:30:09 +00:00
|
|
|
bool ColorMaskCommon = ReadRegisterBool(NvGpuEngine3dReg.ColorMaskCommon);
|
2018-10-14 02:54:14 +00:00
|
|
|
|
2018-10-25 21:30:09 +00:00
|
|
|
State.ColorMaskCommon = ColorMaskCommon;
|
2018-10-17 21:02:23 +00:00
|
|
|
|
|
|
|
for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
|
|
|
|
{
|
2018-10-25 21:30:09 +00:00
|
|
|
int ColorMask = ReadRegister(NvGpuEngine3dReg.ColorMaskN + (ColorMaskCommon ? 0 : Index));
|
2018-10-17 21:02:23 +00:00
|
|
|
|
|
|
|
State.ColorMasks[Index].Red = ((ColorMask >> 0) & 0xf) != 0;
|
|
|
|
State.ColorMasks[Index].Green = ((ColorMask >> 4) & 0xf) != 0;
|
|
|
|
State.ColorMasks[Index].Blue = ((ColorMask >> 8) & 0xf) != 0;
|
|
|
|
State.ColorMasks[Index].Alpha = ((ColorMask >> 12) & 0xf) != 0;
|
|
|
|
}
|
2018-10-14 02:54:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void SetPrimitiveRestart(GalPipelineState State)
|
2018-07-08 16:14:35 +00:00
|
|
|
{
|
2018-09-20 01:02:11 +00:00
|
|
|
State.PrimitiveRestartEnabled = ReadRegisterBool(NvGpuEngine3dReg.PrimRestartEnable);
|
2018-07-08 16:14:35 +00:00
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
if (State.PrimitiveRestartEnabled)
|
2018-07-08 16:14:35 +00:00
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
State.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex);
|
2018-07-08 16:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 05:07:23 +00:00
|
|
|
private void SetRenderTargets()
|
|
|
|
{
|
2018-10-13 01:37:01 +00:00
|
|
|
//Commercial games do not seem to
|
2018-09-25 22:55:30 +00:00
|
|
|
//bool SeparateFragData = ReadRegisterBool(NvGpuEngine3dReg.RTSeparateFragData);
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
uint Control = (uint)(ReadRegister(NvGpuEngine3dReg.RTControl));
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
uint Count = Control & 0xf;
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
if (Count > 0)
|
|
|
|
{
|
2018-08-23 05:07:23 +00:00
|
|
|
int[] Map = new int[Count];
|
|
|
|
|
2018-10-17 21:02:23 +00:00
|
|
|
for (int Index = 0; Index < Count; Index++)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2018-10-17 21:02:23 +00:00
|
|
|
int Shift = 4 + Index * 3;
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2018-10-17 21:02:23 +00:00
|
|
|
Map[Index] = (int)((Control >> Shift) & 7);
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 17:51:50 +00:00
|
|
|
Gpu.Renderer.RenderTarget.SetMap(Map);
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-08 17:51:50 +00:00
|
|
|
Gpu.Renderer.RenderTarget.SetMap(null);
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void UploadTextures(NvGpuVmm Vmm, GalPipelineState State, long[] Keys)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
|
|
|
|
|
|
|
|
int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
|
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
List<(long, GalImage, GalTextureSampler)> UnboundTextures = new List<(long, GalImage, GalTextureSampler)>();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
for (int Index = 0; Index < Keys.Length; Index++)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-08-13 21:22:09 +00:00
|
|
|
long Position;
|
|
|
|
|
|
|
|
if (DeclInfo.IsCb)
|
|
|
|
{
|
|
|
|
Position = ConstBuffers[Index][DeclInfo.Cbuf].Position;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Position = ConstBuffers[Index][TextureCbIndex].Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
UnboundTextures.Add(UploadTexture(Vmm, TextureHandle));
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
for (int Index = 0; Index < UnboundTextures.Count; Index++)
|
|
|
|
{
|
|
|
|
(long Key, GalImage Image, GalTextureSampler Sampler) = UnboundTextures[Index];
|
|
|
|
|
|
|
|
if (Key == 0)
|
|
|
|
{
|
|
|
|
continue;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-12-03 02:38:47 +00:00
|
|
|
|
|
|
|
Gpu.Renderer.Texture.Bind(Key, Index, Image);
|
|
|
|
Gpu.Renderer.Texture.SetSampler(Sampler);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
private (long, GalImage, GalTextureSampler) UploadTexture(NvGpuVmm Vmm, int TextureHandle)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-23 05:00:44 +00:00
|
|
|
if (TextureHandle == 0)
|
|
|
|
{
|
2018-11-01 04:22:24 +00:00
|
|
|
//FIXME: Some games like puyo puyo will use handles with the value 0.
|
|
|
|
//This is a bug, most likely caused by sync issues.
|
2018-12-03 02:38:47 +00:00
|
|
|
return (0, default(GalImage), default(GalTextureSampler));
|
2018-06-23 05:00:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
bool LinkedTsc = ReadRegisterBool(NvGpuEngine3dReg.LinkedTsc);
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
int TicIndex = (TextureHandle >> 0) & 0xfffff;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
|
|
int TscIndex = LinkedTsc ? TicIndex : (TextureHandle >> 20) & 0xfff;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
|
|
|
|
long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
|
|
|
TicPosition += TicIndex * 0x20;
|
|
|
|
TscPosition += TscIndex * 0x20;
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
GalImage Image = TextureFactory.MakeTexture(Vmm, TicPosition);
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-07-08 19:55:15 +00:00
|
|
|
long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
if (Image.Layout == GalMemoryLayout.BlockLinear)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
Key &= ~0x1ffL;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-09-18 04:30:35 +00:00
|
|
|
else if (Image.Layout == GalMemoryLayout.Pitch)
|
2018-04-13 18:12:58 +00:00
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
Key &= ~0x1fL;
|
2018-04-13 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
Key = Vmm.GetPhysicalAddress(Key);
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
if (Key == -1)
|
|
|
|
{
|
|
|
|
//FIXME: Shouldn't ignore invalid addresses.
|
2018-12-03 02:38:47 +00:00
|
|
|
return (0, default(GalImage), default(GalTextureSampler));
|
2018-04-13 18:12:58 +00:00
|
|
|
}
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
Gpu.ResourceManager.SendTexture(Vmm, Key, Image);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
2018-12-03 02:38:47 +00:00
|
|
|
return (Key, Image, Sampler);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private void UploadConstBuffers(NvGpuVmm Vmm, GalPipelineState State, long[] Keys)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
for (int Stage = 0; Stage < Keys.Length; Stage++)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetConstBufferUsage(Keys[Stage]))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
ConstBuffer Cb = ConstBuffers[Stage][DeclInfo.Cbuf];
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
if (!Cb.Enabled)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
long Key = Vmm.GetPhysicalAddress(Cb.Position);
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
if (Gpu.ResourceManager.MemoryRegionModified(Vmm, Key, Cb.Size, NvGpuBufferType.ConstBuffer))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
if (Vmm.TryGetHostAddress(Cb.Position, Cb.Size, out IntPtr CbPtr))
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Buffer.SetData(Key, Cb.Size, CbPtr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Buffer.SetData(Key, Vmm.ReadBytes(Cb.Position, Cb.Size));
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-08-10 04:09:40 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
State.ConstBufferKeys[Stage][DeclInfo.Cbuf] = Key;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-10-13 01:37:01 +00:00
|
|
|
long IbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-10-13 01:37:01 +00:00
|
|
|
long IboKey = Vmm.GetPhysicalAddress(IbPosition);
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
|
|
|
|
int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
|
2018-10-13 01:37:01 +00:00
|
|
|
int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
|
|
|
|
|
|
|
|
GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
int IndexEntrySize = 1 << IndexEntryFmt;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
if (IndexEntrySize > 4)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-01 04:22:24 +00:00
|
|
|
throw new InvalidOperationException("Invalid index entry size \"" + IndexEntrySize + "\"!");
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 13:59:03 +00:00
|
|
|
if (IndexCount != 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
int IbSize = IndexCount * IndexEntrySize;
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2018-07-08 19:55:15 +00:00
|
|
|
bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize);
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2018-10-13 01:37:01 +00:00
|
|
|
bool UsesLegacyQuads =
|
|
|
|
PrimType == GalPrimitiveType.Quads ||
|
|
|
|
PrimType == GalPrimitiveType.QuadStrip;
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
if (!IboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index))
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2018-10-13 01:37:01 +00:00
|
|
|
if (!UsesLegacyQuads)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
if (Vmm.TryGetHostAddress(IbPosition, IbSize, out IntPtr IbPtr))
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, IbPtr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Vmm.ReadBytes(IbPosition, IbSize));
|
|
|
|
}
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
byte[] Buffer = Vmm.ReadBytes(IbPosition, IbSize);
|
|
|
|
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
Buffer = QuadHelper.ConvertQuadsToTris(Buffer, IndexEntrySize, IndexCount);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
else /* if (PrimType == GalPrimitiveType.QuadStrip) */
|
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
Buffer = QuadHelper.ConvertQuadStripToTris(Buffer, IndexEntrySize, IndexCount);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Buffer);
|
|
|
|
}
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-10-13 01:37:01 +00:00
|
|
|
if (!UsesLegacyQuads)
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadsToTris(IbSize), IndexFormat);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
else /* if (PrimType == GalPrimitiveType.QuadStrip) */
|
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadStripToTris(IbSize), IndexFormat);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
|
|
|
|
|
|
|
|
for (int Attr = 0; Attr < 16; Attr++)
|
|
|
|
{
|
|
|
|
int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
|
|
|
|
|
|
|
|
int ArrayIndex = Packed & 0x1f;
|
|
|
|
|
|
|
|
if (Attribs[ArrayIndex] == null)
|
|
|
|
{
|
|
|
|
Attribs[ArrayIndex] = new List<GalVertexAttrib>();
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + ArrayIndex * 4);
|
|
|
|
|
2018-12-26 19:29:50 +00:00
|
|
|
if (VbPosition == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
bool IsConst = ((Packed >> 6) & 1) != 0;
|
2018-10-13 01:37:01 +00:00
|
|
|
|
|
|
|
int Offset = (Packed >> 7) & 0x3fff;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
GalVertexAttribSize Size = (GalVertexAttribSize)((Packed >> 21) & 0x3f);
|
|
|
|
GalVertexAttribType Type = (GalVertexAttribType)((Packed >> 27) & 0x7);
|
|
|
|
|
|
|
|
bool IsRgba = ((Packed >> 31) & 1) != 0;
|
|
|
|
|
2018-10-13 01:37:01 +00:00
|
|
|
//Note: 16 is the maximum size of an attribute,
|
|
|
|
//having a component size of 32-bits with 4 elements (a vec4).
|
2018-11-28 22:18:09 +00:00
|
|
|
byte[] Data = Vmm.ReadBytes(VbPosition + Offset, 16);
|
|
|
|
|
|
|
|
Attribs[ArrayIndex].Add(new GalVertexAttrib(Attr, IsConst, Offset, Data, Size, Type, IsRgba));
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
State.VertexBindings = new GalVertexBinding[32];
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
for (int Index = 0; Index < 32; Index++)
|
|
|
|
{
|
2018-06-09 00:15:56 +00:00
|
|
|
if (Attribs[Index] == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-26 02:11:26 +00:00
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
|
|
|
|
|
|
|
|
bool Enable = (Control & 0x1000) != 0;
|
|
|
|
|
|
|
|
if (!Enable)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
|
|
|
|
long VbEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
|
2018-08-10 04:09:40 +00:00
|
|
|
|
2018-08-25 04:16:58 +00:00
|
|
|
int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4);
|
|
|
|
|
2018-09-20 01:02:11 +00:00
|
|
|
bool Instanced = ReadRegisterBool(NvGpuEngine3dReg.VertexArrayNInstance + Index);
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2018-04-29 20:58:38 +00:00
|
|
|
int Stride = Control & 0xfff;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-25 04:16:58 +00:00
|
|
|
if (Instanced && VertexDivisor != 0)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
VbPosition += Stride * (CurrentInstance / VertexDivisor);
|
2018-08-25 04:16:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if (VbPosition > VbEndPos)
|
2018-08-25 04:16:58 +00:00
|
|
|
{
|
|
|
|
//Instance is invalid, ignore the draw call
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
long VboKey = Vmm.GetPhysicalAddress(VbPosition);
|
2018-08-25 04:16:58 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
long VbSize = (VbEndPos - VbPosition) + 1;
|
2019-01-01 17:08:15 +00:00
|
|
|
int ModifiedVbSize = (int)VbSize;
|
2018-04-29 20:58:38 +00:00
|
|
|
|
2019-01-01 17:08:15 +00:00
|
|
|
|
|
|
|
// If quads convert size to triangle length
|
|
|
|
if (Stride == 0)
|
|
|
|
{
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
|
|
|
ModifiedVbSize = QuadHelper.ConvertSizeQuadsToTris(ModifiedVbSize);
|
|
|
|
}
|
|
|
|
else if (PrimType == GalPrimitiveType.QuadStrip)
|
|
|
|
{
|
|
|
|
ModifiedVbSize = QuadHelper.ConvertSizeQuadStripToTris(ModifiedVbSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, ModifiedVbSize);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
if (!VboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex))
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
if ((PrimType == GalPrimitiveType.Quads | PrimType == GalPrimitiveType.QuadStrip) && Stride != 0)
|
|
|
|
{
|
|
|
|
// Convert quad buffer to triangles
|
|
|
|
byte[] data = Vmm.ReadBytes(VbPosition, VbSize);
|
|
|
|
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
|
|
|
data = QuadHelper.ConvertQuadsToTris(data, Stride, (int)(VbSize / Stride));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = QuadHelper.ConvertQuadStripToTris(data, Stride, (int)(VbSize / Stride));
|
|
|
|
}
|
|
|
|
Gpu.Renderer.Rasterizer.CreateVbo(VboKey, data);
|
|
|
|
}
|
|
|
|
else if (Vmm.TryGetHostAddress(VbPosition, VbSize, out IntPtr VbPtr))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, VbPtr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Vmm.ReadBytes(VbPosition, VbSize));
|
|
|
|
}
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-25 04:16:58 +00:00
|
|
|
State.VertexBindings[Index].Enabled = true;
|
|
|
|
State.VertexBindings[Index].Stride = Stride;
|
|
|
|
State.VertexBindings[Index].VboKey = VboKey;
|
|
|
|
State.VertexBindings[Index].Instanced = Instanced;
|
|
|
|
State.VertexBindings[Index].Divisor = VertexDivisor;
|
|
|
|
State.VertexBindings[Index].Attribs = Attribs[Index].ToArray();
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
2018-08-10 04:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State)
|
|
|
|
{
|
|
|
|
int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
|
|
|
|
int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-09 00:15:56 +00:00
|
|
|
GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-25 04:16:58 +00:00
|
|
|
bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0;
|
|
|
|
bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0;
|
|
|
|
|
|
|
|
if (InstanceNext && InstanceCont)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InstanceNext)
|
|
|
|
{
|
|
|
|
CurrentInstance++;
|
|
|
|
}
|
|
|
|
else if (!InstanceCont)
|
|
|
|
{
|
|
|
|
CurrentInstance = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
State.Instance = CurrentInstance;
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
Gpu.Renderer.Pipeline.Bind(State);
|
|
|
|
|
2018-09-25 22:55:30 +00:00
|
|
|
Gpu.Renderer.RenderTarget.Bind();
|
|
|
|
|
2018-06-09 00:15:56 +00:00
|
|
|
if (IndexCount != 0)
|
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
|
|
|
|
int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
|
|
|
|
int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase);
|
|
|
|
|
|
|
|
long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
|
|
|
|
|
|
|
|
long IboKey = Vmm.GetPhysicalAddress(IndexPosition);
|
2018-06-28 23:48:18 +00:00
|
|
|
|
2018-10-13 01:37:01 +00:00
|
|
|
//Quad primitive types were deprecated on OpenGL 3.x,
|
|
|
|
//they are converted to a triangles index buffer on IB creation,
|
|
|
|
//so we should use the triangles type here too.
|
2019-01-01 17:08:15 +00:00
|
|
|
if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip)
|
2018-10-13 01:37:01 +00:00
|
|
|
{
|
|
|
|
//Note: We assume that index first points to the first
|
|
|
|
//vertex of a quad, if it points to the middle of a
|
|
|
|
//quad (First % 4 != 0 for Quads) then it will not work properly.
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
IndexFirst = QuadHelper.ConvertSizeQuadsToTris(IndexFirst);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
2019-01-01 17:08:15 +00:00
|
|
|
else // QuadStrip
|
2018-10-13 01:37:01 +00:00
|
|
|
{
|
2019-01-01 17:08:15 +00:00
|
|
|
IndexFirst = QuadHelper.ConvertSizeQuadStripToTris(IndexFirst);
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
2019-01-01 17:08:15 +00:00
|
|
|
|
|
|
|
PrimType = GalPrimitiveType.Triangles;
|
2018-10-13 01:37:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:55:15 +00:00
|
|
|
Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType);
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-10 04:09:40 +00:00
|
|
|
int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
|
|
|
|
int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
|
|
|
|
|
2019-01-01 17:08:15 +00:00
|
|
|
//Quad primitive types were deprecated on OpenGL 3.x,
|
|
|
|
//they are converted to a triangles index buffer on IB creation,
|
|
|
|
//so we should use the triangles type here too.
|
|
|
|
if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip)
|
|
|
|
{
|
|
|
|
//Note: We assume that index first points to the first
|
|
|
|
//vertex of a quad, if it points to the middle of a
|
|
|
|
//quad (First % 4 != 0 for Quads) then it will not work properly.
|
|
|
|
if (PrimType == GalPrimitiveType.Quads)
|
|
|
|
{
|
|
|
|
VertexFirst = QuadHelper.ConvertSizeQuadsToTris(VertexFirst);
|
|
|
|
}
|
|
|
|
else // QuadStrip
|
|
|
|
{
|
|
|
|
VertexFirst = QuadHelper.ConvertSizeQuadStripToTris(VertexFirst);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimType = GalPrimitiveType.Triangles;
|
|
|
|
VertexCount = QuadHelper.ConvertSizeQuadsToTris(VertexCount);
|
|
|
|
}
|
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-08-13 21:22:09 +00:00
|
|
|
|
|
|
|
//Is the GPU really clearing those registers after draw?
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0);
|
|
|
|
WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 14:54:56 +00:00
|
|
|
private enum QueryMode
|
|
|
|
{
|
|
|
|
WriteSeq,
|
|
|
|
Sync,
|
|
|
|
WriteCounterAndTimestamp
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void QueryControl(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
WriteRegister(MethCall);
|
2018-09-01 14:54:56 +00:00
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
|
|
|
|
int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-09-01 14:54:56 +00:00
|
|
|
QueryMode Mode = (QueryMode)(Ctrl & 3);
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
|
2018-09-01 14:54:56 +00:00
|
|
|
switch (Mode)
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
{
|
2018-09-01 14:54:56 +00:00
|
|
|
case QueryMode.WriteSeq: Vmm.WriteInt32(Position, Seq); break;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-09-01 14:54:56 +00:00
|
|
|
case QueryMode.WriteCounterAndTimestamp:
|
|
|
|
{
|
|
|
|
//TODO: Implement counters.
|
|
|
|
long Counter = 1;
|
|
|
|
|
2018-10-28 22:31:13 +00:00
|
|
|
long Timestamp = PerformanceCounter.ElapsedMilliseconds;
|
2018-09-01 14:54:56 +00:00
|
|
|
|
|
|
|
Timestamp = (long)(Timestamp * 615384.615385);
|
|
|
|
|
|
|
|
Vmm.WriteInt64(Position + 0, Counter);
|
|
|
|
Vmm.WriteInt64(Position + 8, Timestamp);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void CbData(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
Vmm.WriteInt32(Position + Offset, MethCall.Argument);
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset + 4);
|
2018-08-15 01:27:05 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
Gpu.ResourceManager.ClearPbCache(NvGpuBufferType.ConstBuffer);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void CbBind(NvGpuVmm Vmm, GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
int Stage = (MethCall.Method - 0x904) >> 3;
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
int Index = MethCall.Argument;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
|
|
|
bool Enabled = (Index & 1) != 0;
|
|
|
|
|
|
|
|
Index = (Index >> 4) & 0x1f;
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
long CbKey = Vmm.GetPhysicalAddress(Position);
|
|
|
|
|
2018-08-10 04:09:40 +00:00
|
|
|
int Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
if (!Gpu.Renderer.Buffer.IsCached(CbKey, Size))
|
2018-08-10 04:09:40 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
Gpu.Renderer.Buffer.Create(CbKey, Size);
|
2018-08-10 04:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstBuffer Cb = ConstBuffers[Stage][Index];
|
|
|
|
|
|
|
|
if (Cb.Position != Position || Cb.Enabled != Enabled || Cb.Size != Size)
|
|
|
|
{
|
|
|
|
ConstBuffers[Stage][Index].Position = Position;
|
|
|
|
ConstBuffers[Stage][Index].Enabled = Enabled;
|
|
|
|
ConstBuffers[Stage][Index].Size = Size;
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 18:47:29 +00:00
|
|
|
private float GetFlipSign(NvGpuEngine3dReg Reg)
|
|
|
|
{
|
|
|
|
return MathF.Sign(ReadRegisterFloat(Reg));
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(long)Registers[(int)Reg + 0] << 32 |
|
|
|
|
(uint)Registers[(int)Reg + 1];
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:01:31 +00:00
|
|
|
private void WriteRegister(GpuMethodCall MethCall)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-11-17 04:01:31 +00:00
|
|
|
Registers[MethCall.Method] = MethCall.Argument;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private int ReadRegister(NvGpuEngine3dReg Reg)
|
|
|
|
{
|
|
|
|
return Registers[(int)Reg];
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:47:29 +00:00
|
|
|
private float ReadRegisterFloat(NvGpuEngine3dReg Reg)
|
|
|
|
{
|
|
|
|
return BitConverter.Int32BitsToSingle(ReadRegister(Reg));
|
|
|
|
}
|
|
|
|
|
2018-09-20 01:02:11 +00:00
|
|
|
private bool ReadRegisterBool(NvGpuEngine3dReg Reg)
|
|
|
|
{
|
|
|
|
return (ReadRegister(Reg) & 1) != 0;
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
|
|
|
|
{
|
|
|
|
Registers[(int)Reg] = Value;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|