added OpenCL cloth demo, contributed by AMD.

updated GpuSoftBodySolvers
updated DirectCompute cloth demo
This commit is contained in:
erwin.coumans
2010-08-14 00:56:17 +00:00
parent 40958f2b4a
commit 4f9b450200
72 changed files with 7524 additions and 843 deletions

View File

@@ -14,7 +14,7 @@ IF(BUILD_CPU_DEMOS)
CollisionInterfaceDemo ConcaveConvexcastDemo SimplexDemo DynamicControlDemo
DoublePrecisionDemo ConcaveDemo CollisionDemo
ContinuousConvexCollision ConcaveRaycastDemo GjkConvexCastDemo
MultiMaterialDemo SerializeDemo InternalEdgeDemo
MultiMaterialDemo SerializeDemo InternalEdgeDemo
)
ELSE()
SET(SharedDemoSubdirs
@@ -28,6 +28,7 @@ ENDIF()
MultiThreadedDemo
VectorAdd_OpenCL
ParticlesOpenCL
OpenCLClothDemo
)
ELSE (USE_GLUT)

View File

@@ -1,6 +1,6 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
@@ -13,6 +13,8 @@ subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BT_DIRECT_COMPUTE_SUPPORT_HPP
#define BT_DIRECT_COMPUTE_SUPPORT_HPP

View File

@@ -1,3 +1,18 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
class cap
{

View File

@@ -1,4 +1,22 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <fstream>
#include <iostream>
#include <iomanip>
class piece_of_cloth
{
@@ -171,7 +189,8 @@ public:
pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);
pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );
//pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );
pd3dImmediateContext->DrawIndexed( ((height-1)*(width-1)*3*2), 0, ( UINT )pSubset->VertexStart );
}
SAFE_RELEASE(pd3dImmediateContext);
@@ -246,7 +265,7 @@ public:
//unsigned int indices[] = {0,1,2, 1,3,2};
unsigned int* indices = new unsigned int[width*3*2+2 + height*width*3*2];
unsigned int* indices = new unsigned int[(height-1)*(width-1)*3*2];
for(int y = 0; y < height-1; y++)
{
@@ -265,7 +284,8 @@ public:
}
}
bufferDesc.ByteWidth = sizeof(unsigned int)*(width*3*2+2 + height*width*3*2);
bufferDesc.ByteWidth = sizeof(unsigned int)*((height-1)*(width-1)*3*2);
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
InitData.pSysMem = indices;

View File

@@ -32,18 +32,15 @@ class btDX11SIMDAwareSoftBodySolver;
#include "BulletSoftBody/btSoftBodySolvers.h"
#include "BulletSoftBody/btDefaultSoftBodySolver.h"
#include "BulletMultiThreaded/GpuSoftBodySolvers/CPU/btSoftBodySolver_CPU.h"
//#include "BulletSoftBody/Solvers/CPU/btAcceleratedSoftBody_CPUVertexSolver.h"
#include "BulletMultiThreaded/GpuSoftBodySolvers/DX11/btSoftBodySolver_DX11.h"
//#include "BulletSoftBody/Solvers/DX11/btAcceleratedSoftBody_DX11SIMDAwareSolver.h"
//#include "BulletSoftBody/btAcceleratedSoftBody_DXVertexBuffers.h"
#include "BulletMultiThreaded/GpuSoftBodySolvers/DX11/btSoftBodySolver_DX11SIMDAware.h"
#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"
//#define USE_SIMDAWARE_SOLVER
#define USE_GPU_SOLVER
//#define USE_VERTEX_SOLVER
#define USE_SIMDAWARE_SOLVER
//#define USE_GPU_SOLVER
#define USE_GPU_COPY
const int numFlags = 2;
const int numFlags = 5;
const int clothWidth = 40;
const int clothHeight = 60;//60;
float _windAngle = 1.0;//0.4;
@@ -206,6 +203,7 @@ btSoftRigidDynamicsWorld* m_dynamicsWorld;
btDefaultSoftBodySolver *g_defaultSolver = NULL;
btCPUSoftBodySolver *g_cpuSolver = NULL;
btDX11SoftBodySolver *g_dx11Solver = NULL;
btDX11SIMDAwareSoftBodySolver *g_dx11SIMDSolver = NULL;
btSoftBodySolver *g_solver = NULL;
@@ -454,12 +452,17 @@ void initBullet(void)
#ifdef USE_GPU_SOLVER
g_dx11Solver = new btDX11SoftBodySolver( g_pd3dDevice, DXUTGetD3D11DeviceContext() );
g_solver = g_dx11Solver;
#else
#ifdef USE_SIMDAWARE_SOLVER
g_dx11SIMDSolver = new btDX11SIMDAwareSoftBodySolver( g_pd3dDevice, DXUTGetD3D11DeviceContext() );
g_solver = g_dx11SIMDSolver;
#else
g_cpuSolver = new btCPUSoftBodySolver;
g_solver = g_cpuSolver;
//g_defaultSolver = new btDefaultSoftBodySolver;
//g_solver = g_defaultSolver;
#endif
#endif
@@ -1260,6 +1263,9 @@ void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
delete g_cpuSolver;
if( g_dx11Solver )
delete g_dx11Solver;
if( g_dx11SIMDSolver )
delete g_dx11SIMDSolver;
for(int i=0; i< m_collisionShapes.size(); i++)
delete m_collisionShapes[i];

View File

@@ -1,3 +1,18 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
class cylinder
{

View File

@@ -0,0 +1,102 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL
${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
ADD_DEFINITIONS(-DUSE_AMD_OPENCL)
ADD_DEFINITIONS(-DCL_PLATFORM_AMD)
IF (INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
INCLUDE_DIRECTORIES( $ENV{==ATISTREAMSDKROOT=}/include )
IF (CMAKE_CL_64)
SET(CMAK_ATISTREAMSDK_LIBPATH $ENV{==ATISTREAMSDKROOT=}/lib/x86_64 )
ELSE(CMAKE_CL_64)
SET(CMAK_ATISTREAMSDK_LIBPATH $ENV{==ATISTREAMSDKROOT=}/lib/x86 )
ENDIF(CMAKE_CL_64)
ELSE()
INCLUDE_DIRECTORIES( $ENV{ATISTREAMSDKROOT}/include )
IF (CMAKE_CL_64)
SET(CMAK_ATISTREAMSDK_LIBPATH $ENV{ATISTREAMSDKROOT}/lib/x86_64 )
ELSE(CMAKE_CL_64)
SET(CMAK_ATISTREAMSDK_LIBPATH $ENV{ATISTREAMSDKROOT}/lib/x86 )
ENDIF(CMAKE_CL_64)
ENDIF()
IF (CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY
${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew64.lib )
ELSE(CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY ${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew32.lib )
ENDIF(CMAKE_CL_64)
IF (USE_GLUT)
LINK_LIBRARIES(
OpenGLSupport
BulletSoftBodySolvers_OpenCL_AMD
BulletSoftBodySolvers_CPU
BulletMultiThreaded
BulletSoftBody
BulletDynamics
BulletCollision
LinearMath
${GLUT_glut_LIBRARY}
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
${CMAK_GLEW_LIBRARY}
${CMAK_ATISTREAMSDK_LIBPATH}/OpenCL.lib
)
ADD_EXECUTABLE(AppOpenCLClothDemo_AMD
../cl_cloth_demo.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
../gl_win.cpp
../clstuff.cpp
../bmpLoader.cpp
../bmpLoader.h
../clstuff.h
../gl_win.h
)
ELSE (USE_GLUT)
ENDIF (USE_GLUT)
IF(WIN32)
IF (CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_AMD POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/glut64.dll ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_AMD POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW64.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ELSE(CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_AMD POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLUT32.DLL ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_AMD POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW32.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ENDIF(CMAKE_CL_64)
ENDIF(WIN32)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_AMD POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/amdFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/atiFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
)
IF (UNIX)
TARGET_LINK_LIBRARIES(AppOpenCLClothDemo_AMD pthread)
ENDIF(UNIX)

View File

@@ -0,0 +1,60 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL
${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
IF (APPLE)
FIND_LIBRARY(OPENCL_LIBRARY OpenCL DOC "OpenCL lib for OSX")
FIND_PATH(OPENCL_INCLUDE_DIR OpenCL/cl.h DOC "Include for OpenCL on OSX")
ENDIF (APPLE)
IF (USE_GLUT)
LINK_LIBRARIES(
OpenGLSupport
BulletSoftBodySolvers_OpenCL_Apple
BulletSoftBodySolvers_CPU
BulletMultiThreaded
BulletSoftBody
BulletDynamics
BulletCollision
LinearMath
${OPENCL_LIBRARY}
${GLUT_glut_LIBRARY}
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
${CMAK_GLEW_LIBRARY}
)
ADD_EXECUTABLE(AppOpenCLClothDemo_Apple
../cl_cloth_demo.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
../gl_win.cpp
../clstuff.cpp
../bmpLoader.cpp
../bmpLoader.h
../clstuff.h
../gl_win.h
)
ELSE (USE_GLUT)
ENDIF (USE_GLUT)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Apple POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/amdFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/atiFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
)
IF (UNIX)
TARGET_LINK_LIBRARIES(AppOpenCLClothDemo_Apple pthread)
ENDIF(UNIX)

View File

@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CLClothDemo", "CLClothDemo.vcproj", "{A61906AF-B5DE-454E-99F6-B653C250D221}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A61906AF-B5DE-454E-99F6-B653C250D221}.Debug|Win32.ActiveCfg = Debug|Win32
{A61906AF-B5DE-454E-99F6-B653C250D221}.Debug|Win32.Build.0 = Debug|Win32
{A61906AF-B5DE-454E-99F6-B653C250D221}.Release|Win32.ActiveCfg = Release|Win32
{A61906AF-B5DE-454E-99F6-B653C250D221}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,233 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="CLClothDemo"
ProjectGUID="{A61906AF-B5DE-454E-99F6-B653C250D221}"
RootNamespace="CLClothDemo"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="S:\SVN\GpuClothAMD\Bullet\BulletTrunk\Glut;&quot;C:\Program Files (x86)\ATI Stream\include&quot;;..\..\..\projects\physics\Bullet\BulletTrunk\src;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\src"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkLibraryDependencies="false"
AdditionalDependencies="glew32.lib OpenCL.lib ..\..\lib\Debug\BulletDynamics.lib ..\..\lib\Debug\BulletCollision.lib ..\..\lib\Debug\LinearMath.lib ..\..\lib\Debug\BulletSoftBody.lib ..\..\lib\Debug\BulletSoftBodySolvers_CPU.lib ..\..\lib\Debug\BulletSoftBodySolvers_OpenCL.lib"
LinkIncremental="1"
AdditionalLibraryDirectories="&quot;C:\Program Files (x86)\ATI Stream\lib\x86&quot;;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\Glut;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\lib\Debug"
GenerateDebugInformation="true"
SubSystem="1"
ImportLibrary="S:\SVN\GpuClothAMD\Bullet\BulletTrunk\Demos\DX11ClothDemo\Debug\AppDX11ClothDemo.lib"
TargetMachine="0"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="S:\SVN\GpuClothAMD\Bullet\BulletTrunk\Glut;&quot;C:\Program Files (x86)\ATI Stream\include&quot;;..\..\..\projects\physics\Bullet\BulletTrunk\src;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\src"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="glew32.lib OpenCL.lib BulletSoftBody.lib BulletDynamics.lib BulletCollision.lib LinearMath.lib BulletSoftBodySolvers_CPU.lib BulletSoftBodySolvers_OpenCL.lib"
LinkIncremental="1"
AdditionalLibraryDirectories="..\Bullet\BulletTrunk\lib\Release\;&quot;C:\Program Files (x86)\ATI Stream\lib\x86&quot;;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\Glut;S:\SVN\GpuClothAMD\Bullet\BulletTrunk\lib\Release"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\bmpLoader.cpp"
>
</File>
<File
RelativePath=".\cl_cloth_demo.cpp"
>
</File>
<File
RelativePath=".\clstuff.cpp"
>
</File>
<File
RelativePath=".\gl_win.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\bmpLoader.hpp"
>
</File>
<File
RelativePath=".\btOpenCLSupport.h"
>
</File>
<File
RelativePath=".\cloth.h"
>
</File>
<File
RelativePath=".\clstuff.hpp"
>
</File>
<File
RelativePath=".\gl_win.hpp"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,15 @@
IF(BUILD_MINICL_OPENCL_DEMOS)
SUBDIRS( MiniCL )
ENDIF()
IF(BUILD_AMD_OPENCL_DEMOS)
SUBDIRS(AMD)
ENDIF()
IF(BUILD_NVIDIA_OPENCL_DEMOS)
SUBDIRS(NVidia)
ENDIF()
IF(APPLE)
SUBDIRS(Apple)
ENDIF()

View File

@@ -0,0 +1,86 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL
${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
ADD_DEFINITIONS(-DUSE_MINICL)
IF (WIN32)
IF (CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY
${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew64.lib )
ELSE(CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY ${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew32.lib )
ENDIF(CMAKE_CL_64)
ENDIF()
IF (USE_GLUT)
LINK_LIBRARIES(
OpenGLSupport
BulletSoftBodySolvers_OpenCL_Mini
BulletSoftBodySolvers_CPU
MiniCL
BulletMultiThreaded
BulletSoftBody
BulletDynamics
BulletCollision
LinearMath
${GLUT_glut_LIBRARY}
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
${CMAK_GLEW_LIBRARY}
)
ADD_EXECUTABLE(AppOpenCLClothDemo_Mini
../cl_cloth_demo.cpp
../gl_win.cpp
../clstuff.cpp
../bmpLoader.cpp
../bmpLoader.h
../clstuff.h
../gl_win.h
${BULLET_PHYSICS_SOURCE_DIR}/src/BulletMultiThreaded/GpuSoftBodySolvers/OpenCL/MiniCL/MiniCLTaskWrap.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
)
ELSE (USE_GLUT)
ENDIF (USE_GLUT)
IF(WIN32)
IF (CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Mini POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/glut64.dll ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Mini POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW64.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ELSE(CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Mini POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLUT32.DLL ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Mini POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW32.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ENDIF(CMAKE_CL_64)
ENDIF(WIN32)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_Mini POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/amdFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/atiFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
)
IF (UNIX)
TARGET_LINK_LIBRARIES(AppOpenCLClothDemo_Mini pthread)
ENDIF(UNIX)

View File

@@ -0,0 +1,102 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL
${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
IF(INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
INCLUDE_DIRECTORIES( $ENV{==NVSDKCOMPUTE_ROOT=}/OpenCL/common/inc )
IF (CMAKE_CL_64)
SET(CMAK_NVSDKCOMPUTE_LIBPATH $ENV{==NVSDKCOMPUTE_ROOT=}/OpenCL/common/lib/x64 )
ELSE(CMAKE_CL_64)
SET(CMAK_NVSDKCOMPUTE_LIBPATH $ENV{==NVSDKCOMPUTE_ROOT=}/OpenCL/common/lib/Win32 )
ENDIF(CMAKE_CL_64)
ELSE()
INCLUDE_DIRECTORIES( $ENV{NVSDKCOMPUTE_ROOT}/OpenCL/common/inc )
IF (CMAKE_CL_64)
SET(CMAK_NVSDKCOMPUTE_LIBPATH $ENV{NVSDKCOMPUTE_ROOT}/OpenCL/common/lib/x64 )
ELSE(CMAKE_CL_64)
SET(CMAK_NVSDKCOMPUTE_LIBPATH $ENV{NVSDKCOMPUTE_ROOT}/OpenCL/common/lib/Win32 )
ENDIF(CMAKE_CL_64)
ENDIF()
IF (CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY
${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew64.lib )
ELSE(CMAKE_CL_64)
SET(CMAK_GLEW_LIBRARY ${BULLET_PHYSICS_SOURCE_DIR}/Glut/glew32.lib )
ENDIF(CMAKE_CL_64)
IF (USE_GLUT)
LINK_LIBRARIES(
OpenGLSupport
BulletSoftBodySolvers_OpenCL_NVidia
BulletSoftBodySolvers_CPU
BulletMultiThreaded
BulletSoftBody
BulletDynamics
BulletCollision
LinearMath
${GLUT_glut_LIBRARY}
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
${CMAK_GLEW_LIBRARY}
${CMAK_NVSDKCOMPUTE_LIBPATH}/OpenCL.lib
)
ADD_EXECUTABLE(AppOpenCLClothDemo_NVidia
../cl_cloth_demo.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
../gl_win.cpp
../clstuff.cpp
../bmpLoader.cpp
../bmpLoader.h
../clstuff.h
../gl_win.h
)
ELSE (USE_GLUT)
ENDIF (USE_GLUT)
IF(WIN32)
IF (CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_NVidia POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/glut64.dll ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_NVidia POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW64.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ELSE(CMAKE_CL_64)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_NVidia POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLUT32.DLL ${CMAKE_CURRENT_BINARY_DIR}
)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_NVidia POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLEW32.DLL ${CMAKE_CURRENT_BINARY_DIR})
ENDIF()
ENDIF(CMAKE_CL_64)
ENDIF(WIN32)
ADD_CUSTOM_COMMAND( TARGET AppOpenCLClothDemo_NVidia POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/amdFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenCLClothDemo/atiFlag.bmp ${CMAKE_CURRENT_BINARY_DIR}
)
IF (UNIX)
TARGET_LINK_LIBRARIES(AppOpenCLClothDemo_NVidia pthread)
ENDIF(UNIX)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@@ -0,0 +1,325 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "bmpLoader.h"
#include <new>
#include <cstring>
#include <cstdio>
namespace amd
{
static const short bitMapID = 19778;
void
BitMap::releaseResources(void)
{
if (pixels_ != NULL) {
delete[] pixels_;
}
if (colors_ != NULL) {
delete[] colors_;
}
pixels_ = NULL;
colors_ = NULL;
isLoaded_ = false;
}
BitMap& BitMap::operator=(const BitMap& rhs)
{
if (this == &rhs) {
return *this;
}
// Copy header
id = rhs.id;
size = rhs.size;
reserved1 = rhs.reserved1;
reserved2 = rhs.reserved2;
offset = rhs.offset;
// Copy header info
sizeInfo = rhs.sizeInfo;
width = rhs.width;
height = rhs.height;
planes = rhs.planes;
bitsPerPixel = rhs.bitsPerPixel;
compression = rhs.compression;
imageSize = rhs.imageSize;
xPelsPerMeter = rhs.xPelsPerMeter;
yPelsPerMeter = rhs.yPelsPerMeter;
clrUsed = rhs.clrUsed;
clrImportant = rhs.clrImportant;
numColors_ = rhs.numColors_;
isLoaded_ = rhs.isLoaded_;
pixels_ = NULL;
colors_ = NULL;
if (isLoaded_) {
if (rhs.colors_ != NULL) {
colors_ = new ColorPalette[numColors_];
if (colors_ == NULL) {
isLoaded_ = false;
return *this;
}
memcpy(colors_, rhs.colors_, numColors_ * sizeof(ColorPalette));
}
pixels_ = new uchar4[width * height];
if (pixels_ == NULL) {
delete[] colors_;
colors_ = NULL;
isLoaded_ = false;
return *this;
}
memcpy(pixels_, rhs.pixels_, width * height * sizeof(uchar4));
}
return *this;
}
void
BitMap::load(const char * filename)
{
// Release any existing resources
releaseResources();
// Open BMP file
FILE * fd = fopen(filename, "rb");
// Opened OK
if (fd != NULL) {
// Read header
fread((BitMapHeader *)this, sizeof(BitMapHeader), 1, fd);
// Failed to read header
if (ferror(fd)) {
fclose(fd);
return;
}
// Confirm that we have a bitmap file
if (id != bitMapID) {
fclose(fd);
return;
}
// Read map info header
fread((BitMapInfoHeader *)this, sizeof(BitMapInfoHeader), 1, fd);
// Failed to read map info header
if (ferror(fd)) {
fclose(fd);
return;
}
// No support for compressed images
if (compression) {
fclose(fd);
return;
}
// Support only 8 or 24 bits images
if (bitsPerPixel < 8) {
fclose(fd);
return;
}
// Store number of colors
numColors_ = 1 << bitsPerPixel;
//load the palate for 8 bits per pixel
if(bitsPerPixel == 8) {
colors_ = new ColorPalette[numColors_];
if (colors_ == NULL) {
fclose(fd);
return;
}
fread(
(char *)colors_,
numColors_ * sizeof(ColorPalette),
1,
fd);
// Failed to read colors
if (ferror(fd)) {
fclose(fd);
return;
}
}
// Allocate buffer to hold all pixels
unsigned int sizeBuffer = size - offset;
unsigned char * tmpPixels = new unsigned char[sizeBuffer];
if (tmpPixels == NULL) {
delete colors_;
colors_ = NULL;
fclose(fd);
return;
}
// Read pixels from file, including any padding
fread(tmpPixels, sizeBuffer * sizeof(unsigned char), 1, fd);
// Failed to read pixel data
if (ferror(fd)) {
delete colors_;
colors_ = NULL;
delete tmpPixels;
fclose(fd);
return;
}
// Allocate image
pixels_ = new uchar4[width * height];
if (pixels_ == NULL) {
delete colors_;
colors_ = NULL;
delete tmpPixels;
fclose(fd);
return;
}
// Set image, including w component (white)
memset(pixels_, 0xff, width * height * sizeof(uchar4));
unsigned int index = 0;
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
// Read RGB values
if (bitsPerPixel == 8) {
pixels_[(y * width + x)] = colors_[tmpPixels[index++]];
}
else { // 24 bit
pixels_[(y * width + x)].z = tmpPixels[index++];
pixels_[(y * width + x)].y = tmpPixels[index++];
pixels_[(y * width + x)].x = tmpPixels[index++];
}
}
// Handle padding
for(int x = 0; x < (4 - (3 * width) % 4) % 4; x++) {
index++;
}
}
// Loaded file so we can close the file.
fclose(fd);
delete[] tmpPixels;
// Loaded file so record this fact
isLoaded_ = true;
}
}
int
BitMap::colorIndex(uchar4 color)
{
for (int i = 0; i < numColors_; i++) {
if (colors_[i].x == color.x &&
colors_[i].y == color.y &&
colors_[i].z == color.z &&
colors_[i].w == color.w) {
return i;
}
}
return 0;
}
bool
BitMap::write(const char * filename)
{
if (!isLoaded_) {
return false;
}
// Open BMP file
FILE * fd = fopen(filename, "wb");
// Opened OK
if (fd != NULL) {
// Write header
fwrite((BitMapHeader *)this, sizeof(BitMapHeader), 1, fd);
// Failed to write header
if (ferror(fd)) {
fclose(fd);
return false;
}
// Write map info header
fwrite((BitMapInfoHeader *)this, sizeof(BitMapInfoHeader), 1, fd);
// Failed to write map info header
if (ferror(fd)) {
fclose(fd);
return false;
}
// Write palate for 8 bits per pixel
if(bitsPerPixel == 8) {
fwrite(
(char *)colors_,
numColors_ * sizeof(ColorPalette),
1,
fd);
// Failed to write colors
if (ferror(fd)) {
fclose(fd);
return false;
}
}
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
// Read RGB values
if (bitsPerPixel == 8) {
fputc(
colorIndex(
pixels_[(y * width + x)]),
fd);
}
else { // 24 bit
fputc(pixels_[(y * width + x)].z, fd);
fputc(pixels_[(y * width + x)].y, fd);
fputc(pixels_[(y * width + x)].x, fd);
if (ferror(fd)) {
fclose(fd);
return false;
}
}
}
// Add padding
for(int x = 0; x < (4 - (3 * width) % 4) % 4; x++) {
fputc(0, fd);
}
}
return true;
}
return false;
}
} // amd

View File

@@ -0,0 +1,201 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2010 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BMPLOADER_H_
#define BMPLOADER_H_
#include <cstdlib>
#include <iostream>
namespace amd
{
//! @fixme this needs to be moved to common types header?
#pragma pack(1)
typedef struct
{
unsigned char x;
unsigned char y;
unsigned char z;
unsigned char w;
} uchar4;
typedef uchar4 ColorPalette;
//! \struct Bitmap header info
typedef struct {
short id;
int size;
short reserved1;
short reserved2;
int offset;
} BitMapHeader;
//! \struct Bitmap info header
typedef struct {
int sizeInfo;
int width;
int height;
short planes;
short bitsPerPixel;
unsigned compression;
unsigned imageSize;
int xPelsPerMeter;
int yPelsPerMeter;
int clrUsed;
int clrImportant;
} BitMapInfoHeader;
//! \class Bitmap used to load a bitmap image from a file.
class BitMap : public BitMapHeader, public BitMapInfoHeader
{
private:
uchar4 * pixels_;
int numColors_;
ColorPalette * colors_;
bool isLoaded_;
void releaseResources(void);
int colorIndex(uchar4 color);
public:
//! \brief Default constructor
BitMap()
: pixels_(NULL),
numColors_(0),
colors_(NULL),
isLoaded_(false)
{}
/*!\brief Constructor
*
* Tries to load bitmap image from filename provided.
*
* \param filename pointer to null terminated string that is the path and
* filename to the bitmap image to be loaded.
*
* In the base of an error, e.g. the bitmap file could not be loaded for
* some reason, then a following call to isLoaded will return false.
*/
BitMap(const char * filename)
: pixels_(NULL),
numColors_(0),
colors_(NULL),
isLoaded_(false)
{
load(filename);
}
/*! \brief Copy constructor
*
* \param rhs is the bitmap to be copied (cloned).
*/
BitMap(const BitMap& rhs)
{
*this = rhs;
}
//! \brief Destructor
~BitMap()
{
releaseResources();
}
/*! \brief Assignment
* \param rhs is the bitmap to be assigned (cloned).
*/
BitMap& operator=(const BitMap& rhs);
/*! \brief Load Bitmap image
*
* \param filename is a pointer to a null terminated string that is the
* path and filename name to the the bitmap file to be loaded.
*
* In the base of an error, e.g. the bitmap file could not be loaded for
* some reason, then a following call to isLoaded will return false.
*/
void
load(const char * filename);
/*! \brief Write Bitmap image
*
* \param filename is a pointer to a null terminated string that is the
* path and filename name to the the bitmap file to be written.
*
* \return In the case that the bitmap is written true is returned. In
* the case that a bitmap image is not already loaded or the write fails
* for some reason false is returned.
*/
bool
write(const char * filename);
/*! \brief Get image width
*
* \return If a bitmap image has been successfully loaded, then the width
* image is returned, otherwise -1;
*/
int
getWidth(void) const
{
if (isLoaded_) {
return width;
}
else {
return -1;
}
}
/*! \brief Get image height
*
* \return If a bitmap image has been successfully loaded, then the height
* image is returned, otherwise -1.
*/
int
getHeight(void) const
{
if (isLoaded_) {
return height;
}
else {
return -1;
}
}
/*! \brief Get image width
*
* \return If a bitmap image has been successfully loaded, then returns
* a pointer to image's pixels, otherwise NULL.
*/
const uchar4 *
getPixels(void) const { return pixels_; }
/*! \brief Is an image currently loaded
*
* \return If a bitmap image has been successfully loaded, then returns
* true, otherwise if an image could not be loaded or an image has yet
* to be loaded false is returned.
*/
bool
isLoaded(void) const { return isLoaded_; }
};
#pragma pack()
}
#endif // BMPLOADER_H_

View File

@@ -0,0 +1,189 @@
//
// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved.
//
#ifndef BMPLOADER_H_
#define BMPLOADER_H_
#include <cstdlib>
#include <iostream>
namespace amd
{
//! @fixme this needs to be moved to common types header?
#pragma pack(1)
typedef struct
{
unsigned char x;
unsigned char y;
unsigned char z;
unsigned char w;
} uchar4;
typedef uchar4 ColorPalette;
//! \struct Bitmap header info
typedef struct {
short id;
int size;
short reserved1;
short reserved2;
int offset;
} BitMapHeader;
//! \struct Bitmap info header
typedef struct {
int sizeInfo;
int width;
int height;
short planes;
short bitsPerPixel;
unsigned compression;
unsigned imageSize;
int xPelsPerMeter;
int yPelsPerMeter;
int clrUsed;
int clrImportant;
} BitMapInfoHeader;
//! \class Bitmap used to load a bitmap image from a file.
class BitMap : public BitMapHeader, public BitMapInfoHeader
{
private:
uchar4 * pixels_;
int numColors_;
ColorPalette * colors_;
bool isLoaded_;
void releaseResources(void);
int colorIndex(uchar4 color);
public:
//! \brief Default constructor
BitMap()
: pixels_(NULL),
numColors_(0),
colors_(NULL),
isLoaded_(false)
{}
/*!\brief Constructor
*
* Tries to load bitmap image from filename provided.
*
* \param filename pointer to null terminated string that is the path and
* filename to the bitmap image to be loaded.
*
* In the base of an error, e.g. the bitmap file could not be loaded for
* some reason, then a following call to isLoaded will return false.
*/
BitMap(const char * filename)
: pixels_(NULL),
numColors_(0),
colors_(NULL),
isLoaded_(false)
{
load(filename);
}
/*! \brief Copy constructor
*
* \param rhs is the bitmap to be copied (cloned).
*/
BitMap(const BitMap& rhs)
{
*this = rhs;
}
//! \brief Destructor
~BitMap()
{
releaseResources();
}
/*! \brief Assignment
* \param rhs is the bitmap to be assigned (cloned).
*/
BitMap& operator=(const BitMap& rhs);
/*! \brief Load Bitmap image
*
* \param filename is a pointer to a null terminated string that is the
* path and filename name to the the bitmap file to be loaded.
*
* In the base of an error, e.g. the bitmap file could not be loaded for
* some reason, then a following call to isLoaded will return false.
*/
void
load(const char * filename);
/*! \brief Write Bitmap image
*
* \param filename is a pointer to a null terminated string that is the
* path and filename name to the the bitmap file to be written.
*
* \return In the case that the bitmap is written true is returned. In
* the case that a bitmap image is not already loaded or the write fails
* for some reason false is returned.
*/
bool
write(const char * filename);
/*! \brief Get image width
*
* \return If a bitmap image has been successfully loaded, then the width
* image is returned, otherwise -1;
*/
int
getWidth(void) const
{
if (isLoaded_) {
return width;
}
else {
return -1;
}
}
/*! \brief Get image height
*
* \return If a bitmap image has been successfully loaded, then the height
* image is returned, otherwise -1.
*/
int
getHeight(void) const
{
if (isLoaded_) {
return height;
}
else {
return -1;
}
}
/*! \brief Get image width
*
* \return If a bitmap image has been successfully loaded, then returns
* a pointer to image's pixels, otherwise NULL.
*/
const uchar4 *
getPixels(void) const { return pixels_; }
/*! \brief Is an image currently loaded
*
* \return If a bitmap image has been successfully loaded, then returns
* true, otherwise if an image could not be loaded or an image has yet
* to be loaded false is returned.
*/
bool
isLoaded(void) const { return isLoaded_; }
};
#pragma pack()
}
#endif // BMPLOADER_H_

View File

@@ -0,0 +1,84 @@
#ifndef BT_OPENCL_SUPPORT_HPP
#define BT_OPENCL_SUPPORT_HPP
// OpenCL support
#include <CL/cl.hpp>
namespace BTAcceleratedSoftBody
{
class OpenCLSupportHelper
{
private:
cl::Context m_context;
std::vector<cl::Device> m_devices;
cl::CommandQueue m_queue;
public:
OpenCLSupportHelper()
{
}
virtual ~OpenCLSupportHelper()
{
}
cl::Device getDevice()
{
return m_devices[0];
}
cl::CommandQueue getCommandQueue()
{
return m_queue;
}
cl::Context getContext()
{
return m_context;
}
bool InitOpenCLDevice()
{
cl_int err;
std::vector<cl::Platform> platforms;
err = cl::Platform::get(&platforms);
checkErr(platforms.size() != 0 ? CL_SUCCESS : -1, "Platform::get()");
std::string platformVendor;
platforms[0].getInfo(CL_PLATFORM_VENDOR, &platformVendor);
//std::cout << "Platform is by: " << platformVendor << "\n";
intptr_t properties[] = {
CL_CONTEXT_PLATFORM, (intptr_t)platforms[0](),
0, 0
};
m_context = cl::Context(
CL_DEVICE_TYPE_GPU,
properties,
NULL,
NULL,
&err);
if (err != CL_SUCCESS)
{
btAssert( "Context::Context()" );
}
m_devices = m_context.getInfo<CL_CONTEXT_DEVICES>();
if( m_devices.size() <= 0 )
{
btAssert( "devices.size() > 0" );
}
m_queue = cl::CommandQueue(m_context, m_devices[0], 0, &err);
if (err != CL_SUCCESS)
{
btAssert( "CommandQueue::CommandQueue()");
}
}
};
} // namespace BTAcceleratedSoftBody
#endif // #ifndef BT_OPENCL_SUPPORT_HPP

View File

@@ -0,0 +1,470 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2008 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifdef _WIN32
#include <GL/glew.h>
#endif
#include "clstuff.h"
#include "gl_win.h"
#include "cloth.h"
#define USE_GPU_SOLVER
const int numFlags = 5;
const int clothWidth = 40;
const int clothHeight = 60;//60;
float _windAngle = 1.0;//0.4;
float _windStrength = 15;
#include <iostream>
using namespace std;
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btHashMap.h"
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
#include "vectormath/vmInclude.h"
#include "BulletMultiThreaded/GpuSoftBodySolvers/CPU/btSoftBodySolver_CPU.h"
#include "BulletMultiThreaded/GpuSoftBodySolvers/OpenCL/btSoftBodySolver_OpenCL.h"
using Vectormath::Aos::Vector3;
class piece_of_cloth;
class btBroadphaseInterface;
class btCollisionShape;
class btOverlappingPairCache;
class btCollisionDispatcher;
class btConstraintSolver;
struct btCollisionAlgorithmCreateFunc;
class btDefaultCollisionConfiguration;
namespace Vectormath
{
namespace Aos
{
class Transform3;
}
}
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;
btBroadphaseInterface* m_broadphase;
btCollisionDispatcher* m_dispatcher;
btConstraintSolver* m_solver;
btDefaultCollisionConfiguration* m_collisionConfiguration;
btCPUSoftBodySolver *g_cpuSolver = NULL;
btOpenCLSoftBodySolver *g_openCLSolver = NULL;
btSoftBodySolver *g_solver = NULL;
btAlignedObjectArray<btSoftBody *> m_flags;
btSoftRigidDynamicsWorld* m_dynamicsWorld;
btAlignedObjectArray<piece_of_cloth> cloths;
extern cl_context g_cxMainContext;
extern cl_device_id g_cdDevice;
extern cl_command_queue g_cqCommandQue;
const float flagSpacing = 30.f;
// Helper to test and add links correctly.
// Records links that have already been generated
static bool testAndAddLink( btAlignedObjectArray<int> &trianglesForLinks, btSoftBody *softBody, int triangle, int *triangleVertexIndexArray, int numVertices, int vertex0, int vertex1, int nonLinkVertex, btSoftBody::Material *structuralMaterial, bool createBendLinks, btSoftBody::Material *bendMaterial )
{
if( trianglesForLinks[ numVertices * vertex0 + vertex1 ] >= 0 && createBendLinks)
{
// Already have link so find other triangle and generate cross link
int otherTriangle = trianglesForLinks[numVertices * vertex0 + vertex1];
int otherIndices[3] = {triangleVertexIndexArray[otherTriangle * 3], triangleVertexIndexArray[otherTriangle * 3 + 1], triangleVertexIndexArray[otherTriangle * 3 + 2]};
int nodeA;
// Test all links of the other triangle against this link. The one that's not part of it is what we want.
if( otherIndices[0] != vertex0 && otherIndices[0] != vertex1 )
nodeA = otherIndices[0];
if( otherIndices[1] != vertex0 && otherIndices[1] != vertex1 )
nodeA = otherIndices[1];
if( otherIndices[2] != vertex0 && otherIndices[2] != vertex1 )
nodeA = otherIndices[2];
softBody->appendLink( nodeA, nonLinkVertex, bendMaterial );
} else {
// Don't yet have link so create it
softBody->appendLink( vertex0, vertex1, structuralMaterial );
// If we added a new link, set the triangle array
trianglesForLinks[numVertices * vertex0 + vertex1] = triangle;
trianglesForLinks[numVertices * vertex1 + vertex0] = triangle;
}
return true;
}
btSoftBody *createFromIndexedMesh( btVector3 *vertexArray, int numVertices, int *triangleVertexIndexArray, int numTriangles, bool createBendLinks )
{
btSoftBody* softBody = new btSoftBody(&(m_dynamicsWorld->getWorldInfo()), numVertices, vertexArray, 0);
btSoftBody::Material * structuralMaterial = softBody->appendMaterial();
btSoftBody::Material * bendMaterial;
if( createBendLinks )
{
bendMaterial = softBody->appendMaterial();
bendMaterial->m_kLST = 0.7;
} else {
bendMaterial = NULL;
}
structuralMaterial->m_kLST = 1.0;
// List of values for each link saying which triangle is associated with that link
// -1 to start. Once a value is entered we know the "other" triangle
// and can add a link across the link
btAlignedObjectArray<int> triangleForLinks;
triangleForLinks.resize( numVertices * numVertices, -1 );
int numLinks = 0;
for( int triangle = 0; triangle < numTriangles; ++triangle )
{
int index[3] = {triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2]};
softBody->appendFace( index[0], index[1], index[2] );
// Generate the structural links directly from the triangles
testAndAddLink( triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial );
testAndAddLink( triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial );
testAndAddLink( triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial);
}
return softBody;
}
/**
* Create a sequence of flag objects and add them to the world.
*/
void createFlag( btSoftBodySolver &solver, int width, int height, btAlignedObjectArray<btSoftBody *> &flags )
{
// First create a triangle mesh to represent a flag
using Vectormath::Aos::Matrix3;
using Vectormath::Aos::Vector3;
// Allocate a simple mesh consisting of a vertex array and a triangle index array
btIndexedMesh mesh;
mesh.m_numVertices = width*height;
mesh.m_numTriangles = 2*(width-1)*(height-1);
btVector3 *vertexArray = new btVector3[mesh.m_numVertices];
mesh.m_vertexBase = reinterpret_cast<const unsigned char*>(vertexArray);
int *triangleVertexIndexArray = new int[3*mesh.m_numTriangles];
mesh.m_triangleIndexBase = reinterpret_cast<const unsigned char*>(triangleVertexIndexArray);
mesh.m_triangleIndexStride = sizeof(int)*3;
mesh.m_vertexStride = sizeof(Vector3);
// Generate normalised object space vertex coordinates for a rectangular flag
float zCoordinate = 0.0f;
Matrix3 defaultScale(Vector3(5.f, 0.f, 0.f), Vector3(0.f, 20.f, 0.f), Vector3(0.f, 0.f, 1.f));
for( int y = 0; y < height; ++y )
{
float yCoordinate = y*2.0f/float(height) - 1.0f;
for( int x = 0; x < width; ++x )
{
float xCoordinate = x*2.0f/float(width) - 1.0f;
Vector3 vertex(xCoordinate, yCoordinate, zCoordinate);
Vector3 transformedVertex = defaultScale*vertex;
vertexArray[y*width + x] = btVector3(transformedVertex.getX(), transformedVertex.getY(), transformedVertex.getZ() );
}
}
// Generate vertex indices for triangles
for( int y = 0; y < (height-1); ++y )
{
for( int x = 0; x < (width-1); ++x )
{
// Triangle 0
// Top left of square on mesh
{
int vertex0 = y*width + x;
int vertex1 = vertex0 + 1;
int vertex2 = vertex0 + width;
int triangleIndex = 2*y*(width-1) + 2*x;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex)/sizeof(int)] = vertex0;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex+1)/sizeof(int)+1] = vertex1;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex+2)/sizeof(int)+2] = vertex2;
}
// Triangle 1
// Bottom right of square on mesh
{
int vertex0 = y*width + x + 1;
int vertex1 = vertex0 + width;
int vertex2 = vertex1 - 1;
int triangleIndex = 2*y*(width-1) + 2*x + 1;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex)/sizeof(int)] = vertex0;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex)/sizeof(int)+1] = vertex1;
triangleVertexIndexArray[(mesh.m_triangleIndexStride*triangleIndex)/sizeof(int)+2] = vertex2;
}
}
}
float rotateAngleRoundZ = 0.5;
float rotateAngleRoundX = 0.5;
btMatrix3x3 defaultRotate;
defaultRotate[0] = btVector3(cos(rotateAngleRoundZ), sin(rotateAngleRoundZ), 0.f);
defaultRotate[1] = btVector3(-sin(rotateAngleRoundZ), cos(rotateAngleRoundZ), 0.f);
defaultRotate[2] = btVector3(0.f, 0.f, 1.f);
btMatrix3x3 defaultRotateX;
defaultRotateX[0] = btVector3(1.f, 0.f, 0.f);
defaultRotateX[1] = btVector3( 0.f, cos(rotateAngleRoundX), sin(rotateAngleRoundX));
defaultRotateX[2] = btVector3(0.f, -sin(rotateAngleRoundX), cos(rotateAngleRoundX));
btMatrix3x3 defaultRotateAndScale( (defaultRotateX*defaultRotate) );
// Construct the sequence flags applying a slightly different translation to each one to arrange them
// appropriately in the scene.
for( int i = 0; i < numFlags; ++i )
{
float zTranslate = flagSpacing * (i-numFlags/2);
btVector3 defaultTranslate(0.f, 20.f, zTranslate);
btTransform transform( defaultRotateAndScale, defaultTranslate );
btSoftBody *softBody = createFromIndexedMesh( vertexArray, mesh.m_numVertices, triangleVertexIndexArray, mesh.m_numTriangles, true );
for( int i = 0; i < mesh.m_numVertices; ++i )
{
softBody->setMass(i, 10.f/mesh.m_numVertices);
}
softBody->setMass((height-1)*(width), 0.f);
softBody->setMass((height-1)*(width) + width - 1, 0.f);
softBody->setMass((height-1)*width + width/2, 0.f);
softBody->m_cfg.collisions = btSoftBody::fCollision::CL_SS+btSoftBody::fCollision::CL_RS;
flags.push_back( softBody );
softBody->transform( transform );
m_dynamicsWorld->addSoftBody( softBody );
}
delete [] vertexArray;
delete [] triangleVertexIndexArray;
}
void updatePhysicsWorld()
{
static int counter = 0;
// Change wind velocity a bit based on a frame counter
if( (counter % 400) == 0 )
{
_windAngle = (_windAngle + 0.05f);
if( _windAngle > (2*3.141) )
_windAngle = 0;
for( int flagIndex = 0; flagIndex < m_flags.size(); ++flagIndex )
{
btSoftBody *cloth = 0;
cloth = m_flags[flagIndex];
float localWind = _windAngle + 0.5*(((float(rand())/RAND_MAX))-0.1);
float xCoordinate = cos(localWind)*_windStrength;
float zCoordinate = sin(localWind)*_windStrength;
cloth->setWindVelocity( btVector3(xCoordinate, 0, zCoordinate) );
}
}
//btVector3 origin( capCollider->getWorldTransform().getOrigin() );
//origin.setX( origin.getX() + 0.05 );
//capCollider->getWorldTransform().setOrigin( origin );
counter++;
}
void initBullet(void)
{
#ifdef USE_GPU_SOLVER
g_openCLSolver = new btOpenCLSoftBodySolver( g_cqCommandQue, g_cxMainContext);
g_solver = g_openCLSolver;
#else
g_cpuSolver = new btCPUSoftBodySolver;
g_solver = g_cpuSolver;
#endif
m_collisionConfiguration = new btDefaultCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_broadphase = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
m_solver = sol;
m_dynamicsWorld = new btSoftRigidDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration, g_solver);
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
m_collisionShapes.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-50,0));
m_dynamicsWorld->getWorldInfo().air_density = (btScalar)1.2;
m_dynamicsWorld->getWorldInfo().water_density = 0;
m_dynamicsWorld->getWorldInfo().water_offset = 0;
m_dynamicsWorld->getWorldInfo().water_normal = btVector3(0,0,0);
m_dynamicsWorld->getWorldInfo().m_gravity.setValue(0,-10,0);
#if 0
{
btScalar mass(0.);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
groundShape->calculateLocalInertia(mass,localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//add the body to the dynamics world
m_dynamicsWorld->addRigidBody(body);
}
#endif
#ifdef USE_GPU_SOLVER
createFlag( *g_openCLSolver, clothWidth, clothHeight, m_flags );
#else
createFlag( *g_cpuSolver, clothWidth, clothHeight, m_flags );
#endif
// Create output buffer descriptions for ecah flag
// These describe where the simulation should send output data to
for( int flagIndex = 0; flagIndex < m_flags.size(); ++flagIndex )
{
// m_flags[flagIndex]->setWindVelocity( Vectormath::Aos::Vector3( 0.f, 0.f, 15.f ) );
// In this case we have a DX11 output buffer with a vertex at index 0, 8, 16 and so on as well as a normal at 3, 11, 19 etc.
// Copies will be performed GPU-side directly into the output buffer
btCPUVertexBufferDescriptor *vertexBufferDescriptor = new btCPUVertexBufferDescriptor(reinterpret_cast< float* >(cloths[flagIndex].cpu_buffer), 0, 8, 3, 8);
cloths[flagIndex].m_vertexBufferDescriptor = vertexBufferDescriptor;
}
g_solver->optimize( m_dynamicsWorld->getSoftBodyArray() );
}
btClock m_clock;
void doFlags()
{
//float ms = getDeltaTimeMicroseconds();
btScalar dt = (btScalar)m_clock.getTimeMicroseconds();
m_clock.reset();
///step the simulation
if( m_dynamicsWorld )
{
m_dynamicsWorld->stepSimulation(dt/1000000.);
static int frameCount = 0;
frameCount++;
if (frameCount==100)
{
m_dynamicsWorld->stepSimulation(1./60.,0);
CProfileManager::dumpAll();
}
updatePhysicsWorld();
}
for( int flagIndex = 0; flagIndex < m_flags.size(); ++flagIndex )
{
g_solver->copySoftBodyToVertexBuffer( m_flags[flagIndex], cloths[flagIndex].m_vertexBufferDescriptor );
cloths[flagIndex].draw();
}
}
int main(int argc, char *argv[])
{
initCL();
cloths.resize(numFlags);
for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex )
{
cloths[flagIndex].create_buffers(clothWidth, clothHeight);
}
initBullet();
m_dynamicsWorld->stepSimulation(1./60.,0);
preInitGL(argc, argv);
std::string flagTexs[] = {
"atiFlag.bmp",
"amdFlag.bmp",
};
int numFlagTexs = 2;
for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex )
{
cloths[flagIndex].create_texture(flagTexs[flagIndex % numFlagTexs]);
cloths[flagIndex].x_offset = 0;
cloths[flagIndex].y_offset = 0;
cloths[flagIndex].z_offset = 0;
}
goGL();
return 0;
}

View File

@@ -0,0 +1,183 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2008 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "gl_win.h" //for OpenGL stuff
#include "bmpLoader.h"
#include <string>
#include "LinearMath/btScalar.h"
struct vertex_struct
{
float pos[3];
float normal[3];
float texcoord[2];
};
class btVertexBufferDescriptor;
class piece_of_cloth
{
public:
void destroy(void)
{
if(created)
{
if(cpu_buffer) delete [] cpu_buffer;
}
}
piece_of_cloth()
{
created = false;
cpu_buffer = NULL;
m_vertexBufferDescriptor = NULL;
}
bool created;
vertex_struct* cpu_buffer;
unsigned int* indices;
btVertexBufferDescriptor *m_vertexBufferDescriptor;
double x_offset, y_offset, z_offset;
int width;
int height;
GLuint texture;
void draw(void)
{
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0f, 1.0f, 1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, texture);
glVertexPointer( 3, GL_FLOAT, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].pos[0])) );
//glNormalPointer( 3, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].normal[0])) );
glTexCoordPointer( 2, GL_FLOAT, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].texcoord[0])) );
glDrawElements(GL_TRIANGLES, (height-1 )*(width-1)*3*2, GL_UNSIGNED_INT, indices);
// glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);
}
void create_texture(std::string filename)
{
amd::BitMap texBMP(filename.c_str());
if ( texBMP.isLoaded() ) {
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA8,
texBMP.getWidth(),
texBMP.getHeight(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
texBMP.getPixels());
glBindTexture(GL_TEXTURE_2D, 0);
}
else {
std::cout << "ERROR: could not load bitmap " << "texture.bmp" << std::endl;
exit(1);
}
}
void create_buffers(int width_, int height_)
{
width = width_;
height = height_;
created = true;
cpu_buffer = new vertex_struct[width*height];
memset(cpu_buffer, 0, width*height*sizeof(vertex_struct));
// Initial test data for rendering
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
double coord = btSin(x/5.0)*0.01;
//coord = sin(y/);
cpu_buffer[y*width+x].pos[0] = (x/((float)(width-1)))*1;
cpu_buffer[y*width+x].pos[1] = coord;
cpu_buffer[y*width+x].pos[2] = (y/((float)(height-1)))*1;
cpu_buffer[y*width+x].normal[0] = 1;
cpu_buffer[y*width+x].normal[1] = 0;
cpu_buffer[y*width+x].normal[2] = 0;
cpu_buffer[y*width+x].texcoord[0] = x/((float)(width-1));
cpu_buffer[y*width+x].texcoord[1] = y/((float)(height-1));
}
}
// Generate and fill index array for rendering
indices = new unsigned int[width*3*2+2 + height*width*3*2];
for(int y = 0; y < height-1; y++)
{
for(int x = 0; x < width-1; x++)
{
// *3 indices/triangle, *2 triangles/quad
int baseIndex = (x + y*(width-1))*3*2;
indices[baseIndex] = x + y*width;
indices[baseIndex+1] = x+1 + y*width;
indices[baseIndex+2] = x+width + y*width;
indices[baseIndex+3] = x + 1 + y*width;
indices[baseIndex+4] = x+(width+1) + y*width;
indices[baseIndex+5] = x+width + y*width;
}
}
}
};

View File

@@ -0,0 +1,53 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2008 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "clstuff.h"
#include "gl_win.h"
#include "btOclCommon.h"
#include "btOclUtils.h"
#include "LinearMath/btScalar.h"
cl_context g_cxMainContext;
cl_device_id g_cdDevice;
cl_command_queue g_cqCommandQue;
void initCL(void)
{
int ciErrNum = 0;
//g_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_ALL, &ciErrNum);
//g_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_GPU, &ciErrNum);
//g_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_CPU, &ciErrNum);
//try CL_DEVICE_TYPE_DEBUG for sequential, non-threaded execution, when using MiniCL on CPU, it gives a full callstack at the crash in the kernel
//#ifdef USE_MINICL
// g_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_DEBUG, &ciErrNum);
//#else
g_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_ALL, &ciErrNum);
//#endif
oclCHECKERROR(ciErrNum, CL_SUCCESS);
g_cdDevice = btOclGetMaxFlopsDev(g_cxMainContext);
btOclPrintDevInfo(g_cdDevice);
// create a command-queue
g_cqCommandQue = clCreateCommandQueue(g_cxMainContext, g_cdDevice, 0, &ciErrNum);
oclCHECKERROR(ciErrNum, CL_SUCCESS);
}

View File

@@ -0,0 +1,10 @@
#ifndef __CLSTUFF_HDR__
#define __CLSTUFF_HDR__
void initCL(void);
#endif //__CLSTUFF_HDR__

View File

@@ -0,0 +1,10 @@
#ifndef __CLSTUFF_HDR__
#define __CLSTUFF_HDR__
void initCL(void);
#endif //__CLSTUFF_HDR__

View File

@@ -0,0 +1,7 @@
uniform sampler2D tex;
void main()
{
vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;
}

View File

@@ -0,0 +1,272 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2008 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "clstuff.h"
#include "gl_win.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <math.h>
#include <cmath>
#include <cstring>
//#ifndef _WIN32 && !defined(__APPLE__)
//#include <GL/glx.h>
//#endif //!_WIN32
static GLuint vbo = 0;
#ifdef _WIN32
#include <windows.h>
#endif
static unsigned int windowWidth = 1280;
static unsigned int windowHeight = 1024;
// mouse controls
int mouseOldX;
int mouseOldY;
int mouseButtons = 0;
float rotateX;
float rotateY;
float translateZ;
float translateX;
float translateY;
static GLuint glProgram;
void doFlags();
void render( void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// glDisable ( GL_CULL_FACE );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( translateX, translateY, translateZ );
glRotatef( rotateX, 0.5f , 0.0f, 0.0f );
glRotatef( rotateY, 0.0f, 0.5f, 0.0f );
// glDisable (GL_BLEND);
doFlags();
// TODO:
//glBindBuffer(GL_ARRAY_BUFFER, vbo);
//glVertexPointer(4, GL_FLOAT, 0, NULL);
//glEnableClientState(GL_VERTEX_ARRAY);
//glDrawArrays(GL_POINTS, 0, 4*4);
// glDisableClientState(GL_VERTEX_ARRAY);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// glUseProgram(0);
}
static void initGL(void)
{
//glClearColor( 0.05f, 0.0f, 0.1f, 0.1f );
glClearColor( 0.0f, 0.45f, 0.45f, 1.f);
#if 0
GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_shininess[] = { 50.0f };
GLfloat light_position[] = {
-10.f,
5.f,
-1.f,
1.0f };
glEnable ( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glEnable( GL_LINE_SMOOTH );
glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
glLightfv( GL_LIGHT0, GL_POSITION, light_position );
//glEnable( GL_LIGHTING );
//glEnable( GL_LIGHT0 ); // Switch on and crashes!
glEnable( GL_DEPTH_TEST );
#endif
#if 0
glEnable ( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glEnable( GL_LINE_SMOOTH );
glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
glLightfv( GL_LIGHT0, GL_POSITION, light_position );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_DEPTH_TEST );
#endif
rotateX = 0;
rotateY = 30;
translateX = 0.0f;
translateY = -30.0f;
translateZ = -120.0;
}
void display(void)
{
render();
glutSwapBuffers();
glutPostRedisplay();
}
void keyboard( unsigned char key, int /*x*/, int /*y*/)
{
switch( key) {
case('q') :
#ifdef _WIN32
case VK_ESCAPE:
#endif //_WIN32
exit(0);
break;
case('a'):
translateY += 0.1f;
break;
case('z'):
translateY -= 0.1f;
break;
case('d'):
translateX += 0.1f;
break;
case('s'):
translateX -= 0.1f;
break;
case('f'):
translateZ += 0.1f;
break;
case('g'):
translateZ -= 0.1f;
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouseButtons |= 1<<button;
} else if (state == GLUT_UP) {
mouseButtons = 0;
}
mouseOldX = x;
mouseOldY = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
float dx, dy;
dx = x - mouseOldX;
dy = y - mouseOldY;
if (mouseButtons & 1) {
rotateX += dy * 0.2;
rotateY += dx * 0.2;
}
else if (mouseButtons & 5) {
translateY -= dy * 0.01;
translateX -= dx * 0.01;
}
else if (mouseButtons & 4) {
translateZ += dy * 0.01;
}
mouseOldX = x;
mouseOldY = y;
}
void reshape (int w, int h)
{
windowWidth = w;
windowHeight = h;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60.0,
(GLfloat)windowWidth / (GLfloat) windowHeight,
0.1,
600.0f );
}
void goGL(void)
{
glutMainLoop();
}
void preInitGL(int argc, char ** argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize( windowWidth, windowHeight );
glutCreateWindow ("OpenCL Renderer");
initGL();
glViewport( 0, 0, windowWidth, windowHeight);
reshape( windowWidth, windowHeight );
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
}
/*
int getVBO( std::string, int s)
{
GLuint size = (GLuint)s;
if (vbo == 0) {
// Create VBO
// create buffer object
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
return vbo;
}
*/

View File

@@ -0,0 +1,49 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2008 Advanced Micro Devices
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef __GL_WIN_HDR__
#define __GL_WIN_HDR__
#ifdef _WIN32//for glut.h
#include <windows.h>
#endif
//think different
#if defined(__APPLE__) && !defined (VMDMESA)
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#ifdef _WINDOWS
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#else
#include <GL/glut.h>
#endif //_WINDOWS
#endif //APPLE
#include <string>
void goGL(void);
void preInitGL(int argc, char ** argv);
//int getVBO( std::string, int size );
#endif //__GL_WIN_HDR__

View File

@@ -0,0 +1,34 @@
#ifndef __GL_WIN_HDR__
#define __GL_WIN_HDR__
#ifdef _WIN32//for glut.h
#include <windows.h>
#endif
//think different
#if defined(__APPLE__) && !defined (VMDMESA)
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#ifdef _WINDOWS
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#else
#include <GL/glut.h>
#endif //_WINDOWS
#endif //APPLE
#include <string>
void goGL(void);
void preInitGL(int argc, char ** argv);
int getVBO( std::string, int size );
#endif //__GL_WIN_HDR__

View File

@@ -0,0 +1,535 @@
#pragma OPENCL EXTENSION cl_amd_printf : enable
#define float3 float4
#define uint3 uint4
#define PARTICLE_RADIUS 0.05;
#define width 1280
#define height 1024
#define B 0
#define T height
#define L 0
#define R width
#define shiftNumber 4
#define shiftMask 0xF
#define shiftValue 16.0f
#define stride 4
#define screenWidth1 width
#define screenHeight1 height
#define halfScreenWidth1 screenWidth1/2
#define halfScreenHeight1 screenHeight1/2
#define screenWidth1SubOne (screenWidth1-1)
#define screenHeight1SubOne (screenHeight1-1)
#define stride screenWidth1
#define screenPixelNumber screenWidth1*screenHeight1
#define depthBufferSize screenPixelNumber*depthComplexity
#define WGS 1
//---------------------------------------------------------------
struct __VSSpriteOut
{
float4 position;
float4 particlePosition;
};
typedef struct __VSSpriteout VSSpriteOut;
struct __GSSpriteOut
{
float4 position;
float2 textureUV;
// float4 viewSpacePosition;
// float4 particlePosition;
};
typedef struct __GSSpriteout GSSpriteOut;
//------------------------------------------------------------------------------
__constant float4 g_positions[4] =
{
(float4)(-1.0f, 1.0f, 0.0f, 0.0f),
(float4)( 1.0f, 1.0f, 0.0f, 0.0f),
(float4)( -1.0f, -1.0f, 0.0f, 0.0f),
(float4)( 1.0f, -1.0f, 0.0f, 0.0f)
};
__constant float2 g_texcoords[4] =
{
(float2)(0.0f,0.0f),
(float2)(1.0f,0.0f),
(float2)(0.0f,1.0f),
(float2)(1.0f,1.0f)
};
//------------------------------------------------------------------------------
void copyMatrix(
float matrix[16],
__constant float matrix0[16])
{
uint i;
for (i = 0; i < 16; i++) {
matrix[i] = matrix0[i];
}
}
void matrixMulLoopBody(
uint i,
float matrix[16],
__constant float matrix0[16],
__constant float matrix1[16])
{
matrix[i] = 0.0f;
matrix[i] += matrix0[(i%4) + (0*4)] * matrix1[(0) + ((i/4)*4)];
matrix[i] += matrix0[(i%4) + (1*4)] * matrix1[(1) + ((i/4)*4)];
matrix[i] += matrix0[(i%4) + (2*4)] * matrix1[(2) + ((i/4)*4)];
matrix[i] += matrix0[(i%4) + (3*4)] * matrix1[(3) + ((i/4)*4)];
}
void matrixMul(
float matrix[16],
__constant float matrix0[16],
__constant float matrix1[16])
{
matrixMulLoopBody(0, matrix, matrix0, matrix1);
matrixMulLoopBody(1, matrix, matrix0, matrix1);
matrixMulLoopBody(2, matrix, matrix0, matrix1);
matrixMulLoopBody(3, matrix, matrix0, matrix1);
matrixMulLoopBody(4, matrix, matrix0, matrix1);
matrixMulLoopBody(5, matrix, matrix0, matrix1);
matrixMulLoopBody(6, matrix, matrix0, matrix1);
matrixMulLoopBody(7, matrix, matrix0, matrix1);
matrixMulLoopBody(8, matrix, matrix0, matrix1);
matrixMulLoopBody(9, matrix, matrix0, matrix1);
matrixMulLoopBody(10, matrix, matrix0, matrix1);
matrixMulLoopBody(11, matrix, matrix0, matrix1);
matrixMulLoopBody(12, matrix, matrix0, matrix1);
matrixMulLoopBody(13, matrix, matrix0, matrix1);
matrixMulLoopBody(14, matrix, matrix0, matrix1);
matrixMulLoopBody(15, matrix, matrix0, matrix1);
}
float4 matrixVectorMul(float matrix[16], float4 vector)
{
float4 result;
result.x = matrix[0]*vector.x + matrix[4+0]*vector.y + matrix[8+0]*vector.z + matrix[12+0]*vector.w;
result.y = matrix[1]*vector.x + matrix[4+1]*vector.y + matrix[8+1]*vector.z + matrix[12+1]*vector.w;
result.z = matrix[2]*vector.x + matrix[4+2]*vector.y + matrix[8+2]*vector.z + matrix[12+2]*vector.w;
result.w = matrix[3]*vector.x + matrix[4+3]*vector.y + matrix[8+3]*vector.z + matrix[12+3]*vector.w;
return result;
}
float3 matrixVector3Mul(__constant float matrix[9], float3 vector)
{
float3 result;
result.x = matrix[0]*vector.x + matrix[3+0]*vector.y + matrix[6+0]*vector.z;
result.y = matrix[1]*vector.x + matrix[3+1]*vector.y + matrix[6+1]*vector.z;
result.z = matrix[2]*vector.x + matrix[3+2]*vector.y + matrix[6+2]*vector.z;
return result;
}
//------------------------------------------------------------------------------
//#define DEVICE_CPU 1
#if defined(DEVICE_CPU)
void printMatrix(char * name, __constant float matrix[16])
{
printf("%s[0] = %f, %f, %f, %f\n", name, matrix[0], matrix[1], matrix[2], matrix[3]);
printf("%s[1] = %f, %f, %f, %f\n", name, matrix[4], matrix[5], matrix[6], matrix[7]);
printf("%s[2] = %f, %f, %f, %f\n", name, matrix[8], matrix[9], matrix[10], matrix[11]);
printf("%s[3] = %f, %f, %f, %f\n", name, matrix[12], matrix[13], matrix[14], matrix[15]);
}
#endif
#if 1
__kernel void vertexShader(
__constant float modelview[16],
__constant float projection[16],
__global float4 * inputPrimitives,
__global float4 * outputPrimitives)
{
float matrix[16];
float4 gl_Vertex;
float4 gl_Position;
uint id = get_global_id(0);
gl_Vertex = inputPrimitives[id];
// gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex
matrixMul(matrix, projection, modelview);
gl_Position = matrixVectorMul(matrix, gl_Vertex);
outputPrimitives[id] = gl_Position;
}
#else
__kernel void vertexShader(
__constant float modelview[16],
__constant float projection[16],
__global float4 * inputPrimitives,
__global float4 * outputPrimitives)
{
uint id = get_global_id(0);
outputPrimitives[id] = inputPrimitives[id];
}
#endif
//-----------------------------------------------------------------------------------
__kernel void
clearImage(
__write_only image2d_t image,
float4 color)
{
int2 coords = (int2)(get_global_id(0), get_global_id(1));
write_imagef(image, coords, color);
}
// OpenGL viewport transformation
// The site http://research.cs.queensu.ca/~jstewart/454/notes/pipeline/
// contains a description of this process
void
viewportTransform(float4 v, __constant int4 viewport[1], float2 * output)
{
int4 vp = viewport[0];
*output
= 0.5f *
(float2)(v.x+1,v.y+1) *
(float2)((vp.s2-vp.s0) + vp.s0,
(vp.s3-vp.s1) + vp.s1);
}
#define PARTICLE_WIDTH 32.0f
#define PARTICLE_HEIGHT 32.0f
// Unoptimized triangle rasterizer function
// Details of the algorithm can be found here:
// http://www.devmaster.net/forums/showthread.php?t=1884
//
void
rasterizerUnOpt(
__global struct __GSSpriteOut * outputPrimitives,
// __global float4 * outputPrimitives,
__constant int4 viewport[1],
__write_only image2d_t screen,
__read_only image2d_t particle,
uint v1Offset,
uint v2Offset,
uint v3Offset,
__global float4 * debugOut1)
{
sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
uint id = get_global_id(0);
struct __GSSpriteOut output;
float2 v1, v2, v3;
float2 uv1, uv2, uv3;
output = outputPrimitives[id*4+v1Offset];
uv1 = output.textureUV;
viewportTransform(output.position, viewport, &v1);
output = outputPrimitives[id*4+v2Offset];
uv2 = output.textureUV;
viewportTransform(output.position, viewport, &v2);
output = outputPrimitives[id*4+v3Offset];
uv3 = output.textureUV;
viewportTransform(output.position, viewport, &v3);
// Bounding rectangle
int2 min_ = convert_int2(min(v1, min(v2, v3)));
int2 max_ = convert_int2(max(v1, max(v2, v3)));
// naive bi-linear interploation for texture coords, note this is
// broken with respect to OpenGL and needs to be fixed for the
// general case.
float p1x = v2.x - v1.x;
float p1y = v2.y - v1.y;
float p2x = v3.x - v1.x;
float p2y = v3.y - v1.y;
// Scan through bounding rectangle
for(int y = min_.y; y < max_.y; y++) {
for(int x = min_.x; x < max_.x; x++) {
// When all half-space functions positive, pixel is in triangle
if((v1.x - v2.x) * (y - v1.y) - (v1.y - v2.y) * (x - v1.x) > 0 &&
(v2.x - v3.x) * (y - v2.y) - (v2.y - v3.y) * (x - v2.x) > 0 &&
(v3.x - v1.x) * (y - v3.y) - (v3.y - v1.y) * (x - v3.x) > 0) {
float px = x - v1.x;
float py = y - v1.y;
write_imagef(
screen,
(int2)(x,y),
// texel);
(float4)(1.0f,1.0f,1.0f,1.0f));
}
}
}
}
// Optimized rasterizer function
// Details of the algorithm can be found here:
// http://www.devmaster.net/forums/showthread.php?t=1884
//
// Currently has a bug, still work in progess
__kernel void
rasterizerXX(
__global float4 * outputPrimitives,
__write_only image2d_t screen,
__global float4 * debugOut1,
__global int2 * debugOut2)
{
uint id = get_global_id(0);
// printf("ras\n");
float4 v1 = outputPrimitives[id*4+0];
float4 v2 = outputPrimitives[id*4+1];
float4 v3 = outputPrimitives[id*4+2];
float y1 = 0.5f* (v1.y+1) * (T - B) + B;
float y2 = 0.5f* (v2.y+1) * (T - B) + B;
float y3 = 0.5f* (v3.y+1) * (T - B) + B;
float x1 = 0.5f * (v1.x+1) * (R - L) + L;
float x2 = 0.5f * (v2.x+1) * (R - L) + L;
float x3 = 0.5f * (v3.x+1) * (R - L) + L;
const int Y1 = convert_int(shiftValue * y1);
const int Y2 = convert_int(shiftValue * y2);
const int Y3 = convert_int(shiftValue * y3);
const int X1 = convert_int(shiftValue * x1);
const int X2 = convert_int(shiftValue * x2);
const int X3 = convert_int(shiftValue * x3);
debugOut1[id*4+0] = v1;
debugOut1[id*4+1] = v2;
debugOut1[id*4+2] = v3;
debugOut2[id*3+0] = (int2)(X1, Y1);
debugOut2[id*3+1] = (int2)(X2, Y2);
debugOut2[id*3+2] = (int2)(X3, Y3);
// Deltas
const int DX12 = X1 - X2;
const int DX23 = X2 - X3;
const int DX31 = X3 - X1;
const int DY12 = Y1 - Y2;
const int DY23 = Y2 - Y3;
const int DY31 = Y3 - Y1;
// Fixed-point deltas
const int FDX12 = DX12 << shiftNumber;
const int FDX23 = DX23 << shiftNumber;
const int FDX31 = DX31 << shiftNumber;
const int FDY12 = DY12 << shiftNumber;
const int FDY23 = DY23 << shiftNumber;
const int FDY31 = DY31 << shiftNumber;
// Bounding rectangle
int minx = (min(X1, min(X2, X3)) + shiftMask) >> shiftNumber;
//minx = max(0,minx);
int maxx = (max(X1, min(X2, X3)) + shiftMask) >> shiftNumber;
//min(maxx , screenWidth1SubOne);
int miny = (min(Y1, min(Y2, Y3)) + shiftMask) >> shiftNumber;
//max(0,miny);
int maxy = (max(Y1, min(Y2, Y3)) + shiftMask) >> shiftNumber;
//min(maxy , screenHeight1SubOne);
//(char*&)colorBuffer += miny * stride;
int offset = miny * stride;
// Half-edge constants
int C1 = DY12 * X1 - DX12 * Y1;
int C2 = DY23 * X2 - DX23 * Y2;
int C3 = DY31 * X3 - DX31 * Y3;
// Correct for fill convention
if(DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++;
if(DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++;
if(DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++;
int CY1 = C1 + DX12 * (miny << shiftNumber) - DY12 * (minx << shiftNumber);
int CY2 = C2 + DX23 * (miny << shiftNumber) - DY23 * (minx << shiftNumber);
int CY3 = C3 + DX31 * (miny << shiftNumber) - DY31 * (minx << shiftNumber);
for(int y = miny; y < maxy; y++) {
int CX1 = CY1;
int CX2 = CY2;
int CX3 = CY3;
debugOut2[id*3+0] = (int2)(minx, maxx);
for(int x = minx; x < maxx; x++) {
debugOut2[id*3+0] = (int2)(CX1, CX2);
if(CX1 > 0 && CX2 > 0 && CX3 > 0) {
debugOut2[id*3+0] = (int2)(1, 1);
write_imagef(
screen,
(int2)(x,y),
(float4)(1.0f,1.0f,1.0f,1.0f));
}
CX1 -= FDY12;
CX2 -= FDY23;
CX3 -= FDY31;
}
CY1 += FDX12;
CY2 += FDX23;
CY3 += FDX31;
//(char*&)colorBuffer += stride;
offset += stride;
}
}
//------------------------------------------------------------------------------
void geometryShader(
__constant float modelview[16],
__constant float projection[16],
__constant float inverseView[9],
__constant int4 viewport[1],
__local struct __VSSpriteOut * vsOutputPrimitives,
__global struct __GSSpriteOut * outputPrimitives,
// __global float4 * outputPrimitives,
__write_only image2d_t screen,
__read_only image2d_t particle,
__global float4 * debugOut1,
__global int * debugOut2)
{
float2 texcoords[4] =
{
(float2)(0.0f,0.0f),
(float2)(1.0f,0.0f),
(float2)(0.0f,1.0f),
(float2)(1.0f,1.0f)
};
float matrix[16];
uint id = get_global_id(0);
uint lid = get_local_id(0);
float4 vsPosition = vsOutputPrimitives[lid].position;
matrixMul(matrix, projection, modelview);
//
// Emit two new triangles
//
for (uint i = 0; i<4; i++) {
float3 position = g_positions[i] * PARTICLE_RADIUS;
position = matrixVector3Mul(inverseView, position) + vsPosition;
float3 particlePosition =
matrixVector3Mul(
inverseView,
(float4)(0.0f,0.0f,0.0f,0.0f)) + vsPosition; // world space
// Compute view space position
position.w = 1.0f;
position = matrixVectorMul(matrix, position);
//perspective division
position /= position.w;
struct __GSSpriteOut output;
output.position = position;
//output.textureUV = g_texcoords[i];
output.textureUV = texcoords[i];
outputPrimitives[id*4+i] = output;
}
// Render QUAD - Triangle 1
rasterizerUnOpt(
outputPrimitives,
viewport,
screen,
particle,
0,
1,
2,
debugOut1);
// Render QUAD - Triangle 2
rasterizerUnOpt(
outputPrimitives,
viewport,
screen,
particle,
2,
1,
3,
debugOut1);
}
__kernel void vertexShaderSprite(
__constant float modelview[16],
__constant float projection[16],
__constant float inverseView[9],
__constant int4 viewport[1],
__local struct __VSSpriteOut * vsOutputPrimitives,
__global float4 * inputPrimitives,
__global struct __GSSpriteOut * outputPrimitives,
// __global float4 * outputPrimitives,
__write_only image2d_t screen,
__read_only image2d_t particle,
__global float4 * debugOut1,
__global int * debugOut2)
{
float matrix[16];
uint id = get_global_id(0);
uint lid = get_local_id(0);
// gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex
matrixMul(matrix, projection, modelview);
float4 position = inputPrimitives[id];
vsOutputPrimitives[lid].position = position;
vsOutputPrimitives[lid].particlePosition =
matrixVectorMul(matrix, position);
geometryShader(
modelview,
projection,
inverseView,
viewport,
vsOutputPrimitives,
outputPrimitives,
screen,
particle,
debugOut1,
debugOut2);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

View File

@@ -0,0 +1,7 @@
void main()
{
//gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_Vertex;
}

View File

@@ -7,6 +7,8 @@ ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
ADD_DEFINITIONS(-DUSE_AMD_OPENCL)
ADD_DEFINITIONS(-DCL_PLATFORM_AMD)
IF (INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
INCLUDE_DIRECTORIES( $ENV{==ATISTREAMSDKROOT=}/include )
@@ -53,15 +55,17 @@ IF (USE_GLUT)
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesSharedDefs.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesSharedTypes.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesDemo.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/shaders.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesDemoDynamicsWorld.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/main.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesDemo.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/shaders.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesOCL.cl
)
ELSE (USE_GLUT)

View File

@@ -329,7 +329,9 @@ void btParticlesDynamicsWorld::initCLKernels(int argc, char** argv)
if (!m_cxMainContext)
{
// m_cxMainContext = clCreateContextFromType(0, CL_DEVICE_TYPE_ALL, NULL, NULL, &ciErrNum);
m_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_ALL, &ciErrNum);
m_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_GPU, &ciErrNum);
//m_cxMainContext = btOclCommon::createContextFromType(CL_DEVICE_TYPE_ALL, &ciErrNum);
oclCHECKERROR(ciErrNum, CL_SUCCESS);
m_cdDevice = btOclGetMaxFlopsDev(m_cxMainContext);

View File

@@ -85,7 +85,7 @@ cl_context btOclCommon::createContextFromType(cl_device_type deviceType, cl_int*
/* Use NULL for backward compatibility */
cl_context_properties* cprops = (NULL == platform) ? NULL : cps;
cl_context retContext = clCreateContextFromType(cprops,
CL_DEVICE_TYPE_ALL,
deviceType,
NULL,
NULL,
&ciErrNum);

View File

@@ -1,3 +1,18 @@
/*
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
Copyright (C) 2006 - 2010 Sony Computer Entertainment Inc.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

View File

@@ -1,3 +1,17 @@
/*
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
Copyright (C) 2006 - 2010 Sony Computer Entertainment Inc.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BT_OCL_UTILS_H
#define BT_OCL_UTILS_H

View File

@@ -1,13 +1,4 @@
#ifndef GUID_ARG
#define GUID_ARG
#endif
#ifndef MSTRINGIFY
#define MSTRINGIFY(A) A
#endif
MSTRINGIFY(