clipper.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <boost/container/static_vector.hpp>
  5. #include "video_core/clipper.h"
  6. #include "video_core/pica.h"
  7. #include "video_core/rasterizer.h"
  8. #include "video_core/shader/shader_interpreter.h"
  9. namespace Pica {
  10. namespace Clipper {
  11. struct ClippingEdge {
  12. public:
  13. ClippingEdge(Math::Vec4<float24> coeffs,
  14. Math::Vec4<float24> bias = Math::Vec4<float24>(float24::FromFloat32(0),
  15. float24::FromFloat32(0),
  16. float24::FromFloat32(0),
  17. float24::FromFloat32(0)))
  18. : coeffs(coeffs),
  19. bias(bias)
  20. {
  21. }
  22. bool IsInside(const OutputVertex& vertex) const {
  23. return Math::Dot(vertex.pos + bias, coeffs) <= float24::FromFloat32(0);
  24. }
  25. bool IsOutSide(const OutputVertex& vertex) const {
  26. return !IsInside(vertex);
  27. }
  28. OutputVertex GetIntersection(const OutputVertex& v0, const OutputVertex& v1) const {
  29. float24 dp = Math::Dot(v0.pos + bias, coeffs);
  30. float24 dp_prev = Math::Dot(v1.pos + bias, coeffs);
  31. float24 factor = dp_prev / (dp_prev - dp);
  32. return OutputVertex::Lerp(factor, v0, v1);
  33. }
  34. private:
  35. float24 pos;
  36. Math::Vec4<float24> coeffs;
  37. Math::Vec4<float24> bias;
  38. };
  39. static void InitScreenCoordinates(OutputVertex& vtx)
  40. {
  41. struct {
  42. float24 halfsize_x;
  43. float24 offset_x;
  44. float24 halfsize_y;
  45. float24 offset_y;
  46. float24 zscale;
  47. float24 offset_z;
  48. } viewport;
  49. const auto& regs = g_state.regs;
  50. viewport.halfsize_x = float24::FromRawFloat24(regs.viewport_size_x);
  51. viewport.halfsize_y = float24::FromRawFloat24(regs.viewport_size_y);
  52. viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.viewport_corner.x));
  53. viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.viewport_corner.y));
  54. viewport.zscale = float24::FromRawFloat24(regs.viewport_depth_range);
  55. viewport.offset_z = float24::FromRawFloat24(regs.viewport_depth_far_plane);
  56. float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w;
  57. vtx.color *= inv_w;
  58. vtx.view *= inv_w;
  59. vtx.quat *= inv_w;
  60. vtx.tc0 *= inv_w;
  61. vtx.tc1 *= inv_w;
  62. vtx.tc2 *= inv_w;
  63. vtx.pos.w = inv_w;
  64. vtx.screenpos[0] = (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x;
  65. vtx.screenpos[1] = (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y;
  66. vtx.screenpos[2] = viewport.offset_z + vtx.pos.z * inv_w * viewport.zscale;
  67. }
  68. void ProcessTriangle(const OutputVertex &v0, const OutputVertex &v1, const OutputVertex &v2) {
  69. using boost::container::static_vector;
  70. // Clipping a planar n-gon against a plane will remove at least 1 vertex and introduces 2 at
  71. // the new edge (or less in degenerate cases). As such, we can say that each clipping plane
  72. // introduces at most 1 new vertex to the polygon. Since we start with a triangle and have a
  73. // fixed 6 clipping planes, the maximum number of vertices of the clipped polygon is 3 + 6 = 9.
  74. static const size_t MAX_VERTICES = 9;
  75. static_vector<OutputVertex, MAX_VERTICES> buffer_a = { v0, v1, v2 };
  76. static_vector<OutputVertex, MAX_VERTICES> buffer_b;
  77. auto* output_list = &buffer_a;
  78. auto* input_list = &buffer_b;
  79. // NOTE: We clip against a w=epsilon plane to guarantee that the output has a positive w value.
  80. // TODO: Not sure if this is a valid approach. Also should probably instead use the smallest
  81. // epsilon possible within float24 accuracy.
  82. static const float24 EPSILON = float24::FromFloat32(0.00001f);
  83. static const float24 f0 = float24::FromFloat32(0.0);
  84. static const float24 f1 = float24::FromFloat32(1.0);
  85. static const std::array<ClippingEdge, 7> clipping_edges = {{
  86. { Math::MakeVec( f1, f0, f0, -f1) }, // x = +w
  87. { Math::MakeVec(-f1, f0, f0, -f1) }, // x = -w
  88. { Math::MakeVec( f0, f1, f0, -f1) }, // y = +w
  89. { Math::MakeVec( f0, -f1, f0, -f1) }, // y = -w
  90. { Math::MakeVec( f0, f0, f1, f0) }, // z = 0
  91. { Math::MakeVec( f0, f0, -f1, -f1) }, // z = -w
  92. { Math::MakeVec( f0, f0, f0, -f1), Math::Vec4<float24>(f0, f0, f0, EPSILON) }, // w = EPSILON
  93. }};
  94. // TODO: If one vertex lies outside one of the depth clipping planes, some platforms (e.g. Wii)
  95. // drop the whole primitive instead of clipping the primitive properly. We should test if
  96. // this happens on the 3DS, too.
  97. // Simple implementation of the Sutherland-Hodgman clipping algorithm.
  98. // TODO: Make this less inefficient (currently lots of useless buffering overhead happens here)
  99. for (auto edge : clipping_edges) {
  100. std::swap(input_list, output_list);
  101. output_list->clear();
  102. const OutputVertex* reference_vertex = &input_list->back();
  103. for (const auto& vertex : *input_list) {
  104. // NOTE: This algorithm changes vertex order in some cases!
  105. if (edge.IsInside(vertex)) {
  106. if (edge.IsOutSide(*reference_vertex)) {
  107. output_list->push_back(edge.GetIntersection(vertex, *reference_vertex));
  108. }
  109. output_list->push_back(vertex);
  110. } else if (edge.IsInside(*reference_vertex)) {
  111. output_list->push_back(edge.GetIntersection(vertex, *reference_vertex));
  112. }
  113. reference_vertex = &vertex;
  114. }
  115. // Need to have at least a full triangle to continue...
  116. if (output_list->size() < 3)
  117. return;
  118. }
  119. InitScreenCoordinates((*output_list)[0]);
  120. InitScreenCoordinates((*output_list)[1]);
  121. for (size_t i = 0; i < output_list->size() - 2; i ++) {
  122. OutputVertex& vtx0 = (*output_list)[0];
  123. OutputVertex& vtx1 = (*output_list)[i+1];
  124. OutputVertex& vtx2 = (*output_list)[i+2];
  125. InitScreenCoordinates(vtx2);
  126. LOG_TRACE(Render_Software,
  127. "Triangle %lu/%lu at position (%.3f, %.3f, %.3f, %.3f), "
  128. "(%.3f, %.3f, %.3f, %.3f), (%.3f, %.3f, %.3f, %.3f) and "
  129. "screen position (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f)",
  130. i + 1, output_list->size() - 2,
  131. vtx0.pos.x.ToFloat32(), vtx0.pos.y.ToFloat32(), vtx0.pos.z.ToFloat32(), vtx0.pos.w.ToFloat32(),
  132. vtx1.pos.x.ToFloat32(), vtx1.pos.y.ToFloat32(), vtx1.pos.z.ToFloat32(), vtx1.pos.w.ToFloat32(),
  133. vtx2.pos.x.ToFloat32(), vtx2.pos.y.ToFloat32(), vtx2.pos.z.ToFloat32(), vtx2.pos.w.ToFloat32(),
  134. vtx0.screenpos.x.ToFloat32(), vtx0.screenpos.y.ToFloat32(), vtx0.screenpos.z.ToFloat32(),
  135. vtx1.screenpos.x.ToFloat32(), vtx1.screenpos.y.ToFloat32(), vtx1.screenpos.z.ToFloat32(),
  136. vtx2.screenpos.x.ToFloat32(), vtx2.screenpos.y.ToFloat32(), vtx2.screenpos.z.ToFloat32());
  137. Rasterizer::ProcessTriangle(vtx0, vtx1, vtx2);
  138. }
  139. }
  140. } // namespace
  141. } // namespace