1 /* $Id: osmesa.c,v 1.103 2003/04/01 17:28:55 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Off-Screen Mesa rendering / Rendering into client memory space
31 * Note on thread safety: this driver is thread safe. All
32 * functions are reentrant. The notion of current context is
33 * managed by the core _mesa_make_current() and _mesa_get_current_context()
34 * functions. Those functions are thread-safe.
39 #include "GL/osmesa.h"
44 #include "extensions.h"
49 #include "texformat.h"
52 #include "array_cache/acache.h"
53 #include "swrast/swrast.h"
54 #include "swrast_setup/swrast_setup.h"
55 #include "swrast/s_context.h"
56 #include "swrast/s_depth.h"
57 #include "swrast/s_lines.h"
58 #include "swrast/s_triangle.h"
60 #include "tnl/t_context.h"
61 #include "tnl/t_pipeline.h"
66 * This is the OS/Mesa context struct.
67 * Notice how it includes a GLcontext. By doing this we're mimicking
68 * C++ inheritance/derivation.
69 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
72 struct osmesa_context
{
73 GLcontext mesa
; /* The core GL/Mesa context */
74 GLvisual
*gl_visual
; /* Describes the buffers */
75 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
76 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
77 void *buffer
; /* the image buffer */
78 GLint width
, height
; /* size of image buffer */
79 GLint rowlength
; /* number of pixels per row */
80 GLint userRowLength
; /* user-specified number of pixels per row */
81 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
83 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
84 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
85 GLboolean yup
; /* TRUE -> Y increases upward */
86 /* FALSE -> Y increases downward */
90 /* Just cast, since we're using structure containment */
91 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
95 /**********************************************************************/
96 /*** Private Device Driver Functions ***/
97 /**********************************************************************/
100 static const GLubyte
*
101 get_string( GLcontext
*ctx
, GLenum name
)
107 return (const GLubyte
*) "Mesa OffScreen32";
108 #elif CHAN_BITS == 16
109 return (const GLubyte
*) "Mesa OffScreen16";
111 return (const GLubyte
*) "Mesa OffScreen";
120 osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
122 /* easy - just propogate */
123 _swrast_InvalidateState( ctx
, new_state
);
124 _swsetup_InvalidateState( ctx
, new_state
);
125 _ac_InvalidateState( ctx
, new_state
);
126 _tnl_InvalidateState( ctx
, new_state
);
131 set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
133 /* separate read buffer not supported */
134 ASSERT(buffer
== ctx
->DrawBuffer
);
135 ASSERT(bufferBit
== FRONT_LEFT_BIT
);
140 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
142 /* don't use GET_CURRENT_CONTEXT(ctx) here - it's a problem on Windows */
143 GLcontext
*ctx
= (GLcontext
*) _glapi_get_context();
146 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
147 *width
= osmesa
->width
;
148 *height
= osmesa
->height
;
154 clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
155 GLint x
, GLint y
, GLint width
, GLint height
)
157 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
158 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
160 /* sanity check - we only have a front-left buffer */
161 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
|
163 DD_BACK_RIGHT_BIT
)) == 0);
165 /* use optimized clear for common cases (clear whole buffer to black) */
166 if (mask
& DD_FRONT_LEFT_BIT
) {
167 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
168 if (ctx
->Color
.ClearIndex
== 0 &&
169 ctx
->Color
.IndexMask
== (GLuint
) ~0 &&
170 osmesa
->rowlength
== osmesa
->width
&&
172 /* clear whole buffer to zeros */
173 _mesa_bzero(osmesa
->buffer
,
174 osmesa
->width
* osmesa
->height
* sizeof(GLchan
));
175 mask
&= ~DD_FRONT_LEFT_BIT
;
180 if (*colorMask
== 0xffffffff &&
181 ctx
->Color
.ClearColor
[0] == 0.0F
&&
182 ctx
->Color
.ClearColor
[1] == 0.0F
&&
183 ctx
->Color
.ClearColor
[2] == 0.0F
&&
184 ctx
->Color
.ClearColor
[3] == 0.0F
&&
185 osmesa
->rowlength
== osmesa
->width
&&
188 /* clear whole buffer to black */
189 if (osmesa
->format
== OSMESA_RGBA
||
190 osmesa
->format
== OSMESA_BGRA
||
191 osmesa
->format
== OSMESA_ARGB
)
192 bytesPerPixel
= 4 * sizeof(GLchan
);
193 else if (osmesa
->format
== OSMESA_RGB
||
194 osmesa
->format
== OSMESA_BGR
)
195 bytesPerPixel
= 3 * sizeof(GLchan
);
196 else if (osmesa
->format
== OSMESA_RGB_565
)
197 bytesPerPixel
= sizeof(GLushort
);
199 _mesa_problem(ctx
, "bad pixel format in osmesa_clear()");
202 _mesa_bzero(osmesa
->buffer
,
203 bytesPerPixel
* osmesa
->width
* osmesa
->height
);
204 mask
&= ~DD_FRONT_LEFT_BIT
;
210 /* software fallback (spans) for everything else. */
211 _swrast_Clear(ctx
, mask
, all
, x
, y
, width
, height
);
216 /**********************************************************************/
217 /***** Read/write spans/arrays of pixels *****/
218 /**********************************************************************/
222 #define NAME(PREFIX) PREFIX##_RGBA
224 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
225 #define INIT_PIXEL_PTR(P, X, Y) \
226 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
227 #define INC_PIXEL_PTR(P) P += 4
228 #if CHAN_TYPE == GL_FLOAT
229 #define STORE_RGB_PIXEL(P, R, G, B) \
230 P[0] = MAX2((R), 0.0F); \
231 P[1] = MAX2((G), 0.0F); \
232 P[2] = MAX2((B), 0.0F); \
234 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
235 P[0] = MAX2((R), 0.0F); \
236 P[1] = MAX2((G), 0.0F); \
237 P[2] = MAX2((B), 0.0F); \
238 P[3] = CLAMP((A), 0.0F, CHAN_MAXF)
240 #define STORE_RGB_PIXEL(P, R, G, B) \
241 P[0] = R; P[1] = G; P[2] = B; P[3] = CHAN_MAX
242 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
243 P[0] = R; P[1] = G; P[2] = B; P[3] = A
245 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
246 R = P[0]; G = P[1]; B = P[2]; A = P[3]
247 #include "swrast/s_spantemp.h"
250 #define NAME(PREFIX) PREFIX##_BGRA
252 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
253 #define INIT_PIXEL_PTR(P, X, Y) \
254 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
255 #define INC_PIXEL_PTR(P) P += 4
256 #define STORE_RGB_PIXEL(P, R, G, B) \
257 P[2] = R; P[1] = G; P[0] = B; P[3] = CHAN_MAX
258 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
259 P[2] = R; P[1] = G; P[0] = B; P[3] = A
260 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
261 R = P[2]; G = P[1]; B = P[0]; A = P[3]
262 #include "swrast/s_spantemp.h"
265 #define NAME(PREFIX) PREFIX##_ARGB
267 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
268 #define INIT_PIXEL_PTR(P, X, Y) \
269 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
270 #define INC_PIXEL_PTR(P) P += 4
271 #define STORE_RGB_PIXEL(P, R, G, B) \
272 P[1] = R; P[2] = G; P[3] = B; P[0] = CHAN_MAX
273 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
274 P[1] = R; P[2] = G; P[3] = B; P[0] = A
275 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
276 R = P[1]; G = P[2]; B = P[3]; A = P[0]
277 #include "swrast/s_spantemp.h"
280 #define NAME(PREFIX) PREFIX##_RGB
282 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
283 #define INIT_PIXEL_PTR(P, X, Y) \
284 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
285 #define INC_PIXEL_PTR(P) P += 4
286 #define STORE_RGB_PIXEL(P, R, G, B) \
287 P[0] = R; P[1] = G; P[2] = B
288 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
289 P[0] = R; P[1] = G; P[2] = B
290 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
291 R = P[0]; G = P[1]; B = P[2]; A = CHAN_MAX
292 #include "swrast/s_spantemp.h"
295 #define NAME(PREFIX) PREFIX##_BGR
297 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
298 #define INIT_PIXEL_PTR(P, X, Y) \
299 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
300 #define INC_PIXEL_PTR(P) P += 4
301 #define STORE_RGB_PIXEL(P, R, G, B) \
302 P[0] = B; P[1] = G; P[2] = R
303 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
304 P[0] = B; P[1] = G; P[2] = R
305 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
306 B = P[0]; G = P[1]; R = P[2]; A = CHAN_MAX
307 #include "swrast/s_spantemp.h"
310 #if CHAN_TYPE == GL_UNSIGNED_BYTE
311 #define NAME(PREFIX) PREFIX##_RGB_565
313 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
314 #define INIT_PIXEL_PTR(P, X, Y) \
315 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
316 #define INC_PIXEL_PTR(P) P += 1
317 #define STORE_RGB_PIXEL(P, R, G, B) \
318 *P = ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
319 #define STORE_RGBA_PIXEL(P, R, G, B, A) \
320 *P = ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
321 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
322 R = ( (((*P) >> 8) & 0xf8) | (((*P) >> 11) & 0x7) ); \
323 G = ( (((*P) >> 3) & 0xfc) | (((*P) >> 5) & 0x3) ); \
324 B = ( (((*P) << 3) & 0xf8) | (((*P) ) & 0x7) ); \
326 #include "swrast/s_spantemp.h"
330 #define NAME(PREFIX) PREFIX##_CI
332 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
333 #define INIT_PIXEL_PTR(P, X, Y) \
334 GLchan *P = osmesa->rowaddr[Y] + (X)
335 #define INC_PIXEL_PTR(P) P += 1
336 #define STORE_CI_PIXEL(P, CI) \
338 #define FETCH_CI_PIXEL(CI, P) \
340 #include "swrast/s_spantemp.h"
343 /**********************************************************************/
344 /***** Optimized line rendering *****/
345 /**********************************************************************/
348 #if CHAN_TYPE == GL_FLOAT
349 #define PACK_RGBA(DST, R, G, B, A) \
351 (DST)[0] = MAX2( R, 0.0F ); \
352 (DST)[1] = MAX2( G, 0.0F ); \
353 (DST)[2] = MAX2( B, 0.0F ); \
354 (DST)[3] = CLAMP(A, 0.0F, CHAN_MAXF);\
357 #define PACK_RGBA(DST, R, G, B, A) \
359 (DST)[osmesa->rInd] = R; \
360 (DST)[osmesa->gInd] = G; \
361 (DST)[osmesa->bInd] = B; \
362 (DST)[osmesa->aInd] = A; \
366 #define PACK_RGB(DST, R, G, B) \
373 #define PACK_BGR(DST, R, G, B) \
380 #define PACK_RGB_565(DST, R, G, B) \
382 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
385 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
386 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
387 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
388 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
390 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
391 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
392 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
393 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
397 * Draw a flat-shaded, RGB line into an osmesa buffer.
399 #define NAME flat_rgba_line
402 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
403 const GLchan *color = vert1->color;
407 GLchan *p = PIXELADDR4(X, Y); \
408 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
412 #include "..\swrast\s_linetemp.h"
414 #include "swrast/s_linetemp.h"
420 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
422 #define NAME flat_rgba_z_line
425 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
427 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
428 const GLchan *color = vert1->color;
433 GLchan *p = PIXELADDR4(X, Y); \
434 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
435 color[BCOMP], color[ACOMP]); \
441 #include "..\swrast\s_linetemp.h"
443 #include "swrast/s_linetemp.h"
449 * Analyze context state to see if we can provide a fast line drawing
450 * function, like those in lines.c. Otherwise, return NULL.
452 static swrast_line_func
453 osmesa_choose_line_function( GLcontext
*ctx
)
455 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
456 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
458 if (CHAN_BITS
!= 8) return NULL
;
459 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
460 if (ctx
->Line
.SmoothFlag
) return NULL
;
461 if (ctx
->Texture
._EnabledUnits
) return NULL
;
462 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
463 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
464 if (ctx
->Line
.StippleFlag
) return NULL
;
465 if (ctx
->Line
.SmoothFlag
) return NULL
;
466 if (osmesa
->format
!= OSMESA_RGBA
&&
467 osmesa
->format
!= OSMESA_BGRA
&&
468 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
470 if (swrast
->_RasterMask
==DEPTH_BIT
471 && ctx
->Depth
.Func
==GL_LESS
472 && ctx
->Depth
.Mask
==GL_TRUE
473 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
474 return (swrast_line_func
) flat_rgba_z_line
;
477 if (swrast
->_RasterMask
== 0) {
478 return (swrast_line_func
) flat_rgba_line
;
481 return (swrast_line_func
) NULL
;
485 /**********************************************************************/
486 /***** Optimized triangle rendering *****/
487 /**********************************************************************/
491 * Smooth-shaded, z-less triangle, RGBA color.
493 #define NAME smooth_rgba_z_triangle
495 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
497 #define INTERP_ALPHA 1
499 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
500 #define RENDER_SPAN( span ) \
502 GLchan *img = PIXELADDR4(span.x, span.y); \
503 for (i = 0; i < span.end; i++, img += 4) { \
504 const GLdepth z = FixedToDepth(span.z); \
506 PACK_RGBA(img, FixedToChan(span.red), \
507 FixedToChan(span.green), FixedToChan(span.blue), \
508 FixedToChan(span.alpha)); \
511 span.red += span.redStep; \
512 span.green += span.greenStep; \
513 span.blue += span.blueStep; \
514 span.alpha += span.alphaStep; \
515 span.z += span.zStep; \
518 #include "..\swrast\s_tritemp.h"
520 #include "swrast/s_tritemp.h"
526 * Flat-shaded, z-less triangle, RGBA color.
528 #define NAME flat_rgba_z_triangle
530 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
532 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
534 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
535 v2->color[2], v2->color[3]);
537 #define RENDER_SPAN( span ) \
539 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
540 for (i = 0; i < span.end; i++) { \
541 const GLdepth z = FixedToDepth(span.z); \
546 span.z += span.zStep; \
549 #include "..\swrast\s_tritemp.h"
551 #include "swrast/s_tritemp.h"
557 * Return pointer to an accelerated triangle function if possible.
559 static swrast_tri_func
560 osmesa_choose_triangle_function( GLcontext
*ctx
)
562 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
563 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
565 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
566 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
567 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
568 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
569 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
570 if (osmesa
->format
!= OSMESA_RGBA
&&
571 osmesa
->format
!= OSMESA_BGRA
&&
572 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
573 if (ctx
->Polygon
.CullFlag
&&
574 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
575 return (swrast_tri_func
) NULL
;
577 if (swrast
->_RasterMask
== DEPTH_BIT
&&
578 ctx
->Depth
.Func
== GL_LESS
&&
579 ctx
->Depth
.Mask
== GL_TRUE
&&
580 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
581 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
582 return (swrast_tri_func
) smooth_rgba_z_triangle
;
585 return (swrast_tri_func
) flat_rgba_z_triangle
;
588 return (swrast_tri_func
) NULL
;
593 /* Override for the swrast triangle-selection function. Try to use one
594 * of our internal triangle functions, otherwise fall back to the
595 * standard swrast functions.
598 osmesa_choose_triangle( GLcontext
*ctx
)
600 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
602 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
603 if (!swrast
->Triangle
)
604 _swrast_choose_triangle( ctx
);
608 osmesa_choose_line( GLcontext
*ctx
)
610 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
612 swrast
->Line
= osmesa_choose_line_function( ctx
);
614 _swrast_choose_line( ctx
);
618 #define OSMESA_NEW_LINE (_NEW_LINE | \
623 _SWRAST_NEW_RASTERMASK)
625 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
630 _SWRAST_NEW_RASTERMASK)
632 /* one-time, per-context initialization */
634 hook_in_driver_functions( GLcontext
*ctx
)
636 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
637 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
638 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
639 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
641 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
643 /* use default TCL pipeline */
644 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
646 ctx
->Driver
.GetString
= get_string
;
647 ctx
->Driver
.UpdateState
= osmesa_update_state
;
648 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
649 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
651 ctx
->Driver
.Accum
= _swrast_Accum
;
652 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
653 ctx
->Driver
.Clear
= clear
; /* uses _swrast_Clear */
654 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
655 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
656 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
657 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
659 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
660 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
661 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
662 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
663 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
664 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
665 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
666 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
668 ctx
->Driver
.CompressedTexImage1D
= _mesa_store_compressed_teximage1d
;
669 ctx
->Driver
.CompressedTexImage2D
= _mesa_store_compressed_teximage2d
;
670 ctx
->Driver
.CompressedTexImage3D
= _mesa_store_compressed_teximage3d
;
671 ctx
->Driver
.CompressedTexSubImage1D
= _mesa_store_compressed_texsubimage1d
;
672 ctx
->Driver
.CompressedTexSubImage2D
= _mesa_store_compressed_texsubimage2d
;
673 ctx
->Driver
.CompressedTexSubImage3D
= _mesa_store_compressed_texsubimage3d
;
675 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
676 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
677 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
678 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
679 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
680 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
681 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
682 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
683 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
685 swdd
->SetBuffer
= set_buffer
;
687 /* RGB(A) span/pixel functions */
688 if (osmesa
->format
== OSMESA_RGB
) {
689 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
690 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
691 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGB
;
692 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
693 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGB
;
694 swdd
->ReadRGBASpan
= read_rgba_span_RGB
;
695 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGB
;
697 else if (osmesa
->format
== OSMESA_BGR
) {
698 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
699 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
700 swdd
->WriteMonoRGBASpan
= write_monorgba_span_BGR
;
701 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
702 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_BGR
;
703 swdd
->ReadRGBASpan
= read_rgba_span_BGR
;
704 swdd
->ReadRGBAPixels
= read_rgba_pixels_BGR
;
706 #if CHAN_TYPE == GL_UNSIGNED_BYTE
707 else if (osmesa
->format
== OSMESA_RGB_565
) {
708 swdd
->WriteRGBASpan
= write_rgba_span_RGB_565
;
709 swdd
->WriteRGBSpan
= write_rgb_span_RGB_565
;
710 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGB_565
;
711 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB_565
;
712 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGB_565
;
713 swdd
->ReadRGBASpan
= read_rgba_span_RGB_565
;
714 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGB_565
;
717 else if (osmesa
->format
== OSMESA_RGBA
) {
718 swdd
->WriteRGBASpan
= write_rgba_span_RGBA
;
719 swdd
->WriteRGBSpan
= write_rgb_span_RGBA
;
720 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGBA
;
721 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGBA
;
722 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGBA
;
723 swdd
->ReadRGBASpan
= read_rgba_span_RGBA
;
724 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGBA
;
726 else if (osmesa
->format
== OSMESA_BGRA
) {
727 swdd
->WriteRGBASpan
= write_rgba_span_BGRA
;
728 swdd
->WriteRGBSpan
= write_rgb_span_BGRA
;
729 swdd
->WriteMonoRGBASpan
= write_monorgba_span_BGRA
;
730 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGRA
;
731 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_BGRA
;
732 swdd
->ReadRGBASpan
= read_rgba_span_BGRA
;
733 swdd
->ReadRGBAPixels
= read_rgba_pixels_BGRA
;
735 else if (osmesa
->format
== OSMESA_ARGB
) {
736 swdd
->WriteRGBASpan
= write_rgba_span_ARGB
;
737 swdd
->WriteRGBSpan
= write_rgb_span_ARGB
;
738 swdd
->WriteMonoRGBASpan
= write_monorgba_span_ARGB
;
739 swdd
->WriteRGBAPixels
= write_rgba_pixels_ARGB
;
740 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_ARGB
;
741 swdd
->ReadRGBASpan
= read_rgba_span_ARGB
;
742 swdd
->ReadRGBAPixels
= read_rgba_pixels_ARGB
;
744 else if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
745 swdd
->WriteCI32Span
= write_index32_span_CI
;
746 swdd
->WriteCI8Span
= write_index8_span_CI
;
747 swdd
->WriteMonoCISpan
= write_monoindex_span_CI
;
748 swdd
->WriteCI32Pixels
= write_index_pixels_CI
;
749 swdd
->WriteMonoCIPixels
= write_monoindex_pixels_CI
;
750 swdd
->ReadCI32Span
= read_index_span_CI
;
751 swdd
->ReadCI32Pixels
= read_index_pixels_CI
;
754 _mesa_problem(ctx
, "bad pixel format in osmesa_update_state!\n");
757 /* Extend the software rasterizer with our optimized line and triangle
760 swrast
->choose_line
= osmesa_choose_line
;
761 swrast
->choose_triangle
= osmesa_choose_triangle
;
762 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
763 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
769 /**********************************************************************/
770 /***** Public Functions *****/
771 /**********************************************************************/
775 * Create an Off-Screen Mesa rendering context. The only attribute needed is
776 * an RGBA vs Color-Index mode flag.
778 * Input: format - either GL_RGBA or GL_COLOR_INDEX
779 * sharelist - specifies another OSMesaContext with which to share
780 * display lists. NULL indicates no sharing.
781 * Return: an OSMesaContext or 0 if error
783 GLAPI OSMesaContext GLAPIENTRY
784 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
786 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
795 * Create context and specify size of ancillary buffers.
797 GLAPI OSMesaContext GLAPIENTRY
798 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
799 GLint accumBits
, OSMesaContext sharelist
)
801 OSMesaContext osmesa
;
802 GLint rshift
, gshift
, bshift
, ashift
;
803 GLint rind
, gind
, bind
, aind
;
804 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
807 const GLubyte
*i1
= (GLubyte
*) &i4
;
808 const GLint little_endian
= *i1
;
810 rind
= gind
= bind
= aind
= 0;
811 if (format
==OSMESA_COLOR_INDEX
) {
813 rshift
= gshift
= bshift
= ashift
= 0;
816 else if (format
==OSMESA_RGBA
) {
819 greenBits
= CHAN_BITS
;
820 blueBits
= CHAN_BITS
;
821 alphaBits
= CHAN_BITS
;
840 else if (format
==OSMESA_BGRA
) {
843 greenBits
= CHAN_BITS
;
844 blueBits
= CHAN_BITS
;
845 alphaBits
= CHAN_BITS
;
864 else if (format
==OSMESA_ARGB
) {
867 greenBits
= CHAN_BITS
;
868 blueBits
= CHAN_BITS
;
869 alphaBits
= CHAN_BITS
;
888 else if (format
==OSMESA_RGB
) {
891 greenBits
= CHAN_BITS
;
892 blueBits
= CHAN_BITS
;
903 else if (format
==OSMESA_BGR
) {
906 greenBits
= CHAN_BITS
;
907 blueBits
= CHAN_BITS
;
918 #if CHAN_TYPE == GL_UNSIGNED_BYTE
919 else if (format
==OSMESA_RGB_565
) {
929 rind
= 0; /* not used */
940 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
942 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
943 GL_FALSE
, /* double buffer */
944 GL_FALSE
, /* stereo */
955 alphaBits
? accumBits
: 0,
958 if (!osmesa
->gl_visual
) {
963 /* Setup these pointers here since they're using for making the default
964 * and proxy texture objects. Actually, we don't really need to do
965 * this since we're using the default fallback functions which
966 * _mesa_initialize_context() would plug in if needed.
968 osmesa
->mesa
.Driver
.NewTextureObject
= _mesa_new_texture_object
;
969 osmesa
->mesa
.Driver
.DeleteTexture
= _mesa_delete_texture_object
;
971 if (!_mesa_initialize_context(&osmesa
->mesa
,
973 sharelist
? &sharelist
->mesa
974 : (GLcontext
*) NULL
,
977 _mesa_destroy_visual( osmesa
->gl_visual
);
982 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
983 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
984 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
986 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
987 (GLboolean
) ( osmesa
->gl_visual
->depthBits
> 0 ),
988 (GLboolean
) ( osmesa
->gl_visual
->stencilBits
> 0 ),
989 (GLboolean
) ( osmesa
->gl_visual
->accumRedBits
> 0 ),
990 GL_FALSE
/* s/w alpha */ );
992 if (!osmesa
->gl_buffer
) {
993 _mesa_destroy_visual( osmesa
->gl_visual
);
994 _mesa_free_context_data( &osmesa
->mesa
);
998 osmesa
->format
= format
;
999 osmesa
->buffer
= NULL
;
1002 osmesa
->userRowLength
= 0;
1003 osmesa
->rowlength
= 0;
1004 osmesa
->yup
= GL_TRUE
;
1005 osmesa
->rshift
= rshift
;
1006 osmesa
->gshift
= gshift
;
1007 osmesa
->bshift
= bshift
;
1008 osmesa
->ashift
= ashift
;
1009 osmesa
->rInd
= rind
;
1010 osmesa
->gInd
= gind
;
1011 osmesa
->bInd
= bind
;
1012 osmesa
->aInd
= aind
;
1014 /* Initialize the software rasterizer and helper modules. */
1016 GLcontext
*ctx
= &osmesa
->mesa
;
1018 _swrast_CreateContext( ctx
);
1019 _ac_CreateContext( ctx
);
1020 _tnl_CreateContext( ctx
);
1021 _swsetup_CreateContext( ctx
);
1023 _swsetup_Wakeup( ctx
);
1024 hook_in_driver_functions( ctx
);
1032 * Destroy an Off-Screen Mesa rendering context.
1034 * Input: ctx - the context to destroy
1036 GLAPI
void GLAPIENTRY
1037 OSMesaDestroyContext( OSMesaContext ctx
)
1040 _swsetup_DestroyContext( &ctx
->mesa
);
1041 _tnl_DestroyContext( &ctx
->mesa
);
1042 _ac_DestroyContext( &ctx
->mesa
);
1043 _swrast_DestroyContext( &ctx
->mesa
);
1045 _mesa_destroy_visual( ctx
->gl_visual
);
1046 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
1047 _mesa_free_context_data( &ctx
->mesa
);
1054 * Recompute the values of the context's rowaddr array.
1057 compute_row_addresses( OSMesaContext ctx
)
1059 GLint bytesPerPixel
, bytesPerRow
, i
;
1060 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
1062 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
1064 bytesPerPixel
= 1 * sizeof(GLchan
);
1066 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
1068 bytesPerPixel
= 3 * sizeof(GLchan
);
1070 else if (ctx
->format
== OSMESA_RGB_565
) {
1071 /* 5/6/5 RGB pixel in 16 bits */
1076 bytesPerPixel
= 4 * sizeof(GLchan
);
1079 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
1082 /* Y=0 is bottom line of window */
1083 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1084 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
1088 /* Y=0 is top line of window */
1089 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1090 GLint j
= ctx
->height
- i
- 1;
1091 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
1098 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1099 * block of memory which the client provides. Its size must be at least
1100 * as large as width*height*sizeof(type). Its address should be a multiple
1101 * of 4 if using RGBA mode.
1103 * Image data is stored in the order of glDrawPixels: row-major order
1104 * with the lower-left image pixel stored in the first array position
1105 * (ie. bottom-to-top).
1107 * If the context's viewport hasn't been initialized yet, it will now be
1108 * initialized to (0,0,width,height).
1110 * Input: ctx - the rendering context
1111 * buffer - the image buffer memory
1112 * type - data type for pixel components
1113 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1114 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1115 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
1116 * with CHAN_BITS==32 then type must be GL_FLOAT.
1117 * width, height - size of image buffer in pixels, at least 1
1118 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
1119 * invalid buffer address, invalid type, width<1, height<1,
1120 * width>internal limit or height>internal limit.
1122 GLAPI GLboolean GLAPIENTRY
1123 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
1124 GLsizei width
, GLsizei height
)
1126 if (!ctx
|| !buffer
||
1127 width
< 1 || height
< 1 ||
1128 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1132 if (ctx
->format
== OSMESA_RGB_565
) {
1133 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
1136 else if (type
!= CHAN_TYPE
) {
1140 osmesa_update_state( &ctx
->mesa
, 0 );
1141 _mesa_make_current( &ctx
->mesa
, ctx
->gl_buffer
);
1143 ctx
->buffer
= buffer
;
1145 ctx
->height
= height
;
1146 if (ctx
->userRowLength
)
1147 ctx
->rowlength
= ctx
->userRowLength
;
1149 ctx
->rowlength
= width
;
1151 compute_row_addresses( ctx
);
1154 if (ctx
->mesa
.Viewport
.Width
== 0) {
1155 /* initialize viewport and scissor box to buffer size */
1156 _mesa_Viewport( 0, 0, width
, height
);
1157 ctx
->mesa
.Scissor
.Width
= width
;
1158 ctx
->mesa
.Scissor
.Height
= height
;
1161 /* this will make ensure we recognize the new buffer size */
1162 _mesa_ResizeBuffersMESA();
1165 /* Added by Gerk Huisma: */
1166 _tnl_MakeCurrent( &ctx
->mesa
, ctx
->mesa
.DrawBuffer
,
1167 ctx
->mesa
.ReadBuffer
);
1174 GLAPI OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
1176 GLcontext
*ctx
= _mesa_get_current_context();
1178 return (OSMesaContext
) ctx
;
1185 GLAPI
void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
1187 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1190 case OSMESA_ROW_LENGTH
:
1192 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1193 "OSMesaPixelStore(value)" );
1196 osmesa
->userRowLength
= value
;
1197 osmesa
->rowlength
= value
? value
: osmesa
->width
;
1200 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1203 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1207 compute_row_addresses( osmesa
);
1211 GLAPI
void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1213 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1217 *value
= osmesa
->width
;
1220 *value
= osmesa
->height
;
1223 *value
= osmesa
->format
;
1228 case OSMESA_ROW_LENGTH
:
1229 *value
= osmesa
->userRowLength
;
1232 *value
= osmesa
->yup
;
1234 case OSMESA_MAX_WIDTH
:
1237 case OSMESA_MAX_HEIGHT
:
1238 *value
= MAX_HEIGHT
;
1241 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1247 * Return the depth buffer associated with an OSMesa context.
1248 * Input: c - the OSMesa context
1249 * Output: width, height - size of buffer in pixels
1250 * bytesPerValue - bytes per depth value (2 or 4)
1251 * buffer - pointer to depth buffer values
1252 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1254 GLAPI GLboolean GLAPIENTRY
1255 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1256 GLint
*bytesPerValue
, void **buffer
)
1258 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
1266 *width
= c
->gl_buffer
->Width
;
1267 *height
= c
->gl_buffer
->Height
;
1268 if (c
->gl_visual
->depthBits
<= 16)
1269 *bytesPerValue
= sizeof(GLushort
);
1271 *bytesPerValue
= sizeof(GLuint
);
1272 *buffer
= c
->gl_buffer
->DepthBuffer
;
1278 * Return the color buffer associated with an OSMesa context.
1279 * Input: c - the OSMesa context
1280 * Output: width, height - size of buffer in pixels
1281 * format - the pixel format (OSMESA_FORMAT)
1282 * buffer - pointer to color buffer values
1283 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1285 GLAPI GLboolean GLAPIENTRY
1286 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
1287 GLint
*height
, GLint
*format
, void **buffer
)
1298 *height
= c
->height
;
1299 *format
= c
->format
;
1300 *buffer
= c
->buffer
;
1307 struct name_address
{
1312 static struct name_address functions
[] = {
1313 { "OSMesaCreateContext", (void *) OSMesaCreateContext
},
1314 { "OSMesaCreateContextExt", (void *) OSMesaCreateContextExt
},
1315 { "OSMesaDestroyContext", (void *) OSMesaDestroyContext
},
1316 { "OSMesaMakeCurrent", (void *) OSMesaMakeCurrent
},
1317 { "OSMesaGetCurrentContext", (void *) OSMesaGetCurrentContext
},
1318 { "OSMesaPixelsStore", (void *) OSMesaPixelStore
},
1319 { "OSMesaGetIntegerv", (void *) OSMesaGetIntegerv
},
1320 { "OSMesaGetDepthBuffer", (void *) OSMesaGetDepthBuffer
},
1321 { "OSMesaGetColorBuffer", (void *) OSMesaGetColorBuffer
},
1322 { "OSMesaGetProcAddress", (void *) OSMesaGetProcAddress
},
1326 GLAPI
void * GLAPIENTRY
1327 OSMesaGetProcAddress( const char *funcName
)
1330 for (i
= 0; functions
[i
].Name
; i
++) {
1331 if (_mesa_strcmp(functions
[i
].Name
, funcName
) == 0)
1332 return (void *) functions
[i
].Address
;
1334 return (void *) _glapi_get_proc_address(funcName
);