Declarations.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  8. {
  9. static class Declarations
  10. {
  11. // At least 16 attributes are guaranteed by the spec.
  12. public const int MaxAttributes = 16;
  13. public static void Declare(CodeGenContext context, StructuredProgramInfo info)
  14. {
  15. context.AppendLine("#version 420 core");
  16. context.AppendLine("#extension GL_ARB_shader_storage_buffer_object : enable");
  17. if (context.Config.Stage == ShaderStage.Compute)
  18. {
  19. context.AppendLine("#extension GL_ARB_compute_shader : enable");
  20. }
  21. context.AppendLine();
  22. context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;");
  23. context.AppendLine();
  24. if (context.Config.Stage == ShaderStage.Geometry)
  25. {
  26. string inPrimitive = "points";
  27. if ((context.Config.Flags & TranslationFlags.Unspecialized) != 0)
  28. {
  29. inPrimitive = DefineNames.InputTopologyName;
  30. }
  31. context.AppendLine($"layout ({inPrimitive}) in;");
  32. string outPrimitive = "triangle_strip";
  33. switch (context.Config.OutputTopology)
  34. {
  35. case OutputTopology.LineStrip: outPrimitive = "line_strip"; break;
  36. case OutputTopology.PointList: outPrimitive = "points"; break;
  37. case OutputTopology.TriangleStrip: outPrimitive = "triangle_strip"; break;
  38. }
  39. int maxOutputVertices = context.Config.MaxOutputVertices;
  40. context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;");
  41. context.AppendLine();
  42. }
  43. context.AppendLine("layout (std140) uniform Extra");
  44. context.EnterScope();
  45. context.AppendLine("vec2 flip;");
  46. context.AppendLine("int instance;");
  47. context.LeaveScope(";");
  48. context.AppendLine();
  49. context.AppendLine($"precise float {DefaultNames.LocalMemoryName}[0x100];");
  50. context.AppendLine();
  51. if (info.CBuffers.Count != 0)
  52. {
  53. DeclareUniforms(context, info);
  54. context.AppendLine();
  55. }
  56. if (info.SBuffers.Count != 0)
  57. {
  58. DeclareStorage(context, info);
  59. context.AppendLine();
  60. }
  61. if (info.Samplers.Count != 0)
  62. {
  63. DeclareSamplers(context, info);
  64. context.AppendLine();
  65. }
  66. if (context.Config.Stage != ShaderStage.Compute)
  67. {
  68. if (info.IAttributes.Count != 0)
  69. {
  70. DeclareInputAttributes(context, info);
  71. context.AppendLine();
  72. }
  73. if (info.OAttributes.Count != 0 || context.Config.Stage != ShaderStage.Fragment)
  74. {
  75. DeclareOutputAttributes(context, info);
  76. context.AppendLine();
  77. }
  78. }
  79. else
  80. {
  81. string localSizeX = "1";
  82. string localSizeY = "1";
  83. string localSizeZ = "1";
  84. if ((context.Config.Flags & TranslationFlags.Unspecialized) != 0)
  85. {
  86. localSizeX = DefineNames.LocalSizeX;
  87. localSizeY = DefineNames.LocalSizeY;
  88. localSizeZ = DefineNames.LocalSizeZ;
  89. }
  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. }
  98. public static void DeclareLocals(CodeGenContext context, StructuredProgramInfo info)
  99. {
  100. foreach (AstOperand decl in info.Locals)
  101. {
  102. string name = context.OperandManager.DeclareLocal(decl);
  103. context.AppendLine(GetVarTypeName(decl.VarType) + " " + name + ";");
  104. }
  105. }
  106. private static string GetVarTypeName(VariableType type)
  107. {
  108. switch (type)
  109. {
  110. case VariableType.Bool: return "bool";
  111. case VariableType.F32: return "precise float";
  112. case VariableType.S32: return "int";
  113. case VariableType.U32: return "uint";
  114. }
  115. throw new ArgumentException($"Invalid variable type \"{type}\".");
  116. }
  117. private static void DeclareUniforms(CodeGenContext context, StructuredProgramInfo info)
  118. {
  119. foreach (int cbufSlot in info.CBuffers.OrderBy(x => x))
  120. {
  121. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  122. ubName += "_" + DefaultNames.UniformNamePrefix + cbufSlot;
  123. context.CBufferDescriptors.Add(new BufferDescriptor(ubName, cbufSlot));
  124. context.AppendLine("layout (std140) uniform " + ubName);
  125. context.EnterScope();
  126. string ubSize = "[" + NumberFormatter.FormatInt(context.Config.MaxCBufferSize / 16) + "]";
  127. context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, cbufSlot) + ubSize + ";");
  128. context.LeaveScope(";");
  129. }
  130. }
  131. private static void DeclareStorage(CodeGenContext context, StructuredProgramInfo info)
  132. {
  133. foreach (int sbufSlot in info.SBuffers.OrderBy(x => x))
  134. {
  135. string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  136. sbName += "_" + DefaultNames.StorageNamePrefix + sbufSlot;
  137. context.SBufferDescriptors.Add(new BufferDescriptor(sbName, sbufSlot));
  138. context.AppendLine("layout (std430) buffer " + sbName);
  139. context.EnterScope();
  140. context.AppendLine("precise float " + OperandManager.GetSbName(context.Config.Stage, sbufSlot) + "[];");
  141. context.LeaveScope(";");
  142. }
  143. }
  144. private static void DeclareSamplers(CodeGenContext context, StructuredProgramInfo info)
  145. {
  146. Dictionary<string, AstTextureOperation> samplers = new Dictionary<string, AstTextureOperation>();
  147. foreach (AstTextureOperation texOp in info.Samplers.OrderBy(x => x.Handle))
  148. {
  149. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  150. if (!samplers.TryAdd(samplerName, texOp))
  151. {
  152. continue;
  153. }
  154. string samplerTypeName = GetSamplerTypeName(texOp.Target);
  155. context.AppendLine("uniform " + samplerTypeName + " " + samplerName + ";");
  156. }
  157. foreach (KeyValuePair<string, AstTextureOperation> kv in samplers)
  158. {
  159. string samplerName = kv.Key;
  160. AstTextureOperation texOp = kv.Value;
  161. TextureDescriptor desc;
  162. if ((texOp.Flags & TextureFlags.Bindless) != 0)
  163. {
  164. AstOperand operand = texOp.GetSource(0) as AstOperand;
  165. desc = new TextureDescriptor(samplerName, texOp.Target, operand.CbufSlot, operand.CbufOffset);
  166. }
  167. else
  168. {
  169. desc = new TextureDescriptor(samplerName, texOp.Target, texOp.Handle);
  170. }
  171. context.TextureDescriptors.Add(desc);
  172. }
  173. }
  174. private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
  175. {
  176. string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
  177. foreach (int attr in info.IAttributes.OrderBy(x => x))
  178. {
  179. string iq = info.InterpolationQualifiers[attr].ToGlslQualifier();
  180. if (iq != string.Empty)
  181. {
  182. iq += " ";
  183. }
  184. context.AppendLine($"layout (location = {attr}) {iq}in vec4 {DefaultNames.IAttributePrefix}{attr}{suffix};");
  185. }
  186. }
  187. private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  188. {
  189. if (context.Config.Stage == ShaderStage.Fragment)
  190. {
  191. DeclareUsedOutputAttributes(context, info);
  192. }
  193. else
  194. {
  195. DeclareAllOutputAttributes(context, info);
  196. }
  197. }
  198. private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  199. {
  200. foreach (int attr in info.OAttributes.OrderBy(x => x))
  201. {
  202. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  203. }
  204. }
  205. private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  206. {
  207. for (int attr = 0; attr < MaxAttributes; attr++)
  208. {
  209. string iq = $"{DefineNames.OutQualifierPrefixName}{attr} ";
  210. context.AppendLine($"layout (location = {attr}) {iq}out vec4 {DefaultNames.OAttributePrefix}{attr};");
  211. }
  212. foreach (int attr in info.OAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
  213. {
  214. context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
  215. }
  216. }
  217. private static string GetSamplerTypeName(TextureTarget type)
  218. {
  219. string typeName;
  220. switch (type & TextureTarget.Mask)
  221. {
  222. case TextureTarget.Texture1D: typeName = "sampler1D"; break;
  223. case TextureTarget.Texture2D: typeName = "sampler2D"; break;
  224. case TextureTarget.Texture3D: typeName = "sampler3D"; break;
  225. case TextureTarget.TextureCube: typeName = "samplerCube"; break;
  226. default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
  227. }
  228. if ((type & TextureTarget.Multisample) != 0)
  229. {
  230. typeName += "MS";
  231. }
  232. if ((type & TextureTarget.Array) != 0)
  233. {
  234. typeName += "Array";
  235. }
  236. if ((type & TextureTarget.Shadow) != 0)
  237. {
  238. typeName += "Shadow";
  239. }
  240. return typeName;
  241. }
  242. }
  243. }