2015-07-21 18:38:59 -05:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-04-30 10:34:51 -05:00
|
|
|
#include <atomic>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2015-07-22 22:25:30 -05:00
|
|
|
#include <unordered_map>
|
2016-04-30 10:34:51 -05:00
|
|
|
#include <utility>
|
2015-07-11 18:57:59 -05:00
|
|
|
#include <boost/range/algorithm/fill.hpp>
|
2016-04-30 10:34:51 -05:00
|
|
|
#include "common/bit_field.h"
|
2015-07-22 22:25:30 -05:00
|
|
|
#include "common/hash.h"
|
2016-04-30 10:34:51 -05:00
|
|
|
#include "common/logging/log.h"
|
2015-08-17 16:25:21 -05:00
|
|
|
#include "common/microprofile.h"
|
2015-07-21 18:38:59 -05:00
|
|
|
#include "video_core/pica.h"
|
2016-03-02 21:16:38 -06:00
|
|
|
#include "video_core/pica_state.h"
|
2016-09-21 01:52:38 -05:00
|
|
|
#include "video_core/shader/shader.h"
|
2016-04-30 10:34:51 -05:00
|
|
|
#include "video_core/shader/shader_interpreter.h"
|
2015-08-14 21:29:08 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-04-30 10:34:51 -05:00
|
|
|
#include "video_core/shader/shader_jit_x64.h"
|
2015-08-14 21:29:08 -05:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2016-04-30 10:34:51 -05:00
|
|
|
#include "video_core/video_core.h"
|
|
|
|
|
2015-07-21 18:38:59 -05:00
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace Shader {
|
|
|
|
|
2016-05-13 01:49:20 -05:00
|
|
|
OutputVertex OutputRegisters::ToVertex(const Regs::ShaderConfig& config) {
|
|
|
|
// 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.
|
|
|
|
unsigned index = 0;
|
|
|
|
for (unsigned i = 0; i < 7; ++i) {
|
|
|
|
|
|
|
|
if (index >= g_state.regs.vs_output_total)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((config.output_mask & (1 << i)) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto& output_register_map = g_state.regs.vs_output_attributes[index];
|
|
|
|
|
2016-09-17 19:38:01 -05: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-05-13 01:49:20 -05:00
|
|
|
|
|
|
|
for (unsigned comp = 0; comp < 4; ++comp) {
|
|
|
|
float24* out = ((float24*)&ret) + semantics[comp];
|
|
|
|
if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
|
|
|
|
*out = value[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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2016-09-17 19:38:01 -05:00
|
|
|
// The hardware takes the absolute and saturates vertex colors like this, *before* doing
|
|
|
|
// interpolation
|
2016-05-13 01:49:20 -05:00
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2016-09-17 19:38:01 -05:00
|
|
|
ret.color[i] = float24::FromFloat32(std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
|
2016-05-13 01:49:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
|
2016-09-17 19:38:01 -05:00
|
|
|
"col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
|
|
|
|
ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(),
|
|
|
|
ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(),
|
|
|
|
ret.quat.z.ToFloat32(), ret.quat.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(), ret.view.x.ToFloat32(),
|
|
|
|
ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
|
2016-05-13 01:49:20 -05:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-22 22:25:30 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-04-12 22:34:03 -05:00
|
|
|
static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
|
|
|
|
static const JitShader* jit_shader;
|
2015-07-22 22:25:30 -05:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
|
|
|
|
2016-03-29 19:45:18 -05:00
|
|
|
void ClearCache() {
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
shader_map.clear();
|
|
|
|
#endif // ARCHITECTURE_x86_64
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderSetup::Setup() {
|
2015-07-22 22:25:30 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
if (VideoCore::g_shader_jit_enabled) {
|
2016-09-17 19:38:01 -05:00
|
|
|
u64 cache_key =
|
2016-09-18 20:01:46 -05:00
|
|
|
Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
|
|
|
|
Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data));
|
2015-07-22 22:25:30 -05:00
|
|
|
|
|
|
|
auto iter = shader_map.find(cache_key);
|
|
|
|
if (iter != shader_map.end()) {
|
2016-03-19 23:37:05 -05:00
|
|
|
jit_shader = iter->second.get();
|
2015-07-22 22:25:30 -05:00
|
|
|
} else {
|
2016-04-12 22:34:03 -05:00
|
|
|
auto shader = std::make_unique<JitShader>();
|
2016-03-19 23:37:05 -05:00
|
|
|
shader->Compile();
|
|
|
|
jit_shader = shader.get();
|
|
|
|
shader_map[cache_key] = std::move(shader);
|
2015-07-22 22:25:30 -05:00
|
|
|
}
|
2015-08-14 21:29:08 -05:00
|
|
|
}
|
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-22 22:25:30 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 19:45:18 -05:00
|
|
|
MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
|
2015-07-21 18:38:59 -05:00
|
|
|
|
2016-05-13 01:49:20 -05:00
|
|
|
void ShaderSetup::Run(UnitState<false>& state, const InputVertex& input, int num_attributes) {
|
2015-07-21 18:38:59 -05:00
|
|
|
auto& config = g_state.regs.vs;
|
2016-05-13 01:46:14 -05:00
|
|
|
auto& setup = g_state.vs;
|
2015-07-21 18:38:59 -05:00
|
|
|
|
2016-03-29 19:45:18 -05:00
|
|
|
MICROPROFILE_SCOPE(GPU_Shader);
|
2015-07-21 18:38:59 -05:00
|
|
|
|
|
|
|
state.debug.max_offset = 0;
|
|
|
|
state.debug.max_opdesc_id = 0;
|
|
|
|
|
|
|
|
// Setup input register table
|
|
|
|
const auto& attribute_register_map = config.input_register_map;
|
|
|
|
|
2016-03-29 21:26:07 -05:00
|
|
|
for (unsigned i = 0; i < num_attributes; i++)
|
2016-09-17 19:38:01 -05:00
|
|
|
state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
|
2015-07-21 18:38:59 -05:00
|
|
|
|
|
|
|
state.conditional_code[0] = false;
|
|
|
|
state.conditional_code[1] = false;
|
|
|
|
|
2015-07-22 22:25:30 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
if (VideoCore::g_shader_jit_enabled)
|
2016-05-13 01:46:14 -05:00
|
|
|
jit_shader->Run(setup, state, config.main_offset);
|
2015-07-22 22:25:30 -05:00
|
|
|
else
|
2016-05-13 02:38:40 -05:00
|
|
|
RunInterpreter(setup, state, config.main_offset);
|
2015-07-22 22:25:30 -05:00
|
|
|
#else
|
2016-05-13 02:38:40 -05:00
|
|
|
RunInterpreter(setup, state, config.main_offset);
|
2015-08-14 21:29:08 -05:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 18:38:59 -05:00
|
|
|
}
|
|
|
|
|
2016-09-17 19:38:01 -05:00
|
|
|
DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes,
|
|
|
|
const Regs::ShaderConfig& config,
|
|
|
|
const ShaderSetup& setup) {
|
2015-07-11 18:57:59 -05:00
|
|
|
UnitState<true> state;
|
|
|
|
|
|
|
|
state.debug.max_offset = 0;
|
|
|
|
state.debug.max_opdesc_id = 0;
|
|
|
|
|
|
|
|
// Setup input register table
|
2016-09-16 01:16:39 -05:00
|
|
|
boost::fill(state.registers.input, Math::Vec4<float24>::AssignToAll(float24::Zero()));
|
2015-07-11 18:57:59 -05:00
|
|
|
const auto& attribute_register_map = config.input_register_map;
|
2016-03-29 21:26:07 -05:00
|
|
|
for (unsigned i = 0; i < num_attributes; i++)
|
2016-09-17 19:38:01 -05:00
|
|
|
state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
|
2015-07-11 18:57:59 -05:00
|
|
|
|
|
|
|
state.conditional_code[0] = false;
|
|
|
|
state.conditional_code[1] = false;
|
|
|
|
|
2016-05-13 02:38:40 -05:00
|
|
|
RunInterpreter(setup, state, config.main_offset);
|
2015-07-11 18:57:59 -05:00
|
|
|
return state.debug;
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:38:59 -05:00
|
|
|
} // namespace Shader
|
|
|
|
|
|
|
|
} // namespace Pica
|