Skip to content

Commit c088c4f

Browse files
committed
labs/lsg: align CPU skin cell hashing with GLSL
1 parent 5e1ac05 commit c088c4f

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎labs/lsg/include/rengine/lsg/deterministic_hash.hpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ namespace rengine::lsg {
1212
return static_cast<std::uint32_t>(seed) ^ static_cast<std::uint32_t>(seed >> 32u);
1313
}
1414

15+
[[nodiscard]] constexpr std::uint32_t hash_cell3_32(std::uint32_t seed_key,
16+
std::int32_t x,
17+
std::int32_t y,
18+
std::int32_t z) noexcept {
19+
std::uint32_t h = pcg_hash(seed_key);
20+
h = pcg_hash(h ^ static_cast<std::uint32_t>(x));
21+
h = pcg_hash(h ^ static_cast<std::uint32_t>(y));
22+
h = pcg_hash(h ^ static_cast<std::uint32_t>(z));
23+
return h;
24+
}
25+
26+
[[nodiscard]] constexpr std::uint32_t hash_cell3(std::uint64_t seed,
27+
std::int32_t x,
28+
std::int32_t y,
29+
std::int32_t z) noexcept {
30+
return hash_cell3_32(fold_seed64(seed), x, y, z);
31+
}
32+
1533
[[nodiscard]] constexpr std::uint32_t hash5_32(std::uint32_t seed_key, std::uint32_t region,
1634
std::int32_t x, std::int32_t y, std::int32_t z) noexcept {
1735
std::uint32_t h = pcg_hash(seed_key);

‎labs/lsg/src/surface.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ float value_noise(std::uint64_t seed, Vec3 p) noexcept {
4646
const float fy = smooth01(p.y - std::floor(p.y));
4747
const float fz = smooth01(p.z - std::floor(p.z));
4848
const auto sample = [&](std::int32_t dx, std::int32_t dy, std::int32_t dz) noexcept {
49-
return hash01(hash5(seed, 0u, x0 + dx, y0 + dy, z0 + dz));
49+
return hash01(hash_cell3(seed, x0 + dx, y0 + dy, z0 + dz));
5050
};
5151

5252
const float v000 = sample(0, 0, 0), v100 = sample(1, 0, 0);
@@ -74,7 +74,7 @@ PoreFieldSample sample_pore_field(const CharacterGenomeV0& genome, Vec3 p,
7474
for (std::int32_t dy = -1; dy <= 1; ++dy) {
7575
for (std::int32_t dx = -1; dx <= 1; ++dx) {
7676
const auto cx = bx + dx, cy = by + dy, cz = bz + dz;
77-
const std::uint32_t h = hash5(genome.surface_seed ^ 0xB5297A4Dull, 0u, cx, cy, cz);
77+
const std::uint32_t h = hash_cell3(genome.surface_seed ^ 0xB5297A4Dull, cx, cy, cz);
7878
if (hash01(pcg_hash(h ^ 0xD1B54A35u)) >= density) continue;
7979

8080
const float jx = 0.15f + 0.70f * hash01(pcg_hash(h ^ 0x68E31DA4u));

‎labs/lsg/tests/lsg_tests.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ int main() {
149149
static_assert(h1 == h2); static_assert(h1 != h3);
150150
static_assert(hash5(0x1122334455667788ull, 4, -2, 7, 11) ==
151151
hash5_32(fold_seed64(0x1122334455667788ull), 4, -2, 7, 11));
152+
constexpr std::uint64_t cell_seed = 0x1122334455667788ull;
153+
constexpr std::uint32_t cell_key = fold_seed64(cell_seed);
154+
constexpr std::uint32_t cell_manual =
155+
pcg_hash(pcg_hash(pcg_hash(pcg_hash(cell_key) ^ static_cast<std::uint32_t>(-2)) ^
156+
static_cast<std::uint32_t>(7)) ^
157+
static_cast<std::uint32_t>(11));
158+
static_assert(hash_cell3(cell_seed, -2, 7, 11) == cell_manual);
159+
static_assert(hash_cell3_32(cell_key, -2, 7, 11) == cell_manual);
152160
assert(select_detail_band(4.0f) == DetailBand::macro_only); assert(select_detail_band(2.0f) == DetailBand::meso);
153161
assert(select_detail_band(0.5f) == DetailBand::micro); assert(select_detail_band(0.05f) == DetailBand::micro_high);
154162

0 commit comments

Comments
 (0)