2014-07-27 15:34:11 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-07-27 15:34:11 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-04-30 15:34:51 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
2014-12-28 02:56:32 +00:00
|
|
|
#include <boost/container/static_vector.hpp>
|
2016-04-30 15:34:51 +00:00
|
|
|
#include <boost/container/vector.hpp>
|
|
|
|
#include "common/bit_field.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/vector_math.h"
|
2016-03-03 03:16:38 +00:00
|
|
|
#include "video_core/pica_state.h"
|
2016-04-30 15:34:51 +00:00
|
|
|
#include "video_core/pica_types.h"
|
|
|
|
#include "video_core/shader/shader.h"
|
2017-01-28 23:59:36 +00:00
|
|
|
#include "video_core/swrasterizer/clipper.h"
|
|
|
|
#include "video_core/swrasterizer/rasterizer.h"
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
using Pica::Rasterizer::Vertex;
|
|
|
|
|
2014-07-27 15:34:11 +00:00
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace Clipper {
|
|
|
|
|
|
|
|
struct ClippingEdge {
|
|
|
|
public:
|
2016-09-18 00:38:01 +00:00
|
|
|
ClippingEdge(Math::Vec4<float24> coeffs, Math::Vec4<float24> bias = Math::Vec4<float24>(
|
|
|
|
float24::FromFloat32(0), float24::FromFloat32(0),
|
|
|
|
float24::FromFloat32(0), float24::FromFloat32(0)))
|
2016-09-19 01:01:46 +00:00
|
|
|
: coeffs(coeffs), bias(bias) {}
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
bool IsInside(const Vertex& vertex) const {
|
2017-08-22 06:47:15 +00:00
|
|
|
return Math::Dot(vertex.pos + bias, coeffs) >= float24::FromFloat32(0);
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
bool IsOutSide(const Vertex& vertex) const {
|
2014-07-27 15:34:11 +00:00
|
|
|
return !IsInside(vertex);
|
|
|
|
}
|
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
Vertex GetIntersection(const Vertex& v0, const Vertex& v1) const {
|
2015-01-02 19:37:25 +00:00
|
|
|
float24 dp = Math::Dot(v0.pos + bias, coeffs);
|
|
|
|
float24 dp_prev = Math::Dot(v1.pos + bias, coeffs);
|
2014-07-27 15:34:11 +00:00
|
|
|
float24 factor = dp_prev / (dp_prev - dp);
|
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
return Vertex::Lerp(factor, v0, v1);
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float24 pos;
|
2015-01-02 19:37:25 +00:00
|
|
|
Math::Vec4<float24> coeffs;
|
|
|
|
Math::Vec4<float24> bias;
|
2014-07-27 15:34:11 +00:00
|
|
|
};
|
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
static void InitScreenCoordinates(Vertex& vtx) {
|
2014-07-27 15:34:11 +00:00
|
|
|
struct {
|
|
|
|
float24 halfsize_x;
|
|
|
|
float24 offset_x;
|
|
|
|
float24 halfsize_y;
|
|
|
|
float24 offset_y;
|
|
|
|
float24 zscale;
|
|
|
|
float24 offset_z;
|
|
|
|
} viewport;
|
|
|
|
|
2015-05-14 03:29:27 +00:00
|
|
|
const auto& regs = g_state.regs;
|
2017-01-28 04:16:36 +00:00
|
|
|
viewport.halfsize_x = float24::FromRaw(regs.rasterizer.viewport_size_x);
|
|
|
|
viewport.halfsize_y = float24::FromRaw(regs.rasterizer.viewport_size_y);
|
|
|
|
viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.rasterizer.viewport_corner.x));
|
|
|
|
viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.rasterizer.viewport_corner.y));
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2014-12-23 15:05:51 +00:00
|
|
|
float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w;
|
2017-05-20 20:17:12 +00:00
|
|
|
vtx.pos.w = inv_w;
|
2015-09-10 02:39:43 +00:00
|
|
|
vtx.quat *= inv_w;
|
2017-05-20 20:17:12 +00:00
|
|
|
vtx.color *= inv_w;
|
2014-12-23 15:05:51 +00:00
|
|
|
vtx.tc0 *= inv_w;
|
|
|
|
vtx.tc1 *= inv_w;
|
2017-05-20 20:17:12 +00:00
|
|
|
vtx.tc0_w *= inv_w;
|
|
|
|
vtx.view *= inv_w;
|
2014-12-23 15:05:51 +00:00
|
|
|
vtx.tc2 *= inv_w;
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
vtx.screenpos[0] =
|
|
|
|
(vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x;
|
|
|
|
vtx.screenpos[1] =
|
|
|
|
(vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y;
|
2016-03-20 00:53:49 +00:00
|
|
|
vtx.screenpos[2] = vtx.pos.z * inv_w;
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void ProcessTriangle(const OutputVertex& v0, const OutputVertex& v1, const OutputVertex& v2) {
|
2014-12-28 02:56:32 +00:00
|
|
|
using boost::container::static_vector;
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2014-12-29 01:05:15 +00:00
|
|
|
// Clipping a planar n-gon against a plane will remove at least 1 vertex and introduces 2 at
|
|
|
|
// the new edge (or less in degenerate cases). As such, we can say that each clipping plane
|
|
|
|
// introduces at most 1 new vertex to the polygon. Since we start with a triangle and have a
|
|
|
|
// fixed 6 clipping planes, the maximum number of vertices of the clipped polygon is 3 + 6 = 9.
|
|
|
|
static const size_t MAX_VERTICES = 9;
|
2016-12-19 07:43:37 +00:00
|
|
|
static_vector<Vertex, MAX_VERTICES> buffer_a = {v0, v1, v2};
|
|
|
|
static_vector<Vertex, MAX_VERTICES> buffer_b;
|
2017-06-11 16:55:35 +00:00
|
|
|
|
|
|
|
auto FlipQuaternionIfOpposite = [](auto& a, const auto& b) {
|
|
|
|
if (Math::Dot(a, b) < float24::Zero())
|
|
|
|
a = -a;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Flip the quaternions if they are opposite to prevent interpolating them over the wrong
|
|
|
|
// direction.
|
|
|
|
FlipQuaternionIfOpposite(buffer_a[1].quat, buffer_a[0].quat);
|
|
|
|
FlipQuaternionIfOpposite(buffer_a[2].quat, buffer_a[0].quat);
|
|
|
|
|
2014-12-29 01:05:15 +00:00
|
|
|
auto* output_list = &buffer_a;
|
2016-09-18 00:38:01 +00:00
|
|
|
auto* input_list = &buffer_b;
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2015-01-02 19:37:25 +00:00
|
|
|
// NOTE: We clip against a w=epsilon plane to guarantee that the output has a positive w value.
|
|
|
|
// TODO: Not sure if this is a valid approach. Also should probably instead use the smallest
|
|
|
|
// epsilon possible within float24 accuracy.
|
2015-06-26 07:28:11 +00:00
|
|
|
static const float24 EPSILON = float24::FromFloat32(0.00001f);
|
2015-01-02 19:37:25 +00:00
|
|
|
static const float24 f0 = float24::FromFloat32(0.0);
|
|
|
|
static const float24 f1 = float24::FromFloat32(1.0);
|
|
|
|
static const std::array<ClippingEdge, 7> clipping_edges = {{
|
2017-08-22 06:47:15 +00:00
|
|
|
{Math::MakeVec(-f1, f0, f0, f1)}, // x = +w
|
|
|
|
{Math::MakeVec(f1, f0, f0, f1)}, // x = -w
|
|
|
|
{Math::MakeVec(f0, -f1, f0, f1)}, // y = +w
|
|
|
|
{Math::MakeVec(f0, f1, f0, f1)}, // y = -w
|
|
|
|
{Math::MakeVec(f0, f0, -f1, f0)}, // z = 0
|
|
|
|
{Math::MakeVec(f0, f0, f1, f1)}, // z = -w
|
|
|
|
{Math::MakeVec(f0, f0, f0, f1), Math::Vec4<float24>(f0, f0, f0, EPSILON)}, // w = EPSILON
|
2015-01-02 19:37:25 +00:00
|
|
|
}};
|
|
|
|
|
2014-07-27 15:34:11 +00:00
|
|
|
// Simple implementation of the Sutherland-Hodgman clipping algorithm.
|
|
|
|
// TODO: Make this less inefficient (currently lots of useless buffering overhead happens here)
|
2017-08-22 06:49:26 +00:00
|
|
|
auto Clip = [&](const ClippingEdge& edge) {
|
2014-12-29 01:05:15 +00:00
|
|
|
std::swap(input_list, output_list);
|
|
|
|
output_list->clear();
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2016-12-19 07:43:37 +00:00
|
|
|
const Vertex* reference_vertex = &input_list->back();
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2014-12-29 01:05:15 +00:00
|
|
|
for (const auto& vertex : *input_list) {
|
2014-07-27 15:34:11 +00:00
|
|
|
// NOTE: This algorithm changes vertex order in some cases!
|
2014-12-29 01:05:15 +00:00
|
|
|
if (edge.IsInside(vertex)) {
|
2014-07-27 15:34:11 +00:00
|
|
|
if (edge.IsOutSide(*reference_vertex)) {
|
2014-12-29 01:05:15 +00:00
|
|
|
output_list->push_back(edge.GetIntersection(vertex, *reference_vertex));
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 01:05:15 +00:00
|
|
|
output_list->push_back(vertex);
|
2014-07-27 15:34:11 +00:00
|
|
|
} else if (edge.IsInside(*reference_vertex)) {
|
2014-12-29 01:05:15 +00:00
|
|
|
output_list->push_back(edge.GetIntersection(vertex, *reference_vertex));
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
2014-12-29 01:05:15 +00:00
|
|
|
reference_vertex = &vertex;
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
2017-08-22 06:49:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto edge : clipping_edges) {
|
|
|
|
Clip(edge);
|
2014-07-27 15:34:11 +00:00
|
|
|
|
|
|
|
// Need to have at least a full triangle to continue...
|
2014-12-29 01:05:15 +00:00
|
|
|
if (output_list->size() < 3)
|
2014-07-27 15:34:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-22 06:49:26 +00:00
|
|
|
if (g_state.regs.rasterizer.clip_enable) {
|
2017-08-22 06:47:15 +00:00
|
|
|
ClippingEdge custom_edge{g_state.regs.rasterizer.GetClipCoef()};
|
2017-08-22 06:49:26 +00:00
|
|
|
Clip(custom_edge);
|
|
|
|
|
|
|
|
if (output_list->size() < 3)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 01:05:15 +00:00
|
|
|
InitScreenCoordinates((*output_list)[0]);
|
|
|
|
InitScreenCoordinates((*output_list)[1]);
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
for (size_t i = 0; i < output_list->size() - 2; i++) {
|
2016-12-19 07:43:37 +00:00
|
|
|
Vertex& vtx0 = (*output_list)[0];
|
|
|
|
Vertex& vtx1 = (*output_list)[i + 1];
|
|
|
|
Vertex& vtx2 = (*output_list)[i + 2];
|
2014-07-27 15:34:11 +00:00
|
|
|
|
|
|
|
InitScreenCoordinates(vtx2);
|
|
|
|
|
2014-12-06 01:53:49 +00:00
|
|
|
LOG_TRACE(Render_Software,
|
2014-12-29 01:05:15 +00:00
|
|
|
"Triangle %lu/%lu at position (%.3f, %.3f, %.3f, %.3f), "
|
2014-11-14 16:47:06 +00:00
|
|
|
"(%.3f, %.3f, %.3f, %.3f), (%.3f, %.3f, %.3f, %.3f) and "
|
2014-07-27 15:34:11 +00:00
|
|
|
"screen position (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f)",
|
2016-09-18 00:38:01 +00:00
|
|
|
i + 1, output_list->size() - 2, vtx0.pos.x.ToFloat32(), vtx0.pos.y.ToFloat32(),
|
|
|
|
vtx0.pos.z.ToFloat32(), vtx0.pos.w.ToFloat32(), vtx1.pos.x.ToFloat32(),
|
|
|
|
vtx1.pos.y.ToFloat32(), vtx1.pos.z.ToFloat32(), vtx1.pos.w.ToFloat32(),
|
|
|
|
vtx2.pos.x.ToFloat32(), vtx2.pos.y.ToFloat32(), vtx2.pos.z.ToFloat32(),
|
|
|
|
vtx2.pos.w.ToFloat32(), vtx0.screenpos.x.ToFloat32(),
|
|
|
|
vtx0.screenpos.y.ToFloat32(), vtx0.screenpos.z.ToFloat32(),
|
|
|
|
vtx1.screenpos.x.ToFloat32(), vtx1.screenpos.y.ToFloat32(),
|
|
|
|
vtx1.screenpos.z.ToFloat32(), vtx2.screenpos.x.ToFloat32(),
|
|
|
|
vtx2.screenpos.y.ToFloat32(), vtx2.screenpos.z.ToFloat32());
|
2014-07-27 15:34:11 +00:00
|
|
|
|
2014-07-27 16:02:35 +00:00
|
|
|
Rasterizer::ProcessTriangle(vtx0, vtx1, vtx2);
|
2014-07-27 15:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace
|