fallingCand/shaders/sand_step.comp
2025-11-29 13:59:31 -08:00

43 lines
974 B
Plaintext

#version 430 core
layout(local_size_x = 16, local_size_y = 16) in;
layout(r8ui, binding = 0) readonly uniform uimage2D state_curr;
layout(r8ui, binding = 1) writeonly uniform uimage2D state_next;
uniform ivec2 u_gridSize;
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
if (pos.x >= u_gridSize.x || pos.y >= u_gridSize.y) {
return;
}
const uint AIR = 0u;
const uint SAND = 1u;
uint self = imageLoad(state_curr, pos).r;
bool canFallDown =
(self == SAND) &&
(pos.y + 1 < u_gridSize.y) &&
(imageLoad(state_curr, pos + ivec2(0, 1)).r == AIR);
bool filledFromAbove =
(self == AIR) &&
(pos.y > 0) &&
(imageLoad(state_curr, pos + ivec2(0, -1)).r == SAND);
uint next;
if (canFallDown) {
next = AIR;
} else if (filledFromAbove) {
next = SAND;
} else {
next = self;
}
imageStore(state_next, pos, uvec4(next, 0u, 0u, 0u));
}