--- ray/src/rt/rxpiece.cpp 2024/09/17 00:05:11 2.2 +++ ray/src/rt/rxpiece.cpp 2024/11/06 18:28:52 2.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rxpiece.cpp,v 2.2 2024/09/17 00:05:11 greg Exp $"; +static const char RCSid[] = "$Id: rxpiece.cpp,v 2.7 2024/11/06 18:28:52 greg Exp $"; #endif /* * rxpiece.cpp - main for rxpiece tile rendering program @@ -14,6 +14,8 @@ static const char RCSid[] = "$Id: rxpiece.cpp,v 2.2 20 #include "platform.h" #include "RpictSimulManager.h" +#include "ambient.h" +#include "pmapray.h" #include "random.h" extern char *progname; /* argv[0] */ @@ -35,7 +37,7 @@ double mblur = 0.; /* motion blur parameter (unused double dblur = 0.; /* depth-of-field blur parameter */ -int nproc = 1; /* number of processes to run */ +int nproc = 1; /* number of processes to run (-1 in child) */ RpictSimulManager myRPmanager; // global simulation manager @@ -55,10 +57,15 @@ static RenderDataType rpiece(char *pout, RenderDataTyp "OutputCS=RGB,XYZ,prims,spec\n" +// Exit program void quit(int code) /* quit program */ { - exit(code); + if (nproc < 0) { + ray_pnprocs = -1; // hack to avoid cleanup in child + _exit(code); + } + exit(code); // parent still frees everything (*yawn*) } @@ -315,8 +322,12 @@ main(int argc, char *argv[]) // render tiles dtype = rpiece(outfile, dtype, zfile); - quit(dtype==RDTnone); // clean up and exit + ambsync(); // flush ambient cache + ray_done_pmap(); /* PMAP: free photon maps */ + + quit(dtype==RDTnone); // status is 1 on failure + badopt: sprintf(errmsg, "command line error at '%s'", argv[i]); error(USER, errmsg); @@ -332,6 +343,8 @@ wputs( /* warning output function */ const char *s ) { + if (!erract[WARNING].pf) + return; // warnings were disabled! int lasterrno = errno; eputs(s); errno = lasterrno; @@ -499,6 +512,7 @@ children_finished() if (cpid == 0) { // children render tiles sleep(nproc - cnt); // avoid race conditions + nproc = -1; // flag as child return false; } cow_doneshare(); // parent frees memory and waits @@ -598,20 +612,21 @@ rpiece(char *pout, RenderDataType dt, char *zout) const bool newOutput = (access(pout, F_OK) < 0); FILE *pdfp[2]; - if (!newOutput) { // output exists? + if (newOutput) { // new output file? + CHECK((tileGrid[0] <= 1) & (tileGrid[1] <= 1), + USER, "bad tiling specification"); + } else { dt = myRPmanager.ReopenOutput(pdfp, pout, zout); if (dt == RDTnone) - quit(1); + return RDTnone; if (!fscnresolu(&hresolu, &vresolu, pdfp[0])) error(USER, "missing picture resolution"); pixaspect = .0; // need to leave this as is myRPmanager.NewHeader(pout); // get prev. header info const char * tval = myRPmanager.GetHeadStr("TILED="); if (tval) sscanf(tval, "%d %d", &tileGrid[0], &tileGrid[1]); - if (!myRPmanager.GetView()) { - sprintf(errmsg, "missing view in picture file '%s'", pout); - error(USER, errmsg); - } + CHECK(myRPmanager.GetView()==NULL, + USER, "missing view in picture file"); ourview = *myRPmanager.GetView(); } int hvdim[2] = {hresolu, vresolu}; // set up tiled frame @@ -633,16 +648,17 @@ rpiece(char *pout, RenderDataType dt, char *zout) myRPmanager.AddHeader(buf); dt = myRPmanager.NewOutput(pdfp, pout, dt, zout); if (dt == RDTnone) - quit(1); + return RDTnone; fprtresolu(hresolu, vresolu, pdfp[0]); - } - if (RDTdepthT(dt) == RDTdshort) { - if (newOutput) + fflush(pdfp[0]); + if (RDTdepthT(dt) == RDTdshort) { fprtresolu(hresolu, vresolu, pdfp[1]); - else if (!fscnresolu(&hvdim[0], &hvdim[1], pdfp[1]) || - (hvdim[0] != hresolu) | (hvdim[1] != vresolu)) - error(USER, "mismatched depth file resolution"); - } + fflush(pdfp[1]); + } + } else if (RDTdepthT(dt) == RDTdshort && + (!fscnresolu(&hvdim[0], &hvdim[1], pdfp[1]) || + (hvdim[0] != hresolu) | (hvdim[1] != vresolu))) + error(USER, "mismatched depth file resolution"); // prepare (flat) pixel buffer const long pdata_beg = ftell(pdfp[0]); const size_t pixSiz = (RDTcolorT(dt)==RDTrgbe)|(RDTcolorT(dt)==RDTxyze) ? sizeof(COLR) @@ -653,7 +669,7 @@ rpiece(char *pout, RenderDataType dt, char *zout) if (pmlen&7) pmlen += 8 - (pmlen&7); // 8-byte alignment to be safe pmlen += sizeof(TileProg)*tileGrid[0]*tileGrid[1]; // map picture file to memory - if (ftruncate(fileno(pdfp[0]), pmlen) < 0) + if (newOutput && ftruncate(fileno(pdfp[0]), pmlen) < 0) error(SYSTEM, "cannot extend picture buffer"); uby8 * pixMap = (uby8 *)mmap(NULL, pmlen, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(pdfp[0]), 0); @@ -666,7 +682,7 @@ rpiece(char *pout, RenderDataType dt, char *zout) const size_t zmlen = zdata_beg + zdpSiz*hresolu*vresolu; uby8 * zdMap = NULL; if (RDTdepthT(dt)) { - if (ftruncate(fileno(pdfp[1]), zmlen) < 0) + if (newOutput && ftruncate(fileno(pdfp[1]), zmlen) < 0) error(SYSTEM, "cannot extend depth buffer"); zdMap = (uby8 *)mmap(NULL, zmlen, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(pdfp[1]), 0); @@ -714,6 +730,7 @@ rpiece(char *pout, RenderDataType dt, char *zout) error(USER, errmsg); } tile_p(ti)->status = 1; // mark tile completed + ndone++; } if (!ndone) error(WARNING, "no tiles need rendering, exit");