2018-12-24 04:23:00 +00: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"
|
2019-06-05 01:44:06 +00:00
|
|
|
#include "video_core/shader/node_helper.h"
|
2018-12-24 04:23:00 +00:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
2020-03-28 21:49:50 +00:00
|
|
|
using std::move;
|
2018-12-24 04:23:00 +00:00
|
|
|
using Tegra::Shader::Instruction;
|
|
|
|
using Tegra::Shader::OpCode;
|
|
|
|
using Tegra::Shader::Pred;
|
|
|
|
using Tegra::Shader::VideoType;
|
|
|
|
using Tegra::Shader::VmadShr;
|
2020-03-28 21:49:50 +00:00
|
|
|
using Tegra::Shader::VmnmxOperation;
|
|
|
|
using Tegra::Shader::VmnmxType;
|
2018-12-24 04:23:00 +00:00
|
|
|
|
2019-01-30 05:09:40 +00:00
|
|
|
u32 ShaderIR::DecodeVideo(NodeBlock& bb, u32 pc) {
|
2018-12-24 04:23:00 +00:00
|
|
|
const Instruction instr = {program_code[pc]};
|
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
|
2020-03-28 21:49:50 +00:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::VMNMX) {
|
|
|
|
DecodeVMNMX(bb, instr);
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2018-12-24 04:23:00 +00:00
|
|
|
const Node op_a =
|
|
|
|
GetVideoOperand(GetRegister(instr.gpr8), instr.video.is_byte_chunk_a, instr.video.signed_a,
|
|
|
|
instr.video.type_a, instr.video.byte_height_a);
|
2019-10-24 01:26:07 +00:00
|
|
|
const Node op_b = [this, instr] {
|
2018-12-24 04:23:00 +00:00
|
|
|
if (instr.video.use_register_b) {
|
|
|
|
return GetVideoOperand(GetRegister(instr.gpr20), instr.video.is_byte_chunk_b,
|
|
|
|
instr.video.signed_b, instr.video.type_b,
|
|
|
|
instr.video.byte_height_b);
|
|
|
|
}
|
|
|
|
if (instr.video.signed_b) {
|
|
|
|
const auto imm = static_cast<s16>(instr.alu.GetImm20_16());
|
|
|
|
return Immediate(static_cast<u32>(imm));
|
|
|
|
} else {
|
|
|
|
return Immediate(instr.alu.GetImm20_16());
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::VMAD: {
|
|
|
|
const bool result_signed = instr.video.signed_a == 1 || instr.video.signed_b == 1;
|
|
|
|
const Node op_c = GetRegister(instr.gpr39);
|
|
|
|
|
|
|
|
Node value = SignedOperation(OperationCode::IMul, result_signed, NO_PRECISE, op_a, op_b);
|
|
|
|
value = SignedOperation(OperationCode::IAdd, result_signed, NO_PRECISE, value, op_c);
|
|
|
|
|
|
|
|
if (instr.vmad.shr == VmadShr::Shr7 || instr.vmad.shr == VmadShr::Shr15) {
|
|
|
|
const Node shift = Immediate(instr.vmad.shr == VmadShr::Shr7 ? 7 : 15);
|
|
|
|
value =
|
|
|
|
SignedOperation(OperationCode::IArithmeticShiftRight, result_signed, value, shift);
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:50:36 +00:00
|
|
|
SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
|
2018-12-24 04:23:00 +00:00
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::VSETP: {
|
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.vsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
|
|
|
const bool sign = instr.video.signed_a == 1 || instr.video.signed_b == 1;
|
|
|
|
const Node first_pred = GetPredicateComparisonInteger(instr.vsetp.cond, sign, op_a, op_b);
|
|
|
|
const Node second_pred = GetPredicate(instr.vsetp.pred39, false);
|
|
|
|
|
|
|
|
const OperationCode combiner = GetPredicateCombiner(instr.vsetp.op);
|
|
|
|
|
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(bb, instr.vsetp.pred3, Operation(combiner, first_pred, second_pred));
|
|
|
|
|
|
|
|
if (instr.vsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
|
|
|
const Node negate_pred = Operation(OperationCode::LogicalNegate, first_pred);
|
|
|
|
SetPredicate(bb, instr.vsetp.pred0, Operation(combiner, negate_pred, second_pred));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled video instruction: {}", opcode->get().GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node ShaderIR::GetVideoOperand(Node op, bool is_chunk, bool is_signed,
|
|
|
|
Tegra::Shader::VideoType type, u64 byte_height) {
|
|
|
|
if (!is_chunk) {
|
2018-12-26 05:58:47 +00:00
|
|
|
return BitfieldExtract(op, static_cast<u32>(byte_height * 8), 8);
|
2018-12-24 04:23:00 +00:00
|
|
|
}
|
|
|
|
const Node zero = Immediate(0);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Tegra::Shader::VideoType::Size16_Low:
|
2018-12-26 05:58:47 +00:00
|
|
|
return BitfieldExtract(op, 0, 16);
|
2018-12-24 04:23:00 +00:00
|
|
|
case Tegra::Shader::VideoType::Size16_High:
|
2018-12-26 05:58:47 +00:00
|
|
|
return BitfieldExtract(op, 16, 16);
|
2018-12-24 04:23:00 +00:00
|
|
|
case Tegra::Shader::VideoType::Size32:
|
|
|
|
// TODO(Rodrigo): From my hardware tests it becomes a bit "mad" when this type is used
|
|
|
|
// (1 * 1 + 0 == 0x5b800000). Until a better explanation is found: abort.
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
return zero;
|
|
|
|
case Tegra::Shader::VideoType::Invalid:
|
|
|
|
UNREACHABLE_MSG("Invalid instruction encoding");
|
|
|
|
return zero;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 21:49:50 +00:00
|
|
|
void ShaderIR::DecodeVMNMX(NodeBlock& bb, Tegra::Shader::Instruction instr) {
|
|
|
|
UNIMPLEMENTED_IF(!instr.vmnmx.is_op_b_register);
|
|
|
|
UNIMPLEMENTED_IF(instr.vmnmx.SourceFormatA() != VmnmxType::Bits32);
|
|
|
|
UNIMPLEMENTED_IF(instr.vmnmx.SourceFormatB() != VmnmxType::Bits32);
|
|
|
|
UNIMPLEMENTED_IF(instr.vmnmx.is_src_a_signed != instr.vmnmx.is_src_b_signed);
|
|
|
|
UNIMPLEMENTED_IF(instr.vmnmx.sat);
|
|
|
|
UNIMPLEMENTED_IF(instr.generates_cc);
|
|
|
|
|
|
|
|
Node op_a = GetRegister(instr.gpr8);
|
|
|
|
Node op_b = GetRegister(instr.gpr20);
|
|
|
|
Node op_c = GetRegister(instr.gpr39);
|
|
|
|
|
|
|
|
const bool is_oper1_signed = instr.vmnmx.is_src_a_signed; // Stubbed
|
|
|
|
const bool is_oper2_signed = instr.vmnmx.is_dest_signed;
|
|
|
|
|
|
|
|
const auto operation_a = instr.vmnmx.mx ? OperationCode::IMax : OperationCode::IMin;
|
|
|
|
Node value = SignedOperation(operation_a, is_oper1_signed, move(op_a), move(op_b));
|
|
|
|
|
|
|
|
switch (instr.vmnmx.operation) {
|
|
|
|
case VmnmxOperation::Mrg_16H:
|
|
|
|
value = BitfieldInsert(move(op_c), move(value), 16, 16);
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Mrg_16L:
|
|
|
|
value = BitfieldInsert(move(op_c), move(value), 0, 16);
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Mrg_8B0:
|
|
|
|
value = BitfieldInsert(move(op_c), move(value), 0, 8);
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Mrg_8B2:
|
|
|
|
value = BitfieldInsert(move(op_c), move(value), 16, 8);
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Acc:
|
|
|
|
value = Operation(OperationCode::IAdd, move(value), move(op_c));
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Min:
|
|
|
|
value = SignedOperation(OperationCode::IMin, is_oper2_signed, move(value), move(op_c));
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Max:
|
|
|
|
value = SignedOperation(OperationCode::IMax, is_oper2_signed, move(value), move(op_c));
|
|
|
|
break;
|
|
|
|
case VmnmxOperation::Nop:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetRegister(bb, instr.gpr0, move(value));
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:24:35 +00:00
|
|
|
} // namespace VideoCommon::Shader
|