InstGenMemory.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 == Instruction.ImageLoad ? NumberFormatter.FormatFloat(0) : "// imageStore(bindless)";
  18. }
  19. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  20. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  21. string texCall = texOp.Inst == Instruction.ImageLoad ? "imageLoad" : "imageStore";
  22. int srcIndex = isBindless ? 1 : 0;
  23. string Src(VariableType type)
  24. {
  25. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  26. }
  27. string indexExpr = null;
  28. if (isIndexed)
  29. {
  30. indexExpr = Src(VariableType.S32);
  31. }
  32. string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr);
  33. texCall += "(" + imageName;
  34. int coordsCount = texOp.Type.GetDimensions();
  35. int pCount = coordsCount + (isArray ? 1 : 0);
  36. void Append(string str)
  37. {
  38. texCall += ", " + str;
  39. }
  40. string ApplyScaling(string vector)
  41. {
  42. int index = context.FindImageDescriptorIndex(texOp);
  43. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  44. texOp.Inst == Instruction.ImageLoad &&
  45. !isBindless &&
  46. !isIndexed)
  47. {
  48. // Image scales start after texture ones.
  49. int scaleIndex = context.Config.GetTextureDescriptors().Length + index;
  50. if (pCount == 3 && isArray)
  51. {
  52. // The array index is not scaled, just x and y.
  53. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + scaleIndex + "), (" + vector + ").z)";
  54. }
  55. else if (pCount == 2 && !isArray)
  56. {
  57. vector = "Helper_TexelFetchScale(" + vector + ", " + scaleIndex + ")";
  58. }
  59. }
  60. return vector;
  61. }
  62. if (pCount > 1)
  63. {
  64. string[] elems = new string[pCount];
  65. for (int index = 0; index < pCount; index++)
  66. {
  67. elems[index] = Src(VariableType.S32);
  68. }
  69. Append(ApplyScaling("ivec" + pCount + "(" + string.Join(", ", elems) + ")"));
  70. }
  71. else
  72. {
  73. Append(Src(VariableType.S32));
  74. }
  75. if (texOp.Inst == Instruction.ImageStore)
  76. {
  77. int texIndex = context.FindImageDescriptorIndex(texOp);
  78. VariableType type = texOp.Format.GetComponentType();
  79. string[] cElems = new string[4];
  80. for (int index = 0; index < 4; index++)
  81. {
  82. if (srcIndex < texOp.SourcesCount)
  83. {
  84. cElems[index] = Src(type);
  85. }
  86. else
  87. {
  88. cElems[index] = type switch
  89. {
  90. VariableType.S32 => NumberFormatter.FormatInt(0),
  91. VariableType.U32 => NumberFormatter.FormatUint(0),
  92. _ => NumberFormatter.FormatFloat(0)
  93. };
  94. }
  95. }
  96. string prefix = type switch
  97. {
  98. VariableType.S32 => "i",
  99. VariableType.U32 => "u",
  100. _ => string.Empty
  101. };
  102. Append(prefix + "vec4(" + string.Join(", ", cElems) + ")");
  103. }
  104. texCall += ")" + (texOp.Inst == Instruction.ImageLoad ? GetMask(texOp.Index) : "");
  105. return texCall;
  106. }
  107. public static string LoadAttribute(CodeGenContext context, AstOperation operation)
  108. {
  109. IAstNode src1 = operation.GetSource(0);
  110. IAstNode src2 = operation.GetSource(1);
  111. if (!(src1 is AstOperand attr) || attr.Type != OperandType.Attribute)
  112. {
  113. throw new InvalidOperationException("First source of LoadAttribute must be a attribute.");
  114. }
  115. string indexExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  116. return OperandManager.GetAttributeName(attr, context.Config, isOutAttr: false, indexExpr);
  117. }
  118. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  119. {
  120. IAstNode src1 = operation.GetSource(0);
  121. IAstNode src2 = operation.GetSource(1);
  122. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  123. offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  124. if (src1 is AstOperand oper && oper.Type == OperandType.Constant)
  125. {
  126. bool cbIndexable = context.Config.UsedFeatures.HasFlag(Translation.FeatureFlags.CbIndexing);
  127. return OperandManager.GetConstantBufferName(oper.Value, offsetExpr, context.Config.Stage, cbIndexable);
  128. }
  129. else
  130. {
  131. string slotExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  132. return OperandManager.GetConstantBufferName(slotExpr, offsetExpr, context.Config.Stage);
  133. }
  134. }
  135. public static string LoadLocal(CodeGenContext context, AstOperation operation)
  136. {
  137. return LoadLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  138. }
  139. public static string LoadShared(CodeGenContext context, AstOperation operation)
  140. {
  141. return LoadLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  142. }
  143. private static string LoadLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  144. {
  145. IAstNode src1 = operation.GetSource(0);
  146. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  147. return $"{arrayName}[{offsetExpr}]";
  148. }
  149. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  150. {
  151. IAstNode src1 = operation.GetSource(0);
  152. IAstNode src2 = operation.GetSource(1);
  153. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  154. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  155. return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  156. }
  157. public static string Lod(CodeGenContext context, AstOperation operation)
  158. {
  159. AstTextureOperation texOp = (AstTextureOperation)operation;
  160. int coordsCount = texOp.Type.GetDimensions();
  161. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  162. // TODO: Bindless texture support. For now we just return 0.
  163. if (isBindless)
  164. {
  165. return NumberFormatter.FormatFloat(0);
  166. }
  167. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  168. string indexExpr = null;
  169. if (isIndexed)
  170. {
  171. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  172. }
  173. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  174. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  175. string coordsExpr;
  176. if (coordsCount > 1)
  177. {
  178. string[] elems = new string[coordsCount];
  179. for (int index = 0; index < coordsCount; index++)
  180. {
  181. elems[index] = GetSoureExpr(context, texOp.GetSource(coordsIndex + index), VariableType.F32);
  182. }
  183. coordsExpr = "vec" + coordsCount + "(" + string.Join(", ", elems) + ")";
  184. }
  185. else
  186. {
  187. coordsExpr = GetSoureExpr(context, texOp.GetSource(coordsIndex), VariableType.F32);
  188. }
  189. return $"textureQueryLod({samplerName}, {coordsExpr}){GetMask(texOp.Index)}";
  190. }
  191. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  192. {
  193. return StoreLocalOrShared(context, operation, DefaultNames.LocalMemoryName);
  194. }
  195. public static string StoreShared(CodeGenContext context, AstOperation operation)
  196. {
  197. return StoreLocalOrShared(context, operation, DefaultNames.SharedMemoryName);
  198. }
  199. private static string StoreLocalOrShared(CodeGenContext context, AstOperation operation, string arrayName)
  200. {
  201. IAstNode src1 = operation.GetSource(0);
  202. IAstNode src2 = operation.GetSource(1);
  203. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  204. VariableType srcType = OperandManager.GetNodeDestType(context, src2);
  205. string src = TypeConversion.ReinterpretCast(context, src2, srcType, VariableType.U32);
  206. return $"{arrayName}[{offsetExpr}] = {src}";
  207. }
  208. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  209. {
  210. IAstNode src1 = operation.GetSource(0);
  211. IAstNode src2 = operation.GetSource(1);
  212. IAstNode src3 = operation.GetSource(2);
  213. string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  214. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  215. VariableType srcType = OperandManager.GetNodeDestType(context, src3);
  216. string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.U32);
  217. string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
  218. return $"{sb} = {src}";
  219. }
  220. public static string TextureSample(CodeGenContext context, AstOperation operation)
  221. {
  222. AstTextureOperation texOp = (AstTextureOperation)operation;
  223. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  224. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  225. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  226. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  227. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  228. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  229. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  230. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  231. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  232. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  233. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  234. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  235. SamplerType type = texOp.Type & SamplerType.Mask;
  236. bool is2D = type == SamplerType.Texture2D;
  237. bool isCube = type == SamplerType.TextureCube;
  238. // 2D Array and Cube shadow samplers with LOD level or bias requires an extension.
  239. // If the extension is not supported, just remove the LOD parameter.
  240. if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QuerySupportsTextureShadowLod())
  241. {
  242. hasLodBias = false;
  243. hasLodLevel = false;
  244. }
  245. // Cube shadow samplers with LOD level requires an extension.
  246. // If the extension is not supported, just remove the LOD level parameter.
  247. if (isShadow && isCube && !context.Config.GpuAccessor.QuerySupportsTextureShadowLod())
  248. {
  249. hasLodLevel = false;
  250. }
  251. // TODO: Bindless texture support. For now we just return 0.
  252. if (isBindless)
  253. {
  254. return NumberFormatter.FormatFloat(0);
  255. }
  256. string texCall = intCoords ? "texelFetch" : "texture";
  257. if (isGather)
  258. {
  259. texCall += "Gather";
  260. }
  261. else if (hasDerivatives)
  262. {
  263. texCall += "Grad";
  264. }
  265. else if (hasLodLevel && !intCoords)
  266. {
  267. texCall += "Lod";
  268. }
  269. if (hasOffset)
  270. {
  271. texCall += "Offset";
  272. }
  273. else if (hasOffsets)
  274. {
  275. texCall += "Offsets";
  276. }
  277. int srcIndex = isBindless ? 1 : 0;
  278. string Src(VariableType type)
  279. {
  280. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  281. }
  282. string indexExpr = null;
  283. if (isIndexed)
  284. {
  285. indexExpr = Src(VariableType.S32);
  286. }
  287. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  288. texCall += "(" + samplerName;
  289. int coordsCount = texOp.Type.GetDimensions();
  290. int pCount = coordsCount;
  291. int arrayIndexElem = -1;
  292. if (isArray)
  293. {
  294. arrayIndexElem = pCount++;
  295. }
  296. // The sampler 1D shadow overload expects a
  297. // dummy value on the middle of the vector, who knows why...
  298. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  299. if (hasDummy1DShadowElem)
  300. {
  301. pCount++;
  302. }
  303. if (isShadow && !isGather)
  304. {
  305. pCount++;
  306. }
  307. // On textureGather*, the comparison value is
  308. // always specified as an extra argument.
  309. bool hasExtraCompareArg = isShadow && isGather;
  310. if (pCount == 5)
  311. {
  312. pCount = 4;
  313. hasExtraCompareArg = true;
  314. }
  315. void Append(string str)
  316. {
  317. texCall += ", " + str;
  318. }
  319. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  320. string AssemblePVector(int count)
  321. {
  322. if (count > 1)
  323. {
  324. string[] elems = new string[count];
  325. for (int index = 0; index < count; index++)
  326. {
  327. if (arrayIndexElem == index)
  328. {
  329. elems[index] = Src(VariableType.S32);
  330. if (!intCoords)
  331. {
  332. elems[index] = "float(" + elems[index] + ")";
  333. }
  334. }
  335. else if (index == 1 && hasDummy1DShadowElem)
  336. {
  337. elems[index] = NumberFormatter.FormatFloat(0);
  338. }
  339. else
  340. {
  341. elems[index] = Src(coordType);
  342. }
  343. }
  344. string prefix = intCoords ? "i" : string.Empty;
  345. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  346. }
  347. else
  348. {
  349. return Src(coordType);
  350. }
  351. }
  352. string ApplyScaling(string vector)
  353. {
  354. if (intCoords)
  355. {
  356. int index = context.FindTextureDescriptorIndex(texOp);
  357. if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
  358. !isBindless &&
  359. !isIndexed)
  360. {
  361. if (pCount == 3 && isArray)
  362. {
  363. // The array index is not scaled, just x and y.
  364. vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
  365. }
  366. else if (pCount == 2 && !isArray)
  367. {
  368. vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
  369. }
  370. }
  371. }
  372. return vector;
  373. }
  374. Append(ApplyScaling(AssemblePVector(pCount)));
  375. string AssembleDerivativesVector(int count)
  376. {
  377. if (count > 1)
  378. {
  379. string[] elems = new string[count];
  380. for (int index = 0; index < count; index++)
  381. {
  382. elems[index] = Src(VariableType.F32);
  383. }
  384. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  385. }
  386. else
  387. {
  388. return Src(VariableType.F32);
  389. }
  390. }
  391. if (hasExtraCompareArg)
  392. {
  393. Append(Src(VariableType.F32));
  394. }
  395. if (hasDerivatives)
  396. {
  397. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  398. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  399. }
  400. if (isMultisample)
  401. {
  402. Append(Src(VariableType.S32));
  403. }
  404. else if (hasLodLevel)
  405. {
  406. Append(Src(coordType));
  407. }
  408. string AssembleOffsetVector(int count)
  409. {
  410. if (count > 1)
  411. {
  412. string[] elems = new string[count];
  413. for (int index = 0; index < count; index++)
  414. {
  415. elems[index] = Src(VariableType.S32);
  416. }
  417. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  418. }
  419. else
  420. {
  421. return Src(VariableType.S32);
  422. }
  423. }
  424. if (hasOffset)
  425. {
  426. Append(AssembleOffsetVector(coordsCount));
  427. }
  428. else if (hasOffsets)
  429. {
  430. texCall += $", ivec{coordsCount}[4](";
  431. texCall += AssembleOffsetVector(coordsCount) + ", ";
  432. texCall += AssembleOffsetVector(coordsCount) + ", ";
  433. texCall += AssembleOffsetVector(coordsCount) + ", ";
  434. texCall += AssembleOffsetVector(coordsCount) + ")";
  435. }
  436. if (hasLodBias)
  437. {
  438. Append(Src(VariableType.F32));
  439. }
  440. // textureGather* optional extra component index,
  441. // not needed for shadow samplers.
  442. if (isGather && !isShadow)
  443. {
  444. Append(Src(VariableType.S32));
  445. }
  446. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.Index) : "");
  447. return texCall;
  448. }
  449. public static string TextureSize(CodeGenContext context, AstOperation operation)
  450. {
  451. AstTextureOperation texOp = (AstTextureOperation)operation;
  452. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  453. // TODO: Bindless texture support. For now we just return 0.
  454. if (isBindless)
  455. {
  456. return NumberFormatter.FormatInt(0);
  457. }
  458. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  459. string indexExpr = null;
  460. if (isIndexed)
  461. {
  462. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  463. }
  464. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  465. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  466. IAstNode lod = operation.GetSource(lodSrcIndex);
  467. string lodExpr = GetSoureExpr(context, lod, GetSrcVarType(operation.Inst, lodSrcIndex));
  468. if (texOp.Index == 3)
  469. {
  470. return $"textureQueryLevels({samplerName})";
  471. }
  472. else
  473. {
  474. return $"textureSize({samplerName}, {lodExpr}){GetMask(texOp.Index)}";
  475. }
  476. }
  477. private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage)
  478. {
  479. string sbName = OperandManager.GetShaderStagePrefix(stage);
  480. sbName += "_" + DefaultNames.StorageNamePrefix;
  481. return $"{sbName}[{slotExpr}].{DefaultNames.DataName}[{offsetExpr}]";
  482. }
  483. private static string GetMask(int index)
  484. {
  485. return '.' + "rgba".Substring(index, 1);
  486. }
  487. }
  488. }