InstGenMemory.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. return OperandManager.GetAttributeName(baseAttr.Value + (operand.Value << 2), context.Config, isOutAttr: false, indexExpr);
  166. }
  167. else
  168. {
  169. string attrExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  170. attrExpr = Enclose(attrExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  171. return OperandManager.GetAttributeName(attrExpr, context.Config, isOutAttr: false, indexExpr);
  172. }
  173. }
  174. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  175. {
  176. IAstNode src1 = operation.GetSource(0);
  177. IAstNode src2 = operation.GetSource(1);
  178. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  179. offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  180. var config = context.Config;
  181. bool indexElement = !config.GpuAccessor.QueryHostHasVectorIndexingBug();
  182. if (src1 is AstOperand operand && operand.Type == OperandType.Constant)
  183. {
  184. bool cbIndexable = config.UsedFeatures.HasFlag(Translation.FeatureFlags.CbIndexing);
  185. return OperandManager.GetConstantBufferName(operand.Value, offsetExpr, config.Stage, cbIndexable, indexElement);
  186. }
  187. else
  188. {
  189. string slotExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  190. return OperandManager.GetConstantBufferName(slotExpr, offsetExpr, config.Stage, indexElement);
  191. }
  192. }
  193. public static string LoadLocal(CodeGenContext context, AstOperation operation)
  194. {
  195. return LoadLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  196. }
  197. public static string LoadShared(CodeGenContext context, AstOperation operation)
  198. {
  199. return LoadLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  200. }
  201. private static string LoadLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  202. {
  203. IAstNode src1 = operation.GetSource(0);
  204. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  205. return $"{arrayName}[{offsetExpr}]";
  206. }
  207. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  208. {
  209. IAstNode src1 = operation.GetSource(0);
  210. IAstNode src2 = operation.GetSource(1);
  211. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  212. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  213. return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  214. }
  215. public static string Lod(CodeGenContext context, AstOperation operation)
  216. {
  217. AstTextureOperation texOp = (AstTextureOperation)operation;
  218. int coordsCount = texOp.Type.GetDimensions();
  219. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  220. // TODO: Bindless texture support. For now we just return 0.
  221. if (isBindless)
  222. {
  223. return NumberFormatter.FormatFloat(0);
  224. }
  225. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  226. string indexExpr = null;
  227. if (isIndexed)
  228. {
  229. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  230. }
  231. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  232. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  233. string coordsExpr;
  234. if (coordsCount > 1)
  235. {
  236. string[] elems = new string[coordsCount];
  237. for (int index = 0; index < coordsCount; index++)
  238. {
  239. elems[index] = GetSoureExpr(context, texOp.GetSource(coordsIndex + index), VariableType.F32);
  240. }
  241. coordsExpr = "vec" + coordsCount + "(" + string.Join(", ", elems) + ")";
  242. }
  243. else
  244. {
  245. coordsExpr = GetSoureExpr(context, texOp.GetSource(coordsIndex), VariableType.F32);
  246. }
  247. return $"textureQueryLod({samplerName}, {coordsExpr}){GetMask(texOp.Index)}";
  248. }
  249. public static string StoreAttribute(CodeGenContext context, AstOperation operation)
  250. {
  251. IAstNode src1 = operation.GetSource(0);
  252. IAstNode src2 = operation.GetSource(1);
  253. IAstNode src3 = operation.GetSource(2);
  254. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  255. {
  256. throw new InvalidOperationException($"First input of {nameof(Instruction.StoreAttribute)} must be a constant operand.");
  257. }
  258. string attrName;
  259. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  260. {
  261. attrName = OperandManager.GetAttributeName(baseAttr.Value + (operand.Value << 2), context.Config, isOutAttr: true);
  262. }
  263. else
  264. {
  265. string attrExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  266. attrExpr = Enclose(attrExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  267. attrName = OperandManager.GetAttributeName(attrExpr, context.Config, isOutAttr: true);
  268. }
  269. string value = GetSoureExpr(context, src3, GetSrcVarType(operation.Inst, 2));
  270. return $"{attrName} = {value}";
  271. }
  272. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  273. {
  274. return StoreLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  275. }
  276. public static string StoreShared(CodeGenContext context, AstOperation operation)
  277. {
  278. return StoreLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  279. }
  280. private static string StoreLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  281. {
  282. IAstNode src1 = operation.GetSource(0);
  283. IAstNode src2 = operation.GetSource(1);
  284. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  285. VariableType srcType = OperandManager.GetNodeDestType(context, src2);
  286. string src = TypeConversion.ReinterpretCast(context, src2, srcType, VariableType.U32);
  287. return $"{arrayName}[{offsetExpr}] = {src}";
  288. }
  289. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  290. {
  291. IAstNode src1 = operation.GetSource(0);
  292. IAstNode src2 = operation.GetSource(1);
  293. IAstNode src3 = operation.GetSource(2);
  294. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  295. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  296. VariableType srcType = OperandManager.GetNodeDestType(context, src3);
  297. string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.U32);
  298. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  299. return $"{sb} = {src}";
  300. }
  301. public static string TextureSample(CodeGenContext context, AstOperation operation)
  302. {
  303. AstTextureOperation texOp = (AstTextureOperation)operation;
  304. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  305. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  306. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  307. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  308. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  309. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  310. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  311. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  312. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  313. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  314. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  315. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  316. SamplerType type = texOp.Type & SamplerType.Mask;
  317. bool is2D = type == SamplerType.Texture2D;
  318. bool isCube = type == SamplerType.TextureCube;
  319. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  320. // If the extension is not supported, just remove the LOD parameter.
  321. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  322. {
  323. hasLodBias = false;
  324. hasLodLevel = false;
  325. }
  326. // Cube shadow samplers with LOD level requires an extension.
  327. // If the extension is not supported, just remove the LOD level parameter.
  328. if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  329. {
  330. hasLodLevel = false;
  331. }
  332. // TODO: Bindless texture support. For now we just return 0.
  333. if (isBindless)
  334. {
  335. return NumberFormatter.FormatFloat(0);
  336. }
  337. string texCall = intCoords ? "texelFetch" : "texture";
  338. if (isGather)
  339. {
  340. texCall += "Gather";
  341. }
  342. else if (hasDerivatives)
  343. {
  344. texCall += "Grad";
  345. }
  346. else if (hasLodLevel && !intCoords)
  347. {
  348. texCall += "Lod";
  349. }
  350. if (hasOffset)
  351. {
  352. texCall += "Offset";
  353. }
  354. else if (hasOffsets)
  355. {
  356. texCall += "Offsets";
  357. }
  358. int srcIndex = isBindless ? 1 : 0;
  359. string Src(VariableType type)
  360. {
  361. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  362. }
  363. string indexExpr = null;
  364. if (isIndexed)
  365. {
  366. indexExpr = Src(VariableType.S32);
  367. }
  368. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  369. texCall += "(" + samplerName;
  370. int coordsCount = texOp.Type.GetDimensions();
  371. int pCount = coordsCount;
  372. int arrayIndexElem = -1;
  373. if (isArray)
  374. {
  375. arrayIndexElem = pCount++;
  376. }
  377. // The sampler 1D shadow overload expects a
  378. // dummy value on the middle of the vector, who knows why...
  379. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  380. if (hasDummy1DShadowElem)
  381. {
  382. pCount++;
  383. }
  384. if (isShadow && !isGather)
  385. {
  386. pCount++;
  387. }
  388. // On textureGather*, the comparison value is
  389. // always specified as an extra argument.
  390. bool hasExtraCompareArg = isShadow && isGather;
  391. if (pCount == 5)
  392. {
  393. pCount = 4;
  394. hasExtraCompareArg = true;
  395. }
  396. void Append(string str)
  397. {
  398. texCall += ", " + str;
  399. }
  400. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  401. string AssemblePVector(int count)
  402. {
  403. if (count > 1)
  404. {
  405. string[] elems = new string[count];
  406. for (int index = 0; index < count; index++)
  407. {
  408. if (arrayIndexElem == index)
  409. {
  410. elems[index] = Src(VariableType.S32);
  411. if (!intCoords)
  412. {
  413. elems[index] = "float(" + elems[index] + ")";
  414. }
  415. }
  416. else if (index == 1 && hasDummy1DShadowElem)
  417. {
  418. elems[index] = NumberFormatter.FormatFloat(0);
  419. }
  420. else
  421. {
  422. elems[index] = Src(coordType);
  423. }
  424. }
  425. string prefix = intCoords ? "i" : string.Empty;
  426. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  427. }
  428. else
  429. {
  430. return Src(coordType);
  431. }
  432. }
  433. string ApplyScaling(string vector)
  434. {
  435. if (intCoords)
  436. {
  437. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  438. !isBindless &&
  439. !isIndexed)
  440. {
  441. int index = context.FindTextureDescriptorIndex(texOp);
  442. if (pCount == 3 && isArray)
  443. {
  444. // The array index is not scaled, just x and y.
  445. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
  446. }
  447. else if (pCount == 2 && !isArray)
  448. {
  449. vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
  450. }
  451. }
  452. }
  453. return vector;
  454. }
  455. Append(ApplyScaling(AssemblePVector(pCount)));
  456. string AssembleDerivativesVector(int count)
  457. {
  458. if (count > 1)
  459. {
  460. string[] elems = new string[count];
  461. for (int index = 0; index < count; index++)
  462. {
  463. elems[index] = Src(VariableType.F32);
  464. }
  465. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  466. }
  467. else
  468. {
  469. return Src(VariableType.F32);
  470. }
  471. }
  472. if (hasExtraCompareArg)
  473. {
  474. Append(Src(VariableType.F32));
  475. }
  476. if (hasDerivatives)
  477. {
  478. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  479. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  480. }
  481. if (isMultisample)
  482. {
  483. Append(Src(VariableType.S32));
  484. }
  485. else if (hasLodLevel)
  486. {
  487. Append(Src(coordType));
  488. }
  489. string AssembleOffsetVector(int count)
  490. {
  491. if (count > 1)
  492. {
  493. string[] elems = new string[count];
  494. for (int index = 0; index < count; index++)
  495. {
  496. elems[index] = Src(VariableType.S32);
  497. }
  498. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  499. }
  500. else
  501. {
  502. return Src(VariableType.S32);
  503. }
  504. }
  505. if (hasOffset)
  506. {
  507. Append(AssembleOffsetVector(coordsCount));
  508. }
  509. else if (hasOffsets)
  510. {
  511. texCall += $", ivec{coordsCount}[4](";
  512. texCall += AssembleOffsetVector(coordsCount) + ", ";
  513. texCall += AssembleOffsetVector(coordsCount) + ", ";
  514. texCall += AssembleOffsetVector(coordsCount) + ", ";
  515. texCall += AssembleOffsetVector(coordsCount) + ")";
  516. }
  517. if (hasLodBias)
  518. {
  519. Append(Src(VariableType.F32));
  520. }
  521. // textureGather* optional extra component index,
  522. // not needed for shadow samplers.
  523. if (isGather && !isShadow)
  524. {
  525. Append(Src(VariableType.S32));
  526. }
  527. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.Index) : "");
  528. return texCall;
  529. }
  530. public static string TextureSize(CodeGenContext context, AstOperation operation)
  531. {
  532. AstTextureOperation texOp = (AstTextureOperation)operation;
  533. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  534. // TODO: Bindless texture support. For now we just return 0.
  535. if (isBindless)
  536. {
  537. return NumberFormatter.FormatInt(0);
  538. }
  539. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  540. string indexExpr = null;
  541. if (isIndexed)
  542. {
  543. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  544. }
  545. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  546. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  547. IAstNode lod = operation.GetSource(lodSrcIndex);
  548. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  549. if (texOp.Index == 3)
  550. {
  551. return $"textureQueryLevels({samplerName})";
  552. }
  553. else
  554. {
  555. string texCall = $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  556. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  557. !isBindless &&
  558. !isIndexed)
  559. {
  560. int index = context.FindTextureDescriptorIndex(texOp);
  561. texCall = "Helper_TextureSizeUnscale(" + texCall + ", " + index + ")";
  562. }
  563. return texCall;
  564. }
  565. }
  566. private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage)
  567. {
  568. string sbName = OperandManager.GetShaderStagePrefix(stage);
  569. sbName += "_" + DefaultNames.StorageNamePrefix;
  570. return $"{sbName}[{slotExpr}].{DefaultNames.DataName}[{offsetExpr}]";
  571. }
  572. private static string GetMask(int index)
  573. {
  574. return '.' + "rgba".Substring(index, 1);
  575. }
  576. }
  577. }