InstGenMemory.cs 24 KB

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