1 /* $Id: osmesa.c,v 1.74 2002/03/01 04:23:36 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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"
59 #include "tnl/t_context.h"
60 #include "tnl/t_pipeline.h"
65 * This is the OS/Mesa context struct.
66 * Notice how it includes a GLcontext. By doing this we're mimicking
67 * C++ inheritance/derivation.
68 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
71 struct osmesa_context
{
72 GLcontext gl_ctx
; /* The core GL/Mesa context */
73 GLvisual
*gl_visual
; /* Describes the buffers */
74 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
75 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
76 void *buffer
; /* the image buffer */
77 GLint width
, height
; /* size of image buffer */
78 GLint rowlength
; /* number of pixels per row */
79 GLint userRowLength
; /* user-specified number of pixels per row */
80 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
82 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
83 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
84 GLboolean yup
; /* TRUE -> Y increases upward */
85 /* FALSE -> Y increases downward */
90 /* A forward declaration: */
91 static void osmesa_update_state( GLcontext
*ctx
, GLuint newstate
);
92 static void osmesa_register_swrast_functions( GLcontext
*ctx
);
96 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
100 /**********************************************************************/
101 /***** Public Functions *****/
102 /**********************************************************************/
106 * Create an Off-Screen Mesa rendering context. The only attribute needed is
107 * an RGBA vs Color-Index mode flag.
109 * Input: format - either GL_RGBA or GL_COLOR_INDEX
110 * sharelist - specifies another OSMesaContext with which to share
111 * display lists. NULL indicates no sharing.
112 * Return: an OSMesaContext or 0 if error
114 GLAPI OSMesaContext GLAPIENTRY
115 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
117 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
126 * Create context and specify size of ancillary buffers.
128 GLAPI OSMesaContext GLAPIENTRY
129 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
130 GLint accumBits
, OSMesaContext sharelist
)
132 OSMesaContext osmesa
;
133 GLint rshift
, gshift
, bshift
, ashift
;
134 GLint rind
, gind
, bind
, aind
;
135 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
138 const GLubyte
*i1
= (GLubyte
*) &i4
;
139 const GLint little_endian
= *i1
;
141 rind
= gind
= bind
= aind
= 0;
142 if (format
==OSMESA_COLOR_INDEX
) {
144 rshift
= gshift
= bshift
= ashift
= 0;
147 else if (format
==OSMESA_RGBA
) {
150 greenBits
= CHAN_BITS
;
151 blueBits
= CHAN_BITS
;
152 alphaBits
= CHAN_BITS
;
171 else if (format
==OSMESA_BGRA
) {
174 greenBits
= CHAN_BITS
;
175 blueBits
= CHAN_BITS
;
176 alphaBits
= CHAN_BITS
;
195 else if (format
==OSMESA_ARGB
) {
198 greenBits
= CHAN_BITS
;
199 blueBits
= CHAN_BITS
;
200 alphaBits
= CHAN_BITS
;
219 else if (format
==OSMESA_RGB
) {
222 greenBits
= CHAN_BITS
;
223 blueBits
= CHAN_BITS
;
234 else if (format
==OSMESA_BGR
) {
237 greenBits
= CHAN_BITS
;
238 blueBits
= CHAN_BITS
;
249 else if (format
==OSMESA_RGB_565
) {
259 rind
= 0; /* not used */
269 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
271 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
272 GL_FALSE
, /* double buffer */
273 GL_FALSE
, /* stereo */
284 alphaBits
? accumBits
: 0,
287 if (!osmesa
->gl_visual
) {
292 if (!_mesa_initialize_context(&osmesa
->gl_ctx
,
294 sharelist
? &sharelist
->gl_ctx
295 : (GLcontext
*) NULL
,
296 (void *) osmesa
, GL_TRUE
)) {
297 _mesa_destroy_visual( osmesa
->gl_visual
);
302 _mesa_enable_sw_extensions(&(osmesa
->gl_ctx
));
303 _mesa_enable_1_3_extensions(&(osmesa
->gl_ctx
));
305 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
306 (GLboolean
) ( osmesa
->gl_visual
->depthBits
> 0 ),
307 (GLboolean
) ( osmesa
->gl_visual
->stencilBits
> 0 ),
308 (GLboolean
) ( osmesa
->gl_visual
->accumRedBits
> 0 ),
309 GL_FALSE
/* s/w alpha */ );
311 if (!osmesa
->gl_buffer
) {
312 _mesa_destroy_visual( osmesa
->gl_visual
);
313 _mesa_free_context_data( &osmesa
->gl_ctx
);
317 osmesa
->format
= format
;
318 osmesa
->buffer
= NULL
;
321 osmesa
->userRowLength
= 0;
322 osmesa
->rowlength
= 0;
323 osmesa
->yup
= GL_TRUE
;
324 osmesa
->rshift
= rshift
;
325 osmesa
->gshift
= gshift
;
326 osmesa
->bshift
= bshift
;
327 osmesa
->ashift
= ashift
;
334 /* Initialize the software rasterizer and helper modules.
337 GLcontext
*ctx
= &osmesa
->gl_ctx
;
339 _swrast_CreateContext( ctx
);
340 _ac_CreateContext( ctx
);
341 _tnl_CreateContext( ctx
);
342 _swsetup_CreateContext( ctx
);
344 _swsetup_Wakeup( ctx
);
345 osmesa_register_swrast_functions( ctx
);
355 * Destroy an Off-Screen Mesa rendering context.
357 * Input: ctx - the context to destroy
359 GLAPI
void GLAPIENTRY
OSMesaDestroyContext( OSMesaContext ctx
)
362 _swsetup_DestroyContext( &ctx
->gl_ctx
);
363 _tnl_DestroyContext( &ctx
->gl_ctx
);
364 _ac_DestroyContext( &ctx
->gl_ctx
);
365 _swrast_DestroyContext( &ctx
->gl_ctx
);
367 _mesa_destroy_visual( ctx
->gl_visual
);
368 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
369 _mesa_free_context_data( &ctx
->gl_ctx
);
377 * Recompute the values of the context's rowaddr array.
379 static void compute_row_addresses( OSMesaContext ctx
)
381 GLint bytesPerPixel
, bytesPerRow
, i
;
382 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
384 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
386 bytesPerPixel
= 1 * sizeof(GLchan
);
388 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
390 bytesPerPixel
= 3 * sizeof(GLchan
);
392 else if (ctx
->format
== OSMESA_RGB_565
) {
393 /* 5/6/5 RGB pixel in 16 bits */
398 bytesPerPixel
= 4 * sizeof(GLchan
);
401 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
404 /* Y=0 is bottom line of window */
405 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
406 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
410 /* Y=0 is top line of window */
411 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
412 GLint j
= ctx
->height
- i
- 1;
413 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
420 * Bind an OSMesaContext to an image buffer. The image buffer is just a
421 * block of memory which the client provides. Its size must be at least
422 * as large as width*height*sizeof(type). Its address should be a multiple
423 * of 4 if using RGBA mode.
425 * Image data is stored in the order of glDrawPixels: row-major order
426 * with the lower-left image pixel stored in the first array position
427 * (ie. bottom-to-top).
429 * If the context's viewport hasn't been initialized yet, it will now be
430 * initialized to (0,0,width,height).
432 * Input: ctx - the rendering context
433 * buffer - the image buffer memory
434 * type - data type for pixel components
435 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
436 * are supported. But if Mesa's been compiled with CHAN_BITS==16
437 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
438 * with CHAN_BITS==32 then type must be GL_FLOAT.
439 * width, height - size of image buffer in pixels, at least 1
440 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
441 * invalid buffer address, invalid type, width<1, height<1,
442 * width>internal limit or height>internal limit.
444 GLAPI GLboolean GLAPIENTRY
445 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
446 GLsizei width
, GLsizei height
)
448 if (!ctx
|| !buffer
||
449 width
< 1 || height
< 1 ||
450 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
454 if (ctx
->format
== OSMESA_RGB_565
) {
455 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
458 else if (type
!= CHAN_TYPE
) {
462 osmesa_update_state( &ctx
->gl_ctx
, 0 );
463 _mesa_make_current( &ctx
->gl_ctx
, ctx
->gl_buffer
);
465 ctx
->buffer
= buffer
;
467 ctx
->height
= height
;
468 if (ctx
->userRowLength
)
469 ctx
->rowlength
= ctx
->userRowLength
;
471 ctx
->rowlength
= width
;
473 compute_row_addresses( ctx
);
476 if (ctx
->gl_ctx
.Viewport
.Width
==0) {
477 /* initialize viewport and scissor box to buffer size */
478 _mesa_Viewport( 0, 0, width
, height
);
479 ctx
->gl_ctx
.Scissor
.Width
= width
;
480 ctx
->gl_ctx
.Scissor
.Height
= height
;
488 GLAPI OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
490 GLcontext
*ctx
= _mesa_get_current_context();
492 return (OSMesaContext
) ctx
;
499 GLAPI
void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
501 OSMesaContext ctx
= OSMesaGetCurrentContext();
504 case OSMESA_ROW_LENGTH
:
506 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_VALUE
,
507 "OSMesaPixelStore(value)" );
510 ctx
->userRowLength
= value
;
511 ctx
->rowlength
= value
;
514 ctx
->yup
= value
? GL_TRUE
: GL_FALSE
;
517 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
521 compute_row_addresses( ctx
);
525 GLAPI
void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
527 OSMesaContext ctx
= OSMesaGetCurrentContext();
534 *value
= ctx
->height
;
537 *value
= ctx
->format
;
542 case OSMESA_ROW_LENGTH
:
543 *value
= ctx
->rowlength
;
548 case OSMESA_MAX_WIDTH
:
551 case OSMESA_MAX_HEIGHT
:
555 _mesa_error(&ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
561 * Return the depth buffer associated with an OSMesa context.
562 * Input: c - the OSMesa context
563 * Output: width, height - size of buffer in pixels
564 * bytesPerValue - bytes per depth value (2 or 4)
565 * buffer - pointer to depth buffer values
566 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
568 GLAPI GLboolean GLAPIENTRY
569 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
570 GLint
*bytesPerValue
, void **buffer
)
572 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
580 *width
= c
->gl_buffer
->Width
;
581 *height
= c
->gl_buffer
->Height
;
582 if (c
->gl_visual
->depthBits
<= 16)
583 *bytesPerValue
= sizeof(GLushort
);
585 *bytesPerValue
= sizeof(GLuint
);
586 *buffer
= c
->gl_buffer
->DepthBuffer
;
592 * Return the color buffer associated with an OSMesa context.
593 * Input: c - the OSMesa context
594 * Output: width, height - size of buffer in pixels
595 * format - the pixel format (OSMESA_FORMAT)
596 * buffer - pointer to color buffer values
597 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
599 GLAPI GLboolean GLAPIENTRY
600 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
601 GLint
*height
, GLint
*format
, void **buffer
)
619 /**********************************************************************/
620 /*** Device Driver Functions ***/
621 /**********************************************************************/
628 #define PACK_RGBA(DST, R, G, B, A) \
630 (DST)[osmesa->rInd] = R; \
631 (DST)[osmesa->gInd] = G; \
632 (DST)[osmesa->bInd] = B; \
633 (DST)[osmesa->aInd] = A; \
636 #define PACK_RGB(DST, R, G, B) \
643 #define PACK_BGR(DST, R, G, B) \
650 #define PACK_RGB_565(DST, R, G, B) \
652 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
656 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
657 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
658 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
659 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
662 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
663 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
664 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
665 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
669 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum mode
)
672 if (mode
==GL_FRONT_LEFT
) {
681 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLenum mode
)
683 /* separate read buffer not supported */
684 ASSERT(buffer
== ctx
->DrawBuffer
);
685 ASSERT(mode
== GL_FRONT_LEFT
);
689 static void clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
690 GLint x
, GLint y
, GLint width
, GLint height
)
692 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
693 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
695 /* sanity check - we only have a front-left buffer */
696 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
| DD_BACK_LEFT_BIT
| DD_BACK_RIGHT_BIT
)) == 0);
697 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
698 if (mask
& DD_FRONT_LEFT_BIT
) {
699 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
701 /* Clear whole CI buffer */
702 #if CHAN_TYPE == GL_UNSIGNED_BYTE
703 MEMSET(osmesa
->buffer
, ctx
->Color
.ClearIndex
,
704 osmesa
->rowlength
* osmesa
->height
);
706 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
707 GLchan
*buffer
= (GLchan
*) osmesa
->buffer
;
709 for (i
= 0; i
< n
; i
++) {
710 buffer
[i
] = ctx
->Color
.ClearIndex
;
715 /* Clear part of CI buffer */
716 const GLchan clearIndex
= (GLchan
) ctx
->Color
.ClearIndex
;
718 for (i
= 0; i
< height
; i
++) {
719 GLchan
*ptr1
= PIXELADDR1(x
, (y
+ i
));
720 for (j
= 0; j
< width
; j
++) {
721 *ptr1
++ = clearIndex
;
726 else if (osmesa
->format
== OSMESA_RGB
) {
727 const GLchan r
= ctx
->Color
.ClearColor
[0];
728 const GLchan g
= ctx
->Color
.ClearColor
[1];
729 const GLchan b
= ctx
->Color
.ClearColor
[2];
731 /* Clear whole RGB buffer */
732 GLuint n
= osmesa
->rowlength
* osmesa
->height
;
733 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
735 for (i
= 0; i
< n
; i
++) {
736 PACK_RGB(ptr3
, r
, g
, b
);
741 /* Clear part of RGB buffer */
743 for (i
= 0; i
< height
; i
++) {
744 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
745 for (j
= 0; j
< width
; j
++) {
746 PACK_RGB(ptr3
, r
, g
, b
);
752 else if (osmesa
->format
== OSMESA_BGR
) {
753 const GLchan r
= ctx
->Color
.ClearColor
[0];
754 const GLchan g
= ctx
->Color
.ClearColor
[1];
755 const GLchan b
= ctx
->Color
.ClearColor
[2];
757 /* Clear whole RGB buffer */
758 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
759 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
761 for (i
= 0; i
< n
; i
++) {
762 PACK_BGR(ptr3
, r
, g
, b
);
767 /* Clear part of RGB buffer */
769 for (i
= 0; i
< height
; i
++) {
770 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
771 for (j
= 0; j
< width
; j
++) {
772 PACK_BGR(ptr3
, r
, g
, b
);
778 else if (osmesa
->format
== OSMESA_RGB_565
) {
779 const GLchan r
= ctx
->Color
.ClearColor
[0];
780 const GLchan g
= ctx
->Color
.ClearColor
[1];
781 const GLchan b
= ctx
->Color
.ClearColor
[2];
783 PACK_RGB_565(clearPixel
, r
, g
, b
);
785 /* Clear whole RGB buffer */
786 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
787 GLushort
*ptr2
= (GLushort
*) osmesa
->buffer
;
789 for (i
= 0; i
< n
; i
++) {
795 /* clear scissored region */
797 for (i
= 0; i
< height
; i
++) {
798 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, (y
+ i
));
799 for (j
= 0; j
< width
; j
++) {
807 #if CHAN_TYPE == GL_UNSIGNED_BYTE
808 /* 4-byte pixel value */
810 GLchan
*clr
= (GLchan
*) &clearPixel
;
811 clr
[osmesa
->rInd
] = ctx
->Color
.ClearColor
[0];
812 clr
[osmesa
->gInd
] = ctx
->Color
.ClearColor
[1];
813 clr
[osmesa
->bInd
] = ctx
->Color
.ClearColor
[2];
814 clr
[osmesa
->aInd
] = ctx
->Color
.ClearColor
[3];
816 /* Clear whole RGBA buffer */
817 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
818 GLuint
*ptr4
= (GLuint
*) osmesa
->buffer
;
821 for (i
= 0; i
< n
; i
++) {
822 *ptr4
++ = clearPixel
;
826 BZERO(ptr4
, n
* sizeof(GLuint
));
830 /* Clear part of RGBA buffer */
832 for (i
= 0; i
< height
; i
++) {
833 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, (y
+ i
));
834 for (j
= 0; j
< width
; j
++) {
835 *ptr4
++ = clearPixel
;
840 const GLchan r
= ctx
->Color
.ClearColor
[0];
841 const GLchan g
= ctx
->Color
.ClearColor
[1];
842 const GLchan b
= ctx
->Color
.ClearColor
[2];
843 const GLchan a
= ctx
->Color
.ClearColor
[3];
845 /* Clear whole RGBA buffer */
846 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
847 GLchan
*p
= (GLchan
*) osmesa
->buffer
;
849 for (i
= 0; i
< n
; i
++) {
850 PACK_RGBA(p
, r
, g
, b
, a
);
855 /* Clear part of RGBA buffer */
857 for (i
= 0; i
< height
; i
++) {
858 GLchan
*p
= PIXELADDR4(x
, (y
+ i
));
859 for (j
= 0; j
< width
; j
++) {
860 PACK_RGBA(p
, r
, g
, b
, a
);
868 mask
&= ~DD_FRONT_LEFT_BIT
;
873 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
878 static void buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
880 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
881 *width
= osmesa
->width
;
882 *height
= osmesa
->height
;
886 /**********************************************************************/
887 /***** Read/write spans/arrays of RGBA pixels *****/
888 /**********************************************************************/
890 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
892 write_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
893 CONST GLchan rgba
[][4], const GLubyte mask
[] )
895 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
896 GLchan
*p
= PIXELADDR4(x
, y
);
899 for (i
= 0; i
< n
; i
++, p
+= 4) {
901 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
902 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
907 for (i
= 0; i
< n
; i
++, p
+= 4) {
908 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
909 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
915 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
917 write_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
918 CONST GLchan rgba
[][4], const GLubyte mask
[] )
920 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
921 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
922 const GLuint
*rgba4
= (const GLuint
*) rgba
;
924 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
926 for (i
= 0; i
< n
; i
++) {
933 MEMCPY( ptr4
, rgba4
, n
* 4 );
938 /* Write RGB pixels to an RGBA (or permuted) buffer. */
940 write_rgb_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
941 CONST GLchan rgb
[][3], const GLubyte mask
[] )
943 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
944 GLchan
*p
= PIXELADDR4(x
, y
);
947 for (i
= 0; i
< n
; i
++, p
+=4) {
949 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
954 for (i
= 0; i
< n
; i
++, p
+=4) {
955 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
963 write_monocolor_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
964 const GLchan color
[4], const GLubyte mask
[] )
966 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
967 GLchan
*p
= PIXELADDR4(x
, y
);
969 for (i
= 0; i
< n
; i
++, p
+= 4) {
971 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
979 write_rgba_pixels( const GLcontext
*ctx
, GLuint n
,
980 const GLint x
[], const GLint y
[],
981 CONST GLchan rgba
[][4], const GLubyte mask
[] )
983 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
985 for (i
= 0; i
< n
; i
++) {
987 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
988 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
989 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
997 write_monocolor_pixels( const GLcontext
*ctx
, GLuint n
,
998 const GLint x
[], const GLint y
[],
999 const GLchan color
[4], const GLubyte mask
[] )
1001 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1003 for (i
= 0; i
< n
; i
++) {
1005 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1006 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
1013 read_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1016 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1018 GLchan
*p
= PIXELADDR4(x
, y
);
1019 for (i
= 0; i
< n
; i
++, p
+= 4) {
1020 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1021 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1022 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1023 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1028 /* Read RGBA pixels from an RGBA buffer */
1030 read_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1033 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1034 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
1035 MEMCPY( rgba
, ptr4
, n
* 4 * sizeof(GLchan
) );
1040 read_rgba_pixels( const GLcontext
*ctx
,
1041 GLuint n
, const GLint x
[], const GLint y
[],
1042 GLchan rgba
[][4], const GLubyte mask
[] )
1044 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1046 for (i
= 0; i
< n
; i
++) {
1048 const GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1049 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1050 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1051 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1052 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1057 /**********************************************************************/
1058 /***** 3 byte RGB pixel support funcs *****/
1059 /**********************************************************************/
1061 /* Write RGBA pixels to an RGB buffer. */
1063 write_rgba_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1064 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1066 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1067 GLchan
*p
= PIXELADDR3(x
, y
);
1070 for (i
= 0; i
< n
; i
++, p
+= 3) {
1072 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1077 for (i
= 0; i
< n
; i
++, p
+= 3) {
1078 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1083 /* Write RGBA pixels to an BGR buffer. */
1085 write_rgba_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1086 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1088 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1089 GLchan
*p
= PIXELADDR3(x
, y
);
1092 for (i
= 0; i
< n
; i
++, p
+= 3) {
1094 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1099 for (i
= 0; i
< n
; i
++, p
+= 3) {
1100 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1105 /* Write RGB pixels to an RGB buffer. */
1107 write_rgb_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1108 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1110 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1111 GLchan
*p
= PIXELADDR3(x
, y
);
1114 for (i
= 0; i
< n
; i
++, p
+= 3) {
1116 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1121 for (i
= 0; i
< n
; i
++, p
+= 3) {
1122 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1127 /* Write RGB pixels to an BGR buffer. */
1129 write_rgb_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1130 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1132 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1133 GLchan
*p
= PIXELADDR3(x
, y
);
1136 for (i
= 0; i
< n
; i
++, p
+= 3) {
1138 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1143 for (i
= 0; i
< n
; i
++, p
+= 3) {
1144 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1151 write_monocolor_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1152 const GLchan color
[4], const GLubyte mask
[] )
1154 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1155 GLchan
*p
= PIXELADDR3(x
, y
);
1157 for (i
= 0; i
< n
; i
++, p
+= 3) {
1159 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1165 write_monocolor_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1166 const GLchan color
[4], const GLubyte mask
[] )
1168 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1169 GLchan
*p
= PIXELADDR3(x
, y
);
1171 for (i
= 0; i
< n
; i
++, p
+= 3) {
1173 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1179 write_rgba_pixels_RGB( const GLcontext
*ctx
, GLuint n
,
1180 const GLint x
[], const GLint y
[],
1181 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1183 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1185 for (i
= 0; i
< n
; i
++) {
1187 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1188 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1194 write_rgba_pixels_BGR( const GLcontext
*ctx
, GLuint n
,
1195 const GLint x
[], const GLint y
[],
1196 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1198 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1200 for (i
= 0; i
< n
; i
++) {
1202 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1203 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1209 write_monocolor_pixels_RGB( const GLcontext
*ctx
,
1210 GLuint n
, const GLint x
[], const GLint y
[],
1211 const GLchan color
[4], const GLubyte mask
[] )
1213 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1215 for (i
= 0; i
< n
; i
++) {
1217 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1218 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1224 write_monocolor_pixels_BGR( const GLcontext
*ctx
,
1225 GLuint n
, const GLint x
[], const GLint y
[],
1226 const GLchan color
[4], const GLubyte mask
[] )
1228 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1230 for (i
= 0; i
< n
; i
++) {
1232 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1233 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1239 read_rgba_span3( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1242 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1244 const GLchan
*p
= PIXELADDR3(x
, y
);
1245 for (i
= 0; i
< n
; i
++, p
+= 3) {
1246 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1247 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1248 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1249 rgba
[i
][ACOMP
] = CHAN_MAX
;
1254 read_rgba_pixels3( const GLcontext
*ctx
,
1255 GLuint n
, const GLint x
[], const GLint y
[],
1256 GLchan rgba
[][4], const GLubyte mask
[] )
1258 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1260 for (i
= 0; i
< n
; i
++) {
1262 const GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1263 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1264 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1265 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1266 rgba
[i
][ACOMP
] = CHAN_MAX
;
1272 /**********************************************************************/
1273 /***** 2 byte RGB pixel support funcs *****/
1274 /**********************************************************************/
1276 /* Write RGBA pixels to an RGB_565 buffer. */
1278 write_rgba_span2( const GLcontext
*ctx
,
1279 GLuint n
, GLint x
, GLint y
,
1280 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1282 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1283 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1286 for (i
= 0; i
< n
; i
++, ptr2
++) {
1288 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1293 for (i
= 0; i
< n
; i
++, ptr2
++) {
1294 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1300 /* Write RGB pixels to an RGB_565 buffer. */
1302 write_rgb_span2( const GLcontext
*ctx
,
1303 GLuint n
, GLint x
, GLint y
,
1304 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1306 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1307 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1310 for (i
= 0; i
< n
; i
++, ptr2
++) {
1312 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1317 for (i
= 0; i
< n
; i
++, ptr2
++) {
1318 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1325 write_monocolor_span2( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1326 const GLchan color
[4], const GLubyte mask
[] )
1328 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1330 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1332 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1333 for (i
= 0; i
< n
; i
++, ptr2
++) {
1342 write_rgba_pixels2( const GLcontext
*ctx
,
1343 GLuint n
, const GLint x
[], const GLint y
[],
1344 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1346 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1348 for (i
= 0; i
< n
; i
++) {
1350 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1351 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1357 write_monocolor_pixels2( const GLcontext
*ctx
,
1358 GLuint n
, const GLint x
[], const GLint y
[],
1359 const GLchan color
[4], const GLubyte mask
[] )
1361 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1364 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1365 for (i
= 0; i
< n
; i
++) {
1367 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1374 read_rgba_span2( const GLcontext
*ctx
,
1375 GLuint n
, GLint x
, GLint y
,
1378 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1380 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
, y
);
1381 for (i
= 0; i
< n
; i
++, ptr2
++) {
1382 /* This should be fixed to get the low bits right */
1383 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFe;
1384 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFc;
1385 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFe;
1391 read_rgba_pixels2( const GLcontext
*ctx
,
1392 GLuint n
, const GLint x
[], const GLint y
[],
1393 GLchan rgba
[][4], const GLubyte mask
[] )
1395 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1397 for (i
= 0; i
< n
; i
++) {
1399 /* This should be fixed to get the low bits right */
1400 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1401 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFE;
1402 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFC;
1403 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFE;
1411 /**********************************************************************/
1412 /***** Read/write spans/arrays of CI pixels *****/
1413 /**********************************************************************/
1415 /* Write 32-bit color index to buffer */
1417 write_index32_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1418 const GLuint index
[], const GLubyte mask
[] )
1420 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1421 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1424 for (i
=0;i
<n
;i
++,ptr1
++) {
1426 *ptr1
= (GLchan
) index
[i
];
1431 for (i
=0;i
<n
;i
++,ptr1
++) {
1432 *ptr1
= (GLchan
) index
[i
];
1438 /* Write 8-bit color index to buffer */
1440 write_index8_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1441 const GLubyte index
[], const GLubyte mask
[] )
1443 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1444 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1447 for (i
=0;i
<n
;i
++,ptr1
++) {
1449 *ptr1
= (GLchan
) index
[i
];
1454 MEMCPY(ptr1
, index
, n
* sizeof(GLchan
));
1460 write_monoindex_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1461 GLuint colorIndex
, const GLubyte mask
[] )
1463 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1464 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1466 for (i
=0;i
<n
;i
++,ptr1
++) {
1468 *ptr1
= (GLchan
) colorIndex
;
1475 write_index_pixels( const GLcontext
*ctx
,
1476 GLuint n
, const GLint x
[], const GLint y
[],
1477 const GLuint index
[], const GLubyte mask
[] )
1479 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1483 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1484 *ptr1
= (GLchan
) index
[i
];
1491 write_monoindex_pixels( const GLcontext
*ctx
,
1492 GLuint n
, const GLint x
[], const GLint y
[],
1493 GLuint colorIndex
, const GLubyte mask
[] )
1495 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1499 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1500 *ptr1
= (GLchan
) colorIndex
;
1507 read_index_span( const GLcontext
*ctx
,
1508 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1510 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1512 const GLchan
*ptr1
= (const GLchan
*) PIXELADDR1(x
, y
);
1513 for (i
=0;i
<n
;i
++,ptr1
++) {
1514 index
[i
] = (GLuint
) *ptr1
;
1520 read_index_pixels( const GLcontext
*ctx
,
1521 GLuint n
, const GLint x
[], const GLint y
[],
1522 GLuint index
[], const GLubyte mask
[] )
1524 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1528 const GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1529 index
[i
] = (GLuint
) *ptr1
;
1536 /**********************************************************************/
1537 /***** Optimized line rendering *****/
1538 /**********************************************************************/
1542 * Draw a flat-shaded, RGB line into an osmesa buffer.
1545 flat_rgba_line( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1547 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1548 const GLchan
*color
= vert1
->color
;
1552 #define PLOT(X, Y) \
1554 GLchan *p = PIXELADDR4(X, Y); \
1555 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1559 #include "..\swrast\s_linetemp.h"
1561 #include "swrast/s_linetemp.h"
1567 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1570 flat_rgba_z_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1572 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1573 const GLchan
*color
= vert1
->color
;
1577 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1579 #define PLOT(X, Y) \
1582 GLchan *p = PIXELADDR4(X, Y); \
1583 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1584 color[BCOMP], color[ACOMP]); \
1591 #include "..\swrast\s_linetemp.h"
1593 #include "swrast/s_linetemp.h"
1599 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1600 * XXX update for GLchan
1603 flat_blend_rgba_line( GLcontext
*ctx
,
1604 const SWvertex
*vert0
, const SWvertex
*vert1
)
1606 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1607 const GLint rshift
= osmesa
->rshift
;
1608 const GLint gshift
= osmesa
->gshift
;
1609 const GLint bshift
= osmesa
->bshift
;
1610 const GLint avalue
= vert0
->color
[3];
1611 const GLint msavalue
= CHAN_MAX
- avalue
;
1612 const GLint rvalue
= vert1
->color
[0]*avalue
;
1613 const GLint gvalue
= vert1
->color
[1]*avalue
;
1614 const GLint bvalue
= vert1
->color
[2]*avalue
;
1619 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1621 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1622 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1623 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1627 #if 0 /* XXX use this in the future */
1630 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1631 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1632 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1633 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1634 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1639 #include "..\swrast\s_linetemp.h"
1641 #include "swrast/s_linetemp.h"
1647 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1648 * But don't write to Z buffer.
1649 * XXX update for GLchan
1652 flat_blend_rgba_z_line( GLcontext
*ctx
,
1653 const SWvertex
*vert0
, const SWvertex
*vert1
)
1655 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1656 const GLint rshift
= osmesa
->rshift
;
1657 const GLint gshift
= osmesa
->gshift
;
1658 const GLint bshift
= osmesa
->bshift
;
1659 const GLint avalue
= vert0
->color
[3];
1660 const GLint msavalue
= 256 - avalue
;
1661 const GLint rvalue
= vert1
->color
[0]*avalue
;
1662 const GLint gvalue
= vert1
->color
[1]*avalue
;
1663 const GLint bvalue
= vert1
->color
[2]*avalue
;
1667 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1671 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1673 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1674 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1675 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1679 #if 0 /* XXX use this in the future */
1682 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1683 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1684 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1685 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1686 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1691 #include "..\swrast\s_linetemp.h"
1693 #include "swrast/s_linetemp.h"
1699 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1700 * XXX update for GLchan
1703 flat_blend_rgba_z_line_write( GLcontext
*ctx
,
1704 const SWvertex
*vert0
, const SWvertex
*vert1
)
1706 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1707 const GLint rshift
= osmesa
->rshift
;
1708 const GLint gshift
= osmesa
->gshift
;
1709 const GLint bshift
= osmesa
->bshift
;
1710 const GLint avalue
= vert0
->color
[3];
1711 const GLint msavalue
= 256 - avalue
;
1712 const GLint rvalue
= vert1
->color
[0]*avalue
;
1713 const GLint gvalue
= vert1
->color
[1]*avalue
;
1714 const GLint bvalue
= vert1
->color
[2]*avalue
;
1718 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1722 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1724 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1725 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1726 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1731 #if 0 /* XXX use this in the future */
1734 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1735 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1736 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1737 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1738 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1744 #include "..\swrast\s_linetemp.h"
1746 #include "swrast/s_linetemp.h"
1752 * Analyze context state to see if we can provide a fast line drawing
1753 * function, like those in lines.c. Otherwise, return NULL.
1755 static swrast_line_func
1756 osmesa_choose_line_function( GLcontext
*ctx
)
1758 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1759 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1761 if (CHAN_BITS
!= 8) return NULL
;
1762 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
1763 if (ctx
->Line
.SmoothFlag
) return NULL
;
1764 if (ctx
->Texture
._ReallyEnabled
) return NULL
;
1765 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
1766 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
1767 if (ctx
->Line
.StippleFlag
) return NULL
;
1768 if (ctx
->Line
.SmoothFlag
) return NULL
;
1769 if (osmesa
->format
!= OSMESA_RGBA
&&
1770 osmesa
->format
!= OSMESA_BGRA
&&
1771 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
1773 if (swrast
->_RasterMask
==DEPTH_BIT
1774 && ctx
->Depth
.Func
==GL_LESS
1775 && ctx
->Depth
.Mask
==GL_TRUE
1776 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1777 return (swrast_line_func
) flat_rgba_z_line
;
1780 if (swrast
->_RasterMask
== 0) {
1781 return (swrast_line_func
) flat_rgba_line
;
1784 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1785 && ctx
->Depth
.Func
==GL_LESS
1786 && ctx
->Depth
.Mask
==GL_TRUE
1787 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1788 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1789 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1790 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1791 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1792 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1793 return (swrast_line_func
) flat_blend_rgba_z_line_write
;
1796 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1797 && ctx
->Depth
.Func
==GL_LESS
1798 && ctx
->Depth
.Mask
==GL_FALSE
1799 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1800 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1801 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1802 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1803 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1804 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1805 return (swrast_line_func
) flat_blend_rgba_z_line
;
1808 if (swrast
->_RasterMask
==BLEND_BIT
1809 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1810 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1811 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1812 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1813 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1814 return (swrast_line_func
) flat_blend_rgba_line
;
1817 return (swrast_line_func
) NULL
;
1821 /**********************************************************************/
1822 /***** Optimized triangle rendering *****/
1823 /**********************************************************************/
1827 * Smooth-shaded, z-less triangle, RGBA color.
1829 static void smooth_rgba_z_triangle( GLcontext
*ctx
,
1832 const SWvertex
*v2
)
1834 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1837 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1838 #define INTERP_RGB 1
1839 #define INTERP_ALPHA 1
1840 #define RENDER_SPAN( span ) \
1842 GLchan *img = PIXELADDR4(span.x, span.y); \
1843 for (i = 0; i < span.end; i++, img += 4) { \
1844 const GLdepth z = FixedToDepth(span.z); \
1845 if (z < zRow[i]) { \
1846 PACK_RGBA(img, FixedToChan(span.red), \
1847 FixedToChan(span.green), FixedToChan(span.blue), \
1848 FixedToChan(span.alpha)); \
1851 span.red += span.redStep; \
1852 span.green += span.greenStep; \
1853 span.blue += span.blueStep; \
1854 span.alpha += span.alphaStep; \
1855 span.z += span.zStep; \
1859 #include "..\swrast\s_tritemp.h"
1861 #include "swrast/s_tritemp.h"
1869 * Flat-shaded, z-less triangle, RGBA color.
1871 static void flat_rgba_z_triangle( GLcontext
*ctx
,
1874 const SWvertex
*v2
)
1876 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1878 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1879 #define SETUP_CODE \
1881 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
1882 v2->color[2], v2->color[3]);
1884 #define RENDER_SPAN( span ) \
1886 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1887 for (i = 0; i < span.end; i++) { \
1888 const GLdepth z = FixedToDepth(span.z); \
1889 if (z < zRow[i]) { \
1893 span.z += span.zStep; \
1897 #include "..\swrast\s_tritemp.h"
1899 #include "swrast/s_tritemp.h"
1906 * Return pointer to an accelerated triangle function if possible.
1908 static swrast_tri_func
1909 osmesa_choose_triangle_function( GLcontext
*ctx
)
1911 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1912 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1914 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
1915 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
1916 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
1917 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
1918 if (ctx
->Texture
._ReallyEnabled
) return (swrast_tri_func
) NULL
;
1919 if (osmesa
->format
!= OSMESA_RGBA
&&
1920 osmesa
->format
!= OSMESA_BGRA
&&
1921 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
1923 if (swrast
->_RasterMask
== DEPTH_BIT
&&
1924 ctx
->Depth
.Func
== GL_LESS
&&
1925 ctx
->Depth
.Mask
== GL_TRUE
&&
1926 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1927 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
1928 return (swrast_tri_func
) smooth_rgba_z_triangle
;
1931 return (swrast_tri_func
) flat_rgba_z_triangle
;
1934 return (swrast_tri_func
) NULL
;
1939 /* Override for the swrast triangle-selection function. Try to use one
1940 * of our internal triangle functions, otherwise fall back to the
1941 * standard swrast functions.
1943 static void osmesa_choose_triangle( GLcontext
*ctx
)
1945 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1947 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
1948 if (!swrast
->Triangle
)
1949 _swrast_choose_triangle( ctx
);
1952 static void osmesa_choose_line( GLcontext
*ctx
)
1954 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1956 swrast
->Line
= osmesa_choose_line_function( ctx
);
1958 _swrast_choose_line( ctx
);
1962 #define OSMESA_NEW_LINE (_NEW_LINE | \
1967 _SWRAST_NEW_RASTERMASK)
1969 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1974 _SWRAST_NEW_RASTERMASK)
1977 /* Extend the software rasterizer with our line and triangle
1980 static void osmesa_register_swrast_functions( GLcontext
*ctx
)
1982 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1984 swrast
->choose_line
= osmesa_choose_line
;
1985 swrast
->choose_triangle
= osmesa_choose_triangle
;
1987 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
1988 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1992 static const GLubyte
*get_string( GLcontext
*ctx
, GLenum name
)
1998 return (const GLubyte
*) "Mesa OffScreen32";
1999 #elif CHAN_BITS == 16
2000 return (const GLubyte
*) "Mesa OffScreen16";
2002 return (const GLubyte
*) "Mesa OffScreen";
2010 static void osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
2012 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
2013 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
2014 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
2016 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
2019 * XXX these function pointers could be initialized just once during
2020 * context creation since they don't depend on any state changes.
2023 ctx
->Driver
.GetString
= get_string
;
2024 ctx
->Driver
.UpdateState
= osmesa_update_state
;
2025 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
2026 ctx
->Driver
.ResizeBuffersMESA
= _swrast_alloc_buffers
;
2027 ctx
->Driver
.GetBufferSize
= buffer_size
;
2029 ctx
->Driver
.Accum
= _swrast_Accum
;
2030 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
2031 ctx
->Driver
.Clear
= clear
;
2032 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
2033 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
2034 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
2036 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
2037 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
2038 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
2039 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
2040 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
2041 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
2042 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
2043 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
2045 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
2046 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
2047 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
2048 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
2049 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
2050 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
2051 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
2052 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
2053 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
2055 ctx
->Driver
.BaseCompressedTexFormat
= _mesa_base_compressed_texformat
;
2056 ctx
->Driver
.CompressedTextureSize
= _mesa_compressed_texture_size
;
2057 ctx
->Driver
.GetCompressedTexImage
= _mesa_get_compressed_teximage
;
2059 /* RGB(A) span/pixel functions */
2060 if (osmesa
->format
== OSMESA_RGB
) {
2061 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
2062 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
2063 swdd
->WriteMonoRGBASpan
= write_monocolor_span_RGB
;
2064 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
2065 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_RGB
;
2066 swdd
->ReadRGBASpan
= read_rgba_span3
;
2067 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2069 else if (osmesa
->format
== OSMESA_BGR
) {
2070 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
2071 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
2072 swdd
->WriteMonoRGBASpan
= write_monocolor_span_BGR
;
2073 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
2074 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_BGR
;
2075 swdd
->ReadRGBASpan
= read_rgba_span3
;
2076 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2078 else if (osmesa
->format
== OSMESA_RGB_565
) {
2079 swdd
->WriteRGBASpan
= write_rgba_span2
;
2080 swdd
->WriteRGBSpan
= write_rgb_span2
;
2081 swdd
->WriteMonoRGBASpan
= write_monocolor_span2
;
2082 swdd
->WriteRGBAPixels
= write_rgba_pixels2
;
2083 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels2
;
2084 swdd
->ReadRGBASpan
= read_rgba_span2
;
2085 swdd
->ReadRGBAPixels
= read_rgba_pixels2
;
2088 /* 4 GLchan / pixel in frame buffer */
2089 swdd
->WriteRGBSpan
= write_rgb_span
;
2090 swdd
->WriteRGBAPixels
= write_rgba_pixels
;
2091 swdd
->WriteMonoRGBASpan
= write_monocolor_span
;
2092 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels
;
2093 if (osmesa
->format
== OSMESA_RGBA
&&
2094 CHAN_TYPE
== GL_UNSIGNED_BYTE
&&
2095 RCOMP
==0 && GCOMP
==1 && BCOMP
==2 && ACOMP
==3) {
2096 /* special, fast case */
2097 swdd
->WriteRGBASpan
= write_rgba_span_rgba
;
2098 swdd
->ReadRGBASpan
= read_rgba_span_rgba
;
2101 swdd
->WriteRGBASpan
= write_rgba_span
;
2102 swdd
->ReadRGBASpan
= read_rgba_span
;
2104 swdd
->ReadRGBAPixels
= read_rgba_pixels
;
2107 /* CI span/pixel functions */
2108 swdd
->WriteCI32Span
= write_index32_span
;
2109 swdd
->WriteCI8Span
= write_index8_span
;
2110 swdd
->WriteMonoCISpan
= write_monoindex_span
;
2111 swdd
->WriteCI32Pixels
= write_index_pixels
;
2112 swdd
->WriteMonoCIPixels
= write_monoindex_pixels
;
2113 swdd
->ReadCI32Span
= read_index_span
;
2114 swdd
->ReadCI32Pixels
= read_index_pixels
;
2116 swdd
->SetReadBuffer
= set_read_buffer
;
2118 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
2120 _swrast_InvalidateState( ctx
, new_state
);
2121 _swsetup_InvalidateState( ctx
, new_state
);
2122 _ac_InvalidateState( ctx
, new_state
);
2123 _tnl_InvalidateState( ctx
, new_state
);