mesa-tnl-0-to-NULL.patch from Jeff Muizelaar
[mesa.git] / src / mesa / tnl / t_vertex.c
1 /*
2 * Copyright 2003 Tungsten Graphics, inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "colormac.h"
31
32 #include "t_context.h"
33 #include "t_vertex.h"
34
35
36 /* Build and manage clipspace/ndc/window vertices.
37 *
38 * Another new mechanism designed and crying out for codegen. Before
39 * that, it would be very interesting to investigate the merger of
40 * these vertices and those built in t_vtx_*.
41 */
42
43
44
45
46
47
48 /*
49 * These functions take the NDC coordinates pointed to by 'in', apply the
50 * NDC->Viewport mapping and store the results at 'v'.
51 */
52
53 static INLINE void insert_4f_viewport_4( const struct tnl_clipspace_attr *a, GLubyte *v,
54 const GLfloat *in )
55 {
56 GLfloat *out = (GLfloat *)v;
57 const GLfloat * const vp = a->vp;
58
59 out[0] = vp[0] * in[0] + vp[12];
60 out[1] = vp[5] * in[1] + vp[13];
61 out[2] = vp[10] * in[2] + vp[14];
62 out[3] = in[3];
63 }
64
65 static INLINE void insert_4f_viewport_3( const struct tnl_clipspace_attr *a, GLubyte *v,
66 const GLfloat *in )
67 {
68 GLfloat *out = (GLfloat *)v;
69 const GLfloat * const vp = a->vp;
70
71 out[0] = vp[0] * in[0] + vp[12];
72 out[1] = vp[5] * in[1] + vp[13];
73 out[2] = vp[10] * in[2] + vp[14];
74 out[3] = 1;
75 }
76
77 static INLINE void insert_4f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
78 const GLfloat *in )
79 {
80 GLfloat *out = (GLfloat *)v;
81 const GLfloat * const vp = a->vp;
82
83 out[0] = vp[0] * in[0] + vp[12];
84 out[1] = vp[5] * in[1] + vp[13];
85 out[2] = vp[14];
86 out[3] = 1;
87 }
88
89 static INLINE void insert_4f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
90 const GLfloat *in )
91 {
92 GLfloat *out = (GLfloat *)v;
93 const GLfloat * const vp = a->vp;
94
95 out[0] = vp[0] * in[0] + vp[12];
96 out[1] = vp[13];
97 out[2] = vp[14];
98 out[3] = 1;
99 }
100
101 static INLINE void insert_3f_viewport_3( const struct tnl_clipspace_attr *a, GLubyte *v,
102 const GLfloat *in )
103 {
104 GLfloat *out = (GLfloat *)v;
105 const GLfloat * const vp = a->vp;
106
107 out[0] = vp[0] * in[0] + vp[12];
108 out[1] = vp[5] * in[1] + vp[13];
109 out[2] = vp[10] * in[2] + vp[14];
110 }
111
112 static INLINE void insert_3f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
113 const GLfloat *in )
114 {
115 GLfloat *out = (GLfloat *)v;
116 const GLfloat * const vp = a->vp;
117
118 out[0] = vp[0] * in[0] + vp[12];
119 out[1] = vp[5] * in[1] + vp[13];
120 out[2] = vp[10] * in[2] + vp[14];
121 }
122
123 static INLINE void insert_3f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
124 const GLfloat *in )
125 {
126 GLfloat *out = (GLfloat *)v;
127 const GLfloat * const vp = a->vp;
128
129 out[0] = vp[0] * in[0] + vp[12];
130 out[1] = vp[13];
131 out[2] = vp[14];
132 }
133
134 static INLINE void insert_2f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
135 const GLfloat *in )
136 {
137 GLfloat *out = (GLfloat *)v;
138 const GLfloat * const vp = a->vp;
139
140 out[0] = vp[0] * in[0] + vp[12];
141 out[1] = vp[5] * in[1] + vp[13];
142 }
143
144 static INLINE void insert_2f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
145 const GLfloat *in )
146 {
147 GLfloat *out = (GLfloat *)v;
148 const GLfloat * const vp = a->vp;
149
150 out[0] = vp[0] * in[0] + vp[12];
151 out[1] = vp[13];
152 }
153
154
155 /*
156 * These functions do the same as above, except for the viewport mapping.
157 */
158
159 static INLINE void insert_4f_4( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
160 {
161 GLfloat *out = (GLfloat *)(v);
162 (void) a;
163
164 out[0] = in[0];
165 out[1] = in[1];
166 out[2] = in[2];
167 out[3] = in[3];
168 }
169
170 static INLINE void insert_4f_3( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
171 {
172 GLfloat *out = (GLfloat *)(v);
173 (void) a;
174
175 out[0] = in[0];
176 out[1] = in[1];
177 out[2] = in[2];
178 out[3] = 1;
179 }
180
181 static INLINE void insert_4f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
182 {
183 GLfloat *out = (GLfloat *)(v);
184 (void) a;
185
186 out[0] = in[0];
187 out[1] = in[1];
188 out[2] = 0;
189 out[3] = 1;
190 }
191
192 static INLINE void insert_4f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
193 {
194 GLfloat *out = (GLfloat *)(v);
195 (void) a;
196
197 out[0] = in[0];
198 out[1] = 0;
199 out[2] = 0;
200 out[3] = 1;
201 }
202
203 static INLINE void insert_3f_xyw_4( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
204 {
205 GLfloat *out = (GLfloat *)(v);
206 (void) a;
207
208 out[0] = in[0];
209 out[1] = in[1];
210 out[2] = in[3];
211 }
212
213 static INLINE void insert_3f_xyw_err( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
214 {
215 (void) a; (void) v; (void) in;
216 abort();
217 }
218
219 static INLINE void insert_3f_3( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
220 {
221 GLfloat *out = (GLfloat *)(v);
222 (void) a;
223
224 out[0] = in[0];
225 out[1] = in[1];
226 out[2] = in[2];
227 }
228
229 static INLINE void insert_3f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
230 {
231 GLfloat *out = (GLfloat *)(v);
232 (void) a;
233
234 out[0] = in[0];
235 out[1] = in[1];
236 out[2] = 0;
237 }
238
239 static INLINE void insert_3f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
240 {
241 GLfloat *out = (GLfloat *)(v);
242 (void) a;
243
244 out[0] = in[0];
245 out[1] = 0;
246 out[2] = 0;
247 }
248
249
250 static INLINE void insert_2f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
251 {
252 GLfloat *out = (GLfloat *)(v);
253 (void) a;
254
255 out[0] = in[0];
256 out[1] = in[1];
257 }
258
259 static INLINE void insert_2f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
260 {
261 GLfloat *out = (GLfloat *)(v);
262 (void) a;
263
264 out[0] = in[0];
265 out[1] = 0;
266 }
267
268 static INLINE void insert_1f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
269 {
270 GLfloat *out = (GLfloat *)(v);
271 (void) a;
272
273 out[0] = in[0];
274 }
275
276 static INLINE void insert_null( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
277 {
278 (void) a; (void) v; (void) in;
279 }
280
281 static INLINE void insert_4chan_4f_rgba_4( const struct tnl_clipspace_attr *a, GLubyte *v,
282 const GLfloat *in )
283 {
284 GLchan *c = (GLchan *)v;
285 (void) a;
286 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
287 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
288 UNCLAMPED_FLOAT_TO_CHAN(c[2], in[2]);
289 UNCLAMPED_FLOAT_TO_CHAN(c[3], in[3]);
290 }
291
292 static INLINE void insert_4chan_4f_rgba_3( const struct tnl_clipspace_attr *a, GLubyte *v,
293 const GLfloat *in )
294 {
295 GLchan *c = (GLchan *)v;
296 (void) a;
297 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
298 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
299 UNCLAMPED_FLOAT_TO_CHAN(c[2], in[2]);
300 c[3] = CHAN_MAX;
301 }
302
303 static INLINE void insert_4chan_4f_rgba_2( const struct tnl_clipspace_attr *a, GLubyte *v,
304 const GLfloat *in )
305 {
306 GLchan *c = (GLchan *)v;
307 (void) a;
308 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
309 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
310 c[2] = 0;
311 c[3] = CHAN_MAX;
312 }
313
314 static INLINE void insert_4chan_4f_rgba_1( const struct tnl_clipspace_attr *a, GLubyte *v,
315 const GLfloat *in )
316 {
317 GLchan *c = (GLchan *)v;
318 (void) a;
319 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
320 c[1] = 0;
321 c[2] = 0;
322 c[3] = CHAN_MAX;
323 }
324
325 static INLINE void insert_4ub_4f_rgba_4( const struct tnl_clipspace_attr *a, GLubyte *v,
326 const GLfloat *in )
327 {
328 (void) a;
329 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
330 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
331 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
332 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[3]);
333 }
334
335 static INLINE void insert_4ub_4f_rgba_3( const struct tnl_clipspace_attr *a, GLubyte *v,
336 const GLfloat *in )
337 {
338 (void) a;
339 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
340 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
341 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
342 v[3] = 0xff;
343 }
344
345 static INLINE void insert_4ub_4f_rgba_2( const struct tnl_clipspace_attr *a, GLubyte *v,
346 const GLfloat *in )
347 {
348 (void) a;
349 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
350 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
351 v[2] = 0;
352 v[3] = 0xff;
353 }
354
355 static INLINE void insert_4ub_4f_rgba_1( const struct tnl_clipspace_attr *a, GLubyte *v,
356 const GLfloat *in )
357 {
358 (void) a;
359 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
360 v[1] = 0;
361 v[2] = 0;
362 v[3] = 0xff;
363 }
364
365 static INLINE void insert_4ub_4f_bgra_4( const struct tnl_clipspace_attr *a, GLubyte *v,
366 const GLfloat *in )
367 {
368 (void) a;
369 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
370 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
371 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
372 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[3]);
373 }
374
375 static INLINE void insert_4ub_4f_bgra_3( const struct tnl_clipspace_attr *a, GLubyte *v,
376 const GLfloat *in )
377 {
378 (void) a;
379 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
380 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
381 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
382 v[3] = 0xff;
383 }
384
385 static INLINE void insert_4ub_4f_bgra_2( const struct tnl_clipspace_attr *a, GLubyte *v,
386 const GLfloat *in )
387 {
388 (void) a;
389 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
390 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
391 v[0] = 0;
392 v[3] = 0xff;
393 }
394
395 static INLINE void insert_4ub_4f_bgra_1( const struct tnl_clipspace_attr *a, GLubyte *v,
396 const GLfloat *in )
397 {
398 (void) a;
399 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
400 v[1] = 0;
401 v[0] = 0;
402 v[3] = 0xff;
403 }
404
405 static INLINE void insert_4ub_4f_argb_4( const struct tnl_clipspace_attr *a, GLubyte *v,
406 const GLfloat *in )
407 {
408 (void) a;
409 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[0]);
410 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
411 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[2]);
412 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[3]);
413 }
414
415 static INLINE void insert_4ub_4f_argb_3( const struct tnl_clipspace_attr *a, GLubyte *v,
416 const GLfloat *in )
417 {
418 (void) a;
419 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[0]);
420 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
421 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[2]);
422 v[0] = 0xff;
423 }
424
425 static INLINE void insert_4ub_4f_argb_2( const struct tnl_clipspace_attr *a, GLubyte *v,
426 const GLfloat *in )
427 {
428 (void) a;
429 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[0]);
430 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
431 v[3] = 0x00;
432 v[0] = 0xff;
433 }
434
435 static INLINE void insert_4ub_4f_argb_1( const struct tnl_clipspace_attr *a, GLubyte *v,
436 const GLfloat *in )
437 {
438 (void) a;
439 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[0]);
440 v[2] = 0x00;
441 v[3] = 0x00;
442 v[0] = 0xff;
443 }
444
445 static INLINE void insert_4ub_4f_abgr_4( const struct tnl_clipspace_attr *a, GLubyte *v,
446 const GLfloat *in )
447 {
448 (void) a;
449 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[0]);
450 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
451 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[2]);
452 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[3]);
453 }
454
455 static INLINE void insert_4ub_4f_abgr_3( const struct tnl_clipspace_attr *a, GLubyte *v,
456 const GLfloat *in )
457 {
458 (void) a;
459 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[0]);
460 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
461 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[2]);
462 v[0] = 0xff;
463 }
464
465 static INLINE void insert_4ub_4f_abgr_2( const struct tnl_clipspace_attr *a, GLubyte *v,
466 const GLfloat *in )
467 {
468 (void) a;
469 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[0]);
470 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[1]);
471 v[1] = 0x00;
472 v[0] = 0xff;
473 }
474
475 static INLINE void insert_4ub_4f_abgr_1( const struct tnl_clipspace_attr *a, GLubyte *v,
476 const GLfloat *in )
477 {
478 (void) a;
479 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[0]);
480 v[2] = 0x00;
481 v[1] = 0x00;
482 v[0] = 0xff;
483 }
484
485 static INLINE void insert_3ub_3f_rgb_3( const struct tnl_clipspace_attr *a, GLubyte *v,
486 const GLfloat *in )
487 {
488 (void) a;
489 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
490 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
491 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
492 }
493
494 static INLINE void insert_3ub_3f_rgb_2( const struct tnl_clipspace_attr *a, GLubyte *v,
495 const GLfloat *in )
496 {
497 (void) a;
498 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
499 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
500 v[2] = 0;
501 }
502
503 static INLINE void insert_3ub_3f_rgb_1( const struct tnl_clipspace_attr *a, GLubyte *v,
504 const GLfloat *in )
505 {
506 (void) a;
507 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
508 v[1] = 0;
509 v[2] = 0;
510 }
511
512 static INLINE void insert_3ub_3f_bgr_3( const struct tnl_clipspace_attr *a, GLubyte *v,
513 const GLfloat *in )
514 {
515 (void) a;
516 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
517 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
518 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
519 }
520
521 static INLINE void insert_3ub_3f_bgr_2( const struct tnl_clipspace_attr *a, GLubyte *v,
522 const GLfloat *in )
523 {
524 (void) a;
525 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
526 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
527 v[0] = 0;
528 }
529
530 static INLINE void insert_3ub_3f_bgr_1( const struct tnl_clipspace_attr *a, GLubyte *v,
531 const GLfloat *in )
532 {
533 (void) a;
534 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
535 v[1] = 0;
536 v[0] = 0;
537 }
538
539
540 static INLINE void insert_1ub_1f_1( const struct tnl_clipspace_attr *a, GLubyte *v,
541 const GLfloat *in )
542 {
543 (void) a;
544 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
545 }
546
547
548 /***********************************************************************
549 * Functions to perform the reverse operations to the above, for
550 * swrast translation and clip-interpolation.
551 *
552 * Currently always extracts a full 4 floats.
553 */
554
555 static void extract_4f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
556 const GLubyte *v )
557 {
558 const GLfloat *in = (const GLfloat *)v;
559 const GLfloat * const vp = a->vp;
560
561 /* Although included for completeness, the position coordinate is
562 * usually handled differently during clipping.
563 */
564 out[0] = (in[0] - vp[12]) / vp[0];
565 out[1] = (in[1] - vp[13]) / vp[5];
566 out[2] = (in[2] - vp[14]) / vp[10];
567 out[3] = in[3];
568 }
569
570 static void extract_3f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
571 const GLubyte *v )
572 {
573 const GLfloat *in = (const GLfloat *)v;
574 const GLfloat * const vp = a->vp;
575
576 out[0] = (in[0] - vp[12]) / vp[0];
577 out[1] = (in[1] - vp[13]) / vp[5];
578 out[2] = (in[2] - vp[14]) / vp[10];
579 out[3] = 1;
580 }
581
582
583 static void extract_2f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
584 const GLubyte *v )
585 {
586 const GLfloat *in = (const GLfloat *)v;
587 const GLfloat * const vp = a->vp;
588
589 out[0] = (in[0] - vp[12]) / vp[0];
590 out[1] = (in[1] - vp[13]) / vp[5];
591 out[2] = 0;
592 out[3] = 1;
593 }
594
595
596 static void extract_4f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
597 {
598 const GLfloat *in = (const GLfloat *)v;
599 (void) a;
600
601 out[0] = in[0];
602 out[1] = in[1];
603 out[2] = in[2];
604 out[3] = in[3];
605 }
606
607 static void extract_3f_xyw( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
608 {
609 const GLfloat *in = (const GLfloat *)v;
610 (void) a;
611
612 out[0] = in[0];
613 out[1] = in[1];
614 out[2] = 0;
615 out[3] = in[2];
616 }
617
618
619 static void extract_3f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
620 {
621 const GLfloat *in = (const GLfloat *)v;
622 (void) a;
623
624 out[0] = in[0];
625 out[1] = in[1];
626 out[2] = in[2];
627 out[3] = 1;
628 }
629
630
631 static void extract_2f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
632 {
633 const GLfloat *in = (const GLfloat *)v;
634 (void) a;
635
636 out[0] = in[0];
637 out[1] = in[1];
638 out[2] = 0;
639 out[3] = 1;
640 }
641
642 static void extract_1f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
643 {
644 const GLfloat *in = (const GLfloat *)v;
645 (void) a;
646
647 out[0] = in[0];
648 out[1] = 0;
649 out[2] = 0;
650 out[3] = 1;
651 }
652
653 static void extract_4chan_4f_rgba( const struct tnl_clipspace_attr *a, GLfloat *out,
654 const GLubyte *v )
655 {
656 GLchan *c = (GLchan *)v;
657 (void) a;
658
659 out[0] = CHAN_TO_FLOAT(c[0]);
660 out[1] = CHAN_TO_FLOAT(c[1]);
661 out[2] = CHAN_TO_FLOAT(c[2]);
662 out[3] = CHAN_TO_FLOAT(c[3]);
663 }
664
665 static void extract_4ub_4f_rgba( const struct tnl_clipspace_attr *a, GLfloat *out,
666 const GLubyte *v )
667 {
668 (void) a;
669 out[0] = UBYTE_TO_FLOAT(v[0]);
670 out[1] = UBYTE_TO_FLOAT(v[1]);
671 out[2] = UBYTE_TO_FLOAT(v[2]);
672 out[3] = UBYTE_TO_FLOAT(v[3]);
673 }
674
675 static void extract_4ub_4f_bgra( const struct tnl_clipspace_attr *a, GLfloat *out,
676 const GLubyte *v )
677 {
678 (void) a;
679 out[2] = UBYTE_TO_FLOAT(v[0]);
680 out[1] = UBYTE_TO_FLOAT(v[1]);
681 out[0] = UBYTE_TO_FLOAT(v[2]);
682 out[3] = UBYTE_TO_FLOAT(v[3]);
683 }
684
685 static void extract_4ub_4f_argb( const struct tnl_clipspace_attr *a, GLfloat *out,
686 const GLubyte *v )
687 {
688 (void) a;
689 out[3] = UBYTE_TO_FLOAT(v[0]);
690 out[0] = UBYTE_TO_FLOAT(v[1]);
691 out[1] = UBYTE_TO_FLOAT(v[2]);
692 out[2] = UBYTE_TO_FLOAT(v[3]);
693 }
694
695 static void extract_4ub_4f_abgr( const struct tnl_clipspace_attr *a, GLfloat *out,
696 const GLubyte *v )
697 {
698 (void) a;
699 out[3] = UBYTE_TO_FLOAT(v[0]);
700 out[2] = UBYTE_TO_FLOAT(v[1]);
701 out[1] = UBYTE_TO_FLOAT(v[2]);
702 out[0] = UBYTE_TO_FLOAT(v[3]);
703 }
704
705 static void extract_3ub_3f_rgb( const struct tnl_clipspace_attr *a, GLfloat *out,
706 const GLubyte *v )
707 {
708 (void) a;
709 out[0] = UBYTE_TO_FLOAT(v[0]);
710 out[1] = UBYTE_TO_FLOAT(v[1]);
711 out[2] = UBYTE_TO_FLOAT(v[2]);
712 out[3] = 1;
713 }
714
715 static void extract_3ub_3f_bgr( const struct tnl_clipspace_attr *a, GLfloat *out,
716 const GLubyte *v )
717 {
718 (void) a;
719 out[2] = UBYTE_TO_FLOAT(v[0]);
720 out[1] = UBYTE_TO_FLOAT(v[1]);
721 out[0] = UBYTE_TO_FLOAT(v[2]);
722 out[3] = 1;
723 }
724
725 static void extract_1ub_1f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
726 {
727 (void) a;
728 out[0] = UBYTE_TO_FLOAT(v[0]);
729 out[1] = 0;
730 out[2] = 0;
731 out[3] = 1;
732 }
733
734
735 const static struct {
736 const char *name;
737 tnl_extract_func extract;
738 tnl_insert_func insert[4];
739 const GLuint attrsize;
740 } format_info[EMIT_MAX] = {
741
742 { "1f",
743 extract_1f,
744 { insert_1f_1, insert_1f_1, insert_1f_1, insert_1f_1 },
745 sizeof(GLfloat) },
746
747 { "2f",
748 extract_2f,
749 { insert_2f_1, insert_2f_2, insert_2f_2, insert_2f_2 },
750 2 * sizeof(GLfloat) },
751
752 { "3f",
753 extract_3f,
754 { insert_3f_1, insert_3f_2, insert_3f_3, insert_3f_3 },
755 3 * sizeof(GLfloat) },
756
757 { "4f",
758 extract_4f,
759 { insert_4f_1, insert_4f_2, insert_4f_3, insert_4f_4 },
760 4 * sizeof(GLfloat) },
761
762 { "2f_viewport",
763 extract_2f_viewport,
764 { insert_2f_viewport_1, insert_2f_viewport_2, insert_2f_viewport_2,
765 insert_2f_viewport_2 },
766 2 * sizeof(GLfloat) },
767
768 { "3f_viewport",
769 extract_3f_viewport,
770 { insert_3f_viewport_1, insert_3f_viewport_2, insert_3f_viewport_3,
771 insert_3f_viewport_3 },
772 3 * sizeof(GLfloat) },
773
774 { "4f_viewport",
775 extract_4f_viewport,
776 { insert_4f_viewport_1, insert_4f_viewport_2, insert_4f_viewport_3,
777 insert_4f_viewport_4 },
778 4 * sizeof(GLfloat) },
779
780 { "3f_xyw",
781 extract_3f_xyw,
782 { insert_3f_xyw_err, insert_3f_xyw_err, insert_3f_xyw_err,
783 insert_3f_xyw_4 },
784 3 * sizeof(GLfloat) },
785
786 { "1ub_1f",
787 extract_1ub_1f,
788 { insert_1ub_1f_1, insert_1ub_1f_1, insert_1ub_1f_1, insert_1ub_1f_1 },
789 sizeof(GLubyte) },
790
791 { "3ub_3f_rgb",
792 extract_3ub_3f_rgb,
793 { insert_3ub_3f_rgb_1, insert_3ub_3f_rgb_2, insert_3ub_3f_rgb_3,
794 insert_3ub_3f_rgb_3 },
795 3 * sizeof(GLubyte) },
796
797 { "3ub_3f_bgr",
798 extract_3ub_3f_bgr,
799 { insert_3ub_3f_bgr_1, insert_3ub_3f_bgr_2, insert_3ub_3f_bgr_3,
800 insert_3ub_3f_bgr_3 },
801 3 * sizeof(GLubyte) },
802
803 { "4ub_4f_rgba",
804 extract_4ub_4f_rgba,
805 { insert_4ub_4f_rgba_1, insert_4ub_4f_rgba_2, insert_4ub_4f_rgba_3,
806 insert_4ub_4f_rgba_4 },
807 4 * sizeof(GLubyte) },
808
809 { "4ub_4f_bgra",
810 extract_4ub_4f_bgra,
811 { insert_4ub_4f_bgra_1, insert_4ub_4f_bgra_2, insert_4ub_4f_bgra_3,
812 insert_4ub_4f_bgra_4 },
813 4 * sizeof(GLubyte) },
814
815 { "4ub_4f_argb",
816 extract_4ub_4f_argb,
817 { insert_4ub_4f_argb_1, insert_4ub_4f_argb_2, insert_4ub_4f_argb_3,
818 insert_4ub_4f_argb_4 },
819 4 * sizeof(GLubyte) },
820
821 { "4ub_4f_abgr",
822 extract_4ub_4f_abgr,
823 { insert_4ub_4f_abgr_1, insert_4ub_4f_abgr_2, insert_4ub_4f_abgr_3,
824 insert_4ub_4f_abgr_4 },
825 4 * sizeof(GLubyte) },
826
827 { "4chan_4f_rgba",
828 extract_4chan_4f_rgba,
829 { insert_4chan_4f_rgba_1, insert_4chan_4f_rgba_2, insert_4chan_4f_rgba_3,
830 insert_4chan_4f_rgba_4 },
831 4 * sizeof(GLchan) },
832
833 { "pad",
834 NULL,
835 { NULL, NULL, NULL, NULL },
836 0 }
837
838 };
839
840
841
842
843 /***********************************************************************
844 * Hardwired fastpaths for emitting whole vertices or groups of
845 * vertices
846 */
847 static void choose_emit_func( GLcontext *ctx, GLuint count, GLubyte *dest);
848
849
850 #define EMIT5(NR, F0, F1, F2, F3, F4, NAME) \
851 static void NAME( GLcontext *ctx, \
852 GLuint count, \
853 GLubyte *v ) \
854 { \
855 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx); \
856 struct tnl_clipspace_attr *a = vtx->attr; \
857 GLuint i; \
858 \
859 if (vtx->attr_count != NR || \
860 (NR > 0 && a[0].emit != F0) || \
861 (NR > 1 && a[1].emit != F1) || \
862 (NR > 2 && a[2].emit != F2) || \
863 (NR > 3 && a[3].emit != F3) || \
864 (NR > 4 && a[4].emit != F4)) { \
865 choose_emit_func( ctx, count, v ); \
866 return; \
867 } \
868 \
869 for (i = 0 ; i < count ; i++, v += vtx->vertex_size) { \
870 if (NR > 0) { \
871 F0( &a[0], v + a[0].vertoffset, (GLfloat *)a[0].inputptr ); \
872 a[0].inputptr += a[0].inputstride; \
873 } \
874 \
875 if (NR > 1) { \
876 F1( &a[1], v + a[1].vertoffset, (GLfloat *)a[1].inputptr ); \
877 a[1].inputptr += a[1].inputstride; \
878 } \
879 \
880 if (NR > 2) { \
881 F2( &a[2], v + a[2].vertoffset, (GLfloat *)a[2].inputptr ); \
882 a[2].inputptr += a[2].inputstride; \
883 } \
884 \
885 if (NR > 3) { \
886 F3( &a[3], v + a[3].vertoffset, (GLfloat *)a[3].inputptr ); \
887 a[3].inputptr += a[3].inputstride; \
888 } \
889 \
890 if (NR > 4) { \
891 F4( &a[4], v + a[4].vertoffset, (GLfloat *)a[4].inputptr ); \
892 a[4].inputptr += a[4].inputstride; \
893 } \
894 } \
895 }
896
897
898 #define EMIT2(F0, F1, NAME) EMIT5(2, F0, F1, insert_null, \
899 insert_null, insert_null, NAME)
900
901 #define EMIT3(F0, F1, F2, NAME) EMIT5(3, F0, F1, F2, insert_null, \
902 insert_null, NAME)
903
904 #define EMIT4(F0, F1, F2, F3, NAME) EMIT5(4, F0, F1, F2, F3, \
905 insert_null, NAME)
906
907
908 EMIT2(insert_3f_viewport_3, insert_4ub_4f_rgba_4, emit_viewport3_rgba4)
909 EMIT2(insert_3f_viewport_3, insert_4ub_4f_bgra_4, emit_viewport3_bgra4)
910 EMIT2(insert_3f_3, insert_4ub_4f_rgba_4, emit_xyz3_rgba4)
911
912 EMIT3(insert_4f_viewport_4, insert_4ub_4f_rgba_4, insert_2f_2, emit_viewport4_rgba4_st2)
913 EMIT3(insert_4f_viewport_4, insert_4ub_4f_bgra_4, insert_2f_2, emit_viewport4_bgra4_st2)
914 EMIT3(insert_4f_4, insert_4ub_4f_rgba_4, insert_2f_2, emit_xyzw4_rgba4_st2)
915
916 EMIT4(insert_4f_viewport_4, insert_4ub_4f_rgba_4, insert_2f_2, insert_2f_2, emit_viewport4_rgba4_st2_st2)
917 EMIT4(insert_4f_viewport_4, insert_4ub_4f_bgra_4, insert_2f_2, insert_2f_2, emit_viewport4_bgra4_st2_st2)
918 EMIT4(insert_4f_4, insert_4ub_4f_rgba_4, insert_2f_2, insert_2f_2, emit_xyzw4_rgba4_st2_st2)
919
920
921
922
923
924 /***********************************************************************
925 * Generic (non-codegen) functions for whole vertices or groups of
926 * vertices
927 */
928
929 static void generic_emit( GLcontext *ctx,
930 GLuint count,
931 GLubyte *v )
932 {
933 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
934 struct tnl_clipspace_attr *a = vtx->attr;
935 const GLuint attr_count = vtx->attr_count;
936 const GLuint stride = vtx->vertex_size;
937 GLuint i, j;
938
939 for (i = 0 ; i < count ; i++, v += stride) {
940 for (j = 0; j < attr_count; j++) {
941 GLfloat *in = (GLfloat *)a[j].inputptr;
942 a[j].inputptr += a[j].inputstride;
943 a[j].emit( &a[j], v + a[j].vertoffset, in );
944 }
945 }
946 }
947
948
949 static void generic_interp( GLcontext *ctx,
950 GLfloat t,
951 GLuint edst, GLuint eout, GLuint ein,
952 GLboolean force_boundary )
953 {
954 TNLcontext *tnl = TNL_CONTEXT(ctx);
955 struct vertex_buffer *VB = &tnl->vb;
956 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
957 const GLubyte *vin = vtx->vertex_buf + ein * vtx->vertex_size;
958 const GLubyte *vout = vtx->vertex_buf + eout * vtx->vertex_size;
959 GLubyte *vdst = vtx->vertex_buf + edst * vtx->vertex_size;
960 const struct tnl_clipspace_attr *a = vtx->attr;
961 const GLuint attr_count = vtx->attr_count;
962 GLuint j;
963 (void) force_boundary;
964
965 if (tnl->NeedNdcCoords) {
966 const GLfloat *dstclip = VB->ClipPtr->data[edst];
967 if (dstclip[3] != 0.0) {
968 const GLfloat w = 1.0f / dstclip[3];
969 GLfloat pos[4];
970
971 pos[0] = dstclip[0] * w;
972 pos[1] = dstclip[1] * w;
973 pos[2] = dstclip[2] * w;
974 pos[3] = w;
975
976 a[0].insert[4-1]( &a[0], vdst, pos );
977 }
978 }
979 else {
980 a[0].insert[4-1]( &a[0], vdst, VB->ClipPtr->data[edst] );
981 }
982
983
984 for (j = 1; j < attr_count; j++) {
985 GLfloat fin[4], fout[4], fdst[4];
986
987 a[j].extract( &a[j], fin, vin + a[j].vertoffset );
988 a[j].extract( &a[j], fout, vout + a[j].vertoffset );
989
990 INTERP_F( t, fdst[3], fout[3], fin[3] );
991 INTERP_F( t, fdst[2], fout[2], fin[2] );
992 INTERP_F( t, fdst[1], fout[1], fin[1] );
993 INTERP_F( t, fdst[0], fout[0], fin[0] );
994
995 a[j].insert[4-1]( &a[j], vdst + a[j].vertoffset, fdst );
996 }
997 }
998
999
1000 /* Extract color attributes from one vertex and insert them into
1001 * another. (Shortcircuit extract/insert with memcpy).
1002 */
1003 static void generic_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
1004 {
1005 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1006 GLubyte *vsrc = vtx->vertex_buf + esrc * vtx->vertex_size;
1007 GLubyte *vdst = vtx->vertex_buf + edst * vtx->vertex_size;
1008 const struct tnl_clipspace_attr *a = vtx->attr;
1009 const GLuint attr_count = vtx->attr_count;
1010 GLuint j;
1011
1012 for (j = 0; j < attr_count; j++) {
1013 if (a[j].attrib == VERT_ATTRIB_COLOR0 ||
1014 a[j].attrib == VERT_ATTRIB_COLOR1) {
1015
1016 _mesa_memcpy( vdst + a[j].vertoffset,
1017 vsrc + a[j].vertoffset,
1018 a[j].vertattrsize );
1019 }
1020 }
1021 }
1022
1023
1024 /* Helper functions for hardware which doesn't put back colors and/or
1025 * edgeflags into vertices.
1026 */
1027 static void generic_interp_extras( GLcontext *ctx,
1028 GLfloat t,
1029 GLuint dst, GLuint out, GLuint in,
1030 GLboolean force_boundary )
1031 {
1032 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
1033
1034 /* If stride is zero, ColorPtr[1] is constant across the VB, so
1035 * there is no point interpolating between two values as they will
1036 * be identical. In all other cases, this value is generated by
1037 * t_vb_lighttmp.h and has a stride of 4 dwords.
1038 */
1039 if (VB->ColorPtr[1] && VB->ColorPtr[1]->stride) {
1040 assert(VB->ColorPtr[1]->stride == 4 * sizeof(GLfloat));
1041
1042 INTERP_4F( t,
1043 VB->ColorPtr[1]->data[dst],
1044 VB->ColorPtr[1]->data[out],
1045 VB->ColorPtr[1]->data[in] );
1046 }
1047
1048 if (VB->SecondaryColorPtr[1]) {
1049 assert(VB->SecondaryColorPtr[1]->stride == 4 * sizeof(GLfloat));
1050
1051 INTERP_3F( t,
1052 VB->SecondaryColorPtr[1]->data[dst],
1053 VB->SecondaryColorPtr[1]->data[out],
1054 VB->SecondaryColorPtr[1]->data[in] );
1055 }
1056
1057 if (VB->IndexPtr[1]) {
1058 VB->IndexPtr[1]->data[dst][0] = LINTERP( t,
1059 VB->IndexPtr[1]->data[out][0],
1060 VB->IndexPtr[1]->data[in][0] );
1061 }
1062
1063 if (VB->EdgeFlag) {
1064 VB->EdgeFlag[dst] = VB->EdgeFlag[out] || force_boundary;
1065 }
1066
1067 generic_interp(ctx, t, dst, out, in, force_boundary);
1068 }
1069
1070 static void generic_copy_pv_extras( GLcontext *ctx,
1071 GLuint dst, GLuint src )
1072 {
1073 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
1074
1075 /* See above comment:
1076 */
1077 if (VB->ColorPtr[1] && VB->ColorPtr[1]->stride) {
1078 COPY_4FV( VB->ColorPtr[1]->data[dst],
1079 VB->ColorPtr[1]->data[src] );
1080 }
1081
1082 if (VB->SecondaryColorPtr[1]) {
1083 COPY_4FV( VB->SecondaryColorPtr[1]->data[dst],
1084 VB->SecondaryColorPtr[1]->data[src] );
1085 }
1086
1087 if (VB->IndexPtr[1]) {
1088 VB->IndexPtr[1]->data[dst][0] = VB->IndexPtr[1]->data[src][0];
1089 }
1090
1091 generic_copy_pv(ctx, dst, src);
1092 }
1093
1094
1095
1096
1097 /***********************************************************************
1098 * Build codegen functions or return generic ones:
1099 */
1100
1101
1102 static void choose_emit_func( GLcontext *ctx, GLuint count, GLubyte *dest)
1103 {
1104 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1105 struct tnl_clipspace_attr *a = vtx->attr;
1106 const GLuint attr_count = vtx->attr_count;
1107
1108 vtx->emit = NULL;
1109
1110 if (0)
1111 vtx->emit = _tnl_codegen_emit(ctx);
1112
1113 /* Does it fit a hardwired fastpath? Help! this is growing out of
1114 * control!
1115 */
1116 switch (attr_count) {
1117 case 2:
1118 if (a[0].emit == insert_3f_viewport_3) {
1119 if (a[1].emit == insert_4ub_4f_bgra_4)
1120 vtx->emit = emit_viewport3_bgra4;
1121 else if (a[1].emit == insert_4ub_4f_rgba_4)
1122 vtx->emit = emit_viewport3_rgba4;
1123 }
1124 else if (a[0].emit == insert_3f_3 &&
1125 a[1].emit == insert_4ub_4f_rgba_4) {
1126 vtx->emit = emit_xyz3_rgba4;
1127 }
1128 break;
1129 case 3:
1130 if (a[2].emit == insert_2f_2) {
1131 if (a[1].emit == insert_4ub_4f_rgba_4) {
1132 if (a[0].emit == insert_4f_viewport_4)
1133 vtx->emit = emit_viewport4_rgba4_st2;
1134 else if (a[0].emit == insert_4f_4)
1135 vtx->emit = emit_xyzw4_rgba4_st2;
1136 }
1137 else if (a[1].emit == insert_4ub_4f_bgra_4 &&
1138 a[0].emit == insert_4f_viewport_4)
1139 vtx->emit = emit_viewport4_bgra4_st2;
1140 }
1141 break;
1142 case 4:
1143 if (a[2].emit == insert_2f_2 &&
1144 a[3].emit == insert_2f_2) {
1145 if (a[1].emit == insert_4ub_4f_rgba_4) {
1146 if (a[0].emit == insert_4f_viewport_4)
1147 vtx->emit = emit_viewport4_rgba4_st2_st2;
1148 else if (a[0].emit == insert_4f_4)
1149 vtx->emit = emit_xyzw4_rgba4_st2_st2;
1150 }
1151 else if (a[1].emit == insert_4ub_4f_bgra_4 &&
1152 a[0].emit == insert_4f_viewport_4)
1153 vtx->emit = emit_viewport4_bgra4_st2_st2;
1154 }
1155 break;
1156 }
1157
1158 /* Otherwise use the generic version:
1159 */
1160 if (!vtx->emit)
1161 vtx->emit = generic_emit;
1162
1163 vtx->emit( ctx, count, dest );
1164 }
1165
1166
1167
1168 static void choose_interp_func( GLcontext *ctx,
1169 GLfloat t,
1170 GLuint edst, GLuint eout, GLuint ein,
1171 GLboolean force_boundary )
1172 {
1173 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1174
1175 if (vtx->need_extras &&
1176 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
1177 vtx->interp = generic_interp_extras;
1178 } else {
1179 vtx->interp = generic_interp;
1180 }
1181
1182 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
1183 }
1184
1185
1186 static void choose_copy_pv_func( GLcontext *ctx, GLuint edst, GLuint esrc )
1187 {
1188 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1189
1190 if (vtx->need_extras &&
1191 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
1192 vtx->copy_pv = generic_copy_pv_extras;
1193 } else {
1194 vtx->copy_pv = generic_copy_pv;
1195 }
1196
1197 vtx->copy_pv( ctx, edst, esrc );
1198 }
1199
1200
1201 /***********************************************************************
1202 * Public entrypoints, mostly dispatch to the above:
1203 */
1204
1205
1206 /* Interpolate between two vertices to produce a third:
1207 */
1208 void _tnl_interp( GLcontext *ctx,
1209 GLfloat t,
1210 GLuint edst, GLuint eout, GLuint ein,
1211 GLboolean force_boundary )
1212 {
1213 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1214 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
1215 }
1216
1217 /* Copy colors from one vertex to another:
1218 */
1219 void _tnl_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
1220 {
1221 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1222 vtx->copy_pv( ctx, edst, esrc );
1223 }
1224
1225
1226 /* Extract a named attribute from a hardware vertex. Will have to
1227 * reverse any viewport transformation, swizzling or other conversions
1228 * which may have been applied:
1229 */
1230 void _tnl_get_attr( GLcontext *ctx, const void *vin,
1231 GLenum attr, GLfloat *dest )
1232 {
1233 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1234 const struct tnl_clipspace_attr *a = vtx->attr;
1235 const GLuint attr_count = vtx->attr_count;
1236 GLuint j;
1237
1238 for (j = 0; j < attr_count; j++) {
1239 if (a[j].attrib == attr) {
1240 a[j].extract( &a[j], dest, (GLubyte *)vin + a[j].vertoffset );
1241 return;
1242 }
1243 }
1244
1245 /* Else return the value from ctx->Current.
1246 */
1247 _mesa_memcpy( dest, ctx->Current.Attrib[attr], 4*sizeof(GLfloat));
1248 }
1249
1250
1251 /* Complementary operation to the above.
1252 */
1253 void _tnl_set_attr( GLcontext *ctx, void *vout,
1254 GLenum attr, const GLfloat *src )
1255 {
1256 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1257 const struct tnl_clipspace_attr *a = vtx->attr;
1258 const GLuint attr_count = vtx->attr_count;
1259 GLuint j;
1260
1261 for (j = 0; j < attr_count; j++) {
1262 if (a[j].attrib == attr) {
1263 a[j].insert[4-1]( &a[j], (GLubyte *)vout + a[j].vertoffset, src );
1264 return;
1265 }
1266 }
1267 }
1268
1269
1270 void *_tnl_get_vertex( GLcontext *ctx, GLuint nr )
1271 {
1272 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1273
1274 return vtx->vertex_buf + nr * vtx->vertex_size;
1275 }
1276
1277 void _tnl_invalidate_vertex_state( GLcontext *ctx, GLuint new_state )
1278 {
1279 if (new_state & (_DD_NEW_TRI_LIGHT_TWOSIDE|_DD_NEW_TRI_UNFILLED) ) {
1280 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1281 vtx->new_inputs = ~0;
1282 vtx->interp = choose_interp_func;
1283 vtx->copy_pv = choose_copy_pv_func;
1284 }
1285 }
1286
1287
1288 GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map,
1289 GLuint nr, const GLfloat *vp,
1290 GLuint unpacked_size )
1291 {
1292 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1293 GLuint offset = 0;
1294 GLuint i, j;
1295
1296 assert(nr < _TNL_ATTRIB_MAX);
1297 assert(nr == 0 || map[0].attrib == VERT_ATTRIB_POS);
1298
1299 if (vtx->emit == generic_emit)
1300 vtx->emit = choose_emit_func;
1301
1302 vtx->interp = choose_interp_func;
1303 vtx->copy_pv = choose_copy_pv_func;
1304 vtx->new_inputs = ~0;
1305
1306 for (j = 0, i = 0; i < nr; i++) {
1307 const GLuint format = map[i].format;
1308 if (format == EMIT_PAD) {
1309 /*
1310 fprintf(stderr, "%d: pad %d, offset %d\n", i,
1311 map[i].offset, offset);
1312 */
1313 offset += map[i].offset;
1314
1315 }
1316 else {
1317 vtx->attr[j].attrib = map[i].attrib;
1318 vtx->attr[j].format = format;
1319 vtx->attr[j].vp = vp;
1320 vtx->attr[j].insert = format_info[format].insert;
1321 vtx->attr[j].extract = format_info[format].extract;
1322 vtx->attr[j].vertattrsize = format_info[format].attrsize;
1323
1324 if (unpacked_size)
1325 vtx->attr[j].vertoffset = map[i].offset;
1326 else
1327 vtx->attr[j].vertoffset = offset;
1328
1329 /*
1330 fprintf(stderr, "%d: %s, vp %p, offset %d\n", i,
1331 format_info[format].name, (void *)vp,
1332 vtx->attr[j].vertoffset);
1333 */
1334 offset += format_info[format].attrsize;
1335 j++;
1336 }
1337 }
1338
1339 vtx->attr_count = j;
1340
1341 if (unpacked_size)
1342 vtx->vertex_size = unpacked_size;
1343 else
1344 vtx->vertex_size = offset;
1345
1346 assert(vtx->vertex_size <= vtx->max_vertex_size);
1347
1348 return vtx->vertex_size;
1349 }
1350
1351
1352
1353 void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs )
1354 {
1355 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1356 vtx->new_inputs |= newinputs;
1357 }
1358
1359
1360 void _tnl_build_vertices( GLcontext *ctx,
1361 GLuint start,
1362 GLuint end,
1363 GLuint newinputs )
1364 {
1365 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1366
1367 newinputs |= vtx->new_inputs;
1368 vtx->new_inputs = 0;
1369
1370 if (newinputs) {
1371 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
1372 struct tnl_clipspace_attr *a = vtx->attr;
1373 const GLuint stride = vtx->vertex_size;
1374 const GLuint count = vtx->attr_count;
1375 GLuint j;
1376
1377 for (j = 0; j < count; j++) {
1378 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
1379 a[j].inputstride = vptr->stride;
1380 a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride;
1381 a[j].emit = a[j].insert[vptr->size - 1];
1382 }
1383
1384 vtx->emit( ctx, end - start,
1385 (GLubyte *)vtx->vertex_buf + start * stride );
1386 }
1387 }
1388
1389 /* Emit VB vertices start..end to dest. Note that VB vertex at
1390 * postion start will be emitted to dest at position zero.
1391 */
1392 void *_tnl_emit_vertices_to_buffer( GLcontext *ctx,
1393 GLuint start,
1394 GLuint end,
1395 void *dest )
1396 {
1397 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1398 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
1399 struct tnl_clipspace_attr *a = vtx->attr;
1400 const GLuint count = vtx->attr_count;
1401 GLuint j;
1402
1403 for (j = 0; j < count; j++) {
1404 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
1405 a[j].inputstride = vptr->stride;
1406 a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride;
1407 a[j].emit = a[j].insert[vptr->size - 1];
1408 }
1409
1410 /* Note: dest should not be adjusted for non-zero 'start' values:
1411 */
1412 vtx->emit( ctx, end - start, dest );
1413
1414 return (void *)((GLubyte *)dest + vtx->vertex_size * (end - start));
1415 }
1416
1417
1418 void _tnl_init_vertices( GLcontext *ctx,
1419 GLuint vb_size,
1420 GLuint max_vertex_size )
1421 {
1422 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1423
1424 _tnl_install_attrs( ctx, NULL, 0, NULL, 0 );
1425
1426 vtx->need_extras = GL_TRUE;
1427 if (max_vertex_size > vtx->max_vertex_size) {
1428 _tnl_free_vertices( ctx );
1429 vtx->max_vertex_size = max_vertex_size;
1430 vtx->vertex_buf = (GLubyte *)ALIGN_CALLOC(vb_size * max_vertex_size, 32 );
1431 vtx->emit = choose_emit_func;
1432 }
1433
1434 _tnl_init_c_codegen( &vtx->codegen );
1435 }
1436
1437
1438 void _tnl_free_vertices( GLcontext *ctx )
1439 {
1440 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1441 if (vtx->vertex_buf) {
1442 ALIGN_FREE(vtx->vertex_buf);
1443 vtx->vertex_buf = NULL;
1444 }
1445
1446 _tnl_free_c_codegen( &vtx->codegen );
1447 }