1 |
/* RCSid $Id: RtraceSimulManager.h,v 2.2 2023/07/26 23:27:44 greg Exp $ */ |
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 |
/// How many cores are there? |
36 |
static int GetNCores(); |
37 |
/// Set number of computation threads (0 => #cores) |
38 |
int SetThreadCount(int nt = 0); |
39 |
/// Check thread count (1 means no multi-threading) |
40 |
int NThreads() const { |
41 |
return nThreads; |
42 |
} |
43 |
/// How many threads are currently unoccupied? |
44 |
int ThreadsAvailable() const; |
45 |
/// Are we ready? |
46 |
bool Ready() const { |
47 |
return (octname && nsceneobjs > 0); |
48 |
} |
49 |
/// Close octree, free data, return status |
50 |
int Cleanup(bool everything = false); |
51 |
}; |
52 |
|
53 |
/// Flags to control rendering operations |
54 |
enum {RTdoFIFO=1, RTtraceSources=2, RTlimDist=4, RTimmIrrad=8, RTmask=15}; |
55 |
|
56 |
/// rtrace-like simulation manager (at most one such object) |
57 |
class RtraceSimulManager : public RadSimulManager { |
58 |
RayReportCall * cookedCall; // callback for cooked primary rays |
59 |
void * ccData; // client data for cooked primary rays |
60 |
RayReportCall * traceCall; // call for every ray in tree |
61 |
void * tcData; // client data for traced rays |
62 |
int curFlags; // current operating flags |
63 |
// Call-back for global ray-tracing context |
64 |
static void RTracer(RAY *r); |
65 |
// Check for changes to render flags, etc. |
66 |
bool UpdateMode(); |
67 |
protected: |
68 |
RNUMBER lastRayID; // last ray ID assigned |
69 |
public: |
70 |
int rtFlags; // operation (RT*) flags |
71 |
RtraceSimulManager(RayReportCall *cb = NULL, void *cd = NULL, |
72 |
const char *octn = NULL) : RadSimulManager(octn) { |
73 |
lastRayID = 0; |
74 |
rtFlags = curFlags = 0; |
75 |
SetCookedCall(cb, cd); |
76 |
traceCall = NULL; tcData = NULL; |
77 |
} |
78 |
~RtraceSimulManager() { |
79 |
FlushQueue(); |
80 |
} |
81 |
/// Set number of computation threads (0 => #cores) |
82 |
int SetThreadCount(int nt = 0) { |
83 |
if (nt <= 0) nt = GetNCores(); |
84 |
if (nt == NThreads()) return nt; |
85 |
FlushQueue(); |
86 |
return RadSimulManager::SetThreadCount(nt); |
87 |
} |
88 |
/// Add ray bundle to queue w/ optional 1st ray ID |
89 |
int EnqueueBundle(const FVECT orig_direc[], int n, |
90 |
RNUMBER rID0 = 0); |
91 |
/// Enqueue a single ray w/ optional ray ID |
92 |
bool EnqueueRay(const FVECT org, const FVECT dir, |
93 |
RNUMBER rID = 0) { |
94 |
if (dir == org+1) |
95 |
return(EnqueueBundle((const FVECT *)org, 1, rID) > 0); |
96 |
FVECT orgdir[2]; |
97 |
VCOPY(orgdir[0], org); VCOPY(orgdir[1], dir); |
98 |
return(EnqueueBundle(orgdir, 1, rID) > 0); |
99 |
} |
100 |
/// Set/change cooked ray callback & FIFO flag |
101 |
void SetCookedCall(RayReportCall *cb, void *cd = NULL) { |
102 |
if (cookedCall && (cookedCall != cb) | (ccData != cd)) |
103 |
FlushQueue(); |
104 |
cookedCall = cb; |
105 |
ccData = cd; |
106 |
} |
107 |
/// Set/change trace callback |
108 |
void SetTraceCall(RayReportCall *cb, void *cd = NULL) { |
109 |
traceCall = cb; |
110 |
tcData = cd; |
111 |
} |
112 |
/// Are we ready? |
113 |
bool Ready() const { |
114 |
return (cookedCall != NULL) | (traceCall != NULL) && |
115 |
RadSimulManager::Ready(); |
116 |
} |
117 |
/// Finish pending rays and complete callbacks |
118 |
bool FlushQueue(); |
119 |
/// Close octree, free data, return status |
120 |
int Cleanup(bool everything = false) { |
121 |
SetCookedCall(NULL); |
122 |
SetTraceCall(NULL); |
123 |
rtFlags = 0; |
124 |
UpdateMode(); |
125 |
lastRayID = 0; |
126 |
return RadSimulManager::Cleanup(everything); |
127 |
} |
128 |
}; |
129 |
|
130 |
/// Determine if vector is all zeroes |
131 |
inline bool |
132 |
IsZeroVec(const FVECT vec) |
133 |
{ |
134 |
return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0); |
135 |
} |
136 |
|
137 |
#endif /* RtraceSimulManager_h */ |