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/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/mtypes.h"
46 #include "main/renderbuffer.h"
47 #include "main/version.h"
48 #include "main/vtxfmt.h"
49 #include "swrast/swrast.h"
50 #include "swrast_setup/swrast_setup.h"
51 #include "swrast/s_context.h"
52 #include "swrast/s_lines.h"
53 #include "swrast/s_renderbuffer.h"
54 #include "swrast/s_triangle.h"
56 #include "tnl/t_context.h"
57 #include "tnl/t_pipeline.h"
58 #include "drivers/common/driverfuncs.h"
59 #include "drivers/common/meta.h"
63 #define OSMESA_RENDERBUFFER_CLASS 0x053
67 * OSMesa rendering context, derived from core Mesa struct gl_context.
71 struct gl_context mesa
; /*< Base class - this must be first */
72 struct gl_config
*gl_visual
; /*< Describes the buffers */
73 struct swrast_renderbuffer
*srb
; /*< The user's colorbuffer */
74 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
75 GLenum format
; /*< User-specified context format */
76 GLint userRowLength
; /*< user-specified number of pixels per row */
77 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
78 GLvoid
*rowaddr
[SWRAST_MAX_HEIGHT
]; /*< address of first pixel in each image row */
79 GLboolean yup
; /*< TRUE -> Y increases upward */
80 /*< FALSE -> Y increases downward */
85 static INLINE OSMesaContext
86 OSMESA_CONTEXT(struct gl_context
*ctx
)
88 /* Just cast, since we're using structure containment */
89 return (OSMesaContext
) ctx
;
93 /**********************************************************************/
94 /*** Private Device Driver Functions ***/
95 /**********************************************************************/
98 static const GLubyte
*
99 get_string( struct gl_context
*ctx
, GLenum name
)
105 return (const GLubyte
*) "Mesa OffScreen32";
106 #elif CHAN_BITS == 16
107 return (const GLubyte
*) "Mesa OffScreen16";
109 return (const GLubyte
*) "Mesa OffScreen";
118 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
120 /* easy - just propogate */
121 _swrast_InvalidateState( ctx
, new_state
);
122 _swsetup_InvalidateState( ctx
, new_state
);
123 _tnl_InvalidateState( ctx
, new_state
);
124 _vbo_InvalidateState( ctx
, new_state
);
130 * Macros for optimized line/triangle rendering.
131 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
134 #define PACK_RGBA(DST, R, G, B, A) \
136 (DST)[osmesa->rInd] = R; \
137 (DST)[osmesa->gInd] = G; \
138 (DST)[osmesa->bInd] = B; \
139 (DST)[osmesa->aInd] = A; \
142 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
146 * Draw a flat-shaded, RGB line into an osmesa buffer.
148 #define NAME flat_rgba_line
151 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
152 const GLchan *color = vert1->color;
156 GLchan *p = PIXELADDR4(X, Y); \
157 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
160 #include "swrast/s_linetemp.h"
165 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
167 #define NAME flat_rgba_z_line
170 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
172 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
173 const GLchan *color = vert1->color;
178 GLchan *p = PIXELADDR4(X, Y); \
179 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
180 color[BCOMP], color[ACOMP]); \
185 #include "swrast/s_linetemp.h"
190 * Analyze context state to see if we can provide a fast line drawing
191 * function. Otherwise, return NULL.
193 static swrast_line_func
194 osmesa_choose_line_function( struct gl_context
*ctx
)
196 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
197 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
199 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
200 if (ctx
->Line
.SmoothFlag
) return NULL
;
201 if (ctx
->Texture
._EnabledUnits
) return NULL
;
202 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
203 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
204 if (ctx
->Line
.StippleFlag
) return NULL
;
205 if (ctx
->Line
.SmoothFlag
) return NULL
;
206 if (osmesa
->format
!= OSMESA_RGBA
&&
207 osmesa
->format
!= OSMESA_BGRA
&&
208 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
210 if (swrast
->_RasterMask
==DEPTH_BIT
211 && ctx
->Depth
.Func
==GL_LESS
212 && ctx
->Depth
.Mask
==GL_TRUE
213 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
214 return (swrast_line_func
) flat_rgba_z_line
;
217 if (swrast
->_RasterMask
== 0) {
218 return (swrast_line_func
) flat_rgba_line
;
221 return (swrast_line_func
) NULL
;
225 /**********************************************************************/
226 /***** Optimized triangle rendering *****/
227 /**********************************************************************/
231 * Smooth-shaded, z-less triangle, RGBA color.
233 #define NAME smooth_rgba_z_triangle
235 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
237 #define INTERP_ALPHA 1
239 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
240 #define RENDER_SPAN( span ) { \
242 GLchan *img = PIXELADDR4(span.x, span.y); \
243 for (i = 0; i < span.end; i++, img += 4) { \
244 const GLuint z = FixedToDepth(span.z); \
246 PACK_RGBA(img, FixedToChan(span.red), \
247 FixedToChan(span.green), FixedToChan(span.blue), \
248 FixedToChan(span.alpha)); \
251 span.red += span.redStep; \
252 span.green += span.greenStep; \
253 span.blue += span.blueStep; \
254 span.alpha += span.alphaStep; \
255 span.z += span.zStep; \
258 #include "swrast/s_tritemp.h"
263 * Flat-shaded, z-less triangle, RGBA color.
265 #define NAME flat_rgba_z_triangle
267 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
269 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
271 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
272 v2->color[2], v2->color[3]);
274 #define RENDER_SPAN( span ) { \
276 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
277 for (i = 0; i < span.end; i++) { \
278 const GLuint z = FixedToDepth(span.z); \
283 span.z += span.zStep; \
287 #include "swrast/s_tritemp.h"
292 * Return pointer to an optimized triangle function if possible.
294 static swrast_tri_func
295 osmesa_choose_triangle_function( struct gl_context
*ctx
)
297 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
298 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
300 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
301 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
302 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
303 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
304 if (osmesa
->format
!= OSMESA_RGBA
&&
305 osmesa
->format
!= OSMESA_BGRA
&&
306 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
307 if (ctx
->Polygon
.CullFlag
&&
308 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
309 return (swrast_tri_func
) NULL
;
311 if (swrast
->_RasterMask
== DEPTH_BIT
&&
312 ctx
->Depth
.Func
== GL_LESS
&&
313 ctx
->Depth
.Mask
== GL_TRUE
&&
314 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
315 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
316 return (swrast_tri_func
) smooth_rgba_z_triangle
;
319 return (swrast_tri_func
) flat_rgba_z_triangle
;
322 return (swrast_tri_func
) NULL
;
327 /* Override for the swrast triangle-selection function. Try to use one
328 * of our internal triangle functions, otherwise fall back to the
329 * standard swrast functions.
332 osmesa_choose_triangle( struct gl_context
*ctx
)
334 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
336 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
337 if (!swrast
->Triangle
)
338 _swrast_choose_triangle( ctx
);
342 osmesa_choose_line( struct gl_context
*ctx
)
344 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
346 swrast
->Line
= osmesa_choose_line_function( ctx
);
348 _swrast_choose_line( ctx
);
354 * Recompute the values of the context's rowaddr array.
357 compute_row_addresses( OSMesaContext osmesa
)
359 GLint bytesPerRow
, i
;
360 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
361 GLint rowlength
; /* in pixels */
362 GLint height
= osmesa
->srb
->Base
.Height
;
364 if (osmesa
->userRowLength
)
365 rowlength
= osmesa
->userRowLength
;
367 rowlength
= osmesa
->srb
->Base
.Width
;
369 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
372 /* Y=0 is bottom line of window */
373 for (i
= 0; i
< height
; i
++) {
374 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
378 /* Y=0 is top line of window */
379 for (i
= 0; i
< height
; i
++) {
380 GLint j
= height
- i
- 1;
381 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
389 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
392 osmesa_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
394 _mesa_delete_renderbuffer(ctx
, rb
);
399 * Allocate renderbuffer storage. We don't actually allocate any storage
400 * since we're using a user-provided buffer.
401 * Just set up all the gl_renderbuffer methods.
404 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
405 GLenum internalFormat
, GLuint width
, GLuint height
)
407 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
409 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
410 (void) internalFormat
;
412 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
414 * XXX There aren't Mesa formats for all the possible combinations here!
415 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
417 * XXX The 8-bit/channel formats should all be OK.
419 if (osmesa
->format
== OSMESA_RGBA
) {
420 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
421 if (_mesa_little_endian())
422 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
424 rb
->Format
= MESA_FORMAT_RGBA8888
;
426 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
427 rb
->Format
= MESA_FORMAT_RGBA_16
;
430 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
433 else if (osmesa
->format
== OSMESA_BGRA
) {
434 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
435 if (_mesa_little_endian())
436 rb
->Format
= MESA_FORMAT_ARGB8888
;
438 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
440 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
441 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
442 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
445 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
446 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
449 else if (osmesa
->format
== OSMESA_ARGB
) {
450 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
451 if (_mesa_little_endian())
452 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
454 rb
->Format
= MESA_FORMAT_ARGB8888
;
456 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
457 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
458 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
461 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
462 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
465 else if (osmesa
->format
== OSMESA_RGB
) {
466 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
467 rb
->Format
= MESA_FORMAT_RGB888
;
469 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
470 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
471 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
474 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
475 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
478 else if (osmesa
->format
== OSMESA_BGR
) {
479 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
480 rb
->Format
= MESA_FORMAT_BGR888
;
482 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
483 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
484 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
487 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
488 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
491 else if (osmesa
->format
== OSMESA_RGB_565
) {
492 ASSERT(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
493 rb
->Format
= MESA_FORMAT_RGB565
;
496 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
502 compute_row_addresses( osmesa
);
509 * Allocate a new renderbuffer to describe the user-provided color buffer.
511 static struct swrast_renderbuffer
*
512 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
514 const GLuint name
= 0;
515 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
518 _mesa_init_renderbuffer(&srb
->Base
, name
);
520 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
521 srb
->Base
.RefCount
= 1;
522 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
523 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
525 srb
->Base
.InternalFormat
= GL_RGBA
;
526 srb
->Base
._BaseFormat
= GL_RGBA
;
536 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
537 struct gl_renderbuffer
*rb
,
538 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
540 GLubyte
**mapOut
, GLint
*rowStrideOut
)
542 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
544 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
545 /* this is an OSMesa renderbuffer which wraps user memory */
546 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
547 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
548 GLint rowStride
; /* in bytes */
550 if (osmesa
->userRowLength
)
551 rowStride
= osmesa
->userRowLength
* bpp
;
553 rowStride
= rb
->Width
* bpp
;
556 /* Y=0 is top line of window */
557 y
= rb
->Height
- y
- 1;
558 *rowStrideOut
= -rowStride
;
561 *rowStrideOut
= rowStride
;
564 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
567 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
568 mapOut
, rowStrideOut
);
574 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
576 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
580 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
585 /**********************************************************************/
586 /***** Public Functions *****/
587 /**********************************************************************/
591 * Create an Off-Screen Mesa rendering context. The only attribute needed is
592 * an RGBA vs Color-Index mode flag.
594 * Input: format - Must be GL_RGBA
595 * sharelist - specifies another OSMesaContext with which to share
596 * display lists. NULL indicates no sharing.
597 * Return: an OSMesaContext or 0 if error
599 GLAPI OSMesaContext GLAPIENTRY
600 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
602 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
611 * Create context and specify size of ancillary buffers.
613 GLAPI OSMesaContext GLAPIENTRY
614 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
615 GLint accumBits
, OSMesaContext sharelist
)
617 OSMesaContext osmesa
;
618 struct dd_function_table functions
;
619 GLint rind
, gind
, bind
, aind
;
620 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
622 rind
= gind
= bind
= aind
= 0;
623 if (format
==OSMESA_RGBA
) {
625 greenBits
= CHAN_BITS
;
626 blueBits
= CHAN_BITS
;
627 alphaBits
= CHAN_BITS
;
633 else if (format
==OSMESA_BGRA
) {
635 greenBits
= CHAN_BITS
;
636 blueBits
= CHAN_BITS
;
637 alphaBits
= CHAN_BITS
;
643 else if (format
==OSMESA_ARGB
) {
645 greenBits
= CHAN_BITS
;
646 blueBits
= CHAN_BITS
;
647 alphaBits
= CHAN_BITS
;
653 else if (format
==OSMESA_RGB
) {
655 greenBits
= CHAN_BITS
;
656 blueBits
= CHAN_BITS
;
662 else if (format
==OSMESA_BGR
) {
664 greenBits
= CHAN_BITS
;
665 blueBits
= CHAN_BITS
;
671 #if CHAN_TYPE == GL_UNSIGNED_BYTE
672 else if (format
==OSMESA_RGB_565
) {
677 rind
= 0; /* not used */
686 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
688 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
689 GL_FALSE
, /* stereo */
699 alphaBits
? accumBits
: 0,
702 if (!osmesa
->gl_visual
) {
707 /* Initialize device driver function table */
708 _mesa_init_driver_functions(&functions
);
709 /* override with our functions */
710 functions
.GetString
= get_string
;
711 functions
.UpdateState
= osmesa_update_state
;
712 functions
.GetBufferSize
= NULL
;
714 if (!_mesa_initialize_context(&osmesa
->mesa
,
717 sharelist
? &sharelist
->mesa
718 : (struct gl_context
*) NULL
,
720 _mesa_destroy_visual( osmesa
->gl_visual
);
725 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
726 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
727 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
728 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
729 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
730 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
732 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
733 if (!osmesa
->gl_buffer
) {
734 _mesa_destroy_visual( osmesa
->gl_visual
);
735 _mesa_free_context_data( &osmesa
->mesa
);
740 /* Create depth/stencil/accum buffers. We'll create the color
741 * buffer later in OSMesaMakeCurrent().
743 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
744 GL_FALSE
, /* color */
745 osmesa
->gl_visual
->haveDepthBuffer
,
746 osmesa
->gl_visual
->haveStencilBuffer
,
747 osmesa
->gl_visual
->haveAccumBuffer
,
748 GL_FALSE
, /* alpha */
749 GL_FALSE
/* aux */ );
751 osmesa
->format
= format
;
752 osmesa
->userRowLength
= 0;
753 osmesa
->yup
= GL_TRUE
;
759 _mesa_meta_init(&osmesa
->mesa
);
761 /* Initialize the software rasterizer and helper modules. */
763 struct gl_context
*ctx
= &osmesa
->mesa
;
767 if (!_swrast_CreateContext( ctx
) ||
768 !_vbo_CreateContext( ctx
) ||
769 !_tnl_CreateContext( ctx
) ||
770 !_swsetup_CreateContext( ctx
)) {
771 _mesa_destroy_visual(osmesa
->gl_visual
);
772 _mesa_free_context_data(ctx
);
777 _swsetup_Wakeup( ctx
);
779 /* use default TCL pipeline */
780 tnl
= TNL_CONTEXT(ctx
);
781 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
783 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
784 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
786 /* Extend the software rasterizer with our optimized line and triangle
789 swrast
= SWRAST_CONTEXT( ctx
);
790 swrast
->choose_line
= osmesa_choose_line
;
791 swrast
->choose_triangle
= osmesa_choose_triangle
;
793 _mesa_compute_version(ctx
);
795 /* Exec table initialization requires the version to be computed */
796 _mesa_initialize_exec_table(ctx
);
797 _mesa_initialize_vbo_vtxfmt(ctx
);
805 * Destroy an Off-Screen Mesa rendering context.
807 * \param osmesa the context to destroy
809 GLAPI
void GLAPIENTRY
810 OSMesaDestroyContext( OSMesaContext osmesa
)
814 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
816 _mesa_meta_free( &osmesa
->mesa
);
818 _swsetup_DestroyContext( &osmesa
->mesa
);
819 _tnl_DestroyContext( &osmesa
->mesa
);
820 _vbo_DestroyContext( &osmesa
->mesa
);
821 _swrast_DestroyContext( &osmesa
->mesa
);
823 _mesa_destroy_visual( osmesa
->gl_visual
);
824 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
826 _mesa_free_context_data( &osmesa
->mesa
);
833 * Bind an OSMesaContext to an image buffer. The image buffer is just a
834 * block of memory which the client provides. Its size must be at least
835 * as large as width*height*sizeof(type). Its address should be a multiple
836 * of 4 if using RGBA mode.
838 * Image data is stored in the order of glDrawPixels: row-major order
839 * with the lower-left image pixel stored in the first array position
840 * (ie. bottom-to-top).
842 * If the context's viewport hasn't been initialized yet, it will now be
843 * initialized to (0,0,width,height).
845 * Input: osmesa - the rendering context
846 * buffer - the image buffer memory
847 * type - data type for pixel components
848 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
849 * are supported. But if Mesa's been compiled with CHAN_BITS==16
850 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
851 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
852 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
853 * width, height - size of image buffer in pixels, at least 1
854 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
855 * invalid buffer address, invalid type, width<1, height<1,
856 * width>internal limit or height>internal limit.
858 GLAPI GLboolean GLAPIENTRY
859 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
860 GLsizei width
, GLsizei height
)
862 if (!osmesa
|| !buffer
||
863 width
< 1 || height
< 1 ||
864 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
868 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
873 if (!(type
== GL_UNSIGNED_BYTE
||
874 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
875 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
876 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
881 osmesa_update_state( &osmesa
->mesa
, 0 );
883 /* Call this periodically to detect when the user has begun using
884 * GL rendering from multiple threads.
886 _glapi_check_multithread();
889 /* Create a front/left color buffer which wraps the user-provided buffer.
890 * There is no back color buffer.
891 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
892 * doesn't match what Mesa was compiled for (CHAN_BITS) the
893 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
894 * that converts rendering from CHAN_BITS to the user-requested channel
898 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
899 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
900 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
902 assert(osmesa
->srb
->Base
.RefCount
== 2);
905 osmesa
->DataType
= type
;
907 /* Set renderbuffer fields. Set width/height = 0 to force
908 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
910 osmesa
->srb
->Buffer
= buffer
;
911 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
913 /* Set the framebuffer's size. This causes the
914 * osmesa_renderbuffer_storage() function to get called.
916 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
917 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
919 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
921 /* Remove renderbuffer attachment, then re-add. This installs the
922 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
924 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
925 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
929 /* this updates the visual's red/green/blue/alphaBits fields */
930 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
932 /* update the framebuffer size */
933 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
940 GLAPI OSMesaContext GLAPIENTRY
941 OSMesaGetCurrentContext( void )
943 struct gl_context
*ctx
= _mesa_get_current_context();
945 return (OSMesaContext
) ctx
;
952 GLAPI
void GLAPIENTRY
953 OSMesaPixelStore( GLint pname
, GLint value
)
955 OSMesaContext osmesa
= OSMesaGetCurrentContext();
958 case OSMESA_ROW_LENGTH
:
960 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
961 "OSMesaPixelStore(value)" );
964 osmesa
->userRowLength
= value
;
967 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
970 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
974 compute_row_addresses( osmesa
);
978 GLAPI
void GLAPIENTRY
979 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
981 OSMesaContext osmesa
= OSMesaGetCurrentContext();
985 if (osmesa
->gl_buffer
)
986 *value
= osmesa
->gl_buffer
->Width
;
991 if (osmesa
->gl_buffer
)
992 *value
= osmesa
->gl_buffer
->Height
;
997 *value
= osmesa
->format
;
1000 /* current color buffer's data type */
1001 *value
= osmesa
->DataType
;
1003 case OSMESA_ROW_LENGTH
:
1004 *value
= osmesa
->userRowLength
;
1007 *value
= osmesa
->yup
;
1009 case OSMESA_MAX_WIDTH
:
1010 *value
= SWRAST_MAX_WIDTH
;
1012 case OSMESA_MAX_HEIGHT
:
1013 *value
= SWRAST_MAX_HEIGHT
;
1016 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1023 * Return the depth buffer associated with an OSMesa context.
1024 * Input: c - the OSMesa context
1025 * Output: width, height - size of buffer in pixels
1026 * bytesPerValue - bytes per depth value (2 or 4)
1027 * buffer - pointer to depth buffer values
1028 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1030 GLAPI GLboolean GLAPIENTRY
1031 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1032 GLint
*bytesPerValue
, void **buffer
)
1034 struct swrast_renderbuffer
*srb
= NULL
;
1037 srb
= swrast_renderbuffer(c
->gl_buffer
->
1038 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1040 if (!srb
|| !srb
->Buffer
) {
1048 *width
= srb
->Base
.Width
;
1049 *height
= srb
->Base
.Height
;
1050 if (c
->gl_visual
->depthBits
<= 16)
1051 *bytesPerValue
= sizeof(GLushort
);
1053 *bytesPerValue
= sizeof(GLuint
);
1054 *buffer
= (void *) srb
->Buffer
;
1061 * Return the color buffer associated with an OSMesa context.
1062 * Input: c - the OSMesa context
1063 * Output: width, height - size of buffer in pixels
1064 * format - the pixel format (OSMESA_FORMAT)
1065 * buffer - pointer to color buffer values
1066 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1068 GLAPI GLboolean GLAPIENTRY
1069 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1070 GLint
*height
, GLint
*format
, void **buffer
)
1072 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1073 *width
= osmesa
->srb
->Base
.Width
;
1074 *height
= osmesa
->srb
->Base
.Height
;
1075 *format
= osmesa
->format
;
1076 *buffer
= (void *) osmesa
->srb
->Buffer
;
1089 struct name_function
1092 OSMESAproc Function
;
1095 static struct name_function functions
[] = {
1096 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1097 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1098 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1099 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1100 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1101 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1102 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1103 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1104 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1105 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1106 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1111 GLAPI OSMESAproc GLAPIENTRY
1112 OSMesaGetProcAddress( const char *funcName
)
1115 for (i
= 0; functions
[i
].Name
; i
++) {
1116 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1117 return functions
[i
].Function
;
1119 return _glapi_get_proc_address(funcName
);
1123 GLAPI
void GLAPIENTRY
1124 OSMesaColorClamp(GLboolean enable
)
1126 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1128 if (enable
== GL_TRUE
) {
1129 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1132 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1138 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1139 * libglapi.a. We need to define them here.
1141 #ifdef GLX_INDIRECT_RENDERING
1143 #define GL_GLEXT_PROTOTYPES
1145 #include "glapi/glapi.h"
1146 #include "glapi/glapitable.h"
1148 #if defined(USE_MGL_NAMESPACE)
1149 #define NAME(func) mgl##func
1151 #define NAME(func) gl##func
1154 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1155 GET_DISPATCH()->FUNC ARGS
1157 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1158 return GET_DISPATCH()->FUNC ARGS
1160 /* skip normal ones */
1161 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1162 #include "glapi/glapitemp.h"
1164 #endif /* GLX_INDIRECT_RENDERING */