2014-07-26 22:47:09 +05:30
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 11:08:14 +05:30
|
|
|
// Licensed under GPLv2 or any later version
|
2014-07-26 22:47:09 +05:30
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-04-30 21:04:51 +05:30
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cmath>
|
2016-03-17 10:25:55 +05:30
|
|
|
#include <numeric>
|
2016-04-30 21:04:51 +05:30
|
|
|
|
2014-12-16 05:02:49 +05:30
|
|
|
#include <nihstro/shader_bytecode.h>
|
|
|
|
|
2016-04-30 21:04:51 +05:30
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/vector_math.h"
|
|
|
|
|
2016-03-03 08:46:38 +05:30
|
|
|
#include "video_core/pica_state.h"
|
2016-04-30 21:04:51 +05:30
|
|
|
#include "video_core/pica_types.h"
|
2015-09-11 16:50:02 +05:30
|
|
|
#include "video_core/shader/shader.h"
|
|
|
|
#include "video_core/shader/shader_interpreter.h"
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
using nihstro::OpCode;
|
2014-12-16 05:02:49 +05:30
|
|
|
using nihstro::Instruction;
|
|
|
|
using nihstro::RegisterType;
|
|
|
|
using nihstro::SourceRegister;
|
|
|
|
using nihstro::SwizzlePattern;
|
|
|
|
|
2014-07-26 22:47:09 +05:30
|
|
|
namespace Pica {
|
|
|
|
|
2015-07-22 04:34:05 +05:30
|
|
|
namespace Shader {
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2016-05-12 13:55:38 +05:30
|
|
|
constexpr u32 INVALID_ADDRESS = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
struct CallStackElement {
|
|
|
|
u32 final_address; // Address upon which we jump to return_address
|
|
|
|
u32 return_address; // Where to jump when leaving scope
|
|
|
|
u8 repeat_counter; // How often to repeat until this call stack element is removed
|
|
|
|
u8 loop_increment; // Which value to add to the loop counter after an iteration
|
|
|
|
// TODO: Should this be a signed value? Does it even matter?
|
|
|
|
u32 loop_address; // The address where we'll return to after each loop iteration
|
|
|
|
};
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
template<bool Debug>
|
2016-05-13 13:08:40 +05:30
|
|
|
void RunInterpreter(const ShaderSetup& setup, UnitState<Debug>& state, unsigned offset) {
|
2016-05-12 13:55:38 +05:30
|
|
|
// TODO: Is there a maximal size for this?
|
|
|
|
boost::container::static_vector<CallStackElement, 16> call_stack;
|
|
|
|
|
2016-05-13 13:08:40 +05:30
|
|
|
u32 program_counter = offset;
|
2016-05-12 13:55:38 +05:30
|
|
|
|
2015-05-14 08:59:27 +05:30
|
|
|
const auto& uniforms = g_state.vs.uniforms;
|
|
|
|
const auto& swizzle_data = g_state.vs.swizzle_data;
|
|
|
|
const auto& program_code = g_state.vs.program_code;
|
2014-12-20 00:28:21 +05:30
|
|
|
|
|
|
|
// Placeholder for invalid inputs
|
|
|
|
static float24 dummy_vec4_float24[4];
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
unsigned iteration = 0;
|
|
|
|
bool exit_loop = false;
|
|
|
|
while (!exit_loop) {
|
2016-05-12 13:55:38 +05:30
|
|
|
if (!call_stack.empty()) {
|
|
|
|
auto& top = call_stack.back();
|
|
|
|
if (program_counter == top.final_address) {
|
2014-12-21 07:31:35 +05:30
|
|
|
state.address_registers[2] += top.loop_increment;
|
|
|
|
|
|
|
|
if (top.repeat_counter-- == 0) {
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter = top.return_address;
|
|
|
|
call_stack.pop_back();
|
2015-02-21 23:22:21 +05:30
|
|
|
} else {
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter = top.loop_address;
|
2014-12-21 07:31:35 +05:30
|
|
|
}
|
2014-12-14 02:00:13 +05:30
|
|
|
|
|
|
|
// TODO: Is "trying again" accurate to hardware?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 13:55:38 +05:30
|
|
|
const Instruction instr = { program_code[program_counter] };
|
2015-07-26 16:10:34 +05:30
|
|
|
const SwizzlePattern swizzle = { swizzle_data[instr.common.operand_desc_id] };
|
2014-12-12 23:01:37 +05:30
|
|
|
|
2016-05-12 13:55:38 +05:30
|
|
|
static auto call = [&program_counter, &call_stack](UnitState<Debug>& state, u32 offset, u32 num_instructions,
|
2014-12-21 07:31:35 +05:30
|
|
|
u32 return_offset, u8 repeat_count, u8 loop_increment) {
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter = offset - 1; // -1 to make sure when incrementing the PC we end up at the correct offset
|
|
|
|
ASSERT(call_stack.size() < call_stack.capacity());
|
|
|
|
call_stack.push_back({ offset + num_instructions, return_offset, repeat_count, loop_increment, offset });
|
2014-12-14 02:00:13 +05:30
|
|
|
};
|
2016-05-12 13:55:38 +05:30
|
|
|
Record<DebugDataRecord::CUR_INSTR>(state.debug, iteration, program_counter);
|
2015-07-12 05:27:59 +05:30
|
|
|
if (iteration > 0)
|
2016-05-12 13:55:38 +05:30
|
|
|
Record<DebugDataRecord::NEXT_INSTR>(state.debug, iteration - 1, program_counter);
|
2015-07-12 05:27:59 +05:30
|
|
|
|
2016-05-12 13:55:38 +05:30
|
|
|
state.debug.max_offset = std::max<u32>(state.debug.max_offset, 1 + program_counter);
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2014-12-16 05:02:49 +05:30
|
|
|
auto LookupSourceRegister = [&](const SourceRegister& source_reg) -> const float24* {
|
|
|
|
switch (source_reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Input:
|
2015-08-16 02:21:32 +05:30
|
|
|
return &state.registers.input[source_reg.GetIndex()].x;
|
2014-12-16 05:02:49 +05:30
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
2015-08-16 02:21:32 +05:30
|
|
|
return &state.registers.temporary[source_reg.GetIndex()].x;
|
2014-12-16 05:02:49 +05:30
|
|
|
|
|
|
|
case RegisterType::FloatUniform:
|
2015-05-14 08:59:27 +05:30
|
|
|
return &uniforms.f[source_reg.GetIndex()].x;
|
2014-12-20 00:28:21 +05:30
|
|
|
|
|
|
|
default:
|
|
|
|
return dummy_vec4_float24;
|
2014-12-16 05:02:49 +05:30
|
|
|
}
|
|
|
|
};
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
switch (instr.opcode.Value().GetInfo().type) {
|
|
|
|
case OpCode::Type::Arithmetic:
|
2014-12-12 23:01:37 +05:30
|
|
|
{
|
2015-05-24 19:57:31 +05:30
|
|
|
const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
|
2014-12-13 03:20:09 +05:30
|
|
|
|
|
|
|
const int address_offset = (instr.common.address_register_index == 0)
|
|
|
|
? 0 : state.address_registers[instr.common.address_register_index - 1];
|
|
|
|
|
2015-05-24 19:57:31 +05:30
|
|
|
const float24* src1_ = LookupSourceRegister(instr.common.GetSrc1(is_inverted) + (!is_inverted * address_offset));
|
|
|
|
const float24* src2_ = LookupSourceRegister(instr.common.GetSrc2(is_inverted) + ( is_inverted * address_offset));
|
2014-12-12 23:01:37 +05:30
|
|
|
|
2014-12-21 23:04:20 +05:30
|
|
|
const bool negate_src1 = ((bool)swizzle.negate_src1 != false);
|
|
|
|
const bool negate_src2 = ((bool)swizzle.negate_src2 != false);
|
2014-12-12 23:01:37 +05:30
|
|
|
|
|
|
|
float24 src1[4] = {
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(0)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(1)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(2)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(3)],
|
|
|
|
};
|
|
|
|
if (negate_src1) {
|
|
|
|
src1[0] = src1[0] * float24::FromFloat32(-1);
|
|
|
|
src1[1] = src1[1] * float24::FromFloat32(-1);
|
|
|
|
src1[2] = src1[2] * float24::FromFloat32(-1);
|
|
|
|
src1[3] = src1[3] * float24::FromFloat32(-1);
|
|
|
|
}
|
|
|
|
float24 src2[4] = {
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(0)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(1)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(2)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(3)],
|
|
|
|
};
|
|
|
|
if (negate_src2) {
|
|
|
|
src2[0] = src2[0] * float24::FromFloat32(-1);
|
|
|
|
src2[1] = src2[1] * float24::FromFloat32(-1);
|
|
|
|
src2[2] = src2[2] * float24::FromFloat32(-1);
|
|
|
|
src2[3] = src2[3] * float24::FromFloat32(-1);
|
|
|
|
}
|
|
|
|
|
2015-08-16 02:21:32 +05:30
|
|
|
float24* dest = (instr.common.dest.Value() < 0x10) ? &state.registers.output[instr.common.dest.Value().GetIndex()][0]
|
|
|
|
: (instr.common.dest.Value() < 0x20) ? &state.registers.temporary[instr.common.dest.Value().GetIndex()][0]
|
2014-12-20 00:28:21 +05:30
|
|
|
: dummy_vec4_float24;
|
2014-12-12 23:01:37 +05:30
|
|
|
|
|
|
|
state.debug.max_opdesc_id = std::max<u32>(state.debug.max_opdesc_id, 1+instr.common.operand_desc_id);
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
switch (instr.opcode.Value().EffectiveOpCode()) {
|
|
|
|
case OpCode::Id::ADD:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] + src2[i];
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::MUL:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] * src2[i];
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-07 04:07:12 +05:30
|
|
|
case OpCode::Id::FLR:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-05-07 04:07:12 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = float24::FromFloat32(std::floor(src1[i].ToFloat32()));
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2015-05-07 04:07:12 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::MAX:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2014-12-14 01:52:55 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
2015-08-24 10:16:58 +05:30
|
|
|
// NOTE: Exact form required to match NaN semantics to hardware:
|
|
|
|
// max(0, NaN) -> NaN
|
|
|
|
// max(NaN, 0) -> 0
|
|
|
|
dest[i] = (src1[i] > src2[i]) ? src1[i] : src2[i];
|
2014-12-14 01:52:55 +05:30
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-12-14 01:52:55 +05:30
|
|
|
break;
|
|
|
|
|
2015-05-23 08:43:09 +05:30
|
|
|
case OpCode::Id::MIN:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-05-23 08:43:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
2015-08-24 10:16:58 +05:30
|
|
|
// NOTE: Exact form required to match NaN semantics to hardware:
|
|
|
|
// min(0, NaN) -> NaN
|
|
|
|
// min(NaN, 0) -> 0
|
|
|
|
dest[i] = (src1[i] < src2[i]) ? src1[i] : src2[i];
|
2015-05-23 08:43:09 +05:30
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2015-05-23 08:43:09 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::DP3:
|
|
|
|
case OpCode::Id::DP4:
|
2015-08-21 14:21:41 +05:30
|
|
|
case OpCode::Id::DPH:
|
|
|
|
case OpCode::Id::DPHI:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-08-21 14:21:41 +05:30
|
|
|
|
|
|
|
OpCode::Id opcode = instr.opcode.Value().EffectiveOpCode();
|
|
|
|
if (opcode == OpCode::Id::DPH || opcode == OpCode::Id::DPHI)
|
|
|
|
src1[3] = float24::FromFloat32(1.0f);
|
|
|
|
|
|
|
|
int num_components = (opcode == OpCode::Id::DP3) ? 3 : 4;
|
2016-03-17 10:25:55 +05:30
|
|
|
float24 dot = std::inner_product(src1, src1 + num_components, src2, float24::FromFloat32(0.f));
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2015-07-20 03:31:59 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
2014-07-26 22:47:09 +05:30
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = dot;
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reciprocal
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::RCP:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-08-23 18:43:36 +05:30
|
|
|
float24 rcp_res = float24::FromFloat32(1.0f / src1[0].ToFloat32());
|
2014-07-26 22:47:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
2015-08-23 18:43:36 +05:30
|
|
|
dest[i] = rcp_res;
|
2014-07-26 22:47:09 +05:30
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reciprocal Square Root
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::RSQ:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-08-24 01:33:07 +05:30
|
|
|
float24 rsq_res = float24::FromFloat32(1.0f / std::sqrt(src1[0].ToFloat32()));
|
2014-07-26 22:47:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
2015-08-23 18:43:36 +05:30
|
|
|
dest[i] = rsq_res;
|
2014-07-26 22:47:09 +05:30
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::MOVA:
|
2014-12-13 03:20:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
2014-12-13 03:20:09 +05:30
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: Figure out how the rounding is done on hardware
|
|
|
|
state.address_registers[i] = static_cast<s32>(src1[i].ToFloat32());
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::ADDR_REG_OUT>(state.debug, iteration, state.address_registers);
|
2014-12-13 03:20:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::MOV:
|
2014-07-26 22:47:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i];
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-19 17:27:01 +05:30
|
|
|
case OpCode::Id::SGE:
|
|
|
|
case OpCode::Id::SGEI:
|
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = (src1[i] >= src2[i]) ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
|
|
|
|
}
|
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
|
|
|
break;
|
|
|
|
|
2015-05-23 09:10:43 +05:30
|
|
|
case OpCode::Id::SLT:
|
|
|
|
case OpCode::Id::SLTI:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-05-23 09:10:43 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = (src1[i] < src2[i]) ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2015-05-23 09:10:43 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::CMP:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
2014-12-13 03:20:09 +05:30
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
// TODO: Can you restrict to one compare via dest masking?
|
|
|
|
|
|
|
|
auto compare_op = instr.common.compare_op;
|
|
|
|
auto op = (i == 0) ? compare_op.x.Value() : compare_op.y.Value();
|
|
|
|
|
|
|
|
switch (op) {
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::Equal:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] == src2[i]);
|
|
|
|
break;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::NotEqual:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] != src2[i]);
|
|
|
|
break;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::LessThan:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] < src2[i]);
|
|
|
|
break;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::LessEqual:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] <= src2[i]);
|
|
|
|
break;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::GreaterThan:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] > src2[i]);
|
|
|
|
break;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
case Instruction::Common::CompareOpType::GreaterEqual:
|
2014-12-13 03:20:09 +05:30
|
|
|
state.conditional_code[i] = (src1[i] >= src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG_ERROR(HW_GPU, "Unknown compare mode %x", static_cast<int>(op));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::CMP_RESULT>(state.debug, iteration, state.conditional_code);
|
2014-12-13 03:20:09 +05:30
|
|
|
break;
|
|
|
|
|
2015-08-16 15:21:21 +05:30
|
|
|
case OpCode::Id::EX2:
|
|
|
|
{
|
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
|
|
|
|
|
|
|
// EX2 only takes first component exp2 and writes it to all dest components
|
|
|
|
float24 ex2_res = float24::FromFloat32(std::exp2(src1[0].ToFloat32()));
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = ex2_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OpCode::Id::LG2:
|
|
|
|
{
|
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
|
|
|
|
|
|
|
// LG2 only takes the first component log2 and writes it to all dest components
|
|
|
|
float24 lg2_res = float24::FromFloat32(std::log2(src1[0].ToFloat32()));
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = lg2_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:01:37 +05:30
|
|
|
default:
|
|
|
|
LOG_ERROR(HW_GPU, "Unhandled arithmetic instruction: 0x%02x (%s): 0x%08x",
|
2015-03-09 02:22:38 +05:30
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2015-01-21 06:46:47 +05:30
|
|
|
DEBUG_ASSERT(false);
|
2014-12-12 23:01:37 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2015-01-03 17:59:44 +05:30
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Type::MultiplyAdd:
|
2015-01-03 17:59:44 +05:30
|
|
|
{
|
2015-05-26 00:04:09 +05:30
|
|
|
if ((instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD) ||
|
2015-05-06 07:36:46 +05:30
|
|
|
(instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI)) {
|
2016-03-17 11:21:09 +05:30
|
|
|
const SwizzlePattern& swizzle = *reinterpret_cast<const SwizzlePattern*>(&swizzle_data[instr.mad.operand_desc_id]);
|
2015-01-03 17:59:44 +05:30
|
|
|
|
2015-05-06 07:36:46 +05:30
|
|
|
bool is_inverted = (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI);
|
|
|
|
|
2016-03-09 19:18:45 +05:30
|
|
|
const int address_offset = (instr.mad.address_register_index == 0)
|
|
|
|
? 0 : state.address_registers[instr.mad.address_register_index - 1];
|
|
|
|
|
2015-05-06 07:36:46 +05:30
|
|
|
const float24* src1_ = LookupSourceRegister(instr.mad.GetSrc1(is_inverted));
|
2016-03-09 19:18:45 +05:30
|
|
|
const float24* src2_ = LookupSourceRegister(instr.mad.GetSrc2(is_inverted) + (!is_inverted * address_offset));
|
|
|
|
const float24* src3_ = LookupSourceRegister(instr.mad.GetSrc3(is_inverted) + ( is_inverted * address_offset));
|
2015-01-03 17:59:44 +05:30
|
|
|
|
|
|
|
const bool negate_src1 = ((bool)swizzle.negate_src1 != false);
|
|
|
|
const bool negate_src2 = ((bool)swizzle.negate_src2 != false);
|
|
|
|
const bool negate_src3 = ((bool)swizzle.negate_src3 != false);
|
|
|
|
|
|
|
|
float24 src1[4] = {
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(0)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(1)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(2)],
|
|
|
|
src1_[(int)swizzle.GetSelectorSrc1(3)],
|
|
|
|
};
|
|
|
|
if (negate_src1) {
|
|
|
|
src1[0] = src1[0] * float24::FromFloat32(-1);
|
|
|
|
src1[1] = src1[1] * float24::FromFloat32(-1);
|
|
|
|
src1[2] = src1[2] * float24::FromFloat32(-1);
|
|
|
|
src1[3] = src1[3] * float24::FromFloat32(-1);
|
|
|
|
}
|
|
|
|
float24 src2[4] = {
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(0)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(1)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(2)],
|
|
|
|
src2_[(int)swizzle.GetSelectorSrc2(3)],
|
|
|
|
};
|
|
|
|
if (negate_src2) {
|
|
|
|
src2[0] = src2[0] * float24::FromFloat32(-1);
|
|
|
|
src2[1] = src2[1] * float24::FromFloat32(-1);
|
|
|
|
src2[2] = src2[2] * float24::FromFloat32(-1);
|
|
|
|
src2[3] = src2[3] * float24::FromFloat32(-1);
|
|
|
|
}
|
|
|
|
float24 src3[4] = {
|
|
|
|
src3_[(int)swizzle.GetSelectorSrc3(0)],
|
|
|
|
src3_[(int)swizzle.GetSelectorSrc3(1)],
|
|
|
|
src3_[(int)swizzle.GetSelectorSrc3(2)],
|
|
|
|
src3_[(int)swizzle.GetSelectorSrc3(3)],
|
|
|
|
};
|
|
|
|
if (negate_src3) {
|
|
|
|
src3[0] = src3[0] * float24::FromFloat32(-1);
|
|
|
|
src3[1] = src3[1] * float24::FromFloat32(-1);
|
|
|
|
src3[2] = src3[2] * float24::FromFloat32(-1);
|
|
|
|
src3[3] = src3[3] * float24::FromFloat32(-1);
|
|
|
|
}
|
|
|
|
|
2015-08-16 02:21:32 +05:30
|
|
|
float24* dest = (instr.mad.dest.Value() < 0x10) ? &state.registers.output[instr.mad.dest.Value().GetIndex()][0]
|
|
|
|
: (instr.mad.dest.Value() < 0x20) ? &state.registers.temporary[instr.mad.dest.Value().GetIndex()][0]
|
2015-01-03 17:59:44 +05:30
|
|
|
: dummy_vec4_float24;
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
|
|
|
|
Record<DebugDataRecord::SRC2>(state.debug, iteration, src2);
|
|
|
|
Record<DebugDataRecord::SRC3>(state.debug, iteration, src3);
|
|
|
|
Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
|
2015-01-03 17:59:44 +05:30
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] * src2[i] + src3[i];
|
|
|
|
}
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
|
2015-01-03 17:59:44 +05:30
|
|
|
} else {
|
|
|
|
LOG_ERROR(HW_GPU, "Unhandled multiply-add instruction: 0x%02x (%s): 0x%08x",
|
2015-03-09 02:22:38 +05:30
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2015-01-03 17:59:44 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:01:37 +05:30
|
|
|
default:
|
2015-01-03 02:10:09 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
static auto evaluate_condition = [](const UnitState<Debug>& state, bool refx, bool refy, Instruction::FlowControlType flow_control) {
|
2015-01-03 02:10:09 +05:30
|
|
|
bool results[2] = { refx == state.conditional_code[0],
|
|
|
|
refy == state.conditional_code[1] };
|
|
|
|
|
|
|
|
switch (flow_control.op) {
|
|
|
|
case flow_control.Or:
|
|
|
|
return results[0] || results[1];
|
|
|
|
|
|
|
|
case flow_control.And:
|
|
|
|
return results[0] && results[1];
|
|
|
|
|
|
|
|
case flow_control.JustX:
|
|
|
|
return results[0];
|
|
|
|
|
|
|
|
case flow_control.JustY:
|
|
|
|
return results[1];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-13 03:20:09 +05:30
|
|
|
// Handle each instruction on its own
|
2015-03-09 02:22:38 +05:30
|
|
|
switch (instr.opcode.Value()) {
|
|
|
|
case OpCode::Id::END:
|
2014-12-14 02:00:13 +05:30
|
|
|
exit_loop = true;
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::JMPC:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_CMP_IN>(state.debug, iteration, state.conditional_code);
|
2015-01-03 02:10:09 +05:30
|
|
|
if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter = instr.flow_control.dest_offset - 1;
|
2015-01-03 02:10:09 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::JMPU:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_BOOL_IN>(state.debug, iteration, uniforms.b[instr.flow_control.bool_uniform_id]);
|
2016-01-25 09:50:39 +05:30
|
|
|
|
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id] == !(instr.flow_control.num_instructions & 1)) {
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter = instr.flow_control.dest_offset - 1;
|
2015-01-03 02:10:09 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::CALL:
|
2014-12-20 19:49:36 +05:30
|
|
|
call(state,
|
2014-12-14 02:00:13 +05:30
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1, 0, 0);
|
2014-12-14 02:00:13 +05:30
|
|
|
break;
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::CALLU:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_BOOL_IN>(state.debug, iteration, uniforms.b[instr.flow_control.bool_uniform_id]);
|
2015-05-14 08:59:27 +05:30
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id]) {
|
2015-01-03 02:10:09 +05:30
|
|
|
call(state,
|
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1, 0, 0);
|
2015-01-03 02:10:09 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::CALLC:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_CMP_IN>(state.debug, iteration, state.conditional_code);
|
2015-01-03 02:10:09 +05:30
|
|
|
if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
|
|
|
|
call(state,
|
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1, 0, 0);
|
2015-01-03 02:10:09 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::NOP:
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::IFU:
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_BOOL_IN>(state.debug, iteration, uniforms.b[instr.flow_control.bool_uniform_id]);
|
2015-05-14 08:59:27 +05:30
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id]) {
|
2014-12-20 19:49:36 +05:30
|
|
|
call(state,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - program_counter - 1,
|
2014-12-21 07:31:35 +05:30
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-14 02:00:13 +05:30
|
|
|
} else {
|
2014-12-20 19:49:36 +05:30
|
|
|
call(state,
|
2014-12-14 02:00:13 +05:30
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2014-12-21 07:31:35 +05:30
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-14 02:00:13 +05:30
|
|
|
}
|
|
|
|
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::IFC:
|
2014-12-13 03:20:09 +05:30
|
|
|
{
|
|
|
|
// TODO: Do we need to consider swizzlers here?
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::COND_CMP_IN>(state.debug, iteration, state.conditional_code);
|
2015-01-03 02:10:09 +05:30
|
|
|
if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
|
2014-12-20 19:49:36 +05:30
|
|
|
call(state,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - program_counter - 1,
|
2014-12-21 07:31:35 +05:30
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-13 03:20:09 +05:30
|
|
|
} else {
|
2014-12-20 19:49:36 +05:30
|
|
|
call(state,
|
2014-12-14 02:00:13 +05:30
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2014-12-21 07:31:35 +05:30
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-13 03:20:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-09 02:22:38 +05:30
|
|
|
case OpCode::Id::LOOP:
|
2014-12-21 07:31:35 +05:30
|
|
|
{
|
2015-07-12 05:27:59 +05:30
|
|
|
Math::Vec4<u8> loop_param(uniforms.i[instr.flow_control.int_uniform_id].x,
|
|
|
|
uniforms.i[instr.flow_control.int_uniform_id].y,
|
|
|
|
uniforms.i[instr.flow_control.int_uniform_id].z,
|
|
|
|
uniforms.i[instr.flow_control.int_uniform_id].w);
|
|
|
|
state.address_registers[2] = loop_param.y;
|
2014-12-21 07:31:35 +05:30
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
Record<DebugDataRecord::LOOP_INT_IN>(state.debug, iteration, loop_param);
|
2014-12-21 07:31:35 +05:30
|
|
|
call(state,
|
2016-05-12 13:55:38 +05:30
|
|
|
program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - program_counter + 1,
|
2014-12-21 07:31:35 +05:30
|
|
|
instr.flow_control.dest_offset + 1,
|
2015-07-12 05:27:59 +05:30
|
|
|
loop_param.x,
|
|
|
|
loop_param.z);
|
2014-12-21 07:31:35 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:47:09 +05:30
|
|
|
default:
|
2014-12-06 07:23:49 +05:30
|
|
|
LOG_ERROR(HW_GPU, "Unhandled instruction: 0x%02x (%s): 0x%08x",
|
2015-03-09 02:22:38 +05:30
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2014-07-26 22:47:09 +05:30
|
|
|
break;
|
2014-12-12 23:01:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-07-26 22:47:09 +05:30
|
|
|
}
|
2015-01-03 02:10:09 +05:30
|
|
|
}
|
2014-07-26 22:47:09 +05:30
|
|
|
|
2016-05-12 13:55:38 +05:30
|
|
|
++program_counter;
|
2015-07-12 05:27:59 +05:30
|
|
|
++iteration;
|
2014-07-26 22:47:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 05:27:59 +05:30
|
|
|
// Explicit instantiation
|
2016-05-13 13:08:40 +05:30
|
|
|
template void RunInterpreter(const ShaderSetup& setup, UnitState<false>& state, unsigned offset);
|
|
|
|
template void RunInterpreter(const ShaderSetup& setup, UnitState<true>& state, unsigned offset);
|
2015-07-12 05:27:59 +05:30
|
|
|
|
2014-07-26 22:47:09 +05:30
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace
|