InstGenMemory.cs 15 KB

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