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 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
123 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
125 /* easy - just propogate */
126 _swrast_InvalidateState( ctx
, new_state
);
127 _swsetup_InvalidateState( ctx
, new_state
);
128 _tnl_InvalidateState( ctx
, new_state
);
132 osmesa_update_state_wrapper(struct gl_context
*ctx
)
134 osmesa_update_state(ctx
, ctx
->NewState
);
139 * Macros for optimized line/triangle rendering.
140 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
143 #define PACK_RGBA(DST, R, G, B, A) \
145 (DST)[osmesa->rInd] = R; \
146 (DST)[osmesa->gInd] = G; \
147 (DST)[osmesa->bInd] = B; \
148 (DST)[osmesa->aInd] = A; \
151 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
155 * Draw a flat-shaded, RGB line into an osmesa buffer.
157 #define NAME flat_rgba_line
160 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
161 const GLchan *color = vert1->color;
165 GLchan *p = PIXELADDR4(X, Y); \
166 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
169 #include "swrast/s_linetemp.h"
174 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
176 #define NAME flat_rgba_z_line
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
181 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
182 const GLchan *color = vert1->color;
187 GLchan *p = PIXELADDR4(X, Y); \
188 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
189 color[BCOMP], color[ACOMP]); \
194 #include "swrast/s_linetemp.h"
199 * Analyze context state to see if we can provide a fast line drawing
200 * function. Otherwise, return NULL.
202 static swrast_line_func
203 osmesa_choose_line_function( struct gl_context
*ctx
)
205 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
206 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
208 if (ctx
->DrawBuffer
&&
209 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
210 /* the special-case line functions in this file don't work
211 * for float color channels.
216 if (ctx
->RenderMode
!= GL_RENDER
||
217 ctx
->Texture
._MaxEnabledTexImageUnit
== -1 ||
218 ctx
->Light
.ShadeModel
!= GL_FLAT
||
219 ctx
->Line
.Width
!= 1.0F
||
220 ctx
->Line
.StippleFlag
||
221 ctx
->Line
.SmoothFlag
) {
225 if (osmesa
->format
!= OSMESA_RGBA
&&
226 osmesa
->format
!= OSMESA_BGRA
&&
227 osmesa
->format
!= OSMESA_ARGB
) {
231 if (swrast
->_RasterMask
== DEPTH_BIT
232 && ctx
->Depth
.Func
== GL_LESS
233 && ctx
->Depth
.Mask
== GL_TRUE
234 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
235 return flat_rgba_z_line
;
238 if (swrast
->_RasterMask
== 0) {
239 return flat_rgba_line
;
242 return (swrast_line_func
) NULL
;
246 /**********************************************************************/
247 /***** Optimized triangle rendering *****/
248 /**********************************************************************/
252 * Smooth-shaded, z-less triangle, RGBA color.
254 #define NAME smooth_rgba_z_triangle
256 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
258 #define INTERP_ALPHA 1
260 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
261 #define RENDER_SPAN( span ) { \
263 GLchan *img = PIXELADDR4(span.x, span.y); \
264 for (i = 0; i < span.end; i++, img += 4) { \
265 const GLuint z = FixedToDepth(span.z); \
267 PACK_RGBA(img, FixedToChan(span.red), \
268 FixedToChan(span.green), FixedToChan(span.blue), \
269 FixedToChan(span.alpha)); \
272 span.red += span.redStep; \
273 span.green += span.greenStep; \
274 span.blue += span.blueStep; \
275 span.alpha += span.alphaStep; \
276 span.z += span.zStep; \
279 #include "swrast/s_tritemp.h"
284 * Flat-shaded, z-less triangle, RGBA color.
286 #define NAME flat_rgba_z_triangle
288 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
290 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
292 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
293 v2->color[2], v2->color[3]);
295 #define RENDER_SPAN( span ) { \
297 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
298 for (i = 0; i < span.end; i++) { \
299 const GLuint z = FixedToDepth(span.z); \
304 span.z += span.zStep; \
308 #include "swrast/s_tritemp.h"
313 * Return pointer to an optimized triangle function if possible.
315 static swrast_tri_func
316 osmesa_choose_triangle_function( struct gl_context
*ctx
)
318 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
319 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
321 if (ctx
->DrawBuffer
&&
322 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
323 /* the special-case triangle functions in this file don't work
324 * for float color channels.
329 if (ctx
->RenderMode
!= GL_RENDER
||
330 ctx
->Polygon
.SmoothFlag
||
331 ctx
->Polygon
.StippleFlag
||
332 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1) {
336 if (osmesa
->format
!= OSMESA_RGBA
&&
337 osmesa
->format
!= OSMESA_BGRA
&&
338 osmesa
->format
!= OSMESA_ARGB
) {
342 if (ctx
->Polygon
.CullFlag
&&
343 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
347 if (swrast
->_RasterMask
== DEPTH_BIT
&&
348 ctx
->Depth
.Func
== GL_LESS
&&
349 ctx
->Depth
.Mask
== GL_TRUE
&&
350 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
351 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
352 return smooth_rgba_z_triangle
;
355 return flat_rgba_z_triangle
;
364 /* Override for the swrast triangle-selection function. Try to use one
365 * of our internal triangle functions, otherwise fall back to the
366 * standard swrast functions.
369 osmesa_choose_triangle( struct gl_context
*ctx
)
371 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
373 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
374 if (!swrast
->Triangle
)
375 _swrast_choose_triangle( ctx
);
379 osmesa_choose_line( struct gl_context
*ctx
)
381 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
383 swrast
->Line
= osmesa_choose_line_function( ctx
);
385 _swrast_choose_line( ctx
);
391 * Recompute the values of the context's rowaddr array.
394 compute_row_addresses( OSMesaContext osmesa
)
396 GLint bytesPerRow
, i
;
397 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
398 GLint rowlength
; /* in pixels */
399 GLint height
= osmesa
->srb
->Base
.Height
;
401 if (osmesa
->userRowLength
)
402 rowlength
= osmesa
->userRowLength
;
404 rowlength
= osmesa
->srb
->Base
.Width
;
406 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
409 /* Y=0 is bottom line of window */
410 for (i
= 0; i
< height
; i
++) {
411 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
415 /* Y=0 is top line of window */
416 for (i
= 0; i
< height
; i
++) {
417 GLint j
= height
- i
- 1;
418 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
426 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
429 osmesa_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
431 _mesa_delete_renderbuffer(ctx
, rb
);
436 * Allocate renderbuffer storage. We don't actually allocate any storage
437 * since we're using a user-provided buffer.
438 * Just set up all the gl_renderbuffer methods.
441 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
442 GLenum internalFormat
, GLuint width
, GLuint height
)
444 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
446 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
447 (void) internalFormat
;
449 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
451 * XXX There aren't Mesa formats for all the possible combinations here!
452 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
454 * XXX The 8-bit/channel formats should all be OK.
456 if (osmesa
->format
== OSMESA_RGBA
) {
457 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
458 if (_mesa_little_endian())
459 rb
->Format
= MESA_FORMAT_R8G8B8A8_UNORM
;
461 rb
->Format
= MESA_FORMAT_A8B8G8R8_UNORM
;
463 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
464 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
;
467 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
470 else if (osmesa
->format
== OSMESA_BGRA
) {
471 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
472 if (_mesa_little_endian())
473 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
475 rb
->Format
= MESA_FORMAT_A8R8G8B8_UNORM
;
477 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
478 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
479 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
482 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
483 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
486 else if (osmesa
->format
== OSMESA_ARGB
) {
487 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
488 if (_mesa_little_endian())
489 rb
->Format
= MESA_FORMAT_A8R8G8B8_UNORM
;
491 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
493 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
494 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
495 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
498 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
499 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
502 else if (osmesa
->format
== OSMESA_RGB
) {
503 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
504 rb
->Format
= MESA_FORMAT_BGR_UNORM8
;
506 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
507 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
508 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
511 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
512 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
515 else if (osmesa
->format
== OSMESA_BGR
) {
516 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
517 rb
->Format
= MESA_FORMAT_RGB_UNORM8
;
519 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
520 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
521 rb
->Format
= MESA_FORMAT_RGBA_UNORM16
; /* not exactly right */
524 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
525 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
528 else if (osmesa
->format
== OSMESA_RGB_565
) {
529 assert(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
530 rb
->Format
= MESA_FORMAT_B5G6R5_UNORM
;
533 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
539 compute_row_addresses( osmesa
);
546 * Allocate a new renderbuffer to describe the user-provided color buffer.
548 static struct swrast_renderbuffer
*
549 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
551 const GLuint name
= 0;
552 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
555 _mesa_init_renderbuffer(&srb
->Base
, name
);
557 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
558 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
559 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
561 srb
->Base
.InternalFormat
= GL_RGBA
;
562 srb
->Base
._BaseFormat
= GL_RGBA
;
572 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
573 struct gl_renderbuffer
*rb
,
574 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
576 GLubyte
**mapOut
, GLint
*rowStrideOut
)
578 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
580 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
581 /* this is an OSMesa renderbuffer which wraps user memory */
582 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
583 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
584 GLint rowStride
; /* in bytes */
586 if (osmesa
->userRowLength
)
587 rowStride
= osmesa
->userRowLength
* bpp
;
589 rowStride
= rb
->Width
* bpp
;
592 /* Y=0 is top line of window */
593 y
= rb
->Height
- y
- 1;
594 *rowStrideOut
= -rowStride
;
597 *rowStrideOut
= rowStride
;
600 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
603 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
604 mapOut
, rowStrideOut
);
610 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
612 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
616 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
621 /**********************************************************************/
622 /***** Public Functions *****/
623 /**********************************************************************/
627 * Create an Off-Screen Mesa rendering context. The only attribute needed is
628 * an RGBA vs Color-Index mode flag.
630 * Input: format - Must be GL_RGBA
631 * sharelist - specifies another OSMesaContext with which to share
632 * display lists. NULL indicates no sharing.
633 * Return: an OSMesaContext or 0 if error
635 GLAPI OSMesaContext GLAPIENTRY
636 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
638 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
647 * Create context and specify size of ancillary buffers.
649 GLAPI OSMesaContext GLAPIENTRY
650 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
651 GLint accumBits
, OSMesaContext sharelist
)
653 int attribs
[100], n
= 0;
655 attribs
[n
++] = OSMESA_FORMAT
;
656 attribs
[n
++] = format
;
657 attribs
[n
++] = OSMESA_DEPTH_BITS
;
658 attribs
[n
++] = depthBits
;
659 attribs
[n
++] = OSMESA_STENCIL_BITS
;
660 attribs
[n
++] = stencilBits
;
661 attribs
[n
++] = OSMESA_ACCUM_BITS
;
662 attribs
[n
++] = accumBits
;
665 return OSMesaCreateContextAttribs(attribs
, sharelist
);
672 * Create context with attribute list.
674 GLAPI OSMesaContext GLAPIENTRY
675 OSMesaCreateContextAttribs(const int *attribList
, OSMesaContext sharelist
)
677 OSMesaContext osmesa
;
678 struct dd_function_table functions
;
679 GLint rind
, gind
, bind
, aind
;
680 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
681 GLenum format
= OSMESA_RGBA
;
682 GLint depthBits
= 0, stencilBits
= 0, accumBits
= 0;
683 int profile
= OSMESA_COMPAT_PROFILE
, version_major
= 1, version_minor
= 0;
684 gl_api api_profile
= API_OPENGL_COMPAT
;
687 for (i
= 0; attribList
[i
]; i
+= 2) {
688 switch (attribList
[i
]) {
690 format
= attribList
[i
+1];
692 case OSMESA_COLOR_INDEX
:
705 case OSMESA_DEPTH_BITS
:
706 depthBits
= attribList
[i
+1];
710 case OSMESA_STENCIL_BITS
:
711 stencilBits
= attribList
[i
+1];
715 case OSMESA_ACCUM_BITS
:
716 accumBits
= attribList
[i
+1];
721 profile
= attribList
[i
+1];
722 if (profile
== OSMESA_COMPAT_PROFILE
)
723 api_profile
= API_OPENGL_COMPAT
;
724 else if (profile
== OSMESA_CORE_PROFILE
)
725 api_profile
= API_OPENGL_CORE
;
729 case OSMESA_CONTEXT_MAJOR_VERSION
:
730 version_major
= attribList
[i
+1];
731 if (version_major
< 1)
734 case OSMESA_CONTEXT_MINOR_VERSION
:
735 version_minor
= attribList
[i
+1];
736 if (version_minor
< 0)
743 fprintf(stderr
, "Bad attribute in OSMesaCreateContextAttribs()\n");
748 rind
= gind
= bind
= aind
= 0;
749 if (format
==OSMESA_RGBA
) {
751 greenBits
= CHAN_BITS
;
752 blueBits
= CHAN_BITS
;
753 alphaBits
= CHAN_BITS
;
759 else if (format
==OSMESA_BGRA
) {
761 greenBits
= CHAN_BITS
;
762 blueBits
= CHAN_BITS
;
763 alphaBits
= CHAN_BITS
;
769 else if (format
==OSMESA_ARGB
) {
771 greenBits
= CHAN_BITS
;
772 blueBits
= CHAN_BITS
;
773 alphaBits
= CHAN_BITS
;
779 else if (format
==OSMESA_RGB
) {
781 greenBits
= CHAN_BITS
;
782 blueBits
= CHAN_BITS
;
788 else if (format
==OSMESA_BGR
) {
790 greenBits
= CHAN_BITS
;
791 blueBits
= CHAN_BITS
;
797 #if CHAN_TYPE == GL_UNSIGNED_BYTE
798 else if (format
==OSMESA_RGB_565
) {
803 rind
= 0; /* not used */
812 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
814 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
815 GL_FALSE
, /* stereo */
825 alphaBits
? accumBits
: 0,
828 if (!osmesa
->gl_visual
) {
833 /* Initialize device driver function table */
834 _mesa_init_driver_functions(&functions
);
835 /* override with our functions */
836 functions
.GetString
= get_string
;
837 functions
.UpdateState
= osmesa_update_state_wrapper
;
839 if (!_mesa_initialize_context(&osmesa
->mesa
,
842 sharelist
? &sharelist
->mesa
843 : (struct gl_context
*) NULL
,
845 _mesa_destroy_visual( osmesa
->gl_visual
);
850 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
852 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
853 if (!osmesa
->gl_buffer
) {
854 _mesa_destroy_visual( osmesa
->gl_visual
);
855 _mesa_free_context_data( &osmesa
->mesa
);
860 /* Create depth/stencil/accum buffers. We'll create the color
861 * buffer later in OSMesaMakeCurrent().
863 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
864 GL_FALSE
, /* color */
865 osmesa
->gl_visual
->haveDepthBuffer
,
866 osmesa
->gl_visual
->haveStencilBuffer
,
867 osmesa
->gl_visual
->haveAccumBuffer
,
868 GL_FALSE
, /* alpha */
869 GL_FALSE
/* aux */ );
871 osmesa
->format
= format
;
872 osmesa
->userRowLength
= 0;
873 osmesa
->yup
= GL_TRUE
;
879 _mesa_meta_init(&osmesa
->mesa
);
881 /* Initialize the software rasterizer and helper modules. */
883 struct gl_context
*ctx
= &osmesa
->mesa
;
887 if (!_swrast_CreateContext( ctx
) ||
888 !_vbo_CreateContext( ctx
) ||
889 !_tnl_CreateContext( ctx
) ||
890 !_swsetup_CreateContext( ctx
)) {
891 _mesa_destroy_visual(osmesa
->gl_visual
);
892 _mesa_free_context_data(ctx
);
897 _swsetup_Wakeup( ctx
);
899 /* use default TCL pipeline */
900 tnl
= TNL_CONTEXT(ctx
);
901 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
903 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
904 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
906 ctx
->Driver
.GenerateMipmap
= _mesa_generate_mipmap
;
908 /* Extend the software rasterizer with our optimized line and triangle
911 swrast
= SWRAST_CONTEXT( ctx
);
912 swrast
->choose_line
= osmesa_choose_line
;
913 swrast
->choose_triangle
= osmesa_choose_triangle
;
915 _mesa_override_extensions(ctx
);
916 _mesa_compute_version(ctx
);
918 if (ctx
->Version
< version_major
* 10 + version_minor
) {
919 _mesa_destroy_visual(osmesa
->gl_visual
);
920 _mesa_free_context_data(ctx
);
925 /* Exec table initialization requires the version to be computed */
926 _mesa_initialize_dispatch_tables(ctx
);
927 _mesa_initialize_vbo_vtxfmt(ctx
);
935 * Destroy an Off-Screen Mesa rendering context.
937 * \param osmesa the context to destroy
939 GLAPI
void GLAPIENTRY
940 OSMesaDestroyContext( OSMesaContext osmesa
)
944 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
946 _mesa_meta_free( &osmesa
->mesa
);
948 _swsetup_DestroyContext( &osmesa
->mesa
);
949 _tnl_DestroyContext( &osmesa
->mesa
);
950 _vbo_DestroyContext( &osmesa
->mesa
);
951 _swrast_DestroyContext( &osmesa
->mesa
);
953 _mesa_destroy_visual( osmesa
->gl_visual
);
954 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
956 _mesa_free_context_data( &osmesa
->mesa
);
963 * Bind an OSMesaContext to an image buffer. The image buffer is just a
964 * block of memory which the client provides. Its size must be at least
965 * as large as width*height*sizeof(type). Its address should be a multiple
966 * of 4 if using RGBA mode.
968 * Image data is stored in the order of glDrawPixels: row-major order
969 * with the lower-left image pixel stored in the first array position
970 * (ie. bottom-to-top).
972 * If the context's viewport hasn't been initialized yet, it will now be
973 * initialized to (0,0,width,height).
975 * If both the context and the buffer are null, the current context will be
978 * Input: osmesa - the rendering context
979 * buffer - the image buffer memory
980 * type - data type for pixel components
981 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
982 * are supported. But if Mesa's been compiled with CHAN_BITS==16
983 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
984 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
985 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
986 * width, height - size of image buffer in pixels, at least 1
987 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
988 * invalid buffer address, invalid type, width<1, height<1,
989 * width>internal limit or height>internal limit.
991 GLAPI GLboolean GLAPIENTRY
992 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
993 GLsizei width
, GLsizei height
)
995 if (!osmesa
&& !buffer
) {
996 return _mesa_make_current(NULL
, NULL
, NULL
);
999 if (!osmesa
|| !buffer
||
1000 width
< 1 || height
< 1 ||
1001 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
1005 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
1010 if (!(type
== GL_UNSIGNED_BYTE
||
1011 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
1012 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1013 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1018 osmesa_update_state( &osmesa
->mesa
, 0 );
1020 /* Call this periodically to detect when the user has begun using
1021 * GL rendering from multiple threads.
1023 _glapi_check_multithread();
1026 /* Create a front/left color buffer which wraps the user-provided buffer.
1027 * There is no back color buffer.
1028 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1029 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1030 * _mesa_attach_and_reference_rb() function will create a "wrapper"
1031 * renderbuffer that converts rendering from CHAN_BITS to the
1032 * user-requested channel size.
1035 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
1036 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1037 _mesa_attach_and_reference_rb(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
1038 &osmesa
->srb
->Base
);
1039 assert(osmesa
->srb
->Base
.RefCount
== 2);
1042 osmesa
->DataType
= type
;
1044 /* Set renderbuffer fields. Set width/height = 0 to force
1045 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1047 osmesa
->srb
->Buffer
= buffer
;
1048 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
1050 /* Set the framebuffer's size. This causes the
1051 * osmesa_renderbuffer_storage() function to get called.
1053 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1055 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1057 /* Remove renderbuffer attachment, then re-add. This installs the
1058 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1060 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1061 _mesa_attach_and_reference_rb(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
1062 &osmesa
->srb
->Base
);
1065 /* this updates the visual's red/green/blue/alphaBits fields */
1066 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
1068 /* update the framebuffer size */
1069 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1076 GLAPI OSMesaContext GLAPIENTRY
1077 OSMesaGetCurrentContext( void )
1079 struct gl_context
*ctx
= _mesa_get_current_context();
1081 return (OSMesaContext
) ctx
;
1088 GLAPI
void GLAPIENTRY
1089 OSMesaPixelStore( GLint pname
, GLint value
)
1091 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1094 case OSMESA_ROW_LENGTH
:
1096 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1097 "OSMesaPixelStore(value)" );
1100 osmesa
->userRowLength
= value
;
1103 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1106 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1110 compute_row_addresses( osmesa
);
1114 GLAPI
void GLAPIENTRY
1115 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1117 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1121 if (osmesa
->gl_buffer
)
1122 *value
= osmesa
->gl_buffer
->Width
;
1127 if (osmesa
->gl_buffer
)
1128 *value
= osmesa
->gl_buffer
->Height
;
1133 *value
= osmesa
->format
;
1136 /* current color buffer's data type */
1137 *value
= osmesa
->DataType
;
1139 case OSMESA_ROW_LENGTH
:
1140 *value
= osmesa
->userRowLength
;
1143 *value
= osmesa
->yup
;
1145 case OSMESA_MAX_WIDTH
:
1146 *value
= SWRAST_MAX_WIDTH
;
1148 case OSMESA_MAX_HEIGHT
:
1149 *value
= SWRAST_MAX_HEIGHT
;
1152 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1159 * Return the depth buffer associated with an OSMesa context.
1160 * Input: c - the OSMesa context
1161 * Output: width, height - size of buffer in pixels
1162 * bytesPerValue - bytes per depth value (2 or 4)
1163 * buffer - pointer to depth buffer values
1164 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1166 GLAPI GLboolean GLAPIENTRY
1167 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1168 GLint
*bytesPerValue
, void **buffer
)
1170 struct swrast_renderbuffer
*srb
= NULL
;
1173 srb
= swrast_renderbuffer(c
->gl_buffer
->
1174 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1176 if (!srb
|| !srb
->Buffer
) {
1184 *width
= srb
->Base
.Width
;
1185 *height
= srb
->Base
.Height
;
1186 if (c
->gl_visual
->depthBits
<= 16)
1187 *bytesPerValue
= sizeof(GLushort
);
1189 *bytesPerValue
= sizeof(GLuint
);
1190 *buffer
= (void *) srb
->Buffer
;
1197 * Return the color buffer associated with an OSMesa context.
1198 * Input: c - the OSMesa context
1199 * Output: width, height - size of buffer in pixels
1200 * format - the pixel format (OSMESA_FORMAT)
1201 * buffer - pointer to color buffer values
1202 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1204 GLAPI GLboolean GLAPIENTRY
1205 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1206 GLint
*height
, GLint
*format
, void **buffer
)
1208 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1209 *width
= osmesa
->srb
->Base
.Width
;
1210 *height
= osmesa
->srb
->Base
.Height
;
1211 *format
= osmesa
->format
;
1212 *buffer
= (void *) osmesa
->srb
->Buffer
;
1225 struct name_function
1228 OSMESAproc Function
;
1231 static struct name_function functions
[] = {
1232 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1233 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1234 { "OSMesaCreateContextAttribs", (OSMESAproc
) OSMesaCreateContextAttribs
},
1235 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1236 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1237 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1238 { "OSMesaPixelStore", (OSMESAproc
) OSMesaPixelStore
},
1239 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1240 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1241 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1242 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1243 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1244 { "OSMesaPostprocess", (OSMESAproc
) OSMesaPostprocess
},
1249 GLAPI OSMESAproc GLAPIENTRY
1250 OSMesaGetProcAddress( const char *funcName
)
1253 for (i
= 0; functions
[i
].Name
; i
++) {
1254 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1255 return functions
[i
].Function
;
1257 return _glapi_get_proc_address(funcName
);
1261 GLAPI
void GLAPIENTRY
1262 OSMesaColorClamp(GLboolean enable
)
1264 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1266 if (enable
== GL_TRUE
) {
1267 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1270 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1275 GLAPI
void GLAPIENTRY
1276 OSMesaPostprocess(OSMesaContext osmesa
, const char *filter
,
1277 unsigned enable_value
)
1280 "OSMesaPostProcess() is only available with gallium drivers\n");
1286 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1287 * libglapi.a. We need to define them here.
1289 #ifdef GLX_INDIRECT_RENDERING
1291 #define GL_GLEXT_PROTOTYPES
1293 #include "glapi/glapi.h"
1294 #include "glapitable.h"
1296 #if defined(USE_MGL_NAMESPACE)
1297 #define NAME(func) mgl##func
1299 #define NAME(func) gl##func
1302 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1303 GET_DISPATCH()->FUNC ARGS
1305 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1306 return GET_DISPATCH()->FUNC ARGS
1308 /* skip normal ones */
1309 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1310 #include "glapitemp.h"
1312 #endif /* GLX_INDIRECT_RENDERING */