BindlessElimination.cs 10.0 KB

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