1 |
greg |
2.1 |
/* RCSid $Id$ */ |
2 |
|
|
/* |
3 |
|
|
* RtraceSimulManager.h |
4 |
|
|
* |
5 |
|
|
* Rtrace simulation manager class declaration (along with base class) |
6 |
|
|
* Enqueuing rays will block caller iff #rays >= ThreadsAvail() |
7 |
|
|
* Reporting call-backs made from EnqueBundle() and FlushQueue() |
8 |
|
|
* |
9 |
|
|
* Created by Greg Ward on 11/10/22. |
10 |
|
|
*/ |
11 |
|
|
|
12 |
|
|
#ifndef RtraceSimulManager_h |
13 |
|
|
#define RtraceSimulManager_h |
14 |
|
|
|
15 |
|
|
#include "ray.h" |
16 |
|
|
|
17 |
|
|
extern char * octname; // global octree name |
18 |
|
|
|
19 |
|
|
/// Ray reporting callback method |
20 |
|
|
typedef void RayReportCall(RAY *r, void *cd); |
21 |
|
|
|
22 |
|
|
/// Multi-threaded simulation manager base class |
23 |
|
|
class RadSimulManager { |
24 |
|
|
int nThreads; // number of active threads |
25 |
|
|
public: |
26 |
|
|
RadSimulManager(const char *octn = NULL) { |
27 |
|
|
LoadOctree(octn); |
28 |
|
|
nThreads = 1; |
29 |
|
|
} |
30 |
|
|
~RadSimulManager() { |
31 |
|
|
Cleanup(); |
32 |
|
|
} |
33 |
|
|
/// Load octree and prepare renderer |
34 |
|
|
bool LoadOctree(const char *octn); |
35 |
|
|
/// Set number of computation threads (0 => #cores) |
36 |
|
|
int SetThreadCount(int nt = 0); |
37 |
|
|
/// Check thread count (1 means no multi-threading) |
38 |
|
|
int NThreads() const { |
39 |
|
|
return nThreads; |
40 |
|
|
} |
41 |
|
|
/// How many threads are currently unoccupied? |
42 |
|
|
int ThreadsAvailable() const; |
43 |
|
|
/// Are we ready? |
44 |
|
|
bool Ready() const { |
45 |
|
|
return (octname && nsceneobjs > 0); |
46 |
|
|
} |
47 |
|
|
/// Close octree, free data, return status |
48 |
|
|
int Cleanup(); |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
/// Flags to control rendering operations |
52 |
|
|
enum {RTdoFIFO=1, RTtraceSources=2, RTlimDist=4, RTimmIrrad=8, RTmask=15}; |
53 |
|
|
|
54 |
|
|
/// rtrace-like simulation manager (at most one such object) |
55 |
|
|
class RtraceSimulManager : public RadSimulManager { |
56 |
|
|
RayReportCall * cookedCall; // callback for cooked primary rays |
57 |
|
|
void * ccData; // client data for cooked primary rays |
58 |
|
|
RayReportCall * traceCall; // call for every ray in tree |
59 |
|
|
void * tcData; // client data for traced rays |
60 |
|
|
int curFlags; // current operating flags |
61 |
|
|
// Call-back for global ray-tracing context |
62 |
|
|
static void RTracer(RAY *r); |
63 |
|
|
// Check for changes to render flags, etc. |
64 |
|
|
bool UpdateMode(); |
65 |
|
|
protected: |
66 |
|
|
RNUMBER lastRayID; // last ray ID assigned |
67 |
|
|
public: |
68 |
|
|
int rtFlags; // operation (RT*) flags |
69 |
|
|
RtraceSimulManager(RayReportCall *cb = NULL, void *cd = NULL, |
70 |
|
|
const char *octn = NULL) : RadSimulManager(octn) { |
71 |
|
|
lastRayID = 0; |
72 |
|
|
rtFlags = curFlags = 0; |
73 |
|
|
SetCookedCall(cb, cd); |
74 |
|
|
traceCall = NULL; tcData = NULL; |
75 |
|
|
} |
76 |
|
|
~RtraceSimulManager() {} |
77 |
|
|
/// Add ray bundle to queue w/ optional 1st ray ID |
78 |
|
|
int EnqueueBundle(const FVECT orig_direc[], int n, |
79 |
|
|
RNUMBER rID0 = 0); |
80 |
|
|
/// Enqueue a single ray w/ optional ray ID |
81 |
|
|
bool EnqueueRay(const FVECT org, const FVECT dir, |
82 |
|
|
RNUMBER rID = 0) { |
83 |
|
|
if (dir == org+1) |
84 |
|
|
return EnqueueBundle((const FVECT *)org, 1, rID); |
85 |
|
|
FVECT orgdir[2]; |
86 |
|
|
VCOPY(orgdir[0], org); VCOPY(orgdir[1], dir); |
87 |
|
|
return EnqueueBundle(orgdir, 1, rID); |
88 |
|
|
} |
89 |
|
|
/// Set/change cooked ray callback & FIFO flag |
90 |
|
|
void SetCookedCall(RayReportCall *cb, void *cd = NULL) { |
91 |
|
|
if (cookedCall && (cookedCall != cb) | (ccData != cd)) |
92 |
|
|
FlushQueue(); |
93 |
|
|
cookedCall = cb; |
94 |
|
|
ccData = cd; |
95 |
|
|
} |
96 |
|
|
/// Set/change trace callback |
97 |
|
|
void SetTraceCall(RayReportCall *cb, void *cd = NULL) { |
98 |
|
|
traceCall = cb; |
99 |
|
|
tcData = cd; |
100 |
|
|
} |
101 |
|
|
/// Are we ready? |
102 |
|
|
bool Ready() const { |
103 |
|
|
return (cookedCall != NULL) | (traceCall != NULL) && |
104 |
|
|
RadSimulManager::Ready(); |
105 |
|
|
} |
106 |
|
|
/// Finish pending rays and complete callbacks |
107 |
|
|
bool FlushQueue(); |
108 |
|
|
/// Close octree, free data, return status |
109 |
|
|
int Cleanup() { |
110 |
|
|
SetCookedCall(NULL); |
111 |
|
|
SetTraceCall(NULL); |
112 |
|
|
rtFlags = 0; |
113 |
|
|
UpdateMode(); |
114 |
|
|
lastRayID = 0; |
115 |
|
|
return RadSimulManager::Cleanup(); |
116 |
|
|
} |
117 |
|
|
}; |
118 |
|
|
|
119 |
|
|
/// Determine if vector is all zeroes |
120 |
|
|
inline bool |
121 |
|
|
IsZeroVec(const FVECT vec) |
122 |
|
|
{ |
123 |
|
|
return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
#endif /* RtraceSimulManager_h */ |