2016-03-03 02:01:33 +00:00
# include "InProcessExampleBrowser.h"
//#define EXAMPLE_CONSOLE_ONLY
# ifdef EXAMPLE_CONSOLE_ONLY
2018-09-23 21:17:31 +00:00
# include "EmptyBrowser.h"
typedef EmptyBrowser DefaultBrowser ;
2016-03-03 02:01:33 +00:00
# else
2018-09-23 21:17:31 +00:00
# include "OpenGLExampleBrowser.h"
typedef OpenGLExampleBrowser DefaultBrowser ;
# endif //EXAMPLE_CONSOLE_ONLY
2016-03-03 02:01:33 +00:00
# include "Bullet3Common/b3CommandLineArgs.h"
# include "../Utils/b3Clock.h"
# include "ExampleEntries.h"
2016-05-04 07:16:53 +00:00
# include "Bullet3Common/b3Scalar.h"
2016-03-10 22:36:46 +00:00
# include "../SharedMemory/InProcessMemory.h"
2016-03-03 02:01:33 +00:00
2018-09-23 21:17:31 +00:00
void ExampleBrowserThreadFunc ( void * userPtr , void * lsMemory ) ;
void * ExampleBrowserMemoryFunc ( ) ;
2018-05-02 21:32:43 +00:00
void ExampleBrowserMemoryReleaseFunc ( void * ptr ) ;
2016-03-03 02:01:33 +00:00
# include <stdio.h>
//#include "BulletMultiThreaded/PlatformDefinitions.h"
2016-05-04 07:16:53 +00:00
# include "Bullet3Common/b3Logging.h"
2016-05-04 06:34:48 +00:00
# include "ExampleEntries.h"
# include "LinearMath/btAlignedObjectArray.h"
# include "EmptyExample.h"
# include "../SharedMemory/PhysicsServerExample.h"
2017-05-31 02:54:55 +00:00
# include "../SharedMemory/PhysicsServerExampleBullet2.h"
2020-03-19 21:18:35 +00:00
# include "../SharedMemory/GraphicsServerExample.h"
2017-05-31 02:54:55 +00:00
2016-05-04 06:34:48 +00:00
# include "../SharedMemory/PhysicsClientExample.h"
2016-05-04 07:16:53 +00:00
# ifndef _WIN32
# include "../MultiThreading/b3PosixThreadSupport.h"
static b3ThreadSupportInterface * createExampleBrowserThreadSupport ( int numThreads )
{
b3PosixThreadSupport : : ThreadConstructionInfo constructionInfo ( " testThreads " ,
2018-09-23 21:17:31 +00:00
ExampleBrowserThreadFunc ,
ExampleBrowserMemoryFunc ,
ExampleBrowserMemoryReleaseFunc ,
numThreads ) ;
b3ThreadSupportInterface * threadSupport = new b3PosixThreadSupport ( constructionInfo ) ;
2016-05-04 07:16:53 +00:00
return threadSupport ;
}
2018-09-23 21:17:31 +00:00
# elif defined(_WIN32)
2016-05-04 07:16:53 +00:00
# include "../MultiThreading/b3Win32ThreadSupport.h"
b3ThreadSupportInterface * createExampleBrowserThreadSupport ( int numThreads )
{
2018-09-23 21:17:31 +00:00
b3Win32ThreadSupport : : Win32ThreadConstructionInfo threadConstructionInfo ( " testThreads " , ExampleBrowserThreadFunc , ExampleBrowserMemoryFunc , ExampleBrowserMemoryReleaseFunc , numThreads ) ;
2016-05-04 07:16:53 +00:00
b3Win32ThreadSupport * threadSupport = new b3Win32ThreadSupport ( threadConstructionInfo ) ;
return threadSupport ;
}
# endif
2016-05-04 06:34:48 +00:00
class ExampleEntriesPhysicsServer : public ExampleEntries
{
struct ExampleEntriesInternalData2 * m_data ;
public :
ExampleEntriesPhysicsServer ( ) ;
virtual ~ ExampleEntriesPhysicsServer ( ) ;
2018-09-23 21:17:31 +00:00
static void registerExampleEntry ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option = 0 ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual void initExampleEntries ( ) ;
virtual void initOpenCLExampleEntries ( ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual int getNumRegisteredExamples ( ) ;
virtual CommonExampleInterface : : CreateFunc * getExampleCreateFunc ( int index ) ;
virtual const char * getExampleName ( int index ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual const char * getExampleDescription ( int index ) ;
2018-09-23 21:17:31 +00:00
virtual int getExampleOption ( int index ) ;
2016-05-04 06:34:48 +00:00
} ;
struct ExampleEntryPhysicsServer
{
2018-09-23 21:17:31 +00:00
int m_menuLevel ;
const char * m_name ;
const char * m_description ;
CommonExampleInterface : : CreateFunc * m_createFunc ;
int m_option ;
2016-05-04 06:34:48 +00:00
ExampleEntryPhysicsServer ( int menuLevel , const char * name )
2018-09-23 21:17:31 +00:00
: m_menuLevel ( menuLevel ) , m_name ( name ) , m_description ( 0 ) , m_createFunc ( 0 ) , m_option ( 0 )
2016-05-04 06:34:48 +00:00
{
}
2018-09-23 21:17:31 +00:00
ExampleEntryPhysicsServer ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option = 0 )
: m_menuLevel ( menuLevel ) , m_name ( name ) , m_description ( description ) , m_createFunc ( createFunc ) , m_option ( option )
2016-05-04 06:34:48 +00:00
{
}
} ;
struct ExampleEntriesInternalData2
{
2018-09-23 21:17:31 +00:00
btAlignedObjectArray < ExampleEntryPhysicsServer > m_allExamples ;
2016-05-04 06:34:48 +00:00
} ;
2018-09-23 21:17:31 +00:00
static ExampleEntryPhysicsServer gDefaultExamplesPhysicsServer [ ] =
{
2016-05-18 23:21:40 +00:00
2018-09-23 21:17:31 +00:00
ExampleEntryPhysicsServer ( 0 , " Robotics Control " ) ,
2016-05-18 23:21:40 +00:00
2018-09-23 21:17:31 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server " , " Create a physics server that communicates with a physics client over shared memory " ,
PhysicsServerCreateFuncBullet2 ) ,
ExampleEntryPhysicsServer ( 1 , " Physics Server (RTC) " , " Create a physics server that communicates with a physics client over shared memory. At each update, the Physics Server will continue calling 'stepSimulation' based on the real-time clock (RTC). " ,
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_USE_RTC_CLOCK ) ,
2016-05-04 06:34:48 +00:00
2018-09-23 21:17:31 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server (Logging) " , " Create a physics server that communicates with a physics client over shared memory. It will log all commands to a file. " ,
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_ENABLE_COMMAND_LOGGING ) ,
ExampleEntryPhysicsServer ( 1 , " Physics Server (Replay Log) " , " Create a physics server that replay a command log from disk. " ,
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG ) ,
2020-03-19 21:18:35 +00:00
ExampleEntryPhysicsServer ( 1 , " Graphics Server " , " Create a graphics server " , GraphicsServerCreateFuncBullet ) ,
2016-05-04 06:34:48 +00:00
} ;
ExampleEntriesPhysicsServer : : ExampleEntriesPhysicsServer ( )
{
m_data = new ExampleEntriesInternalData2 ;
}
ExampleEntriesPhysicsServer : : ~ ExampleEntriesPhysicsServer ( )
{
delete m_data ;
}
void ExampleEntriesPhysicsServer : : initOpenCLExampleEntries ( )
{
}
void ExampleEntriesPhysicsServer : : initExampleEntries ( )
{
m_data - > m_allExamples . clear ( ) ;
2018-09-23 21:17:31 +00:00
int numDefaultEntries = sizeof ( gDefaultExamplesPhysicsServer ) / sizeof ( ExampleEntryPhysicsServer ) ;
for ( int i = 0 ; i < numDefaultEntries ; i + + )
2016-05-04 06:34:48 +00:00
{
m_data - > m_allExamples . push_back ( gDefaultExamplesPhysicsServer [ i ] ) ;
}
}
2018-09-23 21:17:31 +00:00
void ExampleEntriesPhysicsServer : : registerExampleEntry ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option )
2016-05-04 06:34:48 +00:00
{
}
int ExampleEntriesPhysicsServer : : getNumRegisteredExamples ( )
{
return m_data - > m_allExamples . size ( ) ;
}
CommonExampleInterface : : CreateFunc * ExampleEntriesPhysicsServer : : getExampleCreateFunc ( int index )
{
return m_data - > m_allExamples [ index ] . m_createFunc ;
}
int ExampleEntriesPhysicsServer : : getExampleOption ( int index )
{
return m_data - > m_allExamples [ index ] . m_option ;
}
const char * ExampleEntriesPhysicsServer : : getExampleName ( int index )
{
return m_data - > m_allExamples [ index ] . m_name ;
}
const char * ExampleEntriesPhysicsServer : : getExampleDescription ( int index )
{
return m_data - > m_allExamples [ index ] . m_description ;
}
2018-09-23 21:17:31 +00:00
struct ExampleBrowserArgs
2016-03-03 02:01:33 +00:00
{
ExampleBrowserArgs ( )
2018-09-23 21:17:31 +00:00
: m_fakeWork ( 1 ) , m_argc ( 0 )
2016-03-03 02:01:33 +00:00
{
}
b3CriticalSection * m_cs ;
float m_fakeWork ;
2018-09-23 21:17:31 +00:00
int m_argc ;
char * * m_argv ;
2016-03-03 02:01:33 +00:00
} ;
struct ExampleBrowserThreadLocalStorage
{
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * m_sharedMem ;
2016-03-03 02:01:33 +00:00
int threadId ;
} ;
enum TestExampleBrowserCommunicationEnums
{
eRequestTerminateExampleBrowser = 13 ,
2016-03-07 22:56:16 +00:00
eExampleBrowserIsUnInitialized ,
eExampleBrowserIsInitialized ,
eExampleBrowserInitializationFailed ,
2016-03-03 02:01:33 +00:00
eExampleBrowserHasTerminated
} ;
2016-12-29 05:51:54 +00:00
static double gMinUpdateTimeMicroSecs = 4000. ;
2018-09-23 21:17:31 +00:00
void ExampleBrowserThreadFunc ( void * userPtr , void * lsMemory )
2016-03-03 02:01:33 +00:00
{
2016-07-09 22:09:09 +00:00
printf ( " ExampleBrowserThreadFunc started \n " ) ;
2016-03-03 02:01:33 +00:00
2018-09-23 21:17:31 +00:00
ExampleBrowserThreadLocalStorage * localStorage = ( ExampleBrowserThreadLocalStorage * ) lsMemory ;
2016-03-03 02:01:33 +00:00
2018-09-23 21:17:31 +00:00
ExampleBrowserArgs * args = ( ExampleBrowserArgs * ) userPtr ;
2017-01-16 06:26:11 +00:00
//int workLeft = true;
2018-09-23 21:17:31 +00:00
b3CommandLineArgs args2 ( args - > m_argc , args - > m_argv ) ;
2019-02-12 18:36:01 +00:00
int minUpdateMs = 4000 ;
if ( args2 . GetCmdLineArgument ( " minGraphicsUpdateTimeMs " , minUpdateMs ) )
{
gMinUpdateTimeMicroSecs = minUpdateMs ;
}
2016-03-03 02:01:33 +00:00
b3Clock clock ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
ExampleEntriesPhysicsServer examples ;
2016-03-03 02:01:33 +00:00
examples . initExampleEntries ( ) ;
2016-03-10 22:36:46 +00:00
DefaultBrowser * exampleBrowser = new DefaultBrowser ( & examples ) ;
exampleBrowser - > setSharedMemoryInterface ( localStorage - > m_sharedMem ) ;
2018-09-23 21:17:31 +00:00
bool init = exampleBrowser - > init ( args - > m_argc , args - > m_argv ) ;
2016-03-03 02:01:33 +00:00
clock . reset ( ) ;
if ( init )
{
2016-03-07 22:56:16 +00:00
args - > m_cs - > lock ( ) ;
2018-09-23 21:17:31 +00:00
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserIsInitialized ) ;
2016-03-07 22:56:16 +00:00
args - > m_cs - > unlock ( ) ;
2016-05-18 23:21:40 +00:00
do
2016-03-03 02:01:33 +00:00
{
2017-07-14 22:12:16 +00:00
clock . usleep ( 0 ) ;
//B3_PROFILE("ExampleBrowserThreadFunc");
2018-09-23 21:17:31 +00:00
float deltaTimeInSeconds = clock . getTimeMicroseconds ( ) / 1000000.f ;
2016-12-29 05:51:54 +00:00
{
if ( deltaTimeInSeconds > 0.1 )
{
deltaTimeInSeconds = 0.1 ;
}
2018-09-23 21:17:31 +00:00
if ( deltaTimeInSeconds < ( gMinUpdateTimeMicroSecs / 1e6 ) )
2016-12-29 05:51:54 +00:00
{
2017-07-14 22:12:16 +00:00
//B3_PROFILE("clock.usleep");
2017-02-16 22:19:09 +00:00
exampleBrowser - > updateGraphics ( ) ;
2018-09-23 21:17:31 +00:00
}
else
2016-12-29 05:51:54 +00:00
{
2017-07-14 22:12:16 +00:00
//B3_PROFILE("exampleBrowser->update");
2016-12-29 05:51:54 +00:00
clock . reset ( ) ;
2017-07-14 22:12:16 +00:00
exampleBrowser - > updateGraphics ( ) ;
2016-12-29 05:51:54 +00:00
exampleBrowser - > update ( deltaTimeInSeconds ) ;
}
}
2016-03-03 02:01:33 +00:00
2018-09-23 21:17:31 +00:00
} while ( ! exampleBrowser - > requestedExit ( ) & & ( args - > m_cs - > getSharedParam ( 0 ) ! = eRequestTerminateExampleBrowser ) ) ;
}
else
2016-03-07 22:56:16 +00:00
{
args - > m_cs - > lock ( ) ;
2018-09-23 21:17:31 +00:00
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserInitializationFailed ) ;
2016-03-07 22:56:16 +00:00
args - > m_cs - > unlock ( ) ;
2016-03-03 02:01:33 +00:00
}
2016-03-07 22:56:16 +00:00
2016-03-03 02:01:33 +00:00
delete exampleBrowser ;
args - > m_cs - > lock ( ) ;
2018-09-23 21:17:31 +00:00
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserHasTerminated ) ;
2016-03-03 02:01:33 +00:00
args - > m_cs - > unlock ( ) ;
printf ( " finished \n " ) ;
//do nothing
}
2018-09-23 21:17:31 +00:00
void * ExampleBrowserMemoryFunc ( )
2016-03-03 02:01:33 +00:00
{
//don't create local store memory, just return 0
return new ExampleBrowserThreadLocalStorage ;
}
2018-05-02 21:32:43 +00:00
void ExampleBrowserMemoryReleaseFunc ( void * ptr )
{
2018-09-23 21:17:31 +00:00
ExampleBrowserThreadLocalStorage * p = ( ExampleBrowserThreadLocalStorage * ) ptr ;
2018-05-02 22:39:16 +00:00
delete p ;
2018-05-02 21:32:43 +00:00
}
2016-03-03 02:01:33 +00:00
struct btInProcessExampleBrowserInternalData
{
ExampleBrowserArgs m_args ;
b3ThreadSupportInterface * m_threadSupport ;
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * m_sharedMem ;
2016-03-03 02:01:33 +00:00
} ;
2018-09-23 21:17:31 +00:00
btInProcessExampleBrowserInternalData * btCreateInProcessExampleBrowser ( int argc , char * * argv2 , bool useInProcessMemory )
2016-03-03 02:01:33 +00:00
{
btInProcessExampleBrowserInternalData * data = new btInProcessExampleBrowserInternalData ;
2018-09-23 21:17:31 +00:00
2017-08-15 00:02:20 +00:00
data - > m_sharedMem = useInProcessMemory ? new InProcessMemory : 0 ;
2016-03-03 02:01:33 +00:00
int numThreads = 1 ;
int i ;
data - > m_threadSupport = createExampleBrowserThreadSupport ( numThreads ) ;
printf ( " argc=%d \n " , argc ) ;
2018-09-23 21:17:31 +00:00
for ( i = 0 ; i < argc ; i + + )
2016-03-03 02:01:33 +00:00
{
2018-09-23 21:17:31 +00:00
printf ( " argv[%d] = %s \n " , i , argv2 [ i ] ) ;
2016-03-03 02:01:33 +00:00
}
2018-09-23 21:17:31 +00:00
for ( i = 0 ; i < data - > m_threadSupport - > getNumTasks ( ) ; i + + )
2016-03-03 02:01:33 +00:00
{
2018-09-23 21:17:31 +00:00
ExampleBrowserThreadLocalStorage * storage = ( ExampleBrowserThreadLocalStorage * ) data - > m_threadSupport - > getThreadLocalMemory ( i ) ;
2016-03-03 02:01:33 +00:00
b3Assert ( storage ) ;
storage - > threadId = i ;
2016-03-10 22:36:46 +00:00
storage - > m_sharedMem = data - > m_sharedMem ;
2016-03-03 02:01:33 +00:00
}
data - > m_args . m_cs = data - > m_threadSupport - > createCriticalSection ( ) ;
2018-09-23 21:17:31 +00:00
data - > m_args . m_cs - > setSharedParam ( 0 , eExampleBrowserIsUnInitialized ) ;
data - > m_args . m_argc = argc ;
data - > m_args . m_argv = argv2 ;
2016-03-03 02:01:33 +00:00
2018-09-23 21:17:31 +00:00
for ( i = 0 ; i < numThreads ; i + + )
2016-03-03 02:01:33 +00:00
{
2018-09-23 21:17:31 +00:00
data - > m_threadSupport - > runTask ( B3_THREAD_SCHEDULE_TASK , ( void * ) & data - > m_args , i ) ;
2016-03-03 02:01:33 +00:00
}
2018-09-23 21:17:31 +00:00
while ( data - > m_args . m_cs - > getSharedParam ( 0 ) = = eExampleBrowserIsUnInitialized )
2016-03-07 22:56:16 +00:00
{
2016-08-13 19:21:18 +00:00
b3Clock : : usleep ( 1000 ) ;
2016-03-07 22:56:16 +00:00
}
2016-03-03 02:01:33 +00:00
return data ;
}
bool btIsExampleBrowserTerminated ( btInProcessExampleBrowserInternalData * data )
{
2018-09-23 21:17:31 +00:00
return ( data - > m_args . m_cs - > getSharedParam ( 0 ) = = eExampleBrowserHasTerminated ) ;
2016-03-03 02:01:33 +00:00
}
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * btGetSharedMemoryInterface ( btInProcessExampleBrowserInternalData * data )
{
return data - > m_sharedMem ;
}
2016-03-03 02:01:33 +00:00
void btShutDownExampleBrowser ( btInProcessExampleBrowserInternalData * data )
{
int numActiveThreads = 1 ;
data - > m_args . m_cs - > lock ( ) ;
2018-09-23 21:17:31 +00:00
data - > m_args . m_cs - > setSharedParam ( 0 , eRequestTerminateExampleBrowser ) ;
2016-03-03 02:01:33 +00:00
data - > m_args . m_cs - > unlock ( ) ;
while ( numActiveThreads )
2018-09-23 21:17:31 +00:00
{
int arg0 , arg1 ;
if ( data - > m_threadSupport - > isTaskCompleted ( & arg0 , & arg1 , 0 ) )
{
numActiveThreads - - ;
printf ( " numActiveThreads = %d \n " , numActiveThreads ) ;
}
else
{
// printf("polling..");
b3Clock : : usleep ( 0 ) ;
}
} ;
2016-03-03 02:01:33 +00:00
2016-07-09 22:09:09 +00:00
printf ( " btShutDownExampleBrowser stopping threads \n " ) ;
2017-01-10 22:57:16 +00:00
data - > m_threadSupport - > deleteCriticalSection ( data - > m_args . m_cs ) ;
2016-03-03 02:01:33 +00:00
delete data - > m_threadSupport ;
2016-03-10 22:36:46 +00:00
delete data - > m_sharedMem ;
2016-03-03 02:01:33 +00:00
delete data ;
}
2016-04-14 15:51:20 +00:00
struct btInProcessExampleBrowserMainThreadInternalData
{
2018-09-23 21:17:31 +00:00
ExampleEntriesPhysicsServer m_examples ;
DefaultBrowser * m_exampleBrowser ;
SharedMemoryInterface * m_sharedMem ;
b3Clock m_clock ;
2016-04-14 15:51:20 +00:00
} ;
2018-09-23 21:17:31 +00:00
btInProcessExampleBrowserMainThreadInternalData * btCreateInProcessExampleBrowserMainThread ( int argc , char * * argv , bool useInProcessMemory )
2016-04-14 15:51:20 +00:00
{
2018-09-23 21:17:31 +00:00
btInProcessExampleBrowserMainThreadInternalData * data = new btInProcessExampleBrowserMainThreadInternalData ;
data - > m_examples . initExampleEntries ( ) ;
data - > m_exampleBrowser = new DefaultBrowser ( & data - > m_examples ) ;
data - > m_sharedMem = useInProcessMemory ? new InProcessMemory : 0 ;
data - > m_exampleBrowser - > setSharedMemoryInterface ( data - > m_sharedMem ) ;
2017-01-16 06:26:11 +00:00
bool init ;
2018-09-23 21:17:31 +00:00
init = data - > m_exampleBrowser - > init ( argc , argv ) ;
data - > m_clock . reset ( ) ;
return data ;
2016-04-14 15:51:20 +00:00
}
bool btIsExampleBrowserMainThreadTerminated ( btInProcessExampleBrowserMainThreadInternalData * data )
{
2018-09-23 21:17:31 +00:00
return data - > m_exampleBrowser - > requestedExit ( ) ;
2016-04-14 15:51:20 +00:00
}
void btUpdateInProcessExampleBrowserMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
2018-09-23 21:17:31 +00:00
float deltaTimeInSeconds = data - > m_clock . getTimeMicroseconds ( ) / 1000000.f ;
data - > m_clock . reset ( ) ;
data - > m_exampleBrowser - > updateGraphics ( ) ;
data - > m_exampleBrowser - > update ( deltaTimeInSeconds ) ;
2016-04-14 15:51:20 +00:00
}
void btShutDownExampleBrowserMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
2018-09-23 21:17:31 +00:00
data - > m_exampleBrowser - > setSharedMemoryInterface ( 0 ) ;
delete data - > m_exampleBrowser ;
delete data ;
2016-04-14 15:51:20 +00:00
}
class SharedMemoryInterface * btGetSharedMemoryInterfaceMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
2018-09-23 21:17:31 +00:00
return data - > m_sharedMem ;
2016-04-14 15:51:20 +00:00
}