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
Texture dabs on dense meshes paint texel tiles instead of triangles
islandmedea66b0f8ade7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cdc7cd7e..d57c65b0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -28,6 +28,11 @@ All notable changes to SculptForge are listed here. The format follows- Preferences > Performance and `perf_info` show the memory of textures and their material channels.### Changed+- Texture painting on dense meshes costs the texels a dab reaches instead of the triangles under it: the brush+ finds the texture tiles under it from the BVH and paints each texel from a cached triangle-and-barycentrics+ map, tiles in parallel. A 0.35-radius dab with X symmetry on a 21M-triangle sphere: 4K 150-360 ms -> 24-39 ms,+ 8K 240-350 ms -> 39-55 ms (M1 Pro, ranges over a busy and a quiet machine); results match the old dab within+ 1/255. Lighter meshes keep the per-triangle dab.- Project format 7 stores material channels. It is written only when a texture has them: other projects arestill saved as format 6, and every older project opens as before.diff --git a/src/core/BVH.cpp b/src/core/BVH.cppindex c2eb102d..108a7f53 100644--- a/src/core/BVH.cpp+++ b/src/core/BVH.cpp@@ -256,6 +256,25 @@ void BVH::queryTriangles(const Mesh& mesh, const glm::vec3& center, float radius}}+void BVH::forEachLeafInSphere(const glm::vec3& center, float radius,+ const std::function<void(std::span<const uint32_t>)>& fn) const {+ if (nodes_.empty()) return;+ const float r2 = radius * radius;+ std::vector<uint32_t> stack;+ stack.reserve(64);+ stack.push_back(0);+ while (!stack.empty()) {+ const Node& n = nodes_[stack.back()];+ stack.pop_back();+ if (!sphereBox(n.box, center, r2)) continue;+ if (n.count > 0) fn(std::span<const uint32_t>(triIndices_.data() + n.first, n.count));+ else if (n.first != kNoLeaf) {+ stack.push_back(n.first);+ stack.push_back(n.first + 1);+ }+ }+}+void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const {out.clear();queryLeaves_.clear();diff --git a/src/core/BVH.h b/src/core/BVH.hindex fe5110a8..1ff0c02f 100644--- a/src/core/BVH.h+++ b/src/core/BVH.h@@ -5,6 +5,7 @@#include "core/IndexBits.h"#include "core/Mesh.h"+#include <functional>#include <span>#include <vector>@@ -29,6 +30,10 @@ public:void queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const;// Appends every triangle whose bounds touch the sphere (cleared first; hidden ones included).void queryTriangles(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const;+ // Calls fn(triangles of the leaf) for every leaf whose box touches the sphere (hidden triangles included);+ // no per-triangle test, for callers that only need where the triangles are (texture painting's tiles).+ void forEachLeafInSphere(const glm::vec3& center, float radius,+ const std::function<void(std::span<const uint32_t>)>& fn) const;// Updates bounds after the given vertices moved. A few leaves grow their ancestors' bounds (conservatively,// which keeps queries correct); many leaves (a big brush) recompute their ancestors from their children, in// parallel.diff --git a/src/core/Texture.cpp b/src/core/Texture.cppindex 8138479a..087ff4aa 100644--- a/src/core/Texture.cpp+++ b/src/core/Texture.cpp@@ -11,6 +11,8 @@#include <atomic>#include <cmath>#include <cstring>+#include <limits>+#include <cstdio>#include <thread>namespace sf {@@ -799,6 +801,364 @@ TexturePainter::Tile& TexturePainter::tile(uint32_t index, const std::vector<uinreturn t;}+// --- Texel-tile dabs for dense meshes ---++namespace {++struct DabMirror {+ glm::vec3 center, viewDir, aUp, aRight;+ uint32_t stamp;+};++std::atomic<int> gDabPath{0}; // TexturePainter::Path++constexpr uint32_t kNoTri = UINT32_MAX;+constexpr size_t kTexelMapBudget = 4096; // cached texel tiles (32 KB each): 128 MB at most++uint64_t mixKey(uint64_t h, uint64_t v) { return (h ^ (v + 0x9E3779B97F4A7C15ull + (h << 6) + (h >> 2))) * 1099511628211ull; }++} // namespace++// The UV triangles binned per kTextureTile tile: each triangle in every tile its UV box (grown by the gutter)+// touches, in triangle order.+struct UvTileIndex {+ uint64_t key = 0;+ int tilesX = 0, tilesY = 0;+ std::vector<uint8_t> triTiles; // 4 per triangle: tile x0, y0, x1, y1 (x0 > x1: none); tiles per side <= 256+ std::vector<uint32_t> binStart; // tilesX * tilesY + 1+ std::vector<uint32_t> bins;+};++// One tile's texels: the triangle and barycentrics (b1, b2 as 16 bits) of each, kNoTri where no island reaches.+struct TexelMapTile {+ std::vector<uint32_t> tri;+ std::vector<uint16_t> bary;+ // Texels more than one island triangle covers (overlapping UVs, e.g. mirrored halves), in texel order: the+ // other triangles, painted after the first as the per-triangle dab would.+ struct Extra {+ uint16_t li;+ uint16_t b1, b2;+ uint32_t tri;+ };+ std::vector<Extra> extras;+ uint64_t used = 0;+};+struct TexelMapCache {+ uint64_t key = 0;+ std::vector<std::unique_ptr<TexelMapTile>> tiles;+ size_t live = 0;+ uint64_t clock = 0;+};++namespace {++uint64_t texelMapKey(const Mesh& m, const PaintTexture& t) {+ uint64_t h = mixKey(uvFingerprint(m), m.triangleCount());+ h = mixKey(h, uint64_t(t.width) << 32 | uint64_t(t.height));+ return mixKey(h, t.coverageUvs);+}++std::shared_ptr<const UvTileIndex> buildUvIndex(const Mesh& m, const PaintTexture& t, uint64_t key) {+ auto idx = std::make_shared<UvTileIndex>();+ idx->key = key;+ idx->tilesX = tileCount(t.width);+ idx->tilesY = tileCount(t.height);+ const size_t nt = m.triangleCount();+ idx->triTiles.resize(nt * 4);+ const float w = float(t.width), h = float(t.height), pad = float(kTextureGutter + 1);+ const int tx1max = idx->tilesX - 1, ty1max = idx->tilesY - 1;+ poolFor(nt, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ uint8_t* o = &idx->triTiles[i * 4];+ const glm::vec2 a = m.uvs[i * 3], c = m.uvs[i * 3 + 1], d = m.uvs[i * 3 + 2];+ const float minX = std::min({a.x, c.x, d.x}) * w - 0.5f - pad, maxX = std::max({a.x, c.x, d.x}) * w - 0.5f + pad;+ const float minY = std::min({a.y, c.y, d.y}) * h - 0.5f - pad, maxY = std::max({a.y, c.y, d.y}) * h - 0.5f + pad;+ if (!(minX <= maxX && minY <= maxY) || maxX < 0.0f || maxY < 0.0f || minX >= w || minY >= h) {+ o[0] = 1, o[1] = 0, o[2] = 0, o[3] = 0;+ continue;+ }+ o[0] = uint8_t(std::clamp(int(std::floor(minX)) / kTextureTile, 0, tx1max));+ o[1] = uint8_t(std::clamp(int(std::floor(minY)) / kTextureTile, 0, ty1max));+ o[2] = uint8_t(std::clamp(int(std::ceil(maxX)) / kTextureTile, 0, tx1max));+ o[3] = uint8_t(std::clamp(int(std::ceil(maxY)) / kTextureTile, 0, ty1max));+ }+ }, 65536);+ const size_t tiles = size_t(idx->tilesX) * size_t(idx->tilesY);+ idx->binStart.assign(tiles + 1, 0);+ for (size_t i = 0; i < nt; ++i) {+ const uint8_t* o = &idx->triTiles[i * 4];+ if (o[0] > o[2]) continue;+ for (int ty = o[1]; ty <= o[3]; ++ty)+ for (int tx = o[0]; tx <= o[2]; ++tx) ++idx->binStart[size_t(ty) * size_t(idx->tilesX) + size_t(tx) + 1];+ }+ for (size_t k = 0; k < tiles; ++k) idx->binStart[k + 1] += idx->binStart[k];+ idx->bins.resize(idx->binStart[tiles]);+ std::vector<uint32_t> fill(idx->binStart.begin(), idx->binStart.end() - 1);+ for (size_t i = 0; i < nt; ++i) {+ const uint8_t* o = &idx->triTiles[i * 4];+ if (o[0] > o[2]) continue;+ for (int ty = o[1]; ty <= o[3]; ++ty)+ for (int tx = o[0]; tx <= o[2]; ++tx) idx->bins[fill[size_t(ty) * size_t(idx->tilesX) + size_t(tx)]++] = uint32_t(i);+ }+ return idx;+}++// The texel map of tile `ti`: every island texel's triangle (exact top-left rule, as rasterizeUvTriangle), and+// every gutter texel's nearest island point within kTextureGutter texels.+std::unique_ptr<TexelMapTile> buildTexelMap(const Mesh& m, const PaintTexture& t, const UvTileIndex& idx, size_t ti) {+ auto map = std::make_unique<TexelMapTile>();+ constexpr size_t n = size_t(kTextureTile) * kTextureTile;+ map->tri.assign(n, kNoTri);+ map->bary.assign(n * 2, 0);+ std::vector<float> dist(n, std::numeric_limits<float>::max());+ const int x0 = int(ti % size_t(idx.tilesX)) * kTextureTile, y0 = int(ti / size_t(idx.tilesX)) * kTextureTile;+ const int x1 = std::min(x0 + kTextureTile, t.width) - 1, y1 = std::min(y0 + kTextureTile, t.height) - 1;+ auto put = [&](size_t li, uint32_t tri, const glm::vec3& b) {+ map->tri[li] = tri;+ map->bary[li * 2] = uint16_t(std::lround(std::clamp(b.y, 0.0f, 1.0f) * 65535.0f));+ map->bary[li * 2 + 1] = uint16_t(std::lround(std::clamp(b.z, 0.0f, 1.0f) * 65535.0f));+ };+ for (uint32_t k = idx.binStart[ti]; k < idx.binStart[ti + 1]; ++k) {+ const uint32_t tri = idx.bins[k];+ const TriSetup s(m.uvs[size_t(tri) * 3], m.uvs[size_t(tri) * 3 + 1], m.uvs[size_t(tri) * 3 + 2], t.width, t.height);+ visitTriangle(s, t.width, t.height, kTextureGutter, &t, x0, y0, x1, y1, [&](int x, int y, const glm::vec3& b, bool inside) {+ const size_t li = size_t(y - y0) * kTextureTile + size_t(x - x0);+ if (dist[li] < 0.0f) { // an island texel that already has a triangle: overlapping UVs+ if (inside)+ map->extras.push_back({uint16_t(li), uint16_t(std::lround(std::clamp(b.y, 0.0f, 1.0f) * 65535.0f)),+ uint16_t(std::lround(std::clamp(b.z, 0.0f, 1.0f) * 65535.0f)), tri});+ return;+ }+ if (inside) {+ dist[li] = -1.0f;+ put(li, tri, b);+ return;+ }+ const glm::vec2 q = s.t[0] * b.x + s.t[1] * b.y + s.t[2] * b.z, c(float(x) + 0.5f, float(y) + 0.5f);+ const float d2 = glm::dot(q - c, q - c);+ if (d2 < dist[li]) {+ dist[li] = d2;+ put(li, tri, b);+ }+ });+ }+ std::stable_sort(map->extras.begin(), map->extras.end(),+ [](const TexelMapTile::Extra& a, const TexelMapTile::Extra& b) { return a.li < b.li; });+ return map;+}++// The tiles a triangle spanning several tiles can paint under the brush sphere: the affine texel -> 3D map turns+// the sphere into an ellipse in texel space (as the per-triangle dab clips), grown by the gutter. False if none.+bool sphereTiles(const Mesh& m, uint32_t tri, int w, int h, const glm::vec3& center, float r2, int& tx0, int& ty0, int& tx1, int& ty1) {+ const TriSetup su(m.uvs[size_t(tri) * 3], m.uvs[size_t(tri) * 3 + 1], m.uvs[size_t(tri) * 3 + 2], w, h);+ if (!su.valid) return false;+ const glm::uvec3& tv = m.triangles[tri];+ const glm::vec3 P0 = m.positions[tv.x], P1 = m.positions[tv.y], P2 = m.positions[tv.z];+ const glm::vec2 e1 = su.t[1] - su.t[0], e2 = su.t[2] - su.t[0];+ const float det = e1.x * e2.y - e1.y * e2.x;+ if (std::abs(det) < 1e-12f) return false;+ const glm::vec3 q1 = P1 - P0, q2 = P2 - P0;+ const glm::vec3 Ax = (q1 * e2.y - q2 * e1.y) / det, Ay = (q2 * e1.x - q1 * e2.x) / det;+ const float mxx = glm::dot(Ax, Ax), mxy = glm::dot(Ax, Ay), myy = glm::dot(Ay, Ay);+ const float mdet = mxx * myy - mxy * mxy;+ int x0 = 0, y0 = 0, x1 = w - 1, y1 = h - 1;+ if (mdet > 1e-30f) {+ const glm::vec3 rel = center - P0;+ const float bx = glm::dot(Ax, rel), by = glm::dot(Ay, rel);+ const float dx = (myy * bx - mxy * by) / mdet, dy = (mxx * by - mxy * bx) / mdet;+ const glm::vec3 closest = P0 + Ax * dx + Ay * dy;+ const float k = r2 - glm::dot(closest - center, closest - center);+ if (k < 0.0f) return false;+ const float hx = std::sqrt(k * myy / mdet), hy = std::sqrt(k * mxx / mdet);+ const glm::vec2 c = su.t[0] + glm::vec2(dx, dy);+ x0 = int(std::floor(c.x - hx)) - kTextureGutter - 1, x1 = int(std::ceil(c.x + hx)) + kTextureGutter + 1;+ y0 = int(std::floor(c.y - hy)) - kTextureGutter - 1, y1 = int(std::ceil(c.y + hy)) + kTextureGutter + 1;+ }+ int bx0, by0, bx1, by1;+ su.bounds(w, h, kTextureGutter, bx0, by0, bx1, by1);+ x0 = std::max(x0, bx0), y0 = std::max(y0, by0), x1 = std::min(x1, bx1), y1 = std::min(y1, by1);+ if (x0 > x1 || y0 > y1) return false;+ tx0 = x0 / kTextureTile, ty0 = y0 / kTextureTile, tx1 = x1 / kTextureTile, ty1 = y1 / kTextureTile;+ return true;+}++} // namespace++void TexturePainter::setPath(Path path) { gDabPath = int(path); }++struct TexturePainter::DabContext {+ Mesh& mesh;+ const BVH& bvh;+ const DabParams& p;+ PaintTexture& tex;+ std::vector<uint8_t>& target;+ float s;+ bool useAlpha, masked, normals;+ float r, r2;+ const std::vector<DabMirror>& mirrors;+};++bool TexturePainter::useTilePath(const Mesh& mesh, PaintTexture& tex) const {+ const Path path = Path(gDabPath.load());+ if (path == Path::Triangles) return false;+ if (tileCount(tex.width) > 256 || tileCount(tex.height) > 256) return false;+ if (path == Path::Auto && mesh.triangleCount() * 64 < tex.texelCount()) return false;+ const uint64_t key = texelMapKey(mesh, tex);+ if (!tex.uvIndex || tex.uvIndex->key != key) tex.uvIndex = buildUvIndex(mesh, tex, key);+ if (!tex.texelMaps || tex.texelMaps->key != key) {+ auto cache = std::make_shared<TexelMapCache>();+ cache->key = key;+ cache->tiles.resize(size_t(tex.uvIndex->tilesX) * size_t(tex.uvIndex->tilesY));+ tex.texelMaps = std::move(cache);+ }+ return true;+}++template <class Paint>+size_t TexturePainter::dabTiles(const DabContext& c, const Paint& paint, bool composite,+ std::vector<std::pair<glm::ivec2, glm::ivec2>>& rects) {+ const UvTileIndex& idx = *c.tex.uvIndex;+ TexelMapCache& cache = *c.tex.texelMaps;+ const Mesh& mesh = c.mesh;+ const int w = c.tex.width, h = c.tex.height;+ // 1. The tiles under the brush: every tile a triangle of a BVH leaf touching the sphere can reach.+ const size_t tiles = cache.tiles.size();+ touchedFlag_.assign(tiles, 0);+ touched_.clear();+ struct Leaf {+ std::span<const uint32_t> tris;+ uint32_t mirror;+ };+ std::vector<Leaf> leaves;+ for (uint32_t mi = 0; mi < uint32_t(c.mirrors.size()); ++mi)+ c.bvh.forEachLeafInSphere(c.mirrors[mi].center, c.r, [&](std::span<const uint32_t> tris) { leaves.push_back({tris, mi}); });+ // The leaves' triangles mark their tiles on every core (a flag set from two threads is still just set).+ poolFor(leaves.size(), [&](size_t b, size_t e) {+ for (size_t k = b; k < e; ++k) {+ const glm::vec3& center = c.mirrors[leaves[k].mirror].center;+ for (uint32_t t : leaves[k].tris) {+ const uint8_t* o = &idx.triTiles[size_t(t) * 4];+ if (o[0] > o[2]) continue;+ int tx0 = o[0], ty0 = o[1], tx1 = o[2], ty1 = o[3];+ // A triangle wider than a tile (big triangles, UV seams): only the tiles the sphere can reach.+ if ((tx1 - tx0 > 1 || ty1 - ty0 > 1) && !sphereTiles(mesh, t, w, h, center, c.r2, tx0, ty0, tx1, ty1)) continue;+ for (int ty = ty0; ty <= ty1; ++ty)+ for (int tx = tx0; tx <= tx1; ++tx) {+ uint8_t& flag = touchedFlag_[size_t(ty) * size_t(idx.tilesX) + size_t(tx)];+ std::atomic_ref<uint8_t> f(flag);+ if (!f.load(std::memory_order_relaxed)) f.store(1, std::memory_order_relaxed);+ }+ }+ }+ }, 256);+ for (uint32_t i = 0; i < uint32_t(tiles); ++i)+ if (touchedFlag_[i]) touched_.push_back(i);+ if (touched_.empty()) return 0;+ // 2. Room in the texel map cache: drop the least recently used tiles this dab doesn't need.+ ++cache.clock;+ size_t missing = 0;+ for (uint32_t i : touched_) {+ if (cache.tiles[i]) cache.tiles[i]->used = cache.clock;+ else ++missing;+ }+ if (cache.live + missing > kTexelMapBudget) {+ std::vector<std::pair<uint64_t, uint32_t>> old;+ for (uint32_t i = 0; i < uint32_t(tiles); ++i)+ if (cache.tiles[i] && !touchedFlag_[i]) old.push_back({cache.tiles[i]->used, i});+ std::sort(old.begin(), old.end());+ for (const auto& [used, i] : old) {+ if (cache.live + missing <= kTexelMapBudget) break;+ cache.tiles[i].reset();+ --cache.live;+ }+ }+ cache.live += missing;+ // 3. Every texel of those tiles, tiles in parallel (each texel in one tile: the same result on any thread count).+ struct Result {+ glm::ivec2 lo{INT32_MAX}, hi{INT32_MIN};+ size_t changed = 0;+ };+ std::vector<Result> results(touched_.size());+ ThreadPool::shared().run(touched_.size(), [&](size_t job) {+ const uint32_t ti = touched_[job];+ std::unique_ptr<TexelMapTile>& slot = cache.tiles[ti];+ if (!slot) {+ slot = buildTexelMap(mesh, c.tex, idx, ti);+ slot->used = cache.clock;+ }+ const TexelMapTile& map = *slot;+ Result& res = results[job];+ const int x0 = int(ti % uint32_t(idx.tilesX)) * kTextureTile, y0 = int(ti / uint32_t(idx.tilesX)) * kTextureTile;+ const int x1 = std::min(x0 + kTextureTile, w), y1 = std::min(y0 + kTextureTile, h);+ Tile* rec = nullptr;+ uint32_t last = kNoTri;+ bool lastHidden = false;+ glm::vec3 P0{}, P1{}, P2{}, N0{}, N1{}, N2{}, faceN{};+ float m0 = 0, m1 = 0, m2 = 0;+ size_t extra = 0;+ // One texel from one of its triangles, for every mirror (each mirror writes a texel once).+ auto texel = [&](int x, int y, size_t li, uint32_t tri, uint16_t ub1, uint16_t ub2) {+ if (tri != last) {+ last = tri;+ lastHidden = mesh.triangleHidden(tri);+ const glm::uvec3& tv = mesh.triangles[tri];+ P0 = mesh.positions[tv.x], P1 = mesh.positions[tv.y], P2 = mesh.positions[tv.z];+ if (c.normals) N0 = mesh.normals[tv.x], N1 = mesh.normals[tv.y], N2 = mesh.normals[tv.z];+ faceN = glm::cross(P1 - P0, P2 - P0);+ if (c.masked) m0 = mesh.mask[tv.x], m1 = mesh.mask[tv.y], m2 = mesh.mask[tv.z];+ }+ if (lastHidden) return;+ const float b1 = float(ub1) / 65535.0f, b2 = float(ub2) / 65535.0f;+ const glm::vec3 b(1.0f - b1 - b2, b1, b2);+ const glm::vec3 P = P0 * b.x + P1 * b.y + P2 * b.z;+ for (const DabMirror& mi : c.mirrors) {+ const glm::vec3 o = P - mi.center;+ const float d2 = glm::dot(o, o);+ if (d2 >= c.r2) continue;+ const glm::vec3 N = c.normals ? N0 * b.x + N1 * b.y + N2 * b.z : faceN;+ if (glm::dot(N, mi.viewDir) > 0.0f) continue;+ const float f = c.useAlpha ? c.p.alpha->sample(glm::dot(o, mi.aRight) / c.r, glm::dot(o, mi.aUp) / c.r)+ : brushFalloff(std::sqrt(d2), c.r, c.p.focal);+ const float wgt = f * c.s * (1.0f - std::clamp(m0 * b.x + m1 * b.y + m2 * b.z, 0.0f, 1.0f));+ if (wgt <= 0.0f) continue;+ if (!rec) rec = &tile(ti, c.target, w, h, c.tex);+ Tile& t = *rec;+ if (t.stamp[li] == mi.stamp) continue;+ t.stamp[li] = mi.stamp;+ float& acc = t.coverage[li];+ acc += std::min(wgt, 1.0f) * (1.0f - acc);+ paint(t, li, x, y, acc);+ res.lo = glm::min(res.lo, glm::ivec2(x, y));+ res.hi = glm::max(res.hi, glm::ivec2(x + 1, y + 1));+ ++res.changed;+ }+ };+ for (int y = y0; y < y1; ++y)+ for (int x = x0; x < x1; ++x) {+ const size_t li = size_t(y - y0) * kTextureTile + size_t(x - x0);+ const uint32_t tri = map.tri[li];+ if (tri == kNoTri) continue;+ texel(x, y, li, tri, map.bary[li * 2], map.bary[li * 2 + 1]);+ for (; extra < map.extras.size() && map.extras[extra].li <= li; ++extra)+ if (map.extras[extra].li == li)+ texel(x, y, li, map.extras[extra].tri, map.extras[extra].b1, map.extras[extra].b2);+ }+ if (composite && res.changed && !c.tex.layers.empty() && c.tex.base.size() == c.tex.pixels.size())+ for (int y = res.lo.y; y < res.hi.y; ++y)+ for (int x = res.lo.x; x < res.hi.x; ++x) {+ const size_t i = size_t(y) * size_t(w) + size_t(x);+ compositeTexel(c.tex, i, &c.tex.pixels[i * 4]);+ }+ });+ size_t changed = 0;+ for (const Result& res : results)+ if (res.changed) {+ changed += res.changed;+ rects.push_back({res.lo, res.hi});+ }+ return changed;+}+// A dab in two passes: (1) per mirror, the triangles under the brush and the texel box each can reach, in a// fixed order; (2) the texture's 16-row bands in parallel, each visiting its triangles in that same order. A// texel lies in one band, so it sees exactly the writes it would get serially: the result doesn't depend on@@ -844,12 +1204,8 @@ size_t TexturePainter::dab(Mesh& mesh, const BVH& bvh, const DabParams& p, constconst bool normals = mesh.normals.size() == mesh.vertexCount();const float r = p.radius, r2 = r * r;- struct Mirror {- glm::vec3 center, viewDir, aUp, aRight;- uint32_t stamp;- };+ using Mirror = DabMirror;std::vector<Mirror> mirrors;- dabTris_.clear();for (const glm::vec3& m : Sculptor::mirrors(sym)) {Mirror mi;mi.center = p.center * m;@@ -860,8 +1216,31 @@ size_t TexturePainter::dab(Mesh& mesh, const BVH& bvh, const DabParams& p, constmi.aUp = glm::length(aUp) > 1e-6f ? glm::normalize(aUp) : glm::vec3(0, 1, 0);mi.aRight = glm::cross(mi.aUp, -mi.viewDir);mi.stamp = ++dab_;- const int mirror = int(mirrors.size());mirrors.push_back(mi);+ }+ // Dense meshes: texel tiles instead of triangles (dabTiles).+ if (useTilePath(mesh, tex)) {+ const DabContext ctx{mesh, bvh, p, tex, target, s, useAlpha, masked, normals, r, r2, mirrors};+ std::vector<std::pair<glm::ivec2, glm::ivec2>> rects;+ const size_t changed = dabTiles(ctx, [&](Tile& t, size_t li, int x, int y, float acc) {+ if (paintColor)+ paintTexel(&target[(size_t(y) * size_t(w) + size_t(x)) * 4], &t.original[li * 4], onLayer, color, acc, p.invert);+ for (const ChanJob& j : jobs) {+ const ChannelTile& ct = t.channels[size_t(j.c)];+ paintChannelTexel(ct.texels + li * size_t(j.tb), ct.origTexels + li * size_t(ct.origStride), j.n, j.bits,+ onLayer, j.val, j.def, acc, p.invert, j.normal);+ }+ }, paintColor, rects);+ for (const auto& [lo, hi] : rects) {+ if (!tex.layers.empty())+ for (const ChanJob& j : jobs) compositeChannel(tex, Channel(j.c), lo.x, lo.y, hi.x, hi.y);+ tex.markDirty(lo.x, lo.y, hi.x, hi.y);+ }+ return changed;+ }+ dabTris_.clear();+ for (int mirror = 0; mirror < int(mirrors.size()); ++mirror) {+ const Mirror& mi = mirrors[size_t(mirror)];bvh.queryTriangles(mesh, mi.center, r, tris_);for (uint32_t ti : tris_) {if (mesh.triangleHidden(ti)) continue;diff --git a/src/core/Texture.h b/src/core/Texture.hindex c8987969..579e4fe0 100644--- a/src/core/Texture.h+++ b/src/core/Texture.h@@ -64,6 +64,9 @@ struct TextureLayer {LayerChannels channels; // material channels (core/TextureChannels.h); [BaseColor] only says whether colour shows};+struct UvTileIndex; // (Texture.cpp)+struct TexelMapCache; // (Texture.cpp)+struct PaintTexture {int width = 0, height = 0;uint64_t serial = 0; // unique per texture object (makeTexture / copies), for GPU caches@@ -105,6 +108,14 @@ struct PaintTexture {return (coverage[i >> 3] >> (i & 7)) & 1;}+ // Texel tiles -> surface (the texture brush on dense meshes): the UV triangles binned per kTextureTile tile+ // (built once per UV layout and size, with `coverage`), and per tile, made when a brush first reaches it, the+ // triangle and barycentrics of every texel (island texels, and gutter texels with their nearest island+ // point). A dab then costs the texels it reaches, not the triangles under it. Caches: shared by copies, not+ // saved or undone.+ std::shared_ptr<const UvTileIndex> uvIndex;+ std::shared_ptr<TexelMapCache> texelMaps;+size_t texelCount() const { return size_t(width) * size_t(height); }// The array painting writes into (layer -1 = Base: `pixels` without layers).std::vector<uint8_t>& target(int layer);@@ -188,6 +199,24 @@ private:// The images material channel c paints into (the target layer's or Base), null when the stroke doesn't paint c.TileImage* channelTarget(PaintTexture& tex, int c) const;uint32_t channels_ = 1u; // what the stroke paints (channelBit; BaseColor = bit 0)++public:+ // Which dab path to use: Auto (texel tiles on dense meshes: under 64 texels per triangle), Tiles or Triangles+ // (tests compare them).+ enum class Path { Auto, Tiles, Triangles };+ static void setPath(Path path);++private:+ // Dense meshes (texels outnumber what each triangle covers): the brush finds the texture tiles under it from+ // the BVH leaves (no per-triangle work), then paints every texel of those tiles from its cached triangle and+ // barycentrics (PaintTexture::texelMaps), tiles in parallel. Costs the texels reached, not the triangles.+ struct DabContext;+ bool useTilePath(const Mesh& mesh, PaintTexture& tex) const;+ template <class Paint> // paint(tile record, texel in tile, x, y, coverage)+ size_t dabTiles(const DabContext& ctx, const Paint& paint, bool composite,+ std::vector<std::pair<glm::ivec2, glm::ivec2>>& rects);+ std::vector<uint32_t> touched_; // tiles a dab reaches+ std::vector<uint8_t> touchedFlag_; // per tile// The tile's stroke record, made on first touch (thread-safe).// Also makes the tile of every material channel the stroke paints writable.Tile& tile(uint32_t index, const std::vector<uint8_t>& target, int width, int height, PaintTexture& tex);diff --git a/tests/test_texture_channels.cpp b/tests/test_texture_channels.cppindex a4af2bb4..1c589a0e 100644--- a/tests/test_texture_channels.cpp+++ b/tests/test_texture_channels.cpp@@ -501,6 +501,7 @@ TEST(material_dense_mesh_benchmark) {auto ms = [](auto a, auto b) { return std::chrono::duration<double, std::milli>(b - a).count(); };std::printf(" tris / tex / radius: colour dab ms, 5-channel dab ms (ratio)\n");for (int level : {7, 9, 10}) {+ if (const char* only = std::getenv("SF_BENCH_DENSE_LEVEL"); only && std::atoi(only) != level) continue; // e.g. 10 = 21M onlyconst auto tb = now();// One mesh per size, shared by every measurement (a 21M copy would cost gigabytes).Document doc;@@ -516,20 +517,25 @@ TEST(material_dense_mesh_benchmark) {double(residentBytes()) / 1e9);for (int size : {4096, 8192}) {for (float radius : {0.05f, 0.35f}) {- double t[2] = {0, 0};- for (int pass = 0; pass < 2; ++pass) {+ // Pass 0: colour with the per-triangle dab (as 0.10 shipped); 1: colour with texel tiles; 2: five+ // channels with texel tiles. `cold`: the first dab of pass 1 (the UV bins and texel tiles are made).+ double t[3] = {0, 0, 0}, cold = 0;+ for (int pass = 0; pass < 3; ++pass) {+ TexturePainter::setPath(pass == 0 ? TexturePainter::Path::Triangles : TexturePainter::Path::Auto);Mesh& m = base;m.texture = makeTexture(size, size);m.paintToTexture = true;BrushMaterial mat = rustyMetal();- mat.channels = pass ? five : channelBit(Channel::BaseColor);- if (pass)+ mat.channels = pass == 2 ? five : channelBit(Channel::BaseColor);+ if (pass == 2)for (Channel c : {Channel::Roughness, Channel::Metallic, Channel::Height, Channel::AO}) setChannelEnabled(*m.texture, c, true);TexturePainter painter;CHECK(painter.begin(m, mat.channels));DabParams p = dab(glm::vec3(0, 0, 1), radius, 0.5f);p.viewDir = glm::vec3(0, 0, -1);+ const auto c0 = now();painter.dab(m, bvh, p, mat, Symmetry{true, false, false}); // warm-up (first touch of the tiles)+ if (pass == 1) cold = ms(c0, now());for (int i = 0; i < 10; ++i) {p.center = glm::normalize(glm::vec3(-0.15f + 0.03f * float(i), 0.2f, 1.0f));const auto t0 = now();@@ -540,8 +546,9 @@ TEST(material_dense_mesh_benchmark) {painter.end(rec);t[pass] /= 10.0;}- std::printf(" %5.1fM / %dK / %.2f: %7.2f ms, %7.2f ms (%.2fx)\n", double(base.triangleCount()) / 1e6, size / 1024,- radius, t[0], t[1], t[1] / t[0]);+ TexturePainter::setPath(TexturePainter::Path::Auto);+ std::printf(" %5.1fM / %dK / %.2f: triangles %7.2f ms -> tiles %6.2f ms (first dab %6.0f ms), 5 channels %6.2f ms (%.2fx)\n",+ double(base.triangleCount()) / 1e6, size / 1024, radius, t[0], t[1], cold, t[2], t[2] / t[1]);}// Memory: 5 channels, Base plus 3 layers, each with a big stroke on every channel; then one layer painted// everywhere (its tiles all dense), and export_maps of the result.@@ -590,3 +597,72 @@ TEST(material_dense_mesh_benchmark) {}}}++TEST(texture_dab_tile_path_matches_triangle_path) {+ // The texel-tile dab (dense meshes) paints what the per-triangle dab paints: island texels within 1/255 in the+ // colour and every channel, on Base and on a layer, with X symmetry and a mask; gutter texels may pick another+ // nearby triangle, so they are only required to be painted.+ Mesh base = makeIcosphere(6); // 82k triangles on a 1K texture: the tile path's case+ sphericalUvs(base);+ base.ensureMask();+ for (uint32_t v = 0; v < base.vertexCount(); ++v) base.mask[v] = base.positions[v].y > 0.3f ? 0.7f : 0.0f;+ BVH bvh;+ bvh.build(base);+ std::shared_ptr<PaintTexture> out[2];+ for (int pass = 0; pass < 2; ++pass) {+ TexturePainter::setPath(pass ? TexturePainter::Path::Tiles : TexturePainter::Path::Triangles);+ Mesh m = base;+ m.texture = makeTexture(1024, 1024);+ m.paintToTexture = true;+ for (Channel c : {Channel::Roughness, Channel::Height, Channel::Normal}) setChannelEnabled(*m.texture, c, true);+ BrushMaterial mat = rustyMetal();+ mat.normal = glm::normalize(glm::vec3(0.3f, -0.2f, 1.0f));+ mat.channels = channelBit(Channel::BaseColor) | channelBit(Channel::Roughness) | channelBit(Channel::Height) |+ channelBit(Channel::Normal);+ for (int layer = 0; layer < 2; ++layer) {+ if (layer) addTextureLayer(*m.texture, "Layer");+ TexturePainter painter;+ CHECK(painter.begin(m, mat.channels));+ DabParams p = dab(glm::vec3(0, 0, 1), layer ? 0.15f : 0.4f, 0.6f);+ p.viewDir = glm::vec3(0, 0, -1);+ for (int i = 0; i < 6; ++i) {+ p.center = glm::normalize(glm::vec3(-0.4f + 0.15f * float(i), 0.25f * float(layer) - 0.1f, 1.0f));+ CHECK(painter.dab(m, bvh, p, mat, Symmetry{true, false, false}) > 0);+ }+ UndoRecord rec;+ painter.end(rec);+ CHECK(rec.textureTiles != nullptr);+ mat.color = glm::vec3(0.1f, 0.5f, 0.9f);+ mat.roughness = 0.9f;+ }+ out[pass] = m.texture;+ }+ TexturePainter::setPath(TexturePainter::Path::Auto);+ const PaintTexture& a = *out[0];+ const PaintTexture& b = *out[1];+ int worst = 0;+ size_t islandPainted = 0, gutterMismatch = 0;+ for (int y = 0; y < a.height; ++y)+ for (int x = 0; x < a.width; ++x) {+ const size_t i = size_t(y) * size_t(a.width) + size_t(x);+ const bool island = a.covered(x, y);+ int d = 0;+ for (int k = 0; k < 3; ++k) d = std::max(d, std::abs(int(a.pixels[i * 4 + size_t(k)]) - int(b.pixels[i * 4 + size_t(k)])));+ for (Channel c : {Channel::Roughness, Channel::Height, Channel::Normal}) {+ float va[4], vb[4];+ channelImage(a, c).values(x, y, va);+ channelImage(b, c).values(x, y, vb);+ for (int k = 0; k < channelFormat(c).components; ++k) d = std::max(d, int(std::lround(std::abs(va[k] - vb[k]) * 255.0f)));+ }+ if (island) {+ worst = std::max(worst, d);+ islandPainted += a.pixels[i * 4] != 255 || a.pixels[i * 4 + 2] != 255;+ } else {+ gutterMismatch += d > 1;+ }+ }+ std::printf(" tile vs triangle dab: %zu island texels painted, worst difference %d/255, %zu gutter texels differ\n",+ islandPainted, worst, gutterMismatch);+ CHECK(islandPainted > 10000);+ CHECK(worst <= 1);+}