shithub: libgraphics

Download patch

ref: 139a46c56af04a0b8c2f06769ac77078f5581395
parent: d3b48593b0fee862ea60687336031fd9374ab667
author: rodri <rgl@antares-labs.eu>
date: Sun Aug 11 17:40:53 EDT 2024

fix FPINVAL error. knob for fb clear color. general improvements.

--- a/camera.c
+++ b/camera.c
@@ -211,14 +211,17 @@
 	static Scene *skyboxscene;
 	static Shadertab skyboxshader = { nil, skyboxvs, skyboxfs };
 	Model *mdl;
+	Framebufctl *fbctl;
 	Renderjob *job;
 	uvlong t0, t1;
 
 	assert(c->view != nil && c->rctl != nil && c->scene != nil && s != nil);
 
+	fbctl = c->view->fbctl;
+
 	job = emalloc(sizeof *job);
 	memset(job, 0, sizeof *job);
-	job->fb = c->view->fbctl->getbb(c->view->fbctl);
+	job->fb = fbctl->getbb(fbctl);
 	job->camera = emalloc(sizeof *c);
 	*job->camera = *c;
 	job->scene = dupscene(c->scene);	/* take a snapshot */	
@@ -225,7 +228,7 @@
 	job->shaders = s;
 	job->donec = chancreate(sizeof(void*), 0);
 
-	c->view->fbctl->reset(c->view->fbctl);
+	fbctl->reset(fbctl, c->clearcolor);
 	t0 = nanosec();
 	sendp(c->rctl->c, job);
 	recvp(job->donec);
@@ -250,7 +253,7 @@
 		delscene(job->scene);
 	}
 	t1 = nanosec();
-	c->view->fbctl->swap(c->view->fbctl);
+	fbctl->swap(fbctl);
 
 	updatestats(c, t1-t0);
 	updatetimes(c, job);
--- a/color.c
+++ b/color.c
@@ -140,3 +140,10 @@
 	c.b = _linear2srgb(c.b);
 	return c;
 }
+
+ulong
+rgba2xrgb(ulong c)
+{
+	return (c & 0xFF)<<24|(c>>8 & 0xFF)<<16|
+		(c>>16 & 0xFF)<<8|(c>>24 & 0xFF);
+}
--- a/fb.c
+++ b/fb.c
@@ -222,7 +222,7 @@
 }
 
 static void
-framebufctl_reset(Framebufctl *ctl)
+framebufctl_reset(Framebufctl *ctl, ulong clr)
 {
 	Framebuf *fb;
 
@@ -229,9 +229,9 @@
 	/* address the back buffer—resetting the front buffer is VERBOTEN */
 	fb = ctl->getbb(ctl);
 	resetAbuf(&fb->abuf);
-	memset(fb->nb, 0, Dx(fb->r)*Dy(fb->r)*4);
+	memsetl(fb->nb, 0, Dx(fb->r)*Dy(fb->r));
 	memsetf(fb->zb, Inf(-1), Dx(fb->r)*Dy(fb->r));
-	memset(fb->cb, 0, Dx(fb->r)*Dy(fb->r)*4);
+	memsetl(fb->cb, rgba2xrgb(clr), Dx(fb->r)*Dy(fb->r));
 }
 
 static Framebuf *
--- a/graphics.h
+++ b/graphics.h
@@ -293,7 +293,7 @@
 	void (*upscalememdraw)(Framebufctl*, Memimage*, Point, Point);
 	void (*drawnormals)(Framebufctl*, Image*);
 	void (*swap)(Framebufctl*);
-	void (*reset)(Framebufctl*);
+	void (*reset)(Framebufctl*, ulong);
 	Framebuf *(*getfb)(Framebufctl*);
 	Framebuf *(*getbb)(Framebufctl*);
 };
@@ -325,6 +325,7 @@
 	} clip;
 	Matrix3 proj;		/* VCS to clip space xform */
 	Projection projtype;
+	int clearcolor;
 	int cullmode;
 	int enableblend;
 	int enabledepth;
@@ -418,6 +419,7 @@
 /* color */
 Color srgb2linear(Color);
 Color linear2srgb(Color);
+ulong rgba2xrgb(ulong);
 
 /* shadeop */
 double sign(double);
--- a/render.c
+++ b/render.c
@@ -42,6 +42,9 @@
 	if(blend){
 		dc = srgb2linear(ul2col(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, col2ul(linear2srgb(c)));
 }
@@ -87,9 +90,8 @@
 	buf = &fb->abuf;
 	stk = &buf->stk[p.y*Dx(fb->r) + p.x];
 	stk->items = erealloc(stk->items, ++stk->size*sizeof(*stk->items));
+	memset(&stk->items[stk->size-1], 0, sizeof(*stk->items));
 
-//fprint(2, "stk %#p items %#p size %lud (%d bytes)\n", stk, stk->items, stk->size, sizeof(*stk));
-
 	for(i = 0; i < stk->size; i++)
 		if(z < stk->items[i].z)
 			break;
@@ -107,7 +109,6 @@
 		buf->act = erealloc(buf->act, ++buf->nact*sizeof(*buf->act));
 		buf->act[buf->nact-1] = stk;
 		qunlock(buf);
-//fprint(2, "act %#p nact %lud (%d bytes)\n", buf->act, buf->nact, sizeof(*buf->act));
 	}
 }