InstGenMemory.cs 18 KB

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