Skip to content

Commit

Permalink
Partial mergeback to update single-gl-context mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Jul 11, 2023
1 parent 351e563 commit ed410ee
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 206 deletions.
2 changes: 2 additions & 0 deletions src/osgEarth/Chonk
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ namespace osgEarth
virtual const char* libraryName() const override { return "osgEarth"; }
virtual const char* className() const override { return "ChonkRenderBin"; }

static void releaseSharedGLObjects(osg::State* state);

private:
osg::ref_ptr<osg::StateSet> _cullSS;

Expand Down
9 changes: 9 additions & 0 deletions src/osgEarth/Chonk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ ChonkDrawable::GLObjects::draw(osg::State& state)
void
ChonkDrawable::GLObjects::release()
{
_ext = nullptr;
_vao = nullptr;
_commandBuf = nullptr;
_instanceInputBuf = nullptr;
Expand Down Expand Up @@ -1274,3 +1275,11 @@ ChonkRenderBin::drawImplementation(
// dispatch.
osgUtil::RenderBin::drawImplementation(ri, previous);
}

void
ChonkRenderBin::releaseSharedGLObjects(osg::State* state)
{
auto proto = static_cast<ChonkRenderBin*>(osgUtil::RenderBin::getRenderBinPrototype("ChonkBin"));
if (proto->_cullSS.valid())
proto->_cullSS->releaseGLObjects(state);
}
80 changes: 47 additions & 33 deletions src/osgEarth/GLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ using namespace osgEarth;

#define LC "[GLUtils] "

//#define USE_RECYCLING

#define OE_DEVEL OE_DEBUG

#ifndef GL_LINE_SMOOTH
Expand Down Expand Up @@ -78,31 +80,31 @@ namespace
struct
{
typedef void (GL_APIENTRY* DebugProc)(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, const void*);
void (GL_APIENTRY * DebugMessageCallback)(DebugProc, const void*);
void (GL_APIENTRY * DebugMessageControl)(GLenum, GLenum, GLenum, GLsizei, const GLuint*, bool);
void (GL_APIENTRY * PushDebugGroup)(GLenum, GLuint, GLsizei, const char*);
void (GL_APIENTRY * PopDebugGroup)(void);

void (GL_APIENTRY* DebugMessageCallback)(DebugProc, const void*);
void (GL_APIENTRY* DebugMessageControl)(GLenum, GLenum, GLenum, GLsizei, const GLuint*, bool);
void (GL_APIENTRY* PushDebugGroup)(GLenum, GLuint, GLsizei, const char*);
void (GL_APIENTRY* PopDebugGroup)(void);

// NV_shader_buffer_load
// https://developer.download.nvidia.com/opengl/specs/GL_NV_shader_buffer_load.txt
void (GL_APIENTRY * MakeNamedBufferResidentNV)(GLuint name, GLenum access);
void (GL_APIENTRY * MakeNamedBufferNonResidentNV)(GLuint name);
void (GL_APIENTRY * GetNamedBufferParameterui64vNV)(GLenum name, GLenum pname, GLuint64* params);
void (GL_APIENTRY* MakeNamedBufferResidentNV)(GLuint name, GLenum access);
void (GL_APIENTRY* MakeNamedBufferNonResidentNV)(GLuint name);
void (GL_APIENTRY* GetNamedBufferParameterui64vNV)(GLenum name, GLenum pname, GLuint64* params);

void (GL_APIENTRY * MakeBufferResidentNV)(GLuint name, GLenum access);
void (GL_APIENTRY * MakeBufferNonResidentNV)(GLuint name);
void (GL_APIENTRY * GetBufferParameterui64vNV)(GLenum target, GLenum pname, GLuint64* params);
void (GL_APIENTRY* MakeBufferResidentNV)(GLuint name, GLenum access);
void (GL_APIENTRY* MakeBufferNonResidentNV)(GLuint name);
void (GL_APIENTRY* GetBufferParameterui64vNV)(GLenum target, GLenum pname, GLuint64* params);

void (GL_APIENTRY * NamedBufferData)(GLuint name, GLsizeiptr size, const void* data, GLenum usage);
void (GL_APIENTRY * NamedBufferSubData)(GLuint name, GLintptr offset ,GLsizeiptr size, const void* data);
void*(GL_APIENTRY * MapNamedBuffer)(GLuint name, GLbitfield access);
void*(GL_APIENTRY * MapNamedBufferRange)(GLuint name, GLintptr offset, GLsizeiptr length, GLbitfield access);
void (GL_APIENTRY * UnmapNamedBuffer)(GLuint name);
void (GL_APIENTRY* NamedBufferData)(GLuint name, GLsizeiptr size, const void* data, GLenum usage);
void (GL_APIENTRY* NamedBufferSubData)(GLuint name, GLintptr offset, GLsizeiptr size, const void* data);
void* (GL_APIENTRY* MapNamedBuffer)(GLuint name, GLbitfield access);
void* (GL_APIENTRY* MapNamedBufferRange)(GLuint name, GLintptr offset, GLsizeiptr length, GLbitfield access);
void (GL_APIENTRY* UnmapNamedBuffer)(GLuint name);

void (GL_APIENTRY * CopyBufferSubData)(GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizei size);
void (GL_APIENTRY * CopyNamedBufferSubData)(GLuint readName, GLuint writeName, GLintptr readOffset, GLintptr writeOffset, GLsizei size);
void (GL_APIENTRY * GetNamedBufferSubData)(GLuint name, GLintptr offset, GLsizei size, void*);
void (GL_APIENTRY* CopyBufferSubData)(GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizei size);
void (GL_APIENTRY* CopyNamedBufferSubData)(GLuint readName, GLuint writeName, GLintptr readOffset, GLintptr writeOffset, GLsizei size);
void (GL_APIENTRY* GetNamedBufferSubData)(GLuint name, GLintptr offset, GLsizei size, void*);

bool useNamedBuffers;

Expand Down Expand Up @@ -166,6 +168,10 @@ GLUtils::useNVGL(bool value)
{
OE_INFO << LC << "Using NVIDIA GL4 extensions" << std::endl;
}
else
{
OE_INFO << LC << "Disabling NVIDIA GL4 extensions" << std::endl;
}
}

namespace
Expand Down Expand Up @@ -196,7 +202,7 @@ GLUtils::getUniqueStateID(const osg::State& state)
return 0;
}

unsigned
unsigned
GLUtils::getSharedContextID(const osg::State& state)
{
#ifdef OSGEARTH_SINGLE_GL_CONTEXT
Expand Down Expand Up @@ -303,7 +309,7 @@ GLUtils::remove(osg::StateSet* stateSet, GLenum cap)
return;

#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
switch(cap)
switch (cap)
{
case GL_LIGHTING:
stateSet->removeMode(GL_LIGHTING);
Expand Down Expand Up @@ -331,8 +337,8 @@ GLUtils::remove(osg::StateSet* stateSet, GLenum cap)
}
#endif

switch(cap)
{
switch (cap)
{
case GL_LIGHTING:
stateSet->removeDefine(OE_LIGHTING_DEFINE);
break;
Expand Down Expand Up @@ -730,7 +736,7 @@ GLObjectPool::releaseOrphans(const osg::GraphicsContext* gc)
_objects.resize(_objects.size() - 1);
}
else
{
{
bytes += object->size();
++i;
}
Expand Down Expand Up @@ -915,6 +921,7 @@ GLBuffer::create(
osg::State& state,
GLsizei sizeHint)
{
#ifdef USE_RECYCLING
const GLObject::Compatible comp = [sizeHint](GLObject* obj) {
return
obj->ns() == GL_BUFFER &&
Expand All @@ -934,6 +941,9 @@ GLBuffer::create(
object->_recyclable = true;
}
return object;
#else
return create(target, state);
#endif
}

void
Expand Down Expand Up @@ -1242,10 +1252,11 @@ GLTexture::create(GLenum target, osg::State& state)

GLTexture::Ptr
GLTexture::create(
GLenum target,
osg::State& state,
GLenum target,
osg::State& state,
const Profile& profileHint)
{
#ifdef USE_RECYCLING
const GLObject::Compatible comp = [profileHint](GLObject* obj) {
return
obj->ns() == GL_TEXTURE &&
Expand All @@ -1264,6 +1275,9 @@ GLTexture::create(
object->_recyclable = true;
}
return object;
#else
return create(target, state);
#endif
}

void
Expand Down Expand Up @@ -1313,7 +1327,7 @@ GLTexture::makeResident(const osg::State& state, bool toggle)
else
ext()->glMakeTextureHandleNonResident(_handle);

OE_DEVEL << "'" << id() << "' name=" << name() <<" resident=" << (toggle ? "yes" : "no") << std::endl;
OE_DEVEL << "'" << id() << "' name=" << name() << " resident=" << (toggle ? "yes" : "no") << std::endl;

resident = toggle;
}
Expand Down Expand Up @@ -1367,13 +1381,13 @@ GLTexture::storage2D(const Profile& profile)
profile._internalFormat,
profile._width,
profile._height);

glTexParameteri(_target, GL_TEXTURE_MIN_FILTER, profile._minFilter);
glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, profile._magFilter);
glTexParameteri(_target, GL_TEXTURE_WRAP_S, profile._wrapS);
glTexParameteri(_target, GL_TEXTURE_WRAP_T, profile._wrapT);
glTexParameterf(_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, profile._maxAnisotropy);

// special trick: signed RGTC1 textures are compressed normals, so
// swizzle the A component to be a zero so we can detect them.
if (profile._internalFormat == GL_COMPRESSED_SIGNED_RED_RGTC1_EXT ||
Expand All @@ -1397,8 +1411,8 @@ GLTexture::storage3D(const Profile& profile)
_profile = profile; // Profile(_target, mipLevels, internalFormat, s, t, r, 0);

ext()->glTexStorage3D(
_target,
profile._numMipmapLevels,
_target,
profile._numMipmapLevels,
profile._internalFormat,
profile._width,
profile._height,
Expand Down Expand Up @@ -1474,7 +1488,7 @@ GLTexture::Ptr
GLFBO::renderToTexture(
GLsizei width,
GLsizei height,
DrawFunction draw,
DrawFunction draw,
osg::State& state)
{
// http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
Expand Down Expand Up @@ -1670,7 +1684,7 @@ ComputeImageSession::setImage(osg::Image* image)
_image = image;
_tex->setImage(image);
_stateSet->setAttribute(new osg::BindImageTexture(
0, _tex, osg::BindImageTexture::READ_WRITE,
0, _tex, osg::BindImageTexture::READ_WRITE,
image->getInternalTextureFormat(), 0, GL_TRUE));
image->dirty();
}
Expand Down
7 changes: 2 additions & 5 deletions src/osgEarth/MapNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <osgEarth/MapNodeObserver>
#include <osgEarth/Utils>
#include <osgEarth/Shaders>
#include <osgEarth/Chonk>
#include <osgUtil/Optimizer>
#include <osgDB/DatabasePager>
#include <osgEarth/HTTPClient>
Expand Down Expand Up @@ -977,11 +978,7 @@ MapNode::releaseGLObjects(osg::State* state) const
for(const osg::Callback* ec = getEventCallback(); ec; ec = ec->getNestedCallback())
ec->releaseGLObjects(state);

// inform the GL object pools for this context
if (state)
{
GLObjectPool::releaseGLObjects(state);
}
ChonkRenderBin::releaseSharedGLObjects(state);

osg::Group::releaseGLObjects(state);
}
Expand Down
19 changes: 9 additions & 10 deletions src/osgEarth/TextureArena
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace osgEarth
//! GL memory functions
void compileGLObjects(osg::State&) const;
void resizeGLObjectBuffers(unsigned);
void releaseGLObjects(osg::State*, bool force=false) const;
void releaseGLObjects(osg::State*, bool force = false) const;

OE_PROPERTY(std::string, name);
OE_PROPERTY(std::string, category);
Expand All @@ -87,7 +87,7 @@ namespace osgEarth
OE_PROPERTY(bool, clamp);
OE_PROPERTY(bool, keepImage);
OE_PROPERTY(unsigned, maxDim);

OE_OPTION(URI, uri);
OE_OPTION(GLenum, internalFormat);
OE_OPTION(float, maxAnisotropy);
Expand Down Expand Up @@ -128,7 +128,7 @@ namespace osgEarth
* After that, you can call activate() or deactivate() on individual textures
* to control their availability on the GPU.
*/
#define OE_TEXTURE_ARENA_SA_TYPE_ID (osg::StateAttribute::Type)(osg::StateAttribute::CAPABILITY+8675309)
#define OE_TEXTURE_ARENA_SA_TYPE_ID (osg::StateAttribute::Type)(osg::StateAttribute::CAPABILITY+8675309)

class OSGEARTH_EXPORT TextureArena : public osg::StateAttribute
{
Expand Down Expand Up @@ -165,7 +165,7 @@ namespace osgEarth

//! Find the texture at index i.
Texture::Ptr find(unsigned index) const;

//! Number of textures registered
size_t size() const { return _textures.size(); }

Expand All @@ -191,22 +191,21 @@ namespace osgEarth

private:
//! disable copy
TextureArena(const TextureArena&, const osg::CopyOp&) { }
TextureArena(const TextureArena&, const osg::CopyOp&) { }

void releaseGLObjects(osg::State*, bool force) const;

// GL objects that can be shared across contexts
struct GLObjects : public BindlessShareableGLObjects
{
GLObjects() : _inUse(false), _lastAppliedFrame(-1) { }
bool _inUse;
bool _inUse = false;
bool _handleBufferDirty = true;
int _lastAppliedFrame = -1;

std::queue<int> _toCompile;
TextureVector _toRemove;

GLBuffer::Ptr _handleBuffer;
std::vector<GLuint64> _handles;
bool _dirty;
int _lastAppliedFrame;
};
mutable osg::buffered_object<GLObjects> _globjects;

Expand Down
Loading

0 comments on commit ed410ee

Please sign in to comment.