InstGenMemory.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Text;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  7. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  8. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  9. {
  10. static class InstGenMemory
  11. {
  12. public static string ImageLoadOrStore(CodeGenContext context, AstOperation operation)
  13. {
  14. AstTextureOperation texOp = (AstTextureOperation)operation;
  15. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  16. // TODO: Bindless texture support. For now we just return 0/do nothing.
  17. if (isBindless)
  18. {
  19. switch (texOp.Inst)
  20. {
  21. case Instruction.ImageStore:
  22. return "// imageStore(bindless)";
  23. case Instruction.ImageLoad:
  24. AggregateType componentType = texOp.Format.GetComponentType();
  25. NumberFormatter.TryFormat(0, componentType, out string imageConst);
  26. AggregateType outputType = texOp.GetVectorType(componentType);
  27. if ((outputType & AggregateType.ElementCountMask) != 0)
  28. {
  29. return $"{Declarations.GetVarTypeName(context, outputType, precise: false)}({imageConst})";
  30. }
  31. return imageConst;
  32. default:
  33. return NumberFormatter.FormatInt(0);
  34. }
  35. }
  36. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  37. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  38. var texCallBuilder = new StringBuilder();
  39. if (texOp.Inst == Instruction.ImageAtomic)
  40. {
  41. texCallBuilder.Append((texOp.Flags & TextureFlags.AtomicMask) switch {
  42. TextureFlags.Add => "imageAtomicAdd",
  43. TextureFlags.Minimum => "imageAtomicMin",
  44. TextureFlags.Maximum => "imageAtomicMax",
  45. TextureFlags.Increment => "imageAtomicAdd", // TODO: Clamp value.
  46. TextureFlags.Decrement => "imageAtomicAdd", // TODO: Clamp value.
  47. TextureFlags.BitwiseAnd => "imageAtomicAnd",
  48. TextureFlags.BitwiseOr => "imageAtomicOr",
  49. TextureFlags.BitwiseXor => "imageAtomicXor",
  50. TextureFlags.Swap => "imageAtomicExchange",
  51. TextureFlags.CAS => "imageAtomicCompSwap",
  52. _ => "imageAtomicAdd",
  53. });
  54. }
  55. else
  56. {
  57. texCallBuilder.Append(texOp.Inst == Instruction.ImageLoad ? "imageLoad" : "imageStore");
  58. }
  59. int srcIndex = isBindless ? 1 : 0;
  60. string Src(AggregateType type)
  61. {
  62. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  63. }
  64. string indexExpr = null;
  65. if (isIndexed)
  66. {
  67. indexExpr = Src(AggregateType.S32);
  68. }
  69. string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr);
  70. texCallBuilder.Append('(');
  71. texCallBuilder.Append(imageName);
  72. int coordsCount = texOp.Type.GetDimensions();
  73. int pCount = coordsCount + (isArray ? 1 : 0);
  74. void Append(string str)
  75. {
  76. texCallBuilder.Append(", ");
  77. texCallBuilder.Append(str);
  78. }
  79. string ApplyScaling(string vector)
  80. {
  81. if (context.Config.Stage.SupportsRenderScale() &&
  82. texOp.Inst == Instruction.ImageLoad &&
  83. !isBindless &&
  84. !isIndexed)
  85. {
  86. // Image scales start after texture ones.
  87. int scaleIndex = context.Config.GetTextureDescriptors().Length + context.Config.FindImageDescriptorIndex(texOp);
  88. if (pCount == 3 && isArray)
  89. {
  90. // The array index is not scaled, just x and y.
  91. vector = $"ivec3(Helper_TexelFetchScale(({vector}).xy, {scaleIndex}), ({vector}).z)";
  92. }
  93. else if (pCount == 2 && !isArray)
  94. {
  95. vector = $"Helper_TexelFetchScale({vector}, {scaleIndex})";
  96. }
  97. }
  98. return vector;
  99. }
  100. if (pCount > 1)
  101. {
  102. string[] elems = new string[pCount];
  103. for (int index = 0; index < pCount; index++)
  104. {
  105. elems[index] = Src(AggregateType.S32);
  106. }
  107. Append(ApplyScaling($"ivec{pCount}({string.Join(", ", elems)})"));
  108. }
  109. else
  110. {
  111. Append(Src(AggregateType.S32));
  112. }
  113. if (texOp.Inst == Instruction.ImageStore)
  114. {
  115. AggregateType type = texOp.Format.GetComponentType();
  116. string[] cElems = new string[4];
  117. for (int index = 0; index < 4; index++)
  118. {
  119. if (srcIndex < texOp.SourcesCount)
  120. {
  121. cElems[index] = Src(type);
  122. }
  123. else
  124. {
  125. cElems[index] = type switch
  126. {
  127. AggregateType.S32 => NumberFormatter.FormatInt(0),
  128. AggregateType.U32 => NumberFormatter.FormatUint(0),
  129. _ => NumberFormatter.FormatFloat(0)
  130. };
  131. }
  132. }
  133. string prefix = type switch
  134. {
  135. AggregateType.S32 => "i",
  136. AggregateType.U32 => "u",
  137. _ => string.Empty
  138. };
  139. Append($"{prefix}vec4({string.Join(", ", cElems)})");
  140. }
  141. if (texOp.Inst == Instruction.ImageAtomic)
  142. {
  143. AggregateType type = texOp.Format.GetComponentType();
  144. if ((texOp.Flags & TextureFlags.AtomicMask) == TextureFlags.CAS)
  145. {
  146. Append(Src(type)); // Compare value.
  147. }
  148. string value = (texOp.Flags & TextureFlags.AtomicMask) switch
  149. {
  150. TextureFlags.Increment => NumberFormatter.FormatInt(1, type), // TODO: Clamp value
  151. TextureFlags.Decrement => NumberFormatter.FormatInt(-1, type), // TODO: Clamp value
  152. _ => Src(type)
  153. };
  154. Append(value);
  155. texCallBuilder.Append(')');
  156. if (type != AggregateType.S32)
  157. {
  158. texCallBuilder
  159. .Insert(0, "int(")
  160. .Append(')');
  161. }
  162. }
  163. else
  164. {
  165. texCallBuilder.Append(')');
  166. if (texOp.Inst == Instruction.ImageLoad)
  167. {
  168. texCallBuilder.Append(GetMaskMultiDest(texOp.Index));
  169. }
  170. }
  171. return texCallBuilder.ToString();
  172. }
  173. public static string LoadAttribute(CodeGenContext context, AstOperation operation)
  174. {
  175. IAstNode src1 = operation.GetSource(0);
  176. IAstNode src2 = operation.GetSource(1);
  177. IAstNode src3 = operation.GetSource(2);
  178. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  179. {
  180. throw new InvalidOperationException($"First input of {nameof(Instruction.LoadAttribute)} must be a constant operand.");
  181. }
  182. string indexExpr = GetSoureExpr(context, src3, GetSrcVarType(operation.Inst, 2));
  183. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  184. {
  185. int attrOffset = baseAttr.Value + (operand.Value << 2);
  186. return OperandManager.GetAttributeName(context, attrOffset, perPatch: false, isOutAttr: false, indexExpr);
  187. }
  188. else
  189. {
  190. string attrExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  191. attrExpr = Enclose(attrExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  192. return OperandManager.GetAttributeName(attrExpr, context.Config, isOutAttr: false, indexExpr);
  193. }
  194. }
  195. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  196. {
  197. IAstNode src1 = operation.GetSource(0);
  198. IAstNode src2 = operation.GetSource(1);
  199. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  200. offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  201. var config = context.Config;
  202. bool indexElement = !config.GpuAccessor.QueryHostHasVectorIndexingBug();
  203. if (src1 is AstOperand operand && operand.Type == OperandType.Constant)
  204. {
  205. bool cbIndexable = config.UsedFeatures.HasFlag(Translation.FeatureFlags.CbIndexing);
  206. return OperandManager.GetConstantBufferName(operand.Value, offsetExpr, config.Stage, cbIndexable, indexElement);
  207. }
  208. else
  209. {
  210. string slotExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  211. return OperandManager.GetConstantBufferName(slotExpr, offsetExpr, config.Stage, indexElement);
  212. }
  213. }
  214. public static string LoadLocal(CodeGenContext context, AstOperation operation)
  215. {
  216. return LoadLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  217. }
  218. public static string LoadShared(CodeGenContext context, AstOperation operation)
  219. {
  220. return LoadLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  221. }
  222. private static string LoadLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  223. {
  224. IAstNode src1 = operation.GetSource(0);
  225. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  226. return $"{arrayName}[{offsetExpr}]";
  227. }
  228. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  229. {
  230. IAstNode src1 = operation.GetSource(0);
  231. IAstNode src2 = operation.GetSource(1);
  232. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  233. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  234. return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  235. }
  236. public static string Lod(CodeGenContext context, AstOperation operation)
  237. {
  238. AstTextureOperation texOp = (AstTextureOperation)operation;
  239. int coordsCount = texOp.Type.GetDimensions();
  240. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  241. // TODO: Bindless texture support. For now we just return 0.
  242. if (isBindless)
  243. {
  244. return NumberFormatter.FormatFloat(0);
  245. }
  246. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  247. string indexExpr = null;
  248. if (isIndexed)
  249. {
  250. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  251. }
  252. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  253. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  254. string coordsExpr;
  255. if (coordsCount > 1)
  256. {
  257. string[] elems = new string[coordsCount];
  258. for (int index = 0; index < coordsCount; index++)
  259. {
  260. elems[index] = GetSoureExpr(context, texOp.GetSource(coordsIndex + index), AggregateType.FP32);
  261. }
  262. coordsExpr = "vec" + coordsCount + "(" + string.Join(", ", elems) + ")";
  263. }
  264. else
  265. {
  266. coordsExpr = GetSoureExpr(context, texOp.GetSource(coordsIndex), AggregateType.FP32);
  267. }
  268. return $"textureQueryLod({samplerName}, {coordsExpr}){GetMask(texOp.Index)}";
  269. }
  270. public static string StoreAttribute(CodeGenContext context, AstOperation operation)
  271. {
  272. IAstNode src1 = operation.GetSource(0);
  273. IAstNode src2 = operation.GetSource(1);
  274. IAstNode src3 = operation.GetSource(2);
  275. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  276. {
  277. throw new InvalidOperationException($"First input of {nameof(Instruction.StoreAttribute)} must be a constant operand.");
  278. }
  279. string attrName;
  280. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  281. {
  282. int attrOffset = baseAttr.Value + (operand.Value << 2);
  283. attrName = OperandManager.GetAttributeName(context, attrOffset, perPatch: false, isOutAttr: true);
  284. }
  285. else
  286. {
  287. string attrExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  288. attrExpr = Enclose(attrExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  289. attrName = OperandManager.GetAttributeName(attrExpr, context.Config, isOutAttr: true);
  290. }
  291. string value = GetSoureExpr(context, src3, GetSrcVarType(operation.Inst, 2));
  292. return $"{attrName} = {value}";
  293. }
  294. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  295. {
  296. return StoreLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  297. }
  298. public static string StoreShared(CodeGenContext context, AstOperation operation)
  299. {
  300. return StoreLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  301. }
  302. private static string StoreLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  303. {
  304. IAstNode src1 = operation.GetSource(0);
  305. IAstNode src2 = operation.GetSource(1);
  306. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  307. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  308. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  309. return $"{arrayName}[{offsetExpr}] = {src}";
  310. }
  311. public static string StoreShared16(CodeGenContext context, AstOperation operation)
  312. {
  313. IAstNode src1 = operation.GetSource(0);
  314. IAstNode src2 = operation.GetSource(1);
  315. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  316. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  317. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  318. return $"{HelperFunctionNames.StoreShared16}({offsetExpr}, {src})";
  319. }
  320. public static string StoreShared8(CodeGenContext context, AstOperation operation)
  321. {
  322. IAstNode src1 = operation.GetSource(0);
  323. IAstNode src2 = operation.GetSource(1);
  324. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  325. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  326. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  327. return $"{HelperFunctionNames.StoreShared8}({offsetExpr}, {src})";
  328. }
  329. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  330. {
  331. IAstNode src1 = operation.GetSource(0);
  332. IAstNode src2 = operation.GetSource(1);
  333. IAstNode src3 = operation.GetSource(2);
  334. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  335. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  336. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  337. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  338. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  339. return $"{sb} = {src}";
  340. }
  341. public static string StoreStorage16(CodeGenContext context, AstOperation operation)
  342. {
  343. IAstNode src1 = operation.GetSource(0);
  344. IAstNode src2 = operation.GetSource(1);
  345. IAstNode src3 = operation.GetSource(2);
  346. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  347. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  348. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  349. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  350. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  351. return $"{HelperFunctionNames.StoreStorage16}({indexExpr}, {offsetExpr}, {src})";
  352. }
  353. public static string StoreStorage8(CodeGenContext context, AstOperation operation)
  354. {
  355. IAstNode src1 = operation.GetSource(0);
  356. IAstNode src2 = operation.GetSource(1);
  357. IAstNode src3 = operation.GetSource(2);
  358. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  359. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  360. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  361. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  362. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  363. return $"{HelperFunctionNames.StoreStorage8}({indexExpr}, {offsetExpr}, {src})";
  364. }
  365. public static string TextureSample(CodeGenContext context, AstOperation operation)
  366. {
  367. AstTextureOperation texOp = (AstTextureOperation)operation;
  368. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  369. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  370. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  371. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  372. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  373. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  374. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  375. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  376. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  377. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  378. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  379. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  380. bool colorIsVector = isGather || !isShadow;
  381. SamplerType type = texOp.Type & SamplerType.Mask;
  382. bool is2D = type == SamplerType.Texture2D;
  383. bool isCube = type == SamplerType.TextureCube;
  384. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  385. // If the extension is not supported, just remove the LOD parameter.
  386. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  387. {
  388. hasLodBias = false;
  389. hasLodLevel = false;
  390. }
  391. // Cube shadow samplers with LOD level requires an extension.
  392. // If the extension is not supported, just remove the LOD level parameter.
  393. if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  394. {
  395. hasLodLevel = false;
  396. }
  397. // TODO: Bindless texture support. For now we just return 0.
  398. if (isBindless)
  399. {
  400. string scalarValue = NumberFormatter.FormatFloat(0);
  401. if (colorIsVector)
  402. {
  403. AggregateType outputType = texOp.GetVectorType(AggregateType.FP32);
  404. if ((outputType & AggregateType.ElementCountMask) != 0)
  405. {
  406. return $"{Declarations.GetVarTypeName(context, outputType, precise: false)}({scalarValue})";
  407. }
  408. }
  409. return scalarValue;
  410. }
  411. string texCall = intCoords ? "texelFetch" : "texture";
  412. if (isGather)
  413. {
  414. texCall += "Gather";
  415. }
  416. else if (hasDerivatives)
  417. {
  418. texCall += "Grad";
  419. }
  420. else if (hasLodLevel && !intCoords)
  421. {
  422. texCall += "Lod";
  423. }
  424. if (hasOffset)
  425. {
  426. texCall += "Offset";
  427. }
  428. else if (hasOffsets)
  429. {
  430. texCall += "Offsets";
  431. }
  432. int srcIndex = isBindless ? 1 : 0;
  433. string Src(AggregateType type)
  434. {
  435. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  436. }
  437. string indexExpr = null;
  438. if (isIndexed)
  439. {
  440. indexExpr = Src(AggregateType.S32);
  441. }
  442. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  443. texCall += "(" + samplerName;
  444. int coordsCount = texOp.Type.GetDimensions();
  445. int pCount = coordsCount;
  446. int arrayIndexElem = -1;
  447. if (isArray)
  448. {
  449. arrayIndexElem = pCount++;
  450. }
  451. // The sampler 1D shadow overload expects a
  452. // dummy value on the middle of the vector, who knows why...
  453. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  454. if (hasDummy1DShadowElem)
  455. {
  456. pCount++;
  457. }
  458. if (isShadow && !isGather)
  459. {
  460. pCount++;
  461. }
  462. // On textureGather*, the comparison value is
  463. // always specified as an extra argument.
  464. bool hasExtraCompareArg = isShadow && isGather;
  465. if (pCount == 5)
  466. {
  467. pCount = 4;
  468. hasExtraCompareArg = true;
  469. }
  470. void Append(string str)
  471. {
  472. texCall += ", " + str;
  473. }
  474. AggregateType coordType = intCoords ? AggregateType.S32 : AggregateType.FP32;
  475. string AssemblePVector(int count)
  476. {
  477. if (count > 1)
  478. {
  479. string[] elems = new string[count];
  480. for (int index = 0; index < count; index++)
  481. {
  482. if (arrayIndexElem == index)
  483. {
  484. elems[index] = Src(AggregateType.S32);
  485. if (!intCoords)
  486. {
  487. elems[index] = "float(" + elems[index] + ")";
  488. }
  489. }
  490. else if (index == 1 && hasDummy1DShadowElem)
  491. {
  492. elems[index] = NumberFormatter.FormatFloat(0);
  493. }
  494. else
  495. {
  496. elems[index] = Src(coordType);
  497. }
  498. }
  499. string prefix = intCoords ? "i" : string.Empty;
  500. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  501. }
  502. else
  503. {
  504. return Src(coordType);
  505. }
  506. }
  507. string ApplyScaling(string vector)
  508. {
  509. if (intCoords)
  510. {
  511. if (context.Config.Stage.SupportsRenderScale() &&
  512. !isBindless &&
  513. !isIndexed)
  514. {
  515. int index = context.Config.FindTextureDescriptorIndex(texOp);
  516. if (pCount == 3 && isArray)
  517. {
  518. // The array index is not scaled, just x and y.
  519. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
  520. }
  521. else if (pCount == 2 && !isArray)
  522. {
  523. vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
  524. }
  525. }
  526. }
  527. return vector;
  528. }
  529. Append(ApplyScaling(AssemblePVector(pCount)));
  530. string AssembleDerivativesVector(int count)
  531. {
  532. if (count > 1)
  533. {
  534. string[] elems = new string[count];
  535. for (int index = 0; index < count; index++)
  536. {
  537. elems[index] = Src(AggregateType.FP32);
  538. }
  539. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  540. }
  541. else
  542. {
  543. return Src(AggregateType.FP32);
  544. }
  545. }
  546. if (hasExtraCompareArg)
  547. {
  548. Append(Src(AggregateType.FP32));
  549. }
  550. if (hasDerivatives)
  551. {
  552. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  553. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  554. }
  555. if (isMultisample)
  556. {
  557. Append(Src(AggregateType.S32));
  558. }
  559. else if (hasLodLevel)
  560. {
  561. Append(Src(coordType));
  562. }
  563. string AssembleOffsetVector(int count)
  564. {
  565. if (count > 1)
  566. {
  567. string[] elems = new string[count];
  568. for (int index = 0; index < count; index++)
  569. {
  570. elems[index] = Src(AggregateType.S32);
  571. }
  572. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  573. }
  574. else
  575. {
  576. return Src(AggregateType.S32);
  577. }
  578. }
  579. if (hasOffset)
  580. {
  581. Append(AssembleOffsetVector(coordsCount));
  582. }
  583. else if (hasOffsets)
  584. {
  585. texCall += $", ivec{coordsCount}[4](";
  586. texCall += AssembleOffsetVector(coordsCount) + ", ";
  587. texCall += AssembleOffsetVector(coordsCount) + ", ";
  588. texCall += AssembleOffsetVector(coordsCount) + ", ";
  589. texCall += AssembleOffsetVector(coordsCount) + ")";
  590. }
  591. if (hasLodBias)
  592. {
  593. Append(Src(AggregateType.FP32));
  594. }
  595. // textureGather* optional extra component index,
  596. // not needed for shadow samplers.
  597. if (isGather && !isShadow)
  598. {
  599. Append(Src(AggregateType.S32));
  600. }
  601. texCall += ")" + (colorIsVector ? GetMaskMultiDest(texOp.Index) : "");
  602. return texCall;
  603. }
  604. public static string TextureSize(CodeGenContext context, AstOperation operation)
  605. {
  606. AstTextureOperation texOp = (AstTextureOperation)operation;
  607. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  608. // TODO: Bindless texture support. For now we just return 0.
  609. if (isBindless)
  610. {
  611. return NumberFormatter.FormatInt(0);
  612. }
  613. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  614. string indexExpr = null;
  615. if (isIndexed)
  616. {
  617. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  618. }
  619. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  620. if (texOp.Index == 3)
  621. {
  622. return $"textureQueryLevels({samplerName})";
  623. }
  624. else
  625. {
  626. (TextureDescriptor descriptor, int descriptorIndex) = context.Config.FindTextureDescriptor(texOp);
  627. bool hasLod = !descriptor.Type.HasFlag(SamplerType.Multisample) && descriptor.Type != SamplerType.TextureBuffer;
  628. string texCall;
  629. if (hasLod)
  630. {
  631. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  632. IAstNode lod = operation.GetSource(lodSrcIndex);
  633. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  634. texCall = $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  635. }
  636. else
  637. {
  638. texCall = $"textureSize({samplerName}){GetMask(texOp.Index)}";
  639. }
  640. if (context.Config.Stage.SupportsRenderScale() &&
  641. (texOp.Index < 2 || (texOp.Type & SamplerType.Mask) == SamplerType.Texture3D) &&
  642. !isBindless &&
  643. !isIndexed)
  644. {
  645. texCall = $"Helper_TextureSizeUnscale({texCall}, {descriptorIndex})";
  646. }
  647. return texCall;
  648. }
  649. }
  650. private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage)
  651. {
  652. string sbName = OperandManager.GetShaderStagePrefix(stage);
  653. sbName += "_" + DefaultNames.StorageNamePrefix;
  654. return $"{sbName}[{slotExpr}].{DefaultNames.DataName}[{offsetExpr}]";
  655. }
  656. private static string GetMask(int index)
  657. {
  658. return $".{"rgba".AsSpan(index, 1)}";
  659. }
  660. private static string GetMaskMultiDest(int mask)
  661. {
  662. string swizzle = ".";
  663. for (int i = 0; i < 4; i++)
  664. {
  665. if ((mask & (1 << i)) != 0)
  666. {
  667. swizzle += "xyzw"[i];
  668. }
  669. }
  670. return swizzle;
  671. }
  672. }
  673. }