-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path37_shader_compute.comp
More file actions
49 lines (39 loc) · 1.52 KB
/
37_shader_compute.comp
File metadata and controls
49 lines (39 loc) · 1.52 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
#version 450
struct Particle {
vec2 position;
vec2 velocity;
vec4 color;
};
layout (binding = 0) uniform ParameterUBO {
float deltaTime;
} ubo;
layout(std140, binding = 1) readonly buffer ParticleSSBOIn {
Particle particlesIn[ ];
};
layout(std140, binding = 2) buffer ParticleSSBOOut {
Particle particlesOut[ ];
};
// Push constants for particle group information
layout(push_constant) uniform PushConstants {
uint startIndex;
uint count;
} pushConstants;
layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
void main()
{
// Calculate the global particle index by adding the thread ID to the start index
uint localIndex = gl_GlobalInvocationID.x;
// Only process particles within the assigned range
if (localIndex < pushConstants.count) {
uint globalIndex = pushConstants.startIndex + localIndex;
particlesOut[globalIndex].position = particlesIn[globalIndex].position + particlesIn[globalIndex].velocity.xy * ubo.deltaTime;
particlesOut[globalIndex].velocity = particlesIn[globalIndex].velocity;
// Flip movement at window border
if ((particlesOut[globalIndex].position.x <= -1.0) || (particlesOut[globalIndex].position.x >= 1.0)) {
particlesOut[globalIndex].velocity.x = -particlesOut[globalIndex].velocity.x;
}
if ((particlesOut[globalIndex].position.y <= -1.0) || (particlesOut[globalIndex].position.y >= 1.0)) {
particlesOut[globalIndex].velocity.y = -particlesOut[globalIndex].velocity.y;
}
}
}