forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.CSForces.comp
More file actions
60 lines (48 loc) · 1.18 KB
/
Example.CSForces.comp
File metadata and controls
60 lines (48 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// GLSL Compute Shader "CSForces"
// Generated by XShaderCompiler
// 11/10/2019 20:18:28
#version 430
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(std140, row_major) uniform SceneState
{
mat4 wvpMatrix;
mat4 wMatrix;
vec4 gravity;
uvec2 gridSize;
uvec2 _pad0;
float damping;
float dTime;
float dStiffness;
float _pad1;
vec4 lightVec;
};
// Particle buffers
layout(std430) readonly buffer parBase
{
vec4 g_parBase[];
};
layout(std430) buffer parCurrPos
{
vec4 g_parCurrPos[];
};
layout(std430) buffer parVelocity
{
vec4 g_parVelocity[];
};
// Returns the particle index for the specified grid
uint GridPosToIndex(uvec2 gridPos)
{
return (gridPos.y * gridSize.x + gridPos.x);
}
void main()
{
uint idx = GridPosToIndex(gl_GlobalInvocationID.xy);
// Accumulate force and multiply by inverse mass
float invMass = g_parBase[idx].z;
vec4 force = gravity;
force *= vec4(invMass);
// Apply velocity and damping
g_parVelocity[idx] += force * dTime * damping;
// Apply position based physics simulation
g_parCurrPos[idx] += vec4(g_parVelocity[idx].xyz, 0.0) * dTime;
}