OperandManager.cs 8.8 KB

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