Skip to content

Commit

Permalink
Add option to avoid cache for drawing globe
Browse files Browse the repository at this point in the history
In very big screen-sizes cache can be near 1GiB.
This could casue OOM exceptions.
  • Loading branch information
Yankes committed Mar 28, 2023
1 parent 32819ca commit 2d7828f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Engine/Options.cpp
Expand Up @@ -178,6 +178,7 @@ void create()
_info.push_back(OptionInfo("canSellLiveAliens", &canSellLiveAliens, false, "STR_CANSELLLIVEALIENS", "STR_GEOSCAPE"));
_info.push_back(OptionInfo("anytimePsiTraining", &anytimePsiTraining, false, "STR_ANYTIMEPSITRAINING", "STR_GEOSCAPE"));
_info.push_back(OptionInfo("globeSeasons", &globeSeasons, false, "STR_GLOBESEASONS", "STR_GEOSCAPE"));
_info.push_back(OptionInfo("globeSurfaceCache", &globeSurfaceCache, false, "STR_GLOBESURFACECACHE", "STR_GEOSCAPE"));
_info.push_back(OptionInfo("psiStrengthEval", &psiStrengthEval, false, "STR_PSISTRENGTHEVAL", "STR_GEOSCAPE"));
_info.push_back(OptionInfo("canTransferCraftsWhileAirborne", &canTransferCraftsWhileAirborne, false, "STR_CANTRANSFERCRAFTSWHILEAIRBORNE", "STR_GEOSCAPE")); // When the craft can reach the destination base with its fuel
_info.push_back(OptionInfo("retainCorpses", &retainCorpses, false, "STR_RETAINCORPSES", "STR_GEOSCAPE"));
Expand Down
3 changes: 2 additions & 1 deletion src/Engine/Options.inc.h
Expand Up @@ -21,7 +21,8 @@ OPT SDLKey keyOk, keyCancel, keyScreenshot, keyFps, keyQuickLoad, keyQuickSave;

// Geoscape options
OPT int geoClockSpeed, dogfightSpeed, geoScrollSpeed, geoDragScrollButton, geoscapeScale;
OPT bool includePrimeStateInSavedLayout, anytimePsiTraining, weaponSelfDestruction, retainCorpses, craftLaunchAlways, globeSeasons, globeDetail, globeRadarLines, globeFlightPaths, globeAllRadarsOnBaseBuild,
OPT bool includePrimeStateInSavedLayout, anytimePsiTraining, weaponSelfDestruction, retainCorpses, craftLaunchAlways,
globeSurfaceCache, globeSeasons, globeDetail, globeRadarLines, globeFlightPaths, globeAllRadarsOnBaseBuild,
storageLimitsEnforced, canSellLiveAliens, canTransferCraftsWhileAirborne, customInitialBase, aggressiveRetaliation, geoDragScrollInvert,
allowBuildingQueue, showFundsOnGeoscape, psiStrengthEval, allowPsiStrengthImprovement, fieldPromotions, meetingPoint;
OPT SDLKey keyGeoLeft, keyGeoRight, keyGeoUp, keyGeoDown, keyGeoZoomIn, keyGeoZoomOut, keyGeoSpeed1, keyGeoSpeed2, keyGeoSpeed3, keyGeoSpeed4, keyGeoSpeed5, keyGeoSpeed6,
Expand Down
57 changes: 42 additions & 15 deletions src/Geoscape/Globe.cpp
Expand Up @@ -244,6 +244,15 @@ struct CreateShadow
}
};

struct CreateShadowWithoutCache
{
static inline void func(Uint8& dest, const helper::Offset& offset, const Cord& sun, const Sint16& noise, const int& radius)
{
Cord earth = static_data.circle_norm(0., 0., radius, offset.x, offset.y);
CreateShadow::func(dest, earth, sun, noise, 0);
}
};

}//namespace


Expand Down Expand Up @@ -976,14 +985,25 @@ Cord Globe::getSunDirection(double lon, double lat) const

void Globe::drawShadow()
{
ShaderMove<Cord> earth = ShaderMove<Cord>(_earthData[_zoom], getWidth(), getHeight());
ShaderRepeat<Sint16> noise = ShaderRepeat<Sint16>(_randomNoiseData, static_data.random_surf_size, static_data.random_surf_size);
if (Options::globeSurfaceCache)
{
ShaderMove<Cord> earth = ShaderMove<Cord>(_earthData[_zoom], getWidth(), getHeight());
ShaderRepeat<Sint16> noise = ShaderRepeat<Sint16>(_randomNoiseData, static_data.random_surf_size, static_data.random_surf_size);

earth.setMove(_cenX-getWidth()/2, _cenY-getHeight()/2);
earth.setMove(_cenX-getWidth()/2, _cenY-getHeight()/2);

lock();
ShaderDraw<CreateShadow>(ShaderSurface(this), earth, ShaderScalar(getSunDirection(_cenLon, _cenLat)), noise);
unlock();
lock();
ShaderDraw<CreateShadow>(ShaderSurface(this), earth, ShaderScalar(getSunDirection(_cenLon, _cenLat)), noise);
unlock();
}
else
{
ShaderRepeat<Sint16> noise = ShaderRepeat<Sint16>(_randomNoiseData, static_data.random_surf_size, static_data.random_surf_size);

lock();
ShaderDraw<CreateShadowWithoutCache>(ShaderSurface(this), helper::Offset(_cenX, _cenY), ShaderScalar(getSunDirection(_cenLon, _cenLat)), noise, ShaderScalar(_zoomRadius[_zoom]));
unlock();
}

}

Expand Down Expand Up @@ -1876,17 +1896,24 @@ void Globe::setupRadii(int width, int height)
_radius = _zoomRadius[_zoom];
_radiusStep = (_zoomRadius[DOGFIGHT_ZOOM] - _zoomRadius[0]) / 10.0;

_earthData.resize(_zoomRadius.size());
//filling normal field for each radius
if (Options::globeSurfaceCache)
{
_earthData.resize(_zoomRadius.size());
//filling normal field for each radius

for (size_t r = 0; r<_zoomRadius.size(); ++r)
for (size_t r = 0; r<_zoomRadius.size(); ++r)
{
_earthData[r].resize(width * height);
for (int j=0; j<height; ++j)
for (int i=0; i<width; ++i)
{
_earthData[r][width*j + i] = static_data.circle_norm(width/2, height/2, _zoomRadius[r], i+.5, j+.5);
}
}
}
else
{
_earthData[r].resize(width * height);
for (int j=0; j<height; ++j)
for (int i=0; i<width; ++i)
{
_earthData[r][width*j + i] = static_data.circle_norm(width/2, height/2, _zoomRadius[r], i+.5, j+.5);
}
_earthData.clear();
}
}

Expand Down

0 comments on commit 2d7828f

Please sign in to comment.