20 |
|
* is desired, a negative value may be returned. |
21 |
|
* |
22 |
|
* The ray_fifo_in() call takes a ray that has been initialized in |
23 |
< |
* the same manner as for the ray_trace() call, i.e., the origin, |
24 |
< |
* direction, and maximum length have been assigned. The ray number |
25 |
< |
* will be set according to the number of rays traced since the |
23 |
> |
* the same manner as for the ray_pqueue() call, i.e., rayorigin() |
24 |
> |
* has been called and the origin, direction and maximum distance |
25 |
> |
* have all been assigned. However, the ray number will be reset |
26 |
> |
* by ray_fifo_in() according to the number of rays traced since the |
27 |
|
* last call to ray_fifo_flush(). This final call completes all |
28 |
|
* pending ray calculations and frees the FIFO buffer. If any of |
29 |
|
* the automatic calls to the ray_fifo_out callback return a |
30 |
|
* negative value, processing stops and -1 is returned. |
31 |
+ |
* |
32 |
+ |
* Note: The ray passed to ray_fifo_in() may be overwritten |
33 |
+ |
* arbitrarily, since it is passed to ray_pqueue(). |
34 |
|
*/ |
35 |
|
|
36 |
|
#include "ray.h" |
42 |
|
static int r_fifo_len = 0; /* allocated FIFO length */ |
43 |
|
static RNUMBER r_fifo_start = 1; /* first awaited ray */ |
44 |
|
static RNUMBER r_fifo_end = 1; /* one past FIFO last */ |
45 |
+ |
static RNUMBER r_fifo_next = 1; /* next ray assignment */ |
46 |
|
|
47 |
|
#define r_fifo(rn) (&r_fifo_buf[(rn)&(r_fifo_len-1)]) |
48 |
|
|
55 |
|
int i; |
56 |
|
|
57 |
|
if (r_fifo_buf == NULL) |
58 |
< |
r_fifo_len = 1<<4; |
58 |
> |
r_fifo_len = 1<<5; |
59 |
|
else |
60 |
|
r_fifo_len <<= 1; |
61 |
|
/* allocate new */ |
81 |
|
|
82 |
|
if (ray_fifo_out == NULL) |
83 |
|
error(INTERNAL, "ray_fifo_out is NULL"); |
84 |
< |
if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_end)) |
85 |
< |
error(INTERNAL, "unexpected ray number in ray_fifo_push"); |
84 |
> |
if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_next)) |
85 |
> |
error(INTERNAL, "unexpected ray number in ray_fifo_push()"); |
86 |
|
|
87 |
|
if (r->rno > r_fifo_start) { /* insert into output queue */ |
88 |
< |
if (r->rno - r_fifo_start >= r_fifo_len) |
89 |
< |
ray_fifo_growbuf(); |
88 |
> |
while (r->rno - r_fifo_start >= r_fifo_len) |
89 |
> |
ray_fifo_growbuf(); /* need more space */ |
90 |
|
*r_fifo(r->rno) = *r; |
91 |
+ |
if (r->rno >= r_fifo_end) |
92 |
+ |
r_fifo_end = r->rno + 1; |
93 |
|
return(0); |
94 |
|
} |
95 |
|
/* r->rno == r_fifo_start, so transfer ray(s) */ |
96 |
|
do { |
97 |
< |
if ((rv = (*ray_fifo_out)(r)) < 0) |
97 |
> |
rv = (*ray_fifo_out)(r); |
98 |
> |
r->rno = 0; /* flag this entry complete */ |
99 |
> |
if (rv < 0) |
100 |
|
return(-1); |
101 |
|
nsent += rv; |
102 |
|
if (++r_fifo_start < r_fifo_end) |
103 |
|
r = r_fifo(r_fifo_start); |
104 |
+ |
else if (r_fifo_start > r_fifo_end) |
105 |
+ |
r_fifo_end = r_fifo_start; |
106 |
|
} while (r->rno == r_fifo_start); |
107 |
|
|
108 |
|
return(nsent); |
114 |
|
RAY *r |
115 |
|
) |
116 |
|
{ |
117 |
< |
int rv, rval = 0; |
117 |
> |
static int incall = 0; /* prevent recursion */ |
118 |
> |
int rv, rval = 0; |
119 |
|
|
120 |
< |
if (r_fifo_start >= 1L<<30) { /* reset our counter */ |
120 |
> |
if (incall++) |
121 |
> |
error(INTERNAL, "recursive call to ray_fifo_in()"); |
122 |
> |
|
123 |
> |
if (r_fifo_start >= 1L<<30) { /* reset our counters */ |
124 |
|
if ((rv = ray_fifo_flush()) < 0) |
125 |
< |
return(-1); |
125 |
> |
{--incall; return(-1);} |
126 |
|
rval += rv; |
127 |
|
} |
128 |
|
/* queue ray */ |
129 |
< |
rayorigin(r, PRIMARY, NULL, NULL); |
115 |
< |
r->rno = r_fifo_end++; |
129 |
> |
r->rno = r_fifo_next++; |
130 |
|
if ((rv = ray_pqueue(r)) < 0) |
131 |
< |
return(-1); |
131 |
> |
{--incall; return(-1);} |
132 |
|
|
133 |
|
if (!rv) /* no result this time? */ |
134 |
< |
return(rval); |
135 |
< |
/* else send/queue result */ |
136 |
< |
if ((rv = ray_fifo_push(r)) < 0) |
137 |
< |
return(-1); |
138 |
< |
rval += rv; |
139 |
< |
return(rval); |
134 |
> |
{--incall; return(rval);} |
135 |
> |
|
136 |
> |
do { /* else send/queue result */ |
137 |
> |
if ((rv = ray_fifo_push(r)) < 0) |
138 |
> |
{--incall; return(-1);} |
139 |
> |
rval += rv; |
140 |
> |
|
141 |
> |
} while (ray_presult(r, -1) > 0); /* empty in-core queue */ |
142 |
> |
|
143 |
> |
--incall; return(rval); |
144 |
|
} |
145 |
|
|
146 |
|
|
154 |
|
(rv = ray_fifo_push(&myRay)) >= 0) |
155 |
|
rval += rv; |
156 |
|
|
157 |
< |
if (rv < 0) /* check for error */ |
157 |
> |
if (rv < 0) /* check for exception */ |
158 |
|
return(-1); |
159 |
|
|
160 |
|
if (r_fifo_start != r_fifo_end) |
161 |
< |
error(INTERNAL, "could not empty queue in ray_fifo_flush"); |
161 |
> |
error(INTERNAL, "could not empty queue in ray_fifo_flush()"); |
162 |
|
|
163 |
< |
free(r_fifo_buf); |
164 |
< |
r_fifo_buf = NULL; |
165 |
< |
r_fifo_len = 0; |
166 |
< |
r_fifo_end = r_fifo_start = 1; |
163 |
> |
if (r_fifo_buf != NULL) { |
164 |
> |
free(r_fifo_buf); |
165 |
> |
r_fifo_buf = NULL; r_fifo_len = 0; |
166 |
> |
} |
167 |
> |
r_fifo_next = r_fifo_end = r_fifo_start = 1; |
168 |
|
|
169 |
|
return(rval); |
170 |
|
} |