OperandManager.cs 12 KB

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