yum-mirror/glslang
git clone https://git.yummers.dev/yum-mirror/glslang
284ceb6d
16.5.0
1/* 2The MIT License (MIT) 3 4Copyright (c) 2022 Sascha Willems 5 6Permission is hereby granted, free of charge, to any person obtaining a copy 7of this software and associated documentation files (the "Software"), to deal 8in the Software without restriction, including without limitation the rights 9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10copies of the Software, and to permit persons to whom the Software is 11furnished to do so, subject to the following conditions: 12 13The above copyright notice and this permission notice shall be included in all 14copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22SOFTWARE. 23*/ 24 25#version 450 26 27struct Particle { 28vec4 pos ; 29vec4 vel ; 30vec4 uv ; 31vec4 normal ; 32float pinned ; 33} ; 34 35layout ( std430 , binding = 0 ) buffer ParticleIn { 36Particle particleIn [ ] ; 37} ; 38 39layout ( std430 , binding = 1 ) buffer ParticleOut { 40Particle particleOut [ ] ; 41} ; 42 43// todo: use shared memory to speed up calculation 44 45layout ( local_size_x = 10 , local_size_y = 10 ) in ; 46 47layout ( binding = 2 ) uniform UBO 48{ 49float deltaT ; 50float particleMass ; 51float springStiffness ; 52float damping ; 53float restDistH ; 54float restDistV ; 55float restDistD ; 56float sphereRadius ; 57vec4 spherePos ; 58vec4 gravity ; 59ivec2 particleCount ; 60} params ; 61 62layout ( push_constant ) uniform PushConsts { 63uint calculateNormals ; 64} pushConsts ; 65 66vec3 springForce ( vec3 p0 , vec3 p1 , float restDist ) 67{ 68vec3 dist = p0 - p1 ; 69return normalize ( dist ) * params . springStiffness * ( length ( dist ) - restDist ) ; 70} 71 72void main () 73{ 74uvec3 id = gl_GlobalInvocationID ; 75 76uint index = id . y * params . particleCount . x + id . x ; 77if ( index > params . particleCount . x * params . particleCount . y ) 78return ; 79 80// Pinned? 81if ( particleIn [ index ] . pinned == 1.0 ) { 82particleOut [ index ] . pos = particleOut [ index ] . pos ; 83particleOut [ index ] . vel = vec4 ( 0.0 ) ; 84return ; 85} 86 87// Initial force from gravity 88vec3 force = params . gravity . xyz * params . particleMass ; 89 90vec3 pos = particleIn [ index ] . pos . xyz ; 91vec3 vel = particleIn [ index ] . vel . xyz ; 92 93// Spring forces from neighboring particles 94// left 95if ( id . x > 0 ) { 96force += springForce ( particleIn [ index - 1 ] . pos . xyz , pos , params . restDistH ) ; 97} 98// right 99if ( id . x < params . particleCount . x - 1 ) { 100force += springForce ( particleIn [ index + 1 ] . pos . xyz , pos , params . restDistH ) ; 101} 102// upper 103if ( id . y < params . particleCount . y - 1 ) { 104force += springForce ( particleIn [ index + params . particleCount . x ] . pos . xyz , pos , params . restDistV ) ; 105} 106// lower 107if ( id . y > 0 ) { 108force += springForce ( particleIn [ index - params . particleCount . x ] . pos . xyz , pos , params . restDistV ) ; 109} 110// upper-left 111if (( id . x > 0 ) && ( id . y < params . particleCount . y - 1 )) { 112force += springForce ( particleIn [ index + params . particleCount . x - 1 ] . pos . xyz , pos , params . restDistD ) ; 113} 114// lower-left 115if (( id . x > 0 ) && ( id . y > 0 )) { 116force += springForce ( particleIn [ index - params . particleCount . x - 1 ] . pos . xyz , pos , params . restDistD ) ; 117} 118// upper-right 119if (( id . x < params . particleCount . x - 1 ) && ( id . y < params . particleCount . y - 1 )) { 120force += springForce ( particleIn [ index + params . particleCount . x + 1 ] . pos . xyz , pos , params . restDistD ) ; 121} 122// lower-right 123if (( id . x < params . particleCount . x - 1 ) && ( id . y > 0 )) { 124force += springForce ( particleIn [ index - params . particleCount . x + 1 ] . pos . xyz , pos , params . restDistD ) ; 125} 126 127force += ( - params . damping * vel ) ; 128 129// Integrate 130vec3 f = force * ( 1.0 / params . particleMass ) ; 131particleOut [ index ] . pos = vec4 ( pos + vel * params . deltaT + 0.5 * f * params . deltaT * params . deltaT , 1.0 ) ; 132particleOut [ index ] . vel = vec4 ( vel + f * params . deltaT , 0.0 ) ; 133 134// Sphere collision 135vec3 sphereDist = particleOut [ index ] . pos . xyz - params . spherePos . xyz ; 136if ( length ( sphereDist ) < params . sphereRadius + 0.01 ) { 137// If the particle is inside the sphere, push it to the outer radius 138particleOut [ index ] . pos . xyz = params . spherePos . xyz + normalize ( sphereDist ) * ( params . sphereRadius + 0.01 ) ; 139// Cancel out velocity 140particleOut [ index ] . vel = vec4 ( 0.0 ) ; 141} 142 143// Normals 144if ( pushConsts . calculateNormals == 1 ) { 145vec3 normal = vec3 ( 0.0 ) ; 146vec3 a , b , c ; 147if ( id . y > 0 ) { 148if ( id . x > 0 ) { 149a = particleIn [ index - 1 ] . pos . xyz - pos ; 150b = particleIn [ index - params . particleCount . x - 1 ] . pos . xyz - pos ; 151c = particleIn [ index - params . particleCount . x ] . pos . xyz - pos ; 152normal += cross ( a , b ) + cross ( b , c ) ; 153} 154if ( id . x < params . particleCount . x - 1 ) { 155a = particleIn [ index - params . particleCount . x ] . pos . xyz - pos ; 156b = particleIn [ index - params . particleCount . x + 1 ] . pos . xyz - pos ; 157c = particleIn [ index + 1 ] . pos . xyz - pos ; 158normal += cross ( a , b ) + cross ( b , c ) ; 159} 160} 161if ( id . y < params . particleCount . y - 1 ) { 162if ( id . x > 0 ) { 163a = particleIn [ index + params . particleCount . x ] . pos . xyz - pos ; 164b = particleIn [ index + params . particleCount . x - 1 ] . pos . xyz - pos ; 165c = particleIn [ index - 1 ] . pos . xyz - pos ; 166normal += cross ( a , b ) + cross ( b , c ) ; 167} 168if ( id . x < params . particleCount . x - 1 ) { 169a = particleIn [ index + 1 ] . pos . xyz - pos ; 170b = particleIn [ index + params . particleCount . x + 1 ] . pos . xyz - pos ; 171c = particleIn [ index + params . particleCount . x ] . pos . xyz - pos ; 172normal += cross ( a , b ) + cross ( b , c ) ; 173} 174} 175particleOut [ index ] . normal = vec4 ( normalize ( normal ) , 0.0f ) ; 176} 177}