ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RtraceSimulManager.h
Revision: 2.2
Committed: Wed Jul 26 23:27:44 2023 UTC (21 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +11 -1 lines
Log Message:
feat: Preparing for multi-threading

File Contents

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