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