2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Erwin Coumans
16 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
18 #pragma OPENCL EXTENSION cl_amd_printf : enable
19 #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
20 #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
21 #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
22 #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
24 #ifdef cl_ext_atomic_counters_32
25 #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
27 #define counter32_t volatile __global int*
32 typedef unsigned int u32;
33 typedef unsigned short u16;
34 typedef unsigned char u8;
36 #define GET_GROUP_IDX get_group_id(0)
37 #define GET_LOCAL_IDX get_local_id(0)
38 #define GET_GLOBAL_IDX get_global_id(0)
39 #define GET_GROUP_SIZE get_local_size(0)
40 #define GET_NUM_GROUPS get_num_groups(0)
41 #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
42 #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
43 #define AtomInc(x) atom_inc(&(x))
44 #define AtomInc1(x, out) out = atom_inc(&(x))
45 #define AppendInc(x, out) out = atomic_inc(x)
46 #define AtomAdd(x, value) atom_add(&(x), value)
47 #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
48 #define AtomXhg(x, value) atom_xchg ( &(x), value )
51 #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
53 #define make_float4 (float4)
54 #define make_float2 (float2)
55 #define make_uint4 (uint4)
56 #define make_int4 (int4)
57 #define make_uint2 (uint2)
58 #define make_int2 (int2)
90 // batching on the GPU
91 __kernel void CreateBatchesBruteForce( __global struct b3Contact4Data* gConstraints, __global const u32* gN, __global const u32* gStart, int m_staticIdx )
93 int wgIdx = GET_GROUP_IDX;
94 int lIdx = GET_LOCAL_IDX;
96 const int m_n = gN[wgIdx];
97 const int m_start = gStart[wgIdx];
101 for (int i=0;i<m_n;i++)
103 int srcIdx = i+m_start;
105 gConstraints[ srcIdx ].m_batchIdx = batchIndex;
111 #define CHECK_SIZE (WG_SIZE)
116 u32 readBuf(__local u32* buff, int idx)
118 idx = idx % (32*CHECK_SIZE);
121 return buff[bufIdx] & (1<<bitIdx);
124 void writeBuf(__local u32* buff, int idx)
126 idx = idx % (32*CHECK_SIZE);
129 buff[bufIdx] |= (1<<bitIdx);
130 //atom_or( &buff[bufIdx], (1<<bitIdx) );
133 u32 tryWrite(__local u32* buff, int idx)
135 idx = idx % (32*CHECK_SIZE);
138 u32 ans = (u32)atom_or( &buff[bufIdx], (1<<bitIdx) );
139 return ((ans >> bitIdx)&1) == 0;
143 // batching on the GPU
144 __kernel void CreateBatchesNew( __global struct b3Contact4Data* gConstraints, __global const u32* gN, __global const u32* gStart, __global int* batchSizes, int staticIdx )
146 int wgIdx = GET_GROUP_IDX;
147 int lIdx = GET_LOCAL_IDX;
148 const int numConstraints = gN[wgIdx];
149 const int m_start = gStart[wgIdx];
150 b3Contact4Data_t tmp;
152 __local u32 ldsFixedBuffer[CHECK_SIZE];
162 __global struct b3Contact4Data* cs = &gConstraints[m_start];
165 int numValidConstraints = 0;
168 while( numValidConstraints < numConstraints)
170 int nCurrentBatch = 0;
173 for(int i=0; i<CHECK_SIZE; i++)
174 ldsFixedBuffer[i] = 0;
176 for(int i=numValidConstraints; i<numConstraints; i++)
179 int bodyAS = cs[i].m_bodyAPtrAndSignBit;
180 int bodyBS = cs[i].m_bodyBPtrAndSignBit;
181 int bodyA = abs(bodyAS);
182 int bodyB = abs(bodyBS);
183 bool aIsStatic = (bodyAS<0) || bodyAS==staticIdx;
184 bool bIsStatic = (bodyBS<0) || bodyBS==staticIdx;
185 int aUnavailable = aIsStatic ? 0 : readBuf( ldsFixedBuffer, bodyA);
186 int bUnavailable = bIsStatic ? 0 : readBuf( ldsFixedBuffer, bodyB);
188 if( aUnavailable==0 && bUnavailable==0 ) // ok
192 writeBuf( ldsFixedBuffer, bodyA );
196 writeBuf( ldsFixedBuffer, bodyB );
199 cs[i].m_batchIdx = batchIdx;
201 if (i!=numValidConstraints)
205 cs[i] = cs[numValidConstraints];
206 cs[numValidConstraints] = tmp;
211 numValidConstraints++;
214 if( nCurrentBatch == SIMD_WIDTH)
217 for(int i=0; i<CHECK_SIZE; i++)
218 ldsFixedBuffer[i] = 0;
226 batchSizes[wgIdx] = batchIdx;