ref: 5e7ea1dfc93eea222c62c818cfab760802459cd1
parent: 93217b6b2c2e5eecc7fe0f412fe2a7d0aa4b4a3c
author: rodri <rgl@antares-labs.eu>
date: Tue Jun 24 13:29:48 EDT 2025
add backend Vertex and Primitive types for internal use this draws a clear distinction between the data processed by the renderer and the one manipulated by the user, decoupling the logic and serving as a first step towards the optimization of geometry storage. there are properties like per-vertex tangents, materials and Vertexattrs (varyings) that have no meaning outside of the rendering pipeline, so it makes no sense to keep space for them.
--- a/clip.c
+++ b/clip.c
@@ -29,7 +29,7 @@
}
static int
-addvert(Polygon *p, Vertex v)
+addvert(Polygon *p, BVertex v)
{
if(++p->n > p->cap)
p->v = _erealloc(p->v, (p->cap = p->n)*sizeof(*p->v));
@@ -63,7 +63,7 @@
* - https://github.com/aap/librw/blob/14dab85dcae6f3762fb2b1eda4d58d8e67541330/tools/playground/tl_tests.cpp#L522
*/
int
-_clipprimitive(Primitive *p, Primitive *cp)
+_clipprimitive(BPrimitive *p, BPrimitive *cp)
{
/* signed distance from each clipping plane */
static double sdm[6][4] = {
@@ -77,7 +77,7 @@
double sd0[6], sd1[6];
double d0, d1, perc;
Polygon Vinp, Voutp, *Vin, *Vout;
- Vertex *v0, *v1, v; /* edge verts and new vertex (line-plane intersection) */
+ BVertex *v0, *v1, v; /* edge verts and new vertex (line-plane intersection) */
int i, j, np;
np = 0;
@@ -184,9 +184,9 @@
/* lerp vertex attributes to match the new positions */
static void
-adjustverts(Point *p0, Point *p1, Vertex *v0, Vertex *v1)
+adjustverts(Point *p0, Point *p1, BVertex *v0, BVertex *v1)
{
- Vertex v[2];
+ BVertex v[2];
Point3 dp;
Point Δp;
double len, perc;
@@ -214,7 +214,7 @@
* Cohen-Sutherland rectangle-line clipping
*/
int
-_rectclipline(Rectangle r, Point *p0, Point *p1, Vertex *v0, Vertex *v1)
+_rectclipline(Rectangle r, Point *p0, Point *p1, BVertex *v0, BVertex *v1)
{
int code0, code1;
int Δx, Δy;
@@ -237,7 +237,7 @@
if(ptisinside(code0)){
SWAP(Point, p0, p1);
SWAP(int, &code0, &code1);
- SWAP(Vertex, v0, v1);
+ SWAP(BVertex, v0, v1);
}
if(code0 & CLIPL){
--- a/graphics.h
+++ b/graphics.h
@@ -58,6 +58,7 @@
typedef struct Vertexattr Vertexattr;
typedef struct Vertexattrs Vertexattrs;
typedef struct Vertex Vertex;
+typedef struct BVertex BVertex;
typedef struct LightSource LightSource;
typedef struct Material Material;
typedef struct Primitive Primitive;
@@ -141,6 +142,16 @@
Point3 n; /* surface normal */
Color c; /* shading color */
Point2 uv; /* texture coordinate */
+};
+
+/*
+ * BVertex is only used internally in the rasterizer, but it's
+ * presented to the user by the Shaderparams so its definition must be
+ * publicly available.
+ */
+struct BVertex
+{
+ Vertex;
Material *mtl;
Point3 tangent;
Vertexattrs; /* attributes (varyings) */
@@ -219,7 +230,7 @@
struct Shaderparams
{
SUparams *su;
- Vertex *v;
+ BVertex *v;
Point p; /* fragment position (fshader-only) */
uint idx; /* vertex index (vshader-only) */
--- a/internal.h
+++ b/internal.h
@@ -10,6 +10,7 @@
OP_END,
};
+typedef struct BPrimitive BPrimitive;
typedef struct Polygon Polygon;
typedef struct Entityparam Entityparam;
typedef struct Tilerparam Tilerparam;
@@ -19,9 +20,17 @@
typedef struct pGradient pGradient;
typedef struct vGradient vGradient;
+struct BPrimitive
+{
+ int type;
+ BVertex v[3];
+ Material *mtl;
+ Point3 tangent; /* used for normal mapping */
+};
+
struct Polygon
{
- Vertex *v;
+ BVertex *v;
ulong n;
ulong cap;
};
@@ -53,7 +62,7 @@
Shaderparams *fsp;
Rectangle wr; /* working rect */
Rectangle *clipr;
- Primitive p;
+ BPrimitive p;
};
struct fGradient
@@ -72,9 +81,9 @@
struct vGradient
{
- Vertex v0;
- Vertex dx;
- Vertex dy;
+ BVertex v0;
+ BVertex dx;
+ BVertex dy;
};
/* alloc */
@@ -102,20 +111,20 @@
void _rmfbctl(Framebufctl*);
/* vertex */
-Vertex _dupvertex(Vertex*);
-void _loadvertex(Vertex*, Vertex*);
-void _lerpvertex(Vertex*, Vertex*, Vertex*, double);
-void _berpvertex(Vertex*, Vertex*, Vertex*, Vertex*, Point3);
-void _addvertex(Vertex*, Vertex*);
-void _mulvertex(Vertex*, double);
-void _delvattrs(Vertex*);
-void _fprintvattrs(int, Vertex*);
+BVertex _dupvertex(BVertex*);
+void _loadvertex(BVertex*, BVertex*);
+void _lerpvertex(BVertex*, BVertex*, BVertex*, double);
+void _berpvertex(BVertex*, BVertex*, BVertex*, BVertex*, Point3);
+void _addvertex(BVertex*, BVertex*);
+void _mulvertex(BVertex*, double);
+void _delvattrs(BVertex*);
+void _fprintvattrs(int, BVertex*);
void _addvattr(Vertexattrs*, char*, int, void*);
Vertexattr *_getvattr(Vertexattrs*, char*);
/* clip */
-int _clipprimitive(Primitive*, Primitive*);
-int _rectclipline(Rectangle, Point*, Point*, Vertex*, Vertex*);
+int _clipprimitive(BPrimitive*, BPrimitive*);
+int _rectclipline(Rectangle, Point*, Point*, BVertex*, BVertex*);
/* util */
void _memsetl(void*, ulong, usize);
--- a/render.c
+++ b/render.c
@@ -84,7 +84,7 @@
}
static int
-isfacingback(Primitive *p)
+isfacingback(BPrimitive *p)
{
double sa; /* signed double area */
@@ -176,7 +176,7 @@
{
SUparams *params;
Raster *cr, *zr;
- Primitive *prim;
+ BPrimitive *prim;
Point p;
Color c;
float z;
@@ -222,7 +222,7 @@
{
SUparams *params;
Raster *cr, *zr;
- Primitive *prim;
+ BPrimitive *prim;
Point p, dp, Δp, p0, p1;
Color c;
double dplen, perc;
@@ -255,7 +255,7 @@
/* make them left-to-right */
if(p0.x > p1.x){
SWAP(Point, &p0, &p1);
- SWAP(Vertex, prim->v+0, prim->v+1);
+ SWAP(BVertex, prim->v+0, prim->v+1);
}
dp = subpt(p1, p0);
@@ -334,11 +334,11 @@
{
SUparams *params;
Raster *cr, *zr;
- Primitive *prim;
+ BPrimitive *prim;
pGradient ∇bc;
// vGradient ∇v;
// fGradient ∇z, ∇pcz;
-// Vertex v, *vp;
+// BVertex v, *vp;
Triangle2 t;
Point p;
Point3 bc;
@@ -423,10 +423,10 @@
// _loadvertex(vp, task->fsp->v);
// _mulvertex(vp, 1/(pcz < ε1? ε1: pcz));
-// SWAP(Vertex*, &vp, &task->fsp->v);
+// SWAP(BVertex*, &vp, &task->fsp->v);
task->fsp->p = p;
c = params->stab->fs(task->fsp);
-// SWAP(Vertex*, &vp, &task->fsp->v);
+// SWAP(BVertex*, &vp, &task->fsp->v);
if(c.a == 0) /* discard non-colors */
goto discard;
if(ropts & RODepth)
@@ -472,7 +472,7 @@
Rastertask *task;
SUparams *params;
Renderjob *job;
- Vertex v;
+ BVertex v;
Shaderparams fsp;
int i;
@@ -556,7 +556,8 @@
SUparams *params, *newparams;
Rastertask *task;
Shaderparams vsp;
- Primitive *ep, *cp, *p, prim; /* primitives to raster */
+ Primitive *ep; /* primitives to raster */
+ BPrimitive prim, *p, *cp;
Rectangle *wr, bbox;
Channel **taskchans;
ulong nproc;
@@ -565,7 +566,7 @@
tp = arg;
threadsetname("tiler %d", tp->id);
- cp = _emalloc(sizeof(*cp)*16);
+ cp = _emalloc(16*sizeof(*cp));
taskchans = tp->taskchans;
nproc = tp->nproc;
wr = _emalloc(nproc*sizeof(Rectangle));
@@ -607,7 +608,11 @@
for(ep = params->eb; ep != params->ee; ep++){
np = 1; /* start with one. after clipping it might change */
- prim = *ep;
+ prim.type = ep->type;
+ for(i = 0; i < prim.type+1; i++)
+ prim.v[i].Vertex = ep->v[i];
+ prim.mtl = ep->mtl;
+ prim.tangent = ep->tangent;
p = &prim;
switch(p->type){
case PPoint:
--- a/vertex.c
+++ b/vertex.c
@@ -25,7 +25,7 @@
}
static void
-copyvattrs(Vertex *d, Vertex *s)
+copyvattrs(BVertex *d, BVertex *s)
{
int i;
@@ -33,10 +33,10 @@
addvattr(d, &s->attrs[i]);
}
-Vertex
-_dupvertex(Vertex *v)
+BVertex
+_dupvertex(BVertex *v)
{
- Vertex nv;
+ BVertex nv;
nv = *v;
nv.attrs = nil;
@@ -46,7 +46,7 @@
}
void
-_loadvertex(Vertex *d, Vertex *s)
+_loadvertex(BVertex *d, BVertex *s)
{
d->p = s->p;
d->n = s->n;
@@ -61,7 +61,7 @@
* linear attribute interpolation
*/
void
-_lerpvertex(Vertex *v, Vertex *v0, Vertex *v1, double t)
+_lerpvertex(BVertex *v, BVertex *v0, BVertex *v1, double t)
{
Vertexattr va;
int i;
@@ -87,7 +87,7 @@
* barycentric attribute interpolation
*/
void
-_berpvertex(Vertex *v, Vertex *v0, Vertex *v1, Vertex *v2, Point3 bc)
+_berpvertex(BVertex *v, BVertex *v0, BVertex *v1, BVertex *v2, Point3 bc)
{
Vertexattr va;
int i;
@@ -110,7 +110,7 @@
}
void
-_addvertex(Vertex *a, Vertex *b)
+_addvertex(BVertex *a, BVertex *b)
{
Vertexattr *va, *vb;
@@ -128,7 +128,7 @@
}
void
-_mulvertex(Vertex *v, double s)
+_mulvertex(BVertex *v, double s)
{
Vertexattr *va;
@@ -171,7 +171,7 @@
}
void
-_delvattrs(Vertex *v)
+_delvattrs(BVertex *v)
{
free(v->attrs);
v->attrs= nil;
@@ -179,7 +179,7 @@
}
void
-_fprintvattrs(int fd, Vertex *v)
+_fprintvattrs(int fd, BVertex *v)
{
int i;
--
⑨