Add _tnl_set_attr() to complement existing get_attr() call.
[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 #define GET_VERTEX_STATE(ctx) &(TNL_CONTEXT(ctx)->clipspace)
47
48 static void insert_4f_viewport_4( const struct tnl_clipspace_attr *a, GLubyte *v,
49 const GLfloat *in )
50 {
51 GLfloat *out = (GLfloat *)v;
52 const GLfloat * const vp = a->vp;
53
54 out[0] = vp[0] * in[0] + vp[12];
55 out[1] = vp[5] * in[1] + vp[13];
56 out[2] = vp[10] * in[2] + vp[14];
57 out[3] = in[3];
58 }
59
60 static void insert_4f_viewport_3( const struct tnl_clipspace_attr *a, GLubyte *v,
61 const GLfloat *in )
62 {
63 GLfloat *out = (GLfloat *)v;
64 const GLfloat * const vp = a->vp;
65
66 out[0] = vp[0] * in[0] + vp[12];
67 out[1] = vp[5] * in[1] + vp[13];
68 out[2] = vp[10] * in[2] + vp[14];
69 out[3] = 1;
70 }
71
72 static void insert_4f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
73 const GLfloat *in )
74 {
75 GLfloat *out = (GLfloat *)v;
76 const GLfloat * const vp = a->vp;
77
78 out[0] = vp[0] * in[0] + vp[12];
79 out[1] = vp[5] * in[1] + vp[13];
80 out[2] = vp[14];
81 out[3] = 1;
82 }
83
84 static void insert_4f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
85 const GLfloat *in )
86 {
87 GLfloat *out = (GLfloat *)v;
88 const GLfloat * const vp = a->vp;
89
90 out[0] = vp[0] * in[0] + vp[12];
91 out[1] = vp[13];
92 out[2] = vp[14];
93 out[3] = 1;
94 }
95
96 static void insert_3f_viewport_3( const struct tnl_clipspace_attr *a, GLubyte *v,
97 const GLfloat *in )
98 {
99 GLfloat *out = (GLfloat *)v;
100 const GLfloat * const vp = a->vp;
101
102 out[0] = vp[0] * in[0] + vp[12];
103 out[1] = vp[5] * in[1] + vp[13];
104 out[2] = vp[10] * in[2] + vp[14];
105 }
106
107 static void insert_3f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
108 const GLfloat *in )
109 {
110 GLfloat *out = (GLfloat *)v;
111 const GLfloat * const vp = a->vp;
112
113 out[0] = vp[0] * in[0] + vp[12];
114 out[1] = vp[5] * in[1] + vp[13];
115 out[2] = vp[10] * in[2] + vp[14];
116 }
117
118 static void insert_3f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
119 const GLfloat *in )
120 {
121 GLfloat *out = (GLfloat *)v;
122 const GLfloat * const vp = a->vp;
123
124 out[0] = vp[0] * in[0] + vp[12];
125 out[1] = vp[13];
126 out[2] = vp[14];
127 }
128
129 static void insert_2f_viewport_2( const struct tnl_clipspace_attr *a, GLubyte *v,
130 const GLfloat *in )
131 {
132 GLfloat *out = (GLfloat *)v;
133 const GLfloat * const vp = a->vp;
134
135 out[0] = vp[0] * in[0] + vp[12];
136 out[1] = vp[5] * in[1] + vp[13];
137 }
138
139 static void insert_2f_viewport_1( const struct tnl_clipspace_attr *a, GLubyte *v,
140 const GLfloat *in )
141 {
142 GLfloat *out = (GLfloat *)v;
143 const GLfloat * const vp = a->vp;
144
145 out[0] = vp[0] * in[0] + vp[12];
146 out[1] = vp[13];
147 }
148
149
150 static void insert_4f_4( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
151 {
152 GLfloat *out = (GLfloat *)(v);
153
154 out[0] = in[0];
155 out[1] = in[1];
156 out[2] = in[2];
157 out[3] = in[3];
158 }
159
160 static void insert_4f_3( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
161 {
162 GLfloat *out = (GLfloat *)(v);
163
164 out[0] = in[0];
165 out[1] = in[1];
166 out[2] = in[2];
167 out[3] = 1;
168 }
169
170 static void insert_4f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
171 {
172 GLfloat *out = (GLfloat *)(v);
173
174 out[0] = in[0];
175 out[1] = in[1];
176 out[2] = 0;
177 out[3] = 1;
178 }
179
180 static void insert_4f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
181 {
182 GLfloat *out = (GLfloat *)(v);
183
184 out[0] = in[0];
185 out[1] = 0;
186 out[2] = 0;
187 out[3] = 1;
188 }
189
190 static void insert_3f_xyw_4( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
191 {
192 GLfloat *out = (GLfloat *)(v);
193
194 out[0] = in[0];
195 out[1] = in[1];
196 out[2] = in[3];
197 }
198
199 static void insert_3f_xyw_err( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
200 {
201 abort();
202 }
203
204 static void insert_3f_3( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
205 {
206 GLfloat *out = (GLfloat *)(v);
207
208 out[0] = in[0];
209 out[1] = in[1];
210 out[2] = in[2];
211 }
212
213 static void insert_3f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
214 {
215 GLfloat *out = (GLfloat *)(v);
216
217 out[0] = in[0];
218 out[1] = in[1];
219 out[2] = 0;
220 }
221
222 static void insert_3f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
223 {
224 GLfloat *out = (GLfloat *)(v);
225
226 out[0] = in[0];
227 out[1] = 0;
228 out[2] = 0;
229 }
230
231
232 static void insert_2f_2( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
233 {
234 GLfloat *out = (GLfloat *)(v);
235
236 out[0] = in[0];
237 out[1] = in[1];
238 }
239
240 static void insert_2f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
241 {
242 GLfloat *out = (GLfloat *)(v);
243
244 out[0] = in[0];
245 out[1] = 0;
246 }
247
248 static void insert_1f_1( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in )
249 {
250 GLfloat *out = (GLfloat *)(v);
251
252 out[0] = in[0];
253 }
254
255 static void insert_4chan_4f_rgba_4( const struct tnl_clipspace_attr *a, GLubyte *v,
256 const GLfloat *in )
257 {
258 GLchan *c = (GLchan *)v;
259 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
260 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
261 UNCLAMPED_FLOAT_TO_CHAN(c[2], in[2]);
262 UNCLAMPED_FLOAT_TO_CHAN(c[3], in[3]);
263 }
264
265 static void insert_4chan_4f_rgba_3( const struct tnl_clipspace_attr *a, GLubyte *v,
266 const GLfloat *in )
267 {
268 GLchan *c = (GLchan *)v;
269 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
270 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
271 UNCLAMPED_FLOAT_TO_CHAN(c[2], in[2]);
272 c[3] = CHAN_MAX;
273 }
274
275 static void insert_4chan_4f_rgba_2( const struct tnl_clipspace_attr *a, GLubyte *v,
276 const GLfloat *in )
277 {
278 GLchan *c = (GLchan *)v;
279 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
280 UNCLAMPED_FLOAT_TO_CHAN(c[1], in[1]);
281 c[2] = 0;
282 c[3] = CHAN_MAX;
283 }
284
285 static void insert_4chan_4f_rgba_1( const struct tnl_clipspace_attr *a, GLubyte *v,
286 const GLfloat *in )
287 {
288 GLchan *c = (GLchan *)v;
289 UNCLAMPED_FLOAT_TO_CHAN(c[0], in[0]);
290 c[1] = 0;
291 c[2] = 0;
292 c[3] = CHAN_MAX;
293 }
294
295 static void insert_4ub_4f_rgba_4( const struct tnl_clipspace_attr *a, GLubyte *v,
296 const GLfloat *in )
297 {
298 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
299 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
300 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
301 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[3]);
302 }
303
304 static void insert_4ub_4f_rgba_3( const struct tnl_clipspace_attr *a, GLubyte *v,
305 const GLfloat *in )
306 {
307 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
308 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
309 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
310 v[3] = 0xff;
311 }
312
313 static void insert_4ub_4f_rgba_2( const struct tnl_clipspace_attr *a, GLubyte *v,
314 const GLfloat *in )
315 {
316 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
317 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
318 v[2] = 0;
319 v[3] = 0xff;
320 }
321
322 static void insert_4ub_4f_rgba_1( const struct tnl_clipspace_attr *a, GLubyte *v,
323 const GLfloat *in )
324 {
325 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
326 v[1] = 0;
327 v[2] = 0;
328 v[3] = 0xff;
329 }
330
331 static void insert_4ub_4f_bgra_4( const struct tnl_clipspace_attr *a, GLubyte *v,
332 const GLfloat *in )
333 {
334 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
335 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
336 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
337 UNCLAMPED_FLOAT_TO_UBYTE(v[3], in[3]);
338 }
339
340 static void insert_4ub_4f_bgra_3( const struct tnl_clipspace_attr *a, GLubyte *v,
341 const GLfloat *in )
342 {
343 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
344 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
345 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
346 v[3] = 0xff;
347 }
348
349 static void insert_4ub_4f_bgra_2( const struct tnl_clipspace_attr *a, GLubyte *v,
350 const GLfloat *in )
351 {
352 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
353 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
354 v[0] = 0;
355 v[3] = 0xff;
356 }
357
358 static void insert_4ub_4f_bgra_1( const struct tnl_clipspace_attr *a, GLubyte *v,
359 const GLfloat *in )
360 {
361 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
362 v[1] = 0;
363 v[0] = 0;
364 v[3] = 0xff;
365 }
366
367 static void insert_3ub_3f_rgb_3( const struct tnl_clipspace_attr *a, GLubyte *v,
368 const GLfloat *in )
369 {
370 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
371 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
372 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[2]);
373 }
374
375 static void insert_3ub_3f_rgb_2( const struct tnl_clipspace_attr *a, GLubyte *v,
376 const GLfloat *in )
377 {
378 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
379 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
380 v[2] = 0;
381 }
382
383 static void insert_3ub_3f_rgb_1( const struct tnl_clipspace_attr *a, GLubyte *v,
384 const GLfloat *in )
385 {
386 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
387 v[1] = 0;
388 v[2] = 0;
389 }
390
391 static void insert_3ub_3f_bgr_3( const struct tnl_clipspace_attr *a, GLubyte *v,
392 const GLfloat *in )
393 {
394 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
395 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
396 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[2]);
397 }
398
399 static void insert_3ub_3f_bgr_2( const struct tnl_clipspace_attr *a, GLubyte *v,
400 const GLfloat *in )
401 {
402 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
403 UNCLAMPED_FLOAT_TO_UBYTE(v[1], in[1]);
404 v[0] = 0;
405 }
406
407 static void insert_3ub_3f_bgr_1( const struct tnl_clipspace_attr *a, GLubyte *v,
408 const GLfloat *in )
409 {
410 UNCLAMPED_FLOAT_TO_UBYTE(v[2], in[0]);
411 v[1] = 0;
412 v[0] = 0;
413 }
414
415
416 static void insert_1ub_1f_1( const struct tnl_clipspace_attr *a, GLubyte *v,
417 const GLfloat *in )
418 {
419 UNCLAMPED_FLOAT_TO_UBYTE(v[0], in[0]);
420 }
421
422
423 /***********************************************************************
424 * Functions to perform the reverse operations to the above, for
425 * swrast translation and clip-interpolation.
426 *
427 * Currently always extracts a full 4 floats.
428 */
429
430 static void extract_4f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
431 const GLubyte *v )
432 {
433 const GLfloat *in = (const GLfloat *)v;
434 const GLfloat * const vp = a->vp;
435
436 /* Although included for completeness, the position coordinate is
437 * usually handled differently during clipping.
438 */
439 out[0] = (in[0] - vp[12]) / vp[0];
440 out[1] = (in[1] - vp[13]) / vp[5];
441 out[2] = (in[2] - vp[14]) / vp[10];
442 out[3] = in[3];
443 }
444
445 static void extract_3f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
446 const GLubyte *v )
447 {
448 const GLfloat *in = (const GLfloat *)v;
449 const GLfloat * const vp = a->vp;
450
451 out[0] = (in[0] - vp[12]) / vp[0];
452 out[1] = (in[1] - vp[13]) / vp[5];
453 out[2] = (in[2] - vp[14]) / vp[10];
454 out[3] = 1;
455 }
456
457
458 static void extract_2f_viewport( const struct tnl_clipspace_attr *a, GLfloat *out,
459 const GLubyte *v )
460 {
461 const GLfloat *in = (const GLfloat *)v;
462 const GLfloat * const vp = a->vp;
463
464 out[0] = (in[0] - vp[12]) / vp[0];
465 out[1] = (in[1] - vp[13]) / vp[5];
466 out[2] = 0;
467 out[3] = 1;
468 }
469
470
471 static void extract_4f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
472 {
473 const GLfloat *in = (const GLfloat *)v;
474
475 out[0] = in[0];
476 out[1] = in[1];
477 out[2] = in[2];
478 out[3] = in[3];
479 }
480
481 static void extract_3f_xyw( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
482 {
483 const GLfloat *in = (const GLfloat *)v;
484
485 out[0] = in[0];
486 out[1] = in[1];
487 out[2] = 0;
488 out[3] = in[2];
489 }
490
491
492 static void extract_3f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
493 {
494 const GLfloat *in = (const GLfloat *)v;
495
496 out[0] = in[0];
497 out[1] = in[1];
498 out[2] = in[2];
499 out[3] = 1;
500 }
501
502
503 static void extract_2f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
504 {
505 const GLfloat *in = (const GLfloat *)v;
506
507 out[0] = in[0];
508 out[1] = in[1];
509 out[2] = 0;
510 out[3] = 1;
511 }
512
513 static void extract_1f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
514 {
515 const GLfloat *in = (const GLfloat *)v;
516
517 out[0] = in[0];
518 out[1] = 0;
519 out[2] = 0;
520 out[3] = 1;
521 }
522
523 static void extract_4chan_4f_rgba( const struct tnl_clipspace_attr *a, GLfloat *out,
524 const GLubyte *v )
525 {
526 GLchan *c = (GLchan *)v;
527
528 out[0] = CHAN_TO_FLOAT(c[0]);
529 out[1] = CHAN_TO_FLOAT(c[1]);
530 out[2] = CHAN_TO_FLOAT(c[2]);
531 out[3] = CHAN_TO_FLOAT(c[3]);
532 }
533
534 static void extract_4ub_4f_rgba( const struct tnl_clipspace_attr *a, GLfloat *out,
535 const GLubyte *v )
536 {
537 out[0] = UBYTE_TO_FLOAT(v[0]);
538 out[1] = UBYTE_TO_FLOAT(v[1]);
539 out[2] = UBYTE_TO_FLOAT(v[2]);
540 out[3] = UBYTE_TO_FLOAT(v[3]);
541 }
542
543 static void extract_4ub_4f_bgra( const struct tnl_clipspace_attr *a, GLfloat *out,
544 const GLubyte *v )
545 {
546 out[2] = UBYTE_TO_FLOAT(v[0]);
547 out[1] = UBYTE_TO_FLOAT(v[1]);
548 out[0] = UBYTE_TO_FLOAT(v[2]);
549 out[3] = UBYTE_TO_FLOAT(v[3]);
550 }
551
552 static void extract_3ub_3f_rgb( const struct tnl_clipspace_attr *a, GLfloat *out,
553 const GLubyte *v )
554 {
555 out[0] = UBYTE_TO_FLOAT(v[0]);
556 out[1] = UBYTE_TO_FLOAT(v[1]);
557 out[2] = UBYTE_TO_FLOAT(v[2]);
558 out[3] = 1;
559 }
560
561 static void extract_3ub_3f_bgr( const struct tnl_clipspace_attr *a, GLfloat *out,
562 const GLubyte *v )
563 {
564 out[2] = UBYTE_TO_FLOAT(v[0]);
565 out[1] = UBYTE_TO_FLOAT(v[1]);
566 out[0] = UBYTE_TO_FLOAT(v[2]);
567 out[3] = 1;
568 }
569
570 static void extract_1ub_1f( const struct tnl_clipspace_attr *a, GLfloat *out, const GLubyte *v )
571 {
572 out[0] = UBYTE_TO_FLOAT(v[0]);
573 out[1] = 0;
574 out[2] = 0;
575 out[3] = 1;
576 }
577
578
579 struct {
580 const char *name;
581 extract_func extract;
582 insert_func insert[4];
583 GLuint attrsize;
584 } format_info[EMIT_MAX] = {
585
586 { "1f",
587 extract_1f,
588 { insert_1f_1, insert_1f_1, insert_1f_1, insert_1f_1 },
589 sizeof(GLfloat) },
590
591 { "2f",
592 extract_2f,
593 { insert_2f_1, insert_2f_2, insert_2f_2, insert_2f_2 },
594 2 * sizeof(GLfloat) },
595
596 { "3f",
597 extract_3f,
598 { insert_3f_1, insert_3f_2, insert_3f_3, insert_3f_3 },
599 3 * sizeof(GLfloat) },
600
601 { "4f",
602 extract_4f,
603 { insert_4f_1, insert_4f_2, insert_4f_3, insert_4f_4 },
604 4 * sizeof(GLfloat) },
605
606 { "2f_viewport",
607 extract_2f_viewport,
608 { insert_2f_viewport_1, insert_2f_viewport_2, insert_2f_viewport_2,
609 insert_2f_viewport_2 },
610 2 * sizeof(GLfloat) },
611
612 { "3f_viewport",
613 extract_3f_viewport,
614 { insert_3f_viewport_1, insert_3f_viewport_2, insert_3f_viewport_3,
615 insert_3f_viewport_3 },
616 3 * sizeof(GLfloat) },
617
618 { "4f_viewport",
619 extract_4f_viewport,
620 { insert_4f_viewport_1, insert_4f_viewport_2, insert_4f_viewport_3,
621 insert_4f_viewport_4 },
622 4 * sizeof(GLfloat) },
623
624 { "3f_xyw",
625 extract_3f_xyw,
626 { insert_3f_xyw_err, insert_3f_xyw_err, insert_3f_xyw_err,
627 insert_3f_xyw_4 },
628 3 * sizeof(GLfloat) },
629
630 { "1ub_1f",
631 extract_1ub_1f,
632 { insert_1ub_1f_1, insert_1ub_1f_1, insert_1ub_1f_1, insert_1ub_1f_1 },
633 sizeof(GLubyte) },
634
635 { "3ub_3f_rgb",
636 extract_3ub_3f_rgb,
637 { insert_3ub_3f_rgb_1, insert_3ub_3f_rgb_2, insert_3ub_3f_rgb_3,
638 insert_3ub_3f_rgb_3 },
639 3 * sizeof(GLubyte) },
640
641 { "3ub_3f_bgr",
642 extract_3ub_3f_bgr,
643 { insert_3ub_3f_bgr_1, insert_3ub_3f_bgr_2, insert_3ub_3f_bgr_3,
644 insert_3ub_3f_bgr_3 },
645 3 * sizeof(GLubyte) },
646
647 { "4ub_4f_rgba",
648 extract_4ub_4f_rgba,
649 { insert_4ub_4f_rgba_1, insert_4ub_4f_rgba_2, insert_4ub_4f_rgba_3,
650 insert_4ub_4f_rgba_4 },
651 4 * sizeof(GLubyte) },
652
653 { "4ub_4f_bgra",
654 extract_4ub_4f_bgra,
655 { insert_4ub_4f_bgra_1, insert_4ub_4f_bgra_2, insert_4ub_4f_bgra_3,
656 insert_4ub_4f_bgra_4 },
657 4 * sizeof(GLubyte) },
658
659 { "4chan_4f_rgba",
660 extract_4chan_4f_rgba,
661 { insert_4chan_4f_rgba_1, insert_4chan_4f_rgba_2, insert_4chan_4f_rgba_3,
662 insert_4chan_4f_rgba_4 },
663 4 * sizeof(GLchan) },
664
665 { "pad",
666 0,
667 { 0, 0, 0, 0 },
668 0 }
669
670 };
671
672
673 /***********************************************************************
674 * Generic (non-codegen) functions for whole vertices or groups of
675 * vertices
676 */
677
678 static void generic_emit( GLcontext *ctx,
679 GLuint start, GLuint end,
680 void *dest )
681 {
682 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
683 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
684 struct tnl_clipspace_attr *a = vtx->attr;
685 GLubyte *v = (GLubyte *)dest;
686 GLuint i, j;
687 GLuint count = vtx->attr_count;
688 GLuint stride;
689
690 for (j = 0; j < count; j++) {
691 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
692 a[j].inputstride = vptr->stride;
693 a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride;
694 a[j].emit = a[j].insert[vptr->size - 1];
695 }
696
697 end -= start;
698 stride = vtx->vertex_size;
699
700 for (i = 0 ; i < end ; i++, v += stride) {
701 for (j = 0; j < count; j++) {
702 GLfloat *in = (GLfloat *)a[j].inputptr;
703 a[j].inputptr += a[j].inputstride;
704 a[j].emit( &a[j], v + a[j].vertoffset, in );
705 }
706 }
707 }
708
709
710 static void generic_interp( GLcontext *ctx,
711 GLfloat t,
712 GLuint edst, GLuint eout, GLuint ein,
713 GLboolean force_boundary )
714 {
715 TNLcontext *tnl = TNL_CONTEXT(ctx);
716 struct vertex_buffer *VB = &tnl->vb;
717 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
718 GLubyte *vin = vtx->vertex_buf + ein * vtx->vertex_size;
719 GLubyte *vout = vtx->vertex_buf + eout * vtx->vertex_size;
720 GLubyte *vdst = vtx->vertex_buf + edst * vtx->vertex_size;
721 const struct tnl_clipspace_attr *a = vtx->attr;
722 int attr_count = vtx->attr_count;
723 int j;
724
725 if (tnl->NeedNdcCoords) {
726 const GLfloat *dstclip = VB->ClipPtr->data[edst];
727 if (dstclip[3] != 0.0) {
728 const GLfloat w = 1.0f / dstclip[3];
729 GLfloat pos[4];
730
731 pos[0] = dstclip[0] * w;
732 pos[1] = dstclip[1] * w;
733 pos[2] = dstclip[2] * w;
734 pos[3] = w;
735
736 a[0].insert[4-1]( &a[0], vdst, pos );
737 }
738 }
739 else {
740 a[0].insert[4-1]( &a[0], vdst, VB->ClipPtr->data[edst] );
741 }
742
743
744 for (j = 1; j < attr_count; j++) {
745 GLfloat fin[4], fout[4], fdst[4];
746
747 a[j].extract( &a[j], fin, vin + a[j].vertoffset );
748 a[j].extract( &a[j], fout, vout + a[j].vertoffset );
749
750 INTERP_F( t, fdst[3], fout[3], fin[3] );
751 INTERP_F( t, fdst[2], fout[2], fin[2] );
752 INTERP_F( t, fdst[1], fout[1], fin[1] );
753 INTERP_F( t, fdst[0], fout[0], fin[0] );
754
755 a[j].insert[4-1]( &a[j], vdst + a[j].vertoffset, fdst );
756 }
757 }
758
759
760 /* Extract color attributes from one vertex and insert them into
761 * another. (Shortcircuit extract/insert with memcpy).
762 */
763 static void generic_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
764 {
765 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
766 GLubyte *vsrc = vtx->vertex_buf + esrc * vtx->vertex_size;
767 GLubyte *vdst = vtx->vertex_buf + edst * vtx->vertex_size;
768 const struct tnl_clipspace_attr *a = vtx->attr;
769 int attr_count = vtx->attr_count;
770 int j;
771
772 for (j = 0; j < attr_count; j++) {
773 if (a[j].attrib == VERT_ATTRIB_COLOR0 ||
774 a[j].attrib == VERT_ATTRIB_COLOR1) {
775
776 _mesa_memcpy( vdst + a[j].vertoffset,
777 vsrc + a[j].vertoffset,
778 a[j].vertattrsize );
779 }
780 }
781 }
782
783
784 /* Helper functions for hardware which doesn't put back colors and/or
785 * edgeflags into vertices.
786 */
787 static void generic_interp_extras( GLcontext *ctx,
788 GLfloat t,
789 GLuint dst, GLuint out, GLuint in,
790 GLboolean force_boundary )
791 {
792 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
793
794 if (VB->ColorPtr[1]) {
795 assert(VB->ColorPtr[1]->stride == 4 * sizeof(GLfloat));
796
797 INTERP_4F( t,
798 VB->ColorPtr[1]->data[dst],
799 VB->ColorPtr[1]->data[out],
800 VB->ColorPtr[1]->data[in] );
801
802 if (VB->SecondaryColorPtr[1]) {
803 INTERP_3F( t,
804 VB->SecondaryColorPtr[1]->data[dst],
805 VB->SecondaryColorPtr[1]->data[out],
806 VB->SecondaryColorPtr[1]->data[in] );
807 }
808 }
809 else if (VB->IndexPtr[1]) {
810 VB->IndexPtr[1]->data[dst][0] = LINTERP( t,
811 VB->IndexPtr[1]->data[out][0],
812 VB->IndexPtr[1]->data[in][0] );
813 }
814
815 if (VB->EdgeFlag) {
816 VB->EdgeFlag[dst] = VB->EdgeFlag[out] || force_boundary;
817 }
818
819 generic_interp(ctx, t, dst, out, in, force_boundary);
820 }
821
822 static void generic_copy_pv_extras( GLcontext *ctx,
823 GLuint dst, GLuint src )
824 {
825 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
826
827 if (VB->ColorPtr[1]) {
828 COPY_4FV( VB->ColorPtr[1]->data[dst],
829 VB->ColorPtr[1]->data[src] );
830
831 if (VB->SecondaryColorPtr[1]) {
832 COPY_4FV( VB->SecondaryColorPtr[1]->data[dst],
833 VB->SecondaryColorPtr[1]->data[src] );
834 }
835 }
836 else if (VB->IndexPtr[1]) {
837 VB->IndexPtr[1]->data[dst][0] = VB->IndexPtr[1]->data[src][0];
838 }
839
840 generic_copy_pv(ctx, dst, src);
841 }
842
843
844
845
846
847
848 /***********************************************************************
849 * Build codegen functions or return generic ones:
850 */
851
852
853 static void choose_emit_func( GLcontext *ctx,
854 GLuint start, GLuint end,
855 void *dest )
856 {
857 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
858 vtx->emit = generic_emit;
859 vtx->emit( ctx, start, end, dest );
860 }
861
862
863 static void choose_interp_func( GLcontext *ctx,
864 GLfloat t,
865 GLuint edst, GLuint eout, GLuint ein,
866 GLboolean force_boundary )
867 {
868 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
869
870 if (vtx->need_extras &&
871 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
872 vtx->interp = generic_interp_extras;
873 } else {
874 vtx->interp = generic_interp;
875 }
876
877 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
878 }
879
880
881 static void choose_copy_pv_func( GLcontext *ctx, GLuint edst, GLuint esrc )
882 {
883 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
884
885 if (vtx->need_extras &&
886 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
887 vtx->copy_pv = generic_copy_pv_extras;
888 } else {
889 vtx->copy_pv = generic_copy_pv;
890 }
891
892 vtx->copy_pv( ctx, edst, esrc );
893 }
894
895
896 /***********************************************************************
897 * Public entrypoints, mostly dispatch to the above:
898 */
899
900
901 /* Interpolate between two vertices to produce a third:
902 */
903 void _tnl_interp( GLcontext *ctx,
904 GLfloat t,
905 GLuint edst, GLuint eout, GLuint ein,
906 GLboolean force_boundary )
907 {
908 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
909 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
910 }
911
912 /* Copy colors from one vertex to another:
913 */
914 void _tnl_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
915 {
916 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
917 vtx->copy_pv( ctx, edst, esrc );
918 }
919
920
921 /* Extract a named attribute from a hardware vertex. Will have to
922 * reverse any viewport transformation, swizzling or other conversions
923 * which may have been applied:
924 */
925 void _tnl_get_attr( GLcontext *ctx, const void *vin,
926 GLenum attr, GLfloat *dest )
927 {
928 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
929 const struct tnl_clipspace_attr *a = vtx->attr;
930 int attr_count = vtx->attr_count;
931 int j;
932
933 for (j = 0; j < attr_count; j++) {
934 if (a[j].attrib == (int)attr) {
935 a[j].extract( &a[j], dest, (GLubyte *)vin + a[j].vertoffset );
936 return;
937 }
938 }
939
940 /* Else return the value from ctx->Current -- dangerous???
941 */
942 _mesa_memcpy( dest, ctx->Current.Attrib[attr], 4*sizeof(GLfloat));
943 }
944
945
946 /* Complementary operation to the above.
947 */
948 void _tnl_set_attr( GLcontext *ctx, void *vout,
949 GLenum attr, const GLfloat *src )
950 {
951 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
952 const struct tnl_clipspace_attr *a = vtx->attr;
953 int attr_count = vtx->attr_count;
954 int j;
955
956 for (j = 0; j < attr_count; j++) {
957 if (a[j].attrib == (int)attr) {
958 a[j].insert[4-1]( &a[j], (GLubyte *)vout + a[j].vertoffset, src );
959 return;
960 }
961 }
962 }
963
964
965 void *_tnl_get_vertex( GLcontext *ctx, GLuint nr )
966 {
967 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
968
969 return vtx->vertex_buf + nr * vtx->vertex_size;
970 }
971
972 void _tnl_invalidate_vertex_state( GLcontext *ctx, GLuint new_state )
973 {
974 if (new_state & (_DD_NEW_TRI_LIGHT_TWOSIDE|_DD_NEW_TRI_UNFILLED) ) {
975 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
976 vtx->new_inputs = ~0;
977 vtx->interp = choose_interp_func;
978 vtx->copy_pv = choose_copy_pv_func;
979 }
980 }
981
982
983 GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map,
984 GLuint nr, const GLfloat *vp,
985 GLuint unpacked_size )
986 {
987 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
988 int offset = 0;
989 GLuint i, j;
990
991 assert(nr < _TNL_ATTRIB_MAX);
992 assert(nr == 0 || map[0].attrib == VERT_ATTRIB_POS);
993
994 vtx->emit = choose_emit_func;
995 vtx->interp = choose_interp_func;
996 vtx->copy_pv = choose_copy_pv_func;
997 vtx->new_inputs = ~0;
998
999 for (j = 0, i = 0; i < nr; i++) {
1000 GLuint format = map[i].format;
1001 if (format == EMIT_PAD) {
1002 offset += map[i].offset;
1003
1004 /* fprintf(stderr, "%d: pad %d, offset now %d\n", i, */
1005 /* map[i].offset, offset); */
1006
1007 }
1008 else {
1009 vtx->attr[j].attrib = map[i].attrib;
1010 vtx->attr[j].vp = vp;
1011 vtx->attr[j].insert = format_info[format].insert;
1012 vtx->attr[j].extract = format_info[format].extract;
1013 vtx->attr[j].vertattrsize = format_info[format].attrsize;
1014
1015 if (unpacked_size)
1016 vtx->attr[j].vertoffset = map[i].offset;
1017 else
1018 vtx->attr[j].vertoffset = offset;
1019
1020 /* fprintf(stderr, "%d: %s offset %d\n", i, */
1021 /* format_info[format].name, vtx->attr[j].vertoffset); */
1022
1023 offset += format_info[format].attrsize;
1024 j++;
1025 }
1026 }
1027
1028 vtx->attr_count = j;
1029
1030 if (unpacked_size)
1031 vtx->vertex_size = unpacked_size;
1032 else
1033 vtx->vertex_size = offset;
1034
1035 assert(vtx->vertex_size <= vtx->max_vertex_size);
1036
1037 return vtx->vertex_size;
1038 }
1039
1040
1041
1042 void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs )
1043 {
1044 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1045 vtx->new_inputs |= newinputs;
1046 }
1047
1048
1049
1050 void _tnl_build_vertices( GLcontext *ctx,
1051 GLuint start,
1052 GLuint end,
1053 GLuint newinputs )
1054 {
1055 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1056 GLuint stride = vtx->vertex_size;
1057 GLubyte *v = ((GLubyte *)vtx->vertex_buf + (start*stride));
1058
1059 newinputs |= vtx->new_inputs;
1060 vtx->new_inputs = 0;
1061
1062 if (newinputs)
1063 vtx->emit( ctx, start, end, v );
1064 }
1065
1066
1067 void *_tnl_emit_vertices_to_buffer( GLcontext *ctx,
1068 GLuint start,
1069 GLuint end,
1070 void *dest )
1071 {
1072 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1073 vtx->emit( ctx, start, end, dest );
1074 return (void *)((GLubyte *)dest + vtx->vertex_size * (end - start));
1075 }
1076
1077
1078 void _tnl_init_vertices( GLcontext *ctx,
1079 GLuint vb_size,
1080 GLuint max_vertex_size )
1081 {
1082 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1083
1084 _tnl_install_attrs( ctx, 0, 0, 0, 0 );
1085
1086 vtx->need_extras = GL_TRUE;
1087 if (max_vertex_size > vtx->max_vertex_size) {
1088 _tnl_free_vertices( ctx );
1089 vtx->max_vertex_size = max_vertex_size;
1090 vtx->vertex_buf = (GLubyte *)ALIGN_CALLOC(vb_size * max_vertex_size, 32 );
1091 }
1092 }
1093
1094
1095 void _tnl_free_vertices( GLcontext *ctx )
1096 {
1097 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
1098 if (vtx->vertex_buf) {
1099 ALIGN_FREE(vtx->vertex_buf);
1100 vtx->vertex_buf = 0;
1101 }
1102 }