add counters for draw calls, vertices, trianagles

Sun, 01 Feb 2026 13:52:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Feb 2026 13:52:50 +0100
changeset 298
fa91e2e06eee
parent 297
02ce7c6251a9
child 299
c0d3a1acc6ae

add counters for draw calls, vertices, trianagles

demo/snake/snake.c file | annotate | diff | comparison | revisions
src/ascension/context.h file | annotate | diff | comparison | revisions
src/ascension/glcontext.h file | annotate | diff | comparison | revisions
src/ascension/text.h file | annotate | diff | comparison | revisions
src/mesh.c file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
--- a/demo/snake/snake.c	Sun Jan 25 19:45:24 2026 +0100
+++ b/demo/snake/snake.c	Sun Feb 01 13:52:50 2026 +0100
@@ -174,7 +174,15 @@
     static float last_fps = 0.f;
     if (fabsf(asc_context.frame_rate - last_fps) > 1) {
         last_fps = asc_context.frame_rate;
-        asc_text_printf(node, "%.2f FPS", asc_context.frame_rate);
+        asc_text_printf(node,
+            "%.2f FPS\n"
+            "%llu draw calls\n"
+            "%llu triangles\n"
+            "%llu vertices",
+            asc_context.frame_rate,
+            asc_active_glctx->counters.draw_calls,
+            asc_active_glctx->counters.triangles_rendered,
+            asc_active_glctx->counters.vertices_rendered);
     }
 }
 
--- a/src/ascension/context.h	Sun Jan 25 19:45:24 2026 +0100
+++ b/src/ascension/context.h	Sun Feb 01 13:52:50 2026 +0100
@@ -80,6 +80,7 @@
 #define asc_active_window asc_active_window_assert()
 AscWindow *asc_active_window_assert(void);
 #endif // NDEBUG
+#define asc_active_glctx (&asc_active_window->glctx)
 
 void asc_context_initialize(void);
 void asc_context_destroy(void);
--- a/src/ascension/glcontext.h	Sun Jan 25 19:45:24 2026 +0100
+++ b/src/ascension/glcontext.h	Sun Feb 01 13:52:50 2026 +0100
@@ -32,6 +32,7 @@
 
 #include <cx/list.h>
 
+// TODO: we might want to get rid of this struct
 typedef struct asc_gl_context_settings_s {
     int gl_major_version;
     int gl_minor_version;
@@ -40,6 +41,12 @@
     bool fullscreen;
 } AscGLContextSettings;
 
+typedef struct asc_gl_counters_s {
+    unsigned long long draw_calls;
+    unsigned long long vertices_rendered;
+    unsigned long long triangles_rendered;
+} AscGLCounters;
+
 typedef struct asc_gl_context_s {
     SDL_Window *window;
     SDL_GLContext glctx;
@@ -52,11 +59,19 @@
      * List of pointers to AscShaderProgram.
      */
     CxList *shaders;
+    /**
+     * Some counters for draw calls, rendered vertices, etc.
+     * Use this to output the information for the last frame.
+     */
+    AscGLCounters counters;
+    /**
+     * Used internally to count the stuff.
+     * Will be synced to the @c counters structure at the end of the frame.
+     */
+    AscGLCounters ctr;
     unsigned active_program;
 } AscGLContext;
 
-#define asc_active_glctx (&asc_active_window->glctx)
-
 AscGLContextSettings asc_gl_context_settings_default(int gl_major_version, int gl_minor_version);
 
 bool asc_gl_context_initialize(
--- a/src/ascension/text.h	Sun Jan 25 19:45:24 2026 +0100
+++ b/src/ascension/text.h	Sun Feb 01 13:52:50 2026 +0100
@@ -139,6 +139,9 @@
  * @param format the format string
  * @param ... the format arguments
  */
+#ifdef __GNUC__
+__attribute__((format(printf, 2, 3)))
+#endif
 void asc_text_printf(
         AscText *node,
         const char *format,
--- a/src/mesh.c	Sun Jan 25 19:45:24 2026 +0100
+++ b/src/mesh.c	Sun Feb 01 13:52:50 2026 +0100
@@ -25,6 +25,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include "ascension/context.h"
 #include "ascension/error.h"
 #include "ascension/mesh.h"
 
@@ -65,8 +66,12 @@
 }
 
 void asc_mesh_draw_triangle_strip(const AscMesh *mesh) {
+    AscGLCounters *counters = &asc_active_glctx->ctr;
+    counters->draw_calls++;
+    counters->vertices_rendered += mesh->vtx_count;
+    counters->triangles_rendered += mesh->vtx_count - 2;
     glBindVertexArray(mesh->vao);
-    glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count);
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei) mesh->vtx_count);
     asc_error_catch_gl("Drawing mesh");
 #ifndef NDEBUG
     // only unbind in debug mode to detect accidental re-use of the wrong VAO
--- a/src/window.c	Sun Jan 25 19:45:24 2026 +0100
+++ b/src/window.c	Sun Feb 01 13:52:50 2026 +0100
@@ -146,9 +146,13 @@
     // necessary safeguard
     if (window->id == 0) return;
 
-    // activate the window that shall be synced temporarily
+    // activate the window that shall be synced
     unsigned int active_index = asc_window_activate(index);
 
+    // copy and reset the counters
+    memcpy(&asc_active_glctx->counters, &asc_active_glctx->ctr, sizeof(AscGLCounters));
+    memset(&asc_active_glctx->ctr, 0, sizeof(AscGLCounters));
+
     // Clear the color buffer for the window frame
     glViewport(0, 0,
         (GLsizei) window->rect.size.width,

mercurial