Declarations.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  177. if (!samplers.TryAdd(samplerName, texOp))
  178. {
  179. continue;
  180. }
  181. string samplerTypeName = GetSamplerTypeName(texOp.Type);
  182. context.AppendLine("uniform " + samplerTypeName + " " + samplerName + ";");
  183. }
  184. foreach (KeyValuePair<string, AstTextureOperation> kv in samplers)
  185. {
  186. string samplerName = kv.Key;
  187. AstTextureOperation texOp = kv.Value;
  188. TextureDescriptor desc;
  189. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  190. {
  191. AstOperand operand = texOp.GetSource(0) as AstOperand;
  192. desc = new TextureDescriptor(samplerName, texOp.Type, operand.CbufSlot, operand.CbufOffset);
  193. }
  194. else
  195. {
  196. desc = new TextureDescriptor(samplerName, texOp.Type, texOp.Handle);
  197. }
  198. context.TextureDescriptors.Add(desc);
  199. }
  200. }
  201. private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info)
  202. {
  203. Dictionary<string, AstTextureOperation> images = new Dictionary<string, AstTextureOperation>();
  204. foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle))
  205. {
  206. string imageName = OperandManager.GetImageName(context.Config.Stage, texOp);
  207. if (!images.TryAdd(imageName, texOp))
  208. {
  209. continue;
  210. }
  211. string imageTypeName = GetImageTypeName(texOp.Type);
  212. context.AppendLine("writeonly uniform " + imageTypeName + " " + imageName + ";");
  213. }
  214. foreach (KeyValuePair<string, AstTextureOperation> kv in images)
  215. {
  216. string imageName = kv.Key;
  217. AstTextureOperation texOp = kv.Value;
  218. TextureDescriptor desc = new TextureDescriptor(imageName, texOp.Type, texOp.Handle);
  219. context.ImageDescriptors.Add(desc);
  220. }
  221. }
  222. private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
  223. {
  224. string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
  225. foreach (int attr in info.IAttributes.OrderBy(x => x))
  226. {
  227. string iq = info.InterpolationQualifiers[attr].ToGlslQualifier();
  228. if (iq != string.Empty)
  229. {
  230. iq += " ";
  231. }
  232. context.AppendLine($"layout (location = {attr}) {iq}in vec4 {DefaultNames.IAttributePrefix}{attr}{suffix};");
  233. }
  234. }
  235. private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  236. {
  237. if (context.Config.Stage == ShaderStage.Fragment)
  238. {
  239. DeclareUsedOutputAttributes(context, info);
  240. }
  241. else
  242. {
  243. DeclareAllOutputAttributes(context, info);
  244. }
  245. }
  246. private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  247. {
  248. foreach (int attr in info.OAttributes.OrderBy(x => x))
  249. {
  250. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  251. }
  252. }
  253. private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  254. {
  255. for (int attr = 0; attr < MaxAttributes; attr++)
  256. {
  257. string iq = $"{DefineNames.OutQualifierPrefixName}{attr} ";
  258. context.AppendLine($"layout (location = {attr}) {iq}out vec4 {DefaultNames.OAttributePrefix}{attr};");
  259. }
  260. foreach (int attr in info.OAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
  261. {
  262. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  263. }
  264. }
  265. private static void AppendHelperFunction(CodeGenContext context, string filename)
  266. {
  267. string code = EmbeddedResources.ReadAllText(filename);
  268. context.AppendLine(code.Replace("\t", CodeGenContext.Tab));
  269. context.AppendLine();
  270. }
  271. private static string GetSamplerTypeName(SamplerType type)
  272. {
  273. string typeName;
  274. switch (type & SamplerType.Mask)
  275. {
  276. case SamplerType.Texture1D: typeName = "sampler1D"; break;
  277. case SamplerType.TextureBuffer: typeName = "samplerBuffer"; break;
  278. case SamplerType.Texture2D: typeName = "sampler2D"; break;
  279. case SamplerType.Texture3D: typeName = "sampler3D"; break;
  280. case SamplerType.TextureCube: typeName = "samplerCube"; break;
  281. default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
  282. }
  283. if ((type & SamplerType.Multisample) != 0)
  284. {
  285. typeName += "MS";
  286. }
  287. if ((type & SamplerType.Array) != 0)
  288. {
  289. typeName += "Array";
  290. }
  291. if ((type & SamplerType.Shadow) != 0)
  292. {
  293. typeName += "Shadow";
  294. }
  295. return typeName;
  296. }
  297. private static string GetImageTypeName(SamplerType type)
  298. {
  299. string typeName;
  300. switch (type & SamplerType.Mask)
  301. {
  302. case SamplerType.Texture1D: typeName = "image1D"; break;
  303. case SamplerType.TextureBuffer: typeName = "imageBuffer"; break;
  304. case SamplerType.Texture2D: typeName = "image2D"; break;
  305. case SamplerType.Texture3D: typeName = "image3D"; break;
  306. case SamplerType.TextureCube: typeName = "imageCube"; break;
  307. default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
  308. }
  309. if ((type & SamplerType.Multisample) != 0)
  310. {
  311. typeName += "MS";
  312. }
  313. if ((type & SamplerType.Array) != 0)
  314. {
  315. typeName += "Array";
  316. }
  317. return typeName;
  318. }
  319. }
  320. }