2014-07-26 19:17:09 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-07-26 19:17:09 +02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-07-26 12:27:36 +02:00
|
|
|
#include <boost/container/static_vector.hpp>
|
2014-12-07 22:22:04 +01:00
|
|
|
#include <boost/range/algorithm.hpp>
|
|
|
|
|
|
|
|
#include <common/file_util.h>
|
|
|
|
|
2014-12-16 00:32:49 +01:00
|
|
|
#include <nihstro/shader_bytecode.h>
|
|
|
|
|
2015-05-12 20:50:17 +02:00
|
|
|
#include "common/profiler.h"
|
2014-12-07 22:22:04 +01:00
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
#include "pica.h"
|
|
|
|
#include "vertex_shader.h"
|
2014-12-12 17:55:43 +01:00
|
|
|
#include "debug_utils/debug_utils.h"
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
using nihstro::OpCode;
|
2014-12-16 00:32:49 +01:00
|
|
|
using nihstro::Instruction;
|
|
|
|
using nihstro::RegisterType;
|
|
|
|
using nihstro::SourceRegister;
|
|
|
|
using nihstro::SwizzlePattern;
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace VertexShader {
|
|
|
|
|
|
|
|
struct VertexShaderState {
|
2015-07-26 12:40:34 +02:00
|
|
|
u32 program_counter;
|
2014-07-26 19:17:09 +02:00
|
|
|
|
|
|
|
const float24* input_register_table[16];
|
2015-03-12 14:18:46 +01:00
|
|
|
Math::Vec4<float24> output_registers[16];
|
2014-07-26 19:17:09 +02:00
|
|
|
|
|
|
|
Math::Vec4<float24> temporary_registers[16];
|
2014-12-12 22:50:09 +01:00
|
|
|
bool conditional_code[2];
|
|
|
|
|
|
|
|
// Two Address registers and one loop counter
|
|
|
|
// TODO: How many bits do these actually have?
|
|
|
|
s32 address_registers[3];
|
2014-07-26 19:17:09 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
INVALID_ADDRESS = 0xFFFFFFFF
|
|
|
|
};
|
2014-08-14 14:30:38 +02:00
|
|
|
|
2014-12-13 21:30:13 +01:00
|
|
|
struct CallStackElement {
|
2014-12-21 03:01:35 +01:00
|
|
|
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?
|
2015-02-21 18:52:21 +01:00
|
|
|
u32 loop_address; // The address where we'll return to after each loop iteration
|
2014-12-13 21:30:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Is there a maximal size for this?
|
2015-07-26 12:27:36 +02:00
|
|
|
boost::container::static_vector<CallStackElement, 16> call_stack;
|
2014-12-12 22:50:09 +01:00
|
|
|
|
2014-08-14 14:30:38 +02:00
|
|
|
struct {
|
|
|
|
u32 max_offset; // maximum program counter ever reached
|
|
|
|
u32 max_opdesc_id; // maximum swizzle pattern index ever used
|
|
|
|
} debug;
|
2014-07-26 19:17:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ProcessShaderCode(VertexShaderState& state) {
|
2015-05-14 05:29:27 +02:00
|
|
|
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-19 19:58:21 +01:00
|
|
|
|
|
|
|
// Placeholder for invalid inputs
|
|
|
|
static float24 dummy_vec4_float24[4];
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
while (true) {
|
2014-12-13 21:30:13 +01:00
|
|
|
if (!state.call_stack.empty()) {
|
2015-07-26 12:27:36 +02:00
|
|
|
auto& top = state.call_stack.back();
|
2015-07-26 12:40:34 +02:00
|
|
|
if (state.program_counter == top.final_address) {
|
2014-12-21 03:01:35 +01:00
|
|
|
state.address_registers[2] += top.loop_increment;
|
|
|
|
|
|
|
|
if (top.repeat_counter-- == 0) {
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = top.return_address;
|
2015-07-26 12:27:36 +02:00
|
|
|
state.call_stack.pop_back();
|
2015-02-21 18:52:21 +01:00
|
|
|
} else {
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = top.loop_address;
|
2014-12-21 03:01:35 +01:00
|
|
|
}
|
2014-12-13 21:30:13 +01:00
|
|
|
|
|
|
|
// TODO: Is "trying again" accurate to hardware?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
bool exit_loop = false;
|
2015-07-26 12:40:34 +02:00
|
|
|
const Instruction instr = { program_code[state.program_counter] };
|
|
|
|
const SwizzlePattern swizzle = { swizzle_data[instr.common.operand_desc_id] };
|
2014-12-12 18:31:37 +01:00
|
|
|
|
2015-07-26 12:40:34 +02:00
|
|
|
static auto call = [](VertexShaderState& state, u32 offset, u32 num_instructions,
|
2014-12-21 03:01:35 +01:00
|
|
|
u32 return_offset, u8 repeat_count, u8 loop_increment) {
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = offset - 1; // -1 to make sure when incrementing the PC we end up at the correct offset
|
2015-07-26 12:27:36 +02:00
|
|
|
ASSERT(state.call_stack.size() < state.call_stack.capacity());
|
|
|
|
state.call_stack.push_back({ offset + num_instructions, return_offset, repeat_count, loop_increment, offset });
|
2014-12-13 21:30:13 +01:00
|
|
|
};
|
2015-07-26 12:40:34 +02:00
|
|
|
state.debug.max_offset = std::max<u32>(state.debug.max_offset, 1 + state.program_counter);
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2014-12-16 00:32:49 +01:00
|
|
|
auto LookupSourceRegister = [&](const SourceRegister& source_reg) -> const float24* {
|
|
|
|
switch (source_reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Input:
|
|
|
|
return state.input_register_table[source_reg.GetIndex()];
|
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
|
|
|
return &state.temporary_registers[source_reg.GetIndex()].x;
|
|
|
|
|
|
|
|
case RegisterType::FloatUniform:
|
2015-05-14 05:29:27 +02:00
|
|
|
return &uniforms.f[source_reg.GetIndex()].x;
|
2014-12-19 19:58:21 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return dummy_vec4_float24;
|
2014-12-16 00:32:49 +01:00
|
|
|
}
|
|
|
|
};
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
switch (instr.opcode.Value().GetInfo().type) {
|
|
|
|
case OpCode::Type::Arithmetic:
|
2014-12-12 18:31:37 +01:00
|
|
|
{
|
2015-05-24 16:27:31 +02:00
|
|
|
const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
|
2014-12-12 22:50:09 +01:00
|
|
|
|
|
|
|
const int address_offset = (instr.common.address_register_index == 0)
|
|
|
|
? 0 : state.address_registers[instr.common.address_register_index - 1];
|
|
|
|
|
2015-05-24 16:27:31 +02:00
|
|
|
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 18:31:37 +01:00
|
|
|
|
2014-12-21 18:34:20 +01:00
|
|
|
const bool negate_src1 = ((bool)swizzle.negate_src1 != false);
|
|
|
|
const bool negate_src2 = ((bool)swizzle.negate_src2 != false);
|
2014-12-12 18:31:37 +01:00
|
|
|
|
|
|
|
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-03-12 14:18:46 +01:00
|
|
|
float24* dest = (instr.common.dest.Value() < 0x10) ? &state.output_registers[instr.common.dest.Value().GetIndex()][0]
|
2015-03-08 21:52:38 +01:00
|
|
|
: (instr.common.dest.Value() < 0x20) ? &state.temporary_registers[instr.common.dest.Value().GetIndex()][0]
|
2014-12-19 19:58:21 +01:00
|
|
|
: dummy_vec4_float24;
|
2014-12-12 18:31:37 +01:00
|
|
|
|
|
|
|
state.debug.max_opdesc_id = std::max<u32>(state.debug.max_opdesc_id, 1+instr.common.operand_desc_id);
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
switch (instr.opcode.Value().EffectiveOpCode()) {
|
|
|
|
case OpCode::Id::ADD:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] + src2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::MUL:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] * src2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-07 00:37:12 +02:00
|
|
|
case OpCode::Id::FLR:
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = float24::FromFloat32(std::floor(src1[i].ToFloat32()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::MAX:
|
2014-12-13 21:22:55 +01:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = std::max(src1[i], src2[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-23 05:13:09 +02:00
|
|
|
case OpCode::Id::MIN:
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = std::min(src1[i], src2[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::DP3:
|
|
|
|
case OpCode::Id::DP4:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
float24 dot = float24::FromFloat32(0.f);
|
2015-03-08 21:52:38 +01:00
|
|
|
int num_components = (instr.opcode.Value() == OpCode::Id::DP3) ? 3 : 4;
|
2014-07-26 19:17:09 +02:00
|
|
|
for (int i = 0; i < num_components; ++i)
|
|
|
|
dot = dot + src1[i] * src2[i];
|
|
|
|
|
2015-07-20 00:01:59 +02:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2014-07-26 19:17:09 +02:00
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = dot;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reciprocal
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::RCP:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: Be stable against division by zero!
|
|
|
|
// TODO: I think this might be wrong... we should only use one component here
|
2015-02-01 21:31:21 +01:00
|
|
|
dest[i] = float24::FromFloat32(1.0f / src1[i].ToFloat32());
|
2014-07-26 19:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reciprocal Square Root
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::RSQ:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: Be stable against division by zero!
|
|
|
|
// TODO: I think this might be wrong... we should only use one component here
|
2015-02-01 21:31:21 +01:00
|
|
|
dest[i] = float24::FromFloat32(1.0f / sqrt(src1[i].ToFloat32()));
|
2014-07-26 19:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::MOVA:
|
2014-12-12 22:50:09 +01:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::MOV:
|
2014-07-26 19:17:09 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:40:43 +02:00
|
|
|
case OpCode::Id::SLT:
|
|
|
|
case OpCode::Id::SLTI:
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::CMP:
|
2014-12-12 22:50:09 +01:00
|
|
|
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) {
|
|
|
|
case compare_op.Equal:
|
|
|
|
state.conditional_code[i] = (src1[i] == src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case compare_op.NotEqual:
|
|
|
|
state.conditional_code[i] = (src1[i] != src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case compare_op.LessThan:
|
|
|
|
state.conditional_code[i] = (src1[i] < src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case compare_op.LessEqual:
|
|
|
|
state.conditional_code[i] = (src1[i] <= src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case compare_op.GreaterThan:
|
|
|
|
state.conditional_code[i] = (src1[i] > src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case compare_op.GreaterEqual:
|
|
|
|
state.conditional_code[i] = (src1[i] >= src2[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG_ERROR(HW_GPU, "Unknown compare mode %x", static_cast<int>(op));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-12-12 18:31:37 +01:00
|
|
|
default:
|
|
|
|
LOG_ERROR(HW_GPU, "Unhandled arithmetic instruction: 0x%02x (%s): 0x%08x",
|
2015-03-08 21:52:38 +01:00
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2015-01-21 02:16:47 +01:00
|
|
|
DEBUG_ASSERT(false);
|
2014-12-12 18:31:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2015-01-03 13:29:44 +01:00
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Type::MultiplyAdd:
|
2015-01-03 13:29:44 +01:00
|
|
|
{
|
2015-05-25 20:34:09 +02:00
|
|
|
if ((instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD) ||
|
2015-05-06 04:06:46 +02:00
|
|
|
(instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI)) {
|
2015-01-03 13:29:44 +01:00
|
|
|
const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.mad.operand_desc_id];
|
|
|
|
|
2015-05-06 04:06:46 +02:00
|
|
|
bool is_inverted = (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI);
|
|
|
|
|
|
|
|
const float24* src1_ = LookupSourceRegister(instr.mad.GetSrc1(is_inverted));
|
|
|
|
const float24* src2_ = LookupSourceRegister(instr.mad.GetSrc2(is_inverted));
|
|
|
|
const float24* src3_ = LookupSourceRegister(instr.mad.GetSrc3(is_inverted));
|
2015-01-03 13:29:44 +01:00
|
|
|
|
|
|
|
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-03-12 14:18:46 +01:00
|
|
|
float24* dest = (instr.mad.dest.Value() < 0x10) ? &state.output_registers[instr.mad.dest.Value().GetIndex()][0]
|
2015-03-08 21:52:38 +01:00
|
|
|
: (instr.mad.dest.Value() < 0x20) ? &state.temporary_registers[instr.mad.dest.Value().GetIndex()][0]
|
2015-01-03 13:29:44 +01:00
|
|
|
: dummy_vec4_float24;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!swizzle.DestComponentEnabled(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dest[i] = src1[i] * src2[i] + src3[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(HW_GPU, "Unhandled multiply-add instruction: 0x%02x (%s): 0x%08x",
|
2015-03-08 21:52:38 +01:00
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2015-01-03 13:29:44 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-12 18:31:37 +01:00
|
|
|
default:
|
2015-01-02 21:40:09 +01:00
|
|
|
{
|
|
|
|
static auto evaluate_condition = [](const VertexShaderState& state, bool refx, bool refy, Instruction::FlowControlType flow_control) {
|
|
|
|
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-12 22:50:09 +01:00
|
|
|
// Handle each instruction on its own
|
2015-03-08 21:52:38 +01:00
|
|
|
switch (instr.opcode.Value()) {
|
|
|
|
case OpCode::Id::END:
|
2014-12-13 21:30:13 +01:00
|
|
|
exit_loop = true;
|
2014-07-26 19:17:09 +02:00
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::JMPC:
|
2015-01-02 21:40:09 +01:00
|
|
|
if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = instr.flow_control.dest_offset - 1;
|
2015-01-02 21:40:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::JMPU:
|
2015-05-14 05:29:27 +02:00
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id]) {
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = instr.flow_control.dest_offset - 1;
|
2015-01-02 21:40:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::CALL:
|
2014-12-20 15:19:36 +01:00
|
|
|
call(state,
|
2014-12-13 21:30:13 +01:00
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1, 0, 0);
|
2014-12-13 21:30:13 +01:00
|
|
|
break;
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::CALLU:
|
2015-05-14 05:29:27 +02:00
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id]) {
|
2015-01-02 21:40:09 +01:00
|
|
|
call(state,
|
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1, 0, 0);
|
2015-01-02 21:40:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::CALLC:
|
2015-01-02 21:40:09 +01:00
|
|
|
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,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1, 0, 0);
|
2015-01-02 21:40:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::NOP:
|
2014-07-26 19:17:09 +02:00
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::IFU:
|
2015-05-14 05:29:27 +02:00
|
|
|
if (uniforms.b[instr.flow_control.bool_uniform_id]) {
|
2014-12-20 15:19:36 +01:00
|
|
|
call(state,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - state.program_counter - 1,
|
2014-12-21 03:01:35 +01:00
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-13 21:30:13 +01:00
|
|
|
} else {
|
2014-12-20 15:19:36 +01:00
|
|
|
call(state,
|
2014-12-13 21:30:13 +01:00
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2014-12-21 03:01:35 +01:00
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-13 21:30:13 +01:00
|
|
|
}
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
break;
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::IFC:
|
2014-12-12 22:50:09 +01:00
|
|
|
{
|
|
|
|
// TODO: Do we need to consider swizzlers here?
|
|
|
|
|
2015-01-02 21:40:09 +01:00
|
|
|
if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
|
2014-12-20 15:19:36 +01:00
|
|
|
call(state,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - state.program_counter - 1,
|
2014-12-21 03:01:35 +01:00
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-12 22:50:09 +01:00
|
|
|
} else {
|
2014-12-20 15:19:36 +01:00
|
|
|
call(state,
|
2014-12-13 21:30:13 +01:00
|
|
|
instr.flow_control.dest_offset,
|
|
|
|
instr.flow_control.num_instructions,
|
2014-12-21 03:01:35 +01:00
|
|
|
instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
|
2014-12-12 22:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:52:38 +01:00
|
|
|
case OpCode::Id::LOOP:
|
2014-12-21 03:01:35 +01:00
|
|
|
{
|
2015-05-14 05:29:27 +02:00
|
|
|
state.address_registers[2] = uniforms.i[instr.flow_control.int_uniform_id].y;
|
2014-12-21 03:01:35 +01:00
|
|
|
|
|
|
|
call(state,
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter + 1,
|
|
|
|
instr.flow_control.dest_offset - state.program_counter + 1,
|
2014-12-21 03:01:35 +01:00
|
|
|
instr.flow_control.dest_offset + 1,
|
2015-05-14 05:29:27 +02:00
|
|
|
uniforms.i[instr.flow_control.int_uniform_id].x,
|
|
|
|
uniforms.i[instr.flow_control.int_uniform_id].z);
|
2014-12-21 03:01:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
default:
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_ERROR(HW_GPU, "Unhandled instruction: 0x%02x (%s): 0x%08x",
|
2015-03-08 21:52:38 +01:00
|
|
|
(int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
|
2014-07-26 19:17:09 +02:00
|
|
|
break;
|
2014-12-12 18:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-07-26 19:17:09 +02:00
|
|
|
}
|
2015-01-02 21:40:09 +01:00
|
|
|
}
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2014-12-13 21:30:13 +01:00
|
|
|
++state.program_counter;
|
2014-12-12 22:50:09 +01:00
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
if (exit_loop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 20:50:17 +02:00
|
|
|
static Common::Profiling::TimingCategory shader_category("Vertex Shader");
|
|
|
|
|
2015-03-22 00:31:40 +01:00
|
|
|
OutputVertex RunShader(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const State::ShaderSetup& setup) {
|
2015-05-12 20:50:17 +02:00
|
|
|
Common::Profiling::ScopeTimer timer(shader_category);
|
|
|
|
|
2014-07-26 19:17:09 +02:00
|
|
|
VertexShaderState state;
|
|
|
|
|
2015-07-26 12:40:34 +02:00
|
|
|
state.program_counter = config.main_offset;
|
2014-08-14 14:30:38 +02:00
|
|
|
state.debug.max_offset = 0;
|
|
|
|
state.debug.max_opdesc_id = 0;
|
2014-07-26 19:17:09 +02:00
|
|
|
|
|
|
|
// Setup input register table
|
2015-03-22 00:31:40 +01:00
|
|
|
const auto& attribute_register_map = config.input_register_map;
|
2014-07-26 19:17:09 +02:00
|
|
|
float24 dummy_register;
|
2014-12-07 22:22:04 +01:00
|
|
|
boost::fill(state.input_register_table, &dummy_register);
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2015-04-11 20:53:35 +02:00
|
|
|
if (num_attributes > 0) state.input_register_table[attribute_register_map.attribute0_register] = &input.attr[0].x;
|
|
|
|
if (num_attributes > 1) state.input_register_table[attribute_register_map.attribute1_register] = &input.attr[1].x;
|
|
|
|
if (num_attributes > 2) state.input_register_table[attribute_register_map.attribute2_register] = &input.attr[2].x;
|
|
|
|
if (num_attributes > 3) state.input_register_table[attribute_register_map.attribute3_register] = &input.attr[3].x;
|
|
|
|
if (num_attributes > 4) state.input_register_table[attribute_register_map.attribute4_register] = &input.attr[4].x;
|
|
|
|
if (num_attributes > 5) state.input_register_table[attribute_register_map.attribute5_register] = &input.attr[5].x;
|
|
|
|
if (num_attributes > 6) state.input_register_table[attribute_register_map.attribute6_register] = &input.attr[6].x;
|
|
|
|
if (num_attributes > 7) state.input_register_table[attribute_register_map.attribute7_register] = &input.attr[7].x;
|
|
|
|
if (num_attributes > 8) state.input_register_table[attribute_register_map.attribute8_register] = &input.attr[8].x;
|
|
|
|
if (num_attributes > 9) state.input_register_table[attribute_register_map.attribute9_register] = &input.attr[9].x;
|
|
|
|
if (num_attributes > 10) state.input_register_table[attribute_register_map.attribute10_register] = &input.attr[10].x;
|
|
|
|
if (num_attributes > 11) state.input_register_table[attribute_register_map.attribute11_register] = &input.attr[11].x;
|
|
|
|
if (num_attributes > 12) state.input_register_table[attribute_register_map.attribute12_register] = &input.attr[12].x;
|
|
|
|
if (num_attributes > 13) state.input_register_table[attribute_register_map.attribute13_register] = &input.attr[13].x;
|
|
|
|
if (num_attributes > 14) state.input_register_table[attribute_register_map.attribute14_register] = &input.attr[14].x;
|
|
|
|
if (num_attributes > 15) state.input_register_table[attribute_register_map.attribute15_register] = &input.attr[15].x;
|
2014-07-26 19:17:09 +02:00
|
|
|
|
2015-03-12 14:18:46 +01:00
|
|
|
state.conditional_code[0] = false;
|
|
|
|
state.conditional_code[1] = false;
|
|
|
|
|
|
|
|
ProcessShaderCode(state);
|
2015-07-26 11:55:47 +02:00
|
|
|
#if PICA_DUMP_SHADERS
|
2015-03-22 00:31:40 +01:00
|
|
|
DebugUtils::DumpShader(setup.program_code.data(), state.debug.max_offset, setup.swizzle_data.data(),
|
|
|
|
state.debug.max_opdesc_id, config.main_offset,
|
|
|
|
g_state.regs.vs_output_attributes); // TODO: Don't hardcode VS here
|
2015-07-26 11:55:47 +02:00
|
|
|
#endif
|
2014-12-28 21:20:33 +01:00
|
|
|
|
2015-03-12 14:18:46 +01:00
|
|
|
// Setup output data
|
|
|
|
OutputVertex ret;
|
|
|
|
// TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
|
|
|
|
// figure out what those circumstances are and enable the remaining outputs then.
|
2014-07-26 19:17:09 +02:00
|
|
|
for (int i = 0; i < 7; ++i) {
|
2015-03-22 00:31:40 +01:00
|
|
|
const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here
|
2014-07-26 19:17:09 +02:00
|
|
|
|
|
|
|
u32 semantics[4] = {
|
|
|
|
output_register_map.map_x, output_register_map.map_y,
|
|
|
|
output_register_map.map_z, output_register_map.map_w
|
|
|
|
};
|
|
|
|
|
2015-03-12 14:18:46 +01:00
|
|
|
for (int comp = 0; comp < 4; ++comp) {
|
|
|
|
float24* out = ((float24*)&ret) + semantics[comp];
|
|
|
|
if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
|
|
|
|
*out = state.output_registers[i][comp];
|
|
|
|
} else {
|
|
|
|
// Zero output so that attributes which aren't output won't have denormals in them,
|
|
|
|
// which would slow us down later.
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
|
|
}
|
|
|
|
}
|
2014-07-26 19:17:09 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 06:24:27 +02:00
|
|
|
// The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
ret.color[i] = float24::FromFloat32(
|
|
|
|
std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
|
2014-07-26 19:17:09 +02:00
|
|
|
ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
|
|
|
|
ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
|
|
|
|
ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace
|