ShaderHeader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Graphics.Shader.Translation
  5. {
  6. enum PixelImap
  7. {
  8. Unused = 0,
  9. Constant = 1,
  10. Perspective = 2,
  11. ScreenLinear = 3
  12. }
  13. struct ImapPixelType
  14. {
  15. public PixelImap X { get; }
  16. public PixelImap Y { get; }
  17. public PixelImap Z { get; }
  18. public PixelImap W { get; }
  19. public ImapPixelType(PixelImap x, PixelImap y, PixelImap z, PixelImap w)
  20. {
  21. X = x;
  22. Y = y;
  23. Z = z;
  24. W = w;
  25. }
  26. public PixelImap GetFirstUsedType()
  27. {
  28. if (X != PixelImap.Unused) return X;
  29. if (Y != PixelImap.Unused) return Y;
  30. if (Z != PixelImap.Unused) return Z;
  31. return W;
  32. }
  33. }
  34. class ShaderHeader
  35. {
  36. public int SphType { get; }
  37. public int Version { get; }
  38. public ShaderStage Stage { get; }
  39. public bool MrtEnable { get; }
  40. public bool KillsPixels { get; }
  41. public bool DoesGlobalStore { get; }
  42. public int SassVersion { get; }
  43. public bool GpPassthrough { get; }
  44. public bool DoesLoadOrStore { get; }
  45. public bool DoesFp64 { get; }
  46. public int StreamOutMask { get; }
  47. public int ShaderLocalMemoryLowSize { get; }
  48. public int PerPatchAttributeCount { get; }
  49. public int ShaderLocalMemoryHighSize { get; }
  50. public int ThreadsPerInputPrimitive { get; }
  51. public int ShaderLocalMemoryCrsSize { get; }
  52. public OutputTopology OutputTopology { get; }
  53. public int MaxOutputVertexCount { get; }
  54. public int StoreReqStart { get; }
  55. public int StoreReqEnd { get; }
  56. public ImapPixelType[] ImapTypes { get; }
  57. public int OmapTargets { get; }
  58. public bool OmapSampleMask { get; }
  59. public bool OmapDepth { get; }
  60. public ShaderHeader(IGpuAccessor gpuAccessor, ulong address)
  61. {
  62. ReadOnlySpan<int> header = MemoryMarshal.Cast<ulong, int>(gpuAccessor.GetCode(address, 0x50));
  63. int commonWord0 = header[0];
  64. int commonWord1 = header[1];
  65. int commonWord2 = header[2];
  66. int commonWord3 = header[3];
  67. int commonWord4 = header[4];
  68. SphType = commonWord0.Extract(0, 5);
  69. Version = commonWord0.Extract(5, 5);
  70. Stage = (ShaderStage)commonWord0.Extract(10, 4);
  71. // Invalid.
  72. if (Stage == ShaderStage.Compute)
  73. {
  74. Stage = ShaderStage.Vertex;
  75. }
  76. MrtEnable = commonWord0.Extract(14);
  77. KillsPixels = commonWord0.Extract(15);
  78. DoesGlobalStore = commonWord0.Extract(16);
  79. SassVersion = commonWord0.Extract(17, 4);
  80. GpPassthrough = commonWord0.Extract(24);
  81. DoesLoadOrStore = commonWord0.Extract(26);
  82. DoesFp64 = commonWord0.Extract(27);
  83. StreamOutMask = commonWord0.Extract(28, 4);
  84. ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);
  85. PerPatchAttributeCount = commonWord1.Extract(24, 8);
  86. ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);
  87. ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);
  88. ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);
  89. OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);
  90. MaxOutputVertexCount = commonWord4.Extract(0, 12);
  91. StoreReqStart = commonWord4.Extract(12, 8);
  92. StoreReqEnd = commonWord4.Extract(24, 8);
  93. ImapTypes = new ImapPixelType[32];
  94. for (int i = 0; i < 32; i++)
  95. {
  96. byte imap = (byte)(header[6 + (i >> 2)] >> ((i & 3) * 8));
  97. ImapTypes[i] = new ImapPixelType(
  98. (PixelImap)((imap >> 0) & 3),
  99. (PixelImap)((imap >> 2) & 3),
  100. (PixelImap)((imap >> 4) & 3),
  101. (PixelImap)((imap >> 6) & 3));
  102. }
  103. int type2OmapTarget = header[18];
  104. int type2Omap = header[19];
  105. OmapTargets = type2OmapTarget;
  106. OmapSampleMask = type2Omap.Extract(0);
  107. OmapDepth = type2Omap.Extract(1);
  108. }
  109. }
  110. }