Declarations.cs 17 KB

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