OperandManager.cs 19 KB

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