InstGenMemory.cs 23 KB

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