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