clipper.cpp 7.3 KB

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