Browse Source

Refactor magic numbers in render/ directory

- Add hash and noise constants to ground_utils.h
- Add XorShift constants to render_constants.h
- Update terrain_renderer.cpp to use shared utilities
- Update river_renderer.cpp to use noiseHash function
- Update riverbank_renderer.cpp to use noiseHash function
- Update humanoid_math.h to use named constants
- Update arrow_vfx_renderer.cpp to use hash01 from humanoid_math.h
- Update firecamp_renderer.cpp to use named constants
- Eliminate duplicate hash functions across multiple files

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 1 month ago
parent
commit
624731bb4e

+ 1 - 7
render/entity/arrow_vfx_renderer.cpp

@@ -8,6 +8,7 @@
 #include "../gl/mesh.h"
 #include "../gl/primitives.h"
 #include "../gl/texture.h"
+#include "../humanoid_math.h"
 #include "archer_renderer.h"
 #include "registry.h"
 
@@ -289,13 +290,6 @@ static inline void drawQuiver(const DrawContext &p, ISubmitter &out,
                               uint32_t seed) {
   using HP = HumanProportions;
 
-  auto hash01 = [](uint32_t x) {
-    x ^= x << 13;
-    x ^= x >> 17;
-    x ^= x << 5;
-    return (x & 0x00FFFFFF) / float(0x01000000);
-  };
-
   QVector3D qTop(-0.08F, HP::SHOULDER_Y + 0.10F, -0.25F);
   QVector3D q_base(-0.10F, HP::CHEST_Y, -0.22F);
 

+ 7 - 0
render/gl/render_constants.h

@@ -79,4 +79,11 @@ inline constexpr int Shift16 = 16;
 inline constexpr int Shift32 = 32;
 inline constexpr unsigned int Mask24Bit = 0xFFFFFF;
 inline constexpr float Mask24BitFloat = 16777215.0F;
+inline constexpr unsigned int Mask24BitHex = 0x01000000U;
 } // namespace Render::GL::BitShift
+
+namespace Render::GL::HashXorShift {
+inline constexpr int Shift5 = 5;
+inline constexpr int Shift13 = 13;
+inline constexpr int Shift17 = 17;
+} // namespace Render::GL::HashXorShift

+ 8 - 5
render/ground/firecamp_renderer.cpp

@@ -139,7 +139,8 @@ void FireCampRenderer::submit(Renderer &renderer, ResourceManager *resources) {
     uint32_t state =
         hashCoords(static_cast<int>(std::floor(camp_pos.x())),
                    static_cast<int>(std::floor(camp_pos.z())),
-                   static_cast<uint32_t>(radius_phase.y() * 37.0F));
+                   static_cast<uint32_t>(radius_phase.y() *
+                                         HashConstants::TemporalHashFrequency));
 
     const float time = params.time;
     const float char_amount =
@@ -309,8 +310,10 @@ void FireCampRenderer::generateFireCampInstances() {
     return true;
   };
 
-  for (int z = 0; z < m_height; z += 20) {
-    for (int x = 0; x < m_width; x += 20) {
+  constexpr int k_grid_spacing = 20;
+
+  for (int z = 0; z < m_height; z += k_grid_spacing) {
+    for (int x = 0; x < m_width; x += k_grid_spacing) {
       int const idx = z * m_width + x;
 
       QVector3D const normal = normals[idx];
@@ -341,8 +344,8 @@ void FireCampRenderer::generateFireCampInstances() {
 
       float const effective_density = fire_camp_density * density_mult;
       if (rand01(state) < effective_density) {
-        float const gx = float(x) + rand01(state) * 20.0F;
-        float const gz = float(z) + rand01(state) * 20.0F;
+        float const gx = float(x) + rand01(state) * float(k_grid_spacing);
+        float const gz = float(z) + rand01(state) * float(k_grid_spacing);
         add_fire_camp(gx, gz, state);
       }
     }

+ 22 - 4
render/ground/ground_utils.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include "../gl/render_constants.h"
+#include <cmath>
 #include <cstdint>
 
 namespace Render::Ground {
@@ -15,7 +16,15 @@ inline constexpr uint32_t LcgMultiplier = 1664525U;
 inline constexpr uint32_t LcgIncrement = 1013904223U;
 inline constexpr uint32_t XorShift17 = 17;
 inline constexpr uint32_t XorShift11 = 11;
-inline constexpr uint32_t MixConstant = 0xed5ad4bbU;
+inline constexpr uint32_t XorShift15 = 15;
+inline constexpr uint32_t XorShift14 = 14;
+inline constexpr uint32_t MixConstant1 = 0xed5ad4bbU;
+inline constexpr uint32_t MixConstant2 = 0xac4c1b51U;
+inline constexpr uint32_t MixConstant3 = 0x31848babU;
+inline constexpr float NoiseHashX = 127.1F;
+inline constexpr float NoiseHashY = 311.7F;
+inline constexpr float NoiseHashScale = 43758.5453123F;
+inline constexpr float TemporalHashFrequency = 37.0F;
 } // namespace HashConstants
 
 inline auto hashCoords(int x, int z, uint32_t salt = 0U) -> uint32_t {
@@ -37,13 +46,22 @@ inline auto remap(float value, float minOut, float maxOut) -> float {
 
 inline auto hashTo01(uint32_t h) -> float {
   h ^= h >> HashConstants::XorShift17;
-  h *= HashConstants::MixConstant;
+  h *= HashConstants::MixConstant1;
   h ^= h >> HashConstants::XorShift11;
-  h *= HashConstants::MixConstant;
-  h ^= h >> HashConstants::XorShift17;
+  h *= HashConstants::MixConstant2;
+  h ^= h >> HashConstants::XorShift15;
+  h *= HashConstants::MixConstant3;
+  h ^= h >> HashConstants::XorShift14;
   return static_cast<float>((h >> ::Render::GL::BitShift::Shift8) &
                             ::Render::GL::BitShift::Mask24Bit) /
          ::Render::GL::BitShift::Mask24BitFloat;
 }
 
+inline auto noiseHash(float x, float y) -> float {
+  float const n = std::sin(x * HashConstants::NoiseHashX +
+                           y * HashConstants::NoiseHashY) *
+                  HashConstants::NoiseHashScale;
+  return n - std::floor(n);
+}
+
 } // namespace Render::Ground

+ 6 - 10
render/ground/river_renderer.cpp

@@ -3,6 +3,7 @@
 #include "../gl/mesh.h"
 #include "../gl/resources.h"
 #include "../scene_renderer.h"
+#include "ground_utils.h"
 #include "map/terrain.h"
 #include <QVector2D>
 #include <QVector3D>
@@ -35,12 +36,7 @@ void RiverRenderer::buildMeshes() {
     return;
   }
 
-  auto noise_hash = [](float x, float y) -> float {
-    float const n = std::sin(x * 127.1F + y * 311.7F) * 43758.5453123F;
-    return n - std::floor(n);
-  };
-
-  auto noise = [&noise_hash](float x, float y) -> float {
+  auto noise = [](float x, float y) -> float {
     float const ix = std::floor(x);
     float const iy = std::floor(y);
     float fx = x - ix;
@@ -49,10 +45,10 @@ void RiverRenderer::buildMeshes() {
     fx = fx * fx * (3.0F - 2.0F * fx);
     fy = fy * fy * (3.0F - 2.0F * fy);
 
-    float const a = noise_hash(ix, iy);
-    float const b = noise_hash(ix + 1.0F, iy);
-    float const c = noise_hash(ix, iy + 1.0F);
-    float const d = noise_hash(ix + 1.0F, iy + 1.0F);
+    float const a = Ground::noiseHash(ix, iy);
+    float const b = Ground::noiseHash(ix + 1.0F, iy);
+    float const c = Ground::noiseHash(ix, iy + 1.0F);
+    float const d = Ground::noiseHash(ix + 1.0F, iy + 1.0F);
 
     return a * (1.0F - fx) * (1.0F - fy) + b * fx * (1.0F - fy) +
            c * (1.0F - fx) * fy + d * fx * fy;

+ 6 - 10
render/ground/riverbank_renderer.cpp

@@ -3,6 +3,7 @@
 #include "../gl/mesh.h"
 #include "../gl/resources.h"
 #include "../scene_renderer.h"
+#include "ground_utils.h"
 #include "map/terrain.h"
 #include <QVector2D>
 #include <QVector3D>
@@ -40,12 +41,7 @@ void RiverbankRenderer::buildMeshes() {
     return;
   }
 
-  auto noise_hash = [](float x, float y) -> float {
-    float const n = std::sin(x * 127.1F + y * 311.7F) * 43758.5453123F;
-    return n - std::floor(n);
-  };
-
-  auto noise = [&noise_hash](float x, float y) -> float {
+  auto noise = [](float x, float y) -> float {
     float const ix = std::floor(x);
     float const iy = std::floor(y);
     float fx = x - ix;
@@ -54,10 +50,10 @@ void RiverbankRenderer::buildMeshes() {
     fx = fx * fx * (3.0F - 2.0F * fx);
     fy = fy * fy * (3.0F - 2.0F * fy);
 
-    float const a = noise_hash(ix, iy);
-    float const b = noise_hash(ix + 1.0F, iy);
-    float const c = noise_hash(ix, iy + 1.0F);
-    float const d = noise_hash(ix + 1.0F, iy + 1.0F);
+    float const a = Ground::noiseHash(ix, iy);
+    float const b = Ground::noiseHash(ix + 1.0F, iy);
+    float const c = Ground::noiseHash(ix, iy + 1.0F);
+    float const d = Ground::noiseHash(ix + 1.0F, iy + 1.0F);
 
     return a * (1.0F - fx) * (1.0F - fy) + b * fx * (1.0F - fy) +
            c * (1.0F - fx) * fy + d * fx * fy;

+ 2 - 27
render/ground/terrain_renderer.cpp

@@ -5,6 +5,7 @@
 #include "../gl/resources.h"
 #include "../scene_renderer.h"
 #include "ground/terrain_gpu.h"
+#include "ground_utils.h"
 #include "map/terrain.h"
 #include <QDebug>
 #include <QElapsedTimer>
@@ -30,25 +31,10 @@ namespace {
 using std::uint32_t;
 using namespace Render::GL::BitShift;
 using namespace Render::GL::Geometry;
+using namespace Render::Ground;
 
 const QMatrix4x4 k_identity_matrix;
 
-inline auto hashCoords(int x, int z, uint32_t salt = 0U) -> uint32_t {
-  auto const ux = static_cast<uint32_t>(x * 73856093);
-  auto const uz = static_cast<uint32_t>(z * 19349663);
-  return ux ^ uz ^ (salt * 83492791U);
-}
-
-inline auto rand01(uint32_t &state) -> float {
-  state = state * 1664525U + 1013904223U;
-  return static_cast<float>((state >> Shift8) & Mask24Bit) / Mask24BitFloat;
-  static_cast<float>(0xFFFFFF);
-}
-
-inline auto remap(float value, float minOut, float maxOut) -> float {
-  return minOut + (maxOut - minOut) * value;
-}
-
 inline auto applyTint(const QVector3D &color, float tint) -> QVector3D {
   QVector3D const c = color * tint;
   return {std::clamp(c.x(), 0.0F, 1.0F), std::clamp(c.y(), 0.0F, 1.0F),
@@ -60,17 +46,6 @@ inline auto clamp01(const QVector3D &c) -> QVector3D {
           std::clamp(c.z(), 0.0F, 1.0F)};
 }
 
-inline auto hashTo01(uint32_t h) -> float {
-  h ^= h >> 17;
-  h *= 0xed5ad4bbU;
-  h ^= h >> 11;
-  h *= 0xac4c1b51U;
-  h ^= h >> 15;
-  h *= 0x31848babU;
-  h ^= h >> 14;
-  return (h & 0x00FFFFFFU) / float(0x01000000);
-}
-
 inline auto linstep(float a, float b, float x) -> float {
   return std::clamp((x - a) / std::max(1e-6F, (b - a)), 0.0F, 1.0F);
 }

+ 5 - 4
render/humanoid_math.h

@@ -1,5 +1,6 @@
 #pragma once
 
+#include "gl/render_constants.h"
 #include <QVector3D>
 #include <cmath>
 #include <cstdint>
@@ -7,10 +8,10 @@
 namespace Render::GL {
 
 inline auto hash01(uint32_t x) -> float {
-  x ^= x << 13;
-  x ^= x >> 17;
-  x ^= x << 5;
-  return (x & 0x00FFFFFF) / float(0x01000000);
+  x ^= x << HashXorShift::Shift13;
+  x ^= x >> HashXorShift::Shift17;
+  x ^= x << HashXorShift::Shift5;
+  return (x & BitShift::Mask24Bit) / float(BitShift::Mask24BitHex);
 }
 
 inline auto rotY(const QVector3D &v, float angle_rad) -> QVector3D {