InstGenMemory.cs 29 KB

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