EmitterContext.cs 6.8 KB

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