Declarations.cs 29 KB

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