shithub: libgraphics

Download patch

ref: 76a156b76482bd1335d2838be1c87db85be76246
parent: 19f4448a73daebe4a5218639839d5982ad1e7a44
author: rodri <rgl@antares-labs.eu>
date: Mon Feb 16 17:02:39 EST 2026

viewport: measure draw time and keep statistics

--- a/graphics.h
+++ b/graphics.h
@@ -165,8 +165,8 @@
 	Point3 n;		/* surface normal */
 	Point2 uv;		/* texture coordinate */
 	Color c;		/* shading color */
-	Material *mtl;
 	Point3 tangent;
+	Material *mtl;
 	Vertexattrs;		/* attributes (varyings) */
 };
 
@@ -343,7 +343,7 @@
 {
 	Rectangle r;
 	Rectangle clipr;
-	Raster *rasters;	/* [0] color, [1] depth, [n] user-defined */
+	Raster *rasters;	/* [0] color, [1] depth, [2..n] user-defined */
 	Abuf abuf;		/* A-buffer */
 
 	void (*createraster)(Framebuf*, char*, ulong);
@@ -372,6 +372,11 @@
 	RFrame;
 	Framebufctl *fbctl;
 	Rectangle r;
+
+	struct {
+		uvlong min, avg, max, acc, n, v;
+		uvlong nframes;
+	} stats;
 
 	void (*draw)(Viewport*, Image*, char*);
 	void (*memdraw)(Viewport*, Memimage*, char*);
--- a/render.c
+++ b/render.c
@@ -81,13 +81,13 @@
 	Color dc;
 
 	if(blend){
-		dc = srgb2linear(ul2col(getpixel(fb, p)));
+		dc = ul2col(srgb2linearul(getpixel(fb, p)));
 		c = lerp3(dc, c, c.a);	/* SoverD */
 //		c = addpt3(mulpt3(dc, 1), mulpt3(c, 1-c.a));
 //		c = subpt3(Vec3(1,1,1), subpt3(dc, c));
 //		c = subpt3(addpt3(dc, c), Vec3(1,1,1));
 	}
-	putpixel(fb, p, mulalpha(col2ul(linear2srgb(c))));
+	putpixel(fb, p, mulalpha(linear2srgbul(col2ul(c))));
 }
 
 static int
--- a/viewport.c
+++ b/viewport.c
@@ -8,9 +8,22 @@
 #include "internal.h"
 
 static void
+updatestats(Viewport *c, uvlong v)
+{
+	c->stats.v = v;
+	c->stats.n++;
+	c->stats.acc += v;
+	c->stats.avg = c->stats.acc/c->stats.n;
+	c->stats.min = v < c->stats.min || c->stats.n == 1? v: c->stats.min;
+	c->stats.max = v > c->stats.max || c->stats.n == 1? v: c->stats.max;
+	c->stats.nframes++;
+}
+
+static void
 viewport_draw(Viewport *v, Image *dst, char *rname)
 {
 	Point off, scale;
+	uvlong t0, t1;
 
 	off = Pt(v->p.x, v->p.y);
 	/* no downsampling support yet */
@@ -17,7 +30,10 @@
 	scale.x = max(v->bx.x, 1);
 	scale.y = max(v->by.y, 1);
 
+	t0 = nanosec();
 	v->fbctl->draw(v->fbctl, dst, rname, off, scale);
+	t1 = nanosec();
+	updatestats(v, t1-t0);
 }
 
 static void
@@ -89,6 +105,7 @@
 	}
 
 	v = _emalloc(sizeof *v);
+	memset(&v->stats, 0, sizeof v->stats);
 	v->p = Pt2(0,0,1);
 	v->bx = Vec2(1,0);
 	v->by = Vec2(0,1);
--