2015-07-21 19:38:59 -04:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-07-22 23:25:30 -04:00
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2015-07-11 19:57:59 -04:00
|
|
|
#include <boost/range/algorithm/fill.hpp>
|
|
|
|
|
2015-07-22 23:25:30 -04:00
|
|
|
#include "common/hash.h"
|
2015-08-17 17:25:21 -04:00
|
|
|
#include "common/microprofile.h"
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
#include "video_core/debug_utils/debug_utils.h"
|
|
|
|
#include "video_core/pica.h"
|
2016-03-02 22:16:38 -05:00
|
|
|
#include "video_core/pica_state.h"
|
2015-07-22 23:25:30 -04:00
|
|
|
#include "video_core/video_core.h"
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
#include "shader.h"
|
|
|
|
#include "shader_interpreter.h"
|
2015-08-12 00:00:44 -04:00
|
|
|
|
2015-08-14 22:29:08 -04:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2015-08-12 00:00:44 -04:00
|
|
|
#include "shader_jit_x64.h"
|
2015-08-14 22:29:08 -04:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace Shader {
|
|
|
|
|
2015-07-22 23:25:30 -04:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-04-12 23:34:03 -04:00
|
|
|
static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
|
|
|
|
static const JitShader* jit_shader;
|
2015-07-22 23:25:30 -04:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
|
|
|
|
2016-04-01 23:33:03 -04:00
|
|
|
void Setup() {
|
2015-07-22 23:25:30 -04:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
if (VideoCore::g_shader_jit_enabled) {
|
|
|
|
u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
|
2016-03-26 21:02:15 -04:00
|
|
|
Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)));
|
2015-07-22 23:25:30 -04:00
|
|
|
|
|
|
|
auto iter = shader_map.find(cache_key);
|
|
|
|
if (iter != shader_map.end()) {
|
2016-03-20 00:37:05 -04:00
|
|
|
jit_shader = iter->second.get();
|
2015-07-22 23:25:30 -04:00
|
|
|
} else {
|
2016-04-12 23:34:03 -04:00
|
|
|
auto shader = std::make_unique<JitShader>();
|
2016-03-20 00:37:05 -04:00
|
|
|
shader->Compile();
|
|
|
|
jit_shader = shader.get();
|
|
|
|
shader_map[cache_key] = std::move(shader);
|
2015-07-22 23:25:30 -04:00
|
|
|
}
|
2015-08-14 22:29:08 -04:00
|
|
|
}
|
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-22 23:25:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
2015-08-26 18:28:19 -04:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-03-20 00:37:05 -04:00
|
|
|
shader_map.clear();
|
2015-08-26 18:28:19 -04:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 19:38:59 -04:00
|
|
|
}
|
|
|
|
|
2015-08-17 17:25:21 -04:00
|
|
|
MICROPROFILE_DEFINE(GPU_VertexShader, "GPU", "Vertex Shader", MP_RGB(50, 50, 240));
|
2015-07-21 19:38:59 -04:00
|
|
|
|
2015-07-11 19:57:59 -04:00
|
|
|
OutputVertex Run(UnitState<false>& state, const InputVertex& input, int num_attributes) {
|
2015-07-21 19:38:59 -04:00
|
|
|
auto& config = g_state.regs.vs;
|
|
|
|
|
2015-08-17 17:25:21 -04:00
|
|
|
MICROPROFILE_SCOPE(GPU_VertexShader);
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
state.program_counter = config.main_offset;
|
|
|
|
state.debug.max_offset = 0;
|
|
|
|
state.debug.max_opdesc_id = 0;
|
|
|
|
|
|
|
|
// Setup input register table
|
|
|
|
const auto& attribute_register_map = config.input_register_map;
|
|
|
|
|
2015-07-11 19:57:59 -04:00
|
|
|
// TODO: Instead of this cumbersome logic, just load the input data directly like
|
|
|
|
// for (int attr = 0; attr < num_attributes; ++attr) { input_attr[0] = state.registers.input[attribute_register_map.attribute0_register]; }
|
2015-08-15 16:51:32 -04:00
|
|
|
if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = input.attr[0];
|
|
|
|
if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = input.attr[1];
|
|
|
|
if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = input.attr[2];
|
|
|
|
if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = input.attr[3];
|
|
|
|
if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = input.attr[4];
|
|
|
|
if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = input.attr[5];
|
|
|
|
if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = input.attr[6];
|
|
|
|
if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = input.attr[7];
|
|
|
|
if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = input.attr[8];
|
|
|
|
if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = input.attr[9];
|
|
|
|
if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = input.attr[10];
|
|
|
|
if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = input.attr[11];
|
|
|
|
if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = input.attr[12];
|
|
|
|
if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = input.attr[13];
|
|
|
|
if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = input.attr[14];
|
|
|
|
if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = input.attr[15];
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
state.conditional_code[0] = false;
|
|
|
|
state.conditional_code[1] = false;
|
|
|
|
|
2015-07-22 23:25:30 -04:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
if (VideoCore::g_shader_jit_enabled)
|
2016-03-26 21:02:15 -04:00
|
|
|
jit_shader->Run(&state.registers, g_state.regs.vs.main_offset);
|
2015-07-22 23:25:30 -04:00
|
|
|
else
|
|
|
|
RunInterpreter(state);
|
|
|
|
#else
|
2015-07-21 19:38:59 -04:00
|
|
|
RunInterpreter(state);
|
2015-08-14 22:29:08 -04:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 19:38:59 -04: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.
|
2016-03-12 21:06:57 -05:00
|
|
|
unsigned index = 0;
|
|
|
|
for (unsigned i = 0; i < 7; ++i) {
|
|
|
|
|
|
|
|
if (index >= g_state.regs.vs_output_total)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((g_state.regs.vs.output_mask & (1 << i)) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto& output_register_map = g_state.regs.vs_output_attributes[index]; // TODO: Don't hardcode VS here
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
u32 semantics[4] = {
|
|
|
|
output_register_map.map_x, output_register_map.map_y,
|
|
|
|
output_register_map.map_z, output_register_map.map_w
|
|
|
|
};
|
|
|
|
|
2016-03-12 21:06:57 -05:00
|
|
|
for (unsigned comp = 0; comp < 4; ++comp) {
|
2015-07-21 19:38:59 -04:00
|
|
|
float24* out = ((float24*)&ret) + semantics[comp];
|
|
|
|
if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
|
2015-08-15 16:51:32 -04:00
|
|
|
*out = state.registers.output[i][comp];
|
2015-07-21 19:38:59 -04:00
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 21:06:57 -05:00
|
|
|
|
|
|
|
index++;
|
2015-07-21 19:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
|
2016-03-12 21:06:57 -05:00
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2015-07-21 19:38:59 -04:00
|
|
|
ret.color[i] = float24::FromFloat32(
|
|
|
|
std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
|
|
|
|
}
|
|
|
|
|
2016-03-12 12:03:32 -05:00
|
|
|
LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
|
2015-09-09 22:39:43 -04:00
|
|
|
"col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
|
2015-07-21 19:38:59 -04:00
|
|
|
ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
|
2015-07-11 19:57:59 -04:00
|
|
|
ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(),
|
2015-07-21 19:38:59 -04:00
|
|
|
ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
|
2015-09-09 22:39:43 -04:00
|
|
|
ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(),
|
|
|
|
ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
|
2015-07-21 19:38:59 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:16:38 -05:00
|
|
|
DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) {
|
2015-07-11 19:57:59 -04:00
|
|
|
UnitState<true> state;
|
|
|
|
|
|
|
|
state.program_counter = config.main_offset;
|
|
|
|
state.debug.max_offset = 0;
|
|
|
|
state.debug.max_opdesc_id = 0;
|
|
|
|
|
|
|
|
// Setup input register table
|
|
|
|
const auto& attribute_register_map = config.input_register_map;
|
|
|
|
float24 dummy_register;
|
|
|
|
boost::fill(state.registers.input, &dummy_register);
|
|
|
|
|
|
|
|
if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = &input.attr[0].x;
|
|
|
|
if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = &input.attr[1].x;
|
|
|
|
if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = &input.attr[2].x;
|
|
|
|
if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = &input.attr[3].x;
|
|
|
|
if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = &input.attr[4].x;
|
|
|
|
if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = &input.attr[5].x;
|
|
|
|
if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = &input.attr[6].x;
|
|
|
|
if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = &input.attr[7].x;
|
|
|
|
if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = &input.attr[8].x;
|
|
|
|
if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = &input.attr[9].x;
|
|
|
|
if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = &input.attr[10].x;
|
|
|
|
if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = &input.attr[11].x;
|
|
|
|
if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = &input.attr[12].x;
|
|
|
|
if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = &input.attr[13].x;
|
|
|
|
if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = &input.attr[14].x;
|
|
|
|
if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = &input.attr[15].x;
|
|
|
|
|
|
|
|
state.conditional_code[0] = false;
|
|
|
|
state.conditional_code[1] = false;
|
|
|
|
|
|
|
|
RunInterpreter(state);
|
|
|
|
return state.debug;
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:38:59 -04:00
|
|
|
} // namespace Shader
|
|
|
|
|
|
|
|
} // namespace Pica
|