InstGenMemory.cs 19 KB

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