1 /* $Id: osmesa.c,v 1.76 2002/03/29 17:20:48 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Off-Screen Mesa rendering / Rendering into client memory space
31 * Note on thread safety: this driver is thread safe. All
32 * functions are reentrant. The notion of current context is
33 * managed by the core _mesa_make_current() and _mesa_get_current_context()
34 * functions. Those functions are thread-safe.
39 #include "GL/osmesa.h"
43 #include "extensions.h"
49 #include "texformat.h"
51 #include "array_cache/acache.h"
52 #include "swrast/swrast.h"
53 #include "swrast_setup/swrast_setup.h"
54 #include "swrast/s_context.h"
55 #include "swrast/s_depth.h"
56 #include "swrast/s_lines.h"
57 #include "swrast/s_triangle.h"
59 #include "tnl/t_context.h"
60 #include "tnl/t_pipeline.h"
65 * This is the OS/Mesa context struct.
66 * Notice how it includes a GLcontext. By doing this we're mimicking
67 * C++ inheritance/derivation.
68 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
71 struct osmesa_context
{
72 GLcontext gl_ctx
; /* The core GL/Mesa context */
73 GLvisual
*gl_visual
; /* Describes the buffers */
74 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
75 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
76 void *buffer
; /* the image buffer */
77 GLint width
, height
; /* size of image buffer */
78 GLint rowlength
; /* number of pixels per row */
79 GLint userRowLength
; /* user-specified number of pixels per row */
80 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
82 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
83 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
84 GLboolean yup
; /* TRUE -> Y increases upward */
85 /* FALSE -> Y increases downward */
90 /* A forward declaration: */
91 static void osmesa_update_state( GLcontext
*ctx
, GLuint newstate
);
92 static void osmesa_register_swrast_functions( GLcontext
*ctx
);
96 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
100 /**********************************************************************/
101 /***** Public Functions *****/
102 /**********************************************************************/
106 * Create an Off-Screen Mesa rendering context. The only attribute needed is
107 * an RGBA vs Color-Index mode flag.
109 * Input: format - either GL_RGBA or GL_COLOR_INDEX
110 * sharelist - specifies another OSMesaContext with which to share
111 * display lists. NULL indicates no sharing.
112 * Return: an OSMesaContext or 0 if error
114 GLAPI OSMesaContext GLAPIENTRY
115 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
117 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
126 * Create context and specify size of ancillary buffers.
128 GLAPI OSMesaContext GLAPIENTRY
129 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
130 GLint accumBits
, OSMesaContext sharelist
)
132 OSMesaContext osmesa
;
133 GLint rshift
, gshift
, bshift
, ashift
;
134 GLint rind
, gind
, bind
, aind
;
135 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
138 const GLubyte
*i1
= (GLubyte
*) &i4
;
139 const GLint little_endian
= *i1
;
141 rind
= gind
= bind
= aind
= 0;
142 if (format
==OSMESA_COLOR_INDEX
) {
144 rshift
= gshift
= bshift
= ashift
= 0;
147 else if (format
==OSMESA_RGBA
) {
150 greenBits
= CHAN_BITS
;
151 blueBits
= CHAN_BITS
;
152 alphaBits
= CHAN_BITS
;
171 else if (format
==OSMESA_BGRA
) {
174 greenBits
= CHAN_BITS
;
175 blueBits
= CHAN_BITS
;
176 alphaBits
= CHAN_BITS
;
195 else if (format
==OSMESA_ARGB
) {
198 greenBits
= CHAN_BITS
;
199 blueBits
= CHAN_BITS
;
200 alphaBits
= CHAN_BITS
;
219 else if (format
==OSMESA_RGB
) {
222 greenBits
= CHAN_BITS
;
223 blueBits
= CHAN_BITS
;
234 else if (format
==OSMESA_BGR
) {
237 greenBits
= CHAN_BITS
;
238 blueBits
= CHAN_BITS
;
249 else if (format
==OSMESA_RGB_565
) {
259 rind
= 0; /* not used */
269 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
271 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
272 GL_FALSE
, /* double buffer */
273 GL_FALSE
, /* stereo */
284 alphaBits
? accumBits
: 0,
287 if (!osmesa
->gl_visual
) {
292 if (!_mesa_initialize_context(&osmesa
->gl_ctx
,
294 sharelist
? &sharelist
->gl_ctx
295 : (GLcontext
*) NULL
,
296 (void *) osmesa
, GL_TRUE
)) {
297 _mesa_destroy_visual( osmesa
->gl_visual
);
302 _mesa_enable_sw_extensions(&(osmesa
->gl_ctx
));
303 _mesa_enable_1_3_extensions(&(osmesa
->gl_ctx
));
305 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
306 (GLboolean
) ( osmesa
->gl_visual
->depthBits
> 0 ),
307 (GLboolean
) ( osmesa
->gl_visual
->stencilBits
> 0 ),
308 (GLboolean
) ( osmesa
->gl_visual
->accumRedBits
> 0 ),
309 GL_FALSE
/* s/w alpha */ );
311 if (!osmesa
->gl_buffer
) {
312 _mesa_destroy_visual( osmesa
->gl_visual
);
313 _mesa_free_context_data( &osmesa
->gl_ctx
);
317 osmesa
->format
= format
;
318 osmesa
->buffer
= NULL
;
321 osmesa
->userRowLength
= 0;
322 osmesa
->rowlength
= 0;
323 osmesa
->yup
= GL_TRUE
;
324 osmesa
->rshift
= rshift
;
325 osmesa
->gshift
= gshift
;
326 osmesa
->bshift
= bshift
;
327 osmesa
->ashift
= ashift
;
334 /* Initialize the software rasterizer and helper modules.
337 GLcontext
*ctx
= &osmesa
->gl_ctx
;
339 _swrast_CreateContext( ctx
);
340 _ac_CreateContext( ctx
);
341 _tnl_CreateContext( ctx
);
342 _swsetup_CreateContext( ctx
);
344 _swsetup_Wakeup( ctx
);
345 osmesa_register_swrast_functions( ctx
);
355 * Destroy an Off-Screen Mesa rendering context.
357 * Input: ctx - the context to destroy
359 GLAPI
void GLAPIENTRY
OSMesaDestroyContext( OSMesaContext ctx
)
362 _swsetup_DestroyContext( &ctx
->gl_ctx
);
363 _tnl_DestroyContext( &ctx
->gl_ctx
);
364 _ac_DestroyContext( &ctx
->gl_ctx
);
365 _swrast_DestroyContext( &ctx
->gl_ctx
);
367 _mesa_destroy_visual( ctx
->gl_visual
);
368 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
369 _mesa_free_context_data( &ctx
->gl_ctx
);
377 * Recompute the values of the context's rowaddr array.
379 static void compute_row_addresses( OSMesaContext ctx
)
381 GLint bytesPerPixel
, bytesPerRow
, i
;
382 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
384 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
386 bytesPerPixel
= 1 * sizeof(GLchan
);
388 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
390 bytesPerPixel
= 3 * sizeof(GLchan
);
392 else if (ctx
->format
== OSMESA_RGB_565
) {
393 /* 5/6/5 RGB pixel in 16 bits */
398 bytesPerPixel
= 4 * sizeof(GLchan
);
401 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
404 /* Y=0 is bottom line of window */
405 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
406 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
410 /* Y=0 is top line of window */
411 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
412 GLint j
= ctx
->height
- i
- 1;
413 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
420 * Bind an OSMesaContext to an image buffer. The image buffer is just a
421 * block of memory which the client provides. Its size must be at least
422 * as large as width*height*sizeof(type). Its address should be a multiple
423 * of 4 if using RGBA mode.
425 * Image data is stored in the order of glDrawPixels: row-major order
426 * with the lower-left image pixel stored in the first array position
427 * (ie. bottom-to-top).
429 * If the context's viewport hasn't been initialized yet, it will now be
430 * initialized to (0,0,width,height).
432 * Input: ctx - the rendering context
433 * buffer - the image buffer memory
434 * type - data type for pixel components
435 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
436 * are supported. But if Mesa's been compiled with CHAN_BITS==16
437 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
438 * with CHAN_BITS==32 then type must be GL_FLOAT.
439 * width, height - size of image buffer in pixels, at least 1
440 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
441 * invalid buffer address, invalid type, width<1, height<1,
442 * width>internal limit or height>internal limit.
444 GLAPI GLboolean GLAPIENTRY
445 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
446 GLsizei width
, GLsizei height
)
448 if (!ctx
|| !buffer
||
449 width
< 1 || height
< 1 ||
450 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
454 if (ctx
->format
== OSMESA_RGB_565
) {
455 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
458 else if (type
!= CHAN_TYPE
) {
462 osmesa_update_state( &ctx
->gl_ctx
, 0 );
463 _mesa_make_current( &ctx
->gl_ctx
, ctx
->gl_buffer
);
465 ctx
->buffer
= buffer
;
467 ctx
->height
= height
;
468 if (ctx
->userRowLength
)
469 ctx
->rowlength
= ctx
->userRowLength
;
471 ctx
->rowlength
= width
;
473 compute_row_addresses( ctx
);
476 if (ctx
->gl_ctx
.Viewport
.Width
==0) {
477 /* initialize viewport and scissor box to buffer size */
478 _mesa_Viewport( 0, 0, width
, height
);
479 ctx
->gl_ctx
.Scissor
.Width
= width
;
480 ctx
->gl_ctx
.Scissor
.Height
= height
;
488 GLAPI OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
490 GLcontext
*ctx
= _mesa_get_current_context();
492 return (OSMesaContext
) ctx
;
499 GLAPI
void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
501 OSMesaContext ctx
= OSMesaGetCurrentContext();
504 case OSMESA_ROW_LENGTH
:
506 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_VALUE
,
507 "OSMesaPixelStore(value)" );
510 ctx
->userRowLength
= value
;
511 ctx
->rowlength
= value
;
514 ctx
->yup
= value
? GL_TRUE
: GL_FALSE
;
517 _mesa_error( &ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
521 compute_row_addresses( ctx
);
525 GLAPI
void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
527 OSMesaContext ctx
= OSMesaGetCurrentContext();
534 *value
= ctx
->height
;
537 *value
= ctx
->format
;
542 case OSMESA_ROW_LENGTH
:
543 *value
= ctx
->rowlength
;
548 case OSMESA_MAX_WIDTH
:
551 case OSMESA_MAX_HEIGHT
:
555 _mesa_error(&ctx
->gl_ctx
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
561 * Return the depth buffer associated with an OSMesa context.
562 * Input: c - the OSMesa context
563 * Output: width, height - size of buffer in pixels
564 * bytesPerValue - bytes per depth value (2 or 4)
565 * buffer - pointer to depth buffer values
566 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
568 GLAPI GLboolean GLAPIENTRY
569 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
570 GLint
*bytesPerValue
, void **buffer
)
572 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
580 *width
= c
->gl_buffer
->Width
;
581 *height
= c
->gl_buffer
->Height
;
582 if (c
->gl_visual
->depthBits
<= 16)
583 *bytesPerValue
= sizeof(GLushort
);
585 *bytesPerValue
= sizeof(GLuint
);
586 *buffer
= c
->gl_buffer
->DepthBuffer
;
592 * Return the color buffer associated with an OSMesa context.
593 * Input: c - the OSMesa context
594 * Output: width, height - size of buffer in pixels
595 * format - the pixel format (OSMESA_FORMAT)
596 * buffer - pointer to color buffer values
597 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
599 GLAPI GLboolean GLAPIENTRY
600 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
601 GLint
*height
, GLint
*format
, void **buffer
)
619 /**********************************************************************/
620 /*** Device Driver Functions ***/
621 /**********************************************************************/
628 #define PACK_RGBA(DST, R, G, B, A) \
630 (DST)[osmesa->rInd] = R; \
631 (DST)[osmesa->gInd] = G; \
632 (DST)[osmesa->bInd] = B; \
633 (DST)[osmesa->aInd] = A; \
636 #define PACK_RGB(DST, R, G, B) \
643 #define PACK_BGR(DST, R, G, B) \
650 #define PACK_RGB_565(DST, R, G, B) \
652 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
656 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
657 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
658 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
659 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
662 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
663 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
664 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
665 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
669 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum mode
)
672 if (mode
==GL_FRONT_LEFT
) {
681 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLenum mode
)
683 /* separate read buffer not supported */
684 ASSERT(buffer
== ctx
->DrawBuffer
);
685 ASSERT(mode
== GL_FRONT_LEFT
);
689 static void clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
690 GLint x
, GLint y
, GLint width
, GLint height
)
692 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
693 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
695 /* sanity check - we only have a front-left buffer */
696 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
| DD_BACK_LEFT_BIT
| DD_BACK_RIGHT_BIT
)) == 0);
697 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
698 if (mask
& DD_FRONT_LEFT_BIT
) {
699 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
701 /* Clear whole CI buffer */
702 #if CHAN_TYPE == GL_UNSIGNED_BYTE
703 MEMSET(osmesa
->buffer
, ctx
->Color
.ClearIndex
,
704 osmesa
->rowlength
* osmesa
->height
);
706 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
707 GLchan
*buffer
= (GLchan
*) osmesa
->buffer
;
709 for (i
= 0; i
< n
; i
++) {
710 buffer
[i
] = ctx
->Color
.ClearIndex
;
715 /* Clear part of CI buffer */
716 const GLchan clearIndex
= (GLchan
) ctx
->Color
.ClearIndex
;
718 for (i
= 0; i
< height
; i
++) {
719 GLchan
*ptr1
= PIXELADDR1(x
, (y
+ i
));
720 for (j
= 0; j
< width
; j
++) {
721 *ptr1
++ = clearIndex
;
726 else if (osmesa
->format
== OSMESA_RGB
) {
727 const GLchan r
= ctx
->Color
.ClearColor
[0];
728 const GLchan g
= ctx
->Color
.ClearColor
[1];
729 const GLchan b
= ctx
->Color
.ClearColor
[2];
731 /* Clear whole RGB buffer */
732 GLuint n
= osmesa
->rowlength
* osmesa
->height
;
733 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
735 for (i
= 0; i
< n
; i
++) {
736 PACK_RGB(ptr3
, r
, g
, b
);
741 /* Clear part of RGB buffer */
743 for (i
= 0; i
< height
; i
++) {
744 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
745 for (j
= 0; j
< width
; j
++) {
746 PACK_RGB(ptr3
, r
, g
, b
);
752 else if (osmesa
->format
== OSMESA_BGR
) {
753 const GLchan r
= ctx
->Color
.ClearColor
[0];
754 const GLchan g
= ctx
->Color
.ClearColor
[1];
755 const GLchan b
= ctx
->Color
.ClearColor
[2];
757 /* Clear whole RGB buffer */
758 const GLint n
= osmesa
->rowlength
* osmesa
->height
;
759 GLchan
*ptr3
= (GLchan
*) osmesa
->buffer
;
761 for (i
= 0; i
< n
; i
++) {
762 PACK_BGR(ptr3
, r
, g
, b
);
767 /* Clear part of RGB buffer */
769 for (i
= 0; i
< height
; i
++) {
770 GLchan
*ptr3
= PIXELADDR3(x
, (y
+ i
));
771 for (j
= 0; j
< width
; j
++) {
772 PACK_BGR(ptr3
, r
, g
, b
);
778 else if (osmesa
->format
== OSMESA_RGB_565
) {
779 const GLchan r
= ctx
->Color
.ClearColor
[0];
780 const GLchan g
= ctx
->Color
.ClearColor
[1];
781 const GLchan b
= ctx
->Color
.ClearColor
[2];
783 PACK_RGB_565(clearPixel
, r
, g
, b
);
785 /* Clear whole RGB buffer */
786 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
787 GLushort
*ptr2
= (GLushort
*) osmesa
->buffer
;
789 for (i
= 0; i
< n
; i
++) {
795 /* clear scissored region */
797 for (i
= 0; i
< height
; i
++) {
798 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, (y
+ i
));
799 for (j
= 0; j
< width
; j
++) {
807 #if CHAN_TYPE == GL_UNSIGNED_BYTE
808 /* 4-byte pixel value */
810 GLchan
*clr
= (GLchan
*) &clearPixel
;
811 clr
[osmesa
->rInd
] = ctx
->Color
.ClearColor
[0];
812 clr
[osmesa
->gInd
] = ctx
->Color
.ClearColor
[1];
813 clr
[osmesa
->bInd
] = ctx
->Color
.ClearColor
[2];
814 clr
[osmesa
->aInd
] = ctx
->Color
.ClearColor
[3];
816 /* Clear whole RGBA buffer */
817 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
818 GLuint
*ptr4
= (GLuint
*) osmesa
->buffer
;
821 for (i
= 0; i
< n
; i
++) {
822 *ptr4
++ = clearPixel
;
826 BZERO(ptr4
, n
* sizeof(GLuint
));
830 /* Clear part of RGBA buffer */
832 for (i
= 0; i
< height
; i
++) {
833 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, (y
+ i
));
834 for (j
= 0; j
< width
; j
++) {
835 *ptr4
++ = clearPixel
;
840 const GLchan r
= ctx
->Color
.ClearColor
[0];
841 const GLchan g
= ctx
->Color
.ClearColor
[1];
842 const GLchan b
= ctx
->Color
.ClearColor
[2];
843 const GLchan a
= ctx
->Color
.ClearColor
[3];
845 /* Clear whole RGBA buffer */
846 const GLuint n
= osmesa
->rowlength
* osmesa
->height
;
847 GLchan
*p
= (GLchan
*) osmesa
->buffer
;
849 for (i
= 0; i
< n
; i
++) {
850 PACK_RGBA(p
, r
, g
, b
, a
);
855 /* Clear part of RGBA buffer */
857 for (i
= 0; i
< height
; i
++) {
858 GLchan
*p
= PIXELADDR4(x
, (y
+ i
));
859 for (j
= 0; j
< width
; j
++) {
860 PACK_RGBA(p
, r
, g
, b
, a
);
868 mask
&= ~DD_FRONT_LEFT_BIT
;
873 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
878 static void buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
880 /* don't use GET_CURRENT_CONTEXT(ctx) here - it's a problem on Windows */
881 GLcontext
*ctx
= (GLcontext
*) _glapi_get_context();
884 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
885 *width
= osmesa
->width
;
886 *height
= osmesa
->height
;
891 /**********************************************************************/
892 /***** Read/write spans/arrays of RGBA pixels *****/
893 /**********************************************************************/
895 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
897 write_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
898 CONST GLchan rgba
[][4], const GLubyte mask
[] )
900 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
901 GLchan
*p
= PIXELADDR4(x
, y
);
904 for (i
= 0; i
< n
; i
++, p
+= 4) {
906 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
907 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
912 for (i
= 0; i
< n
; i
++, p
+= 4) {
913 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
914 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
920 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
922 write_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
923 CONST GLchan rgba
[][4], const GLubyte mask
[] )
925 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
926 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
927 const GLuint
*rgba4
= (const GLuint
*) rgba
;
929 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
931 for (i
= 0; i
< n
; i
++) {
938 MEMCPY( ptr4
, rgba4
, n
* 4 );
943 /* Write RGB pixels to an RGBA (or permuted) buffer. */
945 write_rgb_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
946 CONST GLchan rgb
[][3], const GLubyte mask
[] )
948 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
949 GLchan
*p
= PIXELADDR4(x
, y
);
952 for (i
= 0; i
< n
; i
++, p
+=4) {
954 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
959 for (i
= 0; i
< n
; i
++, p
+=4) {
960 PACK_RGBA(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
], CHAN_MAX
);
968 write_monocolor_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
969 const GLchan color
[4], const GLubyte mask
[] )
971 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
972 GLchan
*p
= PIXELADDR4(x
, y
);
974 for (i
= 0; i
< n
; i
++, p
+= 4) {
976 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
984 write_rgba_pixels( const GLcontext
*ctx
, GLuint n
,
985 const GLint x
[], const GLint y
[],
986 CONST GLchan rgba
[][4], const GLubyte mask
[] )
988 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
990 for (i
= 0; i
< n
; i
++) {
992 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
993 PACK_RGBA(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
],
994 rgba
[i
][BCOMP
], rgba
[i
][ACOMP
]);
1002 write_monocolor_pixels( const GLcontext
*ctx
, GLuint n
,
1003 const GLint x
[], const GLint y
[],
1004 const GLchan color
[4], const GLubyte mask
[] )
1006 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1008 for (i
= 0; i
< n
; i
++) {
1010 GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1011 PACK_RGBA(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
], color
[ACOMP
]);
1018 read_rgba_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1021 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1023 GLchan
*p
= PIXELADDR4(x
, y
);
1024 for (i
= 0; i
< n
; i
++, p
+= 4) {
1025 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1026 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1027 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1028 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1033 /* Read RGBA pixels from an RGBA buffer */
1035 read_rgba_span_rgba( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1038 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1039 GLuint
*ptr4
= (GLuint
*) PIXELADDR4(x
, y
);
1040 MEMCPY( rgba
, ptr4
, n
* 4 * sizeof(GLchan
) );
1045 read_rgba_pixels( const GLcontext
*ctx
,
1046 GLuint n
, const GLint x
[], const GLint y
[],
1047 GLchan rgba
[][4], const GLubyte mask
[] )
1049 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1051 for (i
= 0; i
< n
; i
++) {
1053 const GLchan
*p
= PIXELADDR4(x
[i
], y
[i
]);
1054 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1055 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1056 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1057 rgba
[i
][ACOMP
] = UNPACK_ALPHA(p
);
1062 /**********************************************************************/
1063 /***** 3 byte RGB pixel support funcs *****/
1064 /**********************************************************************/
1066 /* Write RGBA pixels to an RGB buffer. */
1068 write_rgba_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1069 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1071 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1072 GLchan
*p
= PIXELADDR3(x
, y
);
1075 for (i
= 0; i
< n
; i
++, p
+= 3) {
1077 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1082 for (i
= 0; i
< n
; i
++, p
+= 3) {
1083 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1088 /* Write RGBA pixels to an BGR buffer. */
1090 write_rgba_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1091 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1093 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1094 GLchan
*p
= PIXELADDR3(x
, y
);
1097 for (i
= 0; i
< n
; i
++, p
+= 3) {
1099 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1104 for (i
= 0; i
< n
; i
++, p
+= 3) {
1105 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1110 /* Write RGB pixels to an RGB buffer. */
1112 write_rgb_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1113 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1115 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1116 GLchan
*p
= PIXELADDR3(x
, y
);
1119 for (i
= 0; i
< n
; i
++, p
+= 3) {
1121 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1126 for (i
= 0; i
< n
; i
++, p
+= 3) {
1127 PACK_RGB(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1132 /* Write RGB pixels to an BGR buffer. */
1134 write_rgb_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1135 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1137 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1138 GLchan
*p
= PIXELADDR3(x
, y
);
1141 for (i
= 0; i
< n
; i
++, p
+= 3) {
1143 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1148 for (i
= 0; i
< n
; i
++, p
+= 3) {
1149 PACK_BGR(p
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1156 write_monocolor_span_RGB( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1157 const GLchan color
[4], const GLubyte mask
[] )
1159 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1160 GLchan
*p
= PIXELADDR3(x
, y
);
1162 for (i
= 0; i
< n
; i
++, p
+= 3) {
1164 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1170 write_monocolor_span_BGR( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1171 const GLchan color
[4], const GLubyte mask
[] )
1173 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1174 GLchan
*p
= PIXELADDR3(x
, y
);
1176 for (i
= 0; i
< n
; i
++, p
+= 3) {
1178 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1184 write_rgba_pixels_RGB( const GLcontext
*ctx
, GLuint n
,
1185 const GLint x
[], const GLint y
[],
1186 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1188 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1190 for (i
= 0; i
< n
; i
++) {
1192 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1193 PACK_RGB(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1199 write_rgba_pixels_BGR( const GLcontext
*ctx
, GLuint n
,
1200 const GLint x
[], const GLint y
[],
1201 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1203 const OSMesaContext osmesa
= (const OSMesaContext
) ctx
;
1205 for (i
= 0; i
< n
; i
++) {
1207 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1208 PACK_BGR(p
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1214 write_monocolor_pixels_RGB( const GLcontext
*ctx
,
1215 GLuint n
, const GLint x
[], const GLint y
[],
1216 const GLchan color
[4], const GLubyte mask
[] )
1218 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1220 for (i
= 0; i
< n
; i
++) {
1222 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1223 PACK_RGB(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1229 write_monocolor_pixels_BGR( const GLcontext
*ctx
,
1230 GLuint n
, const GLint x
[], const GLint y
[],
1231 const GLchan color
[4], const GLubyte mask
[] )
1233 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1235 for (i
= 0; i
< n
; i
++) {
1237 GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1238 PACK_BGR(p
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1244 read_rgba_span3( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1247 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1249 const GLchan
*p
= PIXELADDR3(x
, y
);
1250 for (i
= 0; i
< n
; i
++, p
+= 3) {
1251 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1252 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1253 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1254 rgba
[i
][ACOMP
] = CHAN_MAX
;
1259 read_rgba_pixels3( const GLcontext
*ctx
,
1260 GLuint n
, const GLint x
[], const GLint y
[],
1261 GLchan rgba
[][4], const GLubyte mask
[] )
1263 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1265 for (i
= 0; i
< n
; i
++) {
1267 const GLchan
*p
= PIXELADDR3(x
[i
], y
[i
]);
1268 rgba
[i
][RCOMP
] = UNPACK_RED(p
);
1269 rgba
[i
][GCOMP
] = UNPACK_GREEN(p
);
1270 rgba
[i
][BCOMP
] = UNPACK_BLUE(p
);
1271 rgba
[i
][ACOMP
] = CHAN_MAX
;
1277 /**********************************************************************/
1278 /***** 2 byte RGB pixel support funcs *****/
1279 /**********************************************************************/
1281 /* Write RGBA pixels to an RGB_565 buffer. */
1283 write_rgba_span2( const GLcontext
*ctx
,
1284 GLuint n
, GLint x
, GLint y
,
1285 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1287 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1288 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1291 for (i
= 0; i
< n
; i
++, ptr2
++) {
1293 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1298 for (i
= 0; i
< n
; i
++, ptr2
++) {
1299 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1305 /* Write RGB pixels to an RGB_565 buffer. */
1307 write_rgb_span2( const GLcontext
*ctx
,
1308 GLuint n
, GLint x
, GLint y
,
1309 CONST GLchan rgb
[][3], const GLubyte mask
[] )
1311 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1312 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1315 for (i
= 0; i
< n
; i
++, ptr2
++) {
1317 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1322 for (i
= 0; i
< n
; i
++, ptr2
++) {
1323 PACK_RGB_565(*ptr2
, rgb
[i
][RCOMP
], rgb
[i
][GCOMP
], rgb
[i
][BCOMP
]);
1330 write_monocolor_span2( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1331 const GLchan color
[4], const GLubyte mask
[] )
1333 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1335 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
, y
);
1337 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1338 for (i
= 0; i
< n
; i
++, ptr2
++) {
1347 write_rgba_pixels2( const GLcontext
*ctx
,
1348 GLuint n
, const GLint x
[], const GLint y
[],
1349 CONST GLchan rgba
[][4], const GLubyte mask
[] )
1351 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1353 for (i
= 0; i
< n
; i
++) {
1355 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1356 PACK_RGB_565(*ptr2
, rgba
[i
][RCOMP
], rgba
[i
][GCOMP
], rgba
[i
][BCOMP
]);
1362 write_monocolor_pixels2( const GLcontext
*ctx
,
1363 GLuint n
, const GLint x
[], const GLint y
[],
1364 const GLchan color
[4], const GLubyte mask
[] )
1366 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1369 PACK_RGB_565(pixel
, color
[RCOMP
], color
[GCOMP
], color
[BCOMP
]);
1370 for (i
= 0; i
< n
; i
++) {
1372 GLushort
*ptr2
= (GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1379 read_rgba_span2( const GLcontext
*ctx
,
1380 GLuint n
, GLint x
, GLint y
,
1383 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1385 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
, y
);
1386 for (i
= 0; i
< n
; i
++, ptr2
++) {
1387 /* This should be fixed to get the low bits right */
1388 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFe;
1389 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFc;
1390 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFe;
1396 read_rgba_pixels2( const GLcontext
*ctx
,
1397 GLuint n
, const GLint x
[], const GLint y
[],
1398 GLchan rgba
[][4], const GLubyte mask
[] )
1400 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1402 for (i
= 0; i
< n
; i
++) {
1404 /* This should be fixed to get the low bits right */
1405 const GLushort
*ptr2
= (const GLushort
*) PIXELADDR2(x
[i
],y
[i
]);
1406 rgba
[i
][RCOMP
] = (*ptr2
>> 8) & 0xFE;
1407 rgba
[i
][GCOMP
] = (*ptr2
>> 3) & 0xFC;
1408 rgba
[i
][BCOMP
] = (*ptr2
<< 3) & 0xFE;
1416 /**********************************************************************/
1417 /***** Read/write spans/arrays of CI pixels *****/
1418 /**********************************************************************/
1420 /* Write 32-bit color index to buffer */
1422 write_index32_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1423 const GLuint index
[], const GLubyte mask
[] )
1425 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1426 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1429 for (i
=0;i
<n
;i
++,ptr1
++) {
1431 *ptr1
= (GLchan
) index
[i
];
1436 for (i
=0;i
<n
;i
++,ptr1
++) {
1437 *ptr1
= (GLchan
) index
[i
];
1443 /* Write 8-bit color index to buffer */
1445 write_index8_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1446 const GLubyte index
[], const GLubyte mask
[] )
1448 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1449 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1452 for (i
=0;i
<n
;i
++,ptr1
++) {
1454 *ptr1
= (GLchan
) index
[i
];
1459 MEMCPY(ptr1
, index
, n
* sizeof(GLchan
));
1465 write_monoindex_span( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
1466 GLuint colorIndex
, const GLubyte mask
[] )
1468 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1469 GLchan
*ptr1
= PIXELADDR1(x
, y
);
1471 for (i
=0;i
<n
;i
++,ptr1
++) {
1473 *ptr1
= (GLchan
) colorIndex
;
1480 write_index_pixels( const GLcontext
*ctx
,
1481 GLuint n
, const GLint x
[], const GLint y
[],
1482 const GLuint index
[], const GLubyte mask
[] )
1484 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1488 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1489 *ptr1
= (GLchan
) index
[i
];
1496 write_monoindex_pixels( const GLcontext
*ctx
,
1497 GLuint n
, const GLint x
[], const GLint y
[],
1498 GLuint colorIndex
, const GLubyte mask
[] )
1500 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1504 GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1505 *ptr1
= (GLchan
) colorIndex
;
1512 read_index_span( const GLcontext
*ctx
,
1513 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1515 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1517 const GLchan
*ptr1
= (const GLchan
*) PIXELADDR1(x
, y
);
1518 for (i
=0;i
<n
;i
++,ptr1
++) {
1519 index
[i
] = (GLuint
) *ptr1
;
1525 read_index_pixels( const GLcontext
*ctx
,
1526 GLuint n
, const GLint x
[], const GLint y
[],
1527 GLuint index
[], const GLubyte mask
[] )
1529 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1533 const GLchan
*ptr1
= PIXELADDR1(x
[i
], y
[i
]);
1534 index
[i
] = (GLuint
) *ptr1
;
1541 /**********************************************************************/
1542 /***** Optimized line rendering *****/
1543 /**********************************************************************/
1547 * Draw a flat-shaded, RGB line into an osmesa buffer.
1550 flat_rgba_line( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1552 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1553 const GLchan
*color
= vert1
->color
;
1557 #define PLOT(X, Y) \
1559 GLchan *p = PIXELADDR4(X, Y); \
1560 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1564 #include "..\swrast\s_linetemp.h"
1566 #include "swrast/s_linetemp.h"
1572 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1575 flat_rgba_z_line(GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
1577 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1578 const GLchan
*color
= vert1
->color
;
1582 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1584 #define PLOT(X, Y) \
1587 GLchan *p = PIXELADDR4(X, Y); \
1588 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1589 color[BCOMP], color[ACOMP]); \
1596 #include "..\swrast\s_linetemp.h"
1598 #include "swrast/s_linetemp.h"
1604 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1605 * XXX update for GLchan
1608 flat_blend_rgba_line( GLcontext
*ctx
,
1609 const SWvertex
*vert0
, const SWvertex
*vert1
)
1611 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1612 const GLint rshift
= osmesa
->rshift
;
1613 const GLint gshift
= osmesa
->gshift
;
1614 const GLint bshift
= osmesa
->bshift
;
1615 const GLint avalue
= vert0
->color
[3];
1616 const GLint msavalue
= CHAN_MAX
- avalue
;
1617 const GLint rvalue
= vert1
->color
[0]*avalue
;
1618 const GLint gvalue
= vert1
->color
[1]*avalue
;
1619 const GLint bvalue
= vert1
->color
[2]*avalue
;
1624 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1626 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1627 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1628 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1632 #if 0 /* XXX use this in the future */
1635 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1636 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1637 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1638 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1639 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1644 #include "..\swrast\s_linetemp.h"
1646 #include "swrast/s_linetemp.h"
1652 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1653 * But don't write to Z buffer.
1654 * XXX update for GLchan
1657 flat_blend_rgba_z_line( GLcontext
*ctx
,
1658 const SWvertex
*vert0
, const SWvertex
*vert1
)
1660 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1661 const GLint rshift
= osmesa
->rshift
;
1662 const GLint gshift
= osmesa
->gshift
;
1663 const GLint bshift
= osmesa
->bshift
;
1664 const GLint avalue
= vert0
->color
[3];
1665 const GLint msavalue
= 256 - avalue
;
1666 const GLint rvalue
= vert1
->color
[0]*avalue
;
1667 const GLint gvalue
= vert1
->color
[1]*avalue
;
1668 const GLint bvalue
= vert1
->color
[2]*avalue
;
1672 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1676 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1678 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1679 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1680 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1684 #if 0 /* XXX use this in the future */
1687 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1688 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1689 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1690 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1691 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1696 #include "..\swrast\s_linetemp.h"
1698 #include "swrast/s_linetemp.h"
1704 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1705 * XXX update for GLchan
1708 flat_blend_rgba_z_line_write( GLcontext
*ctx
,
1709 const SWvertex
*vert0
, const SWvertex
*vert1
)
1711 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1712 const GLint rshift
= osmesa
->rshift
;
1713 const GLint gshift
= osmesa
->gshift
;
1714 const GLint bshift
= osmesa
->bshift
;
1715 const GLint avalue
= vert0
->color
[3];
1716 const GLint msavalue
= 256 - avalue
;
1717 const GLint rvalue
= vert1
->color
[0]*avalue
;
1718 const GLint gvalue
= vert1
->color
[1]*avalue
;
1719 const GLint bvalue
= vert1
->color
[2]*avalue
;
1723 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1727 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1729 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1730 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1731 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1736 #if 0 /* XXX use this in the future */
1739 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1740 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1741 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1742 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1743 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1749 #include "..\swrast\s_linetemp.h"
1751 #include "swrast/s_linetemp.h"
1757 * Analyze context state to see if we can provide a fast line drawing
1758 * function, like those in lines.c. Otherwise, return NULL.
1760 static swrast_line_func
1761 osmesa_choose_line_function( GLcontext
*ctx
)
1763 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1764 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1766 if (CHAN_BITS
!= 8) return NULL
;
1767 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
1768 if (ctx
->Line
.SmoothFlag
) return NULL
;
1769 if (ctx
->Texture
._ReallyEnabled
) return NULL
;
1770 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
1771 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
1772 if (ctx
->Line
.StippleFlag
) return NULL
;
1773 if (ctx
->Line
.SmoothFlag
) return NULL
;
1774 if (osmesa
->format
!= OSMESA_RGBA
&&
1775 osmesa
->format
!= OSMESA_BGRA
&&
1776 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
1778 if (swrast
->_RasterMask
==DEPTH_BIT
1779 && ctx
->Depth
.Func
==GL_LESS
1780 && ctx
->Depth
.Mask
==GL_TRUE
1781 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1782 return (swrast_line_func
) flat_rgba_z_line
;
1785 if (swrast
->_RasterMask
== 0) {
1786 return (swrast_line_func
) flat_rgba_line
;
1789 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1790 && ctx
->Depth
.Func
==GL_LESS
1791 && ctx
->Depth
.Mask
==GL_TRUE
1792 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1793 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1794 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1795 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1796 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1797 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1798 return (swrast_line_func
) flat_blend_rgba_z_line_write
;
1801 if (swrast
->_RasterMask
==(DEPTH_BIT
|BLEND_BIT
)
1802 && ctx
->Depth
.Func
==GL_LESS
1803 && ctx
->Depth
.Mask
==GL_FALSE
1804 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
1805 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1806 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1807 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1808 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1809 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1810 return (swrast_line_func
) flat_blend_rgba_z_line
;
1813 if (swrast
->_RasterMask
==BLEND_BIT
1814 && ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
1815 && ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
1816 && ctx
->Color
.BlendSrcA
==GL_SRC_ALPHA
1817 && ctx
->Color
.BlendDstA
==GL_ONE_MINUS_SRC_ALPHA
1818 && ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
1819 return (swrast_line_func
) flat_blend_rgba_line
;
1822 return (swrast_line_func
) NULL
;
1826 /**********************************************************************/
1827 /***** Optimized triangle rendering *****/
1828 /**********************************************************************/
1832 * Smooth-shaded, z-less triangle, RGBA color.
1834 static void smooth_rgba_z_triangle( GLcontext
*ctx
,
1837 const SWvertex
*v2
)
1839 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1842 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1843 #define INTERP_RGB 1
1844 #define INTERP_ALPHA 1
1845 #define RENDER_SPAN( span ) \
1847 GLchan *img = PIXELADDR4(span.x, span.y); \
1848 for (i = 0; i < span.end; i++, img += 4) { \
1849 const GLdepth z = FixedToDepth(span.z); \
1850 if (z < zRow[i]) { \
1851 PACK_RGBA(img, FixedToChan(span.red), \
1852 FixedToChan(span.green), FixedToChan(span.blue), \
1853 FixedToChan(span.alpha)); \
1856 span.red += span.redStep; \
1857 span.green += span.greenStep; \
1858 span.blue += span.blueStep; \
1859 span.alpha += span.alphaStep; \
1860 span.z += span.zStep; \
1864 #include "..\swrast\s_tritemp.h"
1866 #include "swrast/s_tritemp.h"
1874 * Flat-shaded, z-less triangle, RGBA color.
1876 static void flat_rgba_z_triangle( GLcontext
*ctx
,
1879 const SWvertex
*v2
)
1881 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1883 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1884 #define SETUP_CODE \
1886 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
1887 v2->color[2], v2->color[3]);
1889 #define RENDER_SPAN( span ) \
1891 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1892 for (i = 0; i < span.end; i++) { \
1893 const GLdepth z = FixedToDepth(span.z); \
1894 if (z < zRow[i]) { \
1898 span.z += span.zStep; \
1902 #include "..\swrast\s_tritemp.h"
1904 #include "swrast/s_tritemp.h"
1911 * Return pointer to an accelerated triangle function if possible.
1913 static swrast_tri_func
1914 osmesa_choose_triangle_function( GLcontext
*ctx
)
1916 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
1917 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1919 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
1920 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
1921 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
1922 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
1923 if (ctx
->Texture
._ReallyEnabled
) return (swrast_tri_func
) NULL
;
1924 if (osmesa
->format
!= OSMESA_RGBA
&&
1925 osmesa
->format
!= OSMESA_BGRA
&&
1926 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
1928 if (swrast
->_RasterMask
== DEPTH_BIT
&&
1929 ctx
->Depth
.Func
== GL_LESS
&&
1930 ctx
->Depth
.Mask
== GL_TRUE
&&
1931 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
1932 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
1933 return (swrast_tri_func
) smooth_rgba_z_triangle
;
1936 return (swrast_tri_func
) flat_rgba_z_triangle
;
1939 return (swrast_tri_func
) NULL
;
1944 /* Override for the swrast triangle-selection function. Try to use one
1945 * of our internal triangle functions, otherwise fall back to the
1946 * standard swrast functions.
1948 static void osmesa_choose_triangle( GLcontext
*ctx
)
1950 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1952 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
1953 if (!swrast
->Triangle
)
1954 _swrast_choose_triangle( ctx
);
1957 static void osmesa_choose_line( GLcontext
*ctx
)
1959 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1961 swrast
->Line
= osmesa_choose_line_function( ctx
);
1963 _swrast_choose_line( ctx
);
1967 #define OSMESA_NEW_LINE (_NEW_LINE | \
1972 _SWRAST_NEW_RASTERMASK)
1974 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1979 _SWRAST_NEW_RASTERMASK)
1982 /* Extend the software rasterizer with our line and triangle
1985 static void osmesa_register_swrast_functions( GLcontext
*ctx
)
1987 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1989 swrast
->choose_line
= osmesa_choose_line
;
1990 swrast
->choose_triangle
= osmesa_choose_triangle
;
1992 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
1993 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1997 static const GLubyte
*get_string( GLcontext
*ctx
, GLenum name
)
2003 return (const GLubyte
*) "Mesa OffScreen32";
2004 #elif CHAN_BITS == 16
2005 return (const GLubyte
*) "Mesa OffScreen16";
2007 return (const GLubyte
*) "Mesa OffScreen";
2015 static void osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
2017 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
2018 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
2019 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
2021 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
2024 * XXX these function pointers could be initialized just once during
2025 * context creation since they don't depend on any state changes.
2028 ctx
->Driver
.GetString
= get_string
;
2029 ctx
->Driver
.UpdateState
= osmesa_update_state
;
2030 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
2031 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
2032 ctx
->Driver
.GetBufferSize
= buffer_size
;
2034 ctx
->Driver
.Accum
= _swrast_Accum
;
2035 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
2036 ctx
->Driver
.Clear
= clear
;
2037 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
2038 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
2039 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
2041 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
2042 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
2043 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
2044 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
2045 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
2046 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
2047 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
2048 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
2050 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
2051 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
2052 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
2053 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
2054 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
2055 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
2056 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
2057 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
2058 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
2060 ctx
->Driver
.BaseCompressedTexFormat
= _mesa_base_compressed_texformat
;
2061 ctx
->Driver
.CompressedTextureSize
= _mesa_compressed_texture_size
;
2062 ctx
->Driver
.GetCompressedTexImage
= _mesa_get_compressed_teximage
;
2064 /* RGB(A) span/pixel functions */
2065 if (osmesa
->format
== OSMESA_RGB
) {
2066 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
2067 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
2068 swdd
->WriteMonoRGBASpan
= write_monocolor_span_RGB
;
2069 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
2070 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_RGB
;
2071 swdd
->ReadRGBASpan
= read_rgba_span3
;
2072 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2074 else if (osmesa
->format
== OSMESA_BGR
) {
2075 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
2076 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
2077 swdd
->WriteMonoRGBASpan
= write_monocolor_span_BGR
;
2078 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
2079 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels_BGR
;
2080 swdd
->ReadRGBASpan
= read_rgba_span3
;
2081 swdd
->ReadRGBAPixels
= read_rgba_pixels3
;
2083 else if (osmesa
->format
== OSMESA_RGB_565
) {
2084 swdd
->WriteRGBASpan
= write_rgba_span2
;
2085 swdd
->WriteRGBSpan
= write_rgb_span2
;
2086 swdd
->WriteMonoRGBASpan
= write_monocolor_span2
;
2087 swdd
->WriteRGBAPixels
= write_rgba_pixels2
;
2088 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels2
;
2089 swdd
->ReadRGBASpan
= read_rgba_span2
;
2090 swdd
->ReadRGBAPixels
= read_rgba_pixels2
;
2093 /* 4 GLchan / pixel in frame buffer */
2094 swdd
->WriteRGBSpan
= write_rgb_span
;
2095 swdd
->WriteRGBAPixels
= write_rgba_pixels
;
2096 swdd
->WriteMonoRGBASpan
= write_monocolor_span
;
2097 swdd
->WriteMonoRGBAPixels
= write_monocolor_pixels
;
2098 if (osmesa
->format
== OSMESA_RGBA
&&
2099 CHAN_TYPE
== GL_UNSIGNED_BYTE
&&
2100 RCOMP
==0 && GCOMP
==1 && BCOMP
==2 && ACOMP
==3) {
2101 /* special, fast case */
2102 swdd
->WriteRGBASpan
= write_rgba_span_rgba
;
2103 swdd
->ReadRGBASpan
= read_rgba_span_rgba
;
2106 swdd
->WriteRGBASpan
= write_rgba_span
;
2107 swdd
->ReadRGBASpan
= read_rgba_span
;
2109 swdd
->ReadRGBAPixels
= read_rgba_pixels
;
2112 /* CI span/pixel functions */
2113 swdd
->WriteCI32Span
= write_index32_span
;
2114 swdd
->WriteCI8Span
= write_index8_span
;
2115 swdd
->WriteMonoCISpan
= write_monoindex_span
;
2116 swdd
->WriteCI32Pixels
= write_index_pixels
;
2117 swdd
->WriteMonoCIPixels
= write_monoindex_pixels
;
2118 swdd
->ReadCI32Span
= read_index_span
;
2119 swdd
->ReadCI32Pixels
= read_index_pixels
;
2121 swdd
->SetReadBuffer
= set_read_buffer
;
2123 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
2125 _swrast_InvalidateState( ctx
, new_state
);
2126 _swsetup_InvalidateState( ctx
, new_state
);
2127 _ac_InvalidateState( ctx
, new_state
);
2128 _tnl_InvalidateState( ctx
, new_state
);