| 1 |
greg |
2.1 |
#ifndef lint
|
| 2 |
|
|
static const char RCSid[] = "$Id$";
|
| 3 |
|
|
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* RtraceSimulManager.cpp
|
| 6 |
|
|
*
|
| 7 |
|
|
* Rtrace simulation manager class implementation
|
| 8 |
|
|
*
|
| 9 |
|
|
* Created by Greg Ward on 2/2/2023.
|
| 10 |
|
|
*/
|
| 11 |
|
|
|
| 12 |
|
|
#include "RtraceSimulManager.h"
|
| 13 |
|
|
#include "source.h"
|
| 14 |
|
|
|
| 15 |
|
|
extern int castonly; // doing ray-casting only?
|
| 16 |
|
|
|
| 17 |
|
|
// Load octree and prepare renderer
|
| 18 |
|
|
bool
|
| 19 |
|
|
RadSimulManager::LoadOctree(const char *octn)
|
| 20 |
|
|
{
|
| 21 |
|
|
if (octname) { // already running?
|
| 22 |
|
|
if (octn && !strcmp(octn, octname))
|
| 23 |
|
|
return true;
|
| 24 |
|
|
Cleanup();
|
| 25 |
|
|
}
|
| 26 |
|
|
if (!octn)
|
| 27 |
|
|
return false;
|
| 28 |
|
|
|
| 29 |
|
|
ray_init((char *)octn);
|
| 30 |
|
|
return true;
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
// Set number of computation threads (0 => #cores)
|
| 34 |
|
|
int
|
| 35 |
|
|
RadSimulManager::SetThreadCount(int nt)
|
| 36 |
|
|
{
|
| 37 |
|
|
return nThreads = 1; // XXX temporary
|
| 38 |
|
|
}
|
| 39 |
|
|
|
| 40 |
|
|
// Close octree, free data, return status
|
| 41 |
|
|
int
|
| 42 |
|
|
RadSimulManager::Cleanup()
|
| 43 |
|
|
{
|
| 44 |
|
|
ray_done(0);
|
| 45 |
|
|
return 0;
|
| 46 |
|
|
}
|
| 47 |
|
|
|
| 48 |
|
|
// How many threads are currently unoccupied?
|
| 49 |
|
|
int
|
| 50 |
|
|
RadSimulManager::ThreadsAvailable() const
|
| 51 |
|
|
{
|
| 52 |
|
|
return 1; // XXX temporary
|
| 53 |
|
|
}
|
| 54 |
|
|
|
| 55 |
|
|
// Global pointer to simulation manager for trace call-back (only one)
|
| 56 |
|
|
static const RtraceSimulManager * ourRTsimMan = NULL;
|
| 57 |
|
|
|
| 58 |
|
|
void // static call-back
|
| 59 |
|
|
RtraceSimulManager::RTracer(RAY *r)
|
| 60 |
|
|
{
|
| 61 |
|
|
(*ourRTsimMan->traceCall)(r, ourRTsimMan->tcData);
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
// Check for changes to render flags & adjust accordingly
|
| 65 |
|
|
bool
|
| 66 |
|
|
RtraceSimulManager::UpdateMode()
|
| 67 |
|
|
{
|
| 68 |
|
|
rtFlags &= RTmask;
|
| 69 |
|
|
if (!cookedCall)
|
| 70 |
|
|
rtFlags &= ~RTdoFIFO;
|
| 71 |
|
|
if (!traceCall)
|
| 72 |
|
|
rtFlags &= ~RTtraceSources;
|
| 73 |
|
|
if (rtFlags & RTimmIrrad)
|
| 74 |
|
|
rtFlags &= ~RTlimDist;
|
| 75 |
|
|
|
| 76 |
|
|
int misMatch = rtFlags ^ curFlags;
|
| 77 |
|
|
// updates based on toggled flags
|
| 78 |
|
|
if (misMatch & RTtraceSources) {
|
| 79 |
|
|
if (rtFlags & RTtraceSources) {
|
| 80 |
|
|
for (int sn = 0; sn < nsources; sn++)
|
| 81 |
|
|
source[sn].sflags |= SFOLLOW;
|
| 82 |
|
|
} else // cannot undo this...
|
| 83 |
|
|
rtFlags |= RTtraceSources;
|
| 84 |
|
|
}
|
| 85 |
|
|
if (misMatch & RTdoFIFO) {
|
| 86 |
|
|
if (!FlushQueue())
|
| 87 |
|
|
return false;
|
| 88 |
|
|
}
|
| 89 |
|
|
curFlags = rtFlags;
|
| 90 |
|
|
// update trace callback
|
| 91 |
|
|
if (traceCall) {
|
| 92 |
|
|
if (ourRTsimMan && ourRTsimMan != this)
|
| 93 |
|
|
error(WARNING, "Competing top-level simulation managers?");
|
| 94 |
|
|
ourRTsimMan = this;
|
| 95 |
|
|
trace = RTracer;
|
| 96 |
|
|
} else if (ourRTsimMan == this) {
|
| 97 |
|
|
trace = NULL;
|
| 98 |
|
|
ourRTsimMan = NULL;
|
| 99 |
|
|
}
|
| 100 |
|
|
return true;
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
extern "C" int m_normal(OBJREC *m, RAY *r);
|
| 104 |
|
|
|
| 105 |
|
|
/* compute irradiance rather than radiance */
|
| 106 |
|
|
static void
|
| 107 |
|
|
rayirrad(RAY *r)
|
| 108 |
|
|
{
|
| 109 |
|
|
/* pretend we hit surface */
|
| 110 |
|
|
r->rxt = r->rot = 1e-5;
|
| 111 |
|
|
VSUM(r->rop, r->rorg, r->rdir, r->rot);
|
| 112 |
|
|
r->ron[0] = -r->rdir[0];
|
| 113 |
|
|
r->ron[1] = -r->rdir[1];
|
| 114 |
|
|
r->ron[2] = -r->rdir[2];
|
| 115 |
|
|
r->rod = 1.0;
|
| 116 |
|
|
/* compute result */
|
| 117 |
|
|
r->revf = raytrace;
|
| 118 |
|
|
m_normal(&Lamb, r);
|
| 119 |
|
|
r->revf = rayirrad;
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
/* compute first ray intersection only */
|
| 123 |
|
|
static void
|
| 124 |
|
|
raycast(RAY *r)
|
| 125 |
|
|
{
|
| 126 |
|
|
if (!localhit(r, &thescene)) {
|
| 127 |
|
|
if (r->ro == &Aftplane) { /* clipped */
|
| 128 |
|
|
r->ro = NULL;
|
| 129 |
|
|
r->rot = FHUGE;
|
| 130 |
|
|
} else
|
| 131 |
|
|
sourcehit(r);
|
| 132 |
|
|
}
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
// Add ray bundle to queue w/ optional 1st ray ID
|
| 136 |
|
|
int
|
| 137 |
|
|
RtraceSimulManager::EnqueueBundle(const FVECT orig_direc[], int n, RNUMBER rID0)
|
| 138 |
|
|
{
|
| 139 |
|
|
int nqueued = 0;
|
| 140 |
|
|
RAY res;
|
| 141 |
|
|
|
| 142 |
|
|
if (!Ready())
|
| 143 |
|
|
return -1;
|
| 144 |
|
|
|
| 145 |
|
|
if (castonly && !cookedCall)
|
| 146 |
|
|
error(CONSISTENCY, "EnqueueBundle() called in castonly mode without cookedCall");
|
| 147 |
|
|
|
| 148 |
|
|
if (!UpdateMode()) // update rendering mode if requested
|
| 149 |
|
|
return -1;
|
| 150 |
|
|
|
| 151 |
|
|
while (n-- > 0) { // queue each ray
|
| 152 |
|
|
double d;
|
| 153 |
|
|
VCOPY(res.rorg, orig_direc[0]);
|
| 154 |
|
|
VCOPY(res.rdir, orig_direc[1]);
|
| 155 |
|
|
orig_direc += 2;
|
| 156 |
|
|
rayorigin(&res, PRIMARY, NULL, NULL);
|
| 157 |
|
|
if (rID0) res.rno = rID0++;
|
| 158 |
|
|
else res.rno = ++lastRayID;
|
| 159 |
|
|
if (curFlags & RTimmIrrad)
|
| 160 |
|
|
res.revf = rayirrad;
|
| 161 |
|
|
else if (castonly)
|
| 162 |
|
|
res.revf = raycast;
|
| 163 |
|
|
d = normalize(res.rdir);
|
| 164 |
|
|
if (d > 0) { // direction vector is valid?
|
| 165 |
|
|
if (curFlags & RTlimDist)
|
| 166 |
|
|
res.rmax = d;
|
| 167 |
|
|
samplendx++;
|
| 168 |
|
|
rayvalue(&res); // XXX single-threaded for now
|
| 169 |
|
|
++nqueued;
|
| 170 |
|
|
} else if (ThreadsAvailable() < NThreads() &&
|
| 171 |
|
|
!FlushQueue())
|
| 172 |
|
|
return -1;
|
| 173 |
|
|
if (cookedCall)
|
| 174 |
|
|
(*cookedCall)(&res, ccData);
|
| 175 |
|
|
}
|
| 176 |
|
|
return nqueued;
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
// Finish pending rays and complete callbacks
|
| 180 |
|
|
bool
|
| 181 |
|
|
RtraceSimulManager::FlushQueue()
|
| 182 |
|
|
{
|
| 183 |
|
|
return true; // XXX no-op for now
|
| 184 |
|
|
}
|