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 <cmath>
|
|
|
|
#include <cstring>
|
2016-12-18 19:58:30 -06:00
|
|
|
#include "common/bit_set.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"
|
2016-03-02 21:16:38 -06:00
|
|
|
#include "video_core/pica_state.h"
|
2017-01-28 17:12:09 -06:00
|
|
|
#include "video_core/regs_rasterizer.h"
|
|
|
|
#include "video_core/regs_shader.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-12-17 03:21:16 -06: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 {
|
|
|
|
|
2017-01-27 22:16:36 -06:00
|
|
|
OutputVertex OutputVertex::FromAttributeBuffer(const RasterizerRegs& regs, AttributeBuffer& input) {
|
2016-05-13 01:49:20 -05:00
|
|
|
// Setup output data
|
2016-12-19 01:42:29 -06:00
|
|
|
union {
|
|
|
|
OutputVertex ret{};
|
|
|
|
std::array<float24, 24> vertex_slots;
|
|
|
|
};
|
2016-12-19 01:43:37 -06:00
|
|
|
static_assert(sizeof(vertex_slots) == sizeof(ret), "Struct and array have different sizes.");
|
2016-05-13 01:49:20 -05:00
|
|
|
|
2016-12-18 19:58:30 -06:00
|
|
|
unsigned int num_attributes = regs.vs_output_total;
|
2016-12-19 01:42:29 -06:00
|
|
|
ASSERT(num_attributes <= 7);
|
2016-12-18 19:58:30 -06:00
|
|
|
for (unsigned int i = 0; i < num_attributes; ++i) {
|
|
|
|
const auto& output_register_map = regs.vs_output_attributes[i];
|
2016-05-13 01:49:20 -05:00
|
|
|
|
2017-01-27 22:16:36 -06:00
|
|
|
RasterizerRegs::VSOutputAttributes::Semantic semantics[4] = {
|
2016-12-19 01:42:29 -06:00
|
|
|
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) {
|
2017-01-27 22:16:36 -06:00
|
|
|
RasterizerRegs::VSOutputAttributes::Semantic semantic = semantics[comp];
|
2016-12-19 01:42:29 -06:00
|
|
|
if (semantic < vertex_slots.size()) {
|
2017-02-10 22:51:09 -06:00
|
|
|
vertex_slots[semantic] = input.attr[i][comp];
|
2017-01-27 22:16:36 -06:00
|
|
|
} else if (semantic != RasterizerRegs::VSOutputAttributes::INVALID) {
|
2016-12-19 01:42:29 -06:00
|
|
|
LOG_ERROR(HW_GPU, "Invalid/unknown semantic id: %u", (unsigned int)semantic);
|
2016-05-13 01:49:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-28 15:03:13 -06:00
|
|
|
void UnitState::LoadInput(const ShaderRegs& config, const AttributeBuffer& input) {
|
2016-12-18 19:25:03 -06:00
|
|
|
const unsigned max_attribute = config.max_input_attribute_index;
|
2016-12-16 23:41:38 -06:00
|
|
|
|
2016-12-18 19:25:03 -06:00
|
|
|
for (unsigned attr = 0; attr <= max_attribute; ++attr) {
|
|
|
|
unsigned reg = config.GetRegisterForAttribute(attr);
|
|
|
|
registers.input[reg] = input.attr[attr];
|
|
|
|
}
|
2016-12-16 23:41:38 -06:00
|
|
|
}
|
|
|
|
|
2017-01-28 15:03:13 -06:00
|
|
|
void UnitState::WriteOutput(const ShaderRegs& config, AttributeBuffer& output) {
|
2016-12-18 19:58:30 -06:00
|
|
|
unsigned int output_i = 0;
|
|
|
|
for (unsigned int reg : Common::BitSet<u32>(config.output_mask)) {
|
|
|
|
output.attr[output_i++] = registers.output[reg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 03:21:16 -06:00
|
|
|
MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
|
2015-07-22 22:25:30 -05:00
|
|
|
|
2016-03-29 19:45:18 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 03:21:16 -06:00
|
|
|
static std::unique_ptr<JitX64Engine> jit_engine;
|
2016-03-29 19:45:18 -05:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2016-12-17 03:21:16 -06:00
|
|
|
static InterpreterEngine interpreter_engine;
|
2016-12-17 01:21:26 -06:00
|
|
|
|
2016-12-17 03:21:16 -06:00
|
|
|
ShaderEngine* GetEngine() {
|
2015-07-22 22:25:30 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 03:21:16 -06:00
|
|
|
// TODO(yuriks): Re-initialize on each change rather than being persistent
|
2015-07-22 22:25:30 -05:00
|
|
|
if (VideoCore::g_shader_jit_enabled) {
|
2016-12-17 03:21:16 -06:00
|
|
|
if (jit_engine == nullptr) {
|
|
|
|
jit_engine = std::make_unique<JitX64Engine>();
|
2015-07-22 22:25:30 -05:00
|
|
|
}
|
2016-12-17 03:21:16 -06:00
|
|
|
return jit_engine.get();
|
2015-08-14 21:29:08 -05:00
|
|
|
}
|
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 18:38:59 -05:00
|
|
|
|
2016-12-17 03:21:16 -06:00
|
|
|
return &interpreter_engine;
|
|
|
|
}
|
2015-07-21 18:38:59 -05:00
|
|
|
|
2016-12-17 03:21:16 -06:00
|
|
|
void Shutdown() {
|
2015-07-22 22:25:30 -05:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 03:21:16 -06:00
|
|
|
jit_engine = nullptr;
|
2015-08-14 21:29:08 -05:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-21 18:38:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader
|
|
|
|
|
|
|
|
} // namespace Pica
|