TranslatorContext.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Ryujinx.Graphics.Shader.CodeGen.Glsl;
  2. using Ryujinx.Graphics.Shader.CodeGen.Spirv;
  3. using Ryujinx.Graphics.Shader.Decoders;
  4. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  5. using Ryujinx.Graphics.Shader.StructuredIr;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Numerics;
  10. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  11. using static Ryujinx.Graphics.Shader.Translation.Translator;
  12. namespace Ryujinx.Graphics.Shader.Translation
  13. {
  14. public class TranslatorContext
  15. {
  16. private readonly DecodedProgram _program;
  17. private ShaderConfig _config;
  18. public ulong Address { get; }
  19. public ShaderStage Stage => _config.Stage;
  20. public int Size => _config.Size;
  21. public int Cb1DataSize => _config.Cb1DataSize;
  22. public bool LayerOutputWritten => _config.LayerOutputWritten;
  23. public IGpuAccessor GpuAccessor => _config.GpuAccessor;
  24. internal TranslatorContext(ulong address, DecodedProgram program, ShaderConfig config)
  25. {
  26. Address = address;
  27. _program = program;
  28. _config = config;
  29. }
  30. private static bool IsUserAttribute(Operand operand)
  31. {
  32. if (operand != null && operand.Type.IsAttribute())
  33. {
  34. int value = operand.Value & AttributeConsts.Mask;
  35. return value >= AttributeConsts.UserAttributeBase && value < AttributeConsts.UserAttributeEnd;
  36. }
  37. return false;
  38. }
  39. private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b, int aStart)
  40. {
  41. // Here we combine two shaders.
  42. // For shader A:
  43. // - All user attribute stores on shader A are turned into copies to a
  44. // temporary variable. It's assumed that shader B will consume them.
  45. // - All return instructions are turned into branch instructions, the
  46. // branch target being the start of the shader B code.
  47. // For shader B:
  48. // - All user attribute loads on shader B are turned into copies from a
  49. // temporary variable, as long that attribute is written by shader A.
  50. FunctionCode[] output = new FunctionCode[a.Length + b.Length - 1];
  51. List<Operation> ops = new List<Operation>(a.Length + b.Length);
  52. Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
  53. Operand lblB = Label();
  54. for (int index = aStart; index < a[0].Code.Length; index++)
  55. {
  56. Operation operation = a[0].Code[index];
  57. if (IsUserAttribute(operation.Dest))
  58. {
  59. int tIndex = (operation.Dest.Value - AttributeConsts.UserAttributeBase) / 4;
  60. Operand temp = temps[tIndex];
  61. if (temp == null)
  62. {
  63. temp = Local();
  64. temps[tIndex] = temp;
  65. }
  66. operation.Dest = temp;
  67. }
  68. if (operation.Inst == Instruction.Return)
  69. {
  70. ops.Add(new Operation(Instruction.Branch, lblB));
  71. }
  72. else
  73. {
  74. ops.Add(operation);
  75. }
  76. }
  77. ops.Add(new Operation(Instruction.MarkLabel, lblB));
  78. for (int index = 0; index < b[0].Code.Length; index++)
  79. {
  80. Operation operation = b[0].Code[index];
  81. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  82. {
  83. Operand src = operation.GetSource(srcIndex);
  84. if (IsUserAttribute(src))
  85. {
  86. Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
  87. if (temp != null)
  88. {
  89. operation.SetSource(srcIndex, temp);
  90. }
  91. }
  92. }
  93. ops.Add(operation);
  94. }
  95. output[0] = new FunctionCode(ops.ToArray());
  96. for (int i = 1; i < a.Length; i++)
  97. {
  98. output[i] = a[i];
  99. }
  100. for (int i = 1; i < b.Length; i++)
  101. {
  102. output[a.Length + i - 1] = b[i];
  103. }
  104. return output;
  105. }
  106. public void SetNextStage(TranslatorContext nextStage)
  107. {
  108. _config.MergeFromtNextStage(nextStage._config);
  109. }
  110. public ShaderProgram Translate(TranslatorContext other = null)
  111. {
  112. FunctionCode[] code = EmitShader(_program, _config, initializeOutputs: other == null, out _);
  113. if (other != null)
  114. {
  115. other._config.MergeOutputUserAttributes(_config.UsedOutputAttributes, Enumerable.Empty<int>());
  116. FunctionCode[] otherCode = EmitShader(other._program, other._config, initializeOutputs: true, out int aStart);
  117. code = Combine(otherCode, code, aStart);
  118. _config.InheritFrom(other._config);
  119. }
  120. return Translator.Translate(code, _config);
  121. }
  122. public ShaderProgram GenerateGeometryPassthrough()
  123. {
  124. int outputAttributesMask = _config.UsedOutputAttributes;
  125. int layerOutputAttr = _config.LayerOutputAttribute;
  126. OutputTopology outputTopology;
  127. int maxOutputVertices;
  128. switch (GpuAccessor.QueryPrimitiveTopology())
  129. {
  130. case InputTopology.Points:
  131. outputTopology = OutputTopology.PointList;
  132. maxOutputVertices = 1;
  133. break;
  134. case InputTopology.Lines:
  135. case InputTopology.LinesAdjacency:
  136. outputTopology = OutputTopology.LineStrip;
  137. maxOutputVertices = 2;
  138. break;
  139. default:
  140. outputTopology = OutputTopology.TriangleStrip;
  141. maxOutputVertices = 3;
  142. break;
  143. }
  144. ShaderConfig config = new ShaderConfig(ShaderStage.Geometry, outputTopology, maxOutputVertices, GpuAccessor, _config.Options);
  145. EmitterContext context = new EmitterContext(default, config, false);
  146. for (int v = 0; v < maxOutputVertices; v++)
  147. {
  148. int outAttrsMask = outputAttributesMask;
  149. while (outAttrsMask != 0)
  150. {
  151. int attrIndex = BitOperations.TrailingZeroCount(outAttrsMask);
  152. outAttrsMask &= ~(1 << attrIndex);
  153. for (int c = 0; c < 4; c++)
  154. {
  155. int attr = AttributeConsts.UserAttributeBase + attrIndex * 16 + c * 4;
  156. Operand value = context.LoadAttribute(Const(attr), Const(0), Const(v));
  157. if (attr == layerOutputAttr)
  158. {
  159. context.Copy(Attribute(AttributeConsts.Layer), value);
  160. }
  161. else
  162. {
  163. context.Copy(Attribute(attr), value);
  164. config.SetOutputUserAttribute(attrIndex);
  165. }
  166. config.SetInputUserAttribute(attrIndex, c);
  167. }
  168. }
  169. for (int c = 0; c < 4; c++)
  170. {
  171. int attr = AttributeConsts.PositionX + c * 4;
  172. Operand value = context.LoadAttribute(Const(attr), Const(0), Const(v));
  173. context.Copy(Attribute(attr), value);
  174. }
  175. context.EmitVertex();
  176. }
  177. context.EndPrimitive();
  178. var operations = context.GetOperations();
  179. var cfg = ControlFlowGraph.Create(operations);
  180. var function = new Function(cfg.Blocks, "main", false, 0, 0);
  181. var sInfo = StructuredProgram.MakeStructuredProgram(new[] { function }, config);
  182. var info = config.CreateProgramInfo();
  183. return config.Options.TargetLanguage switch
  184. {
  185. TargetLanguage.Glsl => new ShaderProgram(info, TargetLanguage.Glsl, GlslGenerator.Generate(sInfo, config)),
  186. TargetLanguage.Spirv => new ShaderProgram(info, TargetLanguage.Spirv, SpirvGenerator.Generate(sInfo, config)),
  187. _ => throw new NotImplementedException(config.Options.TargetLanguage.ToString())
  188. };
  189. }
  190. }
  191. }