InstGenMemory.cs 29 KB

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