From 09c9686498c7c987c94f33c79732c7592045e035 Mon Sep 17 00:00:00 2001
From: Mary-nyan <mary@mary.zone>
Date: Mon, 2 Jan 2023 15:48:46 +0100
Subject: [PATCH] misc: Use official names for NVDEC registers (#4192)

* misc: Uses official names for NVDEC registers

* Address gdkchan's comment

* Address comments
---
 Ryujinx.Graphics.Host1x/ThiDevice.cs          |  2 +-
 .../{CodecId.cs => ApplicationId.cs}          |  5 +-
 .../FrameDecodedEventArgs.cs                  | 16 ----
 Ryujinx.Graphics.Nvdec/H264Decoder.cs         |  8 +-
 Ryujinx.Graphics.Nvdec/NvdecDevice.cs         | 14 ++--
 Ryujinx.Graphics.Nvdec/NvdecRegisters.cs      | 79 ++++++++++++-------
 Ryujinx.Graphics.Nvdec/Vp8Decoder.cs          |  8 +-
 Ryujinx.Graphics.Nvdec/Vp9Decoder.cs          | 24 +++---
 8 files changed, 80 insertions(+), 76 deletions(-)
 rename Ryujinx.Graphics.Nvdec/{CodecId.cs => ApplicationId.cs} (68%)
 delete mode 100644 Ryujinx.Graphics.Nvdec/FrameDecodedEventArgs.cs

diff --git a/Ryujinx.Graphics.Host1x/ThiDevice.cs b/Ryujinx.Graphics.Host1x/ThiDevice.cs
index 114ee26e5..259c88366 100644
--- a/Ryujinx.Graphics.Host1x/ThiDevice.cs
+++ b/Ryujinx.Graphics.Host1x/ThiDevice.cs
@@ -106,7 +106,7 @@ namespace Ryujinx.Graphics.Host1x
 
         private void Method1(int data)
         {
-            _commandQueue.Add(new MethodCallAction(_currentContextId, (int)_state.State.Method0 * 4, data));
+            _commandQueue.Add(new MethodCallAction(_currentContextId, (int)_state.State.Method0 * sizeof(uint), data));
         }
 
         private void Process(CommandAction cmdAction)
diff --git a/Ryujinx.Graphics.Nvdec/CodecId.cs b/Ryujinx.Graphics.Nvdec/ApplicationId.cs
similarity index 68%
rename from Ryujinx.Graphics.Nvdec/CodecId.cs
rename to Ryujinx.Graphics.Nvdec/ApplicationId.cs
index 9aaa3d023..ada12f8d3 100644
--- a/Ryujinx.Graphics.Nvdec/CodecId.cs
+++ b/Ryujinx.Graphics.Nvdec/ApplicationId.cs
@@ -1,6 +1,6 @@
 namespace Ryujinx.Graphics.Nvdec
 {
-    public enum CodecId
+    public enum ApplicationId
     {
         Mpeg = 1,
         Vc1 = 2,
@@ -8,6 +8,7 @@
         Mpeg4 = 4,
         Vp8 = 5,
         Hevc = 7,
-        Vp9 = 9
+        Vp9 = 9,
+        HevcParser = 12,
     }
 }
diff --git a/Ryujinx.Graphics.Nvdec/FrameDecodedEventArgs.cs b/Ryujinx.Graphics.Nvdec/FrameDecodedEventArgs.cs
deleted file mode 100644
index 4ee29d9d0..000000000
--- a/Ryujinx.Graphics.Nvdec/FrameDecodedEventArgs.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-namespace Ryujinx.Graphics.Nvdec
-{
-    public readonly struct FrameDecodedEventArgs
-    {
-        public CodecId CodecId { get; }
-        public uint LumaOffset { get; }
-        public uint ChromaOffset { get; }
-
-        internal FrameDecodedEventArgs(CodecId codecId, uint lumaOffset, uint chromaOffset)
-        {
-            CodecId = codecId;
-            LumaOffset = lumaOffset;
-            ChromaOffset = chromaOffset;
-        }
-    }
-}
diff --git a/Ryujinx.Graphics.Nvdec/H264Decoder.cs b/Ryujinx.Graphics.Nvdec/H264Decoder.cs
index 6efeb899e..ecc7dbc79 100644
--- a/Ryujinx.Graphics.Nvdec/H264Decoder.cs
+++ b/Ryujinx.Graphics.Nvdec/H264Decoder.cs
@@ -12,18 +12,18 @@ namespace Ryujinx.Graphics.Nvdec
 
         public static void Decode(NvdecDecoderContext context, ResourceManager rm, ref NvdecRegisters state)
         {
-            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetPictureInfoOffset);
+            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetDrvPicSetupOffset);
             H264PictureInfo info = pictureInfo.Convert();
 
-            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetBitstreamOffset, (int)pictureInfo.BitstreamSize);
+            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetInBufBaseOffset, (int)pictureInfo.BitstreamSize);
 
             int width  = (int)pictureInfo.PicWidthInMbs * MbSizeInPixels;
             int height = (int)pictureInfo.PicHeightInMbs * MbSizeInPixels;
 
             int surfaceIndex = (int)pictureInfo.OutputSurfaceIndex;
 
-            uint lumaOffset   = state.SetSurfaceLumaOffset[surfaceIndex];
-            uint chromaOffset = state.SetSurfaceChromaOffset[surfaceIndex];
+            uint lumaOffset   = state.SetPictureLumaOffset[surfaceIndex];
+            uint chromaOffset = state.SetPictureChromaOffset[surfaceIndex];
 
             Decoder decoder = context.GetH264Decoder();
 
diff --git a/Ryujinx.Graphics.Nvdec/NvdecDevice.cs b/Ryujinx.Graphics.Nvdec/NvdecDevice.cs
index 18c2fc130..ef8185f4d 100644
--- a/Ryujinx.Graphics.Nvdec/NvdecDevice.cs
+++ b/Ryujinx.Graphics.Nvdec/NvdecDevice.cs
@@ -58,24 +58,24 @@ namespace Ryujinx.Graphics.Nvdec
 
         private void Execute(int data)
         {
-            Decode((CodecId)_state.State.SetCodecID);
+            Decode((ApplicationId)_state.State.SetApplicationId);
         }
 
-        private void Decode(CodecId codecId)
+        private void Decode(ApplicationId applicationId)
         {
-            switch (codecId)
+            switch (applicationId)
             {
-                case CodecId.H264:
+                case ApplicationId.H264:
                     H264Decoder.Decode(_currentContext, _rm, ref _state.State);
                     break;
-                case CodecId.Vp8:
+                case ApplicationId.Vp8:
                     Vp8Decoder.Decode(_currentContext, _rm, ref _state.State);
                     break;
-                case CodecId.Vp9:
+                case ApplicationId.Vp9:
                     Vp9Decoder.Decode(_rm, ref _state.State);
                     break;
                 default:
-                    Logger.Error?.Print(LogClass.Nvdec, $"Unsupported codec \"{codecId}\".");
+                    Logger.Error?.Print(LogClass.Nvdec, $"Unsupported codec \"{applicationId}\".");
                     break;
             }
         }
diff --git a/Ryujinx.Graphics.Nvdec/NvdecRegisters.cs b/Ryujinx.Graphics.Nvdec/NvdecRegisters.cs
index 84b2c4e07..cf8677838 100644
--- a/Ryujinx.Graphics.Nvdec/NvdecRegisters.cs
+++ b/Ryujinx.Graphics.Nvdec/NvdecRegisters.cs
@@ -2,43 +2,62 @@
 
 namespace Ryujinx.Graphics.Nvdec
 {
-    // Note: Most of those names are not official.
     struct NvdecRegisters
     {
 #pragma warning disable CS0649
         public Array64<uint> Reserved0;
-        public Array64<uint> Reserved100;
-        public uint SetCodecID;
-        public Array63<uint> Reserved204;
+        public uint Nop;
+        public Array63<uint> Reserved104;
+        public uint SetApplicationId;
+        public uint SetWatchdogTimer;
+        public Array14<uint> Reserved208;
+        public uint SemaphoreA;
+        public uint SemaphoreB;
+        public uint SemaphoreC;
+        public uint CtxSaveArea;
+        public Array44<uint> Reserved254;
         public uint Execute;
-        public Array63<uint> Reserved304;
-        public uint SetPlatformID;
-        public uint SetPictureInfoOffset;
-        public uint SetBitstreamOffset;
-        public uint SetFrameNumber;
-        public uint SetH264SliceDataOffsetsOffset; // Also used by VC1
-        public uint SetH264MvDumpOffset; // Also used by VC1
-        public uint Unknown418; // Used by VC1
-        public uint Unknown41C;
-        public uint Unknown420; // Used by VC1
-        public uint SetFrameStatsOffset;
-        public uint SetH264LastSurfaceLumaOffset;
-        public uint SetH264LastSurfaceChromaOffset;
-        public Array17<uint> SetSurfaceLumaOffset;
-        public Array17<uint> SetSurfaceChromaOffset;
-        public uint Unknown4B8;
-        public uint Unknown4BC;
+        public uint SemaphoreD;
+        public Array62<uint> Reserved308;
+        public uint SetControlParams;
+        public uint SetDrvPicSetupOffset;
+        public uint SetInBufBaseOffset;
+        public uint SetPictureIndex;
+        public uint SetSliceOffsetsBufOffset; // Also used by VC1
+        public uint SetColocDataOffset; // Also used by VC1
+        public uint SetHistoryOffset; // Used by VC1
+        public uint SetDisplayBufSize;
+        public uint SetHistogramOffset; // Used by VC1
+        public uint SetNvDecStatusOffset;
+        public uint SetDisplayBufLumaOffset;
+        public uint SetDisplayBufChromaOffset;
+        public Array17<uint> SetPictureLumaOffset;
+        public Array17<uint> SetPictureChromaOffset;
+        public uint SetPicScratchBufOffset;
+        public uint SetExternalMvBufferOffset;
         public uint SetCryptoData0Offset;
         public uint SetCryptoData1Offset;
-        public Array62<uint> Unknown4C8;
-        public uint SetVp9EntropyProbsOffset;
-        public uint SetVp9BackwardUpdatesOffset;
-        public uint SetVp9LastFrameSegMapOffset;
-        public uint SetVp9CurrFrameSegMapOffset;
-        public uint Unknown5D0;
-        public uint SetVp9LastFrameMvsOffset;
-        public uint SetVp9CurrFrameMvsOffset;
-        public uint Unknown5DC;
+        public Array14<uint> Unknown4C8;
+        public uint H264SetMbHistBufOffset;
+        public Array15<uint> Unknown504;
+        public uint Vp8SetProbDataOffset;
+        public uint Vp8SetHeaderPartitionBufBaseOffset;
+        public Array14<uint> Unknown548;
+        public uint HevcSetScalingListOffset;
+        public uint HevcSetTileSizesOffset;
+        public uint HevcSetFilterBufferOffset;
+        public uint HevcSetSaoBufferOffset;
+        public uint HevcSetSliceInfoBufferOffset;
+        public uint HevcSetSliceGroupIndex;
+        public Array10<uint> Unknown598;
+        public uint Vp9SetProbTabBufOffset;
+        public uint Vp9SetCtxCounterBufOffset;
+        public uint Vp9SetSegmentReadBufOffset;
+        public uint Vp9SetSegmentWriteBufOffset;
+        public uint Vp9SetTileSizeBufOffset;
+        public uint Vp9SetColMvWriteBufOffset;
+        public uint Vp9SetColMvReadBufOffset;
+        public uint Vp9SetFilterBufferOffset;
 #pragma warning restore CS0649
     }
 }
diff --git a/Ryujinx.Graphics.Nvdec/Vp8Decoder.cs b/Ryujinx.Graphics.Nvdec/Vp8Decoder.cs
index 8a369984e..cce9a5744 100644
--- a/Ryujinx.Graphics.Nvdec/Vp8Decoder.cs
+++ b/Ryujinx.Graphics.Nvdec/Vp8Decoder.cs
@@ -10,8 +10,8 @@ namespace Ryujinx.Graphics.Nvdec
     {
         public static void Decode(NvdecDecoderContext context, ResourceManager rm, ref NvdecRegisters state)
         {
-            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetPictureInfoOffset);
-            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetBitstreamOffset, (int)pictureInfo.VLDBufferSize);
+            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetDrvPicSetupOffset);
+            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetInBufBaseOffset, (int)pictureInfo.VLDBufferSize);
 
             Decoder decoder = context.GetVp8Decoder();
 
@@ -19,8 +19,8 @@ namespace Ryujinx.Graphics.Nvdec
 
             Vp8PictureInfo info = pictureInfo.Convert();
 
-            uint lumaOffset = state.SetSurfaceLumaOffset[3];
-            uint chromaOffset = state.SetSurfaceChromaOffset[3];
+            uint lumaOffset = state.SetPictureLumaOffset[3];
+            uint chromaOffset = state.SetPictureChromaOffset[3];
 
             if (decoder.Decode(ref info, outputSurface, bitstream))
             {
diff --git a/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs b/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs
index b56dc56ec..9bb3529e8 100644
--- a/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs
+++ b/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs
@@ -17,18 +17,18 @@ namespace Ryujinx.Graphics.Nvdec
 
         public unsafe static void Decode(ResourceManager rm, ref NvdecRegisters state)
         {
-            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetPictureInfoOffset);
-            EntropyProbs entropy = rm.Gmm.DeviceRead<EntropyProbs>(state.SetVp9EntropyProbsOffset);
+            PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetDrvPicSetupOffset);
+            EntropyProbs entropy = rm.Gmm.DeviceRead<EntropyProbs>(state.Vp9SetProbTabBufOffset);
 
             ISurface Rent(uint lumaOffset, uint chromaOffset, FrameSize size)
             {
                 return rm.Cache.Get(_decoder, lumaOffset, chromaOffset, size.Width, size.Height);
             }
 
-            ISurface lastSurface    = Rent(state.SetSurfaceLumaOffset[0], state.SetSurfaceChromaOffset[0], pictureInfo.LastFrameSize);
-            ISurface goldenSurface  = Rent(state.SetSurfaceLumaOffset[1], state.SetSurfaceChromaOffset[1], pictureInfo.GoldenFrameSize);
-            ISurface altSurface     = Rent(state.SetSurfaceLumaOffset[2], state.SetSurfaceChromaOffset[2], pictureInfo.AltFrameSize);
-            ISurface currentSurface = Rent(state.SetSurfaceLumaOffset[3], state.SetSurfaceChromaOffset[3], pictureInfo.CurrentFrameSize);
+            ISurface lastSurface    = Rent(state.SetPictureLumaOffset[0], state.SetPictureChromaOffset[0], pictureInfo.LastFrameSize);
+            ISurface goldenSurface  = Rent(state.SetPictureLumaOffset[1], state.SetPictureChromaOffset[1], pictureInfo.GoldenFrameSize);
+            ISurface altSurface     = Rent(state.SetPictureLumaOffset[2], state.SetPictureChromaOffset[2], pictureInfo.AltFrameSize);
+            ISurface currentSurface = Rent(state.SetPictureLumaOffset[3], state.SetPictureChromaOffset[3], pictureInfo.CurrentFrameSize);
 
             Vp9PictureInfo info = pictureInfo.Convert();
 
@@ -38,31 +38,31 @@ namespace Ryujinx.Graphics.Nvdec
 
             entropy.Convert(ref info.Entropy);
 
-            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetBitstreamOffset, (int)pictureInfo.BitstreamSize);
+            ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetInBufBaseOffset, (int)pictureInfo.BitstreamSize);
 
             ReadOnlySpan<Vp9MvRef> mvsIn = ReadOnlySpan<Vp9MvRef>.Empty;
 
             if (info.UsePrevInFindMvRefs)
             {
-                mvsIn = GetMvsInput(rm.Gmm, pictureInfo.CurrentFrameSize, state.SetVp9LastFrameMvsOffset);
+                mvsIn = GetMvsInput(rm.Gmm, pictureInfo.CurrentFrameSize, state.Vp9SetColMvReadBufOffset);
             }
 
             int miCols = BitUtils.DivRoundUp(pictureInfo.CurrentFrameSize.Width, 8);
             int miRows = BitUtils.DivRoundUp(pictureInfo.CurrentFrameSize.Height, 8);
 
-            using var mvsRegion = rm.Gmm.GetWritableRegion(ExtendOffset(state.SetVp9CurrFrameMvsOffset), miRows * miCols * 16);
+            using var mvsRegion = rm.Gmm.GetWritableRegion(ExtendOffset(state.Vp9SetColMvWriteBufOffset), miRows * miCols * 16);
 
             Span<Vp9MvRef> mvsOut = MemoryMarshal.Cast<byte, Vp9MvRef>(mvsRegion.Memory.Span);
 
-            uint lumaOffset   = state.SetSurfaceLumaOffset[3];
-            uint chromaOffset = state.SetSurfaceChromaOffset[3];
+            uint lumaOffset   = state.SetPictureLumaOffset[3];
+            uint chromaOffset = state.SetPictureChromaOffset[3];
 
             if (_decoder.Decode(ref info, currentSurface, bitstream, mvsIn, mvsOut))
             {
                 SurfaceWriter.Write(rm.Gmm, currentSurface, lumaOffset, chromaOffset);
             }
 
-            WriteBackwardUpdates(rm.Gmm, state.SetVp9BackwardUpdatesOffset, ref info.BackwardUpdateCounts);
+            WriteBackwardUpdates(rm.Gmm, state.Vp9SetCtxCounterBufOffset, ref info.BackwardUpdateCounts);
 
             rm.Cache.Put(lastSurface);
             rm.Cache.Put(goldenSurface);