| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Notify> |
|---|
| 20 | #include <osg/ArgumentParser> |
|---|
| 21 | #include <osg/Texture1D> |
|---|
| 22 | #include <osg/Texture2D> |
|---|
| 23 | #include <osg/Texture3D> |
|---|
| 24 | |
|---|
| 25 | #include <osgViewer/Viewer> |
|---|
| 26 | |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | |
|---|
| 29 | class MemoryTest : public osg::Referenced |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | class GLObject : public osg::Referenced |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | virtual void apply(osg::State& state) = 0; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | class GLMemoryTest : public MemoryTest |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | virtual GLObject* allocate() = 0; |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | class ContextTest : public MemoryTest |
|---|
| 50 | { |
|---|
| 51 | public: |
|---|
| 52 | ContextTest(int width, int height, bool pbuffer): |
|---|
| 53 | _width(width), |
|---|
| 54 | _height(height), |
|---|
| 55 | _pbuffer(pbuffer) {} |
|---|
| 56 | |
|---|
| 57 | virtual osg::GraphicsContext* allocate() |
|---|
| 58 | { |
|---|
| 59 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 60 | traits->width = _width; |
|---|
| 61 | traits->height = _height; |
|---|
| 62 | traits->windowDecoration = true; |
|---|
| 63 | traits->pbuffer = _pbuffer; |
|---|
| 64 | |
|---|
| 65 | osg::ref_ptr<osg::GraphicsContext> window = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 66 | if (window.valid()) |
|---|
| 67 | { |
|---|
| 68 | if (window->realize()) |
|---|
| 69 | { |
|---|
| 70 | return window.release(); |
|---|
| 71 | } |
|---|
| 72 | else |
|---|
| 73 | { |
|---|
| 74 | if (_pbuffer) throw "Failed to realize PixelBuffer"; |
|---|
| 75 | else throw "Failed to realize GraphicsWindow"; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | else |
|---|
| 79 | { |
|---|
| 80 | if (_pbuffer) throw "Failed to create PixelBuffer"; |
|---|
| 81 | else throw "Failed to create GraphicsWindow"; |
|---|
| 82 | } |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | protected: |
|---|
| 88 | |
|---|
| 89 | int _width; |
|---|
| 90 | int _height; |
|---|
| 91 | bool _pbuffer; |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | class StateAttributeObject : public GLObject |
|---|
| 98 | { |
|---|
| 99 | public: |
|---|
| 100 | |
|---|
| 101 | StateAttributeObject(osg::StateAttribute* sa): _attribute(sa) {} |
|---|
| 102 | |
|---|
| 103 | void apply(osg::State& state) |
|---|
| 104 | { |
|---|
| 105 | _attribute->apply(state); |
|---|
| 106 | |
|---|
| 107 | if (state.checkGLErrors(_attribute.get())) |
|---|
| 108 | { |
|---|
| 109 | throw "OpenGL error"; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | osg::ref_ptr<osg::StateAttribute> _attribute; |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class TextureTest : public GLMemoryTest |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | |
|---|
| 123 | TextureTest(int width=1, int height=1, int depth=1): |
|---|
| 124 | _width(width), |
|---|
| 125 | _height(height), |
|---|
| 126 | _depth(depth) {} |
|---|
| 127 | |
|---|
| 128 | virtual GLObject* allocate() |
|---|
| 129 | { |
|---|
| 130 | if (_depth>1) |
|---|
| 131 | { |
|---|
| 132 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 133 | image->allocateImage(_width, _height, _depth, GL_RGBA, GL_UNSIGNED_BYTE); |
|---|
| 134 | |
|---|
| 135 | osg::ref_ptr<osg::Texture3D> texture = new osg::Texture3D; |
|---|
| 136 | texture->setImage(image.get()); |
|---|
| 137 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 138 | |
|---|
| 139 | return new StateAttributeObject(texture.get()); |
|---|
| 140 | } |
|---|
| 141 | if (_height>1) |
|---|
| 142 | { |
|---|
| 143 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 144 | image->allocateImage(_width, _height, 1, GL_RGBA, GL_UNSIGNED_BYTE); |
|---|
| 145 | |
|---|
| 146 | osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; |
|---|
| 147 | texture->setImage(image.get()); |
|---|
| 148 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 149 | |
|---|
| 150 | return new StateAttributeObject(texture.get()); |
|---|
| 151 | } |
|---|
| 152 | if (_width>1) |
|---|
| 153 | { |
|---|
| 154 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 155 | image->allocateImage(_width, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE); |
|---|
| 156 | |
|---|
| 157 | osg::ref_ptr<osg::Texture1D> texture = new osg::Texture1D; |
|---|
| 158 | texture->setImage(image.get()); |
|---|
| 159 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 160 | |
|---|
| 161 | return new StateAttributeObject(texture.get()); |
|---|
| 162 | } |
|---|
| 163 | else |
|---|
| 164 | { |
|---|
| 165 | throw "Invalid texture size of 0,0,0"; |
|---|
| 166 | } |
|---|
| 167 | return 0; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | protected: |
|---|
| 172 | |
|---|
| 173 | int _width; |
|---|
| 174 | int _height; |
|---|
| 175 | int _depth; |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | class FboTest : public GLMemoryTest |
|---|
| 182 | { |
|---|
| 183 | public: |
|---|
| 184 | |
|---|
| 185 | FboTest(int width=1024, int height=1024, int depth=2): |
|---|
| 186 | _width(width), |
|---|
| 187 | _height(height), |
|---|
| 188 | _depth(depth) {} |
|---|
| 189 | |
|---|
| 190 | virtual GLObject* allocate() |
|---|
| 191 | { |
|---|
| 192 | osg::ref_ptr<osg::FrameBufferObject> fbo = new osg::FrameBufferObject; |
|---|
| 193 | |
|---|
| 194 | if (_depth>=1) fbo->setAttachment(osg::Camera::COLOR_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(_width, _height, GL_RGBA))); |
|---|
| 195 | if (_depth>=2) fbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(_width, _height, GL_DEPTH_COMPONENT24))); |
|---|
| 196 | |
|---|
| 197 | return new StateAttributeObject(fbo.get()); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | protected: |
|---|
| 202 | |
|---|
| 203 | int _width; |
|---|
| 204 | int _height; |
|---|
| 205 | int _depth; |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | int main( int argc, char **argv ) |
|---|
| 209 | { |
|---|
| 210 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 211 | |
|---|
| 212 | typedef std::list< osg::ref_ptr<MemoryTest> > Tests; |
|---|
| 213 | Tests tests; |
|---|
| 214 | |
|---|
| 215 | int width, height, depth; |
|---|
| 216 | while(arguments.read("--pbuffer",width,height)) { tests.push_back(new ContextTest(width, height, true)); } |
|---|
| 217 | while(arguments.read("--pbuffer")) { tests.push_back(new ContextTest(512, 512, true)); } |
|---|
| 218 | |
|---|
| 219 | while(arguments.read("--window",width,height)) { tests.push_back(new ContextTest(width, height, false)); } |
|---|
| 220 | while(arguments.read("--window")) { tests.push_back(new ContextTest(512,512, false)); } |
|---|
| 221 | |
|---|
| 222 | while(arguments.read("--texture",width,height,depth)) { tests.push_back(new TextureTest(width,height,depth)); } |
|---|
| 223 | while(arguments.read("--texture",width,height)) { tests.push_back(new TextureTest(width,height,1)); } |
|---|
| 224 | while(arguments.read("--texture",width)) { tests.push_back(new TextureTest(width,1,1)); } |
|---|
| 225 | |
|---|
| 226 | while(arguments.read("--fbo",width,height,depth)) { tests.push_back(new FboTest(width,height,depth)); } |
|---|
| 227 | while(arguments.read("--fbo",width,height)) { tests.push_back(new FboTest(width,height,2)); } |
|---|
| 228 | while(arguments.read("--fbo")) { tests.push_back(new FboTest(1024,1024,2)); } |
|---|
| 229 | |
|---|
| 230 | unsigned int sleepTime = 0; |
|---|
| 231 | while(arguments.read("--delay",sleepTime)) {} |
|---|
| 232 | |
|---|
| 233 | int maxNumContextIterations = 1; |
|---|
| 234 | while(arguments.read("-c",maxNumContextIterations)) {} |
|---|
| 235 | |
|---|
| 236 | int maxNumGLIterations = 1000; |
|---|
| 237 | while(arguments.read("-g",maxNumGLIterations)) {} |
|---|
| 238 | |
|---|
| 239 | typedef std::list< osg::ref_ptr<GLMemoryTest> > GLMemoryTests; |
|---|
| 240 | typedef std::list< osg::ref_ptr<ContextTest> > ContextTests; |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | ContextTests contextTests; |
|---|
| 244 | GLMemoryTests glMemoryTests; |
|---|
| 245 | |
|---|
| 246 | for(Tests::iterator itr = tests.begin(); |
|---|
| 247 | itr != tests.end(); |
|---|
| 248 | ++itr) |
|---|
| 249 | { |
|---|
| 250 | MemoryTest* test = itr->get(); |
|---|
| 251 | if (dynamic_cast<GLMemoryTest*>(test)!=0) |
|---|
| 252 | { |
|---|
| 253 | glMemoryTests.push_back(dynamic_cast<GLMemoryTest*>(test)); |
|---|
| 254 | } |
|---|
| 255 | else if (dynamic_cast<ContextTest*>(test)!=0) |
|---|
| 256 | { |
|---|
| 257 | contextTests.push_back(dynamic_cast<ContextTest*>(test)); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | typedef std::list< osg::ref_ptr<osg::GraphicsContext> > Contexts; |
|---|
| 262 | typedef std::list< osg::ref_ptr<GLObject> > GLObjects; |
|---|
| 263 | Contexts allocatedContexts; |
|---|
| 264 | GLObjects glObjects; |
|---|
| 265 | |
|---|
| 266 | int numContextIterations = 0; |
|---|
| 267 | int numGLObjectIterations = 0; |
|---|
| 268 | int numGLObjectsApplied = 0; |
|---|
| 269 | try |
|---|
| 270 | { |
|---|
| 271 | for(; numGLObjectIterations<maxNumGLIterations; ++numGLObjectIterations) |
|---|
| 272 | { |
|---|
| 273 | for(GLMemoryTests::iterator itr = glMemoryTests.begin(); |
|---|
| 274 | itr != glMemoryTests.end(); |
|---|
| 275 | ++itr) |
|---|
| 276 | { |
|---|
| 277 | osg::ref_ptr<GLObject> glObject = (*itr)->allocate(); |
|---|
| 278 | if (glObject.valid()) glObjects.push_back(glObject.get()); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | for(;numContextIterations<maxNumContextIterations; ++numContextIterations) |
|---|
| 283 | { |
|---|
| 284 | printf("iteration %i\n",numContextIterations); |
|---|
| 285 | for(ContextTests::iterator itr = contextTests.begin(); |
|---|
| 286 | itr != contextTests.end(); |
|---|
| 287 | ++itr) |
|---|
| 288 | { |
|---|
| 289 | osg::ref_ptr<osg::GraphicsContext> context = (*itr)->allocate(); |
|---|
| 290 | if (context.valid()) |
|---|
| 291 | { |
|---|
| 292 | allocatedContexts.push_back(context); |
|---|
| 293 | |
|---|
| 294 | context->makeCurrent(); |
|---|
| 295 | |
|---|
| 296 | for(GLObjects::iterator gitr = glObjects.begin(); |
|---|
| 297 | gitr != glObjects.end(); |
|---|
| 298 | ++gitr) |
|---|
| 299 | { |
|---|
| 300 | if (sleepTime>0) OpenThreads::Thread::microSleep( sleepTime ); |
|---|
| 301 | |
|---|
| 302 | printf("%i ",numGLObjectsApplied);fflush(stdout); |
|---|
| 303 | |
|---|
| 304 | (*gitr)->apply(*(context->getState())); |
|---|
| 305 | ++numGLObjectsApplied; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | context->releaseContext(); |
|---|
| 309 | printf("\n\n"); fflush(stdout); |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | catch(const char* errorString) |
|---|
| 315 | { |
|---|
| 316 | printf("\nException caught, contexts completed = %i, gl objects successfully applied =%i, error = %s\n\n",numContextIterations, numGLObjectsApplied, errorString); |
|---|
| 317 | return 1; |
|---|
| 318 | } |
|---|
| 319 | catch(...) |
|---|
| 320 | { |
|---|
| 321 | printf("\nException caught, contexts completed = %i, gl objects successfully applied =%i\n\n",numContextIterations, numGLObjectsApplied); |
|---|
| 322 | return 1; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | printf("\nSuccessful completion, contexts created = %i, gl objects applied =%i\n\n",numContextIterations, numGLObjectsApplied); |
|---|
| 326 | |
|---|
| 327 | return 0; |
|---|
| 328 | } |
|---|