ShaderHeader.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 DoesLoadOrStore { get; }
  70. public bool DoesFp64 { get; }
  71. public int StreamOutMask { get; }
  72. public int ShaderLocalMemoryLowSize { get; }
  73. public int PerPatchAttributeCount { get; }
  74. public int ShaderLocalMemoryHighSize { get; }
  75. public int ThreadsPerInputPrimitive { get; }
  76. public int ShaderLocalMemoryCrsSize { get; }
  77. public OutputTopology OutputTopology { get; }
  78. public int MaxOutputVertexCount { get; }
  79. public int StoreReqStart { get; }
  80. public int StoreReqEnd { get; }
  81. public ImapPixelType[] ImapTypes { get; }
  82. public OmapTarget[] OmapTargets { get; }
  83. public bool OmapSampleMask { get; }
  84. public bool OmapDepth { get; }
  85. public ShaderHeader(ReadOnlySpan<byte> code)
  86. {
  87. ReadOnlySpan<int> header = MemoryMarshal.Cast<byte, int>(code);
  88. int commonWord0 = header[0];
  89. int commonWord1 = header[1];
  90. int commonWord2 = header[2];
  91. int commonWord3 = header[3];
  92. int commonWord4 = header[4];
  93. SphType = commonWord0.Extract(0, 5);
  94. Version = commonWord0.Extract(5, 5);
  95. Stage = (ShaderStage)commonWord0.Extract(10, 4);
  96. // Invalid.
  97. if (Stage == ShaderStage.Compute)
  98. {
  99. Stage = ShaderStage.Vertex;
  100. }
  101. MrtEnable = commonWord0.Extract(14);
  102. KillsPixels = commonWord0.Extract(15);
  103. DoesGlobalStore = commonWord0.Extract(16);
  104. SassVersion = commonWord0.Extract(17, 4);
  105. DoesLoadOrStore = commonWord0.Extract(26);
  106. DoesFp64 = commonWord0.Extract(27);
  107. StreamOutMask = commonWord0.Extract(28, 4);
  108. ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);
  109. PerPatchAttributeCount = commonWord1.Extract(24, 8);
  110. ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);
  111. ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);
  112. ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);
  113. OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);
  114. MaxOutputVertexCount = commonWord4.Extract(0, 12);
  115. StoreReqStart = commonWord4.Extract(12, 8);
  116. StoreReqEnd = commonWord4.Extract(24, 8);
  117. ImapTypes = new ImapPixelType[32];
  118. for (int i = 0; i < 8; i++)
  119. {
  120. for (int j = 0; j < 4; j++)
  121. {
  122. byte imap = (byte)(header[6 + i] >> (j * 8));
  123. ImapTypes[i * 4 + j] = new ImapPixelType(
  124. (PixelImap)((imap >> 0) & 3),
  125. (PixelImap)((imap >> 2) & 3),
  126. (PixelImap)((imap >> 4) & 3),
  127. (PixelImap)((imap >> 6) & 3));
  128. }
  129. }
  130. int type2OmapTarget = header[18];
  131. int type2Omap = header[19];
  132. OmapTargets = new OmapTarget[8];
  133. for (int offset = 0; offset < OmapTargets.Length * 4; offset += 4)
  134. {
  135. OmapTargets[offset >> 2] = new OmapTarget(
  136. type2OmapTarget.Extract(offset + 0),
  137. type2OmapTarget.Extract(offset + 1),
  138. type2OmapTarget.Extract(offset + 2),
  139. type2OmapTarget.Extract(offset + 3));
  140. }
  141. OmapSampleMask = type2Omap.Extract(0);
  142. OmapDepth = type2Omap.Extract(1);
  143. }
  144. }
  145. }