MethodConditionalRendering.cs 4.7 KB

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