From e7559f128f99058774a8d53aa45213b51c085e76 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sat, 23 Jun 2018 21:39:25 -0300 Subject: Small OpenGL Renderer refactoring (#177) * Call OpenGL functions directly, remove the pfifo thread, some refactoring * Fix PerformanceStatistics calculating the wrong host fps, remove wait event on PFIFO as this wasn't exactly was causing the freezes (may replace with an exception later) * Organized the Gpu folder a bit more, renamed a few things, address PR feedback * Make PerformanceStatistics thread safe * Remove unused constant * Use unlimited update rate for better pref --- ChocolArm64/Instruction/ASoftFallback.cs | 1 - Ryujinx.Graphics/Gal/GalConsts.cs | 7 - Ryujinx.Graphics/Gal/IGalBlend.cs | 22 + Ryujinx.Graphics/Gal/IGalFrameBuffer.cs | 27 + Ryujinx.Graphics/Gal/IGalRasterizer.cs | 23 + Ryujinx.Graphics/Gal/IGalRenderer.cs | 81 +-- Ryujinx.Graphics/Gal/IGalShader.cs | 21 + Ryujinx.Graphics/Gal/IGalTexture.cs | 13 + Ryujinx.Graphics/Gal/OpenGL/OGLBlend.cs | 2 +- Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs | 39 +- Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs | 35 +- Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs | 50 ++ Ryujinx.Graphics/Gal/OpenGL/OGLShader.cs | 24 +- Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs | 18 +- Ryujinx.Graphics/Gal/OpenGL/OpenGLRenderer.cs | 284 ----------- Ryujinx.Graphics/Gal/Shader/GlslDecl.cs | 2 + Ryujinx.Graphics/Gal/Shader/GlslDecompiler.cs | 2 +- Ryujinx.HLE/Gpu/BlockLinearSwizzle.cs | 59 --- Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs | 11 + Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs | 434 ++++++++++++++++ Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs | 11 + Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs | 170 +++++++ Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs | 25 + Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs | 556 +++++++++++++++++++++ Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs | 61 +++ Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs | 143 ++++++ Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs | 22 + Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs | 183 +++++++ Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs | 11 + Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs | 6 + Ryujinx.HLE/Gpu/Exceptions/GpuException.cs | 11 + Ryujinx.HLE/Gpu/Exceptions/GpuExceptionHelper.cs | 12 + Ryujinx.HLE/Gpu/INvGpuEngine.cs | 9 - Ryujinx.HLE/Gpu/ISwizzle.cs | 7 - Ryujinx.HLE/Gpu/LinearSwizzle.cs | 19 - Ryujinx.HLE/Gpu/MacroInterpreter.cs | 417 ---------------- Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs | 9 + Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs | 23 + Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs | 101 ++++ Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs | 410 +++++++++++++++ Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs | 209 ++++++++ Ryujinx.HLE/Gpu/NvGpu.cs | 22 +- Ryujinx.HLE/Gpu/NvGpuBufferType.cs | 9 - Ryujinx.HLE/Gpu/NvGpuEngine.cs | 11 - Ryujinx.HLE/Gpu/NvGpuEngine2d.cs | 168 ------- Ryujinx.HLE/Gpu/NvGpuEngine2dReg.cs | 25 - Ryujinx.HLE/Gpu/NvGpuEngine3d.cs | 540 -------------------- Ryujinx.HLE/Gpu/NvGpuEngine3dReg.cs | 61 --- Ryujinx.HLE/Gpu/NvGpuEngineDma.cs | 141 ------ Ryujinx.HLE/Gpu/NvGpuEngineDmaReg.cs | 22 - Ryujinx.HLE/Gpu/NvGpuFifo.cs | 182 ------- Ryujinx.HLE/Gpu/NvGpuFifoMeth.cs | 11 - Ryujinx.HLE/Gpu/NvGpuMethod.cs | 4 - Ryujinx.HLE/Gpu/NvGpuPBEntry.cs | 23 - Ryujinx.HLE/Gpu/NvGpuPushBuffer.cs | 101 ---- Ryujinx.HLE/Gpu/NvGpuVmm.cs | 410 --------------- Ryujinx.HLE/Gpu/NvGpuVmmCache.cs | 209 -------- Ryujinx.HLE/Gpu/Texture.cs | 55 -- Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs | 59 +++ Ryujinx.HLE/Gpu/Texture/ISwizzle.cs | 7 + Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs | 19 + Ryujinx.HLE/Gpu/Texture/TextureFactory.cs | 117 +++++ Ryujinx.HLE/Gpu/Texture/TextureHelper.cs | 78 +++ Ryujinx.HLE/Gpu/Texture/TextureInfo.cs | 55 ++ Ryujinx.HLE/Gpu/Texture/TextureReader.cs | 344 +++++++++++++ Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs | 11 + Ryujinx.HLE/Gpu/Texture/TextureWriter.cs | 55 ++ Ryujinx.HLE/Gpu/TextureFactory.cs | 116 ----- Ryujinx.HLE/Gpu/TextureHelper.cs | 77 --- Ryujinx.HLE/Gpu/TextureReader.cs | 344 ------------- Ryujinx.HLE/Gpu/TextureSwizzle.cs | 11 - Ryujinx.HLE/Gpu/TextureWriter.cs | 55 -- Ryujinx.HLE/Loaders/Npdm/ACI0.cs | 3 +- Ryujinx.HLE/Loaders/Npdm/ACID.cs | 3 +- Ryujinx.HLE/Loaders/Npdm/Npdm.cs | 6 +- Ryujinx.HLE/Loaders/Npdm/ServiceAccessControl.cs | 5 +- .../OsHle/Services/Nv/NvGpuAS/NvGpuASIoctl.cs | 2 +- .../Nv/NvHostChannel/NvHostChannelIoctl.cs | 2 +- Ryujinx.HLE/OsHle/Services/Nv/NvMap/NvMapIoctl.cs | 2 +- Ryujinx.HLE/OsHle/Services/Vi/NvFlinger.cs | 11 +- Ryujinx.HLE/PerformanceStatistics.cs | 135 +++-- Ryujinx.HLE/Switch.cs | 5 + Ryujinx/Ui/GLScreen.cs | 27 +- Ryujinx/Ui/Program.cs | 4 +- 84 files changed, 3501 insertions(+), 3616 deletions(-) delete mode 100644 Ryujinx.Graphics/Gal/GalConsts.cs create mode 100644 Ryujinx.Graphics/Gal/IGalBlend.cs create mode 100644 Ryujinx.Graphics/Gal/IGalFrameBuffer.cs create mode 100644 Ryujinx.Graphics/Gal/IGalRasterizer.cs create mode 100644 Ryujinx.Graphics/Gal/IGalShader.cs create mode 100644 Ryujinx.Graphics/Gal/IGalTexture.cs create mode 100644 Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs delete mode 100644 Ryujinx.Graphics/Gal/OpenGL/OpenGLRenderer.cs delete mode 100644 Ryujinx.HLE/Gpu/BlockLinearSwizzle.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs create mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs create mode 100644 Ryujinx.HLE/Gpu/Exceptions/GpuException.cs create mode 100644 Ryujinx.HLE/Gpu/Exceptions/GpuExceptionHelper.cs delete mode 100644 Ryujinx.HLE/Gpu/INvGpuEngine.cs delete mode 100644 Ryujinx.HLE/Gpu/ISwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/LinearSwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/MacroInterpreter.cs create mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs create mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs create mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs create mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs create mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuBufferType.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngine.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngine2d.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngine2dReg.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngine3d.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngine3dReg.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngineDma.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuEngineDmaReg.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuFifo.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuFifoMeth.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuMethod.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuPBEntry.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuPushBuffer.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuVmm.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpuVmmCache.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/ISwizzle.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureFactory.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureHelper.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureInfo.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureReader.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs create mode 100644 Ryujinx.HLE/Gpu/Texture/TextureWriter.cs delete mode 100644 Ryujinx.HLE/Gpu/TextureFactory.cs delete mode 100644 Ryujinx.HLE/Gpu/TextureHelper.cs delete mode 100644 Ryujinx.HLE/Gpu/TextureReader.cs delete mode 100644 Ryujinx.HLE/Gpu/TextureSwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/TextureWriter.cs diff --git a/ChocolArm64/Instruction/ASoftFallback.cs b/ChocolArm64/Instruction/ASoftFallback.cs index 6a407baf..d626622a 100644 --- a/ChocolArm64/Instruction/ASoftFallback.cs +++ b/ChocolArm64/Instruction/ASoftFallback.cs @@ -1,6 +1,5 @@ using ChocolArm64.Translation; using System; -using System.Numerics; namespace ChocolArm64.Instruction { diff --git a/Ryujinx.Graphics/Gal/GalConsts.cs b/Ryujinx.Graphics/Gal/GalConsts.cs deleted file mode 100644 index 6c8857c6..00000000 --- a/Ryujinx.Graphics/Gal/GalConsts.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Ryujinx.Graphics.Gal -{ - public static class GalConsts - { - public const string FlipUniformName = "flip"; - } -} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalBlend.cs b/Ryujinx.Graphics/Gal/IGalBlend.cs new file mode 100644 index 00000000..5c96a492 --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalBlend.cs @@ -0,0 +1,22 @@ +namespace Ryujinx.Graphics.Gal +{ + public interface IGalBlend + { + void Enable(); + + void Disable(); + + void Set( + GalBlendEquation Equation, + GalBlendFactor FuncSrc, + GalBlendFactor FuncDst); + + void SetSeparate( + GalBlendEquation EquationRgb, + GalBlendEquation EquationAlpha, + GalBlendFactor FuncSrcRgb, + GalBlendFactor FuncDstRgb, + GalBlendFactor FuncSrcAlpha, + GalBlendFactor FuncDstAlpha); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs b/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs new file mode 100644 index 00000000..eaae0a49 --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs @@ -0,0 +1,27 @@ +using System; + +namespace Ryujinx.Graphics.Gal +{ + public interface IGalFrameBuffer + { + void Create(long Key, int Width, int Height); + + void Bind(long Key); + + void BindTexture(long Key, int Index); + + void Set(long Key); + + void Set(byte[] Data, int Width, int Height); + + void SetTransform(float SX, float SY, float Rotate, float TX, float TY); + + void SetWindowSize(int Width, int Height); + + void SetViewport(int X, int Y, int Width, int Height); + + void Render(); + + void GetBufferData(long Key, Action Callback); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalRasterizer.cs b/Ryujinx.Graphics/Gal/IGalRasterizer.cs new file mode 100644 index 00000000..81c922be --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalRasterizer.cs @@ -0,0 +1,23 @@ +namespace Ryujinx.Graphics.Gal +{ + public interface IGalRasterizer + { + void ClearBuffers(int RtIndex, GalClearBufferFlags Flags); + + bool IsVboCached(long Key, long DataSize); + + bool IsIboCached(long Key, long DataSize); + + void CreateVbo(long Key, byte[] Buffer); + + void CreateIbo(long Key, byte[] Buffer); + + void SetVertexArray(int VbIndex, int Stride, long VboKey, GalVertexAttrib[] Attribs); + + void SetIndexArray(long Key, int Size, GalIndexFormat Format); + + void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType); + + void DrawElements(long IboKey, int First, GalPrimitiveType PrimType); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalRenderer.cs b/Ryujinx.Graphics/Gal/IGalRenderer.cs index b8f83469..c6324c4a 100644 --- a/Ryujinx.Graphics/Gal/IGalRenderer.cs +++ b/Ryujinx.Graphics/Gal/IGalRenderer.cs @@ -1,90 +1,21 @@ using System; -using System.Collections.Generic; namespace Ryujinx.Graphics.Gal { - public unsafe interface IGalRenderer + public interface IGalRenderer { void QueueAction(Action ActionMthd); void RunActions(); - void Render(); + IGalBlend Blend { get; } - void SetWindowSize(int Width, int Height); + IGalFrameBuffer FrameBuffer { get; } - //Blend - void SetBlendEnable(bool Enable); + IGalRasterizer Rasterizer { get; } - void SetBlend( - GalBlendEquation Equation, - GalBlendFactor FuncSrc, - GalBlendFactor FuncDst); + IGalShader Shader { get; } - void SetBlendSeparate( - GalBlendEquation EquationRgb, - GalBlendEquation EquationAlpha, - GalBlendFactor FuncSrcRgb, - GalBlendFactor FuncDstRgb, - GalBlendFactor FuncSrcAlpha, - GalBlendFactor FuncDstAlpha); - - //Frame Buffer - void CreateFrameBuffer(long Tag, int Width, int Height); - - void BindFrameBuffer(long Tag); - - void BindFrameBufferTexture(long Tag, int Index, GalTextureSampler Sampler); - - void SetFrameBuffer(long Tag); - - void SetFrameBuffer(byte[] Data, int Width, int Height); - - void SetFrameBufferTransform(float SX, float SY, float Rotate, float TX, float TY); - - void SetViewport(int X, int Y, int Width, int Height); - - void GetFrameBufferData(long Tag, Action Callback); - - //Rasterizer - void ClearBuffers(int RtIndex, GalClearBufferFlags Flags); - - bool IsVboCached(long Tag, long DataSize); - - bool IsIboCached(long Tag, long DataSize); - - void CreateVbo(long Tag, byte[] Buffer); - - void CreateIbo(long Tag, byte[] Buffer); - - void SetVertexArray(int VbIndex, int Stride, long VboTag, GalVertexAttrib[] Attribs); - - void SetIndexArray(long Tag, int Size, GalIndexFormat Format); - - void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType); - - void DrawElements(long IboTag, int First, GalPrimitiveType PrimType); - - //Shader - void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type); - - IEnumerable GetTextureUsage(long Tag); - - void SetConstBuffer(long Tag, int Cbuf, byte[] Data); - - void SetUniform1(string UniformName, int Value); - - void SetUniform2F(string UniformName, float X, float Y); - - void BindShader(long Tag); - - void BindProgram(); - - //Texture - void SetTextureAndSampler(long Tag, byte[] Data, GalTexture Texture, GalTextureSampler Sampler); - - bool TryGetCachedTexture(long Tag, long DataSize, out GalTexture Texture); - - void BindTexture(long Tag, int Index); + IGalTexture Texture { get; } } } \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalShader.cs b/Ryujinx.Graphics/Gal/IGalShader.cs new file mode 100644 index 00000000..79e77c0a --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalShader.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Gal +{ + public interface IGalShader + { + void Create(IGalMemory Memory, long Key, GalShaderType Type); + + IEnumerable GetTextureUsage(long Key); + + void SetConstBuffer(long Key, int Cbuf, byte[] Data); + + void EnsureTextureBinding(string UniformName, int Value); + + void SetFlip(float X, float Y); + + void Bind(long Key); + + void BindProgram(); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalTexture.cs b/Ryujinx.Graphics/Gal/IGalTexture.cs new file mode 100644 index 00000000..6379e73a --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalTexture.cs @@ -0,0 +1,13 @@ +namespace Ryujinx.Graphics.Gal +{ + public interface IGalTexture + { + void Create(long Key, byte[] Data, GalTexture Texture); + + bool TryGetCachedTexture(long Key, long DataSize, out GalTexture Texture); + + void Bind(long Key, int Index); + + void SetSampler(GalTextureSampler Sampler); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLBlend.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLBlend.cs index e33851e5..7175e3a0 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLBlend.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLBlend.cs @@ -2,7 +2,7 @@ using OpenTK.Graphics.OpenGL; namespace Ryujinx.Graphics.Gal.OpenGL { - class OGLBlend + public class OGLBlend : IGalBlend { public void Enable() { diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs index 8f265f54..4d91ff97 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; namespace Ryujinx.Graphics.Gal.OpenGL { - class OGLFrameBuffer + public class OGLFrameBuffer : IGalFrameBuffer { private struct Rect { @@ -16,9 +16,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL public Rect(int X, int Y, int Width, int Height) { - this.X = X; - this.Y = Y; - this.Width = Width; + this.X = X; + this.Y = Y; + this.Width = Width; this.Height = Height; } } @@ -76,14 +76,14 @@ namespace Ryujinx.Graphics.Gal.OpenGL Shader = new ShaderProgram(); } - public void Create(long Tag, int Width, int Height) + public void Create(long Key, int Width, int Height) { //TODO: We should either use the original frame buffer size, //or just remove the Width/Height arguments. Width = Window.Width; Height = Window.Height; - if (Fbs.TryGetValue(Tag, out FrameBuffer Fb)) + if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) { if (Fb.Width != Width || Fb.Height != Height) @@ -127,12 +127,12 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.Viewport(0, 0, Width, Height); - Fbs.Add(Tag, Fb); + Fbs.Add(Key, Fb); } - public void Bind(long Tag) + public void Bind(long Key) { - if (Fbs.TryGetValue(Tag, out FrameBuffer Fb)) + if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, Fb.Handle); @@ -140,9 +140,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL } } - public void BindTexture(long Tag, int Index) + public void BindTexture(long Key, int Index) { - if (Fbs.TryGetValue(Tag, out FrameBuffer Fb)) + if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) { GL.ActiveTexture(TextureUnit.Texture0 + Index); @@ -150,9 +150,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL } } - public void Set(long Tag) + public void Set(long Key) { - if (Fbs.TryGetValue(Tag, out FrameBuffer Fb)) + if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) { CurrTexHandle = Fb.TexHandle; } @@ -185,10 +185,17 @@ namespace Ryujinx.Graphics.Gal.OpenGL CurrTexHandle = RawFbTexHandle; } - public void SetTransform(Matrix2 Transform, Vector2 Offs) + public void SetTransform(float SX, float SY, float Rotate, float TX, float TY) { EnsureInitialized(); + Matrix2 Transform; + + Transform = Matrix2.CreateScale(SX, SY); + Transform *= Matrix2.CreateRotation(Rotate); + + Vector2 Offs = new Vector2(TX, TY); + int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram); GL.UseProgram(Shader.Handle); @@ -270,9 +277,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL } } - public void GetBufferData(long Tag, Action Callback) + public void GetBufferData(long Key, Action Callback) { - if (Fbs.TryGetValue(Tag, out FrameBuffer Fb)) + if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) { GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, Fb.Handle); diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs index b63c8b35..bdf22b9c 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace Ryujinx.Graphics.Gal.OpenGL { - class OGLRasterizer + public class OGLRasterizer : IGalRasterizer { private static Dictionary AttribElements = new Dictionary() @@ -74,8 +74,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL { ClearBufferMask Mask = 0; - //OpenGL doesn't support clearing just a single color channel, - //so we can't just clear all channels... + //TODO: Use glColorMask to clear just the specified channels. if (Flags.HasFlag(GalClearBufferFlags.ColorRed) && Flags.HasFlag(GalClearBufferFlags.ColorGreen) && Flags.HasFlag(GalClearBufferFlags.ColorBlue) && @@ -97,45 +96,43 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.Clear(Mask); } - public bool IsVboCached(long Tag, long DataSize) + public bool IsVboCached(long Key, long DataSize) { - return VboCache.TryGetSize(Tag, out long Size) && Size == DataSize; + return VboCache.TryGetSize(Key, out long Size) && Size == DataSize; } - public bool IsIboCached(long Tag, long DataSize) + public bool IsIboCached(long Key, long DataSize) { - return IboCache.TryGetSize(Tag, out long Size) && Size == DataSize; + return IboCache.TryGetSize(Key, out long Size) && Size == DataSize; } - public void CreateVbo(long Tag, byte[] Buffer) + public void CreateVbo(long Key, byte[] Buffer) { int Handle = GL.GenBuffer(); - VboCache.AddOrUpdate(Tag, Handle, (uint)Buffer.Length); + VboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length); IntPtr Length = new IntPtr(Buffer.Length); GL.BindBuffer(BufferTarget.ArrayBuffer, Handle); GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw); - GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } - public void CreateIbo(long Tag, byte[] Buffer) + public void CreateIbo(long Key, byte[] Buffer) { int Handle = GL.GenBuffer(); - IboCache.AddOrUpdate(Tag, Handle, (uint)Buffer.Length); + IboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length); IntPtr Length = new IntPtr(Buffer.Length); GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle); GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw); - GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } - public void SetVertexArray(int VbIndex, int Stride, long VboTag, GalVertexAttrib[] Attribs) + public void SetVertexArray(int VbIndex, int Stride, long VboKey, GalVertexAttrib[] Attribs) { - if (!VboCache.TryGetValue(VboTag, out int VboHandle)) + if (!VboCache.TryGetValue(VboKey, out int VboHandle)) { return; } @@ -178,11 +175,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Stride, Offset); } - - GL.BindVertexArray(0); } - public void SetIndexArray(long Tag, int Size, GalIndexFormat Format) + public void SetIndexArray(long Key, int Size, GalIndexFormat Format) { IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format); @@ -201,9 +196,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, PrimCount); } - public void DrawElements(long IboTag, int First, GalPrimitiveType PrimType) + public void DrawElements(long IboKey, int First, GalPrimitiveType PrimType) { - if (!IboCache.TryGetValue(IboTag, out int IboHandle)) + if (!IboCache.TryGetValue(IboKey, out int IboHandle)) { return; } diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs new file mode 100644 index 00000000..ca70d4f6 --- /dev/null +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Concurrent; + +namespace Ryujinx.Graphics.Gal.OpenGL +{ + public class OGLRenderer : IGalRenderer + { + public IGalBlend Blend { get; private set; } + + public IGalFrameBuffer FrameBuffer { get; private set; } + + public IGalRasterizer Rasterizer { get; private set; } + + public IGalShader Shader { get; private set; } + + public IGalTexture Texture { get; private set; } + + private ConcurrentQueue ActionsQueue; + + public OGLRenderer() + { + Blend = new OGLBlend(); + + FrameBuffer = new OGLFrameBuffer(); + + Rasterizer = new OGLRasterizer(); + + Shader = new OGLShader(); + + Texture = new OGLTexture(); + + ActionsQueue = new ConcurrentQueue(); + } + + public void QueueAction(Action ActionMthd) + { + ActionsQueue.Enqueue(ActionMthd); + } + + public void RunActions() + { + int Count = ActionsQueue.Count; + + while (Count-- > 0 && ActionsQueue.TryDequeue(out Action RenderAction)) + { + RenderAction(); + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLShader.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLShader.cs index be1bb20b..5760d172 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLShader.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLShader.cs @@ -7,7 +7,7 @@ using System.Linq; namespace Ryujinx.Graphics.Gal.OpenGL { - class OGLShader + public class OGLShader : IGalShader { private class ShaderStage : IDisposable { @@ -84,9 +84,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL Programs = new Dictionary(); } - public void Create(IGalMemory Memory, long Tag, GalShaderType Type) + public void Create(IGalMemory Memory, long Key, GalShaderType Type) { - Stages.GetOrAdd(Tag, (Key) => ShaderStageFactory(Memory, Tag, Type)); + Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, Type)); } private ShaderStage ShaderStageFactory(IGalMemory Memory, long Position, GalShaderType Type) @@ -107,9 +107,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL return Decompiler.Decompile(Memory, Position + 0x50, Type); } - public IEnumerable GetTextureUsage(long Tag) + public IEnumerable GetTextureUsage(long Key) { - if (Stages.TryGetValue(Tag, out ShaderStage Stage)) + if (Stages.TryGetValue(Key, out ShaderStage Stage)) { return Stage.TextureUsage; } @@ -117,11 +117,11 @@ namespace Ryujinx.Graphics.Gal.OpenGL return Enumerable.Empty(); } - public void SetConstBuffer(long Tag, int Cbuf, byte[] Data) + public void SetConstBuffer(long Key, int Cbuf, byte[] Data) { BindProgram(); - if (Stages.TryGetValue(Tag, out ShaderStage Stage)) + if (Stages.TryGetValue(Key, out ShaderStage Stage)) { foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage.Where(x => x.Cbuf == Cbuf)) { @@ -144,7 +144,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL } } - public void SetUniform1(string UniformName, int Value) + public void EnsureTextureBinding(string UniformName, int Value) { BindProgram(); @@ -153,18 +153,18 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.Uniform1(Location, Value); } - public void SetUniform2F(string UniformName, float X, float Y) + public void SetFlip(float X, float Y) { BindProgram(); - int Location = GL.GetUniformLocation(CurrentProgramHandle, UniformName); + int Location = GL.GetUniformLocation(CurrentProgramHandle, GlslDecl.FlipUniformName); GL.Uniform2(Location, X, Y); } - public void Bind(long Tag) + public void Bind(long Key) { - if (Stages.TryGetValue(Tag, out ShaderStage Stage)) + if (Stages.TryGetValue(Key, out ShaderStage Stage)) { Bind(Stage); } diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs index 56f157e5..c50bdd71 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs @@ -4,7 +4,7 @@ using System; namespace Ryujinx.Graphics.Gal.OpenGL { - class OGLTexture + public class OGLTexture : IGalTexture { private class TCE { @@ -31,11 +31,11 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.DeleteTexture(CachedTexture.Handle); } - public void Create(long Tag, byte[] Data, GalTexture Texture) + public void Create(long Key, byte[] Data, GalTexture Texture) { int Handle = GL.GenTexture(); - TextureCache.AddOrUpdate(Tag, new TCE(Handle, Texture), (uint)Data.Length); + TextureCache.AddOrUpdate(Key, new TCE(Handle, Texture), (uint)Data.Length); GL.BindTexture(TextureTarget.Texture2D, Handle); @@ -146,11 +146,11 @@ namespace Ryujinx.Graphics.Gal.OpenGL throw new ArgumentException(nameof(Format)); } - public bool TryGetCachedTexture(long Tag, long DataSize, out GalTexture Texture) + public bool TryGetCachedTexture(long Key, long DataSize, out GalTexture Texture) { - if (TextureCache.TryGetSize(Tag, out long Size) && Size == DataSize) + if (TextureCache.TryGetSize(Key, out long Size) && Size == DataSize) { - if (TextureCache.TryGetValue(Tag, out TCE CachedTexture)) + if (TextureCache.TryGetValue(Key, out TCE CachedTexture)) { Texture = CachedTexture.Texture; @@ -163,9 +163,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL return false; } - public void Bind(long Tag, int Index) + public void Bind(long Key, int Index) { - if (TextureCache.TryGetValue(Tag, out TCE CachedTexture)) + if (TextureCache.TryGetValue(Key, out TCE CachedTexture)) { GL.ActiveTexture(TextureUnit.Texture0 + Index); @@ -173,7 +173,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL } } - public static void Set(GalTextureSampler Sampler) + public void SetSampler(GalTextureSampler Sampler) { int WrapS = (int)OGLEnumConverter.GetTextureWrapMode(Sampler.AddressU); int WrapT = (int)OGLEnumConverter.GetTextureWrapMode(Sampler.AddressV); diff --git a/Ryujinx.Graphics/Gal/OpenGL/OpenGLRenderer.cs b/Ryujinx.Graphics/Gal/OpenGL/OpenGLRenderer.cs deleted file mode 100644 index 4c4bd2ca..00000000 --- a/Ryujinx.Graphics/Gal/OpenGL/OpenGLRenderer.cs +++ /dev/null @@ -1,284 +0,0 @@ -using OpenTK; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; - -namespace Ryujinx.Graphics.Gal.OpenGL -{ - public class OpenGLRenderer : IGalRenderer - { - private OGLBlend Blend; - - private OGLFrameBuffer FrameBuffer; - - private OGLRasterizer Rasterizer; - - private OGLShader Shader; - - private OGLTexture Texture; - - private ConcurrentQueue ActionsQueue; - - public OpenGLRenderer() - { - Blend = new OGLBlend(); - - FrameBuffer = new OGLFrameBuffer(); - - Rasterizer = new OGLRasterizer(); - - Shader = new OGLShader(); - - Texture = new OGLTexture(); - - ActionsQueue = new ConcurrentQueue(); - } - - public void QueueAction(Action ActionMthd) - { - ActionsQueue.Enqueue(ActionMthd); - } - - public void RunActions() - { - int Count = ActionsQueue.Count; - - while (Count-- > 0 && ActionsQueue.TryDequeue(out Action RenderAction)) - { - RenderAction(); - } - } - - public void Render() - { - FrameBuffer.Render(); - } - - public void SetWindowSize(int Width, int Height) - { - FrameBuffer.SetWindowSize(Width, Height); - } - - public void SetBlendEnable(bool Enable) - { - if (Enable) - { - ActionsQueue.Enqueue(() => Blend.Enable()); - } - else - { - ActionsQueue.Enqueue(() => Blend.Disable()); - } - } - - public void SetBlend( - GalBlendEquation Equation, - GalBlendFactor FuncSrc, - GalBlendFactor FuncDst) - { - ActionsQueue.Enqueue(() => Blend.Set(Equation, FuncSrc, FuncDst)); - } - - public void SetBlendSeparate( - GalBlendEquation EquationRgb, - GalBlendEquation EquationAlpha, - GalBlendFactor FuncSrcRgb, - GalBlendFactor FuncDstRgb, - GalBlendFactor FuncSrcAlpha, - GalBlendFactor FuncDstAlpha) - { - ActionsQueue.Enqueue(() => - { - Blend.SetSeparate( - EquationRgb, - EquationAlpha, - FuncSrcRgb, - FuncDstRgb, - FuncSrcAlpha, - FuncDstAlpha); - }); - } - - public void CreateFrameBuffer(long Tag, int Width, int Height) - { - ActionsQueue.Enqueue(() => FrameBuffer.Create(Tag, Width, Height)); - } - - public void BindFrameBuffer(long Tag) - { - ActionsQueue.Enqueue(() => FrameBuffer.Bind(Tag)); - } - - public void BindFrameBufferTexture(long Tag, int Index, GalTextureSampler Sampler) - { - ActionsQueue.Enqueue(() => - { - FrameBuffer.BindTexture(Tag, Index); - - OGLTexture.Set(Sampler); - }); - } - - public void SetFrameBuffer(long Tag) - { - ActionsQueue.Enqueue(() => FrameBuffer.Set(Tag)); - } - - public void SetFrameBuffer(byte[] Data, int Width, int Height) - { - ActionsQueue.Enqueue(() => FrameBuffer.Set(Data, Width, Height)); - } - - public void SetFrameBufferTransform(float SX, float SY, float Rotate, float TX, float TY) - { - Matrix2 Transform; - - Transform = Matrix2.CreateScale(SX, SY); - Transform *= Matrix2.CreateRotation(Rotate); - - Vector2 Offs = new Vector2(TX, TY); - - ActionsQueue.Enqueue(() => FrameBuffer.SetTransform(Transform, Offs)); - } - - public void SetViewport(int X, int Y, int Width, int Height) - { - ActionsQueue.Enqueue(() => FrameBuffer.SetViewport(X, Y, Width, Height)); - } - - public void GetFrameBufferData(long Tag, Action Callback) - { - ActionsQueue.Enqueue(() => FrameBuffer.GetBufferData(Tag, Callback)); - } - - public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags) - { - ActionsQueue.Enqueue(() => Rasterizer.ClearBuffers(RtIndex, Flags)); - } - - public bool IsVboCached(long Tag, long DataSize) - { - return Rasterizer.IsVboCached(Tag, DataSize); - } - - public bool IsIboCached(long Tag, long DataSize) - { - return Rasterizer.IsIboCached(Tag, DataSize); - } - - public void CreateVbo(long Tag, byte[] Buffer) - { - ActionsQueue.Enqueue(() => Rasterizer.CreateVbo(Tag, Buffer)); - } - - public void CreateIbo(long Tag, byte[] Buffer) - { - ActionsQueue.Enqueue(() => Rasterizer.CreateIbo(Tag, Buffer)); - } - - public void SetVertexArray(int VbIndex, int Stride, long VboTag, GalVertexAttrib[] Attribs) - { - if ((uint)VbIndex > 31) - { - throw new ArgumentOutOfRangeException(nameof(VbIndex)); - } - - if (Attribs == null) - { - throw new ArgumentNullException(nameof(Attribs)); - } - - ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride, VboTag, Attribs)); - } - - public void SetIndexArray(long Tag, int Size, GalIndexFormat Format) - { - ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Tag, Size, Format)); - } - - public void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType) - { - ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(First, PrimCount, PrimType)); - } - - public void DrawElements(long IboTag, int First, GalPrimitiveType PrimType) - { - ActionsQueue.Enqueue(() => Rasterizer.DrawElements(IboTag, First, PrimType)); - } - - public void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type) - { - if (Memory == null) - { - throw new ArgumentNullException(nameof(Memory)); - } - - Shader.Create(Memory, Tag, Type); - } - - public void SetConstBuffer(long Tag, int Cbuf, byte[] Data) - { - if (Data == null) - { - throw new ArgumentNullException(nameof(Data)); - } - - ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data)); - } - - public void SetUniform1(string UniformName, int Value) - { - if (UniformName == null) - { - throw new ArgumentNullException(nameof(UniformName)); - } - - ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value)); - } - - public void SetUniform2F(string UniformName, float X, float Y) - { - if (UniformName == null) - { - throw new ArgumentNullException(nameof(UniformName)); - } - - ActionsQueue.Enqueue(() => Shader.SetUniform2F(UniformName, X, Y)); - } - - public IEnumerable GetTextureUsage(long Tag) - { - return Shader.GetTextureUsage(Tag); - } - - public void BindShader(long Tag) - { - ActionsQueue.Enqueue(() => Shader.Bind(Tag)); - } - - public void BindProgram() - { - ActionsQueue.Enqueue(() => Shader.BindProgram()); - } - - public void SetTextureAndSampler(long Tag, byte[] Data, GalTexture Texture, GalTextureSampler Sampler) - { - ActionsQueue.Enqueue(() => - { - this.Texture.Create(Tag, Data, Texture); - - OGLTexture.Set(Sampler); - }); - } - - public bool TryGetCachedTexture(long Tag, long DataSize, out GalTexture Texture) - { - return this.Texture.TryGetCachedTexture(Tag, DataSize, out Texture); - } - - public void BindTexture(long Tag, int Index) - { - ActionsQueue.Enqueue(() => Texture.Bind(Tag, Index)); - } - } -} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/Shader/GlslDecl.cs b/Ryujinx.Graphics/Gal/Shader/GlslDecl.cs index 4002c29a..86838ab2 100644 --- a/Ryujinx.Graphics/Gal/Shader/GlslDecl.cs +++ b/Ryujinx.Graphics/Gal/Shader/GlslDecl.cs @@ -26,6 +26,8 @@ namespace Ryujinx.Graphics.Gal.Shader public const string FragmentOutputName = "FragColor"; + public const string FlipUniformName = "flip"; + private string[] StagePrefixes = new string[] { "vp", "tcp", "tep", "gp", "fp" }; private string StagePrefix; diff --git a/Ryujinx.Graphics/Gal/Shader/GlslDecompiler.cs b/Ryujinx.Graphics/Gal/Shader/GlslDecompiler.cs index 77f16b81..24db303f 100644 --- a/Ryujinx.Graphics/Gal/Shader/GlslDecompiler.cs +++ b/Ryujinx.Graphics/Gal/Shader/GlslDecompiler.cs @@ -140,7 +140,7 @@ namespace Ryujinx.Graphics.Gal.Shader { if (Decl.ShaderType == GalShaderType.Vertex) { - SB.AppendLine("uniform vec2 " + GalConsts.FlipUniformName + ";"); + SB.AppendLine("uniform vec2 " + GlslDecl.FlipUniformName + ";"); } foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector)) diff --git a/Ryujinx.HLE/Gpu/BlockLinearSwizzle.cs b/Ryujinx.HLE/Gpu/BlockLinearSwizzle.cs deleted file mode 100644 index 366f5740..00000000 --- a/Ryujinx.HLE/Gpu/BlockLinearSwizzle.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; - -namespace Ryujinx.HLE.Gpu -{ - class BlockLinearSwizzle : ISwizzle - { - private int BhShift; - private int BppShift; - private int BhMask; - - private int XShift; - private int GobStride; - - public BlockLinearSwizzle(int Width, int Bpp, int BlockHeight = 16) - { - BhMask = (BlockHeight * 8) - 1; - - BhShift = CountLsbZeros(BlockHeight * 8); - BppShift = CountLsbZeros(Bpp); - - int WidthInGobs = (int)MathF.Ceiling(Width * Bpp / 64f); - - GobStride = 512 * BlockHeight * WidthInGobs; - - XShift = CountLsbZeros(512 * BlockHeight); - } - - private int CountLsbZeros(int Value) - { - int Count = 0; - - while (((Value >> Count) & 1) == 0) - { - Count++; - } - - return Count; - } - - public int GetSwizzleOffset(int X, int Y) - { - X <<= BppShift; - - int Position = (Y >> BhShift) * GobStride; - - Position += (X >> 6) << XShift; - - Position += ((Y & BhMask) >> 3) << 9; - - Position += ((X & 0x3f) >> 5) << 8; - Position += ((Y & 0x07) >> 1) << 6; - Position += ((X & 0x1f) >> 4) << 5; - Position += ((Y & 0x01) >> 0) << 4; - Position += ((X & 0x0f) >> 0) << 0; - - return Position; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs b/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs new file mode 100644 index 00000000..068878a9 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs @@ -0,0 +1,11 @@ +using Ryujinx.HLE.Gpu.Memory; + +namespace Ryujinx.HLE.Gpu.Engines +{ + interface INvGpuEngine + { + int[] Registers { get; } + + void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs b/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs new file mode 100644 index 00000000..aef2eb4c --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs @@ -0,0 +1,434 @@ +using Ryujinx.HLE.Gpu.Exceptions; +using Ryujinx.HLE.Gpu.Memory; +using System; +using System.Collections.Generic; + +namespace Ryujinx.HLE.Gpu.Engines +{ + class MacroInterpreter + { + private const int MaxCallCountPerRun = 500; + + private int CallCount; + + private enum AssignmentOperation + { + IgnoreAndFetch = 0, + Move = 1, + MoveAndSetMaddr = 2, + FetchAndSend = 3, + MoveAndSend = 4, + FetchAndSetMaddr = 5, + MoveAndSetMaddrThenFetchAndSend = 6, + MoveAndSetMaddrThenSendHigh = 7 + } + + private enum AluOperation + { + AluReg = 0, + AddImmediate = 1, + BitfieldReplace = 2, + BitfieldExtractLslImm = 3, + BitfieldExtractLslReg = 4, + ReadImmediate = 5 + } + + private enum AluRegOperation + { + Add = 0, + AddWithCarry = 1, + Subtract = 2, + SubtractWithBorrow = 3, + BitwiseExclusiveOr = 8, + BitwiseOr = 9, + BitwiseAnd = 10, + BitwiseAndNot = 11, + BitwiseNotAnd = 12 + } + + private NvGpuFifo PFifo; + private INvGpuEngine Engine; + + public Queue Fifo { get; private set; } + + private int[] Gprs; + + private int MethAddr; + private int MethIncr; + + private bool Carry; + + private int OpCode; + + private int PipeOp; + + private int Pc; + + public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine) + { + this.PFifo = PFifo; + this.Engine = Engine; + + Fifo = new Queue(); + + Gprs = new int[8]; + } + + public void Execute(NvGpuVmm Vmm, int[] Mme, int Position, int Param) + { + Reset(); + + Gprs[1] = Param; + + Pc = Position; + + FetchOpCode(Mme); + + while (Step(Vmm, Mme)); + + //Due to the delay slot, we still need to execute + //one more instruction before we actually exit. + Step(Vmm, Mme); + } + + private void Reset() + { + for (int Index = 0; Index < Gprs.Length; Index++) + { + Gprs[Index] = 0; + } + + MethAddr = 0; + MethIncr = 0; + + Carry = false; + + CallCount = 0; + } + + private bool Step(NvGpuVmm Vmm, int[] Mme) + { + int BaseAddr = Pc - 1; + + FetchOpCode(Mme); + + if ((OpCode & 7) < 7) + { + //Operation produces a value. + AssignmentOperation AsgOp = (AssignmentOperation)((OpCode >> 4) & 7); + + int Result = GetAluResult(); + + switch (AsgOp) + { + //Fetch parameter and ignore result. + case AssignmentOperation.IgnoreAndFetch: + { + SetDstGpr(FetchParam()); + + break; + } + + //Move result. + case AssignmentOperation.Move: + { + SetDstGpr(Result); + + break; + } + + //Move result and use as Method Address. + case AssignmentOperation.MoveAndSetMaddr: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + break; + } + + //Fetch parameter and send result. + case AssignmentOperation.FetchAndSend: + { + SetDstGpr(FetchParam()); + + Send(Vmm, Result); + + break; + } + + //Move and send result. + case AssignmentOperation.MoveAndSend: + { + SetDstGpr(Result); + + Send(Vmm, Result); + + break; + } + + //Fetch parameter and use result as Method Address. + case AssignmentOperation.FetchAndSetMaddr: + { + SetDstGpr(FetchParam()); + + SetMethAddr(Result); + + break; + } + + //Move result and use as Method Address, then fetch and send paramter. + case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + Send(Vmm, FetchParam()); + + break; + } + + //Move result and use as Method Address, then send bits 17:12 of result. + case AssignmentOperation.MoveAndSetMaddrThenSendHigh: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + Send(Vmm, (Result >> 12) & 0x3f); + + break; + } + } + } + else + { + //Branch. + bool OnNotZero = ((OpCode >> 4) & 1) != 0; + + bool Taken = OnNotZero + ? GetGprA() != 0 + : GetGprA() == 0; + + if (Taken) + { + Pc = BaseAddr + GetImm(); + + bool NoDelays = (OpCode & 0x20) != 0; + + if (NoDelays) + { + FetchOpCode(Mme); + } + + return true; + } + } + + bool Exit = (OpCode & 0x80) != 0; + + return !Exit; + } + + private void FetchOpCode(int[] Mme) + { + OpCode = PipeOp; + + PipeOp = Mme[Pc++]; + } + + private int GetAluResult() + { + AluOperation Op = (AluOperation)(OpCode & 7); + + switch (Op) + { + case AluOperation.AluReg: + { + AluRegOperation AluOp = (AluRegOperation)((OpCode >> 17) & 0x1f); + + return GetAluResult(AluOp, GetGprA(), GetGprB()); + } + + case AluOperation.AddImmediate: + { + return GetGprA() + GetImm(); + } + + case AluOperation.BitfieldReplace: + case AluOperation.BitfieldExtractLslImm: + case AluOperation.BitfieldExtractLslReg: + { + int BfSrcBit = (OpCode >> 17) & 0x1f; + int BfSize = (OpCode >> 22) & 0x1f; + int BfDstBit = (OpCode >> 27) & 0x1f; + + int BfMask = (1 << BfSize) - 1; + + int Dst = GetGprA(); + int Src = GetGprB(); + + switch (Op) + { + case AluOperation.BitfieldReplace: + { + Src = (int)((uint)Src >> BfSrcBit) & BfMask; + + Dst &= ~(BfMask << BfDstBit); + + Dst |= Src << BfDstBit; + + return Dst; + } + + case AluOperation.BitfieldExtractLslImm: + { + Src = (int)((uint)Src >> Dst) & BfMask; + + return Src << BfDstBit; + } + + case AluOperation.BitfieldExtractLslReg: + { + Src = (int)((uint)Src >> BfSrcBit) & BfMask; + + return Src << Dst; + } + } + + break; + } + + case AluOperation.ReadImmediate: + { + return Read(GetGprA() + GetImm()); + } + } + + throw new ArgumentException(nameof(OpCode)); + } + + private int GetAluResult(AluRegOperation AluOp, int A, int B) + { + switch (AluOp) + { + case AluRegOperation.Add: + { + ulong Result = (ulong)A + (ulong)B; + + Carry = Result > 0xffffffff; + + return (int)Result; + } + + case AluRegOperation.AddWithCarry: + { + ulong Result = (ulong)A + (ulong)B + (Carry ? 1UL : 0UL); + + Carry = Result > 0xffffffff; + + return (int)Result; + } + + case AluRegOperation.Subtract: + { + ulong Result = (ulong)A - (ulong)B; + + Carry = Result < 0x100000000; + + return (int)Result; + } + + case AluRegOperation.SubtractWithBorrow: + { + ulong Result = (ulong)A - (ulong)B - (Carry ? 0UL : 1UL); + + Carry = Result < 0x100000000; + + return (int)Result; + } + + case AluRegOperation.BitwiseExclusiveOr: return A ^ B; + case AluRegOperation.BitwiseOr: return A | B; + case AluRegOperation.BitwiseAnd: return A & B; + case AluRegOperation.BitwiseAndNot: return A & ~B; + case AluRegOperation.BitwiseNotAnd: return ~(A & B); + } + + throw new ArgumentOutOfRangeException(nameof(AluOp)); + } + + private int GetImm() + { + //Note: The immediate is signed, the sign-extension is intended here. + return OpCode >> 14; + } + + private void SetMethAddr(int Value) + { + MethAddr = (Value >> 0) & 0xfff; + MethIncr = (Value >> 12) & 0x3f; + } + + private void SetDstGpr(int Value) + { + Gprs[(OpCode >> 8) & 7] = Value; + } + + private int GetGprA() + { + return GetGprValue((OpCode >> 11) & 7); + } + + private int GetGprB() + { + return GetGprValue((OpCode >> 14) & 7); + } + + private int GetGprValue(int Index) + { + return Index != 0 ? Gprs[Index] : 0; + } + + private int FetchParam() + { + int Value; + + //If we don't have any parameters in the FIFO, + //keep running the PFIFO engine until it writes the parameters. + while (!Fifo.TryDequeue(out Value)) + { + if (!PFifo.Step()) + { + return 0; + } + } + + return Value; + } + + private int Read(int Reg) + { + return Engine.Registers[Reg]; + } + + private void Send(NvGpuVmm Vmm, int Value) + { + //This is an artificial limit that prevents excessive calls + //to VertexEndGl since that triggers rendering, and in the + //case that something is bugged and causes an absurd amount of + //draw calls, this prevents the system from freezing (and throws instead). + if (MethAddr == 0x585 && ++CallCount > MaxCallCountPerRun) + { + GpuExceptionHelper.ThrowCallCoundExceeded(); + } + + NvGpuPBEntry PBEntry = new NvGpuPBEntry(MethAddr, 0, Value); + + Engine.CallMethod(Vmm, PBEntry); + + MethAddr += MethIncr; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs new file mode 100644 index 00000000..f9d6342c --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.HLE.Gpu.Engines +{ + enum NvGpuEngine + { + _2d = 0x902d, + _3d = 0xb197, + Compute = 0xb1c0, + Kepler = 0xa140, + Dma = 0xb0b5 + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs new file mode 100644 index 00000000..f150b3f5 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs @@ -0,0 +1,170 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.HLE.Gpu.Texture; +using System.Collections.Generic; + +namespace Ryujinx.HLE.Gpu.Engines +{ + class NvGpuEngine2d : INvGpuEngine + { + private enum CopyOperation + { + SrcCopyAnd, + RopAnd, + Blend, + SrcCopy, + Rop, + SrcCopyPremult, + BlendPremult + } + + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + public NvGpuEngine2d(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0xe00]; + + Methods = new Dictionary(); + + void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + { + while (Count-- > 0) + { + Methods.Add(Meth, Method); + + Meth += Stride; + } + } + + AddMethod(0xb5, 1, 1, TextureCopy); + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); + + bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; + int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); + int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); + + bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; + int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); + int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); + int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); + int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); + + TextureSwizzle DstSwizzle = DstLinear + ? TextureSwizzle.Pitch + : TextureSwizzle.BlockLinear; + + int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + + long Key = Vmm.GetPhysicalAddress(MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress)); + + long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); + long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); + + bool IsFbTexture = Gpu.Engine3d.IsFrameBufferPosition(Key); + + if (IsFbTexture && DstLinear) + { + DstSwizzle = TextureSwizzle.BlockLinear; + } + + TextureInfo DstTexture = new TextureInfo( + DstAddress, + DstWidth, + DstHeight, + DstBlockHeight, + DstBlockHeight, + DstSwizzle, + GalTextureFormat.A8B8G8R8); + + if (IsFbTexture) + { + //TODO: Change this when the correct frame buffer resolution is used. + //Currently, the frame buffer size is hardcoded to 1280x720. + SrcWidth = 1280; + SrcHeight = 720; + + Gpu.Renderer.FrameBuffer.GetBufferData(Key, (byte[] Buffer) => + { + CopyTexture( + Vmm, + DstTexture, + Buffer, + SrcWidth, + SrcHeight); + }); + } + else + { + long Size = SrcWidth * SrcHeight * 4; + + byte[] Buffer = Vmm.ReadBytes(SrcAddress, Size); + + CopyTexture( + Vmm, + DstTexture, + Buffer, + SrcWidth, + SrcHeight); + } + } + + private void CopyTexture( + NvGpuVmm Vmm, + TextureInfo Texture, + byte[] Buffer, + int Width, + int Height) + { + TextureWriter.Write(Vmm, Texture, Buffer, Width, Height); + } + + private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngine2dReg Reg) + { + return Registers[(int)Reg]; + } + + private void WriteRegister(NvGpuEngine2dReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs new file mode 100644 index 00000000..29d66d46 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs @@ -0,0 +1,25 @@ +namespace Ryujinx.HLE.Gpu.Engines +{ + enum NvGpuEngine2dReg + { + DstFormat = 0x80, + DstLinear = 0x81, + DstBlockDimensions = 0x82, + DstDepth = 0x83, + DstLayer = 0x84, + DstPitch = 0x85, + DstWidth = 0x86, + DstHeight = 0x87, + DstAddress = 0x88, + SrcFormat = 0x8c, + SrcLinear = 0x8d, + SrcBlockDimensions = 0x8e, + SrcDepth = 0x8f, + SrcLayer = 0x90, + SrcPitch = 0x91, + SrcWidth = 0x92, + SrcHeight = 0x93, + SrcAddress = 0x94, + CopyOperation = 0xab + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs new file mode 100644 index 00000000..02461d5d --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs @@ -0,0 +1,556 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.HLE.Gpu.Texture; +using System; +using System.Collections.Generic; + +namespace Ryujinx.HLE.Gpu.Engines +{ + class NvGpuEngine3d : INvGpuEngine + { + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + private struct ConstBuffer + { + public bool Enabled; + public long Position; + public int Size; + } + + private ConstBuffer[][] ConstBuffers; + + private HashSet FrameBuffers; + + public NvGpuEngine3d(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0xe00]; + + Methods = new Dictionary(); + + 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); + AddMethod(0x904, 5, 8, CbBind); + + ConstBuffers = new ConstBuffer[6][]; + + for (int Index = 0; Index < ConstBuffers.Length; Index++) + { + ConstBuffers[Index] = new ConstBuffer[18]; + } + + FrameBuffers = new HashSet(); + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + SetFrameBuffer(Vmm, 0); + + long[] Keys = UploadShaders(Vmm); + + Gpu.Renderer.Shader.BindProgram(); + + SetAlphaBlending(); + + UploadTextures(Vmm, Keys); + UploadUniforms(Vmm); + UploadVertexArrays(Vmm); + } + + private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Arg0 = PBEntry.Arguments[0]; + + int FbIndex = (Arg0 >> 6) & 0xf; + + int Layer = (Arg0 >> 10) & 0x3ff; + + GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f); + + SetFrameBuffer(Vmm, 0); + + //TODO: Enable this once the frame buffer problems are fixed. + //Gpu.Renderer.ClearBuffers(Layer, Flags); + } + + private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex) + { + long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10); + + long PA = Vmm.GetPhysicalAddress(VA); + + FrameBuffers.Add(PA); + + int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); + int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); + + //Note: Using the Width/Height results seems to give incorrect results. + //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely. + Gpu.Renderer.FrameBuffer.Create(PA, 1280, 720); + Gpu.Renderer.FrameBuffer.Bind(PA); + } + + private long[] UploadShaders(NvGpuVmm Vmm) + { + long[] Keys = new long[5]; + + long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + + for (int Index = 0; Index < 6; Index++) + { + 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) + { + continue; + } + + long Key = BasePosition + (uint)Offset; + + GalShaderType ShaderType = GetTypeFromProgram(Index); + + Keys[(int)ShaderType] = Key; + + Gpu.Renderer.Shader.Create(Vmm, Key, ShaderType); + Gpu.Renderer.Shader.Bind(Key); + } + + int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX); + int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY); + + float SX = BitConverter.Int32BitsToSingle(RawSX); + float SY = BitConverter.Int32BitsToSingle(RawSY); + + float SignX = MathF.Sign(SX); + float SignY = MathF.Sign(SY); + + Gpu.Renderer.Shader.SetFlip(SignX, SignY); + + return Keys; + } + + 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)); + } + + private void SetAlphaBlending() + { + //TODO: Support independent blend properly. + bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0; + + if (Enable) + { + Gpu.Renderer.Blend.Enable(); + } + else + { + Gpu.Renderer.Blend.Disable(); + } + + if (!Enable) + { + //If blend is not enabled, then the other values have no effect. + //Note that if it is disabled, the register may contain invalid values. + return; + } + + bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0; + + GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb); + + GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb); + GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb); + + if (BlendSeparateAlpha) + { + GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha); + + GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha); + GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha); + + Gpu.Renderer.Blend.SetSeparate( + EquationRgb, + EquationAlpha, + FuncSrcRgb, + FuncDstRgb, + FuncSrcAlpha, + FuncDstAlpha); + } + else + { + Gpu.Renderer.Blend.Set(EquationRgb, FuncSrcRgb, FuncDstRgb); + } + } + + private void UploadTextures(NvGpuVmm Vmm, long[] Keys) + { + long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + + int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); + + //Note: On the emulator renderer, Texture Unit 0 is + //reserved for drawing the frame buffer. + int TexIndex = 1; + + for (int Index = 0; Index < Keys.Length; Index++) + { + foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index])) + { + long Position = ConstBuffers[Index][TextureCbIndex].Position; + + UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index); + + Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex); + + TexIndex++; + } + } + } + + private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex) + { + long Position = BasePosition + HndIndex * 4; + + int TextureHandle = Vmm.ReadInt32(Position); + + if (TextureHandle == 0) + { + //TODO: Is this correct? + //Some games like puyo puyo will have 0 handles. + //It may be just normal behaviour or a bug caused by sync issues. + //The game does initialize the value properly after through. + return; + } + + int TicIndex = (TextureHandle >> 0) & 0xfffff; + int TscIndex = (TextureHandle >> 20) & 0xfff; + + long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); + long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); + + TicPosition += TicIndex * 0x20; + TscPosition += TscIndex * 0x20; + + GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); + + long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; + + long Key = TextureAddress; + + TextureAddress = Vmm.GetPhysicalAddress(TextureAddress); + + if (IsFrameBufferPosition(TextureAddress)) + { + //This texture is a frame buffer texture, + //we shouldn't read anything from memory and bind + //the frame buffer texture instead, since we're not + //really writing anything to memory. + Gpu.Renderer.FrameBuffer.BindTexture(TextureAddress, TexIndex); + } + else + { + GalTexture NewTexture = TextureFactory.MakeTexture(Vmm, TicPosition); + + long Size = (uint)TextureHelper.GetTextureSize(NewTexture); + + bool HasCachedTexture = false; + + if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalTexture Texture)) + { + if (NewTexture.Equals(Texture) && !Vmm.IsRegionModified(Key, Size, NvGpuBufferType.Texture)) + { + Gpu.Renderer.Texture.Bind(Key, TexIndex); + + HasCachedTexture = true; + } + } + + if (!HasCachedTexture) + { + byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition); + + Gpu.Renderer.Texture.Create(Key, Data, NewTexture); + } + + Gpu.Renderer.Texture.Bind(Key, TexIndex); + } + + Gpu.Renderer.Texture.SetSampler(Sampler); + } + + private void UploadUniforms(NvGpuVmm Vmm) + { + long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + + for (int Index = 0; Index < 5; Index++) + { + int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10); + int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10); + + //Note: Vertex Program (B) is always enabled. + bool Enable = (Control & 1) != 0 || Index == 0; + + if (!Enable) + { + continue; + } + + for (int Cbuf = 0; Cbuf < ConstBuffers[Index].Length; Cbuf++) + { + ConstBuffer Cb = ConstBuffers[Index][Cbuf]; + + if (Cb.Enabled) + { + byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size); + + Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data); + } + } + } + } + + private void UploadVertexArrays(NvGpuVmm Vmm) + { + long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); + + int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); + int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); + int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); + + GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt; + + int IndexEntrySize = 1 << IndexEntryFmt; + + if (IndexEntrySize > 4) + { + throw new InvalidOperationException(); + } + + if (IndexCount != 0) + { + int IbSize = IndexCount * IndexEntrySize; + + bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IndexPosition, (uint)IbSize); + + if (!IboCached || Vmm.IsRegionModified(IndexPosition, (uint)IbSize, NvGpuBufferType.Index)) + { + byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize); + + Gpu.Renderer.Rasterizer.CreateIbo(IndexPosition, Data); + } + + Gpu.Renderer.Rasterizer.SetIndexArray(IndexPosition, IbSize, IndexFormat); + } + + List[] Attribs = new List[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(); + } + + Attribs[ArrayIndex].Add(new GalVertexAttrib( + Attr, + ((Packed >> 6) & 0x1) != 0, + (Packed >> 7) & 0x3fff, + (GalVertexAttribSize)((Packed >> 21) & 0x3f), + (GalVertexAttribType)((Packed >> 27) & 0x7), + ((Packed >> 31) & 0x1) != 0)); + } + + int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); + int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); + + int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); + + for (int Index = 0; Index < 32; Index++) + { + if (Attribs[Index] == null) + { + continue; + } + + int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); + + bool Enable = (Control & 0x1000) != 0; + + long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); + long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); + + if (!Enable) + { + continue; + } + + int Stride = Control & 0xfff; + + long VbSize = 0; + + if (IndexCount != 0) + { + VbSize = (VertexEndPos - VertexPosition) + 1; + } + else + { + VbSize = VertexCount * Stride; + } + + bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VertexPosition, VbSize); + + if (!VboCached || Vmm.IsRegionModified(VertexPosition, VbSize, NvGpuBufferType.Vertex)) + { + byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize); + + Gpu.Renderer.Rasterizer.CreateVbo(VertexPosition, Data); + } + + Gpu.Renderer.Rasterizer.SetVertexArray(Index, Stride, VertexPosition, Attribs[Index].ToArray()); + } + + GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); + + if (IndexCount != 0) + { + Gpu.Renderer.Rasterizer.DrawElements(IndexPosition, IndexFirst, PrimType); + } + else + { + Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); + } + } + + private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); + + int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; + int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; + + int Mode = Ctrl & 3; + + if (Mode == 0) + { + //Write mode. + Vmm.WriteInt32(Position, Seq); + } + + WriteRegister(PBEntry); + } + + private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + + int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); + + foreach (int Arg in PBEntry.Arguments) + { + Vmm.WriteInt32(Position + Offset, Arg); + + Offset += 4; + } + + WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset); + } + + private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Stage = (PBEntry.Method - 0x904) >> 3; + + int Index = PBEntry.Arguments[0]; + + bool Enabled = (Index & 1) != 0; + + Index = (Index >> 4) & 0x1f; + + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + + ConstBuffers[Stage][Index].Position = Position; + ConstBuffers[Stage][Index].Enabled = Enabled; + + ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); + } + + private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngine3dReg Reg) + { + return Registers[(int)Reg]; + } + + private void WriteRegister(NvGpuEngine3dReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + + public bool IsFrameBufferPosition(long Position) + { + return FrameBuffers.Contains(Position); + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs new file mode 100644 index 00000000..de2b2eef --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs @@ -0,0 +1,61 @@ +namespace Ryujinx.HLE.Gpu.Engines +{ + enum NvGpuEngine3dReg + { + FrameBufferNAddress = 0x200, + FrameBufferNWidth = 0x202, + FrameBufferNHeight = 0x203, + FrameBufferNFormat = 0x204, + ViewportScaleX = 0x280, + ViewportScaleY = 0x281, + ViewportScaleZ = 0x282, + ViewportTranslateX = 0x283, + ViewportTranslateY = 0x284, + ViewportTranslateZ = 0x285, + VertexArrayFirst = 0x35d, + VertexArrayCount = 0x35e, + VertexAttribNFormat = 0x458, + IBlendEnable = 0x4b9, + BlendSeparateAlpha = 0x4cf, + BlendEquationRgb = 0x4d0, + BlendFuncSrcRgb = 0x4d1, + BlendFuncDstRgb = 0x4d2, + BlendEquationAlpha = 0x4d3, + BlendFuncSrcAlpha = 0x4d4, + BlendFuncDstAlpha = 0x4d6, + BlendEnableMaster = 0x4d7, + IBlendNEnable = 0x4d8, + VertexArrayElemBase = 0x50d, + TexHeaderPoolOffset = 0x55d, + TexSamplerPoolOffset = 0x557, + ShaderAddress = 0x582, + VertexBeginGl = 0x586, + IndexArrayAddress = 0x5f2, + IndexArrayEndAddr = 0x5f4, + IndexArrayFormat = 0x5f6, + IndexBatchFirst = 0x5f7, + IndexBatchCount = 0x5f8, + QueryAddress = 0x6c0, + QuerySequence = 0x6c2, + QueryControl = 0x6c3, + VertexArrayNControl = 0x700, + VertexArrayNAddress = 0x701, + VertexArrayNDivisor = 0x703, + IBlendNSeparateAlpha = 0x780, + IBlendNEquationRgb = 0x781, + IBlendNFuncSrcRgb = 0x782, + IBlendNFuncDstRgb = 0x783, + IBlendNEquationAlpha = 0x784, + IBlendNFuncSrcAlpha = 0x785, + IBlendNFuncDstAlpha = 0x786, + VertexArrayNEndAddr = 0x7c0, + ShaderNControl = 0x800, + ShaderNOffset = 0x801, + ShaderNMaxGprs = 0x803, + ShaderNType = 0x804, + ConstBufferSize = 0x8e0, + ConstBufferAddress = 0x8e1, + ConstBufferOffset = 0x8e3, + TextureCbIndex = 0x982 + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs new file mode 100644 index 00000000..7e355e8d --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs @@ -0,0 +1,143 @@ +using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.HLE.Gpu.Texture; +using System.Collections.Generic; + +namespace Ryujinx.HLE.Gpu.Engines +{ + class NvGpuEngineDma : INvGpuEngine + { + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + public NvGpuEngineDma(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0x1d6]; + + Methods = new Dictionary(); + + void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + { + while (Count-- > 0) + { + Methods.Add(Meth, Method); + + Meth += Stride; + } + } + + AddMethod(0xc0, 1, 1, Execute); + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + private void Execute(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Control = PBEntry.Arguments[0]; + + bool SrcLinear = ((Control >> 7) & 1) != 0; + bool DstLinear = ((Control >> 8) & 1) != 0; + + long SrcAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.SrcAddress); + long DstAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.DstAddress); + + int SrcPitch = ReadRegister(NvGpuEngineDmaReg.SrcPitch); + int DstPitch = ReadRegister(NvGpuEngineDmaReg.DstPitch); + + int DstBlkDim = ReadRegister(NvGpuEngineDmaReg.DstBlkDim); + int DstSizeX = ReadRegister(NvGpuEngineDmaReg.DstSizeX); + int DstSizeY = ReadRegister(NvGpuEngineDmaReg.DstSizeY); + int DstSizeZ = ReadRegister(NvGpuEngineDmaReg.DstSizeZ); + int DstPosXY = ReadRegister(NvGpuEngineDmaReg.DstPosXY); + int DstPosZ = ReadRegister(NvGpuEngineDmaReg.DstPosZ); + + int SrcBlkDim = ReadRegister(NvGpuEngineDmaReg.SrcBlkDim); + int SrcSizeX = ReadRegister(NvGpuEngineDmaReg.SrcSizeX); + int SrcSizeY = ReadRegister(NvGpuEngineDmaReg.SrcSizeY); + int SrcSizeZ = ReadRegister(NvGpuEngineDmaReg.SrcSizeZ); + int SrcPosXY = ReadRegister(NvGpuEngineDmaReg.SrcPosXY); + int SrcPosZ = ReadRegister(NvGpuEngineDmaReg.SrcPosZ); + + int DstPosX = (DstPosXY >> 0) & 0xffff; + int DstPosY = (DstPosXY >> 16) & 0xffff; + + int SrcPosX = (SrcPosXY >> 0) & 0xffff; + int SrcPosY = (SrcPosXY >> 16) & 0xffff; + + int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); + int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + + ISwizzle SrcSwizzle; + + if (SrcLinear) + { + SrcSwizzle = new LinearSwizzle(SrcPitch, 1); + } + else + { + SrcSwizzle = new BlockLinearSwizzle(SrcSizeX, 1, SrcBlockHeight); + } + + ISwizzle DstSwizzle; + + if (DstLinear) + { + DstSwizzle = new LinearSwizzle(DstPitch, 1); + } + else + { + DstSwizzle = new BlockLinearSwizzle(DstSizeX, 1, DstBlockHeight); + } + + for (int Y = 0; Y < DstSizeY; Y++) + for (int X = 0; X < DstSizeX; X++) + { + long SrcOffset = SrcAddress + (uint)SrcSwizzle.GetSwizzleOffset(X, Y); + long DstOffset = DstAddress + (uint)DstSwizzle.GetSwizzleOffset(X, Y); + + Vmm.WriteByte(DstOffset, Vmm.ReadByte(SrcOffset)); + } + } + + private long MakeInt64From2xInt32(NvGpuEngineDmaReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngineDmaReg Reg) + { + return Registers[(int)Reg]; + } + + private void WriteRegister(NvGpuEngineDmaReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs new file mode 100644 index 00000000..835a822d --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs @@ -0,0 +1,22 @@ +namespace Ryujinx.HLE.Gpu.Engines +{ + enum NvGpuEngineDmaReg + { + SrcAddress = 0x100, + DstAddress = 0x102, + SrcPitch = 0x104, + DstPitch = 0x105, + DstBlkDim = 0x1c3, + DstSizeX = 0x1c4, + DstSizeY = 0x1c5, + DstSizeZ = 0x1c6, + DstPosZ = 0x1c7, + DstPosXY = 0x1c8, + SrcBlkDim = 0x1ca, + SrcSizeX = 0x1cb, + SrcSizeY = 0x1cc, + SrcSizeZ = 0x1cd, + SrcPosZ = 0x1ce, + SrcPosXY = 0x1cf + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs new file mode 100644 index 00000000..0bc682a7 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs @@ -0,0 +1,183 @@ +using Ryujinx.HLE.Gpu.Memory; +using System.Collections.Concurrent; + +namespace Ryujinx.HLE.Gpu.Engines +{ + class NvGpuFifo + { + private const int MacrosCount = 0x80; + private const int MacroIndexMask = MacrosCount - 1; + + //Note: The size of the macro memory is unknown, we just make + //a guess here and use 256kb as the size. Increase if needed. + private const int MmeWords = 256 * 256; + + private NvGpu Gpu; + + private ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry)> BufferQueue; + + private NvGpuEngine[] SubChannels; + + private struct CachedMacro + { + public int Position { get; private set; } + + private MacroInterpreter Interpreter; + + public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position) + { + this.Position = Position; + + Interpreter = new MacroInterpreter(PFifo, Engine); + } + + public void PushParam(int Param) + { + Interpreter?.Fifo.Enqueue(Param); + } + + public void Execute(NvGpuVmm Vmm, int[] Mme, int Param) + { + Interpreter?.Execute(Vmm, Mme, Position, Param); + } + } + + private int CurrMacroPosition; + private int CurrMacroBindIndex; + + private CachedMacro[] Macros; + + private int[] Mme; + + public NvGpuFifo(NvGpu Gpu) + { + this.Gpu = Gpu; + + BufferQueue = new ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry)>(); + + SubChannels = new NvGpuEngine[8]; + + Macros = new CachedMacro[MacrosCount]; + + Mme = new int[MmeWords]; + } + + public void PushBuffer(NvGpuVmm Vmm, NvGpuPBEntry[] Buffer) + { + foreach (NvGpuPBEntry PBEntry in Buffer) + { + BufferQueue.Enqueue((Vmm, PBEntry)); + } + } + + public void DispatchCalls() + { + while (Step()); + } + + public bool Step() + { + if (BufferQueue.TryDequeue(out (NvGpuVmm Vmm, NvGpuPBEntry PBEntry) Tuple)) + { + CallMethod(Tuple.Vmm, Tuple.PBEntry); + + return true; + } + + return false; + } + + private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (PBEntry.Method < 0x80) + { + switch ((NvGpuFifoMeth)PBEntry.Method) + { + case NvGpuFifoMeth.BindChannel: + { + NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0]; + + SubChannels[PBEntry.SubChannel] = Engine; + + break; + } + + case NvGpuFifoMeth.SetMacroUploadAddress: + { + CurrMacroPosition = PBEntry.Arguments[0]; + + break; + } + + case NvGpuFifoMeth.SendMacroCodeData: + { + foreach (int Arg in PBEntry.Arguments) + { + Mme[CurrMacroPosition++] = Arg; + } + break; + } + + case NvGpuFifoMeth.SetMacroBindingIndex: + { + CurrMacroBindIndex = PBEntry.Arguments[0]; + + break; + } + + case NvGpuFifoMeth.BindMacro: + { + int Position = PBEntry.Arguments[0]; + + Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position); + + break; + } + } + } + else + { + switch (SubChannels[PBEntry.SubChannel]) + { + case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break; + case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break; + case NvGpuEngine.Dma: CallDmaMethod(Vmm, PBEntry); break; + } + } + } + + private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + Gpu.Engine2d.CallMethod(Vmm, PBEntry); + } + + private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (PBEntry.Method < 0xe00) + { + Gpu.Engine3d.CallMethod(Vmm, PBEntry); + } + else + { + int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask; + + if ((PBEntry.Method & 1) != 0) + { + foreach (int Arg in PBEntry.Arguments) + { + Macros[MacroIndex].PushParam(Arg); + } + } + else + { + Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]); + } + } + } + + private void CallDmaMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + Gpu.EngineDma.CallMethod(Vmm, PBEntry); + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs new file mode 100644 index 00000000..ffd179f2 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.HLE.Gpu.Engines +{ + enum NvGpuFifoMeth + { + BindChannel = 0, + SetMacroUploadAddress = 0x45, + SendMacroCodeData = 0x46, + SetMacroBindingIndex = 0x47, + BindMacro = 0x48 + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs new file mode 100644 index 00000000..04c92f2a --- /dev/null +++ b/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs @@ -0,0 +1,6 @@ +using Ryujinx.HLE.Gpu.Memory; + +namespace Ryujinx.HLE.Gpu.Engines +{ + delegate void NvGpuMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Exceptions/GpuException.cs b/Ryujinx.HLE/Gpu/Exceptions/GpuException.cs new file mode 100644 index 00000000..c0bce5a5 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Exceptions/GpuException.cs @@ -0,0 +1,11 @@ +using System; + +namespace Ryujinx.HLE.Gpu.Exceptions +{ + class GpuException : Exception + { + public GpuException() : base() { } + + public GpuException(string ExMsg) : base(ExMsg) { } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Exceptions/GpuExceptionHelper.cs b/Ryujinx.HLE/Gpu/Exceptions/GpuExceptionHelper.cs new file mode 100644 index 00000000..aeab9a29 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Exceptions/GpuExceptionHelper.cs @@ -0,0 +1,12 @@ +namespace Ryujinx.HLE.Gpu.Exceptions +{ + static class GpuExceptionHelper + { + private const string CallCountExceeded = "Method call count exceeded the limit allowed per run!"; + + public static void ThrowCallCoundExceeded() + { + throw new GpuException(CallCountExceeded); + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/INvGpuEngine.cs b/Ryujinx.HLE/Gpu/INvGpuEngine.cs deleted file mode 100644 index 62307f58..00000000 --- a/Ryujinx.HLE/Gpu/INvGpuEngine.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - interface INvGpuEngine - { - int[] Registers { get; } - - void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/ISwizzle.cs b/Ryujinx.HLE/Gpu/ISwizzle.cs deleted file mode 100644 index 525707ba..00000000 --- a/Ryujinx.HLE/Gpu/ISwizzle.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - interface ISwizzle - { - int GetSwizzleOffset(int X, int Y); - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/LinearSwizzle.cs b/Ryujinx.HLE/Gpu/LinearSwizzle.cs deleted file mode 100644 index 995866ad..00000000 --- a/Ryujinx.HLE/Gpu/LinearSwizzle.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - class LinearSwizzle : ISwizzle - { - private int Pitch; - private int Bpp; - - public LinearSwizzle(int Pitch, int Bpp) - { - this.Pitch = Pitch; - this.Bpp = Bpp; - } - - public int GetSwizzleOffset(int X, int Y) - { - return X * Bpp + Y * Pitch; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/MacroInterpreter.cs b/Ryujinx.HLE/Gpu/MacroInterpreter.cs deleted file mode 100644 index c333046a..00000000 --- a/Ryujinx.HLE/Gpu/MacroInterpreter.cs +++ /dev/null @@ -1,417 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu -{ - class MacroInterpreter - { - private enum AssignmentOperation - { - IgnoreAndFetch = 0, - Move = 1, - MoveAndSetMaddr = 2, - FetchAndSend = 3, - MoveAndSend = 4, - FetchAndSetMaddr = 5, - MoveAndSetMaddrThenFetchAndSend = 6, - MoveAndSetMaddrThenSendHigh = 7 - } - - private enum AluOperation - { - AluReg = 0, - AddImmediate = 1, - BitfieldReplace = 2, - BitfieldExtractLslImm = 3, - BitfieldExtractLslReg = 4, - ReadImmediate = 5 - } - - private enum AluRegOperation - { - Add = 0, - AddWithCarry = 1, - Subtract = 2, - SubtractWithBorrow = 3, - BitwiseExclusiveOr = 8, - BitwiseOr = 9, - BitwiseAnd = 10, - BitwiseAndNot = 11, - BitwiseNotAnd = 12 - } - - private NvGpuFifo PFifo; - private INvGpuEngine Engine; - - public Queue Fifo { get; private set; } - - private int[] Gprs; - - private int MethAddr; - private int MethIncr; - - private bool Carry; - - private int OpCode; - - private int PipeOp; - - private int Pc; - - public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine) - { - this.PFifo = PFifo; - this.Engine = Engine; - - Fifo = new Queue(); - - Gprs = new int[8]; - } - - public void Execute(NvGpuVmm Vmm, int[] Mme, int Position, int Param) - { - Reset(); - - Gprs[1] = Param; - - Pc = Position; - - FetchOpCode(Mme); - - while (Step(Vmm, Mme)); - - //Due to the delay slot, we still need to execute - //one more instruction before we actually exit. - Step(Vmm, Mme); - } - - private void Reset() - { - for (int Index = 0; Index < Gprs.Length; Index++) - { - Gprs[Index] = 0; - } - - MethAddr = 0; - MethIncr = 0; - - Carry = false; - } - - private bool Step(NvGpuVmm Vmm, int[] Mme) - { - int BaseAddr = Pc - 1; - - FetchOpCode(Mme); - - if ((OpCode & 7) < 7) - { - //Operation produces a value. - AssignmentOperation AsgOp = (AssignmentOperation)((OpCode >> 4) & 7); - - int Result = GetAluResult(); - - switch (AsgOp) - { - //Fetch parameter and ignore result. - case AssignmentOperation.IgnoreAndFetch: - { - SetDstGpr(FetchParam()); - - break; - } - - //Move result. - case AssignmentOperation.Move: - { - SetDstGpr(Result); - - break; - } - - //Move result and use as Method Address. - case AssignmentOperation.MoveAndSetMaddr: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - break; - } - - //Fetch parameter and send result. - case AssignmentOperation.FetchAndSend: - { - SetDstGpr(FetchParam()); - - Send(Vmm, Result); - - break; - } - - //Move and send result. - case AssignmentOperation.MoveAndSend: - { - SetDstGpr(Result); - - Send(Vmm, Result); - - break; - } - - //Fetch parameter and use result as Method Address. - case AssignmentOperation.FetchAndSetMaddr: - { - SetDstGpr(FetchParam()); - - SetMethAddr(Result); - - break; - } - - //Move result and use as Method Address, then fetch and send paramter. - case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - Send(Vmm, FetchParam()); - - break; - } - - //Move result and use as Method Address, then send bits 17:12 of result. - case AssignmentOperation.MoveAndSetMaddrThenSendHigh: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - Send(Vmm, (Result >> 12) & 0x3f); - - break; - } - } - } - else - { - //Branch. - bool OnNotZero = ((OpCode >> 4) & 1) != 0; - - bool Taken = OnNotZero - ? GetGprA() != 0 - : GetGprA() == 0; - - if (Taken) - { - Pc = BaseAddr + GetImm(); - - bool NoDelays = (OpCode & 0x20) != 0; - - if (NoDelays) - { - FetchOpCode(Mme); - } - - return true; - } - } - - bool Exit = (OpCode & 0x80) != 0; - - return !Exit; - } - - private void FetchOpCode(int[] Mme) - { - OpCode = PipeOp; - - PipeOp = Mme[Pc++]; - } - - private int GetAluResult() - { - AluOperation Op = (AluOperation)(OpCode & 7); - - switch (Op) - { - case AluOperation.AluReg: - { - AluRegOperation AluOp = (AluRegOperation)((OpCode >> 17) & 0x1f); - - return GetAluResult(AluOp, GetGprA(), GetGprB()); - } - - case AluOperation.AddImmediate: - { - return GetGprA() + GetImm(); - } - - case AluOperation.BitfieldReplace: - case AluOperation.BitfieldExtractLslImm: - case AluOperation.BitfieldExtractLslReg: - { - int BfSrcBit = (OpCode >> 17) & 0x1f; - int BfSize = (OpCode >> 22) & 0x1f; - int BfDstBit = (OpCode >> 27) & 0x1f; - - int BfMask = (1 << BfSize) - 1; - - int Dst = GetGprA(); - int Src = GetGprB(); - - switch (Op) - { - case AluOperation.BitfieldReplace: - { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; - - Dst &= ~(BfMask << BfDstBit); - - Dst |= Src << BfDstBit; - - return Dst; - } - - case AluOperation.BitfieldExtractLslImm: - { - Src = (int)((uint)Src >> Dst) & BfMask; - - return Src << BfDstBit; - } - - case AluOperation.BitfieldExtractLslReg: - { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; - - return Src << Dst; - } - } - - break; - } - - case AluOperation.ReadImmediate: - { - return Read(GetGprA() + GetImm()); - } - } - - throw new ArgumentException(nameof(OpCode)); - } - - private int GetAluResult(AluRegOperation AluOp, int A, int B) - { - switch (AluOp) - { - case AluRegOperation.Add: - { - ulong Result = (ulong)A + (ulong)B; - - Carry = Result > 0xffffffff; - - return (int)Result; - } - - case AluRegOperation.AddWithCarry: - { - ulong Result = (ulong)A + (ulong)B + (Carry ? 1UL : 0UL); - - Carry = Result > 0xffffffff; - - return (int)Result; - } - - case AluRegOperation.Subtract: - { - ulong Result = (ulong)A - (ulong)B; - - Carry = Result < 0x100000000; - - return (int)Result; - } - - case AluRegOperation.SubtractWithBorrow: - { - ulong Result = (ulong)A - (ulong)B - (Carry ? 0UL : 1UL); - - Carry = Result < 0x100000000; - - return (int)Result; - } - - case AluRegOperation.BitwiseExclusiveOr: return A ^ B; - case AluRegOperation.BitwiseOr: return A | B; - case AluRegOperation.BitwiseAnd: return A & B; - case AluRegOperation.BitwiseAndNot: return A & ~B; - case AluRegOperation.BitwiseNotAnd: return ~(A & B); - } - - throw new ArgumentOutOfRangeException(nameof(AluOp)); - } - - private int GetImm() - { - //Note: The immediate is signed, the sign-extension is intended here. - return OpCode >> 14; - } - - private void SetMethAddr(int Value) - { - MethAddr = (Value >> 0) & 0xfff; - MethIncr = (Value >> 12) & 0x3f; - } - - private void SetDstGpr(int Value) - { - Gprs[(OpCode >> 8) & 7] = Value; - } - - private int GetGprA() - { - return GetGprValue((OpCode >> 11) & 7); - } - - private int GetGprB() - { - return GetGprValue((OpCode >> 14) & 7); - } - - private int GetGprValue(int Index) - { - return Index != 0 ? Gprs[Index] : 0; - } - - private int FetchParam() - { - int Value; - - //If we don't have any parameters in the FIFO, - //keep running the PFIFO engine until it writes the parameters. - while (!Fifo.TryDequeue(out Value)) - { - if (!PFifo.Step()) - { - return 0; - } - } - - return Value; - } - - private int Read(int Reg) - { - return Engine.Registers[Reg]; - } - - private void Send(NvGpuVmm Vmm, int Value) - { - NvGpuPBEntry PBEntry = new NvGpuPBEntry(MethAddr, 0, Value); - - Engine.CallMethod(Vmm, PBEntry); - - MethAddr += MethIncr; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs new file mode 100644 index 00000000..7474aa33 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs @@ -0,0 +1,9 @@ +namespace Ryujinx.HLE.Gpu.Memory +{ + enum NvGpuBufferType + { + Index, + Vertex, + Texture + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs new file mode 100644 index 00000000..aba89e3c --- /dev/null +++ b/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.ObjectModel; + +namespace Ryujinx.HLE.Gpu.Memory +{ + struct NvGpuPBEntry + { + public int Method { get; private set; } + + public int SubChannel { get; private set; } + + private int[] m_Arguments; + + public ReadOnlyCollection Arguments => Array.AsReadOnly(m_Arguments); + + public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments) + { + this.Method = Method; + this.SubChannel = SubChannel; + this.m_Arguments = Arguments; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs new file mode 100644 index 00000000..6121b3e6 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs @@ -0,0 +1,101 @@ +using System.Collections.Generic; +using System.IO; + +namespace Ryujinx.HLE.Gpu.Memory +{ + static class NvGpuPushBuffer + { + private enum SubmissionMode + { + Incrementing = 1, + NonIncrementing = 3, + Immediate = 4, + IncrementOnce = 5 + } + + public static NvGpuPBEntry[] Decode(byte[] Data) + { + using (MemoryStream MS = new MemoryStream(Data)) + { + BinaryReader Reader = new BinaryReader(MS); + + List PushBuffer = new List(); + + bool CanRead() => MS.Position + 4 <= MS.Length; + + while (CanRead()) + { + int Packed = Reader.ReadInt32(); + + int Meth = (Packed >> 0) & 0x1fff; + int SubC = (Packed >> 13) & 7; + int Args = (Packed >> 16) & 0x1fff; + int Mode = (Packed >> 29) & 7; + + switch ((SubmissionMode)Mode) + { + case SubmissionMode.Incrementing: + { + for (int Index = 0; Index < Args && CanRead(); Index++, Meth++) + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); + } + + break; + } + + case SubmissionMode.NonIncrementing: + { + int[] Arguments = new int[Args]; + + for (int Index = 0; Index < Arguments.Length; Index++) + { + if (!CanRead()) + { + break; + } + + Arguments[Index] = Reader.ReadInt32(); + } + + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments)); + + break; + } + + case SubmissionMode.Immediate: + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args)); + + break; + } + + case SubmissionMode.IncrementOnce: + { + if (CanRead()) + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); + } + + if (CanRead() && Args > 1) + { + int[] Arguments = new int[Args - 1]; + + for (int Index = 0; Index < Arguments.Length && CanRead(); Index++) + { + Arguments[Index] = Reader.ReadInt32(); + } + + PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments)); + } + + break; + } + } + } + + return PushBuffer.ToArray(); + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs new file mode 100644 index 00000000..36f6406a --- /dev/null +++ b/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs @@ -0,0 +1,410 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System.Collections.Concurrent; + +namespace Ryujinx.HLE.Gpu.Memory +{ + class NvGpuVmm : IAMemory, IGalMemory + { + public const long AddrSize = 1L << 40; + + private const int PTLvl0Bits = 14; + private const int PTLvl1Bits = 14; + private const int PTPageBits = 12; + + private const int PTLvl0Size = 1 << PTLvl0Bits; + private const int PTLvl1Size = 1 << PTLvl1Bits; + public const int PageSize = 1 << PTPageBits; + + private const int PTLvl0Mask = PTLvl0Size - 1; + private const int PTLvl1Mask = PTLvl1Size - 1; + public const int PageMask = PageSize - 1; + + private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; + private const int PTLvl1Bit = PTPageBits; + + public AMemory Memory { get; private set; } + + private struct MappedMemory + { + public long Size; + + public MappedMemory(long Size) + { + this.Size = Size; + } + } + + private ConcurrentDictionary Maps; + + private NvGpuVmmCache Cache; + + private const long PteUnmapped = -1; + private const long PteReserved = -2; + + private long[][] PageTable; + + public NvGpuVmm(AMemory Memory) + { + this.Memory = Memory; + + Maps = new ConcurrentDictionary(); + + Cache = new NvGpuVmmCache(); + + PageTable = new long[PTLvl0Size][]; + } + + public long Map(long PA, long VA, long Size) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + if (GetPte(VA + Offset) != PteReserved) + { + return Map(PA, Size); + } + } + + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PA + Offset); + } + } + + return VA; + } + + public long Map(long PA, long Size) + { + lock (PageTable) + { + long VA = GetFreePosition(Size); + + if (VA != -1) + { + MappedMemory Map = new MappedMemory(Size); + + Maps.AddOrUpdate(VA, Map, (Key, Old) => Map); + + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PA + Offset); + } + } + + return VA; + } + } + + public bool Unmap(long VA) + { + if (Maps.TryRemove(VA, out MappedMemory Map)) + { + Free(VA, Map.Size); + + return true; + } + + return false; + } + + public long Reserve(long VA, long Size, long Align) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + if (IsPageInUse(VA + Offset)) + { + return Reserve(Size, Align); + } + } + + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PteReserved); + } + } + + return VA; + } + + public long Reserve(long Size, long Align) + { + lock (PageTable) + { + long Position = GetFreePosition(Size, Align); + + if (Position != -1) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(Position + Offset, PteReserved); + } + } + + return Position; + } + } + + public void Free(long VA, long Size) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PteUnmapped); + } + } + } + + private long GetFreePosition(long Size, long Align = 1) + { + long Position = 0; + long FreeSize = 0; + + if (Align < 1) + { + Align = 1; + } + + Align = (Align + PageMask) & ~PageMask; + + while (Position + FreeSize < AddrSize) + { + if (!IsPageInUse(Position + FreeSize)) + { + FreeSize += PageSize; + + if (FreeSize >= Size) + { + return Position; + } + } + else + { + Position += FreeSize + PageSize; + FreeSize = 0; + + long Remainder = Position % Align; + + if (Remainder != 0) + { + Position = (Position - Remainder) + Align; + } + } + } + + return -1; + } + + public long GetPhysicalAddress(long VA) + { + long BasePos = GetPte(VA); + + if (BasePos < 0) + { + return -1; + } + + return BasePos + (VA & PageMask); + } + + public bool IsRegionFree(long VA, long Size) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + if (IsPageInUse(VA + Offset)) + { + return false; + } + } + + return true; + } + + private bool IsPageInUse(long VA) + { + if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0) + { + return false; + } + + long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + return false; + } + + return PageTable[L0][L1] != PteUnmapped; + } + + private long GetPte(long Position) + { + long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + return -1; + } + + return PageTable[L0][L1]; + } + + private void SetPte(long Position, long TgtAddr) + { + long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + PageTable[L0] = new long[PTLvl1Size]; + + for (int Index = 0; Index < PTLvl1Size; Index++) + { + PageTable[L0][Index] = PteUnmapped; + } + } + + PageTable[L0][L1] = TgtAddr; + } + + public bool IsRegionModified(long Position, long Size, NvGpuBufferType BufferType) + { + long PA = GetPhysicalAddress(Position); + + return Cache.IsRegionModified(Memory, BufferType, Position, PA, Size); + } + + public byte ReadByte(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadByte(Position); + } + + public ushort ReadUInt16(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt16(Position); + } + + public uint ReadUInt32(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt32(Position); + } + + public ulong ReadUInt64(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt64(Position); + } + + public sbyte ReadSByte(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadSByte(Position); + } + + public short ReadInt16(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt16(Position); + } + + public int ReadInt32(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt32(Position); + } + + public long ReadInt64(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt64(Position); + } + + public byte[] ReadBytes(long Position, long Size) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadBytes(Position, Size); + } + + public void WriteByte(long Position, byte Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteByte(Position, Value); + } + + public void WriteUInt16(long Position, ushort Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt16(Position, Value); + } + + public void WriteUInt32(long Position, uint Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt32(Position, Value); + } + + public void WriteUInt64(long Position, ulong Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt64(Position, Value); + } + + public void WriteSByte(long Position, sbyte Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteSByte(Position, Value); + } + + public void WriteInt16(long Position, short Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt16(Position, Value); + } + + public void WriteInt32(long Position, int Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt32(Position, Value); + } + + public void WriteInt64(long Position, long Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt64(Position, Value); + } + + public void WriteBytes(long Position, byte[] Data) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteBytes(Position, Data); + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs new file mode 100644 index 00000000..c7108f00 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs @@ -0,0 +1,209 @@ +using ChocolArm64.Memory; +using System; +using System.Collections.Generic; + +namespace Ryujinx.HLE.Gpu.Memory +{ + class NvGpuVmmCache + { + private const int MaxCpCount = 10000; + private const int MaxCpTimeDelta = 60000; + + private class CachedPage + { + private List<(long Start, long End)> Regions; + + public LinkedListNode Node { get; set; } + + public int Count => Regions.Count; + + public int Timestamp { get; private set; } + + public long PABase { get; private set; } + + public NvGpuBufferType BufferType { get; private set; } + + public CachedPage(long PABase, NvGpuBufferType BufferType) + { + this.PABase = PABase; + this.BufferType = BufferType; + + Regions = new List<(long, long)>(); + } + + public bool AddRange(long Start, long End) + { + for (int Index = 0; Index < Regions.Count; Index++) + { + (long RgStart, long RgEnd) = Regions[Index]; + + if (Start >= RgStart && End <= RgEnd) + { + return false; + } + + if (Start <= RgEnd && RgStart <= End) + { + long MinStart = Math.Min(RgStart, Start); + long MaxEnd = Math.Max(RgEnd, End); + + Regions[Index] = (MinStart, MaxEnd); + + Timestamp = Environment.TickCount; + + return true; + } + } + + Regions.Add((Start, End)); + + Timestamp = Environment.TickCount; + + return true; + } + } + + private Dictionary Cache; + + private LinkedList SortedCache; + + private int CpCount; + + public NvGpuVmmCache() + { + Cache = new Dictionary(); + + SortedCache = new LinkedList(); + } + + public bool IsRegionModified( + AMemory Memory, + NvGpuBufferType BufferType, + long VA, + long PA, + long Size) + { + ClearCachedPagesIfNeeded(); + + long PageSize = Memory.GetHostPageSize(); + + long Mask = PageSize - 1; + + long VAEnd = VA + Size; + long PAEnd = PA + Size; + + bool RegMod = false; + + while (VA < VAEnd) + { + long Key = VA & ~Mask; + long PABase = PA & ~Mask; + + long VAPgEnd = Math.Min((VA + PageSize) & ~Mask, VAEnd); + long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd); + + bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp); + + bool PgReset = false; + + if (!IsCached) + { + Cp = new CachedPage(PABase, BufferType); + + Cache.Add(Key, Cp); + } + else + { + CpCount -= Cp.Count; + + SortedCache.Remove(Cp.Node); + + if (Cp.PABase != PABase || + Cp.BufferType != BufferType) + { + PgReset = true; + } + } + + PgReset |= Memory.IsRegionModified(PA, PAPgEnd - PA) && IsCached; + + if (PgReset) + { + Cp = new CachedPage(PABase, BufferType); + + Cache[Key] = Cp; + } + + Cp.Node = SortedCache.AddLast(Key); + + RegMod |= Cp.AddRange(VA, VAPgEnd); + + CpCount += Cp.Count; + + VA = VAPgEnd; + PA = PAPgEnd; + } + + return RegMod; + } + + private void ClearCachedPagesIfNeeded() + { + if (CpCount <= MaxCpCount) + { + return; + } + + int Timestamp = Environment.TickCount; + + int TimeDelta; + + do + { + if (!TryPopOldestCachedPageKey(Timestamp, out long Key)) + { + break; + } + + CachedPage Cp = Cache[Key]; + + Cache.Remove(Key); + + CpCount -= Cp.Count; + + TimeDelta = RingDelta(Cp.Timestamp, Timestamp); + } + while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta); + } + + private bool TryPopOldestCachedPageKey(int Timestamp, out long Key) + { + LinkedListNode Node = SortedCache.First; + + if (Node == null) + { + Key = 0; + + return false; + } + + SortedCache.Remove(Node); + + Key = Node.Value; + + return true; + } + + private int RingDelta(int Old, int New) + { + if ((uint)New < (uint)Old) + { + return New + (~Old + 1); + } + else + { + return New - Old; + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpu.cs b/Ryujinx.HLE/Gpu/NvGpu.cs index 0301fcf2..625cb727 100644 --- a/Ryujinx.HLE/Gpu/NvGpu.cs +++ b/Ryujinx.HLE/Gpu/NvGpu.cs @@ -1,5 +1,5 @@ using Ryujinx.Graphics.Gal; -using System.Threading; +using Ryujinx.HLE.Gpu.Engines; namespace Ryujinx.HLE.Gpu { @@ -13,10 +13,6 @@ namespace Ryujinx.HLE.Gpu public NvGpuEngine3d Engine3d { get; private set; } public NvGpuEngineDma EngineDma { get; private set; } - private Thread FifoProcessing; - - private bool KeepRunning; - public NvGpu(IGalRenderer Renderer) { this.Renderer = Renderer; @@ -26,22 +22,6 @@ namespace Ryujinx.HLE.Gpu Engine2d = new NvGpuEngine2d(this); Engine3d = new NvGpuEngine3d(this); EngineDma = new NvGpuEngineDma(this); - - KeepRunning = true; - - FifoProcessing = new Thread(ProcessFifo); - - FifoProcessing.Start(); - } - - private void ProcessFifo() - { - while (KeepRunning) - { - Fifo.DispatchCalls(); - - Thread.Yield(); - } } } } \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuBufferType.cs b/Ryujinx.HLE/Gpu/NvGpuBufferType.cs deleted file mode 100644 index a44a772d..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuBufferType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuBufferType - { - Index, - Vertex, - Texture - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngine.cs b/Ryujinx.HLE/Gpu/NvGpuEngine.cs deleted file mode 100644 index 41697ed6..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngine.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuEngine - { - _2d = 0x902d, - _3d = 0xb197, - Compute = 0xb1c0, - Kepler = 0xa140, - Dma = 0xb0b5 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngine2d.cs b/Ryujinx.HLE/Gpu/NvGpuEngine2d.cs deleted file mode 100644 index 15667eeb..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngine2d.cs +++ /dev/null @@ -1,168 +0,0 @@ -using Ryujinx.Graphics.Gal; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuEngine2d : INvGpuEngine - { - private enum CopyOperation - { - SrcCopyAnd, - RopAnd, - Blend, - SrcCopy, - Rop, - SrcCopyPremult, - BlendPremult - } - - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - public NvGpuEngine2d(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0xe00]; - - Methods = new Dictionary(); - - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) - { - while (Count-- > 0) - { - Methods.Add(Meth, Method); - - Meth += Stride; - } - } - - AddMethod(0xb5, 1, 1, TextureCopy); - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); - - bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; - int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); - int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); - - bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; - int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); - int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); - int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); - int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); - - TextureSwizzle DstSwizzle = DstLinear - ? TextureSwizzle.Pitch - : TextureSwizzle.BlockLinear; - - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); - - long Tag = Vmm.GetPhysicalAddress(MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress)); - - long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); - - bool IsFbTexture = Gpu.Engine3d.IsFrameBufferPosition(Tag); - - if (IsFbTexture && DstLinear) - { - DstSwizzle = TextureSwizzle.BlockLinear; - } - - Texture DstTexture = new Texture( - DstAddress, - DstWidth, - DstHeight, - DstBlockHeight, - DstBlockHeight, - DstSwizzle, - GalTextureFormat.A8B8G8R8); - - if (IsFbTexture) - { - //TODO: Change this when the correct frame buffer resolution is used. - //Currently, the frame buffer size is hardcoded to 1280x720. - SrcWidth = 1280; - SrcHeight = 720; - - Gpu.Renderer.GetFrameBufferData(Tag, (byte[] Buffer) => - { - CopyTexture( - Vmm, - DstTexture, - Buffer, - SrcWidth, - SrcHeight); - }); - } - else - { - long Size = SrcWidth * SrcHeight * 4; - - byte[] Buffer = Vmm.ReadBytes(SrcAddress, Size); - - CopyTexture( - Vmm, - DstTexture, - Buffer, - SrcWidth, - SrcHeight); - } - } - - private void CopyTexture( - NvGpuVmm Vmm, - Texture Texture, - byte[] Buffer, - int Width, - int Height) - { - TextureWriter.Write(Vmm, Texture, Buffer, Width, Height); - } - - private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngine2dReg Reg) - { - return Registers[(int)Reg]; - } - - private void WriteRegister(NvGpuEngine2dReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngine2dReg.cs b/Ryujinx.HLE/Gpu/NvGpuEngine2dReg.cs deleted file mode 100644 index 1039e368..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngine2dReg.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuEngine2dReg - { - DstFormat = 0x80, - DstLinear = 0x81, - DstBlockDimensions = 0x82, - DstDepth = 0x83, - DstLayer = 0x84, - DstPitch = 0x85, - DstWidth = 0x86, - DstHeight = 0x87, - DstAddress = 0x88, - SrcFormat = 0x8c, - SrcLinear = 0x8d, - SrcBlockDimensions = 0x8e, - SrcDepth = 0x8f, - SrcLayer = 0x90, - SrcPitch = 0x91, - SrcWidth = 0x92, - SrcHeight = 0x93, - SrcAddress = 0x94, - CopyOperation = 0xab - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngine3d.cs b/Ryujinx.HLE/Gpu/NvGpuEngine3d.cs deleted file mode 100644 index 6d03e6b8..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngine3d.cs +++ /dev/null @@ -1,540 +0,0 @@ -using Ryujinx.Graphics.Gal; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuEngine3d : INvGpuEngine - { - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - private struct ConstBuffer - { - public bool Enabled; - public long Position; - public int Size; - } - - private ConstBuffer[][] ConstBuffers; - - private HashSet FrameBuffers; - - public NvGpuEngine3d(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0xe00]; - - Methods = new Dictionary(); - - 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); - AddMethod(0x904, 5, 8, CbBind); - - ConstBuffers = new ConstBuffer[6][]; - - for (int Index = 0; Index < ConstBuffers.Length; Index++) - { - ConstBuffers[Index] = new ConstBuffer[18]; - } - - FrameBuffers = new HashSet(); - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - SetFrameBuffer(Vmm, 0); - - long[] Tags = UploadShaders(Vmm); - - Gpu.Renderer.BindProgram(); - - SetAlphaBlending(); - - UploadTextures(Vmm, Tags); - UploadUniforms(Vmm); - UploadVertexArrays(Vmm); - } - - private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Arg0 = PBEntry.Arguments[0]; - - int FbIndex = (Arg0 >> 6) & 0xf; - - int Layer = (Arg0 >> 10) & 0x3ff; - - GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f); - - SetFrameBuffer(Vmm, 0); - - //TODO: Enable this once the frame buffer problems are fixed. - //Gpu.Renderer.ClearBuffers(Layer, Flags); - } - - private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex) - { - long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10); - - long PA = Vmm.GetPhysicalAddress(VA); - - FrameBuffers.Add(PA); - - int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); - int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); - - //Note: Using the Width/Height results seems to give incorrect results. - //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely. - Gpu.Renderer.CreateFrameBuffer(PA, 1280, 720); - Gpu.Renderer.BindFrameBuffer(PA); - } - - private long[] UploadShaders(NvGpuVmm Vmm) - { - long[] Tags = new long[5]; - - long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - - for (int Index = 0; Index < 6; Index++) - { - 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) - { - continue; - } - - long Tag = BasePosition + (uint)Offset; - - GalShaderType ShaderType = GetTypeFromProgram(Index); - - Tags[(int)ShaderType] = Tag; - - Gpu.Renderer.CreateShader(Vmm, Tag, ShaderType); - Gpu.Renderer.BindShader(Tag); - } - - int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX); - int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY); - - float SX = BitConverter.Int32BitsToSingle(RawSX); - float SY = BitConverter.Int32BitsToSingle(RawSY); - - float SignX = MathF.Sign(SX); - float SignY = MathF.Sign(SY); - - Gpu.Renderer.SetUniform2F(GalConsts.FlipUniformName, SignX, SignY); - - return Tags; - } - - 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)); - } - - private void SetAlphaBlending() - { - //TODO: Support independent blend properly. - bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0; - - Gpu.Renderer.SetBlendEnable(Enable); - - if (!Enable) - { - //If blend is not enabled, then the other values have no effect. - //Note that if it is disabled, the register may contain invalid values. - return; - } - - bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0; - - GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb); - - GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb); - GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb); - - if (BlendSeparateAlpha) - { - GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha); - - GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha); - GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha); - - Gpu.Renderer.SetBlendSeparate( - EquationRgb, - EquationAlpha, - FuncSrcRgb, - FuncDstRgb, - FuncSrcAlpha, - FuncDstAlpha); - } - else - { - Gpu.Renderer.SetBlend(EquationRgb, FuncSrcRgb, FuncDstRgb); - } - } - - private void UploadTextures(NvGpuVmm Vmm, long[] Tags) - { - long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - - int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); - - //Note: On the emulator renderer, Texture Unit 0 is - //reserved for drawing the frame buffer. - int TexIndex = 1; - - for (int Index = 0; Index < Tags.Length; Index++) - { - foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index])) - { - long Position = ConstBuffers[Index][TextureCbIndex].Position; - - UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index); - - Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex); - - TexIndex++; - } - } - } - - private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex) - { - long Position = BasePosition + HndIndex * 4; - - int TextureHandle = Vmm.ReadInt32(Position); - - if (TextureHandle == 0) - { - //TODO: Is this correct? - //Some games like puyo puyo will have 0 handles. - //It may be just normal behaviour or a bug caused by sync issues. - //The game does initialize the value properly after through. - return; - } - - int TicIndex = (TextureHandle >> 0) & 0xfffff; - int TscIndex = (TextureHandle >> 20) & 0xfff; - - long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); - long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); - - TicPosition += TicIndex * 0x20; - TscPosition += TscIndex * 0x20; - - GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); - - long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; - - long Tag = TextureAddress; - - TextureAddress = Vmm.GetPhysicalAddress(TextureAddress); - - if (IsFrameBufferPosition(TextureAddress)) - { - //This texture is a frame buffer texture, - //we shouldn't read anything from memory and bind - //the frame buffer texture instead, since we're not - //really writing anything to memory. - Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler); - } - else - { - GalTexture NewTexture = TextureFactory.MakeTexture(Vmm, TicPosition); - - long Size = (uint)TextureHelper.GetTextureSize(NewTexture); - - if (Gpu.Renderer.TryGetCachedTexture(Tag, Size, out GalTexture Texture)) - { - if (NewTexture.Equals(Texture) && !Vmm.IsRegionModified(Tag, Size, NvGpuBufferType.Texture)) - { - Gpu.Renderer.BindTexture(Tag, TexIndex); - - return; - } - } - - byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition); - - Gpu.Renderer.SetTextureAndSampler(Tag, Data, NewTexture, Sampler); - - Gpu.Renderer.BindTexture(Tag, TexIndex); - } - } - - private void UploadUniforms(NvGpuVmm Vmm) - { - long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - - for (int Index = 0; Index < 5; Index++) - { - int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10); - int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10); - - //Note: Vertex Program (B) is always enabled. - bool Enable = (Control & 1) != 0 || Index == 0; - - if (!Enable) - { - continue; - } - - for (int Cbuf = 0; Cbuf < ConstBuffers[Index].Length; Cbuf++) - { - ConstBuffer Cb = ConstBuffers[Index][Cbuf]; - - if (Cb.Enabled) - { - byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size); - - Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data); - } - } - } - } - - private void UploadVertexArrays(NvGpuVmm Vmm) - { - long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); - - int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); - int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); - int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); - - GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize; - - IndexSize = 1 << IndexSize; - - if (IndexSize > 4) - { - throw new InvalidOperationException(); - } - - if (IndexCount != 0) - { - int IbSize = IndexCount * IndexSize; - - bool IboCached = Gpu.Renderer.IsIboCached(IndexPosition, (uint)IbSize); - - if (!IboCached || Vmm.IsRegionModified(IndexPosition, (uint)IbSize, NvGpuBufferType.Index)) - { - byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize); - - Gpu.Renderer.CreateIbo(IndexPosition, Data); - } - - Gpu.Renderer.SetIndexArray(IndexPosition, IbSize, IndexFormat); - } - - List[] Attribs = new List[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(); - } - - Attribs[ArrayIndex].Add(new GalVertexAttrib( - Attr, - ((Packed >> 6) & 0x1) != 0, - (Packed >> 7) & 0x3fff, - (GalVertexAttribSize)((Packed >> 21) & 0x3f), - (GalVertexAttribType)((Packed >> 27) & 0x7), - ((Packed >> 31) & 0x1) != 0)); - } - - int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); - int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); - - int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); - - for (int Index = 0; Index < 32; Index++) - { - if (Attribs[Index] == null) - { - continue; - } - - int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); - - bool Enable = (Control & 0x1000) != 0; - - long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); - long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); - - if (!Enable) - { - continue; - } - - int Stride = Control & 0xfff; - - long VbSize = 0; - - if (IndexCount != 0) - { - VbSize = (VertexEndPos - VertexPosition) + 1; - } - else - { - VbSize = VertexCount * Stride; - } - - bool VboCached = Gpu.Renderer.IsVboCached(VertexPosition, VbSize); - - if (!VboCached || Vmm.IsRegionModified(VertexPosition, VbSize, NvGpuBufferType.Vertex)) - { - byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize); - - Gpu.Renderer.CreateVbo(VertexPosition, Data); - } - - Gpu.Renderer.SetVertexArray(Index, Stride, VertexPosition, Attribs[Index].ToArray()); - } - - GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); - - if (IndexCount != 0) - { - Gpu.Renderer.DrawElements(IndexPosition, IndexFirst, PrimType); - } - else - { - Gpu.Renderer.DrawArrays(VertexFirst, VertexCount, PrimType); - } - } - - private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); - - int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; - int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; - - int Mode = Ctrl & 3; - - if (Mode == 0) - { - //Write mode. - Vmm.WriteInt32(Position, Seq); - } - - WriteRegister(PBEntry); - } - - private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - - int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); - - foreach (int Arg in PBEntry.Arguments) - { - Vmm.WriteInt32(Position + Offset, Arg); - - Offset += 4; - } - - WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset); - } - - private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Stage = (PBEntry.Method - 0x904) >> 3; - - int Index = PBEntry.Arguments[0]; - - bool Enabled = (Index & 1) != 0; - - Index = (Index >> 4) & 0x1f; - - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - - ConstBuffers[Stage][Index].Position = Position; - ConstBuffers[Stage][Index].Enabled = Enabled; - - ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); - } - - private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngine3dReg Reg) - { - return Registers[(int)Reg]; - } - - private void WriteRegister(NvGpuEngine3dReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - - public bool IsFrameBufferPosition(long Position) - { - return FrameBuffers.Contains(Position); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngine3dReg.cs b/Ryujinx.HLE/Gpu/NvGpuEngine3dReg.cs deleted file mode 100644 index e0de4777..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngine3dReg.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuEngine3dReg - { - FrameBufferNAddress = 0x200, - FrameBufferNWidth = 0x202, - FrameBufferNHeight = 0x203, - FrameBufferNFormat = 0x204, - ViewportScaleX = 0x280, - ViewportScaleY = 0x281, - ViewportScaleZ = 0x282, - ViewportTranslateX = 0x283, - ViewportTranslateY = 0x284, - ViewportTranslateZ = 0x285, - VertexArrayFirst = 0x35d, - VertexArrayCount = 0x35e, - VertexAttribNFormat = 0x458, - IBlendEnable = 0x4b9, - BlendSeparateAlpha = 0x4cf, - BlendEquationRgb = 0x4d0, - BlendFuncSrcRgb = 0x4d1, - BlendFuncDstRgb = 0x4d2, - BlendEquationAlpha = 0x4d3, - BlendFuncSrcAlpha = 0x4d4, - BlendFuncDstAlpha = 0x4d6, - BlendEnableMaster = 0x4d7, - IBlendNEnable = 0x4d8, - VertexArrayElemBase = 0x50d, - TexHeaderPoolOffset = 0x55d, - TexSamplerPoolOffset = 0x557, - ShaderAddress = 0x582, - VertexBeginGl = 0x586, - IndexArrayAddress = 0x5f2, - IndexArrayEndAddr = 0x5f4, - IndexArrayFormat = 0x5f6, - IndexBatchFirst = 0x5f7, - IndexBatchCount = 0x5f8, - QueryAddress = 0x6c0, - QuerySequence = 0x6c2, - QueryControl = 0x6c3, - VertexArrayNControl = 0x700, - VertexArrayNAddress = 0x701, - VertexArrayNDivisor = 0x703, - IBlendNSeparateAlpha = 0x780, - IBlendNEquationRgb = 0x781, - IBlendNFuncSrcRgb = 0x782, - IBlendNFuncDstRgb = 0x783, - IBlendNEquationAlpha = 0x784, - IBlendNFuncSrcAlpha = 0x785, - IBlendNFuncDstAlpha = 0x786, - VertexArrayNEndAddr = 0x7c0, - ShaderNControl = 0x800, - ShaderNOffset = 0x801, - ShaderNMaxGprs = 0x803, - ShaderNType = 0x804, - ConstBufferSize = 0x8e0, - ConstBufferAddress = 0x8e1, - ConstBufferOffset = 0x8e3, - TextureCbIndex = 0x982 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngineDma.cs b/Ryujinx.HLE/Gpu/NvGpuEngineDma.cs deleted file mode 100644 index ed7819e9..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngineDma.cs +++ /dev/null @@ -1,141 +0,0 @@ -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuEngineDma : INvGpuEngine - { - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - public NvGpuEngineDma(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0x1d6]; - - Methods = new Dictionary(); - - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) - { - while (Count-- > 0) - { - Methods.Add(Meth, Method); - - Meth += Stride; - } - } - - AddMethod(0xc0, 1, 1, Execute); - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - private void Execute(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Control = PBEntry.Arguments[0]; - - bool SrcLinear = ((Control >> 7) & 1) != 0; - bool DstLinear = ((Control >> 8) & 1) != 0; - - long SrcAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.DstAddress); - - int SrcPitch = ReadRegister(NvGpuEngineDmaReg.SrcPitch); - int DstPitch = ReadRegister(NvGpuEngineDmaReg.DstPitch); - - int DstBlkDim = ReadRegister(NvGpuEngineDmaReg.DstBlkDim); - int DstSizeX = ReadRegister(NvGpuEngineDmaReg.DstSizeX); - int DstSizeY = ReadRegister(NvGpuEngineDmaReg.DstSizeY); - int DstSizeZ = ReadRegister(NvGpuEngineDmaReg.DstSizeZ); - int DstPosXY = ReadRegister(NvGpuEngineDmaReg.DstPosXY); - int DstPosZ = ReadRegister(NvGpuEngineDmaReg.DstPosZ); - - int SrcBlkDim = ReadRegister(NvGpuEngineDmaReg.SrcBlkDim); - int SrcSizeX = ReadRegister(NvGpuEngineDmaReg.SrcSizeX); - int SrcSizeY = ReadRegister(NvGpuEngineDmaReg.SrcSizeY); - int SrcSizeZ = ReadRegister(NvGpuEngineDmaReg.SrcSizeZ); - int SrcPosXY = ReadRegister(NvGpuEngineDmaReg.SrcPosXY); - int SrcPosZ = ReadRegister(NvGpuEngineDmaReg.SrcPosZ); - - int DstPosX = (DstPosXY >> 0) & 0xffff; - int DstPosY = (DstPosXY >> 16) & 0xffff; - - int SrcPosX = (SrcPosXY >> 0) & 0xffff; - int SrcPosY = (SrcPosXY >> 16) & 0xffff; - - int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); - - ISwizzle SrcSwizzle; - - if (SrcLinear) - { - SrcSwizzle = new LinearSwizzle(SrcPitch, 1); - } - else - { - SrcSwizzle = new BlockLinearSwizzle(SrcSizeX, 1, SrcBlockHeight); - } - - ISwizzle DstSwizzle; - - if (DstLinear) - { - DstSwizzle = new LinearSwizzle(DstPitch, 1); - } - else - { - DstSwizzle = new BlockLinearSwizzle(DstSizeX, 1, DstBlockHeight); - } - - for (int Y = 0; Y < DstSizeY; Y++) - for (int X = 0; X < DstSizeX; X++) - { - long SrcOffset = SrcAddress + (uint)SrcSwizzle.GetSwizzleOffset(X, Y); - long DstOffset = DstAddress + (uint)DstSwizzle.GetSwizzleOffset(X, Y); - - Vmm.WriteByte(DstOffset, Vmm.ReadByte(SrcOffset)); - } - } - - private long MakeInt64From2xInt32(NvGpuEngineDmaReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngineDmaReg Reg) - { - return Registers[(int)Reg]; - } - - private void WriteRegister(NvGpuEngineDmaReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuEngineDmaReg.cs b/Ryujinx.HLE/Gpu/NvGpuEngineDmaReg.cs deleted file mode 100644 index 55b404c5..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuEngineDmaReg.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuEngineDmaReg - { - SrcAddress = 0x100, - DstAddress = 0x102, - SrcPitch = 0x104, - DstPitch = 0x105, - DstBlkDim = 0x1c3, - DstSizeX = 0x1c4, - DstSizeY = 0x1c5, - DstSizeZ = 0x1c6, - DstPosZ = 0x1c7, - DstPosXY = 0x1c8, - SrcBlkDim = 0x1ca, - SrcSizeX = 0x1cb, - SrcSizeY = 0x1cc, - SrcSizeZ = 0x1cd, - SrcPosZ = 0x1ce, - SrcPosXY = 0x1cf - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuFifo.cs b/Ryujinx.HLE/Gpu/NvGpuFifo.cs deleted file mode 100644 index 361c8bce..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuFifo.cs +++ /dev/null @@ -1,182 +0,0 @@ -using System.Collections.Concurrent; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuFifo - { - private const int MacrosCount = 0x80; - private const int MacroIndexMask = MacrosCount - 1; - - //Note: The size of the macro memory is unknown, we just make - //a guess here and use 256kb as the size. Increase if needed. - private const int MmeWords = 256 * 256; - - private NvGpu Gpu; - - private ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry)> BufferQueue; - - private NvGpuEngine[] SubChannels; - - private struct CachedMacro - { - public int Position { get; private set; } - - private MacroInterpreter Interpreter; - - public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position) - { - this.Position = Position; - - Interpreter = new MacroInterpreter(PFifo, Engine); - } - - public void PushParam(int Param) - { - Interpreter?.Fifo.Enqueue(Param); - } - - public void Execute(NvGpuVmm Vmm, int[] Mme, int Param) - { - Interpreter?.Execute(Vmm, Mme, Position, Param); - } - } - - private int CurrMacroPosition; - private int CurrMacroBindIndex; - - private CachedMacro[] Macros; - - private int[] Mme; - - public NvGpuFifo(NvGpu Gpu) - { - this.Gpu = Gpu; - - BufferQueue = new ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry)>(); - - SubChannels = new NvGpuEngine[8]; - - Macros = new CachedMacro[MacrosCount]; - - Mme = new int[MmeWords]; - } - - public void PushBuffer(NvGpuVmm Vmm, NvGpuPBEntry[] Buffer) - { - foreach (NvGpuPBEntry PBEntry in Buffer) - { - BufferQueue.Enqueue((Vmm, PBEntry)); - } - } - - public void DispatchCalls() - { - while (Step()); - } - - public bool Step() - { - if (BufferQueue.TryDequeue(out (NvGpuVmm Vmm, NvGpuPBEntry PBEntry) Tuple)) - { - CallMethod(Tuple.Vmm, Tuple.PBEntry); - - return true; - } - - return false; - } - - private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (PBEntry.Method < 0x80) - { - switch ((NvGpuFifoMeth)PBEntry.Method) - { - case NvGpuFifoMeth.BindChannel: - { - NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0]; - - SubChannels[PBEntry.SubChannel] = Engine; - - break; - } - - case NvGpuFifoMeth.SetMacroUploadAddress: - { - CurrMacroPosition = PBEntry.Arguments[0]; - - break; - } - - case NvGpuFifoMeth.SendMacroCodeData: - { - foreach (int Arg in PBEntry.Arguments) - { - Mme[CurrMacroPosition++] = Arg; - } - break; - } - - case NvGpuFifoMeth.SetMacroBindingIndex: - { - CurrMacroBindIndex = PBEntry.Arguments[0]; - - break; - } - - case NvGpuFifoMeth.BindMacro: - { - int Position = PBEntry.Arguments[0]; - - Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position); - - break; - } - } - } - else - { - switch (SubChannels[PBEntry.SubChannel]) - { - case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break; - case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break; - case NvGpuEngine.Dma: CallDmaMethod(Vmm, PBEntry); break; - } - } - } - - private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - Gpu.Engine2d.CallMethod(Vmm, PBEntry); - } - - private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (PBEntry.Method < 0xe00) - { - Gpu.Engine3d.CallMethod(Vmm, PBEntry); - } - else - { - int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask; - - if ((PBEntry.Method & 1) != 0) - { - foreach (int Arg in PBEntry.Arguments) - { - Macros[MacroIndex].PushParam(Arg); - } - } - else - { - Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]); - } - } - } - - private void CallDmaMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - Gpu.EngineDma.CallMethod(Vmm, PBEntry); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuFifoMeth.cs b/Ryujinx.HLE/Gpu/NvGpuFifoMeth.cs deleted file mode 100644 index 247a7bfc..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuFifoMeth.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum NvGpuFifoMeth - { - BindChannel = 0, - SetMacroUploadAddress = 0x45, - SendMacroCodeData = 0x46, - SetMacroBindingIndex = 0x47, - BindMacro = 0x48 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuMethod.cs b/Ryujinx.HLE/Gpu/NvGpuMethod.cs deleted file mode 100644 index f7ff6647..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuMethod.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - delegate void NvGpuMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuPBEntry.cs b/Ryujinx.HLE/Gpu/NvGpuPBEntry.cs deleted file mode 100644 index 2cd663fe..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuPBEntry.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.ObjectModel; - -namespace Ryujinx.HLE.Gpu -{ - struct NvGpuPBEntry - { - public int Method { get; private set; } - - public int SubChannel { get; private set; } - - private int[] m_Arguments; - - public ReadOnlyCollection Arguments => Array.AsReadOnly(m_Arguments); - - public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments) - { - this.Method = Method; - this.SubChannel = SubChannel; - this.m_Arguments = Arguments; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuPushBuffer.cs b/Ryujinx.HLE/Gpu/NvGpuPushBuffer.cs deleted file mode 100644 index 2d4f0c1a..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuPushBuffer.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Collections.Generic; -using System.IO; - -namespace Ryujinx.HLE.Gpu -{ - static class NvGpuPushBuffer - { - private enum SubmissionMode - { - Incrementing = 1, - NonIncrementing = 3, - Immediate = 4, - IncrementOnce = 5 - } - - public static NvGpuPBEntry[] Decode(byte[] Data) - { - using (MemoryStream MS = new MemoryStream(Data)) - { - BinaryReader Reader = new BinaryReader(MS); - - List PushBuffer = new List(); - - bool CanRead() => MS.Position + 4 <= MS.Length; - - while (CanRead()) - { - int Packed = Reader.ReadInt32(); - - int Meth = (Packed >> 0) & 0x1fff; - int SubC = (Packed >> 13) & 7; - int Args = (Packed >> 16) & 0x1fff; - int Mode = (Packed >> 29) & 7; - - switch ((SubmissionMode)Mode) - { - case SubmissionMode.Incrementing: - { - for (int Index = 0; Index < Args && CanRead(); Index++, Meth++) - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); - } - - break; - } - - case SubmissionMode.NonIncrementing: - { - int[] Arguments = new int[Args]; - - for (int Index = 0; Index < Arguments.Length; Index++) - { - if (!CanRead()) - { - break; - } - - Arguments[Index] = Reader.ReadInt32(); - } - - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments)); - - break; - } - - case SubmissionMode.Immediate: - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args)); - - break; - } - - case SubmissionMode.IncrementOnce: - { - if (CanRead()) - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); - } - - if (CanRead() && Args > 1) - { - int[] Arguments = new int[Args - 1]; - - for (int Index = 0; Index < Arguments.Length && CanRead(); Index++) - { - Arguments[Index] = Reader.ReadInt32(); - } - - PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments)); - } - - break; - } - } - } - - return PushBuffer.ToArray(); - } - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuVmm.cs b/Ryujinx.HLE/Gpu/NvGpuVmm.cs deleted file mode 100644 index b0ba3e90..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuVmm.cs +++ /dev/null @@ -1,410 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System.Collections.Concurrent; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuVmm : IAMemory, IGalMemory - { - public const long AddrSize = 1L << 40; - - private const int PTLvl0Bits = 14; - private const int PTLvl1Bits = 14; - private const int PTPageBits = 12; - - private const int PTLvl0Size = 1 << PTLvl0Bits; - private const int PTLvl1Size = 1 << PTLvl1Bits; - public const int PageSize = 1 << PTPageBits; - - private const int PTLvl0Mask = PTLvl0Size - 1; - private const int PTLvl1Mask = PTLvl1Size - 1; - public const int PageMask = PageSize - 1; - - private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; - private const int PTLvl1Bit = PTPageBits; - - public AMemory Memory { get; private set; } - - private struct MappedMemory - { - public long Size; - - public MappedMemory(long Size) - { - this.Size = Size; - } - } - - private ConcurrentDictionary Maps; - - private NvGpuVmmCache Cache; - - private const long PteUnmapped = -1; - private const long PteReserved = -2; - - private long[][] PageTable; - - public NvGpuVmm(AMemory Memory) - { - this.Memory = Memory; - - Maps = new ConcurrentDictionary(); - - Cache = new NvGpuVmmCache(); - - PageTable = new long[PTLvl0Size][]; - } - - public long Map(long PA, long VA, long Size) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - if (GetPte(VA + Offset) != PteReserved) - { - return Map(PA, Size); - } - } - - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PA + Offset); - } - } - - return VA; - } - - public long Map(long PA, long Size) - { - lock (PageTable) - { - long VA = GetFreePosition(Size); - - if (VA != -1) - { - MappedMemory Map = new MappedMemory(Size); - - Maps.AddOrUpdate(VA, Map, (Key, Old) => Map); - - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PA + Offset); - } - } - - return VA; - } - } - - public bool Unmap(long VA) - { - if (Maps.TryRemove(VA, out MappedMemory Map)) - { - Free(VA, Map.Size); - - return true; - } - - return false; - } - - public long Reserve(long VA, long Size, long Align) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - if (IsPageInUse(VA + Offset)) - { - return Reserve(Size, Align); - } - } - - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PteReserved); - } - } - - return VA; - } - - public long Reserve(long Size, long Align) - { - lock (PageTable) - { - long Position = GetFreePosition(Size, Align); - - if (Position != -1) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(Position + Offset, PteReserved); - } - } - - return Position; - } - } - - public void Free(long VA, long Size) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PteUnmapped); - } - } - } - - private long GetFreePosition(long Size, long Align = 1) - { - long Position = 0; - long FreeSize = 0; - - if (Align < 1) - { - Align = 1; - } - - Align = (Align + PageMask) & ~PageMask; - - while (Position + FreeSize < AddrSize) - { - if (!IsPageInUse(Position + FreeSize)) - { - FreeSize += PageSize; - - if (FreeSize >= Size) - { - return Position; - } - } - else - { - Position += FreeSize + PageSize; - FreeSize = 0; - - long Remainder = Position % Align; - - if (Remainder != 0) - { - Position = (Position - Remainder) + Align; - } - } - } - - return -1; - } - - public long GetPhysicalAddress(long VA) - { - long BasePos = GetPte(VA); - - if (BasePos < 0) - { - return -1; - } - - return BasePos + (VA & PageMask); - } - - public bool IsRegionFree(long VA, long Size) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - if (IsPageInUse(VA + Offset)) - { - return false; - } - } - - return true; - } - - private bool IsPageInUse(long VA) - { - if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0) - { - return false; - } - - long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - return false; - } - - return PageTable[L0][L1] != PteUnmapped; - } - - private long GetPte(long Position) - { - long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - return -1; - } - - return PageTable[L0][L1]; - } - - private void SetPte(long Position, long TgtAddr) - { - long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - PageTable[L0] = new long[PTLvl1Size]; - - for (int Index = 0; Index < PTLvl1Size; Index++) - { - PageTable[L0][Index] = PteUnmapped; - } - } - - PageTable[L0][L1] = TgtAddr; - } - - public bool IsRegionModified(long Position, long Size, NvGpuBufferType BufferType) - { - long PA = GetPhysicalAddress(Position); - - return Cache.IsRegionModified(Memory, BufferType, Position, PA, Size); - } - - public byte ReadByte(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadByte(Position); - } - - public ushort ReadUInt16(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt16(Position); - } - - public uint ReadUInt32(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt32(Position); - } - - public ulong ReadUInt64(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt64(Position); - } - - public sbyte ReadSByte(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadSByte(Position); - } - - public short ReadInt16(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt16(Position); - } - - public int ReadInt32(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt32(Position); - } - - public long ReadInt64(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt64(Position); - } - - public byte[] ReadBytes(long Position, long Size) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadBytes(Position, Size); - } - - public void WriteByte(long Position, byte Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteByte(Position, Value); - } - - public void WriteUInt16(long Position, ushort Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt16(Position, Value); - } - - public void WriteUInt32(long Position, uint Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt32(Position, Value); - } - - public void WriteUInt64(long Position, ulong Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt64(Position, Value); - } - - public void WriteSByte(long Position, sbyte Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteSByte(Position, Value); - } - - public void WriteInt16(long Position, short Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt16(Position, Value); - } - - public void WriteInt32(long Position, int Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt32(Position, Value); - } - - public void WriteInt64(long Position, long Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt64(Position, Value); - } - - public void WriteBytes(long Position, byte[] Data) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteBytes(Position, Data); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpuVmmCache.cs b/Ryujinx.HLE/Gpu/NvGpuVmmCache.cs deleted file mode 100644 index 38b25e4f..00000000 --- a/Ryujinx.HLE/Gpu/NvGpuVmmCache.cs +++ /dev/null @@ -1,209 +0,0 @@ -using ChocolArm64.Memory; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpuVmmCache - { - private const int MaxCpCount = 10000; - private const int MaxCpTimeDelta = 60000; - - private class CachedPage - { - private List<(long Start, long End)> Regions; - - public LinkedListNode Node { get; set; } - - public int Count => Regions.Count; - - public int Timestamp { get; private set; } - - public long PABase { get; private set; } - - public NvGpuBufferType BufferType { get; private set; } - - public CachedPage(long PABase, NvGpuBufferType BufferType) - { - this.PABase = PABase; - this.BufferType = BufferType; - - Regions = new List<(long, long)>(); - } - - public bool AddRange(long Start, long End) - { - for (int Index = 0; Index < Regions.Count; Index++) - { - (long RgStart, long RgEnd) = Regions[Index]; - - if (Start >= RgStart && End <= RgEnd) - { - return false; - } - - if (Start <= RgEnd && RgStart <= End) - { - long MinStart = Math.Min(RgStart, Start); - long MaxEnd = Math.Max(RgEnd, End); - - Regions[Index] = (MinStart, MaxEnd); - - Timestamp = Environment.TickCount; - - return true; - } - } - - Regions.Add((Start, End)); - - Timestamp = Environment.TickCount; - - return true; - } - } - - private Dictionary Cache; - - private LinkedList SortedCache; - - private int CpCount; - - public NvGpuVmmCache() - { - Cache = new Dictionary(); - - SortedCache = new LinkedList(); - } - - public bool IsRegionModified( - AMemory Memory, - NvGpuBufferType BufferType, - long VA, - long PA, - long Size) - { - ClearCachedPagesIfNeeded(); - - long PageSize = Memory.GetHostPageSize(); - - long Mask = PageSize - 1; - - long VAEnd = VA + Size; - long PAEnd = PA + Size; - - bool RegMod = false; - - while (VA < VAEnd) - { - long Key = VA & ~Mask; - long PABase = PA & ~Mask; - - long VAPgEnd = Math.Min((VA + PageSize) & ~Mask, VAEnd); - long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd); - - bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp); - - bool PgReset = false; - - if (!IsCached) - { - Cp = new CachedPage(PABase, BufferType); - - Cache.Add(Key, Cp); - } - else - { - CpCount -= Cp.Count; - - SortedCache.Remove(Cp.Node); - - if (Cp.PABase != PABase || - Cp.BufferType != BufferType) - { - PgReset = true; - } - } - - PgReset |= Memory.IsRegionModified(PA, PAPgEnd - PA) && IsCached; - - if (PgReset) - { - Cp = new CachedPage(PABase, BufferType); - - Cache[Key] = Cp; - } - - Cp.Node = SortedCache.AddLast(Key); - - RegMod |= Cp.AddRange(VA, VAPgEnd); - - CpCount += Cp.Count; - - VA = VAPgEnd; - PA = PAPgEnd; - } - - return RegMod; - } - - private void ClearCachedPagesIfNeeded() - { - if (CpCount <= MaxCpCount) - { - return; - } - - int Timestamp = Environment.TickCount; - - int TimeDelta; - - do - { - if (!TryPopOldestCachedPageKey(Timestamp, out long Key)) - { - break; - } - - CachedPage Cp = Cache[Key]; - - Cache.Remove(Key); - - CpCount -= Cp.Count; - - TimeDelta = RingDelta(Cp.Timestamp, Timestamp); - } - while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta); - } - - private bool TryPopOldestCachedPageKey(int Timestamp, out long Key) - { - LinkedListNode Node = SortedCache.First; - - if (Node == null) - { - Key = 0; - - return false; - } - - SortedCache.Remove(Node); - - Key = Node.Value; - - return true; - } - - private int RingDelta(int Old, int New) - { - if ((uint)New < (uint)Old) - { - return New + (~Old + 1); - } - else - { - return New - Old; - } - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture.cs b/Ryujinx.HLE/Gpu/Texture.cs deleted file mode 100644 index 1de7f302..00000000 --- a/Ryujinx.HLE/Gpu/Texture.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Ryujinx.Graphics.Gal; - -namespace Ryujinx.HLE.Gpu -{ - struct Texture - { - public long Position { get; private set; } - - public int Width { get; private set; } - public int Height { get; private set; } - public int Pitch { get; private set; } - - public int BlockHeight { get; private set; } - - public TextureSwizzle Swizzle { get; private set; } - - public GalTextureFormat Format { get; private set; } - - public Texture( - long Position, - int Width, - int Height) - { - this.Position = Position; - this.Width = Width; - this.Height = Height; - - Pitch = 0; - - BlockHeight = 16; - - Swizzle = TextureSwizzle.BlockLinear; - - Format = GalTextureFormat.A8B8G8R8; - } - - public Texture( - long Position, - int Width, - int Height, - int Pitch, - int BlockHeight, - TextureSwizzle Swizzle, - GalTextureFormat Format) - { - this.Position = Position; - this.Width = Width; - this.Height = Height; - this.Pitch = Pitch; - this.BlockHeight = BlockHeight; - this.Swizzle = Swizzle; - this.Format = Format; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs new file mode 100644 index 00000000..e66d7613 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs @@ -0,0 +1,59 @@ +using System; + +namespace Ryujinx.HLE.Gpu.Texture +{ + class BlockLinearSwizzle : ISwizzle + { + private int BhShift; + private int BppShift; + private int BhMask; + + private int XShift; + private int GobStride; + + public BlockLinearSwizzle(int Width, int Bpp, int BlockHeight = 16) + { + BhMask = (BlockHeight * 8) - 1; + + BhShift = CountLsbZeros(BlockHeight * 8); + BppShift = CountLsbZeros(Bpp); + + int WidthInGobs = (int)MathF.Ceiling(Width * Bpp / 64f); + + GobStride = 512 * BlockHeight * WidthInGobs; + + XShift = CountLsbZeros(512 * BlockHeight); + } + + private int CountLsbZeros(int Value) + { + int Count = 0; + + while (((Value >> Count) & 1) == 0) + { + Count++; + } + + return Count; + } + + public int GetSwizzleOffset(int X, int Y) + { + X <<= BppShift; + + int Position = (Y >> BhShift) * GobStride; + + Position += (X >> 6) << XShift; + + Position += ((Y & BhMask) >> 3) << 9; + + Position += ((X & 0x3f) >> 5) << 8; + Position += ((Y & 0x07) >> 1) << 6; + Position += ((X & 0x1f) >> 4) << 5; + Position += ((Y & 0x01) >> 0) << 4; + Position += ((X & 0x0f) >> 0) << 0; + + return Position; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs b/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs new file mode 100644 index 00000000..222aab16 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs @@ -0,0 +1,7 @@ +namespace Ryujinx.HLE.Gpu.Texture +{ + interface ISwizzle + { + int GetSwizzleOffset(int X, int Y); + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs new file mode 100644 index 00000000..720f7832 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs @@ -0,0 +1,19 @@ +namespace Ryujinx.HLE.Gpu.Texture +{ + class LinearSwizzle : ISwizzle + { + private int Pitch; + private int Bpp; + + public LinearSwizzle(int Pitch, int Bpp) + { + this.Pitch = Pitch; + this.Bpp = Bpp; + } + + public int GetSwizzleOffset(int X, int Y) + { + return X * Bpp + Y * Pitch; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs b/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs new file mode 100644 index 00000000..9df0b600 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs @@ -0,0 +1,117 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.HLE.Gpu.Memory; +using System; + +namespace Ryujinx.HLE.Gpu.Texture +{ + static class TextureFactory + { + public static GalTexture MakeTexture(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); + + GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); + GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); + GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); + GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + return new GalTexture( + Width, + Height, + Format, + XSource, + YSource, + ZSource, + WSource); + } + + public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); + + long TextureAddress = (uint)Tic[1]; + + TextureAddress |= (long)((ushort)Tic[2]) << 32; + + TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); + + if (Swizzle == TextureSwizzle.BlockLinear || + Swizzle == TextureSwizzle.BlockLinearColorKey) + { + TextureAddress &= ~0x1ffL; + } + else if (Swizzle == TextureSwizzle.Pitch || + Swizzle == TextureSwizzle.PitchColorKey) + { + TextureAddress &= ~0x1fL; + } + + int Pitch = (Tic[3] & 0xffff) << 5; + + int BlockHeightLog2 = (Tic[3] >> 3) & 7; + + int BlockHeight = 1 << BlockHeightLog2; + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + TextureInfo Texture = new TextureInfo( + TextureAddress, + Width, + Height, + Pitch, + BlockHeight, + Swizzle, + Format); + + return TextureReader.Read(Vmm, Texture); + } + + public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) + { + int[] Tsc = ReadWords(Vmm, TscPosition, 8); + + GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); + GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); + GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); + + GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); + GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); + GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); + + GalColorF BorderColor = new GalColorF( + BitConverter.Int32BitsToSingle(Tsc[4]), + BitConverter.Int32BitsToSingle(Tsc[5]), + BitConverter.Int32BitsToSingle(Tsc[6]), + BitConverter.Int32BitsToSingle(Tsc[7])); + + return new GalTextureSampler( + AddressU, + AddressV, + AddressP, + MinFilter, + MagFilter, + MipFilter, + BorderColor); + } + + private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) + { + int[] Words = new int[Count]; + + for (int Index = 0; Index < Count; Index++, Position += 4) + { + Words[Index] = Vmm.ReadInt32(Position); + } + + return Words; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs b/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs new file mode 100644 index 00000000..ac8f75c5 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs @@ -0,0 +1,78 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using Ryujinx.HLE.Gpu.Memory; +using System; + +namespace Ryujinx.HLE.Gpu.Texture +{ + static class TextureHelper + { + public static ISwizzle GetSwizzle(TextureInfo Texture, int Width, int Bpp) + { + switch (Texture.Swizzle) + { + case TextureSwizzle._1dBuffer: + case TextureSwizzle.Pitch: + case TextureSwizzle.PitchColorKey: + return new LinearSwizzle(Texture.Pitch, Bpp); + + case TextureSwizzle.BlockLinear: + case TextureSwizzle.BlockLinearColorKey: + return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight); + } + + throw new NotImplementedException(Texture.Swizzle.ToString()); + } + + public static int GetTextureSize(GalTexture Texture) + { + switch (Texture.Format) + { + case GalTextureFormat.R32G32B32A32: return Texture.Width * Texture.Height * 16; + case GalTextureFormat.R16G16B16A16: return Texture.Width * Texture.Height * 8; + case GalTextureFormat.A8B8G8R8: return Texture.Width * Texture.Height * 4; + case GalTextureFormat.R32: return Texture.Width * Texture.Height * 4; + case GalTextureFormat.A1B5G5R5: return Texture.Width * Texture.Height * 2; + case GalTextureFormat.B5G6R5: return Texture.Width * Texture.Height * 2; + case GalTextureFormat.G8R8: return Texture.Width * Texture.Height * 2; + case GalTextureFormat.R16: return Texture.Width * Texture.Height * 2; + case GalTextureFormat.R8: return Texture.Width * Texture.Height; + + case GalTextureFormat.BC1: + case GalTextureFormat.BC4: + { + int W = (Texture.Width + 3) / 4; + int H = (Texture.Height + 3) / 4; + + return W * H * 8; + } + + case GalTextureFormat.BC7U: + case GalTextureFormat.BC2: + case GalTextureFormat.BC3: + case GalTextureFormat.BC5: + case GalTextureFormat.Astc2D4x4: + { + int W = (Texture.Width + 3) / 4; + int H = (Texture.Height + 3) / 4; + + return W * H * 16; + } + } + + throw new NotImplementedException(Texture.Format.ToString()); + } + + public static (AMemory Memory, long Position) GetMemoryAndPosition( + IAMemory Memory, + long Position) + { + if (Memory is NvGpuVmm Vmm) + { + return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); + } + + return ((AMemory)Memory, Position); + } + } +} diff --git a/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs b/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs new file mode 100644 index 00000000..31784bbc --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs @@ -0,0 +1,55 @@ +using Ryujinx.Graphics.Gal; + +namespace Ryujinx.HLE.Gpu.Texture +{ + struct TextureInfo + { + public long Position { get; private set; } + + public int Width { get; private set; } + public int Height { get; private set; } + public int Pitch { get; private set; } + + public int BlockHeight { get; private set; } + + public TextureSwizzle Swizzle { get; private set; } + + public GalTextureFormat Format { get; private set; } + + public TextureInfo( + long Position, + int Width, + int Height) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + + Pitch = 0; + + BlockHeight = 16; + + Swizzle = TextureSwizzle.BlockLinear; + + Format = GalTextureFormat.A8B8G8R8; + } + + public TextureInfo( + long Position, + int Width, + int Height, + int Pitch, + int BlockHeight, + TextureSwizzle Swizzle, + GalTextureFormat Format) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + this.Pitch = Pitch; + this.BlockHeight = BlockHeight; + this.Swizzle = Swizzle; + this.Format = Format; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureReader.cs b/Ryujinx.HLE/Gpu/Texture/TextureReader.cs new file mode 100644 index 00000000..48bf1a90 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureReader.cs @@ -0,0 +1,344 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System; + +namespace Ryujinx.HLE.Gpu.Texture +{ + static class TextureReader + { + public static byte[] Read(IAMemory Memory, TextureInfo Texture) + { + switch (Texture.Format) + { + case GalTextureFormat.R32G32B32A32: return Read16Bpp (Memory, Texture); + case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture); + case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture); + case GalTextureFormat.R32: return Read4Bpp (Memory, Texture); + case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture); + case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture); + case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture); + case GalTextureFormat.R16: return Read2Bpp (Memory, Texture); + case GalTextureFormat.R8: return Read1Bpp (Memory, Texture); + case GalTextureFormat.BC7U: return Read16Bpt4x4(Memory, Texture); + case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture); + case GalTextureFormat.BC2: return Read16Bpt4x4(Memory, Texture); + case GalTextureFormat.BC3: return Read16Bpt4x4(Memory, Texture); + case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture); + case GalTextureFormat.BC5: return Read16Bpt4x4(Memory, Texture); + case GalTextureFormat.Astc2D4x4: return Read16Bpt4x4(Memory, Texture); + } + + throw new NotImplementedException(Texture.Format.ToString()); + } + + private unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 1); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + byte Pixel = CpuMem.ReadByteUnchecked(Position + Offset); + + *(BuffPtr + OutOffs) = Pixel; + + OutOffs++; + } + } + + return Output; + } + + private unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x03e0) << 1 | + (Pixel & 0x7c00) >> 9 | + (Pixel & 0x8000) >> 15; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + private unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x07e0) | + (Pixel & 0xf800) >> 11; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + private unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + short Pixel = CpuMem.ReadInt16Unchecked(Position + Offset); + + *(short*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + private unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 4]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = CpuMem.ReadInt32Unchecked(Position + Offset); + + *(int*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 4; + } + } + + return Output; + } + + private unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Pixel = CpuMem.ReadInt64Unchecked(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 8; + } + } + + return Output; + } + + private unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long PxLow = CpuMem.ReadInt64Unchecked(Position + Offset + 0); + long PxHigh = CpuMem.ReadInt64Unchecked(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = PxLow; + *(long*)(BuffPtr + OutOffs + 8) = PxHigh; + + OutOffs += 16; + } + } + + return Output; + } + + private unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture) + { + int Width = (Texture.Width + 3) / 4; + int Height = (Texture.Height + 3) / 4; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile = CpuMem.ReadInt64Unchecked(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Tile; + + OutOffs += 8; + } + } + + return Output; + } + + private unsafe static byte[] Read16Bpt4x4(IAMemory Memory, TextureInfo Texture) + { + int Width = (Texture.Width + 3) / 4; + int Height = (Texture.Height + 3) / 4; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile0 = CpuMem.ReadInt64Unchecked(Position + Offset + 0); + long Tile1 = CpuMem.ReadInt64Unchecked(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = Tile0; + *(long*)(BuffPtr + OutOffs + 8) = Tile1; + + OutOffs += 16; + } + } + + return Output; + } + } +} diff --git a/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs new file mode 100644 index 00000000..076df97a --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.HLE.Gpu.Texture +{ + enum TextureSwizzle + { + _1dBuffer = 0, + PitchColorKey = 1, + Pitch = 2, + BlockLinear = 3, + BlockLinearColorKey = 4 + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs b/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs new file mode 100644 index 00000000..b64302a5 --- /dev/null +++ b/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs @@ -0,0 +1,55 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System; + +namespace Ryujinx.HLE.Gpu.Texture +{ + static class TextureWriter + { + public static void Write( + IAMemory Memory, + TextureInfo Texture, + byte[] Data, + int Width, + int Height) + { + switch (Texture.Format) + { + case GalTextureFormat.A8B8G8R8: Write4Bpp(Memory, Texture, Data, Width, Height); break; + + default: throw new NotImplementedException(Texture.Format.ToString()); + } + } + + private unsafe static void Write4Bpp( + IAMemory Memory, + TextureInfo Texture, + byte[] Data, + int Width, + int Height) + { + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Data) + { + long InOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = *(int*)(BuffPtr + InOffs); + + CpuMem.WriteInt32Unchecked(Position + Offset, Pixel); + + InOffs += 4; + } + } + } + } +} diff --git a/Ryujinx.HLE/Gpu/TextureFactory.cs b/Ryujinx.HLE/Gpu/TextureFactory.cs deleted file mode 100644 index 94c6eb18..00000000 --- a/Ryujinx.HLE/Gpu/TextureFactory.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu -{ - static class TextureFactory - { - public static GalTexture MakeTexture(NvGpuVmm Vmm, long TicPosition) - { - int[] Tic = ReadWords(Vmm, TicPosition, 8); - - GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); - - GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); - GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); - GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); - GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); - - int Width = (Tic[4] & 0xffff) + 1; - int Height = (Tic[5] & 0xffff) + 1; - - return new GalTexture( - Width, - Height, - Format, - XSource, - YSource, - ZSource, - WSource); - } - - public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition) - { - int[] Tic = ReadWords(Vmm, TicPosition, 8); - - GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); - - long TextureAddress = (uint)Tic[1]; - - TextureAddress |= (long)((ushort)Tic[2]) << 32; - - TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); - - if (Swizzle == TextureSwizzle.BlockLinear || - Swizzle == TextureSwizzle.BlockLinearColorKey) - { - TextureAddress &= ~0x1ffL; - } - else if (Swizzle == TextureSwizzle.Pitch || - Swizzle == TextureSwizzle.PitchColorKey) - { - TextureAddress &= ~0x1fL; - } - - int Pitch = (Tic[3] & 0xffff) << 5; - - int BlockHeightLog2 = (Tic[3] >> 3) & 7; - - int BlockHeight = 1 << BlockHeightLog2; - - int Width = (Tic[4] & 0xffff) + 1; - int Height = (Tic[5] & 0xffff) + 1; - - Texture Texture = new Texture( - TextureAddress, - Width, - Height, - Pitch, - BlockHeight, - Swizzle, - Format); - - return TextureReader.Read(Vmm, Texture); - } - - public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) - { - int[] Tsc = ReadWords(Vmm, TscPosition, 8); - - GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); - GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); - GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); - - GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); - GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); - GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); - - GalColorF BorderColor = new GalColorF( - BitConverter.Int32BitsToSingle(Tsc[4]), - BitConverter.Int32BitsToSingle(Tsc[5]), - BitConverter.Int32BitsToSingle(Tsc[6]), - BitConverter.Int32BitsToSingle(Tsc[7])); - - return new GalTextureSampler( - AddressU, - AddressV, - AddressP, - MinFilter, - MagFilter, - MipFilter, - BorderColor); - } - - private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) - { - int[] Words = new int[Count]; - - for (int Index = 0; Index < Count; Index++, Position += 4) - { - Words[Index] = Vmm.ReadInt32(Position); - } - - return Words; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/TextureHelper.cs b/Ryujinx.HLE/Gpu/TextureHelper.cs deleted file mode 100644 index 237d87ab..00000000 --- a/Ryujinx.HLE/Gpu/TextureHelper.cs +++ /dev/null @@ -1,77 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu -{ - static class TextureHelper - { - public static ISwizzle GetSwizzle(Texture Texture, int Width, int Bpp) - { - switch (Texture.Swizzle) - { - case TextureSwizzle._1dBuffer: - case TextureSwizzle.Pitch: - case TextureSwizzle.PitchColorKey: - return new LinearSwizzle(Texture.Pitch, Bpp); - - case TextureSwizzle.BlockLinear: - case TextureSwizzle.BlockLinearColorKey: - return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight); - } - - throw new NotImplementedException(Texture.Swizzle.ToString()); - } - - public static int GetTextureSize(GalTexture Texture) - { - switch (Texture.Format) - { - case GalTextureFormat.R32G32B32A32: return Texture.Width * Texture.Height * 16; - case GalTextureFormat.R16G16B16A16: return Texture.Width * Texture.Height * 8; - case GalTextureFormat.A8B8G8R8: return Texture.Width * Texture.Height * 4; - case GalTextureFormat.R32: return Texture.Width * Texture.Height * 4; - case GalTextureFormat.A1B5G5R5: return Texture.Width * Texture.Height * 2; - case GalTextureFormat.B5G6R5: return Texture.Width * Texture.Height * 2; - case GalTextureFormat.G8R8: return Texture.Width * Texture.Height * 2; - case GalTextureFormat.R16: return Texture.Width * Texture.Height * 2; - case GalTextureFormat.R8: return Texture.Width * Texture.Height; - - case GalTextureFormat.BC1: - case GalTextureFormat.BC4: - { - int W = (Texture.Width + 3) / 4; - int H = (Texture.Height + 3) / 4; - - return W * H * 8; - } - - case GalTextureFormat.BC7U: - case GalTextureFormat.BC2: - case GalTextureFormat.BC3: - case GalTextureFormat.BC5: - case GalTextureFormat.Astc2D4x4: - { - int W = (Texture.Width + 3) / 4; - int H = (Texture.Height + 3) / 4; - - return W * H * 16; - } - } - - throw new NotImplementedException(Texture.Format.ToString()); - } - - public static (AMemory Memory, long Position) GetMemoryAndPosition( - IAMemory Memory, - long Position) - { - if (Memory is NvGpuVmm Vmm) - { - return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); - } - - return ((AMemory)Memory, Position); - } - } -} diff --git a/Ryujinx.HLE/Gpu/TextureReader.cs b/Ryujinx.HLE/Gpu/TextureReader.cs deleted file mode 100644 index 9e9ff783..00000000 --- a/Ryujinx.HLE/Gpu/TextureReader.cs +++ /dev/null @@ -1,344 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu -{ - static class TextureReader - { - public static byte[] Read(IAMemory Memory, Texture Texture) - { - switch (Texture.Format) - { - case GalTextureFormat.R32G32B32A32: return Read16Bpp (Memory, Texture); - case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture); - case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture); - case GalTextureFormat.R32: return Read4Bpp (Memory, Texture); - case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture); - case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture); - case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture); - case GalTextureFormat.R16: return Read2Bpp (Memory, Texture); - case GalTextureFormat.R8: return Read1Bpp (Memory, Texture); - case GalTextureFormat.BC7U: return Read16Bpt4x4(Memory, Texture); - case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture); - case GalTextureFormat.BC2: return Read16Bpt4x4(Memory, Texture); - case GalTextureFormat.BC3: return Read16Bpt4x4(Memory, Texture); - case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture); - case GalTextureFormat.BC5: return Read16Bpt4x4(Memory, Texture); - case GalTextureFormat.Astc2D4x4: return Read16Bpt4x4(Memory, Texture); - } - - throw new NotImplementedException(Texture.Format.ToString()); - } - - private unsafe static byte[] Read1Bpp(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 1); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - byte Pixel = CpuMem.ReadByteUnchecked(Position + Offset); - - *(BuffPtr + OutOffs) = Pixel; - - OutOffs++; - } - } - - return Output; - } - - private unsafe static byte[] Read5551(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset); - - Pixel = (Pixel & 0x001f) << 11 | - (Pixel & 0x03e0) << 1 | - (Pixel & 0x7c00) >> 9 | - (Pixel & 0x8000) >> 15; - - *(short*)(BuffPtr + OutOffs) = (short)Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read565(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset); - - Pixel = (Pixel & 0x001f) << 11 | - (Pixel & 0x07e0) | - (Pixel & 0xf800) >> 11; - - *(short*)(BuffPtr + OutOffs) = (short)Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read2Bpp(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - short Pixel = CpuMem.ReadInt16Unchecked(Position + Offset); - - *(short*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read4Bpp(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 4]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - int Pixel = CpuMem.ReadInt32Unchecked(Position + Offset); - - *(int*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 4; - } - } - - return Output; - } - - private unsafe static byte[] Read8Bpp(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 8]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Pixel = CpuMem.ReadInt64Unchecked(Position + Offset); - - *(long*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 8; - } - } - - return Output; - } - - private unsafe static byte[] Read16Bpp(IAMemory Memory, Texture Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 16]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long PxLow = CpuMem.ReadInt64Unchecked(Position + Offset + 0); - long PxHigh = CpuMem.ReadInt64Unchecked(Position + Offset + 8); - - *(long*)(BuffPtr + OutOffs + 0) = PxLow; - *(long*)(BuffPtr + OutOffs + 8) = PxHigh; - - OutOffs += 16; - } - } - - return Output; - } - - private unsafe static byte[] Read8Bpt4x4(IAMemory Memory, Texture Texture) - { - int Width = (Texture.Width + 3) / 4; - int Height = (Texture.Height + 3) / 4; - - byte[] Output = new byte[Width * Height * 8]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Tile = CpuMem.ReadInt64Unchecked(Position + Offset); - - *(long*)(BuffPtr + OutOffs) = Tile; - - OutOffs += 8; - } - } - - return Output; - } - - private unsafe static byte[] Read16Bpt4x4(IAMemory Memory, Texture Texture) - { - int Width = (Texture.Width + 3) / 4; - int Height = (Texture.Height + 3) / 4; - - byte[] Output = new byte[Width * Height * 16]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Tile0 = CpuMem.ReadInt64Unchecked(Position + Offset + 0); - long Tile1 = CpuMem.ReadInt64Unchecked(Position + Offset + 8); - - *(long*)(BuffPtr + OutOffs + 0) = Tile0; - *(long*)(BuffPtr + OutOffs + 8) = Tile1; - - OutOffs += 16; - } - } - - return Output; - } - } -} diff --git a/Ryujinx.HLE/Gpu/TextureSwizzle.cs b/Ryujinx.HLE/Gpu/TextureSwizzle.cs deleted file mode 100644 index 5e32f4c7..00000000 --- a/Ryujinx.HLE/Gpu/TextureSwizzle.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu -{ - enum TextureSwizzle - { - _1dBuffer = 0, - PitchColorKey = 1, - Pitch = 2, - BlockLinear = 3, - BlockLinearColorKey = 4 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/TextureWriter.cs b/Ryujinx.HLE/Gpu/TextureWriter.cs deleted file mode 100644 index ad92961c..00000000 --- a/Ryujinx.HLE/Gpu/TextureWriter.cs +++ /dev/null @@ -1,55 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu -{ - static class TextureWriter - { - public static void Write( - IAMemory Memory, - Texture Texture, - byte[] Data, - int Width, - int Height) - { - switch (Texture.Format) - { - case GalTextureFormat.A8B8G8R8: Write4Bpp(Memory, Texture, Data, Width, Height); break; - - default: throw new NotImplementedException(Texture.Format.ToString()); - } - } - - private unsafe static void Write4Bpp( - IAMemory Memory, - Texture Texture, - byte[] Data, - int Width, - int Height) - { - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Data) - { - long InOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - int Pixel = *(int*)(BuffPtr + InOffs); - - CpuMem.WriteInt32Unchecked(Position + Offset, Pixel); - - InOffs += 4; - } - } - } - } -} diff --git a/Ryujinx.HLE/Loaders/Npdm/ACI0.cs b/Ryujinx.HLE/Loaders/Npdm/ACI0.cs index 1f1b810e..47b30a3c 100644 --- a/Ryujinx.HLE/Loaders/Npdm/ACI0.cs +++ b/Ryujinx.HLE/Loaders/Npdm/ACI0.cs @@ -1,4 +1,3 @@ -using Ryujinx.HLE.OsHle.Utilities; using System; using System.IO; @@ -20,7 +19,7 @@ namespace Ryujinx.HLE.Loaders.Npdm public KernelAccessControl KernelAccessControl; public const long ACI0Magic = 'A' << 0 | 'C' << 8 | 'I' << 16 | '0' << 24; - + public ACI0(Stream ACI0Stream, int Offset) { ACI0Stream.Seek(Offset, SeekOrigin.Begin); diff --git a/Ryujinx.HLE/Loaders/Npdm/ACID.cs b/Ryujinx.HLE/Loaders/Npdm/ACID.cs index d0f0acdd..09768a92 100644 --- a/Ryujinx.HLE/Loaders/Npdm/ACID.cs +++ b/Ryujinx.HLE/Loaders/Npdm/ACID.cs @@ -1,4 +1,3 @@ -using Ryujinx.HLE.OsHle.Utilities; using System; using System.IO; @@ -24,7 +23,7 @@ namespace Ryujinx.HLE.Loaders.Npdm public FSAccessControl FSAccessControl; public ServiceAccessControl ServiceAccessControl; public KernelAccessControl KernelAccessControl; - + public const long ACIDMagic = 'A' << 0 | 'C' << 8 | 'I' << 16 | 'D' << 24; public ACID(Stream ACIDStream, int Offset) diff --git a/Ryujinx.HLE/Loaders/Npdm/Npdm.cs b/Ryujinx.HLE/Loaders/Npdm/Npdm.cs index d255e668..eaa662f0 100644 --- a/Ryujinx.HLE/Loaders/Npdm/Npdm.cs +++ b/Ryujinx.HLE/Loaders/Npdm/Npdm.cs @@ -1,6 +1,4 @@ using Ryujinx.HLE.OsHle.Utilities; -using System; -using System.Collections.Generic; using System.IO; using System.Text; @@ -29,7 +27,7 @@ namespace Ryujinx.HLE.Loaders.Npdm public ACI0 ACI0; public ACID ACID; - + public const long NpdmMagic = 'M' << 0 | 'E' << 8 | 'T' << 16 | 'A' << 24; public Npdm(Stream NPDMStream) @@ -61,7 +59,7 @@ namespace Ryujinx.HLE.Loaders.Npdm // ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here. ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32()); - // Main entrypoint stack size + // Main entrypoint stack size // (Should(?) be page-aligned. In non-nspwn scenarios, values of 0 can also rarely break in Horizon. // This might be something auto-adapting or a security feature of some sort ?) MainEntrypointStackSize = Reader.ReadInt32(); diff --git a/Ryujinx.HLE/Loaders/Npdm/ServiceAccessControl.cs b/Ryujinx.HLE/Loaders/Npdm/ServiceAccessControl.cs index fd33841a..ddd7d7ed 100644 --- a/Ryujinx.HLE/Loaders/Npdm/ServiceAccessControl.cs +++ b/Ryujinx.HLE/Loaders/Npdm/ServiceAccessControl.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Text; @@ -25,7 +24,7 @@ namespace Ryujinx.HLE.Loaders.Npdm int Length = ((ControlByte & 0x07)) + 1; bool RegisterAllowed = ((ControlByte & 0x80) != 0); - + Services.Add((Encoding.ASCII.GetString(Reader.ReadBytes(Length), 0, Length), RegisterAllowed)); ByteReaded += Length + 1; diff --git a/Ryujinx.HLE/OsHle/Services/Nv/NvGpuAS/NvGpuASIoctl.cs b/Ryujinx.HLE/OsHle/Services/Nv/NvGpuAS/NvGpuASIoctl.cs index c96c04c8..fcc478a4 100644 --- a/Ryujinx.HLE/OsHle/Services/Nv/NvGpuAS/NvGpuASIoctl.cs +++ b/Ryujinx.HLE/OsHle/Services/Nv/NvGpuAS/NvGpuASIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu; +using Ryujinx.HLE.Gpu.Memory; using Ryujinx.HLE.Logging; using Ryujinx.HLE.OsHle.Services.Nv.NvMap; using System; diff --git a/Ryujinx.HLE/OsHle/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs b/Ryujinx.HLE/OsHle/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs index c461fa28..8f3d3cd7 100644 --- a/Ryujinx.HLE/OsHle/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs +++ b/Ryujinx.HLE/OsHle/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu; +using Ryujinx.HLE.Gpu.Memory; using Ryujinx.HLE.Logging; using Ryujinx.HLE.OsHle.Services.Nv.NvGpuAS; using System; diff --git a/Ryujinx.HLE/OsHle/Services/Nv/NvMap/NvMapIoctl.cs b/Ryujinx.HLE/OsHle/Services/Nv/NvMap/NvMapIoctl.cs index 43de4edb..ec10a375 100644 --- a/Ryujinx.HLE/OsHle/Services/Nv/NvMap/NvMapIoctl.cs +++ b/Ryujinx.HLE/OsHle/Services/Nv/NvMap/NvMapIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu; +using Ryujinx.HLE.Gpu.Memory; using Ryujinx.HLE.Logging; using Ryujinx.HLE.OsHle.Utilities; using System.Collections.Concurrent; diff --git a/Ryujinx.HLE/OsHle/Services/Vi/NvFlinger.cs b/Ryujinx.HLE/OsHle/Services/Vi/NvFlinger.cs index b45dac6b..a3ed3ab5 100644 --- a/Ryujinx.HLE/OsHle/Services/Vi/NvFlinger.cs +++ b/Ryujinx.HLE/OsHle/Services/Vi/NvFlinger.cs @@ -1,5 +1,5 @@ using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu; +using Ryujinx.HLE.Gpu.Texture; using Ryujinx.HLE.Logging; using Ryujinx.HLE.OsHle.Handles; using Ryujinx.HLE.OsHle.Services.Nv.NvMap; @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; + using static Ryujinx.HLE.OsHle.Services.Android.Parcel; namespace Ryujinx.HLE.OsHle.Services.Android @@ -339,7 +340,7 @@ namespace Ryujinx.HLE.OsHle.Services.Android Rotate = -MathF.PI * 0.5f; } - Renderer.SetFrameBufferTransform(ScaleX, ScaleY, Rotate, OffsX, OffsY); + Renderer.QueueAction(() => Renderer.FrameBuffer.SetTransform(ScaleX, ScaleY, Rotate, OffsX, OffsY)); //TODO: Support double buffering here aswell, it is broken for GPU //frame buffers because it seems to be completely out of sync. @@ -347,17 +348,17 @@ namespace Ryujinx.HLE.OsHle.Services.Android { //Frame buffer is rendered to by the GPU, we can just //bind the frame buffer texture, it's not necessary to read anything. - Renderer.SetFrameBuffer(FbAddr); + Renderer.QueueAction(() => Renderer.FrameBuffer.Set(FbAddr)); } else { //Frame buffer is not set on the GPU registers, in this case //assume that the app is manually writing to it. - Texture Texture = new Texture(FbAddr, FbWidth, FbHeight); + TextureInfo Texture = new TextureInfo(FbAddr, FbWidth, FbHeight); byte[] Data = TextureReader.Read(Context.Memory, Texture); - Renderer.SetFrameBuffer(Data, FbWidth, FbHeight); + Renderer.QueueAction(() => Renderer.FrameBuffer.Set(Data, FbWidth, FbHeight)); } Context.Ns.Gpu.Renderer.QueueAction(() => ReleaseBuffer(Slot)); diff --git a/Ryujinx.HLE/PerformanceStatistics.cs b/Ryujinx.HLE/PerformanceStatistics.cs index bbcdc645..9bc3d6b4 100644 --- a/Ryujinx.HLE/PerformanceStatistics.cs +++ b/Ryujinx.HLE/PerformanceStatistics.cs @@ -1,84 +1,119 @@ using System.Diagnostics; using System.Timers; + namespace Ryujinx.HLE { public class PerformanceStatistics { - Stopwatch ExecutionTime = new Stopwatch(); - Timer ResetTimer = new Timer(1000); - - long CurrentGameFrameEnded; - long CurrentSystemFrameEnded; - long CurrentSystemFrameStart; - long LastGameFrameEnded; - long LastSystemFrameEnded; - - double AccumulatedGameFrameTime; - double AccumulatedSystemFrameTime; - double CurrentGameFrameTime; - double CurrentSystemFrameTime; - double PreviousGameFrameTime; - double PreviousSystemFrameTime; - public double GameFrameRate { get; private set; } - public double SystemFrameRate { get; private set; } - public long SystemFramesRendered; - public long GameFramesRendered; - public long ElapsedMilliseconds => ExecutionTime.ElapsedMilliseconds; - public long ElapsedMicroseconds => (long) - (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000); - public long ElapsedNanoseconds => (long) - (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000000); + private const double FrameRateWeight = 0.5; + + private const int FrameTypeSystem = 0; + private const int FrameTypeGame = 1; + + private double[] AverageFrameRate; + private double[] AccumulatedFrameTime; + private double[] PreviousFrameTime; + + private long[] FramesRendered; + + private object[] FrameLock; + + private double TicksToSeconds; + + private Stopwatch ExecutionTime; + + private Timer ResetTimer; public PerformanceStatistics() { + AverageFrameRate = new double[2]; + AccumulatedFrameTime = new double[2]; + PreviousFrameTime = new double[2]; + + FramesRendered = new long[2]; + + FrameLock = new object[] { new object(), new object() }; + + ExecutionTime = new Stopwatch(); + ExecutionTime.Start(); + + ResetTimer = new Timer(1000); + ResetTimer.Elapsed += ResetTimerElapsed; + ResetTimer.AutoReset = true; + ResetTimer.Start(); + + TicksToSeconds = 1.0 / Stopwatch.Frequency; } private void ResetTimerElapsed(object sender, ElapsedEventArgs e) { - ResetStatistics(); + CalculateAverageFrameRate(FrameTypeSystem); + CalculateAverageFrameRate(FrameTypeGame); } - public void StartSystemFrame() + private void CalculateAverageFrameRate(int FrameType) { - PreviousSystemFrameTime = CurrentSystemFrameTime; - LastSystemFrameEnded = CurrentSystemFrameEnded; - CurrentSystemFrameStart = ElapsedMicroseconds; + double FrameRate = 0; + + if (AccumulatedFrameTime[FrameType] > 0) + { + FrameRate = FramesRendered[FrameType] / AccumulatedFrameTime[FrameType]; + } + + lock (FrameLock[FrameType]) + { + AverageFrameRate[FrameType] = LinearInterpolate(AverageFrameRate[FrameType], FrameRate); + + FramesRendered[FrameType] = 0; + + AccumulatedFrameTime[FrameType] = 0; + } } - public void EndSystemFrame() + private double LinearInterpolate(double Old, double New) { - CurrentSystemFrameEnded = ElapsedMicroseconds; - CurrentSystemFrameTime = CurrentSystemFrameEnded - CurrentSystemFrameStart; - AccumulatedSystemFrameTime += CurrentSystemFrameTime; - SystemFramesRendered++; + return Old * (1.0 - FrameRateWeight) + New * FrameRateWeight; + } + + public void RecordSystemFrameTime() + { + RecordFrameTime(FrameTypeSystem); } public void RecordGameFrameTime() { - CurrentGameFrameEnded = ElapsedMicroseconds; - CurrentGameFrameTime = CurrentGameFrameEnded - LastGameFrameEnded; - PreviousGameFrameTime = CurrentGameFrameTime; - LastGameFrameEnded = CurrentGameFrameEnded; - AccumulatedGameFrameTime += CurrentGameFrameTime; - GameFramesRendered++; + RecordFrameTime(FrameTypeGame); + } + + private void RecordFrameTime(int FrameType) + { + double CurrentFrameTime = ExecutionTime.ElapsedTicks * TicksToSeconds; + + double ElapsedFrameTime = CurrentFrameTime - PreviousFrameTime[FrameType]; + + PreviousFrameTime[FrameType] = CurrentFrameTime; + + lock (FrameLock[FrameType]) + { + AccumulatedFrameTime[FrameType] += ElapsedFrameTime; + + FramesRendered[FrameType]++; + } + } + + public double GetSystemFrameRate() + { + return AverageFrameRate[FrameTypeSystem]; } - public void ResetStatistics() + public double GetGameFrameRate() { - GameFrameRate = 1000 / ((AccumulatedGameFrameTime / GameFramesRendered) / 1000); - GameFrameRate = double.IsNaN(GameFrameRate) ? 0 : GameFrameRate; - SystemFrameRate = 1000 / ((AccumulatedSystemFrameTime / SystemFramesRendered) / 1000); - SystemFrameRate = double.IsNaN(SystemFrameRate) ? 0 : SystemFrameRate; - - GameFramesRendered = 0; - SystemFramesRendered = 0; - AccumulatedGameFrameTime = 0; - AccumulatedSystemFrameTime = 0; + return AverageFrameRate[FrameTypeGame]; } } } diff --git a/Ryujinx.HLE/Switch.cs b/Ryujinx.HLE/Switch.cs index f75b2490..f7b263cd 100644 --- a/Ryujinx.HLE/Switch.cs +++ b/Ryujinx.HLE/Switch.cs @@ -71,6 +71,11 @@ namespace Ryujinx.HLE Os.LoadProgram(FileName); } + public void ProcessFrame() + { + Gpu.Fifo.DispatchCalls(); + } + internal virtual void OnFinish(EventArgs e) { Finish?.Invoke(this, e); diff --git a/Ryujinx/Ui/GLScreen.cs b/Ryujinx/Ui/GLScreen.cs index e2024b5b..ab5eaa0f 100644 --- a/Ryujinx/Ui/GLScreen.cs +++ b/Ryujinx/Ui/GLScreen.cs @@ -23,7 +23,7 @@ namespace Ryujinx private KeyboardState? Keyboard = null; private MouseState? Mouse = null; - + public GLScreen(Switch Ns, IGalRenderer Renderer) : base(1280, 720, new GraphicsMode(), "Ryujinx", 0, @@ -42,7 +42,7 @@ namespace Ryujinx { VSync = VSyncMode.On; - Renderer.SetWindowSize(Width, Height); + Renderer.FrameBuffer.SetWindowSize(Width, Height); } protected override void OnUpdateFrame(FrameEventArgs e) @@ -55,7 +55,7 @@ namespace Ryujinx int LeftJoystickDY = 0; int RightJoystickDX = 0; int RightJoystickDY = 0; - + if (Keyboard.HasValue) { KeyboardState Keyboard = this.Keyboard.Value; @@ -83,7 +83,7 @@ namespace Ryujinx if (Keyboard[(Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue; if (Keyboard[(Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue; if (Keyboard[(Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue; - + //RightButtons if (Keyboard[(Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK; if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A; @@ -179,28 +179,31 @@ namespace Ryujinx CurrentButton, LeftJoystick, RightJoystick); + + Ns.ProcessFrame(); + + Renderer.RunActions(); } protected override void OnRenderFrame(FrameEventArgs e) { - Ns.Statistics.StartSystemFrame(); + Renderer.FrameBuffer.Render(); - Title = $"Ryujinx Screen - (Vsync: {VSync} - FPS: {Ns.Statistics.SystemFrameRate:0} - Guest FPS: " + - $"{Ns.Statistics.GameFrameRate:0})"; + Ns.Statistics.RecordSystemFrameTime(); - Renderer.RunActions(); - Renderer.Render(); + double HostFps = Ns.Statistics.GetSystemFrameRate(); + double GameFps = Ns.Statistics.GetGameFrameRate(); - SwapBuffers(); + Title = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}"; - Ns.Statistics.EndSystemFrame(); + SwapBuffers(); Ns.Os.SignalVsync(); } protected override void OnResize(EventArgs e) { - Renderer.SetWindowSize(Width, Height); + Renderer.FrameBuffer.SetWindowSize(Width, Height); } protected override void OnKeyDown(KeyboardKeyEventArgs e) diff --git a/Ryujinx/Ui/Program.cs b/Ryujinx/Ui/Program.cs index 3bb16faf..b1489769 100644 --- a/Ryujinx/Ui/Program.cs +++ b/Ryujinx/Ui/Program.cs @@ -14,7 +14,7 @@ namespace Ryujinx { Console.Title = "Ryujinx Console"; - IGalRenderer Renderer = new OpenGLRenderer(); + IGalRenderer Renderer = new OGLRenderer(); IAalOutput AudioOut = new OpenALAudioOut(); @@ -67,7 +67,7 @@ namespace Ryujinx Screen.Exit(); }; - Screen.Run(60.0); + Screen.Run(0.0, 60.0); } Environment.Exit(0); -- cgit v1.2.3