- Timestamp:
- 11/11/08 13:50:51 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgmemorytest/osgmemorytest.cpp
r9140 r9141 30 30 }; 31 31 32 class GLObject : public osg::Referenced 33 { 34 public: 35 virtual void apply(osg::State& state) = 0; 36 }; 37 32 38 class GLMemoryTest : public MemoryTest 33 39 { 34 40 public: 35 virtual void allocate(osg::State& state) = 0; 36 }; 37 38 class ContextTest : public MemoryTest 39 { 40 public: 41 virtual osg::GraphicsContext* allocate() = 0; 41 virtual GLObject* allocate() = 0; 42 42 }; 43 43 44 44 ///////////////////////////////////////////////////////////////////////// 45 45 // 46 // PBuffertest47 class PBufferTest : public ContextTest48 { 49 public: 50 PBufferTest(int width, int height):46 // Context test 47 class ContextTest : public MemoryTest 48 { 49 public: 50 ContextTest(int width, int height, bool pbuffer): 51 51 _width(width), 52 _height(height) {} 53 54 virtual bool requiresContext() { return false; } 55 virtual osg::GraphicsContext* allocate() 56 { 57 osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 58 traits->width = _width; 59 traits->height = _height; 60 traits->pbuffer = true; 61 62 osg::ref_ptr<osg::GraphicsContext> pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get()); 63 if (pbuffer.valid()) 64 { 65 if (pbuffer->realize()) 66 { 67 pbuffer->makeCurrent(); 68 pbuffer->releaseContext(); 69 70 return pbuffer.release(); 71 } 72 else 73 { 74 throw "Failed to realize Pixelbuffer"; 75 } 76 } 77 else 78 { 79 throw "Failed to created PixelBuffer"; 80 } 81 return 0; 82 } 83 84 85 protected: 86 87 int _width; 88 int _height; 89 }; 90 91 ///////////////////////////////////////////////////////////////////////// 92 // 93 // Window test 94 class WindowTest : public ContextTest 95 { 96 public: 97 WindowTest(int width, int height): 98 _width(width), 99 _height(height) {} 100 101 virtual bool requiresContext() { return false; } 52 _height(height), 53 _pbuffer(pbuffer) {} 54 102 55 virtual osg::GraphicsContext* allocate() 103 56 { … … 106 59 traits->height = _height; 107 60 traits->windowDecoration = true; 61 traits->pbuffer = _pbuffer; 108 62 109 63 osg::ref_ptr<osg::GraphicsContext> window = osg::GraphicsContext::createGraphicsContext(traits.get()); … … 112 66 if (window->realize()) 113 67 { 114 window->makeCurrent();115 window->releaseContext();116 117 68 return window.release(); 118 69 } 119 70 else 120 71 { 121 throw "Failed to realize GraphicsWindow"; 72 if (_pbuffer) throw "Failed to realize PixelBuffer"; 73 else throw "Failed to realize GraphicsWindow"; 122 74 } 123 75 } 124 76 else 125 77 { 126 throw "Failed to created GraphicsWindow"; 78 if (_pbuffer) throw "Failed to create PixelBuffer"; 79 else throw "Failed to create GraphicsWindow"; 127 80 } 128 81 return 0; … … 134 87 int _width; 135 88 int _height; 136 }; 137 89 bool _pbuffer; 90 }; 91 92 //////////////////////////////////////////////////////////////////////// 93 // 94 // Wrap StateAttribute 95 class StateAttributeObject : public GLObject 96 { 97 public: 98 99 StateAttributeObject(osg::StateAttribute* sa): _attribute(sa) {} 100 101 void apply(osg::State& state) 102 { 103 _attribute->apply(state); 104 105 if (state.checkGLErrors(_attribute.get())) 106 { 107 throw "OpenGL error"; 108 } 109 } 110 111 osg::ref_ptr<osg::StateAttribute> _attribute; 112 }; 138 113 139 114 ///////////////////////////////////////////////////////////////////////// 140 115 // 141 // Windowtest116 // Texture test 142 117 class TextureTest : public GLMemoryTest 143 118 { 144 119 public: 120 145 121 TextureTest(int width=1, int height=1, int depth=1): 146 122 _width(width), … … 148 124 _depth(depth) {} 149 125 150 virtual bool requiresContext() { return true; } 151 152 virtual void allocate(osg::State& state) 126 virtual GLObject* allocate() 153 127 { 154 128 if (_depth>1) … … 159 133 osg::ref_ptr<osg::Texture3D> texture = new osg::Texture3D; 160 134 texture->setImage(image.get()); 161 162 texture->apply(state); 163 164 _textures.push_back(texture.get()); 165 135 texture->setResizeNonPowerOfTwoHint(false); 136 137 return new StateAttributeObject(texture.get()); 166 138 } 167 139 if (_height>1) … … 172 144 osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; 173 145 texture->setImage(image.get()); 174 175 texture->apply(state); 176 177 _textures.push_back(texture.get()); 146 texture->setResizeNonPowerOfTwoHint(false); 147 148 return new StateAttributeObject(texture.get()); 178 149 } 179 150 if (_width>1) … … 184 155 osg::ref_ptr<osg::Texture1D> texture = new osg::Texture1D; 185 156 texture->setImage(image.get()); 186 187 texture->apply(state); 188 189 _textures.push_back(texture.get()); 157 texture->setResizeNonPowerOfTwoHint(false); 158 159 return new StateAttributeObject(texture.get()); 190 160 } 191 161 else 192 162 { 193 throw "Invalid texture size of 0,0,0 .";194 } 195 163 throw "Invalid texture size of 0,0,0"; 164 } 165 return 0; 196 166 } 197 167 … … 199 169 protected: 200 170 201 virtual ~TextureTest()202 {203 }204 205 typedef std::list< osg::ref_ptr<osg::Texture> > Textures;206 Textures _textures;207 171 int _width; 208 172 int _height; … … 210 174 }; 211 175 176 ///////////////////////////////////////////////////////////////////////// 177 // 178 // Texture test 179 class FboTest : public GLMemoryTest 180 { 181 public: 182 183 FboTest(int width=1024, int height=1024, int depth=2): 184 _width(width), 185 _height(height), 186 _depth(depth) {} 187 188 virtual GLObject* allocate() 189 { 190 osg::ref_ptr<osg::FrameBufferObject> fbo = new osg::FrameBufferObject; 191 192 if (_depth>=1) fbo->setAttachment(osg::Camera::COLOR_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(_width, _height, GL_RGBA))); 193 if (_depth>=2) fbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(_width, _height, GL_DEPTH_COMPONENT24))); 194 195 return new StateAttributeObject(fbo.get()); 196 } 197 198 199 protected: 200 201 int _width; 202 int _height; 203 int _depth; 204 }; 205 212 206 int main( int argc, char **argv ) 213 207 { … … 218 212 219 213 int width, height, depth; 220 while(arguments.read("--pbuffer",width,height)) { tests.push_back(new PBufferTest(width,height)); }221 while(arguments.read("--pbuffer")) { tests.push_back(new PBufferTest(1024,1024)); }222 223 while(arguments.read("--window",width,height)) { tests.push_back(new WindowTest(width,height)); }224 while(arguments.read("--window")) { tests.push_back(new WindowTest(1024,1024)); }214 while(arguments.read("--pbuffer",width,height)) { tests.push_back(new ContextTest(width, height, true)); } 215 while(arguments.read("--pbuffer")) { tests.push_back(new ContextTest(512, 512, true)); } 216 217 while(arguments.read("--window",width,height)) { tests.push_back(new ContextTest(width, height, false)); } 218 while(arguments.read("--window")) { tests.push_back(new ContextTest(512,512, false)); } 225 219 226 220 while(arguments.read("--texture",width,height,depth)) { tests.push_back(new TextureTest(width,height,depth)); } … … 228 222 while(arguments.read("--texture",width)) { tests.push_back(new TextureTest(width,1,1)); } 229 223 230 int maxNumContextIterations = 1000; 224 while(arguments.read("--fbo",width,height,depth)) { tests.push_back(new FboTest(width,height,depth)); } 225 while(arguments.read("--fbo",width,height)) { tests.push_back(new FboTest(width,height,2)); } 226 while(arguments.read("--fbo")) { tests.push_back(new FboTest(1024,1024,2)); } 227 228 int maxNumContextIterations = 1; 231 229 while(arguments.read("-c",maxNumContextIterations)) {} 232 230 … … 236 234 typedef std::list< osg::ref_ptr<GLMemoryTest> > GLMemoryTests; 237 235 typedef std::list< osg::ref_ptr<ContextTest> > ContextTests; 238 239 236 240 237 … … 258 255 259 256 typedef std::list< osg::ref_ptr<osg::GraphicsContext> > Contexts; 257 typedef std::list< osg::ref_ptr<GLObject> > GLObjects; 260 258 Contexts allocatedContexts; 259 GLObjects glObjects; 261 260 262 261 int numContextIterations = 0; 262 int numGLObjectIterations = 0; 263 int numGLObjectsApplied = 0; 263 264 try 264 265 { 266 for(; numGLObjectIterations<maxNumGLIterations; ++numGLObjectIterations) 267 { 268 for(GLMemoryTests::iterator itr = glMemoryTests.begin(); 269 itr != glMemoryTests.end(); 270 ++itr) 271 { 272 osg::ref_ptr<GLObject> glObject = (*itr)->allocate(); 273 if (glObject.valid()) glObjects.push_back(glObject.get()); 274 } 275 } 276 265 277 for(;numContextIterations<maxNumContextIterations; ++numContextIterations) 266 278 { … … 273 285 if (context.valid()) 274 286 { 287 allocatedContexts.push_back(context); 288 275 289 context->makeCurrent(); 290 291 for(GLObjects::iterator gitr = glObjects.begin(); 292 gitr != glObjects.end(); 293 ++gitr) 294 { 295 (*gitr)->apply(*(context->getState())); 296 ++numGLObjectsApplied; 297 } 298 276 299 context->releaseContext(); 277 278 allocatedContexts.push_back(context);279 300 } 280 301 } … … 283 304 catch(const char* errorString) 284 305 { 285 printf("Exception caught, number of iterations completed = %i, error = %s\n",numContextIterations, errorString);306 printf("Exception caught, contexts completed = %i, gl objects successfully applied =%i, error = %s\n",numContextIterations, numGLObjectsApplied, errorString); 286 307 return 1; 287 308 } 288 309 catch(...) 289 310 { 290 printf("Exception caught, number of iterations completed = %i\n",numContextIterations);311 printf("Exception caught, contexts completed = %i, gl objects successfully applied =%i\n",numContextIterations, numGLObjectsApplied); 291 312 return 1; 292 313 } 293 314 294 printf("Successful completion, number of iterations completed = %i\n",numContextIterations);315 printf("Successful completion, contexts created = %i, gl objects applied =%i\n",numContextIterations, numGLObjectsApplied); 295 316 296 317 return 0;
