InstGenMemory.cs 13 KB

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