OperandManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.StructuredIr;
  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[] { "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("instance", VariableType.S32) },
  44. { AttributeConsts.VertexId, new BuiltInAttribute("gl_VertexID", VariableType.S32) },
  45. { AttributeConsts.FrontFacing, new BuiltInAttribute("gl_FrontFacing", VariableType.Bool) },
  46. { AttributeConsts.FragmentOutputDepth, new BuiltInAttribute("gl_FragDepth", VariableType.F32) }
  47. };
  48. private Dictionary<AstOperand, string> _locals;
  49. public OperandManager()
  50. {
  51. _locals = new Dictionary<AstOperand, string>();
  52. }
  53. public string DeclareLocal(AstOperand operand)
  54. {
  55. string name = $"{DefaultNames.LocalNamePrefix}_{_locals.Count}";
  56. _locals.Add(operand, name);
  57. return name;
  58. }
  59. public string GetExpression(AstOperand operand, GalShaderType shaderType)
  60. {
  61. switch (operand.Type)
  62. {
  63. case OperandType.Attribute:
  64. return GetAttributeName(operand, shaderType);
  65. case OperandType.Constant:
  66. return NumberFormatter.FormatInt(operand.Value);
  67. case OperandType.ConstantBuffer:
  68. return GetConstantBufferName(operand, shaderType);
  69. case OperandType.LocalVariable:
  70. return _locals[operand];
  71. case OperandType.Undefined:
  72. return DefaultNames.UndefinedName;
  73. }
  74. throw new ArgumentException($"Invalid operand type \"{operand.Type}\".");
  75. }
  76. public static string GetConstantBufferName(AstOperand cbuf, GalShaderType shaderType)
  77. {
  78. string ubName = GetUbName(shaderType, cbuf.CbufSlot);
  79. ubName += "[" + (cbuf.CbufOffset >> 2) + "]";
  80. return ubName + "." + GetSwizzleMask(cbuf.CbufOffset & 3);
  81. }
  82. public static string GetConstantBufferName(IAstNode slot, string offsetExpr, GalShaderType shaderType)
  83. {
  84. // Non-constant slots are not supported.
  85. // It is expected that upstream stages are never going to generate non-constant
  86. // slot access.
  87. AstOperand operand = (AstOperand)slot;
  88. string ubName = GetUbName(shaderType, operand.Value);
  89. string index0 = "[" + offsetExpr + " >> 4]";
  90. string index1 = "[" + offsetExpr + " >> 2 & 3]";
  91. return ubName + index0 + index1;
  92. }
  93. public static string GetOutAttributeName(AstOperand attr, GalShaderType shaderType)
  94. {
  95. return GetAttributeName(attr, shaderType, isOutAttr: true);
  96. }
  97. private static string GetAttributeName(AstOperand attr, GalShaderType shaderType, bool isOutAttr = false)
  98. {
  99. int value = attr.Value;
  100. string swzMask = GetSwizzleMask((value >> 2) & 3);
  101. if (value >= AttributeConsts.UserAttributeBase &&
  102. value < AttributeConsts.UserAttributeEnd)
  103. {
  104. value -= AttributeConsts.UserAttributeBase;
  105. string prefix = isOutAttr
  106. ? DefaultNames.OAttributePrefix
  107. : DefaultNames.IAttributePrefix;
  108. string name = $"{prefix}{(value >> 4)}";
  109. if (shaderType == GalShaderType.Geometry && !isOutAttr)
  110. {
  111. name += "[0]";
  112. }
  113. name += "." + swzMask;
  114. return name;
  115. }
  116. else
  117. {
  118. if (value >= AttributeConsts.FragmentOutputColorBase &&
  119. value < AttributeConsts.FragmentOutputColorEnd)
  120. {
  121. value -= AttributeConsts.FragmentOutputColorBase;
  122. return $"{DefaultNames.OAttributePrefix}{(value >> 4)}.{swzMask}";
  123. }
  124. else if (_builtInAttributes.TryGetValue(value & ~3, out BuiltInAttribute builtInAttr))
  125. {
  126. // TODO: There must be a better way to handle this...
  127. if (shaderType == GalShaderType.Fragment)
  128. {
  129. switch (value & ~3)
  130. {
  131. case AttributeConsts.PositionX: return "gl_FragCoord.x";
  132. case AttributeConsts.PositionY: return "gl_FragCoord.y";
  133. case AttributeConsts.PositionZ: return "gl_FragCoord.z";
  134. case AttributeConsts.PositionW: return "1.0";
  135. }
  136. }
  137. string name = builtInAttr.Name;
  138. if (shaderType == GalShaderType.Geometry && !isOutAttr)
  139. {
  140. name = "gl_in[0]." + name;
  141. }
  142. return name;
  143. }
  144. }
  145. // TODO: Warn about unknown built-in attribute.
  146. return isOutAttr ? "// bad_attr0x" + value.ToString("X") : "0.0";
  147. }
  148. public static string GetUbName(GalShaderType shaderType, int slot)
  149. {
  150. string ubName = GetShaderStagePrefix(shaderType);
  151. ubName += "_" + DefaultNames.UniformNamePrefix + slot;
  152. return ubName + "_" + DefaultNames.UniformNameSuffix;
  153. }
  154. public static string GetSamplerName(GalShaderType shaderType, AstTextureOperation texOp)
  155. {
  156. string suffix;
  157. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  158. {
  159. AstOperand operand = texOp.GetSource(0) as AstOperand;
  160. suffix = "_cb" + operand.CbufSlot + "_" + operand.CbufOffset;
  161. }
  162. else
  163. {
  164. suffix = (texOp.Handle - 8).ToString();
  165. }
  166. return GetShaderStagePrefix(shaderType) + "_" + DefaultNames.SamplerNamePrefix + suffix;
  167. }
  168. public static string GetShaderStagePrefix(GalShaderType shaderType)
  169. {
  170. return _stagePrefixes[(int)shaderType];
  171. }
  172. private static string GetSwizzleMask(int value)
  173. {
  174. return "xyzw".Substring(value, 1);
  175. }
  176. public static VariableType GetNodeDestType(IAstNode node)
  177. {
  178. if (node is AstOperation operation)
  179. {
  180. return GetDestVarType(operation.Inst);
  181. }
  182. else if (node is AstOperand operand)
  183. {
  184. if (operand.Type == OperandType.Attribute)
  185. {
  186. if (_builtInAttributes.TryGetValue(operand.Value & ~3, out BuiltInAttribute builtInAttr))
  187. {
  188. return builtInAttr.Type;
  189. }
  190. }
  191. return OperandInfo.GetVarType(operand);
  192. }
  193. else
  194. {
  195. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  196. }
  197. }
  198. }
  199. }