From 915a645761ab2dcfb976d70d6193c7aa8cef50b2 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 10 Nov 2008 20:06:27 +0000 Subject: [PATCH] First cut of osgmemorytest example that is written to allocate many windows/pbuffer/GL objects as test of how many objects can be allocated on a machine. --- examples/CMakeLists.txt | 3 +- examples/osgmemorytest/CMakeLists.txt | 4 + examples/osgmemorytest/osgmemorytest.cpp | 297 +++++++++++++++++++++++ 3 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 examples/osgmemorytest/CMakeLists.txt create mode 100644 examples/osgmemorytest/osgmemorytest.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 93a3a12de..d8b87884f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -28,7 +28,6 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgcompositeviewer) ADD_SUBDIRECTORY(osgcopy) ADD_SUBDIRECTORY(osgcubemap) - ADD_SUBDIRECTORY(osgrobot) ADD_SUBDIRECTORY(osgdelaunay) ADD_SUBDIRECTORY(osgdepthpartition) ADD_SUBDIRECTORY(osgdepthpeeling) @@ -55,6 +54,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osglogicop) ADD_SUBDIRECTORY(osglogo) ADD_SUBDIRECTORY(osgmanipulator) + ADD_SUBDIRECTORY(osgmemorytest) ADD_SUBDIRECTORY(osgmotionblur) ADD_SUBDIRECTORY(osgmovie) ADD_SUBDIRECTORY(osgmultiplerendertargets) @@ -75,6 +75,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgprerender) ADD_SUBDIRECTORY(osgprerendercubemap) ADD_SUBDIRECTORY(osgreflect) + ADD_SUBDIRECTORY(osgrobot) ADD_SUBDIRECTORY(osgscalarbar) ADD_SUBDIRECTORY(osgscreencapture) ADD_SUBDIRECTORY(osgscribe) diff --git a/examples/osgmemorytest/CMakeLists.txt b/examples/osgmemorytest/CMakeLists.txt new file mode 100644 index 000000000..fe4600dbb --- /dev/null +++ b/examples/osgmemorytest/CMakeLists.txt @@ -0,0 +1,4 @@ +SET(TARGET_SRC osgmemorytest.cpp ) + +#### end var setup ### +SETUP_EXAMPLE(osgmemorytest) diff --git a/examples/osgmemorytest/osgmemorytest.cpp b/examples/osgmemorytest/osgmemorytest.cpp new file mode 100644 index 000000000..e6e861bdf --- /dev/null +++ b/examples/osgmemorytest/osgmemorytest.cpp @@ -0,0 +1,297 @@ +/* OpenSceneGraph example, osganimate. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +#include + +class MemoryTest : public osg::Referenced +{ + public: +}; + +class GLMemoryTest : public MemoryTest +{ + public: + virtual void allocate(osg::State& state) = 0; +}; + +class ContextTest : public MemoryTest +{ + public: + virtual osg::GraphicsContext* allocate() = 0; +}; + +///////////////////////////////////////////////////////////////////////// +// +// PBuffer test +class PBufferTest : public ContextTest +{ + public: + PBufferTest(int width, int height): + _width(width), + _height(height) {} + + virtual bool requiresContext() { return false; } + virtual osg::GraphicsContext* allocate() + { + osg::ref_ptr traits = new osg::GraphicsContext::Traits; + traits->width = _width; + traits->height = _height; + traits->pbuffer = true; + + osg::ref_ptr pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get()); + if (pbuffer.valid()) + { + if (pbuffer->realize()) + { + pbuffer->makeCurrent(); + pbuffer->releaseContext(); + + return pbuffer.release(); + } + else + { + throw "Failed to realize Pixelbuffer"; + } + } + else + { + throw "Failed to created PixelBuffer"; + } + return 0; + } + + + protected: + + int _width; + int _height; +}; + +///////////////////////////////////////////////////////////////////////// +// +// Window test +class WindowTest : public ContextTest +{ + public: + WindowTest(int width, int height): + _width(width), + _height(height) {} + + virtual bool requiresContext() { return false; } + virtual osg::GraphicsContext* allocate() + { + osg::ref_ptr traits = new osg::GraphicsContext::Traits; + traits->width = _width; + traits->height = _height; + traits->windowDecoration = true; + + osg::ref_ptr window = osg::GraphicsContext::createGraphicsContext(traits.get()); + if (window.valid()) + { + if (window->realize()) + { + window->makeCurrent(); + window->releaseContext(); + + return window.release(); + } + else + { + throw "Failed to realize GraphicsWindow"; + } + } + else + { + throw "Failed to created GraphicsWindow"; + } + return 0; + } + + + protected: + + int _width; + int _height; +}; + + +///////////////////////////////////////////////////////////////////////// +// +// Window test +class TextureTest : public GLMemoryTest +{ + public: + TextureTest(int width=1, int height=1, int depth=1): + _width(width), + _height(height), + _depth(depth) {} + + virtual bool requiresContext() { return true; } + + virtual void allocate(osg::State& state) + { + if (_depth>1) + { + osg::ref_ptr image = new osg::Image; + image->allocateImage(_width, _height, _depth, GL_RGBA, GL_UNSIGNED_BYTE); + + osg::ref_ptr texture = new osg::Texture3D; + texture->setImage(image.get()); + + texture->apply(state); + + _textures.push_back(texture.get()); + + } + if (_height>1) + { + osg::ref_ptr image = new osg::Image; + image->allocateImage(_width, _height, 1, GL_RGBA, GL_UNSIGNED_BYTE); + + osg::ref_ptr texture = new osg::Texture2D; + texture->setImage(image.get()); + + texture->apply(state); + + _textures.push_back(texture.get()); + } + if (_width>1) + { + osg::ref_ptr image = new osg::Image; + image->allocateImage(_width, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE); + + osg::ref_ptr texture = new osg::Texture1D; + texture->setImage(image.get()); + + texture->apply(state); + + _textures.push_back(texture.get()); + } + else + { + throw "Invalid texture size of 0,0,0."; + } + + } + + + protected: + + virtual ~TextureTest() + { + } + + typedef std::list< osg::ref_ptr > Textures; + Textures _textures; + int _width; + int _height; + int _depth; +}; + +int main( int argc, char **argv ) +{ + osg::ArgumentParser arguments(&argc,argv); + + typedef std::list< osg::ref_ptr > Tests; + Tests tests; + + int width, height, depth; + while(arguments.read("--pbuffer",width,height)) { tests.push_back(new PBufferTest(width,height)); } + while(arguments.read("--pbuffer")) { tests.push_back(new PBufferTest(1024,1024)); } + + while(arguments.read("--window",width,height)) { tests.push_back(new WindowTest(width,height)); } + while(arguments.read("--window")) { tests.push_back(new WindowTest(1024,1024)); } + + while(arguments.read("--texture",width,height,depth)) { tests.push_back(new TextureTest(width,height,depth)); } + while(arguments.read("--texture",width,height)) { tests.push_back(new TextureTest(width,height,1)); } + while(arguments.read("--texture",width)) { tests.push_back(new TextureTest(width,1,1)); } + + int maxNumContextIterations = 1000; + while(arguments.read("-c",maxNumContextIterations)) {} + + int maxNumGLIterations = 1000; + while(arguments.read("-g",maxNumGLIterations)) {} + + typedef std::list< osg::ref_ptr > GLMemoryTests; + typedef std::list< osg::ref_ptr > ContextTests; + + + + ContextTests contextTests; + GLMemoryTests glMemoryTests; + + for(Tests::iterator itr = tests.begin(); + itr != tests.end(); + ++itr) + { + MemoryTest* test = itr->get(); + if (dynamic_cast(test)!=0) + { + glMemoryTests.push_back(dynamic_cast(test)); + } + else if (dynamic_cast(test)!=0) + { + contextTests.push_back(dynamic_cast(test)); + } + } + + typedef std::list< osg::ref_ptr > Contexts; + Contexts allocatedContexts; + + int numContextIterations = 0; + try + { + for(;numContextIterations context = (*itr)->allocate(); + if (context.valid()) + { + context->makeCurrent(); + context->releaseContext(); + + allocatedContexts.push_back(context); + } + } + } + } + catch(const char* errorString) + { + printf("Exception caught, number of iterations completed = %i, error = %s\n",numContextIterations, errorString); + return 1; + } + catch(...) + { + printf("Exception caught, number of iterations completed = %i\n",numContextIterations); + return 1; + } + + printf("Successful completion, number of iterations completed = %i\n",numContextIterations); + + return 0; +}