1 /* $Id: osmesa.c,v 1.70 2001/09/23 21:17:03 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 GLAPI 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 GLAPI 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;
139 const GLubyte
*i1
= (GLubyte
*) &i4
;
140 const GLint little_endian
= *i1
;
142 rind
= gind
= bind
= aind
= 0;
143 if (format
==OSMESA_COLOR_INDEX
) {
145 rshift
= gshift
= bshift
= ashift
= 0;
148 else if (format
==OSMESA_RGBA
) {
151 greenBits
= CHAN_BITS
;
152 blueBits
= CHAN_BITS
;
153 alphaBits
= CHAN_BITS
;
172 else if (format
==OSMESA_BGRA
) {
175 greenBits
= CHAN_BITS
;
176 blueBits
= CHAN_BITS
;
177 alphaBits
= CHAN_BITS
;
196 else if (format
==OSMESA_ARGB
) {
199 greenBits
= CHAN_BITS
;
200 blueBits
= CHAN_BITS
;
201 alphaBits
= CHAN_BITS
;
220 else if (format
==OSMESA_RGB
) {
223 greenBits
= CHAN_BITS
;
224 blueBits
= CHAN_BITS
;
235 else if (format
==OSMESA_BGR
) {
238 greenBits
= CHAN_BITS
;
239 blueBits
= CHAN_BITS
;
250 else if (format
==OSMESA_RGB_565
) {
260 rind
= 0; /* not used */
270 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
272 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
273 GL_FALSE
, /* double buffer */
274 GL_FALSE
, /* stereo */
285 alphaBits
? accumBits
: 0,
288 if (!osmesa
->gl_visual
) {
293 if (!_mesa_initialize_context(&osmesa
->gl_ctx
,
295 sharelist
? &sharelist
->gl_ctx
296 : (GLcontext
*) NULL
,
297 (void *) osmesa
, GL_TRUE
)) {
298 _mesa_destroy_visual( osmesa
->gl_visual
);
303 _mesa_enable_sw_extensions(&(osmesa
->gl_ctx
));
304 _mesa_enable_1_3_extensions(&(osmesa
->gl_ctx
));
306 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
307 (GLboolean
) ( osmesa
->gl_visual
->depthBits
> 0 ),
308 (GLboolean
) ( osmesa
->gl_visual
->stencilBits
> 0 ),
309 (GLboolean
) ( osmesa
->gl_visual
->accumRedBits
> 0 ),
310 GL_FALSE
/* s/w alpha */ );
312 if (!osmesa
->gl_buffer
) {
313 _mesa_destroy_visual( osmesa
->gl_visual
);
314 _mesa_free_context_data( &osmesa
->gl_ctx
);
318 osmesa
->format
= format
;
319 osmesa
->buffer
= NULL
;
322 osmesa
->userRowLength
= 0;
323 osmesa
->rowlength
= 0;
324 osmesa
->yup
= GL_TRUE
;
325 osmesa
->rshift
= rshift
;
326 osmesa
->gshift
= gshift
;
327 osmesa
->bshift
= bshift
;
328 osmesa
->ashift
= ashift
;
335 /* Initialize the software rasterizer and helper modules.
338 GLcontext
*ctx
= &osmesa
->gl_ctx
;
340 _swrast_CreateContext( ctx
);
341 _ac_CreateContext( ctx
);
342 _tnl_CreateContext( ctx
);
343 _swsetup_CreateContext( ctx
);
345 _swsetup_Wakeup( ctx
);
346 osmesa_register_swrast_functions( ctx
);
356 * Destroy an Off-Screen Mesa rendering context.
358 * Input: ctx - the context to destroy
360 GLAPI
void GLAPIENTRY
OSMesaDestroyContext( OSMesaContext ctx
)
363 _swsetup_DestroyContext( &ctx
->gl_ctx
);
364 _tnl_DestroyContext( &ctx
->gl_ctx
);
365 _ac_DestroyContext( &ctx
->gl_ctx
);
366 _swrast_DestroyContext( &ctx
->gl_ctx
);
368 _mesa_destroy_visual( ctx
->gl_visual
);
369 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
370 _mesa_free_context_data( &ctx
->gl_ctx
);
378 * Recompute the values of the context's rowaddr array.
380 static void compute_row_addresses( OSMesaContext ctx
)
382 GLint bytesPerPixel
, bytesPerRow
, i
;
383 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
385 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
387 bytesPerPixel
= 1 * sizeof(GLchan
);
389 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
391 bytesPerPixel
= 3 * sizeof(GLchan
);
393 else if (ctx
->format
== OSMESA_RGB_565
) {
394 /* 5/6/5 RGB pixel in 16 bits */
399 bytesPerPixel
= 4 * sizeof(GLchan
);
402 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
405 /* Y=0 is bottom line of window */
406 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
407 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
411 /* Y=0 is top line of window */
412 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
413 GLint j
= ctx
->height
- i
- 1;
414 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
421 * Bind an OSMesaContext to an image buffer. The image buffer is just a
422 * block of memory which the client provides. Its size must be at least
423 * as large as width*height*sizeof(type). Its address should be a multiple
424 * of 4 if using RGBA mode.
426 * Image data is stored in the order of glDrawPixels: row-major order
427 * with the lower-left image pixel stored in the first array position
428 * (ie. bottom-to-top).
430 * Since the only type initially supported is GL_UNSIGNED_BYTE, if the
431 * context is in RGBA mode, each pixel will be stored as a 4-byte RGBA
432 * value. If the context is in color indexed mode, each pixel will be
433 * stored as a 1-byte value.
435 * If the context's viewport hasn't been initialized yet, it will now be
436 * initialized to (0,0,width,height).
438 * Input: ctx - the rendering context
439 * buffer - the image buffer memory
440 * type - data type for pixel components, only GL_UNSIGNED_BYTE
441 * and GL_UNSIGNED_SHORT_5_6_5 supported now.
442 * width, height - size of image buffer in pixels, at least 1
443 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
444 * invalid buffer address, type!=GL_UNSIGNED_BYTE, width<1, height<1,
445 * width>internal limit or height>internal limit.
447 GLAPI GLboolean GLAPIENTRY
448 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
449 GLsizei width
, GLsizei height
)
451 if (!ctx
|| !buffer
||
452 width
< 1 || height
< 1 ||
453 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
457 if (ctx
->format
== OSMESA_RGB_565
) {
458 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
461 else if (type
!= CHAN_TYPE
) {
465 osmesa_update_state( &ctx
->gl_ctx
, 0 );
466 _mesa_make_current( &ctx
->gl_ctx
, ctx
->gl_buffer
);
468 ctx
->buffer
= buffer
;
470 ctx
->height
= height
;
471 if (ctx
->userRowLength
)
472 ctx
->rowlength
= ctx
->userRowLength
;
474 ctx
->rowlength
= width
;
476 compute_row_addresses( ctx
);
479 if (ctx
->gl_ctx
.Viewport
.Width
==0) {
480 /* initialize viewport and scissor box to buffer size */
481 _mesa_Viewport( 0, 0, width
, height
);
482 ctx
->gl_ctx
.Scissor
.Width
= width
;
483 ctx
->gl_ctx
.Scissor
.Height
= height
;
491 GLAPI OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
493 GLcontext
*ctx
= _mesa_get_current_context();
495 return (OSMesaContext
) ctx
;
502 GLAPI
void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
504 OSMesaContext ctx
= OSMesaGetCurrentContext();
507 case OSMESA_ROW_LENGTH
:
509 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_VALUE
,
510 "OSMesaPixelStore(value)" );
513 ctx
->userRowLength
= value
;
514 ctx
->rowlength
= value
;
517 ctx
->yup
= value
? GL_TRUE
: GL_FALSE
;
520 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
524 compute_row_addresses( ctx
);
528 GLAPI
void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
530 OSMesaContext ctx
= OSMesaGetCurrentContext();
537 *value
= ctx
->height
;
540 *value
= ctx
->format
;
545 case OSMESA_ROW_LENGTH
:
546 *value
= ctx
->rowlength
;
551 case OSMESA_MAX_WIDTH
:
554 case OSMESA_MAX_HEIGHT
:
558 _mesa_error(&ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
564 * Return the depth buffer associated with an OSMesa context.
565 * Input: c - the OSMesa context
566 * Output: width, height - size of buffer in pixels
567 * bytesPerValue - bytes per depth value (2 or 4)
568 * buffer - pointer to depth buffer values
569 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
571 GLAPI GLboolean GLAPIENTRY
572 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
573 GLint
*bytesPerValue
, void **buffer
)
575 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
583 *width
= c
->gl_buffer
->Width
;
584 *height
= c
->gl_buffer
->Height
;
585 if (c
->gl_visual
->depthBits
<= 16)
586 *bytesPerValue
= sizeof(GLushort
);
588 *bytesPerValue
= sizeof(GLuint
);
589 *buffer
= c
->gl_buffer
->DepthBuffer
;
595 * Return the color buffer associated with an OSMesa context.
596 * Input: c - the OSMesa context
597 * Output: width, height - size of buffer in pixels
598 * format - the pixel format (OSMESA_FORMAT)
599 * buffer - pointer to color buffer values
600 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
602 GLAPI GLboolean GLAPIENTRY
603 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
604 GLint
*height
, GLint
*format
, void **buffer
)
622 /**********************************************************************/
623 /*** Device Driver Functions ***/
624 /**********************************************************************/
631 #define PACK_RGBA(DST, R, G, B, A) \
633 (DST)[osmesa->rInd] = R; \
634 (DST)[osmesa->gInd] = G; \
635 (DST)[osmesa->bInd] = B; \
636 (DST)[osmesa->aInd] = A; \
639 #define PACK_RGB(DST, R, G, B) \
646 #define PACK_BGR(DST, R, G, B) \
653 #define PACK_RGB_565(DST, R, G, B) \
655 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
659 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
660 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
661 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
662 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
665 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
666 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
667 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
668 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
672 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum mode
)
675 if (mode
==GL_FRONT_LEFT
) {
684 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLenum mode
)
686 /* separate read buffer not supported */
687 ASSERT(buffer
== ctx
->DrawBuffer
);
688 ASSERT(mode
== GL_FRONT_LEFT
);
692 static void clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
693 GLint x
, GLint y
, GLint width
, GLint height
)
695 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
696 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
698 /* sanity check - we only have a front-left buffer */
699 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
| DD_BACK_LEFT_BIT
| DD_BACK_RIGHT_BIT
)) == 0);
700 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
701 if (mask
& DD_FRONT_LEFT_BIT
) {
702 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
704 /* Clear whole CI buffer */
705 #if CHAN_TYPE == GL_UNSIGNED_BYTE
706 MEMSET(osmesa
->buffer
, ctx
->Color
.ClearIndex
,
707 osmesa
->rowlength
* osmesa
->height
);
709 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
710 GLchan
*buffer
= (GLchan
*) osmesa
->buffer
;
712 for (i
= 0; i
< n
; i
++) {
713 buffer
[i
] = ctx
->Color
.ClearIndex
;
718 /* Clear part of CI buffer */
719 const GLchan clearIndex
= (GLchan
) ctx
->Color
.ClearIndex
;
721 for (i
= 0; i
< height
; i
++) {
722 GLchan
*ptr1
= PIXELADDR1(x
, (y
+ i
));
723 for (j
= 0; j
< width
; j
++) {
724 *ptr1
++ = clearIndex
;
729 else if (osmesa
->format
== OSMESA_RGB
) {
730 const GLchan r
= ctx
->Color
.ClearColor
[0];
731 const GLchan g
= ctx
->Color
.ClearColor
[1];
732 const GLchan b
= ctx
->Color
.ClearColor
[2];
734 /* Clear whole RGB buffer */
735 GLuint n
= osmesa
->rowlength
* osmesa
->height
;
736 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
738 for (i
= 0; i
< n
; i
++) {
739 PACK_RGB(ptr3
, r
, g
, b
);
744 /* Clear part of RGB buffer */
746 for (i
= 0; i
< height
; i
++) {
747 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
748 for (j
= 0; j
< width
; j
++) {
749 PACK_RGB(ptr3
, r
, g
, b
);
755 else if (osmesa
->format
== OSMESA_BGR
) {
756 const GLchan r
= ctx
->Color
.ClearColor
[0];
757 const GLchan g
= ctx
->Color
.ClearColor
[1];
758 const GLchan b
= ctx
->Color
.ClearColor
[2];
760 /* Clear whole RGB buffer */
761 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
762 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
764 for (i
= 0; i
< n
; i
++) {
765 PACK_BGR(ptr3
, r
, g
, b
);
770 /* Clear part of RGB buffer */
772 for (i
= 0; i
< height
; i
++) {
773 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
774 for (j
= 0; j
< width
; j
++) {
775 PACK_BGR(ptr3
, r
, g
, b
);
781 else if (osmesa
->format
== OSMESA_RGB_565
) {
782 const GLchan r
= ctx
->Color
.ClearColor
[0];
783 const GLchan g
= ctx
->Color
.ClearColor
[1];
784 const GLchan b
= ctx
->Color
.ClearColor
[2];
786 PACK_RGB_565(clearPixel
, r
, g
, b
);
788 /* Clear whole RGB buffer */
789 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
790 GLushort
*ptr2
= (GLushort
*) osmesa
->buffer
;
792 for (i
= 0; i
< n
; i
++) {
798 /* clear scissored region */
800 for (i
= 0; i
< height
; i
++) {
801 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, (y
+ i
));
802 for (j
= 0; j
< width
; j
++) {
810 #if CHAN_TYPE == GL_UNSIGNED_BYTE
811 /* 4-byte pixel value */
813 GLchan
*clr
= (GLchan
*) &clearPixel
;
814 clr
[osmesa
->rInd
] = ctx
->Color
.ClearColor
[0];
815 clr
[osmesa
->gInd
] = ctx
->Color
.ClearColor
[1];
816 clr
[osmesa
->bInd
] = ctx
->Color
.ClearColor
[2];
817 clr
[osmesa
->aInd
] = ctx
->Color
.ClearColor
[3];
819 /* Clear whole RGBA buffer */
820 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
821 GLuint
*ptr4
= (GLuint
*) osmesa
->buffer
;
824 for (i
= 0; i
< n
; i
++) {
825 *ptr4
++ = clearPixel
;
829 BZERO(ptr4
, n
* sizeof(GLuint
));
833 /* Clear part of RGBA buffer */
835 for (i
= 0; i
< height
; i
++) {
836 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, (y
+ i
));
837 for (j
= 0; j
< width
; j
++) {
838 *ptr4
++ = clearPixel
;
843 const GLchan r
= ctx
->Color
.ClearColor
[0];
844 const GLchan g
= ctx
->Color
.ClearColor
[1];
845 const GLchan b
= ctx
->Color
.ClearColor
[2];
846 const GLchan a
= ctx
->Color
.ClearColor
[3];
848 /* Clear whole RGBA buffer */
849 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
850 GLchan
*p
= (GLchan
*) osmesa
->buffer
;
852 for (i
= 0; i
< n
; i
++) {
853 PACK_RGBA(p
, r
, g
, b
, a
);
858 /* Clear part of RGBA buffer */
860 for (i
= 0; i
< height
; i
++) {
861 GLchan
*p
= PIXELADDR4(x
, (y
+ i
));
862 for (j
= 0; j
< width
; j
++) {
863 PACK_RGBA(p
, r
, g
, b
, a
);
871 mask
&= ~DD_FRONT_LEFT_BIT
;
876 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
881 static void buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
883 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
884 *width
= osmesa
->width
;
885 *height
= osmesa
->height
;
889 /**********************************************************************/
890 /***** Read/write spans/arrays of RGBA pixels *****/
891 /**********************************************************************/
893 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
895 write_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
896 CONST GLchan rgba
[][4], const GLubyte mask
[] )
898 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
899 GLchan
*p
= PIXELADDR4(x
, y
);
902 for (i
= 0; i
< n
; i
++, p
+= 4) {
904 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
905 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
910 for (i
= 0; i
< n
; i
++, p
+= 4) {
911 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
912 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
918 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
920 write_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
921 CONST GLchan rgba
[][4], const GLubyte mask
[] )
923 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
924 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
925 const GLuint
*rgba4
= (const GLuint
*) rgba
;
927 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
929 for (i
= 0; i
< n
; i
++) {
936 MEMCPY( ptr4
, rgba4
, n
* 4 );
941 /* Write RGB pixels to an RGBA (or permuted) buffer. */
943 write_rgb_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
944 CONST GLchan rgb
[][3], const GLubyte mask
[] )
946 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
947 GLchan
*p
= PIXELADDR4(x
, y
);
950 for (i
= 0; i
< n
; i
++, p
+=4) {
952 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
957 for (i
= 0; i
< n
; i
++, p
+=4) {
958 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
966 write_monocolor_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
967 const GLchan color
[4], const GLubyte mask
[] )
969 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
970 GLchan
*p
= PIXELADDR4(x
, y
);
972 for (i
= 0; i
< n
; i
++, p
+= 4) {
974 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
982 write_rgba_pixels( const GLcontext
*ctx
, GLuint n
,
983 const GLint x
[], const GLint y
[],
984 CONST GLchan rgba
[][4], const GLubyte mask
[] )
986 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
988 for (i
= 0; i
< n
; i
++) {
990 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
991 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
992 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
1000 write_monocolor_pixels( const GLcontext
*ctx
, GLuint n
,
1001 const GLint x
[], const GLint y
[],
1002 const GLchan color
[4], const GLubyte mask
[] )
1004 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1006 for (i
= 0; i
< n
; i
++) {
1008 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1009 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
1016 read_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1019 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1021 GLchan
*p
= PIXELADDR4(x
, y
);
1022 for (i
= 0; i
< n
; i
++, p
+= 4) {
1023 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1024 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1025 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1026 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1031 /* Read RGBA pixels from an RGBA buffer */
1033 read_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1036 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1037 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
1038 MEMCPY( rgba
, ptr4
, n
* 4 * sizeof(GLchan
) );
1043 read_rgba_pixels( const GLcontext
*ctx
,
1044 GLuint n
, const GLint x
[], const GLint y
[],
1045 GLchan rgba
[][4], const GLubyte mask
[] )
1047 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1049 for (i
= 0; i
< n
; i
++) {
1051 const GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1052 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1053 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1054 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1055 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1060 /**********************************************************************/
1061 /***** 3 byte RGB pixel support funcs *****/
1062 /**********************************************************************/
1064 /* Write RGBA pixels to an RGB buffer. */
1066 write_rgba_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1067 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1069 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1070 GLchan
*p
= PIXELADDR3(x
, y
);
1073 for (i
= 0; i
< n
; i
++, p
+= 3) {
1075 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1080 for (i
= 0; i
< n
; i
++, p
+= 3) {
1081 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1086 /* Write RGBA pixels to an BGR buffer. */
1088 write_rgba_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1089 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1091 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1092 GLchan
*p
= PIXELADDR3(x
, y
);
1095 for (i
= 0; i
< n
; i
++, p
+= 3) {
1097 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1102 for (i
= 0; i
< n
; i
++, p
+= 3) {
1103 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1108 /* Write RGB pixels to an RGB buffer. */
1110 write_rgb_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1111 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1113 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1114 GLchan
*p
= PIXELADDR3(x
, y
);
1117 for (i
= 0; i
< n
; i
++, p
+= 3) {
1119 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1124 for (i
= 0; i
< n
; i
++, p
+= 3) {
1125 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1130 /* Write RGB pixels to an BGR buffer. */
1132 write_rgb_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1133 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1135 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1136 GLchan
*p
= PIXELADDR3(x
, y
);
1139 for (i
= 0; i
< n
; i
++, p
+= 3) {
1141 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1146 for (i
= 0; i
< n
; i
++, p
+= 3) {
1147 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1154 write_monocolor_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1155 const GLchan color
[4], const GLubyte mask
[] )
1157 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1158 GLchan
*p
= PIXELADDR3(x
, y
);
1160 for (i
= 0; i
< n
; i
++, p
+= 3) {
1162 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1168 write_monocolor_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1169 const GLchan color
[4], const GLubyte mask
[] )
1171 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1172 GLchan
*p
= PIXELADDR3(x
, y
);
1174 for (i
= 0; i
< n
; i
++, p
+= 3) {
1176 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1182 write_rgba_pixels_RGB( const GLcontext
*ctx
, GLuint n
,
1183 const GLint x
[], const GLint y
[],
1184 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1186 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1188 for (i
= 0; i
< n
; i
++) {
1190 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1191 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1197 write_rgba_pixels_BGR( const GLcontext
*ctx
, GLuint n
,
1198 const GLint x
[], const GLint y
[],
1199 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1201 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1203 for (i
= 0; i
< n
; i
++) {
1205 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1206 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1212 write_monocolor_pixels_RGB( const GLcontext
*ctx
,
1213 GLuint n
, const GLint x
[], const GLint y
[],
1214 const GLchan color
[4], const GLubyte mask
[] )
1216 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1218 for (i
= 0; i
< n
; i
++) {
1220 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1221 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1227 write_monocolor_pixels_BGR( const GLcontext
*ctx
,
1228 GLuint n
, const GLint x
[], const GLint y
[],
1229 const GLchan color
[4], const GLubyte mask
[] )
1231 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1233 for (i
= 0; i
< n
; i
++) {
1235 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1236 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1242 read_rgba_span3( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1245 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1247 const GLchan
*p
= PIXELADDR3(x
, y
);
1248 for (i
= 0; i
< n
; i
++, p
+= 3) {
1249 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1250 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1251 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1252 rgba
[i
][ACOMP
] = CHAN_MAX
;
1257 read_rgba_pixels3( const GLcontext
*ctx
,
1258 GLuint n
, const GLint x
[], const GLint y
[],
1259 GLchan rgba
[][4], const GLubyte mask
[] )
1261 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1263 for (i
= 0; i
< n
; i
++) {
1265 const GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1266 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1267 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1268 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1269 rgba
[i
][ACOMP
] = CHAN_MAX
;
1275 /**********************************************************************/
1276 /***** 2 byte RGB pixel support funcs *****/
1277 /**********************************************************************/
1279 /* Write RGBA pixels to an RGB_565 buffer. */
1281 write_rgba_span2( const GLcontext
*ctx
,
1282 GLuint n
, GLint x
, GLint y
,
1283 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1285 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1286 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1289 for (i
= 0; i
< n
; i
++, ptr2
++) {
1291 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1296 for (i
= 0; i
< n
; i
++, ptr2
++) {
1297 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1303 /* Write RGB pixels to an RGB_565 buffer. */
1305 write_rgb_span2( const GLcontext
*ctx
,
1306 GLuint n
, GLint x
, GLint y
,
1307 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1309 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1310 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1313 for (i
= 0; i
< n
; i
++, ptr2
++) {
1315 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1320 for (i
= 0; i
< n
; i
++, ptr2
++) {
1321 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1328 write_monocolor_span2( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1329 const GLchan color
[4], const GLubyte mask
[] )
1331 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1333 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1335 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1336 for (i
= 0; i
< n
; i
++, ptr2
++) {
1345 write_rgba_pixels2( const GLcontext
*ctx
,
1346 GLuint n
, const GLint x
[], const GLint y
[],
1347 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1349 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1351 for (i
= 0; i
< n
; i
++) {
1353 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1354 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1360 write_monocolor_pixels2( const GLcontext
*ctx
,
1361 GLuint n
, const GLint x
[], const GLint y
[],
1362 const GLchan color
[4], const GLubyte mask
[] )
1364 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1367 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1368 for (i
= 0; i
< n
; i
++) {
1370 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1377 read_rgba_span2( const GLcontext
*ctx
,
1378 GLuint n
, GLint x
, GLint y
,
1381 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1383 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
, y
);
1384 for (i
= 0; i
< n
; i
++, ptr2
++) {
1385 /* This should be fixed to get the low bits right */
1386 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFe;
1387 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFc;
1388 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFe;
1394 read_rgba_pixels2( const GLcontext
*ctx
,
1395 GLuint n
, const GLint x
[], const GLint y
[],
1396 GLchan rgba
[][4], const GLubyte mask
[] )
1398 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1400 for (i
= 0; i
< n
; i
++) {
1402 /* This should be fixed to get the low bits right */
1403 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1404 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFE;
1405 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFC;
1406 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFE;
1414 /**********************************************************************/
1415 /***** Read/write spans/arrays of CI pixels *****/
1416 /**********************************************************************/
1418 /* Write 32-bit color index to buffer */
1420 write_index32_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1421 const GLuint index
[], const GLubyte mask
[] )
1423 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1424 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1427 for (i
=0;i
<n
;i
++,ptr1
++) {
1429 *ptr1
= (GLchan
) index
[i
];
1434 for (i
=0;i
<n
;i
++,ptr1
++) {
1435 *ptr1
= (GLchan
) index
[i
];
1441 /* Write 8-bit color index to buffer */
1443 write_index8_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1444 const GLubyte index
[], const GLubyte mask
[] )
1446 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1447 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1450 for (i
=0;i
<n
;i
++,ptr1
++) {
1452 *ptr1
= (GLchan
) index
[i
];
1457 MEMCPY(ptr1
, index
, n
* sizeof(GLchan
));
1463 write_monoindex_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1464 GLuint colorIndex
, const GLubyte mask
[] )
1466 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1467 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1469 for (i
=0;i
<n
;i
++,ptr1
++) {
1471 *ptr1
= (GLchan
) colorIndex
;
1478 write_index_pixels( const GLcontext
*ctx
,
1479 GLuint n
, const GLint x
[], const GLint y
[],
1480 const GLuint index
[], const GLubyte mask
[] )
1482 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1486 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1487 *ptr1
= (GLchan
) index
[i
];
1494 write_monoindex_pixels( const GLcontext
*ctx
,
1495 GLuint n
, const GLint x
[], const GLint y
[],
1496 GLuint colorIndex
, const GLubyte mask
[] )
1498 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1502 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1503 *ptr1
= (GLchan
) colorIndex
;
1510 read_index_span( const GLcontext
*ctx
,
1511 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1513 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1515 const GLchan
*ptr1
= (const GLchan
*) PIXELADDR1(x
, y
);
1516 for (i
=0;i
<n
;i
++,ptr1
++) {
1517 index
[i
] = (GLuint
) *ptr1
;
1523 read_index_pixels( const GLcontext
*ctx
,
1524 GLuint n
, const GLint x
[], const GLint y
[],
1525 GLuint index
[], const GLubyte mask
[] )
1527 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1531 const GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1532 index
[i
] = (GLuint
) *ptr1
;
1539 /**********************************************************************/
1540 /***** Optimized line rendering *****/
1541 /**********************************************************************/
1545 * Draw a flat-shaded, RGB line into an osmesa buffer.
1548 flat_rgba_line( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1550 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1551 const GLchan
*color
= vert0
->color
;
1555 #define PLOT(X, Y) \
1557 GLchan *p = PIXELADDR4(X, Y); \
1558 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1562 #include "..\swrast\s_linetemp.h"
1564 #include "swrast/s_linetemp.h"
1570 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1573 flat_rgba_z_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1575 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1576 const GLchan
*color
= vert0
->color
;
1580 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1582 #define PLOT(X, Y) \
1585 GLchan *p = PIXELADDR4(X, Y); \
1586 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1587 color[BCOMP], color[ACOMP]); \
1594 #include "..\swrast\s_linetemp.h"
1596 #include "swrast/s_linetemp.h"
1602 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1603 * XXX update for GLchan
1606 flat_blend_rgba_line( GLcontext
*ctx
,
1607 const SWvertex
*vert0
, const SWvertex
*vert1
)
1609 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1610 const GLint rshift
= osmesa
->rshift
;
1611 const GLint gshift
= osmesa
->gshift
;
1612 const GLint bshift
= osmesa
->bshift
;
1613 const GLint avalue
= vert0
->color
[3];
1614 const GLint msavalue
= CHAN_MAX
- avalue
;
1615 const GLint rvalue
= vert0
->color
[0]*avalue
;
1616 const GLint gvalue
= vert0
->color
[1]*avalue
;
1617 const GLint bvalue
= vert0
->color
[2]*avalue
;
1622 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1624 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1625 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1626 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1630 #if 0 /* XXX use this in the future */
1633 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1634 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1635 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1636 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1637 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1642 #include "..\swrast\s_linetemp.h"
1644 #include "swrast/s_linetemp.h"
1650 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1651 * But don't write to Z buffer.
1652 * XXX update for GLchan
1655 flat_blend_rgba_z_line( GLcontext
*ctx
,
1656 const SWvertex
*vert0
, const SWvertex
*vert1
)
1658 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1659 const GLint rshift
= osmesa
->rshift
;
1660 const GLint gshift
= osmesa
->gshift
;
1661 const GLint bshift
= osmesa
->bshift
;
1662 const GLint avalue
= vert0
->color
[3];
1663 const GLint msavalue
= 256 - avalue
;
1664 const GLint rvalue
= vert0
->color
[0]*avalue
;
1665 const GLint gvalue
= vert0
->color
[1]*avalue
;
1666 const GLint bvalue
= vert0
->color
[2]*avalue
;
1670 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1674 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1676 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1677 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1678 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1682 #if 0 /* XXX use this in the future */
1685 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1686 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1687 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1688 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1689 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1694 #include "..\swrast\s_linetemp.h"
1696 #include "swrast/s_linetemp.h"
1702 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1703 * XXX update for GLchan
1706 flat_blend_rgba_z_line_write( GLcontext
*ctx
,
1707 const SWvertex
*vert0
, const SWvertex
*vert1
)
1709 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1710 const GLint rshift
= osmesa
->rshift
;
1711 const GLint gshift
= osmesa
->gshift
;
1712 const GLint bshift
= osmesa
->bshift
;
1713 const GLint avalue
= vert0
->color
[3];
1714 const GLint msavalue
= 256 - avalue
;
1715 const GLint rvalue
= vert0
->color
[0]*avalue
;
1716 const GLint gvalue
= vert0
->color
[1]*avalue
;
1717 const GLint bvalue
= vert0
->color
[2]*avalue
;
1721 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1725 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1727 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1728 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1729 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1734 #if 0 /* XXX use this in the future */
1737 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1738 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1739 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1740 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1741 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1747 #include "..\swrast\s_linetemp.h"
1749 #include "swrast/s_linetemp.h"
1755 * Analyze context state to see if we can provide a fast line drawing
1756 * function, like those in lines.c. Otherwise, return NULL.
1758 static swrast_line_func
1759 osmesa_choose_line_function( GLcontext
*ctx
)
1761 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1762 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1764 if (CHAN_BITS
!= 8) return NULL
;
1765 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
1766 if (ctx
->Line
.SmoothFlag
) return NULL
;
1767 if (ctx
->Texture
._ReallyEnabled
) return NULL
;
1768 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
1769 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
1770 if (ctx
->Line
.StippleFlag
) return NULL
;
1771 if (ctx
->Line
.SmoothFlag
) return NULL
;
1772 if (osmesa
->format
!= OSMESA_RGBA
&&
1773 osmesa
->format
!= OSMESA_BGRA
&&
1774 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
1776 if (swrast
->_RasterMask
==DEPTH_BIT
1777 && ctx
->Depth
.Func
==GL_LESS
1778 && ctx
->Depth
.Mask
==GL_TRUE
1779 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1780 return (swrast_line_func
) flat_rgba_z_line
;
1783 if (swrast
->_RasterMask
== 0) {
1784 return (swrast_line_func
) flat_rgba_line
;
1787 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1788 && ctx
->Depth
.Func
==GL_LESS
1789 && ctx
->Depth
.Mask
==GL_TRUE
1790 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1791 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1792 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1793 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1794 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1795 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1796 return (swrast_line_func
) flat_blend_rgba_z_line_write
;
1799 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1800 && ctx
->Depth
.Func
==GL_LESS
1801 && ctx
->Depth
.Mask
==GL_FALSE
1802 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1803 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1804 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1805 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1806 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1807 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1808 return (swrast_line_func
) flat_blend_rgba_z_line
;
1811 if (swrast
->_RasterMask
==BLEND_BIT
1812 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1813 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1814 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1815 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1816 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1817 return (swrast_line_func
) flat_blend_rgba_line
;
1820 return (swrast_line_func
) NULL
;
1824 /**********************************************************************/
1825 /***** Optimized triangle rendering *****/
1826 /**********************************************************************/
1830 * Smooth-shaded, z-less triangle, RGBA color.
1832 static void smooth_rgba_z_triangle( GLcontext
*ctx
,
1835 const SWvertex
*v2
)
1837 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1840 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1841 #define INTERP_RGB 1
1842 #define INTERP_ALPHA 1
1843 #define RENDER_SPAN( span ) \
1845 GLchan *img = PIXELADDR4(span.x, span.y); \
1846 for (i = 0; i < span.count; i++, img += 4) { \
1847 const GLdepth z = FixedToDepth(span.z); \
1848 if (z < zRow[i]) { \
1849 PACK_RGBA(img, FixedToChan(span.red), \
1850 FixedToChan(span.green), FixedToChan(span.blue), \
1851 FixedToChan(span.alpha)); \
1854 span.red += span.redStep; \
1855 span.green += span.greenStep; \
1856 span.blue += span.blueStep; \
1857 span.alpha += span.alphaStep; \
1858 span.z += span.zStep; \
1862 #include "..\swrast\s_tritemp.h"
1864 #include "swrast/s_tritemp.h"
1872 * Flat-shaded, z-less triangle, RGBA color.
1874 static void flat_rgba_z_triangle( GLcontext
*ctx
,
1877 const SWvertex
*v2
)
1879 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1881 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1882 #define SETUP_CODE \
1884 PACK_RGBA((GLchan *) &pixel, v0->color[0], v0->color[1], \
1885 v0->color[2], v0->color[3]);
1887 #define RENDER_SPAN( span ) \
1889 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1890 for (i = 0; i < span.count; i++) { \
1891 const GLdepth z = FixedToDepth(span.z); \
1892 if (z < zRow[i]) { \
1896 span.z += span.zStep; \
1900 #include "..\swrast\s_tritemp.h"
1902 #include "swrast/s_tritemp.h"
1909 * Return pointer to an accelerated triangle function if possible.
1911 static swrast_tri_func
1912 osmesa_choose_triangle_function( GLcontext
*ctx
)
1914 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1915 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1917 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
1918 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
1919 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
1920 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
1921 if (ctx
->Texture
._ReallyEnabled
) return (swrast_tri_func
) NULL
;
1922 if (osmesa
->format
!= OSMESA_RGBA
&&
1923 osmesa
->format
!= OSMESA_BGRA
&&
1924 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
1926 if (swrast
->_RasterMask
== DEPTH_BIT
&&
1927 ctx
->Depth
.Func
== GL_LESS
&&
1928 ctx
->Depth
.Mask
== GL_TRUE
&&
1929 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1930 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
1931 return (swrast_tri_func
) smooth_rgba_z_triangle
;
1934 return (swrast_tri_func
) flat_rgba_z_triangle
;
1937 return (swrast_tri_func
) NULL
;
1942 /* Override for the swrast triangle-selection function. Try to use one
1943 * of our internal triangle functions, otherwise fall back to the
1944 * standard swrast functions.
1946 static void osmesa_choose_triangle( GLcontext
*ctx
)
1948 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1950 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
1951 if (!swrast
->Triangle
)
1952 _swrast_choose_triangle( ctx
);
1955 static void osmesa_choose_line( GLcontext
*ctx
)
1957 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1959 swrast
->Line
= osmesa_choose_line_function( ctx
);
1961 _swrast_choose_line( ctx
);
1965 #define OSMESA_NEW_LINE (_NEW_LINE | \
1970 _SWRAST_NEW_RASTERMASK)
1972 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1977 _SWRAST_NEW_RASTERMASK)
1980 /* Extend the software rasterizer with our line and triangle
1983 static void osmesa_register_swrast_functions( GLcontext
*ctx
)
1985 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1987 swrast
->choose_line
= osmesa_choose_line
;
1988 swrast
->choose_triangle
= osmesa_choose_triangle
;
1990 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
1991 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1995 static const GLubyte
*get_string( GLcontext
*ctx
, GLenum name
)
2000 return (const GLubyte
*) "Mesa OffScreen";
2007 static void osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
2009 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
2010 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
2011 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
2013 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
2016 * XXX these function pointers could be initialized just once during
2017 * context creation since they don't depend on any state changes.
2020 ctx
->Driver
.GetString
= get_string
;
2021 ctx
->Driver
.UpdateState
= osmesa_update_state
;
2022 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
2023 ctx
->Driver
.ResizeBuffersMESA
= _swrast_alloc_buffers
;
2024 ctx
->Driver
.GetBufferSize
= buffer_size
;
2026 ctx
->Driver
.Accum
= _swrast_Accum
;
2027 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
2028 ctx
->Driver
.Clear
= clear
;
2029 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
2030 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
2031 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
2033 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
2034 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
2035 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
2036 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
2037 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
2038 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
2039 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
2040 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
2042 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
2043 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
2044 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
2045 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
2046 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
2047 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
2048 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
2049 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
2050 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
2052 ctx
->Driver
.BaseCompressedTexFormat
= _mesa_base_compressed_texformat
;
2053 ctx
->Driver
.CompressedTextureSize
= _mesa_compressed_texture_size
;
2054 ctx
->Driver
.GetCompressedTexImage
= _mesa_get_compressed_teximage
;
2056 /* RGB(A) span/pixel functions */
2057 if (osmesa
->format
== OSMESA_RGB
) {
2058 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
2059 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
2060 swdd
->WriteMonoRGBASpan
= write_monocolor_span_RGB
;
2061 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
2062 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_RGB
;
2063 swdd
->ReadRGBASpan
= read_rgba_span3
;
2064 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2066 else if (osmesa
->format
== OSMESA_BGR
) {
2067 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
2068 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
2069 swdd
->WriteMonoRGBASpan
= write_monocolor_span_BGR
;
2070 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
2071 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_BGR
;
2072 swdd
->ReadRGBASpan
= read_rgba_span3
;
2073 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2075 else if (osmesa
->format
== OSMESA_RGB_565
) {
2076 swdd
->WriteRGBASpan
= write_rgba_span2
;
2077 swdd
->WriteRGBSpan
= write_rgb_span2
;
2078 swdd
->WriteMonoRGBASpan
= write_monocolor_span2
;
2079 swdd
->WriteRGBAPixels
= write_rgba_pixels2
;
2080 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels2
;
2081 swdd
->ReadRGBASpan
= read_rgba_span2
;
2082 swdd
->ReadRGBAPixels
= read_rgba_pixels2
;
2085 /* 4 GLchan / pixel in frame buffer */
2086 swdd
->WriteRGBSpan
= write_rgb_span
;
2087 swdd
->WriteRGBAPixels
= write_rgba_pixels
;
2088 swdd
->WriteMonoRGBASpan
= write_monocolor_span
;
2089 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels
;
2090 if (osmesa
->format
== OSMESA_RGBA
&&
2091 CHAN_TYPE
== GL_UNSIGNED_BYTE
&&
2092 RCOMP
==0 && GCOMP
==1 && BCOMP
==2 && ACOMP
==3) {
2093 /* special, fast case */
2094 swdd
->WriteRGBASpan
= write_rgba_span_rgba
;
2095 swdd
->ReadRGBASpan
= read_rgba_span_rgba
;
2098 swdd
->WriteRGBASpan
= write_rgba_span
;
2099 swdd
->ReadRGBASpan
= read_rgba_span
;
2101 swdd
->ReadRGBAPixels
= read_rgba_pixels
;
2104 /* CI span/pixel functions */
2105 swdd
->WriteCI32Span
= write_index32_span
;
2106 swdd
->WriteCI8Span
= write_index8_span
;
2107 swdd
->WriteMonoCISpan
= write_monoindex_span
;
2108 swdd
->WriteCI32Pixels
= write_index_pixels
;
2109 swdd
->WriteMonoCIPixels
= write_monoindex_pixels
;
2110 swdd
->ReadCI32Span
= read_index_span
;
2111 swdd
->ReadCI32Pixels
= read_index_pixels
;
2113 swdd
->SetReadBuffer
= set_read_buffer
;
2115 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
2117 _swrast_InvalidateState( ctx
, new_state
);
2118 _swsetup_InvalidateState( ctx
, new_state
);
2119 _ac_InvalidateState( ctx
, new_state
);
2120 _tnl_InvalidateState( ctx
, new_state
);