BindlessElimination.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using Ryujinx.Graphics.Shader.Instructions;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  5. {
  6. class BindlessElimination
  7. {
  8. public static void RunPass(BasicBlock block, ShaderConfig config)
  9. {
  10. // We can turn a bindless into regular access by recognizing the pattern
  11. // produced by the compiler for separate texture and sampler.
  12. // We check for the following conditions:
  13. // - The handle is a constant buffer value.
  14. // - The handle is the result of a bitwise OR logical operation.
  15. // - Both sources of the OR operation comes from a constant buffer.
  16. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  17. {
  18. if (!(node.Value is TextureOperation texOp))
  19. {
  20. continue;
  21. }
  22. if ((texOp.Flags & TextureFlags.Bindless) == 0)
  23. {
  24. continue;
  25. }
  26. if (texOp.Inst == Instruction.Lod ||
  27. texOp.Inst == Instruction.TextureSample ||
  28. texOp.Inst == Instruction.TextureSize)
  29. {
  30. Operand bindlessHandle = Utils.FindLastOperation(texOp.GetSource(0), block);
  31. // Some instructions do not encode an accurate sampler type:
  32. // - Most instructions uses the same type for 1D and Buffer.
  33. // - Query instructions may not have any type.
  34. // For those cases, we need to try getting the type from current GPU state,
  35. // as long bindless elimination is successful and we know where the texture descriptor is located.
  36. bool rewriteSamplerType =
  37. texOp.Type == SamplerType.TextureBuffer ||
  38. texOp.Inst == Instruction.TextureSize;
  39. if (bindlessHandle.Type == OperandType.ConstantBuffer)
  40. {
  41. SetHandle(config, texOp, bindlessHandle.GetCbufOffset(), bindlessHandle.GetCbufSlot(), rewriteSamplerType, isImage: false);
  42. continue;
  43. }
  44. if (!(bindlessHandle.AsgOp is Operation handleCombineOp))
  45. {
  46. continue;
  47. }
  48. if (handleCombineOp.Inst != Instruction.BitwiseOr)
  49. {
  50. continue;
  51. }
  52. Operand src0 = Utils.FindLastOperation(handleCombineOp.GetSource(0), block);
  53. Operand src1 = Utils.FindLastOperation(handleCombineOp.GetSource(1), block);
  54. // For cases where we have a constant, ensure that the constant is always
  55. // the second operand.
  56. // Since this is a commutative operation, both are fine,
  57. // and having a "canonical" representation simplifies some checks below.
  58. if (src0.Type == OperandType.Constant && src1.Type != OperandType.Constant)
  59. {
  60. Operand temp = src1;
  61. src1 = src0;
  62. src0 = temp;
  63. }
  64. TextureHandleType handleType = TextureHandleType.SeparateSamplerHandle;
  65. // Try to match the following patterns:
  66. // Masked pattern:
  67. // - samplerHandle = samplerHandle & 0xFFF00000;
  68. // - textureHandle = textureHandle & 0xFFFFF;
  69. // - combinedHandle = samplerHandle | textureHandle;
  70. // Where samplerHandle and textureHandle comes from a constant buffer.
  71. // Shifted pattern:
  72. // - samplerHandle = samplerId << 20;
  73. // - combinedHandle = samplerHandle | textureHandle;
  74. // Where samplerId and textureHandle comes from a constant buffer.
  75. // Constant pattern:
  76. // - combinedHandle = samplerHandleConstant | textureHandle;
  77. // Where samplerHandleConstant is a constant value, and textureHandle comes from a constant buffer.
  78. if (src0.AsgOp is Operation src0AsgOp)
  79. {
  80. if (src1.AsgOp is Operation src1AsgOp &&
  81. src0AsgOp.Inst == Instruction.BitwiseAnd &&
  82. src1AsgOp.Inst == Instruction.BitwiseAnd)
  83. {
  84. src0 = GetSourceForMaskedHandle(src0AsgOp, 0xFFFFF);
  85. src1 = GetSourceForMaskedHandle(src1AsgOp, 0xFFF00000);
  86. // The OR operation is commutative, so we can also try to swap the operands to get a match.
  87. if (src0 == null || src1 == null)
  88. {
  89. src0 = GetSourceForMaskedHandle(src1AsgOp, 0xFFFFF);
  90. src1 = GetSourceForMaskedHandle(src0AsgOp, 0xFFF00000);
  91. }
  92. if (src0 == null || src1 == null)
  93. {
  94. continue;
  95. }
  96. }
  97. else if (src0AsgOp.Inst == Instruction.ShiftLeft)
  98. {
  99. Operand shift = src0AsgOp.GetSource(1);
  100. if (shift.Type == OperandType.Constant && shift.Value == 20)
  101. {
  102. src0 = src1;
  103. src1 = src0AsgOp.GetSource(0);
  104. handleType = TextureHandleType.SeparateSamplerId;
  105. }
  106. }
  107. }
  108. else if (src1.AsgOp is Operation src1AsgOp && src1AsgOp.Inst == Instruction.ShiftLeft)
  109. {
  110. Operand shift = src1AsgOp.GetSource(1);
  111. if (shift.Type == OperandType.Constant && shift.Value == 20)
  112. {
  113. src1 = src1AsgOp.GetSource(0);
  114. handleType = TextureHandleType.SeparateSamplerId;
  115. }
  116. }
  117. else if (src1.Type == OperandType.Constant && (src1.Value & 0xfffff) == 0)
  118. {
  119. handleType = TextureHandleType.SeparateConstantSamplerHandle;
  120. }
  121. if (src0.Type != OperandType.ConstantBuffer)
  122. {
  123. continue;
  124. }
  125. if (handleType == TextureHandleType.SeparateConstantSamplerHandle)
  126. {
  127. SetHandle(
  128. config,
  129. texOp,
  130. TextureHandle.PackOffsets(src0.GetCbufOffset(), ((src1.Value >> 20) & 0xfff), handleType),
  131. TextureHandle.PackSlots(src0.GetCbufSlot(), 0),
  132. rewriteSamplerType,
  133. isImage: false);
  134. }
  135. else if (src1.Type == OperandType.ConstantBuffer)
  136. {
  137. SetHandle(
  138. config,
  139. texOp,
  140. TextureHandle.PackOffsets(src0.GetCbufOffset(), src1.GetCbufOffset(), handleType),
  141. TextureHandle.PackSlots(src0.GetCbufSlot(), src1.GetCbufSlot()),
  142. rewriteSamplerType,
  143. isImage: false);
  144. }
  145. }
  146. else if (texOp.Inst == Instruction.ImageLoad ||
  147. texOp.Inst == Instruction.ImageStore ||
  148. texOp.Inst == Instruction.ImageAtomic)
  149. {
  150. Operand src0 = Utils.FindLastOperation(texOp.GetSource(0), block);
  151. if (src0.Type == OperandType.ConstantBuffer)
  152. {
  153. int cbufOffset = src0.GetCbufOffset();
  154. int cbufSlot = src0.GetCbufSlot();
  155. if (texOp.Format == TextureFormat.Unknown)
  156. {
  157. if (texOp.Inst == Instruction.ImageAtomic)
  158. {
  159. texOp.Format = config.GetTextureFormatAtomic(cbufOffset, cbufSlot);
  160. }
  161. else
  162. {
  163. texOp.Format = config.GetTextureFormat(cbufOffset, cbufSlot);
  164. }
  165. }
  166. bool rewriteSamplerType = texOp.Type == SamplerType.TextureBuffer;
  167. SetHandle(config, texOp, cbufOffset, cbufSlot, rewriteSamplerType, isImage: true);
  168. }
  169. }
  170. }
  171. }
  172. private static Operand GetSourceForMaskedHandle(Operation asgOp, uint mask)
  173. {
  174. // Assume it was already checked that the operation is bitwise AND.
  175. Operand src0 = asgOp.GetSource(0);
  176. Operand src1 = asgOp.GetSource(1);
  177. if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.ConstantBuffer)
  178. {
  179. // We can't check if the mask matches here as both operands are from a constant buffer.
  180. // Be optimistic and assume it matches. Avoid constant buffer 1 as official drivers
  181. // uses this one to store compiler constants.
  182. return src0.GetCbufSlot() == 1 ? src1 : src0;
  183. }
  184. else if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.Constant)
  185. {
  186. if ((uint)src1.Value == mask)
  187. {
  188. return src0;
  189. }
  190. }
  191. else if (src0.Type == OperandType.Constant && src1.Type == OperandType.ConstantBuffer)
  192. {
  193. if ((uint)src0.Value == mask)
  194. {
  195. return src1;
  196. }
  197. }
  198. return null;
  199. }
  200. private static void SetHandle(ShaderConfig config, TextureOperation texOp, int cbufOffset, int cbufSlot, bool rewriteSamplerType, bool isImage)
  201. {
  202. texOp.SetHandle(cbufOffset, cbufSlot);
  203. if (rewriteSamplerType)
  204. {
  205. SamplerType newType = config.GpuAccessor.QuerySamplerType(cbufOffset, cbufSlot);
  206. if (texOp.Inst.IsTextureQuery())
  207. {
  208. texOp.Type = newType;
  209. }
  210. else if (texOp.Type == SamplerType.TextureBuffer && newType == SamplerType.Texture1D)
  211. {
  212. int coordsCount = 1;
  213. if (InstEmit.Sample1DAs2D)
  214. {
  215. newType = SamplerType.Texture2D;
  216. texOp.InsertSource(coordsCount++, OperandHelper.Const(0));
  217. }
  218. if (!isImage &&
  219. (texOp.Flags & TextureFlags.IntCoords) != 0 &&
  220. (texOp.Flags & TextureFlags.LodLevel) == 0)
  221. {
  222. // IntCoords textures must always have explicit LOD.
  223. texOp.SetLodLevelFlag();
  224. texOp.InsertSource(coordsCount, OperandHelper.Const(0));
  225. }
  226. texOp.Type = newType;
  227. }
  228. }
  229. config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, cbufSlot, cbufOffset);
  230. }
  231. }
  232. }