ShaderHeader.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Graphics.Shader.Translation
  5. {
  6. struct OutputMapTarget
  7. {
  8. public bool Red { get; }
  9. public bool Green { get; }
  10. public bool Blue { get; }
  11. public bool Alpha { get; }
  12. public bool Enabled => Red || Green || Blue || Alpha;
  13. public OutputMapTarget(bool red, bool green, bool blue, bool alpha)
  14. {
  15. Red = red;
  16. Green = green;
  17. Blue = blue;
  18. Alpha = alpha;
  19. }
  20. public bool ComponentEnabled(int component)
  21. {
  22. switch (component)
  23. {
  24. case 0: return Red;
  25. case 1: return Green;
  26. case 2: return Blue;
  27. case 3: return Alpha;
  28. }
  29. throw new ArgumentOutOfRangeException(nameof(component));
  30. }
  31. }
  32. class ShaderHeader
  33. {
  34. public int SphType { get; }
  35. public int Version { get; }
  36. public ShaderStage Stage { get; }
  37. public bool MrtEnable { get; }
  38. public bool KillsPixels { get; }
  39. public bool DoesGlobalStore { get; }
  40. public int SassVersion { get; }
  41. public bool DoesLoadOrStore { get; }
  42. public bool DoesFp64 { get; }
  43. public int StreamOutMask { get; }
  44. public int ShaderLocalMemoryLowSize { get; }
  45. public int PerPatchAttributeCount { get; }
  46. public int ShaderLocalMemoryHighSize { get; }
  47. public int ThreadsPerInputPrimitive { get; }
  48. public int ShaderLocalMemoryCrsSize { get; }
  49. public OutputTopology OutputTopology { get; }
  50. public int MaxOutputVertexCount { get; }
  51. public int StoreReqStart { get; }
  52. public int StoreReqEnd { get; }
  53. public OutputMapTarget[] OmapTargets { get; }
  54. public bool OmapSampleMask { get; }
  55. public bool OmapDepth { get; }
  56. public ShaderHeader(ReadOnlySpan<byte> code)
  57. {
  58. ReadOnlySpan<int> header = MemoryMarshal.Cast<byte, int>(code);
  59. int commonWord0 = header[0];
  60. int commonWord1 = header[1];
  61. int commonWord2 = header[2];
  62. int commonWord3 = header[3];
  63. int commonWord4 = header[4];
  64. SphType = commonWord0.Extract(0, 5);
  65. Version = commonWord0.Extract(5, 5);
  66. Stage = (ShaderStage)commonWord0.Extract(10, 4);
  67. // Invalid.
  68. if (Stage == ShaderStage.Compute)
  69. {
  70. Stage = ShaderStage.Vertex;
  71. }
  72. MrtEnable = commonWord0.Extract(14);
  73. KillsPixels = commonWord0.Extract(15);
  74. DoesGlobalStore = commonWord0.Extract(16);
  75. SassVersion = commonWord0.Extract(17, 4);
  76. DoesLoadOrStore = commonWord0.Extract(26);
  77. DoesFp64 = commonWord0.Extract(27);
  78. StreamOutMask = commonWord0.Extract(28, 4);
  79. ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);
  80. PerPatchAttributeCount = commonWord1.Extract(24, 8);
  81. ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);
  82. ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);
  83. ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);
  84. OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);
  85. MaxOutputVertexCount = commonWord4.Extract(0, 12);
  86. StoreReqStart = commonWord4.Extract(12, 8);
  87. StoreReqEnd = commonWord4.Extract(24, 8);
  88. int type2OmapTarget = header[18];
  89. int type2Omap = header[19];
  90. OmapTargets = new OutputMapTarget[8];
  91. for (int offset = 0; offset < OmapTargets.Length * 4; offset += 4)
  92. {
  93. OmapTargets[offset >> 2] = new OutputMapTarget(
  94. type2OmapTarget.Extract(offset + 0),
  95. type2OmapTarget.Extract(offset + 1),
  96. type2OmapTarget.Extract(offset + 2),
  97. type2OmapTarget.Extract(offset + 3));
  98. }
  99. OmapSampleMask = type2Omap.Extract(0);
  100. OmapDepth = type2Omap.Extract(1);
  101. }
  102. }
  103. }