Declarations.cs 25 KB

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