Declarations.cs 31 KB

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