BindlessElimination.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = texOp.GetSource(0);
  30. if (bindlessHandle.Type == OperandType.ConstantBuffer)
  31. {
  32. texOp.SetHandle(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 = handleCombineOp.GetSource(0);
  44. Operand src1 = handleCombineOp.GetSource(1);
  45. if (src0.Type != OperandType.ConstantBuffer ||
  46. src1.Type != OperandType.ConstantBuffer || src0.GetCbufSlot() != src1.GetCbufSlot())
  47. {
  48. continue;
  49. }
  50. texOp.SetHandle(src0.GetCbufOffset() | (src1.GetCbufOffset() << 16), src0.GetCbufSlot());
  51. }
  52. else if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore)
  53. {
  54. Operand src0 = texOp.GetSource(0);
  55. if (src0.Type == OperandType.ConstantBuffer)
  56. {
  57. texOp.SetHandle(src0.GetCbufOffset(), src0.GetCbufSlot());
  58. texOp.Format = config.GetTextureFormat(texOp.Handle);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }