InstGenMemory.cs 31 KB

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