ConditionalRendering.cs 5.7 KB

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