1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* gridBroadphaseCL =
3 "int getPosHash(int4 gridPos, __global float4* pParams)\n"
5 " int4 gridDim = *((__global int4*)(pParams + 1));\n"
6 " gridPos.x &= gridDim.x - 1;\n"
7 " gridPos.y &= gridDim.y - 1;\n"
8 " gridPos.z &= gridDim.z - 1;\n"
9 " int hash = gridPos.z * gridDim.y * gridDim.x + gridPos.y * gridDim.x + gridPos.x;\n"
12 "int4 getGridPos(float4 worldPos, __global float4* pParams)\n"
15 " int4 gridDim = *((__global int4*)(pParams + 1));\n"
16 " gridPos.x = (int)floor(worldPos.x * pParams[0].x) & (gridDim.x - 1);\n"
17 " gridPos.y = (int)floor(worldPos.y * pParams[0].y) & (gridDim.y - 1);\n"
18 " gridPos.z = (int)floor(worldPos.z * pParams[0].z) & (gridDim.z - 1);\n"
21 "// calculate grid hash value for each body using its AABB\n"
22 "__kernel void kCalcHashAABB(int numObjects, __global float4* allpAABB, __global const int* smallAabbMapping, __global int2* pHash, __global float4* pParams )\n"
24 " int index = get_global_id(0);\n"
25 " if(index >= numObjects)\n"
29 " float4 bbMin = allpAABB[smallAabbMapping[index]*2];\n"
30 " float4 bbMax = allpAABB[smallAabbMapping[index]*2 + 1];\n"
32 " pos.x = (bbMin.x + bbMax.x) * 0.5f;\n"
33 " pos.y = (bbMin.y + bbMax.y) * 0.5f;\n"
34 " pos.z = (bbMin.z + bbMax.z) * 0.5f;\n"
36 " // get address in grid\n"
37 " int4 gridPos = getGridPos(pos, pParams);\n"
38 " int gridHash = getPosHash(gridPos, pParams);\n"
39 " // store grid hash and body index\n"
41 " hashVal.x = gridHash;\n"
42 " hashVal.y = index;\n"
43 " pHash[index] = hashVal;\n"
45 "__kernel void kClearCellStart( int numCells, \n"
46 " __global int* pCellStart )\n"
48 " int index = get_global_id(0);\n"
49 " if(index >= numCells)\n"
53 " pCellStart[index] = -1;\n"
55 "__kernel void kFindCellStart(int numObjects, __global int2* pHash, __global int* cellStart )\n"
57 " __local int sharedHash[513];\n"
58 " int index = get_global_id(0);\n"
60 " if(index < numObjects)\n"
62 " sortedData = pHash[index];\n"
63 " // Load hash data into shared memory so that we can look \n"
64 " // at neighboring body's hash value without loading\n"
65 " // two hash values per thread\n"
66 " sharedHash[get_local_id(0) + 1] = sortedData.x;\n"
67 " if((index > 0) && (get_local_id(0) == 0))\n"
69 " // first thread in block must load neighbor body hash\n"
70 " sharedHash[0] = pHash[index-1].x;\n"
73 " barrier(CLK_LOCAL_MEM_FENCE);\n"
74 " if(index < numObjects)\n"
76 " if((index == 0) || (sortedData.x != sharedHash[get_local_id(0)]))\n"
78 " cellStart[sortedData.x] = index;\n"
82 "int testAABBOverlap(float4 min0, float4 max0, float4 min1, float4 max1)\n"
84 " return (min0.x <= max1.x)&& (min1.x <= max0.x) && \n"
85 " (min0.y <= max1.y)&& (min1.y <= max0.y) && \n"
86 " (min0.z <= max1.z)&& (min1.z <= max0.z); \n"
88 "//search for AABB 'index' against other AABBs' in this cell\n"
89 "void findPairsInCell( int numObjects,\n"
92 " __global int2* pHash,\n"
93 " __global int* pCellStart,\n"
94 " __global float4* allpAABB, \n"
95 " __global const int* smallAabbMapping,\n"
96 " __global float4* pParams,\n"
97 " volatile __global int* pairCount,\n"
98 " __global int4* pPairBuff2,\n"
102 " int4 pGridDim = *((__global int4*)(pParams + 1));\n"
103 " int maxBodiesPerCell = pGridDim.w;\n"
104 " int gridHash = getPosHash(gridPos, pParams);\n"
105 " // get start of bucket for this cell\n"
106 " int bucketStart = pCellStart[gridHash];\n"
107 " if (bucketStart == -1)\n"
109 " return; // cell empty\n"
111 " // iterate over bodies in this cell\n"
112 " int2 sortedData = pHash[index];\n"
113 " int unsorted_indx = sortedData.y;\n"
114 " float4 min0 = allpAABB[smallAabbMapping[unsorted_indx]*2 + 0]; \n"
115 " float4 max0 = allpAABB[smallAabbMapping[unsorted_indx]*2 + 1];\n"
116 " int handleIndex = as_int(min0.w);\n"
118 " int bucketEnd = bucketStart + maxBodiesPerCell;\n"
119 " bucketEnd = (bucketEnd > numObjects) ? numObjects : bucketEnd;\n"
120 " for(int index2 = bucketStart; index2 < bucketEnd; index2++) \n"
122 " int2 cellData = pHash[index2];\n"
123 " if (cellData.x != gridHash)\n"
125 " break; // no longer in same bucket\n"
127 " int unsorted_indx2 = cellData.y;\n"
128 " //if (unsorted_indx2 < unsorted_indx) // check not colliding with self\n"
129 " if (unsorted_indx2 != unsorted_indx) // check not colliding with self\n"
131 " float4 min1 = allpAABB[smallAabbMapping[unsorted_indx2]*2 + 0];\n"
132 " float4 max1 = allpAABB[smallAabbMapping[unsorted_indx2]*2 + 1];\n"
133 " if(testAABBOverlap(min0, max0, min1, max1))\n"
137 " int handleIndex2 = as_int(min1.w);\n"
138 " if (handleIndex<handleIndex2)\n"
140 " int curPair = atomic_add(pairCount,1);\n"
141 " if (curPair<maxPairs)\n"
144 " newpair.x = handleIndex;\n"
145 " newpair.y = handleIndex2;\n"
148 " pPairBuff2[curPair] = newpair;\n"
157 "__kernel void kFindOverlappingPairs( int numObjects,\n"
158 " __global float4* allpAABB, \n"
159 " __global const int* smallAabbMapping,\n"
160 " __global int2* pHash, \n"
161 " __global int* pCellStart, \n"
162 " __global float4* pParams ,\n"
163 " volatile __global int* pairCount,\n"
164 " __global int4* pPairBuff2,\n"
168 " int index = get_global_id(0);\n"
169 " if(index >= numObjects)\n"
173 " int2 sortedData = pHash[index];\n"
174 " int unsorted_indx = sortedData.y;\n"
175 " float4 bbMin = allpAABB[smallAabbMapping[unsorted_indx]*2 + 0];\n"
176 " float4 bbMax = allpAABB[smallAabbMapping[unsorted_indx]*2 + 1];\n"
178 " pos.x = (bbMin.x + bbMax.x) * 0.5f;\n"
179 " pos.y = (bbMin.y + bbMax.y) * 0.5f;\n"
180 " pos.z = (bbMin.z + bbMax.z) * 0.5f;\n"
181 " // get address in grid\n"
182 " int4 gridPosA = getGridPos(pos, pParams);\n"
184 " // examine only neighbouring cells\n"
185 " for(int z=-1; z<=1; z++) \n"
187 " gridPosB.z = gridPosA.z + z;\n"
188 " for(int y=-1; y<=1; y++) \n"
190 " gridPosB.y = gridPosA.y + y;\n"
191 " for(int x=-1; x<=1; x++) \n"
193 " gridPosB.x = gridPosA.x + x;\n"
194 " findPairsInCell(numObjects, gridPosB, index, pHash, pCellStart, allpAABB,smallAabbMapping, pParams, pairCount,pPairBuff2, maxPairs);\n"