ShaderOpCodeTable.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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("0101110001011x", ShaderDecode.Fadd_R);
  36. Set("010010011xxxxx", ShaderDecode.Ffma_CR);
  37. Set("001100101xxxxx", ShaderDecode.Ffma_I);
  38. Set("010100011xxxxx", ShaderDecode.Ffma_RC);
  39. Set("010110011xxxxx", ShaderDecode.Ffma_RR);
  40. Set("00011110xxxxxx", ShaderDecode.Fmul_I32);
  41. Set("0100110001101x", ShaderDecode.Fmul_C);
  42. Set("0011100x01101x", ShaderDecode.Fmul_I);
  43. Set("0101110001101x", ShaderDecode.Fmul_R);
  44. Set("0100110001100x", ShaderDecode.Fmnmx_C);
  45. Set("0011100x01100x", ShaderDecode.Fmnmx_I);
  46. Set("0101110001100x", ShaderDecode.Fmnmx_R);
  47. Set("0100100xxxxxxx", ShaderDecode.Fset_C);
  48. Set("0011000xxxxxxx", ShaderDecode.Fset_I);
  49. Set("01011000xxxxxx", ShaderDecode.Fset_R);
  50. Set("010010111011xx", ShaderDecode.Fsetp_C);
  51. Set("0011011x1011xx", ShaderDecode.Fsetp_I);
  52. Set("010110111011xx", ShaderDecode.Fsetp_R);
  53. Set("0100110010111x", ShaderDecode.I2f_C);
  54. Set("0011100x10111x", ShaderDecode.I2f_I);
  55. Set("0101110010111x", ShaderDecode.I2f_R);
  56. Set("0100110011100x", ShaderDecode.I2i_C);
  57. Set("0011100x11100x", ShaderDecode.I2i_I);
  58. Set("0101110011100x", ShaderDecode.I2i_R);
  59. Set("0100110000100x", ShaderDecode.Imnmx_C);
  60. Set("0011100x00100x", ShaderDecode.Imnmx_I);
  61. Set("0101110000100x", ShaderDecode.Imnmx_R);
  62. Set("11100000xxxxxx", ShaderDecode.Ipa);
  63. Set("0100110000011x", ShaderDecode.Iscadd_C);
  64. Set("0011100x00011x", ShaderDecode.Iscadd_I);
  65. Set("0101110000011x", ShaderDecode.Iscadd_R);
  66. Set("010010110110xx", ShaderDecode.Isetp_C);
  67. Set("0011011x0110xx", ShaderDecode.Isetp_I);
  68. Set("010110110110xx", ShaderDecode.Isetp_R);
  69. Set("111000110011xx", ShaderDecode.Kil);
  70. Set("1110111111011x", ShaderDecode.Ld_A);
  71. Set("1110111110010x", ShaderDecode.Ld_C);
  72. Set("000001xxxxxxxx", ShaderDecode.Lop_I32);
  73. Set("0100110010011x", ShaderDecode.Mov_C);
  74. Set("0011100x10011x", ShaderDecode.Mov_I);
  75. Set("000000010000xx", ShaderDecode.Mov_I32);
  76. Set("0101110010011x", ShaderDecode.Mov_R);
  77. Set("0101000010000x", ShaderDecode.Mufu);
  78. Set("0100110001001x", ShaderDecode.Shl_C);
  79. Set("0011100x01001x", ShaderDecode.Shl_I);
  80. Set("0101110001001x", ShaderDecode.Shl_R);
  81. Set("0100110000101x", ShaderDecode.Shr_C);
  82. Set("0011100x00101x", ShaderDecode.Shr_I);
  83. Set("0101110000101x", ShaderDecode.Shr_R);
  84. Set("1110111111110x", ShaderDecode.St_A);
  85. Set("1101111101001x", ShaderDecode.Texq);
  86. Set("1101100xxxxxxx", ShaderDecode.Texs);
  87. Set("1101101xxxxxxx", ShaderDecode.Tlds);
  88. Set("0100111xxxxxxx", ShaderDecode.Xmad_CR);
  89. Set("0011011x00xxxx", ShaderDecode.Xmad_I);
  90. Set("010100010xxxxx", ShaderDecode.Xmad_RC);
  91. Set("0101101100xxxx", ShaderDecode.Xmad_RR);
  92. #endregion
  93. }
  94. private static void Set(string Encoding, ShaderDecodeFunc Func)
  95. {
  96. if (Encoding.Length != EncodingBits)
  97. {
  98. throw new ArgumentException(nameof(Encoding));
  99. }
  100. int Bit = Encoding.Length - 1;
  101. int Value = 0;
  102. int XMask = 0;
  103. int XBits = 0;
  104. int[] XPos = new int[Encoding.Length];
  105. for (int Index = 0; Index < Encoding.Length; Index++, Bit--)
  106. {
  107. char Chr = Encoding[Index];
  108. if (Chr == '1')
  109. {
  110. Value |= 1 << Bit;
  111. }
  112. else if (Chr == 'x')
  113. {
  114. XMask |= 1 << Bit;
  115. XPos[XBits++] = Bit;
  116. }
  117. }
  118. XMask = ~XMask;
  119. ShaderDecodeEntry Entry = new ShaderDecodeEntry(Func, XBits);
  120. for (int Index = 0; Index < (1 << XBits); Index++)
  121. {
  122. Value &= XMask;
  123. for (int X = 0; X < XBits; X++)
  124. {
  125. Value |= ((Index >> X) & 1) << XPos[X];
  126. }
  127. if (OpCodes[Value] == null || OpCodes[Value].XBits > XBits)
  128. {
  129. OpCodes[Value] = Entry;
  130. }
  131. }
  132. }
  133. public static ShaderDecodeFunc GetDecoder(long OpCode)
  134. {
  135. return OpCodes[(ulong)OpCode >> (64 - EncodingBits)]?.Func;
  136. }
  137. }
  138. }