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.
|
|
|
|
|
2020-04-07 07:16:51 +02:00
|
|
|
#include <limits>
|
|
|
|
#include <optional>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-12-20 23:09:21 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#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-15 21:32:51 +01:00
|
|
|
using Tegra::Shader::Register;
|
2018-12-20 23:09:21 +01:00
|
|
|
|
2019-08-28 21:09:33 +02:00
|
|
|
namespace {
|
2020-04-07 07:16:51 +02:00
|
|
|
|
2019-08-28 21:09:33 +02:00
|
|
|
constexpr OperationCode GetFloatSelector(u64 selector) {
|
|
|
|
return selector == 0 ? OperationCode::FCastHalf0 : OperationCode::FCastHalf1;
|
|
|
|
}
|
2020-04-07 07:16:51 +02:00
|
|
|
|
|
|
|
constexpr u32 SizeInBits(Register::Size size) {
|
|
|
|
switch (size) {
|
|
|
|
case Register::Size::Byte:
|
|
|
|
return 8;
|
|
|
|
case Register::Size::Short:
|
|
|
|
return 16;
|
|
|
|
case Register::Size::Word:
|
|
|
|
return 32;
|
|
|
|
case Register::Size::Long:
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::optional<std::pair<s32, s32>> IntegerSaturateBounds(Register::Size src_size,
|
|
|
|
Register::Size dst_size,
|
|
|
|
bool src_signed,
|
|
|
|
bool dst_signed) {
|
|
|
|
const u32 dst_bits = SizeInBits(dst_size);
|
|
|
|
if (src_size == Register::Size::Word && dst_size == Register::Size::Word) {
|
|
|
|
if (src_signed == dst_signed) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return std::make_pair(0, std::numeric_limits<s32>::max());
|
|
|
|
}
|
|
|
|
if (dst_signed) {
|
|
|
|
// Signed destination, clamp to [-128, 127] for instance
|
|
|
|
return std::make_pair(-(1 << (dst_bits - 1)), (1 << (dst_bits - 1)) - 1);
|
|
|
|
} else {
|
|
|
|
// Unsigned destination
|
|
|
|
if (dst_bits == 32) {
|
|
|
|
// Avoid shifting by 32, that is undefined behavior
|
|
|
|
return std::make_pair(0, s32(std::numeric_limits<u32>::max()));
|
|
|
|
}
|
|
|
|
return std::make_pair(0, (1 << dst_bits) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:09:33 +02:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2019-01-30 06:09:40 +01:00
|
|
|
u32 ShaderIR::DecodeConversion(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-15 21:32:51 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::I2I_R:
|
|
|
|
case OpCode::Id::I2I_C:
|
|
|
|
case OpCode::Id::I2I_IMM: {
|
2020-04-07 07:16:51 +02:00
|
|
|
const bool src_signed = instr.conversion.is_input_signed;
|
|
|
|
const bool dst_signed = instr.conversion.is_output_signed;
|
|
|
|
const Register::Size src_size = instr.conversion.src_size;
|
|
|
|
const Register::Size dst_size = instr.conversion.dst_size;
|
|
|
|
const u32 selector = static_cast<u32>(instr.conversion.int_src.selector);
|
2018-12-18 01:14:25 +01:00
|
|
|
|
2020-04-07 07:16:51 +02:00
|
|
|
Node value = [this, instr, opcode] {
|
2019-04-16 01:04:33 +02:00
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::I2I_R:
|
|
|
|
return GetRegister(instr.gpr20);
|
|
|
|
case OpCode::Id::I2I_C:
|
|
|
|
return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
|
|
|
|
case OpCode::Id::I2I_IMM:
|
|
|
|
return Immediate(instr.alu.GetSignedImm20_20());
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Immediate(0);
|
|
|
|
}
|
|
|
|
}();
|
2018-12-18 01:14:25 +01:00
|
|
|
|
2020-04-07 07:16:51 +02:00
|
|
|
// Ensure the source selector is valid
|
|
|
|
switch (instr.conversion.src_size) {
|
|
|
|
case Register::Size::Byte:
|
|
|
|
break;
|
|
|
|
case Register::Size::Short:
|
|
|
|
ASSERT(selector == 0 || selector == 2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(selector == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_size != Register::Size::Word || selector != 0) {
|
|
|
|
value = SignedOperation(OperationCode::IBitfieldExtract, src_signed, std::move(value),
|
|
|
|
Immediate(selector * 8), Immediate(SizeInBits(src_size)));
|
|
|
|
}
|
|
|
|
|
|
|
|
value = GetOperandAbsNegInteger(std::move(value), instr.conversion.abs_a,
|
|
|
|
instr.conversion.negate_a, src_signed);
|
|
|
|
|
|
|
|
if (instr.alu.saturate_d) {
|
|
|
|
if (src_signed && !dst_signed) {
|
|
|
|
Node is_negative = Operation(OperationCode::LogicalUGreaterEqual, value,
|
|
|
|
Immediate(1 << (SizeInBits(src_size) - 1)));
|
|
|
|
value = Operation(OperationCode::Select, std::move(is_negative), Immediate(0),
|
|
|
|
std::move(value));
|
|
|
|
|
|
|
|
// Simplify generated expressions, this can be removed without semantic impact
|
|
|
|
SetTemporary(bb, 0, std::move(value));
|
|
|
|
value = GetTemporary(0);
|
|
|
|
|
|
|
|
if (dst_size != Register::Size::Word) {
|
|
|
|
const Node limit = Immediate((1 << SizeInBits(dst_size)) - 1);
|
|
|
|
Node is_large =
|
|
|
|
Operation(OperationCode::LogicalUGreaterThan, std::move(value), limit);
|
|
|
|
value = Operation(OperationCode::Select, std::move(is_large), limit,
|
|
|
|
std::move(value));
|
|
|
|
}
|
|
|
|
} else if (const std::optional bounds =
|
|
|
|
IntegerSaturateBounds(src_size, dst_size, src_signed, dst_signed)) {
|
|
|
|
value = SignedOperation(OperationCode::IMax, src_signed, std::move(value),
|
|
|
|
Immediate(bounds->first));
|
|
|
|
value = SignedOperation(OperationCode::IMin, src_signed, std::move(value),
|
|
|
|
Immediate(bounds->second));
|
|
|
|
}
|
|
|
|
} else if (dst_size != Register::Size::Word) {
|
|
|
|
// No saturation, we only have to mask the result
|
|
|
|
Node mask = Immediate((1 << SizeInBits(dst_size)) - 1);
|
|
|
|
value = Operation(OperationCode::UBitwiseAnd, std::move(value), std::move(mask));
|
2018-12-18 01:14:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
|
2020-04-07 07:16:51 +02:00
|
|
|
SetRegister(bb, instr.gpr0, std::move(value));
|
2018-12-18 01:14:25 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-12-21 05:53:05 +01:00
|
|
|
case OpCode::Id::I2F_R:
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::I2F_C:
|
|
|
|
case OpCode::Id::I2F_IMM: {
|
2019-07-20 23:38:25 +02:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.dst_size == Register::Size::Long);
|
2018-12-21 05:53:05 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in I2F is not implemented");
|
|
|
|
|
2019-12-18 04:05:08 +01:00
|
|
|
Node value = [&] {
|
2019-04-16 01:04:33 +02:00
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::I2F_R:
|
2018-12-21 05:53:05 +01:00
|
|
|
return GetRegister(instr.gpr20);
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::I2F_C:
|
2019-01-28 22:11:23 +01:00
|
|
|
return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::I2F_IMM:
|
|
|
|
return Immediate(instr.alu.GetSignedImm20_20());
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Immediate(0);
|
2018-12-21 05:53:05 +01:00
|
|
|
}
|
|
|
|
}();
|
2019-12-18 04:05:08 +01:00
|
|
|
|
2018-12-21 05:53:05 +01:00
|
|
|
const bool input_signed = instr.conversion.is_input_signed;
|
2019-12-18 04:05:08 +01:00
|
|
|
|
2020-02-19 05:10:26 +01:00
|
|
|
if (const u32 offset = static_cast<u32>(instr.conversion.int_src.selector); offset > 0) {
|
2020-02-19 04:54:37 +01:00
|
|
|
ASSERT(instr.conversion.src_size == Register::Size::Byte ||
|
|
|
|
instr.conversion.src_size == Register::Size::Short);
|
2020-02-19 05:40:35 +01:00
|
|
|
if (instr.conversion.src_size == Register::Size::Short) {
|
|
|
|
ASSERT(offset == 0 || offset == 2);
|
|
|
|
}
|
2020-02-19 04:54:37 +01:00
|
|
|
value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed,
|
|
|
|
std::move(value), Immediate(offset * 8));
|
2019-12-18 04:05:08 +01:00
|
|
|
}
|
2020-02-19 05:02:59 +01:00
|
|
|
|
2018-12-21 05:53:05 +01:00
|
|
|
value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed);
|
|
|
|
value = GetOperandAbsNegInteger(value, instr.conversion.abs_a, false, input_signed);
|
|
|
|
value = SignedOperation(OperationCode::FCastInteger, input_signed, PRECISE, value);
|
|
|
|
value = GetOperandAbsNegFloat(value, false, instr.conversion.negate_a);
|
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
|
2019-07-20 23:38:25 +02:00
|
|
|
|
|
|
|
if (instr.conversion.dst_size == Register::Size::Short) {
|
|
|
|
value = Operation(OperationCode::HCastFloat, PRECISE, value);
|
|
|
|
}
|
|
|
|
|
2018-12-21 05:53:05 +01:00
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-18 01:42:59 +01:00
|
|
|
case OpCode::Id::F2F_R:
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2F_C:
|
|
|
|
case OpCode::Id::F2F_IMM: {
|
2019-07-20 23:38:25 +02:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.dst_size == Register::Size::Long);
|
|
|
|
UNIMPLEMENTED_IF(instr.conversion.src_size == Register::Size::Long);
|
2018-12-15 21:32:51 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in F2F is not implemented");
|
|
|
|
|
2018-12-18 01:42:59 +01:00
|
|
|
Node value = [&]() {
|
2019-04-16 01:04:33 +02:00
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::F2F_R:
|
2018-12-18 01:42:59 +01:00
|
|
|
return GetRegister(instr.gpr20);
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2F_C:
|
2019-01-28 22:11:23 +01:00
|
|
|
return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2F_IMM:
|
|
|
|
return GetImmediate19(instr);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Immediate(0);
|
2018-12-18 01:42:59 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2019-07-20 23:38:25 +02:00
|
|
|
if (instr.conversion.src_size == Register::Size::Short) {
|
2019-08-28 21:09:33 +02:00
|
|
|
value = Operation(GetFloatSelector(instr.conversion.float_src.selector), NO_PRECISE,
|
|
|
|
std::move(value));
|
|
|
|
} else {
|
|
|
|
ASSERT(instr.conversion.float_src.selector == 0);
|
2019-07-20 23:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-12-15 21:32:51 +01:00
|
|
|
value = GetOperandAbsNegFloat(value, instr.conversion.abs_a, instr.conversion.negate_a);
|
|
|
|
|
2020-03-26 05:58:49 +01:00
|
|
|
value = [&] {
|
|
|
|
if (instr.conversion.src_size != instr.conversion.dst_size) {
|
|
|
|
// Rounding operations only matter when the source and destination conversion size
|
|
|
|
// is the same.
|
|
|
|
return value;
|
|
|
|
}
|
2019-04-16 01:04:33 +02:00
|
|
|
switch (instr.conversion.f2f.GetRoundingMode()) {
|
2018-12-15 21:32:51 +01:00
|
|
|
case Tegra::Shader::F2fRoundingOp::None:
|
|
|
|
return value;
|
|
|
|
case Tegra::Shader::F2fRoundingOp::Round:
|
2020-03-26 05:58:49 +01:00
|
|
|
return Operation(OperationCode::FRoundEven, value);
|
2018-12-15 21:32:51 +01:00
|
|
|
case Tegra::Shader::F2fRoundingOp::Floor:
|
2020-03-26 05:58:49 +01:00
|
|
|
return Operation(OperationCode::FFloor, value);
|
2018-12-15 21:32:51 +01:00
|
|
|
case Tegra::Shader::F2fRoundingOp::Ceil:
|
2020-03-26 05:58:49 +01:00
|
|
|
return Operation(OperationCode::FCeil, value);
|
2018-12-15 21:32:51 +01:00
|
|
|
case Tegra::Shader::F2fRoundingOp::Trunc:
|
2020-03-26 05:58:49 +01:00
|
|
|
return Operation(OperationCode::FTrunc, value);
|
2019-04-03 09:33:36 +02:00
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unimplemented F2F rounding mode {}",
|
|
|
|
static_cast<u32>(instr.conversion.f2f.rounding.Value()));
|
2019-07-20 23:38:25 +02:00
|
|
|
return value;
|
2018-12-15 21:32:51 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
value = GetSaturatedFloat(value, instr.alu.saturate_d);
|
|
|
|
|
2018-12-27 20:50:36 +01:00
|
|
|
SetInternalFlagsFromFloat(bb, value, instr.generates_cc);
|
2019-07-20 23:38:25 +02:00
|
|
|
|
|
|
|
if (instr.conversion.dst_size == Register::Size::Short) {
|
|
|
|
value = Operation(OperationCode::HCastFloat, PRECISE, value);
|
|
|
|
}
|
|
|
|
|
2018-12-15 21:32:51 +01:00
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-21 05:57:13 +01:00
|
|
|
case OpCode::Id::F2I_R:
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2I_C:
|
|
|
|
case OpCode::Id::F2I_IMM: {
|
2019-07-20 23:38:25 +02:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.src_size == Register::Size::Long);
|
2018-12-21 05:57:13 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in F2I is not implemented");
|
|
|
|
Node value = [&]() {
|
2019-04-16 01:04:33 +02:00
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::F2I_R:
|
2018-12-21 05:57:13 +01:00
|
|
|
return GetRegister(instr.gpr20);
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2I_C:
|
2019-01-28 22:11:23 +01:00
|
|
|
return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
|
2019-04-16 01:04:33 +02:00
|
|
|
case OpCode::Id::F2I_IMM:
|
|
|
|
return GetImmediate19(instr);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Immediate(0);
|
2018-12-21 05:57:13 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2019-07-20 23:38:25 +02:00
|
|
|
if (instr.conversion.src_size == Register::Size::Short) {
|
2019-08-28 21:09:33 +02:00
|
|
|
value = Operation(GetFloatSelector(instr.conversion.float_src.selector), NO_PRECISE,
|
|
|
|
std::move(value));
|
|
|
|
} else {
|
|
|
|
ASSERT(instr.conversion.float_src.selector == 0);
|
2019-07-20 23:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 05:57:13 +01:00
|
|
|
value = GetOperandAbsNegFloat(value, instr.conversion.abs_a, instr.conversion.negate_a);
|
|
|
|
|
|
|
|
value = [&]() {
|
|
|
|
switch (instr.conversion.f2i.rounding) {
|
2019-02-11 23:46:45 +01:00
|
|
|
case Tegra::Shader::F2iRoundingOp::RoundEven:
|
|
|
|
return Operation(OperationCode::FRoundEven, PRECISE, value);
|
2018-12-21 05:57:13 +01:00
|
|
|
case Tegra::Shader::F2iRoundingOp::Floor:
|
|
|
|
return Operation(OperationCode::FFloor, PRECISE, value);
|
|
|
|
case Tegra::Shader::F2iRoundingOp::Ceil:
|
|
|
|
return Operation(OperationCode::FCeil, PRECISE, value);
|
|
|
|
case Tegra::Shader::F2iRoundingOp::Trunc:
|
|
|
|
return Operation(OperationCode::FTrunc, PRECISE, value);
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unimplemented F2I rounding mode {}",
|
|
|
|
static_cast<u32>(instr.conversion.f2i.rounding.Value()));
|
2018-12-21 22:47:22 +01:00
|
|
|
return Immediate(0);
|
2018-12-21 05:57:13 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
const bool is_signed = instr.conversion.is_output_signed;
|
|
|
|
value = SignedOperation(OperationCode::ICastFloat, is_signed, PRECISE, value);
|
2019-04-16 01:04:33 +02:00
|
|
|
value = ConvertIntegerSize(value, instr.conversion.dst_size, is_signed);
|
2018-12-21 05:57:13 +01:00
|
|
|
|
|
|
|
SetRegister(bb, instr.gpr0, value);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-15 21:32:51 +01:00
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled conversion instruction: {}", opcode->get().GetName());
|
|
|
|
}
|
2018-12-20 23:09:21 +01:00
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:46:45 +01:00
|
|
|
} // namespace VideoCommon::Shader
|