Equalizer  1.2.1
eqNBody/config.cpp
00001 
00002 /*
00003  * Copyright (c) 2009, Philippe Robert <philippe.robert@gmail.com> 
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * - Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  * - Redistributions in binary form must reproduce the above copyright notice,
00011  *   this list of conditions and the following disclaimer in the documentation
00012  *   and/or other materials provided with the distribution.
00013  * - Neither the name of Eyescale Software GmbH nor the names of its
00014  *   contributors may be used to endorse or promote products derived from this
00015  *   software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "config.h"
00031 
00032 namespace eqNbody
00033 {
00034     
00035 Config::Config( eq::ServerPtr parent )
00036         : eq::Config( parent )
00037         , _redraw( true )
00038 {
00039 }
00040         
00041 bool Config::init()
00042 {           
00043     // init distributed objects
00044     _frameData.init( _initData.getNumBodies() );
00045     registerObject( &_frameData );      
00046 
00047     _initData.setFrameDataID( _frameData.getID( ));
00048     registerObject( &_initData );
00049 
00050     // init config
00051     if( !eq::Config::init( _initData.getID( )))
00052     {
00053         _deregisterData();
00054         return false;
00055     }
00056 
00057     return true;
00058 }
00059     
00060 bool Config::exit()
00061 {
00062     const bool ret = eq::Config::exit();
00063     _deregisterData();
00064         
00065     return ret;
00066 }
00067         
00068 void Config::_deregisterData()
00069 {
00070     releaseData();
00071     deregisterObject( &_frameData );
00072         
00073     _initData.setFrameDataID( eq::UUID::ZERO );
00074 }
00075     
00076     
00077 void Config::mapData( const eq::uint128_t& initDataID )
00078 {       
00079     if( !_initData.isAttached( ))
00080     {
00081         EQCHECK( mapObject( &_initData, initDataID ));
00082         releaseData(); // data was retrieved, unmap immediately
00083     }
00084     else  // appNode, _initData is registered already
00085     {
00086         EQASSERT( _initData.getID() == initDataID );
00087     }
00088 }
00089     
00090 void Config::releaseData()
00091 {
00092     releaseObject( &_initData );
00093 }
00094     
00095 uint32_t Config::startFrame()
00096 {
00097     static bool isInitialized = false;
00098         
00099     // Allocate the CUDA memory after the CUDA device initialisation!
00100     if( isInitialized == false ) {
00101         _frameData.initHostData();
00102         _frameData.updateParameters( NBODY_CONFIG_SHELL,
00103                                      2.12f, 2.98f, 0.016f );
00104         isInitialized = true;
00105     }       
00106     
00107     const eq::uint128_t& version = _frameData.commit();    
00108     
00109     _redraw = false;
00110     return eq::Config::startFrame( version );
00111 }
00112     
00113 bool Config::needsRedraw()
00114 {
00115     return ( _redraw );
00116 }
00117     
00118 bool Config::handleEvent( const eq::ConfigEvent* event )
00119 {               
00120     switch( event->data.type )
00121     {
00122       case ConfigEvent::DATA_CHANGED:
00123           _registerData(static_cast< const ConfigEvent* >( event ));
00124           if( _readyToCommit() ) {
00125               _frameData.commit();        // broadcast changed data to all clients
00126           }
00127           break;
00128 
00129       case ConfigEvent::PROXY_CHANGED:
00130       {
00131           _updateData(static_cast< const ConfigEvent* >( event ));
00132           if( _readyToCommit() ) {
00133               _updateSimulation();    // update the simulation every nth frame
00134               _frameData.commit();    // broadcast changed data to all clients
00135           }
00136       }
00137       break;
00138                 
00139       case eq::Event::KEY_PRESS:
00140           if( _handleKeyEvent( event->data.keyPress ))
00141           {
00142               _redraw = true;
00143               return true;
00144           }
00145           break;
00146                                 
00147       case eq::Event::WINDOW_EXPOSE:
00148       case eq::Event::WINDOW_RESIZE:
00149       case eq::Event::WINDOW_CLOSE:
00150       case eq::Event::VIEW_RESIZE:
00151           _redraw = true;
00152           break;
00153                 
00154       default:
00155           break;
00156     }
00157         
00158     _redraw |= eq::Config::handleEvent( event );
00159     return _redraw;
00160 }
00161     
00162 bool Config::_handleKeyEvent( const eq::KeyEvent& event )
00163 {
00164     switch( event.key )
00165     {
00166       case ' ':
00167           //_frameData.reset();
00168           return true;
00169                 
00170       case 's':
00171       case 'S':
00172           _frameData.toggleStatistics();
00173           return true;
00174                 
00175       default:
00176           return false;
00177     }
00178 }
00179     
00180 bool Config::_readyToCommit()
00181 {
00182     return _frameData.isReady();
00183 }
00184             
00185 void Config::_updateSimulation() 
00186 {
00187     static int ctr = 0;     // frame counter
00188     static int demo = 0;    // demo config
00189         
00190     ctr++;
00191 
00192     if(ctr > 200) {
00193         ctr = 0;
00194         switch(demo) {
00195           case 0:
00196               _frameData.updateParameters(NBODY_CONFIG_SHELL, 2.12f, 2.98f, 0.016f);
00197               demo++;
00198               break;
00199           case 1:
00200               _frameData.updateParameters(NBODY_CONFIG_EXPAND, 0.68f, 20.0f, 0.016f);
00201               demo++;
00202               break;
00203           case 2:
00204               _frameData.updateParameters(NBODY_CONFIG_RANDOM, 0.16f, 10.0f, 0.016f);
00205               demo=0;
00206               break;
00207         }
00208     }
00209 }
00210     
00211 void Config::_registerData(const ConfigEvent* event)
00212 {   
00213     _frameData.addProxyID(event->_proxyID, event->_range);
00214 }   
00215 
00216 void Config::_updateData(const ConfigEvent* event)
00217 {   
00218     _frameData.updateProxyID(event->_proxyID, event->_version, event->_range);
00219 }   
00220 }
Generated on Fri Jun 8 2012 15:44:29 for Equalizer 1.2.1 by  doxygen 1.8.0