ShaderOpCodeTable.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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("11100000xxxxxx", ShaderDecode.Ipa);
  60. Set("0100110000011x", ShaderDecode.Iscadd_C);
  61. Set("0011100x00011x", ShaderDecode.Iscadd_I);
  62. Set("0101110000011x", ShaderDecode.Iscadd_R);
  63. Set("010010110110xx", ShaderDecode.Isetp_C);
  64. Set("0011011x0110xx", ShaderDecode.Isetp_I);
  65. Set("010110110110xx", ShaderDecode.Isetp_R);
  66. Set("111000110011xx", ShaderDecode.Kil);
  67. Set("1110111111011x", ShaderDecode.Ld_A);
  68. Set("1110111110010x", ShaderDecode.Ld_C);
  69. Set("000001xxxxxxxx", ShaderDecode.Lop_I32);
  70. Set("0100110010011x", ShaderDecode.Mov_C);
  71. Set("0011100x10011x", ShaderDecode.Mov_I);
  72. Set("000000010000xx", ShaderDecode.Mov_I32);
  73. Set("0101110010011x", ShaderDecode.Mov_R);
  74. Set("0101000010000x", ShaderDecode.Mufu);
  75. Set("0100110001001x", ShaderDecode.Shl_C);
  76. Set("0011100x01001x", ShaderDecode.Shl_I);
  77. Set("0101110001001x", ShaderDecode.Shl_R);
  78. Set("0100110000101x", ShaderDecode.Shr_C);
  79. Set("0011100x00101x", ShaderDecode.Shr_I);
  80. Set("0101110000101x", ShaderDecode.Shr_R);
  81. Set("1110111111110x", ShaderDecode.St_A);
  82. Set("1101111101001x", ShaderDecode.Texq);
  83. Set("1101100xxxxxxx", ShaderDecode.Texs);
  84. Set("1101101xxxxxxx", ShaderDecode.Tlds);
  85. #endregion
  86. }
  87. private static void Set(string Encoding, ShaderDecodeFunc Func)
  88. {
  89. if (Encoding.Length != EncodingBits)
  90. {
  91. throw new ArgumentException(nameof(Encoding));
  92. }
  93. int Bit = Encoding.Length - 1;
  94. int Value = 0;
  95. int XMask = 0;
  96. int XBits = 0;
  97. int[] XPos = new int[Encoding.Length];
  98. for (int Index = 0; Index < Encoding.Length; Index++, Bit--)
  99. {
  100. char Chr = Encoding[Index];
  101. if (Chr == '1')
  102. {
  103. Value |= 1 << Bit;
  104. }
  105. else if (Chr == 'x')
  106. {
  107. XMask |= 1 << Bit;
  108. XPos[XBits++] = Bit;
  109. }
  110. }
  111. XMask = ~XMask;
  112. ShaderDecodeEntry Entry = new ShaderDecodeEntry(Func, XBits);
  113. for (int Index = 0; Index < (1 << XBits); Index++)
  114. {
  115. Value &= XMask;
  116. for (int X = 0; X < XBits; X++)
  117. {
  118. Value |= ((Index >> X) & 1) << XPos[X];
  119. }
  120. if (OpCodes[Value] == null || OpCodes[Value].XBits > XBits)
  121. {
  122. OpCodes[Value] = Entry;
  123. }
  124. }
  125. }
  126. public static ShaderDecodeFunc GetDecoder(long OpCode)
  127. {
  128. return OpCodes[(ulong)OpCode >> (64 - EncodingBits)]?.Func;
  129. }
  130. }
  131. }