CommonOpenCLBase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef COMMON_MULTI_BODY_SETUP_H
  2. #define COMMON_MULTI_BODY_SETUP_H
  3. #include "../CommonInterfaces/CommonExampleInterface.h"
  4. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  5. #include "../CommonInterfaces/CommonRenderInterface.h"
  6. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  7. #include "../CommonInterfaces/CommonWindowInterface.h"
  8. #include "../CommonInterfaces/CommonCameraInterface.h"
  9. #include "GpuDemoInternalData.h"
  10. #include "Bullet3Common/b3Scalar.h"
  11. #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
  12. struct CommonOpenCLBase : public CommonExampleInterface
  13. {
  14. struct GUIHelperInterface* m_guiHelper;
  15. struct GpuDemoInternalData* m_clData;
  16. CommonOpenCLBase (GUIHelperInterface* helper)
  17. :m_guiHelper(helper),
  18. m_clData(0)
  19. {
  20. m_clData = new GpuDemoInternalData();
  21. }
  22. virtual ~CommonOpenCLBase()
  23. {
  24. delete m_clData;
  25. m_clData = 0;
  26. }
  27. virtual void stepSimulation(float deltaTime)
  28. {
  29. }
  30. virtual void initCL(int preferredDeviceIndex, int preferredPlatformIndex)
  31. {
  32. void* glCtx=0;
  33. void* glDC = 0;
  34. int ciErrNum = 0;
  35. cl_device_type deviceType = CL_DEVICE_TYPE_GPU;
  36. //if (gAllowCpuOpenCL)
  37. // deviceType = CL_DEVICE_TYPE_ALL;
  38. // if (useInterop)
  39. // {
  40. // m_data->m_clContext = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, glCtx, glDC);
  41. // } else
  42. {
  43. m_clData->m_clContext = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, 0,0,preferredDeviceIndex, preferredPlatformIndex,&m_clData->m_platformId);
  44. }
  45. oclCHECKERROR(ciErrNum, CL_SUCCESS);
  46. int numDev = b3OpenCLUtils::getNumDevices(m_clData->m_clContext);
  47. if (numDev>0)
  48. {
  49. m_clData->m_clDevice= b3OpenCLUtils::getDevice(m_clData->m_clContext,0);
  50. m_clData->m_clQueue = clCreateCommandQueue(m_clData->m_clContext, m_clData->m_clDevice, 0, &ciErrNum);
  51. oclCHECKERROR(ciErrNum, CL_SUCCESS);
  52. b3OpenCLDeviceInfo info;
  53. b3OpenCLUtils::getDeviceInfo(m_clData->m_clDevice,&info);
  54. m_clData->m_clDeviceName = info.m_deviceName;
  55. m_clData->m_clInitialized = true;
  56. }
  57. }
  58. virtual void exitCL()
  59. {
  60. if (m_clData && m_clData->m_clInitialized)
  61. {
  62. clReleaseCommandQueue(m_clData->m_clQueue);
  63. clReleaseContext(m_clData->m_clContext);
  64. m_clData->m_clInitialized = false;
  65. }
  66. }
  67. virtual void renderScene()
  68. {
  69. if (m_guiHelper->getRenderInterface())
  70. {
  71. m_guiHelper->getRenderInterface()->renderScene();
  72. }
  73. }
  74. virtual void physicsDebugDraw(int debugDrawFlags)
  75. {
  76. }
  77. virtual bool keyboardCallback(int key, int state)
  78. {
  79. return false;//don't handle this key
  80. }
  81. virtual bool mouseMoveCallback(float x,float y)
  82. {
  83. return false;
  84. }
  85. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  86. {
  87. CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
  88. if (!renderer)
  89. {
  90. b3Assert(0);
  91. return false;
  92. }
  93. CommonWindowInterface* window = m_guiHelper->getAppInterface()->m_window;
  94. if (state==1)
  95. {
  96. if(button==0 && (!window->isModifierKeyPressed(B3G_ALT) && !window->isModifierKeyPressed(B3G_CONTROL) ))
  97. {
  98. /*btVector3 camPos;
  99. renderer->getActiveCamera()->getCameraPosition(camPos);
  100. btVector3 rayFrom = camPos;
  101. btVector3 rayTo = getRayTo(int(x),int(y));
  102. pickBody(rayFrom, rayTo);
  103. */
  104. }
  105. } else
  106. {
  107. if (button==0)
  108. {
  109. // removePickingConstraint();
  110. //remove p2p
  111. }
  112. }
  113. //printf("button=%d, state=%d\n",button,state);
  114. return false;
  115. }
  116. };
  117. #endif //COMMON_MULTI_BODY_SETUP_H