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