ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.21
Committed: Mon Jan 5 17:40:28 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.20: +33 -0 lines
Log Message:
added statistics code for DEBUG mode (doesn't work with pauses)

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Holodeck display process.
9     */
10    
11     #include "rholo.h"
12     #include "rhdisp.h"
13     #include "rhdriver.h"
14     #include "selcall.h"
15 gregl 3.10 #include <ctype.h>
16 gregl 3.1
17 gregl 3.12 #ifndef VIEWHISTLEN
18 gregl 3.16 #define VIEWHISTLEN 4 /* number of remembered views */
19 gregl 3.12 #endif
20    
21 gregl 3.1 HOLO *hdlist[HDMAX+1]; /* global holodeck list */
22    
23 gregl 3.10 char cmdlist[DC_NCMDS][8] = DC_INIT;
24    
25 gregl 3.1 int imm_mode = 0; /* bundles are being delivered immediately */
26    
27 gregl 3.20 int do_outside = 0; /* render from outside sections */
28    
29 gregl 3.1 char *progname; /* global argv[0] */
30    
31 gregl 3.10 FILE *sstdin, *sstdout; /* server's standard input and output */
32 gregl 3.1
33 gregl 3.21 #ifdef DEBUG
34     #include <sys/types.h>
35     extern time_t time();
36     static time_t tmodesw;
37     static time_t timm, tadd;
38     static long nimmrays, naddrays;
39     #endif
40    
41 gregl 3.10 #define RDY_SRV 01
42     #define RDY_DEV 02
43     #define RDY_SIN 04
44 gregl 3.1
45 gregl 3.10
46 gregl 3.1 main(argc, argv)
47     int argc;
48     char *argv[];
49     {
50 gregl 3.10 extern int eputs();
51 gregl 3.3 int rdy, inp, res = 0, pause = 0;
52 gregl 3.1
53     progname = argv[0];
54 gregl 3.10 if (argc < 3)
55 gregl 3.1 error(USER, "bad command line arguments");
56     /* open our device */
57     dev_open(argv[1]);
58 gregl 3.10 /* open server process i/o */
59     sstdout = fdopen(atoi(argv[2]), "w");
60     if (argc < 4 || (inp = atoi(argv[3])) < 0)
61     sstdin = NULL;
62     else
63     sstdin = fdopen(inp, "r");
64     /* set command error vector */
65     erract[COMMAND].pf = eputs;
66 gregl 3.21 #ifdef DEBUG
67     tmodesw = time(NULL);
68     #endif
69 gregl 3.1 /* enter main loop */
70     do {
71     rdy = disp_wait();
72 gregl 3.19 if (rdy & RDY_SRV) { /* process server result */
73     res = serv_result();
74     if (pause && res != DS_SHUTDOWN) {
75     serv_request(DR_ATTEN, 0, NULL);
76     while ((res = serv_result()) != DS_ACKNOW &&
77     res != DS_SHUTDOWN)
78     ;
79     }
80     }
81 gregl 3.10 if (rdy & RDY_DEV) { /* user input from driver */
82 gregl 3.3 inp = dev_input();
83 gregl 3.13 if (inp & DFL(DC_SETVIEW))
84     new_view(&odev.v);
85     if (inp & DFL(DC_GETVIEW))
86 gregl 3.10 printview();
87 gregl 3.13 if (inp & DFL(DC_LASTVIEW))
88 gregl 3.12 new_view(NULL);
89 gregl 3.13 if (inp & DFL(DC_RESUME)) {
90 gregl 3.3 serv_request(DR_NOOP, 0, NULL);
91     pause = 0;
92     }
93 gregl 3.15 if (inp & DFL(DC_PAUSE))
94     pause = 1;
95 gregl 3.13 if (inp & DFL(DC_REDRAW))
96     imm_mode = beam_sync() > 0;
97 gregl 3.15 if (inp & DFL(DC_KILL)) {
98 gregl 3.14 serv_request(DR_KILL, 0, NULL);
99 gregl 3.15 pause = 0;
100     }
101 gregl 3.14 if (inp & DFL(DC_CLOBBER))
102     serv_request(DR_CLOBBER, 0, NULL);
103 gregl 3.15 if (inp & DFL(DC_RESTART)) {
104 gregl 3.14 serv_request(DR_RESTART, 0, NULL);
105 gregl 3.15 pause = 0;
106     }
107 gregl 3.13 if (inp & DFL(DC_QUIT))
108     serv_request(DR_SHUTDOWN, 0, NULL);
109 gregl 3.3 }
110 gregl 3.10 if (rdy & RDY_SIN) /* user input from sstdin */
111     switch (usr_input()) {
112     case DC_PAUSE:
113     pause = 1;
114     break;
115     case DC_RESUME:
116     serv_request(DR_NOOP, 0, NULL);
117 gregl 3.15 /* fall through */
118     case DC_KILL:
119     case DC_RESTART:
120 gregl 3.10 pause = 0;
121     break;
122     }
123 gregl 3.1 } while (res != DS_SHUTDOWN);
124 gregl 3.21 #ifdef DEBUG
125     if (timm && nimmrays)
126     fprintf(stderr,
127     "%s: %.1f rays recalled/second (%ld rays total)\n",
128     progname, (double)nimmrays/timm, nimmrays);
129     if (tadd && naddrays)
130     fprintf(stderr,
131     "%s: %.1f rays calculated/second (%ld rays total)\n",
132     progname, (double)naddrays/tadd, naddrays);
133     #endif
134 gregl 3.1 /* all done */
135     quit(0);
136     }
137    
138    
139     int
140     disp_wait() /* wait for more input */
141     {
142     fd_set readset, errset;
143     int flgs;
144     int n;
145     register int i;
146     /* see if we can avoid select call */
147     if (imm_mode || stdin->_cnt > 0)
148 gregl 3.4 return(RDY_SRV);
149 gregl 3.10 if (sstdin != NULL && sstdin->_cnt > 0)
150     return(RDY_SIN);
151 gregl 3.1 if (dev_flush())
152 gregl 3.4 return(RDY_DEV);
153 gregl 3.1 /* make the call */
154     FD_ZERO(&readset); FD_ZERO(&errset);
155     FD_SET(0, &readset);
156     FD_SET(0, &errset);
157     FD_SET(odev.ifd, &readset);
158     FD_SET(odev.ifd, &errset);
159 gregl 3.10 n = odev.ifd+1;
160     if (sstdin != NULL) {
161     FD_SET(fileno(sstdin), &readset);
162     if (fileno(sstdin) >= n)
163     n = fileno(sstdin) + 1;
164     }
165 gregl 3.1 n = select(n, &readset, NULL, &errset, NULL);
166     if (n < 0) {
167     if (errno == EINTR)
168     return(0);
169     error(SYSTEM, "select call failure in disp_wait");
170     }
171     flgs = 0; /* flag what's ready */
172     if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
173 gregl 3.4 flgs |= RDY_SRV;
174 gregl 3.1 if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
175 gregl 3.4 flgs |= RDY_DEV;
176 gregl 3.10 if (sstdin != NULL && FD_ISSET(fileno(sstdin), &readset))
177     flgs |= RDY_SIN;
178 gregl 3.1 return(flgs);
179     }
180    
181    
182     add_holo(hdg) /* register a new holodeck section */
183     HDGRID *hdg;
184     {
185     VIEW nv;
186 gregl 3.20 double d;
187 gregl 3.1 register int hd;
188    
189     for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
190     ;
191     if (hd >= HDMAX)
192     error(INTERNAL, "too many holodeck sections in add_holo");
193     hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
194     if (hdlist[hd] == NULL)
195     error(SYSTEM, "out of memory in add_holo");
196     bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
197     hdcompgrid(hdlist[hd]);
198     if (hd)
199     return;
200     /* set initial viewpoint */
201     copystruct(&nv, &odev.v);
202     VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
203     VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
204     VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
205     fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
206     VCOPY(nv.vup, hdlist[0]->xv[2]);
207 gregl 3.20 if (do_outside) {
208     normalize(nv.vdir);
209     d = VLEN(hdlist[0]->xv[1]);
210     d += VLEN(hdlist[0]->xv[2]);
211     VSUM(nv.vp, nv.vp, nv.vdir, -d);
212     }
213 gregl 3.1 new_view(&nv);
214     }
215    
216    
217     disp_bundle(p) /* display a ray bundle */
218     register PACKHEAD *p;
219     {
220     GCOORD gc[2];
221     FVECT ro, rd, wp;
222     double d;
223     register int i;
224     /* get beam coordinates */
225     if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
226     error(INTERNAL, "bad holodeck number in disp_bundle");
227     if (!hdbcoord(gc, hdlist[p->hd], p->bi))
228     error(INTERNAL, "bad beam index in disp_bundle");
229     /* display each ray */
230     for (i = p->nr; i--; ) {
231     hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
232     d = hddepth(hdlist[p->hd], packra(p)[i].d);
233     VSUM(wp, ro, rd, d); /* might be behind viewpoint */
234 gregl 3.9 dev_value(packra(p)[i].v, wp, rd);
235 gregl 3.1 }
236 gregl 3.21 #ifdef DEBUG
237     if (imm_mode) nimmrays += p->nr;
238     else naddrays += p->nr;
239     #endif
240 gregl 3.1 }
241    
242    
243     new_view(v) /* change view parameters */
244 gregl 3.12 register VIEW *v;
245 gregl 3.1 {
246 gregl 3.12 static VIEW viewhist[VIEWHISTLEN];
247     static unsigned nhist;
248 gregl 3.1 char *err;
249 gregl 3.12 /* restore previous view? */
250     if (v == NULL) {
251 gregl 3.16 if (nhist > 1) /* get one before last setting */
252     nhist--;
253     else /* else go to end of list */
254     while (nhist < VIEWHISTLEN && viewhist[nhist].type)
255     nhist++;
256 gregl 3.12 v = viewhist + ((nhist-1)%VIEWHISTLEN);
257 gregl 3.17 } else
258     again:
259     if ((err = setview(v)) != NULL) {
260 gregl 3.12 error(COMMAND, err);
261     return;
262     }
263     if (v->type == VT_PAR) {
264     error(COMMAND, "cannot handle parallel views");
265     return;
266     }
267     if (!dev_view(v)) /* update display driver */
268     goto again;
269     dev_flush(); /* update screen */
270     if (!beam_view(v)) /* update beam list */
271     goto again;
272     /* record new view */
273     if (v < viewhist || v >= viewhist+VIEWHISTLEN) {
274     copystruct(viewhist + (nhist%VIEWHISTLEN), v);
275     nhist++;
276     }
277 gregl 3.10 }
278    
279    
280     int
281     usr_input() /* get user input and process it */
282     {
283     VIEW vparams;
284     char cmd[128];
285     register char *args;
286 gregl 3.11 register int i;
287 gregl 3.10
288     if (fgets(cmd, sizeof(cmd), sstdin) == NULL)
289     return(DC_QUIT);
290     for (args = cmd; *args && !isspace(*args); args++)
291     ;
292     while (isspace(*args))
293     *args++ = '\0';
294     if (!*cmd)
295     return(DC_RESUME);
296 gregl 3.11 if (*args && args[i=strlen(args)-1] == '\n')
297     args[i] = '\0';
298     for (i = 0; i < DC_NCMDS; i++)
299     if (!strcmp(cmd, cmdlist[i]))
300 gregl 3.10 break;
301 gregl 3.11 if (i >= DC_NCMDS) {
302 gregl 3.10 sprintf(errmsg, "unknown command: %s", cmd);
303     error(COMMAND, errmsg);
304     return(-1);
305     }
306 gregl 3.11 switch (i) {
307 gregl 3.10 case DC_SETVIEW: /* set the view */
308     copystruct(&vparams, &odev.v);
309     if (!sscanview(&vparams, args))
310     error(COMMAND, "missing view options");
311     else
312     new_view(&vparams);
313     break;
314     case DC_GETVIEW: /* print the current view */
315     printview();
316 gregl 3.12 break;
317     case DC_LASTVIEW: /* restore previous view */
318     new_view(NULL);
319 gregl 3.10 break;
320     case DC_PAUSE: /* pause the current calculation */
321     case DC_RESUME: /* resume the calculation */
322     /* handled in main() */
323 gregl 3.13 break;
324     case DC_REDRAW: /* redraw from server */
325     imm_mode = beam_sync() > 0;
326 gregl 3.14 break;
327     case DC_KILL: /* kill rtrace process(es) */
328     serv_request(DR_KILL, 0, NULL);
329     break;
330     case DC_CLOBBER: /* clobber holodeck */
331     serv_request(DR_CLOBBER, 0, NULL);
332     break;
333     case DC_RESTART: /* restart rtrace */
334     serv_request(DR_RESTART, 0, NULL);
335 gregl 3.10 break;
336     case DC_QUIT: /* quit request */
337     serv_request(DR_SHUTDOWN, 0, NULL);
338     break;
339     default:
340     error(CONSISTENCY, "bad command id in usr_input");
341     }
342 gregl 3.11 return(i);
343 gregl 3.10 }
344    
345    
346     printview() /* print our current view to server stdout */
347     {
348     fputs(VIEWSTR, sstdout);
349     fprintview(&odev.v, sstdout);
350     fputc('\n', sstdout);
351     fflush(sstdout);
352 gregl 3.1 }
353    
354    
355     int
356     serv_result() /* get next server result and process it */
357     {
358     static char *buf = NULL;
359     static int bufsiz = 0;
360     MSGHEAD msg;
361     int n;
362     /* read message header */
363     if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
364     goto readerr;
365     if (msg.nbytes > 0) { /* get the message body */
366     if (msg.nbytes > bufsiz) {
367     if (buf == NULL)
368     buf = (char *)malloc(bufsiz=msg.nbytes);
369     else
370     buf = (char *)realloc(buf, bufsiz=msg.nbytes);
371     if (buf == NULL)
372     error(SYSTEM, "out of memory in serv_result");
373     }
374     if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
375     goto readerr;
376     }
377     switch (msg.type) { /* process results */
378     case DS_BUNDLE:
379     if (msg.nbytes < sizeof(PACKHEAD) ||
380     msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
381     error(INTERNAL, "bad display packet from server");
382     disp_bundle((PACKHEAD *)buf);
383     break;
384     case DS_ADDHOLO:
385     if (msg.nbytes != sizeof(HDGRID))
386     error(INTERNAL, "bad holodeck record from server");
387     add_holo((HDGRID *)buf);
388 gregl 3.20 break;
389     case DS_OUTSECT:
390     do_outside = 1;
391 gregl 3.1 break;
392     case DS_STARTIMM:
393     case DS_ENDIMM:
394 gregl 3.21 #ifdef DEBUG
395     if (imm_mode != (msg.type==DS_STARTIMM)) {
396     time_t tnow = time(NULL);
397     if (imm_mode) timm += tnow - tmodesw;
398     else tadd += tnow - tmodesw;
399     tmodesw = tnow;
400     }
401     #endif
402 gregl 3.1 imm_mode = msg.type==DS_STARTIMM;
403     /* fall through */
404     case DS_ACKNOW:
405     case DS_SHUTDOWN:
406     if (msg.nbytes) {
407     sprintf(errmsg,
408     "unexpected body with server message %d",
409     msg.type);
410     error(INTERNAL, errmsg);
411     }
412     break;
413     default:
414     error(INTERNAL, "unrecognized result from server process");
415     }
416     return(msg.type); /* return message type */
417     readerr:
418     if (feof(stdin))
419     error(SYSTEM, "server process died");
420     error(SYSTEM, "error reading from server process");
421     }
422    
423    
424     serv_request(type, nbytes, p) /* send a request to the server process */
425     int type, nbytes;
426     char *p;
427     {
428     MSGHEAD msg;
429     int m;
430     /* get server's attention for big request */
431     if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
432     serv_request(DR_ATTEN, 0, NULL);
433     while ((m = serv_result()) != DS_ACKNOW)
434     if (m == DS_SHUTDOWN) /* the bugger quit on us */
435     quit(0);
436     }
437     msg.type = type; /* write and flush the message */
438     msg.nbytes = nbytes;
439     fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
440     if (nbytes > 0)
441     fwrite(p, 1, nbytes, stdout);
442     if (fflush(stdout) < 0)
443     error(SYSTEM, "write error in serv_request");
444 gregl 3.2 }
445    
446    
447     eputs(s) /* put error message to stderr */
448     register char *s;
449     {
450     static int midline = 0;
451    
452     if (!*s)
453     return;
454     if (!midline++) { /* prepend line with program name */
455     fputs(progname, stderr);
456     fputs(": ", stderr);
457     }
458     fputs(s, stderr);
459     if (s[strlen(s)-1] == '\n') {
460     fflush(stderr);
461     midline = 0;
462     }
463 gregl 3.1 }
464    
465    
466     quit(code) /* clean up and exit */
467     int code;
468     {
469     if (odev.v.type)
470     dev_close();
471     exit(code);
472     }