2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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.
37 #include "GL/osmesa.h"
39 #include "bufferobj.h"
43 #include "extensions.h"
48 #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"
62 #include "drivers/common/driverfuncs.h"
67 * This is the OS/Mesa context struct.
68 * Notice how it includes a GLcontext. By doing this we're mimicking
69 * C++ inheritance/derivation.
70 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
73 struct osmesa_context
{
74 GLcontext mesa
; /* The core GL/Mesa context */
75 GLvisual
*gl_visual
; /* Describes the buffers */
76 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
77 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
78 void *buffer
; /* the image buffer */
79 GLint width
, height
; /* size of image buffer */
80 GLint rowlength
; /* number of pixels per row */
81 GLint userRowLength
; /* user-specified number of pixels per row */
82 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
84 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
85 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
86 GLboolean yup
; /* TRUE -> Y increases upward */
87 /* FALSE -> Y increases downward */
91 /* Just cast, since we're using structure containment */
92 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
96 /**********************************************************************/
97 /*** Private Device Driver Functions ***/
98 /**********************************************************************/
101 static const GLubyte
*
102 get_string( GLcontext
*ctx
, GLenum name
)
108 return (const GLubyte
*) "Mesa OffScreen32";
109 #elif CHAN_BITS == 16
110 return (const GLubyte
*) "Mesa OffScreen16";
112 return (const GLubyte
*) "Mesa OffScreen";
121 osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
123 /* easy - just propogate */
124 _swrast_InvalidateState( ctx
, new_state
);
125 _swsetup_InvalidateState( ctx
, new_state
);
126 _ac_InvalidateState( ctx
, new_state
);
127 _tnl_InvalidateState( ctx
, new_state
);
132 set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
134 /* separate read buffer not supported */
135 ASSERT(buffer
== ctx
->DrawBuffer
);
136 ASSERT(bufferBit
== DD_FRONT_LEFT_BIT
);
141 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
143 /* don't use GET_CURRENT_CONTEXT(ctx) here - it's a problem on Windows */
144 GLcontext
*ctx
= (GLcontext
*) _glapi_get_context();
147 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
148 *width
= osmesa
->width
;
149 *height
= osmesa
->height
;
155 clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
156 GLint x
, GLint y
, GLint width
, GLint height
)
158 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
159 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
161 /* sanity check - we only have a front-left buffer */
162 ASSERT((mask
& (DD_FRONT_RIGHT_BIT
|
164 DD_BACK_RIGHT_BIT
)) == 0);
166 /* use optimized clear for common cases (clear whole buffer to black) */
167 if (mask
& DD_FRONT_LEFT_BIT
) {
168 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
169 if (ctx
->Color
.ClearIndex
== 0 &&
170 ctx
->Color
.IndexMask
== (GLuint
) ~0 &&
171 osmesa
->rowlength
== osmesa
->width
&&
173 /* clear whole buffer to zeros */
174 _mesa_bzero(osmesa
->buffer
,
175 osmesa
->width
* osmesa
->height
* sizeof(GLchan
));
176 mask
&= ~DD_FRONT_LEFT_BIT
;
181 if (*colorMask
== 0xffffffff &&
182 ctx
->Color
.ClearColor
[0] == 0.0F
&&
183 ctx
->Color
.ClearColor
[1] == 0.0F
&&
184 ctx
->Color
.ClearColor
[2] == 0.0F
&&
185 ctx
->Color
.ClearColor
[3] == 0.0F
&&
186 osmesa
->rowlength
== osmesa
->width
&&
189 /* clear whole buffer to black */
190 if (osmesa
->format
== OSMESA_RGBA
||
191 osmesa
->format
== OSMESA_BGRA
||
192 osmesa
->format
== OSMESA_ARGB
)
193 bytesPerPixel
= 4 * sizeof(GLchan
);
194 else if (osmesa
->format
== OSMESA_RGB
||
195 osmesa
->format
== OSMESA_BGR
)
196 bytesPerPixel
= 3 * sizeof(GLchan
);
197 else if (osmesa
->format
== OSMESA_RGB_565
)
198 bytesPerPixel
= sizeof(GLushort
);
200 _mesa_problem(ctx
, "bad pixel format in osmesa_clear()");
203 _mesa_bzero(osmesa
->buffer
,
204 bytesPerPixel
* osmesa
->width
* osmesa
->height
);
205 mask
&= ~DD_FRONT_LEFT_BIT
;
211 /* software fallback (spans) for everything else. */
212 _swrast_Clear(ctx
, mask
, all
, x
, y
, width
, height
);
217 /**********************************************************************/
218 /***** Read/write spans/arrays of pixels *****/
219 /**********************************************************************/
223 #define NAME(PREFIX) PREFIX##_RGBA
225 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
226 #define INIT_PIXEL_PTR(P, X, Y) \
227 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
228 #define INC_PIXEL_PTR(P) P += 4
229 #if CHAN_TYPE == GL_FLOAT
230 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
231 P[0] = MAX2((R), 0.0F); \
232 P[1] = MAX2((G), 0.0F); \
233 P[2] = MAX2((B), 0.0F); \
235 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
236 P[0] = MAX2((R), 0.0F); \
237 P[1] = MAX2((G), 0.0F); \
238 P[2] = MAX2((B), 0.0F); \
239 P[3] = CLAMP((A), 0.0F, CHAN_MAXF)
241 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
242 P[0] = R; P[1] = G; P[2] = B; P[3] = CHAN_MAX
243 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
244 P[0] = R; P[1] = G; P[2] = B; P[3] = A
246 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
247 R = P[0]; G = P[1]; B = P[2]; A = P[3]
248 #include "swrast/s_spantemp.h"
251 #define NAME(PREFIX) PREFIX##_BGRA
253 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
254 #define INIT_PIXEL_PTR(P, X, Y) \
255 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
256 #define INC_PIXEL_PTR(P) P += 4
257 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
258 P[2] = R; P[1] = G; P[0] = B; P[3] = CHAN_MAX
259 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
260 P[2] = R; P[1] = G; P[0] = B; P[3] = A
261 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
262 R = P[2]; G = P[1]; B = P[0]; A = P[3]
263 #include "swrast/s_spantemp.h"
266 #define NAME(PREFIX) PREFIX##_ARGB
268 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
269 #define INIT_PIXEL_PTR(P, X, Y) \
270 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
271 #define INC_PIXEL_PTR(P) P += 4
272 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
273 P[1] = R; P[2] = G; P[3] = B; P[0] = CHAN_MAX
274 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
275 P[1] = R; P[2] = G; P[3] = B; P[0] = A
276 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
277 R = P[1]; G = P[2]; B = P[3]; A = P[0]
278 #include "swrast/s_spantemp.h"
281 #define NAME(PREFIX) PREFIX##_RGB
283 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
284 #define INIT_PIXEL_PTR(P, X, Y) \
285 GLchan *P = osmesa->rowaddr[Y] + 3 * (X)
286 #define INC_PIXEL_PTR(P) P += 3
287 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
288 P[0] = R; P[1] = G; P[2] = B
289 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
290 P[0] = R; P[1] = G; P[2] = B
291 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
292 R = P[0]; G = P[1]; B = P[2]; A = CHAN_MAX
293 #include "swrast/s_spantemp.h"
296 #define NAME(PREFIX) PREFIX##_BGR
298 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
299 #define INIT_PIXEL_PTR(P, X, Y) \
300 GLchan *P = osmesa->rowaddr[Y] + 3 * (X)
301 #define INC_PIXEL_PTR(P) P += 3
302 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
303 P[0] = B; P[1] = G; P[2] = R
304 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
305 P[0] = B; P[1] = G; P[2] = R
306 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
307 B = P[0]; G = P[1]; R = P[2]; A = CHAN_MAX
308 #include "swrast/s_spantemp.h"
311 #if CHAN_TYPE == GL_UNSIGNED_BYTE
312 #define NAME(PREFIX) PREFIX##_RGB_565
314 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
315 #define INIT_PIXEL_PTR(P, X, Y) \
316 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
317 #define INC_PIXEL_PTR(P) P += 1
318 #define STORE_RGB_PIXEL(P, X, Y, R, G, B) \
319 *P = ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
320 #define STORE_RGBA_PIXEL(P, X, Y, R, G, B, A) \
321 *P = ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
322 #define FETCH_RGBA_PIXEL(R, G, B, A, P) \
323 R = ( (((*P) >> 8) & 0xf8) | (((*P) >> 11) & 0x7) ); \
324 G = ( (((*P) >> 3) & 0xfc) | (((*P) >> 5) & 0x3) ); \
325 B = ( (((*P) << 3) & 0xf8) | (((*P) ) & 0x7) ); \
327 #include "swrast/s_spantemp.h"
331 #define NAME(PREFIX) PREFIX##_CI
333 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
334 #define INIT_PIXEL_PTR(P, X, Y) \
335 GLchan *P = osmesa->rowaddr[Y] + (X)
336 #define INC_PIXEL_PTR(P) P += 1
337 #define STORE_CI_PIXEL(P, CI) \
339 #define FETCH_CI_PIXEL(CI, P) \
341 #include "swrast/s_spantemp.h"
344 /**********************************************************************/
345 /***** Optimized line rendering *****/
346 /**********************************************************************/
349 #if CHAN_TYPE == GL_FLOAT
350 #define PACK_RGBA(DST, R, G, B, A) \
352 (DST)[0] = MAX2( R, 0.0F ); \
353 (DST)[1] = MAX2( G, 0.0F ); \
354 (DST)[2] = MAX2( B, 0.0F ); \
355 (DST)[3] = CLAMP(A, 0.0F, CHAN_MAXF);\
358 #define PACK_RGBA(DST, R, G, B, A) \
360 (DST)[osmesa->rInd] = R; \
361 (DST)[osmesa->gInd] = G; \
362 (DST)[osmesa->bInd] = B; \
363 (DST)[osmesa->aInd] = A; \
367 #define PACK_RGB(DST, R, G, B) \
374 #define PACK_BGR(DST, R, G, B) \
381 #define PACK_RGB_565(DST, R, G, B) \
383 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
386 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
387 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
388 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
389 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
391 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
392 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
393 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
394 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
398 * Draw a flat-shaded, RGB line into an osmesa buffer.
400 #define NAME flat_rgba_line
403 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
404 const GLchan *color = vert1->color;
408 GLchan *p = PIXELADDR4(X, Y); \
409 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
413 #include "..\swrast\s_linetemp.h"
415 #include "swrast/s_linetemp.h"
421 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
423 #define NAME flat_rgba_z_line
426 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
428 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
429 const GLchan *color = vert1->color;
434 GLchan *p = PIXELADDR4(X, Y); \
435 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
436 color[BCOMP], color[ACOMP]); \
442 #include "..\swrast\s_linetemp.h"
444 #include "swrast/s_linetemp.h"
450 * Analyze context state to see if we can provide a fast line drawing
451 * function, like those in lines.c. Otherwise, return NULL.
453 static swrast_line_func
454 osmesa_choose_line_function( GLcontext
*ctx
)
456 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
457 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
459 if (CHAN_BITS
!= 8) return NULL
;
460 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
461 if (ctx
->Line
.SmoothFlag
) return NULL
;
462 if (ctx
->Texture
._EnabledUnits
) return NULL
;
463 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
464 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
465 if (ctx
->Line
.StippleFlag
) return NULL
;
466 if (ctx
->Line
.SmoothFlag
) return NULL
;
467 if (osmesa
->format
!= OSMESA_RGBA
&&
468 osmesa
->format
!= OSMESA_BGRA
&&
469 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
471 if (swrast
->_RasterMask
==DEPTH_BIT
472 && ctx
->Depth
.Func
==GL_LESS
473 && ctx
->Depth
.Mask
==GL_TRUE
474 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
475 return (swrast_line_func
) flat_rgba_z_line
;
478 if (swrast
->_RasterMask
== 0) {
479 return (swrast_line_func
) flat_rgba_line
;
482 return (swrast_line_func
) NULL
;
486 /**********************************************************************/
487 /***** Optimized triangle rendering *****/
488 /**********************************************************************/
492 * Smooth-shaded, z-less triangle, RGBA color.
494 #define NAME smooth_rgba_z_triangle
496 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
498 #define INTERP_ALPHA 1
500 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
501 #define RENDER_SPAN( span ) \
503 GLchan *img = PIXELADDR4(span.x, span.y); \
504 for (i = 0; i < span.end; i++, img += 4) { \
505 const GLdepth z = FixedToDepth(span.z); \
507 PACK_RGBA(img, FixedToChan(span.red), \
508 FixedToChan(span.green), FixedToChan(span.blue), \
509 FixedToChan(span.alpha)); \
512 span.red += span.redStep; \
513 span.green += span.greenStep; \
514 span.blue += span.blueStep; \
515 span.alpha += span.alphaStep; \
516 span.z += span.zStep; \
519 #include "..\swrast\s_tritemp.h"
521 #include "swrast/s_tritemp.h"
527 * Flat-shaded, z-less triangle, RGBA color.
529 #define NAME flat_rgba_z_triangle
531 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
533 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
535 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
536 v2->color[2], v2->color[3]);
538 #define RENDER_SPAN( span ) \
540 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
541 for (i = 0; i < span.end; i++) { \
542 const GLdepth z = FixedToDepth(span.z); \
547 span.z += span.zStep; \
550 #include "..\swrast\s_tritemp.h"
552 #include "swrast/s_tritemp.h"
558 * Return pointer to an accelerated triangle function if possible.
560 static swrast_tri_func
561 osmesa_choose_triangle_function( GLcontext
*ctx
)
563 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
564 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
566 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
567 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
568 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
569 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
570 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
571 if (osmesa
->format
!= OSMESA_RGBA
&&
572 osmesa
->format
!= OSMESA_BGRA
&&
573 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
574 if (ctx
->Polygon
.CullFlag
&&
575 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
576 return (swrast_tri_func
) NULL
;
578 if (swrast
->_RasterMask
== DEPTH_BIT
&&
579 ctx
->Depth
.Func
== GL_LESS
&&
580 ctx
->Depth
.Mask
== GL_TRUE
&&
581 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
582 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
583 return (swrast_tri_func
) smooth_rgba_z_triangle
;
586 return (swrast_tri_func
) flat_rgba_z_triangle
;
589 return (swrast_tri_func
) NULL
;
594 /* Override for the swrast triangle-selection function. Try to use one
595 * of our internal triangle functions, otherwise fall back to the
596 * standard swrast functions.
599 osmesa_choose_triangle( GLcontext
*ctx
)
601 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
603 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
604 if (!swrast
->Triangle
)
605 _swrast_choose_triangle( ctx
);
609 osmesa_choose_line( GLcontext
*ctx
)
611 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
613 swrast
->Line
= osmesa_choose_line_function( ctx
);
615 _swrast_choose_line( ctx
);
619 #define OSMESA_NEW_LINE (_NEW_LINE | \
624 _SWRAST_NEW_RASTERMASK)
626 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
631 _SWRAST_NEW_RASTERMASK)
634 /* one-time, per-context initialization */
636 hook_in_driver_functions( GLcontext
*ctx
)
638 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
639 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
640 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
641 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
643 ASSERT((void *) osmesa
== (void *) ctx
->DriverCtx
);
645 /* use default TCL pipeline */
646 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
648 swdd
->SetBuffer
= set_buffer
;
650 /* RGB(A) span/pixel functions */
651 if (osmesa
->format
== OSMESA_RGB
) {
652 swdd
->WriteRGBASpan
= write_rgba_span_RGB
;
653 swdd
->WriteRGBSpan
= write_rgb_span_RGB
;
654 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGB
;
655 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB
;
656 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGB
;
657 swdd
->ReadRGBASpan
= read_rgba_span_RGB
;
658 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGB
;
660 else if (osmesa
->format
== OSMESA_BGR
) {
661 swdd
->WriteRGBASpan
= write_rgba_span_BGR
;
662 swdd
->WriteRGBSpan
= write_rgb_span_BGR
;
663 swdd
->WriteMonoRGBASpan
= write_monorgba_span_BGR
;
664 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGR
;
665 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_BGR
;
666 swdd
->ReadRGBASpan
= read_rgba_span_BGR
;
667 swdd
->ReadRGBAPixels
= read_rgba_pixels_BGR
;
669 #if CHAN_TYPE == GL_UNSIGNED_BYTE
670 else if (osmesa
->format
== OSMESA_RGB_565
) {
671 swdd
->WriteRGBASpan
= write_rgba_span_RGB_565
;
672 swdd
->WriteRGBSpan
= write_rgb_span_RGB_565
;
673 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGB_565
;
674 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGB_565
;
675 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGB_565
;
676 swdd
->ReadRGBASpan
= read_rgba_span_RGB_565
;
677 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGB_565
;
680 else if (osmesa
->format
== OSMESA_RGBA
) {
681 swdd
->WriteRGBASpan
= write_rgba_span_RGBA
;
682 swdd
->WriteRGBSpan
= write_rgb_span_RGBA
;
683 swdd
->WriteMonoRGBASpan
= write_monorgba_span_RGBA
;
684 swdd
->WriteRGBAPixels
= write_rgba_pixels_RGBA
;
685 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_RGBA
;
686 swdd
->ReadRGBASpan
= read_rgba_span_RGBA
;
687 swdd
->ReadRGBAPixels
= read_rgba_pixels_RGBA
;
689 else if (osmesa
->format
== OSMESA_BGRA
) {
690 swdd
->WriteRGBASpan
= write_rgba_span_BGRA
;
691 swdd
->WriteRGBSpan
= write_rgb_span_BGRA
;
692 swdd
->WriteMonoRGBASpan
= write_monorgba_span_BGRA
;
693 swdd
->WriteRGBAPixels
= write_rgba_pixels_BGRA
;
694 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_BGRA
;
695 swdd
->ReadRGBASpan
= read_rgba_span_BGRA
;
696 swdd
->ReadRGBAPixels
= read_rgba_pixels_BGRA
;
698 else if (osmesa
->format
== OSMESA_ARGB
) {
699 swdd
->WriteRGBASpan
= write_rgba_span_ARGB
;
700 swdd
->WriteRGBSpan
= write_rgb_span_ARGB
;
701 swdd
->WriteMonoRGBASpan
= write_monorgba_span_ARGB
;
702 swdd
->WriteRGBAPixels
= write_rgba_pixels_ARGB
;
703 swdd
->WriteMonoRGBAPixels
= write_monorgba_pixels_ARGB
;
704 swdd
->ReadRGBASpan
= read_rgba_span_ARGB
;
705 swdd
->ReadRGBAPixels
= read_rgba_pixels_ARGB
;
707 else if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
708 swdd
->WriteCI32Span
= write_index32_span_CI
;
709 swdd
->WriteCI8Span
= write_index8_span_CI
;
710 swdd
->WriteMonoCISpan
= write_monoindex_span_CI
;
711 swdd
->WriteCI32Pixels
= write_index_pixels_CI
;
712 swdd
->WriteMonoCIPixels
= write_monoindex_pixels_CI
;
713 swdd
->ReadCI32Span
= read_index_span_CI
;
714 swdd
->ReadCI32Pixels
= read_index_pixels_CI
;
717 _mesa_problem(ctx
, "bad pixel format in osmesa_update_state!\n");
720 /* Extend the software rasterizer with our optimized line and triangle
723 swrast
->choose_line
= osmesa_choose_line
;
724 swrast
->choose_triangle
= osmesa_choose_triangle
;
725 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
726 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
732 /**********************************************************************/
733 /***** Public Functions *****/
734 /**********************************************************************/
738 * Create an Off-Screen Mesa rendering context. The only attribute needed is
739 * an RGBA vs Color-Index mode flag.
741 * Input: format - either GL_RGBA or GL_COLOR_INDEX
742 * sharelist - specifies another OSMesaContext with which to share
743 * display lists. NULL indicates no sharing.
744 * Return: an OSMesaContext or 0 if error
746 GLAPI OSMesaContext GLAPIENTRY
747 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
749 const GLint accumBits
= (format
== OSMESA_COLOR_INDEX
) ? 0 : 16;
750 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
751 8, accumBits
, sharelist
);
759 * Create context and specify size of ancillary buffers.
761 GLAPI OSMesaContext GLAPIENTRY
762 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
763 GLint accumBits
, OSMesaContext sharelist
)
765 OSMesaContext osmesa
;
766 struct dd_function_table functions
;
767 GLint rshift
, gshift
, bshift
, ashift
;
768 GLint rind
, gind
, bind
, aind
;
769 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
772 const GLubyte
*i1
= (GLubyte
*) &i4
;
773 const GLint little_endian
= *i1
;
775 rind
= gind
= bind
= aind
= 0;
776 if (format
==OSMESA_COLOR_INDEX
) {
778 rshift
= gshift
= bshift
= ashift
= 0;
781 else if (format
==OSMESA_RGBA
) {
784 greenBits
= CHAN_BITS
;
785 blueBits
= CHAN_BITS
;
786 alphaBits
= CHAN_BITS
;
805 else if (format
==OSMESA_BGRA
) {
808 greenBits
= CHAN_BITS
;
809 blueBits
= CHAN_BITS
;
810 alphaBits
= CHAN_BITS
;
829 else if (format
==OSMESA_ARGB
) {
832 greenBits
= CHAN_BITS
;
833 blueBits
= CHAN_BITS
;
834 alphaBits
= CHAN_BITS
;
853 else if (format
==OSMESA_RGB
) {
856 greenBits
= CHAN_BITS
;
857 blueBits
= CHAN_BITS
;
868 else if (format
==OSMESA_BGR
) {
871 greenBits
= CHAN_BITS
;
872 blueBits
= CHAN_BITS
;
883 #if CHAN_TYPE == GL_UNSIGNED_BYTE
884 else if (format
==OSMESA_RGB_565
) {
894 rind
= 0; /* not used */
905 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
907 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
908 GL_FALSE
, /* double buffer */
909 GL_FALSE
, /* stereo */
920 alphaBits
? accumBits
: 0,
923 if (!osmesa
->gl_visual
) {
928 /* Initialize device driver function table */
929 _mesa_init_driver_functions(&functions
);
930 /* override with our functions */
931 functions
.GetString
= get_string
;
932 functions
.UpdateState
= osmesa_update_state
;
933 functions
.GetBufferSize
= get_buffer_size
;
934 functions
.Clear
= clear
;
936 if (!_mesa_initialize_context(&osmesa
->mesa
,
938 sharelist
? &sharelist
->mesa
939 : (GLcontext
*) NULL
,
940 &functions
, (void *) osmesa
)) {
941 _mesa_destroy_visual( osmesa
->gl_visual
);
946 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
947 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
948 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
949 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
951 osmesa
->gl_buffer
= _mesa_create_framebuffer( osmesa
->gl_visual
,
952 (GLboolean
) ( osmesa
->gl_visual
->depthBits
> 0 ),
953 (GLboolean
) ( osmesa
->gl_visual
->stencilBits
> 0 ),
954 (GLboolean
) ( osmesa
->gl_visual
->accumRedBits
> 0 ),
955 GL_FALSE
/* s/w alpha */ );
957 if (!osmesa
->gl_buffer
) {
958 _mesa_destroy_visual( osmesa
->gl_visual
);
959 _mesa_free_context_data( &osmesa
->mesa
);
963 osmesa
->format
= format
;
964 osmesa
->buffer
= NULL
;
967 osmesa
->userRowLength
= 0;
968 osmesa
->rowlength
= 0;
969 osmesa
->yup
= GL_TRUE
;
970 osmesa
->rshift
= rshift
;
971 osmesa
->gshift
= gshift
;
972 osmesa
->bshift
= bshift
;
973 osmesa
->ashift
= ashift
;
979 /* Initialize the software rasterizer and helper modules. */
981 GLcontext
*ctx
= &osmesa
->mesa
;
983 _swrast_CreateContext( ctx
);
984 _ac_CreateContext( ctx
);
985 _tnl_CreateContext( ctx
);
986 _swsetup_CreateContext( ctx
);
988 _swsetup_Wakeup( ctx
);
989 hook_in_driver_functions( ctx
);
997 * Destroy an Off-Screen Mesa rendering context.
999 * Input: ctx - the context to destroy
1001 GLAPI
void GLAPIENTRY
1002 OSMesaDestroyContext( OSMesaContext ctx
)
1005 _swsetup_DestroyContext( &ctx
->mesa
);
1006 _tnl_DestroyContext( &ctx
->mesa
);
1007 _ac_DestroyContext( &ctx
->mesa
);
1008 _swrast_DestroyContext( &ctx
->mesa
);
1010 _mesa_destroy_visual( ctx
->gl_visual
);
1011 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
1012 _mesa_free_context_data( &ctx
->mesa
);
1019 * Recompute the values of the context's rowaddr array.
1022 compute_row_addresses( OSMesaContext ctx
)
1024 GLint bytesPerPixel
, bytesPerRow
, i
;
1025 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
1027 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
1029 bytesPerPixel
= 1 * sizeof(GLchan
);
1031 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
1033 bytesPerPixel
= 3 * sizeof(GLchan
);
1035 else if (ctx
->format
== OSMESA_RGB_565
) {
1036 /* 5/6/5 RGB pixel in 16 bits */
1041 bytesPerPixel
= 4 * sizeof(GLchan
);
1044 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
1047 /* Y=0 is bottom line of window */
1048 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1049 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
1053 /* Y=0 is top line of window */
1054 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1055 GLint j
= ctx
->height
- i
- 1;
1056 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
1063 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1064 * block of memory which the client provides. Its size must be at least
1065 * as large as width*height*sizeof(type). Its address should be a multiple
1066 * of 4 if using RGBA mode.
1068 * Image data is stored in the order of glDrawPixels: row-major order
1069 * with the lower-left image pixel stored in the first array position
1070 * (ie. bottom-to-top).
1072 * If the context's viewport hasn't been initialized yet, it will now be
1073 * initialized to (0,0,width,height).
1075 * Input: ctx - the rendering context
1076 * buffer - the image buffer memory
1077 * type - data type for pixel components
1078 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1079 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1080 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
1081 * with CHAN_BITS==32 then type must be GL_FLOAT.
1082 * width, height - size of image buffer in pixels, at least 1
1083 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
1084 * invalid buffer address, invalid type, width<1, height<1,
1085 * width>internal limit or height>internal limit.
1087 GLAPI GLboolean GLAPIENTRY
1088 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
1089 GLsizei width
, GLsizei height
)
1091 if (!ctx
|| !buffer
||
1092 width
< 1 || height
< 1 ||
1093 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1097 if (ctx
->format
== OSMESA_RGB_565
) {
1098 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
1101 else if (type
!= CHAN_TYPE
) {
1105 osmesa_update_state( &ctx
->mesa
, 0 );
1106 _mesa_make_current( &ctx
->mesa
, ctx
->gl_buffer
);
1108 ctx
->buffer
= buffer
;
1110 ctx
->height
= height
;
1111 if (ctx
->userRowLength
)
1112 ctx
->rowlength
= ctx
->userRowLength
;
1114 ctx
->rowlength
= width
;
1116 compute_row_addresses( ctx
);
1119 if (ctx
->mesa
.Viewport
.Width
== 0) {
1120 /* initialize viewport and scissor box to buffer size */
1121 _mesa_Viewport( 0, 0, width
, height
);
1122 ctx
->mesa
.Scissor
.Width
= width
;
1123 ctx
->mesa
.Scissor
.Height
= height
;
1126 /* this will make ensure we recognize the new buffer size */
1127 _mesa_ResizeBuffersMESA();
1130 /* Added by Gerk Huisma: */
1131 _tnl_MakeCurrent( &ctx
->mesa
, ctx
->mesa
.DrawBuffer
,
1132 ctx
->mesa
.ReadBuffer
);
1139 GLAPI OSMesaContext GLAPIENTRY
OSMesaGetCurrentContext( void )
1141 GLcontext
*ctx
= _mesa_get_current_context();
1143 return (OSMesaContext
) ctx
;
1150 GLAPI
void GLAPIENTRY
OSMesaPixelStore( GLint pname
, GLint value
)
1152 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1155 case OSMESA_ROW_LENGTH
:
1157 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1158 "OSMesaPixelStore(value)" );
1161 osmesa
->userRowLength
= value
;
1162 osmesa
->rowlength
= value
? value
: osmesa
->width
;
1165 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1168 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1172 compute_row_addresses( osmesa
);
1176 GLAPI
void GLAPIENTRY
OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1178 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1182 *value
= osmesa
->width
;
1185 *value
= osmesa
->height
;
1188 *value
= osmesa
->format
;
1193 case OSMESA_ROW_LENGTH
:
1194 *value
= osmesa
->userRowLength
;
1197 *value
= osmesa
->yup
;
1199 case OSMESA_MAX_WIDTH
:
1202 case OSMESA_MAX_HEIGHT
:
1203 *value
= MAX_HEIGHT
;
1206 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1212 * Return the depth buffer associated with an OSMesa context.
1213 * Input: c - the OSMesa context
1214 * Output: width, height - size of buffer in pixels
1215 * bytesPerValue - bytes per depth value (2 or 4)
1216 * buffer - pointer to depth buffer values
1217 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1219 GLAPI GLboolean GLAPIENTRY
1220 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1221 GLint
*bytesPerValue
, void **buffer
)
1223 if ((!c
->gl_buffer
) || (!c
->gl_buffer
->DepthBuffer
)) {
1231 *width
= c
->gl_buffer
->Width
;
1232 *height
= c
->gl_buffer
->Height
;
1233 if (c
->gl_visual
->depthBits
<= 16)
1234 *bytesPerValue
= sizeof(GLushort
);
1236 *bytesPerValue
= sizeof(GLuint
);
1237 *buffer
= c
->gl_buffer
->DepthBuffer
;
1243 * Return the color buffer associated with an OSMesa context.
1244 * Input: c - the OSMesa context
1245 * Output: width, height - size of buffer in pixels
1246 * format - the pixel format (OSMESA_FORMAT)
1247 * buffer - pointer to color buffer values
1248 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1250 GLAPI GLboolean GLAPIENTRY
1251 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
1252 GLint
*height
, GLint
*format
, void **buffer
)
1263 *height
= c
->height
;
1264 *format
= c
->format
;
1265 *buffer
= c
->buffer
;
1272 struct name_address
{
1277 static struct name_address functions
[] = {
1278 { "OSMesaCreateContext", (void *) OSMesaCreateContext
},
1279 { "OSMesaCreateContextExt", (void *) OSMesaCreateContextExt
},
1280 { "OSMesaDestroyContext", (void *) OSMesaDestroyContext
},
1281 { "OSMesaMakeCurrent", (void *) OSMesaMakeCurrent
},
1282 { "OSMesaGetCurrentContext", (void *) OSMesaGetCurrentContext
},
1283 { "OSMesaPixelsStore", (void *) OSMesaPixelStore
},
1284 { "OSMesaGetIntegerv", (void *) OSMesaGetIntegerv
},
1285 { "OSMesaGetDepthBuffer", (void *) OSMesaGetDepthBuffer
},
1286 { "OSMesaGetColorBuffer", (void *) OSMesaGetColorBuffer
},
1287 { "OSMesaGetProcAddress", (void *) OSMesaGetProcAddress
},
1291 GLAPI
void * GLAPIENTRY
1292 OSMesaGetProcAddress( const char *funcName
)
1295 for (i
= 0; functions
[i
].Name
; i
++) {
1296 if (_mesa_strcmp(functions
[i
].Name
, funcName
) == 0)
1297 return (void *) functions
[i
].Address
;
1299 return (void *) _glapi_get_proc_address(funcName
);