InstGenMemory.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 LoadAttribute(CodeGenContext context, AstOperation operation)
  11. {
  12. IAstNode src1 = operation.GetSource(0);
  13. IAstNode src2 = operation.GetSource(1);
  14. if (!(src1 is AstOperand attr) || attr.Type != OperandType.Attribute)
  15. {
  16. throw new InvalidOperationException("First source of LoadAttribute must be a attribute.");
  17. }
  18. string indexExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  19. return OperandManager.GetAttributeName(attr, context.Config.Stage, isOutAttr: false, indexExpr);
  20. }
  21. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  22. {
  23. IAstNode src1 = operation.GetSource(0);
  24. IAstNode src2 = operation.GetSource(1);
  25. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  26. offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true);
  27. return OperandManager.GetConstantBufferName(src1, offsetExpr, context.Config.Stage);
  28. }
  29. public static string LoadLocal(CodeGenContext context, AstOperation operation)
  30. {
  31. IAstNode src1 = operation.GetSource(0);
  32. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  33. return $"{DefaultNames.LocalMemoryName}[{offsetExpr}]";
  34. }
  35. public static string LoadStorage(CodeGenContext context, AstOperation operation)
  36. {
  37. IAstNode src1 = operation.GetSource(0);
  38. IAstNode src2 = operation.GetSource(1);
  39. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  40. return OperandManager.GetStorageBufferName(src1, offsetExpr, context.Config.Stage);
  41. }
  42. public static string StoreLocal(CodeGenContext context, AstOperation operation)
  43. {
  44. IAstNode src1 = operation.GetSource(0);
  45. IAstNode src2 = operation.GetSource(1);
  46. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
  47. VariableType srcType = OperandManager.GetNodeDestType(src2);
  48. string src = TypeConversion.ReinterpretCast(context, src2, srcType, VariableType.F32);
  49. return $"{DefaultNames.LocalMemoryName}[{offsetExpr}] = {src}";
  50. }
  51. public static string StoreStorage(CodeGenContext context, AstOperation operation)
  52. {
  53. IAstNode src1 = operation.GetSource(0);
  54. IAstNode src2 = operation.GetSource(1);
  55. IAstNode src3 = operation.GetSource(2);
  56. string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
  57. VariableType srcType = OperandManager.GetNodeDestType(src3);
  58. string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.F32);
  59. string sbName = OperandManager.GetStorageBufferName(src1, offsetExpr, context.Config.Stage);
  60. return $"{sbName} = {src}";
  61. }
  62. public static string TextureSample(CodeGenContext context, AstOperation operation)
  63. {
  64. AstTextureOperation texOp = (AstTextureOperation)operation;
  65. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  66. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  67. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  68. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  69. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  70. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  71. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  72. bool isArray = (texOp.Target & TextureTarget.Array) != 0;
  73. bool isMultisample = (texOp.Target & TextureTarget.Multisample) != 0;
  74. bool isShadow = (texOp.Target & TextureTarget.Shadow) != 0;
  75. // This combination is valid, but not available on GLSL.
  76. // For now, ignore the LOD level and do a normal sample.
  77. // TODO: How to implement it properly?
  78. if (hasLodLevel && isArray && isShadow)
  79. {
  80. hasLodLevel = false;
  81. }
  82. string texCall = intCoords ? "texelFetch" : "texture";
  83. if (isGather)
  84. {
  85. texCall += "Gather";
  86. }
  87. else if (hasLodLevel && !intCoords)
  88. {
  89. texCall += "Lod";
  90. }
  91. if (hasOffset)
  92. {
  93. texCall += "Offset";
  94. }
  95. else if (hasOffsets)
  96. {
  97. texCall += "Offsets";
  98. }
  99. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  100. texCall += "(" + samplerName;
  101. int coordsCount = texOp.Target.GetDimensions();
  102. int pCount = coordsCount;
  103. int arrayIndexElem = -1;
  104. if (isArray)
  105. {
  106. arrayIndexElem = pCount++;
  107. }
  108. // The sampler 1D shadow overload expects a
  109. // dummy value on the middle of the vector, who knows why...
  110. bool hasDummy1DShadowElem = texOp.Target == (TextureTarget.Texture1D | TextureTarget.Shadow);
  111. if (hasDummy1DShadowElem)
  112. {
  113. pCount++;
  114. }
  115. if (isShadow && !isGather)
  116. {
  117. pCount++;
  118. }
  119. // On textureGather*, the comparison value is
  120. // always specified as an extra argument.
  121. bool hasExtraCompareArg = isShadow && isGather;
  122. if (pCount == 5)
  123. {
  124. pCount = 4;
  125. hasExtraCompareArg = true;
  126. }
  127. int srcIndex = isBindless ? 1 : 0;
  128. string Src(VariableType type)
  129. {
  130. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  131. }
  132. void Append(string str)
  133. {
  134. texCall += ", " + str;
  135. }
  136. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  137. string AssemblePVector(int count)
  138. {
  139. if (count > 1)
  140. {
  141. string[] elems = new string[count];
  142. for (int index = 0; index < count; index++)
  143. {
  144. if (arrayIndexElem == index)
  145. {
  146. elems[index] = Src(VariableType.S32);
  147. if (!intCoords)
  148. {
  149. elems[index] = "float(" + elems[index] + ")";
  150. }
  151. }
  152. else if (index == 1 && hasDummy1DShadowElem)
  153. {
  154. elems[index] = NumberFormatter.FormatFloat(0);
  155. }
  156. else
  157. {
  158. elems[index] = Src(coordType);
  159. }
  160. }
  161. string prefix = intCoords ? "i" : string.Empty;
  162. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  163. }
  164. else
  165. {
  166. return Src(coordType);
  167. }
  168. }
  169. Append(AssemblePVector(pCount));
  170. if (hasExtraCompareArg)
  171. {
  172. Append(Src(VariableType.F32));
  173. }
  174. if (isMultisample)
  175. {
  176. Append(Src(VariableType.S32));
  177. }
  178. else if (hasLodLevel)
  179. {
  180. Append(Src(coordType));
  181. }
  182. string AssembleOffsetVector(int count)
  183. {
  184. if (count > 1)
  185. {
  186. string[] elems = new string[count];
  187. for (int index = 0; index < count; index++)
  188. {
  189. elems[index] = Src(VariableType.S32);
  190. }
  191. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  192. }
  193. else
  194. {
  195. return Src(VariableType.S32);
  196. }
  197. }
  198. if (hasOffset)
  199. {
  200. Append(AssembleOffsetVector(coordsCount));
  201. }
  202. else if (hasOffsets)
  203. {
  204. texCall += $", ivec{coordsCount}[4](";
  205. texCall += AssembleOffsetVector(coordsCount) + ", ";
  206. texCall += AssembleOffsetVector(coordsCount) + ", ";
  207. texCall += AssembleOffsetVector(coordsCount) + ", ";
  208. texCall += AssembleOffsetVector(coordsCount) + ")";
  209. }
  210. if (hasLodBias)
  211. {
  212. Append(Src(VariableType.F32));
  213. }
  214. // textureGather* optional extra component index,
  215. // not needed for shadow samplers.
  216. if (isGather && !isShadow)
  217. {
  218. Append(Src(VariableType.S32));
  219. }
  220. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.ComponentMask) : "");
  221. return texCall;
  222. }
  223. public static string TextureSize(CodeGenContext context, AstOperation operation)
  224. {
  225. AstTextureOperation texOp = (AstTextureOperation)operation;
  226. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  227. string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp);
  228. IAstNode src0 = operation.GetSource(isBindless ? 1 : 0);
  229. string src0Expr = GetSoureExpr(context, src0, GetSrcVarType(operation.Inst, 0));
  230. return $"textureSize({samplerName}, {src0Expr}){GetMask(texOp.ComponentMask)}";
  231. }
  232. private static string GetMask(int compMask)
  233. {
  234. string mask = ".";
  235. for (int index = 0; index < 4; index++)
  236. {
  237. if ((compMask & (1 << index)) != 0)
  238. {
  239. mask += "rgba".Substring(index, 1);
  240. }
  241. }
  242. return mask;
  243. }
  244. }
  245. }