refactor: constexpr constants and minor optimizations

Replace magic numbers with named constants, add AABB helpers,
reduce redundant normalizations, improve TU organization
This commit is contained in:
eliotbyte 2025-10-25 21:19:06 +03:00
parent dbee19693e
commit 1dfb415843
2 changed files with 44 additions and 39 deletions

View file

@ -12,8 +12,14 @@
const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f); const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f; const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
// forward declaration for helper used below namespace {
static inline void expand_aabb(AABB& aabb, bool& init, const glm::vec3& p); static constexpr float kChunkCenterBias = 0.5f;
static constexpr float kAoNormalPush = 0.75f;
static constexpr float kFaceOffsetEps = 1e-3f;
static inline void expandAabbPoint(AABB& aabb, bool& init, const glm::vec3& p) {
if (!init) { aabb.a = aabb.b = p; init = true; } else { aabb.addPoint(p); }
}
static inline float applyDirectionalFactor(float d) { static inline float applyDirectionalFactor(float d) {
return (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR; return (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
@ -24,10 +30,10 @@ static inline void expandAabb4(
const glm::vec3& p0, const glm::vec3& p1, const glm::vec3& p0, const glm::vec3& p1,
const glm::vec3& p2, const glm::vec3& p3 const glm::vec3& p2, const glm::vec3& p3
) { ) {
expand_aabb(aabb, init, p0); expandAabbPoint(aabb, init, p0);
expand_aabb(aabb, init, p1); expandAabbPoint(aabb, init, p1);
expand_aabb(aabb, init, p2); expandAabbPoint(aabb, init, p2);
expand_aabb(aabb, init, p3); expandAabbPoint(aabb, init, p3);
} }
static inline void fillTexfaces( static inline void fillTexfaces(
@ -37,6 +43,7 @@ static inline void fillTexfaces(
) { ) {
for (int f = 0; f < 6; ++f) out[f] = cache.getRegion(id, variantId, f, densePass); for (int f = 0; f < 6; ++f) out[f] = cache.getRegion(id, variantId, f, densePass);
} }
}
BlocksRenderer::BlocksRenderer( BlocksRenderer::BlocksRenderer(
size_t capacity, size_t capacity,
@ -127,7 +134,7 @@ void BlocksRenderer::face(
auto X = axisX * w; auto X = axisX * w;
auto Y = axisY * h; auto Y = axisY * h;
auto Z = axisZ * d; auto Z = axisZ * d;
float s = 0.5f; float s = kChunkCenterBias;
auto p0 = coord + (-X - Y + Z) * s; auto p0 = coord + (-X - Y + Z) * s;
auto p1 = coord + ( X - Y + Z) * s; auto p1 = coord + ( X - Y + Z) * s;
auto p2 = coord + ( X + Y + Z) * s; auto p2 = coord + ( X + Y + Z) * s;
@ -177,16 +184,17 @@ void BlocksRenderer::faceAO(
return; return;
} }
float s = 0.5f; float s = kChunkCenterBias;
if (lights) { if (lights) {
float d = applyDirectionalFactor(glm::dot(glm::normalize(Z), SUN_VECTOR)); const auto nZ = glm::normalize(Z);
float d = applyDirectionalFactor(glm::dot(nZ, SUN_VECTOR));
auto axisX = glm::normalize(X); auto axisX = glm::normalize(X);
auto axisY = glm::normalize(Y); auto axisY = glm::normalize(Y);
auto axisZ = glm::normalize(Z); auto axisZ = nZ;
glm::vec4 tint(d); glm::vec4 tint(d);
const float nh = 0.75f; // push AO sample a bit farther along normal const float nh = kAoNormalPush; // push AO sample a bit farther along normal
auto p0 = coord + (-X - Y + Z) * s; auto p0 = coord + (-X - Y + Z) * s;
auto p1 = coord + ( X - Y + Z) * s; auto p1 = coord + ( X - Y + Z) * s;
auto p2 = coord + ( X + Y + Z) * s; auto p2 = coord + ( X + Y + Z) * s;
@ -230,15 +238,17 @@ void BlocksRenderer::face(
return; return;
} }
float s = 0.5f; float s = kChunkCenterBias;
const auto nZ = glm::normalize(Z);
if (lights) { if (lights) {
float d = applyDirectionalFactor(glm::dot(glm::normalize(Z), SUN_VECTOR)); float d = applyDirectionalFactor(glm::dot(nZ, SUN_VECTOR));
tint *= d; tint *= d;
} }
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, Z, lights ? 0 : 1); const auto nZ2 = lights ? nZ : Z;
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, Z, lights ? 0 : 1); vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, nZ2, lights ? 0 : 1);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, Z, lights ? 0 : 1); vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, nZ2, lights ? 0 : 1);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, Z, lights ? 0 : 1); vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, nZ2, lights ? 0 : 1);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, nZ2, lights ? 0 : 1);
index(0, 1, 2, 0, 2, 3); index(0, 1, 2, 0, 2, 3);
} }

View file

@ -21,6 +21,12 @@ size_t ChunksRenderer::visibleChunks = 0;
namespace { namespace {
struct CullingBounds { glm::vec3 min; glm::vec3 max; }; struct CullingBounds { glm::vec3 min; glm::vec3 max; };
static constexpr float kChunkCenterBias = 0.5f;
static inline bool hasVolume(const AABB& aabb) {
auto s = aabb.size();
return s.x > 0.0f || s.y > 0.0f || s.z > 0.0f;
}
static inline CullingBounds computeChunkCullingBounds( static inline CullingBounds computeChunkCullingBounds(
const Chunk& chunk, const Chunk& chunk,
@ -35,14 +41,13 @@ static inline CullingBounds computeChunkCullingBounds(
auto it = meshes.find({chunk.x, chunk.z}); auto it = meshes.find({chunk.x, chunk.z});
if (it != meshes.end()) { if (it != meshes.end()) {
const auto& aabb = it->second.localAabb; const auto& aabb = it->second.localAabb;
auto size = aabb.size(); if (hasVolume(aabb)) {
if (size.x > 0.0f || size.y > 0.0f || size.z > 0.0f) { min = glm::vec3(chunk.x * CHUNK_W + aabb.min().x + kChunkCenterBias,
min = glm::vec3(chunk.x * CHUNK_W + aabb.min().x + 0.5f, (std::max)(static_cast<float>(chunk.bottom), aabb.min().y + kChunkCenterBias),
(std::max)(static_cast<float>(chunk.bottom), aabb.min().y + 0.5f), chunk.z * CHUNK_D + aabb.min().z + kChunkCenterBias);
chunk.z * CHUNK_D + aabb.min().z + 0.5f); max = glm::vec3(chunk.x * CHUNK_W + aabb.max().x + kChunkCenterBias,
max = glm::vec3(chunk.x * CHUNK_W + aabb.max().x + 0.5f, (std::min)(static_cast<float>(chunk.top), aabb.max().y + kChunkCenterBias),
(std::min)(static_cast<float>(chunk.top), aabb.max().y + 0.5f), chunk.z * CHUNK_D + aabb.max().z + kChunkCenterBias);
chunk.z * CHUNK_D + aabb.max().z + 0.5f);
} }
} }
return {min, max}; return {min, max};
@ -239,7 +244,7 @@ void ChunksRenderer::drawShadowsPass(
} }
glm::vec3 coord( glm::vec3 coord(
pos.x * CHUNK_W + 0.5f, 0.5f, pos.y * CHUNK_D + 0.5f pos.x * CHUNK_W + kChunkCenterBias, kChunkCenterBias, pos.y * CHUNK_D + kChunkCenterBias
); );
const auto bounds = computeChunkCullingBounds(*chunk, meshes); const auto bounds = computeChunkCullingBounds(*chunk, meshes);
@ -297,7 +302,7 @@ void ChunksRenderer::drawChunks(
if (mesh) { if (mesh) {
glm::vec3 coord( glm::vec3 coord(
chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f chunk->x * CHUNK_W + kChunkCenterBias, kChunkCenterBias, chunk->z * CHUNK_D + kChunkCenterBias
); );
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord); glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
shader.uniformMatrix("u_model", model); shader.uniformMatrix("u_model", model);
@ -361,18 +366,8 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
} }
if (culling) { if (culling) {
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); const auto bounds = computeChunkCullingBounds(*chunk, meshes);
glm::vec3 max(chunk->x * CHUNK_W + CHUNK_W, chunk->top, chunk->z * CHUNK_D + CHUNK_D); if (!frustum.isBoxVisible(bounds.min, bounds.max)) continue;
const auto& aabb = found->second.localAabb;
if (aabb.size().x > 0.0f || aabb.size().y > 0.0f || aabb.size().z > 0.0f) {
min = glm::vec3(chunk->x * CHUNK_W + aabb.min().x + 0.5f,
(std::max)(static_cast<float>(chunk->bottom), aabb.min().y + 0.5f),
chunk->z * CHUNK_D + aabb.min().z + 0.5f);
max = glm::vec3(chunk->x * CHUNK_W + aabb.max().x + 0.5f,
(std::min)(static_cast<float>(chunk->top), aabb.max().y + 0.5f),
chunk->z * CHUNK_D + aabb.max().z + 0.5f);
}
if (!frustum.isBoxVisible(min, max)) continue;
} }
long long nearest = LLONG_MAX; long long nearest = LLONG_MAX;