ShaderIdentifier.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Store && operation.StorageKind == StorageKind.Output)
  47. {
  48. Operand src = operation.GetSource(operation.SourcesCount - 1);
  49. Operation srcAttributeAsgOp = null;
  50. if (src.Type == OperandType.LocalVariable &&
  51. src.AsgOp is Operation asgOp &&
  52. asgOp.Inst == Instruction.Load &&
  53. asgOp.StorageKind.IsInputOrOutput())
  54. {
  55. if (asgOp.StorageKind != StorageKind.Input)
  56. {
  57. return false;
  58. }
  59. srcAttributeAsgOp = asgOp;
  60. }
  61. if (srcAttributeAsgOp != null)
  62. {
  63. IoVariable dstAttribute = (IoVariable)operation.GetSource(0).Value;
  64. IoVariable srcAttribute = (IoVariable)srcAttributeAsgOp.GetSource(0).Value;
  65. if (dstAttribute == IoVariable.Layer && srcAttribute == IoVariable.UserDefined)
  66. {
  67. if (srcAttributeAsgOp.SourcesCount != 4)
  68. {
  69. return false;
  70. }
  71. writesLayer = true;
  72. layerInputAttr = srcAttributeAsgOp.GetSource(1).Value * 4 + srcAttributeAsgOp.GetSource(3).Value;;
  73. }
  74. else
  75. {
  76. if (dstAttribute != srcAttribute)
  77. {
  78. return false;
  79. }
  80. int inputsCount = operation.SourcesCount - 2;
  81. if (dstAttribute == IoVariable.UserDefined)
  82. {
  83. if (operation.GetSource(1).Value != srcAttributeAsgOp.GetSource(1).Value)
  84. {
  85. return false;
  86. }
  87. inputsCount--;
  88. }
  89. for (int i = 0; i < inputsCount; i++)
  90. {
  91. int dstIndex = operation.SourcesCount - 2 - i;
  92. int srcIndex = srcAttributeAsgOp.SourcesCount - 1 - i;
  93. if ((dstIndex | srcIndex) < 0)
  94. {
  95. return false;
  96. }
  97. if (operation.GetSource(dstIndex).Type != OperandType.Constant ||
  98. srcAttributeAsgOp.GetSource(srcIndex).Type != OperandType.Constant ||
  99. operation.GetSource(dstIndex).Value != srcAttributeAsgOp.GetSource(srcIndex).Value)
  100. {
  101. return false;
  102. }
  103. }
  104. }
  105. }
  106. else if (src.Type == OperandType.Constant)
  107. {
  108. int dstComponent = operation.GetSource(operation.SourcesCount - 2).Value;
  109. float expectedValue = dstComponent == 3 ? 1f : 0f;
  110. if (src.AsFloat() != expectedValue)
  111. {
  112. return false;
  113. }
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119. }
  120. else if (operation.Inst == Instruction.EmitVertex)
  121. {
  122. verticesCount++;
  123. }
  124. else if (operation.Inst == Instruction.EndPrimitive)
  125. {
  126. totalVerticesCount += verticesCount;
  127. verticesCount = 0;
  128. }
  129. }
  130. }
  131. return totalVerticesCount + verticesCount == 3 && writesLayer;
  132. }
  133. private static bool IsResourceWrite(Instruction inst)
  134. {
  135. switch (inst)
  136. {
  137. case Instruction.AtomicAdd:
  138. case Instruction.AtomicAnd:
  139. case Instruction.AtomicCompareAndSwap:
  140. case Instruction.AtomicMaxS32:
  141. case Instruction.AtomicMaxU32:
  142. case Instruction.AtomicMinS32:
  143. case Instruction.AtomicMinU32:
  144. case Instruction.AtomicOr:
  145. case Instruction.AtomicSwap:
  146. case Instruction.AtomicXor:
  147. case Instruction.ImageAtomic:
  148. case Instruction.ImageStore:
  149. case Instruction.StoreGlobal:
  150. case Instruction.StoreGlobal16:
  151. case Instruction.StoreGlobal8:
  152. case Instruction.StoreStorage:
  153. case Instruction.StoreStorage16:
  154. case Instruction.StoreStorage8:
  155. return true;
  156. }
  157. return false;
  158. }
  159. }
  160. }