BindlessElimination.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. TextureHandleType handleType = TextureHandleType.SeparateSamplerHandle;
  47. // Try to match masked pattern:
  48. // - samplerHandle = samplerHandle & 0xFFF00000;
  49. // - textureHandle = textureHandle & 0xFFFFF;
  50. // - combinedHandle = samplerHandle | textureHandle;
  51. // where samplerHandle and textureHandle comes from a constant buffer, and shifted pattern:
  52. // - samplerHandle = samplerId << 20;
  53. // - combinedHandle = samplerHandle | textureHandle;
  54. // where samplerId and textureHandle comes from a constant buffer.
  55. if (src0.AsgOp is Operation src0AsgOp)
  56. {
  57. if (src1.AsgOp is Operation src1AsgOp &&
  58. src0AsgOp.Inst == Instruction.BitwiseAnd &&
  59. src1AsgOp.Inst == Instruction.BitwiseAnd)
  60. {
  61. src0 = GetSourceForMaskedHandle(src0AsgOp, 0xFFFFF);
  62. src1 = GetSourceForMaskedHandle(src1AsgOp, 0xFFF00000);
  63. // The OR operation is commutative, so we can also try to swap the operands to get a match.
  64. if (src0 == null || src1 == null)
  65. {
  66. src0 = GetSourceForMaskedHandle(src1AsgOp, 0xFFFFF);
  67. src1 = GetSourceForMaskedHandle(src0AsgOp, 0xFFF00000);
  68. }
  69. if (src0 == null || src1 == null)
  70. {
  71. continue;
  72. }
  73. }
  74. else if (src0AsgOp.Inst == Instruction.ShiftLeft)
  75. {
  76. Operand shift = src0AsgOp.GetSource(1);
  77. if (shift.Type == OperandType.Constant && shift.Value == 20)
  78. {
  79. src0 = src1;
  80. src1 = src0AsgOp.GetSource(0);
  81. handleType = TextureHandleType.SeparateSamplerId;
  82. }
  83. }
  84. }
  85. else if (src1.AsgOp is Operation src1AsgOp && src1AsgOp.Inst == Instruction.ShiftLeft)
  86. {
  87. Operand shift = src1AsgOp.GetSource(1);
  88. if (shift.Type == OperandType.Constant && shift.Value == 20)
  89. {
  90. src1 = src1AsgOp.GetSource(0);
  91. handleType = TextureHandleType.SeparateSamplerId;
  92. }
  93. }
  94. if (src0.Type != OperandType.ConstantBuffer || src1.Type != OperandType.ConstantBuffer)
  95. {
  96. continue;
  97. }
  98. SetHandle(
  99. config,
  100. texOp,
  101. TextureHandle.PackOffsets(src0.GetCbufOffset(), src1.GetCbufOffset(), handleType),
  102. TextureHandle.PackSlots(src0.GetCbufSlot(), src1.GetCbufSlot()),
  103. rewriteSamplerType);
  104. }
  105. else if (texOp.Inst == Instruction.ImageLoad ||
  106. texOp.Inst == Instruction.ImageStore ||
  107. texOp.Inst == Instruction.ImageAtomic)
  108. {
  109. Operand src0 = Utils.FindLastOperation(texOp.GetSource(0), block);
  110. if (src0.Type == OperandType.ConstantBuffer)
  111. {
  112. int cbufOffset = src0.GetCbufOffset();
  113. int cbufSlot = src0.GetCbufSlot();
  114. if (texOp.Format == TextureFormat.Unknown)
  115. {
  116. if (texOp.Inst == Instruction.ImageAtomic)
  117. {
  118. texOp.Format = config.GetTextureFormatAtomic(cbufOffset, cbufSlot);
  119. }
  120. else
  121. {
  122. texOp.Format = config.GetTextureFormat(cbufOffset, cbufSlot);
  123. }
  124. }
  125. SetHandle(config, texOp, cbufOffset, cbufSlot, false);
  126. }
  127. }
  128. }
  129. }
  130. private static Operand GetSourceForMaskedHandle(Operation asgOp, uint mask)
  131. {
  132. // Assume it was already checked that the operation is bitwise AND.
  133. Operand src0 = asgOp.GetSource(0);
  134. Operand src1 = asgOp.GetSource(1);
  135. if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.ConstantBuffer)
  136. {
  137. // We can't check if the mask matches here as both operands are from a constant buffer.
  138. // Be optimistic and assume it matches. Avoid constant buffer 1 as official drivers
  139. // uses this one to store compiler constants.
  140. return src0.GetCbufSlot() == 1 ? src1 : src0;
  141. }
  142. else if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.Constant)
  143. {
  144. if ((uint)src1.Value == mask)
  145. {
  146. return src0;
  147. }
  148. }
  149. else if (src0.Type == OperandType.Constant && src1.Type == OperandType.ConstantBuffer)
  150. {
  151. if ((uint)src0.Value == mask)
  152. {
  153. return src1;
  154. }
  155. }
  156. return null;
  157. }
  158. private static void SetHandle(ShaderConfig config, TextureOperation texOp, int cbufOffset, int cbufSlot, bool rewriteSamplerType)
  159. {
  160. texOp.SetHandle(cbufOffset, cbufSlot);
  161. if (rewriteSamplerType)
  162. {
  163. texOp.Type = config.GpuAccessor.QuerySamplerType(cbufOffset, cbufSlot);
  164. }
  165. config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, cbufSlot, cbufOffset);
  166. }
  167. }
  168. }