InstGenMemory.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  121. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  122. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  123. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  124. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  125. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  126. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  127. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  128. // This combination is valid, but not available on GLSL.
  129. // For now, ignore the LOD level and do a normal sample.
  130. // TODO: How to implement it properly?
  131. if (hasLodLevel && isArray && isShadow)
  132. {
  133. hasLodLevel = false;
  134. }
  135. string texCall = intCoords ? "texelFetch" : "texture";
  136. if (isGather)
  137. {
  138. texCall += "Gather";
  139. }
  140. else if (hasLodLevel && !intCoords)
  141. {
  142. texCall += "Lod";
  143. }
  144. if (hasOffset)
  145. {
  146. texCall += "Offset";
  147. }
  148. else if (hasOffsets)
  149. {
  150. texCall += "Offsets";
  151. }
  152. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  153. texCall += "(" + samplerName;
  154. int coordsCount = texOp.Type.GetDimensions();
  155. int pCount = coordsCount;
  156. int arrayIndexElem = -1;
  157. if (isArray)
  158. {
  159. arrayIndexElem = pCount++;
  160. }
  161. // The sampler 1D shadow overload expects a
  162. // dummy value on the middle of the vector, who knows why...
  163. bool hasDummy1DShadowElem = texOp.Type == (SamplerType.Texture1D | SamplerType.Shadow);
  164. if (hasDummy1DShadowElem)
  165. {
  166. pCount++;
  167. }
  168. if (isShadow && !isGather)
  169. {
  170. pCount++;
  171. }
  172. // On textureGather*, the comparison value is
  173. // always specified as an extra argument.
  174. bool hasExtraCompareArg = isShadow && isGather;
  175. if (pCount == 5)
  176. {
  177. pCount = 4;
  178. hasExtraCompareArg = true;
  179. }
  180. int srcIndex = isBindless ? 1 : 0;
  181. string Src(VariableType type)
  182. {
  183. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  184. }
  185. void Append(string str)
  186. {
  187. texCall += ", " + str;
  188. }
  189. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  190. string AssemblePVector(int count)
  191. {
  192. if (count > 1)
  193. {
  194. string[] elems = new string[count];
  195. for (int index = 0; index < count; index++)
  196. {
  197. if (arrayIndexElem == index)
  198. {
  199. elems[index] = Src(VariableType.S32);
  200. if (!intCoords)
  201. {
  202. elems[index] = "float(" + elems[index] + ")";
  203. }
  204. }
  205. else if (index == 1 && hasDummy1DShadowElem)
  206. {
  207. elems[index] = NumberFormatter.FormatFloat(0);
  208. }
  209. else
  210. {
  211. elems[index] = Src(coordType);
  212. }
  213. }
  214. string prefix = intCoords ? "i" : string.Empty;
  215. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  216. }
  217. else
  218. {
  219. return Src(coordType);
  220. }
  221. }
  222. Append(AssemblePVector(pCount));
  223. if (hasExtraCompareArg)
  224. {
  225. Append(Src(VariableType.F32));
  226. }
  227. if (isMultisample)
  228. {
  229. Append(Src(VariableType.S32));
  230. }
  231. else if (hasLodLevel)
  232. {
  233. Append(Src(coordType));
  234. }
  235. string AssembleOffsetVector(int count)
  236. {
  237. if (count > 1)
  238. {
  239. string[] elems = new string[count];
  240. for (int index = 0; index < count; index++)
  241. {
  242. elems[index] = Src(VariableType.S32);
  243. }
  244. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  245. }
  246. else
  247. {
  248. return Src(VariableType.S32);
  249. }
  250. }
  251. if (hasOffset)
  252. {
  253. Append(AssembleOffsetVector(coordsCount));
  254. }
  255. else if (hasOffsets)
  256. {
  257. texCall += $", ivec{coordsCount}[4](";
  258. texCall += AssembleOffsetVector(coordsCount) + ", ";
  259. texCall += AssembleOffsetVector(coordsCount) + ", ";
  260. texCall += AssembleOffsetVector(coordsCount) + ", ";
  261. texCall += AssembleOffsetVector(coordsCount) + ")";
  262. }
  263. if (hasLodBias)
  264. {
  265. Append(Src(VariableType.F32));
  266. }
  267. // textureGather* optional extra component index,
  268. // not needed for shadow samplers.
  269. if (isGather && !isShadow)
  270. {
  271. Append(Src(VariableType.S32));
  272. }
  273. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.ComponentMask) : "");
  274. return texCall;
  275. }
  276. public static string TextureSize(CodeGenContext context, AstOperation operation)
  277. {
  278. AstTextureOperation texOp = (AstTextureOperation)operation;
  279. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  280. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  281. IAstNode src0 = operation.GetSource(isBindless ? 1 : 0);
  282. string src0Expr = GetSoureExpr(context, src0, GetSrcVarType(operation.Inst, 0));
  283. return $"textureSize({samplerName}, {src0Expr}){GetMask(texOp.ComponentMask)}";
  284. }
  285. private static string GetMask(int compMask)
  286. {
  287. string mask = ".";
  288. for (int index = 0; index < 4; index++)
  289. {
  290. if ((compMask & (1 << index)) != 0)
  291. {
  292. mask += "rgba".Substring(index, 1);
  293. }
  294. }
  295. return mask;
  296. }
  297. }
  298. }