2018-12-20 17:09:21 -05: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"
|
|
|
|
#include "video_core/engines/shader_bytecode.h"
|
|
|
|
#include "video_core/shader/shader_ir.h"
|
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
|
|
|
using Tegra::Shader::Instruction;
|
|
|
|
using Tegra::Shader::OpCode;
|
|
|
|
|
2019-01-30 00:09:40 -05:00
|
|
|
u32 ShaderIR::DecodeXmad(NodeBlock& bb, u32 pc) {
|
2018-12-20 17:09:21 -05:00
|
|
|
const Instruction instr = {program_code[pc]};
|
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
|
2018-12-17 16:09:40 -05:00
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_a);
|
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_b);
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in XMAD is not implemented");
|
|
|
|
|
2018-12-21 01:12:16 -05:00
|
|
|
Node op_a = GetRegister(instr.gpr8);
|
2018-12-17 16:09:40 -05:00
|
|
|
|
|
|
|
// TODO(bunnei): Needs to be fixed once op_a or op_b is signed
|
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_a != instr.xmad.sign_b);
|
|
|
|
const bool is_signed_a = instr.xmad.sign_a == 1;
|
|
|
|
const bool is_signed_b = instr.xmad.sign_b == 1;
|
|
|
|
const bool is_signed_c = is_signed_a;
|
|
|
|
|
|
|
|
auto [is_merge, op_b, op_c] = [&]() -> std::tuple<bool, Node, Node> {
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::XMAD_CR:
|
2019-01-28 16:11:23 -05:00
|
|
|
return {instr.xmad.merge_56,
|
|
|
|
GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset()),
|
2018-12-17 16:09:40 -05:00
|
|
|
GetRegister(instr.gpr39)};
|
|
|
|
case OpCode::Id::XMAD_RR:
|
|
|
|
return {instr.xmad.merge_37, GetRegister(instr.gpr20), GetRegister(instr.gpr39)};
|
|
|
|
case OpCode::Id::XMAD_RC:
|
|
|
|
return {false, GetRegister(instr.gpr39),
|
2019-01-28 16:11:23 -05:00
|
|
|
GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
|
2018-12-17 16:09:40 -05:00
|
|
|
case OpCode::Id::XMAD_IMM:
|
|
|
|
return {instr.xmad.merge_37, Immediate(static_cast<u32>(instr.xmad.imm20_16)),
|
|
|
|
GetRegister(instr.gpr39)};
|
|
|
|
}
|
2018-12-21 16:47:22 -05:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled XMAD instruction: {}", opcode->get().GetName());
|
|
|
|
return {false, Immediate(0), Immediate(0)};
|
2018-12-17 16:09:40 -05:00
|
|
|
}();
|
|
|
|
|
2018-12-26 00:58:47 -05:00
|
|
|
op_a = BitfieldExtract(op_a, instr.xmad.high_a ? 16 : 0, 16);
|
2018-12-17 16:09:40 -05:00
|
|
|
|
|
|
|
const Node original_b = op_b;
|
2018-12-26 00:58:47 -05:00
|
|
|
op_b = BitfieldExtract(op_b, instr.xmad.high_b ? 16 : 0, 16);
|
2018-12-17 16:09:40 -05:00
|
|
|
|
|
|
|
// TODO(Rodrigo): Use an appropiate sign for this operation
|
|
|
|
Node product = Operation(OperationCode::IMul, NO_PRECISE, op_a, op_b);
|
|
|
|
if (instr.xmad.product_shift_left) {
|
2018-12-29 23:05:14 -05:00
|
|
|
product = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, product, Immediate(16));
|
2018-12-17 16:09:40 -05:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:06:05 -05:00
|
|
|
const Node original_c = op_c;
|
2018-12-17 16:09:40 -05:00
|
|
|
op_c = [&]() {
|
|
|
|
switch (instr.xmad.mode) {
|
|
|
|
case Tegra::Shader::XmadMode::None:
|
2019-01-15 19:06:05 -05:00
|
|
|
return original_c;
|
2018-12-17 16:09:40 -05:00
|
|
|
case Tegra::Shader::XmadMode::CLo:
|
2019-01-15 19:06:05 -05:00
|
|
|
return BitfieldExtract(original_c, 0, 16);
|
2018-12-17 16:09:40 -05:00
|
|
|
case Tegra::Shader::XmadMode::CHi:
|
2019-01-15 19:06:05 -05:00
|
|
|
return BitfieldExtract(original_c, 16, 16);
|
2018-12-17 16:09:40 -05:00
|
|
|
case Tegra::Shader::XmadMode::CBcc: {
|
|
|
|
const Node shifted_b = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed_b,
|
|
|
|
NO_PRECISE, original_b, Immediate(16));
|
2019-01-15 19:06:05 -05:00
|
|
|
return SignedOperation(OperationCode::IAdd, is_signed_c, NO_PRECISE, original_c,
|
|
|
|
shifted_b);
|
2018-12-17 16:09:40 -05:00
|
|
|
}
|
2018-12-21 16:47:22 -05:00
|
|
|
default:
|
2018-12-17 16:09:40 -05:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled XMAD mode: {}", static_cast<u32>(instr.xmad.mode.Value()));
|
2018-12-21 16:47:22 -05:00
|
|
|
return Immediate(0);
|
2018-12-17 16:09:40 -05:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
// TODO(Rodrigo): Use an appropiate sign for this operation
|
|
|
|
Node sum = Operation(OperationCode::IAdd, product, op_c);
|
|
|
|
if (is_merge) {
|
2018-12-26 00:58:47 -05:00
|
|
|
const Node a = BitfieldExtract(sum, 0, 16);
|
2018-12-17 16:09:40 -05:00
|
|
|
const Node b =
|
2018-12-26 00:58:47 -05:00
|
|
|
Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, original_b, Immediate(16));
|
2018-12-17 16:09:40 -05:00
|
|
|
sum = Operation(OperationCode::IBitwiseOr, NO_PRECISE, a, b);
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:50:36 -05:00
|
|
|
SetInternalFlagsFromInteger(bb, sum, instr.generates_cc);
|
2018-12-17 16:09:40 -05:00
|
|
|
SetRegister(bb, instr.gpr0, sum);
|
2018-12-20 17:09:21 -05:00
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace VideoCommon::Shader
|