InstGenMemory.cs 22 KB

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