MethodConditionalRendering.cs 5.1 KB

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