1 /* $Id: osmesa.c,v 1.55 2001/05/14 16:23:04 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Off-Screen Mesa rendering / Rendering into client memory space
31 * Note on thread safety: this driver is thread safe. All
32 * functions are reentrant. The notion of current context is
33 * managed by the core _mesa_make_current() and _mesa_get_current_context()
34 * functions. Those functions are thread-safe.
39 #include "GL/osmesa.h"
43 #include "extensions.h"
49 #include "texformat.h"
51 #include "array_cache/acache.h"
52 #include "swrast/swrast.h"
53 #include "swrast_setup/swrast_setup.h"
54 #include "swrast/s_context.h"
55 #include "swrast/s_depth.h"
56 #include "swrast/s_lines.h"
57 #include "swrast/s_triangle.h"
58 #include "swrast/s_trispan.h"
60 #include "tnl/t_context.h"
61 #include "tnl/t_pipeline.h"
66 * This is the OS/Mesa context struct.
67 * Notice how it includes a GLcontext. By doing this we're mimicking
68 * C++ inheritance/derivation.
69 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
72 struct osmesa_context
{
73 GLcontext gl_ctx
; /* The core GL/Mesa context */
74 GLvisual
*gl_visual
; /* Describes the buffers */
75 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
76 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
77 void *buffer
; /* the image buffer */
78 GLint width
, height
; /* size of image buffer */
79 GLint rowlength
; /* number of pixels per row */
80 GLint userRowLength
; /* user-specified number of pixels per row */
81 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
83 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
84 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
85 GLboolean yup
; /* TRUE -> Y increases upward */
86 /* FALSE -> Y increases downward */
91 /* A forward declaration: */
92 static void osmesa_update_state( GLcontext
*ctx
, GLuint newstate
);
93 static void osmesa_register_swrast_functions( GLcontext
*ctx
);
97 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
101 /**********************************************************************/
102 /***** Public Functions *****/
103 /**********************************************************************/
107 * Create an Off-Screen Mesa rendering context. The only attribute needed is
108 * an RGBA vs Color-Index mode flag.
110 * Input: format - either GL_RGBA or GL_COLOR_INDEX
111 * sharelist - specifies another OSMesaContext with which to share
112 * display lists. NULL indicates no sharing.
113 * Return: an OSMesaContext or 0 if error
115 OSMesaContext GLAPIENTRY
116 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
118 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
127 * Create context and specify size of ancillary buffers.
129 OSMesaContext GLAPIENTRY
130 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
131 GLint accumBits
, OSMesaContext sharelist
)
133 OSMesaContext osmesa
;
134 GLint rshift
, gshift
, bshift
, ashift
;
135 GLint rind
, gind
, bind
, aind
;
136 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
140 const GLubyte
*i1
= (GLubyte
*) &i4
;
141 const GLint little_endian
= *i1
;
144 rind
= gind
= bind
= aind
= 0;
145 if (format
==OSMESA_COLOR_INDEX
) {
147 rshift
= gshift
= bshift
= ashift
= 0;
150 else if (format
==OSMESA_RGBA
) {
153 greenBits
= CHAN_BITS
;
154 blueBits
= CHAN_BITS
;
155 alphaBits
= CHAN_BITS
;
174 else if (format
==OSMESA_BGRA
) {
177 greenBits
= CHAN_BITS
;
178 blueBits
= CHAN_BITS
;
179 alphaBits
= CHAN_BITS
;
198 else if (format
==OSMESA_ARGB
) {
201 greenBits
= CHAN_BITS
;
202 blueBits
= CHAN_BITS
;
203 alphaBits
= CHAN_BITS
;
222 else if (format
==OSMESA_RGB
) {
225 greenBits
= CHAN_BITS
;
226 blueBits
= CHAN_BITS
;
238 else if (format
==OSMESA_BGR
) {
241 greenBits
= CHAN_BITS
;
242 blueBits
= CHAN_BITS
;
259 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
261 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
262 GL_FALSE
, /* double buffer */
263 GL_FALSE
, /* stereo */
274 alphaBits
? accumBits
: 0,
277 if (!osmesa
->gl_visual
) {
282 if (!_mesa_initialize_context(&osmesa
->gl_ctx
,
284 sharelist
? &sharelist
->gl_ctx
285 : (GLcontext
*) NULL
,
286 (void *) osmesa
, GL_TRUE
)) {
287 _mesa_destroy_visual( osmesa
->gl_visual
);
292 _mesa_enable_sw_extensions(&(osmesa
->gl_ctx
));
294 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
295 osmesa
->gl_visual
->depthBits
> 0,
296 osmesa
->gl_visual
->stencilBits
> 0,
297 osmesa
->gl_visual
->accumRedBits
> 0,
298 osmesa
->gl_visual
->alphaBits
> 0 );
300 if (!osmesa
->gl_buffer
) {
301 _mesa_destroy_visual( osmesa
->gl_visual
);
302 _mesa_free_context_data( &osmesa
->gl_ctx
);
306 osmesa
->format
= format
;
307 osmesa
->buffer
= NULL
;
310 osmesa
->userRowLength
= 0;
311 osmesa
->rowlength
= 0;
312 osmesa
->yup
= GL_TRUE
;
313 osmesa
->rshift
= rshift
;
314 osmesa
->gshift
= gshift
;
315 osmesa
->bshift
= bshift
;
316 osmesa
->ashift
= ashift
;
323 /* Initialize the software rasterizer and helper modules.
326 GLcontext
*ctx
= &osmesa
->gl_ctx
;
328 _swrast_CreateContext( ctx
);
329 _ac_CreateContext( ctx
);
330 _tnl_CreateContext( ctx
);
331 _swsetup_CreateContext( ctx
);
333 osmesa_register_swrast_functions( ctx
);
343 * Destroy an Off-Screen Mesa rendering context.
345 * Input: ctx - the context to destroy
347 void GLAPIENTRY
OSMesaDestroyContext( OSMesaContext ctx
)
350 _swsetup_DestroyContext( &ctx
->gl_ctx
);
351 _tnl_DestroyContext( &ctx
->gl_ctx
);
352 _ac_DestroyContext( &ctx
->gl_ctx
);
353 _swrast_DestroyContext( &ctx
->gl_ctx
);
355 _mesa_destroy_visual( ctx
->gl_visual
);
356 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
357 _mesa_free_context_data( &ctx
->gl_ctx
);
365 * Recompute the values of the context's rowaddr array.
367 static void compute_row_addresses( OSMesaContext ctx
)
369 GLint bytesPerPixel
, bytesPerRow
, i
;
370 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
372 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
374 bytesPerPixel
= 1 * sizeof(GLchan
);
376 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
378 bytesPerPixel
= 3 * sizeof(GLchan
);
382 bytesPerPixel
= 4 * sizeof(GLchan
);
385 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
388 /* Y=0 is bottom line of window */
389 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
390 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
394 /* Y=0 is top line of window */
395 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
396 GLint j
= ctx
->height
- i
- 1;
397 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
404 * Bind an OSMesaContext to an image buffer. The image buffer is just a
405 * block of memory which the client provides. Its size must be at least
406 * as large as width*height*sizeof(type). Its address should be a multiple
407 * of 4 if using RGBA mode.
409 * Image data is stored in the order of glDrawPixels: row-major order
410 * with the lower-left image pixel stored in the first array position
411 * (ie. bottom-to-top).
413 * Since the only type initially supported is GL_UNSIGNED_BYTE, if the
414 * context is in RGBA mode, each pixel will be stored as a 4-byte RGBA
415 * value. If the context is in color indexed mode, each pixel will be
416 * stored as a 1-byte value.
418 * If the context's viewport hasn't been initialized yet, it will now be
419 * initialized to (0,0,width,height).
421 * Input: ctx - the rendering context
422 * buffer - the image buffer memory
423 * type - data type for pixel components, only GL_UNSIGNED_BYTE
425 * width, height - size of image buffer in pixels, at least 1
426 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
427 * invalid buffer address, type!=GL_UNSIGNED_BYTE, width<1, height<1,
428 * width>internal limit or height>internal limit.
431 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
432 GLsizei width
, GLsizei height
)
434 if (!ctx
|| !buffer
|| type
!= CHAN_TYPE
||
435 width
< 1 || height
< 1 ||
436 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
440 osmesa_update_state( &ctx
->gl_ctx
, 0 );
441 _mesa_make_current( &ctx
->gl_ctx
, ctx
->gl_buffer
);
443 ctx
->buffer
= buffer
;
445 ctx
->height
= height
;
446 if (ctx
->userRowLength
)
447 ctx
->rowlength
= ctx
->userRowLength
;
449 ctx
->rowlength
= width
;
451 compute_row_addresses( ctx
);
454 if (ctx
->gl_ctx
.Viewport
.Width
==0) {
455 /* initialize viewport and scissor box to buffer size */
456 _mesa_Viewport( 0, 0, width
, height
);
457 ctx
->gl_ctx
.Scissor
.Width
= width
;
458 ctx
->gl_ctx
.Scissor
.Height
= height
;
466 OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
468 GLcontext
*ctx
= _mesa_get_current_context();
470 return (OSMesaContext
) ctx
;
477 void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
479 OSMesaContext ctx
= OSMesaGetCurrentContext();
482 case OSMESA_ROW_LENGTH
:
484 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_VALUE
,
485 "OSMesaPixelStore(value)" );
488 ctx
->userRowLength
= value
;
489 ctx
->rowlength
= value
;
492 ctx
->yup
= value
? GL_TRUE
: GL_FALSE
;
495 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
499 compute_row_addresses( ctx
);
503 void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
505 OSMesaContext ctx
= OSMesaGetCurrentContext();
512 *value
= ctx
->height
;
515 *value
= ctx
->format
;
520 case OSMESA_ROW_LENGTH
:
521 *value
= ctx
->rowlength
;
527 _mesa_error(&ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
533 * Return the depth buffer associated with an OSMesa context.
534 * Input: c - the OSMesa context
535 * Output: width, height - size of buffer in pixels
536 * bytesPerValue - bytes per depth value (2 or 4)
537 * buffer - pointer to depth buffer values
538 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
541 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
542 GLint
*bytesPerValue
, void **buffer
)
544 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
552 *width
= c
->gl_buffer
->Width
;
553 *height
= c
->gl_buffer
->Height
;
554 if (c
->gl_visual
->depthBits
<= 16)
555 *bytesPerValue
= sizeof(GLushort
);
557 *bytesPerValue
= sizeof(GLuint
);
558 *buffer
= c
->gl_buffer
->DepthBuffer
;
564 * Return the color buffer associated with an OSMesa context.
565 * Input: c - the OSMesa context
566 * Output: width, height - size of buffer in pixels
567 * format - the pixel format (OSMESA_FORMAT)
568 * buffer - pointer to color buffer values
569 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
572 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
573 GLint
*height
, GLint
*format
, void **buffer
)
591 /**********************************************************************/
592 /*** Device Driver Functions ***/
593 /**********************************************************************/
600 #define PACK_RGBA(DST, R, G, B, A) \
602 (DST)[osmesa->rInd] = R; \
603 (DST)[osmesa->gInd] = G; \
604 (DST)[osmesa->bInd] = B; \
605 (DST)[osmesa->aInd] = A; \
608 #define PACK_RGB(DST, R, G, B) \
615 #define PACK_BGR(DST, R, G, B) \
623 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
624 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
625 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
626 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
629 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
630 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
631 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
635 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum mode
)
638 if (mode
==GL_FRONT_LEFT
) {
647 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLenum mode
)
649 /* separate read buffer not supported */
650 ASSERT(buffer
== ctx
->DrawBuffer
);
651 ASSERT(mode
== GL_FRONT_LEFT
);
655 static void clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
656 GLint x
, GLint y
, GLint width
, GLint height
)
658 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
659 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
661 /* sanity check - we only have a front-left buffer */
662 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
| DD_BACK_LEFT_BIT
| DD_BACK_RIGHT_BIT
)) == 0);
663 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
664 if (mask
& DD_FRONT_LEFT_BIT
) {
665 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
667 /* Clear whole CI buffer */
668 #if CHAN_TYPE == GL_UNSIGNED_BYTE
669 MEMSET(osmesa
->buffer
, ctx
->Color
.ClearIndex
,
670 osmesa
->rowlength
* osmesa
->height
);
672 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
673 GLchan
*buffer
= (GLchan
*) osmesa
->buffer
;
675 for (i
= 0; i
< n
; i
++) {
676 buffer
[i
] = ctx
->Color
.ClearIndex
;
681 /* Clear part of CI buffer */
682 const GLchan clearIndex
= (GLchan
) ctx
->Color
.ClearIndex
;
684 for (i
= 0; i
< height
; i
++) {
685 GLchan
*ptr1
= PIXELADDR1(x
, (y
+ i
));
686 for (j
= 0; j
< width
; j
++) {
687 *ptr1
++ = clearIndex
;
692 else if (osmesa
->format
== OSMESA_RGB
) {
693 const GLchan r
= ctx
->Color
.ClearColor
[0];
694 const GLchan g
= ctx
->Color
.ClearColor
[1];
695 const GLchan b
= ctx
->Color
.ClearColor
[2];
697 /* Clear whole RGB buffer */
698 GLuint n
= osmesa
->rowlength
* osmesa
->height
;
699 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
701 for (i
= 0; i
< n
; i
++) {
702 PACK_RGB(ptr3
, r
, g
, b
);
707 /* Clear part of RGB buffer */
709 for (i
= 0; i
< height
; i
++) {
710 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
711 for (j
= 0; j
< width
; j
++) {
712 PACK_RGB(ptr3
, r
, g
, b
);
718 else if (osmesa
->format
== OSMESA_BGR
) {
719 const GLchan r
= ctx
->Color
.ClearColor
[0];
720 const GLchan g
= ctx
->Color
.ClearColor
[1];
721 const GLchan b
= ctx
->Color
.ClearColor
[2];
723 /* Clear whole RGB buffer */
724 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
725 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
727 for (i
= 0; i
< n
; i
++) {
728 PACK_BGR(ptr3
, r
, g
, b
);
733 /* Clear part of RGB buffer */
735 for (i
= 0; i
< height
; i
++) {
736 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
737 for (j
= 0; j
< width
; j
++) {
738 PACK_BGR(ptr3
, r
, g
, b
);
745 #if CHAN_TYPE == GL_UNSIGNED_BYTE
746 /* 4-byte pixel value */
748 GLchan
*clr
= (GLchan
*) &clearPixel
;
749 clr
[osmesa
->rInd
] = ctx
->Color
.ClearColor
[0];
750 clr
[osmesa
->gInd
] = ctx
->Color
.ClearColor
[1];
751 clr
[osmesa
->bInd
] = ctx
->Color
.ClearColor
[2];
752 clr
[osmesa
->aInd
] = ctx
->Color
.ClearColor
[3];
754 /* Clear whole RGBA buffer */
755 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
756 GLuint
*ptr4
= (GLuint
*) osmesa
->buffer
;
759 for (i
= 0; i
< n
; i
++) {
760 *ptr4
++ = clearPixel
;
764 BZERO(ptr4
, n
* sizeof(GLuint
));
768 /* Clear part of RGBA buffer */
770 for (i
= 0; i
< height
; i
++) {
771 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, (y
+ i
));
772 for (j
= 0; j
< width
; j
++) {
773 *ptr4
++ = clearPixel
;
778 const GLchan r
= ctx
->Color
.ClearColor
[0];
779 const GLchan g
= ctx
->Color
.ClearColor
[1];
780 const GLchan b
= ctx
->Color
.ClearColor
[2];
781 const GLchan a
= ctx
->Color
.ClearColor
[3];
783 /* Clear whole RGBA buffer */
784 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
785 GLchan
*p
= (GLchan
*) osmesa
->buffer
;
787 for (i
= 0; i
< n
; i
++) {
788 PACK_RGBA(p
, r
, g
, b
, a
);
793 /* Clear part of RGBA buffer */
795 for (i
= 0; i
< height
; i
++) {
796 GLchan
*p
= PIXELADDR4(x
, (y
+ i
));
797 for (j
= 0; j
< width
; j
++) {
798 PACK_RGBA(p
, r
, g
, b
, a
);
806 mask
&= ~DD_FRONT_LEFT_BIT
;
811 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
816 static void buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
818 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
819 *width
= osmesa
->width
;
820 *height
= osmesa
->height
;
824 /**********************************************************************/
825 /***** Read/write spans/arrays of RGBA pixels *****/
826 /**********************************************************************/
828 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
830 write_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
831 CONST GLchan rgba
[][4], const GLubyte mask
[] )
833 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
834 GLchan
*p
= PIXELADDR4(x
, y
);
837 for (i
= 0; i
< n
; i
++, p
+= 4) {
839 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
840 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
845 for (i
= 0; i
< n
; i
++, p
+= 4) {
846 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
847 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
853 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
855 write_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
856 CONST GLchan rgba
[][4], const GLubyte mask
[] )
858 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
859 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
860 const GLuint
*rgba4
= (const GLuint
*) rgba
;
862 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
864 for (i
= 0; i
< n
; i
++) {
871 MEMCPY( ptr4
, rgba4
, n
* 4 );
876 /* Write RGB pixels to an RGBA (or permuted) buffer. */
878 write_rgb_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
879 CONST GLchan rgb
[][3], const GLubyte mask
[] )
881 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
882 GLchan
*p
= PIXELADDR4(x
, y
);
885 for (i
= 0; i
< n
; i
++, p
+=4) {
887 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], 255);
892 for (i
= 0; i
< n
; i
++, p
+=4) {
893 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], 255);
901 write_monocolor_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
902 const GLchan color
[4], const GLubyte mask
[] )
904 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
905 GLchan
*p
= PIXELADDR4(x
, y
);
907 for (i
= 0; i
< n
; i
++, p
+= 4) {
909 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
917 write_rgba_pixels( const GLcontext
*ctx
, GLuint n
,
918 const GLint x
[], const GLint y
[],
919 CONST GLchan rgba
[][4], const GLubyte mask
[] )
921 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
923 for (i
= 0; i
< n
; i
++) {
925 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
926 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
927 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
935 write_monocolor_pixels( const GLcontext
*ctx
, GLuint n
,
936 const GLint x
[], const GLint y
[],
937 const GLchan color
[4], const GLubyte mask
[] )
939 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
941 for (i
= 0; i
< n
; i
++) {
943 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
944 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
951 read_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
954 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
956 GLchan
*p
= PIXELADDR4(x
, y
);
957 for (i
= 0; i
< n
; i
++, p
+= 4) {
958 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
959 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
960 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
961 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
966 /* Read RGBA pixels from an RGBA buffer */
968 read_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
971 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
972 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
973 MEMCPY( rgba
, ptr4
, n
* 4 * sizeof(GLchan
) );
978 read_rgba_pixels( const GLcontext
*ctx
,
979 GLuint n
, const GLint x
[], const GLint y
[],
980 GLchan rgba
[][4], const GLubyte mask
[] )
982 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
984 for (i
= 0; i
< n
; i
++) {
986 const GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
987 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
988 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
989 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
990 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
995 /**********************************************************************/
996 /***** 3 byte RGB pixel support funcs *****/
997 /**********************************************************************/
999 /* Write RGBA pixels to an RGB buffer. */
1001 write_rgba_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1002 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1004 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1005 GLchan
*p
= PIXELADDR3(x
, y
);
1008 for (i
= 0; i
< n
; i
++, p
+= 3) {
1010 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1015 for (i
= 0; i
< n
; i
++, p
+= 3) {
1016 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1021 /* Write RGBA pixels to an BGR buffer. */
1023 write_rgba_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1024 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1026 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1027 GLchan
*p
= PIXELADDR3(x
, y
);
1030 for (i
= 0; i
< n
; i
++, p
+= 3) {
1032 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1037 for (i
= 0; i
< n
; i
++, p
+= 3) {
1038 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1043 /* Write RGB pixels to an RGB buffer. */
1045 write_rgb_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1046 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1048 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1049 GLchan
*p
= PIXELADDR3(x
, y
);
1052 for (i
= 0; i
< n
; i
++, p
+= 3) {
1054 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1059 for (i
= 0; i
< n
; i
++, p
+= 3) {
1060 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1065 /* Write RGB pixels to an BGR buffer. */
1067 write_rgb_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1068 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1070 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1071 GLchan
*p
= PIXELADDR3(x
, y
);
1074 for (i
= 0; i
< n
; i
++, p
+= 3) {
1076 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1081 for (i
= 0; i
< n
; i
++, p
+= 3) {
1082 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1089 write_monocolor_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1090 const GLchan color
[4], const GLubyte mask
[] )
1092 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1093 GLchan
*p
= PIXELADDR3(x
, y
);
1095 for (i
= 0; i
< n
; i
++, p
+= 3) {
1097 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1103 write_monocolor_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1104 const GLchan color
[4], const GLubyte mask
[] )
1106 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1107 GLchan
*p
= PIXELADDR3(x
, y
);
1109 for (i
= 0; i
< n
; i
++, p
+= 3) {
1111 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1117 write_rgba_pixels_RGB( const GLcontext
*ctx
, GLuint n
,
1118 const GLint x
[], const GLint y
[],
1119 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1121 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1123 for (i
= 0; i
< n
; i
++) {
1125 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1126 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1132 write_rgba_pixels_BGR( const GLcontext
*ctx
, GLuint n
,
1133 const GLint x
[], const GLint y
[],
1134 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1136 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1138 for (i
= 0; i
< n
; i
++) {
1140 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1141 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1147 write_monocolor_pixels_RGB( const GLcontext
*ctx
,
1148 GLuint n
, const GLint x
[], const GLint y
[],
1149 const GLchan color
[4], const GLubyte mask
[] )
1151 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1153 for (i
= 0; i
< n
; i
++) {
1155 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1156 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1162 write_monocolor_pixels_BGR( const GLcontext
*ctx
,
1163 GLuint n
, const GLint x
[], const GLint y
[],
1164 const GLchan color
[4], const GLubyte mask
[] )
1166 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1168 for (i
= 0; i
< n
; i
++) {
1170 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1171 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1177 read_rgba_span3( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1180 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1182 const GLchan
*p
= PIXELADDR3(x
, y
);
1183 for (i
= 0; i
< n
; i
++, p
+= 3) {
1184 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1185 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1186 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1187 rgba
[i
][ACOMP
] = 255;
1192 read_rgba_pixels3( const GLcontext
*ctx
,
1193 GLuint n
, const GLint x
[], const GLint y
[],
1194 GLchan rgba
[][4], const GLubyte mask
[] )
1196 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1198 for (i
= 0; i
< n
; i
++) {
1200 const GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1201 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1202 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1203 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1204 rgba
[i
][ACOMP
] = 255;
1210 /**********************************************************************/
1211 /***** Read/write spans/arrays of CI pixels *****/
1212 /**********************************************************************/
1214 /* Write 32-bit color index to buffer */
1216 write_index32_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1217 const GLuint index
[], const GLubyte mask
[] )
1219 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1220 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1223 for (i
=0;i
<n
;i
++,ptr1
++) {
1225 *ptr1
= (GLchan
) index
[i
];
1230 for (i
=0;i
<n
;i
++,ptr1
++) {
1231 *ptr1
= (GLchan
) index
[i
];
1237 /* Write 8-bit color index to buffer */
1239 write_index8_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1240 const GLubyte index
[], const GLubyte mask
[] )
1242 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1243 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1246 for (i
=0;i
<n
;i
++,ptr1
++) {
1248 *ptr1
= (GLchan
) index
[i
];
1253 MEMCPY(ptr1
, index
, n
* sizeof(GLchan
));
1259 write_monoindex_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1260 GLuint colorIndex
, const GLubyte mask
[] )
1262 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1263 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1265 for (i
=0;i
<n
;i
++,ptr1
++) {
1267 *ptr1
= (GLchan
) colorIndex
;
1274 write_index_pixels( const GLcontext
*ctx
,
1275 GLuint n
, const GLint x
[], const GLint y
[],
1276 const GLuint index
[], const GLubyte mask
[] )
1278 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1282 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1283 *ptr1
= (GLchan
) index
[i
];
1290 write_monoindex_pixels( const GLcontext
*ctx
,
1291 GLuint n
, const GLint x
[], const GLint y
[],
1292 GLuint colorIndex
, const GLubyte mask
[] )
1294 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1298 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1299 *ptr1
= (GLchan
) colorIndex
;
1306 read_index_span( const GLcontext
*ctx
,
1307 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1309 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1311 const GLchan
*ptr1
= (const GLchan
*) PIXELADDR1(x
, y
);
1312 for (i
=0;i
<n
;i
++,ptr1
++) {
1313 index
[i
] = (GLuint
) *ptr1
;
1319 read_index_pixels( const GLcontext
*ctx
,
1320 GLuint n
, const GLint x
[], const GLint y
[],
1321 GLuint index
[], const GLubyte mask
[] )
1323 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1327 const GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1328 index
[i
] = (GLuint
) *ptr1
;
1335 /**********************************************************************/
1336 /***** Optimized line rendering *****/
1337 /**********************************************************************/
1341 * Draw a flat-shaded, RGB line into an osmesa buffer.
1344 flat_rgba_line( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1346 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1347 const GLchan
*color
= vert0
->color
;
1351 #define PLOT(X, Y) \
1353 GLchan *p = PIXELADDR4(X, Y); \
1354 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1358 #include "..\swrast\s_linetemp.h"
1360 #include "swrast/s_linetemp.h"
1366 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1369 flat_rgba_z_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1371 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1372 const GLchan
*color
= vert0
->color
;
1376 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1378 #define PLOT(X, Y) \
1381 GLchan *p = PIXELADDR4(X, Y); \
1382 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1383 color[BCOMP], color[ACOMP]); \
1390 #include "..\swrast\s_linetemp.h"
1392 #include "swrast/s_linetemp.h"
1398 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1399 * XXX update for GLchan
1402 flat_blend_rgba_line( GLcontext
*ctx
,
1403 const SWvertex
*vert0
, const SWvertex
*vert1
)
1405 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1406 const GLint rshift
= osmesa
->rshift
;
1407 const GLint gshift
= osmesa
->gshift
;
1408 const GLint bshift
= osmesa
->bshift
;
1409 const GLint avalue
= vert0
->color
[3];
1410 const GLint msavalue
= 255 - avalue
;
1411 const GLint rvalue
= vert0
->color
[0]*avalue
;
1412 const GLint gvalue
= vert0
->color
[1]*avalue
;
1413 const GLint bvalue
= vert0
->color
[2]*avalue
;
1418 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1420 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1421 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1422 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1427 #include "..\swrast\s_linetemp.h"
1429 #include "swrast/s_linetemp.h"
1435 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1436 * XXX update for GLchan
1439 flat_blend_rgba_z_line( GLcontext
*ctx
,
1440 const SWvertex
*vert0
, const SWvertex
*vert1
)
1442 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1443 const GLint rshift
= osmesa
->rshift
;
1444 const GLint gshift
= osmesa
->gshift
;
1445 const GLint bshift
= osmesa
->bshift
;
1446 const GLint avalue
= vert0
->color
[3];
1447 const GLint msavalue
= 256 - avalue
;
1448 const GLint rvalue
= vert0
->color
[0]*avalue
;
1449 const GLint gvalue
= vert0
->color
[1]*avalue
;
1450 const GLint bvalue
= vert0
->color
[2]*avalue
;
1454 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1458 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1460 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1461 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1462 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1467 #include "..\swrast\s_linetemp.h"
1469 #include "swrast/s_linetemp.h"
1475 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1476 * XXX update for GLchan
1479 flat_blend_rgba_z_line_write( GLcontext
*ctx
,
1480 const SWvertex
*vert0
, const SWvertex
*vert1
)
1482 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1483 const GLint rshift
= osmesa
->rshift
;
1484 const GLint gshift
= osmesa
->gshift
;
1485 const GLint bshift
= osmesa
->bshift
;
1486 const GLint avalue
= vert0
->color
[3];
1487 const GLint msavalue
= 256 - avalue
;
1488 const GLint rvalue
= vert0
->color
[0]*avalue
;
1489 const GLint gvalue
= vert0
->color
[1]*avalue
;
1490 const GLint bvalue
= vert0
->color
[2]*avalue
;
1494 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1498 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1500 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1501 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1502 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1508 #include "..\swrast\s_linetemp.h"
1510 #include "swrast/s_linetemp.h"
1516 * Analyze context state to see if we can provide a fast line drawing
1517 * function, like those in lines.c. Otherwise, return NULL.
1519 static swrast_line_func
1520 osmesa_choose_line_function( GLcontext
*ctx
)
1522 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1523 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1525 if (CHAN_BITS
!= 8) return NULL
;
1526 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
1527 if (ctx
->Line
.SmoothFlag
) return NULL
;
1528 if (ctx
->Texture
._ReallyEnabled
) return NULL
;
1529 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
1530 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
1531 if (ctx
->Line
.StippleFlag
) return NULL
;
1532 if (ctx
->Line
.SmoothFlag
) return NULL
;
1533 if (osmesa
->format
!= OSMESA_RGBA
&&
1534 osmesa
->format
!= OSMESA_BGRA
&&
1535 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
1537 if (swrast
->_RasterMask
==DEPTH_BIT
1538 && ctx
->Depth
.Func
==GL_LESS
1539 && ctx
->Depth
.Mask
==GL_TRUE
1540 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1541 return flat_rgba_z_line
;
1544 if (swrast
->_RasterMask
== 0) {
1545 return flat_rgba_line
;
1548 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1549 && ctx
->Depth
.Func
==GL_LESS
1550 && ctx
->Depth
.Mask
==GL_TRUE
1551 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1552 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1553 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1554 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1555 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1556 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1557 return flat_blend_rgba_z_line_write
;
1560 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1561 && ctx
->Depth
.Func
==GL_LESS
1562 && ctx
->Depth
.Mask
==GL_FALSE
1563 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1564 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1565 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1566 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1567 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1568 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1569 return flat_blend_rgba_z_line
;
1572 if (swrast
->_RasterMask
==BLEND_BIT
1573 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1574 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1575 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1576 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1577 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1578 return flat_blend_rgba_line
;
1585 /**********************************************************************/
1586 /***** Optimized triangle rendering *****/
1587 /**********************************************************************/
1591 * Smooth-shaded, z-less triangle, RGBA color.
1593 static void smooth_rgba_z_triangle( GLcontext
*ctx
,
1596 const SWvertex
*v2
)
1598 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1601 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1602 #define INTERP_RGB 1
1603 #define INTERP_ALPHA 1
1604 #define RENDER_SPAN( span ) \
1606 GLchan *img = PIXELADDR4(span.x, span.y); \
1607 for (i = 0; i < span.count; i++, img += 4) { \
1608 const GLdepth z = FixedToDepth(span.z); \
1609 if (z < zRow[i]) { \
1610 PACK_RGBA(img, FixedToInt(span.red), \
1611 FixedToInt(span.green), FixedToInt(span.blue), \
1612 FixedToInt(span.alpha)); \
1615 span.red += span.redStep; \
1616 span.green += span.greenStep; \
1617 span.blue += span.blueStep; \
1618 span.alpha += span.alphaStep; \
1619 span.z += span.zStep; \
1623 #include "..\swrast\s_tritemp.h"
1625 #include "swrast/s_tritemp.h"
1633 * Flat-shaded, z-less triangle, RGBA color.
1635 static void flat_rgba_z_triangle( GLcontext
*ctx
,
1638 const SWvertex
*v2
)
1640 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1642 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1643 #define SETUP_CODE \
1645 PACK_RGBA((GLchan *) &pixel, v0->color[0], v0->color[1], \
1646 v0->color[2], v0->color[3]);
1648 #define RENDER_SPAN( span ) \
1650 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1651 for (i = 0; i < span.count; i++) { \
1652 const GLdepth z = FixedToDepth(span.z); \
1653 if (z < zRow[i]) { \
1657 span.z += span.zStep; \
1661 #include "..\swrast\s_tritemp.h"
1663 #include "swrast/s_tritemp.h"
1670 * Return pointer to an accelerated triangle function if possible.
1672 static swrast_tri_func
1673 osmesa_choose_triangle_function( GLcontext
*ctx
)
1675 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1676 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1678 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
1679 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
1680 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
1681 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
1682 if (ctx
->Texture
._ReallyEnabled
) return (swrast_tri_func
) NULL
;
1683 if (osmesa
->format
!= OSMESA_RGBA
&&
1684 osmesa
->format
!= OSMESA_BGRA
&&
1685 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
1687 if (swrast
->_RasterMask
== DEPTH_BIT
&&
1688 ctx
->Depth
.Func
== GL_LESS
&&
1689 ctx
->Depth
.Mask
== GL_TRUE
&&
1690 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1691 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
1692 return smooth_rgba_z_triangle
;
1695 return flat_rgba_z_triangle
;
1698 return (swrast_tri_func
) NULL
;
1703 /* Override for the swrast triangle-selection function. Try to use one
1704 * of our internal triangle functions, otherwise fall back to the
1705 * standard swrast functions.
1707 static void osmesa_choose_triangle( GLcontext
*ctx
)
1709 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1711 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
1712 if (!swrast
->Triangle
)
1713 _swrast_choose_triangle( ctx
);
1716 static void osmesa_choose_line( GLcontext
*ctx
)
1718 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1720 swrast
->Line
= osmesa_choose_line_function( ctx
);
1722 _swrast_choose_line( ctx
);
1726 #define OSMESA_NEW_LINE (_NEW_LINE | \
1731 _SWRAST_NEW_RASTERMASK)
1733 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1738 _SWRAST_NEW_RASTERMASK)
1741 /* Extend the software rasterizer with our line and triangle
1744 static void osmesa_register_swrast_functions( GLcontext
*ctx
)
1746 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1748 swrast
->choose_line
= osmesa_choose_line
;
1749 swrast
->choose_triangle
= osmesa_choose_triangle
;
1751 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
1752 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1756 static const GLubyte
*get_string( GLcontext
*ctx
, GLenum name
)
1761 return (const GLubyte
*) "Mesa OffScreen";
1768 static void osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1770 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1771 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
1772 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1774 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
1777 * XXX these function pointers could be initialized just once during
1778 * context creation since they don't depend on any state changes.
1781 ctx
->Driver
.GetString
= get_string
;
1782 ctx
->Driver
.UpdateState
= osmesa_update_state
;
1783 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
1784 ctx
->Driver
.ResizeBuffersMESA
= _swrast_alloc_buffers
;
1785 ctx
->Driver
.GetBufferSize
= buffer_size
;
1787 ctx
->Driver
.Accum
= _swrast_Accum
;
1788 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
1789 ctx
->Driver
.Clear
= clear
;
1790 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
1791 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
1792 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
1794 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
1795 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
1796 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
1797 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
1798 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
1799 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
1800 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
1801 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
1803 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
1804 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
1805 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
1806 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
1807 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
1808 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
1809 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
1810 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
1811 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
1814 /* RGB(A) span/pixel functions */
1815 if (osmesa
->format
== OSMESA_RGB
) {
1816 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
1817 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
1818 swdd
->WriteMonoRGBASpan
= write_monocolor_span_RGB
;
1819 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
1820 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_RGB
;
1821 swdd
->ReadRGBASpan
= read_rgba_span3
;
1822 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
1824 else if (osmesa
->format
== OSMESA_BGR
) {
1825 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
1826 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
1827 swdd
->WriteMonoRGBASpan
= write_monocolor_span_BGR
;
1828 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
1829 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_BGR
;
1830 swdd
->ReadRGBASpan
= read_rgba_span3
;
1831 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
1834 /* 4 bytes / pixel in frame buffer */
1835 swdd
->WriteRGBSpan
= write_rgb_span
;
1836 swdd
->WriteRGBAPixels
= write_rgba_pixels
;
1837 swdd
->WriteMonoRGBASpan
= write_monocolor_span
;
1838 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels
;
1839 if (osmesa
->format
== OSMESA_RGBA
&&
1840 CHAN_TYPE
== GL_UNSIGNED_BYTE
&&
1841 RCOMP
==0 && GCOMP
==1 && BCOMP
==2 && ACOMP
==3) {
1842 /* special, fast case */
1843 swdd
->WriteRGBASpan
= write_rgba_span_rgba
;
1844 swdd
->ReadRGBASpan
= read_rgba_span_rgba
;
1847 swdd
->WriteRGBASpan
= write_rgba_span
;
1848 swdd
->ReadRGBASpan
= read_rgba_span
;
1850 swdd
->ReadRGBAPixels
= read_rgba_pixels
;
1853 /* CI span/pixel functions */
1854 swdd
->WriteCI32Span
= write_index32_span
;
1855 swdd
->WriteCI8Span
= write_index8_span
;
1856 swdd
->WriteMonoCISpan
= write_monoindex_span
;
1857 swdd
->WriteCI32Pixels
= write_index_pixels
;
1858 swdd
->WriteMonoCIPixels
= write_monoindex_pixels
;
1859 swdd
->ReadCI32Span
= read_index_span
;
1860 swdd
->ReadCI32Pixels
= read_index_pixels
;
1862 swdd
->SetReadBuffer
= set_read_buffer
;
1864 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1865 tnl
->Driver
.RenderStart
= _swsetup_RenderStart
;
1866 tnl
->Driver
.RenderFinish
= _swsetup_RenderFinish
;
1867 tnl
->Driver
.BuildProjectedVertices
= _swsetup_BuildProjectedVertices
;
1868 tnl
->Driver
.RenderPrimitive
= _swsetup_RenderPrimitive
;
1869 tnl
->Driver
.PointsFunc
= _swsetup_Points
;
1870 tnl
->Driver
.LineFunc
= _swsetup_Line
;
1871 tnl
->Driver
.TriangleFunc
= _swsetup_Triangle
;
1872 tnl
->Driver
.QuadFunc
= _swsetup_Quad
;
1873 tnl
->Driver
.ResetLineStipple
= _swrast_ResetLineStipple
;
1874 tnl
->Driver
.RenderInterp
= _swsetup_RenderInterp
;
1875 tnl
->Driver
.RenderCopyPV
= _swsetup_RenderCopyPV
;
1876 tnl
->Driver
.RenderClippedLine
= _swsetup_RenderClippedLine
;
1877 tnl
->Driver
.RenderClippedPolygon
= _swsetup_RenderClippedPolygon
;
1880 _swrast_InvalidateState( ctx
, new_state
);
1881 _swsetup_InvalidateState( ctx
, new_state
);
1882 _ac_InvalidateState( ctx
, new_state
);
1883 _tnl_InvalidateState( ctx
, new_state
);