GlobalToStorage.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  4. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  5. {
  6. static class GlobalToStorage
  7. {
  8. private const int StorageDescsBaseOffset = 0x44; // In words.
  9. private const int UbeStorageDescsBaseOffset = 0x84; // In words.
  10. private const int UbeStorageMaxCount = 14;
  11. private const int StorageDescSize = 4; // In words.
  12. private const int StorageMaxCount = 16;
  13. private const int StorageDescsSize = StorageDescSize * StorageMaxCount;
  14. public static void RunPass(BasicBlock block, ShaderStage stage)
  15. {
  16. int sbStart = GetStorageBaseCbOffset(stage);
  17. int sbEnd = sbStart + StorageDescsSize;
  18. // This one is only used on compute shaders.
  19. // Compute shaders uses two separate sets of storage.
  20. int ubeSbStart = UbeStorageDescsBaseOffset;
  21. int ubeSbEnd = UbeStorageDescsBaseOffset + StorageDescSize * UbeStorageMaxCount;
  22. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  23. {
  24. if (!(node.Value is Operation operation))
  25. {
  26. continue;
  27. }
  28. if (operation.Inst == Instruction.LoadGlobal ||
  29. operation.Inst == Instruction.StoreGlobal)
  30. {
  31. Operand source = operation.GetSource(0);
  32. if (source.AsgOp is Operation asgOperation)
  33. {
  34. int storageIndex = SearchForStorageBase(asgOperation, sbStart, sbEnd);
  35. /*if (storageIndex < 0 && stage == ShaderStage.Compute)
  36. {
  37. storageIndex = SearchForStorageBase(asgOperation, ubeSbStart, ubeSbEnd);
  38. }*/
  39. if (storageIndex >= 0)
  40. {
  41. node = ReplaceGlobalWithStorage(node, storageIndex);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, int storageIndex)
  48. {
  49. Operation operation = (Operation)node.Value;
  50. Operation storageOp;
  51. if (operation.Inst == Instruction.LoadGlobal)
  52. {
  53. Operand source = operation.GetSource(0);
  54. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, Const(storageIndex), source);
  55. }
  56. else
  57. {
  58. Operand src1 = operation.GetSource(0);
  59. Operand src2 = operation.GetSource(1);
  60. storageOp = new Operation(Instruction.StoreStorage, null, Const(storageIndex), src1, src2);
  61. }
  62. for (int index = 0; index < operation.SourcesCount; index++)
  63. {
  64. operation.SetSource(index, null);
  65. }
  66. LinkedListNode<INode> oldNode = node;
  67. node = node.List.AddAfter(node, storageOp);
  68. node.List.Remove(oldNode);
  69. return node;
  70. }
  71. private static int SearchForStorageBase(Operation operation, int sbStart, int sbEnd)
  72. {
  73. Queue<Operation> assignments = new Queue<Operation>();
  74. assignments.Enqueue(operation);
  75. while (assignments.TryDequeue(out operation))
  76. {
  77. for (int index = 0; index < operation.SourcesCount; index++)
  78. {
  79. Operand source = operation.GetSource(index);
  80. if (source.Type == OperandType.ConstantBuffer)
  81. {
  82. int slot = source.GetCbufSlot();
  83. int offset = source.GetCbufOffset();
  84. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  85. {
  86. int storageIndex = (offset - sbStart) / StorageDescSize;
  87. return storageIndex;
  88. }
  89. }
  90. if (source.AsgOp is Operation asgOperation)
  91. {
  92. assignments.Enqueue(asgOperation);
  93. }
  94. }
  95. }
  96. return -1;
  97. }
  98. private static int GetStorageBaseCbOffset(ShaderStage stage)
  99. {
  100. switch (stage)
  101. {
  102. case ShaderStage.Compute: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  103. case ShaderStage.Vertex: return StorageDescsBaseOffset;
  104. case ShaderStage.TessellationControl: return StorageDescsBaseOffset + 1 * StorageDescsSize;
  105. case ShaderStage.TessellationEvaluation: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  106. case ShaderStage.Geometry: return StorageDescsBaseOffset + 3 * StorageDescsSize;
  107. case ShaderStage.Fragment: return StorageDescsBaseOffset + 4 * StorageDescsSize;
  108. }
  109. return 0;
  110. }
  111. }
  112. }