1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 /** @file intel_tris.c
30 * This file contains functions for managing the vertex buffer and emitting
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"
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"
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"
57 #include "i915_context.h"
59 static void intelRenderPrimitive(struct gl_context * ctx, GLenum prim);
60 static void intelRasterPrimitive(struct gl_context * ctx, GLenum rprim,
64 intel_flush_inline_primitive(struct intel_context *intel)
66 GLuint used = intel->batch.used - intel->prim.start_ptr;
68 assert(intel->prim.primitive != ~0);
75 intel->batch.map[intel->prim.start_ptr] =
76 _3DPRIMITIVE | intel->prim.primitive | (used - 2);
81 intel->batch.used = intel->prim.start_ptr;
84 intel->prim.primitive = ~0;
85 intel->prim.start_ptr = 0;
86 intel->prim.flush = 0;
89 static void intel_start_inline(struct intel_context *intel, uint32_t prim)
93 intel->vtbl.emit_state(intel);
95 intel->no_batch_wrap = GL_TRUE;
97 /*printf("%s *", __progname);*/
99 /* Emit a slot which will be filled with the inline primitive
104 intel->prim.start_ptr = intel->batch.used;
105 intel->prim.primitive = prim;
106 intel->prim.flush = intel_flush_inline_primitive;
111 intel->no_batch_wrap = GL_FALSE;
115 static void intel_wrap_inline(struct intel_context *intel)
117 GLuint prim = intel->prim.primitive;
119 intel_flush_inline_primitive(intel);
120 intel_batchbuffer_flush(intel);
121 intel_start_inline(intel, prim); /* ??? */
124 static GLuint *intel_extend_inline(struct intel_context *intel, GLuint dwords)
128 assert(intel->prim.flush == intel_flush_inline_primitive);
130 if (intel_batchbuffer_space(intel) < dwords * sizeof(GLuint))
131 intel_wrap_inline(intel);
135 intel->vtbl.assert_not_dirty(intel);
137 ptr = intel->batch.map + intel->batch.used;
138 intel->batch.used += dwords;
143 /** Sets the primitive type for a primitive sequence, flushing as needed. */
144 void intel_set_prim(struct intel_context *intel, uint32_t prim)
146 /* if we have no VBOs */
148 if (intel->intelScreen->no_vbo) {
149 intel_start_inline(intel, prim);
152 if (prim != intel->prim.primitive) {
153 INTEL_FIREVERTICES(intel);
154 intel->prim.primitive = prim;
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)
163 if (intel->intelScreen->no_vbo) {
164 return intel_extend_inline(intel, count * intel->vertex_size);
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);
175 intel_finish_vb(intel);
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",
182 intel->prim.start_offset = 0;
183 intel->prim.current_offset = 0;
186 intel->prim.flush = intel_flush_prim;
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;
195 /** Dispatches the accumulated primitive to the batchbuffer. */
196 void intel_flush_prim(struct intel_context *intel)
198 drm_intel_bo *aper_array[2];
200 unsigned int offset, count;
203 /* Must be called after an intel_start_prim. */
204 assert(intel->prim.primitive != ~0);
206 if (intel->prim.count == 0)
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.
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;
219 intel->prim.current_offset = intel->prim.start_offset = ALIGN(intel->prim.start_offset, 128);
220 intel->prim.flush = NULL;
222 intel->vtbl.emit_state(intel);
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);
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.
235 intel->no_batch_wrap = GL_TRUE;
238 printf("emitting %d..%d=%d vertices size %d\n", offset,
239 intel->prim.current_offset, count,
240 intel->vertex_size * 4);
243 if (intel->gen >= 3) {
244 struct i915_context *i915 = i915_context(&intel->ctx);
245 unsigned int cmd = 0, len = 0;
247 if (vb_bo != i915->current_vb_bo) {
252 if (intel->vertex_size != i915->current_vertex_size) {
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;
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;
271 OUT_BATCH(_3DPRIMITIVE |
273 PRIM_INDIRECT_SEQUENTIAL |
274 intel->prim.primitive |
276 OUT_BATCH(offset / (intel->vertex_size * 4));
279 struct i830_context *i830 = i830_context(&intel->ctx);
282 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
283 I1_LOAD_S(0) | I1_LOAD_S(2) | 1);
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) |
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.
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);
300 OUT_BATCH(_3DPRIMITIVE |
302 PRIM_INDIRECT_SEQUENTIAL |
303 intel->prim.primitive |
305 OUT_BATCH(0); /* Beginning vertex index */
309 intel->no_batch_wrap = GL_FALSE;
311 drm_intel_bo_unreference(vb_bo);
315 * Uploads the locally-accumulated VB into the buffer object.
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.
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.
325 void intel_finish_vb(struct intel_context *intel)
327 if (intel->prim.vb_bo == NULL)
330 drm_intel_bo_subdata(intel->prim.vb_bo, 0, intel->prim.start_offset,
332 drm_intel_bo_unreference(intel->prim.vb_bo);
333 intel->prim.vb_bo = NULL;
336 /***********************************************************************
337 * Emit primitives as inline vertices *
338 ***********************************************************************/
341 #define COPY_DWORDS( j, vb, vertsize, v ) \
344 __asm__ __volatile__( "rep ; movsl" \
345 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
351 #define COPY_DWORDS( j, vb, vertsize, v ) \
353 for ( j = 0 ; j < vertsize ; j++ ) { \
354 vb[j] = ((GLuint *)v)[j]; \
361 intel_draw_quad(struct intel_context *intel,
363 intelVertexPtr v1, intelVertexPtr v2, intelVertexPtr v3)
365 GLuint vertsize = intel->vertex_size;
366 GLuint *vb = intel_get_prim_space(intel, 6);
369 COPY_DWORDS(j, vb, vertsize, v0);
370 COPY_DWORDS(j, vb, vertsize, v1);
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.
376 if (intel->ctx.Light.ShadeModel == GL_FLAT) {
377 COPY_DWORDS(j, vb, vertsize, v3);
378 COPY_DWORDS(j, vb, vertsize, v1);
381 COPY_DWORDS(j, vb, vertsize, v2);
382 COPY_DWORDS(j, vb, vertsize, v0);
385 COPY_DWORDS(j, vb, vertsize, v2);
386 COPY_DWORDS(j, vb, vertsize, v3);
390 intel_draw_triangle(struct intel_context *intel,
391 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
393 GLuint vertsize = intel->vertex_size;
394 GLuint *vb = intel_get_prim_space(intel, 3);
397 COPY_DWORDS(j, vb, vertsize, v0);
398 COPY_DWORDS(j, vb, vertsize, v1);
399 COPY_DWORDS(j, vb, vertsize, v2);
404 intel_draw_line(struct intel_context *intel,
405 intelVertexPtr v0, intelVertexPtr v1)
407 GLuint vertsize = intel->vertex_size;
408 GLuint *vb = intel_get_prim_space(intel, 2);
411 COPY_DWORDS(j, vb, vertsize, v0);
412 COPY_DWORDS(j, vb, vertsize, v1);
417 intel_draw_point(struct intel_context *intel, intelVertexPtr v0)
419 GLuint vertsize = intel->vertex_size;
420 GLuint *vb = intel_get_prim_space(intel, 1);
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++)
432 /***********************************************************************
433 * Fixup for ARB_point_parameters *
434 ***********************************************************************/
436 /* Currently not working - VERT_ATTRIB_POINTSIZE isn't correctly
437 * represented in the fragment program InputsRead field.
440 intel_atten_point(struct intel_context *intel, intelVertexPtr v0)
442 struct gl_context *ctx = &intel->ctx;
443 GLfloat psz[4], col[4], restore_psz, restore_alpha;
445 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
446 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
448 restore_psz = psz[0];
449 restore_alpha = col[3];
451 if (psz[0] >= ctx->Point.Threshold) {
452 psz[0] = MIN2(psz[0], ctx->Point.MaxSize);
455 GLfloat dsize = psz[0] / ctx->Point.Threshold;
456 psz[0] = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
457 col[3] *= dsize * dsize;
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);
467 intel_draw_point(intel, v0);
469 psz[0] = restore_psz;
470 col[3] = restore_alpha;
472 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
473 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
476 intel_draw_point(intel, v0);
483 /***********************************************************************
484 * Fixup for I915 WPOS texture coordinate *
485 ***********************************************************************/
488 intel_emit_fragcoord(struct intel_context *intel, intelVertexPtr v)
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);
496 fragcoord[0] = vertex_position[0];
499 fragcoord[1] = vertex_position[1];
501 fragcoord[1] = fb->Height - vertex_position[1];
503 fragcoord[2] = vertex_position[2];
504 fragcoord[3] = vertex_position[3];
508 intel_wpos_triangle(struct intel_context *intel,
509 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
511 intel_emit_fragcoord(intel, v0);
512 intel_emit_fragcoord(intel, v1);
513 intel_emit_fragcoord(intel, v2);
515 intel_draw_triangle(intel, v0, v1, v2);
520 intel_wpos_line(struct intel_context *intel,
521 intelVertexPtr v0, intelVertexPtr v1)
523 intel_emit_fragcoord(intel, v0);
524 intel_emit_fragcoord(intel, v1);
525 intel_draw_line(intel, v0, v1);
530 intel_wpos_point(struct intel_context *intel, intelVertexPtr v0)
532 intel_emit_fragcoord(intel, v0);
533 intel_draw_point(intel, v0);
541 /***********************************************************************
542 * Macros for t_dd_tritmp.h to draw basic primitives *
543 ***********************************************************************/
545 #define TRI( a, b, c ) \
548 intel->draw_tri( intel, a, b, c ); \
550 intel_draw_triangle( intel, a, b, c ); \
553 #define QUAD( a, b, c, d ) \
556 intel->draw_tri( intel, a, b, d ); \
557 intel->draw_tri( intel, b, c, d ); \
559 intel_draw_quad( intel, a, b, c, d ); \
562 #define LINE( v0, v1 ) \
565 intel->draw_line( intel, v0, v1 ); \
567 intel_draw_line( intel, v0, v1 ); \
570 #define POINT( v0 ) \
573 intel->draw_point( intel, v0 ); \
575 intel_draw_point( intel, v0 ); \
579 /***********************************************************************
580 * Build render functions from dd templates *
581 ***********************************************************************/
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
592 tnl_points_func points;
594 tnl_triangle_func triangle;
596 } rast_tab[INTEL_MAX_TRIFUNC];
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)
608 #define DO_FULL_QUAD 1
611 #define HAVE_BACK_COLORS 0
612 #define HAVE_HW_FLATSHADE 1
613 #define VERTEX intelVertex
616 /* Only used to pull back colors into vertices (ie, we know color is
619 #define INTEL_COLOR( dst, src ) \
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]); \
627 #define INTEL_SPEC( dst, src ) \
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]); \
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)))
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]
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]
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;
662 /***********************************************************************
663 * Helpers for rendering unfilled primitives *
664 ***********************************************************************/
666 static const GLuint hw_prim[GL_POLYGON + 1] = {
679 #define RASTERIZE(x) intelRasterPrimitive( ctx, x, hw_prim[x] )
680 #define RENDER_PRIMITIVE intel->render_primitive
682 #define IND INTEL_FALLBACK_BIT
683 #include "tnl_dd/t_dd_unfilled.h"
686 /***********************************************************************
687 * Generate GL render functions *
688 ***********************************************************************/
692 #include "tnl_dd/t_dd_tritmp.h"
694 #define IND (INTEL_OFFSET_BIT)
695 #define TAG(x) x##_offset
696 #include "tnl_dd/t_dd_tritmp.h"
698 #define IND (INTEL_TWOSIDE_BIT)
699 #define TAG(x) x##_twoside
700 #include "tnl_dd/t_dd_tritmp.h"
702 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT)
703 #define TAG(x) x##_twoside_offset
704 #include "tnl_dd/t_dd_tritmp.h"
706 #define IND (INTEL_UNFILLED_BIT)
707 #define TAG(x) x##_unfilled
708 #include "tnl_dd/t_dd_tritmp.h"
710 #define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
711 #define TAG(x) x##_offset_unfilled
712 #include "tnl_dd/t_dd_tritmp.h"
714 #define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT)
715 #define TAG(x) x##_twoside_unfilled
716 #include "tnl_dd/t_dd_tritmp.h"
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"
722 #define IND (INTEL_FALLBACK_BIT)
723 #define TAG(x) x##_fallback
724 #include "tnl_dd/t_dd_tritmp.h"
726 #define IND (INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
727 #define TAG(x) x##_offset_fallback
728 #include "tnl_dd/t_dd_tritmp.h"
730 #define IND (INTEL_TWOSIDE_BIT|INTEL_FALLBACK_BIT)
731 #define TAG(x) x##_twoside_fallback
732 #include "tnl_dd/t_dd_tritmp.h"
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"
738 #define IND (INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
739 #define TAG(x) x##_unfilled_fallback
740 #include "tnl_dd/t_dd_tritmp.h"
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"
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"
750 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT| \
752 #define TAG(x) x##_twoside_offset_unfilled_fallback
753 #include "tnl_dd/t_dd_tritmp.h"
762 init_twoside_offset();
764 init_offset_unfilled();
765 init_twoside_unfilled();
766 init_twoside_offset_unfilled();
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();
778 /***********************************************************************
779 * Rasterization fallback helpers *
780 ***********************************************************************/
783 /* This code is hit only when a mix of accelerated and unaccelerated
784 * primitives are being drawn, and only for the unaccelerated
788 intel_fallback_tri(struct intel_context *intel,
789 intelVertex * v0, intelVertex * v1, intelVertex * v2)
791 struct gl_context *ctx = &intel->ctx;
795 fprintf(stderr, "\n%s\n", __FUNCTION__);
797 INTEL_FIREVERTICES(intel);
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);
809 intel_fallback_line(struct intel_context *intel,
810 intelVertex * v0, intelVertex * v1)
812 struct gl_context *ctx = &intel->ctx;
816 fprintf(stderr, "\n%s\n", __FUNCTION__);
818 INTEL_FIREVERTICES(intel);
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);
828 intel_fallback_point(struct intel_context *intel,
831 struct gl_context *ctx = &intel->ctx;
835 fprintf(stderr, "\n%s\n", __FUNCTION__);
837 INTEL_FIREVERTICES(intel);
839 _swsetup_Translate(ctx, v0, &v[0]);
840 intelSpanRenderStart(ctx);
841 _swrast_Point(ctx, &v[0]);
842 intelSpanRenderFinish(ctx);
846 /**********************************************************************/
847 /* Render unclipped begin/end objects */
848 /**********************************************************************/
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 )
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; \
865 #define RESET_STIPPLE
866 #define RESET_OCCLUSION
867 #define PRESERVE_VB_DEFS
869 #define TAG(x) intel_##x##_verts
870 #include "tnl/t_vb_rendertmp.h"
873 #define TAG(x) intel_##x##_elts
874 #define ELT(x) elt[x]
875 #include "tnl/t_vb_rendertmp.h"
877 /**********************************************************************/
878 /* Render clipped primitives */
879 /**********************************************************************/
884 intelRenderClippedPoly(struct gl_context * ctx, const GLuint * elts, GLuint n)
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;
891 /* Render the new vertices as an unclipped polygon.
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);
901 /* Restore the render primitive
903 if (prim != GL_POLYGON)
904 tnl->Driver.Render.PrimitiveNotify(ctx, prim);
908 intelRenderClippedLine(struct gl_context * ctx, GLuint ii, GLuint jj)
910 TNLcontext *tnl = TNL_CONTEXT(ctx);
912 tnl->Driver.Render.Line(ctx, ii, jj);
916 intelFastRenderClippedPoly(struct gl_context * ctx, const GLuint * elts, GLuint n)
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]);
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);
932 /**********************************************************************/
933 /* Choose render functions */
934 /**********************************************************************/
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)
943 intelChooseRenderState(struct gl_context * ctx)
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));
952 if (INTEL_DEBUG & DEBUG_STATE)
953 fprintf(stderr, "\n%s\n", __FUNCTION__);
955 if ((flags & (ANY_FALLBACK_FLAGS | ANY_RASTER_FLAGS)) || have_wpos) {
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;
967 intel->draw_point = intel_wpos_point;
968 intel->draw_line = intel_wpos_line;
969 intel->draw_tri = intel_wpos_triangle;
971 /* Make sure these get called:
973 index |= INTEL_FALLBACK_BIT;
976 intel->draw_point = intel_draw_point;
977 intel->draw_line = intel_draw_line;
978 intel->draw_tri = intel_draw_triangle;
981 /* Hook in fallbacks for specific primitives.
983 if (flags & ANY_FALLBACK_FLAGS) {
984 if (flags & DD_LINE_STIPPLE)
985 intel->draw_line = intel_fallback_line;
987 if ((flags & DD_TRI_STIPPLE) && !intel->hw_stipple)
988 intel->draw_tri = intel_fallback_tri;
990 if (flags & DD_TRI_SMOOTH) {
991 if (intel->conformance_mode > 0)
992 intel->draw_tri = intel_fallback_tri;
995 if (flags & DD_POINT_ATTEN) {
997 intel->draw_point = intel_atten_point;
999 intel->draw_point = intel_fallback_point;
1002 if (flags & DD_POINT_SMOOTH) {
1003 if (intel->conformance_mode > 0)
1004 intel->draw_point = intel_fallback_point;
1007 index |= INTEL_FALLBACK_BIT;
1011 if (intel->RenderIndex != index) {
1012 intel->RenderIndex = index;
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;
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;
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;
1034 static const GLenum reduced_prim[GL_POLYGON + 1] = {
1048 /**********************************************************************/
1049 /* High level hooks for t_vb_render.c */
1050 /**********************************************************************/
1056 intelRunPipeline(struct gl_context * ctx)
1058 struct intel_context *intel = intel_context(ctx);
1060 _mesa_lock_context_textures(ctx);
1063 _mesa_update_state_locked(ctx);
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.
1070 intel_prepare_render(intel);
1072 if (intel->NewGLState) {
1073 if (intel->NewGLState & _NEW_TEXTURE) {
1074 intel->vtbl.update_texture_state(intel);
1077 if (!intel->Fallback) {
1078 if (intel->NewGLState & _INTEL_NEW_RENDERSTATE)
1079 intelChooseRenderState(ctx);
1082 intel->NewGLState = 0;
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);
1091 _mesa_unlock_context_textures(ctx);
1095 intelRenderStart(struct gl_context * ctx)
1097 struct intel_context *intel = intel_context(ctx);
1099 intel_check_front_buffer_rendering(intel);
1100 intel->vtbl.render_start(intel_context(ctx));
1101 intel->vtbl.emit_state(intel);
1105 intelRenderFinish(struct gl_context * ctx)
1107 struct intel_context *intel = intel_context(ctx);
1109 if (intel->RenderIndex & INTEL_FALLBACK_BIT)
1112 INTEL_FIREVERTICES(intel);
1118 /* System to flush dma and emit state changes based on the rasterized
1122 intelRasterPrimitive(struct gl_context * ctx, GLenum rprim, GLuint hwprim)
1124 struct intel_context *intel = intel_context(ctx);
1127 fprintf(stderr, "%s %s %x\n", __FUNCTION__,
1128 _mesa_lookup_enum_by_nr(rprim), hwprim);
1130 intel->vtbl.reduced_primitive_state(intel, rprim);
1132 /* Start a new primitive. Arrange to have it flushed later on.
1134 if (hwprim != intel->prim.primitive) {
1135 INTEL_FIREVERTICES(intel);
1137 intel_set_prim(intel, hwprim);
1145 intelRenderPrimitive(struct gl_context * ctx, GLenum prim)
1147 struct intel_context *intel = intel_context(ctx);
1150 fprintf(stderr, "%s %s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(prim));
1152 /* Let some clipping routines know which primitive they're dealing
1155 intel->render_primitive = prim;
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
1162 if (reduced_prim[prim] == GL_TRIANGLES &&
1163 (ctx->_TriangleCaps & DD_TRI_UNFILLED))
1166 /* Set some primitive-dependent state and Start? a new primitive.
1168 intelRasterPrimitive(ctx, reduced_prim[prim], hw_prim[prim]);
1172 /**********************************************************************/
1173 /* Transition to/from hardware rasterization. */
1174 /**********************************************************************/
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",
1185 [13] = "Color mask",
1190 [18] = "Smooth polygon",
1191 [19] = "Smooth point",
1192 [20] = "point sprite coord origin",
1193 [21] = "depth/color drawing offset",
1198 getFallbackString(GLuint bit)
1205 return fallbackStrings[i];
1211 * Enable/disable a fallback flag.
1212 * \param bit one of INTEL_FALLBACK_x flags.
1215 intelFallback(struct intel_context *intel, GLbitfield bit, GLboolean mode)
1217 struct gl_context *ctx = &intel->ctx;
1218 TNLcontext *tnl = TNL_CONTEXT(ctx);
1219 const GLbitfield oldfallback = intel->Fallback;
1222 intel->Fallback |= bit;
1223 if (oldfallback == 0) {
1224 assert(!intel->tnl_pipeline_running);
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;
1235 intel->Fallback &= ~bit;
1236 if (oldfallback == bit) {
1237 assert(!intel->tnl_pipeline_running);
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;
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);
1256 intel->NewGLState |= _INTEL_NEW_RENDERSTATE;
1267 /**********************************************************************/
1268 /* Initialization. */
1269 /**********************************************************************/
1273 intelInitTriFuncs(struct gl_context * ctx)
1275 TNLcontext *tnl = TNL_CONTEXT(ctx);
1276 static int firsttime = 1;
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;