EmitterContext.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if (Config.Stage != ShaderStage.Fragment && attribute == AttributeConsts.Layer)
  105. {
  106. Config.SetUsedFeature(FeatureFlags.RtLayer);
  107. }
  108. }
  109. public void MarkLabel(Operand label)
  110. {
  111. Add(Instruction.MarkLabel, label);
  112. }
  113. public Operand GetLabel(ulong address)
  114. {
  115. if (!_labels.TryGetValue(address, out Operand label))
  116. {
  117. label = Label();
  118. _labels.Add(address, label);
  119. }
  120. return label;
  121. }
  122. public int GetFunctionId(ulong address)
  123. {
  124. return _funcs[address];
  125. }
  126. public void PrepareForReturn()
  127. {
  128. if (!IsNonMain && Config.Stage == ShaderStage.Fragment)
  129. {
  130. if (Config.OmapDepth)
  131. {
  132. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  133. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  134. this.Copy(dest, src);
  135. }
  136. int regIndexBase = 0;
  137. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  138. {
  139. OmapTarget target = Config.OmapTargets[rtIndex];
  140. for (int component = 0; component < 4; component++)
  141. {
  142. if (!target.ComponentEnabled(component))
  143. {
  144. continue;
  145. }
  146. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  147. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  148. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  149. if (component == 0 || component == 2)
  150. {
  151. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  152. Operand lblIsBgra = Label();
  153. Operand lblEnd = Label();
  154. this.BranchIfTrue(lblIsBgra, isBgra);
  155. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  156. this.Branch(lblEnd);
  157. MarkLabel(lblIsBgra);
  158. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  159. MarkLabel(lblEnd);
  160. }
  161. else
  162. {
  163. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  164. }
  165. }
  166. if (target.Enabled)
  167. {
  168. Config.SetOutputUserAttribute(rtIndex);
  169. regIndexBase += 4;
  170. }
  171. }
  172. }
  173. }
  174. public Operation[] GetOperations()
  175. {
  176. return _operations.ToArray();
  177. }
  178. }
  179. }