Declarations.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. using Ryujinx.Common;
  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. using System.Numerics;
  8. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  9. {
  10. static class Declarations
  11. {
  12. public static void Declare(CodeGenContext context, StructuredProgramInfo info)
  13. {
  14. context.AppendLine(context.Config.Options.TargetApi == TargetApi.Vulkan ? "#version 460 core" : "#version 450 core");
  15. context.AppendLine("#extension GL_ARB_gpu_shader_int64 : enable");
  16. if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
  17. {
  18. context.AppendLine("#extension GL_ARB_shader_ballot : enable");
  19. }
  20. else
  21. {
  22. context.AppendLine("#extension GL_KHR_shader_subgroup_basic : enable");
  23. context.AppendLine("#extension GL_KHR_shader_subgroup_ballot : enable");
  24. }
  25. context.AppendLine("#extension GL_ARB_shader_group_vote : enable");
  26. context.AppendLine("#extension GL_EXT_shader_image_load_formatted : enable");
  27. context.AppendLine("#extension GL_EXT_texture_shadow_lod : enable");
  28. if (context.Config.Stage == ShaderStage.Compute)
  29. {
  30. context.AppendLine("#extension GL_ARB_compute_shader : enable");
  31. }
  32. else if (context.Config.Stage == ShaderStage.Fragment)
  33. {
  34. if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock())
  35. {
  36. context.AppendLine("#extension GL_ARB_fragment_shader_interlock : enable");
  37. }
  38. else if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderOrderingIntel())
  39. {
  40. context.AppendLine("#extension GL_INTEL_fragment_shader_ordering : enable");
  41. }
  42. }
  43. else
  44. {
  45. if (context.Config.Stage == ShaderStage.Vertex)
  46. {
  47. context.AppendLine("#extension GL_ARB_shader_draw_parameters : enable");
  48. }
  49. context.AppendLine("#extension GL_ARB_shader_viewport_layer_array : enable");
  50. }
  51. if (context.Config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
  52. {
  53. context.AppendLine("#extension GL_NV_geometry_shader_passthrough : enable");
  54. }
  55. context.AppendLine("#pragma optionNV(fastmath off)");
  56. context.AppendLine();
  57. context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;");
  58. context.AppendLine();
  59. if (context.Config.Stage == ShaderStage.Compute)
  60. {
  61. int localMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeLocalMemorySize(), 4);
  62. if (localMemorySize != 0)
  63. {
  64. string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);
  65. context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
  66. context.AppendLine();
  67. }
  68. int sharedMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeSharedMemorySize(), 4);
  69. if (sharedMemorySize != 0)
  70. {
  71. string sharedMemorySizeStr = NumberFormatter.FormatInt(sharedMemorySize);
  72. context.AppendLine($"shared uint {DefaultNames.SharedMemoryName}[{sharedMemorySizeStr}];");
  73. context.AppendLine();
  74. }
  75. }
  76. else if (context.Config.LocalMemorySize != 0)
  77. {
  78. int localMemorySize = BitUtils.DivRoundUp(context.Config.LocalMemorySize, 4);
  79. string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);
  80. context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
  81. context.AppendLine();
  82. }
  83. var cBufferDescriptors = context.Config.GetConstantBufferDescriptors();
  84. if (cBufferDescriptors.Length != 0)
  85. {
  86. DeclareUniforms(context, cBufferDescriptors);
  87. context.AppendLine();
  88. }
  89. var sBufferDescriptors = context.Config.GetStorageBufferDescriptors();
  90. if (sBufferDescriptors.Length != 0)
  91. {
  92. DeclareStorages(context, sBufferDescriptors);
  93. context.AppendLine();
  94. }
  95. var textureDescriptors = context.Config.GetTextureDescriptors();
  96. if (textureDescriptors.Length != 0)
  97. {
  98. DeclareSamplers(context, textureDescriptors);
  99. context.AppendLine();
  100. }
  101. var imageDescriptors = context.Config.GetImageDescriptors();
  102. if (imageDescriptors.Length != 0)
  103. {
  104. DeclareImages(context, imageDescriptors);
  105. context.AppendLine();
  106. }
  107. if (context.Config.Stage != ShaderStage.Compute)
  108. {
  109. if (context.Config.Stage == ShaderStage.Geometry)
  110. {
  111. InputTopology inputTopology = context.Config.GpuAccessor.QueryPrimitiveTopology();
  112. string inPrimitive = inputTopology.ToGlslString();
  113. context.AppendLine($"layout (invocations = {context.Config.ThreadsPerInputPrimitive}, {inPrimitive}) in;");
  114. if (context.Config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
  115. {
  116. context.AppendLine($"layout (passthrough) in gl_PerVertex");
  117. context.EnterScope();
  118. context.AppendLine("vec4 gl_Position;");
  119. context.AppendLine("float gl_PointSize;");
  120. context.AppendLine("float gl_ClipDistance[];");
  121. context.LeaveScope(";");
  122. }
  123. else
  124. {
  125. string outPrimitive = context.Config.OutputTopology.ToGlslString();
  126. int maxOutputVertices = context.Config.GpPassthrough
  127. ? inputTopology.ToInputVertices()
  128. : context.Config.MaxOutputVertices;
  129. context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;");
  130. }
  131. context.AppendLine();
  132. }
  133. else if (context.Config.Stage == ShaderStage.TessellationControl)
  134. {
  135. int threadsPerInputPrimitive = context.Config.ThreadsPerInputPrimitive;
  136. context.AppendLine($"layout (vertices = {threadsPerInputPrimitive}) out;");
  137. context.AppendLine();
  138. }
  139. else if (context.Config.Stage == ShaderStage.TessellationEvaluation)
  140. {
  141. bool tessCw = context.Config.GpuAccessor.QueryTessCw();
  142. if (context.Config.Options.TargetApi == TargetApi.Vulkan)
  143. {
  144. // We invert the front face on Vulkan backend, so we need to do that here aswell.
  145. tessCw = !tessCw;
  146. }
  147. string patchType = context.Config.GpuAccessor.QueryTessPatchType().ToGlsl();
  148. string spacing = context.Config.GpuAccessor.QueryTessSpacing().ToGlsl();
  149. string windingOrder = tessCw ? "cw" : "ccw";
  150. context.AppendLine($"layout ({patchType}, {spacing}, {windingOrder}) in;");
  151. context.AppendLine();
  152. }
  153. if (context.Config.UsedInputAttributes != 0 || context.Config.GpPassthrough)
  154. {
  155. DeclareInputAttributes(context, info);
  156. context.AppendLine();
  157. }
  158. if (context.Config.UsedOutputAttributes != 0 || context.Config.Stage != ShaderStage.Fragment)
  159. {
  160. DeclareOutputAttributes(context, info);
  161. context.AppendLine();
  162. }
  163. if (context.Config.UsedInputAttributesPerPatch.Count != 0)
  164. {
  165. DeclareInputAttributesPerPatch(context, context.Config.UsedInputAttributesPerPatch);
  166. context.AppendLine();
  167. }
  168. if (context.Config.UsedOutputAttributesPerPatch.Count != 0)
  169. {
  170. DeclareUsedOutputAttributesPerPatch(context, context.Config.UsedOutputAttributesPerPatch);
  171. context.AppendLine();
  172. }
  173. if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline)
  174. {
  175. var tfOutput = context.Info.GetTransformFeedbackOutput(AttributeConsts.PositionX);
  176. if (tfOutput.Valid)
  177. {
  178. context.AppendLine($"layout (xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}) out gl_PerVertex");
  179. context.EnterScope();
  180. context.AppendLine("vec4 gl_Position;");
  181. context.LeaveScope(context.Config.Stage == ShaderStage.TessellationControl ? " gl_out[];" : ";");
  182. }
  183. }
  184. }
  185. else
  186. {
  187. string localSizeX = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeX());
  188. string localSizeY = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeY());
  189. string localSizeZ = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeZ());
  190. context.AppendLine(
  191. "layout (" +
  192. $"local_size_x = {localSizeX}, " +
  193. $"local_size_y = {localSizeY}, " +
  194. $"local_size_z = {localSizeZ}) in;");
  195. context.AppendLine();
  196. }
  197. bool isFragment = context.Config.Stage == ShaderStage.Fragment;
  198. if (isFragment || context.Config.Stage == ShaderStage.Compute || context.Config.Stage == ShaderStage.Vertex)
  199. {
  200. if (isFragment && context.Config.GpuAccessor.QueryEarlyZForce())
  201. {
  202. context.AppendLine("layout(early_fragment_tests) in;");
  203. context.AppendLine();
  204. }
  205. if ((context.Config.UsedFeatures & (FeatureFlags.FragCoordXY | FeatureFlags.IntegerSampling)) != 0)
  206. {
  207. string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  208. int scaleElements = context.Config.GetTextureDescriptors().Length + context.Config.GetImageDescriptors().Length;
  209. if (isFragment)
  210. {
  211. scaleElements++; // Also includes render target scale, for gl_FragCoord.
  212. }
  213. DeclareSupportUniformBlock(context, context.Config.Stage, scaleElements);
  214. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IntegerSampling) && scaleElements != 0)
  215. {
  216. AppendHelperFunction(context, $"Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_{stage}.glsl");
  217. context.AppendLine();
  218. }
  219. }
  220. else if (isFragment || context.Config.Stage == ShaderStage.Vertex)
  221. {
  222. DeclareSupportUniformBlock(context, context.Config.Stage, 0);
  223. }
  224. }
  225. if ((info.HelperFunctionsMask & HelperFunctionsMask.AtomicMinMaxS32Shared) != 0)
  226. {
  227. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/AtomicMinMaxS32Shared.glsl");
  228. }
  229. if ((info.HelperFunctionsMask & HelperFunctionsMask.AtomicMinMaxS32Storage) != 0)
  230. {
  231. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/AtomicMinMaxS32Storage.glsl");
  232. }
  233. if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighS32) != 0)
  234. {
  235. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighS32.glsl");
  236. }
  237. if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighU32) != 0)
  238. {
  239. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighU32.glsl");
  240. }
  241. if ((info.HelperFunctionsMask & HelperFunctionsMask.Shuffle) != 0)
  242. {
  243. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl");
  244. }
  245. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleDown) != 0)
  246. {
  247. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl");
  248. }
  249. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleUp) != 0)
  250. {
  251. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl");
  252. }
  253. if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleXor) != 0)
  254. {
  255. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl");
  256. }
  257. if ((info.HelperFunctionsMask & HelperFunctionsMask.StoreSharedSmallInt) != 0)
  258. {
  259. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/StoreSharedSmallInt.glsl");
  260. }
  261. if ((info.HelperFunctionsMask & HelperFunctionsMask.StoreStorageSmallInt) != 0)
  262. {
  263. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/StoreStorageSmallInt.glsl");
  264. }
  265. if ((info.HelperFunctionsMask & HelperFunctionsMask.SwizzleAdd) != 0)
  266. {
  267. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl");
  268. }
  269. }
  270. private static string GetTfLayout(TransformFeedbackOutput tfOutput)
  271. {
  272. if (tfOutput.Valid)
  273. {
  274. return $"layout (xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}) ";
  275. }
  276. return string.Empty;
  277. }
  278. public static void DeclareLocals(CodeGenContext context, StructuredFunction function)
  279. {
  280. foreach (AstOperand decl in function.Locals)
  281. {
  282. string name = context.OperandManager.DeclareLocal(decl);
  283. context.AppendLine(GetVarTypeName(context, decl.VarType) + " " + name + ";");
  284. }
  285. }
  286. public static string GetVarTypeName(CodeGenContext context, AggregateType type, bool precise = true)
  287. {
  288. if (context.Config.GpuAccessor.QueryHostReducedPrecision())
  289. {
  290. precise = false;
  291. }
  292. return type switch
  293. {
  294. AggregateType.Void => "void",
  295. AggregateType.Bool => "bool",
  296. AggregateType.FP32 => precise ? "precise float" : "float",
  297. AggregateType.FP64 => "double",
  298. AggregateType.S32 => "int",
  299. AggregateType.U32 => "uint",
  300. AggregateType.Vector2 | AggregateType.Bool => "bvec2",
  301. AggregateType.Vector2 | AggregateType.FP32 => precise ? "precise vec2" : "vec2",
  302. AggregateType.Vector2 | AggregateType.FP64 => "dvec2",
  303. AggregateType.Vector2 | AggregateType.S32 => "ivec2",
  304. AggregateType.Vector2 | AggregateType.U32 => "uvec2",
  305. AggregateType.Vector3 | AggregateType.Bool => "bvec3",
  306. AggregateType.Vector3 | AggregateType.FP32 => precise ? "precise vec3" : "vec3",
  307. AggregateType.Vector3 | AggregateType.FP64 => "dvec3",
  308. AggregateType.Vector3 | AggregateType.S32 => "ivec3",
  309. AggregateType.Vector3 | AggregateType.U32 => "uvec3",
  310. AggregateType.Vector4 | AggregateType.Bool => "bvec4",
  311. AggregateType.Vector4 | AggregateType.FP32 => precise ? "precise vec4" : "vec4",
  312. AggregateType.Vector4 | AggregateType.FP64 => "dvec4",
  313. AggregateType.Vector4 | AggregateType.S32 => "ivec4",
  314. AggregateType.Vector4 | AggregateType.U32 => "uvec4",
  315. _ => throw new ArgumentException($"Invalid variable type \"{type}\".")
  316. };
  317. }
  318. private static void DeclareUniforms(CodeGenContext context, BufferDescriptor[] descriptors)
  319. {
  320. string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]";
  321. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing))
  322. {
  323. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  324. ubName += "_" + DefaultNames.UniformNamePrefix;
  325. string blockName = $"{ubName}_{DefaultNames.BlockSuffix}";
  326. context.AppendLine($"layout (binding = {context.Config.FirstConstantBufferBinding}, std140) uniform {blockName}");
  327. context.EnterScope();
  328. context.AppendLine("vec4 " + DefaultNames.DataName + ubSize + ";");
  329. context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
  330. }
  331. else
  332. {
  333. foreach (var descriptor in descriptors)
  334. {
  335. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  336. ubName += "_" + DefaultNames.UniformNamePrefix + descriptor.Slot;
  337. context.AppendLine($"layout (binding = {descriptor.Binding}, std140) uniform {ubName}");
  338. context.EnterScope();
  339. context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, descriptor.Slot, false) + ubSize + ";");
  340. context.LeaveScope(";");
  341. }
  342. }
  343. }
  344. private static void DeclareStorages(CodeGenContext context, BufferDescriptor[] descriptors)
  345. {
  346. string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  347. sbName += "_" + DefaultNames.StorageNamePrefix;
  348. string blockName = $"{sbName}_{DefaultNames.BlockSuffix}";
  349. string layout = context.Config.Options.TargetApi == TargetApi.Vulkan ? ", set = 1" : string.Empty;
  350. context.AppendLine($"layout (binding = {context.Config.FirstStorageBufferBinding}{layout}, std430) buffer {blockName}");
  351. context.EnterScope();
  352. context.AppendLine("uint " + DefaultNames.DataName + "[];");
  353. context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
  354. }
  355. private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors)
  356. {
  357. int arraySize = 0;
  358. foreach (var descriptor in descriptors)
  359. {
  360. if (descriptor.Type.HasFlag(SamplerType.Indexed))
  361. {
  362. if (arraySize == 0)
  363. {
  364. arraySize = ShaderConfig.SamplerArraySize;
  365. }
  366. else if (--arraySize != 0)
  367. {
  368. continue;
  369. }
  370. }
  371. string indexExpr = NumberFormatter.FormatInt(arraySize);
  372. string samplerName = OperandManager.GetSamplerName(
  373. context.Config.Stage,
  374. descriptor.CbufSlot,
  375. descriptor.HandleIndex,
  376. descriptor.Type.HasFlag(SamplerType.Indexed),
  377. indexExpr);
  378. string samplerTypeName = descriptor.Type.ToGlslSamplerType();
  379. string layout = string.Empty;
  380. if (context.Config.Options.TargetApi == TargetApi.Vulkan)
  381. {
  382. layout = ", set = 2";
  383. }
  384. context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {samplerTypeName} {samplerName};");
  385. }
  386. }
  387. private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors)
  388. {
  389. int arraySize = 0;
  390. foreach (var descriptor in descriptors)
  391. {
  392. if (descriptor.Type.HasFlag(SamplerType.Indexed))
  393. {
  394. if (arraySize == 0)
  395. {
  396. arraySize = ShaderConfig.SamplerArraySize;
  397. }
  398. else if (--arraySize != 0)
  399. {
  400. continue;
  401. }
  402. }
  403. string indexExpr = NumberFormatter.FormatInt(arraySize);
  404. string imageName = OperandManager.GetImageName(
  405. context.Config.Stage,
  406. descriptor.CbufSlot,
  407. descriptor.HandleIndex,
  408. descriptor.Format,
  409. descriptor.Type.HasFlag(SamplerType.Indexed),
  410. indexExpr);
  411. string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType());
  412. if (descriptor.Flags.HasFlag(TextureUsageFlags.ImageCoherent))
  413. {
  414. imageTypeName = "coherent " + imageTypeName;
  415. }
  416. string layout = descriptor.Format.ToGlslFormat();
  417. if (!string.IsNullOrEmpty(layout))
  418. {
  419. layout = ", " + layout;
  420. }
  421. if (context.Config.Options.TargetApi == TargetApi.Vulkan)
  422. {
  423. layout = $", set = 3{layout}";
  424. }
  425. context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};");
  426. }
  427. }
  428. private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
  429. {
  430. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing))
  431. {
  432. string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
  433. context.AppendLine($"layout (location = 0) in vec4 {DefaultNames.IAttributePrefix}{suffix}[{Constants.MaxAttributes}];");
  434. }
  435. else
  436. {
  437. int usedAttributes = context.Config.UsedInputAttributes | context.Config.PassthroughAttributes;
  438. while (usedAttributes != 0)
  439. {
  440. int index = BitOperations.TrailingZeroCount(usedAttributes);
  441. DeclareInputAttribute(context, info, index);
  442. usedAttributes &= ~(1 << index);
  443. }
  444. }
  445. }
  446. private static void DeclareInputAttributesPerPatch(CodeGenContext context, HashSet<int> attrs)
  447. {
  448. foreach (int attr in attrs.Order())
  449. {
  450. DeclareInputAttributePerPatch(context, attr);
  451. }
  452. }
  453. private static void DeclareInputAttribute(CodeGenContext context, StructuredProgramInfo info, int attr)
  454. {
  455. string suffix = AttributeInfo.IsArrayAttributeGlsl(context.Config.Stage, isOutAttr: false) ? "[]" : string.Empty;
  456. string iq = string.Empty;
  457. if (context.Config.Stage == ShaderStage.Fragment)
  458. {
  459. iq = context.Config.ImapTypes[attr].GetFirstUsedType() switch
  460. {
  461. PixelImap.Constant => "flat ",
  462. PixelImap.ScreenLinear => "noperspective ",
  463. _ => string.Empty
  464. };
  465. }
  466. string name = $"{DefaultNames.IAttributePrefix}{attr}";
  467. if (context.Config.TransformFeedbackEnabled && context.Config.Stage == ShaderStage.Fragment)
  468. {
  469. for (int c = 0; c < 4; c++)
  470. {
  471. char swzMask = "xyzw"[c];
  472. context.AppendLine($"layout (location = {attr}, component = {c}) {iq}in float {name}_{swzMask}{suffix};");
  473. }
  474. }
  475. else
  476. {
  477. bool passthrough = (context.Config.PassthroughAttributes & (1 << attr)) != 0;
  478. string pass = passthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough() ? "passthrough, " : string.Empty;
  479. string type;
  480. if (context.Config.Stage == ShaderStage.Vertex)
  481. {
  482. type = context.Config.GpuAccessor.QueryAttributeType(attr).ToVec4Type();
  483. }
  484. else
  485. {
  486. type = AttributeType.Float.ToVec4Type();
  487. }
  488. context.AppendLine($"layout ({pass}location = {attr}) {iq}in {type} {name}{suffix};");
  489. }
  490. }
  491. private static void DeclareInputAttributePerPatch(CodeGenContext context, int attr)
  492. {
  493. int location = context.Config.GetPerPatchAttributeLocation(attr);
  494. string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}";
  495. context.AppendLine($"layout (location = {location}) patch in vec4 {name};");
  496. }
  497. private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  498. {
  499. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing))
  500. {
  501. context.AppendLine($"layout (location = 0) out vec4 {DefaultNames.OAttributePrefix}[{Constants.MaxAttributes}];");
  502. }
  503. else
  504. {
  505. int usedAttributes = context.Config.UsedOutputAttributes;
  506. while (usedAttributes != 0)
  507. {
  508. int index = BitOperations.TrailingZeroCount(usedAttributes);
  509. DeclareOutputAttribute(context, index);
  510. usedAttributes &= ~(1 << index);
  511. }
  512. }
  513. }
  514. private static void DeclareOutputAttribute(CodeGenContext context, int attr)
  515. {
  516. string suffix = AttributeInfo.IsArrayAttributeGlsl(context.Config.Stage, isOutAttr: true) ? "[]" : string.Empty;
  517. string name = $"{DefaultNames.OAttributePrefix}{attr}{suffix}";
  518. if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline)
  519. {
  520. int attrOffset = AttributeConsts.UserAttributeBase + attr * 16;
  521. int components = context.Config.LastInPipeline ? context.Info.GetTransformFeedbackOutputComponents(attrOffset) : 1;
  522. if (components > 1)
  523. {
  524. string type = components switch
  525. {
  526. 2 => "vec2",
  527. 3 => "vec3",
  528. 4 => "vec4",
  529. _ => "float"
  530. };
  531. string xfb = string.Empty;
  532. var tfOutput = context.Info.GetTransformFeedbackOutput(attrOffset);
  533. if (tfOutput.Valid)
  534. {
  535. xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}";
  536. }
  537. context.AppendLine($"layout (location = {attr}{xfb}) out {type} {name};");
  538. }
  539. else
  540. {
  541. for (int c = 0; c < 4; c++)
  542. {
  543. char swzMask = "xyzw"[c];
  544. string xfb = string.Empty;
  545. var tfOutput = context.Info.GetTransformFeedbackOutput(attrOffset + c * 4);
  546. if (tfOutput.Valid)
  547. {
  548. xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}";
  549. }
  550. context.AppendLine($"layout (location = {attr}, component = {c}{xfb}) out float {name}_{swzMask};");
  551. }
  552. }
  553. }
  554. else
  555. {
  556. string type = context.Config.Stage != ShaderStage.Fragment ? "vec4" :
  557. context.Config.GpuAccessor.QueryFragmentOutputType(attr) switch
  558. {
  559. AttributeType.Sint => "ivec4",
  560. AttributeType.Uint => "uvec4",
  561. _ => "vec4"
  562. };
  563. if (context.Config.GpuAccessor.QueryHostReducedPrecision() && context.Config.Stage == ShaderStage.Vertex && attr == 0)
  564. {
  565. context.AppendLine($"layout (location = {attr}) invariant out {type} {name};");
  566. }
  567. else
  568. {
  569. context.AppendLine($"layout (location = {attr}) out {type} {name};");
  570. }
  571. }
  572. }
  573. private static void DeclareUsedOutputAttributesPerPatch(CodeGenContext context, HashSet<int> attrs)
  574. {
  575. foreach (int attr in attrs.Order())
  576. {
  577. DeclareOutputAttributePerPatch(context, attr);
  578. }
  579. }
  580. private static void DeclareOutputAttributePerPatch(CodeGenContext context, int attr)
  581. {
  582. int location = context.Config.GetPerPatchAttributeLocation(attr);
  583. string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}";
  584. context.AppendLine($"layout (location = {location}) patch out vec4 {name};");
  585. }
  586. private static void DeclareSupportUniformBlock(CodeGenContext context, ShaderStage stage, int scaleElements)
  587. {
  588. bool needsSupportBlock = stage == ShaderStage.Fragment ||
  589. (context.Config.LastInVertexPipeline && context.Config.GpuAccessor.QueryViewportTransformDisable());
  590. if (!needsSupportBlock && scaleElements == 0)
  591. {
  592. return;
  593. }
  594. context.AppendLine($"layout (binding = 0, std140) uniform {DefaultNames.SupportBlockName}");
  595. context.EnterScope();
  596. switch (stage)
  597. {
  598. case ShaderStage.Fragment:
  599. case ShaderStage.Vertex:
  600. context.AppendLine($"uint {DefaultNames.SupportBlockAlphaTestName};");
  601. context.AppendLine($"bool {DefaultNames.SupportBlockIsBgraName}[{SupportBuffer.FragmentIsBgraCount}];");
  602. context.AppendLine($"vec4 {DefaultNames.SupportBlockViewportInverse};");
  603. context.AppendLine($"int {DefaultNames.SupportBlockFragmentScaleCount};");
  604. break;
  605. case ShaderStage.Compute:
  606. context.AppendLine($"uint s_reserved[{SupportBuffer.ComputeRenderScaleOffset / SupportBuffer.FieldSize}];");
  607. break;
  608. }
  609. context.AppendLine($"float {DefaultNames.SupportBlockRenderScaleName}[{SupportBuffer.RenderScaleMaxCount}];");
  610. context.LeaveScope(";");
  611. context.AppendLine();
  612. }
  613. private static void AppendHelperFunction(CodeGenContext context, string filename)
  614. {
  615. string code = EmbeddedResources.ReadAllText(filename);
  616. code = code.Replace("\t", CodeGenContext.Tab);
  617. code = code.Replace("$SHARED_MEM$", DefaultNames.SharedMemoryName);
  618. code = code.Replace("$STORAGE_MEM$", OperandManager.GetShaderStagePrefix(context.Config.Stage) + "_" + DefaultNames.StorageNamePrefix);
  619. if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
  620. {
  621. code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubGroupInvocationARB");
  622. code = code.Replace("$SUBGROUP_BROADCAST$", "readInvocationARB");
  623. }
  624. else
  625. {
  626. code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubgroupInvocationID");
  627. code = code.Replace("$SUBGROUP_BROADCAST$", "subgroupBroadcast");
  628. }
  629. context.AppendLine(code);
  630. context.AppendLine();
  631. }
  632. }
  633. }