2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Off-Screen Mesa rendering / Rendering into client memory space
29 * Note on thread safety: this driver is thread safe. All
30 * functions are reentrant. The notion of current context is
31 * managed by the core _mesa_make_current() and _mesa_get_current_context()
32 * functions. Those functions are thread-safe.
37 #include "main/glheader.h"
38 #include "GL/osmesa.h"
39 #include "main/api_exec.h"
40 #include "main/context.h"
41 #include "main/extensions.h"
42 #include "main/formats.h"
43 #include "main/framebuffer.h"
44 #include "main/imports.h"
45 #include "main/macros.h"
46 #include "main/mipmap.h"
47 #include "main/mtypes.h"
48 #include "main/renderbuffer.h"
49 #include "main/version.h"
50 #include "main/vtxfmt.h"
51 #include "swrast/swrast.h"
52 #include "swrast_setup/swrast_setup.h"
53 #include "swrast/s_context.h"
54 #include "swrast/s_lines.h"
55 #include "swrast/s_renderbuffer.h"
56 #include "swrast/s_triangle.h"
58 #include "tnl/t_context.h"
59 #include "tnl/t_pipeline.h"
60 #include "drivers/common/driverfuncs.h"
61 #include "drivers/common/meta.h"
65 #define OSMESA_RENDERBUFFER_CLASS 0x053
69 * OSMesa rendering context, derived from core Mesa struct gl_context.
73 struct gl_context mesa
; /*< Base class - this must be first */
74 struct gl_config
*gl_visual
; /*< Describes the buffers */
75 struct swrast_renderbuffer
*srb
; /*< The user's colorbuffer */
76 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
77 GLenum format
; /*< User-specified context format */
78 GLint userRowLength
; /*< user-specified number of pixels per row */
79 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
80 GLvoid
*rowaddr
[SWRAST_MAX_HEIGHT
]; /*< address of first pixel in each image row */
81 GLboolean yup
; /*< TRUE -> Y increases upward */
82 /*< FALSE -> Y increases downward */
87 static inline OSMesaContext
88 OSMESA_CONTEXT(struct gl_context
*ctx
)
90 /* Just cast, since we're using structure containment */
91 return (OSMesaContext
) ctx
;
95 /**********************************************************************/
96 /*** Private Device Driver Functions ***/
97 /**********************************************************************/
100 static const GLubyte
*
101 get_string( struct gl_context
*ctx
, GLenum name
)
107 return (const GLubyte
*) "Mesa OffScreen32";
108 #elif CHAN_BITS == 16
109 return (const GLubyte
*) "Mesa OffScreen16";
111 return (const GLubyte
*) "Mesa OffScreen";
120 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
122 /* easy - just propogate */
123 _swrast_InvalidateState( ctx
, new_state
);
124 _swsetup_InvalidateState( ctx
, new_state
);
125 _tnl_InvalidateState( ctx
, new_state
);
126 _vbo_InvalidateState( ctx
, new_state
);
132 * Macros for optimized line/triangle rendering.
133 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
136 #define PACK_RGBA(DST, R, G, B, A) \
138 (DST)[osmesa->rInd] = R; \
139 (DST)[osmesa->gInd] = G; \
140 (DST)[osmesa->bInd] = B; \
141 (DST)[osmesa->aInd] = A; \
144 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
148 * Draw a flat-shaded, RGB line into an osmesa buffer.
150 #define NAME flat_rgba_line
153 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
154 const GLchan *color = vert1->color;
158 GLchan *p = PIXELADDR4(X, Y); \
159 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
162 #include "swrast/s_linetemp.h"
167 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
169 #define NAME flat_rgba_z_line
172 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
174 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
175 const GLchan *color = vert1->color;
180 GLchan *p = PIXELADDR4(X, Y); \
181 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
182 color[BCOMP], color[ACOMP]); \
187 #include "swrast/s_linetemp.h"
192 * Analyze context state to see if we can provide a fast line drawing
193 * function. Otherwise, return NULL.
195 static swrast_line_func
196 osmesa_choose_line_function( struct gl_context
*ctx
)
198 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
199 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
201 if (ctx
->DrawBuffer
&&
202 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
203 /* the special-case line functions in this file don't work
204 * for float color channels.
209 if (ctx
->RenderMode
!= GL_RENDER
||
210 ctx
->Line
.SmoothFlag
||
211 ctx
->Texture
._MaxEnabledTexImageUnit
== -1 ||
212 ctx
->Light
.ShadeModel
!= GL_FLAT
||
213 ctx
->Line
.Width
!= 1.0F
||
214 ctx
->Line
.StippleFlag
||
215 ctx
->Line
.SmoothFlag
) {
219 if (osmesa
->format
!= OSMESA_RGBA
&&
220 osmesa
->format
!= OSMESA_BGRA
&&
221 osmesa
->format
!= OSMESA_ARGB
) {
225 if (swrast
->_RasterMask
== DEPTH_BIT
226 && ctx
->Depth
.Func
== GL_LESS
227 && ctx
->Depth
.Mask
== GL_TRUE
228 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
229 return flat_rgba_z_line
;
232 if (swrast
->_RasterMask
== 0) {
233 return flat_rgba_line
;
236 return (swrast_line_func
) NULL
;
240 /**********************************************************************/
241 /***** Optimized triangle rendering *****/
242 /**********************************************************************/
246 * Smooth-shaded, z-less triangle, RGBA color.
248 #define NAME smooth_rgba_z_triangle
250 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
252 #define INTERP_ALPHA 1
254 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
255 #define RENDER_SPAN( span ) { \
257 GLchan *img = PIXELADDR4(span.x, span.y); \
258 for (i = 0; i < span.end; i++, img += 4) { \
259 const GLuint z = FixedToDepth(span.z); \
261 PACK_RGBA(img, FixedToChan(span.red), \
262 FixedToChan(span.green), FixedToChan(span.blue), \
263 FixedToChan(span.alpha)); \
266 span.red += span.redStep; \
267 span.green += span.greenStep; \
268 span.blue += span.blueStep; \
269 span.alpha += span.alphaStep; \
270 span.z += span.zStep; \
273 #include "swrast/s_tritemp.h"
278 * Flat-shaded, z-less triangle, RGBA color.
280 #define NAME flat_rgba_z_triangle
282 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
284 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
286 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
287 v2->color[2], v2->color[3]);
289 #define RENDER_SPAN( span ) { \
291 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
292 for (i = 0; i < span.end; i++) { \
293 const GLuint z = FixedToDepth(span.z); \
298 span.z += span.zStep; \
302 #include "swrast/s_tritemp.h"
307 * Return pointer to an optimized triangle function if possible.
309 static swrast_tri_func
310 osmesa_choose_triangle_function( struct gl_context
*ctx
)
312 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
313 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
315 if (ctx
->DrawBuffer
&&
316 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
317 /* the special-case triangle functions in this file don't work
318 * for float color channels.
323 if (ctx
->RenderMode
!= GL_RENDER
||
324 ctx
->Polygon
.SmoothFlag
||
325 ctx
->Polygon
.StippleFlag
||
326 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1) {
330 if (osmesa
->format
!= OSMESA_RGBA
&&
331 osmesa
->format
!= OSMESA_BGRA
&&
332 osmesa
->format
!= OSMESA_ARGB
) {
336 if (ctx
->Polygon
.CullFlag
&&
337 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
341 if (swrast
->_RasterMask
== DEPTH_BIT
&&
342 ctx
->Depth
.Func
== GL_LESS
&&
343 ctx
->Depth
.Mask
== GL_TRUE
&&
344 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
345 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
346 return smooth_rgba_z_triangle
;
349 return flat_rgba_z_triangle
;
358 /* Override for the swrast triangle-selection function. Try to use one
359 * of our internal triangle functions, otherwise fall back to the
360 * standard swrast functions.
363 osmesa_choose_triangle( struct gl_context
*ctx
)
365 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
367 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
368 if (!swrast
->Triangle
)
369 _swrast_choose_triangle( ctx
);
373 osmesa_choose_line( struct gl_context
*ctx
)
375 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
377 swrast
->Line
= osmesa_choose_line_function( ctx
);
379 _swrast_choose_line( ctx
);
385 * Recompute the values of the context's rowaddr array.
388 compute_row_addresses( OSMesaContext osmesa
)
390 GLint bytesPerRow
, i
;
391 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
392 GLint rowlength
; /* in pixels */
393 GLint height
= osmesa
->srb
->Base
.Height
;
395 if (osmesa
->userRowLength
)
396 rowlength
= osmesa
->userRowLength
;
398 rowlength
= osmesa
->srb
->Base
.Width
;
400 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
403 /* Y=0 is bottom line of window */
404 for (i
= 0; i
< height
; i
++) {
405 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
409 /* Y=0 is top line of window */
410 for (i
= 0; i
< height
; i
++) {
411 GLint j
= height
- i
- 1;
412 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
420 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
423 osmesa_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
425 _mesa_delete_renderbuffer(ctx
, rb
);
430 * Allocate renderbuffer storage. We don't actually allocate any storage
431 * since we're using a user-provided buffer.
432 * Just set up all the gl_renderbuffer methods.
435 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
436 GLenum internalFormat
, GLuint width
, GLuint height
)
438 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
440 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
441 (void) internalFormat
;
443 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
445 * XXX There aren't Mesa formats for all the possible combinations here!
446 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
448 * XXX The 8-bit/channel formats should all be OK.
450 if (osmesa
->format
== OSMESA_RGBA
) {
451 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
452 if (_mesa_little_endian())
453 rb
->Format
= MESA_FORMAT_R8G8B8A8_UNORM
;
455 rb
->Format
= MESA_FORMAT_A8B8G8R8_UNORM
;
457 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
458 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
;
461 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
464 else if (osmesa
->format
== OSMESA_BGRA
) {
465 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
466 if (_mesa_little_endian())
467 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
469 rb
->Format
= MESA_FORMAT_A8R8G8B8_UNORM
;
471 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
472 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
473 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
476 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
477 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
480 else if (osmesa
->format
== OSMESA_ARGB
) {
481 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
482 if (_mesa_little_endian())
483 rb
->Format
= MESA_FORMAT_A8R8G8B8_UNORM
;
485 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
487 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
488 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
489 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
492 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
493 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
496 else if (osmesa
->format
== OSMESA_RGB
) {
497 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
498 rb
->Format
= MESA_FORMAT_BGR_UNORM8
;
500 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
501 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
502 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
505 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
506 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
509 else if (osmesa
->format
== OSMESA_BGR
) {
510 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
511 rb
->Format
= MESA_FORMAT_RGB_UNORM8
;
513 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
514 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
515 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
518 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
519 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
522 else if (osmesa
->format
== OSMESA_RGB_565
) {
523 assert(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
524 rb
->Format
= MESA_FORMAT_B5G6R5_UNORM
;
527 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
533 compute_row_addresses( osmesa
);
540 * Allocate a new renderbuffer to describe the user-provided color buffer.
542 static struct swrast_renderbuffer
*
543 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
545 const GLuint name
= 0;
546 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
549 _mesa_init_renderbuffer(&srb
->Base
, name
);
551 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
552 srb
->Base
.RefCount
= 1;
553 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
554 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
556 srb
->Base
.InternalFormat
= GL_RGBA
;
557 srb
->Base
._BaseFormat
= GL_RGBA
;
567 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
568 struct gl_renderbuffer
*rb
,
569 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
571 GLubyte
**mapOut
, GLint
*rowStrideOut
)
573 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
575 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
576 /* this is an OSMesa renderbuffer which wraps user memory */
577 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
578 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
579 GLint rowStride
; /* in bytes */
581 if (osmesa
->userRowLength
)
582 rowStride
= osmesa
->userRowLength
* bpp
;
584 rowStride
= rb
->Width
* bpp
;
587 /* Y=0 is top line of window */
588 y
= rb
->Height
- y
- 1;
589 *rowStrideOut
= -rowStride
;
592 *rowStrideOut
= rowStride
;
595 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
598 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
599 mapOut
, rowStrideOut
);
605 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
607 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
611 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
616 /**********************************************************************/
617 /***** Public Functions *****/
618 /**********************************************************************/
622 * Create an Off-Screen Mesa rendering context. The only attribute needed is
623 * an RGBA vs Color-Index mode flag.
625 * Input: format - Must be GL_RGBA
626 * sharelist - specifies another OSMesaContext with which to share
627 * display lists. NULL indicates no sharing.
628 * Return: an OSMesaContext or 0 if error
630 GLAPI OSMesaContext GLAPIENTRY
631 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
633 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
642 * Create context and specify size of ancillary buffers.
644 GLAPI OSMesaContext GLAPIENTRY
645 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
646 GLint accumBits
, OSMesaContext sharelist
)
648 int attribs
[100], n
= 0;
650 attribs
[n
++] = OSMESA_FORMAT
;
651 attribs
[n
++] = format
;
652 attribs
[n
++] = OSMESA_DEPTH_BITS
;
653 attribs
[n
++] = depthBits
;
654 attribs
[n
++] = OSMESA_STENCIL_BITS
;
655 attribs
[n
++] = stencilBits
;
656 attribs
[n
++] = OSMESA_ACCUM_BITS
;
657 attribs
[n
++] = accumBits
;
660 return OSMesaCreateContextAttribs(attribs
, sharelist
);
667 * Create context with attribute list.
669 GLAPI OSMesaContext GLAPIENTRY
670 OSMesaCreateContextAttribs(const int *attribList
, OSMesaContext sharelist
)
672 OSMesaContext osmesa
;
673 struct dd_function_table functions
;
674 GLint rind
, gind
, bind
, aind
;
675 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
676 GLenum format
= OSMESA_RGBA
;
677 GLint depthBits
= 0, stencilBits
= 0, accumBits
= 0;
678 int profile
= OSMESA_COMPAT_PROFILE
, version_major
= 1, version_minor
= 0;
679 gl_api api_profile
= API_OPENGL_COMPAT
;
682 for (i
= 0; attribList
[i
]; i
+= 2) {
683 switch (attribList
[i
]) {
685 format
= attribList
[i
+1];
687 case OSMESA_COLOR_INDEX
:
700 case OSMESA_DEPTH_BITS
:
701 depthBits
= attribList
[i
+1];
705 case OSMESA_STENCIL_BITS
:
706 stencilBits
= attribList
[i
+1];
710 case OSMESA_ACCUM_BITS
:
711 accumBits
= attribList
[i
+1];
716 profile
= attribList
[i
+1];
717 if (profile
== OSMESA_COMPAT_PROFILE
)
718 api_profile
= API_OPENGL_COMPAT
;
719 else if (profile
== OSMESA_CORE_PROFILE
)
720 api_profile
= API_OPENGL_CORE
;
724 case OSMESA_CONTEXT_MAJOR_VERSION
:
725 version_major
= attribList
[i
+1];
726 if (version_major
< 1)
729 case OSMESA_CONTEXT_MINOR_VERSION
:
730 version_minor
= attribList
[i
+1];
731 if (version_minor
< 0)
738 fprintf(stderr
, "Bad attribute in OSMesaCreateContextAttribs()\n");
743 rind
= gind
= bind
= aind
= 0;
744 if (format
==OSMESA_RGBA
) {
746 greenBits
= CHAN_BITS
;
747 blueBits
= CHAN_BITS
;
748 alphaBits
= CHAN_BITS
;
754 else if (format
==OSMESA_BGRA
) {
756 greenBits
= CHAN_BITS
;
757 blueBits
= CHAN_BITS
;
758 alphaBits
= CHAN_BITS
;
764 else if (format
==OSMESA_ARGB
) {
766 greenBits
= CHAN_BITS
;
767 blueBits
= CHAN_BITS
;
768 alphaBits
= CHAN_BITS
;
774 else if (format
==OSMESA_RGB
) {
776 greenBits
= CHAN_BITS
;
777 blueBits
= CHAN_BITS
;
783 else if (format
==OSMESA_BGR
) {
785 greenBits
= CHAN_BITS
;
786 blueBits
= CHAN_BITS
;
792 #if CHAN_TYPE == GL_UNSIGNED_BYTE
793 else if (format
==OSMESA_RGB_565
) {
798 rind
= 0; /* not used */
807 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
809 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
810 GL_FALSE
, /* stereo */
820 alphaBits
? accumBits
: 0,
823 if (!osmesa
->gl_visual
) {
828 /* Initialize device driver function table */
829 _mesa_init_driver_functions(&functions
);
830 /* override with our functions */
831 functions
.GetString
= get_string
;
832 functions
.UpdateState
= osmesa_update_state
;
834 if (!_mesa_initialize_context(&osmesa
->mesa
,
837 sharelist
? &sharelist
->mesa
838 : (struct gl_context
*) NULL
,
840 _mesa_destroy_visual( osmesa
->gl_visual
);
845 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
847 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
848 if (!osmesa
->gl_buffer
) {
849 _mesa_destroy_visual( osmesa
->gl_visual
);
850 _mesa_free_context_data( &osmesa
->mesa
);
855 /* Create depth/stencil/accum buffers. We'll create the color
856 * buffer later in OSMesaMakeCurrent().
858 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
859 GL_FALSE
, /* color */
860 osmesa
->gl_visual
->haveDepthBuffer
,
861 osmesa
->gl_visual
->haveStencilBuffer
,
862 osmesa
->gl_visual
->haveAccumBuffer
,
863 GL_FALSE
, /* alpha */
864 GL_FALSE
/* aux */ );
866 osmesa
->format
= format
;
867 osmesa
->userRowLength
= 0;
868 osmesa
->yup
= GL_TRUE
;
874 _mesa_meta_init(&osmesa
->mesa
);
876 /* Initialize the software rasterizer and helper modules. */
878 struct gl_context
*ctx
= &osmesa
->mesa
;
882 if (!_swrast_CreateContext( ctx
) ||
883 !_vbo_CreateContext( ctx
) ||
884 !_tnl_CreateContext( ctx
) ||
885 !_swsetup_CreateContext( ctx
)) {
886 _mesa_destroy_visual(osmesa
->gl_visual
);
887 _mesa_free_context_data(ctx
);
892 _swsetup_Wakeup( ctx
);
894 /* use default TCL pipeline */
895 tnl
= TNL_CONTEXT(ctx
);
896 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
898 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
899 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
901 ctx
->Driver
.GenerateMipmap
= _mesa_generate_mipmap
;
903 /* Extend the software rasterizer with our optimized line and triangle
906 swrast
= SWRAST_CONTEXT( ctx
);
907 swrast
->choose_line
= osmesa_choose_line
;
908 swrast
->choose_triangle
= osmesa_choose_triangle
;
910 _mesa_compute_version(ctx
);
912 if (ctx
->Version
< version_major
* 10 + version_minor
) {
913 _mesa_destroy_visual(osmesa
->gl_visual
);
914 _mesa_free_context_data(ctx
);
919 /* Exec table initialization requires the version to be computed */
920 _mesa_initialize_dispatch_tables(ctx
);
921 _mesa_initialize_vbo_vtxfmt(ctx
);
929 * Destroy an Off-Screen Mesa rendering context.
931 * \param osmesa the context to destroy
933 GLAPI
void GLAPIENTRY
934 OSMesaDestroyContext( OSMesaContext osmesa
)
938 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
940 _mesa_meta_free( &osmesa
->mesa
);
942 _swsetup_DestroyContext( &osmesa
->mesa
);
943 _tnl_DestroyContext( &osmesa
->mesa
);
944 _vbo_DestroyContext( &osmesa
->mesa
);
945 _swrast_DestroyContext( &osmesa
->mesa
);
947 _mesa_destroy_visual( osmesa
->gl_visual
);
948 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
950 _mesa_free_context_data( &osmesa
->mesa
);
957 * Bind an OSMesaContext to an image buffer. The image buffer is just a
958 * block of memory which the client provides. Its size must be at least
959 * as large as width*height*sizeof(type). Its address should be a multiple
960 * of 4 if using RGBA mode.
962 * Image data is stored in the order of glDrawPixels: row-major order
963 * with the lower-left image pixel stored in the first array position
964 * (ie. bottom-to-top).
966 * If the context's viewport hasn't been initialized yet, it will now be
967 * initialized to (0,0,width,height).
969 * Input: osmesa - the rendering context
970 * buffer - the image buffer memory
971 * type - data type for pixel components
972 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
973 * are supported. But if Mesa's been compiled with CHAN_BITS==16
974 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
975 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
976 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
977 * width, height - size of image buffer in pixels, at least 1
978 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
979 * invalid buffer address, invalid type, width<1, height<1,
980 * width>internal limit or height>internal limit.
982 GLAPI GLboolean GLAPIENTRY
983 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
984 GLsizei width
, GLsizei height
)
986 if (!osmesa
|| !buffer
||
987 width
< 1 || height
< 1 ||
988 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
992 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
997 if (!(type
== GL_UNSIGNED_BYTE
||
998 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
999 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1000 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1005 osmesa_update_state( &osmesa
->mesa
, 0 );
1007 /* Call this periodically to detect when the user has begun using
1008 * GL rendering from multiple threads.
1010 _glapi_check_multithread();
1013 /* Create a front/left color buffer which wraps the user-provided buffer.
1014 * There is no back color buffer.
1015 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1016 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1017 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
1018 * that converts rendering from CHAN_BITS to the user-requested channel
1022 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
1023 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1024 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
1025 &osmesa
->srb
->Base
);
1026 assert(osmesa
->srb
->Base
.RefCount
== 2);
1029 osmesa
->DataType
= type
;
1031 /* Set renderbuffer fields. Set width/height = 0 to force
1032 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1034 osmesa
->srb
->Buffer
= buffer
;
1035 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
1037 /* Set the framebuffer's size. This causes the
1038 * osmesa_renderbuffer_storage() function to get called.
1040 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1042 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1044 /* Remove renderbuffer attachment, then re-add. This installs the
1045 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1047 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1048 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
1049 &osmesa
->srb
->Base
);
1052 /* this updates the visual's red/green/blue/alphaBits fields */
1053 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
1055 /* update the framebuffer size */
1056 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1063 GLAPI OSMesaContext GLAPIENTRY
1064 OSMesaGetCurrentContext( void )
1066 struct gl_context
*ctx
= _mesa_get_current_context();
1068 return (OSMesaContext
) ctx
;
1075 GLAPI
void GLAPIENTRY
1076 OSMesaPixelStore( GLint pname
, GLint value
)
1078 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1081 case OSMESA_ROW_LENGTH
:
1083 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1084 "OSMesaPixelStore(value)" );
1087 osmesa
->userRowLength
= value
;
1090 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1093 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1097 compute_row_addresses( osmesa
);
1101 GLAPI
void GLAPIENTRY
1102 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1104 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1108 if (osmesa
->gl_buffer
)
1109 *value
= osmesa
->gl_buffer
->Width
;
1114 if (osmesa
->gl_buffer
)
1115 *value
= osmesa
->gl_buffer
->Height
;
1120 *value
= osmesa
->format
;
1123 /* current color buffer's data type */
1124 *value
= osmesa
->DataType
;
1126 case OSMESA_ROW_LENGTH
:
1127 *value
= osmesa
->userRowLength
;
1130 *value
= osmesa
->yup
;
1132 case OSMESA_MAX_WIDTH
:
1133 *value
= SWRAST_MAX_WIDTH
;
1135 case OSMESA_MAX_HEIGHT
:
1136 *value
= SWRAST_MAX_HEIGHT
;
1139 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1146 * Return the depth buffer associated with an OSMesa context.
1147 * Input: c - the OSMesa context
1148 * Output: width, height - size of buffer in pixels
1149 * bytesPerValue - bytes per depth value (2 or 4)
1150 * buffer - pointer to depth buffer values
1151 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1153 GLAPI GLboolean GLAPIENTRY
1154 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1155 GLint
*bytesPerValue
, void **buffer
)
1157 struct swrast_renderbuffer
*srb
= NULL
;
1160 srb
= swrast_renderbuffer(c
->gl_buffer
->
1161 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1163 if (!srb
|| !srb
->Buffer
) {
1171 *width
= srb
->Base
.Width
;
1172 *height
= srb
->Base
.Height
;
1173 if (c
->gl_visual
->depthBits
<= 16)
1174 *bytesPerValue
= sizeof(GLushort
);
1176 *bytesPerValue
= sizeof(GLuint
);
1177 *buffer
= (void *) srb
->Buffer
;
1184 * Return the color buffer associated with an OSMesa context.
1185 * Input: c - the OSMesa context
1186 * Output: width, height - size of buffer in pixels
1187 * format - the pixel format (OSMESA_FORMAT)
1188 * buffer - pointer to color buffer values
1189 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1191 GLAPI GLboolean GLAPIENTRY
1192 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1193 GLint
*height
, GLint
*format
, void **buffer
)
1195 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1196 *width
= osmesa
->srb
->Base
.Width
;
1197 *height
= osmesa
->srb
->Base
.Height
;
1198 *format
= osmesa
->format
;
1199 *buffer
= (void *) osmesa
->srb
->Buffer
;
1212 struct name_function
1215 OSMESAproc Function
;
1218 static struct name_function functions
[] = {
1219 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1220 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1221 { "OSMesaCreateContextAttribs", (OSMESAproc
) OSMesaCreateContextAttribs
},
1222 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1223 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1224 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1225 { "OSMesaPixelStore", (OSMESAproc
) OSMesaPixelStore
},
1226 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1227 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1228 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1229 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1230 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1231 { "OSMesaPostprocess", (OSMESAproc
) OSMesaPostprocess
},
1236 GLAPI OSMESAproc GLAPIENTRY
1237 OSMesaGetProcAddress( const char *funcName
)
1240 for (i
= 0; functions
[i
].Name
; i
++) {
1241 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1242 return functions
[i
].Function
;
1244 return _glapi_get_proc_address(funcName
);
1248 GLAPI
void GLAPIENTRY
1249 OSMesaColorClamp(GLboolean enable
)
1251 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1253 if (enable
== GL_TRUE
) {
1254 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1257 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1262 GLAPI
void GLAPIENTRY
1263 OSMesaPostprocess(OSMesaContext osmesa
, const char *filter
,
1264 unsigned enable_value
)
1267 "OSMesaPostProcess() is only available with gallium drivers\n");
1273 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1274 * libglapi.a. We need to define them here.
1276 #ifdef GLX_INDIRECT_RENDERING
1278 #define GL_GLEXT_PROTOTYPES
1280 #include "glapi/glapi.h"
1281 #include "glapi/glapitable.h"
1283 #if defined(USE_MGL_NAMESPACE)
1284 #define NAME(func) mgl##func
1286 #define NAME(func) gl##func
1289 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1290 GET_DISPATCH()->FUNC ARGS
1292 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1293 return GET_DISPATCH()->FUNC ARGS
1295 /* skip normal ones */
1296 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1297 #include "glapi/glapitemp.h"
1299 #endif /* GLX_INDIRECT_RENDERING */