ShaderOpCodeTable.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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("0100110000010x", ShaderDecode.Iadd_C);
  61. Set("0011100000010x", ShaderDecode.Iadd_I);
  62. Set("0001110x0xxxxx", ShaderDecode.Iadd_I32);
  63. Set("0101110000010x", ShaderDecode.Iadd_R);
  64. Set("010011001100xx", ShaderDecode.Iadd3_C);
  65. Set("001110001100xx", ShaderDecode.Iadd3_I);
  66. Set("010111001100xx", ShaderDecode.Iadd3_R);
  67. Set("0100110000100x", ShaderDecode.Imnmx_C);
  68. Set("0011100x00100x", ShaderDecode.Imnmx_I);
  69. Set("0101110000100x", ShaderDecode.Imnmx_R);
  70. Set("1110111111010x", ShaderDecode.Isberd);
  71. Set("11100000xxxxxx", ShaderDecode.Ipa);
  72. Set("0100110000011x", ShaderDecode.Iscadd_C);
  73. Set("0011100x00011x", ShaderDecode.Iscadd_I);
  74. Set("0101110000011x", ShaderDecode.Iscadd_R);
  75. Set("010010110101xx", ShaderDecode.Iset_C);
  76. Set("001101100101xx", ShaderDecode.Iset_I);
  77. Set("010110110101xx", ShaderDecode.Iset_R);
  78. Set("010010110110xx", ShaderDecode.Isetp_C);
  79. Set("0011011x0110xx", ShaderDecode.Isetp_I);
  80. Set("010110110110xx", ShaderDecode.Isetp_R);
  81. Set("111000110011xx", ShaderDecode.Kil);
  82. Set("1110111111011x", ShaderDecode.Ld_A);
  83. Set("1110111110010x", ShaderDecode.Ld_C);
  84. Set("0100110001000x", ShaderDecode.Lop_C);
  85. Set("0011100001000x", ShaderDecode.Lop_I);
  86. Set("000001xxxxxxxx", ShaderDecode.Lop_I32);
  87. Set("0101110001000x", ShaderDecode.Lop_R);
  88. Set("0100110010011x", ShaderDecode.Mov_C);
  89. Set("0011100x10011x", ShaderDecode.Mov_I);
  90. Set("000000010000xx", ShaderDecode.Mov_I32);
  91. Set("0101110010011x", ShaderDecode.Mov_R);
  92. Set("1111000011001x", ShaderDecode.Mov_S);
  93. Set("0101000010000x", ShaderDecode.Mufu);
  94. Set("1111101111100x", ShaderDecode.Out_R);
  95. Set("0101000010010x", ShaderDecode.Psetp);
  96. Set("0100110010010x", ShaderDecode.Rro_C);
  97. Set("0011100x10010x", ShaderDecode.Rro_I);
  98. Set("0101110010010x", ShaderDecode.Rro_R);
  99. Set("0100110010100x", ShaderDecode.Sel_C);
  100. Set("0011100010100x", ShaderDecode.Sel_I);
  101. Set("0101110010100x", ShaderDecode.Sel_R);
  102. Set("0100110001001x", ShaderDecode.Shl_C);
  103. Set("0011100x01001x", ShaderDecode.Shl_I);
  104. Set("0101110001001x", ShaderDecode.Shl_R);
  105. Set("0100110000101x", ShaderDecode.Shr_C);
  106. Set("0011100x00101x", ShaderDecode.Shr_I);
  107. Set("0101110000101x", ShaderDecode.Shr_R);
  108. Set("111000101001xx", ShaderDecode.Ssy);
  109. Set("1110111111110x", ShaderDecode.St_A);
  110. Set("1111000011111x", ShaderDecode.Sync);
  111. Set("110000xxxx111x", ShaderDecode.Tex);
  112. Set("1101111010111x", ShaderDecode.Tex_B);
  113. Set("1101111101001x", ShaderDecode.Texq);
  114. Set("1101100xxxxxxx", ShaderDecode.Texs);
  115. Set("1101101xxxxxxx", ShaderDecode.Tlds);
  116. Set("01011111xxxxxx", ShaderDecode.Vmad);
  117. Set("0100111xxxxxxx", ShaderDecode.Xmad_CR);
  118. Set("0011011x00xxxx", ShaderDecode.Xmad_I);
  119. Set("010100010xxxxx", ShaderDecode.Xmad_RC);
  120. Set("0101101100xxxx", ShaderDecode.Xmad_RR);
  121. #endregion
  122. }
  123. private static void Set(string Encoding, ShaderDecodeFunc Func)
  124. {
  125. if (Encoding.Length != EncodingBits)
  126. {
  127. throw new ArgumentException(nameof(Encoding));
  128. }
  129. int Bit = Encoding.Length - 1;
  130. int Value = 0;
  131. int XMask = 0;
  132. int XBits = 0;
  133. int[] XPos = new int[Encoding.Length];
  134. for (int Index = 0; Index < Encoding.Length; Index++, Bit--)
  135. {
  136. char Chr = Encoding[Index];
  137. if (Chr == '1')
  138. {
  139. Value |= 1 << Bit;
  140. }
  141. else if (Chr == 'x')
  142. {
  143. XMask |= 1 << Bit;
  144. XPos[XBits++] = Bit;
  145. }
  146. }
  147. XMask = ~XMask;
  148. ShaderDecodeEntry Entry = new ShaderDecodeEntry(Func, XBits);
  149. for (int Index = 0; Index < (1 << XBits); Index++)
  150. {
  151. Value &= XMask;
  152. for (int X = 0; X < XBits; X++)
  153. {
  154. Value |= ((Index >> X) & 1) << XPos[X];
  155. }
  156. if (OpCodes[Value] == null || OpCodes[Value].XBits > XBits)
  157. {
  158. OpCodes[Value] = Entry;
  159. }
  160. }
  161. }
  162. public static ShaderDecodeFunc GetDecoder(long OpCode)
  163. {
  164. return OpCodes[(ulong)OpCode >> (64 - EncodingBits)]?.Func;
  165. }
  166. }
  167. }