InstGenMemory.cs 17 KB

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