From 6e9bd4de138e6ddedef3d38d711d161a0e400d5c Mon Sep 17 00:00:00 2001
From: riperiperi <rhy3756547@hotmail.com>
Date: Sun, 12 Mar 2023 17:01:15 +0000
Subject: GPU: Scale counter results before addition (#4471)

* GPU: Scale counter results before addition

Counter results were being scaled on ReportCounter, which meant that the _total_ value of the counter was being scaled. Not only could this result in very large numbers and weird overflows if the game doesn't clear the counter, but it also caused the result to change drastically.

This PR changes scaling to be done when the value is added to the counter on the backend. This should evaluate the scale at the same time as before, on report counter, but avoiding the issue with scaling the total.

Fixes scaling in Warioware, at least in the demo, where it seems to compare old/new counters and broke down when scaling was enabled.

* Fix issues when result is partially uploaded.

Drivers tend to write the low half first, then the high half. Retry if the high half is FFFFFFFF.
---
 Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

(limited to 'Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs')

diff --git a/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs b/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs
index 84b2bfdc..e0aafa6f 100644
--- a/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs
+++ b/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs
@@ -13,6 +13,8 @@ namespace Ryujinx.Graphics.OpenGL.Queries
         public CounterType Type { get; }
         public bool Disposed { get; private set; }
 
+        private readonly Pipeline _pipeline;
+
         private Queue<CounterQueueEvent> _events = new Queue<CounterQueueEvent>();
         private CounterQueueEvent _current;
 
@@ -28,10 +30,12 @@ namespace Ryujinx.Graphics.OpenGL.Queries
 
         private Thread _consumerThread;
 
-        internal CounterQueue(CounterType type)
+        internal CounterQueue(Pipeline pipeline, CounterType type)
         {
             Type = type;
 
+            _pipeline = pipeline;
+
             QueryTarget glType = GetTarget(Type);
 
             _queryPool = new Queue<BufferedQuery>(QueryPoolInitialSize);
@@ -119,7 +123,7 @@ namespace Ryujinx.Graphics.OpenGL.Queries
                     _current.ReserveForHostAccess();
                 }
 
-                _current.Complete(draws > 0);
+                _current.Complete(draws > 0, _pipeline.GetCounterDivisor(Type));
                 _events.Enqueue(_current);
 
                 _current.OnResult += resultHandler;
-- 
cgit v1.2.3-70-g09d2