first it worked, then i broke it and finally remembered to commit

This commit is contained in:
Kasper Sauramo
2025-08-01 19:18:36 +03:00
parent 6e04bf27ff
commit e2f1c8c13a
4 changed files with 134 additions and 58 deletions

View File

@@ -4,32 +4,66 @@
// Invocations in the (x, y, z) dimension
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D current_trail;
layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D output_trail;
layout(set = 2, binding = 1) restrict buffer AgentBuffer {
vec2 positions[];
layout(r32f, binding = 0) uniform restrict readonly image2D current_trail;
layout(r32f, binding = 1) uniform restrict writeonly image2D output_trail;
layout(binding = 2) restrict buffer AgentBuffer {
vec2 positions[1024*1024];
float headings[1024*1024];
} agent_buffer;
layout(push_constant, std430) uniform Params {
vec2 texture_size;
ivec2 texture_size;
float sensor_angle;
float sensor_distance;
} params;
const float SPEED = 1.0;
void main() {
ivec2 size = ivec2(params.texture_size.x - 1, params.texture_size.y - 1);
vec2 position = agent_buffer.positions[gl_GlobalInvocationID.x];
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
uint id = gl_GlobalInvocationID.x;
vec2 position = agent_buffer.positions[id];
float heading = agent_buffer.headings[id];
ivec2 tl = ivec2(0, 0);
float current_v = imageLoad(current_trail, clamp(uv - ivec2(0, 1), tl, size)).r;
float up_v = imageLoad(current_trail, clamp(uv - ivec2(0, 1), tl, size)).r;
float down_v = imageLoad(current_trail, clamp(uv + ivec2(0, 1), tl, size)).r;
float left_v = imageLoad(current_trail, clamp(uv - ivec2(1, 0), tl, size)).r;
float right_v = imageLoad(current_trail, clamp(uv + ivec2(1, 0), tl, size)).r;
float sa = params.sensor_angle;
float left_heading = heading - sa;
float right_heading = heading + sa;
float new_v = current_v + 0.05;
vec2 left_detect_dir = vec2(cos(left_heading), sin(left_heading));
vec2 right_detect_dir = vec2(cos(right_heading), sin(right_heading));
vec2 forward_detect_dir = vec2(cos(heading), sin(heading));
float sd = params.sensor_distance;
vec2 left_detect_pos = position + left_detect_dir * sd;
vec2 right_detect_pos = position + right_detect_dir * sd;
vec2 forward_detect_pos = position + forward_detect_dir * sd;
left_detect_pos = mod(left_detect_pos, size);
right_detect_pos = mod(right_detect_pos, size);
forward_detect_pos = mod(forward_detect_pos, size);
float left = imageLoad(current_trail, ivec2(left_detect_pos)).r;
float right = imageLoad(current_trail, ivec2(right_detect_pos)).r;
float forward = imageLoad(current_trail, ivec2(forward_detect_pos)).r;
vec2 pos = position;
if (left > forward && left > right) {
pos += left_detect_dir * SPEED;
} else if (right > forward && right > left) {
pos += right_detect_dir * SPEED;
} else {
pos += forward_detect_dir * SPEED;
}
pos = mod(pos, size);
agent_buffer.positions[id] = pos;
ivec2 uv = ivec2(pos);
float current_v = imageLoad(current_trail, uv).r;
float new_v = current_v + 0.3;
vec4 result = vec4(new_v, new_v, new_v, 1.0);
imageStore(output_trail, uv, result);

View File

@@ -12,8 +12,17 @@ layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D output_ima
layout(push_constant, std430) uniform Params {
vec2 texture_size;
float decay_factor;
float dissipation_factor;
} params;
float get_dissipation_from(ivec2 uv) {
float v = imageLoad(current_image, uv).r;
return v * params.dissipation_factor;
}
void store_value(ivec2 uv, vec4 v) {
imageStore(output_image, uv, v);
}
// The code we want to execute in each invocation
void main() {
@@ -21,24 +30,31 @@ void main() {
ivec2 size = ivec2(params.texture_size.x - 1, params.texture_size.y - 1);
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
ivec2 tl = ivec2(0, 0);
ivec2 up_uv = clamp(uv - ivec2(0, 1), tl, size);
ivec2 down_uv = clamp(uv + ivec2(0, 1), tl, size);
ivec2 left_uv = clamp(uv - ivec2(1, 0), tl, size);
ivec2 right_uv = clamp(uv + ivec2(1, 0), tl, size);
// Just in case the texture size is not divisable by 8.
if ((uv.x > size.x) || (uv.y > size.y)) {
return;
}
ivec2 tl = ivec2(0, 0);
float current_v = imageLoad(current_image, uv).r;
float up_v = imageLoad(current_image, clamp(uv - ivec2(0, 1), tl, size)).r;
float down_v = imageLoad(current_image, clamp(uv + ivec2(0, 1), tl, size)).r;
float left_v = imageLoad(current_image, clamp(uv - ivec2(1, 0), tl, size)).r;
float right_v = imageLoad(current_image, clamp(uv + ivec2(1, 0), tl, size)).r;
float new_v = current_v * params.decay_factor;
new_v += get_dissipation_from(up_uv);
new_v += get_dissipation_from(down_uv);
new_v += get_dissipation_from(right_uv);
new_v += get_dissipation_from(left_uv);
new_v = clamp(new_v, 0, 1);
if (new_v < 0.0) {
new_v = 0.0;
}
vec4 result = vec4(new_v, new_v, new_v, 1.0);
imageStore(output_image, uv, result);
}