Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rare segfault in Text::drawImplementationSinglePass fixed: _textureGl… #1148

Open
wants to merge 1 commit into
base: OpenSceneGraph-3.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions include/osgText/Text
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public:
BackdropImplementation getBackdropImplementation() const { return DELAYED_DEPTH_WRITES; }

// internal structures, variable and methods used for rendering of characters.
struct OSGTEXT_EXPORT GlyphQuads
struct OSGTEXT_EXPORT GlyphQuads : public osg::Referenced
{
typedef std::vector<Glyph*> Glyphs;

Expand All @@ -222,16 +222,14 @@ public:
GlyphQuads();
GlyphQuads(const GlyphQuads& gq);

void setupPrimitives(Text::BackdropType backdropType);

Glyphs& getGlyphs() { return _glyphs; }
const Glyphs& getGlyphs() const { return _glyphs; }

/** Resize any per context GLObject buffers to specified size. */
void resizeGLObjectBuffers(unsigned int maxSize);

/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* the specified graphics context. Otherwise, releases OpenGL objects
* for all graphics contexts. */
void releaseGLObjects(osg::State* state=0) const;

Expand All @@ -240,15 +238,15 @@ public:
GlyphQuads& operator = (const GlyphQuads&) { return *this; }
};

typedef std::map<osg::ref_ptr<GlyphTexture>,GlyphQuads> TextureGlyphQuadMap;
typedef std::map< osg::ref_ptr<GlyphTexture>, osg::ref_ptr<GlyphQuads> > TextureGlyphQuadMap;

/** Direct Access to GlyphQuads */
const GlyphQuads* getGlyphQuads(GlyphTexture* texture) const
{
TextureGlyphQuadMap::const_iterator itGlyphQuad = _textureGlyphQuadMap.find(texture);
if (itGlyphQuad == _textureGlyphQuadMap.end()) return NULL;

return &itGlyphQuad->second;
return itGlyphQuad->second.get();
}

const TextureGlyphQuadMap& getTextureGlyphQuadMap() const
Expand Down
81 changes: 47 additions & 34 deletions src/osgText/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* OpenSceneGraph Public License for more details.
*/


#include <climits>
#include <osgText/Text>

#include <osg/Math>
Expand Down Expand Up @@ -393,18 +393,38 @@ void Text::addGlyphQuad(Glyph* glyph, const osg::Vec2& minc, const osg::Vec2& ma
// set up the coords of the quad
const Glyph::TextureInfo* info = glyph->getOrCreateTextureInfo(_shaderTechnique);
GlyphTexture* glyphTexture = info ? info->texture : 0;
GlyphQuads& glyphquad = _textureGlyphQuadMap[glyphTexture];

glyphquad._glyphs.push_back(glyph);
osg::ref_ptr<osg::DrawElements> primitives;

TextureGlyphQuadMap::iterator gqIter = _textureGlyphQuadMap.find(glyphTexture);
if (gqIter != _textureGlyphQuadMap.end()) // Found in the map
{
osg::ref_ptr<GlyphQuads> gq = gqIter->second;
gq->_glyphs.push_back(glyph);

osg::DrawElements* primitives = glyphquad._primitives.get();
if (!primitives)
if (gq->_primitives->getType() != osg::PrimitiveSet::DrawElementsUIntPrimitiveType && _text.size()*4 >= USHRT_MAX)
{
primitives = new osg::DrawElementsUInt(GL_TRIANGLES);
primitives->setBufferObject(_ebo.get());
gq->_primitives = primitives; // The same buffer object will be used, so we should not worry about releaseGLObjects()
}
else
primitives = gq->_primitives.get();
}
else
{
unsigned int maxIndices = _text.size()*4;
if (maxIndices>=16384) primitives = new osg::DrawElementsUInt(GL_TRIANGLES);
else primitives = new osg::DrawElementsUShort(GL_TRIANGLES);
osg::ref_ptr<GlyphQuads> gq = new GlyphQuads;
gq->_glyphs.push_back(glyph);

if (_text.size()*4 >= USHRT_MAX)
primitives = new osg::DrawElementsUInt(GL_TRIANGLES);
else
primitives = new osg::DrawElementsUShort(GL_TRIANGLES);

primitives->setBufferObject(_ebo.get());
glyphquad._primitives = primitives;
gq->_primitives = primitives;

_textureGlyphQuadMap[glyphTexture] = gq; // Insert new GlyphQuads with valid _primitives
}


Expand Down Expand Up @@ -448,12 +468,12 @@ void Text::computeGlyphRepresentation()
itr != _textureGlyphQuadMap.end();
++itr)
{
GlyphQuads& glyphquads = itr->second;
glyphquads._glyphs.clear();
if (glyphquads._primitives.valid())
osg::ref_ptr<GlyphQuads> gq = itr->second;
gq->_glyphs.clear();
if (gq->_primitives.valid())
{
glyphquads._primitives->resizeElements(0);
glyphquads._primitives->dirty();
gq->_primitives->resizeElements(0);
gq->_primitives->dirty();
}
}

Expand Down Expand Up @@ -1133,7 +1153,7 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
// need to set the texture here...
state.applyTextureAttribute(0,titr->first.get());

const GlyphQuads& glyphquad = titr->second;
const GlyphQuads& glyphquad = *(titr->second);

if(_colorGradientMode == SOLID)
{
Expand All @@ -1148,7 +1168,8 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
}
}

glyphquad._primitives->draw(state, usingVertexBufferObjects);
osg::ref_ptr<osg::DrawElements> primitives = glyphquad._primitives;
primitives->draw(state, usingVertexBufferObjects);
}
}
}
Expand Down Expand Up @@ -1269,22 +1290,14 @@ void Text::accept(osg::PrimitiveFunctor& pf) const
titr!=_textureGlyphQuadMap.end();
++titr)
{
const GlyphQuads& glyphquad = titr->second;
if (glyphquad._primitives.valid())
const GlyphQuads& glyphquad = *(titr->second);
osg::ref_ptr<osg::DrawElements> drawElements = glyphquad._primitives;
if (drawElements.valid() && drawElements->getNumIndices() > 0)
{
const osg::DrawElementsUShort* drawElementsUShort = dynamic_cast<const osg::DrawElementsUShort*>(glyphquad._primitives.get());
if (drawElementsUShort && drawElementsUShort->size() > 0)
{
pf.drawElements(GL_TRIANGLES, drawElementsUShort->size(), &(drawElementsUShort->front()));
}
else
{
const osg::DrawElementsUInt* drawElementsUInt = dynamic_cast<const osg::DrawElementsUInt*>(glyphquad._primitives.get());
if (drawElementsUInt && drawElementsUInt->size() > 0)
{
pf.drawElements(GL_TRIANGLES, drawElementsUInt->size(), &(drawElementsUInt->front()));
}
}
if (drawElements->getType() == osg::PrimitiveSet::DrawElementsUShortPrimitiveType)
pf.drawElements(GL_TRIANGLES, drawElements->getNumIndices(), (const GLushort*)(drawElements->getDataPointer()));
else if (drawElements->getType() == osg::PrimitiveSet::DrawElementsUIntPrimitiveType)
pf.drawElements(GL_TRIANGLES, drawElements->getNumIndices(), (const GLuint*)(drawElements->getDataPointer()));
}
}
}
Expand Down Expand Up @@ -1312,7 +1325,7 @@ void Text::resizeGLObjectBuffers(unsigned int maxSize)
itr != _textureGlyphQuadMap.end();
++itr)
{
itr->second.resizeGLObjectBuffers(maxSize);
itr->second->resizeGLObjectBuffers(maxSize);
}
}

Expand All @@ -1324,7 +1337,7 @@ void Text::releaseGLObjects(osg::State* state) const
itr != _textureGlyphQuadMap.end();
++itr)
{
itr->second.releaseGLObjects(state);
itr->second->releaseGLObjects(state);
}
}

Expand Down Expand Up @@ -1405,7 +1418,7 @@ Text::GlyphQuads::GlyphQuads()
{
}

Text::GlyphQuads::GlyphQuads(const GlyphQuads&)
Text::GlyphQuads::GlyphQuads(const GlyphQuads& glyphQuads)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/osgWidget/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void Input::_calculateCursorOffsets() {
std::vector<osgText::Glyph*> glyphs;
for ( ; tgqmi != tgqm.end(); tgqmi++ )
{
const osgText::Text::GlyphQuads& gq = tgqmi->second;
const osgText::Text::GlyphQuads& gq = *(tgqmi->second);

//coords.insert(coords.end(),gq.getTransformedCoords(0).begin(),gq.getTransformedCoords(0).end());
for (unsigned int i=0; i<gq.getGlyphs().size(); ++i)
Expand Down