Counters.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class Counters
  7. {
  8. private int[] _queryObjects;
  9. private ulong[] _accumulatedCounters;
  10. public Counters()
  11. {
  12. int count = Enum.GetNames(typeof(CounterType)).Length;
  13. _queryObjects = new int[count];
  14. _accumulatedCounters = new ulong[count];
  15. }
  16. public void Initialize()
  17. {
  18. for (int index = 0; index < _queryObjects.Length; index++)
  19. {
  20. int handle = GL.GenQuery();
  21. _queryObjects[index] = handle;
  22. CounterType type = (CounterType)index;
  23. GL.BeginQuery(GetTarget(type), handle);
  24. }
  25. }
  26. public ulong GetCounter(CounterType type)
  27. {
  28. UpdateAccumulatedCounter(type);
  29. return _accumulatedCounters[(int)type];
  30. }
  31. public void ResetCounter(CounterType type)
  32. {
  33. UpdateAccumulatedCounter(type);
  34. _accumulatedCounters[(int)type] = 0;
  35. }
  36. private void UpdateAccumulatedCounter(CounterType type)
  37. {
  38. int handle = _queryObjects[(int)type];
  39. QueryTarget target = GetTarget(type);
  40. GL.EndQuery(target);
  41. GL.GetQueryObject(handle, GetQueryObjectParam.QueryResult, out long result);
  42. _accumulatedCounters[(int)type] += (ulong)result;
  43. GL.BeginQuery(target, handle);
  44. }
  45. private static QueryTarget GetTarget(CounterType type)
  46. {
  47. switch (type)
  48. {
  49. case CounterType.SamplesPassed: return QueryTarget.SamplesPassed;
  50. case CounterType.PrimitivesGenerated: return QueryTarget.PrimitivesGenerated;
  51. case CounterType.TransformFeedbackPrimitivesWritten: return QueryTarget.TransformFeedbackPrimitivesWritten;
  52. }
  53. return QueryTarget.SamplesPassed;
  54. }
  55. }
  56. }