shithub: libgraphics

Download patch

ref: 534e1bec9497b97b4de91b58e078206eff061a7c
parent: 24f4f51732b775cb78a7fedd06d23af24d3a309c
author: rodri <rgl@antares-labs.eu>
date: Wed Feb 18 14:40:12 EST 2026

clip: make Polygons hold a static amount of vertices

this speeds up clipping considerably.

arrived at 8 empirically, but the geometry tells
us that the maximum number of vertices you can end
up with in 2-d space is 7 (when a vertex of the
triangle clips a corner of the frustum, and the
other two clip independent planes); if you then
clip the result with another plane in 3-space
(near or far) you can get at most 8 vertices.

maybe i should make it 9, for cases where the near
plane is very close to the far one, or for scenes
with big triangles that could clip both planes.

i had to increase the tiler proc stack size
because the polygons blew up big time.

--- a/clip.c
+++ b/clip.c
@@ -31,9 +31,7 @@
 static int
 addvert(Polygon *p, BVertex v)
 {
-	if(++p->n > p->cap)
-		p->v = _erealloc(p->v, (p->cap = p->n)*sizeof(*p->v));
-	p->v[p->n-1] = v;
+	p->v[p->n++] = v;
 	return p->n;
 }
 
@@ -43,7 +41,7 @@
 	int i;
 
 	for(i = 0; i < p->n; i++)
-		fprint(fd, "%d/%lud p %V\n", i, p->n, p->v[i].p);
+		fprint(fd, "%d n %lud p %V\n", i, p->n, p->v[i].p);
 }
 
 /*
@@ -129,8 +127,6 @@
 			break;
 		}
 	}
-	free(Vout->v);
-	free(Vin->v);
 
 	return np;
 }
--- a/internal.h
+++ b/internal.h
@@ -26,9 +26,8 @@
 
 struct Polygon
 {
-	BVertex *v;
+	BVertex v[8];
 	ulong n;
-	ulong cap;
 };
 
 /* common task params */
--- a/render.c
+++ b/render.c
@@ -7,6 +7,9 @@
 #include "graphics.h"
 #include "internal.h"
 
+#define TILESTKSZ	(32*1024)
+#define PROCSTKSZ	(8*1024)
+
 static Vertexattr *
 sparams_getuniform(Shaderparams *sp, char *id)
 {
@@ -763,13 +766,13 @@
 		tp->taskc = ttaskchans[i];
 		tp->taskchans = rtaskchans;
 		tp->nproc = nproc;
-		proccreate(tiler, tp, mainstacksize);
+		proccreate(tiler, tp, TILESTKSZ);
 	}
 	for(i = 0; i < nproc; i++){
 		rp = _emalloc(sizeof *rp);
 		rp->id = i;
 		rp->taskc = rtaskchans[i] = chancreate(sizeof(Rastertask), 2048);
-		proccreate(rasterizer, rp, mainstacksize);
+		proccreate(rasterizer, rp, PROCSTKSZ);
 	}
 
 	while(recv(ep->taskc, &task) > 0){
@@ -843,7 +846,7 @@
 	ep = _emalloc(sizeof *ep);
 	ep->rctl = rctl;
 	ep->taskc = chancreate(sizeof(Entitytask), 256);
-	proccreate(entityproc, ep, mainstacksize);
+	proccreate(entityproc, ep, PROCSTKSZ);
 
 	while((job = recvp(rctl->jobq)) != nil){
 		if(job->rctl->doprof)
@@ -891,6 +894,6 @@
 	memset(r, 0, sizeof *r);
 	r->jobq = chancreate(sizeof(Renderjob*), 8);
 	r->nprocs = nproc;
-	proccreate(renderer, r, mainstacksize);
+	proccreate(renderer, r, PROCSTKSZ);
 	return r;
 }
--