Skip to content

Commit

Permalink
Fix for the recursive registry creation caused by invoking capabiliti…
Browse files Browse the repository at this point in the history
…es during the ctor; other cleanups.
  • Loading branch information
gwaldron committed Apr 8, 2024
1 parent a9eb99c commit 5094ed0
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 105 deletions.
6 changes: 4 additions & 2 deletions src/osgEarth/GLUtils
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ namespace osgEarth
//! Pop a GL debugging group
static void popDebugGroup();

//! Whether to use NVGL when possible (and if supported)
//! Whether to use NVGL if available
static void useNVGL(bool value);
static bool useNVGL() { return _useNVGL; }

//! Whether NVGL is requested AND supported.
static bool useNVGL();

//! Unique ID associated with this State object (and by extension
//! its unique graphics context). You can use this to track
Expand Down
12 changes: 7 additions & 5 deletions src/osgEarth/GLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ bool GLUtils::_useNVGL = false;
void
GLUtils::useNVGL(bool value)
{
bool oldValue = _useNVGL;

_useNVGL =
value == true &&
Capabilities::get().supportsNVGL();
_useNVGL = value;

if (_useNVGL)
{
Expand All @@ -175,6 +171,12 @@ GLUtils::useNVGL(bool value)
}
}

bool
GLUtils::useNVGL()
{
return _useNVGL && Registry::capabilities().supportsNVGL();
}

namespace
{
struct Mapping {
Expand Down
7 changes: 0 additions & 7 deletions src/osgEarth/Registry
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
#include <unordered_set>
#include <unordered_map>

#define GDAL_SCOPED_LOCK \
std::lock_guard<osgEarth::Threading::RecursiveMutex> \
_oe_gdal_lock( osgEarth::getGDALMutex() )

namespace osgText {
class Font;
}
Expand All @@ -51,9 +47,6 @@ namespace osgEarth
class ShaderFactory;
}

//! Global mutex used to serialize access to GDAL/OGR/PROJ functionality
extern OSGEARTH_EXPORT Threading::RecursiveMutex& getGDALMutex();

/**
* Application-wide global repository.
*/
Expand Down
31 changes: 8 additions & 23 deletions src/osgEarth/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@
#include <cpl_conv.h>
#include <cstdlib>

// Fails to build on the GitHub Linux Action runner, so leaving out for now -gw
//#ifndef OSG_GL3_AVAILABLE
//#error osgEarth requires OpenSceneGraph built with OSG_GL3_AVAILABLE.
//#endif

using namespace osgEarth;

#define LC "[Registry] "
Expand Down Expand Up @@ -87,8 +82,6 @@ namespace
}

Registry::Registry() :
_caps(nullptr),
_defaultFont(nullptr),
_terrainEngineDriver("rex"),
_cacheDriver("filesystem"),
_overrideCachePolicyInitialized(false),
Expand All @@ -97,7 +90,6 @@ Registry::Registry() :
_maxImageDimension(INT_MAX)
{
OE_INFO << "Hello, world." << std::endl;
//OE_INFO << LC << "Registry starting up" << std::endl;

// set up GDAL and OGR.
OGRRegisterAll();
Expand Down Expand Up @@ -233,16 +225,10 @@ Registry::Registry() :
_maxVertsPerDrawable = 65536;
}

// use the GDAL global mutex?
if (getenv("OSGEARTH_DISABLE_GDAL_MUTEX"))
{
//getGDALMutex().disable();
}

// disable work stealing in the jobs system?
if (getenv("OSGEARTH_DISABLE_WORK_STEALING"))
if (getenv("OSGEARTH_ENABLE_WORK_STEALING"))
{
jobs::set_allow_work_stealing(false);
jobs::set_allow_work_stealing(true);
}

// register the system stock Units.
Expand Down Expand Up @@ -300,14 +286,19 @@ namespace
Registry*
Registry::instance()
{
if (g_registry_created == true && g_registry == nullptr)
{
OE_HARD_ASSERT(false, "Registry::instance() called recursively. Contact support.");
}

// Create registry the first time through, explicitly rather than depending on static object
// initialization order, which is undefined in c++ across separate compilation units. An
// explicit hook is registered to tear it down on exit. atexit() hooks are run on exit in
// the reverse order of their registration during setup.
if (!g_registry && !g_registry_created)
{
g_registry = new Registry();
g_registry_created = true;
g_registry = new Registry();
std::atexit(destroyRegistry);
}

Expand Down Expand Up @@ -348,12 +339,6 @@ Registry::release()
_objectIndex = nullptr;
}

Threading::RecursiveMutex& osgEarth::getGDALMutex()
{
static osgEarth::Threading::RecursiveMutex _gdal_mutex;
return _gdal_mutex;
}

const Profile*
Registry::getGlobalGeodeticProfile() const
{
Expand Down
8 changes: 4 additions & 4 deletions src/osgEarth/TerrainTileModelFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

using namespace osgEarth;

#define LABEL_IMAGERY "REX textures"
#define LABEL_NORMALMAP "REX textures"
#define LABEL_ELEVATION "REX textures"
#define LABEL_COVERAGE "REX textures"
#define LABEL_IMAGERY "Terrain textures"
#define LABEL_NORMALMAP "Terrain textures"
#define LABEL_ELEVATION "Terrain textures"
#define LABEL_COVERAGE "Terrain textures"

//.........................................................................

Expand Down
4 changes: 0 additions & 4 deletions src/osgEarth/TileIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ using namespace osgEarth;
using namespace osgEarth::Contrib;
using namespace std;

#define OGR_SCOPED_LOCK GDAL_SCOPED_LOCK

TileIndex::TileIndex()
{
}
Expand Down Expand Up @@ -77,8 +75,6 @@ TileIndex::create( const std::string& filename, const osgEarth::SpatialReference
// Make sure the registry is loaded since that is where the OGR/GDAL registration happens
osgEarth::Registry::instance();

OGR_SCOPED_LOCK;

OGRSFDriverH driver = OGRGetDriverByName( "ESRI Shapefile" );

//Create the datasource itself.
Expand Down
46 changes: 0 additions & 46 deletions src/osgEarth/Units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,52 +162,6 @@ Units::registerAll(Registry* r)
r->registerUnits(Units::PIXELS);
}

#if 0
// Factor converts unit into METERS:
const Units Units::CENTIMETERS ( "centimeters", "cm", Units::TYPE_LINEAR, 0.01 );
const Units Units::FEET ( "feet", "ft", Units::TYPE_LINEAR, 0.3048 );
const Units Units::FEET_US_SURVEY ( "feet(us)", "ft", Units::TYPE_LINEAR, 12.0/39.37 );
const Units Units::KILOMETERS ( "kilometers", "km", Units::TYPE_LINEAR, 1000.0 );
const Units Units::METERS ( "meters", "m", Units::TYPE_LINEAR, 1.0 );
const Units Units::MILES ( "miles", "mi", Units::TYPE_LINEAR, 1609.334 );
const Units Units::MILLIMETERS ( "millimeters", "mm", Units::TYPE_LINEAR, 0.001 );
const Units Units::YARDS ( "yards", "yd", Units::TYPE_LINEAR, 0.9144 );
const Units Units::NAUTICAL_MILES ( "nautical miles", "nm", Units::TYPE_LINEAR, 1852.0 );
const Units Units::DATA_MILES ( "data miles", "dm", Units::TYPE_LINEAR, 1828.8 );
const Units Units::INCHES ( "inches", "in", Units::TYPE_LINEAR, 0.0254 );
const Units Units::FATHOMS ( "fathoms", "fm", Units::TYPE_LINEAR, 1.8288 );
const Units Units::KILOFEET ( "kilofeet", "kf", Units::TYPE_LINEAR, 304.8 );
const Units Units::KILOYARDS ( "kiloyards", "kyd", Units::TYPE_LINEAR, 914.4 );

// Factor converts unit into RADIANS:
const Units Units::DEGREES ( "degrees", "\xb0",Units::TYPE_ANGULAR, 0.017453292519943295 );
const Units Units::RADIANS ( "radians", "rad", Units::TYPE_ANGULAR, 1.0 );
const Units Units::BAM ( "BAM", "bam", Units::TYPE_ANGULAR, 6.283185307179586476925286766559 );
const Units Units::NATO_MILS ( "mils", "mil", Units::TYPE_ANGULAR, 9.8174770424681038701957605727484e-4 );
const Units Units::DECIMAL_HOURS ( "hours", "h", Units::TYPE_ANGULAR, 15.0*0.017453292519943295 );

// Factor convert unit into SECONDS:
const Units Units::DAYS ( "days", "d", Units::TYPE_TEMPORAL, 86400.0 );
const Units Units::HOURS ( "hours", "hr", Units::TYPE_TEMPORAL, 3600.0 );
const Units Units::MICROSECONDS ( "microseconds", "us", Units::TYPE_TEMPORAL, 0.000001 );
const Units Units::MILLISECONDS ( "milliseconds", "ms", Units::TYPE_TEMPORAL, 0.001 );
const Units Units::MINUTES ( "minutes", "min", Units::TYPE_TEMPORAL, 60.0 );
const Units Units::SECONDS ( "seconds", "s", Units::TYPE_TEMPORAL, 1.0 );
const Units Units::WEEKS ( "weeks", "wk", Units::TYPE_TEMPORAL, 604800.0 );

const Units Units::FEET_PER_SECOND ( "feet per second", "ft/s", Units::FEET, Units::SECONDS );
const Units Units::YARDS_PER_SECOND ( "yards per second", "yd/s", Units::YARDS, Units::SECONDS );
const Units Units::METERS_PER_SECOND ( "meters per second", "m/s", Units::METERS, Units::SECONDS );
const Units Units::KILOMETERS_PER_SECOND( "kilometers per second", "km/s", Units::KILOMETERS, Units::SECONDS );
const Units Units::KILOMETERS_PER_HOUR ( "kilometers per hour", "kmh", Units::KILOMETERS, Units::HOURS );
const Units Units::MILES_PER_HOUR ( "miles per hour", "mph", Units::MILES, Units::HOURS );
const Units Units::DATA_MILES_PER_HOUR ( "data miles per hour", "dm/h", Units::DATA_MILES, Units::HOURS );
const Units Units::KNOTS ( "nautical miles per hour", "kts", Units::NAUTICAL_MILES, Units::HOURS );

const Units Units::PIXELS ( "pixels", "px", Units::TYPE_SCREEN_SIZE, 1.0 );
#endif


int
Units::unitTest()
{
Expand Down
4 changes: 2 additions & 2 deletions src/osgEarthDrivers/engine_rex/GeometryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ SharedGeometry::getOrCreateNVGLCommand(osg::State& state)
{
de._ebo = GLBuffer::create_shared(GL_ELEMENT_ARRAY_BUFFER_ARB, state);
de._ebo->bind();
de._ebo->debugLabel("REX geometry", "Shared EBO");
de._ebo->debugLabel("Terrain geometry", "Shared EBO");
de._ebo->bufferStorage(_drawElements->getTotalDataSize(), _drawElements->getDataPointer(), 0);
de._ebo->unbind();

Expand All @@ -364,7 +364,7 @@ SharedGeometry::getOrCreateNVGLCommand(osg::State& state)
gs._vbo = GLBuffer::create_shared(GL_ARRAY_BUFFER_ARB, state, size);

gs._vbo->bind();
gs._vbo->debugLabel("REX geometry", "Shared VBO");
gs._vbo->debugLabel("Terrain geometry", "Shared VBO");
gs._vbo->bufferStorage(size, _verts.data());
gs._vbo->unbind();

Expand Down
8 changes: 4 additions & 4 deletions src/osgEarthDrivers/engine_rex/LayerDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ LayerDrawableNVGL::drawImplementation(osg::RenderInfo& ri) const

gl.tiles = GLBuffer::create(GL_SHADER_STORAGE_BUFFER, state);
gl.tiles->bind();
gl.tiles->debugLabel("REX geometry", "Tiles SSBO");
gl.tiles->debugLabel("Terrain geometry", "Tiles SSBO");

// preallocate space for a bunch of tiles (just for fun)
gl.tiles->bufferData(
Expand All @@ -362,7 +362,7 @@ LayerDrawableNVGL::drawImplementation(osg::RenderInfo& ri) const
{
gl.commands = GLBuffer::create(GL_DRAW_INDIRECT_BUFFER, state);
gl.commands->bind();
gl.commands->debugLabel("REX geometry", "GL_DRAW_INDIRECT_BUFFER");
gl.commands->debugLabel("Terrain geometry", "GL_DRAW_INDIRECT_BUFFER");
// preallocate space for a bunch of draw commands (just for fun)
gl.commands->bufferData(
512 * sizeof(DrawElementsIndirectBindlessCommandNV),
Expand Down Expand Up @@ -390,7 +390,7 @@ LayerDrawableNVGL::drawImplementation(osg::RenderInfo& ri) const
gl.vao->bind();

// after the bind, please
gl.vao->debugLabel("REX geometry", "VAO");
gl.vao->debugLabel("Terrain geometry", "VAO");

// set up the VAO for NVIDIA bindless buffers
glEnableClientState_(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
Expand Down Expand Up @@ -435,7 +435,7 @@ LayerDrawableNVGL::drawImplementation(osg::RenderInfo& ri) const
gl.shared = GLBuffer::create(GL_SHADER_STORAGE_BUFFER, state);

gl.shared->bind();
gl.shared->debugLabel("REX geometry", "Global data SSBO");
gl.shared->debugLabel("Terrain geometry", "Global data SSBO");
gl.shared->bufferStorage(sizeof(GL4GlobalData), &buf, 0); // permanent
gl.shared->unbind();
}
Expand Down
16 changes: 8 additions & 8 deletions src/osgEarthDrivers/engine_rex/RexTerrainEngineNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,20 @@ RexTerrainEngineNode::RexTerrainEngineNode() :

// static shaders.
osg::StateSet* stateset = getOrCreateStateSet();
stateset->setName("REX node");
stateset->setName("Terrain node");
//VirtualProgram* vp = VirtualProgram::getOrCreate(stateset);
//vp->setName(typeid(*this).name());
//vp->setIsAbstract(true); // cannot run by itself, requires additional children

_surfaceSS = new osg::StateSet();
_surfaceSS->setName("REX surface");
_surfaceSS->setName("Terrain surface");

_imageLayerSS = new osg::StateSet();
_imageLayerSS->setName("REX image layer");
_imageLayerSS->setName("Terrain image layer");

_terrain = new osg::Group();
_terrainSS = _terrain->getOrCreateStateSet();
_terrainSS->setName("REX terrain");
_terrainSS->setName("Terrain terrain");

addChild(_terrain.get());

Expand Down Expand Up @@ -647,7 +647,7 @@ RexTerrainEngineNode::setupRenderBindings()
color.samplerName() = "oe_layer_tex";
color.matrixName() = "oe_layer_texMatrix";
color.setDefaultTexture(new osg::Texture2D(ImageUtils::createEmptyImage(1, 1)));
color.getDefaultTexture()->setName("rex default color");
color.getDefaultTexture()->setName("terrain default color");

if (!GLUtils::useNVGL())
getResources()->reserveTextureImageUnit(color.unit(), "Terrain Color");
Expand All @@ -659,7 +659,7 @@ RexTerrainEngineNode::setupRenderBindings()
elevation.samplerName() = "oe_tile_elevationTex";
elevation.matrixName() = "oe_tile_elevationTexMatrix";
elevation.setDefaultTexture(osgEarth::createEmptyElevationTexture());
elevation.getDefaultTexture()->setName("rex default elevation");
elevation.getDefaultTexture()->setName("terrain default elevation");

if (!GLUtils::useNVGL())
getResources()->reserveTextureImageUnit(elevation.unit(), "Terrain Elevation");
Expand All @@ -672,7 +672,7 @@ RexTerrainEngineNode::setupRenderBindings()
normal.samplerName() = "oe_tile_normalTex";
normal.matrixName() = "oe_tile_normalTexMatrix";
normal.setDefaultTexture(osgEarth::createEmptyNormalMapTexture());
normal.getDefaultTexture()->setName("rex default normalmap");
normal.getDefaultTexture()->setName("terrain default normalmap");

if (!GLUtils::useNVGL())
getResources()->reserveTextureImageUnit(normal.unit(), "Terrain Normals");
Expand All @@ -696,7 +696,7 @@ RexTerrainEngineNode::setupRenderBindings()
landCover.samplerName() = "oe_tile_landCoverTex";
landCover.matrixName() = "oe_tile_landCoverTexMatrix";
landCover.setDefaultTexture(LandCover::createEmptyTexture());
landCover.getDefaultTexture()->setName("rex default landcover");
landCover.getDefaultTexture()->setName("terrain default landcover");
getOrCreateStateSet()->setDefine("OE_LANDCOVER_TEX", landCover.samplerName());
getOrCreateStateSet()->setDefine("OE_LANDCOVER_TEX_MATRIX", landCover.matrixName());

Expand Down

0 comments on commit 5094ed0

Please sign in to comment.