ShaderIdentifier.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  3. namespace Ryujinx.Graphics.Shader.Translation
  4. {
  5. static class ShaderIdentifier
  6. {
  7. public static ShaderIdentification Identify(Function[] functions, ShaderConfig config)
  8. {
  9. if (config.Stage == ShaderStage.Geometry &&
  10. config.GpuAccessor.QueryPrimitiveTopology() == InputTopology.Triangles &&
  11. !config.GpuAccessor.QueryHostSupportsGeometryShader() &&
  12. IsLayerPassthroughGeometryShader(functions, out int layerInputAttr))
  13. {
  14. config.SetGeometryShaderLayerInputAttribute(layerInputAttr);
  15. return ShaderIdentification.GeometryLayerPassthrough;
  16. }
  17. return ShaderIdentification.None;
  18. }
  19. private static bool IsLayerPassthroughGeometryShader(Function[] functions, out int layerInputAttr)
  20. {
  21. bool writesLayer = false;
  22. layerInputAttr = 0;
  23. if (functions.Length != 1)
  24. {
  25. return false;
  26. }
  27. int verticesCount = 0;
  28. int totalVerticesCount = 0;
  29. foreach (BasicBlock block in functions[0].Blocks)
  30. {
  31. // We are not expecting loops or any complex control flow here, so fail in those cases.
  32. if (block.Branch != null && block.Branch.Index <= block.Index)
  33. {
  34. return false;
  35. }
  36. foreach (INode node in block.Operations)
  37. {
  38. if (!(node is Operation operation))
  39. {
  40. continue;
  41. }
  42. if (IsResourceWrite(operation.Inst))
  43. {
  44. return false;
  45. }
  46. if (operation.Inst == Instruction.StoreAttribute)
  47. {
  48. return false;
  49. }
  50. if (operation.Inst == Instruction.Copy && operation.Dest.Type == OperandType.Attribute)
  51. {
  52. Operand src = operation.GetSource(0);
  53. if (src.Type == OperandType.LocalVariable && src.AsgOp is Operation asgOp && asgOp.Inst == Instruction.LoadAttribute)
  54. {
  55. src = Attribute(asgOp.GetSource(0).Value);
  56. }
  57. if (src.Type == OperandType.Attribute)
  58. {
  59. if (operation.Dest.Value == AttributeConsts.Layer)
  60. {
  61. if ((src.Value & AttributeConsts.LoadOutputMask) != 0)
  62. {
  63. return false;
  64. }
  65. writesLayer = true;
  66. layerInputAttr = src.Value;
  67. }
  68. else if (src.Value != operation.Dest.Value)
  69. {
  70. return false;
  71. }
  72. }
  73. else if (src.Type == OperandType.Constant)
  74. {
  75. int dstComponent = (operation.Dest.Value >> 2) & 3;
  76. float expectedValue = dstComponent == 3 ? 1f : 0f;
  77. if (src.AsFloat() != expectedValue)
  78. {
  79. return false;
  80. }
  81. }
  82. else
  83. {
  84. return false;
  85. }
  86. }
  87. else if (operation.Inst == Instruction.EmitVertex)
  88. {
  89. verticesCount++;
  90. }
  91. else if (operation.Inst == Instruction.EndPrimitive)
  92. {
  93. totalVerticesCount += verticesCount;
  94. verticesCount = 0;
  95. }
  96. }
  97. }
  98. return totalVerticesCount + verticesCount == 3 && writesLayer;
  99. }
  100. private static bool IsResourceWrite(Instruction inst)
  101. {
  102. switch (inst)
  103. {
  104. case Instruction.AtomicAdd:
  105. case Instruction.AtomicAnd:
  106. case Instruction.AtomicCompareAndSwap:
  107. case Instruction.AtomicMaxS32:
  108. case Instruction.AtomicMaxU32:
  109. case Instruction.AtomicMinS32:
  110. case Instruction.AtomicMinU32:
  111. case Instruction.AtomicOr:
  112. case Instruction.AtomicSwap:
  113. case Instruction.AtomicXor:
  114. case Instruction.ImageAtomic:
  115. case Instruction.ImageStore:
  116. case Instruction.StoreGlobal:
  117. case Instruction.StoreGlobal16:
  118. case Instruction.StoreGlobal8:
  119. case Instruction.StoreStorage:
  120. case Instruction.StoreStorage16:
  121. case Instruction.StoreStorage8:
  122. return true;
  123. }
  124. return false;
  125. }
  126. }
  127. }