EmitterContext.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  5. namespace Ryujinx.Graphics.Shader.Translation
  6. {
  7. class EmitterContext
  8. {
  9. public Block CurrBlock { get; set; }
  10. public OpCode CurrOp { get; set; }
  11. public ShaderConfig Config { get; }
  12. public bool IsNonMain { get; }
  13. public int OperationsCount => _operations.Count;
  14. private readonly IReadOnlyDictionary<ulong, int> _funcs;
  15. private readonly List<Operation> _operations;
  16. private readonly Dictionary<ulong, Operand> _labels;
  17. public EmitterContext(ShaderConfig config, bool isNonMain, IReadOnlyDictionary<ulong, int> funcs)
  18. {
  19. Config = config;
  20. IsNonMain = isNonMain;
  21. _funcs = funcs;
  22. _operations = new List<Operation>();
  23. _labels = new Dictionary<ulong, Operand>();
  24. }
  25. public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
  26. {
  27. Operation operation = new Operation(inst, dest, sources);
  28. Add(operation);
  29. return dest;
  30. }
  31. public (Operand, Operand) Add(Instruction inst, (Operand, Operand) dest, params Operand[] sources)
  32. {
  33. Operand[] dests = new[] { dest.Item1, dest.Item2 };
  34. Operation operation = new Operation(inst, 0, dests, sources);
  35. Add(operation);
  36. return dest;
  37. }
  38. public void Add(Operation operation)
  39. {
  40. _operations.Add(operation);
  41. }
  42. public TextureOperation CreateTextureOperation(
  43. Instruction inst,
  44. SamplerType type,
  45. TextureFlags flags,
  46. int handle,
  47. int compIndex,
  48. Operand dest,
  49. params Operand[] sources)
  50. {
  51. return CreateTextureOperation(inst, type, TextureFormat.Unknown, flags, handle, compIndex, dest, sources);
  52. }
  53. public TextureOperation CreateTextureOperation(
  54. Instruction inst,
  55. SamplerType type,
  56. TextureFormat format,
  57. TextureFlags flags,
  58. int handle,
  59. int compIndex,
  60. Operand dest,
  61. params Operand[] sources)
  62. {
  63. if (!flags.HasFlag(TextureFlags.Bindless))
  64. {
  65. Config.SetUsedTexture(inst, type, format, flags, TextureOperation.DefaultCbufSlot, handle);
  66. }
  67. return new TextureOperation(inst, type, format, flags, handle, compIndex, dest, sources);
  68. }
  69. public void FlagAttributeRead(int attribute)
  70. {
  71. if (Config.Stage == ShaderStage.Vertex && attribute == AttributeConsts.InstanceId)
  72. {
  73. Config.SetUsedFeature(FeatureFlags.InstanceId);
  74. }
  75. else if (Config.Stage == ShaderStage.Fragment)
  76. {
  77. switch (attribute)
  78. {
  79. case AttributeConsts.PositionX:
  80. case AttributeConsts.PositionY:
  81. Config.SetUsedFeature(FeatureFlags.FragCoordXY);
  82. break;
  83. }
  84. }
  85. }
  86. public void FlagAttributeWritten(int attribute)
  87. {
  88. if (Config.Stage == ShaderStage.Vertex)
  89. {
  90. switch (attribute)
  91. {
  92. case AttributeConsts.ClipDistance0:
  93. case AttributeConsts.ClipDistance1:
  94. case AttributeConsts.ClipDistance2:
  95. case AttributeConsts.ClipDistance3:
  96. case AttributeConsts.ClipDistance4:
  97. case AttributeConsts.ClipDistance5:
  98. case AttributeConsts.ClipDistance6:
  99. case AttributeConsts.ClipDistance7:
  100. Config.SetClipDistanceWritten((attribute - AttributeConsts.ClipDistance0) / 4);
  101. break;
  102. }
  103. }
  104. }
  105. public void MarkLabel(Operand label)
  106. {
  107. Add(Instruction.MarkLabel, label);
  108. }
  109. public Operand GetLabel(ulong address)
  110. {
  111. if (!_labels.TryGetValue(address, out Operand label))
  112. {
  113. label = Label();
  114. _labels.Add(address, label);
  115. }
  116. return label;
  117. }
  118. public int GetFunctionId(ulong address)
  119. {
  120. return _funcs[address];
  121. }
  122. public void PrepareForReturn()
  123. {
  124. if (!IsNonMain && Config.Stage == ShaderStage.Fragment)
  125. {
  126. if (Config.OmapDepth)
  127. {
  128. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  129. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  130. this.Copy(dest, src);
  131. }
  132. int regIndexBase = 0;
  133. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  134. {
  135. OmapTarget target = Config.OmapTargets[rtIndex];
  136. for (int component = 0; component < 4; component++)
  137. {
  138. if (!target.ComponentEnabled(component))
  139. {
  140. continue;
  141. }
  142. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  143. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  144. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  145. if (component == 0 || component == 2)
  146. {
  147. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  148. Operand lblIsBgra = Label();
  149. Operand lblEnd = Label();
  150. this.BranchIfTrue(lblIsBgra, isBgra);
  151. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  152. this.Branch(lblEnd);
  153. MarkLabel(lblIsBgra);
  154. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  155. MarkLabel(lblEnd);
  156. }
  157. else
  158. {
  159. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  160. }
  161. }
  162. if (target.Enabled)
  163. {
  164. Config.SetOutputUserAttribute(rtIndex);
  165. regIndexBase += 4;
  166. }
  167. }
  168. }
  169. }
  170. public Operation[] GetOperations()
  171. {
  172. return _operations.ToArray();
  173. }
  174. }
  175. }