shithub: libgraphics

Download patch

ref: d36e762e12d108fec8cd19071b22909b4466812a
parent: 406aa46cad83725c4560737aa6f2d4a40277a108
author: rodri <rgl@antares-labs.eu>
date: Sun Sep 1 12:23:01 EDT 2024

render: get more accurate stage completion times.

--- a/render.c
+++ b/render.c
@@ -192,8 +192,8 @@
 	SUparams *params;
 	Raster *cr, *zr;
 	Primitive prim;
-	Shaderparams fsp;
 	Vertex v;
+	Shaderparams fsp;
 	Triangle2 t;
 	Rectangle bbox;
 	Point p, dp, Δp, p0, p1;
@@ -316,36 +316,36 @@
 		bbox.max.y = min(bbox.max.y, task->wr.max.y);
 
 		for(p.y = bbox.min.y; p.y < bbox.max.y; p.y++)
-			for(p.x = bbox.min.x; p.x < bbox.max.x; p.x++){
-				bc = _barycoords(t, Pt2(p.x+0.5,p.y+0.5,1));
-				if(bc.x < 0 || bc.y < 0 || bc.z < 0)
+		for(p.x = bbox.min.x; p.x < bbox.max.x; p.x++){
+			bc = _barycoords(t, Pt2(p.x+0.5,p.y+0.5,1));
+			if(bc.x < 0 || bc.y < 0 || bc.z < 0)
+				continue;
+
+			z = fberp(prim.v[0].p.z, prim.v[1].p.z, prim.v[2].p.z, bc);
+			if(params->camera->enabledepth){
+				if(z <= getdepth(zr, p))
 					continue;
+				putdepth(zr, p, z);
+			}
 
-				z = fberp(prim.v[0].p.z, prim.v[1].p.z, prim.v[2].p.z, bc);
-				if(params->camera->enabledepth){
-					if(z <= getdepth(zr, p))
-						continue;
-					putdepth(zr, p, z);
-				}
+			/* interpolate z⁻¹ and get actual z */
+			pcz = fberp(prim.v[0].p.w, prim.v[1].p.w, prim.v[2].p.w, bc);
+			pcz = 1.0/(pcz < 1e-5? 1e-5: pcz);
 
-				/* interpolate z⁻¹ and get actual z */
-				pcz = fberp(prim.v[0].p.w, prim.v[1].p.w, prim.v[2].p.w, bc);
-				pcz = 1.0/(pcz < 1e-5? 1e-5: pcz);
+			/* perspective-correct attribute interpolation  */
+			bc = modulapt3(bc, Vec3(prim.v[0].p.w*pcz,
+						prim.v[1].p.w*pcz,
+						prim.v[2].p.w*pcz));
+			berpvertex(fsp.v, &prim.v[0], &prim.v[1], &prim.v[2], bc);
 
-				/* perspective-correct attribute interpolation  */
-				bc = modulapt3(bc, Vec3(prim.v[0].p.w*pcz,
-							prim.v[1].p.w*pcz,
-							prim.v[2].p.w*pcz));
-				berpvertex(fsp.v, &prim.v[0], &prim.v[1], &prim.v[2], bc);
-
-				fsp.p = p;
-				c = params->fshader(&fsp);
-				if(params->camera->enableAbuff)
-					pushtoAbuf(params->fb, p, c, z);
-				else
-					pixel(cr, p, c, params->camera->enableblend);
-				delvattrs(fsp.v);
-			}
+			fsp.p = p;
+			c = params->fshader(&fsp);
+			if(params->camera->enableAbuff)
+				pushtoAbuf(params->fb, p, c, z);
+			else
+				pixel(cr, p, c, params->camera->enableblend);
+			delvattrs(fsp.v);
+		}
 		break;
 	default: sysfatal("alien primitive detected");
 	}
@@ -373,6 +373,7 @@
 			if(decref(params->job) < 1){
 				if(params->job->camera->enableAbuff)
 					squashAbuf(params->job->fb, params->job->camera->enableblend);
+				params->job->times.Rn.t1 = nanosec();
 				nbsend(params->job->donec, nil);
 				free(params);
 			}
@@ -387,7 +388,6 @@
 
 		for(i = 0; i < task->p.type+1; i++)
 			delvattrs(&task->p.v[i]);
-		params->job->times.Rn.t1 = nanosec();
 		free(params);
 		free(task);
 	}
@@ -436,6 +436,7 @@
 					task->params = params;
 					sendp(taskchans[i], task);
 				}
+				params->job->times.Tn.t1 = nanosec();
 			}
 			continue;
 		}
@@ -590,7 +591,6 @@
 			default: sysfatal("alien primitive detected");
 			}
 		}
-		params->job->times.Tn.t1 = nanosec();
 		free(params);
 	}
 }
@@ -646,6 +646,7 @@
 			params->job->ref = nproc;
 			for(i = 0; i < nproc; i++)
 				sendp(paramsout[i], params);
+			params->job->times.E.t1 = nanosec();
 			continue;
 		}
 
@@ -668,7 +669,6 @@
 			newparams->ee = i == nworkers-1? ee: newparams->eb + stride;
 			sendp(paramsout[i], newparams);
 		}
-		params->job->times.E.t1 = nanosec();
 		free(params);
 	}
 }