Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / GpuSoftBodySolvers / OpenCL / OpenCLC10 / SolvePositionsSIMDBatched.cl
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/\r
4 \r
5 This software is provided 'as-is', without any express or implied warranty.\r
6 In no event will the authors be held liable for any damages arising from the use of this software.\r
7 Permission is granted to anyone to use this software for any purpose, \r
8 including commercial applications, and to alter it and redistribute it freely, \r
9 subject to the following restrictions:\r
10 \r
11 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.\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
13 3. This notice may not be removed or altered from any source distribution.\r
14 */\r
15 \r
16 MSTRINGIFY(\r
17 \r
18 float mydot3(float4 a, float4 b)\r
19 {\r
20    return a.x*b.x + a.y*b.y + a.z*b.z;\r
21 }\r
22 \r
23 __kernel __attribute__((reqd_work_group_size(WAVEFRONT_BLOCK_MULTIPLIER*WAVEFRONT_SIZE, 1, 1)))\r
24 void \r
25 SolvePositionsFromLinksKernel( \r
26         const int startWaveInBatch,\r
27         const int numWaves,\r
28         const float kst,\r
29         const float ti,\r
30         __global int2 *g_wavefrontBatchCountsVertexCounts,\r
31         __global int *g_vertexAddressesPerWavefront,\r
32         __global int2 * g_linksVertexIndices,\r
33         __global float * g_linksMassLSC,\r
34         __global float * g_linksRestLengthSquared,\r
35         __global float * g_verticesInverseMass,\r
36         __global float4 * g_vertexPositions,\r
37         __local int2 *wavefrontBatchCountsVertexCounts,\r
38         __local float4 *vertexPositionSharedData,\r
39         __local float *vertexInverseMassSharedData)\r
40 {\r
41         const int laneInWavefront = (get_global_id(0) & (WAVEFRONT_SIZE-1));\r
42         const int wavefront = startWaveInBatch + (get_global_id(0) / WAVEFRONT_SIZE);\r
43         const int firstWavefrontInBlock = startWaveInBatch + get_group_id(0) * WAVEFRONT_BLOCK_MULTIPLIER;\r
44         const int localWavefront = wavefront - firstWavefrontInBlock;\r
45 \r
46         // Mask out in case there's a stray "wavefront" at the end that's been forced in through the multiplier \r
47         if( wavefront < (startWaveInBatch + numWaves) )\r
48         {       \r
49                 // Load the batch counts for the wavefronts\r
50                 \r
51                 int2 batchesAndVerticesWithinWavefront = g_wavefrontBatchCountsVertexCounts[wavefront];\r
52                 int batchesWithinWavefront = batchesAndVerticesWithinWavefront.x;\r
53                 int verticesUsedByWave = batchesAndVerticesWithinWavefront.y;\r
54 \r
55                 // Load the vertices for the wavefronts\r
56                 for( int vertex = laneInWavefront; vertex < verticesUsedByWave; vertex+=WAVEFRONT_SIZE )\r
57                 {\r
58                         int vertexAddress = g_vertexAddressesPerWavefront[wavefront*MAX_NUM_VERTICES_PER_WAVE + vertex];\r
59 \r
60                         vertexPositionSharedData[localWavefront*MAX_NUM_VERTICES_PER_WAVE + vertex] = g_vertexPositions[vertexAddress];\r
61                         vertexInverseMassSharedData[localWavefront*MAX_NUM_VERTICES_PER_WAVE + vertex] = g_verticesInverseMass[vertexAddress];\r
62                 }\r
63                 \r
64                 barrier(CLK_LOCAL_MEM_FENCE);\r
65 \r
66                 // Loop through the batches performing the solve on each in LDS\r
67                 int baseDataLocationForWave = WAVEFRONT_SIZE * wavefront * MAX_BATCHES_PER_WAVE;        \r
68 \r
69                 //for( int batch = 0; batch < batchesWithinWavefront; ++batch )\r
70                 \r
71                 int batch = 0;\r
72                 do\r
73                 {\r
74                         int baseDataLocation = baseDataLocationForWave + WAVEFRONT_SIZE * batch;\r
75                         int locationOfValue = baseDataLocation + laneInWavefront;\r
76                         \r
77                         \r
78                         // These loads should all be perfectly linear across the WF\r
79                         int2 localVertexIndices = g_linksVertexIndices[locationOfValue];\r
80                         float massLSC = g_linksMassLSC[locationOfValue];\r
81                         float restLengthSquared = g_linksRestLengthSquared[locationOfValue];\r
82                         \r
83                         // LDS vertex addresses based on logical wavefront number in block and loaded index\r
84                         int vertexAddress0 = MAX_NUM_VERTICES_PER_WAVE * localWavefront + localVertexIndices.x;\r
85                         int vertexAddress1 = MAX_NUM_VERTICES_PER_WAVE * localWavefront + localVertexIndices.y;\r
86                         \r
87                         float4 position0 = vertexPositionSharedData[vertexAddress0];\r
88                         float4 position1 = vertexPositionSharedData[vertexAddress1];\r
89 \r
90                         float inverseMass0 = vertexInverseMassSharedData[vertexAddress0];\r
91                         float inverseMass1 = vertexInverseMassSharedData[vertexAddress1]; \r
92 \r
93                         float4 del = position1 - position0;\r
94                         float len = mydot3(del, del);\r
95                         \r
96                         float k = 0;\r
97                         if( massLSC > 0.0f )\r
98                         {               \r
99                                 k = ((restLengthSquared - len)/(massLSC*(restLengthSquared+len)))*kst;\r
100                         }\r
101                         \r
102                         position0 = position0 - del*(k*inverseMass0);\r
103                         position1 = position1 + del*(k*inverseMass1);\r
104                         \r
105                         // Ensure compiler does not re-order memory operations\r
106                         barrier(CLK_LOCAL_MEM_FENCE);\r
107 \r
108                         vertexPositionSharedData[vertexAddress0] = position0;\r
109                         vertexPositionSharedData[vertexAddress1] = position1;\r
110                         \r
111                         // Ensure compiler does not re-order memory operations\r
112                         barrier(CLK_LOCAL_MEM_FENCE);\r
113                                 \r
114                         \r
115                         ++batch;\r
116                 } while( batch < batchesWithinWavefront );\r
117 \r
118                 // Update the global memory vertices for the wavefronts\r
119                 for( int vertex = laneInWavefront; vertex < verticesUsedByWave; vertex+=WAVEFRONT_SIZE )\r
120                 {\r
121                         int vertexAddress = g_vertexAddressesPerWavefront[wavefront*MAX_NUM_VERTICES_PER_WAVE + vertex];\r
122 \r
123                         g_vertexPositions[vertexAddress] = (float4)(vertexPositionSharedData[localWavefront*MAX_NUM_VERTICES_PER_WAVE + vertex].xyz, 0.f);\r
124                 }               \r
125                 \r
126         }\r
127 \r
128 }\r
129 \r
130 );\r