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.
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
->DrawBuffer
&&
201 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
202 /* the special-case line functions in this file don't work
203 * for float color channels.
208 if (ctx
->RenderMode
!= GL_RENDER
||
209 ctx
->Line
.SmoothFlag
||
210 ctx
->Texture
._EnabledUnits
||
211 ctx
->Light
.ShadeModel
!= GL_FLAT
||
212 ctx
->Line
.Width
!= 1.0F
||
213 ctx
->Line
.StippleFlag
||
214 ctx
->Line
.SmoothFlag
) {
218 if (osmesa
->format
!= OSMESA_RGBA
&&
219 osmesa
->format
!= OSMESA_BGRA
&&
220 osmesa
->format
!= OSMESA_ARGB
) {
224 if (swrast
->_RasterMask
== DEPTH_BIT
225 && ctx
->Depth
.Func
== GL_LESS
226 && ctx
->Depth
.Mask
== GL_TRUE
227 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
228 return flat_rgba_z_line
;
231 if (swrast
->_RasterMask
== 0) {
232 return flat_rgba_line
;
235 return (swrast_line_func
) NULL
;
239 /**********************************************************************/
240 /***** Optimized triangle rendering *****/
241 /**********************************************************************/
245 * Smooth-shaded, z-less triangle, RGBA color.
247 #define NAME smooth_rgba_z_triangle
249 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
251 #define INTERP_ALPHA 1
253 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
254 #define RENDER_SPAN( span ) { \
256 GLchan *img = PIXELADDR4(span.x, span.y); \
257 for (i = 0; i < span.end; i++, img += 4) { \
258 const GLuint z = FixedToDepth(span.z); \
260 PACK_RGBA(img, FixedToChan(span.red), \
261 FixedToChan(span.green), FixedToChan(span.blue), \
262 FixedToChan(span.alpha)); \
265 span.red += span.redStep; \
266 span.green += span.greenStep; \
267 span.blue += span.blueStep; \
268 span.alpha += span.alphaStep; \
269 span.z += span.zStep; \
272 #include "swrast/s_tritemp.h"
277 * Flat-shaded, z-less triangle, RGBA color.
279 #define NAME flat_rgba_z_triangle
281 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
283 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
285 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
286 v2->color[2], v2->color[3]);
288 #define RENDER_SPAN( span ) { \
290 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
291 for (i = 0; i < span.end; i++) { \
292 const GLuint z = FixedToDepth(span.z); \
297 span.z += span.zStep; \
301 #include "swrast/s_tritemp.h"
306 * Return pointer to an optimized triangle function if possible.
308 static swrast_tri_func
309 osmesa_choose_triangle_function( struct gl_context
*ctx
)
311 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
312 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
314 if (ctx
->DrawBuffer
&&
315 ctx
->DrawBuffer
->Visual
.redBits
== 32) {
316 /* the special-case triangle functions in this file don't work
317 * for float color channels.
322 if (ctx
->RenderMode
!= GL_RENDER
||
323 ctx
->Polygon
.SmoothFlag
||
324 ctx
->Polygon
.StippleFlag
||
325 ctx
->Texture
._EnabledUnits
) {
329 if (osmesa
->format
!= OSMESA_RGBA
&&
330 osmesa
->format
!= OSMESA_BGRA
&&
331 osmesa
->format
!= OSMESA_ARGB
) {
335 if (ctx
->Polygon
.CullFlag
&&
336 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
340 if (swrast
->_RasterMask
== DEPTH_BIT
&&
341 ctx
->Depth
.Func
== GL_LESS
&&
342 ctx
->Depth
.Mask
== GL_TRUE
&&
343 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
344 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
345 return smooth_rgba_z_triangle
;
348 return flat_rgba_z_triangle
;
357 /* Override for the swrast triangle-selection function. Try to use one
358 * of our internal triangle functions, otherwise fall back to the
359 * standard swrast functions.
362 osmesa_choose_triangle( struct gl_context
*ctx
)
364 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
366 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
367 if (!swrast
->Triangle
)
368 _swrast_choose_triangle( ctx
);
372 osmesa_choose_line( struct gl_context
*ctx
)
374 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
376 swrast
->Line
= osmesa_choose_line_function( ctx
);
378 _swrast_choose_line( ctx
);
384 * Recompute the values of the context's rowaddr array.
387 compute_row_addresses( OSMesaContext osmesa
)
389 GLint bytesPerRow
, i
;
390 GLubyte
*origin
= (GLubyte
*) osmesa
->srb
->Buffer
;
391 GLint rowlength
; /* in pixels */
392 GLint height
= osmesa
->srb
->Base
.Height
;
394 if (osmesa
->userRowLength
)
395 rowlength
= osmesa
->userRowLength
;
397 rowlength
= osmesa
->srb
->Base
.Width
;
399 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->srb
->Base
.Format
);
402 /* Y=0 is bottom line of window */
403 for (i
= 0; i
< height
; i
++) {
404 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
408 /* Y=0 is top line of window */
409 for (i
= 0; i
< height
; i
++) {
410 GLint j
= height
- i
- 1;
411 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
419 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
422 osmesa_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
424 _mesa_delete_renderbuffer(ctx
, rb
);
429 * Allocate renderbuffer storage. We don't actually allocate any storage
430 * since we're using a user-provided buffer.
431 * Just set up all the gl_renderbuffer methods.
434 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
435 GLenum internalFormat
, GLuint width
, GLuint height
)
437 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
439 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
440 (void) internalFormat
;
442 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
444 * XXX There aren't Mesa formats for all the possible combinations here!
445 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
447 * XXX The 8-bit/channel formats should all be OK.
449 if (osmesa
->format
== OSMESA_RGBA
) {
450 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
451 if (_mesa_little_endian())
452 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
454 rb
->Format
= MESA_FORMAT_RGBA8888
;
456 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
457 rb
->Format
= MESA_FORMAT_RGBA_16
;
460 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
463 else if (osmesa
->format
== OSMESA_BGRA
) {
464 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
465 if (_mesa_little_endian())
466 rb
->Format
= MESA_FORMAT_ARGB8888
;
468 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
470 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
471 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
472 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
475 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
476 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
479 else if (osmesa
->format
== OSMESA_ARGB
) {
480 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
481 if (_mesa_little_endian())
482 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
484 rb
->Format
= MESA_FORMAT_ARGB8888
;
486 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
487 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
488 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
491 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
492 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
495 else if (osmesa
->format
== OSMESA_RGB
) {
496 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
497 rb
->Format
= MESA_FORMAT_RGB888
;
499 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
500 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
501 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
504 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
505 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
508 else if (osmesa
->format
== OSMESA_BGR
) {
509 if (osmesa
->DataType
== GL_UNSIGNED_BYTE
) {
510 rb
->Format
= MESA_FORMAT_BGR888
;
512 else if (osmesa
->DataType
== GL_UNSIGNED_SHORT
) {
513 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
514 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
517 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
518 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
521 else if (osmesa
->format
== OSMESA_RGB_565
) {
522 ASSERT(osmesa
->DataType
== GL_UNSIGNED_BYTE
);
523 rb
->Format
= MESA_FORMAT_RGB565
;
526 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
532 compute_row_addresses( osmesa
);
539 * Allocate a new renderbuffer to describe the user-provided color buffer.
541 static struct swrast_renderbuffer
*
542 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
544 const GLuint name
= 0;
545 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
548 _mesa_init_renderbuffer(&srb
->Base
, name
);
550 srb
->Base
.ClassID
= OSMESA_RENDERBUFFER_CLASS
;
551 srb
->Base
.RefCount
= 1;
552 srb
->Base
.Delete
= osmesa_delete_renderbuffer
;
553 srb
->Base
.AllocStorage
= osmesa_renderbuffer_storage
;
555 srb
->Base
.InternalFormat
= GL_RGBA
;
556 srb
->Base
._BaseFormat
= GL_RGBA
;
566 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
567 struct gl_renderbuffer
*rb
,
568 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
570 GLubyte
**mapOut
, GLint
*rowStrideOut
)
572 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
574 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
575 /* this is an OSMesa renderbuffer which wraps user memory */
576 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
577 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
578 GLint rowStride
; /* in bytes */
580 if (osmesa
->userRowLength
)
581 rowStride
= osmesa
->userRowLength
* bpp
;
583 rowStride
= rb
->Width
* bpp
;
586 /* Y=0 is top line of window */
587 y
= rb
->Height
- y
- 1;
588 *rowStrideOut
= -rowStride
;
591 *rowStrideOut
= rowStride
;
594 *mapOut
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
597 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
598 mapOut
, rowStrideOut
);
604 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
606 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
610 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
615 /**********************************************************************/
616 /***** Public Functions *****/
617 /**********************************************************************/
621 * Create an Off-Screen Mesa rendering context. The only attribute needed is
622 * an RGBA vs Color-Index mode flag.
624 * Input: format - Must be GL_RGBA
625 * sharelist - specifies another OSMesaContext with which to share
626 * display lists. NULL indicates no sharing.
627 * Return: an OSMesaContext or 0 if error
629 GLAPI OSMesaContext GLAPIENTRY
630 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
632 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
641 * Create context and specify size of ancillary buffers.
643 GLAPI OSMesaContext GLAPIENTRY
644 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
645 GLint accumBits
, OSMesaContext sharelist
)
647 OSMesaContext osmesa
;
648 struct dd_function_table functions
;
649 GLint rind
, gind
, bind
, aind
;
650 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
652 rind
= gind
= bind
= aind
= 0;
653 if (format
==OSMESA_RGBA
) {
655 greenBits
= CHAN_BITS
;
656 blueBits
= CHAN_BITS
;
657 alphaBits
= CHAN_BITS
;
663 else if (format
==OSMESA_BGRA
) {
665 greenBits
= CHAN_BITS
;
666 blueBits
= CHAN_BITS
;
667 alphaBits
= CHAN_BITS
;
673 else if (format
==OSMESA_ARGB
) {
675 greenBits
= CHAN_BITS
;
676 blueBits
= CHAN_BITS
;
677 alphaBits
= CHAN_BITS
;
683 else if (format
==OSMESA_RGB
) {
685 greenBits
= CHAN_BITS
;
686 blueBits
= CHAN_BITS
;
692 else if (format
==OSMESA_BGR
) {
694 greenBits
= CHAN_BITS
;
695 blueBits
= CHAN_BITS
;
701 #if CHAN_TYPE == GL_UNSIGNED_BYTE
702 else if (format
==OSMESA_RGB_565
) {
707 rind
= 0; /* not used */
716 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
718 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
719 GL_FALSE
, /* stereo */
729 alphaBits
? accumBits
: 0,
732 if (!osmesa
->gl_visual
) {
737 /* Initialize device driver function table */
738 _mesa_init_driver_functions(&functions
);
739 /* override with our functions */
740 functions
.GetString
= get_string
;
741 functions
.UpdateState
= osmesa_update_state
;
743 if (!_mesa_initialize_context(&osmesa
->mesa
,
746 sharelist
? &sharelist
->mesa
747 : (struct gl_context
*) NULL
,
749 _mesa_destroy_visual( osmesa
->gl_visual
);
754 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
756 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
757 if (!osmesa
->gl_buffer
) {
758 _mesa_destroy_visual( osmesa
->gl_visual
);
759 _mesa_free_context_data( &osmesa
->mesa
);
764 /* Create depth/stencil/accum buffers. We'll create the color
765 * buffer later in OSMesaMakeCurrent().
767 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
768 GL_FALSE
, /* color */
769 osmesa
->gl_visual
->haveDepthBuffer
,
770 osmesa
->gl_visual
->haveStencilBuffer
,
771 osmesa
->gl_visual
->haveAccumBuffer
,
772 GL_FALSE
, /* alpha */
773 GL_FALSE
/* aux */ );
775 osmesa
->format
= format
;
776 osmesa
->userRowLength
= 0;
777 osmesa
->yup
= GL_TRUE
;
783 _mesa_meta_init(&osmesa
->mesa
);
785 /* Initialize the software rasterizer and helper modules. */
787 struct gl_context
*ctx
= &osmesa
->mesa
;
791 if (!_swrast_CreateContext( ctx
) ||
792 !_vbo_CreateContext( ctx
) ||
793 !_tnl_CreateContext( ctx
) ||
794 !_swsetup_CreateContext( ctx
)) {
795 _mesa_destroy_visual(osmesa
->gl_visual
);
796 _mesa_free_context_data(ctx
);
801 _swsetup_Wakeup( ctx
);
803 /* use default TCL pipeline */
804 tnl
= TNL_CONTEXT(ctx
);
805 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
807 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
808 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
810 ctx
->Driver
.GenerateMipmap
= _mesa_generate_mipmap
;
812 /* Extend the software rasterizer with our optimized line and triangle
815 swrast
= SWRAST_CONTEXT( ctx
);
816 swrast
->choose_line
= osmesa_choose_line
;
817 swrast
->choose_triangle
= osmesa_choose_triangle
;
819 _mesa_compute_version(ctx
);
821 /* Exec table initialization requires the version to be computed */
822 _mesa_initialize_dispatch_tables(ctx
);
823 _mesa_initialize_vbo_vtxfmt(ctx
);
831 * Destroy an Off-Screen Mesa rendering context.
833 * \param osmesa the context to destroy
835 GLAPI
void GLAPIENTRY
836 OSMesaDestroyContext( OSMesaContext osmesa
)
840 _mesa_reference_renderbuffer((struct gl_renderbuffer
**) &osmesa
->srb
, NULL
);
842 _mesa_meta_free( &osmesa
->mesa
);
844 _swsetup_DestroyContext( &osmesa
->mesa
);
845 _tnl_DestroyContext( &osmesa
->mesa
);
846 _vbo_DestroyContext( &osmesa
->mesa
);
847 _swrast_DestroyContext( &osmesa
->mesa
);
849 _mesa_destroy_visual( osmesa
->gl_visual
);
850 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
852 _mesa_free_context_data( &osmesa
->mesa
);
859 * Bind an OSMesaContext to an image buffer. The image buffer is just a
860 * block of memory which the client provides. Its size must be at least
861 * as large as width*height*sizeof(type). Its address should be a multiple
862 * of 4 if using RGBA mode.
864 * Image data is stored in the order of glDrawPixels: row-major order
865 * with the lower-left image pixel stored in the first array position
866 * (ie. bottom-to-top).
868 * If the context's viewport hasn't been initialized yet, it will now be
869 * initialized to (0,0,width,height).
871 * Input: osmesa - the rendering context
872 * buffer - the image buffer memory
873 * type - data type for pixel components
874 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
875 * are supported. But if Mesa's been compiled with CHAN_BITS==16
876 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
877 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
878 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
879 * width, height - size of image buffer in pixels, at least 1
880 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
881 * invalid buffer address, invalid type, width<1, height<1,
882 * width>internal limit or height>internal limit.
884 GLAPI GLboolean GLAPIENTRY
885 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
886 GLsizei width
, GLsizei height
)
888 if (!osmesa
|| !buffer
||
889 width
< 1 || height
< 1 ||
890 width
> SWRAST_MAX_WIDTH
|| height
> SWRAST_MAX_HEIGHT
) {
894 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
899 if (!(type
== GL_UNSIGNED_BYTE
||
900 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
901 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
902 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
907 osmesa_update_state( &osmesa
->mesa
, 0 );
909 /* Call this periodically to detect when the user has begun using
910 * GL rendering from multiple threads.
912 _glapi_check_multithread();
915 /* Create a front/left color buffer which wraps the user-provided buffer.
916 * There is no back color buffer.
917 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
918 * doesn't match what Mesa was compiled for (CHAN_BITS) the
919 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
920 * that converts rendering from CHAN_BITS to the user-requested channel
924 osmesa
->srb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
925 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
926 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
928 assert(osmesa
->srb
->Base
.RefCount
== 2);
931 osmesa
->DataType
= type
;
933 /* Set renderbuffer fields. Set width/height = 0 to force
934 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
936 osmesa
->srb
->Buffer
= buffer
;
937 osmesa
->srb
->Base
.Width
= osmesa
->srb
->Base
.Height
= 0;
939 /* Set the framebuffer's size. This causes the
940 * osmesa_renderbuffer_storage() function to get called.
942 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
944 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
946 /* Remove renderbuffer attachment, then re-add. This installs the
947 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
949 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
950 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
954 /* this updates the visual's red/green/blue/alphaBits fields */
955 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
957 /* update the framebuffer size */
958 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
965 GLAPI OSMesaContext GLAPIENTRY
966 OSMesaGetCurrentContext( void )
968 struct gl_context
*ctx
= _mesa_get_current_context();
970 return (OSMesaContext
) ctx
;
977 GLAPI
void GLAPIENTRY
978 OSMesaPixelStore( GLint pname
, GLint value
)
980 OSMesaContext osmesa
= OSMesaGetCurrentContext();
983 case OSMESA_ROW_LENGTH
:
985 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
986 "OSMesaPixelStore(value)" );
989 osmesa
->userRowLength
= value
;
992 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
995 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
999 compute_row_addresses( osmesa
);
1003 GLAPI
void GLAPIENTRY
1004 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1006 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1010 if (osmesa
->gl_buffer
)
1011 *value
= osmesa
->gl_buffer
->Width
;
1016 if (osmesa
->gl_buffer
)
1017 *value
= osmesa
->gl_buffer
->Height
;
1022 *value
= osmesa
->format
;
1025 /* current color buffer's data type */
1026 *value
= osmesa
->DataType
;
1028 case OSMESA_ROW_LENGTH
:
1029 *value
= osmesa
->userRowLength
;
1032 *value
= osmesa
->yup
;
1034 case OSMESA_MAX_WIDTH
:
1035 *value
= SWRAST_MAX_WIDTH
;
1037 case OSMESA_MAX_HEIGHT
:
1038 *value
= SWRAST_MAX_HEIGHT
;
1041 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1048 * Return the depth buffer associated with an OSMesa context.
1049 * Input: c - the OSMesa context
1050 * Output: width, height - size of buffer in pixels
1051 * bytesPerValue - bytes per depth value (2 or 4)
1052 * buffer - pointer to depth buffer values
1053 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1055 GLAPI GLboolean GLAPIENTRY
1056 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1057 GLint
*bytesPerValue
, void **buffer
)
1059 struct swrast_renderbuffer
*srb
= NULL
;
1062 srb
= swrast_renderbuffer(c
->gl_buffer
->
1063 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1065 if (!srb
|| !srb
->Buffer
) {
1073 *width
= srb
->Base
.Width
;
1074 *height
= srb
->Base
.Height
;
1075 if (c
->gl_visual
->depthBits
<= 16)
1076 *bytesPerValue
= sizeof(GLushort
);
1078 *bytesPerValue
= sizeof(GLuint
);
1079 *buffer
= (void *) srb
->Buffer
;
1086 * Return the color buffer associated with an OSMesa context.
1087 * Input: c - the OSMesa context
1088 * Output: width, height - size of buffer in pixels
1089 * format - the pixel format (OSMESA_FORMAT)
1090 * buffer - pointer to color buffer values
1091 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1093 GLAPI GLboolean GLAPIENTRY
1094 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1095 GLint
*height
, GLint
*format
, void **buffer
)
1097 if (osmesa
->srb
&& osmesa
->srb
->Buffer
) {
1098 *width
= osmesa
->srb
->Base
.Width
;
1099 *height
= osmesa
->srb
->Base
.Height
;
1100 *format
= osmesa
->format
;
1101 *buffer
= (void *) osmesa
->srb
->Buffer
;
1114 struct name_function
1117 OSMESAproc Function
;
1120 static struct name_function functions
[] = {
1121 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1122 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1123 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1124 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1125 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1126 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1127 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1128 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1129 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1130 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1131 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1136 GLAPI OSMESAproc GLAPIENTRY
1137 OSMesaGetProcAddress( const char *funcName
)
1140 for (i
= 0; functions
[i
].Name
; i
++) {
1141 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1142 return functions
[i
].Function
;
1144 return _glapi_get_proc_address(funcName
);
1148 GLAPI
void GLAPIENTRY
1149 OSMesaColorClamp(GLboolean enable
)
1151 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1153 if (enable
== GL_TRUE
) {
1154 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1157 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1163 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1164 * libglapi.a. We need to define them here.
1166 #ifdef GLX_INDIRECT_RENDERING
1168 #define GL_GLEXT_PROTOTYPES
1170 #include "glapi/glapi.h"
1171 #include "glapi/glapitable.h"
1173 #if defined(USE_MGL_NAMESPACE)
1174 #define NAME(func) mgl##func
1176 #define NAME(func) gl##func
1179 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1180 GET_DISPATCH()->FUNC ARGS
1182 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1183 return GET_DISPATCH()->FUNC ARGS
1185 /* skip normal ones */
1186 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1187 #include "glapi/glapitemp.h"
1189 #endif /* GLX_INDIRECT_RENDERING */