Declarations.cs 17 KB

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