MethodConditionalRendering.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.State;
  4. namespace Ryujinx.Graphics.Gpu.Engine
  5. {
  6. partial class Methods
  7. {
  8. enum ConditionalRenderEnabled
  9. {
  10. False,
  11. True,
  12. Host
  13. }
  14. /// <summary>
  15. /// Checks if draws and clears should be performed, according
  16. /// to currently set conditional rendering conditions.
  17. /// </summary>
  18. /// <param name="state">GPU state</param>
  19. /// <returns>True if rendering is enabled, false otherwise</returns>
  20. private ConditionalRenderEnabled GetRenderEnable(GpuState state)
  21. {
  22. ConditionState condState = state.Get<ConditionState>(MethodOffset.ConditionState);
  23. switch (condState.Condition)
  24. {
  25. case Condition.Always:
  26. return ConditionalRenderEnabled.True;
  27. case Condition.Never:
  28. return ConditionalRenderEnabled.False;
  29. case Condition.ResultNonZero:
  30. return CounterNonZero(condState.Address.Pack());
  31. case Condition.Equal:
  32. return CounterCompare(condState.Address.Pack(), true);
  33. case Condition.NotEqual:
  34. return CounterCompare(condState.Address.Pack(), false);
  35. }
  36. Logger.PrintWarning(LogClass.Gpu, $"Invalid conditional render condition \"{condState.Condition}\".");
  37. return ConditionalRenderEnabled.True;
  38. }
  39. /// <summary>
  40. /// Checks if the counter value at a given GPU memory address is non-zero.
  41. /// </summary>
  42. /// <param name="gpuVa">GPU virtual address of the counter value</param>
  43. /// <returns>True if the value is not zero, false otherwise. Returns host if handling with host conditional rendering</returns>
  44. private ConditionalRenderEnabled CounterNonZero(ulong gpuVa)
  45. {
  46. ICounterEvent evt = _counterCache.FindEvent(gpuVa);
  47. if (evt == null)
  48. {
  49. return ConditionalRenderEnabled.False;
  50. }
  51. if (_context.Renderer.Pipeline.TryHostConditionalRendering(evt, 0L, false))
  52. {
  53. return ConditionalRenderEnabled.Host;
  54. }
  55. else
  56. {
  57. evt.Flush();
  58. return (_context.MemoryManager.Read<ulong>(gpuVa) != 0) ? ConditionalRenderEnabled.True : ConditionalRenderEnabled.False;
  59. }
  60. }
  61. /// <summary>
  62. /// Checks if the counter at a given GPU memory address passes a specified equality comparison.
  63. /// </summary>
  64. /// <param name="gpuVa">GPU virtual address</param>
  65. /// <param name="isEqual">True to check if the values are equal, false to check if they are not equal</param>
  66. /// <returns>True if the condition is met, false otherwise. Returns host if handling with host conditional rendering</returns>
  67. private ConditionalRenderEnabled CounterCompare(ulong gpuVa, bool isEqual)
  68. {
  69. ICounterEvent evt = FindEvent(gpuVa);
  70. ICounterEvent evt2 = FindEvent(gpuVa + 16);
  71. if (evt == null && evt2 == null)
  72. {
  73. return ConditionalRenderEnabled.False;
  74. }
  75. bool useHost;
  76. if (evt != null && evt2 == null)
  77. {
  78. useHost = _context.Renderer.Pipeline.TryHostConditionalRendering(evt, _context.MemoryManager.Read<ulong>(gpuVa + 16), isEqual);
  79. }
  80. else if (evt == null && evt2 != null)
  81. {
  82. useHost = _context.Renderer.Pipeline.TryHostConditionalRendering(evt2, _context.MemoryManager.Read<ulong>(gpuVa), isEqual);
  83. }
  84. else
  85. {
  86. useHost = _context.Renderer.Pipeline.TryHostConditionalRendering(evt, evt2, isEqual);
  87. }
  88. if (useHost)
  89. {
  90. return ConditionalRenderEnabled.Host;
  91. }
  92. else
  93. {
  94. evt?.Flush();
  95. evt2?.Flush();
  96. ulong x = _context.MemoryManager.Read<ulong>(gpuVa);
  97. ulong y = _context.MemoryManager.Read<ulong>(gpuVa + 16);
  98. return (isEqual ? x == y : x != y) ? ConditionalRenderEnabled.True : ConditionalRenderEnabled.False;
  99. }
  100. }
  101. /// <summary>
  102. /// Tries to find a counter that is supposed to be written at the specified address,
  103. /// returning the related event.
  104. /// </summary>
  105. /// <param name="gpuVa">GPU virtual address where the counter is supposed to be written</param>
  106. /// <returns>The counter event, or null if not present</returns>
  107. private ICounterEvent FindEvent(ulong gpuVa)
  108. {
  109. return _counterCache.FindEvent(gpuVa);
  110. }
  111. }
  112. }