Declarations.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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.SwizzleAdd) != 0)
  218. {
  219. AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl");
  220. }
  221. }
  222. public static void DeclareLocals(CodeGenContext context, StructuredFunction function)
  223. {
  224. foreach (AstOperand decl in function.Locals)
  225. {
  226. string name = context.OperandManager.DeclareLocal(decl);
  227. context.AppendLine(GetVarTypeName(decl.VarType) + " " + name + ";");
  228. }
  229. }
  230. public static string GetVarTypeName(VariableType type)
  231. {
  232. switch (type)
  233. {
  234. case VariableType.Bool: return "bool";
  235. case VariableType.F32: return "precise float";
  236. case VariableType.F64: return "double";
  237. case VariableType.None: return "void";
  238. case VariableType.S32: return "int";
  239. case VariableType.U32: return "uint";
  240. }
  241. throw new ArgumentException($"Invalid variable type \"{type}\".");
  242. }
  243. private static void DeclareUniforms(CodeGenContext context, BufferDescriptor[] descriptors)
  244. {
  245. string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]";
  246. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing))
  247. {
  248. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  249. ubName += "_" + DefaultNames.UniformNamePrefix;
  250. string blockName = $"{ubName}_{DefaultNames.BlockSuffix}";
  251. context.AppendLine($"layout (binding = {context.Config.FirstConstantBufferBinding}, std140) uniform {blockName}");
  252. context.EnterScope();
  253. context.AppendLine("vec4 " + DefaultNames.DataName + ubSize + ";");
  254. context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
  255. }
  256. else
  257. {
  258. foreach (var descriptor in descriptors)
  259. {
  260. string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  261. ubName += "_" + DefaultNames.UniformNamePrefix + descriptor.Slot;
  262. context.AppendLine($"layout (binding = {descriptor.Binding}, std140) uniform {ubName}");
  263. context.EnterScope();
  264. context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, descriptor.Slot, false) + ubSize + ";");
  265. context.LeaveScope(";");
  266. }
  267. }
  268. }
  269. private static void DeclareStorages(CodeGenContext context, BufferDescriptor[] descriptors)
  270. {
  271. string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
  272. sbName += "_" + DefaultNames.StorageNamePrefix;
  273. string blockName = $"{sbName}_{DefaultNames.BlockSuffix}";
  274. string layout = context.Config.Options.TargetApi == TargetApi.Vulkan ? ", set = 1" : string.Empty;
  275. context.AppendLine($"layout (binding = {context.Config.FirstStorageBufferBinding}{layout}, std430) buffer {blockName}");
  276. context.EnterScope();
  277. context.AppendLine("uint " + DefaultNames.DataName + "[];");
  278. context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
  279. }
  280. private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors)
  281. {
  282. int arraySize = 0;
  283. foreach (var descriptor in descriptors)
  284. {
  285. if (descriptor.Type.HasFlag(SamplerType.Indexed))
  286. {
  287. if (arraySize == 0)
  288. {
  289. arraySize = ShaderConfig.SamplerArraySize;
  290. }
  291. else if (--arraySize != 0)
  292. {
  293. continue;
  294. }
  295. }
  296. string indexExpr = NumberFormatter.FormatInt(arraySize);
  297. string samplerName = OperandManager.GetSamplerName(
  298. context.Config.Stage,
  299. descriptor.CbufSlot,
  300. descriptor.HandleIndex,
  301. descriptor.Type.HasFlag(SamplerType.Indexed),
  302. indexExpr);
  303. string samplerTypeName = descriptor.Type.ToGlslSamplerType();
  304. string layout = string.Empty;
  305. if (context.Config.Options.TargetApi == TargetApi.Vulkan)
  306. {
  307. bool isBuffer = (descriptor.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
  308. int setIndex = isBuffer ? 4 : 2;
  309. layout = $", set = {setIndex}";
  310. }
  311. context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {samplerTypeName} {samplerName};");
  312. }
  313. }
  314. private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors)
  315. {
  316. int arraySize = 0;
  317. foreach (var descriptor in descriptors)
  318. {
  319. if (descriptor.Type.HasFlag(SamplerType.Indexed))
  320. {
  321. if (arraySize == 0)
  322. {
  323. arraySize = ShaderConfig.SamplerArraySize;
  324. }
  325. else if (--arraySize != 0)
  326. {
  327. continue;
  328. }
  329. }
  330. string indexExpr = NumberFormatter.FormatInt(arraySize);
  331. string imageName = OperandManager.GetImageName(
  332. context.Config.Stage,
  333. descriptor.CbufSlot,
  334. descriptor.HandleIndex,
  335. descriptor.Format,
  336. descriptor.Type.HasFlag(SamplerType.Indexed),
  337. indexExpr);
  338. string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType());
  339. string layout = descriptor.Format.ToGlslFormat();
  340. if (!string.IsNullOrEmpty(layout))
  341. {
  342. layout = ", " + layout;
  343. }
  344. if (context.Config.Options.TargetApi == TargetApi.Vulkan)
  345. {
  346. bool isBuffer = (descriptor.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
  347. int setIndex = isBuffer ? 5 : 3;
  348. layout = $", set = {setIndex}{layout}";
  349. }
  350. context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};");
  351. }
  352. }
  353. private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
  354. {
  355. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing))
  356. {
  357. string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
  358. context.AppendLine($"layout (location = 0) in vec4 {DefaultNames.IAttributePrefix}{suffix}[{Constants.MaxAttributes}];");
  359. }
  360. else
  361. {
  362. int usedAttributes = context.Config.UsedInputAttributes;
  363. while (usedAttributes != 0)
  364. {
  365. int index = BitOperations.TrailingZeroCount(usedAttributes);
  366. DeclareInputAttribute(context, info, index);
  367. usedAttributes &= ~(1 << index);
  368. }
  369. }
  370. }
  371. private static void DeclareInputAttributesPerPatch(CodeGenContext context, int usedAttributes)
  372. {
  373. while (usedAttributes != 0)
  374. {
  375. int index = BitOperations.TrailingZeroCount(usedAttributes);
  376. DeclareInputAttributePerPatch(context, index);
  377. usedAttributes &= ~(1 << index);
  378. }
  379. }
  380. private static void DeclareInputAttribute(CodeGenContext context, StructuredProgramInfo info, int attr)
  381. {
  382. string suffix = OperandManager.IsArrayAttribute(context.Config.Stage, isOutAttr: false) ? "[]" : string.Empty;
  383. string iq = string.Empty;
  384. if (context.Config.Stage == ShaderStage.Fragment)
  385. {
  386. iq = context.Config.ImapTypes[attr].GetFirstUsedType() switch
  387. {
  388. PixelImap.Constant => "flat ",
  389. PixelImap.ScreenLinear => "noperspective ",
  390. _ => string.Empty
  391. };
  392. }
  393. string pass = (context.Config.PassthroughAttributes & (1 << attr)) != 0 ? "passthrough, " : string.Empty;
  394. string name = $"{DefaultNames.IAttributePrefix}{attr}";
  395. if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0)
  396. {
  397. for (int c = 0; c < 4; c++)
  398. {
  399. char swzMask = "xyzw"[c];
  400. context.AppendLine($"layout ({pass}location = {attr}, component = {c}) {iq}in float {name}_{swzMask}{suffix};");
  401. }
  402. }
  403. else
  404. {
  405. context.AppendLine($"layout ({pass}location = {attr}) {iq}in vec4 {name}{suffix};");
  406. }
  407. }
  408. private static void DeclareInputAttributePerPatch(CodeGenContext context, int attr)
  409. {
  410. string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}";
  411. context.AppendLine($"patch in vec4 {name};");
  412. }
  413. private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
  414. {
  415. if (context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing))
  416. {
  417. context.AppendLine($"layout (location = 0) out vec4 {DefaultNames.OAttributePrefix}[{Constants.MaxAttributes}];");
  418. }
  419. else
  420. {
  421. int usedAttributes = context.Config.UsedOutputAttributes;
  422. while (usedAttributes != 0)
  423. {
  424. int index = BitOperations.TrailingZeroCount(usedAttributes);
  425. DeclareOutputAttribute(context, index);
  426. usedAttributes &= ~(1 << index);
  427. }
  428. }
  429. }
  430. private static void DeclareOutputAttribute(CodeGenContext context, int attr)
  431. {
  432. string suffix = OperandManager.IsArrayAttribute(context.Config.Stage, isOutAttr: true) ? "[]" : string.Empty;
  433. string name = $"{DefaultNames.OAttributePrefix}{attr}{suffix}";
  434. if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0)
  435. {
  436. for (int c = 0; c < 4; c++)
  437. {
  438. char swzMask = "xyzw"[c];
  439. context.AppendLine($"layout (location = {attr}, component = {c}) out float {name}_{swzMask};");
  440. }
  441. }
  442. else
  443. {
  444. context.AppendLine($"layout (location = {attr}) out vec4 {name};");
  445. }
  446. }
  447. private static void DeclareUsedOutputAttributesPerPatch(CodeGenContext context, int usedAttributes)
  448. {
  449. while (usedAttributes != 0)
  450. {
  451. int index = BitOperations.TrailingZeroCount(usedAttributes);
  452. DeclareOutputAttributePerPatch(context, index);
  453. usedAttributes &= ~(1 << index);
  454. }
  455. }
  456. private static void DeclareOutputAttributePerPatch(CodeGenContext context, int attr)
  457. {
  458. string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}";
  459. context.AppendLine($"patch out vec4 {name};");
  460. }
  461. private static void DeclareSupportUniformBlock(CodeGenContext context, bool isFragment, int scaleElements)
  462. {
  463. if (!isFragment && scaleElements == 0)
  464. {
  465. return;
  466. }
  467. context.AppendLine($"layout (binding = 0, std140) uniform {DefaultNames.SupportBlockName}");
  468. context.EnterScope();
  469. if (isFragment)
  470. {
  471. context.AppendLine($"uint {DefaultNames.SupportBlockAlphaTestName};");
  472. context.AppendLine($"bool {DefaultNames.SupportBlockIsBgraName}[{SupportBuffer.FragmentIsBgraCount}];");
  473. }
  474. else
  475. {
  476. context.AppendLine($"uint s_reserved[{SupportBuffer.ComputeRenderScaleOffset / SupportBuffer.FieldSize}];");
  477. }
  478. if (scaleElements != 0)
  479. {
  480. context.AppendLine($"float {DefaultNames.SupportBlockRenderScaleName}[{scaleElements}];");
  481. }
  482. context.LeaveScope(";");
  483. context.AppendLine();
  484. }
  485. private static void AppendHelperFunction(CodeGenContext context, string filename)
  486. {
  487. string code = EmbeddedResources.ReadAllText(filename);
  488. code = code.Replace("\t", CodeGenContext.Tab);
  489. code = code.Replace("$SHARED_MEM$", DefaultNames.SharedMemoryName);
  490. code = code.Replace("$STORAGE_MEM$", OperandManager.GetShaderStagePrefix(context.Config.Stage) + "_" + DefaultNames.StorageNamePrefix);
  491. if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
  492. {
  493. code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubGroupInvocationARB");
  494. code = code.Replace("$SUBGROUP_BROADCAST$", "readInvocationARB");
  495. }
  496. else
  497. {
  498. code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubgroupInvocationID");
  499. code = code.Replace("$SUBGROUP_BROADCAST$", "subgroupBroadcast");
  500. }
  501. context.AppendLine(code);
  502. context.AppendLine();
  503. }
  504. }
  505. }