conversion.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <limits>
  5. #include <optional>
  6. #include <utility>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "video_core/engines/shader_bytecode.h"
  10. #include "video_core/shader/node_helper.h"
  11. #include "video_core/shader/shader_ir.h"
  12. namespace VideoCommon::Shader {
  13. using Tegra::Shader::Instruction;
  14. using Tegra::Shader::OpCode;
  15. using Tegra::Shader::Register;
  16. namespace {
  17. constexpr OperationCode GetFloatSelector(u64 selector) {
  18. return selector == 0 ? OperationCode::FCastHalf0 : OperationCode::FCastHalf1;
  19. }
  20. constexpr u32 SizeInBits(Register::Size size) {
  21. switch (size) {
  22. case Register::Size::Byte:
  23. return 8;
  24. case Register::Size::Short:
  25. return 16;
  26. case Register::Size::Word:
  27. return 32;
  28. case Register::Size::Long:
  29. return 64;
  30. }
  31. return 0;
  32. }
  33. constexpr std::optional<std::pair<s32, s32>> IntegerSaturateBounds(Register::Size src_size,
  34. Register::Size dst_size,
  35. bool src_signed,
  36. bool dst_signed) {
  37. const u32 dst_bits = SizeInBits(dst_size);
  38. if (src_size == Register::Size::Word && dst_size == Register::Size::Word) {
  39. if (src_signed == dst_signed) {
  40. return std::nullopt;
  41. }
  42. return std::make_pair(0, std::numeric_limits<s32>::max());
  43. }
  44. if (dst_signed) {
  45. // Signed destination, clamp to [-128, 127] for instance
  46. return std::make_pair(-(1 << (dst_bits - 1)), (1 << (dst_bits - 1)) - 1);
  47. } else {
  48. // Unsigned destination
  49. if (dst_bits == 32) {
  50. // Avoid shifting by 32, that is undefined behavior
  51. return std::make_pair(0, s32(std::numeric_limits<u32>::max()));
  52. }
  53. return std::make_pair(0, (1 << dst_bits) - 1);
  54. }
  55. }
  56. } // Anonymous namespace
  57. u32 ShaderIR::DecodeConversion(NodeBlock& bb, u32 pc) {
  58. const Instruction instr = {program_code[pc]};
  59. const auto opcode = OpCode::Decode(instr);
  60. switch (opcode->get().GetId()) {
  61. case OpCode::Id::I2I_R:
  62. case OpCode::Id::I2I_C:
  63. case OpCode::Id::I2I_IMM: {
  64. const bool src_signed = instr.conversion.is_input_signed;
  65. const bool dst_signed = instr.conversion.is_output_signed;
  66. const Register::Size src_size = instr.conversion.src_size;
  67. const Register::Size dst_size = instr.conversion.dst_size;
  68. const u32 selector = static_cast<u32>(instr.conversion.int_src.selector);
  69. Node value = [this, instr, opcode] {
  70. switch (opcode->get().GetId()) {
  71. case OpCode::Id::I2I_R:
  72. return GetRegister(instr.gpr20);
  73. case OpCode::Id::I2I_C:
  74. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  75. case OpCode::Id::I2I_IMM:
  76. return Immediate(instr.alu.GetSignedImm20_20());
  77. default:
  78. UNREACHABLE();
  79. return Immediate(0);
  80. }
  81. }();
  82. // Ensure the source selector is valid
  83. switch (instr.conversion.src_size) {
  84. case Register::Size::Byte:
  85. break;
  86. case Register::Size::Short:
  87. ASSERT(selector == 0 || selector == 2);
  88. break;
  89. default:
  90. ASSERT(selector == 0);
  91. break;
  92. }
  93. if (src_size != Register::Size::Word || selector != 0) {
  94. value = SignedOperation(OperationCode::IBitfieldExtract, src_signed, std::move(value),
  95. Immediate(selector * 8), Immediate(SizeInBits(src_size)));
  96. }
  97. value = GetOperandAbsNegInteger(std::move(value), instr.conversion.abs_a,
  98. instr.conversion.negate_a, src_signed);
  99. if (instr.alu.saturate_d) {
  100. if (src_signed && !dst_signed) {
  101. Node is_negative = Operation(OperationCode::LogicalUGreaterEqual, value,
  102. Immediate(1 << (SizeInBits(src_size) - 1)));
  103. value = Operation(OperationCode::Select, std::move(is_negative), Immediate(0),
  104. std::move(value));
  105. // Simplify generated expressions, this can be removed without semantic impact
  106. SetTemporary(bb, 0, std::move(value));
  107. value = GetTemporary(0);
  108. if (dst_size != Register::Size::Word) {
  109. const Node limit = Immediate((1 << SizeInBits(dst_size)) - 1);
  110. Node is_large =
  111. Operation(OperationCode::LogicalUGreaterThan, std::move(value), limit);
  112. value = Operation(OperationCode::Select, std::move(is_large), limit,
  113. std::move(value));
  114. }
  115. } else if (const std::optional bounds =
  116. IntegerSaturateBounds(src_size, dst_size, src_signed, dst_signed)) {
  117. value = SignedOperation(OperationCode::IMax, src_signed, std::move(value),
  118. Immediate(bounds->first));
  119. value = SignedOperation(OperationCode::IMin, src_signed, std::move(value),
  120. Immediate(bounds->second));
  121. }
  122. } else if (dst_size != Register::Size::Word) {
  123. // No saturation, we only have to mask the result
  124. Node mask = Immediate((1 << SizeInBits(dst_size)) - 1);
  125. value = Operation(OperationCode::UBitwiseAnd, std::move(value), std::move(mask));
  126. }
  127. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  128. SetRegister(bb, instr.gpr0, std::move(value));
  129. break;
  130. }
  131. case OpCode::Id::I2F_R:
  132. case OpCode::Id::I2F_C:
  133. case OpCode::Id::I2F_IMM: {
  134. UNIMPLEMENTED_IF(instr.conversion.dst_size == Register::Size::Long);
  135. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  136. "Condition codes generation in I2F is not implemented");
  137. Node value = [&] {
  138. switch (opcode->get().GetId()) {
  139. case OpCode::Id::I2F_R:
  140. return GetRegister(instr.gpr20);
  141. case OpCode::Id::I2F_C:
  142. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  143. case OpCode::Id::I2F_IMM:
  144. return Immediate(instr.alu.GetSignedImm20_20());
  145. default:
  146. UNREACHABLE();
  147. return Immediate(0);
  148. }
  149. }();
  150. const bool input_signed = instr.conversion.is_input_signed;
  151. if (const u32 offset = static_cast<u32>(instr.conversion.int_src.selector); offset > 0) {
  152. ASSERT(instr.conversion.src_size == Register::Size::Byte ||
  153. instr.conversion.src_size == Register::Size::Short);
  154. if (instr.conversion.src_size == Register::Size::Short) {
  155. ASSERT(offset == 0 || offset == 2);
  156. }
  157. value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed,
  158. std::move(value), Immediate(offset * 8));
  159. }
  160. value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed);
  161. value = GetOperandAbsNegInteger(value, instr.conversion.abs_a, false, input_signed);
  162. value = SignedOperation(OperationCode::FCastInteger, input_signed, PRECISE, value);
  163. value = GetOperandAbsNegFloat(value, false, instr.conversion.negate_a);
  164. SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
  165. if (instr.conversion.dst_size == Register::Size::Short) {
  166. value = Operation(OperationCode::HCastFloat, PRECISE, value);
  167. }
  168. SetRegister(bb, instr.gpr0, value);
  169. break;
  170. }
  171. case OpCode::Id::F2F_R:
  172. case OpCode::Id::F2F_C:
  173. case OpCode::Id::F2F_IMM: {
  174. UNIMPLEMENTED_IF(instr.conversion.dst_size == Register::Size::Long);
  175. UNIMPLEMENTED_IF(instr.conversion.src_size == Register::Size::Long);
  176. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  177. "Condition codes generation in F2F is not implemented");
  178. Node value = [&]() {
  179. switch (opcode->get().GetId()) {
  180. case OpCode::Id::F2F_R:
  181. return GetRegister(instr.gpr20);
  182. case OpCode::Id::F2F_C:
  183. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  184. case OpCode::Id::F2F_IMM:
  185. return GetImmediate19(instr);
  186. default:
  187. UNREACHABLE();
  188. return Immediate(0);
  189. }
  190. }();
  191. if (instr.conversion.src_size == Register::Size::Short) {
  192. value = Operation(GetFloatSelector(instr.conversion.float_src.selector), NO_PRECISE,
  193. std::move(value));
  194. } else {
  195. ASSERT(instr.conversion.float_src.selector == 0);
  196. }
  197. value = GetOperandAbsNegFloat(value, instr.conversion.abs_a, instr.conversion.negate_a);
  198. value = [&] {
  199. if (instr.conversion.src_size != instr.conversion.dst_size) {
  200. // Rounding operations only matter when the source and destination conversion size
  201. // is the same.
  202. return value;
  203. }
  204. switch (instr.conversion.f2f.GetRoundingMode()) {
  205. case Tegra::Shader::F2fRoundingOp::None:
  206. return value;
  207. case Tegra::Shader::F2fRoundingOp::Round:
  208. return Operation(OperationCode::FRoundEven, value);
  209. case Tegra::Shader::F2fRoundingOp::Floor:
  210. return Operation(OperationCode::FFloor, value);
  211. case Tegra::Shader::F2fRoundingOp::Ceil:
  212. return Operation(OperationCode::FCeil, value);
  213. case Tegra::Shader::F2fRoundingOp::Trunc:
  214. return Operation(OperationCode::FTrunc, value);
  215. default:
  216. UNIMPLEMENTED_MSG("Unimplemented F2F rounding mode {}",
  217. instr.conversion.f2f.rounding.Value());
  218. return value;
  219. }
  220. }();
  221. value = GetSaturatedFloat(value, instr.alu.saturate_d);
  222. SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
  223. if (instr.conversion.dst_size == Register::Size::Short) {
  224. value = Operation(OperationCode::HCastFloat, PRECISE, value);
  225. }
  226. SetRegister(bb, instr.gpr0, value);
  227. break;
  228. }
  229. case OpCode::Id::F2I_R:
  230. case OpCode::Id::F2I_C:
  231. case OpCode::Id::F2I_IMM: {
  232. UNIMPLEMENTED_IF(instr.conversion.src_size == Register::Size::Long);
  233. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  234. "Condition codes generation in F2I is not implemented");
  235. Node value = [&]() {
  236. switch (opcode->get().GetId()) {
  237. case OpCode::Id::F2I_R:
  238. return GetRegister(instr.gpr20);
  239. case OpCode::Id::F2I_C:
  240. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  241. case OpCode::Id::F2I_IMM:
  242. return GetImmediate19(instr);
  243. default:
  244. UNREACHABLE();
  245. return Immediate(0);
  246. }
  247. }();
  248. if (instr.conversion.src_size == Register::Size::Short) {
  249. value = Operation(GetFloatSelector(instr.conversion.float_src.selector), NO_PRECISE,
  250. std::move(value));
  251. } else {
  252. ASSERT(instr.conversion.float_src.selector == 0);
  253. }
  254. value = GetOperandAbsNegFloat(value, instr.conversion.abs_a, instr.conversion.negate_a);
  255. value = [&]() {
  256. switch (instr.conversion.f2i.rounding) {
  257. case Tegra::Shader::F2iRoundingOp::RoundEven:
  258. return Operation(OperationCode::FRoundEven, PRECISE, value);
  259. case Tegra::Shader::F2iRoundingOp::Floor:
  260. return Operation(OperationCode::FFloor, PRECISE, value);
  261. case Tegra::Shader::F2iRoundingOp::Ceil:
  262. return Operation(OperationCode::FCeil, PRECISE, value);
  263. case Tegra::Shader::F2iRoundingOp::Trunc:
  264. return Operation(OperationCode::FTrunc, PRECISE, value);
  265. default:
  266. UNIMPLEMENTED_MSG("Unimplemented F2I rounding mode {}",
  267. instr.conversion.f2i.rounding.Value());
  268. return Immediate(0);
  269. }
  270. }();
  271. const bool is_signed = instr.conversion.is_output_signed;
  272. value = SignedOperation(OperationCode::ICastFloat, is_signed, PRECISE, value);
  273. value = ConvertIntegerSize(value, instr.conversion.dst_size, is_signed);
  274. SetRegister(bb, instr.gpr0, value);
  275. break;
  276. }
  277. default:
  278. UNIMPLEMENTED_MSG("Unhandled conversion instruction: {}", opcode->get().GetName());
  279. }
  280. return pc;
  281. }
  282. } // namespace VideoCommon::Shader