PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llworldmipmap.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 268 lines | 164 code | 27 blank | 77 comment | 29 complexity | 0a471134f4d60384344cc5b689538042 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llworldmipmap.cpp
  3. * @brief Data storage for the S3 mipmap of the entire world.
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llworldmipmap.h"
  28. #include "llviewercontrol.h" // LLControlGroup
  29. #include "llviewertexturelist.h"
  30. #include "math.h" // log()
  31. // Turn this on to output tile stats in the standard output
  32. #define DEBUG_TILES_STAT 0
  33. LLWorldMipmap::LLWorldMipmap() :
  34. mCurrentLevel(0)
  35. {
  36. }
  37. LLWorldMipmap::~LLWorldMipmap()
  38. {
  39. reset();
  40. }
  41. // Delete all sublevel maps and clean them
  42. void LLWorldMipmap::reset()
  43. {
  44. for (int level = 0; level < MAP_LEVELS; level++)
  45. {
  46. mWorldObjectsMipMap[level].clear();
  47. }
  48. }
  49. // This method should be called before each use of the mipmap (typically, before each draw), so that to let
  50. // the boost level of unused tiles to drop to 0 (BOOST_NONE).
  51. // Tiles that are accessed have had their boost level pushed to BOOST_MAP_VISIBLE so we can identify them.
  52. // The result of this strategy is that if a tile is not used during 2 consecutive loops, its boost level drops to 0.
  53. void LLWorldMipmap::equalizeBoostLevels()
  54. {
  55. #if DEBUG_TILES_STAT
  56. S32 nb_missing = 0;
  57. S32 nb_tiles = 0;
  58. S32 nb_visible = 0;
  59. #endif // DEBUG_TILES_STAT
  60. // For each level
  61. for (S32 level = 0; level < MAP_LEVELS; level++)
  62. {
  63. sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level];
  64. // For each tile
  65. for (sublevel_tiles_t::iterator iter = level_mipmap.begin(); iter != level_mipmap.end(); iter++)
  66. {
  67. LLPointer<LLViewerFetchedTexture> img = iter->second;
  68. S32 current_boost_level = img->getBoostLevel();
  69. if (current_boost_level == LLViewerTexture::BOOST_MAP_VISIBLE)
  70. {
  71. // If level was BOOST_MAP_VISIBLE, the tile has been used in the last draw so keep it high
  72. img->setBoostLevel(LLViewerTexture::BOOST_MAP);
  73. }
  74. else
  75. {
  76. // If level was BOOST_MAP only (or anything else...), the tile wasn't used in the last draw
  77. // so we drop its boost level to BOOST_NONE.
  78. img->setBoostLevel(LLViewerTexture::BOOST_NONE);
  79. }
  80. #if DEBUG_TILES_STAT
  81. // Increment some stats if compile option on
  82. nb_tiles++;
  83. if (current_boost_level == LLViewerTexture::BOOST_MAP_VISIBLE)
  84. {
  85. nb_visible++;
  86. }
  87. if (img->isMissingAsset())
  88. {
  89. nb_missing++;
  90. }
  91. #endif // DEBUG_TILES_STAT
  92. }
  93. }
  94. #if DEBUG_TILES_STAT
  95. LL_INFOS("World Map") << "LLWorldMipmap tile stats : total requested = " << nb_tiles << ", visible = " << nb_visible << ", missing = " << nb_missing << LL_ENDL;
  96. #endif // DEBUG_TILES_STAT
  97. }
  98. // This method should be used when the mipmap is not actively used for a while, e.g., the map UI is hidden
  99. void LLWorldMipmap::dropBoostLevels()
  100. {
  101. // For each level
  102. for (S32 level = 0; level < MAP_LEVELS; level++)
  103. {
  104. sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level];
  105. // For each tile
  106. for (sublevel_tiles_t::iterator iter = level_mipmap.begin(); iter != level_mipmap.end(); iter++)
  107. {
  108. LLPointer<LLViewerFetchedTexture> img = iter->second;
  109. img->setBoostLevel(LLViewerTexture::BOOST_NONE);
  110. }
  111. }
  112. }
  113. LLPointer<LLViewerFetchedTexture> LLWorldMipmap::getObjectsTile(U32 grid_x, U32 grid_y, S32 level, bool load)
  114. {
  115. // Check the input data
  116. llassert(level <= MAP_LEVELS);
  117. llassert(level >= 1);
  118. // If the *loading* level changed, cleared the new level from "missed" tiles
  119. // so that we get a chance to reload them
  120. if (load && (level != mCurrentLevel))
  121. {
  122. cleanMissedTilesFromLevel(level);
  123. mCurrentLevel = level;
  124. }
  125. // Build the region handle
  126. U64 handle = convertGridToHandle(grid_x, grid_y);
  127. // Check if the image is around already
  128. sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level-1];
  129. sublevel_tiles_t::iterator found = level_mipmap.find(handle);
  130. // If not there and load on, go load it
  131. if (found == level_mipmap.end())
  132. {
  133. if (load)
  134. {
  135. // Load it
  136. LLPointer<LLViewerFetchedTexture> img = loadObjectsTile(grid_x, grid_y, level);
  137. // Insert the image in the map
  138. level_mipmap.insert(sublevel_tiles_t::value_type( handle, img ));
  139. // Find the element again in the map (it's there now...)
  140. found = level_mipmap.find(handle);
  141. }
  142. else
  143. {
  144. // Return with NULL if not found and we're not trying to load
  145. return NULL;
  146. }
  147. }
  148. // Get the image pointer and check if this asset is missing
  149. LLPointer<LLViewerFetchedTexture> img = found->second;
  150. if (img->isMissingAsset())
  151. {
  152. // Return NULL if asset missing
  153. return NULL;
  154. }
  155. else
  156. {
  157. // Boost the tile level so to mark it's in use *if* load on
  158. if (load)
  159. {
  160. img->setBoostLevel(LLViewerTexture::BOOST_MAP_VISIBLE);
  161. }
  162. return img;
  163. }
  164. }
  165. LLPointer<LLViewerFetchedTexture> LLWorldMipmap::loadObjectsTile(U32 grid_x, U32 grid_y, S32 level)
  166. {
  167. // Get the grid coordinates
  168. std::string imageurl = gSavedSettings.getString("CurrentMapServerURL") + llformat("map-%d-%d-%d-objects.jpg", level, grid_x, grid_y);
  169. // DO NOT COMMIT!! DEBUG ONLY!!!
  170. // Use a local jpeg for every tile to test map speed without S3 access
  171. //imageurl = "file://C:\\Develop\\mapserver-distribute-3\\indra\\build-vc80\\mapserver\\relwithdebinfo\\regions\\00995\\01001\\region-995-1001-prims.jpg";
  172. // END DEBUG
  173. //LL_INFOS("World Map") << "LLWorldMipmap::loadObjectsTile(), URL = " << imageurl << LL_ENDL;
  174. LLPointer<LLViewerFetchedTexture> img = LLViewerTextureManager::getFetchedTextureFromUrl(imageurl, TRUE, LLViewerTexture::BOOST_NONE, LLViewerTexture::LOD_TEXTURE);
  175. img->setBoostLevel(LLViewerTexture::BOOST_MAP);
  176. // Return the smart pointer
  177. return img;
  178. }
  179. // This method is used to clean up a level from tiles marked as "missing".
  180. // The idea is to allow tiles that have been improperly marked missing to be reloaded when retraversing the level again.
  181. // When zooming in and out rapidly, some tiles are never properly loaded and, eventually marked missing.
  182. // This creates "blue" areas in a subresolution that never got a chance to reload if we don't clean up the level.
  183. void LLWorldMipmap::cleanMissedTilesFromLevel(S32 level)
  184. {
  185. // Check the input data
  186. llassert(level <= MAP_LEVELS);
  187. llassert(level >= 0);
  188. // This happens when the object is first initialized
  189. if (level == 0)
  190. {
  191. return;
  192. }
  193. // Iterate through the subresolution level and suppress the tiles that are marked as missing
  194. // Note: erasing in a map while iterating through it is bug prone. Using a postfix increment is mandatory here.
  195. sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level-1];
  196. sublevel_tiles_t::iterator it = level_mipmap.begin();
  197. while (it != level_mipmap.end())
  198. {
  199. LLPointer<LLViewerFetchedTexture> img = it->second;
  200. if (img->isMissingAsset())
  201. {
  202. level_mipmap.erase(it++);
  203. }
  204. else
  205. {
  206. ++it;
  207. }
  208. }
  209. return;
  210. }
  211. // static methods
  212. // Compute the level in the world mipmap (between 1 and MAP_LEVELS, as in the URL) given the scale (size of a sim in screen pixels)
  213. S32 LLWorldMipmap::scaleToLevel(F32 scale)
  214. {
  215. // If scale really small, picks up the higest level there is (lowest resolution)
  216. if (scale <= F32_MIN)
  217. return MAP_LEVELS;
  218. // Compute the power of two resolution level knowing the base level
  219. S32 level = llfloor((log(REGION_WIDTH_METERS/scale)/log(2.0f)) + 1.0f);
  220. // Check bounds and return the value
  221. if (level > MAP_LEVELS)
  222. return MAP_LEVELS;
  223. else if (level < 1)
  224. return 1;
  225. else
  226. return level;
  227. }
  228. // Convert world coordinates to mipmap grid coordinates at a given level (between 1 and MAP_LEVELS)
  229. void LLWorldMipmap::globalToMipmap(F64 global_x, F64 global_y, S32 level, U32* grid_x, U32* grid_y)
  230. {
  231. // Check the input data
  232. llassert(level <= MAP_LEVELS);
  233. llassert(level >= 1);
  234. // Convert world coordinates into grid coordinates
  235. *grid_x = lltrunc(global_x/REGION_WIDTH_METERS);
  236. *grid_y = lltrunc(global_y/REGION_WIDTH_METERS);
  237. // Compute the valid grid coordinates at that level of the mipmap
  238. S32 regions_in_tile = 1 << (level - 1);
  239. *grid_x = *grid_x - (*grid_x % regions_in_tile);
  240. *grid_y = *grid_y - (*grid_y % regions_in_tile);
  241. }