Declarations.cs 17 KB

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