1 /**************************************************************************
3 * Copyright 2007 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 **************************************************************************/
29 * \brief Primitive rasterization/rendering (points, lines, triangles)
31 * \author Keith Whitwell <keith@tungstengraphics.com>
35 #include "lp_context.h"
36 #include "lp_prim_setup.h"
40 #include "draw/draw_context.h"
41 #include "draw/draw_private.h"
42 #include "draw/draw_vertex.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "pipe/p_thread.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "lp_bld_debug.h"
48 #include "lp_tile_cache.h"
49 #include "lp_tile_soa.h"
59 float dx
; /**< X(v1) - X(v0), used only during setup */
60 float dy
; /**< Y(v1) - Y(v0), used only during setup */
61 float dxdy
; /**< dx/dy */
62 float sx
, sy
; /**< first sample point coord */
63 int lines
; /**< number of lines on this edge */
71 * Triangle setup info (derived from draw_stage).
72 * Also used for line drawing (taking some liberties).
74 struct setup_context
{
75 struct llvmpipe_context
*llvmpipe
;
77 /* Vertices are just an array of floats making up each attribute in
78 * turn. Currently fixed at 4 floats, but should change in time.
79 * Codegen will help cope with this.
81 const float (*vmax
)[4];
82 const float (*vmid
)[4];
83 const float (*vmin
)[4];
84 const float (*vprovoke
)[4];
93 struct quad_header quad
[MAX_QUADS
];
94 struct quad_header
*quad_ptrs
[MAX_QUADS
];
97 struct quad_interp_coef coef
;
100 int left
[2]; /**< [0] = row0, [1] = row1 */
106 uint numFragsEmitted
; /**< per primitive */
107 uint numFragsWritten
; /**< per primitive */
110 unsigned winding
; /* which winding to cull */
116 * Execute fragment shader for the four fragments in the quad.
119 shade_quads(struct llvmpipe_context
*llvmpipe
,
120 struct quad_header
*quads
[],
123 struct lp_fragment_shader
*fs
= llvmpipe
->fs
;
124 struct quad_header
*quad
= quads
[0];
125 const unsigned x
= quad
->input
.x0
;
126 const unsigned y
= quad
->input
.y0
;
127 uint8_t *tile
= lp_get_cached_tile(llvmpipe
->cbuf_cache
[0], x
, y
);
130 uint32_t ALIGN16_ATTRIB mask
[4][NUM_CHANNELS
];
139 assert(nr
* QUAD_SIZE
== TILE_VECTOR_HEIGHT
* TILE_VECTOR_WIDTH
);
140 assert(x
% TILE_VECTOR_WIDTH
== 0);
141 assert(y
% TILE_VECTOR_HEIGHT
== 0);
142 for (q
= 0; q
< nr
; ++q
) {
143 assert(quads
[q
]->input
.x0
== x
+ q
*2);
144 assert(quads
[q
]->input
.y0
== y
);
148 for (q
= 0; q
< 4; ++q
)
149 for (chan_index
= 0; chan_index
< NUM_CHANNELS
; ++chan_index
)
150 mask
[q
][chan_index
] = quads
[q
]->inout
.mask
& (1 << chan_index
) ? ~0 : 0;
153 color
= &TILE_PIXEL(tile
, x
& (TILE_SIZE
-1), y
& (TILE_SIZE
-1), 0);
156 if(llvmpipe
->zsbuf_map
) {
157 assert((x
% 2) == 0);
158 assert((y
% 2) == 0);
159 depth
= llvmpipe
->zsbuf_map
+
160 y
*llvmpipe
->zsbuf_transfer
->stride
+
161 2*x
*llvmpipe
->zsbuf_transfer
->block
.size
;
166 /* TODO: blend color */
168 /* XXX: This will most likely fail on 32bit x86 without -mstackrealign */
169 assert(lp_check_alignment(mask
, 16));
171 assert(lp_check_alignment(depth
, 16));
172 assert(lp_check_alignment(color
, 16));
173 assert(lp_check_alignment(llvmpipe
->jit_context
.blend_color
, 16));
176 fs
->current
->jit_function( &llvmpipe
->jit_context
,
190 * Do triangle cull test using tri determinant (sign indicates orientation)
191 * \return true if triangle is to be culled.
193 static INLINE boolean
194 cull_tri(const struct setup_context
*setup
, float det
)
197 /* if (det < 0 then Z points toward camera and triangle is
198 * counter-clockwise winding.
200 unsigned winding
= (det
< 0) ? PIPE_WINDING_CCW
: PIPE_WINDING_CW
;
202 if ((winding
& setup
->winding
) == 0)
214 * Clip setup->quad against the scissor/surface bounds.
217 quad_clip( struct setup_context
*setup
, struct quad_header
*quad
)
219 const struct pipe_scissor_state
*cliprect
= &setup
->llvmpipe
->cliprect
;
220 const int minx
= (int) cliprect
->minx
;
221 const int maxx
= (int) cliprect
->maxx
;
222 const int miny
= (int) cliprect
->miny
;
223 const int maxy
= (int) cliprect
->maxy
;
225 if (quad
->input
.x0
>= maxx
||
226 quad
->input
.y0
>= maxy
||
227 quad
->input
.x0
+ 1 < minx
||
228 quad
->input
.y0
+ 1 < miny
) {
229 /* totally clipped */
230 quad
->inout
.mask
= 0x0;
233 if (quad
->input
.x0
< minx
)
234 quad
->inout
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
235 if (quad
->input
.y0
< miny
)
236 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
237 if (quad
->input
.x0
== maxx
- 1)
238 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
239 if (quad
->input
.y0
== maxy
- 1)
240 quad
->inout
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
246 * Given an X or Y coordinate, return the block/quad coordinate that it
249 static INLINE
int block( int x
)
254 static INLINE
int block_x( int x
)
256 return x
& ~(TILE_VECTOR_WIDTH
- 1);
261 * Emit a quad (pass to next stage) with clipping.
264 clip_emit_quad( struct setup_context
*setup
, struct quad_header
*quad
)
266 quad_clip( setup
, quad
);
268 if (quad
->inout
.mask
) {
269 struct llvmpipe_context
*lp
= setup
->llvmpipe
;
272 /* XXX: The blender expects 4 quads. This is far from efficient, but
273 * until we codegenerate single-quad variants of the fragment pipeline
274 * we need this hack. */
275 const unsigned nr_quads
= TILE_VECTOR_HEIGHT
*TILE_VECTOR_WIDTH
/QUAD_SIZE
;
276 struct quad_header quads
[nr_quads
];
277 struct quad_header
*quad_ptrs
[nr_quads
];
278 int x0
= block_x(quad
->input
.x0
);
281 for(i
= 0; i
< nr_quads
; ++i
) {
283 if(x
== quad
->input
.x0
)
284 memcpy(&quads
[i
], quad
, sizeof quads
[i
]);
286 memset(&quads
[i
], 0, sizeof quads
[i
]);
287 quads
[i
].input
.x0
= x
;
288 quads
[i
].input
.y0
= quad
->input
.y0
;
289 quads
[i
].coef
= quad
->coef
;
291 quad_ptrs
[i
] = &quads
[i
];
294 shade_quads( lp
, quad_ptrs
, nr_quads
);
296 shade_quads( lp
, &quad
, 1 );
303 * Render a horizontal span of quads
305 static void flush_spans( struct setup_context
*setup
)
307 const int step
= TILE_VECTOR_WIDTH
;
308 const int xleft0
= setup
->span
.left
[0];
309 const int xleft1
= setup
->span
.left
[1];
310 const int xright0
= setup
->span
.right
[0];
311 const int xright1
= setup
->span
.right
[1];
314 int minleft
= block_x(MIN2(xleft0
, xleft1
));
315 int maxright
= MAX2(xright0
, xright1
);
318 for (x
= minleft
; x
< maxright
; x
+= step
) {
319 unsigned skip_left0
= CLAMP(xleft0
- x
, 0, step
);
320 unsigned skip_left1
= CLAMP(xleft1
- x
, 0, step
);
321 unsigned skip_right0
= CLAMP(x
+ step
- xright0
, 0, step
);
322 unsigned skip_right1
= CLAMP(x
+ step
- xright1
, 0, step
);
324 const unsigned nr_quads
= TILE_VECTOR_HEIGHT
*TILE_VECTOR_WIDTH
/QUAD_SIZE
;
327 unsigned skipmask_left0
= (1U << skip_left0
) - 1U;
328 unsigned skipmask_left1
= (1U << skip_left1
) - 1U;
330 /* These calculations fail when step == 32 and skip_right == 0.
332 unsigned skipmask_right0
= ~0U << (unsigned)(step
- skip_right0
);
333 unsigned skipmask_right1
= ~0U << (unsigned)(step
- skip_right1
);
335 unsigned mask0
= ~skipmask_left0
& ~skipmask_right0
;
336 unsigned mask1
= ~skipmask_left1
& ~skipmask_right1
;
339 for(q
= 0; q
< nr_quads
; ++q
) {
340 unsigned quadmask
= (mask0
& 3) | ((mask1
& 3) << 2);
341 setup
->quad
[q
].input
.x0
= lx
;
342 setup
->quad
[q
].input
.y0
= setup
->span
.y
;
343 setup
->quad
[q
].inout
.mask
= quadmask
;
344 setup
->quad_ptrs
[q
] = &setup
->quad
[q
];
349 assert(!(mask0
| mask1
));
351 shade_quads(setup
->llvmpipe
, setup
->quad_ptrs
, nr_quads
);
357 setup
->span
.right
[0] = 0;
358 setup
->span
.right
[1] = 0;
359 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
360 setup
->span
.left
[1] = 1000000; /* greater than right[1] */
365 static void print_vertex(const struct setup_context
*setup
,
369 debug_printf(" Vertex: (%p)\n", v
);
370 for (i
= 0; i
< setup
->quad
[0].nr_attrs
; i
++) {
371 debug_printf(" %d: %f %f %f %f\n", i
,
372 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
373 if (util_is_inf_or_nan(v
[i
][0])) {
374 debug_printf(" NaN!\n");
381 * Sort the vertices from top to bottom order, setting up the triangle
382 * edge fields (ebot, emaj, etop).
383 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
385 static boolean
setup_sort_vertices( struct setup_context
*setup
,
387 const float (*v0
)[4],
388 const float (*v1
)[4],
389 const float (*v2
)[4] )
391 setup
->vprovoke
= v2
;
393 /* determine bottom to top order of vertices */
440 setup
->ebot
.dx
= setup
->vmid
[0][0] - setup
->vmin
[0][0];
441 setup
->ebot
.dy
= setup
->vmid
[0][1] - setup
->vmin
[0][1];
442 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
443 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
444 setup
->etop
.dx
= setup
->vmax
[0][0] - setup
->vmid
[0][0];
445 setup
->etop
.dy
= setup
->vmax
[0][1] - setup
->vmid
[0][1];
448 * Compute triangle's area. Use 1/area to compute partial
449 * derivatives of attributes later.
451 * The area will be the same as prim->det, but the sign may be
452 * different depending on how the vertices get sorted above.
454 * To determine whether the primitive is front or back facing we
455 * use the prim->det value because its sign is correct.
458 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
459 setup
->ebot
.dx
* setup
->emaj
.dy
);
461 setup
->oneoverarea
= 1.0f
/ area
;
464 debug_printf("%s one-over-area %f area %f det %f\n",
465 __FUNCTION__, setup->oneoverarea, area, det );
467 if (util_is_inf_or_nan(setup
->oneoverarea
))
471 /* We need to know if this is a front or back-facing triangle for:
472 * - the GLSL gl_FrontFacing fragment attribute (bool)
473 * - two-sided stencil test
477 (setup
->llvmpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
));
484 * Compute a0, dadx and dady for a linearly interpolated coefficient,
487 static void tri_pos_coeff( struct setup_context
*setup
,
488 uint vertSlot
, unsigned i
)
490 float botda
= setup
->vmid
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
491 float majda
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
492 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
493 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
494 float dadx
= a
* setup
->oneoverarea
;
495 float dady
= b
* setup
->oneoverarea
;
499 setup
->coef
.dadx
[0][i
] = dadx
;
500 setup
->coef
.dady
[0][i
] = dady
;
502 /* calculate a0 as the value which would be sampled for the
503 * fragment at (0,0), taking into account that we want to sample at
504 * pixel centers, in other words (0.5, 0.5).
506 * this is neat but unfortunately not a good way to do things for
507 * triangles with very large values of dadx or dady as it will
508 * result in the subtraction and re-addition from a0 of a very
509 * large number, which means we'll end up loosing a lot of the
510 * fractional bits and precision from a0. the way to fix this is
511 * to define a0 as the sample at a pixel center somewhere near vmin
512 * instead - i'll switch to this later.
514 setup
->coef
.a0
[0][i
] = (setup
->vmin
[vertSlot
][i
] -
515 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
516 dady
* (setup
->vmin
[0][1] - 0.5f
)));
519 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
521 setup->coef[slot].a0[i],
522 setup->coef[slot].dadx[i],
523 setup->coef[slot].dady[i]);
529 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
530 * The value value comes from vertex[slot][i].
531 * The result will be put into setup->coef[slot].a0[i].
532 * \param slot which attribute slot
533 * \param i which component of the slot (0..3)
535 static void const_pos_coeff( struct setup_context
*setup
,
536 uint vertSlot
, unsigned i
)
538 setup
->coef
.dadx
[0][i
] = 0;
539 setup
->coef
.dady
[0][i
] = 0;
541 /* need provoking vertex info!
543 setup
->coef
.a0
[0][i
] = setup
->vprovoke
[vertSlot
][i
];
548 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
549 * The value value comes from vertex[slot][i].
550 * The result will be put into setup->coef[slot].a0[i].
551 * \param slot which attribute slot
552 * \param i which component of the slot (0..3)
554 static void const_coeff( struct setup_context
*setup
,
559 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
560 setup
->coef
.dadx
[1 + attrib
][i
] = 0;
561 setup
->coef
.dady
[1 + attrib
][i
] = 0;
563 /* need provoking vertex info!
565 setup
->coef
.a0
[1 + attrib
][i
] = setup
->vprovoke
[vertSlot
][i
];
571 * Compute a0, dadx and dady for a linearly interpolated coefficient,
574 static void tri_linear_coeff( struct setup_context
*setup
,
579 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
580 float botda
= setup
->vmid
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
581 float majda
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
582 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
583 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
584 float dadx
= a
* setup
->oneoverarea
;
585 float dady
= b
* setup
->oneoverarea
;
589 setup
->coef
.dadx
[1 + attrib
][i
] = dadx
;
590 setup
->coef
.dady
[1 + attrib
][i
] = dady
;
592 /* calculate a0 as the value which would be sampled for the
593 * fragment at (0,0), taking into account that we want to sample at
594 * pixel centers, in other words (0.5, 0.5).
596 * this is neat but unfortunately not a good way to do things for
597 * triangles with very large values of dadx or dady as it will
598 * result in the subtraction and re-addition from a0 of a very
599 * large number, which means we'll end up loosing a lot of the
600 * fractional bits and precision from a0. the way to fix this is
601 * to define a0 as the sample at a pixel center somewhere near vmin
602 * instead - i'll switch to this later.
604 setup
->coef
.a0
[1 + attrib
][i
] = (setup
->vmin
[vertSlot
][i
] -
605 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
606 dady
* (setup
->vmin
[0][1] - 0.5f
)));
609 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
611 setup->coef[slot].a0[i],
612 setup->coef[slot].dadx[i],
613 setup->coef[slot].dady[i]);
620 * Compute a0, dadx and dady for a perspective-corrected interpolant,
622 * We basically multiply the vertex value by 1/w before computing
623 * the plane coefficients (a0, dadx, dady).
624 * Later, when we compute the value at a particular fragment position we'll
625 * divide the interpolated value by the interpolated W at that fragment.
627 static void tri_persp_coeff( struct setup_context
*setup
,
632 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
633 /* premultiply by 1/w (v[0][3] is always W):
635 float mina
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
636 float mida
= setup
->vmid
[vertSlot
][i
] * setup
->vmid
[0][3];
637 float maxa
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
638 float botda
= mida
- mina
;
639 float majda
= maxa
- mina
;
640 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
641 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
642 float dadx
= a
* setup
->oneoverarea
;
643 float dady
= b
* setup
->oneoverarea
;
646 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
647 setup->vmin[vertSlot][i],
648 setup->vmid[vertSlot][i],
649 setup->vmax[vertSlot][i]
654 setup
->coef
.dadx
[1 + attrib
][i
] = dadx
;
655 setup
->coef
.dady
[1 + attrib
][i
] = dady
;
656 setup
->coef
.a0
[1 + attrib
][i
] = (mina
-
657 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
658 dady
* (setup
->vmin
[0][1] - 0.5f
)));
664 * Special coefficient setup for gl_FragCoord.
665 * X and Y are trivial, though Y has to be inverted for OpenGL.
666 * Z and W are copied from posCoef which should have already been computed.
667 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
670 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
673 setup
->coef
.a0
[1 + slot
][0] = 0;
674 setup
->coef
.dadx
[1 + slot
][0] = 1.0;
675 setup
->coef
.dady
[1 + slot
][0] = 0.0;
677 setup
->coef
.a0
[1 + slot
][1] = 0.0;
678 setup
->coef
.dadx
[1 + slot
][1] = 0.0;
679 setup
->coef
.dady
[1 + slot
][1] = 1.0;
681 setup
->coef
.a0
[1 + slot
][2] = setup
->coef
.a0
[0][2];
682 setup
->coef
.dadx
[1 + slot
][2] = setup
->coef
.dadx
[0][2];
683 setup
->coef
.dady
[1 + slot
][2] = setup
->coef
.dady
[0][2];
685 setup
->coef
.a0
[1 + slot
][3] = setup
->coef
.a0
[0][3];
686 setup
->coef
.dadx
[1 + slot
][3] = setup
->coef
.dadx
[0][3];
687 setup
->coef
.dady
[1 + slot
][3] = setup
->coef
.dady
[0][3];
693 * Compute the setup->coef[] array dadx, dady, a0 values.
694 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
696 static void setup_tri_coefficients( struct setup_context
*setup
)
698 struct llvmpipe_context
*llvmpipe
= setup
->llvmpipe
;
699 const struct lp_fragment_shader
*lpfs
= llvmpipe
->fs
;
700 const struct vertex_info
*vinfo
= llvmpipe_get_vertex_info(llvmpipe
);
703 /* z and w are done by linear interpolation:
705 tri_pos_coeff(setup
, 0, 2);
706 tri_pos_coeff(setup
, 0, 3);
708 /* setup interpolation for all the remaining attributes:
710 for (fragSlot
= 0; fragSlot
< lpfs
->info
.num_inputs
; fragSlot
++) {
711 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
713 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
714 case INTERP_CONSTANT
:
715 const_coeff(setup
, fragSlot
, vertSlot
);
718 tri_linear_coeff(setup
, fragSlot
, vertSlot
);
720 case INTERP_PERSPECTIVE
:
721 tri_persp_coeff(setup
, fragSlot
, vertSlot
);
724 setup_fragcoord_coeff(setup
, fragSlot
);
730 if (lpfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
731 setup
->coef
.a0
[1 + fragSlot
][0] = 1.0f
- setup
->facing
;
732 setup
->coef
.dadx
[1 + fragSlot
][0] = 0.0;
733 setup
->coef
.dady
[1 + fragSlot
][0] = 0.0;
740 static void setup_tri_edges( struct setup_context
*setup
)
742 float vmin_x
= setup
->vmin
[0][0] + 0.5f
;
743 float vmid_x
= setup
->vmid
[0][0] + 0.5f
;
745 float vmin_y
= setup
->vmin
[0][1] - 0.5f
;
746 float vmid_y
= setup
->vmid
[0][1] - 0.5f
;
747 float vmax_y
= setup
->vmax
[0][1] - 0.5f
;
749 setup
->emaj
.sy
= ceilf(vmin_y
);
750 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
751 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
752 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
754 setup
->etop
.sy
= ceilf(vmid_y
);
755 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
756 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
757 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
759 setup
->ebot
.sy
= ceilf(vmin_y
);
760 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
761 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
762 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
767 * Render the upper or lower half of a triangle.
768 * Scissoring/cliprect is applied here too.
770 static void subtriangle( struct setup_context
*setup
,
775 const struct pipe_scissor_state
*cliprect
= &setup
->llvmpipe
->cliprect
;
776 const int minx
= (int) cliprect
->minx
;
777 const int maxx
= (int) cliprect
->maxx
;
778 const int miny
= (int) cliprect
->miny
;
779 const int maxy
= (int) cliprect
->maxy
;
780 int y
, start_y
, finish_y
;
781 int sy
= (int)eleft
->sy
;
783 assert((int)eleft
->sy
== (int) eright
->sy
);
785 /* clip top/bottom */
790 finish_y
= sy
+ lines
;
798 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
801 for (y
= start_y
; y
< finish_y
; y
++) {
803 /* avoid accumulating adds as floats don't have the precision to
804 * accurately iterate large triangle edges that way. luckily we
805 * can just multiply these days.
807 * this is all drowned out by the attribute interpolation anyway.
809 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
810 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
812 /* clip left/right */
820 if (block(_y
) != setup
->span
.y
) {
822 setup
->span
.y
= block(_y
);
825 setup
->span
.left
[_y
&1] = left
;
826 setup
->span
.right
[_y
&1] = right
;
831 /* save the values so that emaj can be restarted:
833 eleft
->sx
+= lines
* eleft
->dxdy
;
834 eright
->sx
+= lines
* eright
->dxdy
;
841 * Recalculate prim's determinant. This is needed as we don't have
842 * get this information through the vbuf_render interface & we must
846 calc_det( const float (*v0
)[4],
847 const float (*v1
)[4],
848 const float (*v2
)[4] )
850 /* edge vectors e = v0 - v2, f = v1 - v2 */
851 const float ex
= v0
[0][0] - v2
[0][0];
852 const float ey
= v0
[0][1] - v2
[0][1];
853 const float fx
= v1
[0][0] - v2
[0][0];
854 const float fy
= v1
[0][1] - v2
[0][1];
856 /* det = cross(e,f).z */
857 return ex
* fy
- ey
* fx
;
862 * Do setup for triangle rasterization, then render the triangle.
864 void llvmpipe_setup_tri( struct setup_context
*setup
,
865 const float (*v0
)[4],
866 const float (*v1
)[4],
867 const float (*v2
)[4] )
872 debug_printf("Setup triangle:\n");
873 print_vertex(setup
, v0
);
874 print_vertex(setup
, v1
);
875 print_vertex(setup
, v2
);
878 if (setup
->llvmpipe
->no_rast
)
881 det
= calc_det(v0
, v1
, v2
);
883 debug_printf("%s\n", __FUNCTION__ );
887 setup
->numFragsEmitted
= 0;
888 setup
->numFragsWritten
= 0;
891 if (cull_tri( setup
, det
))
894 if (!setup_sort_vertices( setup
, det
, v0
, v1
, v2
))
896 setup_tri_coefficients( setup
);
897 setup_tri_edges( setup
);
899 assert(setup
->llvmpipe
->reduced_prim
== PIPE_PRIM_TRIANGLES
);
902 setup
->span
.right
[0] = 0;
903 setup
->span
.right
[1] = 0;
904 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
906 /* init_constant_attribs( setup ); */
908 if (setup
->oneoverarea
< 0.0) {
911 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
912 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
917 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
918 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
921 flush_spans( setup
);
924 printf("Tri: %u frags emitted, %u written\n",
925 setup
->numFragsEmitted
,
926 setup
->numFragsWritten
);
933 * Compute a0, dadx and dady for a linearly interpolated coefficient,
937 linear_pos_coeff(struct setup_context
*setup
,
938 uint vertSlot
, uint i
)
940 const float da
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
941 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
942 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
943 setup
->coef
.dadx
[0][i
] = dadx
;
944 setup
->coef
.dady
[0][i
] = dady
;
945 setup
->coef
.a0
[0][i
] = (setup
->vmin
[vertSlot
][i
] -
946 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
947 dady
* (setup
->vmin
[0][1] - 0.5f
)));
952 * Compute a0, dadx and dady for a linearly interpolated coefficient,
956 line_linear_coeff(struct setup_context
*setup
,
961 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
962 const float da
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
963 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
964 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
965 setup
->coef
.dadx
[1 + attrib
][i
] = dadx
;
966 setup
->coef
.dady
[1 + attrib
][i
] = dady
;
967 setup
->coef
.a0
[1 + attrib
][i
] = (setup
->vmin
[vertSlot
][i
] -
968 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
969 dady
* (setup
->vmin
[0][1] - 0.5f
)));
975 * Compute a0, dadx and dady for a perspective-corrected interpolant,
979 line_persp_coeff(struct setup_context
*setup
,
984 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
985 /* XXX double-check/verify this arithmetic */
986 const float a0
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
987 const float a1
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
988 const float da
= a1
- a0
;
989 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
990 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
991 setup
->coef
.dadx
[1 + attrib
][i
] = dadx
;
992 setup
->coef
.dady
[1 + attrib
][i
] = dady
;
993 setup
->coef
.a0
[1 + attrib
][i
] = (setup
->vmin
[vertSlot
][i
] -
994 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
995 dady
* (setup
->vmin
[0][1] - 0.5f
)));
1001 * Compute the setup->coef[] array dadx, dady, a0 values.
1002 * Must be called after setup->vmin,vmax are initialized.
1004 static INLINE boolean
1005 setup_line_coefficients(struct setup_context
*setup
,
1006 const float (*v0
)[4],
1007 const float (*v1
)[4])
1009 struct llvmpipe_context
*llvmpipe
= setup
->llvmpipe
;
1010 const struct lp_fragment_shader
*lpfs
= llvmpipe
->fs
;
1011 const struct vertex_info
*vinfo
= llvmpipe_get_vertex_info(llvmpipe
);
1015 /* use setup->vmin, vmax to point to vertices */
1016 if (llvmpipe
->rasterizer
->flatshade_first
)
1017 setup
->vprovoke
= v0
;
1019 setup
->vprovoke
= v1
;
1023 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
1024 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
1026 /* NOTE: this is not really area but something proportional to it */
1027 area
= setup
->emaj
.dx
* setup
->emaj
.dx
+ setup
->emaj
.dy
* setup
->emaj
.dy
;
1028 if (area
== 0.0f
|| util_is_inf_or_nan(area
))
1030 setup
->oneoverarea
= 1.0f
/ area
;
1032 /* z and w are done by linear interpolation:
1034 linear_pos_coeff(setup
, 0, 2);
1035 linear_pos_coeff(setup
, 0, 3);
1037 /* setup interpolation for all the remaining attributes:
1039 for (fragSlot
= 0; fragSlot
< lpfs
->info
.num_inputs
; fragSlot
++) {
1040 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1042 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1043 case INTERP_CONSTANT
:
1044 const_coeff(setup
, fragSlot
, vertSlot
);
1047 line_linear_coeff(setup
, fragSlot
, vertSlot
);
1049 case INTERP_PERSPECTIVE
:
1050 line_persp_coeff(setup
, fragSlot
, vertSlot
);
1053 setup_fragcoord_coeff(setup
, fragSlot
);
1059 if (lpfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1060 setup
->coef
.a0
[1 + fragSlot
][0] = 1.0f
- setup
->facing
;
1061 setup
->coef
.dadx
[1 + fragSlot
][0] = 0.0;
1062 setup
->coef
.dady
[1 + fragSlot
][0] = 0.0;
1070 * Plot a pixel in a line segment.
1073 plot(struct setup_context
*setup
, int x
, int y
)
1075 const int iy
= y
& 1;
1076 const int ix
= x
& 1;
1077 const int quadX
= x
- ix
;
1078 const int quadY
= y
- iy
;
1079 const int mask
= (1 << ix
) << (2 * iy
);
1081 if (quadX
!= setup
->quad
[0].input
.x0
||
1082 quadY
!= setup
->quad
[0].input
.y0
)
1084 /* flush prev quad, start new quad */
1086 if (setup
->quad
[0].input
.x0
!= -1)
1087 clip_emit_quad( setup
, &setup
->quad
[0] );
1089 setup
->quad
[0].input
.x0
= quadX
;
1090 setup
->quad
[0].input
.y0
= quadY
;
1091 setup
->quad
[0].inout
.mask
= 0x0;
1094 setup
->quad
[0].inout
.mask
|= mask
;
1099 * Do setup for line rasterization, then render the line.
1100 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
1101 * to handle stippling and wide lines.
1104 llvmpipe_setup_line(struct setup_context
*setup
,
1105 const float (*v0
)[4],
1106 const float (*v1
)[4])
1108 int x0
= (int) v0
[0][0];
1109 int x1
= (int) v1
[0][0];
1110 int y0
= (int) v0
[0][1];
1111 int y1
= (int) v1
[0][1];
1117 debug_printf("Setup line:\n");
1118 print_vertex(setup
, v0
);
1119 print_vertex(setup
, v1
);
1122 if (setup
->llvmpipe
->no_rast
)
1125 if (dx
== 0 && dy
== 0)
1128 if (!setup_line_coefficients(setup
, v0
, v1
))
1131 assert(v0
[0][0] < 1.0e9
);
1132 assert(v0
[0][1] < 1.0e9
);
1133 assert(v1
[0][0] < 1.0e9
);
1134 assert(v1
[0][1] < 1.0e9
);
1137 dx
= -dx
; /* make positive */
1145 dy
= -dy
; /* make positive */
1154 assert(setup
->llvmpipe
->reduced_prim
== PIPE_PRIM_LINES
);
1156 setup
->quad
[0].input
.x0
= setup
->quad
[0].input
.y0
= -1;
1157 setup
->quad
[0].inout
.mask
= 0x0;
1159 /* XXX temporary: set coverage to 1.0 so the line appears
1160 * if AA mode happens to be enabled.
1162 setup
->quad
[0].input
.coverage
[0] =
1163 setup
->quad
[0].input
.coverage
[1] =
1164 setup
->quad
[0].input
.coverage
[2] =
1165 setup
->quad
[0].input
.coverage
[3] = 1.0;
1168 /*** X-major line ***/
1170 const int errorInc
= dy
+ dy
;
1171 int error
= errorInc
- dx
;
1172 const int errorDec
= error
- dx
;
1174 for (i
= 0; i
< dx
; i
++) {
1175 plot(setup
, x0
, y0
);
1188 /*** Y-major line ***/
1190 const int errorInc
= dx
+ dx
;
1191 int error
= errorInc
- dy
;
1192 const int errorDec
= error
- dy
;
1194 for (i
= 0; i
< dy
; i
++) {
1195 plot(setup
, x0
, y0
);
1208 /* draw final quad */
1209 if (setup
->quad
[0].inout
.mask
) {
1210 clip_emit_quad( setup
, &setup
->quad
[0] );
1216 point_persp_coeff(struct setup_context
*setup
,
1217 const float (*vert
)[4],
1222 for(i
= 0; i
< NUM_CHANNELS
; ++i
) {
1223 setup
->coef
.dadx
[1 + attrib
][i
] = 0.0F
;
1224 setup
->coef
.dady
[1 + attrib
][i
] = 0.0F
;
1225 setup
->coef
.a0
[1 + attrib
][i
] = vert
[vertSlot
][i
] * vert
[0][3];
1231 * Do setup for point rasterization, then render the point.
1232 * Round or square points...
1233 * XXX could optimize a lot for 1-pixel points.
1236 llvmpipe_setup_point( struct setup_context
*setup
,
1237 const float (*v0
)[4] )
1239 struct llvmpipe_context
*llvmpipe
= setup
->llvmpipe
;
1240 const struct lp_fragment_shader
*lpfs
= llvmpipe
->fs
;
1241 const int sizeAttr
= setup
->llvmpipe
->psize_slot
;
1243 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1244 : setup
->llvmpipe
->rasterizer
->point_size
;
1245 const float halfSize
= 0.5F
* size
;
1246 const boolean round
= (boolean
) setup
->llvmpipe
->rasterizer
->point_smooth
;
1247 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1248 const float y
= v0
[0][1];
1249 const struct vertex_info
*vinfo
= llvmpipe_get_vertex_info(llvmpipe
);
1253 debug_printf("Setup point:\n");
1254 print_vertex(setup
, v0
);
1257 if (llvmpipe
->no_rast
)
1260 assert(setup
->llvmpipe
->reduced_prim
== PIPE_PRIM_POINTS
);
1262 /* For points, all interpolants are constant-valued.
1263 * However, for point sprites, we'll need to setup texcoords appropriately.
1264 * XXX: which coefficients are the texcoords???
1265 * We may do point sprites as textured quads...
1267 * KW: We don't know which coefficients are texcoords - ultimately
1268 * the choice of what interpolation mode to use for each attribute
1269 * should be determined by the fragment program, using
1270 * per-attribute declaration statements that include interpolation
1271 * mode as a parameter. So either the fragment program will have
1272 * to be adjusted for pointsprite vs normal point behaviour, or
1273 * otherwise a special interpolation mode will have to be defined
1274 * which matches the required behaviour for point sprites. But -
1275 * the latter is not a feature of normal hardware, and as such
1276 * probably should be ruled out on that basis.
1278 setup
->vprovoke
= v0
;
1281 const_pos_coeff(setup
, 0, 2);
1282 const_pos_coeff(setup
, 0, 3);
1284 for (fragSlot
= 0; fragSlot
< lpfs
->info
.num_inputs
; fragSlot
++) {
1285 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1287 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1288 case INTERP_CONSTANT
:
1291 const_coeff(setup
, fragSlot
, vertSlot
);
1293 case INTERP_PERSPECTIVE
:
1294 point_persp_coeff(setup
, setup
->vprovoke
, fragSlot
, vertSlot
);
1297 setup_fragcoord_coeff(setup
, fragSlot
);
1303 if (lpfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1304 setup
->coef
.a0
[1 + fragSlot
][0] = 1.0f
- setup
->facing
;
1305 setup
->coef
.dadx
[1 + fragSlot
][0] = 0.0;
1306 setup
->coef
.dady
[1 + fragSlot
][0] = 0.0;
1311 if (halfSize
<= 0.5 && !round
) {
1312 /* special case for 1-pixel points */
1313 const int ix
= ((int) x
) & 1;
1314 const int iy
= ((int) y
) & 1;
1315 setup
->quad
[0].input
.x0
= (int) x
- ix
;
1316 setup
->quad
[0].input
.y0
= (int) y
- iy
;
1317 setup
->quad
[0].inout
.mask
= (1 << ix
) << (2 * iy
);
1318 clip_emit_quad( setup
, &setup
->quad
[0] );
1322 /* rounded points */
1323 const int ixmin
= block((int) (x
- halfSize
));
1324 const int ixmax
= block((int) (x
+ halfSize
));
1325 const int iymin
= block((int) (y
- halfSize
));
1326 const int iymax
= block((int) (y
+ halfSize
));
1327 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1328 const float rmax
= halfSize
+ 0.7071F
;
1329 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1330 const float rmax2
= rmax
* rmax
;
1331 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1334 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1335 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1336 float dx
, dy
, dist2
, cover
;
1338 setup
->quad
[0].inout
.mask
= 0x0;
1340 dx
= (ix
+ 0.5f
) - x
;
1341 dy
= (iy
+ 0.5f
) - y
;
1342 dist2
= dx
* dx
+ dy
* dy
;
1343 if (dist2
<= rmax2
) {
1344 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1345 setup
->quad
[0].input
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1346 setup
->quad
[0].inout
.mask
|= MASK_TOP_LEFT
;
1349 dx
= (ix
+ 1.5f
) - x
;
1350 dy
= (iy
+ 0.5f
) - y
;
1351 dist2
= dx
* dx
+ dy
* dy
;
1352 if (dist2
<= rmax2
) {
1353 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1354 setup
->quad
[0].input
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1355 setup
->quad
[0].inout
.mask
|= MASK_TOP_RIGHT
;
1358 dx
= (ix
+ 0.5f
) - x
;
1359 dy
= (iy
+ 1.5f
) - y
;
1360 dist2
= dx
* dx
+ dy
* dy
;
1361 if (dist2
<= rmax2
) {
1362 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1363 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1364 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_LEFT
;
1367 dx
= (ix
+ 1.5f
) - x
;
1368 dy
= (iy
+ 1.5f
) - y
;
1369 dist2
= dx
* dx
+ dy
* dy
;
1370 if (dist2
<= rmax2
) {
1371 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1372 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1373 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_RIGHT
;
1376 if (setup
->quad
[0].inout
.mask
) {
1377 setup
->quad
[0].input
.x0
= ix
;
1378 setup
->quad
[0].input
.y0
= iy
;
1379 clip_emit_quad( setup
, &setup
->quad
[0] );
1386 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1387 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1388 const int xmax
= xmin
+ (int) size
;
1389 const int ymax
= ymin
+ (int) size
;
1390 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1391 const int ixmin
= block(xmin
);
1392 const int ixmax
= block(xmax
- 1);
1393 const int iymin
= block(ymin
);
1394 const int iymax
= block(ymax
- 1);
1398 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1400 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1403 /* above the top edge */
1404 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1406 if (iy
+ 1 >= ymax
) {
1407 /* below the bottom edge */
1408 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1411 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1412 uint mask
= rowMask
;
1415 /* fragment is past left edge of point, turn off left bits */
1416 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1418 if (ix
+ 1 >= xmax
) {
1419 /* past the right edge */
1420 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1423 setup
->quad
[0].inout
.mask
= mask
;
1424 setup
->quad
[0].input
.x0
= ix
;
1425 setup
->quad
[0].input
.y0
= iy
;
1426 clip_emit_quad( setup
, &setup
->quad
[0] );
1433 void llvmpipe_setup_prepare( struct setup_context
*setup
)
1435 struct llvmpipe_context
*lp
= setup
->llvmpipe
;
1438 llvmpipe_update_derived(lp
);
1441 if (lp
->reduced_api_prim
== PIPE_PRIM_TRIANGLES
&&
1442 lp
->rasterizer
->fill_cw
== PIPE_POLYGON_MODE_FILL
&&
1443 lp
->rasterizer
->fill_ccw
== PIPE_POLYGON_MODE_FILL
) {
1444 /* we'll do culling */
1445 setup
->winding
= lp
->rasterizer
->cull_mode
;
1448 /* 'draw' will do culling */
1449 setup
->winding
= PIPE_WINDING_NONE
;
1455 void llvmpipe_setup_destroy_context( struct setup_context
*setup
)
1457 align_free( setup
);
1462 * Create a new primitive setup/render stage.
1464 struct setup_context
*llvmpipe_setup_create_context( struct llvmpipe_context
*llvmpipe
)
1466 struct setup_context
*setup
;
1469 setup
= align_malloc(sizeof(struct setup_context
), 16);
1473 memset(setup
, 0, sizeof *setup
);
1474 setup
->llvmpipe
= llvmpipe
;
1476 for (i
= 0; i
< MAX_QUADS
; i
++) {
1477 setup
->quad
[i
].coef
= &setup
->coef
;
1480 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
1481 setup
->span
.left
[1] = 1000000; /* greater than right[1] */