Add debug timers

This commit is contained in:
Phireh 2023-10-29 14:56:22 +01:00
commit ae874570c1
2 changed files with 325 additions and 182 deletions

View file

@ -9,7 +9,7 @@ Size=550,680
Collapsed=0
[Window][Hi there]
Pos=158,190
Pos=138,172
Size=422,452
Collapsed=0

251
main.cpp
View file

@ -30,6 +30,46 @@
// STL
#include <iostream>
// POSIX
#include <time.h>
struct debug_timer_t
{
const char *name;
int64_t usec;
};
extern debug_timer_t debug_timers[];
extern int ntimers;
struct debug_clock_t
{
const char *name;
int id;
int64_t start;
int64_t end;
debug_clock_t(const char *name, int id)
{
this->name = name;
this->id = id;
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
start = spec.tv_sec * 1000 * 1000 + spec.tv_nsec / 1000;
}
~debug_clock_t()
{
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
end = spec.tv_sec * 1000 * 1000 + spec.tv_nsec / 1000;
debug_timers[id].name = this->name;
debug_timers[id].usec = end - start;
}
};
#define TIME_BLOCK(name) debug_clock_t _name_##timer(#name, __COUNTER__);
struct charglyph_t {
uint32_t tex; // handle to glyph texture
glm::ivec2 size; // 2d dimensions
@ -38,6 +78,7 @@ struct charglyph_t {
};
struct hex_t {
int32_t id;
glm::vec3 position;
float radius;
glm::vec3 vertices[7];
@ -58,6 +99,11 @@ struct camera_t {
glm::vec2 size;
};
struct selection_t {
int32_t *indices;
int32_t nselected;
};
charglyph_t glyphmap[128];
uint32_t text_program;
uint32_t textvao, textvbo;
@ -79,6 +125,30 @@ double mouse_scroll = 0.0;
#define MAIN_ERROR(e) do { main_errcode = e; errcode_string = #e; goto exit_main_with_error; } while(0)
bool is_selected(int idx, selection_t *sel)
{
for (int i = 0; i < sel->nselected; ++i)
if (sel->indices[i] == idx)
return true;
return false;
}
void select(int idx, selection_t *sel)
{
if (!is_selected(idx, sel))
sel->indices[sel->nselected++] = idx;
}
void deselect(int idx, selection_t *sel)
{
for (int i = 0; i < sel->nselected; ++i)
if (sel->indices[i] == idx)
{
sel->indices[i] = -1;
--sel->nselected;
}
}
void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW error %d: %s\n", error, description);
@ -216,6 +286,72 @@ bool inside_hex(hex_t *hex, glm::vec2 point)
return true;
}
void create_grid(grid_t *grid, int rows, int columns)
{
grid->rows = rows;
grid->columns = columns;
if (!grid->hexes)
grid->hexes = (hex_t*)calloc(grid->rows * grid->columns, sizeof(hex_t));
else
{
hex_t *new_ptr = (hex_t*)realloc(grid->hexes, grid->rows * grid->columns * sizeof(hex_t));
if (new_ptr)
grid->hexes = new_ptr;
else
{
free(grid->hexes);
grid->hexes = (hex_t*)calloc(grid->rows * grid->columns, sizeof(hex_t));
}
}
for (int i = 0; i < grid->rows; ++i)
for (int j = 0; j < grid->columns; ++j)
{
hex_t *the_hexagon = &grid->hexes[i*grid->columns+j];
float radius = 0.2;
the_hexagon->position = glm::vec3(0,0,0) + glm::vec3(j * radius * 3.0f * glm::cos(glm::radians(60.0))
, (i * radius * 2.0f * glm::sin(glm::radians(60.0))) + (j % 2 ? 0 : radius * glm::sin(glm::radians(60.0))),
0);
the_hexagon->radius = radius;
the_hexagon->id = i * grid->columns + j;
the_hexagon->color = glm::vec4(1.0 - i * 0.1, 1.0 - j * 0.1, 1.0, 1.0);
the_hexagon->vertices[0] = the_hexagon->position;
the_hexagon->vertices[1] = the_hexagon->position + glm::vec3(-radius, 0, 0);
the_hexagon->vertices[2] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[3] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[4] = the_hexagon->position + glm::vec3(radius, 0, 0);
the_hexagon->vertices[5] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[6] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
glGenVertexArrays(1, &the_hexagon->vao);
glGenBuffers(1, &the_hexagon->vbo);
glBindVertexArray(the_hexagon->vao);
glBindBuffer(GL_ARRAY_BUFFER, the_hexagon->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 7 * 3, the_hexagon->vertices, GL_STATIC_DRAW);
// EBO setup
unsigned int indices[18];
indices[0] = 0; indices[1] = 2; indices[2] = 1;
indices[3] = 0; indices[4] = 3; indices[5] = 2;
indices[6] = 0; indices[7] = 4; indices[8] = 3;
indices[9] = 0; indices[10] = 5; indices[11] = 4;
indices[12] = 0; indices[13] = 6; indices[14] = 5;
indices[15] = 0; indices[16] = 1; indices[17] = 6;
glGenBuffers(1, &the_hexagon->ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, the_hexagon->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*3 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
}
// global state
GLFWwindow *window = nullptr;
hex_t hexes[4];
@ -238,6 +374,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetScrollCallback(window, scroll_callback);
if (!gladLoadGL())
@ -322,54 +459,9 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
grid_t grid;
grid.rows = 10;
grid.columns = 10;
grid.hexes = (hex_t*)calloc(grid.rows * grid.columns, sizeof(hex_t));
static grid_t grid = {};
for (int i = 0; i < grid.rows; ++i)
for (int j = 0; j < grid.columns; ++j)
{
hex_t *the_hexagon = &grid.hexes[i*grid.columns+j];
float radius = 0.2;
the_hexagon->position = glm::vec3(0,0,0) + glm::vec3(j * radius * 3.0f * glm::cos(glm::radians(60.0))
, (i * radius * 2.0f * glm::sin(glm::radians(60.0))) + (j % 2 ? 0 : radius * glm::sin(glm::radians(60.0))),
0);
the_hexagon->radius = radius;
the_hexagon->color = glm::vec4(1.0 - i * 0.1, 1.0 - j * 0.1, 1.0, 1.0);
the_hexagon->vertices[0] = the_hexagon->position;
the_hexagon->vertices[1] = the_hexagon->position + glm::vec3(-radius, 0, 0);
the_hexagon->vertices[2] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[3] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[4] = the_hexagon->position + glm::vec3(radius, 0, 0);
the_hexagon->vertices[5] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
the_hexagon->vertices[6] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
glGenVertexArrays(1, &the_hexagon->vao);
glGenBuffers(1, &the_hexagon->vbo);
glBindVertexArray(the_hexagon->vao);
glBindBuffer(GL_ARRAY_BUFFER, the_hexagon->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 7 * 3, the_hexagon->vertices, GL_STATIC_DRAW);
// EBO setup
unsigned int indices[18];
indices[0] = 0; indices[1] = 2; indices[2] = 1;
indices[3] = 0; indices[4] = 3; indices[5] = 2;
indices[6] = 0; indices[7] = 4; indices[8] = 3;
indices[9] = 0; indices[10] = 5; indices[11] = 4;
indices[12] = 0; indices[13] = 6; indices[14] = 5;
indices[15] = 0; indices[16] = 1; indices[17] = 6;
glGenBuffers(1, &the_hexagon->ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, the_hexagon->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*3 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
create_grid(&grid, 10, 10);
hex_program = make_gl_program("shaders/hex.vert", "shaders/hex.frag");
@ -387,20 +479,29 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
camera_t the_camera;
bool mouse_pressed;
bool mouse_over_gui;
static selection_t selection;
selection.indices = (int32_t*)calloc(1, sizeof(int32_t) * grid.rows * grid.columns);
selection.nselected = 0;
the_camera.position = glm::vec3(0.0f, 0.0f, 1.0f);
the_camera.size = glm::vec2(1.0f, 1.0f);
// main loop
while (!glfwWindowShouldClose(window))
{
TIME_BLOCK(full_frame);
double cursor_dx = 0.0;
double cursor_dy = 0.0;
int hovered_hex = -1;
int32_t window_width, window_height;
{
TIME_BLOCK(window_size_polling);
glfwGetWindowSize(window, &window_width, &window_height);
glViewport(0, 0, window_width, window_height);
}
float aspect_ratio = (float)window_width/window_height;
{
TIME_BLOCK(input_handling);
/* Input handling */
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
the_camera.position.y += 0.1;
@ -450,14 +551,22 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
the_camera.size.x = 0.1;
if (the_camera.size.y < 0.1)
the_camera.size.y = 0.1;
}
glm::mat4 model_matrix;
glm::mat4 view_matrix;
glm::mat4 proj_matrix;
glm::vec2 cursor_world;
/* Rendering */
{
TIME_BLOCK(hex_render);
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 model_matrix = glm::mat4(1.0f);
glm::mat4 view_matrix = glm::mat4(1.0f);
glm::mat4 proj_matrix = glm::ortho((the_camera.position.x - the_camera.size.x/2) * aspect_ratio, (the_camera.position.x + the_camera.size.x/2) * aspect_ratio, the_camera.position.y - the_camera.size.y/2, the_camera.position.y + the_camera.size.y/2);
model_matrix = glm::mat4(1.0f);
view_matrix = glm::mat4(1.0f);
proj_matrix = glm::ortho((the_camera.position.x - the_camera.size.x/2) * aspect_ratio, (the_camera.position.x + the_camera.size.x/2) * aspect_ratio, the_camera.position.y - the_camera.size.y/2, the_camera.position.y + the_camera.size.y/2);
glm::vec4 aux((float)cursor_x, (float)cursor_y, 0.0f, 1.0f);
@ -467,10 +576,9 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
aux.y -= window_height/2.0f;
aux.y /= -window_height/2.0f;
aux = glm::inverse(proj_matrix) * aux;
glm::vec2 cursor_world = glm::vec2(aux.x, aux.y);
cursor_world = glm::vec2(aux.x, aux.y);
glUseProgram(hex_program);
glUniformMatrix4fv(glGetUniformLocation(hex_program, "model"), 1, GL_FALSE, &model_matrix[0][0]);
glUniformMatrix4fv(glGetUniformLocation(hex_program, "view"), 1, GL_FALSE, &view_matrix[0][0]);
glUniformMatrix4fv(glGetUniformLocation(hex_program, "proj"), 1, GL_FALSE, &proj_matrix[0][0]);
@ -483,12 +591,20 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
{
hovered_hex = i * grid.columns + j;
if (mouse_pressed)
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 1.0f, 0.0f, 1.0f);
{
select(idx, &selection);
}
else
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
if (is_selected(idx, &selection))
{
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 1.0f, 0.0f, 1.0f);
}
else
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), grid.hexes[idx].color.x, grid.hexes[idx].color.y, grid.hexes[idx].color.z, grid.hexes[idx].color.w);
}
@ -505,13 +621,15 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
// text rendering
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glm::mat4 text_proj_matrix = glm::ortho(0.0f, (float)window_width, 0.0f, (float)window_height);
glUniformMatrix4fv(glGetUniformLocation(text_program, "projection"), 1, GL_FALSE, &text_proj_matrix[0][0]);
static char debug_text_buf[256];
{
TIME_BLOCK(text_render);
snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y);
render_text(debug_text_buf, 25, 25, .5f, glm::vec3(1.0f, 1.0f, 1.0f));
for (int i = 0; i < grid.rows * grid.columns; ++i)
@ -533,18 +651,39 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
snprintf(buf, 30, "Cursor INSIDE hex %d", hovered_hex);
render_text(buf, 25.0f, window_height - 25.0f, .5f, glm::vec3(1.0f, 1.0f, 1.0f));
}
}
glDisable(GL_BLEND);
glfwPollEvents();
static int grid_rows = grid.rows;
static int grid_columns = grid.columns;
if (grid_rows != grid.rows || grid_columns != grid.columns)
{
create_grid(&grid, grid_rows, grid_columns);
}
// Start the Dear ImGui frame
{
TIME_BLOCK(gui_render);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Hi there");
ImGui::SliderInt("Rows", &grid_rows, 1, 100);
ImGui::SliderInt("Columns", &grid_columns, 1, 100);
if (ImGui::CollapsingHeader("Debug timers"))
{
for (int i = 0; i < ntimers; ++i)
{
ImGui::Text("%s: %d nsecs", debug_timers[i].name, debug_timers[i].usec);
}
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
glfwSwapBuffers(window);
}
@ -562,3 +701,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
fprintf(stderr, "Hexnando failed with errcode %d: %s\n", main_errcode, errcode_string);
return main_errcode;
}
#define NTIMERS __COUNTER__
int ntimers = NTIMERS;
debug_timer_t debug_timers[NTIMERS];