Declarations.cs 29 KB

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