Declarations.cs 26 KB

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