2015-07-22 00:38:59 +01:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-04-30 16:34:51 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2016-12-19 01:58:30 +00:00
|
|
|
#include "common/bit_set.h"
|
2016-04-30 16:34:51 +01:00
|
|
|
#include "common/logging/log.h"
|
2015-08-17 22:25:21 +01:00
|
|
|
#include "common/microprofile.h"
|
2016-03-03 03:16:38 +00:00
|
|
|
#include "video_core/pica_state.h"
|
2017-01-28 23:12:09 +00:00
|
|
|
#include "video_core/regs_rasterizer.h"
|
|
|
|
#include "video_core/regs_shader.h"
|
2016-09-21 07:52:38 +01:00
|
|
|
#include "video_core/shader/shader.h"
|
2016-04-30 16:34:51 +01:00
|
|
|
#include "video_core/shader/shader_interpreter.h"
|
2015-08-15 03:29:08 +01:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 09:21:16 +00:00
|
|
|
#include "video_core/shader/shader_jit_x64.h"
|
2015-08-15 03:29:08 +01:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2016-04-30 16:34:51 +01:00
|
|
|
#include "video_core/video_core.h"
|
|
|
|
|
2019-02-19 08:09:57 +00:00
|
|
|
namespace Pica::Shader {
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2018-01-02 23:32:33 +00:00
|
|
|
void OutputVertex::ValidateSemantics(const RasterizerRegs& regs) {
|
|
|
|
unsigned int num_attributes = regs.vs_output_total;
|
|
|
|
ASSERT(num_attributes <= 7);
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t attrib = 0; attrib < num_attributes; ++attrib) {
|
2018-01-02 23:32:33 +00:00
|
|
|
u32 output_register_map = regs.vs_output_attributes[attrib].raw;
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t comp = 0; comp < 4; ++comp) {
|
2018-01-02 23:32:33 +00:00
|
|
|
u32 semantic = (output_register_map >> (8 * comp)) & 0x1F;
|
|
|
|
ASSERT_MSG(semantic < 24 || semantic == RasterizerRegs::VSOutputAttributes::INVALID,
|
2018-03-27 16:28:42 +01:00
|
|
|
"Invalid/unknown semantic id: {}", semantic);
|
2018-01-02 23:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:39:43 +01:00
|
|
|
OutputVertex OutputVertex::FromAttributeBuffer(const RasterizerRegs& regs,
|
|
|
|
const AttributeBuffer& input) {
|
2016-05-13 07:49:20 +01:00
|
|
|
// Setup output data
|
2016-12-19 07:42:29 +00:00
|
|
|
union {
|
|
|
|
OutputVertex ret{};
|
2018-01-02 23:32:33 +00:00
|
|
|
// Allow us to overflow OutputVertex to avoid branches, since
|
|
|
|
// RasterizerRegs::VSOutputAttributes::INVALID would write to slot 31, which
|
|
|
|
// would be out of bounds otherwise.
|
|
|
|
std::array<float24, 32> vertex_slots_overflow;
|
2016-12-19 07:42:29 +00:00
|
|
|
};
|
2016-05-13 07:49:20 +01:00
|
|
|
|
2018-01-02 23:32:33 +00:00
|
|
|
// Assert that OutputVertex has enough space for 24 semantic registers
|
|
|
|
static_assert(sizeof(std::array<float24, 24>) == sizeof(ret),
|
|
|
|
"Struct and array have different sizes.");
|
|
|
|
|
|
|
|
unsigned int num_attributes = regs.vs_output_total & 7;
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t attrib = 0; attrib < num_attributes; ++attrib) {
|
2018-01-02 23:32:33 +00:00
|
|
|
const auto output_register_map = regs.vs_output_attributes[attrib];
|
|
|
|
vertex_slots_overflow[output_register_map.map_x] = input.attr[attrib][0];
|
|
|
|
vertex_slots_overflow[output_register_map.map_y] = input.attr[attrib][1];
|
|
|
|
vertex_slots_overflow[output_register_map.map_z] = input.attr[attrib][2];
|
|
|
|
vertex_slots_overflow[output_register_map.map_w] = input.attr[attrib][3];
|
2016-05-13 07:49:20 +01:00
|
|
|
}
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
// The hardware takes the absolute and saturates vertex colors like this, *before* doing
|
|
|
|
// interpolation
|
2016-05-13 07:49:20 +01:00
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2017-09-17 15:42:45 +01:00
|
|
|
float c = std::fabs(ret.color[i].ToFloat32());
|
|
|
|
ret.color[i] = float24::FromFloat32(c < 1.0f ? c : 1.0f);
|
2016-05-13 07:49:20 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_TRACE(HW_GPU,
|
2018-06-29 14:56:12 +01:00
|
|
|
"Output vertex: pos({:.2}, {:.2}, {:.2}, {:.2}), quat({:.2}, {:.2}, {:.2}, {:.2}), "
|
|
|
|
"col({:.2}, {:.2}, {:.2}, {:.2}), tc0({:.2}, {:.2}), view({:.2}, {:.2}, {:.2})",
|
|
|
|
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 07:49:20 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-28 21:03:13 +00:00
|
|
|
void UnitState::LoadInput(const ShaderRegs& config, const AttributeBuffer& input) {
|
2016-12-19 01:25:03 +00:00
|
|
|
const unsigned max_attribute = config.max_input_attribute_index;
|
2016-12-17 05:41:38 +00:00
|
|
|
|
2016-12-19 01:25:03 +00:00
|
|
|
for (unsigned attr = 0; attr <= max_attribute; ++attr) {
|
|
|
|
unsigned reg = config.GetRegisterForAttribute(attr);
|
|
|
|
registers.input[reg] = input.attr[attr];
|
|
|
|
}
|
2016-12-17 05:41:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 03:38:34 +00:00
|
|
|
static void CopyRegistersToOutput(const Common::Vec4<float24>* regs, u32 mask,
|
2017-12-10 04:23:27 +00:00
|
|
|
AttributeBuffer& buffer) {
|
|
|
|
int output_i = 0;
|
|
|
|
for (int reg : Common::BitSet<u32>(mask)) {
|
|
|
|
buffer.attr[output_i++] = regs[reg];
|
2016-12-19 01:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 04:23:27 +00:00
|
|
|
void UnitState::WriteOutput(const ShaderRegs& config, AttributeBuffer& output) {
|
|
|
|
CopyRegistersToOutput(registers.output, config.output_mask, output);
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
UnitState::UnitState(GSEmitter* emitter) : emitter_ptr(emitter) {}
|
|
|
|
|
|
|
|
GSEmitter::GSEmitter() {
|
|
|
|
handlers = new Handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSEmitter::~GSEmitter() {
|
|
|
|
delete handlers;
|
|
|
|
}
|
|
|
|
|
2019-02-27 03:38:34 +00:00
|
|
|
void GSEmitter::Emit(Common::Vec4<float24> (&output_regs)[16]) {
|
2017-07-25 20:30:29 +01:00
|
|
|
ASSERT(vertex_id < 3);
|
2017-12-10 04:30:14 +00:00
|
|
|
// TODO: This should be merged with UnitState::WriteOutput somehow
|
|
|
|
CopyRegistersToOutput(output_regs, output_mask, buffer[vertex_id]);
|
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
if (prim_emit) {
|
|
|
|
if (winding)
|
|
|
|
handlers->winding_setter();
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < buffer.size(); ++i) {
|
2017-12-10 04:30:14 +00:00
|
|
|
handlers->vertex_handler(buffer[i]);
|
2017-07-25 20:30:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GSUnitState::GSUnitState() : UnitState(&emitter) {}
|
|
|
|
|
|
|
|
void GSUnitState::SetVertexHandler(VertexHandler vertex_handler, WindingSetter winding_setter) {
|
|
|
|
emitter.handlers->vertex_handler = std::move(vertex_handler);
|
|
|
|
emitter.handlers->winding_setter = std::move(winding_setter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSUnitState::ConfigOutput(const ShaderRegs& config) {
|
|
|
|
emitter.output_mask = config.output_mask;
|
|
|
|
}
|
|
|
|
|
2016-12-17 09:21:16 +00:00
|
|
|
MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
|
2015-07-23 04:25:30 +01:00
|
|
|
|
2016-03-30 01:45:18 +01:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 09:21:16 +00:00
|
|
|
static std::unique_ptr<JitX64Engine> jit_engine;
|
2016-03-30 01:45:18 +01:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2016-12-17 09:21:16 +00:00
|
|
|
static InterpreterEngine interpreter_engine;
|
2016-12-17 07:21:26 +00:00
|
|
|
|
2016-12-17 09:21:16 +00:00
|
|
|
ShaderEngine* GetEngine() {
|
2015-07-23 04:25:30 +01:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 09:21:16 +00:00
|
|
|
// TODO(yuriks): Re-initialize on each change rather than being persistent
|
2015-07-23 04:25:30 +01:00
|
|
|
if (VideoCore::g_shader_jit_enabled) {
|
2016-12-17 09:21:16 +00:00
|
|
|
if (jit_engine == nullptr) {
|
|
|
|
jit_engine = std::make_unique<JitX64Engine>();
|
2015-07-23 04:25:30 +01:00
|
|
|
}
|
2016-12-17 09:21:16 +00:00
|
|
|
return jit_engine.get();
|
2015-08-15 03:29:08 +01:00
|
|
|
}
|
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2016-12-17 09:21:16 +00:00
|
|
|
return &interpreter_engine;
|
|
|
|
}
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2016-12-17 09:21:16 +00:00
|
|
|
void Shutdown() {
|
2015-07-23 04:25:30 +01:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2016-12-17 09:21:16 +00:00
|
|
|
jit_engine = nullptr;
|
2015-08-15 03:29:08 +01:00
|
|
|
#endif // ARCHITECTURE_x86_64
|
2015-07-22 00:38:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 08:09:57 +00:00
|
|
|
} // namespace Pica::Shader
|