OperandManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Collections.Generic;
  6. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  7. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  8. {
  9. class OperandManager
  10. {
  11. private static string[] _stagePrefixes = new string[] { "cp", "vp", "tcp", "tep", "gp", "fp" };
  12. private struct BuiltInAttribute
  13. {
  14. public string Name { get; }
  15. public VariableType Type { get; }
  16. public BuiltInAttribute(string name, VariableType type)
  17. {
  18. Name = name;
  19. Type = type;
  20. }
  21. }
  22. private static Dictionary<int, BuiltInAttribute> _builtInAttributes =
  23. new Dictionary<int, BuiltInAttribute>()
  24. {
  25. { AttributeConsts.Layer, new BuiltInAttribute("gl_Layer", VariableType.S32) },
  26. { AttributeConsts.PointSize, new BuiltInAttribute("gl_PointSize", VariableType.F32) },
  27. { AttributeConsts.PositionX, new BuiltInAttribute("gl_Position.x", VariableType.F32) },
  28. { AttributeConsts.PositionY, new BuiltInAttribute("gl_Position.y", VariableType.F32) },
  29. { AttributeConsts.PositionZ, new BuiltInAttribute("gl_Position.z", VariableType.F32) },
  30. { AttributeConsts.PositionW, new BuiltInAttribute("gl_Position.w", VariableType.F32) },
  31. { AttributeConsts.ClipDistance0, new BuiltInAttribute("gl_ClipDistance[0]", VariableType.F32) },
  32. { AttributeConsts.ClipDistance1, new BuiltInAttribute("gl_ClipDistance[1]", VariableType.F32) },
  33. { AttributeConsts.ClipDistance2, new BuiltInAttribute("gl_ClipDistance[2]", VariableType.F32) },
  34. { AttributeConsts.ClipDistance3, new BuiltInAttribute("gl_ClipDistance[3]", VariableType.F32) },
  35. { AttributeConsts.ClipDistance4, new BuiltInAttribute("gl_ClipDistance[4]", VariableType.F32) },
  36. { AttributeConsts.ClipDistance5, new BuiltInAttribute("gl_ClipDistance[5]", VariableType.F32) },
  37. { AttributeConsts.ClipDistance6, new BuiltInAttribute("gl_ClipDistance[6]", VariableType.F32) },
  38. { AttributeConsts.ClipDistance7, new BuiltInAttribute("gl_ClipDistance[7]", VariableType.F32) },
  39. { AttributeConsts.PointCoordX, new BuiltInAttribute("gl_PointCoord.x", VariableType.F32) },
  40. { AttributeConsts.PointCoordY, new BuiltInAttribute("gl_PointCoord.y", VariableType.F32) },
  41. { AttributeConsts.TessCoordX, new BuiltInAttribute("gl_TessCoord.x", VariableType.F32) },
  42. { AttributeConsts.TessCoordY, new BuiltInAttribute("gl_TessCoord.y", VariableType.F32) },
  43. { AttributeConsts.InstanceId, new BuiltInAttribute("gl_InstanceID", VariableType.S32) },
  44. { AttributeConsts.VertexId, new BuiltInAttribute("gl_VertexID", VariableType.S32) },
  45. { AttributeConsts.FrontFacing, new BuiltInAttribute("gl_FrontFacing", VariableType.Bool) },
  46. // Special.
  47. { AttributeConsts.FragmentOutputDepth, new BuiltInAttribute("gl_FragDepth", VariableType.F32) },
  48. { AttributeConsts.ThreadIdX, new BuiltInAttribute("gl_LocalInvocationID.x", VariableType.U32) },
  49. { AttributeConsts.ThreadIdY, new BuiltInAttribute("gl_LocalInvocationID.y", VariableType.U32) },
  50. { AttributeConsts.ThreadIdZ, new BuiltInAttribute("gl_LocalInvocationID.z", VariableType.U32) },
  51. { AttributeConsts.CtaIdX, new BuiltInAttribute("gl_WorkGroupID.x", VariableType.U32) },
  52. { AttributeConsts.CtaIdY, new BuiltInAttribute("gl_WorkGroupID.y", VariableType.U32) },
  53. { AttributeConsts.CtaIdZ, new BuiltInAttribute("gl_WorkGroupID.z", VariableType.U32) },
  54. { AttributeConsts.LaneId, new BuiltInAttribute("gl_SubGroupInvocationARB", VariableType.U32) },
  55. { AttributeConsts.EqMask, new BuiltInAttribute("unpackUint2x32(gl_SubGroupEqMaskARB).x", VariableType.U32) },
  56. { AttributeConsts.GeMask, new BuiltInAttribute("unpackUint2x32(gl_SubGroupGeMaskARB).x", VariableType.U32) },
  57. { AttributeConsts.GtMask, new BuiltInAttribute("unpackUint2x32(gl_SubGroupGtMaskARB).x", VariableType.U32) },
  58. { AttributeConsts.LeMask, new BuiltInAttribute("unpackUint2x32(gl_SubGroupLeMaskARB).x", VariableType.U32) },
  59. { AttributeConsts.LtMask, new BuiltInAttribute("unpackUint2x32(gl_SubGroupLtMaskARB).x", VariableType.U32) },
  60. // Support uniforms.
  61. { AttributeConsts.FragmentOutputIsBgraBase + 0, new BuiltInAttribute($"{DefaultNames.IsBgraName}[0]", VariableType.Bool) },
  62. { AttributeConsts.FragmentOutputIsBgraBase + 4, new BuiltInAttribute($"{DefaultNames.IsBgraName}[1]", VariableType.Bool) },
  63. { AttributeConsts.FragmentOutputIsBgraBase + 8, new BuiltInAttribute($"{DefaultNames.IsBgraName}[2]", VariableType.Bool) },
  64. { AttributeConsts.FragmentOutputIsBgraBase + 12, new BuiltInAttribute($"{DefaultNames.IsBgraName}[3]", VariableType.Bool) },
  65. { AttributeConsts.FragmentOutputIsBgraBase + 16, new BuiltInAttribute($"{DefaultNames.IsBgraName}[4]", VariableType.Bool) },
  66. { AttributeConsts.FragmentOutputIsBgraBase + 20, new BuiltInAttribute($"{DefaultNames.IsBgraName}[5]", VariableType.Bool) },
  67. { AttributeConsts.FragmentOutputIsBgraBase + 24, new BuiltInAttribute($"{DefaultNames.IsBgraName}[6]", VariableType.Bool) },
  68. { AttributeConsts.FragmentOutputIsBgraBase + 28, new BuiltInAttribute($"{DefaultNames.IsBgraName}[7]", VariableType.Bool) }
  69. };
  70. private Dictionary<AstOperand, string> _locals;
  71. public OperandManager()
  72. {
  73. _locals = new Dictionary<AstOperand, string>();
  74. }
  75. public string DeclareLocal(AstOperand operand)
  76. {
  77. string name = $"{DefaultNames.LocalNamePrefix}_{_locals.Count}";
  78. _locals.Add(operand, name);
  79. return name;
  80. }
  81. public string GetExpression(AstOperand operand, ShaderConfig config, bool cbIndexable)
  82. {
  83. switch (operand.Type)
  84. {
  85. case OperandType.Attribute:
  86. return GetAttributeName(operand, config);
  87. case OperandType.Constant:
  88. return NumberFormatter.FormatInt(operand.Value);
  89. case OperandType.ConstantBuffer:
  90. return GetConstantBufferName(operand.CbufSlot, operand.CbufOffset, config.Stage, cbIndexable);
  91. case OperandType.LocalVariable:
  92. return _locals[operand];
  93. case OperandType.Undefined:
  94. return DefaultNames.UndefinedName;
  95. }
  96. throw new ArgumentException($"Invalid operand type \"{operand.Type}\".");
  97. }
  98. public static string GetConstantBufferName(int slot, int offset, ShaderStage stage, bool cbIndexable)
  99. {
  100. return $"{GetUbName(stage, slot, cbIndexable)}[{offset >> 2}].{GetSwizzleMask(offset & 3)}";
  101. }
  102. private static string GetVec4Indexed(string vectorName, string indexExpr)
  103. {
  104. string result = $"{vectorName}.x";
  105. for (int i = 1; i < 4; i++)
  106. {
  107. result = $"(({indexExpr}) == {i}) ? ({vectorName}.{GetSwizzleMask(i)}) : ({result})";
  108. }
  109. return $"({result})";
  110. }
  111. public static string GetConstantBufferName(int slot, string offsetExpr, ShaderStage stage, bool cbIndexable)
  112. {
  113. return GetVec4Indexed(GetUbName(stage, slot, cbIndexable) + $"[{offsetExpr} >> 2]", offsetExpr + " & 3");
  114. }
  115. public static string GetConstantBufferName(string slotExpr, string offsetExpr, ShaderStage stage)
  116. {
  117. return GetVec4Indexed(GetUbName(stage, slotExpr) + $"[{offsetExpr} >> 2]", offsetExpr + " & 3");
  118. }
  119. public static string GetOutAttributeName(AstOperand attr, ShaderConfig config)
  120. {
  121. return GetAttributeName(attr, config, isOutAttr: true);
  122. }
  123. public static string GetAttributeName(AstOperand attr, ShaderConfig config, bool isOutAttr = false, string indexExpr = "0")
  124. {
  125. int value = attr.Value;
  126. char swzMask = GetSwizzleMask((value >> 2) & 3);
  127. if (value >= AttributeConsts.UserAttributeBase && value < AttributeConsts.UserAttributeEnd)
  128. {
  129. value -= AttributeConsts.UserAttributeBase;
  130. string prefix = isOutAttr
  131. ? DefaultNames.OAttributePrefix
  132. : DefaultNames.IAttributePrefix;
  133. if ((config.Flags & TranslationFlags.Feedback) != 0)
  134. {
  135. string name = $"{prefix}{(value >> 4)}_{swzMask}";
  136. if (config.Stage == ShaderStage.Geometry && !isOutAttr)
  137. {
  138. name += $"[{indexExpr}]";
  139. }
  140. return name;
  141. }
  142. else
  143. {
  144. string name = $"{prefix}{(value >> 4)}";
  145. if (config.Stage == ShaderStage.Geometry && !isOutAttr)
  146. {
  147. name += $"[{indexExpr}]";
  148. }
  149. return name + '.' + swzMask;
  150. }
  151. }
  152. else
  153. {
  154. if (value >= AttributeConsts.FragmentOutputColorBase && value < AttributeConsts.FragmentOutputColorEnd)
  155. {
  156. value -= AttributeConsts.FragmentOutputColorBase;
  157. return $"{DefaultNames.OAttributePrefix}{(value >> 4)}.{swzMask}";
  158. }
  159. else if (_builtInAttributes.TryGetValue(value & ~3, out BuiltInAttribute builtInAttr))
  160. {
  161. // TODO: There must be a better way to handle this...
  162. if (config.Stage == ShaderStage.Fragment)
  163. {
  164. switch (value & ~3)
  165. {
  166. case AttributeConsts.PositionX: return "(gl_FragCoord.x / fp_renderScale[0])";
  167. case AttributeConsts.PositionY: return "(gl_FragCoord.y / fp_renderScale[0])";
  168. case AttributeConsts.PositionZ: return "gl_FragCoord.z";
  169. case AttributeConsts.PositionW: return "gl_FragCoord.w";
  170. }
  171. }
  172. string name = builtInAttr.Name;
  173. if (config.Stage == ShaderStage.Geometry && (value & AttributeConsts.SpecialMask) == 0 && !isOutAttr)
  174. {
  175. name = $"gl_in[{indexExpr}].{name}";
  176. }
  177. return name;
  178. }
  179. }
  180. // TODO: Warn about unknown built-in attribute.
  181. return isOutAttr ? "// bad_attr0x" + value.ToString("X") : "0.0";
  182. }
  183. public static string GetUbName(ShaderStage stage, int slot, bool cbIndexable)
  184. {
  185. if (cbIndexable)
  186. {
  187. return GetUbName(stage, NumberFormatter.FormatInt(slot, VariableType.S32));
  188. }
  189. return $"{GetShaderStagePrefix(stage)}_{DefaultNames.UniformNamePrefix}{slot}_{DefaultNames.UniformNameSuffix}";
  190. }
  191. private static string GetUbName(ShaderStage stage, string slotExpr)
  192. {
  193. return $"{GetShaderStagePrefix(stage)}_{DefaultNames.UniformNamePrefix}[{slotExpr}].{DefaultNames.DataName}";
  194. }
  195. public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  196. {
  197. string suffix;
  198. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  199. {
  200. AstOperand operand = texOp.GetSource(0) as AstOperand;
  201. suffix = "_cb" + operand.CbufSlot + "_" + operand.CbufOffset;
  202. }
  203. else
  204. {
  205. suffix = texOp.Handle.ToString("X");
  206. if ((texOp.Type & SamplerType.Indexed) != 0)
  207. {
  208. suffix += $"a[{indexExpr}]";
  209. }
  210. }
  211. return GetShaderStagePrefix(stage) + "_" + DefaultNames.SamplerNamePrefix + suffix;
  212. }
  213. public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  214. {
  215. string suffix = texOp.Handle.ToString("X") + "_" + texOp.Format.ToGlslFormat();
  216. if ((texOp.Type & SamplerType.Indexed) != 0)
  217. {
  218. suffix += $"a[{indexExpr}]";
  219. }
  220. return GetShaderStagePrefix(stage) + "_" + DefaultNames.ImageNamePrefix + suffix;
  221. }
  222. public static string GetShaderStagePrefix(ShaderStage stage)
  223. {
  224. int index = (int)stage;
  225. if ((uint)index >= _stagePrefixes.Length)
  226. {
  227. return "invalid";
  228. }
  229. return _stagePrefixes[index];
  230. }
  231. private static char GetSwizzleMask(int value)
  232. {
  233. return "xyzw"[value];
  234. }
  235. public static VariableType GetNodeDestType(IAstNode node)
  236. {
  237. if (node is AstOperation operation)
  238. {
  239. // Load attribute basically just returns the attribute value.
  240. // Some built-in attributes may have different types, so we need
  241. // to return the type based on the attribute that is being read.
  242. if (operation.Inst == Instruction.LoadAttribute)
  243. {
  244. return GetOperandVarType((AstOperand)operation.GetSource(0));
  245. }
  246. else if (operation is AstTextureOperation texOp &&
  247. (texOp.Inst == Instruction.ImageLoad ||
  248. texOp.Inst == Instruction.ImageStore))
  249. {
  250. return texOp.Format.GetComponentType();
  251. }
  252. return GetDestVarType(operation.Inst);
  253. }
  254. else if (node is AstOperand operand)
  255. {
  256. return GetOperandVarType(operand);
  257. }
  258. else
  259. {
  260. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  261. }
  262. }
  263. private static VariableType GetOperandVarType(AstOperand operand)
  264. {
  265. if (operand.Type == OperandType.Attribute)
  266. {
  267. if (_builtInAttributes.TryGetValue(operand.Value & ~3, out BuiltInAttribute builtInAttr))
  268. {
  269. return builtInAttr.Type;
  270. }
  271. }
  272. return OperandInfo.GetVarType(operand);
  273. }
  274. }
  275. }