1/* 2Copyright (c) 2012 Advanced Micro Devices, Inc. 3 4This software is provided 'as-is', without any express or implied warranty. 5In no event will the authors be held liable for any damages arising from the use of this software. 6Permission is granted to anyone to use this software for any purpose, 7including commercial applications, and to alter it and redistribute it freely, 8subject to the following restrictions: 9 101. 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. 112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 123. This notice may not be removed or altered from any source distribution. 13*/ 14//Originally written by Erwin Coumans 15 16#include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h" 17 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 23 24#ifdef cl_ext_atomic_counters_32 25#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable 26#else 27#define counter32_t volatile __global int* 28#endif 29 30#define SIMD_WIDTH 64 31 32typedef unsigned int u32; 33typedef unsigned short u16; 34typedef unsigned char u8; 35 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 ) 49 50 51#define SELECT_UINT4( b, a, condition ) select( b,a,condition ) 52 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) 59 60 61#define max2 max 62#define min2 min 63 64 65#define WG_SIZE 64 66 67 68 69 70 71typedef struct 72{ 73 int m_n; 74 int m_start; 75 int m_staticIdx; 76 int m_paddings[1]; 77} ConstBuffer; 78 79typedef struct 80{ 81 int m_a; 82 int m_b; 83 u32 m_idx; 84}Elem; 85 86 87 88 89 90// batching on the GPU 91__kernel void CreateBatchesBruteForce( __global struct b3Contact4Data* gConstraints, __global const u32* gN, __global const u32* gStart, int m_staticIdx ) 92{ 93 int wgIdx = GET_GROUP_IDX; 94 int lIdx = GET_LOCAL_IDX; 95 96 const int m_n = gN[wgIdx]; 97 const int m_start = gStart[wgIdx]; 98 99 if( lIdx == 0 ) 100 { 101 for (int i=0;i<m_n;i++) 102 { 103 int srcIdx = i+m_start; 104 int batchIndex = i; 105 gConstraints[ srcIdx ].m_batchIdx = batchIndex; 106 } 107 } 108} 109 110 111#define CHECK_SIZE (WG_SIZE) 112 113 114 115 116u32 readBuf(__local u32* buff, int idx) 117{ 118 idx = idx % (32*CHECK_SIZE); 119 int bitIdx = idx%32; 120 int bufIdx = idx/32; 121 return buff[bufIdx] & (1<<bitIdx); 122} 123 124void writeBuf(__local u32* buff, int idx) 125{ 126 idx = idx % (32*CHECK_SIZE); 127 int bitIdx = idx%32; 128 int bufIdx = idx/32; 129 buff[bufIdx] |= (1<<bitIdx); 130 //atom_or( &buff[bufIdx], (1<<bitIdx) ); 131} 132 133u32 tryWrite(__local u32* buff, int idx) 134{ 135 idx = idx % (32*CHECK_SIZE); 136 int bitIdx = idx%32; 137 int bufIdx = idx/32; 138 u32 ans = (u32)atom_or( &buff[bufIdx], (1<<bitIdx) ); 139 return ((ans >> bitIdx)&1) == 0; 140} 141 142 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 ) 145{ 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; 151 152 __local u32 ldsFixedBuffer[CHECK_SIZE]; 153 154 155 156 157 158 if( lIdx == 0 ) 159 { 160 161 162 __global struct b3Contact4Data* cs = &gConstraints[m_start]; 163 164 165 int numValidConstraints = 0; 166 int batchIdx = 0; 167 168 while( numValidConstraints < numConstraints) 169 { 170 int nCurrentBatch = 0; 171 // clear flag 172 173 for(int i=0; i<CHECK_SIZE; i++) 174 ldsFixedBuffer[i] = 0; 175 176 for(int i=numValidConstraints; i<numConstraints; i++) 177 { 178 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); 187 188 if( aUnavailable==0 && bUnavailable==0 ) // ok 189 { 190 if (!aIsStatic) 191 { 192 writeBuf( ldsFixedBuffer, bodyA ); 193 } 194 if (!bIsStatic) 195 { 196 writeBuf( ldsFixedBuffer, bodyB ); 197 } 198 199 cs[i].m_batchIdx = batchIdx; 200 201 if (i!=numValidConstraints) 202 { 203 204 tmp = cs[i]; 205 cs[i] = cs[numValidConstraints]; 206 cs[numValidConstraints] = tmp; 207 208 209 } 210 211 numValidConstraints++; 212 213 nCurrentBatch++; 214 if( nCurrentBatch == SIMD_WIDTH) 215 { 216 nCurrentBatch = 0; 217 for(int i=0; i<CHECK_SIZE; i++) 218 ldsFixedBuffer[i] = 0; 219 220 } 221 } 222 }//for 223 batchIdx ++; 224 }//while 225 226 batchSizes[wgIdx] = batchIdx; 227 228 }//if( lIdx == 0 ) 229 230 //return batchIdx; 231} 232