Răsfoiți Sursa

getting there

Charles Croz 1 an în urmă
părinte
comite
16da3a569f

+ 2 - 1
CppGPU/include/Application.hpp

@@ -25,9 +25,10 @@ public:
 
     void init();
     void terminate();
-    bool createWindow();
+    bool create_window();
     bool running();
     void update();
+    int key_status(int code);
     float time();
 };
 

+ 14 - 5
CppGPU/shaders/draw.glsl

@@ -39,9 +39,18 @@ void main(){
     uint cell_id = y * map_width + x;
     cell_t cell = map[cell_id]; 
 
+    float pop_value = cell.population / display_population_value;
+    float phe_value = cell.pheromone / display_pheromone_value;
+
     imageStore(canvas, ivec2(gl_GlobalInvocationID.xy), 
-        vec4(cell.population/display_population_value, 
-             cell.pheromone/display_pheromone_value,
-            //  0,
-             0, 1));
-}
+        vec4(pop_value + phe_value,
+             pop_value,
+             phe_value,
+             1));
+    // Particules rouges, phero bleues
+    // imageStore(canvas, ivec2(gl_GlobalInvocationID.xy), 
+    //     vec4(cell.population / display_population_value, 
+    //          cell.pheromone / display_pheromone_value,
+    //          cell.pheromone / (display_pheromone_value),
+    //          1));
+}

+ 1 - 1
CppGPU/shaders/map_reset.glsl

@@ -17,7 +17,7 @@ layout(std430, binding = 2) coherent buffer map_buffer
 layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
 void main(){
     uint cell_id = gl_GlobalInvocationID.x;
-    if(cell_id >= map_width * map_height){
+    if(cell_id >= uint(map_width * map_height)){
         return;
     }
     map[cell_id].pheromone = map[cell_id].result;

+ 1 - 1
CppGPU/shaders/map_update.glsl

@@ -21,7 +21,7 @@ layout(std430, binding = 2) coherent buffer map_buffer
 };
 
 uint cell_id(uint x, uint y){
-    return uint(mod(x, map_width) + mod(x, map_height) * map_width);
+    return uint(mod(y + map_height, map_height) * map_width + mod(x + map_width, map_width));
 }
 
 layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

+ 1 - 1
CppGPU/shaders/particles_update.glsl

@@ -65,7 +65,7 @@ void main(){
     
     // Count particle
     uint p_cell_id = cell_id(p.pos);
-    // atomicAdd(map[p_cell_id].population, 1);
+    atomicAdd(map[p_cell_id].population, 1);
 
     // Sense and turn
     vec2 look_ahead = wrap_coord(ahead(p.pos, p.dir, sensing_distance));

+ 6 - 1
CppGPU/src/Application.cpp

@@ -22,7 +22,7 @@ void Application::terminate()
     glfwTerminate();
 }
 
-bool Application::createWindow()
+bool Application::create_window()
 {
     window = glfwCreateWindow(m_window_width, m_window_heigth, m_appName.c_str(), nullptr, nullptr);
     if(window == nullptr){
@@ -55,6 +55,11 @@ void Application::update()
     glfwPollEvents();
 }
 
+int Application::key_status(int code)
+{
+    return glfwGetKey(window, code);
+}
+
 float Application::time()
 {
     return (float)glfwGetTime();

+ 0 - 4
CppGPU/src/ComputeShader.cpp

@@ -95,8 +95,6 @@ void ComputeShader::execute(size_t x, size_t group_size_x)
 {
     activate();
     size_t n_group_x = x/group_size_x + (x % group_size_x ? 1 : 0);
-    // fprintf(stdout, "Dispatching (%lu, 1, 1) groups of size (%lu, 1, 1)\n", 
-    //         n_group_x, group_size_x);
     glDispatchCompute(n_group_x, 1, 1);
     deactivate();
 }
@@ -106,8 +104,6 @@ void ComputeShader::execute(size_t x, size_t y, size_t group_size_x, size_t grou
     activate();
     size_t n_group_x = x/group_size_x + (x % group_size_x ? 1 : 0);
     size_t n_group_y = y/group_size_y + (y % group_size_y ? 1 : 0);
-    // fprintf(stdout, "Dispatching (%lu, %lu, 1) groups of size (%lu, %lu, 1)\n", 
-    //         n_group_x, n_group_y, group_size_x, group_size_y);
     glDispatchCompute(n_group_x, n_group_y, 1);
     deactivate();
 }

+ 35 - 26
CppGPU/src/main.cpp

@@ -36,19 +36,19 @@ int main(int argc, char *argv[]){
     Canvas canvas;
 
     config = {
-        .map_width=800,
-        .map_height=600,
+        .map_width=1500,
+        .map_height=1200,
 
-        .population=100,
+        .population=5000000,
 
         .increase=10.0f,
-        .decay=5.0f,
-        .diffusion=2.0f,
+        .decay=0.5f,
+        .diffusion=5.0f,
 
-        .speed=0.5f,
-        .steering=90.0f * deg_to_rad,
-        .sensing_distance=1.0f,
-        .sensing_angle=60.0f * deg_to_rad,
+        .speed=60.0f,
+        .steering=180.0f * deg_to_rad,
+        .sensing_distance=3.0f,
+        .sensing_angle=45.0f * deg_to_rad,
     };
     float average_population = (float)config.population / (float)(config.map_width * config.map_height);
     float average_pheromone = (float)config.increase * average_population;
@@ -57,28 +57,23 @@ int main(int argc, char *argv[]){
     
     app = Application(config.map_width, config.map_height);
     app.init();
-    app.createWindow();
+    app.create_window();
 
     /* Load and compile particles update shader */
     cs_particles_update.load("./shaders/particles_update.glsl");
     cs_particles_update.compile();
-    particle_uniform(config, cs_particles_update);    
 
     /* Load and compile map update shader */
     cs_map_update.load("./shaders/map_update.glsl");
     cs_map_update.compile();
-    map_update_uniform(config, cs_map_update);    
 
     /* Load and compile map reset shader */
     cs_map_reset.load("./shaders/map_reset.glsl");
     cs_map_reset.compile();
-    map_reset_uniform(config, cs_map_reset);    
 
     /* Load and compile draw shader */
     cs_draw.load("./shaders/draw.glsl");
     cs_draw.compile();
-    cs_draw.set_uniform("map_width", config.map_width);    
-    cs_draw.set_uniform("map_height", config.map_height);    
 
     /* Particles init */
     std::vector<Particle> particles(config.population);
@@ -90,11 +85,6 @@ int main(int argc, char *argv[]){
         particle.dir_x = cosf(angle);
         particle.dir_y = sinf(angle);
     }
-    particles[0].pos_x = 0.5;
-    particles[0].pos_y = 0.5;
-    particles[0].dir_x = 1;
-    particles[0].dir_y = 0;
-    printf("Bob %f, %f\n", particles[0].pos_x, particles[0].pos_y);
 
     /* Create and fill particles buffer */
     particles_buffer.create();
@@ -132,12 +122,22 @@ int main(int argc, char *argv[]){
     /* Main loop */
     float pervious_time;
     float current_time = app.time();
+    int key_previous_status;
+    int key_current_status = GLFW_RELEASE;
     while(app.running()){
         pervious_time = current_time;
         current_time = app.time();
 
         float dt = current_time - pervious_time;
-        // printf("[% 6f]\n", dt * 1000);
+
+        /* Update uniforms */
+        particle_uniform(config, cs_particles_update);    
+        map_update_uniform(config, cs_map_update);    
+        map_reset_uniform(config, cs_map_reset);    
+        cs_draw.set_uniform("map_width", config.map_width);    
+        cs_draw.set_uniform("map_height", config.map_height);    
+        cs_draw.set_uniform("display_population_value", average_population);
+        cs_draw.set_uniform("display_pheromone_value", average_pheromone);
 
         /* Update particles */
         cs_particles_update.set_uniform("dt", dt);
@@ -162,16 +162,25 @@ int main(int argc, char *argv[]){
         canvas.bind();
         cs_draw.execute(config.map_width, config.map_height, 16, 16);
         ComputeShader::wait();
-        cs_draw.set_uniform("display_population_value", 10*average_population);
-        cs_draw.set_uniform("display_pheromone_value", 1000*average_pheromone);
         canvas.draw();
 
-        app.update();
-
         /* Reset map */
-        cs_map_reset.execute(config.population, 256);
+        cs_map_reset.execute(config.map_width * config.map_height, 256);
         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){
+            printf("Reloading draw Shader\n");
+            cs_draw.load("./shaders/draw.glsl");
+            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);
+        }
     }
     
     cs_particles_update.release();