ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RtraceSimulManager.h
(Generate patch)

Comparing ray/src/rt/RtraceSimulManager.h (file contents):
Revision 2.3 by greg, Mon Jul 31 23:14:02 2023 UTC vs.
Revision 2.21 by greg, Thu Jan 2 16:16:49 2025 UTC

# Line 13 | Line 13
13   #define RtraceSimulManager_h
14  
15   #include "ray.h"
16 + #include "abitmap.h"
17  
18   extern char *   octname;        // global octree name
19  
20 < /// Ray reporting callback method
20 < typedef void    RayReportCall(RAY *r, void *cd);
20 > extern int      castonly;       // doing ray-casting only?
21  
22 + /// Ray reporting callback method -- returns # successfully reported, -1 to abort
23 + typedef int     RayReportCall(RAY *r, void *cd);
24 +
25   /// Multi-threaded simulation manager base class
26   class RadSimulManager {
27 <        int                     nThreads;       // number of active threads
27 >        char *                  header;                 // header (less intro and format)
28 >        int                     hlen;                   // header string length
29 > protected:
30 >        bool                    SplitRay(RAY *r) {
31 >                                        return (ray_pnprocs && ray_psend(r) > 0);
32 >                                }
33   public:
34                                  RadSimulManager(const char *octn = NULL) {
35 +                                        header = NULL; hlen = 0;
36                                          LoadOctree(octn);
28                                        nThreads = 1;
37                                  }
38                                  ~RadSimulManager() {
39 <                                        Cleanup();
39 >                                        // Cleanup();
40                                  }
41                                  /// Load octree and prepare renderer
42          bool                    LoadOctree(const char *octn);
43 <                                /// How many cores are there?
43 >                                /// Prepare header from previous input (or clear)
44 >                                /// Normally called during octree load
45 >        bool                    NewHeader(const char *inspec = NULL);
46 >                                /// Add a line to header (adds newline if none)
47 >        bool                    AddHeader(const char *str);
48 >                                /// Append program line to header
49 >        bool                    AddHeader(int ac, char *av[]);
50 >                                /// Get current header length in bytes
51 >        int                     GetHeadLen() const {
52 >                                        return hlen;
53 >                                }
54 >                                /// Get header lines or empty string
55 >        const char *            GetHeadStr() const {
56 >                                        return hlen ? header : "";
57 >                                }
58 >                                /// Look for specific header keyword, return value
59 >        const char *            GetHeadStr(const char *key, bool inOK = false) const;
60 >                                /// How many cores are available?
61          static int              GetNCores();
62                                  /// Set number of computation threads (0 => #cores)
63          int                     SetThreadCount(int nt = 0);
64                                  /// Check thread count (1 means no multi-threading)
65          int                     NThreads() const {
66 <                                        return nThreads;
66 >                                        return ray_pnprocs + !ray_pnprocs;
67                                  }
68                                  /// How many threads are currently unoccupied?
69 <        int                     ThreadsAvailable() const;
69 >        int                     ThreadsAvailable() const {
70 >                                        return ray_pnprocs ? ray_pnidle : 1;
71 >                                }
72                                  /// Are we ready?
73          bool                    Ready() const {
74 <                                        return (octname && nsceneobjs > 0);
74 >                                        return (octname && nobjects > 0);
75                                  }
76 +                                /// Process a ray (in subthread), optional result
77 +        int                     ProcessRay(RAY *r);
78 +                                /// Wait for next result (or fail)
79 +        bool                    WaitResult(RAY *r);
80                                  /// Close octree, free data, return status
81          int                     Cleanup(bool everything = false);
82   };
# Line 60 | Line 91 | class RtraceSimulManager : public RadSimulManager {
91          RayReportCall *         traceCall;      // call for every ray in tree
92          void *                  tcData;         // client data for traced rays
93          int                     curFlags;       // current operating flags
94 +        ABitMap                 srcFollowed;    // source flags changed
95                                  // Call-back for global ray-tracing context
96          static void             RTracer(RAY *r);
97 +                                // Call-back for FIFO
98 +        static int              Rfifout(RAY *r);
99 + protected:
100                                  // Check for changes to render flags, etc.
101          bool                    UpdateMode();
67 protected:
102          RNUMBER                 lastRayID;      // last ray ID assigned
103   public:
104          int                     rtFlags;        // operation (RT*) flags
# Line 78 | Line 112 | class RtraceSimulManager : public RadSimulManager {
112                                  ~RtraceSimulManager() {
113                                          FlushQueue();
114                                  }
115 +                                /// Load octree and prepare renderer
116 +        bool                    LoadOctree(const char *octn) {
117 +                                        if ((octn != NULL) & (octname != NULL) &&
118 +                                                        !strcmp(octn, octname))
119 +                                                return true;
120 +                                        srcFollowed.NewBitMap(0);
121 +                                        curFlags &= ~RTtraceSources;
122 +                                        return RadSimulManager::LoadOctree(octn);
123 +                                }
124                                  /// Set number of computation threads (0 => #cores)
125          int                     SetThreadCount(int nt = 0) {
126 <                                        if (nt <= 0) nt = GetNCores();
126 >                                        if (nt <= 0) nt = castonly ? 1 : GetNCores();
127                                          if (nt == NThreads()) return nt;
128 <                                        FlushQueue();
128 >                                        if (nt < NThreads() && FlushQueue() < 0) return 0;
129                                          return RadSimulManager::SetThreadCount(nt);
130                                  }
131                                  /// Add ray bundle to queue w/ optional 1st ray ID
132          int                     EnqueueBundle(const FVECT orig_direc[], int n,
133                                                  RNUMBER rID0 = 0);
134                                  /// Enqueue a single ray w/ optional ray ID
135 <        bool                    EnqueueRay(const FVECT org, const FVECT dir,
135 >        int                     EnqueueRay(const FVECT org, const FVECT dir,
136                                                  RNUMBER rID = 0) {
137                                          if (dir == org+1)
138                                                  return(EnqueueBundle((const FVECT *)org, 1, rID) > 0);
139                                          FVECT   orgdir[2];
140                                          VCOPY(orgdir[0], org); VCOPY(orgdir[1], dir);
141 <                                        return(EnqueueBundle(orgdir, 1, rID) > 0);
141 >                                        return EnqueueBundle(orgdir, 1, rID);
142                                  }
143 <                                /// Set/change cooked ray callback & FIFO flag
143 >                                /// Set/change cooked ray callback
144          void                    SetCookedCall(RayReportCall *cb, void *cd = NULL) {
145                                          if (cookedCall && (cookedCall != cb) | (ccData != cd))
146                                                  FlushQueue();
147                                          cookedCall = cb;
148 <                                        ccData = cd;
148 >                                        ccData = cb ? cd : NULL;
149                                  }
150                                  /// Set/change trace callback
151          void                    SetTraceCall(RayReportCall *cb, void *cd = NULL) {
152 +                                        if ((cb == traceCall) & (cd == tcData)) return;
153 +                                        int     nt = NThreads();
154 +                                        if (nt > 1) SetThreadCount(1);
155                                          traceCall = cb;
156 <                                        tcData = cd;
156 >                                        tcData = cb ? cd : NULL;
157 >                                        UpdateMode();
158 >                                        if (nt > 1) SetThreadCount(nt);
159                                  }
160 <                                /// Are we ready?
161 <        bool                    Ready() const {
114 <                                        return (cookedCall != NULL) | (traceCall != NULL) &&
115 <                                                RadSimulManager::Ready();
116 <                                }
117 <                                /// Finish pending rays and complete callbacks
118 <        bool                    FlushQueue();
160 >                                /// Finish pending rays and complete callbacks (return #sent)
161 >        int                     FlushQueue();
162                                  /// Close octree, free data, return status
163          int                     Cleanup(bool everything = false) {
164 <                                        SetCookedCall(NULL);
165 <                                        SetTraceCall(NULL);
166 <                                        rtFlags = 0;
164 >                                        int     st = RadSimulManager::Cleanup(everything);
165 >                                        srcFollowed.NewBitMap(0);
166 >                                        curFlags &= ~RTtraceSources;
167 >                                        if (everything) {
168 >                                                SetCookedCall(NULL);
169 >                                                SetTraceCall(NULL);
170 >                                                rtFlags &= ~RTmask;
171 >                                        }
172                                          UpdateMode();
173                                          lastRayID = 0;
174 <                                        return RadSimulManager::Cleanup(everything);
174 >                                        return st;
175                                  }
176   };
177  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines