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