Path: blob/master/thirdparty/meshoptimizer/rasterizer.cpp
9903 views
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details1#include "meshoptimizer.h"23#include <assert.h>4#include <float.h>5#include <string.h>67// This work is based on:8// Nicolas Capens. Advanced Rasterization. 20049namespace meshopt10{1112const int kViewport = 256;1314struct OverdrawBuffer15{16float z[kViewport][kViewport][2];17unsigned int overdraw[kViewport][kViewport][2];18};1920static float computeDepthGradients(float& dzdx, float& dzdy, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)21{22// z2 = z1 + dzdx * (x2 - x1) + dzdy * (y2 - y1)23// z3 = z1 + dzdx * (x3 - x1) + dzdy * (y3 - y1)24// (x2-x1 y2-y1)(dzdx) = (z2-z1)25// (x3-x1 y3-y1)(dzdy) (z3-z1)26// we'll solve it with Cramer's rule27float det = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);28float invdet = (det == 0) ? 0 : 1 / det;2930dzdx = ((z2 - z1) * (y3 - y1) - (y2 - y1) * (z3 - z1)) * invdet;31dzdy = ((x2 - x1) * (z3 - z1) - (z2 - z1) * (x3 - x1)) * invdet;3233return det;34}3536// half-space fixed point triangle rasterizer37static void rasterize(OverdrawBuffer* buffer, float v1x, float v1y, float v1z, float v2x, float v2y, float v2z, float v3x, float v3y, float v3z)38{39// compute depth gradients40float DZx, DZy;41float det = computeDepthGradients(DZx, DZy, v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z);42int sign = det > 0;4344// flip backfacing triangles to simplify rasterization logic45if (sign)46{47// flipping v2 & v3 preserves depth gradients since they're based on v1; only v1z is used below48float t;49t = v2x, v2x = v3x, v3x = t;50t = v2y, v2y = v3y, v3y = t;5152// flip depth since we rasterize backfacing triangles to second buffer with reverse Z; only v1z is used below53v1z = kViewport - v1z;54DZx = -DZx;55DZy = -DZy;56}5758// coordinates, 28.4 fixed point59int X1 = int(16.0f * v1x + 0.5f);60int X2 = int(16.0f * v2x + 0.5f);61int X3 = int(16.0f * v3x + 0.5f);6263int Y1 = int(16.0f * v1y + 0.5f);64int Y2 = int(16.0f * v2y + 0.5f);65int Y3 = int(16.0f * v3y + 0.5f);6667// bounding rectangle, clipped against viewport68// since we rasterize pixels with covered centers, min >0.5 should round up69// as for max, due to top-left filling convention we will never rasterize right/bottom edges70// so max >= 0.5 should round down for inclusive bounds, and up for exclusive (in our case)71int minx = X1 < X2 ? X1 : X2;72minx = minx < X3 ? minx : X3;73minx = (minx + 7) >> 4;74minx = minx < 0 ? 0 : minx;7576int miny = Y1 < Y2 ? Y1 : Y2;77miny = miny < Y3 ? miny : Y3;78miny = (miny + 7) >> 4;79miny = miny < 0 ? 0 : miny;8081int maxx = X1 > X2 ? X1 : X2;82maxx = maxx > X3 ? maxx : X3;83maxx = (maxx + 7) >> 4;84maxx = maxx > kViewport ? kViewport : maxx;8586int maxy = Y1 > Y2 ? Y1 : Y2;87maxy = maxy > Y3 ? maxy : Y3;88maxy = (maxy + 7) >> 4;89maxy = maxy > kViewport ? kViewport : maxy;9091// deltas, 28.4 fixed point92int DX12 = X1 - X2;93int DX23 = X2 - X3;94int DX31 = X3 - X1;9596int DY12 = Y1 - Y2;97int DY23 = Y2 - Y3;98int DY31 = Y3 - Y1;99100// fill convention correction101int TL1 = DY12 < 0 || (DY12 == 0 && DX12 > 0);102int TL2 = DY23 < 0 || (DY23 == 0 && DX23 > 0);103int TL3 = DY31 < 0 || (DY31 == 0 && DX31 > 0);104105// half edge equations, 24.8 fixed point106// note that we offset minx/miny by half pixel since we want to rasterize pixels with covered centers107int FX = (minx << 4) + 8;108int FY = (miny << 4) + 8;109int CY1 = DX12 * (FY - Y1) - DY12 * (FX - X1) + TL1 - 1;110int CY2 = DX23 * (FY - Y2) - DY23 * (FX - X2) + TL2 - 1;111int CY3 = DX31 * (FY - Y3) - DY31 * (FX - X3) + TL3 - 1;112float ZY = v1z + (DZx * float(FX - X1) + DZy * float(FY - Y1)) * (1 / 16.f);113114for (int y = miny; y < maxy; y++)115{116int CX1 = CY1;117int CX2 = CY2;118int CX3 = CY3;119float ZX = ZY;120121for (int x = minx; x < maxx; x++)122{123// check if all CXn are non-negative124if ((CX1 | CX2 | CX3) >= 0)125{126if (ZX >= buffer->z[y][x][sign])127{128buffer->z[y][x][sign] = ZX;129buffer->overdraw[y][x][sign]++;130}131}132133// signed left shift is UB for negative numbers so use unsigned-signed casts134CX1 -= int(unsigned(DY12) << 4);135CX2 -= int(unsigned(DY23) << 4);136CX3 -= int(unsigned(DY31) << 4);137ZX += DZx;138}139140// signed left shift is UB for negative numbers so use unsigned-signed casts141CY1 += int(unsigned(DX12) << 4);142CY2 += int(unsigned(DX23) << 4);143CY3 += int(unsigned(DX31) << 4);144ZY += DZy;145}146}147148static 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)149{150size_t vertex_stride_float = vertex_positions_stride / sizeof(float);151152float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};153float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};154155for (size_t i = 0; i < vertex_count; ++i)156{157const float* v = vertex_positions + i * vertex_stride_float;158159for (int j = 0; j < 3; ++j)160{161float vj = v[j];162163minv[j] = minv[j] > vj ? vj : minv[j];164maxv[j] = maxv[j] < vj ? vj : maxv[j];165}166}167168float extent = 0.f;169170extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);171extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);172extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);173174float scale = kViewport / extent;175176for (size_t i = 0; i < index_count; ++i)177{178unsigned int index = indices[i];179assert(index < vertex_count);180181const float* v = vertex_positions + index * vertex_stride_float;182183triangles[i * 3 + 0] = (v[0] - minv[0]) * scale;184triangles[i * 3 + 1] = (v[1] - minv[1]) * scale;185triangles[i * 3 + 2] = (v[2] - minv[2]) * scale;186}187188return extent;189}190191static void rasterizeTriangles(OverdrawBuffer* buffer, const float* triangles, size_t index_count, int axis)192{193for (size_t i = 0; i < index_count; i += 3)194{195const float* vn0 = &triangles[3 * (i + 0)];196const float* vn1 = &triangles[3 * (i + 1)];197const float* vn2 = &triangles[3 * (i + 2)];198199switch (axis)200{201case 0:202rasterize(buffer, vn0[2], vn0[1], vn0[0], vn1[2], vn1[1], vn1[0], vn2[2], vn2[1], vn2[0]);203break;204case 1:205rasterize(buffer, vn0[0], vn0[2], vn0[1], vn1[0], vn1[2], vn1[1], vn2[0], vn2[2], vn2[1]);206break;207case 2:208rasterize(buffer, vn0[1], vn0[0], vn0[2], vn1[1], vn1[0], vn1[2], vn2[1], vn2[0], vn2[2]);209break;210}211}212}213214} // namespace meshopt215216meshopt_OverdrawStatistics meshopt_analyzeOverdraw(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)217{218using namespace meshopt;219220assert(index_count % 3 == 0);221assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);222assert(vertex_positions_stride % sizeof(float) == 0);223224meshopt_Allocator allocator;225226meshopt_OverdrawStatistics result = {};227228float* triangles = allocator.allocate<float>(index_count * 3);229transformTriangles(triangles, indices, index_count, vertex_positions, vertex_count, vertex_positions_stride);230231OverdrawBuffer* buffer = allocator.allocate<OverdrawBuffer>(1);232233for (int axis = 0; axis < 3; ++axis)234{235memset(buffer, 0, sizeof(OverdrawBuffer));236rasterizeTriangles(buffer, triangles, index_count, axis);237238for (int y = 0; y < kViewport; ++y)239for (int x = 0; x < kViewport; ++x)240for (int s = 0; s < 2; ++s)241{242unsigned int overdraw = buffer->overdraw[y][x][s];243244result.pixels_covered += overdraw > 0;245result.pixels_shaded += overdraw;246}247}248249result.overdraw = result.pixels_covered ? float(result.pixels_shaded) / float(result.pixels_covered) : 0.f;250251return result;252}253254meshopt_CoverageStatistics meshopt_analyzeCoverage(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)255{256using namespace meshopt;257258assert(index_count % 3 == 0);259assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);260assert(vertex_positions_stride % sizeof(float) == 0);261262meshopt_Allocator allocator;263264meshopt_CoverageStatistics result = {};265266float* triangles = allocator.allocate<float>(index_count * 3);267float extent = transformTriangles(triangles, indices, index_count, vertex_positions, vertex_count, vertex_positions_stride);268269OverdrawBuffer* buffer = allocator.allocate<OverdrawBuffer>(1);270271for (int axis = 0; axis < 3; ++axis)272{273memset(buffer, 0, sizeof(OverdrawBuffer));274rasterizeTriangles(buffer, triangles, index_count, axis);275276unsigned int covered = 0;277278for (int y = 0; y < kViewport; ++y)279for (int x = 0; x < kViewport; ++x)280covered += (buffer->overdraw[y][x][0] | buffer->overdraw[y][x][1]) > 0;281282result.coverage[axis] = float(covered) / float(kViewport * kViewport);283}284285result.extent = extent;286287return result;288}289290291