intel_tris.c revision c641703f21c858464ca34a1f99c79ccfd9a85d70
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/** @file intel_tris.c
29 *
30 * This file contains functions for managing the vertex buffer and emitting
31 * primitives into it.
32 */
33
34#include "main/glheader.h"
35#include "main/context.h"
36#include "main/macros.h"
37#include "main/enums.h"
38#include "main/texobj.h"
39#include "main/state.h"
40#include "main/dd.h"
41
42#include "swrast/swrast.h"
43#include "swrast_setup/swrast_setup.h"
44#include "tnl/t_context.h"
45#include "tnl/t_pipeline.h"
46#include "tnl/t_vertex.h"
47
48#include "intel_screen.h"
49#include "intel_context.h"
50#include "intel_tris.h"
51#include "intel_batchbuffer.h"
52#include "intel_buffers.h"
53#include "intel_reg.h"
54#include "intel_span.h"
55#include "i830_context.h"
56#include "i830_reg.h"
57
58static void intelRenderPrimitive(GLcontext * ctx, GLenum prim);
59static void intelRasterPrimitive(GLcontext * ctx, GLenum rprim,
60                                 GLuint hwprim);
61
62static void
63intel_flush_inline_primitive(struct intel_context *intel)
64{
65   GLuint used = intel->batch->ptr - intel->prim.start_ptr;
66
67   assert(intel->prim.primitive != ~0);
68
69/*    _mesa_printf("/\n"); */
70
71   if (used < 8)
72      goto do_discard;
73
74   *(int *) intel->prim.start_ptr = (_3DPRIMITIVE |
75                                     intel->prim.primitive | (used / 4 - 2));
76
77   goto finished;
78
79 do_discard:
80   intel->batch->ptr -= used;
81
82 finished:
83   intel->prim.primitive = ~0;
84   intel->prim.start_ptr = 0;
85   intel->prim.flush = 0;
86}
87
88static void intel_start_inline(struct intel_context *intel, uint32_t prim)
89{
90   BATCH_LOCALS;
91
92   intel->vtbl.emit_state(intel);
93
94   intel->no_batch_wrap = GL_TRUE;
95
96   /*_mesa_printf("%s *", __progname);*/
97
98   /* Emit a slot which will be filled with the inline primitive
99    * command later.
100    */
101   BEGIN_BATCH(2);
102   OUT_BATCH(0);
103
104   assert((intel->batch->dirty_state & (1<<1)) == 0);
105
106   intel->prim.start_ptr = intel->batch->ptr;
107   intel->prim.primitive = prim;
108   intel->prim.flush = intel_flush_inline_primitive;
109
110   OUT_BATCH(0);
111   ADVANCE_BATCH();
112
113   intel->no_batch_wrap = GL_FALSE;
114/*    _mesa_printf(">"); */
115}
116
117static void intel_wrap_inline(struct intel_context *intel)
118{
119   GLuint prim = intel->prim.primitive;
120
121   intel_flush_inline_primitive(intel);
122   intel_batchbuffer_flush(intel->batch);
123   intel_start_inline(intel, prim);  /* ??? */
124}
125
126static GLuint *intel_extend_inline(struct intel_context *intel, GLuint dwords)
127{
128   GLuint sz = dwords * sizeof(GLuint);
129   GLuint *ptr;
130
131   assert(intel->prim.flush == intel_flush_inline_primitive);
132
133   if (intel_batchbuffer_space(intel->batch) < sz)
134      intel_wrap_inline(intel);
135
136/*    _mesa_printf("."); */
137
138   intel->vtbl.assert_not_dirty(intel);
139
140   ptr = (GLuint *) intel->batch->ptr;
141   intel->batch->ptr += sz;
142
143   return ptr;
144}
145
146/** Sets the primitive type for a primitive sequence, flushing as needed. */
147void intel_set_prim(struct intel_context *intel, uint32_t prim)
148{
149   /* if we have no VBOs */
150
151   if (intel->intelScreen->no_vbo) {
152      intel_start_inline(intel, prim);
153      return;
154   }
155   if (prim != intel->prim.primitive) {
156      INTEL_FIREVERTICES(intel);
157      intel->prim.primitive = prim;
158   }
159}
160
161/** Returns mapped VB space for the given number of vertices */
162uint32_t *intel_get_prim_space(struct intel_context *intel, unsigned int count)
163{
164   uint32_t *addr;
165
166   if (intel->intelScreen->no_vbo) {
167      return intel_extend_inline(intel, count * intel->vertex_size);
168   }
169
170   /* Check for space in the existing VB */
171   if (intel->prim.vb_bo == NULL ||
172       (intel->prim.current_offset +
173	count * intel->vertex_size * 4) > INTEL_VB_SIZE ||
174       (intel->prim.count + count) >= (1 << 16)) {
175      /* Flush existing prim if any */
176      INTEL_FIREVERTICES(intel);
177
178      intel_finish_vb(intel);
179
180      /* Start a new VB */
181      if (intel->prim.vb == NULL)
182	 intel->prim.vb = malloc(INTEL_VB_SIZE);
183      intel->prim.vb_bo = dri_bo_alloc(intel->bufmgr, "vb",
184				       INTEL_VB_SIZE, 4);
185      intel->prim.start_offset = 0;
186      intel->prim.current_offset = 0;
187   }
188
189   intel->prim.flush = intel_flush_prim;
190
191   addr = (uint32_t *)(intel->prim.vb + intel->prim.current_offset);
192   intel->prim.current_offset += intel->vertex_size * 4 * count;
193   intel->prim.count += count;
194
195   return addr;
196}
197
198/** Dispatches the accumulated primitive to the batchbuffer. */
199void intel_flush_prim(struct intel_context *intel)
200{
201   dri_bo *aper_array[2];
202   dri_bo *vb_bo;
203   unsigned int offset, count;
204   BATCH_LOCALS;
205
206   /* Must be called after an intel_start_prim. */
207   assert(intel->prim.primitive != ~0);
208
209   if (intel->prim.count == 0)
210      return;
211
212   /* Clear the current prims out of the context state so that a batch flush
213    * flush triggered by emit_state doesn't loop back to flush_prim again.
214    */
215   vb_bo = intel->prim.vb_bo;
216   dri_bo_reference(vb_bo);
217   count = intel->prim.count;
218   intel->prim.count = 0;
219   offset = intel->prim.start_offset;
220   intel->prim.start_offset = intel->prim.current_offset;
221   if (!intel->gen >= 3)
222      intel->prim.start_offset = ALIGN(intel->prim.start_offset, 128);
223   intel->prim.flush = NULL;
224
225   intel->vtbl.emit_state(intel);
226
227   aper_array[0] = intel->batch->buf;
228   aper_array[1] = vb_bo;
229   if (dri_bufmgr_check_aperture_space(aper_array, 2)) {
230      intel_batchbuffer_flush(intel->batch);
231      intel->vtbl.emit_state(intel);
232   }
233
234   /* Ensure that we don't start a new batch for the following emit, which
235    * depends on the state just emitted. emit_state should be making sure we
236    * have the space for this.
237    */
238   intel->no_batch_wrap = GL_TRUE;
239
240   /* Check that we actually emitted the state into this batch, using the
241    * UPLOAD_CTX bit as the signal.
242    */
243   assert((intel->batch->dirty_state & (1<<1)) == 0);
244
245#if 0
246   printf("emitting %d..%d=%d vertices size %d\n", offset,
247	  intel->prim.current_offset, count,
248	  intel->vertex_size * 4);
249#endif
250
251   if (intel->gen >= 3) {
252      BEGIN_BATCH(5);
253      OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
254		I1_LOAD_S(0) | I1_LOAD_S(1) | 1);
255      assert((offset & !S0_VB_OFFSET_MASK) == 0);
256      OUT_RELOC(vb_bo, I915_GEM_DOMAIN_VERTEX, 0, offset);
257      OUT_BATCH((intel->vertex_size << S1_VERTEX_WIDTH_SHIFT) |
258		(intel->vertex_size << S1_VERTEX_PITCH_SHIFT));
259
260      OUT_BATCH(_3DPRIMITIVE |
261		PRIM_INDIRECT |
262		PRIM_INDIRECT_SEQUENTIAL |
263		intel->prim.primitive |
264		count);
265      OUT_BATCH(0); /* Beginning vertex index */
266      ADVANCE_BATCH();
267   } else {
268      struct i830_context *i830 = i830_context(&intel->ctx);
269
270      BEGIN_BATCH(5);
271      OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
272		I1_LOAD_S(0) | I1_LOAD_S(2) | 1);
273      /* S0 */
274      assert((offset & !S0_VB_OFFSET_MASK_830) == 0);
275      OUT_RELOC(vb_bo, I915_GEM_DOMAIN_VERTEX, 0,
276		offset | (intel->vertex_size << S0_VB_PITCH_SHIFT_830) |
277		S0_VB_ENABLE_830);
278      /* S2
279       * This is somewhat unfortunate -- VB width is tied up with
280       * vertex format data that we've already uploaded through
281       * _3DSTATE_VFT[01]_CMD.  We may want to replace emits of VFT state with
282       * STATE_IMMEDIATE_1 like this to avoid duplication.
283       */
284      OUT_BATCH((i830->state.Ctx[I830_CTXREG_VF] & VFT0_TEX_COUNT_MASK) >>
285		VFT0_TEX_COUNT_SHIFT << S2_TEX_COUNT_SHIFT_830 |
286		(i830->state.Ctx[I830_CTXREG_VF2] << 16) |
287		intel->vertex_size << S2_VERTEX_0_WIDTH_SHIFT_830);
288
289      OUT_BATCH(_3DPRIMITIVE |
290		PRIM_INDIRECT |
291		PRIM_INDIRECT_SEQUENTIAL |
292		intel->prim.primitive |
293		count);
294      OUT_BATCH(0); /* Beginning vertex index */
295      ADVANCE_BATCH();
296   }
297
298   intel->no_batch_wrap = GL_FALSE;
299
300   dri_bo_unreference(vb_bo);
301}
302
303/**
304 * Uploads the locally-accumulated VB into the buffer object.
305 *
306 * This avoids us thrashing the cachelines in and out as the buffer gets
307 * filled, dispatched, then reused as the hardware completes rendering from it,
308 * and also lets us clflush less if we dispatch with a partially-filled VB.
309 *
310 * This is called normally from get_space when we're finishing a BO, but also
311 * at batch flush time so that we don't try accessing the contents of a
312 * just-dispatched buffer.
313 */
314void intel_finish_vb(struct intel_context *intel)
315{
316   if (intel->prim.vb_bo == NULL)
317      return;
318
319   dri_bo_subdata(intel->prim.vb_bo, 0, intel->prim.start_offset,
320		  intel->prim.vb);
321   dri_bo_unreference(intel->prim.vb_bo);
322   intel->prim.vb_bo = NULL;
323}
324
325/***********************************************************************
326 *                    Emit primitives as inline vertices               *
327 ***********************************************************************/
328
329#ifdef __i386__
330#define COPY_DWORDS( j, vb, vertsize, v )			\
331do {								\
332   int __tmp;							\
333   __asm__ __volatile__( "rep ; movsl"				\
334			 : "=%c" (j), "=D" (vb), "=S" (__tmp)	\
335			 : "0" (vertsize),			\
336			 "D" ((long)vb),			\
337			 "S" ((long)v) );			\
338} while (0)
339#else
340#define COPY_DWORDS( j, vb, vertsize, v )	\
341do {						\
342   for ( j = 0 ; j < vertsize ; j++ ) {		\
343      vb[j] = ((GLuint *)v)[j];			\
344   }						\
345   vb += vertsize;				\
346} while (0)
347#endif
348
349static void
350intel_draw_quad(struct intel_context *intel,
351                intelVertexPtr v0,
352                intelVertexPtr v1, intelVertexPtr v2, intelVertexPtr v3)
353{
354   GLuint vertsize = intel->vertex_size;
355   GLuint *vb = intel_get_prim_space(intel, 6);
356   int j;
357
358   COPY_DWORDS(j, vb, vertsize, v0);
359   COPY_DWORDS(j, vb, vertsize, v1);
360
361   /* If smooth shading, draw like a trifan which gives better
362    * rasterization.  Otherwise draw as two triangles with provoking
363    * vertex in third position as required for flat shading.
364    */
365   if (intel->ctx.Light.ShadeModel == GL_FLAT) {
366      COPY_DWORDS(j, vb, vertsize, v3);
367      COPY_DWORDS(j, vb, vertsize, v1);
368   }
369   else {
370      COPY_DWORDS(j, vb, vertsize, v2);
371      COPY_DWORDS(j, vb, vertsize, v0);
372   }
373
374   COPY_DWORDS(j, vb, vertsize, v2);
375   COPY_DWORDS(j, vb, vertsize, v3);
376}
377
378static void
379intel_draw_triangle(struct intel_context *intel,
380                    intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
381{
382   GLuint vertsize = intel->vertex_size;
383   GLuint *vb = intel_get_prim_space(intel, 3);
384   int j;
385
386   COPY_DWORDS(j, vb, vertsize, v0);
387   COPY_DWORDS(j, vb, vertsize, v1);
388   COPY_DWORDS(j, vb, vertsize, v2);
389}
390
391
392static void
393intel_draw_line(struct intel_context *intel,
394                intelVertexPtr v0, intelVertexPtr v1)
395{
396   GLuint vertsize = intel->vertex_size;
397   GLuint *vb = intel_get_prim_space(intel, 2);
398   int j;
399
400   COPY_DWORDS(j, vb, vertsize, v0);
401   COPY_DWORDS(j, vb, vertsize, v1);
402}
403
404
405static void
406intel_draw_point(struct intel_context *intel, intelVertexPtr v0)
407{
408   GLuint vertsize = intel->vertex_size;
409   GLuint *vb = intel_get_prim_space(intel, 1);
410   int j;
411
412   /* Adjust for sub pixel position -- still required for conform. */
413   *(float *) &vb[0] = v0->v.x;
414   *(float *) &vb[1] = v0->v.y;
415   for (j = 2; j < vertsize; j++)
416      vb[j] = v0->ui[j];
417}
418
419
420
421/***********************************************************************
422 *                Fixup for ARB_point_parameters                       *
423 ***********************************************************************/
424
425/* Currently not working - VERT_ATTRIB_POINTSIZE isn't correctly
426 * represented in the fragment program InputsRead field.
427 */
428static void
429intel_atten_point(struct intel_context *intel, intelVertexPtr v0)
430{
431   GLcontext *ctx = &intel->ctx;
432   GLfloat psz[4], col[4], restore_psz, restore_alpha;
433
434   _tnl_get_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
435   _tnl_get_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
436
437   restore_psz = psz[0];
438   restore_alpha = col[3];
439
440   if (psz[0] >= ctx->Point.Threshold) {
441      psz[0] = MIN2(psz[0], ctx->Point.MaxSize);
442   }
443   else {
444      GLfloat dsize = psz[0] / ctx->Point.Threshold;
445      psz[0] = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
446      col[3] *= dsize * dsize;
447   }
448
449   if (psz[0] < 1.0)
450      psz[0] = 1.0;
451
452   if (restore_psz != psz[0] || restore_alpha != col[3]) {
453      _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
454      _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
455
456      intel_draw_point(intel, v0);
457
458      psz[0] = restore_psz;
459      col[3] = restore_alpha;
460
461      _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
462      _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
463   }
464   else
465      intel_draw_point(intel, v0);
466}
467
468
469
470
471
472/***********************************************************************
473 *                Fixup for I915 WPOS texture coordinate                *
474 ***********************************************************************/
475
476
477
478static void
479intel_wpos_triangle(struct intel_context *intel,
480                    intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
481{
482   GLuint offset = intel->wpos_offset;
483   GLuint size = intel->wpos_size;
484   GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
485   GLfloat *v1_wpos = (GLfloat *)((char *)v1 + offset);
486   GLfloat *v2_wpos = (GLfloat *)((char *)v2 + offset);
487
488   __memcpy(v0_wpos, v0, size);
489   __memcpy(v1_wpos, v1, size);
490   __memcpy(v2_wpos, v2, size);
491
492   v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
493   v1_wpos[1] = -v1_wpos[1] + intel->driDrawable->h;
494   v2_wpos[1] = -v2_wpos[1] + intel->driDrawable->h;
495
496
497   intel_draw_triangle(intel, v0, v1, v2);
498}
499
500
501static void
502intel_wpos_line(struct intel_context *intel,
503                intelVertexPtr v0, intelVertexPtr v1)
504{
505   GLuint offset = intel->wpos_offset;
506   GLuint size = intel->wpos_size;
507   GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
508   GLfloat *v1_wpos = (GLfloat *)((char *)v1 + offset);
509
510   __memcpy(v0_wpos, v0, size);
511   __memcpy(v1_wpos, v1, size);
512
513   v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
514   v1_wpos[1] = -v1_wpos[1] + intel->driDrawable->h;
515
516   intel_draw_line(intel, v0, v1);
517}
518
519
520static void
521intel_wpos_point(struct intel_context *intel, intelVertexPtr v0)
522{
523   GLuint offset = intel->wpos_offset;
524   GLuint size = intel->wpos_size;
525   GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
526
527   __memcpy(v0_wpos, v0, size);
528   v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
529
530   intel_draw_point(intel, v0);
531}
532
533
534
535
536
537
538/***********************************************************************
539 *          Macros for t_dd_tritmp.h to draw basic primitives          *
540 ***********************************************************************/
541
542#define TRI( a, b, c )				\
543do { 						\
544   if (DO_FALLBACK)				\
545      intel->draw_tri( intel, a, b, c );	\
546   else						\
547      intel_draw_triangle( intel, a, b, c );	\
548} while (0)
549
550#define QUAD( a, b, c, d )			\
551do { 						\
552   if (DO_FALLBACK) {				\
553      intel->draw_tri( intel, a, b, d );	\
554      intel->draw_tri( intel, b, c, d );	\
555   } else					\
556      intel_draw_quad( intel, a, b, c, d );	\
557} while (0)
558
559#define LINE( v0, v1 )				\
560do { 						\
561   if (DO_FALLBACK)				\
562      intel->draw_line( intel, v0, v1 );	\
563   else						\
564      intel_draw_line( intel, v0, v1 );		\
565} while (0)
566
567#define POINT( v0 )				\
568do { 						\
569   if (DO_FALLBACK)				\
570      intel->draw_point( intel, v0 );		\
571   else						\
572      intel_draw_point( intel, v0 );		\
573} while (0)
574
575
576/***********************************************************************
577 *              Build render functions from dd templates               *
578 ***********************************************************************/
579
580#define INTEL_OFFSET_BIT 	0x01
581#define INTEL_TWOSIDE_BIT	0x02
582#define INTEL_UNFILLED_BIT	0x04
583#define INTEL_FALLBACK_BIT	0x08
584#define INTEL_MAX_TRIFUNC	0x10
585
586
587static struct
588{
589   tnl_points_func points;
590   tnl_line_func line;
591   tnl_triangle_func triangle;
592   tnl_quad_func quad;
593} rast_tab[INTEL_MAX_TRIFUNC];
594
595
596#define DO_FALLBACK (IND & INTEL_FALLBACK_BIT)
597#define DO_OFFSET   (IND & INTEL_OFFSET_BIT)
598#define DO_UNFILLED (IND & INTEL_UNFILLED_BIT)
599#define DO_TWOSIDE  (IND & INTEL_TWOSIDE_BIT)
600#define DO_FLAT      0
601#define DO_TRI       1
602#define DO_QUAD      1
603#define DO_LINE      1
604#define DO_POINTS    1
605#define DO_FULL_QUAD 1
606
607#define HAVE_RGBA         1
608#define HAVE_SPEC         1
609#define HAVE_BACK_COLORS  0
610#define HAVE_HW_FLATSHADE 1
611#define VERTEX            intelVertex
612#define TAB               rast_tab
613
614/* Only used to pull back colors into vertices (ie, we know color is
615 * floating point).
616 */
617#define INTEL_COLOR( dst, src )				\
618do {							\
619   UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]);	\
620   UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]);	\
621   UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]);	\
622   UNCLAMPED_FLOAT_TO_UBYTE((dst)[3], (src)[3]);	\
623} while (0)
624
625#define INTEL_SPEC( dst, src )				\
626do {							\
627   UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]);	\
628   UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]);	\
629   UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]);	\
630} while (0)
631
632
633#define DEPTH_SCALE intel->polygon_offset_scale
634#define UNFILLED_TRI unfilled_tri
635#define UNFILLED_QUAD unfilled_quad
636#define VERT_X(_v) _v->v.x
637#define VERT_Y(_v) _v->v.y
638#define VERT_Z(_v) _v->v.z
639#define AREA_IS_CCW( a ) (a > 0)
640#define GET_VERTEX(e) (intel->verts + (e * intel->vertex_size * sizeof(GLuint)))
641
642#define VERT_SET_RGBA( v, c )    if (coloroffset) INTEL_COLOR( v->ub4[coloroffset], c )
643#define VERT_COPY_RGBA( v0, v1 ) if (coloroffset) v0->ui[coloroffset] = v1->ui[coloroffset]
644#define VERT_SAVE_RGBA( idx )    if (coloroffset) color[idx] = v[idx]->ui[coloroffset]
645#define VERT_RESTORE_RGBA( idx ) if (coloroffset) v[idx]->ui[coloroffset] = color[idx]
646
647#define VERT_SET_SPEC( v, c )    if (specoffset) INTEL_SPEC( v->ub4[specoffset], c )
648#define VERT_COPY_SPEC( v0, v1 ) if (specoffset) COPY_3V(v0->ub4[specoffset], v1->ub4[specoffset])
649#define VERT_SAVE_SPEC( idx )    if (specoffset) spec[idx] = v[idx]->ui[specoffset]
650#define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
651
652#define LOCAL_VARS(n)							\
653   struct intel_context *intel = intel_context(ctx);			\
654   GLuint color[n] = { 0, }, spec[n] = { 0, };				\
655   GLuint coloroffset = intel->coloroffset;				\
656   GLboolean specoffset = intel->specoffset;				\
657   (void) color; (void) spec; (void) coloroffset; (void) specoffset;
658
659
660/***********************************************************************
661 *                Helpers for rendering unfilled primitives            *
662 ***********************************************************************/
663
664static const GLuint hw_prim[GL_POLYGON + 1] = {
665   PRIM3D_POINTLIST,
666   PRIM3D_LINELIST,
667   PRIM3D_LINELIST,
668   PRIM3D_LINELIST,
669   PRIM3D_TRILIST,
670   PRIM3D_TRILIST,
671   PRIM3D_TRILIST,
672   PRIM3D_TRILIST,
673   PRIM3D_TRILIST,
674   PRIM3D_TRILIST
675};
676
677#define RASTERIZE(x) intelRasterPrimitive( ctx, x, hw_prim[x] )
678#define RENDER_PRIMITIVE intel->render_primitive
679#define TAG(x) x
680#define IND INTEL_FALLBACK_BIT
681#include "tnl_dd/t_dd_unfilled.h"
682#undef IND
683
684/***********************************************************************
685 *                      Generate GL render functions                   *
686 ***********************************************************************/
687
688#define IND (0)
689#define TAG(x) x
690#include "tnl_dd/t_dd_tritmp.h"
691
692#define IND (INTEL_OFFSET_BIT)
693#define TAG(x) x##_offset
694#include "tnl_dd/t_dd_tritmp.h"
695
696#define IND (INTEL_TWOSIDE_BIT)
697#define TAG(x) x##_twoside
698#include "tnl_dd/t_dd_tritmp.h"
699
700#define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT)
701#define TAG(x) x##_twoside_offset
702#include "tnl_dd/t_dd_tritmp.h"
703
704#define IND (INTEL_UNFILLED_BIT)
705#define TAG(x) x##_unfilled
706#include "tnl_dd/t_dd_tritmp.h"
707
708#define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
709#define TAG(x) x##_offset_unfilled
710#include "tnl_dd/t_dd_tritmp.h"
711
712#define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT)
713#define TAG(x) x##_twoside_unfilled
714#include "tnl_dd/t_dd_tritmp.h"
715
716#define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
717#define TAG(x) x##_twoside_offset_unfilled
718#include "tnl_dd/t_dd_tritmp.h"
719
720#define IND (INTEL_FALLBACK_BIT)
721#define TAG(x) x##_fallback
722#include "tnl_dd/t_dd_tritmp.h"
723
724#define IND (INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
725#define TAG(x) x##_offset_fallback
726#include "tnl_dd/t_dd_tritmp.h"
727
728#define IND (INTEL_TWOSIDE_BIT|INTEL_FALLBACK_BIT)
729#define TAG(x) x##_twoside_fallback
730#include "tnl_dd/t_dd_tritmp.h"
731
732#define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
733#define TAG(x) x##_twoside_offset_fallback
734#include "tnl_dd/t_dd_tritmp.h"
735
736#define IND (INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
737#define TAG(x) x##_unfilled_fallback
738#include "tnl_dd/t_dd_tritmp.h"
739
740#define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
741#define TAG(x) x##_offset_unfilled_fallback
742#include "tnl_dd/t_dd_tritmp.h"
743
744#define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
745#define TAG(x) x##_twoside_unfilled_fallback
746#include "tnl_dd/t_dd_tritmp.h"
747
748#define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT| \
749	     INTEL_FALLBACK_BIT)
750#define TAG(x) x##_twoside_offset_unfilled_fallback
751#include "tnl_dd/t_dd_tritmp.h"
752
753
754static void
755init_rast_tab(void)
756{
757   init();
758   init_offset();
759   init_twoside();
760   init_twoside_offset();
761   init_unfilled();
762   init_offset_unfilled();
763   init_twoside_unfilled();
764   init_twoside_offset_unfilled();
765   init_fallback();
766   init_offset_fallback();
767   init_twoside_fallback();
768   init_twoside_offset_fallback();
769   init_unfilled_fallback();
770   init_offset_unfilled_fallback();
771   init_twoside_unfilled_fallback();
772   init_twoside_offset_unfilled_fallback();
773}
774
775
776/***********************************************************************
777 *                    Rasterization fallback helpers                   *
778 ***********************************************************************/
779
780
781/* This code is hit only when a mix of accelerated and unaccelerated
782 * primitives are being drawn, and only for the unaccelerated
783 * primitives.
784 */
785static void
786intel_fallback_tri(struct intel_context *intel,
787                   intelVertex * v0, intelVertex * v1, intelVertex * v2)
788{
789   GLcontext *ctx = &intel->ctx;
790   SWvertex v[3];
791
792   if (0)
793      fprintf(stderr, "\n%s\n", __FUNCTION__);
794
795   INTEL_FIREVERTICES(intel);
796
797   _swsetup_Translate(ctx, v0, &v[0]);
798   _swsetup_Translate(ctx, v1, &v[1]);
799   _swsetup_Translate(ctx, v2, &v[2]);
800   intelSpanRenderStart(ctx);
801   _swrast_Triangle(ctx, &v[0], &v[1], &v[2]);
802   intelSpanRenderFinish(ctx);
803}
804
805
806static void
807intel_fallback_line(struct intel_context *intel,
808                    intelVertex * v0, intelVertex * v1)
809{
810   GLcontext *ctx = &intel->ctx;
811   SWvertex v[2];
812
813   if (0)
814      fprintf(stderr, "\n%s\n", __FUNCTION__);
815
816   INTEL_FIREVERTICES(intel);
817
818   _swsetup_Translate(ctx, v0, &v[0]);
819   _swsetup_Translate(ctx, v1, &v[1]);
820   intelSpanRenderStart(ctx);
821   _swrast_Line(ctx, &v[0], &v[1]);
822   intelSpanRenderFinish(ctx);
823}
824
825static void
826intel_fallback_point(struct intel_context *intel,
827		     intelVertex * v0)
828{
829   GLcontext *ctx = &intel->ctx;
830   SWvertex v[1];
831
832   if (0)
833      fprintf(stderr, "\n%s\n", __FUNCTION__);
834
835   INTEL_FIREVERTICES(intel);
836
837   _swsetup_Translate(ctx, v0, &v[0]);
838   intelSpanRenderStart(ctx);
839   _swrast_Point(ctx, &v[0]);
840   intelSpanRenderFinish(ctx);
841}
842
843
844/**********************************************************************/
845/*               Render unclipped begin/end objects                   */
846/**********************************************************************/
847
848#define IND 0
849#define V(x) (intelVertex *)(vertptr + ((x)*vertsize*sizeof(GLuint)))
850#define RENDER_POINTS( start, count )	\
851   for ( ; start < count ; start++) POINT( V(ELT(start)) );
852#define RENDER_LINE( v0, v1 )         LINE( V(v0), V(v1) )
853#define RENDER_TRI(  v0, v1, v2 )     TRI(  V(v0), V(v1), V(v2) )
854#define RENDER_QUAD( v0, v1, v2, v3 ) QUAD( V(v0), V(v1), V(v2), V(v3) )
855#define INIT(x) intelRenderPrimitive( ctx, x )
856#undef LOCAL_VARS
857#define LOCAL_VARS						\
858    struct intel_context *intel = intel_context(ctx);			\
859    GLubyte *vertptr = (GLubyte *)intel->verts;			\
860    const GLuint vertsize = intel->vertex_size;       	\
861    const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts;	\
862    (void) elt;
863#define RESET_STIPPLE
864#define RESET_OCCLUSION
865#define PRESERVE_VB_DEFS
866#define ELT(x) x
867#define TAG(x) intel_##x##_verts
868#include "tnl/t_vb_rendertmp.h"
869#undef ELT
870#undef TAG
871#define TAG(x) intel_##x##_elts
872#define ELT(x) elt[x]
873#include "tnl/t_vb_rendertmp.h"
874
875/**********************************************************************/
876/*                   Render clipped primitives                        */
877/**********************************************************************/
878
879
880
881static void
882intelRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
883{
884   struct intel_context *intel = intel_context(ctx);
885   TNLcontext *tnl = TNL_CONTEXT(ctx);
886   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
887   GLuint prim = intel->render_primitive;
888
889   /* Render the new vertices as an unclipped polygon.
890    */
891   {
892      GLuint *tmp = VB->Elts;
893      VB->Elts = (GLuint *) elts;
894      tnl->Driver.Render.PrimTabElts[GL_POLYGON] (ctx, 0, n,
895                                                  PRIM_BEGIN | PRIM_END);
896      VB->Elts = tmp;
897   }
898
899   /* Restore the render primitive
900    */
901   if (prim != GL_POLYGON)
902      tnl->Driver.Render.PrimitiveNotify(ctx, prim);
903}
904
905static void
906intelRenderClippedLine(GLcontext * ctx, GLuint ii, GLuint jj)
907{
908   TNLcontext *tnl = TNL_CONTEXT(ctx);
909
910   tnl->Driver.Render.Line(ctx, ii, jj);
911}
912
913static void
914intelFastRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
915{
916   struct intel_context *intel = intel_context(ctx);
917   const GLuint vertsize = intel->vertex_size;
918   GLuint *vb = intel_get_prim_space(intel, (n - 2) * 3);
919   GLubyte *vertptr = (GLubyte *) intel->verts;
920   const GLuint *start = (const GLuint *) V(elts[0]);
921   int i, j;
922
923   for (i = 2; i < n; i++) {
924      COPY_DWORDS(j, vb, vertsize, V(elts[i - 1]));
925      COPY_DWORDS(j, vb, vertsize, V(elts[i]));
926      COPY_DWORDS(j, vb, vertsize, start);
927   }
928}
929
930/**********************************************************************/
931/*                    Choose render functions                         */
932/**********************************************************************/
933
934
935
936
937#define ANY_FALLBACK_FLAGS (DD_LINE_STIPPLE | DD_TRI_STIPPLE | DD_POINT_ATTEN | DD_POINT_SMOOTH | DD_TRI_SMOOTH)
938#define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE | DD_TRI_OFFSET | DD_TRI_UNFILLED)
939
940void
941intelChooseRenderState(GLcontext * ctx)
942{
943   TNLcontext *tnl = TNL_CONTEXT(ctx);
944   struct intel_context *intel = intel_context(ctx);
945   GLuint flags = ctx->_TriangleCaps;
946   const struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
947   GLboolean have_wpos = (fprog && (fprog->Base.InputsRead & FRAG_BIT_WPOS));
948   GLuint index = 0;
949
950   if (INTEL_DEBUG & DEBUG_STATE)
951      fprintf(stderr, "\n%s\n", __FUNCTION__);
952
953   if ((flags & (ANY_FALLBACK_FLAGS | ANY_RASTER_FLAGS)) || have_wpos) {
954
955      if (flags & ANY_RASTER_FLAGS) {
956         if (flags & DD_TRI_LIGHT_TWOSIDE)
957            index |= INTEL_TWOSIDE_BIT;
958         if (flags & DD_TRI_OFFSET)
959            index |= INTEL_OFFSET_BIT;
960         if (flags & DD_TRI_UNFILLED)
961            index |= INTEL_UNFILLED_BIT;
962      }
963
964      if (have_wpos) {
965         intel->draw_point = intel_wpos_point;
966         intel->draw_line = intel_wpos_line;
967         intel->draw_tri = intel_wpos_triangle;
968
969         /* Make sure these get called:
970          */
971         index |= INTEL_FALLBACK_BIT;
972      }
973      else {
974         intel->draw_point = intel_draw_point;
975         intel->draw_line = intel_draw_line;
976         intel->draw_tri = intel_draw_triangle;
977      }
978
979      /* Hook in fallbacks for specific primitives.
980       */
981      if (flags & ANY_FALLBACK_FLAGS) {
982         if (flags & DD_LINE_STIPPLE)
983            intel->draw_line = intel_fallback_line;
984
985         if ((flags & DD_TRI_STIPPLE) && !intel->hw_stipple)
986            intel->draw_tri = intel_fallback_tri;
987
988         if (flags & DD_TRI_SMOOTH) {
989	    if (intel->conformance_mode > 0)
990	       intel->draw_tri = intel_fallback_tri;
991	 }
992
993         if (flags & DD_POINT_ATTEN) {
994	    if (0)
995	       intel->draw_point = intel_atten_point;
996	    else
997	       intel->draw_point = intel_fallback_point;
998	 }
999
1000	 if (flags & DD_POINT_SMOOTH) {
1001	    if (intel->conformance_mode > 0)
1002	       intel->draw_point = intel_fallback_point;
1003	 }
1004
1005         index |= INTEL_FALLBACK_BIT;
1006      }
1007   }
1008
1009   if (intel->RenderIndex != index) {
1010      intel->RenderIndex = index;
1011
1012      tnl->Driver.Render.Points = rast_tab[index].points;
1013      tnl->Driver.Render.Line = rast_tab[index].line;
1014      tnl->Driver.Render.Triangle = rast_tab[index].triangle;
1015      tnl->Driver.Render.Quad = rast_tab[index].quad;
1016
1017      if (index == 0) {
1018         tnl->Driver.Render.PrimTabVerts = intel_render_tab_verts;
1019         tnl->Driver.Render.PrimTabElts = intel_render_tab_elts;
1020         tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
1021         tnl->Driver.Render.ClippedPolygon = intelFastRenderClippedPoly;
1022      }
1023      else {
1024         tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
1025         tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
1026         tnl->Driver.Render.ClippedLine = intelRenderClippedLine;
1027         tnl->Driver.Render.ClippedPolygon = intelRenderClippedPoly;
1028      }
1029   }
1030}
1031
1032static const GLenum reduced_prim[GL_POLYGON + 1] = {
1033   GL_POINTS,
1034   GL_LINES,
1035   GL_LINES,
1036   GL_LINES,
1037   GL_TRIANGLES,
1038   GL_TRIANGLES,
1039   GL_TRIANGLES,
1040   GL_TRIANGLES,
1041   GL_TRIANGLES,
1042   GL_TRIANGLES
1043};
1044
1045
1046/**********************************************************************/
1047/*                 High level hooks for t_vb_render.c                 */
1048/**********************************************************************/
1049
1050
1051
1052
1053static void
1054intelRunPipeline(GLcontext * ctx)
1055{
1056   struct intel_context *intel = intel_context(ctx);
1057
1058   _mesa_lock_context_textures(ctx);
1059
1060   if (ctx->NewState)
1061      _mesa_update_state_locked(ctx);
1062
1063   if (intel->NewGLState) {
1064      if (intel->NewGLState & _NEW_TEXTURE) {
1065         intel->vtbl.update_texture_state(intel);
1066      }
1067
1068      if (!intel->Fallback) {
1069         if (intel->NewGLState & _INTEL_NEW_RENDERSTATE)
1070            intelChooseRenderState(ctx);
1071      }
1072
1073      intel->NewGLState = 0;
1074   }
1075
1076   intel_map_vertex_shader_textures(ctx);
1077   _tnl_run_pipeline(ctx);
1078   intel_unmap_vertex_shader_textures(ctx);
1079
1080   _mesa_unlock_context_textures(ctx);
1081}
1082
1083static void
1084intelRenderStart(GLcontext * ctx)
1085{
1086   struct intel_context *intel = intel_context(ctx);
1087
1088   intel_check_front_buffer_rendering(intel);
1089   intel->vtbl.render_start(intel_context(ctx));
1090   intel->vtbl.emit_state(intel);
1091}
1092
1093static void
1094intelRenderFinish(GLcontext * ctx)
1095{
1096   struct intel_context *intel = intel_context(ctx);
1097
1098   if (intel->RenderIndex & INTEL_FALLBACK_BIT)
1099      _swrast_flush(ctx);
1100
1101   INTEL_FIREVERTICES(intel);
1102}
1103
1104
1105
1106
1107 /* System to flush dma and emit state changes based on the rasterized
1108  * primitive.
1109  */
1110static void
1111intelRasterPrimitive(GLcontext * ctx, GLenum rprim, GLuint hwprim)
1112{
1113   struct intel_context *intel = intel_context(ctx);
1114
1115   if (0)
1116      fprintf(stderr, "%s %s %x\n", __FUNCTION__,
1117              _mesa_lookup_enum_by_nr(rprim), hwprim);
1118
1119   intel->vtbl.reduced_primitive_state(intel, rprim);
1120
1121   /* Start a new primitive.  Arrange to have it flushed later on.
1122    */
1123   if (hwprim != intel->prim.primitive) {
1124      INTEL_FIREVERTICES(intel);
1125
1126      intel_set_prim(intel, hwprim);
1127   }
1128}
1129
1130
1131 /*
1132  */
1133static void
1134intelRenderPrimitive(GLcontext * ctx, GLenum prim)
1135{
1136   struct intel_context *intel = intel_context(ctx);
1137
1138   if (0)
1139      fprintf(stderr, "%s %s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(prim));
1140
1141   /* Let some clipping routines know which primitive they're dealing
1142    * with.
1143    */
1144   intel->render_primitive = prim;
1145
1146   /* Shortcircuit this when called from t_dd_rendertmp.h for unfilled
1147    * triangles.  The rasterized primitive will always be reset by
1148    * lower level functions in that case, potentially pingponging the
1149    * state:
1150    */
1151   if (reduced_prim[prim] == GL_TRIANGLES &&
1152       (ctx->_TriangleCaps & DD_TRI_UNFILLED))
1153      return;
1154
1155   /* Set some primitive-dependent state and Start? a new primitive.
1156    */
1157   intelRasterPrimitive(ctx, reduced_prim[prim], hw_prim[prim]);
1158}
1159
1160
1161 /**********************************************************************/
1162 /*           Transition to/from hardware rasterization.               */
1163 /**********************************************************************/
1164
1165static char *fallbackStrings[] = {
1166   [0] = "Draw buffer",
1167   [1] = "Read buffer",
1168   [2] = "Depth buffer",
1169   [3] = "Stencil buffer",
1170   [4] = "User disable",
1171   [5] = "Render mode",
1172
1173   [12] = "Texture",
1174   [13] = "Color mask",
1175   [14] = "Stencil",
1176   [15] = "Stipple",
1177   [16] = "Program",
1178   [17] = "Logic op",
1179   [18] = "Smooth polygon",
1180   [19] = "Smooth point",
1181};
1182
1183
1184static char *
1185getFallbackString(GLuint bit)
1186{
1187   int i = 0;
1188   while (bit > 1) {
1189      i++;
1190      bit >>= 1;
1191   }
1192   return fallbackStrings[i];
1193}
1194
1195
1196
1197/**
1198 * Enable/disable a fallback flag.
1199 * \param bit  one of INTEL_FALLBACK_x flags.
1200 */
1201void
1202intelFallback(struct intel_context *intel, GLbitfield bit, GLboolean mode)
1203{
1204   GLcontext *ctx = &intel->ctx;
1205   TNLcontext *tnl = TNL_CONTEXT(ctx);
1206   const GLbitfield oldfallback = intel->Fallback;
1207
1208   if (mode) {
1209      intel->Fallback |= bit;
1210      if (oldfallback == 0) {
1211         intelFlush(ctx);
1212         if (INTEL_DEBUG & DEBUG_FALLBACKS)
1213            fprintf(stderr, "ENTER FALLBACK %x: %s\n",
1214                    bit, getFallbackString(bit));
1215         _swsetup_Wakeup(ctx);
1216         intel->RenderIndex = ~0;
1217      }
1218   }
1219   else {
1220      intel->Fallback &= ~bit;
1221      if (oldfallback == bit) {
1222         _swrast_flush(ctx);
1223         if (INTEL_DEBUG & DEBUG_FALLBACKS)
1224            fprintf(stderr, "LEAVE FALLBACK %s\n", getFallbackString(bit));
1225         tnl->Driver.Render.Start = intelRenderStart;
1226         tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1227         tnl->Driver.Render.Finish = intelRenderFinish;
1228         tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1229         tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1230         tnl->Driver.Render.Interp = _tnl_interp;
1231
1232         _tnl_invalidate_vertex_state(ctx, ~0);
1233         _tnl_invalidate_vertices(ctx, ~0);
1234         _tnl_install_attrs(ctx,
1235                            intel->vertex_attrs,
1236                            intel->vertex_attr_count,
1237                            intel->ViewportMatrix.m, 0);
1238
1239         intel->NewGLState |= _INTEL_NEW_RENDERSTATE;
1240      }
1241   }
1242}
1243
1244union fi
1245{
1246   GLfloat f;
1247   GLint i;
1248};
1249
1250/**********************************************************************/
1251/*                            Initialization.                         */
1252/**********************************************************************/
1253
1254
1255void
1256intelInitTriFuncs(GLcontext * ctx)
1257{
1258   TNLcontext *tnl = TNL_CONTEXT(ctx);
1259   static int firsttime = 1;
1260
1261   if (firsttime) {
1262      init_rast_tab();
1263      firsttime = 0;
1264   }
1265
1266   tnl->Driver.RunPipeline = intelRunPipeline;
1267   tnl->Driver.Render.Start = intelRenderStart;
1268   tnl->Driver.Render.Finish = intelRenderFinish;
1269   tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1270   tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1271   tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1272   tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1273   tnl->Driver.Render.Interp = _tnl_interp;
1274}
1275