From 27378f3537e9dbe0bfeb1ebb8a653e6050f2ea4a Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 8 Feb 2026 18:29:34 -0800 Subject: Implement basic perf counters --- include/swgfx.h | 13 +++++++++++++ src/swgfx.c | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/swgfx.h b/include/swgfx.h index 9c3361d..72164b1 100644 --- a/include/swgfx.h +++ b/include/swgfx.h @@ -22,6 +22,8 @@ Multi-threading: #include #include +#define SWGFX_PROFILING 1 // TODO: Move this to client project cmake. + typedef float R; typedef struct sgVec2i { int x, y; } sgVec2i; @@ -66,6 +68,15 @@ typedef struct sgImage { typedef struct swgfx swgfx; +#if SWGFX_PROFILING +typedef struct sgCounters { + uint64_t frames; // Frames drawn. + uint64_t triangles3; // 3D triangles processed. + uint64_t triangles2; // 2D triangles processed. + uint64_t pixels; // Pixels written. +} sgCounters; +#endif // SWGFX_PROFILING + size_t sgMem(int width, int height); // Get memory requirements. swgfx* sgNew(int width, int height, void* mem); void sgDel(swgfx**); @@ -98,4 +109,6 @@ void sgTrianglesIndexedNonUniform(swgfx*, size_t numTris, const sgTriIdx* tris, void sgGamma (swgfx*, sgPixel*, int width, int height); void sgGammaInv(swgfx*, sgPixel*, int width, int height); +sgCounters sgGetCounters(const swgfx*); + void sgCheck(swgfx*); diff --git a/src/swgfx.c b/src/swgfx.c index bbf9a5b..78d5434 100644 --- a/src/swgfx.c +++ b/src/swgfx.c @@ -19,6 +19,8 @@ Coordinate systems: #include #include + + static constexpr sgVec3 Up3 = (sgVec3){0,1,0}; typedef struct sgViewport_t { int x0, y0, width, height; } sgViewport_t; @@ -50,6 +52,7 @@ typedef struct swgfx { sgTextureFilter textureFilter; // Filter method for the texture. sgImage defaultTexture; // A default for when no texture is provided. sgPixel defaultPixel; // The single-pixel of the default texture. + sgCounters counters; } swgfx; static inline int mod(int a, int m) { return (m + (a % m)) % m; } @@ -306,6 +309,9 @@ static inline R* Depth(swgfx* gfx, int x, int y) { static inline void SetPixel(swgfx* gfx, const sgVec2i p, sgPixel colour) { assert(gfx); *Pixel(gfx, p.x, p.y) = colour; +#if SWGFX_PROFILING + gfx->counters.pixels++; +#endif // SWGFX_PROFILING } static inline void SetDepth(swgfx* gfx, const sgVec2i p, R depth) { @@ -448,6 +454,9 @@ static void DrawTriangle2(swgfx* gfx, const sgTri2* const tri) { } } } +#if SWGFX_PROFILING + gfx->counters.triangles2++; +#endif // SWGFX_PROFILING } static inline sgVec4 PerspDivide(sgVec4 v) { @@ -677,6 +686,9 @@ static void DrawTriangle3(swgfx* gfx, const sgTri3* const tri) { for (int i = 0; i < numTris; ++i) { DrawTriangle3PostClip(gfx, &tris[i]); } +#if SWGFX_PROFILING + gfx->counters.triangles3++; +#endif // SWGFX_PROFILING } #define is_pow2_or_0(X) ((X & (X - 1)) == 0) @@ -768,6 +780,10 @@ void sgPresent(swgfx* gfx, sgVec2i dimensions, sgScreenPixel* screen) { } } } + +#if SWGFX_PROFILING + gfx->counters.frames++; +#endif // SWGFX_PROFILING } static void sgUpdateViewProjection(swgfx* gfx) { @@ -930,6 +946,11 @@ static bool ViewportWithinBuffer(swgfx* gfx) { ((vp.y0 + vp.height) <= gfx->dims.y); } +sgCounters sgGetCounters(const swgfx* gfx) { + assert(gfx); + return gfx->counters; +} + void sgCheck(swgfx* gfx) { assert(gfx); assert(ViewportWithinBuffer(gfx)); -- cgit v1.2.3