ShaderHeader.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. struct OmapTarget
  35. {
  36. public bool Red { get; }
  37. public bool Green { get; }
  38. public bool Blue { get; }
  39. public bool Alpha { get; }
  40. public bool Enabled => Red || Green || Blue || Alpha;
  41. public OmapTarget(bool red, bool green, bool blue, bool alpha)
  42. {
  43. Red = red;
  44. Green = green;
  45. Blue = blue;
  46. Alpha = alpha;
  47. }
  48. public bool ComponentEnabled(int component)
  49. {
  50. switch (component)
  51. {
  52. case 0: return Red;
  53. case 1: return Green;
  54. case 2: return Blue;
  55. case 3: return Alpha;
  56. }
  57. throw new ArgumentOutOfRangeException(nameof(component));
  58. }
  59. }
  60. class ShaderHeader
  61. {
  62. public int SphType { get; }
  63. public int Version { get; }
  64. public ShaderStage Stage { get; }
  65. public bool MrtEnable { get; }
  66. public bool KillsPixels { get; }
  67. public bool DoesGlobalStore { get; }
  68. public int SassVersion { get; }
  69. public bool GpPassthrough { get; }
  70. public bool DoesLoadOrStore { get; }
  71. public bool DoesFp64 { get; }
  72. public int StreamOutMask { get; }
  73. public int ShaderLocalMemoryLowSize { get; }
  74. public int PerPatchAttributeCount { get; }
  75. public int ShaderLocalMemoryHighSize { get; }
  76. public int ThreadsPerInputPrimitive { get; }
  77. public int ShaderLocalMemoryCrsSize { get; }
  78. public OutputTopology OutputTopology { get; }
  79. public int MaxOutputVertexCount { get; }
  80. public int StoreReqStart { get; }
  81. public int StoreReqEnd { get; }
  82. public ImapPixelType[] ImapTypes { get; }
  83. public OmapTarget[] OmapTargets { get; }
  84. public bool OmapSampleMask { get; }
  85. public bool OmapDepth { get; }
  86. public ShaderHeader(IGpuAccessor gpuAccessor, ulong address)
  87. {
  88. ReadOnlySpan<int> header = MemoryMarshal.Cast<ulong, int>(gpuAccessor.GetCode(address, 0x50));
  89. int commonWord0 = header[0];
  90. int commonWord1 = header[1];
  91. int commonWord2 = header[2];
  92. int commonWord3 = header[3];
  93. int commonWord4 = header[4];
  94. SphType = commonWord0.Extract(0, 5);
  95. Version = commonWord0.Extract(5, 5);
  96. Stage = (ShaderStage)commonWord0.Extract(10, 4);
  97. // Invalid.
  98. if (Stage == ShaderStage.Compute)
  99. {
  100. Stage = ShaderStage.Vertex;
  101. }
  102. MrtEnable = commonWord0.Extract(14);
  103. KillsPixels = commonWord0.Extract(15);
  104. DoesGlobalStore = commonWord0.Extract(16);
  105. SassVersion = commonWord0.Extract(17, 4);
  106. GpPassthrough = commonWord0.Extract(24);
  107. DoesLoadOrStore = commonWord0.Extract(26);
  108. DoesFp64 = commonWord0.Extract(27);
  109. StreamOutMask = commonWord0.Extract(28, 4);
  110. ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);
  111. PerPatchAttributeCount = commonWord1.Extract(24, 8);
  112. ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);
  113. ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);
  114. ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);
  115. OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);
  116. MaxOutputVertexCount = commonWord4.Extract(0, 12);
  117. StoreReqStart = commonWord4.Extract(12, 8);
  118. StoreReqEnd = commonWord4.Extract(24, 8);
  119. ImapTypes = new ImapPixelType[32];
  120. for (int i = 0; i < 32; i++)
  121. {
  122. byte imap = (byte)(header[6 + (i >> 2)] >> ((i & 3) * 8));
  123. ImapTypes[i] = new ImapPixelType(
  124. (PixelImap)((imap >> 0) & 3),
  125. (PixelImap)((imap >> 2) & 3),
  126. (PixelImap)((imap >> 4) & 3),
  127. (PixelImap)((imap >> 6) & 3));
  128. }
  129. int type2OmapTarget = header[18];
  130. int type2Omap = header[19];
  131. OmapTargets = new OmapTarget[8];
  132. for (int offset = 0; offset < OmapTargets.Length * 4; offset += 4)
  133. {
  134. OmapTargets[offset >> 2] = new OmapTarget(
  135. type2OmapTarget.Extract(offset + 0),
  136. type2OmapTarget.Extract(offset + 1),
  137. type2OmapTarget.Extract(offset + 2),
  138. type2OmapTarget.Extract(offset + 3));
  139. }
  140. OmapSampleMask = type2Omap.Extract(0);
  141. OmapDepth = type2Omap.Extract(1);
  142. }
  143. }
  144. }