Declarations.cs 18 KB

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