ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RtraceSimulManager.h
Revision: 2.7
Committed: Wed May 1 22:06:09 2024 UTC (5 weeks, 3 days ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +2 -1 lines
Log Message:
fix(rxtrace): Fixed spectral sampling set-up

File Contents

# User Rev Content
1 greg 2.7 /* RCSid $Id: RtraceSimulManager.h,v 2.6 2024/04/30 23:16:23 greg Exp $ */
2 greg 2.1 /*
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 greg 2.6 extern int castonly; // doing ray-casting only?
20    
21     /// Ray reporting callback method -- returns # successfully reported, -1 to abort
22     typedef int RayReportCall(RAY *r, void *cd);
23 greg 2.1
24     /// Multi-threaded simulation manager base class
25     class RadSimulManager {
26 greg 2.4 protected:
27     // Assign ray to subthread (fails if NThreads()<2)
28     bool SplitRay(RAY *r);
29 greg 2.1 public:
30     RadSimulManager(const char *octn = NULL) {
31     LoadOctree(octn);
32     }
33     ~RadSimulManager() {
34     Cleanup();
35     }
36     /// Load octree and prepare renderer
37     bool LoadOctree(const char *octn);
38 greg 2.5 /// How many cores are available?
39 greg 2.3 static int GetNCores();
40 greg 2.1 /// Set number of computation threads (0 => #cores)
41     int SetThreadCount(int nt = 0);
42     /// Check thread count (1 means no multi-threading)
43     int NThreads() const {
44 greg 2.6 return ray_pnprocs + !ray_pnprocs;
45 greg 2.1 }
46     /// How many threads are currently unoccupied?
47     int ThreadsAvailable() const;
48     /// Are we ready?
49     bool Ready() const {
50 greg 2.6 return (octname && nobjects > 0);
51 greg 2.1 }
52 greg 2.4 /// Process a ray (in subthread), optional result
53     bool ProcessRay(RAY *r);
54     /// Wait for next result (or fail)
55     bool WaitResult(RAY *r);
56 greg 2.1 /// Close octree, free data, return status
57 greg 2.3 int Cleanup(bool everything = false);
58 greg 2.1 };
59    
60     /// Flags to control rendering operations
61     enum {RTdoFIFO=1, RTtraceSources=2, RTlimDist=4, RTimmIrrad=8, RTmask=15};
62    
63     /// rtrace-like simulation manager (at most one such object)
64     class RtraceSimulManager : public RadSimulManager {
65     RayReportCall * cookedCall; // callback for cooked primary rays
66     void * ccData; // client data for cooked primary rays
67     RayReportCall * traceCall; // call for every ray in tree
68     void * tcData; // client data for traced rays
69     int curFlags; // current operating flags
70     // Call-back for global ray-tracing context
71     static void RTracer(RAY *r);
72 greg 2.6 // Call-back for FIFO
73     static int Rfifout(RAY *r);
74 greg 2.1 // Check for changes to render flags, etc.
75     bool UpdateMode();
76     protected:
77     RNUMBER lastRayID; // last ray ID assigned
78     public:
79     int rtFlags; // operation (RT*) flags
80     RtraceSimulManager(RayReportCall *cb = NULL, void *cd = NULL,
81     const char *octn = NULL) : RadSimulManager(octn) {
82     lastRayID = 0;
83     rtFlags = curFlags = 0;
84     SetCookedCall(cb, cd);
85     traceCall = NULL; tcData = NULL;
86     }
87 greg 2.3 ~RtraceSimulManager() {
88     FlushQueue();
89     }
90 greg 2.2 /// Set number of computation threads (0 => #cores)
91     int SetThreadCount(int nt = 0) {
92 greg 2.6 if (nt <= 0) nt = castonly ? 1 : GetNCores();
93 greg 2.2 if (nt == NThreads()) return nt;
94 greg 2.6 if (FlushQueue() < 0) return 0;
95 greg 2.2 return RadSimulManager::SetThreadCount(nt);
96     }
97 greg 2.1 /// Add ray bundle to queue w/ optional 1st ray ID
98     int EnqueueBundle(const FVECT orig_direc[], int n,
99     RNUMBER rID0 = 0);
100     /// Enqueue a single ray w/ optional ray ID
101     bool EnqueueRay(const FVECT org, const FVECT dir,
102     RNUMBER rID = 0) {
103     if (dir == org+1)
104 greg 2.3 return(EnqueueBundle((const FVECT *)org, 1, rID) > 0);
105 greg 2.1 FVECT orgdir[2];
106     VCOPY(orgdir[0], org); VCOPY(orgdir[1], dir);
107 greg 2.3 return(EnqueueBundle(orgdir, 1, rID) > 0);
108 greg 2.1 }
109 greg 2.6 /// Set/change cooked ray callback
110 greg 2.1 void SetCookedCall(RayReportCall *cb, void *cd = NULL) {
111     if (cookedCall && (cookedCall != cb) | (ccData != cd))
112     FlushQueue();
113     cookedCall = cb;
114 greg 2.6 ccData = cb ? cd : NULL;
115 greg 2.1 }
116     /// Set/change trace callback
117     void SetTraceCall(RayReportCall *cb, void *cd = NULL) {
118     traceCall = cb;
119 greg 2.6 tcData = cb ? cd : NULL;
120 greg 2.1 }
121     /// Are we ready?
122     bool Ready() const {
123     return (cookedCall != NULL) | (traceCall != NULL) &&
124     RadSimulManager::Ready();
125     }
126 greg 2.4 /// Finish pending rays and complete callbacks (return #sent)
127     int FlushQueue();
128 greg 2.1 /// Close octree, free data, return status
129 greg 2.3 int Cleanup(bool everything = false) {
130 greg 2.7 FlushQueue();
131 greg 2.1 SetCookedCall(NULL);
132     SetTraceCall(NULL);
133     rtFlags = 0;
134     UpdateMode();
135     lastRayID = 0;
136 greg 2.3 return RadSimulManager::Cleanup(everything);
137 greg 2.1 }
138     };
139    
140     /// Determine if vector is all zeroes
141     inline bool
142     IsZeroVec(const FVECT vec)
143     {
144     return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
145     }
146    
147     #endif /* RtraceSimulManager_h */