InstGenMemory.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 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. VariableType srcType = OperandManager.GetNodeDestType(context, src3);
  299. string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.U32);
  300. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  301. return $"{sb} = {src}";
  302. }
  303. public static string TextureSample(CodeGenContext context, AstOperation operation)
  304. {
  305. AstTextureOperation texOp = (AstTextureOperation)operation;
  306. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  307. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  308. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  309. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  310. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  311. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  312. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  313. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  314. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  315. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  316. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  317. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  318. SamplerType type = texOp.Type & SamplerType.Mask;
  319. bool is2D = type == SamplerType.Texture2D;
  320. bool isCube = type == SamplerType.TextureCube;
  321. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  322. // If the extension is not supported, just remove the LOD parameter.
  323. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  324. {
  325. hasLodBias = false;
  326. hasLodLevel = false;
  327. }
  328. // Cube shadow samplers with LOD level requires an extension.
  329. // If the extension is not supported, just remove the LOD level parameter.
  330. if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod())
  331. {
  332. hasLodLevel = false;
  333. }
  334. // TODO: Bindless texture support. For now we just return 0.
  335. if (isBindless)
  336. {
  337. return NumberFormatter.FormatFloat(0);
  338. }
  339. string texCall = intCoords ? "texelFetch" : "texture";
  340. if (isGather)
  341. {
  342. texCall += "Gather";
  343. }
  344. else if (hasDerivatives)
  345. {
  346. texCall += "Grad";
  347. }
  348. else if (hasLodLevel && !intCoords)
  349. {
  350. texCall += "Lod";
  351. }
  352. if (hasOffset)
  353. {
  354. texCall += "Offset";
  355. }
  356. else if (hasOffsets)
  357. {
  358. texCall += "Offsets";
  359. }
  360. int srcIndex = isBindless ? 1 : 0;
  361. string Src(VariableType type)
  362. {
  363. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  364. }
  365. string indexExpr = null;
  366. if (isIndexed)
  367. {
  368. indexExpr = Src(VariableType.S32);
  369. }
  370. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  371. texCall += "(" + samplerName;
  372. int coordsCount = texOp.Type.GetDimensions();
  373. int pCount = coordsCount;
  374. int arrayIndexElem = -1;
  375. if (isArray)
  376. {
  377. arrayIndexElem = pCount++;
  378. }
  379. // The sampler 1D shadow overload expects a
  380. // dummy value on the middle of the vector, who knows why...
  381. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  382. if (hasDummy1DShadowElem)
  383. {
  384. pCount++;
  385. }
  386. if (isShadow && !isGather)
  387. {
  388. pCount++;
  389. }
  390. // On textureGather*, the comparison value is
  391. // always specified as an extra argument.
  392. bool hasExtraCompareArg = isShadow && isGather;
  393. if (pCount == 5)
  394. {
  395. pCount = 4;
  396. hasExtraCompareArg = true;
  397. }
  398. void Append(string str)
  399. {
  400. texCall += ", " + str;
  401. }
  402. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  403. string AssemblePVector(int count)
  404. {
  405. if (count > 1)
  406. {
  407. string[] elems = new string[count];
  408. for (int index = 0; index < count; index++)
  409. {
  410. if (arrayIndexElem == index)
  411. {
  412. elems[index] = Src(VariableType.S32);
  413. if (!intCoords)
  414. {
  415. elems[index] = "float(" + elems[index] + ")";
  416. }
  417. }
  418. else if (index == 1 && hasDummy1DShadowElem)
  419. {
  420. elems[index] = NumberFormatter.FormatFloat(0);
  421. }
  422. else
  423. {
  424. elems[index] = Src(coordType);
  425. }
  426. }
  427. string prefix = intCoords ? "i" : string.Empty;
  428. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  429. }
  430. else
  431. {
  432. return Src(coordType);
  433. }
  434. }
  435. string ApplyScaling(string vector)
  436. {
  437. if (intCoords)
  438. {
  439. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  440. !isBindless &&
  441. !isIndexed)
  442. {
  443. int index = context.FindTextureDescriptorIndex(texOp);
  444. if (pCount == 3 && isArray)
  445. {
  446. // The array index is not scaled, just x and y.
  447. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
  448. }
  449. else if (pCount == 2 && !isArray)
  450. {
  451. vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
  452. }
  453. }
  454. }
  455. return vector;
  456. }
  457. Append(ApplyScaling(AssemblePVector(pCount)));
  458. string AssembleDerivativesVector(int count)
  459. {
  460. if (count > 1)
  461. {
  462. string[] elems = new string[count];
  463. for (int index = 0; index < count; index++)
  464. {
  465. elems[index] = Src(VariableType.F32);
  466. }
  467. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  468. }
  469. else
  470. {
  471. return Src(VariableType.F32);
  472. }
  473. }
  474. if (hasExtraCompareArg)
  475. {
  476. Append(Src(VariableType.F32));
  477. }
  478. if (hasDerivatives)
  479. {
  480. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  481. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  482. }
  483. if (isMultisample)
  484. {
  485. Append(Src(VariableType.S32));
  486. }
  487. else if (hasLodLevel)
  488. {
  489. Append(Src(coordType));
  490. }
  491. string AssembleOffsetVector(int count)
  492. {
  493. if (count > 1)
  494. {
  495. string[] elems = new string[count];
  496. for (int index = 0; index < count; index++)
  497. {
  498. elems[index] = Src(VariableType.S32);
  499. }
  500. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  501. }
  502. else
  503. {
  504. return Src(VariableType.S32);
  505. }
  506. }
  507. if (hasOffset)
  508. {
  509. Append(AssembleOffsetVector(coordsCount));
  510. }
  511. else if (hasOffsets)
  512. {
  513. texCall += $", ivec{coordsCount}[4](";
  514. texCall += AssembleOffsetVector(coordsCount) + ", ";
  515. texCall += AssembleOffsetVector(coordsCount) + ", ";
  516. texCall += AssembleOffsetVector(coordsCount) + ", ";
  517. texCall += AssembleOffsetVector(coordsCount) + ")";
  518. }
  519. if (hasLodBias)
  520. {
  521. Append(Src(VariableType.F32));
  522. }
  523. // textureGather* optional extra component index,
  524. // not needed for shadow samplers.
  525. if (isGather && !isShadow)
  526. {
  527. Append(Src(VariableType.S32));
  528. }
  529. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.Index) : "");
  530. return texCall;
  531. }
  532. public static string TextureSize(CodeGenContext context, AstOperation operation)
  533. {
  534. AstTextureOperation texOp = (AstTextureOperation)operation;
  535. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  536. // TODO: Bindless texture support. For now we just return 0.
  537. if (isBindless)
  538. {
  539. return NumberFormatter.FormatInt(0);
  540. }
  541. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  542. string indexExpr = null;
  543. if (isIndexed)
  544. {
  545. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  546. }
  547. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  548. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  549. IAstNode lod = operation.GetSource(lodSrcIndex);
  550. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  551. if (texOp.Index == 3)
  552. {
  553. return $"textureQueryLevels({samplerName})";
  554. }
  555. else
  556. {
  557. string texCall = $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  558. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  559. !isBindless &&
  560. !isIndexed)
  561. {
  562. int index = context.FindTextureDescriptorIndex(texOp);
  563. texCall = "Helper_TextureSizeUnscale(" + texCall + ", " + index + ")";
  564. }
  565. return texCall;
  566. }
  567. }
  568. private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage)
  569. {
  570. string sbName = OperandManager.GetShaderStagePrefix(stage);
  571. sbName += "_" + DefaultNames.StorageNamePrefix;
  572. return $"{sbName}[{slotExpr}].{DefaultNames.DataName}[{offsetExpr}]";
  573. }
  574. private static string GetMask(int index)
  575. {
  576. return '.' + "rgba".Substring(index, 1);
  577. }
  578. }
  579. }