InstGenMemory.cs 15 KB

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