Browse Source

Realigned

Charles Croz 1 year ago
parent
commit
a7313c9b15

+ 1 - 1
CppGPU/Makefile

@@ -5,7 +5,7 @@ OBJ_OPEN_GL=./src/glad.o
 HEADERS=./include/Config.hpp ./include/Helper.hpp ./include/Particle.hpp ./include/Cell.hpp
 
 GPP=g++
-CFLAGS=-O3 -I./include
+CFLAGS=-O2 -I./include
 CLIB=-lm -lSDL2 -lglfw -lGLU -lGL -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lrt -ldl
 
 all: $(APP)

+ 1 - 1
CppGPU/configs/config0.cfg

@@ -2,7 +2,7 @@ increase=10.0f
 decay=0.5f
 diffusion=5.0f
 
-speed=60.0f
+speed=50.0f
 steering=360.0f
 sensing_distance=3.0f
 sensing_angle=45.0f

+ 4 - 4
CppGPU/include/Canvas.hpp

@@ -10,11 +10,11 @@
 class Canvas
 {
 private:
-    GLuint m_texture;
-    GLuint m_FBO;
+    GLuint m_texture{};
+    GLuint m_FBO{};
 
-    GLuint m_width;
-    GLuint m_height;
+    GLuint m_width{};
+    GLuint m_height{};
 public:
     Canvas() = default;
     Canvas(GLuint width, GLuint height);

+ 1 - 1
CppGPU/include/Cell.hpp

@@ -3,7 +3,7 @@
 
 #include <stddef.h>
 
-struct alignas(32) Cell
+struct Cell
 {
     float pheromone{};
     float result{};

+ 1 - 1
CppGPU/include/Particle.hpp

@@ -1,7 +1,7 @@
 #ifndef PARTICLE_PARTICLE
 #define PARTICLE_PARTICLE
 
-struct alignas(32) Particle
+struct Particle
 {
     float pos_x;
     float pos_y;

+ 50 - 0
CppGPU/shaders/draw2.glsl

@@ -0,0 +1,50 @@
+#version 430 core
+
+struct particle_t {
+    vec2 pos;
+    vec2 dir;
+};
+
+struct cell_t {
+    float pheromone;
+    float result;
+    int population;
+};
+
+layout(binding = 0, rgba8) writeonly uniform image2D canvas;
+
+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
+{
+    particle_t particles[];
+};
+
+layout(std430, binding = 2) coherent buffer map_buffer
+{
+    cell_t map[];
+};
+
+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;
+    if(x >= map_width || y >= map_height){
+        return;
+    }
+    uint cell_id = y * map_width + x;
+    cell_t cell = map[cell_id]; 
+
+    float pop_value = cell.population / display_population_value / 2.5;
+    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,
+             1));
+}

+ 17 - 9
CppGPU/shaders/particles_update.glsl

@@ -1,8 +1,10 @@
 #version 430 core
 
 struct particle_t {
-    vec2 pos;
-    vec2 dir;
+    float pos_x;
+    float pos_y;
+    float dir_x;
+    float dir_y;
 };
 
 struct cell_t {
@@ -61,24 +63,30 @@ void main(){
 
     // Update particle position
     particle_t p = particles[particle_id];
-    particles[particle_id].pos = wrap_coord(p.pos + dt * speed * p.dir);
+    vec2 pos = vec2(p.pos_x, p.pos_y);
+    vec2 dir = vec2(p.dir_x, p.dir_y);
+    pos = wrap_coord(pos + dt * speed * dir);
+    particles[particle_id].pos_x = pos.x;
+    particles[particle_id].pos_y = pos.y;
     
     // Count particle
-    uint p_cell_id = cell_id(p.pos);
+    uint p_cell_id = cell_id(pos);
     atomicAdd(map[p_cell_id].population, 1);
 
     // Sense and turn
-    vec2 look_ahead = wrap_coord(ahead(p.pos, p.dir, sensing_distance));
-    vec2 look_left  = wrap_coord(ahead(p.pos, rotate(p.dir,  sensing_angle), sensing_distance));
-    vec2 look_right = wrap_coord(ahead(p.pos, rotate(p.dir, -sensing_angle), sensing_distance));
+    vec2 look_ahead = wrap_coord(ahead(pos, dir, sensing_distance));
+    vec2 look_left  = wrap_coord(ahead(pos, rotate(dir,  sensing_angle), sensing_distance));
+    vec2 look_right = wrap_coord(ahead(pos, rotate(dir, -sensing_angle), sensing_distance));
 
     float pheromone_ahead = map[cell_id(look_ahead)].pheromone;
     float pheromone_left  = map[cell_id(look_left )].pheromone;
     float pheromone_right = map[cell_id(look_right)].pheromone;
 
     if(pheromone_left > pheromone_ahead && pheromone_left > pheromone_right){
-        particles[particle_id].dir = rotate(p.dir,  steering * dt);
+        dir = rotate(dir,  steering * dt);
     }else if(pheromone_right > pheromone_ahead && pheromone_right > pheromone_left){
-        particles[particle_id].dir = rotate(p.dir, -steering * dt);
+        dir = rotate(dir, -steering * dt);
     }
+    particles[particle_id].dir_x = dir.x;
+    particles[particle_id].dir_y = dir.y;
 }

+ 0 - 1
CppGPU/src/Application.cpp

@@ -6,7 +6,6 @@ Application::Application(int width, int height) : m_window_width(width), m_windo
 
 Application::~Application()
 {
-    glfwTerminate();
 }
 
 void Application::init()

+ 0 - 1
CppGPU/src/Buffer.cpp

@@ -2,7 +2,6 @@
 
 Buffer::~Buffer()
 {
-    release();
 }
 
 void Buffer::create()

+ 1 - 4
CppGPU/src/Canvas.cpp

@@ -7,7 +7,6 @@ m_width(width), m_height(height)
 
 Canvas::~Canvas()
 {
-    release();
 }
 
 void Canvas::create()
@@ -30,16 +29,14 @@ void Canvas::create()
 
 void Canvas::bind()
 {
-
     glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
     glBindImageTexture(0, m_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
-    // glBindTexture(GL_TEXTURE_2D, m_texture);
 }
 
 void Canvas::unbind()
 {
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
-    glBindTexture(GL_TEXTURE_2D, 0);
+    // glBindTexture(GL_TEXTURE_2D, 0);
 }
 
 void Canvas::draw()

+ 6 - 0
CppGPU/src/main.cpp

@@ -205,6 +205,7 @@ int main(int argc, char *argv[]){
         cs_draw.execute(config.map_width, config.map_height, 16, 16);
         ComputeShader::wait();
         canvas.draw();
+        canvas.unbind();
 
         /* Reset map */
         cs_map_reset.execute(config.map_width * config.map_height, 256);
@@ -220,5 +221,10 @@ int main(int argc, char *argv[]){
         }
     }
 
+    particles_buffer.release();
+    map_buffer.release();
+    canvas.release();
+    app.terminate();
+
     return 0;
 }