From 3bdcf23a05e1e2dc2a2466242a7d036ca45dbf26 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Sat, 13 Aug 2016 12:21:18 -0700 Subject: [PATCH] Add sleep to avoid 100% busy CPU loop in PhysicsServerExample Added btClock::usleep Fix broken TinyRenderer example code. --- build3/premake4.lua | 6 +++--- build_visual_studio.bat | 3 ++- .../InProcessExampleBrowser.cpp | 2 ++ .../ExampleBrowser/OpenGLExampleBrowser.cpp | 18 ++++++++++++------ .../SharedMemory/PhysicsServerExample.cpp | 14 +++++++++++++- .../main_tinyrenderer_single_example.cpp | 7 ++++++- examples/Utils/b3Clock.cpp | 19 +++++++++++++++++++ examples/Utils/b3Clock.h | 4 ++++ examples/pybullet/premake4.lua | 1 - 9 files changed, 61 insertions(+), 13 deletions(-) diff --git a/build3/premake4.lua b/build3/premake4.lua index 744a55214..d8826a348 100644 --- a/build3/premake4.lua +++ b/build3/premake4.lua @@ -96,8 +96,8 @@ end if os.is("Windows") then - default_python_include_dir = "C:/Python34/include" - default_python_lib_dir = "C:/Python34/libs" + default_python_include_dir = "C:\Python-3.5.2/include" + default_python_lib_dir = "C:/Python-3.5.2/libs" end newoption @@ -164,7 +164,7 @@ end platforms {"x32"} end else - platforms {"x64"} + platforms {"x32","x64"} end configuration {"x32"} diff --git a/build_visual_studio.bat b/build_visual_studio.bat index 0da0089b1..5f33e30e7 100644 --- a/build_visual_studio.bat +++ b/build_visual_studio.bat @@ -2,7 +2,8 @@ rem premake4 --with-pe vs2010 rem premake4 --bullet2demos vs2010 cd build3 -premake4 --enable_openvr --targetdir="../bin" vs2010 +premake4 --enable_openvr --targetdir="../bin" vs2010 +rem premake4 --enable_openvr --enable_pybullet --targetdir="../bin" vs2010 rem premake4 --targetdir="../server2bin" vs2010 rem cd vs2010 rem rename 0_Bullet3Solution.sln 0_server.sln diff --git a/examples/ExampleBrowser/InProcessExampleBrowser.cpp b/examples/ExampleBrowser/InProcessExampleBrowser.cpp index e562b5d52..4924a5da9 100644 --- a/examples/ExampleBrowser/InProcessExampleBrowser.cpp +++ b/examples/ExampleBrowser/InProcessExampleBrowser.cpp @@ -332,6 +332,7 @@ btInProcessExampleBrowserInternalData* btCreateInProcessExampleBrowser(int argc, while (data->m_args.m_cs->getSharedParam(0)==eExampleBrowserIsUnInitialized) { + b3Clock::usleep(1000); } return data; @@ -366,6 +367,7 @@ void btShutDownExampleBrowser(btInProcessExampleBrowserInternalData* data) } else { // printf("polling.."); + b3Clock::usleep(1000); } }; diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index 5866e0ae4..6a8c87355 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -107,7 +107,7 @@ static CommonExampleInterface* sCurrentDemo = 0; static b3AlignedObjectArray allNames; static float gFixedTimeStep = 0; bool gAllowRetina = true; - +bool gDisableDemoSelection = false; static class ExampleEntries* gAllExamples=0; bool sUseOpenGL2 = false; bool drawGUI=true; @@ -556,9 +556,11 @@ struct MyMenuItemHander :public Gwen::Event::Handler Gwen::String laa = Gwen::Utility::UnicodeToString(la); //const char* ha = laa.c_str(); - - selectDemo(sCurrentHightlighted); - saveCurrentSettings(sCurrentDemoIndex, startFileName); + if (!gDisableDemoSelection ) + { + selectDemo(sCurrentHightlighted); + saveCurrentSettings(sCurrentDemoIndex, startFileName); + } } void onButtonC(Gwen::Controls::Base* pControl) { @@ -580,8 +582,11 @@ struct MyMenuItemHander :public Gwen::Event::Handler */ // printf("onKeyReturn ! \n"); - selectDemo(sCurrentHightlighted); - saveCurrentSettings(sCurrentDemoIndex, startFileName); + if (!gDisableDemoSelection ) + { + selectDemo(sCurrentHightlighted); + saveCurrentSettings(sCurrentDemoIndex, startFileName); + } } @@ -1228,5 +1233,6 @@ void OpenGLExampleBrowser::update(float deltaTime) void OpenGLExampleBrowser::setSharedMemoryInterface(class SharedMemoryInterface* sharedMem) { + gDisableDemoSelection = true; sSharedMem = sharedMem; } diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index ca868b29f..59e81ac4c 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -107,6 +107,7 @@ struct MotionThreadLocalStorage }; int skip = 0; +int skip1 = 0; void MotionThreadFunc(void* userPtr,void* lsMemory) { @@ -134,8 +135,15 @@ void MotionThreadFunc(void* userPtr,void* lsMemory) if (deltaTimeInSeconds<(1./5000.)) { skip++; + skip1++; + if (0==(skip1&0x3)) + { + b3Clock::usleep(250); + } } else { + skip1=0; + //process special controller commands, such as //VR controller button press/release and controller motion @@ -186,8 +194,9 @@ void MotionThreadFunc(void* userPtr,void* lsMemory) } - printf("finished, #skip = %d\n",skip); + printf("finished, #skip = %d, skip1 = %d\n",skip,skip1); skip=0; + skip1=0; //do nothing } @@ -638,8 +647,10 @@ void PhysicsServerExample::initPhysics() int index = 0; m_threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*) &this->m_args[w], w); + while (m_args[w].m_cs->getSharedParam(0)==eMotionIsUnInitialized) { + b3Clock::usleep(1000); } } @@ -669,6 +680,7 @@ void PhysicsServerExample::exitPhysics() } else { + b3Clock::usleep(1000); } }; diff --git a/examples/StandaloneMain/main_tinyrenderer_single_example.cpp b/examples/StandaloneMain/main_tinyrenderer_single_example.cpp index 5babcb023..6ca0efa36 100644 --- a/examples/StandaloneMain/main_tinyrenderer_single_example.cpp +++ b/examples/StandaloneMain/main_tinyrenderer_single_example.cpp @@ -348,7 +348,12 @@ struct TinyRendererGUIHelper : public GUIHelperInterface } - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16],unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int width, int height, int* numPixelsCopied) + + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied) { if (numPixelsCopied) *numPixelsCopied = 0; diff --git a/examples/Utils/b3Clock.cpp b/examples/Utils/b3Clock.cpp index 6802726d9..d2d5d0930 100644 --- a/examples/Utils/b3Clock.cpp +++ b/examples/Utils/b3Clock.cpp @@ -36,6 +36,7 @@ const T& b3ClockMin(const T& a, const T& b) #else //_WIN32 #include +#include #endif //_WIN32 @@ -227,3 +228,21 @@ double b3Clock::getTimeInSeconds() return double(getTimeMicroseconds()/1.e6); } +void b3Clock::usleep(int microSeconds) +{ +#ifdef _WIN32 + int millis = microSeconds/1000; + if (millis < 1) + { + millis = 1; + } + Sleep(millis); +#else + + usleep(microSeconds); / + //struct timeval tv; + //tv.tv_sec = microSeconds/1000000L; + //tv.tv_usec = microSeconds%1000000L; + //return select(0, 0, 0, 0, &tv); +#endif +} diff --git a/examples/Utils/b3Clock.h b/examples/Utils/b3Clock.h index cb1c0c29d..10a36686a 100644 --- a/examples/Utils/b3Clock.h +++ b/examples/Utils/b3Clock.h @@ -28,6 +28,10 @@ public: /// the Clock was created. double getTimeInSeconds(); + ///Sleep for 'microSeconds', to yield to other threads and not waste 100% CPU cycles. + ///Note that some operating systems may sleep a longer time. + static void usleep(int microSeconds); + private: struct b3ClockData* m_data; }; diff --git a/examples/pybullet/premake4.lua b/examples/pybullet/premake4.lua index a0585a128..cacb7218e 100644 --- a/examples/pybullet/premake4.lua +++ b/examples/pybullet/premake4.lua @@ -5,7 +5,6 @@ project ("pybullet") kind "SharedLib" targetsuffix ("") targetprefix ("") - targetextension (".so") includedirs {"../../src", "../../examples", "../../examples/ThirdPartyLibs"} defines {"PHYSICS_IN_PROCESS_EXAMPLE_BROWSER"}