InstGenMemory.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  4. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  5. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  6. {
  7. static class InstGenMemory
  8. {
  9. public static string LoadConstant(CodeGenContext context, AstOperation operation)
  10. {
  11. IAstNode src1 = operation.GetSource(1);
  12. string offsetExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 1));
  13. offsetExpr = Enclose(offsetExpr, src1, Instruction.ShiftRightS32, isLhs: true);
  14. return OperandManager.GetConstantBufferName(operation.GetSource(0), offsetExpr, context.Config.Type);
  15. }
  16. public static string TextureSample(CodeGenContext context, AstOperation operation)
  17. {
  18. AstTextureOperation texOp = (AstTextureOperation)operation;
  19. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  20. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  21. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  22. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  23. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  24. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  25. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  26. bool isArray = (texOp.Type & TextureType.Array) != 0;
  27. bool isMultisample = (texOp.Type & TextureType.Multisample) != 0;
  28. bool isShadow = (texOp.Type & TextureType.Shadow) != 0;
  29. string texCall = intCoords ? "texelFetch" : "texture";
  30. if (isGather)
  31. {
  32. texCall += "Gather";
  33. }
  34. else if (hasLodLevel && !intCoords)
  35. {
  36. texCall += "Lod";
  37. }
  38. if (hasOffset)
  39. {
  40. texCall += "Offset";
  41. }
  42. else if (hasOffsets)
  43. {
  44. texCall += "Offsets";
  45. }
  46. string samplerName = OperandManager.GetSamplerName(context.Config.Type, texOp);
  47. texCall += "(" + samplerName;
  48. int coordsCount = texOp.Type.GetCoordsCount();
  49. int pCount = coordsCount;
  50. int arrayIndexElem = -1;
  51. if (isArray)
  52. {
  53. arrayIndexElem = pCount++;
  54. }
  55. // The sampler 1D shadow overload expects a
  56. // dummy value on the middle of the vector, who knows why...
  57. bool hasDummy1DShadowElem = texOp.Type == (TextureType.Texture1D | TextureType.Shadow);
  58. if (hasDummy1DShadowElem)
  59. {
  60. pCount++;
  61. }
  62. if (isShadow && !isGather)
  63. {
  64. pCount++;
  65. }
  66. // On textureGather*, the comparison value is
  67. // always specified as an extra argument.
  68. bool hasExtraCompareArg = isShadow && isGather;
  69. if (pCount == 5)
  70. {
  71. pCount = 4;
  72. hasExtraCompareArg = true;
  73. }
  74. int srcIndex = isBindless ? 1 : 0;
  75. string Src(VariableType type)
  76. {
  77. return GetSoureExpr(context, texOp.GetSource(srcIndex++), type);
  78. }
  79. void Append(string str)
  80. {
  81. texCall += ", " + str;
  82. }
  83. VariableType coordType = intCoords ? VariableType.S32 : VariableType.F32;
  84. string AssemblePVector(int count)
  85. {
  86. if (count > 1)
  87. {
  88. string[] elems = new string[count];
  89. for (int index = 0; index < count; index++)
  90. {
  91. if (arrayIndexElem == index)
  92. {
  93. elems[index] = Src(VariableType.S32);
  94. if (!intCoords)
  95. {
  96. elems[index] = "float(" + elems[index] + ")";
  97. }
  98. }
  99. else if (index == 1 && hasDummy1DShadowElem)
  100. {
  101. elems[index] = NumberFormatter.FormatFloat(0);
  102. }
  103. else
  104. {
  105. elems[index] = Src(coordType);
  106. }
  107. }
  108. string prefix = intCoords ? "i" : string.Empty;
  109. return prefix + "vec" + count + "(" + string.Join(", ", elems) + ")";
  110. }
  111. else
  112. {
  113. return Src(coordType);
  114. }
  115. }
  116. Append(AssemblePVector(pCount));
  117. if (hasExtraCompareArg)
  118. {
  119. Append(Src(VariableType.F32));
  120. }
  121. if (isMultisample)
  122. {
  123. Append(Src(VariableType.S32));
  124. }
  125. else if (hasLodLevel)
  126. {
  127. Append(Src(coordType));
  128. }
  129. string AssembleOffsetVector(int count)
  130. {
  131. if (count > 1)
  132. {
  133. string[] elems = new string[count];
  134. for (int index = 0; index < count; index++)
  135. {
  136. elems[index] = Src(VariableType.S32);
  137. }
  138. return "ivec" + count + "(" + string.Join(", ", elems) + ")";
  139. }
  140. else
  141. {
  142. return Src(VariableType.S32);
  143. }
  144. }
  145. if (hasOffset)
  146. {
  147. Append(AssembleOffsetVector(coordsCount));
  148. }
  149. else if (hasOffsets)
  150. {
  151. texCall += $", ivec{coordsCount}[4](";
  152. texCall += AssembleOffsetVector(coordsCount) + ", ";
  153. texCall += AssembleOffsetVector(coordsCount) + ", ";
  154. texCall += AssembleOffsetVector(coordsCount) + ", ";
  155. texCall += AssembleOffsetVector(coordsCount) + ")";
  156. }
  157. if (hasLodBias)
  158. {
  159. Append(Src(VariableType.F32));
  160. }
  161. // textureGather* optional extra component index,
  162. // not needed for shadow samplers.
  163. if (isGather && !isShadow)
  164. {
  165. Append(Src(VariableType.S32));
  166. }
  167. texCall += ")" + (isGather || !isShadow ? GetMask(texOp.ComponentMask) : "");
  168. return texCall;
  169. }
  170. public static string TextureSize(CodeGenContext context, AstOperation operation)
  171. {
  172. AstTextureOperation texOp = (AstTextureOperation)operation;
  173. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  174. string samplerName = OperandManager.GetSamplerName(context.Config.Type, texOp);
  175. IAstNode src0 = operation.GetSource(isBindless ? 1 : 0);
  176. string src0Expr = GetSoureExpr(context, src0, GetSrcVarType(operation.Inst, 0));
  177. return $"textureSize({samplerName}, {src0Expr}){GetMask(texOp.ComponentMask)}";
  178. }
  179. private static string GetMask(int compMask)
  180. {
  181. string mask = ".";
  182. for (int index = 0; index < 4; index++)
  183. {
  184. if ((compMask & (1 << index)) != 0)
  185. {
  186. mask += "rgba".Substring(index, 1);
  187. }
  188. }
  189. return mask;
  190. }
  191. }
  192. }