InstGenMemory.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. IAstNode src1 = operation.GetSource(0);
  91. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  92. return $"{DefaultNames.LocalMemoryName}[{offsetExpr}]";
  93. }
  94. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  95. {
  96. IAstNode src1 = operation.GetSource(0);
  97. IAstNode src2 = operation.GetSource(1);
  98. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  99. return OperandManager.GetStorageBufferName(src1, offsetExpr, context.Config.Stage);
  100. }
  101. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  102. {
  103. IAstNode src1 = operation.GetSource(0);
  104. IAstNode src2 = operation.GetSource(1);
  105. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  106. VariableType srcType = OperandManager.GetNodeDestType(src2);
  107. string src = TypeConversion.ReinterpretCast(context, src2, srcType, VariableType.F32);
  108. return $"{DefaultNames.LocalMemoryName}[{offsetExpr}] = {src}";
  109. }
  110. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  111. {
  112. IAstNode src1 = operation.GetSource(0);
  113. IAstNode src2 = operation.GetSource(1);
  114. IAstNode src3 = operation.GetSource(2);
  115. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  116. VariableType srcType = OperandManager.GetNodeDestType(src3);
  117. string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.F32);
  118. string sbName = OperandManager.GetStorageBufferName(src1, offsetExpr, context.Config.Stage);
  119. return $"{sbName} = {src}";
  120. }
  121. public static string TextureSample(CodeGenContext context, AstOperation operation)
  122. {
  123. AstTextureOperation texOp = (AstTextureOperation)operation;
  124. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  125. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  126. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  127. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  128. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  129. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  130. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  131. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  132. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  133. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  134. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  135. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  136. // This combination is valid, but not available on GLSL.
  137. // For now, ignore the LOD level and do a normal sample.
  138. // TODO: How to implement it properly?
  139. if (hasLodLevel && isArray && isShadow)
  140. {
  141. hasLodLevel = false;
  142. }
  143. string texCall = intCoords ? "texelFetch" : "texture";
  144. if (isGather)
  145. {
  146. texCall += "Gather";
  147. }
  148. else if (hasDerivatives)
  149. {
  150. texCall += "Grad";
  151. }
  152. else if (hasLodLevel && !intCoords)
  153. {
  154. texCall += "Lod";
  155. }
  156. if (hasOffset)
  157. {
  158. texCall += "Offset";
  159. }
  160. else if (hasOffsets)
  161. {
  162. texCall += "Offsets";
  163. }
  164. int srcIndex = isBindless ? 1 : 0;
  165. string Src(VariableType type)
  166. {
  167. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  168. }
  169. string indexExpr = null;
  170. if (isIndexed)
  171. {
  172. indexExpr = Src(VariableType.S32);
  173. }
  174. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  175. texCall += "(" + samplerName;
  176. int coordsCount = texOp.Type.GetDimensions();
  177. int pCount = coordsCount;
  178. int arrayIndexElem = -1;
  179. if (isArray)
  180. {
  181. arrayIndexElem = pCount++;
  182. }
  183. // The sampler 1D shadow overload expects a
  184. // dummy value on the middle of the vector, who knows why...
  185. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  186. if (hasDummy1DShadowElem)
  187. {
  188. pCount++;
  189. }
  190. if (isShadow && !isGather)
  191. {
  192. pCount++;
  193. }
  194. // On textureGather*, the comparison value is
  195. // always specified as an extra argument.
  196. bool hasExtraCompareArg = isShadow && isGather;
  197. if (pCount == 5)
  198. {
  199. pCount = 4;
  200. hasExtraCompareArg = true;
  201. }
  202. void Append(string str)
  203. {
  204. texCall += ", " + str;
  205. }
  206. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  207. string AssemblePVector(int count)
  208. {
  209. if (count > 1)
  210. {
  211. string[] elems = new string[count];
  212. for (int index = 0; index < count; index++)
  213. {
  214. if (arrayIndexElem == index)
  215. {
  216. elems[index] = Src(VariableType.S32);
  217. if (!intCoords)
  218. {
  219. elems[index] = "float(" + elems[index] + ")";
  220. }
  221. }
  222. else if (index == 1 && hasDummy1DShadowElem)
  223. {
  224. elems[index] = NumberFormatter.FormatFloat(0);
  225. }
  226. else
  227. {
  228. elems[index] = Src(coordType);
  229. }
  230. }
  231. string prefix = intCoords ? "i" : string.Empty;
  232. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  233. }
  234. else
  235. {
  236. return Src(coordType);
  237. }
  238. }
  239. Append(AssemblePVector(pCount));
  240. string AssembleDerivativesVector(int count)
  241. {
  242. if (count > 1)
  243. {
  244. string[] elems = new string[count];
  245. for (int index = 0; index < count; index++)
  246. {
  247. elems[index] = Src(VariableType.F32);
  248. }
  249. return "vec" + count + "(" + string.Join(", ", elems) + ")";
  250. }
  251. else
  252. {
  253. return Src(VariableType.F32);
  254. }
  255. }
  256. if (hasDerivatives)
  257. {
  258. Append(AssembleDerivativesVector(coordsCount)); // dPdx
  259. Append(AssembleDerivativesVector(coordsCount)); // dPdy
  260. }
  261. if (hasExtraCompareArg)
  262. {
  263. Append(Src(VariableType.F32));
  264. }
  265. if (isMultisample)
  266. {
  267. Append(Src(VariableType.S32));
  268. }
  269. else if (hasLodLevel)
  270. {
  271. Append(Src(coordType));
  272. }
  273. string AssembleOffsetVector(int count)
  274. {
  275. if (count > 1)
  276. {
  277. string[] elems = new string[count];
  278. for (int index = 0; index < count; index++)
  279. {
  280. elems[index] = Src(VariableType.S32);
  281. }
  282. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  283. }
  284. else
  285. {
  286. return Src(VariableType.S32);
  287. }
  288. }
  289. if (hasOffset)
  290. {
  291. Append(AssembleOffsetVector(coordsCount));
  292. }
  293. else if (hasOffsets)
  294. {
  295. texCall += $", ivec{coordsCount}[4](";
  296. texCall += AssembleOffsetVector(coordsCount) + ", ";
  297. texCall += AssembleOffsetVector(coordsCount) + ", ";
  298. texCall += AssembleOffsetVector(coordsCount) + ", ";
  299. texCall += AssembleOffsetVector(coordsCount) + ")";
  300. }
  301. if (hasLodBias)
  302. {
  303. Append(Src(VariableType.F32));
  304. }
  305. // textureGather* optional extra component index,
  306. // not needed for shadow samplers.
  307. if (isGather && !isShadow)
  308. {
  309. Append(Src(VariableType.S32));
  310. }
  311. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.ComponentMask) : "");
  312. return texCall;
  313. }
  314. public static string TextureSize(CodeGenContext context, AstOperation operation)
  315. {
  316. AstTextureOperation texOp = (AstTextureOperation)operation;
  317. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  318. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  319. string indexExpr = null;
  320. if (isIndexed)
  321. {
  322. indexExpr = GetSoureExpr(context, texOp.GetSource(0), VariableType.S32);
  323. }
  324. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
  325. IAstNode src0 = operation.GetSource(isBindless || isIndexed ? 1 : 0);
  326. string src0Expr = GetSoureExpr(context, src0, GetSrcVarType(operation.Inst, 0));
  327. return $"textureSize({samplerName}, {src0Expr}){GetMask(texOp.ComponentMask)}";
  328. }
  329. private static string GetMask(int compMask)
  330. {
  331. string mask = ".";
  332. for (int index = 0; index < 4; index++)
  333. {
  334. if ((compMask & (1 << index)) != 0)
  335. {
  336. mask += "rgba".Substring(index, 1);
  337. }
  338. }
  339. return mask;
  340. }
  341. }
  342. }