GlobalToStorage.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 StorageDescSize = 4; // In words.
  10. private const int StorageMaxCount = 16;
  11. private const int StorageDescsSize = StorageDescSize * StorageMaxCount;
  12. public static void RunPass(BasicBlock block, ShaderStage stage)
  13. {
  14. int sbStart = GetStorageBaseCbOffset(stage);
  15. int sbEnd = sbStart + StorageDescsSize;
  16. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  17. {
  18. if (!(node.Value is Operation operation))
  19. {
  20. continue;
  21. }
  22. if (operation.Inst == Instruction.LoadGlobal ||
  23. operation.Inst == Instruction.StoreGlobal)
  24. {
  25. Operand source = operation.GetSource(0);
  26. if (source.AsgOp is Operation asgOperation)
  27. {
  28. int storageIndex = SearchForStorageBase(asgOperation, sbStart, sbEnd);
  29. if (storageIndex >= 0)
  30. {
  31. node = ReplaceGlobalWithStorage(node, storageIndex);
  32. }
  33. }
  34. }
  35. }
  36. }
  37. private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, int storageIndex)
  38. {
  39. Operation operation = (Operation)node.Value;
  40. Operation storageOp;
  41. if (operation.Inst == Instruction.LoadGlobal)
  42. {
  43. Operand source = operation.GetSource(0);
  44. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, Const(storageIndex), source);
  45. }
  46. else
  47. {
  48. Operand src1 = operation.GetSource(0);
  49. Operand src2 = operation.GetSource(1);
  50. storageOp = new Operation(Instruction.StoreStorage, null, Const(storageIndex), src1, src2);
  51. }
  52. for (int index = 0; index < operation.SourcesCount; index++)
  53. {
  54. operation.SetSource(index, null);
  55. }
  56. LinkedListNode<INode> oldNode = node;
  57. node = node.List.AddAfter(node, storageOp);
  58. node.List.Remove(oldNode);
  59. return node;
  60. }
  61. private static int SearchForStorageBase(Operation operation, int sbStart, int sbEnd)
  62. {
  63. Queue<Operation> assignments = new Queue<Operation>();
  64. assignments.Enqueue(operation);
  65. while (assignments.TryDequeue(out operation))
  66. {
  67. for (int index = 0; index < operation.SourcesCount; index++)
  68. {
  69. Operand source = operation.GetSource(index);
  70. if (source.Type == OperandType.ConstantBuffer)
  71. {
  72. int slot = source.GetCbufSlot();
  73. int offset = source.GetCbufOffset();
  74. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  75. {
  76. int storageIndex = (offset - sbStart) / StorageDescSize;
  77. return storageIndex;
  78. }
  79. }
  80. if (source.AsgOp is Operation asgOperation)
  81. {
  82. assignments.Enqueue(asgOperation);
  83. }
  84. }
  85. }
  86. return -1;
  87. }
  88. private static int GetStorageBaseCbOffset(ShaderStage stage)
  89. {
  90. switch (stage)
  91. {
  92. case ShaderStage.Compute: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  93. case ShaderStage.Vertex: return StorageDescsBaseOffset;
  94. case ShaderStage.TessellationControl: return StorageDescsBaseOffset + 1 * StorageDescsSize;
  95. case ShaderStage.TessellationEvaluation: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  96. case ShaderStage.Geometry: return StorageDescsBaseOffset + 3 * StorageDescsSize;
  97. case ShaderStage.Fragment: return StorageDescsBaseOffset + 4 * StorageDescsSize;
  98. }
  99. return 0;
  100. }
  101. }
  102. }