ShaderOpCodeTable.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. namespace Ryujinx.Graphics.Gal.Shader
  3. {
  4. static class ShaderOpCodeTable
  5. {
  6. private const int EncodingBits = 14;
  7. private class ShaderDecodeEntry
  8. {
  9. public ShaderDecodeFunc Func;
  10. public int XBits;
  11. public ShaderDecodeEntry(ShaderDecodeFunc Func, int XBits)
  12. {
  13. this.Func = Func;
  14. this.XBits = XBits;
  15. }
  16. }
  17. private static ShaderDecodeEntry[] OpCodes;
  18. static ShaderOpCodeTable()
  19. {
  20. OpCodes = new ShaderDecodeEntry[1 << EncodingBits];
  21. #region Instructions
  22. Set("0100110000000x", ShaderDecode.Bfe_C);
  23. Set("0011100x00000x", ShaderDecode.Bfe_I);
  24. Set("0101110000000x", ShaderDecode.Bfe_R);
  25. Set("111000100100xx", ShaderDecode.Bra);
  26. Set("111000110000xx", ShaderDecode.Exit);
  27. Set("0100110010101x", ShaderDecode.F2f_C);
  28. Set("0011100x10101x", ShaderDecode.F2f_I);
  29. Set("0101110010101x", ShaderDecode.F2f_R);
  30. Set("0100110010110x", ShaderDecode.F2i_C);
  31. Set("0011100x10110x", ShaderDecode.F2i_I);
  32. Set("0101110010110x", ShaderDecode.F2i_R);
  33. Set("0100110001011x", ShaderDecode.Fadd_C);
  34. Set("0011100x01011x", ShaderDecode.Fadd_I);
  35. Set("000010xxxxxxxx", ShaderDecode.Fadd_I32);
  36. Set("0101110001011x", ShaderDecode.Fadd_R);
  37. Set("010010011xxxxx", ShaderDecode.Ffma_CR);
  38. Set("0011001x1xxxxx", ShaderDecode.Ffma_I);
  39. Set("010100011xxxxx", ShaderDecode.Ffma_RC);
  40. Set("010110011xxxxx", ShaderDecode.Ffma_RR);
  41. Set("0100110001101x", ShaderDecode.Fmul_C);
  42. Set("0011100x01101x", ShaderDecode.Fmul_I);
  43. Set("00011110xxxxxx", ShaderDecode.Fmul_I32);
  44. Set("0101110001101x", ShaderDecode.Fmul_R);
  45. Set("0100110001100x", ShaderDecode.Fmnmx_C);
  46. Set("0011100x01100x", ShaderDecode.Fmnmx_I);
  47. Set("0101110001100x", ShaderDecode.Fmnmx_R);
  48. Set("0100100xxxxxxx", ShaderDecode.Fset_C);
  49. Set("0011000xxxxxxx", ShaderDecode.Fset_I);
  50. Set("01011000xxxxxx", ShaderDecode.Fset_R);
  51. Set("010010111011xx", ShaderDecode.Fsetp_C);
  52. Set("0011011x1011xx", ShaderDecode.Fsetp_I);
  53. Set("010110111011xx", ShaderDecode.Fsetp_R);
  54. Set("0100110010111x", ShaderDecode.I2f_C);
  55. Set("0011100x10111x", ShaderDecode.I2f_I);
  56. Set("0101110010111x", ShaderDecode.I2f_R);
  57. Set("0100110011100x", ShaderDecode.I2i_C);
  58. Set("0011100x11100x", ShaderDecode.I2i_I);
  59. Set("0101110011100x", ShaderDecode.I2i_R);
  60. Set("0100110000100x", ShaderDecode.Imnmx_C);
  61. Set("0011100x00100x", ShaderDecode.Imnmx_I);
  62. Set("0101110000100x", ShaderDecode.Imnmx_R);
  63. Set("11100000xxxxxx", ShaderDecode.Ipa);
  64. Set("0100110000011x", ShaderDecode.Iscadd_C);
  65. Set("0011100x00011x", ShaderDecode.Iscadd_I);
  66. Set("0101110000011x", ShaderDecode.Iscadd_R);
  67. Set("010010110110xx", ShaderDecode.Isetp_C);
  68. Set("0011011x0110xx", ShaderDecode.Isetp_I);
  69. Set("010110110110xx", ShaderDecode.Isetp_R);
  70. Set("111000110011xx", ShaderDecode.Kil);
  71. Set("1110111111011x", ShaderDecode.Ld_A);
  72. Set("1110111110010x", ShaderDecode.Ld_C);
  73. Set("000001xxxxxxxx", ShaderDecode.Lop_I32);
  74. Set("0100110010011x", ShaderDecode.Mov_C);
  75. Set("0011100x10011x", ShaderDecode.Mov_I);
  76. Set("000000010000xx", ShaderDecode.Mov_I32);
  77. Set("0101110010011x", ShaderDecode.Mov_R);
  78. Set("0101000010000x", ShaderDecode.Mufu);
  79. Set("0101000010010x", ShaderDecode.Psetp);
  80. Set("0100110010010x", ShaderDecode.Rro_C);
  81. Set("0011100x10010x", ShaderDecode.Rro_I);
  82. Set("0101110010010x", ShaderDecode.Rro_R);
  83. Set("0100110001001x", ShaderDecode.Shl_C);
  84. Set("0011100x01001x", ShaderDecode.Shl_I);
  85. Set("0101110001001x", ShaderDecode.Shl_R);
  86. Set("0100110000101x", ShaderDecode.Shr_C);
  87. Set("0011100x00101x", ShaderDecode.Shr_I);
  88. Set("0101110000101x", ShaderDecode.Shr_R);
  89. Set("1110111111110x", ShaderDecode.St_A);
  90. Set("110000xxxx111x", ShaderDecode.Tex);
  91. Set("1101111101001x", ShaderDecode.Texq);
  92. Set("1101100xxxxxxx", ShaderDecode.Texs);
  93. Set("1101101xxxxxxx", ShaderDecode.Tlds);
  94. Set("0100111xxxxxxx", ShaderDecode.Xmad_CR);
  95. Set("0011011x00xxxx", ShaderDecode.Xmad_I);
  96. Set("010100010xxxxx", ShaderDecode.Xmad_RC);
  97. Set("0101101100xxxx", ShaderDecode.Xmad_RR);
  98. #endregion
  99. }
  100. private static void Set(string Encoding, ShaderDecodeFunc Func)
  101. {
  102. if (Encoding.Length != EncodingBits)
  103. {
  104. throw new ArgumentException(nameof(Encoding));
  105. }
  106. int Bit = Encoding.Length - 1;
  107. int Value = 0;
  108. int XMask = 0;
  109. int XBits = 0;
  110. int[] XPos = new int[Encoding.Length];
  111. for (int Index = 0; Index < Encoding.Length; Index++, Bit--)
  112. {
  113. char Chr = Encoding[Index];
  114. if (Chr == '1')
  115. {
  116. Value |= 1 << Bit;
  117. }
  118. else if (Chr == 'x')
  119. {
  120. XMask |= 1 << Bit;
  121. XPos[XBits++] = Bit;
  122. }
  123. }
  124. XMask = ~XMask;
  125. ShaderDecodeEntry Entry = new ShaderDecodeEntry(Func, XBits);
  126. for (int Index = 0; Index < (1 << XBits); Index++)
  127. {
  128. Value &= XMask;
  129. for (int X = 0; X < XBits; X++)
  130. {
  131. Value |= ((Index >> X) & 1) << XPos[X];
  132. }
  133. if (OpCodes[Value] == null || OpCodes[Value].XBits > XBits)
  134. {
  135. OpCodes[Value] = Entry;
  136. }
  137. }
  138. }
  139. public static ShaderDecodeFunc GetDecoder(long OpCode)
  140. {
  141. return OpCodes[(ulong)OpCode >> (64 - EncodingBits)]?.Func;
  142. }
  143. }
  144. }