Add the GPU rigid body pipeline from https://github.com/erwincoumans/experiments as a Bullet 3.x preview for Bullet 2.80
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
|
||||
hasCL = findOpenCL_AMD()
|
||||
|
||||
if (hasCL) then
|
||||
|
||||
project "OpenCL_global_atomics_AMD"
|
||||
|
||||
initOpenCL_AMD()
|
||||
|
||||
language "C++"
|
||||
|
||||
kind "ConsoleApp"
|
||||
targetdir "../../../bin"
|
||||
|
||||
-- includedirs {"..","../../../../include/gpu_research"}
|
||||
|
||||
files {
|
||||
"../main.cpp",
|
||||
"../../basic_initialize/btOpenCLUtils.cpp",
|
||||
"../../basic_initialize/btOpenCLUtils.h"
|
||||
}
|
||||
|
||||
end
|
||||
@@ -0,0 +1,36 @@
|
||||
static const char* globalAtomicsKernelString= \
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"//OpenCL 1.1 has atomic_inc build-in (no extension needed)\n"
|
||||
"//see http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/atomic_inc.html\n"
|
||||
"__kernel void globalAtomicKernelOpenCL1_1( volatile __global int* counter)\n"
|
||||
"{\n"
|
||||
" atomic_inc(counter);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//OpenCL 1.1 atomic device counters extension, usually faster on current AMD hardware\n"
|
||||
"//http://www.khronos.org/registry/cl/extensions/ext/cl_ext_atomic_counters_32.txt\n"
|
||||
"#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
|
||||
"__kernel void counterAtomicKernelExt( counter32_t counter)\n"
|
||||
"{\n"
|
||||
" atomic_inc(counter);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"//OpenCL 1.0 optional extension, using atom_inc\n"
|
||||
"//see http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_khr_global_int32_base_atomics.html\n"
|
||||
"#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable //atomic_inc\n"
|
||||
"__kernel void globalAtomicKernelExt( __global int* counter)\n"
|
||||
"{\n"
|
||||
" atom_inc(counter);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"__kernel void globalAtomicKernelCounters32Broken( __global int* counter)\n"
|
||||
"{\n"
|
||||
" (*counter)++;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
;
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
|
||||
|
||||
|
||||
//OpenCL 1.1 has atomic_inc build-in (no extension needed)
|
||||
//see http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/atomic_inc.html
|
||||
__kernel void globalAtomicKernelOpenCL1_1( volatile __global int* counter)
|
||||
{
|
||||
atomic_inc(counter);
|
||||
}
|
||||
|
||||
//OpenCL 1.1 atomic device counters extension, usually faster on current AMD hardware
|
||||
//http://www.khronos.org/registry/cl/extensions/ext/cl_ext_atomic_counters_32.txt
|
||||
#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
|
||||
__kernel void counterAtomicKernelExt( counter32_t counter)
|
||||
{
|
||||
atomic_inc(counter);
|
||||
}
|
||||
|
||||
|
||||
//OpenCL 1.0 optional extension, using atom_inc
|
||||
//see http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_khr_global_int32_base_atomics.html
|
||||
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable //atomic_inc
|
||||
__kernel void globalAtomicKernelExt( __global int* counter)
|
||||
{
|
||||
atom_inc(counter);
|
||||
}
|
||||
|
||||
|
||||
__kernel void globalAtomicKernelCounters32Broken( __global int* counter)
|
||||
{
|
||||
(*counter)++;
|
||||
}
|
||||
|
||||
201
Extras/RigidBodyGpuPipeline/opencl/global_atomics/main.cpp
Normal file
201
Extras/RigidBodyGpuPipeline/opencl/global_atomics/main.cpp
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
///original author: Erwin Coumans
|
||||
|
||||
#include "../basic_initialize/btOpenCLUtils.h"
|
||||
#include <stdio.h>
|
||||
|
||||
cl_context g_cxMainContext;
|
||||
cl_command_queue g_cqCommandQue;
|
||||
cl_kernel g_atomicsKernel;
|
||||
static const size_t workGroupSize = 128;//todo figure out an appropriate workgroup size suitable for the OpenCL platform/context/device/kernel
|
||||
#define NUM_OBJECTS 1024
|
||||
|
||||
#include "globalAtomicsKernel.h"
|
||||
|
||||
|
||||
char * findAndReplace( char const * const original, char const * const pattern, char const * const replacement);
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ciErrNum = 0;
|
||||
|
||||
printf("press a key to start\n");
|
||||
getchar();
|
||||
|
||||
const char* vendorSDK = btOpenCLUtils::getSdkVendorName();
|
||||
printf("This program was compiled using the %s OpenCL SDK\n",vendorSDK);
|
||||
|
||||
cl_device_type deviceType = CL_DEVICE_TYPE_GPU;//CL_DEVICE_TYPE_ALL
|
||||
|
||||
void* glCtx=0;
|
||||
void* glDC = 0;
|
||||
printf("Initialize OpenCL using btOpenCLUtils::createContextFromType for CL_DEVICE_TYPE_GPU\n");
|
||||
g_cxMainContext = btOpenCLUtils::createContextFromType(deviceType, &ciErrNum, glCtx, glDC);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
int numDev = btOpenCLUtils::getNumDevices(g_cxMainContext);
|
||||
|
||||
if (numDev>0)
|
||||
{
|
||||
int deviceIndex=0;
|
||||
|
||||
cl_device_id device;
|
||||
device = btOpenCLUtils::getDevice(g_cxMainContext,deviceIndex);
|
||||
btOpenCLDeviceInfo clInfo;
|
||||
btOpenCLUtils::getDeviceInfo(device,clInfo);
|
||||
btOpenCLUtils::printDeviceInfo(device);
|
||||
|
||||
|
||||
const char* globalAtomicsKernelStringPatched = globalAtomicsKernelString;
|
||||
if (!strstr(clInfo.m_deviceExtensions,"cl_ext_atomic_counters_32"))
|
||||
{
|
||||
globalAtomicsKernelStringPatched = findAndReplace(globalAtomicsKernelString,"counter32_t", "volatile __global int*");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// create a command-queue
|
||||
g_cqCommandQue = clCreateCommandQueue(g_cxMainContext, device, 0, &ciErrNum);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
cl_mem counterBuffer = clCreateBuffer(g_cxMainContext, CL_MEM_READ_WRITE, sizeof(int), NULL, &ciErrNum);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
char* kernelMethods[] =
|
||||
{
|
||||
"globalAtomicKernelOpenCL1_1",
|
||||
"counterAtomicKernelExt",
|
||||
"globalAtomicKernelExt",
|
||||
"globalAtomicKernelCounters32Broken"
|
||||
};
|
||||
int numKernelMethods = sizeof(kernelMethods)/sizeof(char*);
|
||||
|
||||
for (int i=0;i<numKernelMethods;i++)
|
||||
{
|
||||
int myCounter = 0;
|
||||
|
||||
//write to counterBuffer
|
||||
int deviceOffset=0;
|
||||
int hostOffset=0;
|
||||
|
||||
ciErrNum = clEnqueueWriteBuffer(g_cqCommandQue, counterBuffer,CL_FALSE, deviceOffset, sizeof(int), &myCounter, 0, NULL, NULL);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
g_atomicsKernel = btOpenCLUtils::compileCLKernelFromString(g_cxMainContext,device,globalAtomicsKernelStringPatched,kernelMethods[i], &ciErrNum);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
|
||||
|
||||
|
||||
ciErrNum = clSetKernelArg(g_atomicsKernel, 0, sizeof(cl_mem),(void*)&counterBuffer);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
size_t numWorkItems = workGroupSize*((NUM_OBJECTS + (workGroupSize-1)) / workGroupSize);
|
||||
ciErrNum = clEnqueueNDRangeKernel(g_cqCommandQue, g_atomicsKernel, 1, NULL, &numWorkItems, &workGroupSize,0 ,0 ,0);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
clFinish(g_cqCommandQue);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
//read from counterBuffer
|
||||
ciErrNum = clEnqueueReadBuffer(g_cqCommandQue, counterBuffer, CL_TRUE, deviceOffset, sizeof(int), &myCounter, 0, NULL, NULL);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
|
||||
if (myCounter != NUM_OBJECTS)
|
||||
{
|
||||
printf("%s is broken, expected %d got %d\n",kernelMethods[i],NUM_OBJECTS,myCounter);
|
||||
} else
|
||||
{
|
||||
printf("%s success, got %d\n",kernelMethods[i],myCounter);
|
||||
}
|
||||
}
|
||||
|
||||
clReleaseCommandQueue(g_cqCommandQue);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
}
|
||||
|
||||
clReleaseContext(g_cxMainContext);
|
||||
|
||||
printf("press a key to end\n");
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4996 )
|
||||
#endif //_WIN32
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char * findAndReplace(
|
||||
char const * const original,
|
||||
char const * const pattern,
|
||||
char const * const replacement
|
||||
) {
|
||||
size_t const replen = strlen(replacement);
|
||||
size_t const patlen = strlen(pattern);
|
||||
size_t const orilen = strlen(original);
|
||||
|
||||
size_t patcnt = 0;
|
||||
const char * oriptr;
|
||||
const char * patloc;
|
||||
|
||||
// find how many times the pattern occurs in the original string
|
||||
for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
|
||||
{
|
||||
patcnt++;
|
||||
}
|
||||
|
||||
{
|
||||
// allocate memory for the new string
|
||||
size_t const retlen = orilen + patcnt * (replen - patlen);
|
||||
char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );
|
||||
|
||||
if (returned != NULL)
|
||||
{
|
||||
// copy the original string,
|
||||
// replacing all the instances of the pattern
|
||||
char * retptr = returned;
|
||||
for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
|
||||
{
|
||||
size_t const skplen = patloc - oriptr;
|
||||
// copy the section until the occurence of the pattern
|
||||
strncpy(retptr, oriptr, skplen);
|
||||
retptr += skplen;
|
||||
// copy the replacement
|
||||
strncpy(retptr, replacement, replen);
|
||||
retptr += replen;
|
||||
}
|
||||
// copy the rest of the string.
|
||||
strcpy(retptr, oriptr);
|
||||
}
|
||||
return returned;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning( pop )
|
||||
#endif //_WIN32
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
include "AMD"
|
||||
--include "Intel"
|
||||
--include "NVIDIA"
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
|
||||
arg = sys.argv[1]
|
||||
fh = open(arg)
|
||||
|
||||
print 'static const char* '+sys.argv[2]+'= \\'
|
||||
for line in fh.readlines():
|
||||
a = line.strip('\n')
|
||||
print '"'+a+'\\n"'
|
||||
print ';'
|
||||
@@ -0,0 +1,5 @@
|
||||
stringify.py global_atomics.cl globalAtomicsKernelString >globalAtomicsKernel.h
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user