BindlessElimination.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (bindlessHandle.Type == OperandType.ConstantBuffer)
  31. {
  32. SetHandle(config, texOp, bindlessHandle.GetCbufOffset(), bindlessHandle.GetCbufSlot());
  33. continue;
  34. }
  35. if (!(bindlessHandle.AsgOp is Operation handleCombineOp))
  36. {
  37. continue;
  38. }
  39. if (handleCombineOp.Inst != Instruction.BitwiseOr)
  40. {
  41. continue;
  42. }
  43. Operand src0 = Utils.FindLastOperation(handleCombineOp.GetSource(0), block);
  44. Operand src1 = Utils.FindLastOperation(handleCombineOp.GetSource(1), block);
  45. if (src0.Type != OperandType.ConstantBuffer || src1.Type != OperandType.ConstantBuffer)
  46. {
  47. continue;
  48. }
  49. SetHandle(
  50. config,
  51. texOp,
  52. src0.GetCbufOffset() | (src1.GetCbufOffset() << 16),
  53. src0.GetCbufSlot() | ((src1.GetCbufSlot() + 1) << 16));
  54. }
  55. else if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore)
  56. {
  57. Operand src0 = Utils.FindLastOperation(texOp.GetSource(0), block);
  58. if (src0.Type == OperandType.ConstantBuffer)
  59. {
  60. int cbufOffset = src0.GetCbufOffset();
  61. int cbufSlot = src0.GetCbufSlot();
  62. texOp.Format = config.GetTextureFormat(cbufOffset, cbufSlot);
  63. SetHandle(config, texOp, cbufOffset, cbufSlot);
  64. }
  65. }
  66. }
  67. }
  68. private static void SetHandle(ShaderConfig config, TextureOperation texOp, int cbufOffset, int cbufSlot)
  69. {
  70. texOp.SetHandle(cbufOffset, cbufSlot);
  71. config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, cbufSlot, cbufOffset);
  72. }
  73. }
  74. }