First attempt at using private back/z buffers.
[mesa.git] / src / mesa / drivers / dri / i915tex / intel_tris.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "macros.h"
31 #include "enums.h"
32 #include "texobj.h"
33 #include "state.h"
34 #include "dd.h"
35
36 #include "swrast/swrast.h"
37 #include "swrast_setup/swrast_setup.h"
38 #include "tnl/t_context.h"
39 #include "tnl/t_pipeline.h"
40 #include "tnl/t_vertex.h"
41
42 #include "intel_screen.h"
43 #include "intel_context.h"
44 #include "intel_tris.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_buffers.h"
47 #include "intel_reg.h"
48 #include "intel_span.h"
49 #include "intel_tex.h"
50
51 static void intelRenderPrimitive(GLcontext * ctx, GLenum prim);
52 static void intelRasterPrimitive(GLcontext * ctx, GLenum rprim,
53 GLuint hwprim);
54
55 /*
56 */
57 static void
58 intel_flush_inline_primitive(struct intel_context *intel)
59 {
60 GLuint used = intel->batch->ptr - intel->prim.start_ptr;
61
62 assert(intel->prim.primitive != ~0);
63
64 /* _mesa_printf("/\n"); */
65
66 if (used < 8)
67 goto do_discard;
68
69 *(int *) intel->prim.start_ptr = (_3DPRIMITIVE |
70 intel->prim.primitive | (used / 4 - 2));
71
72 goto finished;
73
74 do_discard:
75 intel->batch->ptr -= used;
76
77 finished:
78 intel->prim.primitive = ~0;
79 intel->prim.start_ptr = 0;
80 intel->prim.flush = 0;
81 }
82
83
84 /* Emit a primitive referencing vertices in a vertex buffer.
85 */
86 void
87 intelStartInlinePrimitive(struct intel_context *intel,
88 GLuint prim, GLuint batch_flags)
89 {
90 BATCH_LOCALS;
91
92 intel->vtbl.emit_state(intel);
93
94 /* Need to make sure at the very least that we don't wrap
95 * batchbuffers in BEGIN_BATCH below, otherwise the primitive will
96 * be emitted to a batchbuffer missing the required full-state
97 * preamble.
98 */
99 if (intel_batchbuffer_space(intel->batch) < 100) {
100 intel_batchbuffer_flush(intel->batch);
101 intel->vtbl.emit_state(intel);
102 }
103
104 /* _mesa_printf("%s *", __progname); */
105
106 intel_wait_flips(intel, batch_flags);
107
108 /* Emit a slot which will be filled with the inline primitive
109 * command later.
110 */
111 BEGIN_BATCH(2, batch_flags);
112 OUT_BATCH(0);
113
114 intel->prim.start_ptr = intel->batch->ptr;
115 intel->prim.primitive = prim;
116 intel->prim.flush = intel_flush_inline_primitive;
117
118 OUT_BATCH(0);
119 ADVANCE_BATCH();
120
121 /* _mesa_printf(">"); */
122 }
123
124
125 void
126 intelWrapInlinePrimitive(struct intel_context *intel)
127 {
128 GLuint prim = intel->prim.primitive;
129 GLuint batchflags = intel->batch->flags;
130
131 intel_flush_inline_primitive(intel);
132 intel_batchbuffer_flush(intel->batch);
133 intelStartInlinePrimitive(intel, prim, batchflags); /* ??? */
134 }
135
136 GLuint *
137 intelExtendInlinePrimitive(struct intel_context *intel, GLuint dwords)
138 {
139 GLuint sz = dwords * sizeof(GLuint);
140 GLuint *ptr;
141
142 assert(intel->prim.flush == intel_flush_inline_primitive);
143
144 if (intel_batchbuffer_space(intel->batch) < sz)
145 intelWrapInlinePrimitive(intel);
146
147 /* _mesa_printf("."); */
148
149 intel->vtbl.assert_not_dirty(intel);
150
151 ptr = (GLuint *) intel->batch->ptr;
152 intel->batch->ptr += sz;
153
154 return ptr;
155 }
156
157
158
159 /***********************************************************************
160 * Emit primitives as inline vertices *
161 ***********************************************************************/
162
163 #ifdef __i386__
164 #define COPY_DWORDS( j, vb, vertsize, v ) \
165 do { \
166 int __tmp; \
167 __asm__ __volatile__( "rep ; movsl" \
168 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
169 : "0" (vertsize), \
170 "D" ((long)vb), \
171 "S" ((long)v) ); \
172 } while (0)
173 #else
174 #define COPY_DWORDS( j, vb, vertsize, v ) \
175 do { \
176 for ( j = 0 ; j < vertsize ; j++ ) { \
177 vb[j] = ((GLuint *)v)[j]; \
178 } \
179 vb += vertsize; \
180 } while (0)
181 #endif
182
183 static void
184 intel_draw_quad(struct intel_context *intel,
185 intelVertexPtr v0,
186 intelVertexPtr v1, intelVertexPtr v2, intelVertexPtr v3)
187 {
188 GLuint vertsize = intel->vertex_size;
189 GLuint *vb = intelExtendInlinePrimitive(intel, 6 * vertsize);
190 int j;
191
192 COPY_DWORDS(j, vb, vertsize, v0);
193 COPY_DWORDS(j, vb, vertsize, v1);
194
195 /* If smooth shading, draw like a trifan which gives better
196 * rasterization. Otherwise draw as two triangles with provoking
197 * vertex in third position as required for flat shading.
198 */
199 if (intel->ctx.Light.ShadeModel == GL_FLAT) {
200 COPY_DWORDS(j, vb, vertsize, v3);
201 COPY_DWORDS(j, vb, vertsize, v1);
202 }
203 else {
204 COPY_DWORDS(j, vb, vertsize, v2);
205 COPY_DWORDS(j, vb, vertsize, v0);
206 }
207
208 COPY_DWORDS(j, vb, vertsize, v2);
209 COPY_DWORDS(j, vb, vertsize, v3);
210 }
211
212 static void
213 intel_draw_triangle(struct intel_context *intel,
214 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
215 {
216 GLuint vertsize = intel->vertex_size;
217 GLuint *vb = intelExtendInlinePrimitive(intel, 3 * vertsize);
218 int j;
219
220 COPY_DWORDS(j, vb, vertsize, v0);
221 COPY_DWORDS(j, vb, vertsize, v1);
222 COPY_DWORDS(j, vb, vertsize, v2);
223 }
224
225
226 static void
227 intel_draw_line(struct intel_context *intel,
228 intelVertexPtr v0, intelVertexPtr v1)
229 {
230 GLuint vertsize = intel->vertex_size;
231 GLuint *vb = intelExtendInlinePrimitive(intel, 2 * vertsize);
232 int j;
233
234 COPY_DWORDS(j, vb, vertsize, v0);
235 COPY_DWORDS(j, vb, vertsize, v1);
236 }
237
238
239 static void
240 intel_draw_point(struct intel_context *intel, intelVertexPtr v0)
241 {
242 GLuint vertsize = intel->vertex_size;
243 GLuint *vb = intelExtendInlinePrimitive(intel, vertsize);
244 int j;
245
246 /* Adjust for sub pixel position -- still required for conform. */
247 *(float *) &vb[0] = v0->v.x - 0.125;
248 *(float *) &vb[1] = v0->v.y - 0.125;
249 for (j = 2; j < vertsize; j++)
250 vb[j] = v0->ui[j];
251 }
252
253
254
255 /***********************************************************************
256 * Fixup for ARB_point_parameters *
257 ***********************************************************************/
258
259 /* Currently not working - VERT_ATTRIB_POINTSIZE isn't correctly
260 * represented in the fragment program InputsRead field.
261 */
262 static void
263 intel_atten_point(struct intel_context *intel, intelVertexPtr v0)
264 {
265 GLcontext *ctx = &intel->ctx;
266 GLfloat psz[4], col[4], restore_psz, restore_alpha;
267
268 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
269 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
270
271 restore_psz = psz[0];
272 restore_alpha = col[3];
273
274 if (psz[0] >= ctx->Point.Threshold) {
275 psz[0] = MIN2(psz[0], ctx->Point.MaxSize);
276 }
277 else {
278 GLfloat dsize = psz[0] / ctx->Point.Threshold;
279 psz[0] = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
280 col[3] *= dsize * dsize;
281 }
282
283 if (psz[0] < 1.0)
284 psz[0] = 1.0;
285
286 if (restore_psz != psz[0] || restore_alpha != col[3]) {
287 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
288 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
289
290 intel_draw_point(intel, v0);
291
292 psz[0] = restore_psz;
293 col[3] = restore_alpha;
294
295 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
296 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
297 }
298 else
299 intel_draw_point(intel, v0);
300 }
301
302
303
304
305
306 /***********************************************************************
307 * Fixup for I915 WPOS texture coordinate *
308 ***********************************************************************/
309
310
311
312 static void
313 intel_wpos_triangle(struct intel_context *intel,
314 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
315 {
316 GLuint offset = intel->wpos_offset;
317 GLuint size = intel->wpos_size;
318
319 __memcpy(((char *) v0) + offset, v0, size);
320 __memcpy(((char *) v1) + offset, v1, size);
321 __memcpy(((char *) v2) + offset, v2, size);
322
323 intel_draw_triangle(intel, v0, v1, v2);
324 }
325
326
327 static void
328 intel_wpos_line(struct intel_context *intel,
329 intelVertexPtr v0, intelVertexPtr v1)
330 {
331 GLuint offset = intel->wpos_offset;
332 GLuint size = intel->wpos_size;
333
334 __memcpy(((char *) v0) + offset, v0, size);
335 __memcpy(((char *) v1) + offset, v1, size);
336
337 intel_draw_line(intel, v0, v1);
338 }
339
340
341 static void
342 intel_wpos_point(struct intel_context *intel, intelVertexPtr v0)
343 {
344 GLuint offset = intel->wpos_offset;
345 GLuint size = intel->wpos_size;
346
347 __memcpy(((char *) v0) + offset, v0, size);
348
349 intel_draw_point(intel, v0);
350 }
351
352
353
354
355
356
357 /***********************************************************************
358 * Macros for t_dd_tritmp.h to draw basic primitives *
359 ***********************************************************************/
360
361 #define TRI( a, b, c ) \
362 do { \
363 if (DO_FALLBACK) \
364 intel->draw_tri( intel, a, b, c ); \
365 else \
366 intel_draw_triangle( intel, a, b, c ); \
367 } while (0)
368
369 #define QUAD( a, b, c, d ) \
370 do { \
371 if (DO_FALLBACK) { \
372 intel->draw_tri( intel, a, b, d ); \
373 intel->draw_tri( intel, b, c, d ); \
374 } else \
375 intel_draw_quad( intel, a, b, c, d ); \
376 } while (0)
377
378 #define LINE( v0, v1 ) \
379 do { \
380 if (DO_FALLBACK) \
381 intel->draw_line( intel, v0, v1 ); \
382 else \
383 intel_draw_line( intel, v0, v1 ); \
384 } while (0)
385
386 #define POINT( v0 ) \
387 do { \
388 if (DO_FALLBACK) \
389 intel->draw_point( intel, v0 ); \
390 else \
391 intel_draw_point( intel, v0 ); \
392 } while (0)
393
394
395 /***********************************************************************
396 * Build render functions from dd templates *
397 ***********************************************************************/
398
399 #define INTEL_OFFSET_BIT 0x01
400 #define INTEL_TWOSIDE_BIT 0x02
401 #define INTEL_UNFILLED_BIT 0x04
402 #define INTEL_FALLBACK_BIT 0x08
403 #define INTEL_MAX_TRIFUNC 0x10
404
405
406 static struct
407 {
408 tnl_points_func points;
409 tnl_line_func line;
410 tnl_triangle_func triangle;
411 tnl_quad_func quad;
412 } rast_tab[INTEL_MAX_TRIFUNC];
413
414
415 #define DO_FALLBACK (IND & INTEL_FALLBACK_BIT)
416 #define DO_OFFSET (IND & INTEL_OFFSET_BIT)
417 #define DO_UNFILLED (IND & INTEL_UNFILLED_BIT)
418 #define DO_TWOSIDE (IND & INTEL_TWOSIDE_BIT)
419 #define DO_FLAT 0
420 #define DO_TRI 1
421 #define DO_QUAD 1
422 #define DO_LINE 1
423 #define DO_POINTS 1
424 #define DO_FULL_QUAD 1
425
426 #define HAVE_RGBA 1
427 #define HAVE_SPEC 1
428 #define HAVE_BACK_COLORS 0
429 #define HAVE_HW_FLATSHADE 1
430 #define VERTEX intelVertex
431 #define TAB rast_tab
432
433 /* Only used to pull back colors into vertices (ie, we know color is
434 * floating point).
435 */
436 #define INTEL_COLOR( dst, src ) \
437 do { \
438 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
439 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
440 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
441 UNCLAMPED_FLOAT_TO_UBYTE((dst)[3], (src)[3]); \
442 } while (0)
443
444 #define INTEL_SPEC( dst, src ) \
445 do { \
446 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
447 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
448 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
449 } while (0)
450
451
452 #define DEPTH_SCALE intel->polygon_offset_scale
453 #define UNFILLED_TRI unfilled_tri
454 #define UNFILLED_QUAD unfilled_quad
455 #define VERT_X(_v) _v->v.x
456 #define VERT_Y(_v) _v->v.y
457 #define VERT_Z(_v) _v->v.z
458 #define AREA_IS_CCW( a ) (a > 0)
459 #define GET_VERTEX(e) (intel->verts + (e * intel->vertex_size * sizeof(GLuint)))
460
461 #define VERT_SET_RGBA( v, c ) if (coloroffset) INTEL_COLOR( v->ub4[coloroffset], c )
462 #define VERT_COPY_RGBA( v0, v1 ) if (coloroffset) v0->ui[coloroffset] = v1->ui[coloroffset]
463 #define VERT_SAVE_RGBA( idx ) if (coloroffset) color[idx] = v[idx]->ui[coloroffset]
464 #define VERT_RESTORE_RGBA( idx ) if (coloroffset) v[idx]->ui[coloroffset] = color[idx]
465
466 #define VERT_SET_SPEC( v, c ) if (specoffset) INTEL_SPEC( v->ub4[specoffset], c )
467 #define VERT_COPY_SPEC( v0, v1 ) if (specoffset) COPY_3V(v0->ub4[specoffset], v1->ub4[specoffset])
468 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
469 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
470
471 #define LOCAL_VARS(n) \
472 struct intel_context *intel = intel_context(ctx); \
473 GLuint color[n], spec[n]; \
474 GLuint coloroffset = intel->coloroffset; \
475 GLboolean specoffset = intel->specoffset; \
476 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
477
478
479 /***********************************************************************
480 * Helpers for rendering unfilled primitives *
481 ***********************************************************************/
482
483 static const GLuint hw_prim[GL_POLYGON + 1] = {
484 PRIM3D_POINTLIST,
485 PRIM3D_LINELIST,
486 PRIM3D_LINELIST,
487 PRIM3D_LINELIST,
488 PRIM3D_TRILIST,
489 PRIM3D_TRILIST,
490 PRIM3D_TRILIST,
491 PRIM3D_TRILIST,
492 PRIM3D_TRILIST,
493 PRIM3D_TRILIST
494 };
495
496 #define RASTERIZE(x) intelRasterPrimitive( ctx, x, hw_prim[x] )
497 #define RENDER_PRIMITIVE intel->render_primitive
498 #define TAG(x) x
499 #define IND INTEL_FALLBACK_BIT
500 #include "tnl_dd/t_dd_unfilled.h"
501 #undef IND
502
503 /***********************************************************************
504 * Generate GL render functions *
505 ***********************************************************************/
506
507 #define IND (0)
508 #define TAG(x) x
509 #include "tnl_dd/t_dd_tritmp.h"
510
511 #define IND (INTEL_OFFSET_BIT)
512 #define TAG(x) x##_offset
513 #include "tnl_dd/t_dd_tritmp.h"
514
515 #define IND (INTEL_TWOSIDE_BIT)
516 #define TAG(x) x##_twoside
517 #include "tnl_dd/t_dd_tritmp.h"
518
519 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT)
520 #define TAG(x) x##_twoside_offset
521 #include "tnl_dd/t_dd_tritmp.h"
522
523 #define IND (INTEL_UNFILLED_BIT)
524 #define TAG(x) x##_unfilled
525 #include "tnl_dd/t_dd_tritmp.h"
526
527 #define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
528 #define TAG(x) x##_offset_unfilled
529 #include "tnl_dd/t_dd_tritmp.h"
530
531 #define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT)
532 #define TAG(x) x##_twoside_unfilled
533 #include "tnl_dd/t_dd_tritmp.h"
534
535 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
536 #define TAG(x) x##_twoside_offset_unfilled
537 #include "tnl_dd/t_dd_tritmp.h"
538
539 #define IND (INTEL_FALLBACK_BIT)
540 #define TAG(x) x##_fallback
541 #include "tnl_dd/t_dd_tritmp.h"
542
543 #define IND (INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
544 #define TAG(x) x##_offset_fallback
545 #include "tnl_dd/t_dd_tritmp.h"
546
547 #define IND (INTEL_TWOSIDE_BIT|INTEL_FALLBACK_BIT)
548 #define TAG(x) x##_twoside_fallback
549 #include "tnl_dd/t_dd_tritmp.h"
550
551 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
552 #define TAG(x) x##_twoside_offset_fallback
553 #include "tnl_dd/t_dd_tritmp.h"
554
555 #define IND (INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
556 #define TAG(x) x##_unfilled_fallback
557 #include "tnl_dd/t_dd_tritmp.h"
558
559 #define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
560 #define TAG(x) x##_offset_unfilled_fallback
561 #include "tnl_dd/t_dd_tritmp.h"
562
563 #define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
564 #define TAG(x) x##_twoside_unfilled_fallback
565 #include "tnl_dd/t_dd_tritmp.h"
566
567 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT| \
568 INTEL_FALLBACK_BIT)
569 #define TAG(x) x##_twoside_offset_unfilled_fallback
570 #include "tnl_dd/t_dd_tritmp.h"
571
572
573 static void
574 init_rast_tab(void)
575 {
576 init();
577 init_offset();
578 init_twoside();
579 init_twoside_offset();
580 init_unfilled();
581 init_offset_unfilled();
582 init_twoside_unfilled();
583 init_twoside_offset_unfilled();
584 init_fallback();
585 init_offset_fallback();
586 init_twoside_fallback();
587 init_twoside_offset_fallback();
588 init_unfilled_fallback();
589 init_offset_unfilled_fallback();
590 init_twoside_unfilled_fallback();
591 init_twoside_offset_unfilled_fallback();
592 }
593
594
595 /***********************************************************************
596 * Rasterization fallback helpers *
597 ***********************************************************************/
598
599
600 /* This code is hit only when a mix of accelerated and unaccelerated
601 * primitives are being drawn, and only for the unaccelerated
602 * primitives.
603 */
604 static void
605 intel_fallback_tri(struct intel_context *intel,
606 intelVertex * v0, intelVertex * v1, intelVertex * v2)
607 {
608 GLcontext *ctx = &intel->ctx;
609 SWvertex v[3];
610
611 if (0)
612 fprintf(stderr, "\n%s\n", __FUNCTION__);
613
614 INTEL_FIREVERTICES(intel);
615
616 _swsetup_Translate(ctx, v0, &v[0]);
617 _swsetup_Translate(ctx, v1, &v[1]);
618 _swsetup_Translate(ctx, v2, &v[2]);
619 intelSpanRenderStart(ctx);
620 _swrast_Triangle(ctx, &v[0], &v[1], &v[2]);
621 intelSpanRenderFinish(ctx);
622 }
623
624
625 static void
626 intel_fallback_line(struct intel_context *intel,
627 intelVertex * v0, intelVertex * v1)
628 {
629 GLcontext *ctx = &intel->ctx;
630 SWvertex v[2];
631
632 if (0)
633 fprintf(stderr, "\n%s\n", __FUNCTION__);
634
635 INTEL_FIREVERTICES(intel);
636
637 _swsetup_Translate(ctx, v0, &v[0]);
638 _swsetup_Translate(ctx, v1, &v[1]);
639 intelSpanRenderStart(ctx);
640 _swrast_Line(ctx, &v[0], &v[1]);
641 intelSpanRenderFinish(ctx);
642 }
643
644 static void
645 intel_fallback_point(struct intel_context *intel,
646 intelVertex * v0)
647 {
648 GLcontext *ctx = &intel->ctx;
649 SWvertex v[1];
650
651 if (0)
652 fprintf(stderr, "\n%s\n", __FUNCTION__);
653
654 INTEL_FIREVERTICES(intel);
655
656 _swsetup_Translate(ctx, v0, &v[0]);
657 intelSpanRenderStart(ctx);
658 _swrast_Point(ctx, &v[0]);
659 intelSpanRenderFinish(ctx);
660 }
661
662
663 /**********************************************************************/
664 /* Render unclipped begin/end objects */
665 /**********************************************************************/
666
667 #define IND 0
668 #define V(x) (intelVertex *)(vertptr + ((x)*vertsize*sizeof(GLuint)))
669 #define RENDER_POINTS( start, count ) \
670 for ( ; start < count ; start++) POINT( V(ELT(start)) );
671 #define RENDER_LINE( v0, v1 ) LINE( V(v0), V(v1) )
672 #define RENDER_TRI( v0, v1, v2 ) TRI( V(v0), V(v1), V(v2) )
673 #define RENDER_QUAD( v0, v1, v2, v3 ) QUAD( V(v0), V(v1), V(v2), V(v3) )
674 #define INIT(x) intelRenderPrimitive( ctx, x )
675 #undef LOCAL_VARS
676 #define LOCAL_VARS \
677 struct intel_context *intel = intel_context(ctx); \
678 GLubyte *vertptr = (GLubyte *)intel->verts; \
679 const GLuint vertsize = intel->vertex_size; \
680 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
681 (void) elt;
682 #define RESET_STIPPLE
683 #define RESET_OCCLUSION
684 #define PRESERVE_VB_DEFS
685 #define ELT(x) x
686 #define TAG(x) intel_##x##_verts
687 #include "tnl/t_vb_rendertmp.h"
688 #undef ELT
689 #undef TAG
690 #define TAG(x) intel_##x##_elts
691 #define ELT(x) elt[x]
692 #include "tnl/t_vb_rendertmp.h"
693
694 /**********************************************************************/
695 /* Render clipped primitives */
696 /**********************************************************************/
697
698
699
700 static void
701 intelRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
702 {
703 struct intel_context *intel = intel_context(ctx);
704 TNLcontext *tnl = TNL_CONTEXT(ctx);
705 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
706 GLuint prim = intel->render_primitive;
707
708 /* Render the new vertices as an unclipped polygon.
709 */
710 {
711 GLuint *tmp = VB->Elts;
712 VB->Elts = (GLuint *) elts;
713 tnl->Driver.Render.PrimTabElts[GL_POLYGON] (ctx, 0, n,
714 PRIM_BEGIN | PRIM_END);
715 VB->Elts = tmp;
716 }
717
718 /* Restore the render primitive
719 */
720 if (prim != GL_POLYGON)
721 tnl->Driver.Render.PrimitiveNotify(ctx, prim);
722 }
723
724 static void
725 intelRenderClippedLine(GLcontext * ctx, GLuint ii, GLuint jj)
726 {
727 TNLcontext *tnl = TNL_CONTEXT(ctx);
728
729 tnl->Driver.Render.Line(ctx, ii, jj);
730 }
731
732 static void
733 intelFastRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
734 {
735 struct intel_context *intel = intel_context(ctx);
736 const GLuint vertsize = intel->vertex_size;
737 GLuint *vb = intelExtendInlinePrimitive(intel, (n - 2) * 3 * vertsize);
738 GLubyte *vertptr = (GLubyte *) intel->verts;
739 const GLuint *start = (const GLuint *) V(elts[0]);
740 int i, j;
741
742 for (i = 2; i < n; i++) {
743 COPY_DWORDS(j, vb, vertsize, V(elts[i - 1]));
744 COPY_DWORDS(j, vb, vertsize, V(elts[i]));
745 COPY_DWORDS(j, vb, vertsize, start);
746 }
747 }
748
749 /**********************************************************************/
750 /* Choose render functions */
751 /**********************************************************************/
752
753
754
755
756 #define ANY_FALLBACK_FLAGS (DD_LINE_STIPPLE | DD_TRI_STIPPLE | DD_POINT_ATTEN | DD_POINT_SMOOTH | DD_TRI_SMOOTH)
757 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE | DD_TRI_OFFSET | DD_TRI_UNFILLED)
758
759 void
760 intelChooseRenderState(GLcontext * ctx)
761 {
762 TNLcontext *tnl = TNL_CONTEXT(ctx);
763 struct intel_context *intel = intel_context(ctx);
764 GLuint flags = ctx->_TriangleCaps;
765 const struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
766 GLboolean have_wpos = (fprog && (fprog->Base.InputsRead & FRAG_BIT_WPOS));
767 GLuint index = 0;
768
769 if (INTEL_DEBUG & DEBUG_STATE)
770 fprintf(stderr, "\n%s\n", __FUNCTION__);
771
772 if ((flags & (ANY_FALLBACK_FLAGS | ANY_RASTER_FLAGS)) || have_wpos) {
773
774 if (flags & ANY_RASTER_FLAGS) {
775 if (flags & DD_TRI_LIGHT_TWOSIDE)
776 index |= INTEL_TWOSIDE_BIT;
777 if (flags & DD_TRI_OFFSET)
778 index |= INTEL_OFFSET_BIT;
779 if (flags & DD_TRI_UNFILLED)
780 index |= INTEL_UNFILLED_BIT;
781 }
782
783 if (have_wpos) {
784 intel->draw_point = intel_wpos_point;
785 intel->draw_line = intel_wpos_line;
786 intel->draw_tri = intel_wpos_triangle;
787
788 /* Make sure these get called:
789 */
790 index |= INTEL_FALLBACK_BIT;
791 }
792 else {
793 intel->draw_point = intel_draw_point;
794 intel->draw_line = intel_draw_line;
795 intel->draw_tri = intel_draw_triangle;
796 }
797
798 /* Hook in fallbacks for specific primitives.
799 */
800 if (flags & ANY_FALLBACK_FLAGS) {
801 if (flags & DD_LINE_STIPPLE)
802 intel->draw_line = intel_fallback_line;
803
804 if ((flags & DD_TRI_STIPPLE) && !intel->hw_stipple)
805 intel->draw_tri = intel_fallback_tri;
806
807 if (flags & DD_TRI_SMOOTH) {
808 if (intel->strict_conformance)
809 intel->draw_tri = intel_fallback_tri;
810 }
811
812 if (flags & DD_POINT_ATTEN) {
813 if (0)
814 intel->draw_point = intel_atten_point;
815 else
816 intel->draw_point = intel_fallback_point;
817 }
818
819 if (flags & DD_POINT_SMOOTH) {
820 if (intel->strict_conformance)
821 intel->draw_point = intel_fallback_point;
822 }
823
824 index |= INTEL_FALLBACK_BIT;
825 }
826 }
827
828 if (intel->RenderIndex != index) {
829 intel->RenderIndex = index;
830
831 tnl->Driver.Render.Points = rast_tab[index].points;
832 tnl->Driver.Render.Line = rast_tab[index].line;
833 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
834 tnl->Driver.Render.Quad = rast_tab[index].quad;
835
836 if (index == 0) {
837 tnl->Driver.Render.PrimTabVerts = intel_render_tab_verts;
838 tnl->Driver.Render.PrimTabElts = intel_render_tab_elts;
839 tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
840 tnl->Driver.Render.ClippedPolygon = intelFastRenderClippedPoly;
841 }
842 else {
843 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
844 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
845 tnl->Driver.Render.ClippedLine = intelRenderClippedLine;
846 tnl->Driver.Render.ClippedPolygon = intelRenderClippedPoly;
847 }
848 }
849 }
850
851 static const GLenum reduced_prim[GL_POLYGON + 1] = {
852 GL_POINTS,
853 GL_LINES,
854 GL_LINES,
855 GL_LINES,
856 GL_TRIANGLES,
857 GL_TRIANGLES,
858 GL_TRIANGLES,
859 GL_TRIANGLES,
860 GL_TRIANGLES,
861 GL_TRIANGLES
862 };
863
864
865 /**********************************************************************/
866 /* High level hooks for t_vb_render.c */
867 /**********************************************************************/
868
869
870
871
872 static void
873 intelRunPipeline(GLcontext * ctx)
874 {
875 struct intel_context *intel = intel_context(ctx);
876
877 _mesa_lock_context_textures(ctx);
878
879 if (ctx->NewState)
880 _mesa_update_state_locked(ctx);
881
882 if (intel->NewGLState) {
883 if (intel->NewGLState & _NEW_TEXTURE) {
884 intel->vtbl.update_texture_state(intel);
885 }
886
887 if (!intel->Fallback) {
888 if (intel->NewGLState & _INTEL_NEW_RENDERSTATE)
889 intelChooseRenderState(ctx);
890 }
891
892 intel->NewGLState = 0;
893 }
894
895 _tnl_run_pipeline(ctx);
896
897 _mesa_unlock_context_textures(ctx);
898 }
899
900 static void
901 intelRenderStart(GLcontext * ctx)
902 {
903 struct intel_context *intel = intel_context(ctx);
904
905 intel->vtbl.render_start(intel_context(ctx));
906 intel->vtbl.emit_state(intel);
907 }
908
909 static void
910 intelRenderFinish(GLcontext * ctx)
911 {
912 struct intel_context *intel = intel_context(ctx);
913
914 if (intel->RenderIndex & INTEL_FALLBACK_BIT)
915 _swrast_flush(ctx);
916
917 INTEL_FIREVERTICES(intel);
918 }
919
920
921
922
923 /* System to flush dma and emit state changes based on the rasterized
924 * primitive.
925 */
926 static void
927 intelRasterPrimitive(GLcontext * ctx, GLenum rprim, GLuint hwprim)
928 {
929 struct intel_context *intel = intel_context(ctx);
930
931 if (0)
932 fprintf(stderr, "%s %s %x\n", __FUNCTION__,
933 _mesa_lookup_enum_by_nr(rprim), hwprim);
934
935 intel->vtbl.reduced_primitive_state(intel, rprim);
936
937 /* Start a new primitive. Arrange to have it flushed later on.
938 */
939 if (hwprim != intel->prim.primitive) {
940 INTEL_FIREVERTICES(intel);
941
942 intelStartInlinePrimitive(intel, hwprim, INTEL_BATCH_CLIPRECTS);
943 }
944 }
945
946
947 /*
948 */
949 static void
950 intelRenderPrimitive(GLcontext * ctx, GLenum prim)
951 {
952 struct intel_context *intel = intel_context(ctx);
953
954 if (0)
955 fprintf(stderr, "%s %s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(prim));
956
957 /* Let some clipping routines know which primitive they're dealing
958 * with.
959 */
960 intel->render_primitive = prim;
961
962 /* Shortcircuit this when called from t_dd_rendertmp.h for unfilled
963 * triangles. The rasterized primitive will always be reset by
964 * lower level functions in that case, potentially pingponging the
965 * state:
966 */
967 if (reduced_prim[prim] == GL_TRIANGLES &&
968 (ctx->_TriangleCaps & DD_TRI_UNFILLED))
969 return;
970
971 /* Set some primitive-dependent state and Start? a new primitive.
972 */
973 intelRasterPrimitive(ctx, reduced_prim[prim], hw_prim[prim]);
974 }
975
976
977 /**********************************************************************/
978 /* Transition to/from hardware rasterization. */
979 /**********************************************************************/
980
981 static char *fallbackStrings[] = {
982 [0] = "Draw buffer",
983 [1] = "Read buffer",
984 [2] = "Depth buffer",
985 [3] = "Stencil buffer",
986 [4] = "User disable",
987 [5] = "Render mode",
988
989 [12] = "Texture",
990 [13] = "Color mask",
991 [14] = "Stencil",
992 [15] = "Stipple",
993 [16] = "Program",
994 [17] = "Logic op",
995 [18] = "Smooth polygon",
996 [19] = "Smooth point",
997 };
998
999
1000 static char *
1001 getFallbackString(GLuint bit)
1002 {
1003 int i = 0;
1004 while (bit > 1) {
1005 i++;
1006 bit >>= 1;
1007 }
1008 return fallbackStrings[i];
1009 }
1010
1011
1012
1013 void
1014 intelFallback(struct intel_context *intel, GLuint bit, GLboolean mode)
1015 {
1016 GLcontext *ctx = &intel->ctx;
1017 TNLcontext *tnl = TNL_CONTEXT(ctx);
1018 GLuint oldfallback = intel->Fallback;
1019
1020 if (mode) {
1021 intel->Fallback |= bit;
1022 if (oldfallback == 0) {
1023 intelFlush(ctx);
1024 if (INTEL_DEBUG & DEBUG_FALLBACKS)
1025 fprintf(stderr, "ENTER FALLBACK %x: %s\n",
1026 bit, getFallbackString(bit));
1027 _swsetup_Wakeup(ctx);
1028 intel->RenderIndex = ~0;
1029 }
1030 }
1031 else {
1032 intel->Fallback &= ~bit;
1033 if (oldfallback == bit) {
1034 _swrast_flush(ctx);
1035 if (INTEL_DEBUG & DEBUG_FALLBACKS)
1036 fprintf(stderr, "LEAVE FALLBACK %s\n", getFallbackString(bit));
1037 tnl->Driver.Render.Start = intelRenderStart;
1038 tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1039 tnl->Driver.Render.Finish = intelRenderFinish;
1040 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1041 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1042 tnl->Driver.Render.Interp = _tnl_interp;
1043
1044 _tnl_invalidate_vertex_state(ctx, ~0);
1045 _tnl_invalidate_vertices(ctx, ~0);
1046 _tnl_install_attrs(ctx,
1047 intel->vertex_attrs,
1048 intel->vertex_attr_count,
1049 intel->ViewportMatrix.m, 0);
1050
1051 intel->NewGLState |= _INTEL_NEW_RENDERSTATE;
1052 }
1053 }
1054 }
1055
1056 union fi
1057 {
1058 GLfloat f;
1059 GLint i;
1060 };
1061
1062
1063 /**********************************************************************/
1064 /* Used only with the metaops callbacks. */
1065 /**********************************************************************/
1066 void
1067 intel_meta_draw_poly(struct intel_context *intel,
1068 GLuint n,
1069 GLfloat xy[][2],
1070 GLfloat z, GLuint color, GLfloat tex[][2])
1071 {
1072 union fi *vb;
1073 GLint i;
1074
1075 /* All 3d primitives should be emitted with INTEL_BATCH_CLIPRECTS,
1076 * otherwise the drawing origin (DR4) might not be set correctly.
1077 */
1078 intelStartInlinePrimitive(intel, PRIM3D_TRIFAN, INTEL_BATCH_CLIPRECTS);
1079 vb = (union fi *) intelExtendInlinePrimitive(intel, n * 6);
1080
1081 for (i = 0; i < n; i++) {
1082 vb[0].f = xy[i][0];
1083 vb[1].f = xy[i][1];
1084 vb[2].f = z;
1085 vb[3].i = color;
1086 vb[4].f = tex[i][0];
1087 vb[5].f = tex[i][1];
1088 vb += 6;
1089 }
1090
1091 INTEL_FIREVERTICES(intel);
1092 }
1093
1094 void
1095 intel_meta_draw_quad(struct intel_context *intel,
1096 GLfloat x0, GLfloat x1,
1097 GLfloat y0, GLfloat y1,
1098 GLfloat z,
1099 GLuint color,
1100 GLfloat s0, GLfloat s1, GLfloat t0, GLfloat t1)
1101 {
1102 GLfloat xy[4][2];
1103 GLfloat tex[4][2];
1104
1105 xy[0][0] = x0;
1106 xy[0][1] = y0;
1107 xy[1][0] = x1;
1108 xy[1][1] = y0;
1109 xy[2][0] = x1;
1110 xy[2][1] = y1;
1111 xy[3][0] = x0;
1112 xy[3][1] = y1;
1113
1114 tex[0][0] = s0;
1115 tex[0][1] = t0;
1116 tex[1][0] = s1;
1117 tex[1][1] = t0;
1118 tex[2][0] = s1;
1119 tex[2][1] = t1;
1120 tex[3][0] = s0;
1121 tex[3][1] = t1;
1122
1123 intel_meta_draw_poly(intel, 4, xy, z, color, tex);
1124 }
1125
1126
1127
1128 /**********************************************************************/
1129 /* Initialization. */
1130 /**********************************************************************/
1131
1132
1133 void
1134 intelInitTriFuncs(GLcontext * ctx)
1135 {
1136 TNLcontext *tnl = TNL_CONTEXT(ctx);
1137 static int firsttime = 1;
1138
1139 if (firsttime) {
1140 init_rast_tab();
1141 firsttime = 0;
1142 }
1143
1144 tnl->Driver.RunPipeline = intelRunPipeline;
1145 tnl->Driver.Render.Start = intelRenderStart;
1146 tnl->Driver.Render.Finish = intelRenderFinish;
1147 tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1148 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1149 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1150 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1151 tnl->Driver.Render.Interp = _tnl_interp;
1152 }