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