2018-12-20 23:09:21 +01:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
2019-05-22 23:17:19 +02:00
|
|
|
#include "common/logging/log.h"
|
2018-12-20 23:09:21 +01:00
|
|
|
#include "video_core/engines/shader_bytecode.h"
|
2019-06-05 03:44:06 +02:00
|
|
|
#include "video_core/shader/node_helper.h"
|
2018-12-20 23:09:21 +01:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
|
|
|
using Tegra::Shader::Instruction;
|
|
|
|
using Tegra::Shader::OpCode;
|
2018-12-21 03:53:50 +01:00
|
|
|
using Tegra::Shader::SubOp;
|
2018-12-20 23:09:21 +01:00
|
|
|
|
2019-01-30 06:09:40 +01:00
|
|
|
u32 ShaderIR::DecodeArithmetic(NodeBlock& bb, u32 pc) {
|
2018-12-20 23:09:21 +01:00
|
|
|
const Instruction instr = {program_code[pc]};
|
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
|
2018-12-21 03:53:50 +01:00
|
|
|
Node op_a = GetRegister(instr.gpr8);
|
|
|
|
|
2020-01-27 05:14:25 +01:00
|
|
|
Node op_b = [&] {
|
2018-12-21 03:53:50 +01:00
|
|
|
if (instr.is_b_imm) {
|
|
|
|
return GetImmediate19(instr);
|
|
|
|
} else if (instr.is_b_gpr) {
|
|
|
|
return GetRegister(instr.gpr20);
|
|
|
|
} else {
|
2019-01-28 22:11:23 +01:00
|
|
|
return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
|
2018-12-21 03:53:50 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::MOV_C:
|
|
|
|
case OpCode::Id::MOV_R: {
|
|
|
|
// MOV does not have neither 'abs' nor 'neg' bits.
|
|
|
|
SetRegister(bb, instr.gpr0, op_b);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:54:47 +01:00
|
|
|
case OpCode::Id::FMUL_C:
|
|
|
|
case OpCode::Id::FMUL_R:
|
|
|
|
case OpCode::Id::FMUL_IMM: {
|
|
|
|
// FMUL does not have 'abs' bits and only the second operand has a 'neg' bit.
|
2019-07-20 04:15:34 +02:00
|
|
|
if (instr.fmul.tab5cb8_2 != 0) {
|
2019-11-04 06:24:58 +01:00
|
|
|
LOG_DEBUG(HW_GPU, "FMUL tab5cb8_2({}) is not implemented",
|
|
|
|
instr.fmul.tab5cb8_2.Value());
|
2019-07-20 04:15:34 +02:00
|
|
|
}
|
|
|
|
if (instr.fmul.tab5c68_0 != 1) {
|
2019-11-04 06:24:58 +01:00
|
|
|
LOG_DEBUG(HW_GPU, "FMUL tab5cb8_0({}) is not implemented",
|
|
|
|
instr.fmul.tab5c68_0.Value());
|
2019-07-20 04:15:34 +02:00
|
|
|
}
|
2018-12-21 03:54:47 +01:00
|
|
|
|
|
|
|
op_b = GetOperandAbsNegFloat(op_b, false, instr.fmul.negate_b);
|
|
|
|
|
2020-02-27 17:14:25 +01:00
|
|
|
static constexpr std::array FmulPostFactor = {
|
|
|
|
1.000f, // None
|
|
|
|
0.500f, // Divide 2
|
|
|
|
0.250f, // Divide 4
|
|
|
|
0.125f, // Divide 8
|
|
|
|
8.000f, // Mul 8
|
|
|
|
4.000f, // Mul 4
|
|
|
|
2.000f, // Mul 2
|
|
|
|
};
|
2018-12-21 03:54:47 +01:00
|
|
|
|
|
|
|
if (instr.fmul.postfactor != 0) {
|
2020-02-27 17:14:25 +01:00
|
|
|
op_a = Operation(OperationCode::FMul, NO_PRECISE, op_a,
|
|
|
|
Immediate(FmulPostFactor[instr.fmul.postfactor]));
|
2018-12-21 03:54:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:14:25 +01:00
|
|
|
// TODO(Rodrigo): Should precise be used when there's a postfactor?
|
|
|
|
Node value = Operation(OperationCode::FMul, PRECISE, op_a, op_b);
|
|
|
|
|
2018-12-21 03:54:47 +01:00
|
|
|
value = GetSaturatedFloat(value, instr.alu.saturate_d);
|
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
|
2018-12-21 03:54:47 +01:00
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:55:19 +01:00
|
|
|
case OpCode::Id::FADD_C:
|
|
|
|
case OpCode::Id::FADD_R:
|
|
|
|
case OpCode::Id::FADD_IMM: {
|
|
|
|
op_a = GetOperandAbsNegFloat(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
|
|
|
op_b = GetOperandAbsNegFloat(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
|
|
|
|
|
|
|
Node value = Operation(OperationCode::FAdd, PRECISE, op_a, op_b);
|
|
|
|
value = GetSaturatedFloat(value, instr.alu.saturate_d);
|
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
|
2018-12-21 03:55:19 +01:00
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:56:21 +01:00
|
|
|
case OpCode::Id::MUFU: {
|
|
|
|
op_a = GetOperandAbsNegFloat(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
|
|
|
|
|
|
|
Node value = [&]() {
|
|
|
|
switch (instr.sub_op) {
|
|
|
|
case SubOp::Cos:
|
|
|
|
return Operation(OperationCode::FCos, PRECISE, op_a);
|
|
|
|
case SubOp::Sin:
|
|
|
|
return Operation(OperationCode::FSin, PRECISE, op_a);
|
|
|
|
case SubOp::Ex2:
|
|
|
|
return Operation(OperationCode::FExp2, PRECISE, op_a);
|
|
|
|
case SubOp::Lg2:
|
|
|
|
return Operation(OperationCode::FLog2, PRECISE, op_a);
|
|
|
|
case SubOp::Rcp:
|
|
|
|
return Operation(OperationCode::FDiv, PRECISE, Immediate(1.0f), op_a);
|
|
|
|
case SubOp::Rsq:
|
|
|
|
return Operation(OperationCode::FInverseSqrt, PRECISE, op_a);
|
|
|
|
case SubOp::Sqrt:
|
|
|
|
return Operation(OperationCode::FSqrt, PRECISE, op_a);
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled MUFU sub op={0:x}",
|
|
|
|
static_cast<unsigned>(instr.sub_op.Value()));
|
2018-12-21 22:47:22 +01:00
|
|
|
return Immediate(0);
|
2018-12-21 03:56:21 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
value = GetSaturatedFloat(value, instr.alu.saturate_d);
|
|
|
|
|
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:56:45 +01:00
|
|
|
case OpCode::Id::FMNMX_C:
|
|
|
|
case OpCode::Id::FMNMX_R:
|
|
|
|
case OpCode::Id::FMNMX_IMM: {
|
|
|
|
op_a = GetOperandAbsNegFloat(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
|
|
|
op_b = GetOperandAbsNegFloat(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
|
|
|
|
|
|
|
const Node condition = GetPredicate(instr.alu.fmnmx.pred, instr.alu.fmnmx.negate_pred != 0);
|
|
|
|
|
|
|
|
const Node min = Operation(OperationCode::FMin, NO_PRECISE, op_a, op_b);
|
|
|
|
const Node max = Operation(OperationCode::FMax, NO_PRECISE, op_a, op_b);
|
2018-12-27 20:50:36 +01:00
|
|
|
const Node value = Operation(OperationCode::Select, NO_PRECISE, condition, min, max);
|
2018-12-21 03:56:45 +01:00
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
|
|
|
|
SetRegister(bb, instr.gpr0, value);
|
2018-12-21 03:56:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-27 05:14:25 +01:00
|
|
|
case OpCode::Id::FCMP_R: {
|
|
|
|
UNIMPLEMENTED_IF(instr.fcmp.ftz == 0);
|
|
|
|
Node op_c = GetRegister(instr.gpr39);
|
|
|
|
Node comp = GetPredicateComparisonFloat(instr.fcmp.cond, std::move(op_c), Immediate(0.0f));
|
|
|
|
SetRegister(
|
|
|
|
bb, instr.gpr0,
|
|
|
|
Operation(OperationCode::Select, std::move(comp), std::move(op_a), std::move(op_b)));
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:57:09 +01:00
|
|
|
case OpCode::Id::RRO_C:
|
|
|
|
case OpCode::Id::RRO_R:
|
|
|
|
case OpCode::Id::RRO_IMM: {
|
2019-11-04 06:19:45 +01:00
|
|
|
LOG_DEBUG(HW_GPU, "(STUBBED) RRO used");
|
|
|
|
|
2018-12-21 03:57:09 +01:00
|
|
|
// Currently RRO is only implemented as a register move.
|
|
|
|
op_b = GetOperandAbsNegFloat(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
|
|
|
SetRegister(bb, instr.gpr0, op_b);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 03:53:50 +01:00
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled arithmetic instruction: {}", opcode->get().GetName());
|
|
|
|
}
|
2018-12-20 23:09:21 +01:00
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:24:35 +02:00
|
|
|
} // namespace VideoCommon::Shader
|