2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * This file contains "accelerated" point, line, and triangle functions.
28 * It should be fairly easy to write new special-purpose point, line or
29 * triangle functions and hook them into this module.
33 #include "glxheader.h"
34 #include "main/depth.h"
35 #include "main/macros.h"
36 #include "main/mtypes.h"
39 /* Internal swrast includes:
41 #include "swrast/s_depth.h"
42 #include "swrast/s_points.h"
43 #include "swrast/s_lines.h"
44 #include "swrast/s_context.h"
47 /**********************************************************************/
48 /*** Point rendering ***/
49 /**********************************************************************/
53 * Render an array of points into a pixmap, any pixel format.
56 /* XXX don't use this, it doesn't dither correctly */
57 static void draw_points_ANY_pixmap( GLcontext
*ctx
, const SWvertex
*vert
)
59 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
60 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
61 XMesaDrawable buffer
= xmesa
->xm_buffer
->buffer
;
62 XMesaGC gc
= xmesa
->xm_buffer
->gc
;
64 if (xmesa
->xm_visual
->mesa_visual
.RGBAflag
) {
66 const GLubyte
*color
= vert
->color
;
67 unsigned long pixel
= xmesa_color_to_pixel( xmesa
,
71 XMesaSetForeground( dpy
, gc
, pixel
);
72 x
= (GLint
) vert
->win
[0];
73 y
= YFLIP( xrb
, (GLint
) vert
->win
[1] );
74 XMesaDrawPoint( dpy
, buffer
, gc
, x
, y
);
77 /* Color index mode */
79 XMesaSetForeground( dpy
, gc
, vert
->index
);
80 x
= (GLint
) vert
->win
[0];
81 y
= YFLIP( xrb
, (GLint
) vert
->win
[1] );
82 XMesaDrawPoint( dpy
, buffer
, gc
, x
, y
);
88 /* Override the swrast point-selection function. Try to use one of
89 * our internal point functions, otherwise fall back to the standard
92 void xmesa_choose_point( GLcontext
*ctx
)
95 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
96 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
98 if (ctx
->RenderMode
== GL_RENDER
99 && ctx
->Point
.Size
== 1.0F
&& !ctx
->Point
.SmoothFlag
100 && swrast
->_RasterMask
== 0
101 && !ctx
->Texture
._EnabledUnits
102 && xmesa
->xm_buffer
->buffer
!= XIMAGE
) {
103 swrast
->Point
= draw_points_ANY_pixmap
;
106 _swrast_choose_point( ctx
);
109 _swrast_choose_point( ctx
);
115 /**********************************************************************/
116 /*** Line rendering ***/
117 /**********************************************************************/
123 #define GET_XRB(XRB) struct xmesa_renderbuffer *XRB = \
124 xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped)
128 * Draw a flat-shaded, PF_TRUECOLOR line into an XImage.
130 #define NAME flat_TRUECOLOR_line
132 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
134 const GLubyte *color = vert1->color; \
135 unsigned long pixel; \
136 PACK_TRUECOLOR( pixel, color[0], color[1], color[2] );
138 #define PLOT(X,Y) XMesaPutPixel(xrb->ximage, X, YFLIP(xrb, Y), pixel );
139 #include "swrast/s_linetemp.h"
144 * Draw a flat-shaded, PF_8A8B8G8R line into an XImage.
146 #define NAME flat_8A8B8G8R_line
149 const GLubyte *color = vert1->color; \
150 GLuint pixel = PACK_8A8B8G8R(color[0], color[1], color[2], color[3]);
151 #define PIXEL_TYPE GLuint
152 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
153 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X, Y)
155 #define PLOT(X,Y) *pixelPtr = pixel;
156 #include "swrast/s_linetemp.h"
161 * Draw a flat-shaded, PF_8A8R8G8B line into an XImage.
163 #define NAME flat_8A8R8G8B_line
166 const GLubyte *color = vert1->color; \
167 GLuint pixel = PACK_8A8R8G8B(color[0], color[1], color[2], color[3]);
168 #define PIXEL_TYPE GLuint
169 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
170 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X, Y)
172 #define PLOT(X,Y) *pixelPtr = pixel;
173 #include "swrast/s_linetemp.h"
178 * Draw a flat-shaded, PF_8R8G8B line into an XImage.
180 #define NAME flat_8R8G8B_line
183 const GLubyte *color = vert1->color; \
184 GLuint pixel = PACK_8R8G8B( color[0], color[1], color[2] );
185 #define PIXEL_TYPE GLuint
186 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
187 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X, Y)
189 #define PLOT(X,Y) *pixelPtr = pixel;
190 #include "swrast/s_linetemp.h"
195 * Draw a flat-shaded, PF_8R8G8B24 line into an XImage.
197 #define NAME flat_8R8G8B24_line
200 const GLubyte *color = vert1->color;
201 #define PIXEL_TYPE bgr_t
202 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
203 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR3(xrb, X, Y)
205 #define PLOT(X,Y) { \
206 pixelPtr->r = color[RCOMP]; \
207 pixelPtr->g = color[GCOMP]; \
208 pixelPtr->b = color[BCOMP]; \
210 #include "swrast/s_linetemp.h"
215 * Draw a flat-shaded, PF_5R6G5B line into an XImage.
217 #define NAME flat_5R6G5B_line
220 const GLubyte *color = vert1->color; \
221 GLushort pixel = PACK_5R6G5B( color[0], color[1], color[2] );
222 #define PIXEL_TYPE GLushort
223 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
224 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR2(xrb, X, Y)
226 #define PLOT(X,Y) *pixelPtr = pixel;
227 #include "swrast/s_linetemp.h"
232 * Draw a flat-shaded, PF_DITHER_5R6G5B line into an XImage.
234 #define NAME flat_DITHER_5R6G5B_line
237 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
238 const GLubyte *color = vert1->color;
239 #define PIXEL_TYPE GLushort
240 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
241 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR2(xrb, X, Y)
243 #define PLOT(X,Y) PACK_TRUEDITHER( *pixelPtr, X, Y, color[0], color[1], color[2] );
244 #include "swrast/s_linetemp.h"
250 * Draw a flat-shaded, PF_DITHER 8-bit line into an XImage.
252 #define NAME flat_DITHER8_line
255 const GLubyte *color = vert1->color; \
256 GLint r = color[0], g = color[1], b = color[2]; \
258 #define PIXEL_TYPE GLubyte
259 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
260 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X, Y)
262 #define PLOT(X,Y) *pixelPtr = DITHER(X,Y,r,g,b);
263 #include "swrast/s_linetemp.h"
268 * Draw a flat-shaded, PF_LOOKUP 8-bit line into an XImage.
270 #define NAME flat_LOOKUP8_line
273 const GLubyte *color = vert1->color; \
276 pixel = (GLubyte) LOOKUP( color[0], color[1], color[2] );
277 #define PIXEL_TYPE GLubyte
278 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
279 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X,Y)
281 #define PLOT(X,Y) *pixelPtr = pixel;
282 #include "swrast/s_linetemp.h"
287 * Draw a flat-shaded, PF_HPCR line into an XImage.
289 #define NAME flat_HPCR_line
292 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
293 const GLubyte *color = vert1->color; \
294 GLint r = color[0], g = color[1], b = color[2];
295 #define PIXEL_TYPE GLubyte
296 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
297 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X,Y)
299 #define PLOT(X,Y) *pixelPtr = (GLubyte) DITHER_HPCR(X,Y,r,g,b);
300 #include "swrast/s_linetemp.h"
306 * Draw a flat-shaded, Z-less, PF_TRUECOLOR line into an XImage.
308 #define NAME flat_TRUECOLOR_z_line
311 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
312 const GLubyte *color = vert1->color; \
313 unsigned long pixel; \
314 PACK_TRUECOLOR( pixel, color[0], color[1], color[2] );
316 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
321 XMesaPutPixel(xrb->ximage, X, YFLIP(xrb, Y), pixel); \
323 #include "swrast/s_linetemp.h"
328 * Draw a flat-shaded, Z-less, PF_8A8B8G8R line into an XImage.
330 #define NAME flat_8A8B8G8R_z_line
333 const GLubyte *color = vert1->color; \
334 GLuint pixel = PACK_8A8B8G8R(color[0], color[1], color[2], color[3]);
336 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
337 #define PIXEL_TYPE GLuint
338 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
339 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X,Y)
346 #include "swrast/s_linetemp.h"
351 * Draw a flat-shaded, Z-less, PF_8A8R8G8B line into an XImage.
353 #define NAME flat_8A8R8G8B_z_line
356 const GLubyte *color = vert1->color; \
357 GLuint pixel = PACK_8A8R8G8B(color[0], color[1], color[2], color[3]);
359 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
360 #define PIXEL_TYPE GLuint
361 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
362 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X,Y)
369 #include "swrast/s_linetemp.h"
374 * Draw a flat-shaded, Z-less, PF_8R8G8B line into an XImage.
376 #define NAME flat_8R8G8B_z_line
379 const GLubyte *color = vert1->color; \
380 GLuint pixel = PACK_8R8G8B( color[0], color[1], color[2] );
382 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
383 #define PIXEL_TYPE GLuint
384 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
385 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR4(xrb, X,Y)
392 #include "swrast/s_linetemp.h"
397 * Draw a flat-shaded, Z-less, PF_8R8G8B24 line into an XImage.
399 #define NAME flat_8R8G8B24_z_line
402 const GLubyte *color = vert1->color;
404 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
405 #define PIXEL_TYPE bgr_t
406 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
407 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR3(xrb, X,Y)
412 pixelPtr->r = color[RCOMP]; \
413 pixelPtr->g = color[GCOMP]; \
414 pixelPtr->b = color[BCOMP]; \
416 #include "swrast/s_linetemp.h"
421 * Draw a flat-shaded, Z-less, PF_5R6G5B line into an XImage.
423 #define NAME flat_5R6G5B_z_line
426 const GLubyte *color = vert1->color; \
427 GLushort pixel = PACK_5R6G5B( color[0], color[1], color[2] );
429 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
430 #define PIXEL_TYPE GLushort
431 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
432 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR2(xrb, X,Y)
439 #include "swrast/s_linetemp.h"
444 * Draw a flat-shaded, Z-less, PF_DITHER_5R6G5B line into an XImage.
446 #define NAME flat_DITHER_5R6G5B_z_line
449 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
450 const GLubyte *color = vert1->color;
452 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
453 #define PIXEL_TYPE GLushort
454 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
455 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR2(xrb, X,Y)
460 PACK_TRUEDITHER(*pixelPtr, X, Y, color[0], color[1], color[2]); \
462 #include "swrast/s_linetemp.h"
467 * Draw a flat-shaded, Z-less, PF_DITHER 8-bit line into an XImage.
469 #define NAME flat_DITHER8_z_line
472 const GLubyte *color = vert1->color; \
473 GLint r = color[0], g = color[1], b = color[2]; \
476 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
477 #define PIXEL_TYPE GLubyte
478 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
479 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X,Y)
484 *pixelPtr = (GLubyte) DITHER( X, Y, r, g, b); \
486 #include "swrast/s_linetemp.h"
491 * Draw a flat-shaded, Z-less, PF_LOOKUP 8-bit line into an XImage.
493 #define NAME flat_LOOKUP8_z_line
496 const GLubyte *color = vert1->color; \
499 pixel = (GLubyte) LOOKUP( color[0], color[1], color[2] );
501 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
502 #define PIXEL_TYPE GLubyte
503 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
504 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X,Y)
511 #include "swrast/s_linetemp.h"
516 * Draw a flat-shaded, Z-less, PF_HPCR line into an XImage.
518 #define NAME flat_HPCR_z_line
521 XMesaContext xmesa = XMESA_CONTEXT(ctx); \
522 const GLubyte *color = vert1->color; \
523 GLint r = color[0], g = color[1], b = color[2];
525 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
526 #define PIXEL_TYPE GLubyte
527 #define BYTES_PER_ROW (xrb->ximage->bytes_per_line)
528 #define PIXEL_ADDRESS(X,Y) PIXEL_ADDR1(xrb, X,Y)
533 *pixelPtr = (GLubyte) DITHER_HPCR( X, Y, r, g, b); \
535 #include "swrast/s_linetemp.h"
540 #ifndef XFree86Server
542 * Draw fast, XOR line with XDrawLine in front color buffer.
543 * WARNING: this isn't fully OpenGL conformant because different pixels
544 * will be hit versus using the other line functions.
545 * Don't use the code in X server GLcore module since we need a wrapper
546 * for the XSetLineAttributes() function call.
549 xor_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
551 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
552 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
553 XMesaGC gc
= xmesa
->xm_buffer
->gc
;
555 unsigned long pixel
= xmesa_color_to_pixel(ctx
,
556 vert1
->color
[0], vert1
->color
[1],
557 vert1
->color
[2], vert1
->color
[3],
559 int x0
= (GLint
) vert0
->attrib
[FRAG_ATTRIB_WPOS
][0];
560 int y0
= YFLIP(xrb
, (GLint
) vert0
->attrib
[FRAG_ATTRIB_WPOS
][1]);
561 int x1
= (GLint
) vert1
->attrib
[FRAG_ATTRIB_WPOS
][0];
562 int y1
= YFLIP(xrb
, (GLint
) vert1
->attrib
[FRAG_ATTRIB_WPOS
][1]);
563 XMesaSetForeground(dpy
, gc
, pixel
);
564 XMesaSetFunction(dpy
, gc
, GXxor
);
565 XSetLineAttributes(dpy
, gc
, (int) ctx
->Line
.Width
,
566 LineSolid
, CapButt
, JoinMiter
);
567 XDrawLine(dpy
, xrb
->pixmap
, gc
, x0
, y0
, x1
, y1
);
568 XMesaSetFunction(dpy
, gc
, GXcopy
); /* this gc is used elsewhere */
570 #endif /* XFree86Server */
573 #endif /* CHAN_BITS == 8 */
577 * Return pointer to line drawing function, or NULL if we should use a
580 static swrast_line_func
581 get_line_func(GLcontext
*ctx
)
584 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
585 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
586 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
587 const int depth
= GET_VISUAL_DEPTH(xmesa
->xm_visual
);
588 const struct xmesa_renderbuffer
*xrb
;
590 if ((ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[0] != BUFFER_BIT_FRONT_LEFT
) &&
591 (ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[0] != BUFFER_BIT_BACK_LEFT
))
592 return (swrast_line_func
) NULL
;
593 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_line_func
) NULL
;
594 if (ctx
->Line
.SmoothFlag
) return (swrast_line_func
) NULL
;
595 if (ctx
->Texture
._EnabledUnits
) return (swrast_line_func
) NULL
;
596 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return (swrast_line_func
) NULL
;
597 if (ctx
->Line
.StippleFlag
) return (swrast_line_func
) NULL
;
598 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) return (swrast_line_func
) NULL
;
599 if (xmbuf
->swAlpha
) return (swrast_line_func
) NULL
;
601 xrb
= xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
604 && swrast
->_RasterMask
==DEPTH_BIT
605 && ctx
->Depth
.Func
==GL_LESS
606 && ctx
->Depth
.Mask
==GL_TRUE
607 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
608 && ctx
->Line
.Width
==1.0F
) {
609 switch (xmesa
->pixelformat
) {
611 return flat_TRUECOLOR_z_line
;
613 return flat_8A8B8G8R_z_line
;
615 return flat_8A8R8G8B_z_line
;
617 return flat_8R8G8B_z_line
;
619 return flat_8R8G8B24_z_line
;
621 return flat_5R6G5B_z_line
;
622 case PF_Dither_5R6G5B
:
623 return flat_DITHER_5R6G5B_z_line
;
625 return (depth
==8) ? flat_DITHER8_z_line
: (swrast_line_func
) NULL
;
627 return (depth
==8) ? flat_LOOKUP8_z_line
: (swrast_line_func
) NULL
;
629 return flat_HPCR_z_line
;
631 return (swrast_line_func
)NULL
;
635 && swrast
->_RasterMask
==0
636 && ctx
->Line
.Width
==1.0F
) {
637 switch (xmesa
->pixelformat
) {
639 return flat_TRUECOLOR_line
;
641 return flat_8A8B8G8R_line
;
643 return flat_8A8R8G8B_line
;
645 return flat_8R8G8B_line
;
647 return flat_8R8G8B24_line
;
649 return flat_5R6G5B_line
;
650 case PF_Dither_5R6G5B
:
651 return flat_DITHER_5R6G5B_line
;
653 return (depth
==8) ? flat_DITHER8_line
: (swrast_line_func
) NULL
;
655 return (depth
==8) ? flat_LOOKUP8_line
: (swrast_line_func
) NULL
;
657 return flat_HPCR_line
;
659 return (swrast_line_func
)NULL
;
663 #ifndef XFree86Server
664 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1
665 && ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
666 && swrast
->_RasterMask
== LOGIC_OP_BIT
667 && ctx
->Color
.LogicOp
== GL_XOR
668 && !ctx
->Line
.StippleFlag
669 && !ctx
->Line
.SmoothFlag
) {
672 #endif /* XFree86Server */
674 #endif /* CHAN_BITS == 8 */
675 return (swrast_line_func
) NULL
;
680 * Override for the swrast line-selection function. Try to use one
681 * of our internal line functions, otherwise fall back to the
682 * standard swrast functions.
685 xmesa_choose_line(GLcontext
*ctx
)
687 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
689 if (!(swrast
->Line
= get_line_func( ctx
)))
690 _swrast_choose_line( ctx
);