rasterizer.cpp 8.5 KB

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