InstGenMemory.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 Load(CodeGenContext context, AstOperation operation)
  174. {
  175. return GenerateLoadOrStore(context, operation, isStore: false);
  176. }
  177. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  178. {
  179. IAstNode src1 = operation.GetSource(0);
  180. IAstNode src2 = operation.GetSource(1);
  181. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  182. offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  183. var config = context.Config;
  184. bool indexElement = !config.GpuAccessor.QueryHostHasVectorIndexingBug();
  185. if (src1 is AstOperand operand && operand.Type == OperandType.Constant)
  186. {
  187. bool cbIndexable = config.UsedFeatures.HasFlag(Translation.FeatureFlags.CbIndexing);
  188. return OperandManager.GetConstantBufferName(operand.Value, offsetExpr, config.Stage, cbIndexable, indexElement);
  189. }
  190. else
  191. {
  192. string slotExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  193. return OperandManager.GetConstantBufferName(slotExpr, offsetExpr, config.Stage, indexElement);
  194. }
  195. }
  196. public static string LoadLocal(CodeGenContext context, AstOperation operation)
  197. {
  198. return LoadLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  199. }
  200. public static string LoadShared(CodeGenContext context, AstOperation operation)
  201. {
  202. return LoadLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  203. }
  204. private static string LoadLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  205. {
  206. IAstNode src1 = operation.GetSource(0);
  207. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  208. return $"{arrayName}[{offsetExpr}]";
  209. }
  210. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  211. {
  212. IAstNode src1 = operation.GetSource(0);
  213. IAstNode src2 = operation.GetSource(1);
  214. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  215. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  216. return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  217. }
  218. public static string Lod(CodeGenContext context, AstOperation operation)
  219. {
  220. AstTextureOperation texOp = (AstTextureOperation)operation;
  221. int coordsCount = texOp.Type.GetDimensions();
  222. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  223. // TODO: Bindless texture support. For now we just return 0.
  224. if (isBindless)
  225. {
  226. return NumberFormatter.FormatFloat(0);
  227. }
  228. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  229. string indexExpr = null;
  230. if (isIndexed)
  231. {
  232. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  233. }
  234. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  235. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  236. string coordsExpr;
  237. if (coordsCount > 1)
  238. {
  239. string[] elems = new string[coordsCount];
  240. for (int index = 0; index < coordsCount; index++)
  241. {
  242. elems[index] = GetSoureExpr(context, texOp.GetSource(coordsIndex + index), AggregateType.FP32);
  243. }
  244. coordsExpr = "vec" + coordsCount + "(" + string.Join(", ", elems) + ")";
  245. }
  246. else
  247. {
  248. coordsExpr = GetSoureExpr(context, texOp.GetSource(coordsIndex), AggregateType.FP32);
  249. }
  250. return $"textureQueryLod({samplerName}, {coordsExpr}){GetMask(texOp.Index)}";
  251. }
  252. public static string Store(CodeGenContext context, AstOperation operation)
  253. {
  254. return GenerateLoadOrStore(context, operation, isStore: true);
  255. }
  256. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  257. {
  258. return StoreLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  259. }
  260. public static string StoreShared(CodeGenContext context, AstOperation operation)
  261. {
  262. return StoreLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  263. }
  264. private static string StoreLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  265. {
  266. IAstNode src1 = operation.GetSource(0);
  267. IAstNode src2 = operation.GetSource(1);
  268. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  269. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  270. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  271. return $"{arrayName}[{offsetExpr}] = {src}";
  272. }
  273. public static string StoreShared16(CodeGenContext context, AstOperation operation)
  274. {
  275. IAstNode src1 = operation.GetSource(0);
  276. IAstNode src2 = operation.GetSource(1);
  277. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  278. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  279. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  280. return $"{HelperFunctionNames.StoreShared16}({offsetExpr}, {src})";
  281. }
  282. public static string StoreShared8(CodeGenContext context, AstOperation operation)
  283. {
  284. IAstNode src1 = operation.GetSource(0);
  285. IAstNode src2 = operation.GetSource(1);
  286. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  287. AggregateType srcType = OperandManager.GetNodeDestType(context, src2);
  288. string src = TypeConversion.ReinterpretCast(context, src2, srcType, AggregateType.U32);
  289. return $"{HelperFunctionNames.StoreShared8}({offsetExpr}, {src})";
  290. }
  291. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  292. {
  293. IAstNode src1 = operation.GetSource(0);
  294. IAstNode src2 = operation.GetSource(1);
  295. IAstNode src3 = operation.GetSource(2);
  296. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  297. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  298. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  299. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  300. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  301. return $"{sb} = {src}";
  302. }
  303. public static string StoreStorage16(CodeGenContext context, AstOperation operation)
  304. {
  305. IAstNode src1 = operation.GetSource(0);
  306. IAstNode src2 = operation.GetSource(1);
  307. IAstNode src3 = operation.GetSource(2);
  308. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  309. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  310. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  311. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  312. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  313. return $"{HelperFunctionNames.StoreStorage16}({indexExpr}, {offsetExpr}, {src})";
  314. }
  315. public static string StoreStorage8(CodeGenContext context, AstOperation operation)
  316. {
  317. IAstNode src1 = operation.GetSource(0);
  318. IAstNode src2 = operation.GetSource(1);
  319. IAstNode src3 = operation.GetSource(2);
  320. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  321. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  322. AggregateType srcType = OperandManager.GetNodeDestType(context, src3);
  323. string src = TypeConversion.ReinterpretCast(context, src3, srcType, AggregateType.U32);
  324. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  325. return $"{HelperFunctionNames.StoreStorage8}({indexExpr}, {offsetExpr}, {src})";
  326. }
  327. public static string TextureSample(CodeGenContext context, AstOperation operation)
  328. {
  329. AstTextureOperation texOp = (AstTextureOperation)operation;
  330. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  331. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  332. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  333. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  334. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  335. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  336. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  337. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  338. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  339. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  340. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  341. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  342. bool colorIsVector = isGather || !isShadow;
  343. SamplerType type = texOp.Type & SamplerType.Mask;
  344. bool is2D = type == SamplerType.Texture2D;
  345. bool isCube = type == SamplerType.TextureCube;
  346. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  347. // If the extension is not supported, just remove the LOD parameter.
  348. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  349. {
  350. hasLodBias = false;
  351. hasLodLevel = false;
  352. }
  353. // Cube shadow samplers with LOD level requires an extension.
  354. // If the extension is not supported, just remove the LOD level parameter.
  355. if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  356. {
  357. hasLodLevel = false;
  358. }
  359. // TODO: Bindless texture support. For now we just return 0.
  360. if (isBindless)
  361. {
  362. string scalarValue = NumberFormatter.FormatFloat(0);
  363. if (colorIsVector)
  364. {
  365. AggregateType outputType = texOp.GetVectorType(AggregateType.FP32);
  366. if ((outputType & AggregateType.ElementCountMask) != 0)
  367. {
  368. return $"{Declarations.GetVarTypeName(context, outputType, precise: false)}({scalarValue})";
  369. }
  370. }
  371. return scalarValue;
  372. }
  373. string texCall = intCoords ? "texelFetch" : "texture";
  374. if (isGather)
  375. {
  376. texCall += "Gather";
  377. }
  378. else if (hasDerivatives)
  379. {
  380. texCall += "Grad";
  381. }
  382. else if (hasLodLevel && !intCoords)
  383. {
  384. texCall += "Lod";
  385. }
  386. if (hasOffset)
  387. {
  388. texCall += "Offset";
  389. }
  390. else if (hasOffsets)
  391. {
  392. texCall += "Offsets";
  393. }
  394. int srcIndex = isBindless ? 1 : 0;
  395. string Src(AggregateType type)
  396. {
  397. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  398. }
  399. string indexExpr = null;
  400. if (isIndexed)
  401. {
  402. indexExpr = Src(AggregateType.S32);
  403. }
  404. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  405. texCall += "(" + samplerName;
  406. int coordsCount = texOp.Type.GetDimensions();
  407. int pCount = coordsCount;
  408. int arrayIndexElem = -1;
  409. if (isArray)
  410. {
  411. arrayIndexElem = pCount++;
  412. }
  413. // The sampler 1D shadow overload expects a
  414. // dummy value on the middle of the vector, who knows why...
  415. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  416. if (hasDummy1DShadowElem)
  417. {
  418. pCount++;
  419. }
  420. if (isShadow && !isGather)
  421. {
  422. pCount++;
  423. }
  424. // On textureGather*, the comparison value is
  425. // always specified as an extra argument.
  426. bool hasExtraCompareArg = isShadow && isGather;
  427. if (pCount == 5)
  428. {
  429. pCount = 4;
  430. hasExtraCompareArg = true;
  431. }
  432. void Append(string str)
  433. {
  434. texCall += ", " + str;
  435. }
  436. AggregateType coordType = intCoords ? AggregateType.S32 : AggregateType.FP32;
  437. string AssemblePVector(int count)
  438. {
  439. if (count > 1)
  440. {
  441. string[] elems = new string[count];
  442. for (int index = 0; index < count; index++)
  443. {
  444. if (arrayIndexElem == index)
  445. {
  446. elems[index] = Src(AggregateType.S32);
  447. if (!intCoords)
  448. {
  449. elems[index] = "float(" + elems[index] + ")";
  450. }
  451. }
  452. else if (index == 1 && hasDummy1DShadowElem)
  453. {
  454. elems[index] = NumberFormatter.FormatFloat(0);
  455. }
  456. else
  457. {
  458. elems[index] = Src(coordType);
  459. }
  460. }
  461. string prefix = intCoords ? "i" : string.Empty;
  462. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  463. }
  464. else
  465. {
  466. return Src(coordType);
  467. }
  468. }
  469. string ApplyScaling(string vector)
  470. {
  471. if (intCoords)
  472. {
  473. if (context.Config.Stage.SupportsRenderScale() &&
  474. !isBindless &&
  475. !isIndexed)
  476. {
  477. int index = context.Config.FindTextureDescriptorIndex(texOp);
  478. if (pCount == 3 && isArray)
  479. {
  480. // The array index is not scaled, just x and y.
  481. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
  482. }
  483. else if (pCount == 2 && !isArray)
  484. {
  485. vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
  486. }
  487. }
  488. }
  489. return vector;
  490. }
  491. string ApplyBias(string vector)
  492. {
  493. int gatherBiasPrecision = context.Config.GpuAccessor.QueryHostGatherBiasPrecision();
  494. if (isGather && gatherBiasPrecision != 0)
  495. {
  496. // GPU requires texture gather to be slightly offset to match NVIDIA behaviour when point is exactly between two texels.
  497. // Offset by the gather precision divided by 2 to correct for rounding.
  498. if (pCount == 1)
  499. {
  500. vector = $"{vector} + (1.0 / (float(textureSize({samplerName}, 0)) * float({1 << (gatherBiasPrecision + 1)})))";
  501. }
  502. else
  503. {
  504. vector = $"{vector} + (1.0 / (vec{pCount}(textureSize({samplerName}, 0).{"xyz".Substring(0, pCount)}) * float({1 << (gatherBiasPrecision + 1)})))";
  505. }
  506. }
  507. return vector;
  508. }
  509. Append(ApplyBias(ApplyScaling(AssemblePVector(pCount))));
  510. string AssembleDerivativesVector(int count)
  511. {
  512. if (count > 1)
  513. {
  514. string[] elems = new string[count];
  515. for (int index = 0; index < count; index++)
  516. {
  517. elems[index] = Src(AggregateType.FP32);
  518. }
  519. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  520. }
  521. else
  522. {
  523. return Src(AggregateType.FP32);
  524. }
  525. }
  526. if (hasExtraCompareArg)
  527. {
  528. Append(Src(AggregateType.FP32));
  529. }
  530. if (hasDerivatives)
  531. {
  532. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  533. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  534. }
  535. if (isMultisample)
  536. {
  537. Append(Src(AggregateType.S32));
  538. }
  539. else if (hasLodLevel)
  540. {
  541. Append(Src(coordType));
  542. }
  543. string AssembleOffsetVector(int count)
  544. {
  545. if (count > 1)
  546. {
  547. string[] elems = new string[count];
  548. for (int index = 0; index < count; index++)
  549. {
  550. elems[index] = Src(AggregateType.S32);
  551. }
  552. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  553. }
  554. else
  555. {
  556. return Src(AggregateType.S32);
  557. }
  558. }
  559. if (hasOffset)
  560. {
  561. Append(AssembleOffsetVector(coordsCount));
  562. }
  563. else if (hasOffsets)
  564. {
  565. texCall += $", ivec{coordsCount}[4](";
  566. texCall += AssembleOffsetVector(coordsCount) + ", ";
  567. texCall += AssembleOffsetVector(coordsCount) + ", ";
  568. texCall += AssembleOffsetVector(coordsCount) + ", ";
  569. texCall += AssembleOffsetVector(coordsCount) + ")";
  570. }
  571. if (hasLodBias)
  572. {
  573. Append(Src(AggregateType.FP32));
  574. }
  575. // textureGather* optional extra component index,
  576. // not needed for shadow samplers.
  577. if (isGather && !isShadow)
  578. {
  579. Append(Src(AggregateType.S32));
  580. }
  581. texCall += ")" + (colorIsVector ? GetMaskMultiDest(texOp.Index) : "");
  582. return texCall;
  583. }
  584. public static string TextureSize(CodeGenContext context, AstOperation operation)
  585. {
  586. AstTextureOperation texOp = (AstTextureOperation)operation;
  587. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  588. // TODO: Bindless texture support. For now we just return 0.
  589. if (isBindless)
  590. {
  591. return NumberFormatter.FormatInt(0);
  592. }
  593. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  594. string indexExpr = null;
  595. if (isIndexed)
  596. {
  597. indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32);
  598. }
  599. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  600. if (texOp.Index == 3)
  601. {
  602. return $"textureQueryLevels({samplerName})";
  603. }
  604. else
  605. {
  606. (TextureDescriptor descriptor, int descriptorIndex) = context.Config.FindTextureDescriptor(texOp);
  607. bool hasLod = !descriptor.Type.HasFlag(SamplerType.Multisample) && descriptor.Type != SamplerType.TextureBuffer;
  608. string texCall;
  609. if (hasLod)
  610. {
  611. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  612. IAstNode lod = operation.GetSource(lodSrcIndex);
  613. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  614. texCall = $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  615. }
  616. else
  617. {
  618. texCall = $"textureSize({samplerName}){GetMask(texOp.Index)}";
  619. }
  620. if (context.Config.Stage.SupportsRenderScale() &&
  621. (texOp.Index < 2 || (texOp.Type & SamplerType.Mask) == SamplerType.Texture3D) &&
  622. !isBindless &&
  623. !isIndexed)
  624. {
  625. texCall = $"Helper_TextureSizeUnscale({texCall}, {descriptorIndex})";
  626. }
  627. return texCall;
  628. }
  629. }
  630. private static string GenerateLoadOrStore(CodeGenContext context, AstOperation operation, bool isStore)
  631. {
  632. StorageKind storageKind = operation.StorageKind;
  633. string varName;
  634. AggregateType varType;
  635. int srcIndex = 0;
  636. switch (storageKind)
  637. {
  638. case StorageKind.Input:
  639. case StorageKind.InputPerPatch:
  640. case StorageKind.Output:
  641. case StorageKind.OutputPerPatch:
  642. if (!(operation.GetSource(srcIndex++) is AstOperand varId) || varId.Type != OperandType.Constant)
  643. {
  644. throw new InvalidOperationException($"First input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  645. }
  646. IoVariable ioVariable = (IoVariable)varId.Value;
  647. bool isOutput = storageKind.IsOutput();
  648. bool isPerPatch = storageKind.IsPerPatch();
  649. int location = -1;
  650. int component = 0;
  651. if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput))
  652. {
  653. if (!(operation.GetSource(srcIndex++) is AstOperand vecIndex) || vecIndex.Type != OperandType.Constant)
  654. {
  655. throw new InvalidOperationException($"Second input of {operation.Inst} with {storageKind} storage must be a constant operand.");
  656. }
  657. location = vecIndex.Value;
  658. if (operation.SourcesCount > srcIndex &&
  659. operation.GetSource(srcIndex) is AstOperand elemIndex &&
  660. elemIndex.Type == OperandType.Constant &&
  661. context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput))
  662. {
  663. component = elemIndex.Value;
  664. srcIndex++;
  665. }
  666. }
  667. (varName, varType) = IoMap.GetGlslVariable(context.Config, ioVariable, location, component, isOutput, isPerPatch);
  668. if (IoMap.IsPerVertexBuiltIn(context.Config.Stage, ioVariable, isOutput))
  669. {
  670. // Since those exist both as input and output on geometry and tessellation shaders,
  671. // we need the gl_in and gl_out prefixes to disambiguate.
  672. if (storageKind == StorageKind.Input)
  673. {
  674. string expr = GetSoureExpr(context, operation.GetSource(srcIndex++), AggregateType.S32);
  675. varName = $"gl_in[{expr}].{varName}";
  676. }
  677. else if (storageKind == StorageKind.Output)
  678. {
  679. string expr = GetSoureExpr(context, operation.GetSource(srcIndex++), AggregateType.S32);
  680. varName = $"gl_out[{expr}].{varName}";
  681. }
  682. }
  683. int firstSrcIndex = srcIndex;
  684. int inputsCount = isStore ? operation.SourcesCount - 1 : operation.SourcesCount;
  685. for (; srcIndex < inputsCount; srcIndex++)
  686. {
  687. IAstNode src = operation.GetSource(srcIndex);
  688. if ((varType & AggregateType.ElementCountMask) != 0 &&
  689. srcIndex == inputsCount - 1 &&
  690. src is AstOperand elementIndex &&
  691. elementIndex.Type == OperandType.Constant)
  692. {
  693. varName += "." + "xyzw"[elementIndex.Value & 3];
  694. }
  695. else if (srcIndex == firstSrcIndex && context.Config.Stage == ShaderStage.TessellationControl && storageKind == StorageKind.Output)
  696. {
  697. // GLSL requires that for tessellation control shader outputs,
  698. // that the index expression must be *exactly* "gl_InvocationID",
  699. // otherwise the compilation fails.
  700. // TODO: Get rid of this and use expression propagation to make sure we generate the correct code from IR.
  701. varName += "[gl_InvocationID]";
  702. }
  703. else
  704. {
  705. varName += $"[{GetSoureExpr(context, src, AggregateType.S32)}]";
  706. }
  707. }
  708. break;
  709. default:
  710. throw new InvalidOperationException($"Invalid storage kind {storageKind}.");
  711. }
  712. if (isStore)
  713. {
  714. varType &= AggregateType.ElementTypeMask;
  715. varName = $"{varName} = {GetSoureExpr(context, operation.GetSource(srcIndex), varType)}";
  716. }
  717. return varName;
  718. }
  719. private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage)
  720. {
  721. string sbName = OperandManager.GetShaderStagePrefix(stage);
  722. sbName += "_" + DefaultNames.StorageNamePrefix;
  723. return $"{sbName}[{slotExpr}].{DefaultNames.DataName}[{offsetExpr}]";
  724. }
  725. private static string GetMask(int index)
  726. {
  727. return $".{"rgba".AsSpan(index, 1)}";
  728. }
  729. private static string GetMaskMultiDest(int mask)
  730. {
  731. string swizzle = ".";
  732. for (int i = 0; i < 4; i++)
  733. {
  734. if ((mask & (1 << i)) != 0)
  735. {
  736. swizzle += "xyzw"[i];
  737. }
  738. }
  739. return swizzle;
  740. }
  741. }
  742. }