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