diff options
| author | 2014-07-27 17:34:11 +0200 | |
|---|---|---|
| committer | 2014-08-12 13:49:33 +0200 | |
| commit | 94aa9da562457e1fed4911d1cda770c3e42bd419 (patch) | |
| tree | ba3b698dd4032c2882ea9b4d4395c9fde1d2c498 /src/video_core/clipper.cpp | |
| parent | Pica: Add primitive assembly stage. (diff) | |
| download | yuzu-94aa9da562457e1fed4911d1cda770c3e42bd419.tar.gz yuzu-94aa9da562457e1fed4911d1cda770c3e42bd419.tar.xz yuzu-94aa9da562457e1fed4911d1cda770c3e42bd419.zip | |
Pica: Add triangle clipper.
Diffstat (limited to 'src/video_core/clipper.cpp')
| -rw-r--r-- | src/video_core/clipper.cpp | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/video_core/clipper.cpp b/src/video_core/clipper.cpp new file mode 100644 index 000000000..e9ab6242c --- /dev/null +++ b/src/video_core/clipper.cpp | |||
| @@ -0,0 +1,178 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "clipper.h" | ||
| 8 | #include "pica.h" | ||
| 9 | #include "vertex_shader.h" | ||
| 10 | |||
| 11 | namespace Pica { | ||
| 12 | |||
| 13 | namespace Clipper { | ||
| 14 | |||
| 15 | struct ClippingEdge { | ||
| 16 | public: | ||
| 17 | enum Type { | ||
| 18 | POS_X = 0, | ||
| 19 | NEG_X = 1, | ||
| 20 | POS_Y = 2, | ||
| 21 | NEG_Y = 3, | ||
| 22 | POS_Z = 4, | ||
| 23 | NEG_Z = 5, | ||
| 24 | }; | ||
| 25 | |||
| 26 | ClippingEdge(Type type, float24 position) : type(type), pos(position) {} | ||
| 27 | |||
| 28 | bool IsInside(const OutputVertex& vertex) const { | ||
| 29 | switch (type) { | ||
| 30 | case POS_X: return vertex.pos.x <= pos * vertex.pos.w; | ||
| 31 | case NEG_X: return vertex.pos.x >= pos * vertex.pos.w; | ||
| 32 | case POS_Y: return vertex.pos.y <= pos * vertex.pos.w; | ||
| 33 | case NEG_Y: return vertex.pos.y >= pos * vertex.pos.w; | ||
| 34 | |||
| 35 | // TODO: Check z compares ... should be 0..1 instead? | ||
| 36 | case POS_Z: return vertex.pos.z <= pos * vertex.pos.w; | ||
| 37 | |||
| 38 | default: | ||
| 39 | case NEG_Z: return vertex.pos.z >= pos * vertex.pos.w; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | bool IsOutSide(const OutputVertex& vertex) const { | ||
| 44 | return !IsInside(vertex); | ||
| 45 | } | ||
| 46 | |||
| 47 | OutputVertex GetIntersection(const OutputVertex& v0, const OutputVertex& v1) const { | ||
| 48 | auto dotpr = [this](const OutputVertex& vtx) { | ||
| 49 | switch (type) { | ||
| 50 | case POS_X: return vtx.pos.x - vtx.pos.w; | ||
| 51 | case NEG_X: return -vtx.pos.x - vtx.pos.w; | ||
| 52 | case POS_Y: return vtx.pos.y - vtx.pos.w; | ||
| 53 | case NEG_Y: return -vtx.pos.y - vtx.pos.w; | ||
| 54 | |||
| 55 | // TODO: Verify z clipping | ||
| 56 | case POS_Z: return vtx.pos.z - vtx.pos.w; | ||
| 57 | |||
| 58 | default: | ||
| 59 | case NEG_Z: return -vtx.pos.w; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | float24 dp = dotpr(v0); | ||
| 64 | float24 dp_prev = dotpr(v1); | ||
| 65 | float24 factor = dp_prev / (dp_prev - dp); | ||
| 66 | |||
| 67 | return OutputVertex::Lerp(factor, v0, v1); | ||
| 68 | } | ||
| 69 | |||
| 70 | private: | ||
| 71 | Type type; | ||
| 72 | float24 pos; | ||
| 73 | }; | ||
| 74 | |||
| 75 | static void InitScreenCoordinates(OutputVertex& vtx) | ||
| 76 | { | ||
| 77 | struct { | ||
| 78 | float24 halfsize_x; | ||
| 79 | float24 offset_x; | ||
| 80 | float24 halfsize_y; | ||
| 81 | float24 offset_y; | ||
| 82 | float24 zscale; | ||
| 83 | float24 offset_z; | ||
| 84 | } viewport; | ||
| 85 | |||
| 86 | viewport.halfsize_x = float24::FromRawFloat24(registers.viewport_size_x); | ||
| 87 | viewport.halfsize_y = float24::FromRawFloat24(registers.viewport_size_y); | ||
| 88 | viewport.offset_x = float24::FromFloat32(registers.viewport_corner.x); | ||
| 89 | viewport.offset_y = float24::FromFloat32(registers.viewport_corner.y); | ||
| 90 | viewport.zscale = float24::FromRawFloat24(registers.viewport_depth_range); | ||
| 91 | viewport.offset_z = float24::FromRawFloat24(registers.viewport_depth_far_plane); | ||
| 92 | |||
| 93 | // TODO: Not sure why the viewport width needs to be divided by 2 but the viewport height does not | ||
| 94 | vtx.screenpos[0] = (vtx.pos.x / vtx.pos.w + float24::FromFloat32(1.0)) * viewport.halfsize_x / float24::FromFloat32(2.0) + viewport.offset_x; | ||
| 95 | vtx.screenpos[1] = (vtx.pos.y / vtx.pos.w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y; | ||
| 96 | vtx.screenpos[2] = viewport.offset_z - vtx.pos.z / vtx.pos.w * viewport.zscale; | ||
| 97 | } | ||
| 98 | |||
| 99 | void ProcessTriangle(OutputVertex &v0, OutputVertex &v1, OutputVertex &v2) { | ||
| 100 | |||
| 101 | // TODO (neobrain): | ||
| 102 | // The list of output vertices has some fixed maximum size, | ||
| 103 | // however I haven't taken the time to figure out what it is exactly. | ||
| 104 | // For now, we hence just assume a maximal size of 1000 vertices. | ||
| 105 | const size_t max_vertices = 1000; | ||
| 106 | std::vector<OutputVertex> buffer_vertices; | ||
| 107 | std::vector<OutputVertex*> output_list{ &v0, &v1, &v2 }; | ||
| 108 | |||
| 109 | // Make sure to reserve space for all vertices. | ||
| 110 | // Without this, buffer reallocation would invalidate references. | ||
| 111 | buffer_vertices.reserve(max_vertices); | ||
| 112 | |||
| 113 | // Simple implementation of the Sutherland-Hodgman clipping algorithm. | ||
| 114 | // TODO: Make this less inefficient (currently lots of useless buffering overhead happens here) | ||
| 115 | for (auto edge : { ClippingEdge(ClippingEdge::POS_X, float24::FromFloat32(+1.0)), | ||
| 116 | ClippingEdge(ClippingEdge::NEG_X, float24::FromFloat32(-1.0)), | ||
| 117 | ClippingEdge(ClippingEdge::POS_Y, float24::FromFloat32(+1.0)), | ||
| 118 | ClippingEdge(ClippingEdge::NEG_Y, float24::FromFloat32(-1.0)), | ||
| 119 | ClippingEdge(ClippingEdge::POS_Z, float24::FromFloat32(+1.0)), | ||
| 120 | ClippingEdge(ClippingEdge::NEG_Z, float24::FromFloat32(-1.0)) }) { | ||
| 121 | |||
| 122 | const std::vector<OutputVertex*> input_list = output_list; | ||
| 123 | output_list.clear(); | ||
| 124 | |||
| 125 | const OutputVertex* reference_vertex = input_list.back(); | ||
| 126 | |||
| 127 | for (const auto& vertex : input_list) { | ||
| 128 | // NOTE: This algorithm changes vertex order in some cases! | ||
| 129 | if (edge.IsInside(*vertex)) { | ||
| 130 | if (edge.IsOutSide(*reference_vertex)) { | ||
| 131 | buffer_vertices.push_back(edge.GetIntersection(*vertex, *reference_vertex)); | ||
| 132 | output_list.push_back(&(buffer_vertices.back())); | ||
| 133 | } | ||
| 134 | |||
| 135 | output_list.push_back(vertex); | ||
| 136 | } else if (edge.IsInside(*reference_vertex)) { | ||
| 137 | buffer_vertices.push_back(edge.GetIntersection(*vertex, *reference_vertex)); | ||
| 138 | output_list.push_back(&(buffer_vertices.back())); | ||
| 139 | } | ||
| 140 | |||
| 141 | reference_vertex = vertex; | ||
| 142 | } | ||
| 143 | |||
| 144 | // Need to have at least a full triangle to continue... | ||
| 145 | if (output_list.size() < 3) | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | InitScreenCoordinates(*(output_list[0])); | ||
| 150 | InitScreenCoordinates(*(output_list[1])); | ||
| 151 | |||
| 152 | for (int i = 0; i < output_list.size() - 2; i ++) { | ||
| 153 | OutputVertex& vtx0 = *(output_list[0]); | ||
| 154 | OutputVertex& vtx1 = *(output_list[i+1]); | ||
| 155 | OutputVertex& vtx2 = *(output_list[i+2]); | ||
| 156 | |||
| 157 | InitScreenCoordinates(vtx2); | ||
| 158 | |||
| 159 | DEBUG_LOG(GPU, | ||
| 160 | "Triangle %d/%d (%d buffer vertices) at position (%.3f, %.3f, %.3f, %.3f), " | ||
| 161 | "(%.3f, %.3f, %.3f, %.3f), (%.3f, %.3f, %.3f, %.3f) and " | ||
| 162 | "screen position (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f)", | ||
| 163 | i,output_list.size(), buffer_vertices.size(), | ||
| 164 | vtx0.pos.x.ToFloat32(), vtx0.pos.y.ToFloat32(), vtx0.pos.z.ToFloat32(), vtx0.pos.w.ToFloat32(),output_list.size(), | ||
| 165 | vtx1.pos.x.ToFloat32(), vtx1.pos.y.ToFloat32(), vtx1.pos.z.ToFloat32(), vtx1.pos.w.ToFloat32(), | ||
| 166 | vtx2.pos.x.ToFloat32(), vtx2.pos.y.ToFloat32(), vtx2.pos.z.ToFloat32(), vtx2.pos.w.ToFloat32(), | ||
| 167 | vtx0.screenpos.x.ToFloat32(), vtx0.screenpos.y.ToFloat32(), vtx0.screenpos.z.ToFloat32(), | ||
| 168 | vtx1.screenpos.x.ToFloat32(), vtx1.screenpos.y.ToFloat32(), vtx1.screenpos.z.ToFloat32(), | ||
| 169 | vtx2.screenpos.x.ToFloat32(), vtx2.screenpos.y.ToFloat32(), vtx2.screenpos.z.ToFloat32()); | ||
| 170 | |||
| 171 | // TODO: Send triangle to rasterizer | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | } // namespace | ||
| 177 | |||
| 178 | } // namespace | ||