shithub: libgraphics

Download patch

ref: b6a336aff26fbc94e7803719a8aeb8fa29eddb9b
parent: 453d100ac7734cd64652aa4d3a0685e3494428f7
author: rodri <rgl@antares-labs.eu>
date: Tue Oct 1 16:21:13 EDT 2024

implement a uniforms interface through Shadertab.

--- a/camera.c
+++ b/camera.c
@@ -16,7 +16,7 @@
 {
 	Point3 p;
 
-	addvattr(sp->v, "dir", VAPoint, &sp->v->p);
+	sp->setattr(sp, "dir", VAPoint, &sp->v->p);
 	/* only rotate along with the camera */
 	p = sp->v->p;
 	p.w = 0; p = world2vcs(sp->su->camera, p);
@@ -32,7 +32,7 @@
 	Vertexattr *va;
 	Color c;
 
-	va = getvattr(sp->v, "dir");
+	va = sp->getattr(sp, "dir");
 	c = samplecubemap(sp->su->camera->scene->skybox, va->p, neartexsampler);
 	return c;
 }
--- a/graphics.h
+++ b/graphics.h
@@ -51,6 +51,7 @@
 typedef struct Texture Texture;
 typedef struct Cubemap Cubemap;
 typedef struct Vertexattr Vertexattr;
+typedef struct Vertexattrs Vertexattrs;
 typedef struct Vertex Vertex;
 typedef struct LightSource LightSource;
 typedef struct Material Material;
@@ -123,6 +124,12 @@
 	};
 };
 
+struct Vertexattrs
+{
+	Vertexattr *attrs;
+	ulong nattrs;
+};
+
 struct Vertex
 {
 	Point3 p;		/* position */
@@ -131,10 +138,7 @@
 	Point2 uv;		/* texture coordinate */
 	Material *mtl;
 	Point3 tangent;
-
-	/* TODO it'd be neat to use a dynamic hash table instead */
-	Vertexattr *attrs;	/* attributes (aka varyings) */
-	ulong nattrs;
+	Vertexattrs;		/* attributes (varyings) */
 };
 
 struct LightSource
@@ -219,15 +223,11 @@
 struct SUparams
 {
 	Framebuf *fb;
+	Shadertab *stab;
 	Renderjob *job;
 	Camera *camera;
 	Entity *entity;
 	Primitive *eb, *ee;
-
-	uvlong uni_time;
-
-	Point3 (*vshader)(Shaderparams*);
-	Color (*fshader)(Shaderparams*);
 };
 
 struct Shadertab
@@ -235,6 +235,7 @@
 	char *name;
 	Point3 (*vshader)(Shaderparams*);	/* vertex shader */
 	Color (*fshader)(Shaderparams*);	/* fragment shader */
+	Vertexattrs;				/* uniforms */
 };
 
 struct Rendertime
@@ -387,6 +388,7 @@
 
 /* render */
 Renderer *initgraphics(void);
+void setuniform(Shadertab*, char*, int, void*);
 
 /* xform */
 Point3 model2world(Entity*, Point3);
@@ -420,10 +422,6 @@
 Scene *dupscene(Scene*);
 void delscene(Scene*);
 void clearscene(Scene*);
-
-/* vertex */
-void addvattr(Vertex*, char*, int, void*);
-Vertexattr *getvattr(Vertex*, char*);
 
 /* texture */
 Texture *alloctexture(int, Memimage*);
--- a/internal.h
+++ b/internal.h
@@ -73,6 +73,8 @@
 void berpvertex(Vertex*, Vertex*, Vertex*, Vertex*, Point3);
 void delvattrs(Vertex*);
 void fprintvattrs(int, Vertex*);
+void addvattr(Vertexattrs*, char*, int, void*);
+Vertexattr *getvattr(Vertexattrs*, char*);
 
 /* clip */
 int clipprimitive(Primitive*, Primitive*);
--- a/model.6.txt
+++ b/model.6.txt
@@ -28,6 +28,7 @@
 T	tangent
 P	primitive
 mtl	material definition
+	* name can contain spaces if quoted (see quote(2))
 	* ambient, diffuse and specular parameters take colors in linear RGB space
 	* diffusemap assumes the image colors are gamma-corrected (sRGBTexture)
 	* specularmap and normals both assume image contents are linear (RAWTexture)
--- a/render.c
+++ b/render.c
@@ -12,10 +12,15 @@
 static Vertexattr *
 sparams_getuniform(Shaderparams *sp, char *id)
 {
-	USED(sp, id);
-	return nil;
+	return getvattr(sp->su->stab, id);
 }
 
+void
+setuniform(Shadertab *st, char *id, int type, void *val)
+{
+	addvattr(st, id, type, val);
+}
+
 static Vertexattr *
 sparams_getattr(Shaderparams *sp, char *id)
 {
@@ -202,7 +207,7 @@
 
 		fsp.v = &prim->v[0];
 		fsp.p = p;
-		c = params->fshader(&fsp);
+		c = params->stab->fshader(&fsp);
 		if(c.a == 0)			/* discard non-colors */
 			break;
 		if(ropts & RODepth)
@@ -268,7 +273,7 @@
 			lerpvertex(fsp.v, &prim->v[0], &prim->v[1], perc);
 
 			fsp.p = p;
-			c = params->fshader(&fsp);
+			c = params->stab->fshader(&fsp);
 			if(c.a == 0)			/* discard non-colors */
 				goto discard;
 			if(ropts & RODepth)
@@ -322,7 +327,7 @@
 			berpvertex(fsp.v, &prim->v[0], &prim->v[1], &prim->v[2], bc);
 
 			fsp.p = p;
-			c = params->fshader(&fsp);
+			c = params->stab->fshader(&fsp);
 			if(c.a == 0)			/* discard non-colors */
 				continue;
 			if(ropts & RODepth)
@@ -477,7 +482,7 @@
 
 				vsp.v = &p->v[0];
 				vsp.idx = 0;
-				p->v[0].p = params->vshader(&vsp);
+				p->v[0].p = params->stab->vshader(&vsp);
 
 				if(!isvisible(p->v[0].p))
 					break;
@@ -512,7 +517,7 @@
 
 					vsp.v = &p->v[i];
 					vsp.idx = i;
-					p->v[i].p = params->vshader(&vsp);
+					p->v[i].p = params->stab->vshader(&vsp);
 				}
 
 				if(!isvisible(p->v[0].p) || !isvisible(p->v[1].p)){
@@ -558,7 +563,7 @@
 
 					vsp.v = &p->v[i];
 					vsp.idx = i;
-					p->v[i].p = params->vshader(&vsp);
+					p->v[i].p = params->stab->vshader(&vsp);
 				}
 
 				if(!isvisible(p->v[0].p) || !isvisible(p->v[1].p) || !isvisible(p->v[2].p)){
@@ -751,12 +756,10 @@
 			params = emalloc(sizeof *params);
 			memset(params, 0, sizeof *params);
 			params->fb = job->fb;
+			params->stab = job->shaders;
 			params->job = job;
 			params->camera = job->camera;
 			params->entity = ent;
-			params->uni_time = time;
-			params->vshader = job->shaders->vshader;
-			params->fshader = job->shaders->fshader;
 			sendp(ep->paramsc, params);
 		}
 		/* mark end of job */
--- a/vertex.c
+++ b/vertex.c
@@ -8,7 +8,7 @@
 #include "internal.h"
 
 static void
-_addvattr(Vertex *v, Vertexattr *va)
+_addvattr(Vertexattrs *v, Vertexattr *va)
 {
 	int i;
 
@@ -95,7 +95,7 @@
 }
 
 void
-addvattr(Vertex *v, char *id, int type, void *val)
+addvattr(Vertexattrs *v, char *id, int type, void *val)
 {
 	Vertexattr va;
 
@@ -110,7 +110,7 @@
 }
 
 Vertexattr *
-getvattr(Vertex *v, char *id)
+getvattr(Vertexattrs *v, char *id)
 {
 	int i;