Declarations.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using Ryujinx.Common;
  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.Linq;
  8. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  9. {
  10. static class Declarations
  11. {
  12. // At least 16 attributes are guaranteed by the spec.
  13. public const int MaxAttributes = 16;
  14. public static void Declare(CodeGenContext context, StructuredProgramInfo info)
  15. {
  16. context.AppendLine("#version 420 core");
  17. context.AppendLine("#extension GL_ARB_shader_ballot : enable");
  18. context.AppendLine("#extension GL_ARB_shader_storage_buffer_object : enable");
  19. if (context.Config.Stage == ShaderStage.Compute)
  20. {
  21. context.AppendLine("#extension GL_ARB_compute_shader : enable");
  22. }
  23. context.AppendLine();
  24. context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;");
  25. context.AppendLine();
  26. if (context.Config.Stage == ShaderStage.Geometry)
  27. {
  28. string inPrimitive = "points";
  29. if ((context.Config.Flags & TranslationFlags.Unspecialized) != 0)
  30. {
  31. inPrimitive = DefineNames.InputTopologyName;
  32. }
  33. context.AppendLine($"layout ({inPrimitive}) in;");
  34. string outPrimitive = "triangle_strip";
  35. switch (context.Config.OutputTopology)
  36. {
  37. case OutputTopology.LineStrip: outPrimitive = "line_strip"; break;
  38. case OutputTopology.PointList: outPrimitive = "points"; break;
  39. case OutputTopology.TriangleStrip: outPrimitive = "triangle_strip"; break;
  40. }
  41. int maxOutputVertices = context.Config.MaxOutputVertices;
  42. context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;");
  43. context.AppendLine();
  44. }
  45. context.AppendLine("layout (std140) uniform Extra");
  46. context.EnterScope();
  47. context.AppendLine("vec2 flip;");
  48. context.AppendLine("int instance;");
  49. context.LeaveScope(";");
  50. context.AppendLine();
  51. context.AppendLine($"precise float {DefaultNames.LocalMemoryName}[0x100];");
  52. context.AppendLine();
  53. if (info.CBuffers.Count != 0)
  54. {
  55. DeclareUniforms(context, info);
  56. context.AppendLine();
  57. }
  58. if (info.SBuffers.Count != 0)
  59. {
  60. DeclareStorage(context, info);
  61. context.AppendLine();
  62. }
  63. if (info.Samplers.Count != 0)
  64. {
  65. DeclareSamplers(context, info);
  66. context.AppendLine();
  67. }
  68. if (info.Images.Count != 0)
  69. {
  70. DeclareImages(context, info);
  71. context.AppendLine();
  72. }
  73. if (context.Config.Stage != ShaderStage.Compute)
  74. {
  75. if (info.IAttributes.Count != 0)
  76. {
  77. DeclareInputAttributes(context, info);
  78. context.AppendLine();
  79. }
  80. if (info.OAttributes.Count != 0 || context.Config.Stage != ShaderStage.Fragment)
  81. {
  82. DeclareOutputAttributes(context, info);
  83. context.AppendLine();
  84. }
  85. }
  86. else
  87. {
  88. string localSizeX = "1";
  89. string localSizeY = "1";
  90. string localSizeZ = "1";
  91. if ((context.Config.Flags & TranslationFlags.Unspecialized) != 0)
  92. {
  93. localSizeX = DefineNames.LocalSizeX;
  94. localSizeY = DefineNames.LocalSizeY;
  95. localSizeZ = DefineNames.LocalSizeZ;
  96. }
  97. context.AppendLine(
  98. $"layout (" +
  99. $"local_size_x = {localSizeX}, " +
  100. $"local_size_y = {localSizeY}, " +
  101. $"local_size_z = {localSizeZ}) in;");
  102. context.AppendLine();
  103. }
  104. if ((info.HelperFunctionsMask & HelperFunctionsMask.Shuffle) != 0)
  105. {
  106. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl");
  107. }
  108. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleDown) != 0)
  109. {
  110. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl");
  111. }
  112. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleUp) != 0)
  113. {
  114. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl");
  115. }
  116. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleXor) != 0)
  117. {
  118. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl");
  119. }
  120. if ((info.HelperFunctionsMask & HelperFunctionsMask.SwizzleAdd) != 0)
  121. {
  122. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl");
  123. }
  124. }
  125. public static void DeclareLocals(CodeGenContext context, StructuredProgramInfo info)
  126. {
  127. foreach (AstOperand decl in info.Locals)
  128. {
  129. string name = context.OperandManager.DeclareLocal(decl);
  130. context.AppendLine(GetVarTypeName(decl.VarType) + " " + name + ";");
  131. }
  132. }
  133. private static string GetVarTypeName(VariableType type)
  134. {
  135. switch (type)
  136. {
  137. case VariableType.Bool: return "bool";
  138. case VariableType.F32: return "precise float";
  139. case VariableType.S32: return "int";
  140. case VariableType.U32: return "uint";
  141. }
  142. throw new ArgumentException($"Invalid variable type \"{type}\".");
  143. }
  144. private static void DeclareUniforms(CodeGenContext context, StructuredProgramInfo info)
  145. {
  146. foreach (int cbufSlot in info.CBuffers.OrderBy(x => x))
  147. {
  148. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  149. ubName += "_" + DefaultNames.UniformNamePrefix + cbufSlot;
  150. context.CBufferDescriptors.Add(new BufferDescriptor(ubName, cbufSlot));
  151. context.AppendLine("layout (std140) uniform " + ubName);
  152. context.EnterScope();
  153. string ubSize = "[" + NumberFormatter.FormatInt(context.Config.MaxCBufferSize / 16) + "]";
  154. context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, cbufSlot) + ubSize + ";");
  155. context.LeaveScope(";");
  156. }
  157. }
  158. private static void DeclareStorage(CodeGenContext context, StructuredProgramInfo info)
  159. {
  160. foreach (int sbufSlot in info.SBuffers.OrderBy(x => x))
  161. {
  162. string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  163. sbName += "_" + DefaultNames.StorageNamePrefix + sbufSlot;
  164. context.SBufferDescriptors.Add(new BufferDescriptor(sbName, sbufSlot));
  165. context.AppendLine("layout (std430) buffer " + sbName);
  166. context.EnterScope();
  167. context.AppendLine("precise float " + OperandManager.GetSbName(context.Config.Stage, sbufSlot) + "[];");
  168. context.LeaveScope(";");
  169. }
  170. }
  171. private static void DeclareSamplers(CodeGenContext context, StructuredProgramInfo info)
  172. {
  173. Dictionary<string, AstTextureOperation> samplers = new Dictionary<string, AstTextureOperation>();
  174. foreach (AstTextureOperation texOp in info.Samplers.OrderBy(x => x.Handle))
  175. {
  176. string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize);
  177. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  178. if (!samplers.TryAdd(samplerName, texOp))
  179. {
  180. continue;
  181. }
  182. string samplerTypeName = GetSamplerTypeName(texOp.Type);
  183. context.AppendLine("uniform " + samplerTypeName + " " + samplerName + ";");
  184. }
  185. foreach (KeyValuePair<string, AstTextureOperation> kv in samplers)
  186. {
  187. string samplerName = kv.Key;
  188. AstTextureOperation texOp = kv.Value;
  189. TextureDescriptor desc;
  190. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  191. {
  192. AstOperand operand = texOp.GetSource(0) as AstOperand;
  193. desc = new TextureDescriptor(samplerName, texOp.Type, operand.CbufSlot, operand.CbufOffset);
  194. }
  195. else if ((texOp.Type & SamplerType.Indexed) != 0)
  196. {
  197. for (int index = 0; index < texOp.ArraySize; index++)
  198. {
  199. string indexExpr = NumberFormatter.FormatInt(index);
  200. string indexedSamplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  201. desc = new TextureDescriptor(indexedSamplerName, texOp.Type, texOp.Handle + index * 2);
  202. context.TextureDescriptors.Add(desc);
  203. }
  204. }
  205. else
  206. {
  207. desc = new TextureDescriptor(samplerName, texOp.Type, texOp.Handle);
  208. context.TextureDescriptors.Add(desc);
  209. }
  210. }
  211. }
  212. private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info)
  213. {
  214. Dictionary<string, AstTextureOperation> images = new Dictionary<string, AstTextureOperation>();
  215. foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle))
  216. {
  217. string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize);
  218. string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr);
  219. if (!images.TryAdd(imageName, texOp))
  220. {
  221. continue;
  222. }
  223. string imageTypeName = GetImageTypeName(texOp.Type);
  224. context.AppendLine("writeonly uniform " + imageTypeName + " " + imageName + ";");
  225. }
  226. foreach (KeyValuePair<string, AstTextureOperation> kv in images)
  227. {
  228. string imageName = kv.Key;
  229. AstTextureOperation texOp = kv.Value;
  230. if ((texOp.Type & SamplerType.Indexed) != 0)
  231. {
  232. for (int index = 0; index < texOp.ArraySize; index++)
  233. {
  234. string indexExpr = NumberFormatter.FormatInt(index);
  235. string indexedSamplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  236. var desc = new TextureDescriptor(indexedSamplerName, texOp.Type, texOp.Handle + index * 2);
  237. context.TextureDescriptors.Add(desc);
  238. }
  239. }
  240. else
  241. {
  242. var desc = new TextureDescriptor(imageName, texOp.Type, texOp.Handle);
  243. context.ImageDescriptors.Add(desc);
  244. }
  245. }
  246. }
  247. private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
  248. {
  249. string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
  250. foreach (int attr in info.IAttributes.OrderBy(x => x))
  251. {
  252. string iq = info.InterpolationQualifiers[attr].ToGlslQualifier();
  253. if (iq != string.Empty)
  254. {
  255. iq += " ";
  256. }
  257. context.AppendLine($"layout (location = {attr}) {iq}in vec4 {DefaultNames.IAttributePrefix}{attr}{suffix};");
  258. }
  259. }
  260. private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  261. {
  262. if (context.Config.Stage == ShaderStage.Fragment)
  263. {
  264. DeclareUsedOutputAttributes(context, info);
  265. }
  266. else
  267. {
  268. DeclareAllOutputAttributes(context, info);
  269. }
  270. }
  271. private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  272. {
  273. foreach (int attr in info.OAttributes.OrderBy(x => x))
  274. {
  275. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  276. }
  277. }
  278. private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  279. {
  280. for (int attr = 0; attr < MaxAttributes; attr++)
  281. {
  282. string iq = $"{DefineNames.OutQualifierPrefixName}{attr} ";
  283. context.AppendLine($"layout (location = {attr}) {iq}out vec4 {DefaultNames.OAttributePrefix}{attr};");
  284. }
  285. foreach (int attr in info.OAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
  286. {
  287. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  288. }
  289. }
  290. private static void AppendHelperFunction(CodeGenContext context, string filename)
  291. {
  292. string code = EmbeddedResources.ReadAllText(filename);
  293. context.AppendLine(code.Replace("\t", CodeGenContext.Tab));
  294. context.AppendLine();
  295. }
  296. private static string GetSamplerTypeName(SamplerType type)
  297. {
  298. string typeName;
  299. switch (type & SamplerType.Mask)
  300. {
  301. case SamplerType.Texture1D: typeName = "sampler1D"; break;
  302. case SamplerType.TextureBuffer: typeName = "samplerBuffer"; break;
  303. case SamplerType.Texture2D: typeName = "sampler2D"; break;
  304. case SamplerType.Texture3D: typeName = "sampler3D"; break;
  305. case SamplerType.TextureCube: typeName = "samplerCube"; break;
  306. default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
  307. }
  308. if ((type & SamplerType.Multisample) != 0)
  309. {
  310. typeName += "MS";
  311. }
  312. if ((type & SamplerType.Array) != 0)
  313. {
  314. typeName += "Array";
  315. }
  316. if ((type & SamplerType.Shadow) != 0)
  317. {
  318. typeName += "Shadow";
  319. }
  320. return typeName;
  321. }
  322. private static string GetImageTypeName(SamplerType type)
  323. {
  324. string typeName;
  325. switch (type & SamplerType.Mask)
  326. {
  327. case SamplerType.Texture1D: typeName = "image1D"; break;
  328. case SamplerType.TextureBuffer: typeName = "imageBuffer"; break;
  329. case SamplerType.Texture2D: typeName = "image2D"; break;
  330. case SamplerType.Texture3D: typeName = "image3D"; break;
  331. case SamplerType.TextureCube: typeName = "imageCube"; break;
  332. default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
  333. }
  334. if ((type & SamplerType.Multisample) != 0)
  335. {
  336. typeName += "MS";
  337. }
  338. if ((type & SamplerType.Array) != 0)
  339. {
  340. typeName += "Array";
  341. }
  342. return typeName;
  343. }
  344. }
  345. }