Skip to content

Commit

Permalink
PyBullet OpenGL/EGL hardware getCameraImage: use glViewport to reduce…
Browse files Browse the repository at this point in the history
… the glReadPixels calling cost dramatically for small images

PyBullet Allow OpenGL/EGL hardware to render segmentation mask. Use pybullet.ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX or pybullet.ER_SEGMENTATION_MASK
PyBullet.removeBody fix indexing bug (use foundIndex, not i)
PyBullet bump up version to 2.2.3
  • Loading branch information
erwincoumans committed Sep 30, 2018
1 parent 254edb6 commit 5bcd437
Show file tree
Hide file tree
Showing 32 changed files with 740 additions and 183 deletions.
6 changes: 6 additions & 0 deletions build3/stringifyShaders.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
rem @echo off




premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/segmentationMaskInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/segmentationMaskInstancingVS.h" --stringname="segmentationMaskInstancingVertexShader" stringify
premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/segmentationMaskInstancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/segmentationMaskInstancingPS.h" --stringname="segmentationMaskInstancingFragmentShader" stringify


premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/instancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/instancingVS.h" --stringname="instancingVertexShader" stringify
premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/instancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/instancingPS.h" --stringname="instancingFragmentShader" stringify
premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/pointSpriteVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/pointSpriteVS.h" --stringname="pointSpriteVertexShader" stringify
Expand Down
1 change: 1 addition & 0 deletions examples/CommonInterfaces/CommonGraphicsAppInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct CommonGraphicsApp
virtual void dumpFramesToVideo(const char* mp4Filename) {}

virtual void getScreenPixels(unsigned char* rgbaBuffer, int bufferSizeInBytes, float* depthBuffer, int depthBufferSizeInBytes) {}
virtual void setViewport(int width, int height) {}

virtual void getBackgroundColor(float* red, float* green, float* blue) const
{
Expand Down
1 change: 1 addition & 0 deletions examples/CommonInterfaces/CommonRenderInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum
B3_USE_SHADOWMAP_RENDERMODE_REFLECTION,
B3_USE_SHADOWMAP_RENDERMODE_REFLECTION_PLANE,
B3_USE_PROJECTIVE_TEXTURE_RENDERMODE,
B3_SEGMENTATION_MASK_RENDERMODE,
};

struct GfxVertexFormat0
Expand Down
91 changes: 83 additions & 8 deletions examples/ExampleBrowser/OpenGLGuiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ struct OpenGLGuiHelperInternalData

btAlignedObjectArray<unsigned char> m_rgbaPixelBuffer1;
btAlignedObjectArray<float> m_depthBuffer1;
btAlignedObjectArray<int> m_segmentationMaskBuffer;
btHashMap<MyHashShape, int> m_hashShapes;

VisualizerFlagCallback m_visualizerFlagCallback;
Expand Down Expand Up @@ -990,7 +991,7 @@ void OpenGLGuiHelper::setVisualizerFlag(int flag, int enable)
getRenderInterface()->setPlaneReflectionShapeIndex(enable);
}
if (m_data->m_visualizerFlagCallback)
(m_data->m_visualizerFlagCallback)(flag, enable);
(m_data->m_visualizerFlagCallback)(flag, enable!=0);
}

void OpenGLGuiHelper::resetCamera(float camDist, float yaw, float pitch, float camPosX, float camPosY, float camPosZ)
Expand Down Expand Up @@ -1070,15 +1071,19 @@ void OpenGLGuiHelper::setProjectiveTexture(bool useProjectiveTexture)
m_data->m_glApp->m_renderer->setProjectiveTexture(useProjectiveTexture);
}


void OpenGLGuiHelper::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)
{
int sourceWidth = m_data->m_glApp->m_window->getWidth() * m_data->m_glApp->m_window->getRetinaScale();
int sourceHeight = m_data->m_glApp->m_window->getHeight() * m_data->m_glApp->m_window->getRetinaScale();


int sourceWidth = btMin(destinationWidth, (int)(m_data->m_glApp->m_window->getWidth() * m_data->m_glApp->m_window->getRetinaScale()));
int sourceHeight = btMin(destinationHeight, (int)(m_data->m_glApp->m_window->getHeight() * m_data->m_glApp->m_window->getRetinaScale()));
m_data->m_glApp->setViewport(sourceWidth, sourceHeight);

if (numPixelsCopied)
*numPixelsCopied = 0;
Expand All @@ -1099,8 +1104,7 @@ void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const floa
BT_PROFILE("renderScene");
getRenderInterface()->renderScene();
}
getRenderInterface()->setActiveCamera(oldCam);


{
BT_PROFILE("copy pixels");
btAlignedObjectArray<unsigned char> sourceRgbaPixelBuffer;
Expand Down Expand Up @@ -1151,6 +1155,65 @@ void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const floa
}
}

//segmentation mask

if (segmentationMaskBuffer)
{
{
m_data->m_glApp->m_window->startRendering();
BT_PROFILE("renderScene");
getRenderInterface()->renderSceneInternal(B3_SEGMENTATION_MASK_RENDERMODE);
}

{
BT_PROFILE("copy pixels");
btAlignedObjectArray<unsigned char> sourceRgbaPixelBuffer;
btAlignedObjectArray<float> sourceDepthBuffer;
//copy the image into our local cache
sourceRgbaPixelBuffer.resize(sourceWidth * sourceHeight * numBytesPerPixel);
sourceDepthBuffer.resize(sourceWidth * sourceHeight);
{
BT_PROFILE("getScreenPixelsSegmentationMask");
m_data->m_glApp->getScreenPixels(&(sourceRgbaPixelBuffer[0]), sourceRgbaPixelBuffer.size(), &sourceDepthBuffer[0], sizeof(float) * sourceDepthBuffer.size());
}
m_data->m_segmentationMaskBuffer.resize(destinationWidth * destinationHeight,-1);

//rescale and flip
{
BT_PROFILE("resize and flip segmentation mask");
for (int j = 0; j < destinationHeight; j++)
{
for (int i = 0; i < destinationWidth; i++)
{
int xIndex = int(float(i) * (float(sourceWidth) / float(destinationWidth)));
int yIndex = int(float(destinationHeight - 1 - j) * (float(sourceHeight) / float(destinationHeight)));
btClamp(xIndex, 0, sourceWidth);
btClamp(yIndex, 0, sourceHeight);
int bytesPerPixel = 4; //RGBA
int sourcePixelIndex = (xIndex + yIndex * sourceWidth) * bytesPerPixel;
int sourceDepthIndex = xIndex + yIndex * sourceWidth;

if (segmentationMaskBuffer)
{
float depth = sourceDepthBuffer[sourceDepthIndex];
if (depth<1)
{
int segMask = sourceRgbaPixelBuffer[sourcePixelIndex + 0]+256*(sourceRgbaPixelBuffer[sourcePixelIndex + 1])+256*256*(sourceRgbaPixelBuffer[sourcePixelIndex + 2]);
m_data->m_segmentationMaskBuffer[i + j * destinationWidth] = segMask;
} else
{
m_data->m_segmentationMaskBuffer[i + j * destinationWidth] = -1;
}
}
}
}
}
}
}


getRenderInterface()->setActiveCamera(oldCam);

if (1)
{
getRenderInterface()->getActiveCamera()->disableVRCamera();
Expand All @@ -1159,6 +1222,8 @@ void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const floa
getRenderInterface()->updateCamera(dg.upAxis);
m_data->m_glApp->m_window->startRendering();
}


}
if (pixelsRGBA)
{
Expand All @@ -1178,9 +1243,19 @@ void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const floa
depthBuffer[i] = m_data->m_depthBuffer1[i + startPixelIndex];
}
}
if (segmentationMaskBuffer)
{
BT_PROFILE("copy segmentation mask pixels");
for (int i = 0; i < numRequestedPixels; i++)
{
segmentationMaskBuffer[i] = m_data->m_segmentationMaskBuffer[i + startPixelIndex];
}
}
if (numPixelsCopied)
*numPixelsCopied = numRequestedPixels;
}

m_data->m_glApp->setViewport(-1,-1);
}

struct MyConvertPointerSizeT
Expand Down Expand Up @@ -1271,10 +1346,10 @@ void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape,
b3Assert(collisionShape->getUserPointer());
btSoftBody* psb = (btSoftBody*)collisionShape->getUserPointer();
gfxVertices.resize(psb->m_faces.size() * 3);
int i, j, k;
for (i = 0; i < psb->m_faces.size(); i++) // Foreach face

for (int i = 0; i < psb->m_faces.size(); i++) // Foreach face
{
for (k = 0; k < 3; k++) // Foreach vertex on a face
for (int k = 0; k < 3; k++) // Foreach vertex on a face
{
int currentIndex = i * 3 + k;
for (int j = 0; j < 3; j++)
Expand Down
Loading

0 comments on commit 5bcd437

Please sign in to comment.