Charles Croz 1 rok pred
rodič
commit
c4b8b9f579

+ 6 - 6
CppGPU/configs/config3.cfg

@@ -1,8 +1,8 @@
 increase=10.0f
-decay=0.03f
-diffusion=10.0f
+decay=2.3f
+diffusion=4.0f
 
-speed=20.0f
-steering=240.0f
-sensing_distance=3.0f
-sensing_angle=45.0f
+speed=100.0f
+steering=150.0f
+sensing_distance=5.0f
+sensing_angle=30.0f

+ 4 - 4
CppGPU/shaders/draw0.glsl

@@ -13,18 +13,18 @@ struct cell_t {
 
 layout(binding = 0, rgba8) writeonly uniform image2D canvas;
 
-uniform int map_width;
-uniform int map_height;
+uniform uint map_width;
+uniform uint map_height;
 
 uniform float display_population_value;
 uniform float display_pheromone_value;
 
-layout(std430, binding = 1) coherent buffer particles_buffer
+layout(std430, binding = 1) readonly buffer particles_buffer
 {
     particle_t particles[];
 };
 
-layout(std430, binding = 2) coherent buffer map_buffer
+layout(std430, binding = 2) readonly buffer map_buffer
 {
     cell_t map[];
 };

+ 5 - 5
CppGPU/shaders/draw1.glsl

@@ -13,8 +13,8 @@ struct cell_t {
 
 layout(binding = 0, rgba8) writeonly uniform image2D canvas;
 
-uniform int map_width;
-uniform int map_height;
+uniform uint map_width;
+uniform uint map_height;
 
 uniform float display_population_value;
 uniform float display_pheromone_value;
@@ -43,8 +43,8 @@ void main(){
     float phe_value = cell.pheromone / display_pheromone_value;
 
     imageStore(canvas, ivec2(gl_GlobalInvocationID.xy), 
-        vec4(phe_value / 3,
-             phe_value / 5,
-             pop_value / 2,
+        vec4(phe_value/2,
+             phe_value/5,
+             pop_value,
              1));
 }

+ 3 - 3
CppGPU/shaders/draw2.glsl

@@ -43,8 +43,8 @@ void main(){
     float phe_value = cell.pheromone / display_pheromone_value;
 
     imageStore(canvas, ivec2(gl_GlobalInvocationID.xy), 
-        vec4(sin(phe_value * phe_value / 2000),
-             sin(phe_value * phe_value / 2000),
-             pop_value / 2,
+        vec4(phe_value * pop_value,
+             pop_value,
+             phe_value,
              1));
 }

+ 16 - 8
CppGPU/shaders/map_reset.glsl

@@ -6,21 +6,29 @@ struct cell_t {
     int population;
 };
 
-uniform int map_width;
-uniform int map_height;
+uniform uint map_width;
+uniform uint map_height;
 
 layout(std430, binding = 2) coherent buffer map_buffer
 {
     cell_t map[];
 };
 
-layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
+uint cell_id(uint x, uint y){
+    return uint(mod(y, map_height) * map_width + mod(x, map_width));
+}
+
+layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
 void main(){
-    uint cell_id = gl_GlobalInvocationID.x;
-    if(cell_id >= uint(map_width * map_height)){
+    uint x = gl_GlobalInvocationID.x;
+    uint y = gl_GlobalInvocationID.y;
+
+    if(x >= map_width || y >= map_height){
         return;
     }
-    map[cell_id].pheromone = map[cell_id].result;
-    map[cell_id].result = 0;
-    map[cell_id].population = 0;
+
+    uint id = cell_id(x, y); 
+    map[id].pheromone = map[id].result;
+    map[id].result = 0;
+    map[id].population = 0;
 }

+ 6 - 6
CppGPU/shaders/map_update.glsl

@@ -6,8 +6,8 @@ struct cell_t {
     int population;
 };
 
-uniform int map_width;
-uniform int map_height;
+uniform uint map_width;
+uniform uint map_height;
 
 uniform float increase;
 uniform float decay;
@@ -20,14 +20,14 @@ layout(std430, binding = 2) coherent buffer map_buffer
     cell_t map[];
 };
 
-uint cell_id(uint x, uint y){
-    return uint(mod(y + map_height, map_height) * map_width + mod(x + map_width, map_width));
+uint cell_id(int x, int y){
+    return uint(mod(y, map_height) * map_width + mod(x, map_width));
 }
 
 layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
 void main(){
-    uint x = gl_GlobalInvocationID.x;
-    uint y = gl_GlobalInvocationID.y;
+    int x = int(gl_GlobalInvocationID.x);
+    int y = int(gl_GlobalInvocationID.y);
 
     if(x >= map_width || y >= map_height){
         return;

+ 3 - 3
CppGPU/shaders/particles_update.glsl

@@ -13,8 +13,8 @@ struct cell_t {
     int population;
 };
 
-uniform int map_width;
-uniform int map_height;
+uniform uint map_width;
+uniform uint map_height;
 
 uniform uint population;
 
@@ -56,7 +56,7 @@ uint cell_id(vec2 v){
 
 layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
 void main(){
-    uint particle_id = gl_GlobalInvocationID.x;
+    uint particle_id = uint(gl_GlobalInvocationID.x);
     if(particle_id >= population){
         return;
     }

+ 1 - 1
CppGPU/src/ComputeShader.cpp

@@ -134,7 +134,7 @@ bool ComputeShader::set_uniform(char const *name, int value)
         std::cout << "Uniform 'int " << name << "' not found in program." << std::endl;
         return false;
     }
-    glUniform1i(location, value);
+    glUniform1ui(location, value);
     deactivate();
     return true;
 }

+ 33 - 20
CppGPU/src/main.cpp

@@ -51,6 +51,28 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
         cs_draw.compile();
         particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
         map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
+    }else if (key == GLFW_KEY_C){
+        float distance = std::min(config.map_width, config.map_height) / 5;
+        std::vector<Particle> particles(config.population);
+        for(auto &particle : particles){
+            float angle = random_float(0, 360) * deg_to_rad;
+            particle.dir_x = cosf(angle);
+            particle.dir_y = sinf(angle);
+
+            particle.pos_x = config.map_width / 2 + particle.dir_x * distance;
+            particle.pos_y = config.map_height / 2 + particle.dir_y * distance;
+        }
+        particles_buffer.set(particles.data(), particles.size(), sizeof(particles[0]));
+
+        std::vector<Cell> map(config.map_width * config.map_height);
+        for(auto &cell : map){
+            cell = {
+                .pheromone = 0,
+                .result = 0,
+                .population = 0,
+            };
+        }
+        map_buffer.set(map.data(), map.size(), sizeof(map[0]));
     }
 }
 
@@ -64,7 +86,7 @@ int main(int argc, char *argv[]){
     Canvas canvas;
 
     int map_width =  1500;
-    int map_height = 1200;
+    int map_height = 800;
     size_t population = 5000000;
     if(argc > 2){
         map_width  = strtol(argv[1], nullptr, 0);
@@ -96,12 +118,9 @@ int main(int argc, char *argv[]){
         config = new_conf;
     }
 
-
     float average_population = (float)config.population / (float)(config.map_width * config.map_height);
     float average_pheromone = (float)config.increase * average_population;
 
-    printf("Avg pop %08f - Avg phe %08f\n", average_population, average_pheromone);
-    
     app = Application(config.map_width, config.map_height);
     app.init();
     app.create_window();
@@ -128,7 +147,7 @@ int main(int argc, char *argv[]){
         particle.pos_x = random_float(0.0f, config.map_width);
         particle.pos_y = random_float(0.0f, config.map_height);
 
-        float angle = random_float(0, 360) * deg_to_rad;
+        float angle = random_float(0, 360 * deg_to_rad);
         particle.dir_x = cosf(angle);
         particle.dir_y = sinf(angle);
     }
@@ -170,15 +189,11 @@ int main(int argc, char *argv[]){
     app.register_callback(key_callback);
 
     /* Main loop */
-    float pervious_time;
-    float current_time = app.time();
-    int key_previous_status;
-    int key_current_status = GLFW_RELEASE;
+    float pervious_time = app.time();
     while(app.running()){
-        pervious_time = current_time;
-        current_time = app.time();
-
-        float dt = current_time - pervious_time;
+        float tick = app.time();
+        float dt = tick - pervious_time;
+        pervious_time = tick;
 
         /* Update uniforms */
         particle_uniform(config, cs_particles_update);    
@@ -208,22 +223,20 @@ int main(int argc, char *argv[]){
         canvas.unbind();
 
         /* Reset map */
-        cs_map_reset.execute(config.map_width * config.map_height, 256);
+        cs_map_reset.execute(config.map_width, config.map_height, 16, 16);
         ComputeShader::wait();
 
         /* Display, move on */
         app.update();
-
-        key_previous_status = key_current_status;
-        key_current_status = app.key_status(GLFW_KEY_R);
-        if(key_current_status != key_previous_status
-           && key_current_status == GLFW_RELEASE){
-        }
     }
 
     particles_buffer.release();
     map_buffer.release();
     canvas.release();
+    cs_draw.release();
+    cs_map_reset.release();
+    cs_map_update.release();
+    cs_particles_update.release();
     app.terminate();
 
     return 0;