InstGenMemory.cs 22 KB

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