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 * THE AUTHORS 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/api_exec.h"
39 #include "main/context.h"
40 #include "main/extensions.h"
41 #include "main/formats.h"
42 #include "main/framebuffer.h"
43 #include "main/imports.h"
44 #include "main/macros.h"
45 #include "main/mipmap.h"
46 #include "main/mtypes.h"
47 #include "main/renderbuffer.h"
48 #include "main/version.h"
49 #include "main/vtxfmt.h"
50 #include "swrast/swrast.h"
51 #include "swrast_setup/swrast_setup.h"
52 #include "swrast/s_context.h"
53 #include "swrast/s_lines.h"
54 #include "swrast/s_renderbuffer.h"
55 #include "swrast/s_triangle.h"
57 #include "tnl/t_context.h"
58 #include "tnl/t_pipeline.h"
59 #include "drivers/common/driverfuncs.h"
60 #include "drivers/common/meta.h"
64 #define OSMESA_RENDERBUFFER_CLASS 0x053
68 * OSMesa rendering context, derived from core Mesa struct gl_context.
72 struct gl_context mesa
; /*< Base class - this must be first */
73 struct gl_config
*gl_visual
; /*< Describes the buffers */
74 struct swrast_renderbuffer
*srb
; /*< The user's colorbuffer */
75 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
76 GLenum format
; /*< User-specified context format */
77 GLint userRowLength
; /*< user-specified number of pixels per row */
78 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
79 GLvoid
*rowaddr
[SWRAST_MAX_HEIGHT
]; /*< address of first pixel in each image row */
80 GLboolean yup
; /*< TRUE -> Y increases upward */
81 /*< FALSE -> Y increases downward */
86 static INLINE OSMesaContext
87 OSMESA_CONTEXT(struct gl_context
*ctx
)
89 /* Just cast, since we're using structure containment */
90 return (OSMesaContext
) ctx
;
94 /**********************************************************************/
95 /*** Private Device Driver Functions ***/
96 /**********************************************************************/
99 static const GLubyte
*
100 get_string( struct gl_context
*ctx
, GLenum name
)
106 return (const GLubyte
*) "Mesa OffScreen32";
107 #elif CHAN_BITS == 16
108 return (const GLubyte
*) "Mesa OffScreen16";
110 return (const GLubyte
*) "Mesa OffScreen";
119 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
121 /* easy - just propogate */
122 _swrast_InvalidateState( ctx
, new_state
);
123 _swsetup_InvalidateState( ctx
, new_state
);
124 _tnl_InvalidateState( ctx
, new_state
);
125 _vbo_InvalidateState( ctx
, new_state
);
131 * Macros for optimized line/triangle rendering.
132 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
135 #define PACK_RGBA(DST, R, G, B, A) \
137 (DST)[osmesa->rInd] = R; \
138 (DST)[osmesa->gInd] = G; \
139 (DST)[osmesa->bInd] = B; \
140 (DST)[osmesa->aInd] = A; \
143 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
147 * Draw a flat-shaded, RGB line into an osmesa buffer.
149 #define NAME flat_rgba_line
152 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
153 const GLchan *color = vert1->color;
157 GLchan *p = PIXELADDR4(X, Y); \
158 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
161 #include "swrast/s_linetemp.h"
166 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
168 #define NAME flat_rgba_z_line
171 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
173 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
174 const GLchan *color = vert1->color;
179 GLchan *p = PIXELADDR4(X, Y); \
180 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
181 color[BCOMP], color[ACOMP]); \
186 #include "swrast/s_linetemp.h"
191 * Analyze context state to see if we can provide a fast line drawing
192 * function. Otherwise, return NULL.
194 static swrast_line_func
195 osmesa_choose_line_function( struct gl_context
*ctx
)
197 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
198 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
200 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
201 if (ctx
->Line
.SmoothFlag
) return NULL
;
202 if (ctx
->Texture
._EnabledUnits
) return NULL
;
203 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
204 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
205 if (ctx
->Line
.StippleFlag
) return NULL
;
206 if (ctx
->Line
.SmoothFlag
) return NULL
;
207 if (osmesa
->format
!= OSMESA_RGBA
&&
208 osmesa
->format
!= OSMESA_BGRA
&&
209 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
211 if (swrast
->_RasterMask
==DEPTH_BIT
212 && ctx
->Depth
.Func
==GL_LESS
213 && ctx
->Depth
.Mask
==GL_TRUE
214 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
215 return (swrast_line_func
) flat_rgba_z_line
;
218 if (swrast
->_RasterMask
== 0) {
219 return (swrast_line_func
) flat_rgba_line
;
222 return (swrast_line_func
) NULL
;
226 /**********************************************************************/
227 /***** Optimized triangle rendering *****/
228 /**********************************************************************/
232 * Smooth-shaded, z-less triangle, RGBA color.
234 #define NAME smooth_rgba_z_triangle
236 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
238 #define INTERP_ALPHA 1
240 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
241 #define RENDER_SPAN( span ) { \
243 GLchan *img = PIXELADDR4(span.x, span.y); \
244 for (i = 0; i < span.end; i++, img += 4) { \
245 const GLuint z = FixedToDepth(span.z); \
247 PACK_RGBA(img, FixedToChan(span.red), \
248 FixedToChan(span.green), FixedToChan(span.blue), \
249 FixedToChan(span.alpha)); \
252 span.red += span.redStep; \
253 span.green += span.greenStep; \
254 span.blue += span.blueStep; \
255 span.alpha += span.alphaStep; \
256 span.z += span.zStep; \
259 #include "swrast/s_tritemp.h"
264 * Flat-shaded, z-less triangle, RGBA color.
266 #define NAME flat_rgba_z_triangle
268 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
270 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
272 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
273 v2->color[2], v2->color[3]);
275 #define RENDER_SPAN( span ) { \
277 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
278 for (i = 0; i < span.end; i++) { \
279 const GLuint z = FixedToDepth(span.z); \
284 span.z += span.zStep; \
288 #include "swrast/s_tritemp.h"
293 * Return pointer to an optimized triangle function if possible.
295 static swrast_tri_func
296 osmesa_choose_triangle_function( struct gl_context
*ctx
)
298 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
299 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
301 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
302 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
303 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
304 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
305 if (osmesa
->format
!= OSMESA_RGBA
&&
306 osmesa
->format
!= OSMESA_BGRA
&&
307 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
308 if (ctx
->Polygon
.CullFlag
&&
309 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
310 return (swrast_tri_func
) NULL
;
312 if (swrast
->_RasterMask
== DEPTH_BIT
&&
313 ctx
->Depth
.Func
== GL_LESS
&&
314 ctx
->Depth
.Mask
== GL_TRUE
&&
315 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
316 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
317 return (swrast_tri_func
) smooth_rgba_z_triangle
;
320 return (swrast_tri_func
) flat_rgba_z_triangle
;
323 return (swrast_tri_func
) NULL
;
328 /* Override for the swrast triangle-selection function. Try to use one
329 * of our internal triangle functions, otherwise fall back to the
330 * standard swrast functions.
333 osmesa_choose_triangle( struct gl_context
*ctx
)
335 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
337 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
338 if (!swrast
->Triangle
)
339 _swrast_choose_triangle( ctx
);
343 osmesa_choose_line( struct gl_context
*ctx
)
345 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
347 swrast
->Line
= osmesa_choose_line_function( ctx
);
349 _swrast_choose_line( ctx
);
355 * Recompute the values of the context's rowaddr array.
358 compute_row_addresses( OSMesaContext osmesa
)
360 GLint bytesPerRow
, i
;
361 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
362 GLint rowlength
; /* in pixels */
363 GLint height
= osmesa
->srb
->Base
.Height
;
365 if (osmesa
->userRowLength
)
366 rowlength
= osmesa
->userRowLength
;
368 rowlength
= osmesa
->srb
->Base
.Width
;
370 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
373 /* Y=0 is bottom line of window */
374 for (i
= 0; i
< height
; i
++) {
375 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
379 /* Y=0 is top line of window */
380 for (i
= 0; i
< height
; i
++) {
381 GLint j
= height
- i
- 1;
382 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
390 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
393 osmesa_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
395 _mesa_delete_renderbuffer(ctx
, rb
);
400 * Allocate renderbuffer storage. We don't actually allocate any storage
401 * since we're using a user-provided buffer.
402 * Just set up all the gl_renderbuffer methods.
405 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
406 GLenum internalFormat
, GLuint width
, GLuint height
)
408 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
410 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
411 (void) internalFormat
;
413 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
415 * XXX There aren't Mesa formats for all the possible combinations here!
416 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
418 * XXX The 8-bit/channel formats should all be OK.
420 if (osmesa
->format
== OSMESA_RGBA
) {
421 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
422 if (_mesa_little_endian())
423 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
425 rb
->Format
= MESA_FORMAT_RGBA8888
;
427 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
428 rb
->Format
= MESA_FORMAT_RGBA_16
;
431 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
434 else if (osmesa
->format
== OSMESA_BGRA
) {
435 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
436 if (_mesa_little_endian())
437 rb
->Format
= MESA_FORMAT_ARGB8888
;
439 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
441 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
442 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
443 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
446 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
447 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
450 else if (osmesa
->format
== OSMESA_ARGB
) {
451 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
452 if (_mesa_little_endian())
453 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
455 rb
->Format
= MESA_FORMAT_ARGB8888
;
457 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
458 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
459 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
462 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
463 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
466 else if (osmesa
->format
== OSMESA_RGB
) {
467 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
468 rb
->Format
= MESA_FORMAT_RGB888
;
470 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
471 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
472 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
475 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
476 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
479 else if (osmesa
->format
== OSMESA_BGR
) {
480 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
481 rb
->Format
= MESA_FORMAT_BGR888
;
483 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
484 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
485 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
488 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
489 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
492 else if (osmesa
->format
== OSMESA_RGB_565
) {
493 ASSERT(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
494 rb
->Format
= MESA_FORMAT_RGB565
;
497 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
503 compute_row_addresses( osmesa
);
510 * Allocate a new renderbuffer to describe the user-provided color buffer.
512 static struct swrast_renderbuffer
*
513 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
515 const GLuint name
= 0;
516 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
519 _mesa_init_renderbuffer(&srb
->Base
, name
);
521 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
522 srb
->Base
.RefCount
= 1;
523 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
524 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
526 srb
->Base
.InternalFormat
= GL_RGBA
;
527 srb
->Base
._BaseFormat
= GL_RGBA
;
537 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
538 struct gl_renderbuffer
*rb
,
539 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
541 GLubyte
**mapOut
, GLint
*rowStrideOut
)
543 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
545 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
546 /* this is an OSMesa renderbuffer which wraps user memory */
547 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
548 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
549 GLint rowStride
; /* in bytes */
551 if (osmesa
->userRowLength
)
552 rowStride
= osmesa
->userRowLength
* bpp
;
554 rowStride
= rb
->Width
* bpp
;
557 /* Y=0 is top line of window */
558 y
= rb
->Height
- y
- 1;
559 *rowStrideOut
= -rowStride
;
562 *rowStrideOut
= rowStride
;
565 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
568 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
569 mapOut
, rowStrideOut
);
575 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
577 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
581 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
586 /**********************************************************************/
587 /***** Public Functions *****/
588 /**********************************************************************/
592 * Create an Off-Screen Mesa rendering context. The only attribute needed is
593 * an RGBA vs Color-Index mode flag.
595 * Input: format - Must be GL_RGBA
596 * sharelist - specifies another OSMesaContext with which to share
597 * display lists. NULL indicates no sharing.
598 * Return: an OSMesaContext or 0 if error
600 GLAPI OSMesaContext GLAPIENTRY
601 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
603 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
612 * Create context and specify size of ancillary buffers.
614 GLAPI OSMesaContext GLAPIENTRY
615 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
616 GLint accumBits
, OSMesaContext sharelist
)
618 OSMesaContext osmesa
;
619 struct dd_function_table functions
;
620 GLint rind
, gind
, bind
, aind
;
621 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
623 rind
= gind
= bind
= aind
= 0;
624 if (format
==OSMESA_RGBA
) {
626 greenBits
= CHAN_BITS
;
627 blueBits
= CHAN_BITS
;
628 alphaBits
= CHAN_BITS
;
634 else if (format
==OSMESA_BGRA
) {
636 greenBits
= CHAN_BITS
;
637 blueBits
= CHAN_BITS
;
638 alphaBits
= CHAN_BITS
;
644 else if (format
==OSMESA_ARGB
) {
646 greenBits
= CHAN_BITS
;
647 blueBits
= CHAN_BITS
;
648 alphaBits
= CHAN_BITS
;
654 else if (format
==OSMESA_RGB
) {
656 greenBits
= CHAN_BITS
;
657 blueBits
= CHAN_BITS
;
663 else if (format
==OSMESA_BGR
) {
665 greenBits
= CHAN_BITS
;
666 blueBits
= CHAN_BITS
;
672 #if CHAN_TYPE == GL_UNSIGNED_BYTE
673 else if (format
==OSMESA_RGB_565
) {
678 rind
= 0; /* not used */
687 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
689 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
690 GL_FALSE
, /* stereo */
700 alphaBits
? accumBits
: 0,
703 if (!osmesa
->gl_visual
) {
708 /* Initialize device driver function table */
709 _mesa_init_driver_functions(&functions
);
710 /* override with our functions */
711 functions
.GetString
= get_string
;
712 functions
.UpdateState
= osmesa_update_state
;
713 functions
.GetBufferSize
= NULL
;
715 if (!_mesa_initialize_context(&osmesa
->mesa
,
718 sharelist
? &sharelist
->mesa
719 : (struct gl_context
*) NULL
,
721 _mesa_destroy_visual( osmesa
->gl_visual
);
726 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
727 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
728 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
729 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
730 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
731 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
733 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
734 if (!osmesa
->gl_buffer
) {
735 _mesa_destroy_visual( osmesa
->gl_visual
);
736 _mesa_free_context_data( &osmesa
->mesa
);
741 /* Create depth/stencil/accum buffers. We'll create the color
742 * buffer later in OSMesaMakeCurrent().
744 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
745 GL_FALSE
, /* color */
746 osmesa
->gl_visual
->haveDepthBuffer
,
747 osmesa
->gl_visual
->haveStencilBuffer
,
748 osmesa
->gl_visual
->haveAccumBuffer
,
749 GL_FALSE
, /* alpha */
750 GL_FALSE
/* aux */ );
752 osmesa
->format
= format
;
753 osmesa
->userRowLength
= 0;
754 osmesa
->yup
= GL_TRUE
;
760 _mesa_meta_init(&osmesa
->mesa
);
762 /* Initialize the software rasterizer and helper modules. */
764 struct gl_context
*ctx
= &osmesa
->mesa
;
768 if (!_swrast_CreateContext( ctx
) ||
769 !_vbo_CreateContext( ctx
) ||
770 !_tnl_CreateContext( ctx
) ||
771 !_swsetup_CreateContext( ctx
)) {
772 _mesa_destroy_visual(osmesa
->gl_visual
);
773 _mesa_free_context_data(ctx
);
778 _swsetup_Wakeup( ctx
);
780 /* use default TCL pipeline */
781 tnl
= TNL_CONTEXT(ctx
);
782 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
784 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
785 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
787 ctx
->Driver
.GenerateMipmap
= _mesa_generate_mipmap
;
789 /* Extend the software rasterizer with our optimized line and triangle
792 swrast
= SWRAST_CONTEXT( ctx
);
793 swrast
->choose_line
= osmesa_choose_line
;
794 swrast
->choose_triangle
= osmesa_choose_triangle
;
796 _mesa_compute_version(ctx
);
798 /* Exec table initialization requires the version to be computed */
799 _mesa_initialize_dispatch_tables(ctx
);
800 _mesa_initialize_vbo_vtxfmt(ctx
);
808 * Destroy an Off-Screen Mesa rendering context.
810 * \param osmesa the context to destroy
812 GLAPI
void GLAPIENTRY
813 OSMesaDestroyContext( OSMesaContext osmesa
)
817 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
819 _mesa_meta_free( &osmesa
->mesa
);
821 _swsetup_DestroyContext( &osmesa
->mesa
);
822 _tnl_DestroyContext( &osmesa
->mesa
);
823 _vbo_DestroyContext( &osmesa
->mesa
);
824 _swrast_DestroyContext( &osmesa
->mesa
);
826 _mesa_destroy_visual( osmesa
->gl_visual
);
827 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
829 _mesa_free_context_data( &osmesa
->mesa
);
836 * Bind an OSMesaContext to an image buffer. The image buffer is just a
837 * block of memory which the client provides. Its size must be at least
838 * as large as width*height*sizeof(type). Its address should be a multiple
839 * of 4 if using RGBA mode.
841 * Image data is stored in the order of glDrawPixels: row-major order
842 * with the lower-left image pixel stored in the first array position
843 * (ie. bottom-to-top).
845 * If the context's viewport hasn't been initialized yet, it will now be
846 * initialized to (0,0,width,height).
848 * Input: osmesa - the rendering context
849 * buffer - the image buffer memory
850 * type - data type for pixel components
851 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
852 * are supported. But if Mesa's been compiled with CHAN_BITS==16
853 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
854 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
855 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
856 * width, height - size of image buffer in pixels, at least 1
857 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
858 * invalid buffer address, invalid type, width<1, height<1,
859 * width>internal limit or height>internal limit.
861 GLAPI GLboolean GLAPIENTRY
862 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
863 GLsizei width
, GLsizei height
)
865 if (!osmesa
|| !buffer
||
866 width
< 1 || height
< 1 ||
867 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
871 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
876 if (!(type
== GL_UNSIGNED_BYTE
||
877 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
878 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
879 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
884 osmesa_update_state( &osmesa
->mesa
, 0 );
886 /* Call this periodically to detect when the user has begun using
887 * GL rendering from multiple threads.
889 _glapi_check_multithread();
892 /* Create a front/left color buffer which wraps the user-provided buffer.
893 * There is no back color buffer.
894 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
895 * doesn't match what Mesa was compiled for (CHAN_BITS) the
896 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
897 * that converts rendering from CHAN_BITS to the user-requested channel
901 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
902 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
903 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
905 assert(osmesa
->srb
->Base
.RefCount
== 2);
908 osmesa
->DataType
= type
;
910 /* Set renderbuffer fields. Set width/height = 0 to force
911 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
913 osmesa
->srb
->Buffer
= buffer
;
914 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
916 /* Set the framebuffer's size. This causes the
917 * osmesa_renderbuffer_storage() function to get called.
919 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
920 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
922 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
924 /* Remove renderbuffer attachment, then re-add. This installs the
925 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
927 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
928 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
932 /* this updates the visual's red/green/blue/alphaBits fields */
933 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
935 /* update the framebuffer size */
936 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
943 GLAPI OSMesaContext GLAPIENTRY
944 OSMesaGetCurrentContext( void )
946 struct gl_context
*ctx
= _mesa_get_current_context();
948 return (OSMesaContext
) ctx
;
955 GLAPI
void GLAPIENTRY
956 OSMesaPixelStore( GLint pname
, GLint value
)
958 OSMesaContext osmesa
= OSMesaGetCurrentContext();
961 case OSMESA_ROW_LENGTH
:
963 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
964 "OSMesaPixelStore(value)" );
967 osmesa
->userRowLength
= value
;
970 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
973 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
977 compute_row_addresses( osmesa
);
981 GLAPI
void GLAPIENTRY
982 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
984 OSMesaContext osmesa
= OSMesaGetCurrentContext();
988 if (osmesa
->gl_buffer
)
989 *value
= osmesa
->gl_buffer
->Width
;
994 if (osmesa
->gl_buffer
)
995 *value
= osmesa
->gl_buffer
->Height
;
1000 *value
= osmesa
->format
;
1003 /* current color buffer's data type */
1004 *value
= osmesa
->DataType
;
1006 case OSMESA_ROW_LENGTH
:
1007 *value
= osmesa
->userRowLength
;
1010 *value
= osmesa
->yup
;
1012 case OSMESA_MAX_WIDTH
:
1013 *value
= SWRAST_MAX_WIDTH
;
1015 case OSMESA_MAX_HEIGHT
:
1016 *value
= SWRAST_MAX_HEIGHT
;
1019 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1026 * Return the depth buffer associated with an OSMesa context.
1027 * Input: c - the OSMesa context
1028 * Output: width, height - size of buffer in pixels
1029 * bytesPerValue - bytes per depth value (2 or 4)
1030 * buffer - pointer to depth buffer values
1031 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1033 GLAPI GLboolean GLAPIENTRY
1034 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1035 GLint
*bytesPerValue
, void **buffer
)
1037 struct swrast_renderbuffer
*srb
= NULL
;
1040 srb
= swrast_renderbuffer(c
->gl_buffer
->
1041 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1043 if (!srb
|| !srb
->Buffer
) {
1051 *width
= srb
->Base
.Width
;
1052 *height
= srb
->Base
.Height
;
1053 if (c
->gl_visual
->depthBits
<= 16)
1054 *bytesPerValue
= sizeof(GLushort
);
1056 *bytesPerValue
= sizeof(GLuint
);
1057 *buffer
= (void *) srb
->Buffer
;
1064 * Return the color buffer associated with an OSMesa context.
1065 * Input: c - the OSMesa context
1066 * Output: width, height - size of buffer in pixels
1067 * format - the pixel format (OSMESA_FORMAT)
1068 * buffer - pointer to color buffer values
1069 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1071 GLAPI GLboolean GLAPIENTRY
1072 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1073 GLint
*height
, GLint
*format
, void **buffer
)
1075 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1076 *width
= osmesa
->srb
->Base
.Width
;
1077 *height
= osmesa
->srb
->Base
.Height
;
1078 *format
= osmesa
->format
;
1079 *buffer
= (void *) osmesa
->srb
->Buffer
;
1092 struct name_function
1095 OSMESAproc Function
;
1098 static struct name_function functions
[] = {
1099 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1100 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1101 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1102 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1103 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1104 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1105 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1106 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1107 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1108 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1109 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1114 GLAPI OSMESAproc GLAPIENTRY
1115 OSMesaGetProcAddress( const char *funcName
)
1118 for (i
= 0; functions
[i
].Name
; i
++) {
1119 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1120 return functions
[i
].Function
;
1122 return _glapi_get_proc_address(funcName
);
1126 GLAPI
void GLAPIENTRY
1127 OSMesaColorClamp(GLboolean enable
)
1129 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1131 if (enable
== GL_TRUE
) {
1132 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1135 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1141 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1142 * libglapi.a. We need to define them here.
1144 #ifdef GLX_INDIRECT_RENDERING
1146 #define GL_GLEXT_PROTOTYPES
1148 #include "glapi/glapi.h"
1149 #include "glapi/glapitable.h"
1151 #if defined(USE_MGL_NAMESPACE)
1152 #define NAME(func) mgl##func
1154 #define NAME(func) gl##func
1157 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1158 GET_DISPATCH()->FUNC ARGS
1160 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1161 return GET_DISPATCH()->FUNC ARGS
1163 /* skip normal ones */
1164 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1165 #include "glapi/glapitemp.h"
1167 #endif /* GLX_INDIRECT_RENDERING */