BindlessElimination.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  4. {
  5. class BindlessElimination
  6. {
  7. private const int NvnTextureBufferSlot = 2;
  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 the result of a bitwise OR logical operation.
  14. // - Both sources of the OR operation comes from CB2 (used by NVN to hold texture handles).
  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.TextureSample)
  26. {
  27. if (!(texOp.GetSource(0).AsgOp is Operation handleCombineOp))
  28. {
  29. continue;
  30. }
  31. if (handleCombineOp.Inst != Instruction.BitwiseOr)
  32. {
  33. continue;
  34. }
  35. Operand src0 = handleCombineOp.GetSource(0);
  36. Operand src1 = handleCombineOp.GetSource(1);
  37. if (src0.Type != OperandType.ConstantBuffer || src0.GetCbufSlot() != NvnTextureBufferSlot ||
  38. src1.Type != OperandType.ConstantBuffer || src1.GetCbufSlot() != NvnTextureBufferSlot)
  39. {
  40. continue;
  41. }
  42. texOp.SetHandle(src0.GetCbufOffset() | (src1.GetCbufOffset() << 16));
  43. }
  44. else if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore)
  45. {
  46. Operand src0 = texOp.GetSource(0);
  47. if (src0.Type == OperandType.ConstantBuffer && src0.GetCbufSlot() == NvnTextureBufferSlot)
  48. {
  49. texOp.SetHandle(src0.GetCbufOffset());
  50. texOp.Format = config.GetTextureFormat(texOp.Handle);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }