Declarations.cs 28 KB

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