LCOV - code coverage report
Current view: top level - source/graphics - HeightMipmap.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 81 135 60.0 %
Date: 2026-12-03 01:19:39 Functions: 8 11 72.7 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "HeightMipmap.h"
      21             : 
      22             : #include "lib/alignment.h"
      23             : #include "lib/allocators/dynarray.h"
      24             : #include "lib/allocators/shared_ptr.h"
      25             : #include "lib/debug.h"
      26             : #include "lib/file/vfs/vfs.h"
      27             : #include "lib/file/vfs/vfs_path.h"
      28             : #include "lib/posix/posix_types.h"
      29             : #include "lib/status.h"
      30             : #include "lib/tex/tex.h"
      31             : #include "maths/MathUtil.h"
      32             : #include "ps/Filesystem.h"
      33             : 
      34             : #include <bit>
      35             : #include <cmath>
      36             : #include <cstring>
      37             : #include <memory>
      38             : #include <tuple>
      39             : 
      40          24 : CHeightMipmap::CHeightMipmap()
      41             : {
      42          24 : }
      43             : 
      44          24 : CHeightMipmap::~CHeightMipmap()
      45             : {
      46          24 :     ReleaseData();
      47          24 : }
      48             : 
      49         114 : void CHeightMipmap::ReleaseData()
      50             : {
      51         298 :     for (size_t i = 0; i < m_Mipmap.size(); ++i)
      52             :     {
      53         184 :         delete[] m_Mipmap[i].m_Heightmap;
      54         184 :         m_Mipmap[i].m_MapSize = 0;
      55             :     }
      56         114 :     m_Mipmap.clear();
      57         114 : }
      58             : 
      59         100 : void CHeightMipmap::Update(const std::uint16_t* ptr)
      60             : {
      61         100 :     ENSURE(ptr != 0);
      62             : 
      63         100 :     Update(ptr, 0, 0, m_MapSize, m_MapSize);
      64         100 : }
      65             : 
      66         100 : void CHeightMipmap::Update(const std::uint16_t* ptr, size_t left, size_t bottom, size_t right, size_t top)
      67             : {
      68         100 :     ENSURE(ptr != 0);
      69             : 
      70         100 :     size_t mapSize = m_MapSize;
      71             : 
      72         689 :     for (size_t i = 0; i < m_Mipmap.size(); ++i)
      73             :     {
      74             :         // update window
      75         589 :         left = Clamp<size_t>(floorf(static_cast<float>(left) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1);
      76         589 :         bottom = Clamp<size_t>(floorf(static_cast<float>(bottom) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1);
      77             : 
      78         589 :         right = Clamp<size_t>(ceilf(static_cast<float>(right) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize);
      79         589 :         top = Clamp<size_t>(ceilf(static_cast<float>(top) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize);
      80             : 
      81             :         // TODO: should verify that the bounds calculations are actually correct
      82             : 
      83             :         // update mipmap
      84         589 :         BilinearUpdate(m_Mipmap[i], mapSize, ptr, left, bottom, right, top);
      85             : 
      86         589 :         mapSize = m_Mipmap[i].m_MapSize;
      87         589 :         ptr = m_Mipmap[i].m_Heightmap;
      88             :     }
      89         100 : }
      90             : 
      91          33 : void CHeightMipmap::Initialize(size_t mapSize, const std::uint16_t* ptr)
      92             : {
      93          33 :     ENSURE(ptr != 0);
      94          33 :     ENSURE(mapSize > 0);
      95             : 
      96          33 :     ReleaseData();
      97             : 
      98          33 :     m_MapSize = mapSize;
      99          33 :     size_t mipmapSize = std::bit_floor(mapSize);
     100             : 
     101         217 :     while (mipmapSize > 1)
     102             :     {
     103         184 :         m_Mipmap.push_back(SMipmap(mipmapSize, new std::uint16_t[mipmapSize*mipmapSize]));
     104         184 :         mipmapSize >>= 1;
     105          33 :     };
     106             : 
     107          33 :     Update(ptr);
     108          33 : }
     109             : 
     110           0 : float CHeightMipmap::GetTrilinearGroundLevel(float x, float z, float radius) const
     111             : {
     112           0 :     float y;
     113           0 :     if (radius <= 0.0f) // avoid logf of non-positive value
     114             :         y = 0.0f;
     115             :     else
     116           0 :         y = Clamp<float>(logf(radius * m_Mipmap[0].m_MapSize) / logf(2), 0.0f, m_Mipmap.size());
     117             : 
     118           0 :     const size_t iy = static_cast<size_t>(Clamp<ssize_t>(floorf(y), 0, m_Mipmap.size() - 2));
     119             : 
     120           0 :     const float fy = y - iy;
     121             : 
     122           0 :     const float h0 = BilinearFilter(m_Mipmap[iy], x, z);
     123           0 :     const float h1 = BilinearFilter(m_Mipmap[iy + 1], x, z);
     124             : 
     125           0 :     return (1 - fy) * h0 + fy * h1;
     126             : }
     127             : 
     128           0 : float CHeightMipmap::BilinearFilter(const SMipmap &mipmap, float x, float z) const
     129             : {
     130           0 :     x *= mipmap.m_MapSize;
     131           0 :     z *= mipmap.m_MapSize;
     132             : 
     133           0 :     const size_t xi = static_cast<size_t>(Clamp<ssize_t>(floor(x), 0, mipmap.m_MapSize - 2));
     134           0 :     const size_t zi = static_cast<size_t>(Clamp<ssize_t>(floor(z), 0, mipmap.m_MapSize - 2));
     135             : 
     136           0 :     const float xf = Clamp<float>(x-xi, 0.0f, 1.0f);
     137           0 :     const float zf = Clamp<float>(z-zi, 0.0f, 1.0f);
     138             : 
     139           0 :     const float h00 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + xi];
     140           0 :     const float h01 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + xi];
     141           0 :     const float h10 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + (xi+1)];
     142           0 :     const float h11 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + (xi+1)];
     143             : 
     144           0 :     return
     145           0 :         (1.f - xf) * (1.f - zf) * h00 +
     146           0 :                xf  * (1.f - zf) * h10 +
     147           0 :         (1.f - xf) *        zf  * h01 +
     148           0 :                xf  *        zf  * h11;
     149             : }
     150             : 
     151         489 : void CHeightMipmap::HalfResizeUpdate(SMipmap &out_mipmap, size_t mapSize, const std::uint16_t* ptr,
     152             :     size_t left, size_t bottom, size_t right, size_t top)
     153             : {
     154             :     // specialized, faster version of BilinearUpdate for powers of 2
     155             : 
     156         489 :     ENSURE(out_mipmap.m_MapSize != 0);
     157             : 
     158         489 :     if (out_mipmap.m_MapSize * 2 != mapSize)
     159           0 :         debug_warn(L"wrong size");
     160             : 
     161             :     // valid update window
     162         489 :     ENSURE(left < out_mipmap.m_MapSize);
     163         489 :     ENSURE(bottom < out_mipmap.m_MapSize);
     164         489 :     ENSURE(right > left && right <= out_mipmap.m_MapSize);
     165         489 :     ENSURE(top > bottom && top <= out_mipmap.m_MapSize);
     166             : 
     167        6545 :     for (size_t dstZ = bottom; dstZ < top; ++dstZ)
     168             :     {
     169      150392 :         for (size_t dstX = left; dstX < right; ++dstX)
     170             :         {
     171      144336 :             size_t srcX = dstX << 1;
     172      144336 :             size_t srcZ = dstZ << 1;
     173             : 
     174      144336 :             std::uint16_t h00 = ptr[srcX + 0 + srcZ * mapSize];
     175      144336 :             std::uint16_t h10 = ptr[srcX + 1 + srcZ * mapSize];
     176      144336 :             std::uint16_t h01 = ptr[srcX + 0 + (srcZ + 1) * mapSize];
     177      144336 :             std::uint16_t h11 = ptr[srcX + 1 + (srcZ + 1) * mapSize];
     178             : 
     179      144336 :             out_mipmap.m_Heightmap[dstX + dstZ * out_mipmap.m_MapSize] = (h00 + h10 + h01 + h11) / 4;
     180             :         }
     181             :     }
     182         489 : }
     183             : 
     184         589 : void CHeightMipmap::BilinearUpdate(SMipmap &out_mipmap, size_t mapSize, const std::uint16_t* ptr,
     185             :     size_t left, size_t bottom, size_t right, size_t top)
     186             : {
     187         589 :     ENSURE(out_mipmap.m_MapSize != 0);
     188             : 
     189             :     // filter should have full coverage
     190         589 :     ENSURE(out_mipmap.m_MapSize <= mapSize && out_mipmap.m_MapSize * 2 >= mapSize);
     191             : 
     192             :     // valid update window
     193         589 :     ENSURE(left < out_mipmap.m_MapSize);
     194         589 :     ENSURE(bottom < out_mipmap.m_MapSize);
     195         589 :     ENSURE(right > left && right <= out_mipmap.m_MapSize);
     196         589 :     ENSURE(top > bottom && top <= out_mipmap.m_MapSize);
     197             : 
     198         589 :     if (out_mipmap.m_MapSize * 2 == mapSize)
     199             :     {
     200             :         // optimized for powers of 2
     201         489 :         HalfResizeUpdate(out_mipmap, mapSize, ptr, left, bottom, right, top);
     202             :     }
     203             :     else
     204             :     {
     205        6356 :         for (size_t dstZ = bottom; dstZ < top; ++dstZ)
     206             :         {
     207      439664 :             for (size_t dstX = left; dstX < right; ++dstX)
     208             :             {
     209      433408 :                 const float x = ((float)dstX / (float)out_mipmap.m_MapSize) * mapSize;
     210      433408 :                 const float z = ((float)dstZ / (float)out_mipmap.m_MapSize) * mapSize;
     211             : 
     212      433408 :                 const size_t srcX = Clamp<size_t>(x, 0, mapSize - 2);
     213      433408 :                 const size_t srcZ = Clamp<size_t>(z, 0, mapSize - 2);
     214             : 
     215      433408 :                 const float fx = Clamp<float>(x - srcX, 0.0f, 1.0f);
     216      433408 :                 const float fz = Clamp<float>(z - srcZ, 0.0f, 1.0f);
     217             : 
     218      433408 :                 const float h00 = ptr[srcX + 0 + srcZ * mapSize];
     219      433408 :                 const float h10 = ptr[srcX + 1 + srcZ * mapSize];
     220      433408 :                 const float h01 = ptr[srcX + 0 + (srcZ + 1) * mapSize];
     221      433408 :                 const float h11 = ptr[srcX + 1 + (srcZ + 1) * mapSize];
     222             : 
     223      433408 :                 out_mipmap.m_Heightmap[dstX + dstZ * out_mipmap.m_MapSize] =
     224      433408 :                     static_cast<std::uint16_t>((1.f - fx) * (1.f - fz) * h00 +
     225      433408 :                         fx * (1.f - fz) * h10 + (1.f - fx) * fz * h01 + fx * fz * h11);
     226             :             }
     227             :         }
     228             :     }
     229         589 : }
     230             : 
     231           0 : void CHeightMipmap::DumpToDisk(const VfsPath& filename) const
     232             : {
     233           0 :     const size_t w = m_MapSize;
     234           0 :     const size_t h = m_MapSize * 2;
     235           0 :     const size_t bpp = 8;
     236           0 :     int flags = TEX_GREY|TEX_TOP_DOWN;
     237             : 
     238           0 :     const size_t img_size = w * h * bpp/8;
     239           0 :     const size_t hdr_size = tex_hdr_size(filename);
     240           0 :     std::shared_ptr<std::uint8_t> buf;
     241           0 :     AllocateAligned(buf, hdr_size+img_size, maxSectorSize);
     242           0 :     void* img = buf.get() + hdr_size;
     243           0 :     Tex t;
     244           0 :     WARN_IF_ERR(t.wrap(w, h, bpp, flags, buf, hdr_size));
     245             : 
     246           0 :     memset(img, 0x00, img_size);
     247           0 :     size_t yoff = 0;
     248           0 :     for (size_t i = 0; i < m_Mipmap.size(); ++i)
     249             :     {
     250           0 :         size_t size = m_Mipmap[i].m_MapSize;
     251           0 :         std::uint16_t* heightmap = m_Mipmap[i].m_Heightmap;
     252           0 :         ENSURE(size+yoff <= h);
     253           0 :         for (size_t y = 0; y < size; ++y)
     254             :         {
     255           0 :             for (size_t x = 0; x < size; ++x)
     256             :             {
     257           0 :                 std::uint16_t val = heightmap[x + y*size];
     258           0 :                 static_cast<std::uint8_t*>(img)[x + (y+yoff)*w] = val >> 8;
     259             :             }
     260             :         }
     261           0 :         yoff += size;
     262             :     }
     263             : 
     264           0 :     DynArray da;
     265           0 :     WARN_IF_ERR(t.encode(filename.Extension(), &da));
     266           0 :     g_VFS->CreateFile(filename, {da.base, da.pos});
     267           0 :     std::ignore = da_free(&da);
     268           0 : }

Generated by: LCOV version 1.16