Small Unichrome fixes:
[mesa.git] / src / mesa / drivers / dri / unichrome / via_tris.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. 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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <math.h>
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "mtypes.h"
31 #include "macros.h"
32 #include "colormac.h"
33 #include "enums.h"
34
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
37 #include "tnl/t_context.h"
38 #include "tnl/t_pipeline.h"
39
40 #include "via_context.h"
41 #include "via_tris.h"
42 #include "via_state.h"
43 #include "via_span.h"
44 #include "via_ioctl.h"
45
46 /***********************************************************************
47 * Emit primitives as inline vertices *
48 ***********************************************************************/
49
50 #if 1
51 #define COPY_DWORDS(vb, vertsize, v) \
52 do { \
53 int j; \
54 int __tmp; \
55 __asm__ __volatile__("rep ; movsl" \
56 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
57 : "0" (vertsize), \
58 "D" ((long)vb), \
59 "S" ((long)v)); \
60 } while (0)
61 #else
62 #define COPY_DWORDS(vb, vertsize, v) \
63 do { \
64 int j; \
65 for (j = 0; j < vertsize; j++) \
66 vb[j] = ((GLuint *)v)[j]; \
67 vb += vertsize; \
68 } while (0)
69 #endif
70
71 static void via_draw_triangle(viaContextPtr vmesa,
72 viaVertexPtr v0,
73 viaVertexPtr v1,
74 viaVertexPtr v2)
75 {
76 GLuint vertsize = vmesa->vertexSize;
77 GLuint *vb = viaExtendPrimitive(vmesa, 3 * 4 * vertsize);
78
79 COPY_DWORDS(vb, vertsize, v0);
80 COPY_DWORDS(vb, vertsize, v1);
81 COPY_DWORDS(vb, vertsize, v2);
82 }
83
84
85 static void via_draw_quad(viaContextPtr vmesa,
86 viaVertexPtr v0,
87 viaVertexPtr v1,
88 viaVertexPtr v2,
89 viaVertexPtr v3)
90 {
91 GLuint vertsize = vmesa->vertexSize;
92 GLuint *vb = viaExtendPrimitive(vmesa, 6 * 4 * vertsize);
93
94 COPY_DWORDS(vb, vertsize, v0);
95 COPY_DWORDS(vb, vertsize, v1);
96 COPY_DWORDS(vb, vertsize, v3);
97 COPY_DWORDS(vb, vertsize, v1);
98 COPY_DWORDS(vb, vertsize, v2);
99 COPY_DWORDS(vb, vertsize, v3);
100 }
101
102 static void via_draw_line(viaContextPtr vmesa,
103 viaVertexPtr v0,
104 viaVertexPtr v1)
105 {
106 GLuint vertsize = vmesa->vertexSize;
107 GLuint *vb = viaExtendPrimitive(vmesa, 2 * 4 * vertsize);
108 COPY_DWORDS(vb, vertsize, v0);
109 COPY_DWORDS(vb, vertsize, v1);
110 }
111
112
113 static void via_draw_point(viaContextPtr vmesa,
114 viaVertexPtr v0)
115 {
116 GLuint vertsize = vmesa->vertexSize;
117 GLuint *vb = viaExtendPrimitive(vmesa, 4 * vertsize);
118 COPY_DWORDS(vb, vertsize, v0);
119 }
120
121
122 /* Fallback drawing functions for the ptex hack.
123 */
124 #define PTEX_VERTEX( tmp, vertex_size, v) \
125 do { \
126 GLuint j; \
127 GLfloat rhw = 1.0 / v->f[vertex_size]; \
128 for ( j = 0 ; j < vertex_size ; j++ ) \
129 tmp.f[j] = v->f[j]; \
130 tmp.f[3] *= v->f[vertex_size]; \
131 tmp.f[vertex_size-2] *= rhw; \
132 tmp.f[vertex_size-1] *= rhw; \
133 } while (0)
134
135 static void via_ptex_tri (viaContextPtr vmesa,
136 viaVertexPtr v0,
137 viaVertexPtr v1,
138 viaVertexPtr v2)
139 {
140 GLuint vertsize = vmesa->hwVertexSize;
141 GLuint *vb = viaExtendPrimitive(vmesa, 3*4*vertsize);
142 viaVertex tmp;
143
144 PTEX_VERTEX(tmp, vertsize, v0); COPY_DWORDS(vb, vertsize, &tmp);
145 PTEX_VERTEX(tmp, vertsize, v1); COPY_DWORDS(vb, vertsize, &tmp);
146 PTEX_VERTEX(tmp, vertsize, v2); COPY_DWORDS(vb, vertsize, &tmp);
147 }
148
149 static void via_ptex_line (viaContextPtr vmesa,
150 viaVertexPtr v0,
151 viaVertexPtr v1)
152 {
153 GLuint vertsize = vmesa->hwVertexSize;
154 GLuint *vb = viaExtendPrimitive(vmesa, 2*4*vertsize);
155 viaVertex tmp;
156
157 PTEX_VERTEX(tmp, vertsize, v0); COPY_DWORDS(vb, vertsize, &tmp);
158 PTEX_VERTEX(tmp, vertsize, v1); COPY_DWORDS(vb, vertsize, &tmp);
159 }
160
161 static void via_ptex_point (viaContextPtr vmesa,
162 viaVertexPtr v0)
163 {
164 GLuint vertsize = vmesa->hwVertexSize;
165 GLuint *vb = viaExtendPrimitive(vmesa, 1*4*vertsize);
166 viaVertex tmp;
167
168 PTEX_VERTEX(tmp, vertsize, v0); COPY_DWORDS(vb, vertsize, &tmp);
169 }
170
171
172
173
174
175 /***********************************************************************
176 * Macros for via_dd_tritmp.h to draw basic primitives *
177 ***********************************************************************/
178
179 #define TRI(a, b, c) \
180 do { \
181 if (DO_FALLBACK) \
182 vmesa->drawTri(vmesa, a, b, c); \
183 else \
184 via_draw_triangle(vmesa, a, b, c); \
185 } while (0)
186
187 #define QUAD(a, b, c, d) \
188 do { \
189 if (DO_FALLBACK) { \
190 vmesa->drawTri(vmesa, a, b, d); \
191 vmesa->drawTri(vmesa, b, c, d); \
192 } \
193 else \
194 via_draw_quad(vmesa, a, b, c, d); \
195 } while (0)
196
197 #define LINE(v0, v1) \
198 do { \
199 if (DO_FALLBACK) \
200 vmesa->drawLine(vmesa, v0, v1); \
201 else \
202 via_draw_line(vmesa, v0, v1); \
203 } while (0)
204
205 #define POINT(v0) \
206 do { \
207 if (DO_FALLBACK) \
208 vmesa->drawPoint(vmesa, v0); \
209 else \
210 via_draw_point(vmesa, v0); \
211 } while (0)
212
213
214 /***********************************************************************
215 * Build render functions from dd templates *
216 ***********************************************************************/
217
218 #define VIA_OFFSET_BIT 0x01
219 #define VIA_TWOSIDE_BIT 0x02
220 #define VIA_UNFILLED_BIT 0x04
221 #define VIA_FALLBACK_BIT 0x08
222 #define VIA_MAX_TRIFUNC 0x10
223
224
225 static struct {
226 tnl_points_func points;
227 tnl_line_func line;
228 tnl_triangle_func triangle;
229 tnl_quad_func quad;
230 } rast_tab[VIA_MAX_TRIFUNC];
231
232
233 #define DO_FALLBACK (IND & VIA_FALLBACK_BIT)
234 #define DO_OFFSET (IND & VIA_OFFSET_BIT)
235 #define DO_UNFILLED (IND & VIA_UNFILLED_BIT)
236 #define DO_TWOSIDE (IND & VIA_TWOSIDE_BIT)
237 #define DO_FLAT 0
238 #define DO_TRI 1
239 #define DO_QUAD 1
240 #define DO_LINE 1
241 #define DO_POINTS 1
242 #define DO_FULL_QUAD 1
243
244 #define HAVE_RGBA 1
245 #define HAVE_SPEC 1
246 #define HAVE_BACK_COLORS 0
247 #define HAVE_HW_FLATSHADE 1
248 #define VERTEX viaVertex
249 #define TAB rast_tab
250
251 /* Only used to pull back colors into vertices (ie, we know color is
252 * floating point).
253 */
254 #define VIA_COLOR(dst, src) \
255 do { \
256 dst[0] = src[2]; \
257 dst[1] = src[1]; \
258 dst[2] = src[0]; \
259 dst[3] = src[3]; \
260 } while (0)
261
262 #define VIA_SPEC(dst, src) \
263 do { \
264 dst[0] = src[2]; \
265 dst[1] = src[1]; \
266 dst[2] = src[0]; \
267 } while (0)
268
269
270 #define DEPTH_SCALE vmesa->polygon_offset_scale
271 #define UNFILLED_TRI unfilled_tri
272 #define UNFILLED_QUAD unfilled_quad
273 #define VERT_X(_v) _v->v.x
274 #define VERT_Y(_v) _v->v.y
275 #define VERT_Z(_v) _v->v.z
276 #define AREA_IS_CCW(a) (a > 0)
277 #define GET_VERTEX(e) (vmesa->verts + (e * vmesa->vertexSize * sizeof(int)))
278
279 #define VERT_SET_RGBA( v, c ) \
280 do { \
281 via_color_t *color = (via_color_t *)&((v)->ui[coloroffset]); \
282 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
283 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
284 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
285 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
286 } while (0)
287
288 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
289
290 #define VERT_SET_SPEC( v0, c ) \
291 do { \
292 if (specoffset) { \
293 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]); \
294 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]); \
295 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]); \
296 } \
297 } while (0)
298 #define VERT_COPY_SPEC( v0, v1 ) \
299 do { \
300 if (specoffset) { \
301 v0->v.specular.red = v1->v.specular.red; \
302 v0->v.specular.green = v1->v.specular.green; \
303 v0->v.specular.blue = v1->v.specular.blue; \
304 } \
305 } while (0)
306
307
308 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
309 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
310 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
311 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
312
313
314 #define LOCAL_VARS(n) \
315 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
316 GLuint color[n], spec[n]; \
317 GLuint coloroffset = vmesa->coloroffset; \
318 GLuint specoffset = vmesa->specoffset; \
319 (void)color; (void)spec; (void)coloroffset; (void)specoffset;
320
321
322 /***********************************************************************
323 * Helpers for rendering unfilled primitives *
324 ***********************************************************************/
325
326 static const GLenum hwPrim[GL_POLYGON + 1] = {
327 GL_POINTS,
328 GL_LINES,
329 GL_LINES,
330 GL_LINES,
331 GL_TRIANGLES,
332 GL_TRIANGLES,
333 GL_TRIANGLES,
334 GL_TRIANGLES,
335 GL_TRIANGLES,
336 GL_TRIANGLES
337 };
338
339
340 #define RASTERIZE(x) viaRasterPrimitive( ctx, x, hwPrim[x] )
341 #define RENDER_PRIMITIVE vmesa->renderPrimitive
342 #define TAG(x) x
343 #define IND VIA_FALLBACK_BIT
344 #include "tnl_dd/t_dd_unfilled.h"
345 #undef IND
346 #undef RASTERIZE
347
348 /***********************************************************************
349 * Generate GL render functions *
350 ***********************************************************************/
351 #define RASTERIZE(x)
352
353 #define IND (0)
354 #define TAG(x) x
355 #include "tnl_dd/t_dd_tritmp.h"
356
357 #define IND (VIA_OFFSET_BIT)
358 #define TAG(x) x##_offset
359 #include "tnl_dd/t_dd_tritmp.h"
360
361 #define IND (VIA_TWOSIDE_BIT)
362 #define TAG(x) x##_twoside
363 #include "tnl_dd/t_dd_tritmp.h"
364
365 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT)
366 #define TAG(x) x##_twoside_offset
367 #include "tnl_dd/t_dd_tritmp.h"
368
369 #define IND (VIA_UNFILLED_BIT)
370 #define TAG(x) x##_unfilled
371 #include "tnl_dd/t_dd_tritmp.h"
372
373 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
374 #define TAG(x) x##_offset_unfilled
375 #include "tnl_dd/t_dd_tritmp.h"
376
377 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT)
378 #define TAG(x) x##_twoside_unfilled
379 #include "tnl_dd/t_dd_tritmp.h"
380
381 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
382 #define TAG(x) x##_twoside_offset_unfilled
383 #include "tnl_dd/t_dd_tritmp.h"
384
385 #define IND (VIA_FALLBACK_BIT)
386 #define TAG(x) x##_fallback
387 #include "tnl_dd/t_dd_tritmp.h"
388
389 #define IND (VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
390 #define TAG(x) x##_offset_fallback
391 #include "tnl_dd/t_dd_tritmp.h"
392
393 #define IND (VIA_TWOSIDE_BIT|VIA_FALLBACK_BIT)
394 #define TAG(x) x##_twoside_fallback
395 #include "tnl_dd/t_dd_tritmp.h"
396
397 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
398 #define TAG(x) x##_twoside_offset_fallback
399 #include "tnl_dd/t_dd_tritmp.h"
400
401 #define IND (VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
402 #define TAG(x) x##_unfilled_fallback
403 #include "tnl_dd/t_dd_tritmp.h"
404
405 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
406 #define TAG(x) x##_offset_unfilled_fallback
407 #include "tnl_dd/t_dd_tritmp.h"
408
409 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
410 #define TAG(x) x##_twoside_unfilled_fallback
411 #include "tnl_dd/t_dd_tritmp.h"
412
413 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT| \
414 VIA_FALLBACK_BIT)
415 #define TAG(x) x##_twoside_offset_unfilled_fallback
416 #include "tnl_dd/t_dd_tritmp.h"
417
418
419 static void init_rast_tab(void)
420 {
421 init();
422 init_offset();
423 init_twoside();
424 init_twoside_offset();
425 init_unfilled();
426 init_offset_unfilled();
427 init_twoside_unfilled();
428 init_twoside_offset_unfilled();
429 init_fallback();
430 init_offset_fallback();
431 init_twoside_fallback();
432 init_twoside_offset_fallback();
433 init_unfilled_fallback();
434 init_offset_unfilled_fallback();
435 init_twoside_unfilled_fallback();
436 init_twoside_offset_unfilled_fallback();
437 }
438
439
440 /***********************************************************************
441 * Rasterization fallback helpers *
442 ***********************************************************************/
443
444
445 /* This code is hit only when a mix of accelerated and unaccelerated
446 * primitives are being drawn, and only for the unaccelerated
447 * primitives.
448 */
449 static void
450 via_fallback_tri(viaContextPtr vmesa,
451 viaVertex *v0,
452 viaVertex *v1,
453 viaVertex *v2)
454 {
455 GLcontext *ctx = vmesa->glCtx;
456 SWvertex v[3];
457 _swsetup_Translate(ctx, v0, &v[0]);
458 _swsetup_Translate(ctx, v1, &v[1]);
459 _swsetup_Translate(ctx, v2, &v[2]);
460 viaSpanRenderStart( ctx );
461 _swrast_Triangle(ctx, &v[0], &v[1], &v[2]);
462 viaSpanRenderFinish( ctx );
463 }
464
465
466 static void
467 via_fallback_line(viaContextPtr vmesa,
468 viaVertex *v0,
469 viaVertex *v1)
470 {
471 GLcontext *ctx = vmesa->glCtx;
472 SWvertex v[2];
473 _swsetup_Translate(ctx, v0, &v[0]);
474 _swsetup_Translate(ctx, v1, &v[1]);
475 viaSpanRenderStart( ctx );
476 _swrast_Line(ctx, &v[0], &v[1]);
477 viaSpanRenderFinish( ctx );
478 }
479
480
481 static void
482 via_fallback_point(viaContextPtr vmesa,
483 viaVertex *v0)
484 {
485 GLcontext *ctx = vmesa->glCtx;
486 SWvertex v[1];
487 _swsetup_Translate(ctx, v0, &v[0]);
488 viaSpanRenderStart( ctx );
489 _swrast_Point(ctx, &v[0]);
490 viaSpanRenderFinish( ctx );
491 }
492
493 /**********************************************************************/
494 /* Render unclipped begin/end objects */
495 /**********************************************************************/
496 #define IND 0
497 #define V(x) (viaVertex *)(vertptr + ((x) * vertsize * sizeof(int)))
498 #define RENDER_POINTS(start, count) \
499 for (; start < count; start++) POINT(V(ELT(start)));
500 #define RENDER_LINE(v0, v1) LINE(V(v0), V(v1))
501 #define RENDER_TRI( v0, v1, v2) TRI( V(v0), V(v1), V(v2))
502 #define RENDER_QUAD(v0, v1, v2, v3) QUAD(V(v0), V(v1), V(v2), V(v3))
503 #define INIT(x) viaRasterPrimitive(ctx, x, hwPrim[x])
504 #undef LOCAL_VARS
505 #define LOCAL_VARS \
506 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
507 GLubyte *vertptr = (GLubyte *)vmesa->verts; \
508 const GLuint vertsize = vmesa->vertexSize; \
509 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
510 (void)elt;
511 #define RESET_STIPPLE
512 #define RESET_OCCLUSION
513 #define PRESERVE_VB_DEFS
514 #define ELT(x) x
515 #define TAG(x) via_fast##x##_verts
516 #include "tnl/t_vb_rendertmp.h"
517 #undef ELT
518 #undef TAG
519 #define TAG(x) via_fast##x##_elts
520 #define ELT(x) elt[x]
521 #include "tnl/t_vb_rendertmp.h"
522 #undef ELT
523 #undef TAG
524 #undef NEED_EDGEFLAG_SETUP
525 #undef EDGEFLAG_GET
526 #undef EDGEFLAG_SET
527 #undef RESET_OCCLUSION
528
529
530 /**********************************************************************/
531 /* Render clipped primitives */
532 /**********************************************************************/
533
534
535
536 static void viaRenderClippedPoly(GLcontext *ctx, const GLuint *elts,
537 GLuint n)
538 {
539 TNLcontext *tnl = TNL_CONTEXT(ctx);
540 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
541 GLuint prim = VIA_CONTEXT(ctx)->renderPrimitive;
542
543 /* Render the new vertices as an unclipped polygon.
544 */
545 {
546 GLuint *tmp = VB->Elts;
547 VB->Elts = (GLuint *)elts;
548 tnl->Driver.Render.PrimTabElts[GL_POLYGON](ctx, 0, n,
549 PRIM_BEGIN|PRIM_END);
550 VB->Elts = tmp;
551 }
552
553 /* Restore the render primitive
554 */
555 if (prim != GL_POLYGON)
556 tnl->Driver.Render.PrimitiveNotify( ctx, prim );
557 }
558
559 static void viaRenderClippedLine(GLcontext *ctx, GLuint ii, GLuint jj)
560 {
561 TNLcontext *tnl = TNL_CONTEXT(ctx);
562 tnl->Driver.Render.Line(ctx, ii, jj);
563 }
564
565 static void viaFastRenderClippedPoly(GLcontext *ctx, const GLuint *elts,
566 GLuint n)
567 {
568 viaContextPtr vmesa = VIA_CONTEXT(ctx);
569 GLuint vertsize = vmesa->vertexSize;
570 GLuint *vb = viaExtendPrimitive(vmesa, (n - 2) * 3 * 4 * vertsize);
571 GLubyte *vertptr = (GLubyte *)vmesa->verts;
572 const GLuint *start = (const GLuint *)V(elts[0]);
573 int i;
574
575 for (i = 2; i < n; i++) {
576 COPY_DWORDS(vb, vertsize, V(elts[i - 1]));
577 COPY_DWORDS(vb, vertsize, V(elts[i]));
578 COPY_DWORDS(vb, vertsize, start);
579 }
580 }
581
582 /**********************************************************************/
583 /* Choose render functions */
584 /**********************************************************************/
585
586
587
588
589 #define _VIA_NEW_VERTEX (_NEW_TEXTURE | \
590 _DD_NEW_SEPARATE_SPECULAR | \
591 _DD_NEW_TRI_UNFILLED | \
592 _DD_NEW_TRI_LIGHT_TWOSIDE | \
593 _NEW_FOG)
594
595 #define _VIA_NEW_RENDERSTATE (_DD_NEW_LINE_STIPPLE | \
596 _DD_NEW_TRI_UNFILLED | \
597 _DD_NEW_TRI_LIGHT_TWOSIDE | \
598 _DD_NEW_TRI_OFFSET | \
599 _DD_NEW_TRI_STIPPLE | \
600 _NEW_POLYGONSTIPPLE)
601
602 /* Via does support line stipple in hardware, and it is partially
603 * working in the older versions of this driver:
604 */
605 #define LINE_FALLBACK (DD_LINE_STIPPLE)
606 #define POINT_FALLBACK (0)
607 #define TRI_FALLBACK (0)
608 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
609 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET|DD_TRI_UNFILLED)
610
611 static void viaChooseRenderState(GLcontext *ctx)
612 {
613 TNLcontext *tnl = TNL_CONTEXT(ctx);
614 viaContextPtr vmesa = VIA_CONTEXT(ctx);
615 GLuint flags = ctx->_TriangleCaps;
616 GLuint index = 0;
617
618 if (vmesa->ptexHack) {
619 vmesa->drawPoint = via_ptex_point;
620 vmesa->drawLine = via_ptex_line;
621 vmesa->drawTri = via_ptex_tri;
622 index |= VIA_FALLBACK_BIT;
623 }
624 else {
625 vmesa->drawPoint = via_draw_point;
626 vmesa->drawLine = via_draw_line;
627 vmesa->drawTri = via_draw_triangle;
628 }
629
630 if (flags & (ANY_FALLBACK_FLAGS|ANY_RASTER_FLAGS)) {
631 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= VIA_TWOSIDE_BIT;
632 if (flags & DD_TRI_OFFSET) index |= VIA_OFFSET_BIT;
633 if (flags & DD_TRI_UNFILLED) index |= VIA_UNFILLED_BIT;
634 if (flags & ANY_FALLBACK_FLAGS) index |= VIA_FALLBACK_BIT;
635
636 /* Hook in fallbacks for specific primitives.
637 */
638 if (flags & POINT_FALLBACK)
639 vmesa->drawPoint = via_fallback_point;
640
641 if (flags & LINE_FALLBACK)
642 vmesa->drawLine = via_fallback_line;
643
644 if (flags & TRI_FALLBACK)
645 vmesa->drawTri = via_fallback_tri;
646 }
647
648 if (vmesa->renderIndex != index) {
649 vmesa->renderIndex = index;
650
651 tnl->Driver.Render.Points = rast_tab[index].points;
652 tnl->Driver.Render.Line = rast_tab[index].line;
653 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
654 tnl->Driver.Render.Quad = rast_tab[index].quad;
655
656 if (index == 0) {
657 tnl->Driver.Render.PrimTabVerts = via_fastrender_tab_verts;
658 tnl->Driver.Render.PrimTabElts = via_fastrender_tab_elts;
659 tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
660 tnl->Driver.Render.ClippedPolygon = viaFastRenderClippedPoly;
661 }
662 else {
663 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
664 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
665 tnl->Driver.Render.ClippedLine = viaRenderClippedLine;
666 tnl->Driver.Render.ClippedPolygon = viaRenderClippedPoly;
667 }
668 }
669 }
670
671
672 #define VIA_EMIT_TEX1 0x01
673 #define VIA_EMIT_TEX0 0x02
674 #define VIA_EMIT_PTEX0 0x04
675 #define VIA_EMIT_RGBA 0x08
676 #define VIA_EMIT_SPEC 0x10
677 #define VIA_EMIT_FOG 0x20
678 #define VIA_EMIT_W 0x40
679
680 #define EMIT_ATTR( ATTR, STYLE, INDEX, REGB ) \
681 do { \
682 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = (ATTR); \
683 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = (STYLE); \
684 vmesa->vertex_attr_count++; \
685 setupIndex |= (INDEX); \
686 regCmdB |= (REGB); \
687 } while (0)
688
689 #define EMIT_PAD( N ) \
690 do { \
691 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = 0; \
692 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = EMIT_PAD; \
693 vmesa->vertex_attrs[vmesa->vertex_attr_count].offset = (N); \
694 vmesa->vertex_attr_count++; \
695 } while (0)
696
697
698
699 static void viaChooseVertexState( GLcontext *ctx )
700 {
701 viaContextPtr vmesa = VIA_CONTEXT(ctx);
702 TNLcontext *tnl = TNL_CONTEXT(ctx);
703 GLuint index = tnl->render_inputs;
704 GLuint regCmdB = HC_HVPMSK_X | HC_HVPMSK_Y | HC_HVPMSK_Z;
705 GLuint setupIndex = 0;
706
707 vmesa->vertex_attr_count = 0;
708
709 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
710 * build up a hardware vertex.
711 */
712 if (index & (_TNL_BITS_TEX_ANY|_TNL_BIT_FOG)) {
713 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, VIA_EMIT_W, HC_HVPMSK_W );
714 vmesa->coloroffset = 4;
715 }
716 else {
717 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, 0, 0 );
718 vmesa->coloroffset = 3;
719 }
720
721 /* t_context.c always includes a diffuse color */
722 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, VIA_EMIT_RGBA, HC_HVPMSK_Cd );
723
724 vmesa->specoffset = 0;
725 if (index & (_TNL_BIT_COLOR1|_TNL_BIT_FOG)) {
726 if ((index & _TNL_BIT_COLOR1)) {
727 vmesa->specoffset = vmesa->coloroffset + 1;
728 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_3UB_3F_BGR, VIA_EMIT_SPEC, HC_HVPMSK_Cs );
729 }
730 else
731 EMIT_PAD( 3 );
732
733 if ((index & _TNL_BIT_FOG))
734 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1UB_1F, VIA_EMIT_FOG, HC_HVPMSK_Cs );
735 else
736 EMIT_PAD( 1 );
737 }
738
739 if (index & _TNL_BIT_TEX(0)) {
740 if (vmesa->ptexHack)
741 EMIT_ATTR( _TNL_ATTRIB_TEX0, EMIT_3F_XYW, VIA_EMIT_PTEX0, (HC_HVPMSK_S | HC_HVPMSK_T) );
742 else
743 EMIT_ATTR( _TNL_ATTRIB_TEX0, EMIT_2F, VIA_EMIT_TEX0, (HC_HVPMSK_S | HC_HVPMSK_T) );
744 }
745
746 if (index & _TNL_BIT_TEX(1)) {
747 EMIT_ATTR( _TNL_ATTRIB_TEX1, EMIT_2F, VIA_EMIT_TEX1, 0 ); /* how does the hardware find out about this? */
748 }
749
750 if (setupIndex != vmesa->setupIndex) {
751 vmesa->vertexSize = _tnl_install_attrs( ctx,
752 vmesa->vertex_attrs,
753 vmesa->vertex_attr_count,
754 vmesa->ViewportMatrix.m, 0 );
755 vmesa->vertexSize >>= 2;
756 vmesa->setupIndex = setupIndex;
757 vmesa->regCmdB &= ~HC_HVPMSK_MASK;
758 vmesa->regCmdB |= regCmdB;
759
760 if (vmesa->ptexHack)
761 vmesa->hwVertexSize = vmesa->vertexSize - 1;
762 else
763 vmesa->hwVertexSize = vmesa->vertexSize;
764 }
765 }
766
767
768
769
770 /* Check if projective texture coordinates are used and if we can fake
771 * them. Fallback to swrast we can't. Returns GL_TRUE if projective
772 * texture coordinates must be faked, GL_FALSE otherwise.
773 */
774 static GLboolean viaCheckPTexHack( GLcontext *ctx )
775 {
776 TNLcontext *tnl = TNL_CONTEXT(ctx);
777 struct vertex_buffer *VB = &tnl->vb;
778 GLuint index = tnl->render_inputs;
779 GLboolean fallback = GL_FALSE;
780 GLboolean ptexHack = GL_FALSE;
781
782 if (index & _TNL_BIT_TEX(0) && VB->TexCoordPtr[0]->size == 4) {
783 if ((index & _TNL_BITS_TEX_ANY) == _TNL_BIT_TEX(0))
784 ptexHack = GL_TRUE;
785 else
786 fallback = GL_TRUE;
787 }
788 if ((index & _TNL_BIT_TEX(1)) && VB->TexCoordPtr[1]->size == 4)
789 fallback = GL_TRUE;
790
791 FALLBACK(VIA_CONTEXT(ctx), VIA_FALLBACK_PROJ_TEXTURE, fallback);
792 return ptexHack;
793 }
794
795
796
797
798 /**********************************************************************/
799 /* High level hooks for t_vb_render.c */
800 /**********************************************************************/
801
802
803 static void viaRenderStart(GLcontext *ctx)
804 {
805 viaContextPtr vmesa = VIA_CONTEXT(ctx);
806 TNLcontext *tnl = TNL_CONTEXT(ctx);
807 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
808
809 {
810 GLboolean ptexHack = viaCheckPTexHack( ctx );
811 if (ptexHack != vmesa->ptexHack) {
812 vmesa->ptexHack = ptexHack;
813 vmesa->newRenderState |= _VIA_NEW_RENDERSTATE;
814 }
815 }
816
817 if (vmesa->newState) {
818 vmesa->newRenderState |= vmesa->newState;
819 viaValidateState( ctx );
820 }
821
822 if (vmesa->Fallback) {
823 tnl->Driver.Render.Start(ctx);
824 return;
825 }
826
827 if (vmesa->newRenderState) {
828 viaChooseVertexState(ctx);
829 viaChooseRenderState(ctx);
830 vmesa->newRenderState = 0;
831 }
832
833 /* Important:
834 */
835 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
836 }
837
838 static void viaRenderFinish(GLcontext *ctx)
839 {
840 if (VIA_CONTEXT(ctx)->renderIndex & VIA_FALLBACK_BIT)
841 _swrast_flush(ctx);
842 else
843 VIA_FINISH_PRIM(VIA_CONTEXT(ctx));
844 }
845
846
847 /* System to flush dma and emit state changes based on the rasterized
848 * primitive.
849 */
850 void viaRasterPrimitive(GLcontext *ctx,
851 GLenum glprim,
852 GLenum hwprim)
853 {
854 viaContextPtr vmesa = VIA_CONTEXT(ctx);
855 GLuint regCmdB;
856 RING_VARS;
857
858 if (VIA_DEBUG)
859 fprintf(stderr, "%s: %s/%s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(glprim),
860 _mesa_lookup_enum_by_nr(hwprim));
861
862 VIA_FINISH_PRIM(vmesa);
863
864 viaCheckDma( vmesa, 1024 ); /* Ensure no wrapping inside this function */
865
866 if (vmesa->newEmitState) {
867 viaEmitState(vmesa);
868 }
869
870
871 regCmdB = vmesa->regCmdB;
872
873 switch (hwprim) {
874 case GL_POINTS:
875 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Point | HC_HVCycle_Full;
876 if (ctx->Light.ShadeModel == GL_FLAT)
877 vmesa->regCmdA_End |= HC_HShading_FlatA;
878 break;
879 case GL_LINES:
880 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Line | HC_HVCycle_Full;
881 if (ctx->Light.ShadeModel == GL_FLAT)
882 vmesa->regCmdA_End |= HC_HShading_FlatB;
883 break;
884 case GL_LINE_LOOP:
885 case GL_LINE_STRIP:
886 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Line | HC_HVCycle_AFP |
887 HC_HVCycle_AB | HC_HVCycle_NewB;
888 regCmdB |= HC_HVCycle_AB | HC_HVCycle_NewB | HC_HLPrst_MASK;
889 if (ctx->Light.ShadeModel == GL_FLAT)
890 vmesa->regCmdA_End |= HC_HShading_FlatB;
891 break;
892 case GL_TRIANGLES:
893 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_Full;
894 if (ctx->Light.ShadeModel == GL_FLAT)
895 vmesa->regCmdA_End |= HC_HShading_FlatC;
896 break;
897 case GL_TRIANGLE_STRIP:
898 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
899 HC_HVCycle_AC | HC_HVCycle_BB | HC_HVCycle_NewC;
900 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
901 if (ctx->Light.ShadeModel == GL_FLAT)
902 vmesa->regCmdA_End |= HC_HShading_FlatB;
903 break;
904 case GL_TRIANGLE_FAN:
905 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
906 HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
907 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
908 if (ctx->Light.ShadeModel == GL_FLAT)
909 vmesa->regCmdA_End |= HC_HShading_FlatC;
910 break;
911 case GL_QUADS:
912 abort();
913 return;
914 case GL_QUAD_STRIP:
915 abort();
916 return;
917 case GL_POLYGON:
918 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
919 HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
920 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
921 if (ctx->Light.ShadeModel == GL_FLAT)
922 vmesa->regCmdA_End |= HC_HShading_FlatC;
923 break;
924 default:
925 abort();
926 return;
927 }
928
929 /* assert((vmesa->dmaLow & 0x4) == 0); */
930
931 if (vmesa->dmaCliprectAddr == ~0) {
932 if (VIA_DEBUG) fprintf(stderr, "reserve cliprect space at %x\n", vmesa->dmaLow);
933 vmesa->dmaCliprectAddr = vmesa->dmaLow;
934 BEGIN_RING(8);
935 OUT_RING( HC_HEADER2 );
936 OUT_RING( (HC_ParaType_NotTex << 16) );
937 OUT_RING( 0xCCCCCCCC );
938 OUT_RING( 0xCCCCCCCC );
939 OUT_RING( 0xCCCCCCCC );
940 OUT_RING( 0xCCCCCCCC );
941 OUT_RING( 0xCCCCCCCC );
942 OUT_RING( 0xCCCCCCCC );
943 ADVANCE_RING();
944 }
945
946 assert(vmesa->dmaLastPrim == 0);
947
948 BEGIN_RING(8);
949 OUT_RING( HC_HEADER2 );
950 OUT_RING( (HC_ParaType_NotTex << 16) );
951 OUT_RING( 0xCCCCCCCC );
952 OUT_RING( 0xDDDDDDDD );
953
954 OUT_RING( HC_HEADER2 );
955 OUT_RING( (HC_ParaType_CmdVdata << 16) );
956 OUT_RING( regCmdB );
957 OUT_RING( vmesa->regCmdA_End );
958 ADVANCE_RING();
959
960
961 vmesa->renderPrimitive = glprim;
962 vmesa->hwPrimitive = hwprim;
963 vmesa->dmaLastPrim = vmesa->dmaLow;
964 }
965
966 /* Callback for mesa:
967 */
968 static void viaRenderPrimitive( GLcontext *ctx, GLuint prim )
969 {
970 viaRasterPrimitive( ctx, prim, hwPrim[prim] );
971 }
972
973
974 void viaFinishPrimitive(viaContextPtr vmesa)
975 {
976 if (VIA_DEBUG)
977 fprintf(stderr, "%s\n", __FUNCTION__);
978
979 if (!vmesa->dmaLastPrim || vmesa->dmaCliprectAddr == ~0) {
980 assert(0);
981 }
982 else if (vmesa->dmaLow != vmesa->dmaLastPrim) {
983 GLuint cmdA = vmesa->regCmdA_End | HC_HPLEND_MASK | HC_HPMValidN_MASK | HC_HE3Fire_MASK;
984 RING_VARS;
985
986 vmesa->dmaLastPrim = 0;
987
988 /* KW: modified 0x1 to 0x4 below:
989 */
990 if ((vmesa->dmaLow & 0x4) || !vmesa->useAgp) {
991 BEGIN_RING_NOCHECK( 1 );
992 OUT_RING( cmdA );
993 ADVANCE_RING();
994 }
995 else {
996 BEGIN_RING_NOCHECK( 2 );
997 OUT_RING( cmdA );
998 OUT_RING( cmdA );
999 ADVANCE_RING();
1000 }
1001
1002 if (vmesa->dmaLow > VIA_DMA_HIGHWATER)
1003 viaFlushDma( vmesa );
1004 }
1005 else {
1006 if (VIA_DEBUG)
1007 fprintf(stderr, "remove empty primitive\n");
1008
1009 /* Remove the primitive header:
1010 */
1011 vmesa->dmaLastPrim = 0;
1012 vmesa->dmaLow -= 8 * sizeof(GLuint);
1013
1014 /* Maybe remove the cliprect as well:
1015 */
1016 if (vmesa->dmaCliprectAddr == vmesa->dmaLow - 8 * sizeof(GLuint)) {
1017 vmesa->dmaLow -= 8 * sizeof(GLuint);
1018 vmesa->dmaCliprectAddr = ~0;
1019 }
1020 }
1021
1022 vmesa->renderPrimitive = GL_POLYGON + 1;
1023 vmesa->hwPrimitive = GL_POLYGON + 1;
1024 vmesa->dmaLastPrim = 0;
1025 }
1026
1027
1028 /**********************************************************************/
1029 /* Transition to/from hardware rasterization. */
1030 /**********************************************************************/
1031
1032
1033 void viaFallback(viaContextPtr vmesa, GLuint bit, GLboolean mode)
1034 {
1035 GLcontext *ctx = vmesa->glCtx;
1036 TNLcontext *tnl = TNL_CONTEXT(ctx);
1037 GLuint oldfallback = vmesa->Fallback;
1038 if (VIA_DEBUG) fprintf(stderr, "%s old %x bit %x mode %d\n", __FUNCTION__,
1039 vmesa->Fallback, bit, mode);
1040
1041 if (mode) {
1042 vmesa->Fallback |= bit;
1043 if (oldfallback == 0) {
1044 VIA_FLUSH_DMA(vmesa);
1045 _swsetup_Wakeup(ctx);
1046 vmesa->renderIndex = ~0;
1047 }
1048 }
1049 else {
1050 vmesa->Fallback &= ~bit;
1051 if (oldfallback == bit) {
1052 _swrast_flush( ctx );
1053
1054 tnl->Driver.Render.Start = viaRenderStart;
1055 tnl->Driver.Render.PrimitiveNotify = viaRenderPrimitive;
1056 tnl->Driver.Render.Finish = viaRenderFinish;
1057
1058 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1059 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1060 tnl->Driver.Render.Interp = _tnl_interp;
1061
1062 _tnl_invalidate_vertex_state( ctx, ~0 );
1063 _tnl_invalidate_vertices( ctx, ~0 );
1064 _tnl_install_attrs( ctx,
1065 vmesa->vertex_attrs,
1066 vmesa->vertex_attr_count,
1067 vmesa->ViewportMatrix.m, 0 );
1068
1069 vmesa->newState |= (_VIA_NEW_RENDERSTATE|_VIA_NEW_VERTEX);
1070 }
1071 }
1072 }
1073
1074
1075 /**********************************************************************/
1076 /* Initialization. */
1077 /**********************************************************************/
1078
1079
1080 void viaInitTriFuncs(GLcontext *ctx)
1081 {
1082 viaContextPtr vmesa = VIA_CONTEXT(ctx);
1083 TNLcontext *tnl = TNL_CONTEXT(ctx);
1084 static int firsttime = 1;
1085
1086 if (firsttime) {
1087 init_rast_tab();
1088 firsttime = 0;
1089 }
1090
1091 tnl->Driver.RunPipeline = _tnl_run_pipeline;
1092 tnl->Driver.Render.Start = viaRenderStart;
1093 tnl->Driver.Render.Finish = viaRenderFinish;
1094 tnl->Driver.Render.PrimitiveNotify = viaRenderPrimitive;
1095 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1096 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1097 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1098 tnl->Driver.Render.Interp = _tnl_interp;
1099
1100 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
1101 (6 + 2*ctx->Const.MaxTextureUnits) * sizeof(GLfloat) );
1102
1103 vmesa->verts = (char *)tnl->clipspace.vertex_buf;
1104
1105 }