summaryrefslogtreecommitdiff
path: root/thirdparty/bullet/Bullet3OpenCL/BroadphaseCollision/kernels/sapKernels.h
blob: 04d40fcf26ed02ce76333a724c909bd626195bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* sapCL= \
"/*\n"
"Copyright (c) 2012 Advanced Micro Devices, Inc.  \n"
"This software is provided 'as-is', without any express or implied warranty.\n"
"In no event will the authors be held liable for any damages arising from the use of this software.\n"
"Permission is granted to anyone to use this software for any purpose, \n"
"including commercial applications, and to alter it and redistribute it freely, \n"
"subject to the following restrictions:\n"
"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.\n"
"2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"
"3. This notice may not be removed or altered from any source distribution.\n"
"*/\n"
"//Originally written by Erwin Coumans\n"
"#define NEW_PAIR_MARKER -1\n"
"typedef struct \n"
"{\n"
"	union\n"
"	{\n"
"		float4	m_min;\n"
"		float   m_minElems[4];\n"
"		int			m_minIndices[4];\n"
"	};\n"
"	union\n"
"	{\n"
"		float4	m_max;\n"
"		float   m_maxElems[4];\n"
"		int			m_maxIndices[4];\n"
"	};\n"
"} btAabbCL;\n"
"/// conservative test for overlap between two aabbs\n"
"bool TestAabbAgainstAabb2(const btAabbCL* aabb1, __local const btAabbCL* aabb2);\n"
"bool TestAabbAgainstAabb2(const btAabbCL* aabb1, __local const btAabbCL* aabb2)\n"
"{\n"
"	bool overlap = true;\n"
"	overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
"	overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
"	overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
"	return overlap;\n"
"}\n"
"bool TestAabbAgainstAabb2GlobalGlobal(__global const btAabbCL* aabb1, __global const btAabbCL* aabb2);\n"
"bool TestAabbAgainstAabb2GlobalGlobal(__global const btAabbCL* aabb1, __global const btAabbCL* aabb2)\n"
"{\n"
"	bool overlap = true;\n"
"	overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
"	overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
"	overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
"	return overlap;\n"
"}\n"
"bool TestAabbAgainstAabb2Global(const btAabbCL* aabb1, __global const btAabbCL* aabb2);\n"
"bool TestAabbAgainstAabb2Global(const btAabbCL* aabb1, __global const btAabbCL* aabb2)\n"
"{\n"
"	bool overlap = true;\n"
"	overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
"	overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
"	overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
"	return overlap;\n"
"}\n"
"__kernel void   computePairsKernelTwoArrays( __global const btAabbCL* unsortedAabbs, __global const int* unsortedAabbMapping,  __global const int* unsortedAabbMapping2, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numUnsortedAabbs, int numUnSortedAabbs2, int axis, int maxPairs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numUnsortedAabbs)\n"
"		return;\n"
"	int j = get_global_id(1);\n"
"	if (j>=numUnSortedAabbs2)\n"
"		return;\n"
"	__global const btAabbCL* unsortedAabbPtr = &unsortedAabbs[unsortedAabbMapping[i]];\n"
"	__global const btAabbCL* unsortedAabbPtr2 = &unsortedAabbs[unsortedAabbMapping2[j]];\n"
"	if (TestAabbAgainstAabb2GlobalGlobal(unsortedAabbPtr,unsortedAabbPtr2))\n"
"	{\n"
"		int4 myPair;\n"
"		\n"
"		int xIndex = unsortedAabbPtr[0].m_minIndices[3];\n"
"		int yIndex = unsortedAabbPtr2[0].m_minIndices[3];\n"
"		if (xIndex>yIndex)\n"
"		{\n"
"			int tmp = xIndex;\n"
"			xIndex=yIndex;\n"
"			yIndex=tmp;\n"
"		}\n"
"		\n"
"		myPair.x = xIndex;\n"
"		myPair.y = yIndex;\n"
"		myPair.z = NEW_PAIR_MARKER;\n"
"		myPair.w = NEW_PAIR_MARKER;\n"
"		int curPair = atomic_inc (pairCount);\n"
"		if (curPair<maxPairs)\n"
"		{\n"
"				pairsOut[curPair] = myPair; //flush to main memory\n"
"		}\n"
"	}\n"
"}\n"
"__kernel void   computePairsKernelBruteForce( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numObjects)\n"
"		return;\n"
"	for (int j=i+1;j<numObjects;j++)\n"
"	{\n"
"		if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
"		{\n"
"			int4 myPair;\n"
"			myPair.x = aabbs[i].m_minIndices[3];\n"
"			myPair.y = aabbs[j].m_minIndices[3];\n"
"			myPair.z = NEW_PAIR_MARKER;\n"
"			myPair.w = NEW_PAIR_MARKER;\n"
"			int curPair = atomic_inc (pairCount);\n"
"			if (curPair<maxPairs)\n"
"			{\n"
"					pairsOut[curPair] = myPair; //flush to main memory\n"
"			}\n"
"		}\n"
"	}\n"
"}\n"
"__kernel void   computePairsKernelOriginal( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numObjects)\n"
"		return;\n"
"	for (int j=i+1;j<numObjects;j++)\n"
"	{\n"
"  	if(aabbs[i].m_maxElems[axis] < (aabbs[j].m_minElems[axis])) \n"
"		{\n"
"			break;\n"
"		}\n"
"		if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
"		{\n"
"			int4 myPair;\n"
"			myPair.x = aabbs[i].m_minIndices[3];\n"
"			myPair.y = aabbs[j].m_minIndices[3];\n"
"			myPair.z = NEW_PAIR_MARKER;\n"
"			myPair.w = NEW_PAIR_MARKER;\n"
"			int curPair = atomic_inc (pairCount);\n"
"			if (curPair<maxPairs)\n"
"			{\n"
"					pairsOut[curPair] = myPair; //flush to main memory\n"
"			}\n"
"		}\n"
"	}\n"
"}\n"
"__kernel void   computePairsKernelBarrier( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	int localId = get_local_id(0);\n"
"	__local int numActiveWgItems[1];\n"
"	__local int breakRequest[1];\n"
"	if (localId==0)\n"
"	{\n"
"		numActiveWgItems[0] = 0;\n"
"		breakRequest[0] = 0;\n"
"	}\n"
"	barrier(CLK_LOCAL_MEM_FENCE);\n"
"	atomic_inc(numActiveWgItems);\n"
"	barrier(CLK_LOCAL_MEM_FENCE);\n"
"	int localBreak = 0;\n"
"	int j=i+1;\n"
"	do\n"
"	{\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"	\n"
"		if (j<numObjects)\n"
"		{\n"
"	  	if(aabbs[i].m_maxElems[axis] < (aabbs[j].m_minElems[axis])) \n"
"			{\n"
"				if (!localBreak)\n"
"				{\n"
"					atomic_inc(breakRequest);\n"
"					localBreak = 1;\n"
"				}\n"
"			}\n"
"		}\n"
"		\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"		\n"
"		if (j>=numObjects && !localBreak)\n"
"		{\n"
"			atomic_inc(breakRequest);\n"
"			localBreak = 1;\n"
"		}\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"		\n"
"		if (!localBreak)\n"
"		{\n"
"			if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
"			{\n"
"				int4 myPair;\n"
"				myPair.x = aabbs[i].m_minIndices[3];\n"
"				myPair.y = aabbs[j].m_minIndices[3];\n"
"				myPair.z = NEW_PAIR_MARKER;\n"
"				myPair.w = NEW_PAIR_MARKER;\n"
"				int curPair = atomic_inc (pairCount);\n"
"				if (curPair<maxPairs)\n"
"				{\n"
"						pairsOut[curPair] = myPair; //flush to main memory\n"
"				}\n"
"			}\n"
"		}\n"
"		j++;\n"
"	} while (breakRequest[0]<numActiveWgItems[0]);\n"
"}\n"
"__kernel void   computePairsKernelLocalSharedMemory( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	int localId = get_local_id(0);\n"
"	__local int numActiveWgItems[1];\n"
"	__local int breakRequest[1];\n"
"	__local btAabbCL localAabbs[128];// = aabbs[i];\n"
"	\n"
"	btAabbCL myAabb;\n"
"	\n"
"	myAabb = (i<numObjects)? aabbs[i]:aabbs[0];\n"
"	float testValue = 	myAabb.m_maxElems[axis];\n"
"	\n"
"	if (localId==0)\n"
"	{\n"
"		numActiveWgItems[0] = 0;\n"
"		breakRequest[0] = 0;\n"
"	}\n"
"	int localCount=0;\n"
"	int block=0;\n"
"	localAabbs[localId] = (i+block)<numObjects? aabbs[i+block] : aabbs[0];\n"
"	localAabbs[localId+64] = (i+block+64)<numObjects? aabbs[i+block+64]: aabbs[0];\n"
"	\n"
"	barrier(CLK_LOCAL_MEM_FENCE);\n"
"	atomic_inc(numActiveWgItems);\n"
"	barrier(CLK_LOCAL_MEM_FENCE);\n"
"	int localBreak = 0;\n"
"	\n"
"	int j=i+1;\n"
"	do\n"
"	{\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"	\n"
"		if (j<numObjects)\n"
"		{\n"
"	  	if(testValue < (localAabbs[localCount+localId+1].m_minElems[axis])) \n"
"			{\n"
"				if (!localBreak)\n"
"				{\n"
"					atomic_inc(breakRequest);\n"
"					localBreak = 1;\n"
"				}\n"
"			}\n"
"		}\n"
"		\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"		\n"
"		if (j>=numObjects && !localBreak)\n"
"		{\n"
"			atomic_inc(breakRequest);\n"
"			localBreak = 1;\n"
"		}\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"		\n"
"		if (!localBreak)\n"
"		{\n"
"			if (TestAabbAgainstAabb2(&myAabb,&localAabbs[localCount+localId+1]))\n"
"			{\n"
"				int4 myPair;\n"
"				myPair.x = myAabb.m_minIndices[3];\n"
"				myPair.y = localAabbs[localCount+localId+1].m_minIndices[3];\n"
"				myPair.z = NEW_PAIR_MARKER;\n"
"				myPair.w = NEW_PAIR_MARKER;\n"
"				int curPair = atomic_inc (pairCount);\n"
"				if (curPair<maxPairs)\n"
"				{\n"
"						pairsOut[curPair] = myPair; //flush to main memory\n"
"				}\n"
"			}\n"
"		}\n"
"		\n"
"		barrier(CLK_LOCAL_MEM_FENCE);\n"
"		localCount++;\n"
"		if (localCount==64)\n"
"		{\n"
"			localCount = 0;\n"
"			block+=64;			\n"
"			localAabbs[localId] = ((i+block)<numObjects) ? aabbs[i+block] : aabbs[0];\n"
"			localAabbs[localId+64] = ((i+64+block)<numObjects) ? aabbs[i+block+64] : aabbs[0];\n"
"		}\n"
"		j++;\n"
"		\n"
"	} while (breakRequest[0]<numActiveWgItems[0]);\n"
"	\n"
"}\n"
"//http://stereopsis.com/radix.html\n"
"unsigned int FloatFlip(float fl);\n"
"unsigned int FloatFlip(float fl)\n"
"{\n"
"	unsigned int f = *(unsigned int*)&fl;\n"
"	unsigned int mask = -(int)(f >> 31) | 0x80000000;\n"
"	return f ^ mask;\n"
"}\n"
"float IFloatFlip(unsigned int f);\n"
"float IFloatFlip(unsigned int f)\n"
"{\n"
"	unsigned int mask = ((f >> 31) - 1) | 0x80000000;\n"
"	unsigned int fl = f ^ mask;\n"
"	return *(float*)&fl;\n"
"}\n"
"__kernel void   copyAabbsKernel( __global const btAabbCL* allAabbs, __global btAabbCL* destAabbs, int numObjects)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numObjects)\n"
"		return;\n"
"	int src = destAabbs[i].m_maxIndices[3];\n"
"	destAabbs[i] = allAabbs[src];\n"
"	destAabbs[i].m_maxIndices[3] = src;\n"
"}\n"
"__kernel void   flipFloatKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, __global int2* sortData, int numObjects, int axis)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numObjects)\n"
"		return;\n"
"	\n"
"	\n"
"	sortData[i].x = FloatFlip(allAabbs[smallAabbMapping[i]].m_minElems[axis]);\n"
"	sortData[i].y = i;\n"
"		\n"
"}\n"
"__kernel void   scatterKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, volatile __global const int2* sortData, __global btAabbCL* sortedAabbs, int numObjects)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numObjects)\n"
"		return;\n"
"	\n"
"	sortedAabbs[i] = allAabbs[smallAabbMapping[sortData[i].y]];\n"
"}\n"
"__kernel void   prepareSumVarianceKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, __global float4* sum, __global float4* sum2,int numAabbs)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numAabbs)\n"
"		return;\n"
"	\n"
"	btAabbCL smallAabb = allAabbs[smallAabbMapping[i]];\n"
"	\n"
"	float4 s;\n"
"	s = (smallAabb.m_max+smallAabb.m_min)*0.5f;\n"
"	sum[i]=s;\n"
"	sum2[i]=s*s;	\n"
"}\n"
;