osg::GraphicsContext, in order to give good integration with the application's GUI toolkit. This works really well. However, I need to share OpenGL texture resources with the standard osgViewer GraphicsContext implementations, in particular the PixelBuffers. This is essential for my application to conserve graphics memory on low-end hardware. Currently the standard osg implementations will not share resources with another derived osg::GraphicsContext, other than the pre-defined osgViewer classes e.g. PixelBufferX11 is hardcoded to only share resources with GraphicsWindowX11 and PixelBufferX11 objects, and no other osg::GraphicsContext object. To address this in the cleanest way I could think of, I have moved the OpenGL handle variables for each platform into a small utility class, e.g. GraphicsHandleX11 for unix. Then GraphicsWindowX11, PixelBufferX11 and any other derived osg::GraphicsContext class can inherit from GraphicsHandleX11 to share OpenGL resources. I have updated the X11, Win32 and Carbon implementations to use this. The changes are minor. I haven't touched the Cocoa implmentation as I'm not familiar with it at all and couldn't test it - it will work unchanged. Without this I had some horrible hacks in my application, this greatly simplifies things for me. It also simplifies the osgViewer implementations slightly. Perhaps it may help with other users' desires to share resources with external graphics contexts, as was discussed on the user list recently." Notes from Robert Osfield, adapted Colin's submission to work with the new EGL related changes.
164 lines
5.6 KiB
C++
164 lines
5.6 KiB
C++
/*
|
|
* PixelBufferCarbon.cpp
|
|
* OpenSceneGraph
|
|
*
|
|
* Created by Stephan Huber on 27.06.07.
|
|
* Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#if defined (__APPLE__) && (!__LP64__)
|
|
|
|
#include <osg/observer_ptr>
|
|
#include <osgViewer/api/Carbon/PixelBufferCarbon>
|
|
#include <osgViewer/api/Carbon/GraphicsWindowCarbon>
|
|
#include <Carbon/Carbon.h>
|
|
#include <OpenGL/OpenGL.h>
|
|
using namespace osgViewer;
|
|
|
|
|
|
|
|
|
|
/** creates a pixelformat from a Trait */
|
|
AGLPixelFormat PixelBufferCarbon::createPixelFormat(osg::GraphicsContext::Traits* traits) {
|
|
|
|
std::vector<GLint> attributes;
|
|
|
|
attributes.push_back(AGL_NO_RECOVERY);
|
|
attributes.push_back(AGL_RGBA);
|
|
if (!traits->pbuffer)
|
|
attributes.push_back(AGL_COMPLIANT);
|
|
else
|
|
attributes.push_back(AGL_CLOSEST_POLICY);
|
|
|
|
if (traits->doubleBuffer) attributes.push_back(AGL_DOUBLEBUFFER);
|
|
if (traits->quadBufferStereo) attributes.push_back(AGL_STEREO);
|
|
|
|
attributes.push_back(AGL_RED_SIZE); attributes.push_back(traits->red);
|
|
attributes.push_back(AGL_GREEN_SIZE); attributes.push_back(traits->green);
|
|
attributes.push_back(AGL_BLUE_SIZE); attributes.push_back(traits->blue);
|
|
attributes.push_back(AGL_DEPTH_SIZE); attributes.push_back(traits->depth);
|
|
|
|
if (traits->alpha) { attributes.push_back(AGL_ALPHA_SIZE); attributes.push_back(traits->alpha); }
|
|
|
|
if (traits->stencil) { attributes.push_back(AGL_STENCIL_SIZE); attributes.push_back(traits->stencil); }
|
|
|
|
// TODO
|
|
// missing accumulation-buffer-stuff
|
|
|
|
#if defined(AGL_SAMPLE_BUFFERS_ARB) && defined (AGL_SAMPLES_ARB)
|
|
|
|
if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLE_BUFFERS_ARB); attributes.push_back(traits->sampleBuffers); }
|
|
if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLES_ARB); attributes.push_back(traits->samples); }
|
|
|
|
#endif
|
|
attributes.push_back(AGL_NONE);
|
|
|
|
return aglChoosePixelFormat(NULL, 0, &(attributes.front()));
|
|
}
|
|
|
|
|
|
void PixelBufferCarbon::init()
|
|
{
|
|
_context = NULL;
|
|
_pixelformat = PixelBufferCarbon::createPixelFormat(_traits.get());
|
|
if (!_pixelformat)
|
|
osg::notify(osg::WARN) << "PixelBufferCarbon::init could not create a valid pixelformat" << std::endl;
|
|
_valid = (_pixelformat != NULL);
|
|
}
|
|
|
|
|
|
/** This is the class we need to create for pbuffers, note its not a GraphicsWindow as it won't need any of the event handling and window mapping facilities.*/
|
|
/** Realise the GraphicsContext implementation,
|
|
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
|
|
bool PixelBufferCarbon::realizeImplementation()
|
|
{
|
|
if (!_valid) {
|
|
osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl;
|
|
return false;
|
|
}
|
|
|
|
AGLContext sharedContext = NULL;
|
|
|
|
// get any shared AGL contexts
|
|
GraphicsHandleCarbon* graphicsHandleCarbon = dynamic_cast<GraphicsHandleCarbon*>(_traits->sharedContext);
|
|
if (graphicsHandleCarbon)
|
|
{
|
|
sharedContext = graphicsHandleCarbon->getAGLContext();
|
|
}
|
|
|
|
_context = aglCreateContext (_pixelformat, sharedContext);
|
|
|
|
if (!_context) {
|
|
osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl;
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
_realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer));
|
|
if (!_realized) {
|
|
osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
|
|
}
|
|
|
|
makeCurrentImplementation();
|
|
|
|
_realized = aglSetPBuffer(_context, _pbuffer, _traits->face, _traits->level, 0);
|
|
if (!_realized) {
|
|
osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglSetPBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
|
|
}
|
|
return _realized;
|
|
}
|
|
|
|
void PixelBufferCarbon::closeImplementation()
|
|
{
|
|
if (_pbuffer) aglDestroyPBuffer(_pbuffer);
|
|
if (_context) aglDestroyContext(_context);
|
|
if (_pixelformat) aglDestroyPixelFormat(_pixelformat);
|
|
_valid = _realized = false;
|
|
}
|
|
|
|
/** Make this graphics context current implementation.
|
|
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
|
|
bool PixelBufferCarbon::makeCurrentImplementation()
|
|
{
|
|
return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false;
|
|
}
|
|
|
|
/** Make this graphics context current with specified read context implementation.
|
|
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
|
|
bool PixelBufferCarbon::makeContextCurrentImplementation(GraphicsContext* /*readContext*/) {
|
|
return makeCurrentImplementation();
|
|
}
|
|
|
|
/** Release the graphics context.*/
|
|
bool PixelBufferCarbon::releaseContextImplementation()
|
|
{
|
|
return (aglSetCurrentContext(NULL) == GL_TRUE);
|
|
}
|
|
|
|
|
|
/** Pure virtual, Bind the graphics context to associated texture implementation.
|
|
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
|
|
void PixelBufferCarbon::bindPBufferToTextureImplementation( GLenum buffer ){
|
|
|
|
osg::notify(osg::NOTICE)<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl;
|
|
}
|
|
|
|
/** Swap the front and back buffers implementation.
|
|
* Pure virtual - must be implemented by Concrate implementations of GraphicsContext. */
|
|
void PixelBufferCarbon::swapBuffersImplementation()
|
|
{
|
|
aglSwapBuffers(_context);
|
|
}
|
|
|
|
|
|
PixelBufferCarbon::~PixelBufferCarbon()
|
|
{
|
|
close(true);
|
|
}
|
|
|
|
|
|
|
|
#endif
|