bfe.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, v1, Immediate(mask));
  40. }
  41. Node v2 = op_a;
  42. if (mask != 0) {
  43. v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, op_a, Immediate(mask));
  44. }
  45. v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, v2, Immediate(s));
  46. return SignedOperation(OperationCode::IBitwiseOr, is_signed, v1, v2);
  47. };
  48. op_a = swap(1, 0x55555555U);
  49. op_a = swap(2, 0x33333333U);
  50. op_a = swap(4, 0x0F0F0F0FU);
  51. op_a = swap(8, 0x00FF00FFU);
  52. op_a = swap(16, 0);
  53. }
  54. const auto offset = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b,
  55. Immediate(0), Immediate(8));
  56. const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b,
  57. Immediate(8), Immediate(8));
  58. const auto result =
  59. SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits);
  60. SetRegister(bb, instr.gpr0, result);
  61. return pc;
  62. }
  63. } // namespace VideoCommon::Shader