2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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.
36 #include "main/glheader.h"
37 #include "GL/osmesa.h"
38 #include "main/context.h"
39 #include "main/extensions.h"
40 #include "main/formats.h"
41 #include "main/framebuffer.h"
42 #include "main/imports.h"
43 #include "main/macros.h"
44 #include "main/mtypes.h"
45 #include "main/renderbuffer.h"
46 #include "swrast/swrast.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "swrast/s_context.h"
49 #include "swrast/s_lines.h"
50 #include "swrast/s_renderbuffer.h"
51 #include "swrast/s_triangle.h"
53 #include "tnl/t_context.h"
54 #include "tnl/t_pipeline.h"
55 #include "drivers/common/driverfuncs.h"
56 #include "drivers/common/meta.h"
60 #define OSMESA_RENDERBUFFER_CLASS 0x053
64 * OSMesa rendering context, derived from core Mesa struct gl_context.
68 struct gl_context mesa
; /*< Base class - this must be first */
69 struct gl_config
*gl_visual
; /*< Describes the buffers */
70 struct swrast_renderbuffer
*srb
; /*< The user's colorbuffer */
71 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
72 GLenum format
; /*< User-specified context format */
73 GLint userRowLength
; /*< user-specified number of pixels per row */
74 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
75 GLvoid
*rowaddr
[SWRAST_MAX_HEIGHT
]; /*< address of first pixel in each image row */
76 GLboolean yup
; /*< TRUE -> Y increases upward */
77 /*< FALSE -> Y increases downward */
82 static INLINE OSMesaContext
83 OSMESA_CONTEXT(struct gl_context
*ctx
)
85 /* Just cast, since we're using structure containment */
86 return (OSMesaContext
) ctx
;
90 /**********************************************************************/
91 /*** Private Device Driver Functions ***/
92 /**********************************************************************/
95 static const GLubyte
*
96 get_string( struct gl_context
*ctx
, GLenum name
)
102 return (const GLubyte
*) "Mesa OffScreen32";
103 #elif CHAN_BITS == 16
104 return (const GLubyte
*) "Mesa OffScreen16";
106 return (const GLubyte
*) "Mesa OffScreen";
115 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
117 /* easy - just propogate */
118 _swrast_InvalidateState( ctx
, new_state
);
119 _swsetup_InvalidateState( ctx
, new_state
);
120 _tnl_InvalidateState( ctx
, new_state
);
121 _vbo_InvalidateState( ctx
, new_state
);
127 * Macros for optimized line/triangle rendering.
128 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
131 #define PACK_RGBA(DST, R, G, B, A) \
133 (DST)[osmesa->rInd] = R; \
134 (DST)[osmesa->gInd] = G; \
135 (DST)[osmesa->bInd] = B; \
136 (DST)[osmesa->aInd] = A; \
139 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
143 * Draw a flat-shaded, RGB line into an osmesa buffer.
145 #define NAME flat_rgba_line
148 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
149 const GLchan *color = vert1->color;
153 GLchan *p = PIXELADDR4(X, Y); \
154 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
157 #include "swrast/s_linetemp.h"
162 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
164 #define NAME flat_rgba_z_line
167 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
169 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
170 const GLchan *color = vert1->color;
175 GLchan *p = PIXELADDR4(X, Y); \
176 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
177 color[BCOMP], color[ACOMP]); \
182 #include "swrast/s_linetemp.h"
187 * Analyze context state to see if we can provide a fast line drawing
188 * function. Otherwise, return NULL.
190 static swrast_line_func
191 osmesa_choose_line_function( struct gl_context
*ctx
)
193 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
194 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
196 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
197 if (ctx
->Line
.SmoothFlag
) return NULL
;
198 if (ctx
->Texture
._EnabledUnits
) return NULL
;
199 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
200 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
201 if (ctx
->Line
.StippleFlag
) return NULL
;
202 if (ctx
->Line
.SmoothFlag
) return NULL
;
203 if (osmesa
->format
!= OSMESA_RGBA
&&
204 osmesa
->format
!= OSMESA_BGRA
&&
205 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
207 if (swrast
->_RasterMask
==DEPTH_BIT
208 && ctx
->Depth
.Func
==GL_LESS
209 && ctx
->Depth
.Mask
==GL_TRUE
210 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
211 return (swrast_line_func
) flat_rgba_z_line
;
214 if (swrast
->_RasterMask
== 0) {
215 return (swrast_line_func
) flat_rgba_line
;
218 return (swrast_line_func
) NULL
;
222 /**********************************************************************/
223 /***** Optimized triangle rendering *****/
224 /**********************************************************************/
228 * Smooth-shaded, z-less triangle, RGBA color.
230 #define NAME smooth_rgba_z_triangle
232 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
234 #define INTERP_ALPHA 1
236 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
237 #define RENDER_SPAN( span ) { \
239 GLchan *img = PIXELADDR4(span.x, span.y); \
240 for (i = 0; i < span.end; i++, img += 4) { \
241 const GLuint z = FixedToDepth(span.z); \
243 PACK_RGBA(img, FixedToChan(span.red), \
244 FixedToChan(span.green), FixedToChan(span.blue), \
245 FixedToChan(span.alpha)); \
248 span.red += span.redStep; \
249 span.green += span.greenStep; \
250 span.blue += span.blueStep; \
251 span.alpha += span.alphaStep; \
252 span.z += span.zStep; \
255 #include "swrast/s_tritemp.h"
260 * Flat-shaded, z-less triangle, RGBA color.
262 #define NAME flat_rgba_z_triangle
264 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
266 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
268 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
269 v2->color[2], v2->color[3]);
271 #define RENDER_SPAN( span ) { \
273 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
274 for (i = 0; i < span.end; i++) { \
275 const GLuint z = FixedToDepth(span.z); \
280 span.z += span.zStep; \
284 #include "swrast/s_tritemp.h"
289 * Return pointer to an optimized triangle function if possible.
291 static swrast_tri_func
292 osmesa_choose_triangle_function( struct gl_context
*ctx
)
294 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
295 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
297 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
298 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
299 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
300 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
301 if (osmesa
->format
!= OSMESA_RGBA
&&
302 osmesa
->format
!= OSMESA_BGRA
&&
303 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
304 if (ctx
->Polygon
.CullFlag
&&
305 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
306 return (swrast_tri_func
) NULL
;
308 if (swrast
->_RasterMask
== DEPTH_BIT
&&
309 ctx
->Depth
.Func
== GL_LESS
&&
310 ctx
->Depth
.Mask
== GL_TRUE
&&
311 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
312 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
313 return (swrast_tri_func
) smooth_rgba_z_triangle
;
316 return (swrast_tri_func
) flat_rgba_z_triangle
;
319 return (swrast_tri_func
) NULL
;
324 /* Override for the swrast triangle-selection function. Try to use one
325 * of our internal triangle functions, otherwise fall back to the
326 * standard swrast functions.
329 osmesa_choose_triangle( struct gl_context
*ctx
)
331 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
333 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
334 if (!swrast
->Triangle
)
335 _swrast_choose_triangle( ctx
);
339 osmesa_choose_line( struct gl_context
*ctx
)
341 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
343 swrast
->Line
= osmesa_choose_line_function( ctx
);
345 _swrast_choose_line( ctx
);
351 * Recompute the values of the context's rowaddr array.
354 compute_row_addresses( OSMesaContext osmesa
)
356 GLint bytesPerRow
, i
;
357 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
358 GLint rowlength
; /* in pixels */
359 GLint height
= osmesa
->srb
->Base
.Height
;
361 if (osmesa
->userRowLength
)
362 rowlength
= osmesa
->userRowLength
;
364 rowlength
= osmesa
->srb
->Base
.Width
;
366 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
369 /* Y=0 is bottom line of window */
370 for (i
= 0; i
< height
; i
++) {
371 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
375 /* Y=0 is top line of window */
376 for (i
= 0; i
< height
; i
++) {
377 GLint j
= height
- i
- 1;
378 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
386 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
389 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
396 * Allocate renderbuffer storage. We don't actually allocate any storage
397 * since we're using a user-provided buffer.
398 * Just set up all the gl_renderbuffer methods.
401 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
402 GLenum internalFormat
, GLuint width
, GLuint height
)
404 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
406 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
407 (void) internalFormat
;
409 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
411 * XXX There aren't Mesa formats for all the possible combinations here!
412 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
414 * XXX The 8-bit/channel formats should all be OK.
416 if (osmesa
->format
== OSMESA_RGBA
) {
417 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
418 if (_mesa_little_endian())
419 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
421 rb
->Format
= MESA_FORMAT_RGBA8888
;
423 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
424 rb
->Format
= MESA_FORMAT_RGBA_16
;
427 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
430 else if (osmesa
->format
== OSMESA_BGRA
) {
431 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
432 if (_mesa_little_endian())
433 rb
->Format
= MESA_FORMAT_ARGB8888
;
435 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
437 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
438 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
439 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
442 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
443 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
446 else if (osmesa
->format
== OSMESA_ARGB
) {
447 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
448 if (_mesa_little_endian())
449 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
451 rb
->Format
= MESA_FORMAT_ARGB8888
;
453 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
454 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
455 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
458 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
459 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
462 else if (osmesa
->format
== OSMESA_RGB
) {
463 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
464 rb
->Format
= MESA_FORMAT_RGB888
;
466 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
467 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
468 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
471 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
472 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
475 else if (osmesa
->format
== OSMESA_BGR
) {
476 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
477 rb
->Format
= MESA_FORMAT_BGR888
;
479 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
480 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
481 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
484 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
485 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
488 else if (osmesa
->format
== OSMESA_RGB_565
) {
489 ASSERT(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
490 rb
->Format
= MESA_FORMAT_RGB565
;
493 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
499 compute_row_addresses( osmesa
);
506 * Allocate a new renderbuffer to describe the user-provided color buffer.
508 static struct swrast_renderbuffer
*
509 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
511 const GLuint name
= 0;
512 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
515 _mesa_init_renderbuffer(&srb
->Base
, name
);
517 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
518 srb
->Base
.RefCount
= 1;
519 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
520 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
522 srb
->Base
.InternalFormat
= GL_RGBA
;
523 srb
->Base
._BaseFormat
= GL_RGBA
;
533 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
534 struct gl_renderbuffer
*rb
,
535 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
537 GLubyte
**mapOut
, GLint
*rowStrideOut
)
539 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
541 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
542 /* this is an OSMesa renderbuffer which wraps user memory */
543 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
544 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
545 GLint rowStride
; /* in bytes */
547 if (osmesa
->userRowLength
)
548 rowStride
= osmesa
->userRowLength
* bpp
;
550 rowStride
= rb
->Width
* bpp
;
553 /* Y=0 is top line of window */
554 y
= rb
->Height
- y
- 1;
555 *rowStrideOut
= -rowStride
;
558 *rowStrideOut
= rowStride
;
561 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
564 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
565 mapOut
, rowStrideOut
);
571 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
573 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
577 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
582 /**********************************************************************/
583 /***** Public Functions *****/
584 /**********************************************************************/
588 * Create an Off-Screen Mesa rendering context. The only attribute needed is
589 * an RGBA vs Color-Index mode flag.
591 * Input: format - Must be GL_RGBA
592 * sharelist - specifies another OSMesaContext with which to share
593 * display lists. NULL indicates no sharing.
594 * Return: an OSMesaContext or 0 if error
596 GLAPI OSMesaContext GLAPIENTRY
597 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
599 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
608 * Create context and specify size of ancillary buffers.
610 GLAPI OSMesaContext GLAPIENTRY
611 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
612 GLint accumBits
, OSMesaContext sharelist
)
614 OSMesaContext osmesa
;
615 struct dd_function_table functions
;
616 GLint rind
, gind
, bind
, aind
;
617 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
619 rind
= gind
= bind
= aind
= 0;
620 if (format
==OSMESA_RGBA
) {
622 greenBits
= CHAN_BITS
;
623 blueBits
= CHAN_BITS
;
624 alphaBits
= CHAN_BITS
;
630 else if (format
==OSMESA_BGRA
) {
632 greenBits
= CHAN_BITS
;
633 blueBits
= CHAN_BITS
;
634 alphaBits
= CHAN_BITS
;
640 else if (format
==OSMESA_ARGB
) {
642 greenBits
= CHAN_BITS
;
643 blueBits
= CHAN_BITS
;
644 alphaBits
= CHAN_BITS
;
650 else if (format
==OSMESA_RGB
) {
652 greenBits
= CHAN_BITS
;
653 blueBits
= CHAN_BITS
;
659 else if (format
==OSMESA_BGR
) {
661 greenBits
= CHAN_BITS
;
662 blueBits
= CHAN_BITS
;
668 #if CHAN_TYPE == GL_UNSIGNED_BYTE
669 else if (format
==OSMESA_RGB_565
) {
674 rind
= 0; /* not used */
683 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
685 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
686 GL_FALSE
, /* stereo */
696 alphaBits
? accumBits
: 0,
699 if (!osmesa
->gl_visual
) {
704 /* Initialize device driver function table */
705 _mesa_init_driver_functions(&functions
);
706 /* override with our functions */
707 functions
.GetString
= get_string
;
708 functions
.UpdateState
= osmesa_update_state
;
709 functions
.GetBufferSize
= NULL
;
711 if (!_mesa_initialize_context(&osmesa
->mesa
,
714 sharelist
? &sharelist
->mesa
715 : (struct gl_context
*) NULL
,
717 _mesa_destroy_visual( osmesa
->gl_visual
);
722 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
723 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
724 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
725 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
726 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
727 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
729 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
730 if (!osmesa
->gl_buffer
) {
731 _mesa_destroy_visual( osmesa
->gl_visual
);
732 _mesa_free_context_data( &osmesa
->mesa
);
737 /* Create depth/stencil/accum buffers. We'll create the color
738 * buffer later in OSMesaMakeCurrent().
740 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
741 GL_FALSE
, /* color */
742 osmesa
->gl_visual
->haveDepthBuffer
,
743 osmesa
->gl_visual
->haveStencilBuffer
,
744 osmesa
->gl_visual
->haveAccumBuffer
,
745 GL_FALSE
, /* alpha */
746 GL_FALSE
/* aux */ );
748 osmesa
->format
= format
;
749 osmesa
->userRowLength
= 0;
750 osmesa
->yup
= GL_TRUE
;
756 _mesa_meta_init(&osmesa
->mesa
);
758 /* Initialize the software rasterizer and helper modules. */
760 struct gl_context
*ctx
= &osmesa
->mesa
;
764 if (!_swrast_CreateContext( ctx
) ||
765 !_vbo_CreateContext( ctx
) ||
766 !_tnl_CreateContext( ctx
) ||
767 !_swsetup_CreateContext( ctx
)) {
768 _mesa_destroy_visual(osmesa
->gl_visual
);
769 _mesa_free_context_data(ctx
);
774 _swsetup_Wakeup( ctx
);
776 /* use default TCL pipeline */
777 tnl
= TNL_CONTEXT(ctx
);
778 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
780 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
781 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
783 /* Extend the software rasterizer with our optimized line and triangle
786 swrast
= SWRAST_CONTEXT( ctx
);
787 swrast
->choose_line
= osmesa_choose_line
;
788 swrast
->choose_triangle
= osmesa_choose_triangle
;
796 * Destroy an Off-Screen Mesa rendering context.
798 * \param osmesa the context to destroy
800 GLAPI
void GLAPIENTRY
801 OSMesaDestroyContext( OSMesaContext osmesa
)
805 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
807 _mesa_meta_free( &osmesa
->mesa
);
809 _swsetup_DestroyContext( &osmesa
->mesa
);
810 _tnl_DestroyContext( &osmesa
->mesa
);
811 _vbo_DestroyContext( &osmesa
->mesa
);
812 _swrast_DestroyContext( &osmesa
->mesa
);
814 _mesa_destroy_visual( osmesa
->gl_visual
);
815 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
817 _mesa_free_context_data( &osmesa
->mesa
);
824 * Bind an OSMesaContext to an image buffer. The image buffer is just a
825 * block of memory which the client provides. Its size must be at least
826 * as large as width*height*sizeof(type). Its address should be a multiple
827 * of 4 if using RGBA mode.
829 * Image data is stored in the order of glDrawPixels: row-major order
830 * with the lower-left image pixel stored in the first array position
831 * (ie. bottom-to-top).
833 * If the context's viewport hasn't been initialized yet, it will now be
834 * initialized to (0,0,width,height).
836 * Input: osmesa - the rendering context
837 * buffer - the image buffer memory
838 * type - data type for pixel components
839 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
840 * are supported. But if Mesa's been compiled with CHAN_BITS==16
841 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
842 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
843 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
844 * width, height - size of image buffer in pixels, at least 1
845 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
846 * invalid buffer address, invalid type, width<1, height<1,
847 * width>internal limit or height>internal limit.
849 GLAPI GLboolean GLAPIENTRY
850 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
851 GLsizei width
, GLsizei height
)
853 if (!osmesa
|| !buffer
||
854 width
< 1 || height
< 1 ||
855 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
859 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
864 if (!(type
== GL_UNSIGNED_BYTE
||
865 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
866 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
867 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
872 osmesa_update_state( &osmesa
->mesa
, 0 );
874 /* Call this periodically to detect when the user has begun using
875 * GL rendering from multiple threads.
877 _glapi_check_multithread();
880 /* Create a front/left color buffer which wraps the user-provided buffer.
881 * There is no back color buffer.
882 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
883 * doesn't match what Mesa was compiled for (CHAN_BITS) the
884 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
885 * that converts rendering from CHAN_BITS to the user-requested channel
889 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
890 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
891 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
893 assert(osmesa
->srb
->Base
.RefCount
== 2);
896 osmesa
->DataType
= type
;
898 /* Set renderbuffer fields. Set width/height = 0 to force
899 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
901 osmesa
->srb
->Buffer
= buffer
;
902 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
904 /* Set the framebuffer's size. This causes the
905 * osmesa_renderbuffer_storage() function to get called.
907 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
908 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
910 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
912 /* Remove renderbuffer attachment, then re-add. This installs the
913 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
915 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
916 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
920 /* this updates the visual's red/green/blue/alphaBits fields */
921 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
923 /* update the framebuffer size */
924 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
931 GLAPI OSMesaContext GLAPIENTRY
932 OSMesaGetCurrentContext( void )
934 struct gl_context
*ctx
= _mesa_get_current_context();
936 return (OSMesaContext
) ctx
;
943 GLAPI
void GLAPIENTRY
944 OSMesaPixelStore( GLint pname
, GLint value
)
946 OSMesaContext osmesa
= OSMesaGetCurrentContext();
949 case OSMESA_ROW_LENGTH
:
951 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
952 "OSMesaPixelStore(value)" );
955 osmesa
->userRowLength
= value
;
958 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
961 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
965 compute_row_addresses( osmesa
);
969 GLAPI
void GLAPIENTRY
970 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
972 OSMesaContext osmesa
= OSMesaGetCurrentContext();
976 if (osmesa
->gl_buffer
)
977 *value
= osmesa
->gl_buffer
->Width
;
982 if (osmesa
->gl_buffer
)
983 *value
= osmesa
->gl_buffer
->Height
;
988 *value
= osmesa
->format
;
991 /* current color buffer's data type */
992 *value
= osmesa
->DataType
;
994 case OSMESA_ROW_LENGTH
:
995 *value
= osmesa
->userRowLength
;
998 *value
= osmesa
->yup
;
1000 case OSMESA_MAX_WIDTH
:
1001 *value
= SWRAST_MAX_WIDTH
;
1003 case OSMESA_MAX_HEIGHT
:
1004 *value
= SWRAST_MAX_HEIGHT
;
1007 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1014 * Return the depth buffer associated with an OSMesa context.
1015 * Input: c - the OSMesa context
1016 * Output: width, height - size of buffer in pixels
1017 * bytesPerValue - bytes per depth value (2 or 4)
1018 * buffer - pointer to depth buffer values
1019 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1021 GLAPI GLboolean GLAPIENTRY
1022 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1023 GLint
*bytesPerValue
, void **buffer
)
1025 struct swrast_renderbuffer
*srb
= NULL
;
1028 srb
= swrast_renderbuffer(c
->gl_buffer
->
1029 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1031 if (!srb
|| !srb
->Buffer
) {
1039 *width
= srb
->Base
.Width
;
1040 *height
= srb
->Base
.Height
;
1041 if (c
->gl_visual
->depthBits
<= 16)
1042 *bytesPerValue
= sizeof(GLushort
);
1044 *bytesPerValue
= sizeof(GLuint
);
1045 *buffer
= (void *) srb
->Buffer
;
1052 * Return the color buffer associated with an OSMesa context.
1053 * Input: c - the OSMesa context
1054 * Output: width, height - size of buffer in pixels
1055 * format - the pixel format (OSMESA_FORMAT)
1056 * buffer - pointer to color buffer values
1057 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1059 GLAPI GLboolean GLAPIENTRY
1060 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1061 GLint
*height
, GLint
*format
, void **buffer
)
1063 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1064 *width
= osmesa
->srb
->Base
.Width
;
1065 *height
= osmesa
->srb
->Base
.Height
;
1066 *format
= osmesa
->format
;
1067 *buffer
= (void *) osmesa
->srb
->Buffer
;
1080 struct name_function
1083 OSMESAproc Function
;
1086 static struct name_function functions
[] = {
1087 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1088 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1089 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1090 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1091 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1092 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1093 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1094 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1095 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1096 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1097 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1102 GLAPI OSMESAproc GLAPIENTRY
1103 OSMesaGetProcAddress( const char *funcName
)
1106 for (i
= 0; functions
[i
].Name
; i
++) {
1107 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1108 return functions
[i
].Function
;
1110 return _glapi_get_proc_address(funcName
);
1114 GLAPI
void GLAPIENTRY
1115 OSMesaColorClamp(GLboolean enable
)
1117 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1119 if (enable
== GL_TRUE
) {
1120 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1123 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1129 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1130 * libglapi.a. We need to define them here.
1132 #ifdef GLX_INDIRECT_RENDERING
1134 #define GL_GLEXT_PROTOTYPES
1136 #include "glapi/glapi.h"
1137 #include "glapi/glapitable.h"
1139 #if defined(USE_MGL_NAMESPACE)
1140 #define NAME(func) mgl##func
1142 #define NAME(func) gl##func
1145 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1146 GET_DISPATCH()->FUNC ARGS
1148 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1149 return GET_DISPATCH()->FUNC ARGS
1151 /* skip normal ones */
1152 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1153 #include "glapi/glapitemp.h"
1155 #endif /* GLX_INDIRECT_RENDERING */