OperandManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.StructuredIr;
  4. using Ryujinx.Graphics.Shader.Translation;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  9. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  10. {
  11. class OperandManager
  12. {
  13. private static readonly string[] _stagePrefixes = new string[] { "cp", "vp", "tcp", "tep", "gp", "fp" };
  14. private Dictionary<AstOperand, string> _locals;
  15. public OperandManager()
  16. {
  17. _locals = new Dictionary<AstOperand, string>();
  18. }
  19. public string DeclareLocal(AstOperand operand)
  20. {
  21. string name = $"{DefaultNames.LocalNamePrefix}_{_locals.Count}";
  22. _locals.Add(operand, name);
  23. return name;
  24. }
  25. public string GetExpression(CodeGenContext context, AstOperand operand)
  26. {
  27. return operand.Type switch
  28. {
  29. OperandType.Argument => GetArgumentName(operand.Value),
  30. OperandType.Constant => NumberFormatter.FormatInt(operand.Value),
  31. OperandType.ConstantBuffer => GetConstantBufferName(operand, context.Config),
  32. OperandType.LocalVariable => _locals[operand],
  33. OperandType.Undefined => DefaultNames.UndefinedName,
  34. _ => throw new ArgumentException($"Invalid operand type \"{operand.Type}\".")
  35. };
  36. }
  37. private static string GetConstantBufferName(AstOperand operand, ShaderConfig config)
  38. {
  39. return GetConstantBufferName(operand.CbufSlot, operand.CbufOffset, config.Stage, config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing));
  40. }
  41. public static string GetConstantBufferName(int slot, int offset, ShaderStage stage, bool cbIndexable)
  42. {
  43. return $"{GetUbName(stage, slot, cbIndexable)}[{offset >> 2}].{GetSwizzleMask(offset & 3)}";
  44. }
  45. private static string GetVec4Indexed(string vectorName, string indexExpr, bool indexElement)
  46. {
  47. if (indexElement)
  48. {
  49. return $"{vectorName}[{indexExpr}]";
  50. }
  51. string result = $"{vectorName}.x";
  52. for (int i = 1; i < 4; i++)
  53. {
  54. result = $"(({indexExpr}) == {i}) ? ({vectorName}.{GetSwizzleMask(i)}) : ({result})";
  55. }
  56. return $"({result})";
  57. }
  58. public static string GetConstantBufferName(int slot, string offsetExpr, ShaderStage stage, bool cbIndexable, bool indexElement)
  59. {
  60. return GetVec4Indexed(GetUbName(stage, slot, cbIndexable) + $"[{offsetExpr} >> 2]", offsetExpr + " & 3", indexElement);
  61. }
  62. public static string GetConstantBufferName(string slotExpr, string offsetExpr, ShaderStage stage, bool indexElement)
  63. {
  64. return GetVec4Indexed(GetUbName(stage, slotExpr) + $"[{offsetExpr} >> 2]", offsetExpr + " & 3", indexElement);
  65. }
  66. public static string GetUbName(ShaderStage stage, int slot, bool cbIndexable)
  67. {
  68. if (cbIndexable)
  69. {
  70. return GetUbName(stage, NumberFormatter.FormatInt(slot, AggregateType.S32));
  71. }
  72. return $"{GetShaderStagePrefix(stage)}_{DefaultNames.UniformNamePrefix}{slot}_{DefaultNames.UniformNameSuffix}";
  73. }
  74. private static string GetUbName(ShaderStage stage, string slotExpr)
  75. {
  76. return $"{GetShaderStagePrefix(stage)}_{DefaultNames.UniformNamePrefix}[{slotExpr}].{DefaultNames.DataName}";
  77. }
  78. public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  79. {
  80. return GetSamplerName(stage, texOp.CbufSlot, texOp.Handle, texOp.Type.HasFlag(SamplerType.Indexed), indexExpr);
  81. }
  82. public static string GetSamplerName(ShaderStage stage, int cbufSlot, int handle, bool indexed, string indexExpr)
  83. {
  84. string suffix = cbufSlot < 0 ? $"_tcb_{handle:X}" : $"_cb{cbufSlot}_{handle:X}";
  85. if (indexed)
  86. {
  87. suffix += $"a[{indexExpr}]";
  88. }
  89. return GetShaderStagePrefix(stage) + "_" + DefaultNames.SamplerNamePrefix + suffix;
  90. }
  91. public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr)
  92. {
  93. return GetImageName(stage, texOp.CbufSlot, texOp.Handle, texOp.Format, texOp.Type.HasFlag(SamplerType.Indexed), indexExpr);
  94. }
  95. public static string GetImageName(
  96. ShaderStage stage,
  97. int cbufSlot,
  98. int handle,
  99. TextureFormat format,
  100. bool indexed,
  101. string indexExpr)
  102. {
  103. string suffix = cbufSlot < 0
  104. ? $"_tcb_{handle:X}_{format.ToGlslFormat()}"
  105. : $"_cb{cbufSlot}_{handle:X}_{format.ToGlslFormat()}";
  106. if (indexed)
  107. {
  108. suffix += $"a[{indexExpr}]";
  109. }
  110. return GetShaderStagePrefix(stage) + "_" + DefaultNames.ImageNamePrefix + suffix;
  111. }
  112. public static string GetShaderStagePrefix(ShaderStage stage)
  113. {
  114. int index = (int)stage;
  115. if ((uint)index >= _stagePrefixes.Length)
  116. {
  117. return "invalid";
  118. }
  119. return _stagePrefixes[index];
  120. }
  121. private static char GetSwizzleMask(int value)
  122. {
  123. return "xyzw"[value];
  124. }
  125. public static string GetArgumentName(int argIndex)
  126. {
  127. return $"{DefaultNames.ArgumentNamePrefix}{argIndex}";
  128. }
  129. public static AggregateType GetNodeDestType(CodeGenContext context, IAstNode node)
  130. {
  131. // TODO: Get rid of that function entirely and return the type from the operation generation
  132. // functions directly, like SPIR-V does.
  133. if (node is AstOperation operation)
  134. {
  135. if (operation.Inst == Instruction.Load)
  136. {
  137. switch (operation.StorageKind)
  138. {
  139. case StorageKind.Input:
  140. case StorageKind.InputPerPatch:
  141. case StorageKind.Output:
  142. case StorageKind.OutputPerPatch:
  143. if (!(operation.GetSource(0) is AstOperand varId) || varId.Type != OperandType.Constant)
  144. {
  145. throw new InvalidOperationException($"First input of {operation.Inst} with {operation.StorageKind} storage must be a constant operand.");
  146. }
  147. IoVariable ioVariable = (IoVariable)varId.Value;
  148. bool isOutput = operation.StorageKind == StorageKind.Output || operation.StorageKind == StorageKind.OutputPerPatch;
  149. bool isPerPatch = operation.StorageKind == StorageKind.InputPerPatch || operation.StorageKind == StorageKind.OutputPerPatch;
  150. int location = 0;
  151. int component = 0;
  152. if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput))
  153. {
  154. if (!(operation.GetSource(1) is AstOperand vecIndex) || vecIndex.Type != OperandType.Constant)
  155. {
  156. throw new InvalidOperationException($"Second input of {operation.Inst} with {operation.StorageKind} storage must be a constant operand.");
  157. }
  158. location = vecIndex.Value;
  159. if (operation.SourcesCount > 2 &&
  160. operation.GetSource(2) is AstOperand elemIndex &&
  161. elemIndex.Type == OperandType.Constant &&
  162. context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput))
  163. {
  164. component = elemIndex.Value;
  165. }
  166. }
  167. (_, AggregateType varType) = IoMap.GetGlslVariable(context.Config, ioVariable, location, component, isOutput, isPerPatch);
  168. return varType & AggregateType.ElementTypeMask;
  169. }
  170. }
  171. else if (operation.Inst == Instruction.Call)
  172. {
  173. AstOperand funcId = (AstOperand)operation.GetSource(0);
  174. Debug.Assert(funcId.Type == OperandType.Constant);
  175. return context.GetFunction(funcId.Value).ReturnType;
  176. }
  177. else if (operation.Inst == Instruction.VectorExtract)
  178. {
  179. return GetNodeDestType(context, operation.GetSource(0)) & ~AggregateType.ElementCountMask;
  180. }
  181. else if (operation is AstTextureOperation texOp)
  182. {
  183. if (texOp.Inst == Instruction.ImageLoad ||
  184. texOp.Inst == Instruction.ImageStore ||
  185. texOp.Inst == Instruction.ImageAtomic)
  186. {
  187. return texOp.GetVectorType(texOp.Format.GetComponentType());
  188. }
  189. else if (texOp.Inst == Instruction.TextureSample)
  190. {
  191. return texOp.GetVectorType(GetDestVarType(operation.Inst));
  192. }
  193. }
  194. return GetDestVarType(operation.Inst);
  195. }
  196. else if (node is AstOperand operand)
  197. {
  198. if (operand.Type == OperandType.Argument)
  199. {
  200. int argIndex = operand.Value;
  201. return context.CurrentFunction.GetArgumentType(argIndex);
  202. }
  203. return OperandInfo.GetVarType(operand);
  204. }
  205. else
  206. {
  207. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  208. }
  209. }
  210. }
  211. }