Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Renderer: unchanged blocks aren't re-sent (paint and mask dabs send only colours / mask) and keep their culling bounds; curvature stored square-root companded over its whole range; culling needs a positive signed volume
islandmeddce39d24a79e
diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cppindex c708dc05..08043ae0 100644--- a/src/render/Clusters.cpp+++ b/src/render/Clusters.cpp@@ -74,8 +74,9 @@ bool packVertex(const PositionGrid& grid, const glm::vec3& p, const glm::vec3& nif (a.z < 0.0f)e = glm::vec2((1.0f - std::abs(a.y)) * (a.x >= 0.0f ? 1.0f : -1.0f), (1.0f - std::abs(a.x)) * (a.y >= 0.0f ? 1.0f : -1.0f));}- const float c = std::isfinite(curvature) ? curvature / kCurvatureRange : 0.0f;- out.normal = snorm(e.x, 11) | (snorm(e.y, 11) << 11) | (snorm(c, 10) << 22);+ const float c = std::isfinite(curvature) ? std::clamp(curvature / kCurvatureRange, -1.0f, 1.0f) : 0.0f;+ const float companded = c < 0.0f ? -std::sqrt(-c) : std::sqrt(c);+ out.normal = snorm(e.x, 11) | (snorm(e.y, 11) << 11) | (snorm(companded, 10) << 22);return inside;}@@ -94,7 +95,10 @@ glm::vec3 unpackNormal(const PackedVertex& v) {return glm::normalize(n);}-float unpackCurvature(const PackedVertex& v) { return unsnorm(v.normal >> 22, 10) * kCurvatureRange; }+float unpackCurvature(const PackedVertex& v) {+ const float q = unsnorm(v.normal >> 22, 10);+ return q * std::abs(q) * kCurvatureRange;+}uint32_t packColor(const glm::vec3& c) {static const std::vector<uint8_t> table = [] {@@ -123,6 +127,8 @@ ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_tbox.expand(a);box.expand(P[tri.y]);box.expand(P[tri.z]);+ const glm::dvec3 da(a), db(P[tri.y]), dc(P[tri.z]);+ b.volume += glm::dot(da, glm::cross(db, dc));const glm::vec3 c = glm::cross(e1, e2);const float l2 = glm::dot(c, c);if (l2 > 0.0f && std::isfinite(l2)) {diff --git a/src/render/Clusters.h b/src/render/Clusters.hindex 07187dc2..c07fcd1f 100644--- a/src/render/Clusters.h+++ b/src/render/Clusters.h@@ -6,7 +6,7 @@// - The compact vertex the GPU keeps (12 bytes, was 28): the position on a 21-bit grid over the mesh's bounds// (with a margin, so sculpting rarely leaves it; one grid for the whole mesh, so a vertex copied into two// clusters lands on exactly the same point and seams can't crack), an octahedral normal (2 x 11 bits) and the-// curvature the shading uses for cavities (10 bits). The shader decodes it the same way (Renderer.cpp).+// curvature the shading uses for cavities (10 bits, square-root companded). The shader decodes it the same way (Renderer.cpp).// - Culling per cluster (core/UploadOrder.h): outside the view, or every triangle facing away. The second only// holds for a closed mesh seen from outside (closedOriented, and nothing cut by the near plane): its back// faces are then always behind front faces. Open meshes, hidden triangles and views from inside draw them.@@ -42,7 +42,10 @@ bool packVertex(const PositionGrid& grid, const glm::vec3& p, const glm::vec3& nglm::vec3 unpackPosition(const PositionGrid& grid, const PackedVertex& v);glm::vec3 unpackNormal(const PackedVertex& v);float unpackCurvature(const PackedVertex& v);-constexpr float kCurvatureRange = 0.25f; // stored range +-; the shading saturates at 0.16 / 0.2+// Curvature (Renderer.cpp: neighbour average over mean edge length, so within +-1) is stored as+// sign(c) sqrt(|c|): finest near 0, where the cavity shading ramps (to 0.16 and 0.2), and the whole range kept,+// since coarse meshes interpolate it across big triangles.+constexpr float kCurvatureRange = 1.0f;// Polypaint colours in 8 bits per channel, gamma 2.2 encoded so dark colours keep their steps (the shader// decodes with pow 2.2). A table lookup, it is called for every vertex of an upload.@@ -56,6 +59,7 @@ struct ClusterBounds {float radius = -1.0f; // < 0: emptyglm::vec3 axis{0.0f, 0.0f, 1.0f};float coneAngle = -1.0f;+ double volume = 0.0; // six times the signed volume its triangles add to a closed mesh's (outward winding: > 0)};ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t cluster);@@ -70,7 +74,9 @@ bool clusterProxy(const Mesh& mesh, const UploadOrder& order, uint32_t c, size_tClusterBounds& bounds);// Every directed edge a->b has as many b->a: the mesh is the closed, consistently wound boundary of a volume-// (it may intersect itself). Then from outside, every back face lies behind a front face.+// (it may intersect itself). Wound outwards (a positive signed volume: the sum of ClusterBounds::volume), every+// back face then lies behind a front face from outside. (Strictly: where no part of the surface is turned+// inside out, a lobe pushed through the rest; there a back face can show.)bool closedOriented(const Mesh& mesh);// One frame's culling: the view's frustum planes, the eye, and whether back-facing clusters may go.diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 2a7c6ec1..a525876a 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -20,7 +20,7 @@ namespace sf {namespace {// Mesh vertices are PackedVertex (render/Clusters.h): the position on the slot's grid (21 bits per axis in two-// words), an octahedral normal and the curvature in a third. Decoded here exactly as Clusters.cpp does.+// words), an octahedral normal and the (companded) curvature in a third. Decoded here exactly as Clusters.cpp does.// Compiled per feature set: PAINT (polypaint colour and mask attributes), PRIM (per-triangle data: the first// drawn triangle of the vertex's cluster, from a per-block table indexed by gl_VertexID, or uPrimBase when the// clusters are drawn one by one), TEXTURED and NORMALMAP (with a geometry shader, below).@@ -53,7 +53,8 @@ vec3 decodeNormal(uint a) {void main() {uvec3 q = uvec3(aPos.x & 0x1FFFFFu, (aPos.x >> 21) | ((aPos.y & 0x3FFu) << 11), (aPos.y >> 10) & 0x1FFFFFu);vec4 p = uView * vec4(uGridOrigin + vec3(q) * uGridStep, 1.0);- vNormalCurv = vec4(mat3(uView) * decodeNormal(aNormal), float(int(aNormal >> 22) - 512) / 511.0 * 0.25);+ float curvature = float(int(aNormal >> 22) - 512) / 511.0; // sign(c) sqrt(|c|), render/Clusters.h+ vNormalCurv = vec4(mat3(uView) * decodeNormal(aNormal), curvature * abs(curvature));#ifdef PAINTvPaint = vec4(pow(aColor.rgb, vec3(2.2)), aMask);#endif@@ -762,10 +763,11 @@ using Runs = std::vector<std::pair<uint32_t, uint32_t>>;// Uploads the GPU index ranges `runs` of one vertex array (`allocate` sizes it to `count` elements first):// fill(begin, end, dst) writes those elements, in GPU order, to dst. The runs are cut into pieces of a few// thousand elements filled in parallel (a dab's runs are short, so per run would be one thread), batched-// through `staging` so a full upload never copies the whole array at once.+// through `staging` so a full upload never copies the whole array at once. Pieces start on block boundaries.+// With `changed` (a flag per UploadOrder block, set by fill), only the blocks flagged are sent.template <class Fill>void uploadGathered(std::vector<uint8_t>& staging, GLuint buffer, size_t elementSize, size_t count, bool allocate,- const Runs& runs, const Fill& fill) {+ const Runs& runs, const Fill& fill, const std::vector<uint8_t>* changed = nullptr) {constexpr uint32_t kPiece = 2048, kBatch = 1u << 18;gl::BindBuffer(GL_ARRAY_BUFFER, buffer);if (allocate) gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(count * elementSize), nullptr, GL_DYNAMIC_DRAW);@@ -779,14 +781,27 @@ void uploadGathered(std::vector<uint8_t>& staging, GLuint buffer, size_t elementpoolFor(pieces.size(), [&](size_t i0, size_t i1) {for (size_t i = i0; i < i1; ++i) fill(pieces[i].begin, pieces[i].end, staging.data() + pieces[i].offset);}, 1, 2);- // One upload per stretch of adjacent pieces.- for (size_t i = 0; i < pieces.size();) {- size_t j = i + 1;- while (j < pieces.size() && pieces[j].begin == pieces[j - 1].end) ++j;- gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(pieces[i].begin) * elementSize),- GLsizeiptr(size_t(pieces[j - 1].end - pieces[i].begin) * elementSize), staging.data() + pieces[i].offset);- i = j;- }+ // One upload per stretch of adjacent (changed) elements; adjacent pieces are adjacent in staging too.+ uint32_t from = 0, to = 0;+ size_t fromOffset = 0;+ auto send = [&]() {+ if (to > from)+ gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(from) * elementSize), GLsizeiptr(size_t(to - from) * elementSize),+ staging.data() + fromOffset);+ from = to = 0;+ };+ for (const Piece& p : pieces)+ for (uint32_t b = p.begin; b < p.end; b = std::min(p.end, (b & ~(UploadOrder::kBlock - 1)) + UploadOrder::kBlock)) {+ const uint32_t e = std::min(p.end, (b & ~(UploadOrder::kBlock - 1)) + UploadOrder::kBlock);+ if (changed && !(*changed)[b >> UploadOrder::kBlockShift]) continue;+ if (b != to || to == from) {+ send();+ from = b;+ fromOffset = p.offset + size_t(b - p.begin) * elementSize;+ }+ to = e;+ }+ send();pieces.clear();};// A whole array: written straight into the mapped buffer, in parallel (no staging copy).@@ -884,29 +899,59 @@ void Renderer::syncScene(const std::vector<SceneMesh>& scene) {// the position grid (sculpting outwards) gets the mesh a new grid and every vertex re-sent.void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {const size_t ns = s.order.size();+ const size_t nb = (ns + UploadOrder::kBlock - 1) / UploadOrder::kBlock;const std::span<const uint32_t> order = s.order.fromGpu();std::atomic<bool> outside{false};const bool normals = mesh.normals.size() == mesh.vertexCount();+ // Per block, hashes of what it last sent (positions; normals and curvature): a block a dab marked but didn't+ // change (paint, mask) isn't sent again, and a cluster whose positions didn't move keeps its bounds and proxy.+ s.blockHash.resize(nb * 2, 0);+ blockChanged_.assign(nb, 0);+ blockMoved_.assign(nb, 0);+ bool everything = full;auto fill = [&](uint32_t b, uint32_t e, void* dst) {PackedVertex* out = static_cast<PackedVertex*>(dst);bool inside = true;+ uint64_t hp = 0, hn = 0;for (uint32_t i = b; i < e; ++i) {const uint32_t v = order[i];+ PackedVertex& pv = out[i - b];inside &= packVertex(s.grid, mesh.positions[v], normals ? mesh.normals[v] : glm::vec3(0.0f, 0.0f, 1.0f),- curvatureAt(mesh, v), out[i - b]);+ curvatureAt(mesh, v), pv);+ hp = (hp ^ (uint64_t(pv.pos[0]) | uint64_t(pv.pos[1]) << 32)) * 0x100000001B3ull + i;+ hn = (hn ^ pv.normal) * 0x100000001B3ull + i;+ if ((i + 1) % UploadOrder::kBlock == 0 || i + 1 == e) {+ const size_t g = i >> UploadOrder::kBlockShift;+ blockMoved_[g] = everything || s.blockHash[g * 2] != hp;+ blockChanged_[g] = blockMoved_[g] || s.blockHash[g * 2 + 1] != hn;+ s.blockHash[g * 2] = hp;+ s.blockHash[g * 2 + 1] = hn;+ hp = hn = 0;+ }}if (!inside) outside.store(true, std::memory_order_relaxed);};- uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, full, dirtyRuns_, fill);+ uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, full, dirtyRuns_, fill, &blockChanged_);if (outside.load()) {+ // A vertex left the grid: a new grid, and everything sent again.s.grid = PositionGrid::around(meshBounds(mesh));dirtyRuns_.assign(1, {0u, uint32_t(ns)});- uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, false, dirtyRuns_, fill);+ everything = true;+ uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, false, dirtyRuns_, fill, &blockChanged_);}+ // The runs whose positions moved, for the culling bounds and motion proxies.+ movedRuns_.clear();+ for (const auto& [b, e] : dirtyRuns_)+ for (uint32_t g = b >> UploadOrder::kBlockShift; g < (e + UploadOrder::kBlock - 1) >> UploadOrder::kBlockShift; ++g) {+ if (!blockMoved_[g]) continue;+ const uint32_t lo = g << UploadOrder::kBlockShift, hi = std::min(uint32_t(ns), lo + UploadOrder::kBlock);+ if (!movedRuns_.empty() && movedRuns_.back().second == lo) movedRuns_.back().second = hi;+ else movedRuns_.emplace_back(lo, hi);+ }s.bytes[0] = ns * sizeof(PackedVertex);}-// Culling bounds of the clusters dirtyRuns_ touches (every copy of a moved vertex is in a run, so every cluster+// Culling bounds of the clusters movedRuns_ touches (every copy of a moved vertex is in a run, so every cluster// with a moved triangle is), or of all.void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {const auto clusters = s.order.clusters();@@ -915,7 +960,7 @@ void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {if (all) {for (uint32_t c = 0; c < clusters.size(); ++c) cullScratch_.push_back(c);} else {- for (const auto& [b, e] : dirtyRuns_) {+ for (const auto& [b, e] : movedRuns_) {if (b >= e) continue;const uint32_t c0 = s.order.clusterOfSlot(b), c1 = s.order.clusterOfSlot(e - 1);for (uint32_t c = c0; c <= c1; ++c)@@ -931,6 +976,8 @@ void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {}}, 1, 2);s.box = AABB();+ s.volume = 0.0;+ for (const ClusterBounds& b : s.bounds) s.volume += b.volume;for (const ClusterBounds& b : s.bounds)if (b.radius >= 0.0f) {s.box.expand(b.center - glm::vec3(b.radius));@@ -1654,7 +1701,7 @@ void Renderer::cull(const MeshSlot& s, const CullView& frame, bool proxy) {proxy = proxy && s.proxyCount.size() == s.drawCount.size() && s.proxyBounds.size() == s.drawCount.size();CullView v = frame;const glm::vec3 e = v.eye;- v.backfaces = s.closed && !s.anyHidden && s.box.valid() &&+ v.backfaces = s.closed && s.volume > 0.0 && !s.anyHidden && s.box.valid() &&(e.x < s.box.min.x || e.y < s.box.min.y || e.z < s.box.min.z || e.x > s.box.max.x || e.y > s.box.max.y ||e.z > s.box.max.z);if (v.backfaces)diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex 02a99e80..89ff3134 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -157,6 +157,7 @@ private:PositionGrid grid; // the positions' quantisationstd::vector<ClusterBounds> bounds; // per cluster, for cullingAABB box; // all clusters+ double volume = 0.0; // sum of the clusters' volumes (> 0: wound outwards)bool closed = false; // closedOriented: back-facing clusters may be culled (without hidden triangles)bool anyHidden = false;std::vector<GLsizei> drawCount; // per cluster: indices drawn (hidden triangles left out)@@ -172,6 +173,7 @@ private:std::vector<uint32_t> drawnOrder; // the mesh triangle of each drawn triangle (meshes with UVs only)size_t drawnTriangles = 0;size_t vertexCapacity = 0;+ std::vector<uint64_t> blockHash; // per UploadOrder block: hashes of its packed positions, and the restsize_t bytes[8] = {}; // GPU sizes: vertices, indices, lines, colours, mask, groups, prims, uvs + tangents};MeshSlot& slot(uint32_t id); // created on first use@@ -189,6 +191,8 @@ private:void uploadIndices(MeshSlot& s, const Mesh& mesh);std::vector<std::pair<uint32_t, uint32_t>> dirtyRuns_; // scratch for sync: UploadOrder::dirtyRunsstd::vector<uint8_t> staging_; // scratch for sync: gathered vertex data+ std::vector<uint8_t> blockChanged_, blockMoved_; // scratch for uploadVertices, per block+ std::vector<std::pair<uint32_t, uint32_t>> movedRuns_; // the dirty runs whose positions movedvoid syncTexture(MeshSlot& s, Mesh& mesh);// The clusters of `s` to draw this frame: in full (drawClusters_), and with `proxy` as their motion proxy// where it is built (proxyClusters_).diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cppindex 07d862a4..9c7161e5 100644--- a/tests/test_render_clusters.cpp+++ b/tests/test_render_clusters.cpp@@ -146,7 +146,7 @@ TEST(packed_vertex_round_trip) {glm::vec3 n(u(rng), u(rng), u(rng));if (glm::length(n) < 1e-3f) continue;n = glm::normalize(n);- const float curv = u(rng) * 0.3f;+ const float curv = u(rng) * u(rng) * 1.2f; // mostly small, some beyond the +-1 keptPackedVertex v;inside = inside && packVertex(grid, p, n, curv, v);worstPos = std::max(worstPos, glm::length(unpackPosition(grid, v) - p) / grid.step);@@ -158,12 +158,19 @@ TEST(packed_vertex_round_trip) {CHECK(inside);CHECK(worstPos < 1.25f); // half a step per axis, plus float roundingCHECK(worstAngle < glm::radians(0.15f)); // 11-bit octahedral normals- CHECK(worstCurv <= kCurvatureRange / 511.0f); // 10-bit curvature+ CHECK(worstCurv <= 2.0f / 511.0f); // 10 bits, square-root companded: within 0.004, finer near 0// Exact values where it matters: zero curvature, axis normals, and the same point always packs the same.PackedVertex a, b;packVertex(grid, glm::vec3(0.1f), glm::vec3(0, 0, -1), 0.0f, a);packVertex(grid, glm::vec3(0.1f), glm::vec3(0, 0, -1), 0.0f, b);CHECK(unpackCurvature(a) == 0.0f);+ float worstSmall = 0.0f; // where the cavity shading ramps+ for (float c = -0.2f; c <= 0.2f; c += 0.0007f) {+ PackedVertex q;+ packVertex(grid, glm::vec3(0.1f), glm::vec3(0, 0, 1), c, q);+ worstSmall = std::max(worstSmall, std::abs(unpackCurvature(q) - c));+ }+ CHECK(worstSmall < 0.0011f);CHECK(glm::length(unpackNormal(a) - glm::vec3(0, 0, -1)) < 1e-6f);CHECK(a.pos[0] == b.pos[0] && a.pos[1] == b.pos[1] && a.normal == b.normal);// Outside the grid: reported (the renderer then fits a new one) and clamped.