bfe.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "video_core/engines/shader_bytecode.h"
  7. #include "video_core/shader/node_helper.h"
  8. #include "video_core/shader/shader_ir.h"
  9. namespace VideoCommon::Shader {
  10. using Tegra::Shader::Instruction;
  11. using Tegra::Shader::OpCode;
  12. u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) {
  13. const Instruction instr = {program_code[pc]};
  14. const auto opcode = OpCode::Decode(instr);
  15. Node op_a = GetRegister(instr.gpr8);
  16. Node op_b = [&] {
  17. switch (opcode->get().GetId()) {
  18. case OpCode::Id::BFE_R:
  19. return GetRegister(instr.gpr20);
  20. case OpCode::Id::BFE_C:
  21. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  22. case OpCode::Id::BFE_IMM:
  23. return Immediate(instr.alu.GetSignedImm20_20());
  24. default:
  25. UNREACHABLE();
  26. return Immediate(0);
  27. }
  28. }();
  29. UNIMPLEMENTED_IF_MSG(instr.bfe.rd_cc, "Condition codes in BFE is not implemented");
  30. const bool is_signed = instr.bfe.is_signed;
  31. // using reverse parallel method in
  32. // https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
  33. // note for later if possible to implement faster method.
  34. if (instr.bfe.brev) {
  35. const auto swap = [&](u32 s, u32 mask) {
  36. Node v1 =
  37. SignedOperation(OperationCode::ILogicalShiftRight, is_signed, op_a, Immediate(s));
  38. if (mask != 0) {
  39. v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v1),
  40. Immediate(mask));
  41. }
  42. Node v2 = op_a;
  43. if (mask != 0) {
  44. v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v2),
  45. Immediate(mask));
  46. }
  47. v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, std::move(v2),
  48. Immediate(s));
  49. return SignedOperation(OperationCode::IBitwiseOr, is_signed, std::move(v1),
  50. std::move(v2));
  51. };
  52. op_a = swap(1, 0x55555555U);
  53. op_a = swap(2, 0x33333333U);
  54. op_a = swap(4, 0x0F0F0F0FU);
  55. op_a = swap(8, 0x00FF00FFU);
  56. op_a = swap(16, 0);
  57. }
  58. const auto offset = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b,
  59. Immediate(0), Immediate(8));
  60. const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b,
  61. Immediate(8), Immediate(8));
  62. const auto result =
  63. SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits);
  64. SetRegister(bb, instr.gpr0, std::move(result));
  65. return pc;
  66. }
  67. } // namespace VideoCommon::Shader