InstGenMemory.cs 17 KB

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