shithub: tinyrend

Download patch

ref: b34446e7025a15bd5b74fd5f88bb952028cc8a4a
parent: f69cd8f04bbe059ce21f0c7be93fd081495c6371
author: rodri <rgl@antares-labs.eu>
date: Fri Dec 22 19:12:34 EST 2023

organize the code better.

--- /dev/null
+++ b/alloc.c
@@ -1,0 +1,64 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <geometry.h>
+#include "libobj/obj.h"
+#include "dat.h"
+#include "fns.h"
+
+void *
+emalloc(ulong n)
+{
+	void *p;
+
+	p = malloc(n);
+	if(p == nil)
+		sysfatal("malloc: %r");
+	setmalloctag(p, getcallerpc(&n));
+	return p;
+}
+
+void *
+erealloc(void *p, ulong n)
+{
+	void *np;
+
+	np = realloc(p, n);
+	if(np == nil){
+		if(n == 0)
+			return nil;
+		sysfatal("realloc: %r");
+	}
+	if(p == nil)
+		setmalloctag(np, getcallerpc(&p));
+	else
+		setrealloctag(np, getcallerpc(&p));
+	return np;
+}
+
+Image *
+eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col)
+{
+	Image *i;
+
+	i = allocimage(d, r, chan, repl, col);
+	if(i == nil)
+		sysfatal("allocimage: %r");
+	return i;
+}
+
+Memimage *
+eallocmemimage(Rectangle r, ulong chan)
+{
+	Memimage *i;
+
+	i = allocmemimage(r, chan);
+	if(i == nil)
+		sysfatal("allocmemimage: %r");
+	memfillcolor(i, DTransparent);
+	return i;
+}
--- /dev/null
+++ b/dat.h
@@ -1,0 +1,75 @@
+typedef Point Triangle[3];
+typedef struct VSparams VSparams;
+typedef struct FSparams FSparams;
+typedef struct SUparams SUparams;
+typedef struct Shader Shader;
+typedef struct Framebuf Framebuf;
+typedef struct Framebufctl Framebufctl;
+
+/* shader params */
+struct VSparams
+{
+	SUparams *su;
+	Point3 *p;
+	Point3 *n;
+	uint idx;
+};
+
+struct FSparams
+{
+	SUparams *su;
+	Memimage *frag;
+	Point p;
+	Point3 bc;
+	uchar *cbuf;
+};
+
+/* shader unit params */
+struct SUparams
+{
+	Framebuf *fb;
+	OBJElem **b, **e;
+	int id;
+	Channel *donec;
+
+	double var_intensity[3];
+
+	uvlong uni_time;
+
+	Point3 (*vshader)(VSparams*);
+	Memimage *(*fshader)(FSparams*);
+};
+
+struct Shader
+{
+	char *name;
+	Point3 (*vshader)(VSparams*);
+	Memimage *(*fshader)(FSparams*);
+};
+
+struct Framebuf
+{
+	Memimage *cb;
+	Memimage *zb;
+	double *zbuf;
+	Lock zbuflk;
+	Memimage *nb;	/* XXX DBG */
+	Rectangle r;
+};
+
+struct Framebufctl
+{
+	Framebuf *fb[2];
+	uint idx;
+	Lock swplk;
+
+	void (*draw)(Framebufctl*, Memimage*, int);
+	void (*swap)(Framebufctl*);
+	void (*reset)(Framebufctl*);
+};
+
+typedef struct Stats Stats;
+struct Stats
+{
+	uvlong min, avg, max, acc, n, v;
+};
--- /dev/null
+++ b/fb.c
@@ -1,0 +1,76 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <geometry.h>
+#include "libobj/obj.h"
+#include "dat.h"
+#include "fns.h"
+
+extern int shownormals;	/* XXX DBG */
+
+static void
+framebufctl_draw(Framebufctl *ctl, Memimage *dst, int showz)
+{
+	lock(&ctl->swplk);
+	memimagedraw(dst, dst->r, showz? ctl->fb[ctl->idx]->zb: ctl->fb[ctl->idx]->cb, ZP, nil, ZP, SoverD);
+	/* XXX DBG */
+	if(shownormals)
+		memimagedraw(dst, dst->r, ctl->fb[ctl->idx]->nb, ZP, nil, ZP, SoverD);
+	unlock(&ctl->swplk);
+}
+
+static void
+framebufctl_swap(Framebufctl *ctl)
+{
+	lock(&ctl->swplk);
+	ctl->idx ^= 1;
+	unlock(&ctl->swplk);
+}
+
+static void
+framebufctl_reset(Framebufctl *ctl)
+{
+	Framebuf *fb;
+
+	/* address the back buffer—resetting the front buffer is VERBOTEN */
+	fb = ctl->fb[ctl->idx^1];
+	memsetd(fb->zbuf, Inf(-1), Dx(fb->r)*Dy(fb->r));
+	memfillcolor(fb->cb, DTransparent);
+	memfillcolor(fb->zb, DTransparent);
+	memfillcolor(fb->nb, DTransparent);	/* XXX DBG */
+}
+
+Framebuf *
+mkfb(Rectangle r)
+{
+	Framebuf *fb;
+
+	fb = emalloc(sizeof *fb);
+	fb->cb = eallocmemimage(r, RGBA32);
+	fb->zb = eallocmemimage(r, RGBA32);
+	fb->zbuf = emalloc(Dx(r)*Dy(r)*sizeof(*fb->zbuf));
+	memsetd(fb->zbuf, Inf(-1), Dx(r)*Dy(r));
+	memset(&fb->zbuflk, 0, sizeof(fb->zbuflk));
+	fb->nb = eallocmemimage(r, RGBA32);	/* XXX DBG */
+	fb->r = r;
+	return fb;
+}
+
+Framebufctl *
+newfbctl(Rectangle r)
+{
+	Framebufctl *fc;
+
+	fc = emalloc(sizeof *fc);
+	memset(fc, 0, sizeof *fc);
+	fc->fb[0] = mkfb(r);
+	fc->fb[1] = mkfb(r);
+	fc->draw = framebufctl_draw;
+	fc->swap = framebufctl_swap;
+	fc->reset = framebufctl_reset;
+	return fc;
+}
--- /dev/null
+++ b/fns.h
@@ -1,0 +1,32 @@
+#define HZ2MS(hz)	(1000/(hz))
+
+void resized(void);
+
+/* nanosec */
+uvlong nanosec(void);
+
+/* alloc */
+void *emalloc(ulong);
+void *erealloc(void*, ulong);
+Image *eallocimage(Display*, Rectangle, ulong, int, ulong);
+Memimage *eallocmemimage(Rectangle, ulong);
+
+/* fb */
+Framebuf *mkfb(Rectangle);
+Framebufctl *newfbctl(Rectangle);
+
+/* shadeop */
+double step(double, double);
+double smoothstep(double, double, double);
+
+/* util */
+int min(int, int);
+int max(int, int);
+double fmin(double, double);
+double fmax(double, double);
+void swap(int*, int*);
+void swappt2(Point2*, Point2*);
+void swappt3(Point3*, Point3*);
+void memsetd(double*, double, usize);
+Memimage *readtga(char*);
+Memimage *rgb(ulong);
--- a/main.c
+++ b/main.c
@@ -1,6 +1,5 @@
 #include <u.h>
 #include <libc.h>
-#include <bio.h>
 #include <thread.h>
 #include <draw.h>
 #include <memdraw.h>
@@ -8,86 +7,9 @@
 #include <keyboard.h>
 #include <geometry.h>
 #include "libobj/obj.h"
+#include "dat.h"
+#include "fns.h"
 
-#define HZ2MS(hz)	(1000/(hz))
-
-typedef Point Triangle[3];
-typedef struct VSparams VSparams;
-typedef struct FSparams FSparams;
-typedef struct SUparams SUparams;
-typedef struct Shader Shader;
-typedef struct Framebuf Framebuf;
-typedef struct Framebufctl Framebufctl;
-
-/* shader params */
-struct VSparams
-{
-	SUparams *su;
-	Point3 *p;
-	Point3 *n;
-	uint idx;
-};
-
-struct FSparams
-{
-	SUparams *su;
-	Memimage *frag;
-	Point p;
-	Point3 bc;
-	uchar *cbuf;
-};
-
-/* shader unit params */
-struct SUparams
-{
-	Framebuf *fb;
-	OBJElem **b, **e;
-	int id;
-	Channel *donec;
-
-	double var_intensity[3];
-
-	uvlong uni_time;
-
-	Point3 (*vshader)(VSparams*);
-	Memimage *(*fshader)(FSparams*);
-};
-
-struct Shader
-{
-	char *name;
-	Point3 (*vshader)(VSparams*);
-	Memimage *(*fshader)(FSparams*);
-};
-
-struct Framebuf
-{
-	Memimage *cb;
-	Memimage *zb;
-	double *zbuf;
-	Lock zbuflk;
-	Memimage *nb;	/* XXX DBG */
-	Rectangle r;
-};
-
-struct Framebufctl
-{
-	Framebuf *fb[2];
-	uint idx;
-	Lock swplk;
-
-	void (*draw)(Framebufctl*, Memimage*, int);
-	void (*swap)(Framebufctl*);
-	void (*reset)(Framebufctl*);
-};
-
-typedef struct Stats Stats;
-struct Stats
-{
-	uvlong min, avg, max, acc, n, v;
-};
-
-
 Stats fps;
 Framebufctl *fbctl;
 Memimage *screenfb;
@@ -97,7 +19,7 @@
 Channel *drawc;
 int nprocs;
 int showzbuffer;
-int shownormals;
+int shownormals;	/* XXX DBG */
 
 char winspec[32];
 Point3 light = {0,-1,1,1};	/* global directional light */
@@ -108,73 +30,7 @@
 double θ, ω;
 double scale;
 
-void resized(void);
-uvlong nanosec(void);
-
-int
-min(int a, int b)
-{
-	return a < b? a: b;
-}
-
-int
-max(int a, int b)
-{
-	return a > b? a: b;
-}
-
-double
-fmin(double a, double b)
-{
-	return a < b? a: b;
-}
-
-double
-fmax(double a, double b)
-{
-	return a > b? a: b;
-}
-
 void
-swap(int *a, int *b)
-{
-	int t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
-void
-swappt2(Point2 *a, Point2 *b)
-{
-	Point2 t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
-void
-swappt3(Point3 *a, Point3 *b)
-{
-	Point3 t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
-void
-memsetd(double *p, double v, usize len)
-{
-	double *dp;
-
-	for(dp = p; dp < p+len; dp++)
-		*dp = v;
-}
-
-void
 updatestats(Stats *s, uvlong v)
 {
 	s->v = v;
@@ -185,190 +41,6 @@
 	s->max = v > s->max || s->n == 1? v: s->max;
 }
 
-double
-step(double edge, double n)
-{
-	if(n < edge)
-		return 0;
-	return 1;
-}
-
-double
-smoothstep(double edge0, double edge1, double n)
-{
-	double t;
-
-	t = fclamp((n-edge0)/(edge1-edge0), 0, 1);
-	return t*t * (3 - 2*t);
-}
-
-void *
-emalloc(ulong n)
-{
-	void *p;
-
-	p = malloc(n);
-	if(p == nil)
-		sysfatal("malloc: %r");
-	setmalloctag(p, getcallerpc(&n));
-	return p;
-}
-
-void *
-erealloc(void *p, ulong n)
-{
-	void *np;
-
-	np = realloc(p, n);
-	if(np == nil){
-		if(n == 0)
-			return nil;
-		sysfatal("realloc: %r");
-	}
-	if(p == nil)
-		setmalloctag(np, getcallerpc(&p));
-	else
-		setrealloctag(np, getcallerpc(&p));
-	return np;
-}
-
-Image *
-eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col)
-{
-	Image *i;
-
-	i = allocimage(d, r, chan, repl, col);
-	if(i == nil)
-		sysfatal("allocimage: %r");
-	return i;
-}
-
-Memimage *
-eallocmemimage(Rectangle r, ulong chan)
-{
-	Memimage *i;
-
-	i = allocmemimage(r, chan);
-	if(i == nil)
-		sysfatal("allocmemimage: %r");
-	memfillcolor(i, DTransparent);
-	return i;
-}
-
-static void
-framebufctl_draw(Framebufctl *ctl, Memimage *dst, int showz)
-{
-	lock(&ctl->swplk);
-	memimagedraw(dst, dst->r, showz? ctl->fb[ctl->idx]->zb: ctl->fb[ctl->idx]->cb, ZP, nil, ZP, SoverD);
-	/* XXX DBG */
-	if(shownormals)
-		memimagedraw(dst, dst->r, ctl->fb[ctl->idx]->nb, ZP, nil, ZP, SoverD);
-	unlock(&ctl->swplk);
-}
-
-static void
-framebufctl_swap(Framebufctl *ctl)
-{
-	lock(&ctl->swplk);
-	ctl->idx ^= 1;
-	unlock(&ctl->swplk);
-}
-
-static void
-framebufctl_reset(Framebufctl *ctl)
-{
-	Framebuf *fb;
-
-	/* address the back buffer—resetting the front buffer is VERBOTEN */
-	fb = ctl->fb[ctl->idx^1];
-	memsetd(fb->zbuf, Inf(-1), Dx(fb->r)*Dy(fb->r));
-	memfillcolor(fb->cb, DTransparent);
-	memfillcolor(fb->zb, DTransparent);
-	memfillcolor(fb->nb, DTransparent);	/* XXX DBG */
-}
-
-Framebuf *
-mkfb(Rectangle r)
-{
-	Framebuf *fb;
-
-	fb = emalloc(sizeof *fb);
-	fb->cb = eallocmemimage(r, RGBA32);
-	fb->zb = eallocmemimage(r, RGBA32);
-	fb->zbuf = emalloc(Dx(r)*Dy(r)*sizeof(*fb->zbuf));
-	memsetd(fb->zbuf, Inf(-1), Dx(r)*Dy(r));
-	memset(&fb->zbuflk, 0, sizeof(fb->zbuflk));
-	fb->nb = eallocmemimage(r, RGBA32);	/* XXX DBG */
-	fb->r = r;
-	return fb;
-}
-
-Framebufctl *
-newfbctl(Rectangle r)
-{
-	Framebufctl *fc;
-
-	fc = emalloc(sizeof *fc);
-	memset(fc, 0, sizeof *fc);
-	fc->fb[0] = mkfb(r);
-	fc->fb[1] = mkfb(r);
-	fc->draw = framebufctl_draw;
-	fc->swap = framebufctl_swap;
-	fc->reset = framebufctl_reset;
-	return fc;
-}
-
-static void
-decproc(void *arg)
-{
-	int fd, *pfd;
-
-	pfd = arg;
-	fd = pfd[2];
-
-	close(pfd[0]);
-	dup(fd, 0);
-	close(fd);
-	dup(pfd[1], 1);
-	close(pfd[1]);
-
-	execl("/bin/tga", "tga", "-9t", nil);
-	threadexitsall("execl: %r");
-}
-
-Memimage *
-readtga(char *path)
-{
-	Memimage *i;
-	int fd, pfd[3];
-
-	if(pipe(pfd) < 0)
-		sysfatal("pipe: %r");
-	fd = open(path, OREAD);
-	if(fd < 0)
-		sysfatal("open: %r");
-	pfd[2] = fd;
-	procrfork(decproc, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
-	close(pfd[1]);
-	i = readmemimage(pfd[0]);
-	close(pfd[0]);
-	close(fd);
-
-	return i;
-}
-
-Memimage *
-rgb(ulong c)
-{
-	Memimage *i;
-
-	i = eallocmemimage(Rect(0,0,1,1), screen->chan);
-	i->flags |= Frepl;
-	i->clipr = Rect(-1e6, -1e6, 1e6, 1e6);
-	memfillcolor(i, c);
-	return i;
-}
-
 void
 pixel(Memimage *dst, Point p, Memimage *src)
 {
@@ -978,7 +650,6 @@
 		fbctl->swap(fbctl);
 		nbsendp(drawc, nil);
 	}
-	threadexits(nil);
 }
 
 static char *
--- a/mkfile
+++ b/mkfile
@@ -5,9 +5,15 @@
 OFILES=\
 	main.$O\
 	nanosec.$O\
+	alloc.$O\
+	fb.$O\
+	shadeop.$O\
+	util.$O\
 
 LIB=\
 	libobj/libobj.a$O\
+
+HFILES=dat.h fns.h
 
 </sys/src/cmd/mkone
 
--- /dev/null
+++ b/shadeop.c
@@ -1,0 +1,28 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <geometry.h>
+#include "libobj/obj.h"
+#include "dat.h"
+#include "fns.h"
+
+double
+step(double edge, double n)
+{
+	if(n < edge)
+		return 0;
+	return 1;
+}
+
+double
+smoothstep(double edge0, double edge1, double n)
+{
+	double t;
+
+	t = fclamp((n-edge0)/(edge1-edge0), 0, 1);
+	return t*t * (3 - 2*t);
+}
--- /dev/null
+++ b/util.c
@@ -1,0 +1,125 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <geometry.h>
+#include "libobj/obj.h"
+#include "dat.h"
+#include "fns.h"
+
+int
+min(int a, int b)
+{
+	return a < b? a: b;
+}
+
+int
+max(int a, int b)
+{
+	return a > b? a: b;
+}
+
+double
+fmin(double a, double b)
+{
+	return a < b? a: b;
+}
+
+double
+fmax(double a, double b)
+{
+	return a > b? a: b;
+}
+
+void
+swap(int *a, int *b)
+{
+	int t;
+
+	t = *a;
+	*a = *b;
+	*b = t;
+}
+
+void
+swappt2(Point2 *a, Point2 *b)
+{
+	Point2 t;
+
+	t = *a;
+	*a = *b;
+	*b = t;
+}
+
+void
+swappt3(Point3 *a, Point3 *b)
+{
+	Point3 t;
+
+	t = *a;
+	*a = *b;
+	*b = t;
+}
+
+void
+memsetd(double *p, double v, usize len)
+{
+	double *dp;
+
+	for(dp = p; dp < p+len; dp++)
+		*dp = v;
+}
+
+static void
+decproc(void *arg)
+{
+	int fd, *pfd;
+
+	pfd = arg;
+	fd = pfd[2];
+
+	close(pfd[0]);
+	dup(fd, 0);
+	close(fd);
+	dup(pfd[1], 1);
+	close(pfd[1]);
+
+	execl("/bin/tga", "tga", "-9t", nil);
+	threadexitsall("execl: %r");
+}
+
+Memimage *
+readtga(char *path)
+{
+	Memimage *i;
+	int fd, pfd[3];
+
+	if(pipe(pfd) < 0)
+		sysfatal("pipe: %r");
+	fd = open(path, OREAD);
+	if(fd < 0)
+		sysfatal("open: %r");
+	pfd[2] = fd;
+	procrfork(decproc, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
+	close(pfd[1]);
+	i = readmemimage(pfd[0]);
+	close(pfd[0]);
+	close(fd);
+
+	return i;
+}
+
+Memimage *
+rgb(ulong c)
+{
+	Memimage *i;
+
+	i = eallocmemimage(Rect(0,0,1,1), screen->chan);
+	i->flags |= Frepl;
+	i->clipr = Rect(-1e6, -1e6, 1e6, 1e6);
+	memfillcolor(i, c);
+	return i;
+}