InstGenMemory.cs 23 KB

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