1 /* $Id: osmesa.c,v 1.63 2001/07/14 17:53:04 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Off-Screen Mesa rendering / Rendering into client memory space
31 * Note on thread safety: this driver is thread safe. All
32 * functions are reentrant. The notion of current context is
33 * managed by the core _mesa_make_current() and _mesa_get_current_context()
34 * functions. Those functions are thread-safe.
39 #include "GL/osmesa.h"
43 #include "extensions.h"
49 #include "texformat.h"
51 #include "array_cache/acache.h"
52 #include "swrast/swrast.h"
53 #include "swrast_setup/swrast_setup.h"
54 #include "swrast/s_context.h"
55 #include "swrast/s_depth.h"
56 #include "swrast/s_lines.h"
57 #include "swrast/s_triangle.h"
58 #include "swrast/s_trispan.h"
60 #include "tnl/t_context.h"
61 #include "tnl/t_pipeline.h"
66 * This is the OS/Mesa context struct.
67 * Notice how it includes a GLcontext. By doing this we're mimicking
68 * C++ inheritance/derivation.
69 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
72 struct osmesa_context
{
73 GLcontext gl_ctx
; /* The core GL/Mesa context */
74 GLvisual
*gl_visual
; /* Describes the buffers */
75 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
76 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
77 void *buffer
; /* the image buffer */
78 GLint width
, height
; /* size of image buffer */
79 GLint rowlength
; /* number of pixels per row */
80 GLint userRowLength
; /* user-specified number of pixels per row */
81 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
83 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
84 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
85 GLboolean yup
; /* TRUE -> Y increases upward */
86 /* FALSE -> Y increases downward */
91 /* A forward declaration: */
92 static void osmesa_update_state( GLcontext
*ctx
, GLuint newstate
);
93 static void osmesa_register_swrast_functions( GLcontext
*ctx
);
97 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
101 /**********************************************************************/
102 /***** Public Functions *****/
103 /**********************************************************************/
107 * Create an Off-Screen Mesa rendering context. The only attribute needed is
108 * an RGBA vs Color-Index mode flag.
110 * Input: format - either GL_RGBA or GL_COLOR_INDEX
111 * sharelist - specifies another OSMesaContext with which to share
112 * display lists. NULL indicates no sharing.
113 * Return: an OSMesaContext or 0 if error
115 OSMesaContext GLAPIENTRY
116 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
118 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
127 * Create context and specify size of ancillary buffers.
129 OSMesaContext GLAPIENTRY
130 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
131 GLint accumBits
, OSMesaContext sharelist
)
133 OSMesaContext osmesa
;
134 GLint rshift
, gshift
, bshift
, ashift
;
135 GLint rind
, gind
, bind
, aind
;
136 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
140 const GLubyte
*i1
= (GLubyte
*) &i4
;
141 const GLint little_endian
= *i1
;
144 rind
= gind
= bind
= aind
= 0;
145 if (format
==OSMESA_COLOR_INDEX
) {
147 rshift
= gshift
= bshift
= ashift
= 0;
150 else if (format
==OSMESA_RGBA
) {
153 greenBits
= CHAN_BITS
;
154 blueBits
= CHAN_BITS
;
155 alphaBits
= CHAN_BITS
;
174 else if (format
==OSMESA_BGRA
) {
177 greenBits
= CHAN_BITS
;
178 blueBits
= CHAN_BITS
;
179 alphaBits
= CHAN_BITS
;
198 else if (format
==OSMESA_ARGB
) {
201 greenBits
= CHAN_BITS
;
202 blueBits
= CHAN_BITS
;
203 alphaBits
= CHAN_BITS
;
222 else if (format
==OSMESA_RGB
) {
225 greenBits
= CHAN_BITS
;
226 blueBits
= CHAN_BITS
;
238 else if (format
==OSMESA_BGR
) {
241 greenBits
= CHAN_BITS
;
242 blueBits
= CHAN_BITS
;
254 else if (format
==OSMESA_RGB_565
) {
264 rind
= 0; /* not used */
275 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
277 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
278 GL_FALSE
, /* double buffer */
279 GL_FALSE
, /* stereo */
290 alphaBits
? accumBits
: 0,
293 if (!osmesa
->gl_visual
) {
298 if (!_mesa_initialize_context(&osmesa
->gl_ctx
,
300 sharelist
? &sharelist
->gl_ctx
301 : (GLcontext
*) NULL
,
302 (void *) osmesa
, GL_TRUE
)) {
303 _mesa_destroy_visual( osmesa
->gl_visual
);
308 _mesa_enable_sw_extensions(&(osmesa
->gl_ctx
));
310 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
311 osmesa
->gl_visual
->depthBits
> 0,
312 osmesa
->gl_visual
->stencilBits
> 0,
313 osmesa
->gl_visual
->accumRedBits
> 0,
316 if (!osmesa
->gl_buffer
) {
317 _mesa_destroy_visual( osmesa
->gl_visual
);
318 _mesa_free_context_data( &osmesa
->gl_ctx
);
322 osmesa
->format
= format
;
323 osmesa
->buffer
= NULL
;
326 osmesa
->userRowLength
= 0;
327 osmesa
->rowlength
= 0;
328 osmesa
->yup
= GL_TRUE
;
329 osmesa
->rshift
= rshift
;
330 osmesa
->gshift
= gshift
;
331 osmesa
->bshift
= bshift
;
332 osmesa
->ashift
= ashift
;
339 /* Initialize the software rasterizer and helper modules.
342 GLcontext
*ctx
= &osmesa
->gl_ctx
;
344 _swrast_CreateContext( ctx
);
345 _ac_CreateContext( ctx
);
346 _tnl_CreateContext( ctx
);
347 _swsetup_CreateContext( ctx
);
349 _swsetup_Wakeup( ctx
);
350 osmesa_register_swrast_functions( ctx
);
360 * Destroy an Off-Screen Mesa rendering context.
362 * Input: ctx - the context to destroy
364 void GLAPIENTRY
OSMesaDestroyContext( OSMesaContext ctx
)
367 _swsetup_DestroyContext( &ctx
->gl_ctx
);
368 _tnl_DestroyContext( &ctx
->gl_ctx
);
369 _ac_DestroyContext( &ctx
->gl_ctx
);
370 _swrast_DestroyContext( &ctx
->gl_ctx
);
372 _mesa_destroy_visual( ctx
->gl_visual
);
373 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
374 _mesa_free_context_data( &ctx
->gl_ctx
);
382 * Recompute the values of the context's rowaddr array.
384 static void compute_row_addresses( OSMesaContext ctx
)
386 GLint bytesPerPixel
, bytesPerRow
, i
;
387 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
389 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
391 bytesPerPixel
= 1 * sizeof(GLchan
);
393 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
395 bytesPerPixel
= 3 * sizeof(GLchan
);
397 else if (ctx
->format
== OSMESA_RGB_565
) {
398 /* 5/6/5 RGB pixel in 16 bits */
403 bytesPerPixel
= 4 * sizeof(GLchan
);
406 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
409 /* Y=0 is bottom line of window */
410 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
411 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
415 /* Y=0 is top line of window */
416 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
417 GLint j
= ctx
->height
- i
- 1;
418 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
425 * Bind an OSMesaContext to an image buffer. The image buffer is just a
426 * block of memory which the client provides. Its size must be at least
427 * as large as width*height*sizeof(type). Its address should be a multiple
428 * of 4 if using RGBA mode.
430 * Image data is stored in the order of glDrawPixels: row-major order
431 * with the lower-left image pixel stored in the first array position
432 * (ie. bottom-to-top).
434 * Since the only type initially supported is GL_UNSIGNED_BYTE, if the
435 * context is in RGBA mode, each pixel will be stored as a 4-byte RGBA
436 * value. If the context is in color indexed mode, each pixel will be
437 * stored as a 1-byte value.
439 * If the context's viewport hasn't been initialized yet, it will now be
440 * initialized to (0,0,width,height).
442 * Input: ctx - the rendering context
443 * buffer - the image buffer memory
444 * type - data type for pixel components, only GL_UNSIGNED_BYTE
445 * and GL_UNSIGNED_SHORT_5_6_5 supported now.
446 * width, height - size of image buffer in pixels, at least 1
447 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
448 * invalid buffer address, type!=GL_UNSIGNED_BYTE, width<1, height<1,
449 * width>internal limit or height>internal limit.
452 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
453 GLsizei width
, GLsizei height
)
455 if (!ctx
|| !buffer
||
456 width
< 1 || height
< 1 ||
457 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
461 if (ctx
->format
== OSMESA_RGB_565
) {
462 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
465 else if (type
!= CHAN_TYPE
) {
469 osmesa_update_state( &ctx
->gl_ctx
, 0 );
470 _mesa_make_current( &ctx
->gl_ctx
, ctx
->gl_buffer
);
472 ctx
->buffer
= buffer
;
474 ctx
->height
= height
;
475 if (ctx
->userRowLength
)
476 ctx
->rowlength
= ctx
->userRowLength
;
478 ctx
->rowlength
= width
;
480 compute_row_addresses( ctx
);
483 if (ctx
->gl_ctx
.Viewport
.Width
==0) {
484 /* initialize viewport and scissor box to buffer size */
485 _mesa_Viewport( 0, 0, width
, height
);
486 ctx
->gl_ctx
.Scissor
.Width
= width
;
487 ctx
->gl_ctx
.Scissor
.Height
= height
;
495 OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
497 GLcontext
*ctx
= _mesa_get_current_context();
499 return (OSMesaContext
) ctx
;
506 void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
508 OSMesaContext ctx
= OSMesaGetCurrentContext();
511 case OSMESA_ROW_LENGTH
:
513 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_VALUE
,
514 "OSMesaPixelStore(value)" );
517 ctx
->userRowLength
= value
;
518 ctx
->rowlength
= value
;
521 ctx
->yup
= value
? GL_TRUE
: GL_FALSE
;
524 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
528 compute_row_addresses( ctx
);
532 void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
534 OSMesaContext ctx
= OSMesaGetCurrentContext();
541 *value
= ctx
->height
;
544 *value
= ctx
->format
;
549 case OSMESA_ROW_LENGTH
:
550 *value
= ctx
->rowlength
;
556 _mesa_error(&ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
562 * Return the depth buffer associated with an OSMesa context.
563 * Input: c - the OSMesa context
564 * Output: width, height - size of buffer in pixels
565 * bytesPerValue - bytes per depth value (2 or 4)
566 * buffer - pointer to depth buffer values
567 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
570 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
571 GLint
*bytesPerValue
, void **buffer
)
573 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
581 *width
= c
->gl_buffer
->Width
;
582 *height
= c
->gl_buffer
->Height
;
583 if (c
->gl_visual
->depthBits
<= 16)
584 *bytesPerValue
= sizeof(GLushort
);
586 *bytesPerValue
= sizeof(GLuint
);
587 *buffer
= c
->gl_buffer
->DepthBuffer
;
593 * Return the color buffer associated with an OSMesa context.
594 * Input: c - the OSMesa context
595 * Output: width, height - size of buffer in pixels
596 * format - the pixel format (OSMESA_FORMAT)
597 * buffer - pointer to color buffer values
598 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
601 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
602 GLint
*height
, GLint
*format
, void **buffer
)
620 /**********************************************************************/
621 /*** Device Driver Functions ***/
622 /**********************************************************************/
629 #define PACK_RGBA(DST, R, G, B, A) \
631 (DST)[osmesa->rInd] = R; \
632 (DST)[osmesa->gInd] = G; \
633 (DST)[osmesa->bInd] = B; \
634 (DST)[osmesa->aInd] = A; \
637 #define PACK_RGB(DST, R, G, B) \
644 #define PACK_BGR(DST, R, G, B) \
651 #define PACK_RGB_565(DST, R, G, B) \
653 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
657 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
658 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
659 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
660 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
663 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
664 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
665 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
666 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
670 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum mode
)
673 if (mode
==GL_FRONT_LEFT
) {
682 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLenum mode
)
684 /* separate read buffer not supported */
685 ASSERT(buffer
== ctx
->DrawBuffer
);
686 ASSERT(mode
== GL_FRONT_LEFT
);
690 static void clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
691 GLint x
, GLint y
, GLint width
, GLint height
)
693 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
694 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
696 /* sanity check - we only have a front-left buffer */
697 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
| DD_BACK_LEFT_BIT
| DD_BACK_RIGHT_BIT
)) == 0);
698 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
699 if (mask
& DD_FRONT_LEFT_BIT
) {
700 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
702 /* Clear whole CI buffer */
703 #if CHAN_TYPE == GL_UNSIGNED_BYTE
704 MEMSET(osmesa
->buffer
, ctx
->Color
.ClearIndex
,
705 osmesa
->rowlength
* osmesa
->height
);
707 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
708 GLchan
*buffer
= (GLchan
*) osmesa
->buffer
;
710 for (i
= 0; i
< n
; i
++) {
711 buffer
[i
] = ctx
->Color
.ClearIndex
;
716 /* Clear part of CI buffer */
717 const GLchan clearIndex
= (GLchan
) ctx
->Color
.ClearIndex
;
719 for (i
= 0; i
< height
; i
++) {
720 GLchan
*ptr1
= PIXELADDR1(x
, (y
+ i
));
721 for (j
= 0; j
< width
; j
++) {
722 *ptr1
++ = clearIndex
;
727 else if (osmesa
->format
== OSMESA_RGB
) {
728 const GLchan r
= ctx
->Color
.ClearColor
[0];
729 const GLchan g
= ctx
->Color
.ClearColor
[1];
730 const GLchan b
= ctx
->Color
.ClearColor
[2];
732 /* Clear whole RGB buffer */
733 GLuint n
= osmesa
->rowlength
* osmesa
->height
;
734 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
736 for (i
= 0; i
< n
; i
++) {
737 PACK_RGB(ptr3
, r
, g
, b
);
742 /* Clear part of RGB buffer */
744 for (i
= 0; i
< height
; i
++) {
745 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
746 for (j
= 0; j
< width
; j
++) {
747 PACK_RGB(ptr3
, r
, g
, b
);
753 else if (osmesa
->format
== OSMESA_BGR
) {
754 const GLchan r
= ctx
->Color
.ClearColor
[0];
755 const GLchan g
= ctx
->Color
.ClearColor
[1];
756 const GLchan b
= ctx
->Color
.ClearColor
[2];
758 /* Clear whole RGB buffer */
759 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
760 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
762 for (i
= 0; i
< n
; i
++) {
763 PACK_BGR(ptr3
, r
, g
, b
);
768 /* Clear part of RGB buffer */
770 for (i
= 0; i
< height
; i
++) {
771 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
772 for (j
= 0; j
< width
; j
++) {
773 PACK_BGR(ptr3
, r
, g
, b
);
779 else if (osmesa
->format
== OSMESA_RGB_565
) {
780 const GLchan r
= ctx
->Color
.ClearColor
[0];
781 const GLchan g
= ctx
->Color
.ClearColor
[1];
782 const GLchan b
= ctx
->Color
.ClearColor
[2];
784 PACK_RGB_565(clearPixel
, r
, g
, b
);
786 /* Clear whole RGB buffer */
787 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
788 GLushort
*ptr2
= (GLushort
*) osmesa
->buffer
;
790 for (i
= 0; i
< n
; i
++) {
796 /* clear scissored region */
798 for (i
= 0; i
< height
; i
++) {
799 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, (y
+ i
));
800 for (j
= 0; j
< width
; j
++) {
808 #if CHAN_TYPE == GL_UNSIGNED_BYTE
809 /* 4-byte pixel value */
811 GLchan
*clr
= (GLchan
*) &clearPixel
;
812 clr
[osmesa
->rInd
] = ctx
->Color
.ClearColor
[0];
813 clr
[osmesa
->gInd
] = ctx
->Color
.ClearColor
[1];
814 clr
[osmesa
->bInd
] = ctx
->Color
.ClearColor
[2];
815 clr
[osmesa
->aInd
] = ctx
->Color
.ClearColor
[3];
817 /* Clear whole RGBA buffer */
818 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
819 GLuint
*ptr4
= (GLuint
*) osmesa
->buffer
;
822 for (i
= 0; i
< n
; i
++) {
823 *ptr4
++ = clearPixel
;
827 BZERO(ptr4
, n
* sizeof(GLuint
));
831 /* Clear part of RGBA buffer */
833 for (i
= 0; i
< height
; i
++) {
834 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, (y
+ i
));
835 for (j
= 0; j
< width
; j
++) {
836 *ptr4
++ = clearPixel
;
841 const GLchan r
= ctx
->Color
.ClearColor
[0];
842 const GLchan g
= ctx
->Color
.ClearColor
[1];
843 const GLchan b
= ctx
->Color
.ClearColor
[2];
844 const GLchan a
= ctx
->Color
.ClearColor
[3];
846 /* Clear whole RGBA buffer */
847 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
848 GLchan
*p
= (GLchan
*) osmesa
->buffer
;
850 for (i
= 0; i
< n
; i
++) {
851 PACK_RGBA(p
, r
, g
, b
, a
);
856 /* Clear part of RGBA buffer */
858 for (i
= 0; i
< height
; i
++) {
859 GLchan
*p
= PIXELADDR4(x
, (y
+ i
));
860 for (j
= 0; j
< width
; j
++) {
861 PACK_RGBA(p
, r
, g
, b
, a
);
869 mask
&= ~DD_FRONT_LEFT_BIT
;
874 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
879 static void buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
881 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
882 *width
= osmesa
->width
;
883 *height
= osmesa
->height
;
887 /**********************************************************************/
888 /***** Read/write spans/arrays of RGBA pixels *****/
889 /**********************************************************************/
891 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
893 write_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
894 CONST GLchan rgba
[][4], const GLubyte mask
[] )
896 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
897 GLchan
*p
= PIXELADDR4(x
, y
);
900 for (i
= 0; i
< n
; i
++, p
+= 4) {
902 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
903 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
908 for (i
= 0; i
< n
; i
++, p
+= 4) {
909 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
910 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
916 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
918 write_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
919 CONST GLchan rgba
[][4], const GLubyte mask
[] )
921 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
922 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
923 const GLuint
*rgba4
= (const GLuint
*) rgba
;
925 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
927 for (i
= 0; i
< n
; i
++) {
934 MEMCPY( ptr4
, rgba4
, n
* 4 );
939 /* Write RGB pixels to an RGBA (or permuted) buffer. */
941 write_rgb_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
942 CONST GLchan rgb
[][3], const GLubyte mask
[] )
944 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
945 GLchan
*p
= PIXELADDR4(x
, y
);
948 for (i
= 0; i
< n
; i
++, p
+=4) {
950 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
955 for (i
= 0; i
< n
; i
++, p
+=4) {
956 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
964 write_monocolor_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
965 const GLchan color
[4], const GLubyte mask
[] )
967 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
968 GLchan
*p
= PIXELADDR4(x
, y
);
970 for (i
= 0; i
< n
; i
++, p
+= 4) {
972 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
980 write_rgba_pixels( const GLcontext
*ctx
, GLuint n
,
981 const GLint x
[], const GLint y
[],
982 CONST GLchan rgba
[][4], const GLubyte mask
[] )
984 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
986 for (i
= 0; i
< n
; i
++) {
988 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
989 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
990 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
998 write_monocolor_pixels( const GLcontext
*ctx
, GLuint n
,
999 const GLint x
[], const GLint y
[],
1000 const GLchan color
[4], const GLubyte mask
[] )
1002 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1004 for (i
= 0; i
< n
; i
++) {
1006 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1007 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
1014 read_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1017 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1019 GLchan
*p
= PIXELADDR4(x
, y
);
1020 for (i
= 0; i
< n
; i
++, p
+= 4) {
1021 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1022 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1023 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1024 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1029 /* Read RGBA pixels from an RGBA buffer */
1031 read_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1034 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1035 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
1036 MEMCPY( rgba
, ptr4
, n
* 4 * sizeof(GLchan
) );
1041 read_rgba_pixels( const GLcontext
*ctx
,
1042 GLuint n
, const GLint x
[], const GLint y
[],
1043 GLchan rgba
[][4], const GLubyte mask
[] )
1045 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1047 for (i
= 0; i
< n
; i
++) {
1049 const GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1050 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1051 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1052 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1053 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1058 /**********************************************************************/
1059 /***** 3 byte RGB pixel support funcs *****/
1060 /**********************************************************************/
1062 /* Write RGBA pixels to an RGB buffer. */
1064 write_rgba_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1065 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1067 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1068 GLchan
*p
= PIXELADDR3(x
, y
);
1071 for (i
= 0; i
< n
; i
++, p
+= 3) {
1073 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1078 for (i
= 0; i
< n
; i
++, p
+= 3) {
1079 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1084 /* Write RGBA pixels to an BGR buffer. */
1086 write_rgba_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1087 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1089 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1090 GLchan
*p
= PIXELADDR3(x
, y
);
1093 for (i
= 0; i
< n
; i
++, p
+= 3) {
1095 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1100 for (i
= 0; i
< n
; i
++, p
+= 3) {
1101 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1106 /* Write RGB pixels to an RGB buffer. */
1108 write_rgb_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1109 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1111 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1112 GLchan
*p
= PIXELADDR3(x
, y
);
1115 for (i
= 0; i
< n
; i
++, p
+= 3) {
1117 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1122 for (i
= 0; i
< n
; i
++, p
+= 3) {
1123 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1128 /* Write RGB pixels to an BGR buffer. */
1130 write_rgb_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1131 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1133 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1134 GLchan
*p
= PIXELADDR3(x
, y
);
1137 for (i
= 0; i
< n
; i
++, p
+= 3) {
1139 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1144 for (i
= 0; i
< n
; i
++, p
+= 3) {
1145 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1152 write_monocolor_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1153 const GLchan color
[4], const GLubyte mask
[] )
1155 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1156 GLchan
*p
= PIXELADDR3(x
, y
);
1158 for (i
= 0; i
< n
; i
++, p
+= 3) {
1160 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1166 write_monocolor_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1167 const GLchan color
[4], const GLubyte mask
[] )
1169 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1170 GLchan
*p
= PIXELADDR3(x
, y
);
1172 for (i
= 0; i
< n
; i
++, p
+= 3) {
1174 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1180 write_rgba_pixels_RGB( const GLcontext
*ctx
, GLuint n
,
1181 const GLint x
[], const GLint y
[],
1182 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1184 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1186 for (i
= 0; i
< n
; i
++) {
1188 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1189 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1195 write_rgba_pixels_BGR( const GLcontext
*ctx
, GLuint n
,
1196 const GLint x
[], const GLint y
[],
1197 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1199 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1201 for (i
= 0; i
< n
; i
++) {
1203 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1204 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1210 write_monocolor_pixels_RGB( const GLcontext
*ctx
,
1211 GLuint n
, const GLint x
[], const GLint y
[],
1212 const GLchan color
[4], const GLubyte mask
[] )
1214 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1216 for (i
= 0; i
< n
; i
++) {
1218 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1219 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1225 write_monocolor_pixels_BGR( const GLcontext
*ctx
,
1226 GLuint n
, const GLint x
[], const GLint y
[],
1227 const GLchan color
[4], const GLubyte mask
[] )
1229 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1231 for (i
= 0; i
< n
; i
++) {
1233 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1234 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1240 read_rgba_span3( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1243 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1245 const GLchan
*p
= PIXELADDR3(x
, y
);
1246 for (i
= 0; i
< n
; i
++, p
+= 3) {
1247 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1248 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1249 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1250 rgba
[i
][ACOMP
] = CHAN_MAX
;
1255 read_rgba_pixels3( const GLcontext
*ctx
,
1256 GLuint n
, const GLint x
[], const GLint y
[],
1257 GLchan rgba
[][4], const GLubyte mask
[] )
1259 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1261 for (i
= 0; i
< n
; i
++) {
1263 const GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1264 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1265 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1266 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1267 rgba
[i
][ACOMP
] = CHAN_MAX
;
1273 /**********************************************************************/
1274 /***** 2 byte RGB pixel support funcs *****/
1275 /**********************************************************************/
1277 /* Write RGBA pixels to an RGB_565 buffer. */
1279 write_rgba_span2( const GLcontext
*ctx
,
1280 GLuint n
, GLint x
, GLint y
,
1281 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1283 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1284 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1287 for (i
= 0; i
< n
; i
++, ptr2
++) {
1289 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1294 for (i
= 0; i
< n
; i
++, ptr2
++) {
1295 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1301 /* Write RGB pixels to an RGB_565 buffer. */
1303 write_rgb_span2( const GLcontext
*ctx
,
1304 GLuint n
, GLint x
, GLint y
,
1305 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1307 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1308 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1311 for (i
= 0; i
< n
; i
++, ptr2
++) {
1313 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1318 for (i
= 0; i
< n
; i
++, ptr2
++) {
1319 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1326 write_monocolor_span2( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1327 const GLchan color
[4], const GLubyte mask
[] )
1329 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1331 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1333 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1334 for (i
= 0; i
< n
; i
++, ptr2
++) {
1343 write_rgba_pixels2( const GLcontext
*ctx
,
1344 GLuint n
, const GLint x
[], const GLint y
[],
1345 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1347 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1349 for (i
= 0; i
< n
; i
++) {
1351 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1352 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1358 write_monocolor_pixels2( const GLcontext
*ctx
,
1359 GLuint n
, const GLint x
[], const GLint y
[],
1360 const GLchan color
[4], const GLubyte mask
[] )
1362 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1365 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1366 for (i
= 0; i
< n
; i
++) {
1368 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1375 read_rgba_span2( const GLcontext
*ctx
,
1376 GLuint n
, GLint x
, GLint y
,
1379 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1381 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
, y
);
1382 for (i
= 0; i
< n
; i
++, ptr2
++) {
1383 /* This should be fixed to get the low bits right */
1384 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFe;
1385 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFc;
1386 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFe;
1392 read_rgba_pixels2( const GLcontext
*ctx
,
1393 GLuint n
, const GLint x
[], const GLint y
[],
1394 GLchan rgba
[][4], const GLubyte mask
[] )
1396 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1398 for (i
= 0; i
< n
; i
++) {
1400 /* This should be fixed to get the low bits right */
1401 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1402 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFE;
1403 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFC;
1404 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFE;
1412 /**********************************************************************/
1413 /***** Read/write spans/arrays of CI pixels *****/
1414 /**********************************************************************/
1416 /* Write 32-bit color index to buffer */
1418 write_index32_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1419 const GLuint index
[], const GLubyte mask
[] )
1421 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1422 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1425 for (i
=0;i
<n
;i
++,ptr1
++) {
1427 *ptr1
= (GLchan
) index
[i
];
1432 for (i
=0;i
<n
;i
++,ptr1
++) {
1433 *ptr1
= (GLchan
) index
[i
];
1439 /* Write 8-bit color index to buffer */
1441 write_index8_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1442 const GLubyte index
[], const GLubyte mask
[] )
1444 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1445 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1448 for (i
=0;i
<n
;i
++,ptr1
++) {
1450 *ptr1
= (GLchan
) index
[i
];
1455 MEMCPY(ptr1
, index
, n
* sizeof(GLchan
));
1461 write_monoindex_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1462 GLuint colorIndex
, const GLubyte mask
[] )
1464 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1465 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1467 for (i
=0;i
<n
;i
++,ptr1
++) {
1469 *ptr1
= (GLchan
) colorIndex
;
1476 write_index_pixels( const GLcontext
*ctx
,
1477 GLuint n
, const GLint x
[], const GLint y
[],
1478 const GLuint index
[], const GLubyte mask
[] )
1480 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1484 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1485 *ptr1
= (GLchan
) index
[i
];
1492 write_monoindex_pixels( const GLcontext
*ctx
,
1493 GLuint n
, const GLint x
[], const GLint y
[],
1494 GLuint colorIndex
, const GLubyte mask
[] )
1496 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1500 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1501 *ptr1
= (GLchan
) colorIndex
;
1508 read_index_span( const GLcontext
*ctx
,
1509 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1511 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1513 const GLchan
*ptr1
= (const GLchan
*) PIXELADDR1(x
, y
);
1514 for (i
=0;i
<n
;i
++,ptr1
++) {
1515 index
[i
] = (GLuint
) *ptr1
;
1521 read_index_pixels( const GLcontext
*ctx
,
1522 GLuint n
, const GLint x
[], const GLint y
[],
1523 GLuint index
[], const GLubyte mask
[] )
1525 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1529 const GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1530 index
[i
] = (GLuint
) *ptr1
;
1537 /**********************************************************************/
1538 /***** Optimized line rendering *****/
1539 /**********************************************************************/
1543 * Draw a flat-shaded, RGB line into an osmesa buffer.
1546 flat_rgba_line( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1548 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1549 const GLchan
*color
= vert0
->color
;
1553 #define PLOT(X, Y) \
1555 GLchan *p = PIXELADDR4(X, Y); \
1556 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1560 #include "..\swrast\s_linetemp.h"
1562 #include "swrast/s_linetemp.h"
1568 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1571 flat_rgba_z_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1573 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1574 const GLchan
*color
= vert0
->color
;
1578 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1580 #define PLOT(X, Y) \
1583 GLchan *p = PIXELADDR4(X, Y); \
1584 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1585 color[BCOMP], color[ACOMP]); \
1592 #include "..\swrast\s_linetemp.h"
1594 #include "swrast/s_linetemp.h"
1600 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1601 * XXX update for GLchan
1604 flat_blend_rgba_line( GLcontext
*ctx
,
1605 const SWvertex
*vert0
, const SWvertex
*vert1
)
1607 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1608 const GLint rshift
= osmesa
->rshift
;
1609 const GLint gshift
= osmesa
->gshift
;
1610 const GLint bshift
= osmesa
->bshift
;
1611 const GLint avalue
= vert0
->color
[3];
1612 const GLint msavalue
= CHAN_MAX
- avalue
;
1613 const GLint rvalue
= vert0
->color
[0]*avalue
;
1614 const GLint gvalue
= vert0
->color
[1]*avalue
;
1615 const GLint bvalue
= vert0
->color
[2]*avalue
;
1620 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1622 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1623 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1624 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1629 #include "..\swrast\s_linetemp.h"
1631 #include "swrast/s_linetemp.h"
1637 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1638 * XXX update for GLchan
1641 flat_blend_rgba_z_line( GLcontext
*ctx
,
1642 const SWvertex
*vert0
, const SWvertex
*vert1
)
1644 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1645 const GLint rshift
= osmesa
->rshift
;
1646 const GLint gshift
= osmesa
->gshift
;
1647 const GLint bshift
= osmesa
->bshift
;
1648 const GLint avalue
= vert0
->color
[3];
1649 const GLint msavalue
= 256 - avalue
;
1650 const GLint rvalue
= vert0
->color
[0]*avalue
;
1651 const GLint gvalue
= vert0
->color
[1]*avalue
;
1652 const GLint bvalue
= vert0
->color
[2]*avalue
;
1656 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1660 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1662 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1663 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1664 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1669 #include "..\swrast\s_linetemp.h"
1671 #include "swrast/s_linetemp.h"
1677 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1678 * XXX update for GLchan
1681 flat_blend_rgba_z_line_write( GLcontext
*ctx
,
1682 const SWvertex
*vert0
, const SWvertex
*vert1
)
1684 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1685 const GLint rshift
= osmesa
->rshift
;
1686 const GLint gshift
= osmesa
->gshift
;
1687 const GLint bshift
= osmesa
->bshift
;
1688 const GLint avalue
= vert0
->color
[3];
1689 const GLint msavalue
= 256 - avalue
;
1690 const GLint rvalue
= vert0
->color
[0]*avalue
;
1691 const GLint gvalue
= vert0
->color
[1]*avalue
;
1692 const GLint bvalue
= vert0
->color
[2]*avalue
;
1696 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1700 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1702 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1703 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1704 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1710 #include "..\swrast\s_linetemp.h"
1712 #include "swrast/s_linetemp.h"
1718 * Analyze context state to see if we can provide a fast line drawing
1719 * function, like those in lines.c. Otherwise, return NULL.
1721 static swrast_line_func
1722 osmesa_choose_line_function( GLcontext
*ctx
)
1724 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1725 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1727 if (CHAN_BITS
!= 8) return NULL
;
1728 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
1729 if (ctx
->Line
.SmoothFlag
) return NULL
;
1730 if (ctx
->Texture
._ReallyEnabled
) return NULL
;
1731 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
1732 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
1733 if (ctx
->Line
.StippleFlag
) return NULL
;
1734 if (ctx
->Line
.SmoothFlag
) return NULL
;
1735 if (osmesa
->format
!= OSMESA_RGBA
&&
1736 osmesa
->format
!= OSMESA_BGRA
&&
1737 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
1739 if (swrast
->_RasterMask
==DEPTH_BIT
1740 && ctx
->Depth
.Func
==GL_LESS
1741 && ctx
->Depth
.Mask
==GL_TRUE
1742 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1743 return flat_rgba_z_line
;
1746 if (swrast
->_RasterMask
== 0) {
1747 return flat_rgba_line
;
1750 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1751 && ctx
->Depth
.Func
==GL_LESS
1752 && ctx
->Depth
.Mask
==GL_TRUE
1753 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1754 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1755 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1756 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1757 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1758 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1759 return flat_blend_rgba_z_line_write
;
1762 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1763 && ctx
->Depth
.Func
==GL_LESS
1764 && ctx
->Depth
.Mask
==GL_FALSE
1765 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1766 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1767 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1768 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1769 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1770 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1771 return flat_blend_rgba_z_line
;
1774 if (swrast
->_RasterMask
==BLEND_BIT
1775 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1776 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1777 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1778 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1779 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1780 return flat_blend_rgba_line
;
1787 /**********************************************************************/
1788 /***** Optimized triangle rendering *****/
1789 /**********************************************************************/
1793 * Smooth-shaded, z-less triangle, RGBA color.
1795 static void smooth_rgba_z_triangle( GLcontext
*ctx
,
1798 const SWvertex
*v2
)
1800 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1803 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1804 #define INTERP_RGB 1
1805 #define INTERP_ALPHA 1
1806 #define RENDER_SPAN( span ) \
1808 GLchan *img = PIXELADDR4(span.x, span.y); \
1809 for (i = 0; i < span.count; i++, img += 4) { \
1810 const GLdepth z = FixedToDepth(span.z); \
1811 if (z < zRow[i]) { \
1812 PACK_RGBA(img, FixedToChan(span.red), \
1813 FixedToChan(span.green), FixedToChan(span.blue), \
1814 FixedToChan(span.alpha)); \
1817 span.red += span.redStep; \
1818 span.green += span.greenStep; \
1819 span.blue += span.blueStep; \
1820 span.alpha += span.alphaStep; \
1821 span.z += span.zStep; \
1825 #include "..\swrast\s_tritemp.h"
1827 #include "swrast/s_tritemp.h"
1835 * Flat-shaded, z-less triangle, RGBA color.
1837 static void flat_rgba_z_triangle( GLcontext
*ctx
,
1840 const SWvertex
*v2
)
1842 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1844 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1845 #define SETUP_CODE \
1847 PACK_RGBA((GLchan *) &pixel, v0->color[0], v0->color[1], \
1848 v0->color[2], v0->color[3]);
1850 #define RENDER_SPAN( span ) \
1852 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1853 for (i = 0; i < span.count; i++) { \
1854 const GLdepth z = FixedToDepth(span.z); \
1855 if (z < zRow[i]) { \
1859 span.z += span.zStep; \
1863 #include "..\swrast\s_tritemp.h"
1865 #include "swrast/s_tritemp.h"
1872 * Return pointer to an accelerated triangle function if possible.
1874 static swrast_tri_func
1875 osmesa_choose_triangle_function( GLcontext
*ctx
)
1877 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1878 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1880 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
1881 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
1882 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
1883 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
1884 if (ctx
->Texture
._ReallyEnabled
) return (swrast_tri_func
) NULL
;
1885 if (osmesa
->format
!= OSMESA_RGBA
&&
1886 osmesa
->format
!= OSMESA_BGRA
&&
1887 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
1889 if (swrast
->_RasterMask
== DEPTH_BIT
&&
1890 ctx
->Depth
.Func
== GL_LESS
&&
1891 ctx
->Depth
.Mask
== GL_TRUE
&&
1892 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1893 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
1894 return smooth_rgba_z_triangle
;
1897 return flat_rgba_z_triangle
;
1900 return (swrast_tri_func
) NULL
;
1905 /* Override for the swrast triangle-selection function. Try to use one
1906 * of our internal triangle functions, otherwise fall back to the
1907 * standard swrast functions.
1909 static void osmesa_choose_triangle( GLcontext
*ctx
)
1911 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1913 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
1914 if (!swrast
->Triangle
)
1915 _swrast_choose_triangle( ctx
);
1918 static void osmesa_choose_line( GLcontext
*ctx
)
1920 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1922 swrast
->Line
= osmesa_choose_line_function( ctx
);
1924 _swrast_choose_line( ctx
);
1928 #define OSMESA_NEW_LINE (_NEW_LINE | \
1933 _SWRAST_NEW_RASTERMASK)
1935 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1940 _SWRAST_NEW_RASTERMASK)
1943 /* Extend the software rasterizer with our line and triangle
1946 static void osmesa_register_swrast_functions( GLcontext
*ctx
)
1948 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1950 swrast
->choose_line
= osmesa_choose_line
;
1951 swrast
->choose_triangle
= osmesa_choose_triangle
;
1953 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
1954 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1958 static const GLubyte
*get_string( GLcontext
*ctx
, GLenum name
)
1963 return (const GLubyte
*) "Mesa OffScreen";
1970 static void osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1972 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1973 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
1974 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1976 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
1979 * XXX these function pointers could be initialized just once during
1980 * context creation since they don't depend on any state changes.
1983 ctx
->Driver
.GetString
= get_string
;
1984 ctx
->Driver
.UpdateState
= osmesa_update_state
;
1985 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
1986 ctx
->Driver
.ResizeBuffersMESA
= _swrast_alloc_buffers
;
1987 ctx
->Driver
.GetBufferSize
= buffer_size
;
1989 ctx
->Driver
.Accum
= _swrast_Accum
;
1990 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
1991 ctx
->Driver
.Clear
= clear
;
1992 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
1993 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
1994 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
1996 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
1997 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
1998 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
1999 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
2000 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
2001 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
2002 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
2003 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
2005 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
2006 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
2007 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
2008 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
2009 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
2010 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
2011 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
2012 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
2013 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
2016 /* RGB(A) span/pixel functions */
2017 if (osmesa
->format
== OSMESA_RGB
) {
2018 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
2019 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
2020 swdd
->WriteMonoRGBASpan
= write_monocolor_span_RGB
;
2021 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
2022 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_RGB
;
2023 swdd
->ReadRGBASpan
= read_rgba_span3
;
2024 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2026 else if (osmesa
->format
== OSMESA_BGR
) {
2027 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
2028 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
2029 swdd
->WriteMonoRGBASpan
= write_monocolor_span_BGR
;
2030 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
2031 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_BGR
;
2032 swdd
->ReadRGBASpan
= read_rgba_span3
;
2033 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2035 else if (osmesa
->format
== OSMESA_RGB_565
) {
2036 swdd
->WriteRGBASpan
= write_rgba_span2
;
2037 swdd
->WriteRGBSpan
= write_rgb_span2
;
2038 swdd
->WriteMonoRGBASpan
= write_monocolor_span2
;
2039 swdd
->WriteRGBAPixels
= write_rgba_pixels2
;
2040 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels2
;
2041 swdd
->ReadRGBASpan
= read_rgba_span2
;
2042 swdd
->ReadRGBAPixels
= read_rgba_pixels2
;
2045 /* 4 GLchan / pixel in frame buffer */
2046 swdd
->WriteRGBSpan
= write_rgb_span
;
2047 swdd
->WriteRGBAPixels
= write_rgba_pixels
;
2048 swdd
->WriteMonoRGBASpan
= write_monocolor_span
;
2049 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels
;
2050 if (osmesa
->format
== OSMESA_RGBA
&&
2051 CHAN_TYPE
== GL_UNSIGNED_BYTE
&&
2052 RCOMP
==0 && GCOMP
==1 && BCOMP
==2 && ACOMP
==3) {
2053 /* special, fast case */
2054 swdd
->WriteRGBASpan
= write_rgba_span_rgba
;
2055 swdd
->ReadRGBASpan
= read_rgba_span_rgba
;
2058 swdd
->WriteRGBASpan
= write_rgba_span
;
2059 swdd
->ReadRGBASpan
= read_rgba_span
;
2061 swdd
->ReadRGBAPixels
= read_rgba_pixels
;
2064 /* CI span/pixel functions */
2065 swdd
->WriteCI32Span
= write_index32_span
;
2066 swdd
->WriteCI8Span
= write_index8_span
;
2067 swdd
->WriteMonoCISpan
= write_monoindex_span
;
2068 swdd
->WriteCI32Pixels
= write_index_pixels
;
2069 swdd
->WriteMonoCIPixels
= write_monoindex_pixels
;
2070 swdd
->ReadCI32Span
= read_index_span
;
2071 swdd
->ReadCI32Pixels
= read_index_pixels
;
2073 swdd
->SetReadBuffer
= set_read_buffer
;
2075 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
2077 _swrast_InvalidateState( ctx
, new_state
);
2078 _swsetup_InvalidateState( ctx
, new_state
);
2079 _ac_InvalidateState( ctx
, new_state
);
2080 _tnl_InvalidateState( ctx
, new_state
);