OSDN Git Service

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