overdrawanalyzer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
  2. #include "meshoptimizer.h"
  3. #include <assert.h>
  4. #include <float.h>
  5. #include <string.h>
  6. // This work is based on:
  7. // Nicolas Capens. Advanced Rasterization. 2004
  8. namespace meshopt
  9. {
  10. const int kViewport = 256;
  11. struct OverdrawBuffer
  12. {
  13. float z[kViewport][kViewport][2];
  14. unsigned int overdraw[kViewport][kViewport][2];
  15. };
  16. #ifndef min
  17. #define min(a, b) ((a) < (b) ? (a) : (b))
  18. #endif
  19. #ifndef max
  20. #define max(a, b) ((a) > (b) ? (a) : (b))
  21. #endif
  22. static float computeDepthGradients(float& dzdx, float& dzdy, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
  23. {
  24. // z2 = z1 + dzdx * (x2 - x1) + dzdy * (y2 - y1)
  25. // z3 = z1 + dzdx * (x3 - x1) + dzdy * (y3 - y1)
  26. // (x2-x1 y2-y1)(dzdx) = (z2-z1)
  27. // (x3-x1 y3-y1)(dzdy) (z3-z1)
  28. // we'll solve it with Cramer's rule
  29. float det = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
  30. float invdet = (det == 0) ? 0 : 1 / det;
  31. dzdx = (z2 - z1) * (y3 - y1) - (y2 - y1) * (z3 - z1) * invdet;
  32. dzdy = (x2 - x1) * (z3 - z1) - (z2 - z1) * (x3 - x1) * invdet;
  33. return det;
  34. }
  35. // half-space fixed point triangle rasterizer
  36. static void rasterize(OverdrawBuffer* buffer, float v1x, float v1y, float v1z, float v2x, float v2y, float v2z, float v3x, float v3y, float v3z)
  37. {
  38. // compute depth gradients
  39. float DZx, DZy;
  40. float det = computeDepthGradients(DZx, DZy, v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z);
  41. int sign = det > 0;
  42. // flip backfacing triangles to simplify rasterization logic
  43. if (sign)
  44. {
  45. // flipping v2 & v3 preserves depth gradients since they're based on v1; only v1z is used below
  46. float t;
  47. t = v2x, v2x = v3x, v3x = t;
  48. t = v2y, v2y = v3y, v3y = t;
  49. // flip depth since we rasterize backfacing triangles to second buffer with reverse Z; only v1z is used below
  50. v1z = kViewport - v1z;
  51. DZx = -DZx;
  52. DZy = -DZy;
  53. }
  54. // coordinates, 28.4 fixed point
  55. int X1 = int(16.0f * v1x + 0.5f);
  56. int X2 = int(16.0f * v2x + 0.5f);
  57. int X3 = int(16.0f * v3x + 0.5f);
  58. int Y1 = int(16.0f * v1y + 0.5f);
  59. int Y2 = int(16.0f * v2y + 0.5f);
  60. int Y3 = int(16.0f * v3y + 0.5f);
  61. // bounding rectangle, clipped against viewport
  62. // since we rasterize pixels with covered centers, min >0.5 should round up
  63. // as for max, due to top-left filling convention we will never rasterize right/bottom edges
  64. // so max >= 0.5 should round down
  65. int minx = max((min(X1, min(X2, X3)) + 7) >> 4, 0);
  66. int maxx = min((max(X1, max(X2, X3)) + 7) >> 4, kViewport);
  67. int miny = max((min(Y1, min(Y2, Y3)) + 7) >> 4, 0);
  68. int maxy = min((max(Y1, max(Y2, Y3)) + 7) >> 4, kViewport);
  69. // deltas, 28.4 fixed point
  70. int DX12 = X1 - X2;
  71. int DX23 = X2 - X3;
  72. int DX31 = X3 - X1;
  73. int DY12 = Y1 - Y2;
  74. int DY23 = Y2 - Y3;
  75. int DY31 = Y3 - Y1;
  76. // fill convention correction
  77. int TL1 = DY12 < 0 || (DY12 == 0 && DX12 > 0);
  78. int TL2 = DY23 < 0 || (DY23 == 0 && DX23 > 0);
  79. int TL3 = DY31 < 0 || (DY31 == 0 && DX31 > 0);
  80. // half edge equations, 24.8 fixed point
  81. // note that we offset minx/miny by half pixel since we want to rasterize pixels with covered centers
  82. int FX = (minx << 4) + 8;
  83. int FY = (miny << 4) + 8;
  84. int CY1 = DX12 * (FY - Y1) - DY12 * (FX - X1) + TL1 - 1;
  85. int CY2 = DX23 * (FY - Y2) - DY23 * (FX - X2) + TL2 - 1;
  86. int CY3 = DX31 * (FY - Y3) - DY31 * (FX - X3) + TL3 - 1;
  87. float ZY = v1z + (DZx * float(FX - X1) + DZy * float(FY - Y1)) * (1 / 16.f);
  88. for (int y = miny; y < maxy; y++)
  89. {
  90. int CX1 = CY1;
  91. int CX2 = CY2;
  92. int CX3 = CY3;
  93. float ZX = ZY;
  94. for (int x = minx; x < maxx; x++)
  95. {
  96. // check if all CXn are non-negative
  97. if ((CX1 | CX2 | CX3) >= 0)
  98. {
  99. if (ZX >= buffer->z[y][x][sign])
  100. {
  101. buffer->z[y][x][sign] = ZX;
  102. buffer->overdraw[y][x][sign]++;
  103. }
  104. }
  105. // signed left shift is UB for negative numbers so use unsigned-signed casts
  106. CX1 -= int(unsigned(DY12) << 4);
  107. CX2 -= int(unsigned(DY23) << 4);
  108. CX3 -= int(unsigned(DY31) << 4);
  109. ZX += DZx;
  110. }
  111. // signed left shift is UB for negative numbers so use unsigned-signed casts
  112. CY1 += int(unsigned(DX12) << 4);
  113. CY2 += int(unsigned(DX23) << 4);
  114. CY3 += int(unsigned(DX31) << 4);
  115. ZY += DZy;
  116. }
  117. }
  118. } // namespace meshopt
  119. meshopt_OverdrawStatistics meshopt_analyzeOverdraw(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  120. {
  121. using namespace meshopt;
  122. assert(index_count % 3 == 0);
  123. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  124. assert(vertex_positions_stride % sizeof(float) == 0);
  125. meshopt_Allocator allocator;
  126. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  127. meshopt_OverdrawStatistics result = {};
  128. float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
  129. float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
  130. for (size_t i = 0; i < vertex_count; ++i)
  131. {
  132. const float* v = vertex_positions + i * vertex_stride_float;
  133. for (int j = 0; j < 3; ++j)
  134. {
  135. minv[j] = min(minv[j], v[j]);
  136. maxv[j] = max(maxv[j], v[j]);
  137. }
  138. }
  139. float extent = max(maxv[0] - minv[0], max(maxv[1] - minv[1], maxv[2] - minv[2]));
  140. float scale = kViewport / extent;
  141. float* triangles = allocator.allocate<float>(index_count * 3);
  142. for (size_t i = 0; i < index_count; ++i)
  143. {
  144. unsigned int index = indices[i];
  145. assert(index < vertex_count);
  146. const float* v = vertex_positions + index * vertex_stride_float;
  147. triangles[i * 3 + 0] = (v[0] - minv[0]) * scale;
  148. triangles[i * 3 + 1] = (v[1] - minv[1]) * scale;
  149. triangles[i * 3 + 2] = (v[2] - minv[2]) * scale;
  150. }
  151. OverdrawBuffer* buffer = allocator.allocate<OverdrawBuffer>(1);
  152. for (int axis = 0; axis < 3; ++axis)
  153. {
  154. memset(buffer, 0, sizeof(OverdrawBuffer));
  155. for (size_t i = 0; i < index_count; i += 3)
  156. {
  157. const float* vn0 = &triangles[3 * (i + 0)];
  158. const float* vn1 = &triangles[3 * (i + 1)];
  159. const float* vn2 = &triangles[3 * (i + 2)];
  160. switch (axis)
  161. {
  162. case 0:
  163. rasterize(buffer, vn0[2], vn0[1], vn0[0], vn1[2], vn1[1], vn1[0], vn2[2], vn2[1], vn2[0]);
  164. break;
  165. case 1:
  166. rasterize(buffer, vn0[0], vn0[2], vn0[1], vn1[0], vn1[2], vn1[1], vn2[0], vn2[2], vn2[1]);
  167. break;
  168. case 2:
  169. rasterize(buffer, vn0[1], vn0[0], vn0[2], vn1[1], vn1[0], vn1[2], vn2[1], vn2[0], vn2[2]);
  170. break;
  171. }
  172. }
  173. for (int y = 0; y < kViewport; ++y)
  174. for (int x = 0; x < kViewport; ++x)
  175. for (int s = 0; s < 2; ++s)
  176. {
  177. unsigned int overdraw = buffer->overdraw[y][x][s];
  178. result.pixels_covered += overdraw > 0;
  179. result.pixels_shaded += overdraw;
  180. }
  181. }
  182. result.overdraw = result.pixels_covered ? float(result.pixels_shaded) / float(result.pixels_covered) : 0.f;
  183. return result;
  184. }