1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* sm_samp.c |
9 |
*/ |
10 |
#include "standard.h" |
11 |
#include "sm_flag.h" |
12 |
#include "rhd_sample.h" |
13 |
|
14 |
SAMP rsL; |
15 |
int4 *samp_flag=NULL; |
16 |
|
17 |
|
18 |
/* Each sample has a world coord point, and direction, brightness,chrominance, |
19 |
and RGB triples |
20 |
*/ |
21 |
#ifndef HP_VERSION |
22 |
#define TMSIZE sizeof(TMbright) |
23 |
#else |
24 |
#define TMSIZE 0 |
25 |
#endif |
26 |
#define SAMPSIZ (3*sizeof(float)+sizeof(int4)+ 6*sizeof(BYTE) + TMSIZE) |
27 |
|
28 |
/* Extra points just need direction and world space point */ |
29 |
#define POINTSIZ (3*sizeof(float)) |
30 |
|
31 |
/* Clear the flags for all samples */ |
32 |
sClear_all_flags(s) |
33 |
SAMP *s; |
34 |
{ |
35 |
if(samp_flag) |
36 |
bzero((char *)samp_flag,FLAG_BYTES(S_MAX_SAMP(s))); |
37 |
} |
38 |
|
39 |
sInit(s) |
40 |
SAMP *s; |
41 |
{ |
42 |
S_REPLACE_SAMP(s) = 0; |
43 |
S_NUM_SAMP(s) = 0; |
44 |
S_TONE_MAP(s) = 0; |
45 |
S_FREE_SAMP(s) = -1; |
46 |
sClear_base_points(s); |
47 |
sClear_all_flags(s); |
48 |
|
49 |
} |
50 |
|
51 |
sFree(s) |
52 |
SAMP *s; |
53 |
{ |
54 |
if(S_BASE(s)) |
55 |
{ |
56 |
free(S_BASE(s)); |
57 |
S_BASE(s)=NULL; |
58 |
} |
59 |
if(samp_flag) |
60 |
{ |
61 |
free(samp_flag); |
62 |
samp_flag = NULL; |
63 |
} |
64 |
} |
65 |
/* Initialize/clear global smL sample list for at least n samples */ |
66 |
SAMP |
67 |
*sAlloc(nptr,extra_points) |
68 |
int *nptr,extra_points; |
69 |
{ |
70 |
unsigned nbytes; |
71 |
register unsigned i; |
72 |
SAMP *s; |
73 |
int n; |
74 |
|
75 |
s = &rsL; |
76 |
/* round space up to nearest power of 2 */ |
77 |
nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8; |
78 |
for (i = 1024; nbytes > i; i <<= 1) |
79 |
; |
80 |
/* get number of samples that fit in block */ |
81 |
n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ; |
82 |
|
83 |
S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ); |
84 |
if (!S_BASE(s)) |
85 |
error(SYSTEM,"smInit_samples(): Unable to allocate memory"); |
86 |
|
87 |
/* assign larger alignment types earlier */ |
88 |
S_W_PT(s) = (float (*)[3])S_BASE(s); |
89 |
S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points); |
90 |
#ifndef HP_VERSION |
91 |
S_BRT(s) = (TMbright *)(S_W_DIR(s) + n); |
92 |
S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n); |
93 |
#else |
94 |
S_CHR(s) = (BYTE (*)[3])(S_W_DIR(s) + n); |
95 |
#endif |
96 |
S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n); |
97 |
S_MAX_SAMP(s) = n; |
98 |
S_MAX_BASE_PT(s) = n + extra_points; |
99 |
|
100 |
/* Allocate memory for a per/sample bit flag */ |
101 |
if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n)))) |
102 |
error(SYSTEM,"smInit_samples(): Unable to allocate flag memory"); |
103 |
|
104 |
sInit(s); |
105 |
*nptr = n; |
106 |
|
107 |
return(s); |
108 |
} |
109 |
|
110 |
|
111 |
/* Add a base point to the sample structure: only the world point |
112 |
is added: These points are not displayed-they are used to form the |
113 |
initial mesh |
114 |
*/ |
115 |
int |
116 |
sAdd_base_point(s,v) |
117 |
SAMP *s; |
118 |
FVECT v; |
119 |
{ |
120 |
int id; |
121 |
|
122 |
/* Get pointer to next available point */ |
123 |
id = S_NEXT_BASE_PT(s); |
124 |
if(id >= S_MAX_BASE_PT(s)) |
125 |
return(-1); |
126 |
|
127 |
/* Update free aux pointer */ |
128 |
S_NEXT_BASE_PT(s)++; |
129 |
|
130 |
/* Copy vector into point */ |
131 |
VCOPY(S_NTH_W_PT(s,id),v); |
132 |
return(id); |
133 |
} |
134 |
|
135 |
/* Initialize the sample 'id' to contain world point 'p', direction 'd' and |
136 |
color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In |
137 |
this case, the world point is set to the projection of the direction on |
138 |
the view sphere, and the direction value is set to -1 |
139 |
*/ |
140 |
void |
141 |
sInit_samp(s,id,c,d,p) |
142 |
SAMP *s; |
143 |
int id; |
144 |
COLR c; |
145 |
FVECT d,p; |
146 |
{ |
147 |
/* Copy vector into point */ |
148 |
VCOPY(S_NTH_W_PT(s,id),p); |
149 |
if(d) |
150 |
S_NTH_W_DIR(s,id) = encodedir(d); |
151 |
/* direction only */ |
152 |
else |
153 |
S_NTH_W_DIR(s,id) = -1; |
154 |
|
155 |
/* calculate the brightness and chrominance */ |
156 |
#ifndef TEST_DRIVER |
157 |
tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1); |
158 |
#else |
159 |
if(c) |
160 |
{ |
161 |
S_NTH_RGB(s,id)[0] = c[0]; |
162 |
S_NTH_RGB(s,id)[1] = c[1]; |
163 |
S_NTH_RGB(s,id)[2] = c[2]; |
164 |
} |
165 |
else |
166 |
{ |
167 |
S_NTH_RGB(s,id)[0] = 100; |
168 |
S_NTH_RGB(s,id)[1] = 0; |
169 |
S_NTH_RGB(s,id)[2] = 0; |
170 |
} |
171 |
#endif |
172 |
/* Set ACTIVE bit upon creation */ |
173 |
S_SET_FLAG(id); |
174 |
|
175 |
} |
176 |
|
177 |
|
178 |
/* Copy the values of sample n_id into sample id: Note must not call with |
179 |
Base point n_id |
180 |
*/ |
181 |
int |
182 |
sReset_samp(s,n_id,id) |
183 |
SAMP *s; |
184 |
int n_id,id; |
185 |
{ |
186 |
|
187 |
#ifdef DEBUG |
188 |
if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s)) |
189 |
{ |
190 |
eputs("smReset_sample():invalid sample id\n"); |
191 |
return(0); |
192 |
} |
193 |
#endif |
194 |
|
195 |
/* Copy vector into point */ |
196 |
VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id)); |
197 |
S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id); |
198 |
|
199 |
#ifndef HP_VERSION |
200 |
S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id); |
201 |
#endif |
202 |
S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0]; |
203 |
S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1]; |
204 |
S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2]; |
205 |
return(1); |
206 |
} |
207 |
|
208 |
/* Allocate a new sample. If an existing sample was replaced: set flag */ |
209 |
int |
210 |
sAlloc_samp(s,replaced) |
211 |
SAMP *s; |
212 |
int *replaced; |
213 |
|
214 |
{ |
215 |
int id; |
216 |
|
217 |
if(replaced) |
218 |
*replaced = 0; |
219 |
|
220 |
if((id = S_FREE_SAMP(s)) != INVALID) |
221 |
{ |
222 |
S_FREE_SAMP(s) = INVALID; |
223 |
return(id); |
224 |
} |
225 |
/* If havn't reached end of sample array:return next sample */ |
226 |
if(S_NUM_SAMP(s) < S_MAX_SAMP(s)) |
227 |
id = S_NUM_SAMP(s)++; |
228 |
else |
229 |
{ |
230 |
/* CLOCKED LRU replacement policy: Search through samples starting |
231 |
with S_REPLACE_SAMP for a sample that does not have its active |
232 |
bit set. Clear bit along the way |
233 |
*/ |
234 |
id = S_REPLACE_SAMP(s); |
235 |
while(S_IS_FLAG(id)) |
236 |
{ |
237 |
S_CLR_FLAG(id); |
238 |
id = (id +1)% S_MAX_SAMP(s); |
239 |
} |
240 |
S_REPLACE_SAMP(s) = id = (id +1)% S_MAX_SAMP(s); |
241 |
if(replaced) |
242 |
*replaced = 1; |
243 |
} |
244 |
return(id); |
245 |
} |
246 |
|
247 |
/* Set the sample flag to be unused- so will get replaced on next LRU |
248 |
iteration |
249 |
*/ |
250 |
|
251 |
/* NOTE: Do we want a free list as well? */ |
252 |
int |
253 |
sDelete_samp(s,id) |
254 |
SAMP *s; |
255 |
int id; |
256 |
{ |
257 |
#ifdef DEBUG |
258 |
/* Fist check for a valid id */ |
259 |
if(id >= S_MAX_SAMP(s) || id < 0) |
260 |
return(0); |
261 |
#endif |
262 |
|
263 |
S_CLR_FLAG(id); |
264 |
return(1); |
265 |
} |
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|