Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / opencl / primitives / AdlPrimitives / Sort / RadixSortSimpleKernelsCL.h
1 static const char* radixSortSimpleKernelsCL= \\r
2 "/*\n"\r
3 "Bullet Continuous Collision Detection and Physics Library\n"\r
4 "Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org\n"\r
5 "\n"\r
6 "This software is provided 'as-is', without any express or implied warranty.\n"\r
7 "In no event will the authors be held liable for any damages arising from the use of this software.\n"\r
8 "Permission is granted to anyone to use this software for any purpose, \n"\r
9 "including commercial applications, and to alter it and redistribute it freely, \n"\r
10 "subject to the following restrictions:\n"\r
11 "\n"\r
12 "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"\r
13 "2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"\r
14 "3. This notice may not be removed or altered from any source distribution.\n"\r
15 "*/\n"\r
16 "//Author Takahiro Harada\n"\r
17 "\n"\r
18 "#pragma OPENCL EXTENSION cl_amd_printf : enable\n"\r
19 "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"\r
20 "\n"\r
21 "typedef unsigned int u32;\n"\r
22 "#define GET_GROUP_IDX get_group_id(0)\n"\r
23 "#define GET_LOCAL_IDX get_local_id(0)\n"\r
24 "#define GET_GLOBAL_IDX get_global_id(0)\n"\r
25 "#define GET_GROUP_SIZE get_local_size(0)\n"\r
26 "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"\r
27 "#define AtomInc(x) atom_inc(&(x))\n"\r
28 "#define AtomInc1(x, out) out = atom_inc(&(x))\n"\r
29 "\n"\r
30 "\n"\r
31 "#define WG_SIZE 128\n"\r
32 "#define NUM_PER_WI 4\n"\r
33 "\n"\r
34 "\n"\r
35 "typedef struct\n"\r
36 "{\n"\r
37 "       u32 m_key; \n"\r
38 "       u32 m_value;\n"\r
39 "}SortData;\n"\r
40 "\n"\r
41 "\n"\r
42 "typedef struct\n"\r
43 "{\n"\r
44 "       u32 m_startBit;\n"\r
45 "       u32 m_numGroups;\n"\r
46 "       u32 m_padding[2];\n"\r
47 "} ConstBuffer;\n"\r
48 "\n"\r
49 "\n"\r
50 "__kernel\n"\r
51 "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"\r
52 "void LocalCountKernel(__global SortData* sortData, \n"\r
53 "                                               __global u32* ldsHistogramOut,\n"\r
54 "                                               ConstBuffer cb)\n"\r
55 "{\n"\r
56 "       __local u32 ldsHistogram[16][256];\n"\r
57 "\n"\r
58 "       int lIdx = GET_LOCAL_IDX;\n"\r
59 "       int gIdx = GET_GLOBAL_IDX;\n"\r
60 "       \n"\r
61 "       for(int i=0; i<16; i++)\n"\r
62 "       {\n"\r
63 "               ldsHistogram[i][lIdx] = 0.f;\n"\r
64 "               ldsHistogram[i][lIdx+128] = 0.f;\n"\r
65 "       }\n"\r
66 "       \n"\r
67 "       GROUP_LDS_BARRIER;\n"\r
68 "       \n"\r
69 "       SortData datas[NUM_PER_WI];\n"\r
70 "       datas[0] = sortData[gIdx*NUM_PER_WI+0];\n"\r
71 "       datas[1] = sortData[gIdx*NUM_PER_WI+1];\n"\r
72 "       datas[2] = sortData[gIdx*NUM_PER_WI+2];\n"\r
73 "       datas[3] = sortData[gIdx*NUM_PER_WI+3];\n"\r
74 "\n"\r
75 "       datas[0].m_key = (datas[0].m_key >> cb.m_startBit) & 0xff;\n"\r
76 "       datas[1].m_key = (datas[1].m_key >> cb.m_startBit) & 0xff;\n"\r
77 "       datas[2].m_key = (datas[2].m_key >> cb.m_startBit) & 0xff;\n"\r
78 "       datas[3].m_key = (datas[3].m_key >> cb.m_startBit) & 0xff;\n"\r
79 "\n"\r
80 "       int tableIdx = lIdx%16;\n"\r
81 "       \n"\r
82 "       AtomInc(ldsHistogram[tableIdx][datas[0].m_key]);\n"\r
83 "       AtomInc(ldsHistogram[tableIdx][datas[1].m_key]);\n"\r
84 "       AtomInc(ldsHistogram[tableIdx][datas[2].m_key]);\n"\r
85 "       AtomInc(ldsHistogram[tableIdx][datas[3].m_key]);\n"\r
86 "\n"\r
87 "       GROUP_LDS_BARRIER;\n"\r
88 "       \n"\r
89 "       u32 sum0, sum1;\n"\r
90 "       sum0 = sum1 = 0;\n"\r
91 "       for(int i=0; i<16; i++)\n"\r
92 "       {\n"\r
93 "               sum0 += ldsHistogram[i][lIdx];\n"\r
94 "               sum1 += ldsHistogram[i][lIdx+128];\n"\r
95 "       }\n"\r
96 "\n"\r
97 "       ldsHistogramOut[lIdx*cb.m_numGroups+GET_GROUP_IDX] = sum0;\n"\r
98 "       ldsHistogramOut[(lIdx+128)*cb.m_numGroups+GET_GROUP_IDX] = sum1;\n"\r
99 "}\n"\r
100 "\n"\r
101 "__kernel\n"\r
102 "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"\r
103 "void ScatterKernel(__global SortData* sortData,\n"\r
104 "                                       __global SortData* sortDataOut,\n"\r
105 "                                       __global u32* scannedHistogram, \n"\r
106 "                                       ConstBuffer cb)\n"\r
107 "{\n"\r
108 "       __local u32 ldsCurrentLocation[256];\n"\r
109 "\n"\r
110 "       int lIdx = GET_LOCAL_IDX;\n"\r
111 "       int gIdx = GET_GLOBAL_IDX;\n"\r
112 "       \n"\r
113 "       {\n"\r
114 "               ldsCurrentLocation[lIdx] = scannedHistogram[lIdx*cb.m_numGroups+GET_GROUP_IDX];\n"\r
115 "               ldsCurrentLocation[lIdx+128] = scannedHistogram[(lIdx+128)*cb.m_numGroups+GET_GROUP_IDX];\n"\r
116 "       }\n"\r
117 "\n"\r
118 "       GROUP_LDS_BARRIER;\n"\r
119 "       \n"\r
120 "       SortData datas[NUM_PER_WI];\n"\r
121 "       int keys[NUM_PER_WI];\n"\r
122 "       datas[0] = sortData[gIdx*NUM_PER_WI+0];\n"\r
123 "       datas[1] = sortData[gIdx*NUM_PER_WI+1];\n"\r
124 "       datas[2] = sortData[gIdx*NUM_PER_WI+2];\n"\r
125 "       datas[3] = sortData[gIdx*NUM_PER_WI+3];\n"\r
126 "\n"\r
127 "       keys[0] = (datas[0].m_key >> cb.m_startBit) & 0xff;\n"\r
128 "       keys[1] = (datas[1].m_key >> cb.m_startBit) & 0xff;\n"\r
129 "       keys[2] = (datas[2].m_key >> cb.m_startBit) & 0xff;\n"\r
130 "       keys[3] = (datas[3].m_key >> cb.m_startBit) & 0xff;\n"\r
131 "\n"\r
132 "       int dst[NUM_PER_WI];\n"\r
133 "       for(int i=0; i<WG_SIZE; i++)\n"\r
134 "       {\n"\r
135 "               if( i==lIdx )\n"\r
136 "               {\n"\r
137 "                       AtomInc1(ldsCurrentLocation[keys[0]], dst[0]);\n"\r
138 "                       AtomInc1(ldsCurrentLocation[keys[1]], dst[1]);\n"\r
139 "                       AtomInc1(ldsCurrentLocation[keys[2]], dst[2]);\n"\r
140 "                       AtomInc1(ldsCurrentLocation[keys[3]], dst[3]);\n"\r
141 "               }\n"\r
142 "               GROUP_LDS_BARRIER;\n"\r
143 "       }\n"\r
144 "       sortDataOut[dst[0]] = datas[0];\n"\r
145 "       sortDataOut[dst[1]] = datas[1];\n"\r
146 "       sortDataOut[dst[2]] = datas[2];\n"\r
147 "       sortDataOut[dst[3]] = datas[3];\n"\r
148 "}\n"\r
149 ;\r