OperandManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. };
  61. private Dictionary<AstOperand, string> _locals;
  62. public OperandManager()
  63. {
  64. _locals = new Dictionary<AstOperand, string>();
  65. }
  66. public string DeclareLocal(AstOperand operand)
  67. {
  68. string name = $"{DefaultNames.LocalNamePrefix}_{_locals.Count}";
  69. _locals.Add(operand, name);
  70. return name;
  71. }
  72. public string GetExpression(AstOperand operand, ShaderStage stage)
  73. {
  74. switch (operand.Type)
  75. {
  76. case OperandType.Attribute:
  77. return GetAttributeName(operand, stage);
  78. case OperandType.Constant:
  79. return NumberFormatter.FormatInt(operand.Value);
  80. case OperandType.ConstantBuffer:
  81. return GetConstantBufferName(operand.CbufSlot, operand.CbufOffset, stage);
  82. case OperandType.LocalVariable:
  83. return _locals[operand];
  84. case OperandType.Undefined:
  85. return DefaultNames.UndefinedName;
  86. }
  87. throw new ArgumentException($"Invalid operand type \"{operand.Type}\".");
  88. }
  89. public static string GetConstantBufferName(int slot, int offset, ShaderStage stage)
  90. {
  91. string ubName = GetUbName(stage, slot);
  92. ubName += "[" + (offset >> 2) + "]";
  93. return ubName + "." + GetSwizzleMask(offset & 3);
  94. }
  95. private static string GetVec4Indexed(string vectorName, string indexExpr)
  96. {
  97. string result = $"{vectorName}.x";
  98. for (int i = 1; i < 4; i++)
  99. {
  100. result = $"(({indexExpr}) == {i}) ? ({vectorName}.{GetSwizzleMask(i)}) : ({result})";
  101. }
  102. return $"({result})";
  103. }
  104. public static string GetConstantBufferName(IAstNode slot, string offsetExpr, ShaderStage stage)
  105. {
  106. // Non-constant slots are not supported.
  107. // It is expected that upstream stages are never going to generate non-constant
  108. // slot access.
  109. AstOperand operand = (AstOperand)slot;
  110. string ubName = GetUbName(stage, operand.Value);
  111. string index0 = "[" + offsetExpr + " >> 2]";
  112. return GetVec4Indexed(ubName + index0, offsetExpr + " & 3");
  113. }
  114. public static string GetOutAttributeName(AstOperand attr, ShaderStage stage)
  115. {
  116. return GetAttributeName(attr, stage, isOutAttr: true);
  117. }
  118. public static string GetAttributeName(AstOperand attr, ShaderStage stage, bool isOutAttr = false, string indexExpr = "0")
  119. {
  120. int value = attr.Value;
  121. string swzMask = GetSwizzleMask((value >> 2) & 3);
  122. if (value >= AttributeConsts.UserAttributeBase &&
  123. value < AttributeConsts.UserAttributeEnd)
  124. {
  125. value -= AttributeConsts.UserAttributeBase;
  126. string prefix = isOutAttr
  127. ? DefaultNames.OAttributePrefix
  128. : DefaultNames.IAttributePrefix;
  129. string name = $"{prefix}{(value >> 4)}";
  130. if (stage == ShaderStage.Geometry && !isOutAttr)
  131. {
  132. name += $"[{indexExpr}]";
  133. }
  134. name += "." + swzMask;
  135. return name;
  136. }
  137. else
  138. {
  139. if (value >= AttributeConsts.FragmentOutputColorBase &&
  140. value < AttributeConsts.FragmentOutputColorEnd)
  141. {
  142. value -= AttributeConsts.FragmentOutputColorBase;
  143. return $"{DefaultNames.OAttributePrefix}{(value >> 4)}.{swzMask}";
  144. }
  145. else if (_builtInAttributes.TryGetValue(value & ~3, out BuiltInAttribute builtInAttr))
  146. {
  147. // TODO: There must be a better way to handle this...
  148. if (stage == ShaderStage.Fragment)
  149. {
  150. switch (value & ~3)
  151. {
  152. case AttributeConsts.PositionX: return "gl_FragCoord.x";
  153. case AttributeConsts.PositionY: return "gl_FragCoord.y";
  154. case AttributeConsts.PositionZ: return "gl_FragCoord.z";
  155. case AttributeConsts.PositionW: return "gl_FragCoord.w";
  156. }
  157. }
  158. string name = builtInAttr.Name;
  159. if (stage == ShaderStage.Geometry && !isOutAttr)
  160. {
  161. name = $"gl_in[{indexExpr}].{name}";
  162. }
  163. return name;
  164. }
  165. }
  166. // TODO: Warn about unknown built-in attribute.
  167. return isOutAttr ? "// bad_attr0x" + value.ToString("X") : "0.0";
  168. }
  169. public static string GetUbName(ShaderStage stage, int slot)
  170. {
  171. string ubName = GetShaderStagePrefix(stage);
  172. ubName += "_" + DefaultNames.UniformNamePrefix + slot;
  173. return ubName + "_" + DefaultNames.UniformNameSuffix;
  174. }
  175. public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  176. {
  177. string suffix;
  178. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  179. {
  180. AstOperand operand = texOp.GetSource(0) as AstOperand;
  181. suffix = "_cb" + operand.CbufSlot + "_" + operand.CbufOffset;
  182. }
  183. else
  184. {
  185. suffix = texOp.Handle.ToString("X");
  186. if ((texOp.Type & SamplerType.Indexed) != 0)
  187. {
  188. suffix += $"a[{indexExpr}]";
  189. }
  190. }
  191. return GetShaderStagePrefix(stage) + "_" + DefaultNames.SamplerNamePrefix + suffix;
  192. }
  193. public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  194. {
  195. string suffix = texOp.Handle.ToString("X");
  196. if ((texOp.Type & SamplerType.Indexed) != 0)
  197. {
  198. suffix += $"a[{indexExpr}]";
  199. }
  200. return GetShaderStagePrefix(stage) + "_" + DefaultNames.ImageNamePrefix + suffix;
  201. }
  202. public static string GetShaderStagePrefix(ShaderStage stage)
  203. {
  204. int index = (int)stage;
  205. if ((uint)index >= _stagePrefixes.Length)
  206. {
  207. return "invalid";
  208. }
  209. return _stagePrefixes[index];
  210. }
  211. private static string GetSwizzleMask(int value)
  212. {
  213. return "xyzw".Substring(value, 1);
  214. }
  215. public static VariableType GetNodeDestType(IAstNode node)
  216. {
  217. if (node is AstOperation operation)
  218. {
  219. // Load attribute basically just returns the attribute value.
  220. // Some built-in attributes may have different types, so we need
  221. // to return the type based on the attribute that is being read.
  222. if (operation.Inst == Instruction.LoadAttribute)
  223. {
  224. return GetOperandVarType((AstOperand)operation.GetSource(0));
  225. }
  226. else if (operation is AstTextureOperation texOp &&
  227. (texOp.Inst == Instruction.ImageLoad ||
  228. texOp.Inst == Instruction.ImageStore))
  229. {
  230. return texOp.Format.GetComponentType();
  231. }
  232. return GetDestVarType(operation.Inst);
  233. }
  234. else if (node is AstOperand operand)
  235. {
  236. return GetOperandVarType(operand);
  237. }
  238. else
  239. {
  240. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  241. }
  242. }
  243. private static VariableType GetOperandVarType(AstOperand operand)
  244. {
  245. if (operand.Type == OperandType.Attribute)
  246. {
  247. if (_builtInAttributes.TryGetValue(operand.Value & ~3, out BuiltInAttribute builtInAttr))
  248. {
  249. return builtInAttr.Type;
  250. }
  251. }
  252. return OperandInfo.GetVarType(operand);
  253. }
  254. }
  255. }