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