From 4ed884f87d9007e426e4157220b50d73b6087e4e Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Sun, 29 Oct 2023 16:39:57 +0100 Subject: [PATCH] WIP: cleaning - with still an error in erase_arrow() --- src/graphics/displays.c | 62 ++++++++++++- src/graphics/graphics.c | 188 ++++++++++++++++------------------------ 2 files changed, 131 insertions(+), 119 deletions(-) diff --git a/src/graphics/displays.c b/src/graphics/displays.c index 4b9b2ca..8647382 100644 --- a/src/graphics/displays.c +++ b/src/graphics/displays.c @@ -29,6 +29,60 @@ #include "../../include/displays.h" +/* TO REMEMBER : Ici, tout ce qui gène la lecture du code en cours de debugging + * #pragma omp parallel schedule(static, 12) + * void __attribute__((optimize("no-unroll-loops"))) main_test_graphics (void) +{ +// assert : space dimensions (x,y,z) > 0 +// assert : arrows localization within space and sites +// assert : no more than one arrow per address +// notify : weights are replaced, NOT added (could be !) +/* + * Init space and arrows (= initial state) + * and allows ulterior creations, suppressions or modifications of the arrows[] array + * + * Draws space and arrows + * + * // Initialisation du générateur pseudo-aléatoire + * // Attention, à ce stade, pas de vertex redondant +*/ + + + +/* in set_arrow() : */ + /* if (console) print_vertex_and_lines_buffers (arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ + /* if (0) print_vertex_and_lines_buffers (arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ + /* if (requested_weight == 0 && console) */ + /* printf(" - set_arrow() invoked with requested weight = %2d bf_vrtx = %d bf_ln = %d add: %d",\ */ + /* requested_weight, buffer_vertex_size / 3, buffer_lines_size / 2, address); */ + /* if (console) show_user_action(arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ + + +/* + * Calls one of the functions create_arrow(), erase_arrow() or rewrite_arrow() + * according to requested weight and address (coord site, x, y, z) + * + * May not call any of these three functions if : + * - Current_weight of an arrow located at the requested address == requested_weight + * - No arrow was found at the requested addres AND current_weight == requested_weight + */ +/* + * Removes an arrow at address (address) +*/ +/* + * Creates a new arrow at address (address) + * NB Weights (or loads) are NOT added +*/ + +/* + * Assigns a new load to the arrow at address (address) +*/ + + +/*--------------------------------------------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------------------*/ + /* Prints the arrows[] array * @@ -41,9 +95,9 @@ void print_arrows_array (struct arrow_t *arrows, int arrows_nb, int invoked_by) { printf(" [rank] load | site x y z ("); switch (invoked_by) { - case 0: printf("delete)"); break; - case 1: printf("create)"); break; - case 2: printf("modify)"); break; + case 0: printf("after deletion)"); break; + case 1: printf("after creation)"); break; + case 2: printf("after modification)"); break; case 3: printf("address >= 0 && current_weight == requested_weight)"); break; case 4: printf("address == -1 && requested_weight == 0)"); break; case 5: printf("print_user_choices)"); break; @@ -67,7 +121,7 @@ void print_user_choices(struct arrow_t *arrows, int arrows_nb, int space_size_x, int space_size_y, int space_size_z, int show_array, int show_space_design) { - printf("model + user constraints :\tspace size x,y,z (%3d,%3d,%3d)\tarrows nb : %6d\t",\ + printf("model + user constraints :\tspace size x,y,z (%3d,%3d,%3d)\t\tarrows nb : %6d\t",\ space_size_x, space_size_y, space_size_z, arrows_nb); if (show_space_design) printf(" (grilles alternées)"); diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 8e62c03..f0cada6 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -35,8 +35,7 @@ #define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define FRAG_SHADER_FILE "src/shaders/shader.frag" -static struct arrow_t *arrows_ptr; /* nom modifié pour pouvoir plus facilement distinguer le tableau - * des autres noms incluant "arrows" (de arrows_nb, par ex.) */ +static struct arrow_t *arrows_ptr; GLfloat *buffer_vertex_origin = NULL; GLfloat *buffer_colors_origin = NULL; GLuint *buffer_lines_origin = NULL; @@ -618,9 +617,18 @@ static inline void print_vertex_and_lines_buffers (struct arrow_t *arrows_ptr, i -/* - * Assigns a new load to the arrow at address (address) -*/ + + + + + + + + + + + + static int rewrite_arrow (int arrows_nb, int address, int load, int site, int x, int y, int z, int console) { arrows_ptr[address].load = load; @@ -630,10 +638,6 @@ static int rewrite_arrow (int arrows_nb, int address, int load, int site, int x, return arrows_nb; } -/* - * Creates a new arrow at address (address) - * NB Weights (or loads) are NOT added -*/ static inline int create_arrow (int arrows_nb, int space_X, int space_Y, int space_Z, int load, int site, int x, int y, int z, @@ -665,9 +669,6 @@ static inline int create_arrow (int arrows_nb, return arrows_nb; } -/* - * Removes an arrow at address (address) -*/ static inline int erase_arrow (int arrows_nb, int address, int site, int x, int y, int z, int console) @@ -675,22 +676,20 @@ static inline int erase_arrow (int arrows_nb, int address, arrows_nb --; assert (arrows_nb); - arrows_ptr[address].load = arrows_ptr[arrows_nb].load; - arrows_ptr[address].site = arrows_ptr[arrows_nb].site; - arrows_ptr[address].x = arrows_ptr[arrows_nb].x; - arrows_ptr[address].y = arrows_ptr[arrows_nb].y; - arrows_ptr[address].z = arrows_ptr[arrows_nb].z; + int mem = arrows_ptr[address].site; + + arrows_ptr[address].load = arrows_ptr[arrows_nb].load; + arrows_ptr[address].site = arrows_ptr[arrows_nb].site; + arrows_ptr[address].x = arrows_ptr[arrows_nb].x; + arrows_ptr[address].y = arrows_ptr[arrows_nb].y; + arrows_ptr[address].z = arrows_ptr[arrows_nb].z; - buffer_lines_size -= 2; + if (console) printf("in erase_arrow() : arrows_nb = %d > %d arrow to erase > (address = %d, site = %d)\n\ + : arrows_ptr[%d].site = %d >> %d (%d is last arrow.site)\n",\ + arrows_nb + 1, arrows_nb, address, address, site, mem, arrows_ptr[address].site, arrows_ptr[arrows_nb].site); - /* void *new_arrows_lines_ptr = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLfloat)); */ - /* if (new_arrows_lines_ptr) buffer_lines_origin = new_arrows_lines_ptr; */ - /* else perror("In graphics.erase_arrow(), can't re_allocate for arrows lines buffer.\n"); */ - - if (console) printf("arrows_nb = %d address = %d\n", arrows_nb, address); - - // Attention : à la création des vertex, tous les vertex centraux sont redondants (au maximum 6) + // Attention : lors de la création des vertex des flèches, tous les vertex centraux sont redondants (au maximum 6) for (int i = 0 + address * 6; i < 6 + address * 6; i++) buffer_vertex_origin [(long) buffer_vertex_0_arrow + i] @@ -700,13 +699,23 @@ static inline int erase_arrow (int arrows_nb, int address, buffer_colors_origin [(long) buffer_colors_0_arrow + (address + 4) * 6 + i] = buffer_colors_origin [(long) buffer_colors_size - 6 + i]; - buffer_vertex_size -= 6; - buffer_colors_size -= 6; - void *new_arrows_vertex_ptr = g_realloc(buffer_vertex_origin, buffer_vertex_size * sizeof(GLfloat)); if (new_arrows_vertex_ptr) buffer_vertex_origin = new_arrows_vertex_ptr; else perror("In graphics.erase_arrow(), can't re_allocate for arrows vertex buffer.\n"); + void *new_arrows_colors_ptr = g_realloc(buffer_colors_origin, buffer_colors_size * sizeof(GLfloat)); + if (new_arrows_colors_ptr) buffer_colors_origin = new_arrows_colors_ptr; + else perror("In graphics.erase_arrow(), can't re_allocate for arrows colors buffer.\n"); + + + /* buffer_vertex_size -= 6; <<< ceci est fait dans : graphics_write_vertex() */ + /* buffer_colors_size -= 6; <<< ceci est fait dans : graphics_write_colors() */ + /* buffer_lines_size -= 2; <<< ceci est fait dans : graphics_write_line() */ + + /* Il ne faut pas réécrire ce qui suit: ces lignes dessinent maintenant à partir d'autres vertex */ + /* void *new_arrows_lines_ptr = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLfloat)); */ + /* if (new_arrows_lines_ptr) buffer_lines_origin = new_arrows_lines_ptr; */ + /* else perror("In graphics.erase_arrow(), can't re_allocate for arrows lines buffer.\n"); */ if (console) print_arrows_array (arrows_ptr, arrows_nb, 0); @@ -715,95 +724,43 @@ static inline int erase_arrow (int arrows_nb, int address, -static inline void print_vertex_and_lines_buffers(struct arrow_t *arrows_ptr, int arrows_nb, int address, int requested_weight, - int current_weight, int site, int x, int y, int z); -static inline void show_user_action(struct arrow_t *arrows_ptr, int arrows_nb, int address, int requested_weight, - int current_weight, int site, int x, int y, int z); -/* - * Calls one of the functions create_arrow(), erase_arrow() or rewrite_arrow() - * according to requested weight and address (coord site, x, y, z) - * - * May not call any of these three functions if : - * - Current_weight of an arrow located at the requested address == requested_weight - * - No arrow was found at the requested addres AND current_weight == requested_weight - */ -static inline int set_arrow (struct arrow_t *arrows_ptr, int arrows_nb, - int space_X, int space_Y, int space_Z, - int requested_weight, int site, int x, int y, int z, int console) +static inline int set_arrow (struct arrow_t *arrows_ptr, int arrows_nb, int space_X, int space_Y, int space_Z, + int requested_weight, int site, int arrow_x, int arrow_y, int arrow_z, int console) { int address = -1, current_weight = -1; -#pragma omp parallel schedule(static, 12) -{ - for (int i = 0; i < arrows_nb; i++) { - if ((site == arrows_ptr[i].site) - && (x == arrows_ptr[i].x) - && (y == arrows_ptr[i].y) - && (z == arrows_ptr[i].z)) + for (int i = 0; i < arrows_nb; i++) + if ((site == arrows_ptr[i].site) + && (arrow_x == arrows_ptr[i].x) + && (arrow_y == arrows_ptr[i].y) + && (arrow_z == arrows_ptr[i].z)) { - address = i * 5; + address = i; current_weight = arrows_ptr[i].load; break; } - } -} - - /* if (console) print_vertex_and_lines_buffers (arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ - /* if (0) print_vertex_and_lines_buffers (arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ - /* if (requested_weight == 0 && console) */ - /* printf(" - set_arrow() invoked with requested weight = %2d bf_vrtx = %d bf_ln = %d add: %d",\ */ - /* requested_weight, buffer_vertex_size / 3, buffer_lines_size / 2, address); */ - /* if (console) show_user_action(arrows_ptr, arrows_nb, address, requested_weight, current_weight, site, x, y, z); */ - - if (address == -1 && requested_weight > 0) - return create_arrow (arrows_nb, space_X, space_Y, space_Z, requested_weight, site, x, y, z, console); - + return create_arrow (arrows_nb, space_X, space_Y, space_Z, requested_weight, site, arrow_x, arrow_y, arrow_z, console); if (address >= 0 && requested_weight == 0) // address >= 0 if and only if arrows_nb > 0 - return erase_arrow (arrows_nb, address, site, x, y, z, console); - + return erase_arrow (arrows_nb, address, site, arrow_x, arrow_y, arrow_z, console); if (address >= 0 && current_weight != requested_weight) - return rewrite_arrow (arrows_nb, address/5, requested_weight, site, x, y, z, console); - - if (address >= 0 && current_weight == requested_weight && console) print_arrows_array(arrows_ptr, arrows_nb, 3); - - if (address == -1 && requested_weight == 0 && console) print_arrows_array(arrows_ptr, arrows_nb, 4); + return rewrite_arrow (arrows_nb, address/5, requested_weight, site, arrow_x, arrow_y, arrow_z, console); + if (address >= 0 && current_weight == requested_weight && console) print_arrows_array(arrows_ptr, arrows_nb, 3); + if (address == -1 && requested_weight == 0 && console) print_arrows_array(arrows_ptr, arrows_nb, 4); return arrows_nb; } -/* - * Init space and arrows (= initial state) - * and allows ulterior creations, suppressions or modifications of the arrows[] array - * - * Draws space and arrows - */ -void __attribute__((optimize("no-unroll-loops"))) main_test_graphics (void) +void main_test_graphics (void) { -// assert : space dimensions (x,y,z) > 0 -// assert : arrows localization within space and sites -// assert : no more than one arrow per address -// notify : weights are replaced, NOT added (could be !) - - srand(time(NULL)); // Initialisation du générateur pseudo-aléatoire - int rand(void); - void srand(unsigned int seed); // printf ("Valeur max : %d\n", RAND_MAX); min + rand() % (max+1 - min); - - int arbitrary = 4; - int space_X = 1,// + rand() % arbitrary, - space_Y = 1,// + rand() % arbitrary, - space_Z = 1;// + rand() % arbitrary; - int density_max = space_X * space_Y * space_Z; + srand(time(NULL)); int rand(void); void srand(unsigned int seed); int arbitrary = 4, randomize = 0, console = 0; + int space_X = 1, space_Y = 1, space_Z = 1, density_max = space_X * space_Y * space_Z; + if (randomize) {space_X = 1 + rand() % arbitrary; space_Y = 1 + rand() % arbitrary; space_Z = 1 + rand() % arbitrary;} int max = fmax(space_X, space_Y); max = fmax(max, space_Z); - - int print_arrows_data = 0, console = 0; - - int load = 0, site = 0, x = 0, y = 0, z = 0; - int specif_arrows_nb = rand() % density_max / 2; - int arrows_nb = 0, cpt = 0, t_initial = 0; + int load = 0, site = 0, x = 0, y = 0, z = 0, arrows_nb = 0, specif_arrows_nb = rand() % density_max / 2; /*--------------------------------------------------------------------------------*/ @@ -822,21 +779,24 @@ void __attribute__((optimize("no-unroll-loops"))) main_test_graphics (void) write_grids_on_space_faces_vertex (space_X, space_Y, space_Z); write_grids_on_space_faces_lines (buffer_lines_size, space_X, space_Y, space_Z); - buffer_vertex_0_arrow = buffer_vertex_size; // Attention, à ce stade, pas de vertex redondant + buffer_vertex_0_arrow = buffer_vertex_size; buffer_lines_0_arrow = buffer_lines_size; /* A R R O W S */ - for (int i = 0; i < 6; i++) + if (1) for (int i = 0; i < 6; i++) arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, - 1, // load - i % 6, // site - 0,//rand() % space_X, // x - 0,//rand() % space_Y, // y - 0,//rand() % space_Z, // z + 1, i % 6, 0, 0, 0, console); + if (0) for (int i = 0; i < rand() % density_max; i++) + arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, + 1, // load + rand() % 6, // site + rand() % space_X, // x + rand() % space_Y, // y + rand() % space_Z, // z console); - print_user_choices(arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 0); + if (1) print_user_choices(arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 0); /*--------------------------------------------------------------------------------*/ @@ -849,15 +809,13 @@ void __attribute__((optimize("no-unroll-loops"))) main_test_graphics (void) console = 1; - /* If load == 0 then erase the arrow at this address */ - - arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, - 0, // load - 0, // site - 0, // x - 0, // y - 0, // z - console); + /* If load == 0 then erase the arrow at this address v */ + arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 0, 0, 0, 0, console); + arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 1, 0, 0, 0, console); + arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 2, 0, 0, 0, console); + /* arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 3, 0, 0, 0, console); */ + /* arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 4, 0, 0, 0, console); */ + /* arrows_nb = set_arrow (arrows_ptr, arrows_nb, space_X, space_Y, space_Z, 0, 5, 0, 0, 0, console); */