2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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 "extensions.h"
40 #include "framebuffer.h"
44 #include "renderbuffer.h"
45 #include "array_cache/acache.h"
46 #include "swrast/swrast.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "swrast/s_context.h"
49 #include "swrast/s_depth.h"
50 #include "swrast/s_lines.h"
51 #include "swrast/s_triangle.h"
53 #include "tnl/t_context.h"
54 #include "tnl/t_pipeline.h"
55 #include "drivers/common/driverfuncs.h"
60 * This is the OS/Mesa context struct.
61 * Notice how it includes a GLcontext. By doing this we're mimicking
62 * C++ inheritance/derivation.
63 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
66 struct osmesa_context
{
67 GLcontext mesa
; /* The core GL/Mesa context */
68 GLvisual
*gl_visual
; /* Describes the buffers */
69 GLframebuffer
*gl_buffer
; /* Depth, stencil, accum, etc buffers */
70 GLenum format
; /* either GL_RGBA or GL_COLOR_INDEX */
71 void *buffer
; /* the image buffer */
72 GLint width
, height
; /* size of image buffer */
73 GLint rowlength
; /* number of pixels per row */
74 GLint userRowLength
; /* user-specified number of pixels per row */
75 GLint rshift
, gshift
; /* bit shifts for RGBA formats */
77 GLint rInd
, gInd
, bInd
, aInd
;/* index offsets for RGBA formats */
78 GLchan
*rowaddr
[MAX_HEIGHT
]; /* address of first pixel in each image row */
79 GLboolean yup
; /* TRUE -> Y increases upward */
80 /* FALSE -> Y increases downward */
84 /* Just cast, since we're using structure containment */
85 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
89 /**********************************************************************/
90 /*** Private Device Driver Functions ***/
91 /**********************************************************************/
94 static const GLubyte
*
95 get_string( GLcontext
*ctx
, GLenum name
)
101 return (const GLubyte
*) "Mesa OffScreen32";
102 #elif CHAN_BITS == 16
103 return (const GLubyte
*) "Mesa OffScreen16";
105 return (const GLubyte
*) "Mesa OffScreen";
114 osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
116 /* easy - just propogate */
117 _swrast_InvalidateState( ctx
, new_state
);
118 _swsetup_InvalidateState( ctx
, new_state
);
119 _ac_InvalidateState( ctx
, new_state
);
120 _tnl_InvalidateState( ctx
, new_state
);
125 set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
127 /* separate read buffer not supported */
128 ASSERT(buffer
== ctx
->DrawBuffer
);
129 ASSERT(bufferBit
== BUFFER_BIT_FRONT_LEFT
);
134 * Just return the current buffer size.
135 * There's no window to track the size of.
138 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
140 /* don't use GET_CURRENT_CONTEXT(ctx) here - it's a problem on Windows */
141 GLcontext
*ctx
= (GLcontext
*) _glapi_get_context();
144 OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
145 *width
= osmesa
->width
;
146 *height
= osmesa
->height
;
151 /**********************************************************************/
152 /***** Read/write spans/arrays of pixels *****/
153 /**********************************************************************/
156 #define NAME(PREFIX) PREFIX##_RGBA
157 #define FORMAT GL_RGBA
159 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
160 #define INIT_PIXEL_PTR(P, X, Y) \
161 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
162 #define INC_PIXEL_PTR(P) P += 4
163 #if CHAN_TYPE == GL_FLOAT
164 #define STORE_PIXEL(DST, X, Y, VALUE) \
165 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
166 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
167 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
168 DST[3] = CLAMP((VALUE[ACOMP]), 0.0F, CHAN_MAXF)
170 #define STORE_PIXEL(DST, X, Y, VALUE) \
171 DST[0] = VALUE[RCOMP]; \
172 DST[1] = VALUE[GCOMP]; \
173 DST[2] = VALUE[BCOMP]; \
174 DST[3] = VALUE[ACOMP]
176 #define FETCH_PIXEL(DST, SRC) \
177 DST[RCOMP] = SRC[0]; \
178 DST[GCOMP] = SRC[1]; \
179 DST[BCOMP] = SRC[2]; \
181 #include "swrast/s_spantemp.h"
184 #define NAME(PREFIX) PREFIX##_BGRA
185 #define FORMAT GL_RGBA
187 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
188 #define INIT_PIXEL_PTR(P, X, Y) \
189 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
190 #define INC_PIXEL_PTR(P) P += 4
191 #define STORE_PIXEL(DST, X, Y, VALUE) \
192 DST[2] = VALUE[RCOMP]; \
193 DST[1] = VALUE[GCOMP]; \
194 DST[0] = VALUE[BCOMP]; \
195 DST[3] = VALUE[ACOMP]
196 #define FETCH_PIXEL(DST, SRC) \
197 DST[RCOMP] = SRC[2]; \
198 DST[GCOMP] = SRC[1]; \
199 DST[BCOMP] = SRC[0]; \
201 #include "swrast/s_spantemp.h"
204 #define NAME(PREFIX) PREFIX##_ARGB
205 #define FORMAT GL_RGBA
207 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
208 #define INIT_PIXEL_PTR(P, X, Y) \
209 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
210 #define INC_PIXEL_PTR(P) P += 4
211 #define STORE_PIXEL(DST, X, Y, VALUE) \
212 DST[1] = VALUE[RCOMP]; \
213 DST[2] = VALUE[GCOMP]; \
214 DST[3] = VALUE[BCOMP]; \
215 DST[0] = VALUE[ACOMP]
216 #define FETCH_PIXEL(DST, SRC) \
217 DST[RCOMP] = SRC[1]; \
218 DST[GCOMP] = SRC[2]; \
219 DST[BCOMP] = SRC[3]; \
221 #include "swrast/s_spantemp.h"
224 #define NAME(PREFIX) PREFIX##_RGB
225 #define FORMAT GL_RGBA
227 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
228 #define INIT_PIXEL_PTR(P, X, Y) \
229 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
230 #define INC_PIXEL_PTR(P) P += 3
231 #define STORE_PIXEL(DST, X, Y, VALUE) \
232 DST[0] = VALUE[RCOMP]; \
233 DST[1] = VALUE[GCOMP]; \
234 DST[2] = VALUE[BCOMP]
235 #define FETCH_PIXEL(DST, SRC) \
236 DST[RCOMP] = SRC[0]; \
237 DST[GCOMP] = SRC[1]; \
238 DST[BCOMP] = SRC[2]; \
239 DST[BCOMP] = CHAN_MAX
240 #include "swrast/s_spantemp.h"
243 #define NAME(PREFIX) PREFIX##_BGR
244 #define FORMAT GL_RGBA
246 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
247 #define INIT_PIXEL_PTR(P, X, Y) \
248 GLchan *P = osmesa->rowaddr[Y] + 4 * (X)
249 #define INC_PIXEL_PTR(P) P += 3
250 #define STORE_PIXEL(DST, X, Y, VALUE) \
251 DST[2] = VALUE[RCOMP]; \
252 DST[1] = VALUE[GCOMP]; \
253 DST[0] = VALUE[BCOMP]
254 #define FETCH_PIXEL(DST, SRC) \
255 DST[RCOMP] = SRC[2]; \
256 DST[GCOMP] = SRC[1]; \
257 DST[BCOMP] = SRC[0]; \
258 DST[BCOMP] = CHAN_MAX
259 #include "swrast/s_spantemp.h"
262 #if CHAN_TYPE == GL_UNSIGNED_BYTE
263 #define NAME(PREFIX) PREFIX##_RGB_565
264 #define FORMAT GL_RGBA
266 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
267 #define INIT_PIXEL_PTR(P, X, Y) \
268 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
269 #define INC_PIXEL_PTR(P) P += 1
270 #define STORE_PIXEL(DST, X, Y, VALUE) \
271 *DST = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
272 #define FETCH_PIXEL(DST, SRC) \
273 DST[RCOMP] = ( (((*SRC) >> 8) & 0xf8) | (((*SRC) >> 11) & 0x7) ); \
274 DST[GCOMP] = ( (((*SRC) >> 3) & 0xfc) | (((*SRC) >> 5) & 0x3) ); \
275 DST[BCOMP] = ( (((*SRC) << 3) & 0xf8) | (((*SRC) ) & 0x7) ); \
276 DST[ACOMP] = CHAN_MAX
277 #include "swrast/s_spantemp.h"
278 #endif /* CHAN_TYPE == GL_UNSIGNED_BYTE */
281 #define NAME(PREFIX) PREFIX##_CI
282 #define FORMAT GL_COLOR_INDEX8_EXT
284 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
285 #define INIT_PIXEL_PTR(P, X, Y) \
286 GLubyte *P = osmesa->rowaddr[Y] + (X)
287 #define INC_PIXEL_PTR(P) P += 1
288 #define STORE_PIXEL(DST, X, Y, VALUE) \
290 #define FETCH_PIXEL(DST, SRC) \
292 #include "swrast/s_spantemp.h"
297 /**********************************************************************/
298 /***** Optimized line rendering *****/
299 /**********************************************************************/
302 #if CHAN_TYPE == GL_FLOAT
303 #define PACK_RGBA(DST, R, G, B, A) \
305 (DST)[0] = MAX2( R, 0.0F ); \
306 (DST)[1] = MAX2( G, 0.0F ); \
307 (DST)[2] = MAX2( B, 0.0F ); \
308 (DST)[3] = CLAMP(A, 0.0F, CHAN_MAXF);\
311 #define PACK_RGBA(DST, R, G, B, A) \
313 (DST)[osmesa->rInd] = R; \
314 (DST)[osmesa->gInd] = G; \
315 (DST)[osmesa->bInd] = B; \
316 (DST)[osmesa->aInd] = A; \
320 #define PACK_RGB(DST, R, G, B) \
327 #define PACK_BGR(DST, R, G, B) \
334 #define PACK_RGB_565(DST, R, G, B) \
336 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
339 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
340 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
341 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
342 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
344 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
345 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
346 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
347 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
351 * Draw a flat-shaded, RGB line into an osmesa buffer.
353 #define NAME flat_rgba_line
356 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
357 const GLchan *color = vert1->color;
361 GLchan *p = PIXELADDR4(X, Y); \
362 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
366 #include "..\swrast\s_linetemp.h"
368 #include "swrast/s_linetemp.h"
374 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
376 #define NAME flat_rgba_z_line
379 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
381 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
382 const GLchan *color = vert1->color;
387 GLchan *p = PIXELADDR4(X, Y); \
388 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
389 color[BCOMP], color[ACOMP]); \
395 #include "..\swrast\s_linetemp.h"
397 #include "swrast/s_linetemp.h"
403 * Analyze context state to see if we can provide a fast line drawing
404 * function, like those in lines.c. Otherwise, return NULL.
406 static swrast_line_func
407 osmesa_choose_line_function( GLcontext
*ctx
)
409 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
410 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
412 if (CHAN_BITS
!= 8) return NULL
;
413 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
414 if (ctx
->Line
.SmoothFlag
) return NULL
;
415 if (ctx
->Texture
._EnabledUnits
) return NULL
;
416 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
417 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
418 if (ctx
->Line
.StippleFlag
) return NULL
;
419 if (ctx
->Line
.SmoothFlag
) return NULL
;
420 if (osmesa
->format
!= OSMESA_RGBA
&&
421 osmesa
->format
!= OSMESA_BGRA
&&
422 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
424 if (swrast
->_RasterMask
==DEPTH_BIT
425 && ctx
->Depth
.Func
==GL_LESS
426 && ctx
->Depth
.Mask
==GL_TRUE
427 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
428 return (swrast_line_func
) flat_rgba_z_line
;
431 if (swrast
->_RasterMask
== 0) {
432 return (swrast_line_func
) flat_rgba_line
;
435 return (swrast_line_func
) NULL
;
439 /**********************************************************************/
440 /***** Optimized triangle rendering *****/
441 /**********************************************************************/
445 * Smooth-shaded, z-less triangle, RGBA color.
447 #define NAME smooth_rgba_z_triangle
449 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
451 #define INTERP_ALPHA 1
453 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
454 #define RENDER_SPAN( span ) \
456 GLchan *img = PIXELADDR4(span.x, span.y); \
457 for (i = 0; i < span.end; i++, img += 4) { \
458 const GLdepth z = FixedToDepth(span.z); \
460 PACK_RGBA(img, FixedToChan(span.red), \
461 FixedToChan(span.green), FixedToChan(span.blue), \
462 FixedToChan(span.alpha)); \
465 span.red += span.redStep; \
466 span.green += span.greenStep; \
467 span.blue += span.blueStep; \
468 span.alpha += span.alphaStep; \
469 span.z += span.zStep; \
472 #include "..\swrast\s_tritemp.h"
474 #include "swrast/s_tritemp.h"
480 * Flat-shaded, z-less triangle, RGBA color.
482 #define NAME flat_rgba_z_triangle
484 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
486 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
488 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
489 v2->color[2], v2->color[3]);
491 #define RENDER_SPAN( span ) \
493 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
494 for (i = 0; i < span.end; i++) { \
495 const GLdepth z = FixedToDepth(span.z); \
500 span.z += span.zStep; \
503 #include "..\swrast\s_tritemp.h"
505 #include "swrast/s_tritemp.h"
511 * Return pointer to an accelerated triangle function if possible.
513 static swrast_tri_func
514 osmesa_choose_triangle_function( GLcontext
*ctx
)
516 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
517 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
519 if (CHAN_BITS
!= 8) return (swrast_tri_func
) NULL
;
520 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
521 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
522 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
523 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
524 if (osmesa
->format
!= OSMESA_RGBA
&&
525 osmesa
->format
!= OSMESA_BGRA
&&
526 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
527 if (ctx
->Polygon
.CullFlag
&&
528 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
529 return (swrast_tri_func
) NULL
;
531 if (swrast
->_RasterMask
== DEPTH_BIT
&&
532 ctx
->Depth
.Func
== GL_LESS
&&
533 ctx
->Depth
.Mask
== GL_TRUE
&&
534 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
535 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
536 return (swrast_tri_func
) smooth_rgba_z_triangle
;
539 return (swrast_tri_func
) flat_rgba_z_triangle
;
542 return (swrast_tri_func
) NULL
;
547 /* Override for the swrast triangle-selection function. Try to use one
548 * of our internal triangle functions, otherwise fall back to the
549 * standard swrast functions.
552 osmesa_choose_triangle( GLcontext
*ctx
)
554 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
556 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
557 if (!swrast
->Triangle
)
558 _swrast_choose_triangle( ctx
);
562 osmesa_choose_line( GLcontext
*ctx
)
564 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
566 swrast
->Line
= osmesa_choose_line_function( ctx
);
568 _swrast_choose_line( ctx
);
572 #define OSMESA_NEW_LINE (_NEW_LINE | \
577 _SWRAST_NEW_RASTERMASK)
579 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
584 _SWRAST_NEW_RASTERMASK)
588 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
591 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
598 * Allocate renderbuffer storage. We don't actually allocate any storage
599 * since we're using a user-provided buffer.
600 * Just set up all the gl_renderbuffer methods.
603 osmesa_renderbuffer_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
604 GLenum internalFormat
, GLuint width
, GLuint height
)
606 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
608 if (osmesa
->format
== OSMESA_RGBA
) {
609 rb
->GetRow
= get_row_RGBA
;
610 rb
->GetValues
= get_values_RGBA
;
611 rb
->PutRow
= put_row_RGBA
;
612 rb
->PutMonoRow
= put_mono_row_RGBA
;
613 rb
->PutValues
= put_values_RGBA
;
614 rb
->PutMonoValues
= put_mono_values_RGBA
;
616 else if (osmesa
->format
== OSMESA_BGRA
) {
617 rb
->GetRow
= get_row_BGRA
;
618 rb
->GetValues
= get_values_BGRA
;
619 rb
->PutRow
= put_row_BGRA
;
620 rb
->PutMonoRow
= put_mono_row_BGRA
;
621 rb
->PutValues
= put_values_BGRA
;
622 rb
->PutMonoValues
= put_mono_values_BGRA
;
624 else if (osmesa
->format
== OSMESA_ARGB
) {
625 rb
->GetRow
= get_row_ARGB
;
626 rb
->GetValues
= get_values_ARGB
;
627 rb
->PutRow
= put_row_ARGB
;
628 rb
->PutMonoRow
= put_mono_row_ARGB
;
629 rb
->PutValues
= put_values_ARGB
;
630 rb
->PutMonoValues
= put_mono_values_ARGB
;
632 else if (osmesa
->format
== OSMESA_RGB
) {
633 rb
->GetRow
= get_row_RGB
;
634 rb
->GetValues
= get_values_RGB
;
635 rb
->PutRow
= put_row_RGB
;
636 rb
->PutMonoRow
= put_mono_row_RGB
;
637 rb
->PutValues
= put_values_RGB
;
638 rb
->PutMonoValues
= put_mono_values_RGB
;
640 else if (osmesa
->format
== OSMESA_BGR
) {
641 rb
->GetRow
= get_row_BGR
;
642 rb
->GetValues
= get_values_BGR
;
643 rb
->PutRow
= put_row_BGR
;
644 rb
->PutMonoRow
= put_mono_row_BGR
;
645 rb
->PutValues
= put_values_BGR
;
646 rb
->PutMonoValues
= put_mono_values_BGR
;
648 #if CHAN_TYPE == GL_UNSIGNED_BYTE
649 else if (osmesa
->format
== OSMESA_RGB_565
) {
650 rb
->GetRow
= get_row_RGB_565
;
651 rb
->GetValues
= get_values_RGB_565
;
652 rb
->PutRow
= put_row_RGB_565
;
653 rb
->PutMonoRow
= put_mono_row_RGB_565
;
654 rb
->PutValues
= put_values_RGB_565
;
655 rb
->PutMonoValues
= put_mono_values_RGB_565
;
658 else if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
659 rb
->GetRow
= get_row_CI
;
660 rb
->GetValues
= get_values_CI
;
661 rb
->PutRow
= put_row_CI
;
662 rb
->PutMonoRow
= put_mono_row_CI
;
663 rb
->PutValues
= put_values_CI
;
664 rb
->PutMonoValues
= put_mono_values_CI
;
667 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
675 * Allocate a new renderbuffer tpo describe the user-provided color buffer.
677 static struct gl_renderbuffer
*
678 new_osmesa_renderbuffer(GLenum format
)
680 struct gl_renderbuffer
*rb
= CALLOC_STRUCT(gl_renderbuffer
);
682 const GLuint name
= 0;
683 _mesa_init_renderbuffer(rb
, name
);
685 rb
->Delete
= osmesa_delete_renderbuffer
;
686 rb
->AllocStorage
= osmesa_renderbuffer_storage
;
688 if (format
== OSMESA_COLOR_INDEX
) {
689 rb
->_BaseFormat
= GL_COLOR_INDEX
;
690 rb
->InternalFormat
= GL_COLOR_INDEX
;
691 rb
->DataType
= GL_UNSIGNED_BYTE
;
694 rb
->_BaseFormat
= GL_RGBA
;
695 rb
->InternalFormat
= GL_RGBA
;
696 rb
->DataType
= CHAN_TYPE
;
704 /**********************************************************************/
705 /***** Public Functions *****/
706 /**********************************************************************/
710 * Create an Off-Screen Mesa rendering context. The only attribute needed is
711 * an RGBA vs Color-Index mode flag.
713 * Input: format - either GL_RGBA or GL_COLOR_INDEX
714 * sharelist - specifies another OSMesaContext with which to share
715 * display lists. NULL indicates no sharing.
716 * Return: an OSMesaContext or 0 if error
718 GLAPI OSMesaContext GLAPIENTRY
719 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
721 const GLint accumBits
= (format
== OSMESA_COLOR_INDEX
) ? 0 : 16;
722 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
723 8, accumBits
, sharelist
);
731 * Create context and specify size of ancillary buffers.
733 GLAPI OSMesaContext GLAPIENTRY
734 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
735 GLint accumBits
, OSMesaContext sharelist
)
737 OSMesaContext osmesa
;
738 struct dd_function_table functions
;
739 GLint rshift
, gshift
, bshift
, ashift
;
740 GLint rind
, gind
, bind
, aind
;
741 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
744 const GLubyte
*i1
= (GLubyte
*) &i4
;
745 const GLint little_endian
= *i1
;
747 rind
= gind
= bind
= aind
= 0;
748 if (format
==OSMESA_COLOR_INDEX
) {
750 rshift
= gshift
= bshift
= ashift
= 0;
753 else if (format
==OSMESA_RGBA
) {
756 greenBits
= CHAN_BITS
;
757 blueBits
= CHAN_BITS
;
758 alphaBits
= CHAN_BITS
;
777 else if (format
==OSMESA_BGRA
) {
780 greenBits
= CHAN_BITS
;
781 blueBits
= CHAN_BITS
;
782 alphaBits
= CHAN_BITS
;
801 else if (format
==OSMESA_ARGB
) {
804 greenBits
= CHAN_BITS
;
805 blueBits
= CHAN_BITS
;
806 alphaBits
= CHAN_BITS
;
825 else if (format
==OSMESA_RGB
) {
828 greenBits
= CHAN_BITS
;
829 blueBits
= CHAN_BITS
;
840 else if (format
==OSMESA_BGR
) {
843 greenBits
= CHAN_BITS
;
844 blueBits
= CHAN_BITS
;
855 #if CHAN_TYPE == GL_UNSIGNED_BYTE
856 else if (format
==OSMESA_RGB_565
) {
866 rind
= 0; /* not used */
876 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
878 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
879 GL_FALSE
, /* double buffer */
880 GL_FALSE
, /* stereo */
891 alphaBits
? accumBits
: 0,
894 if (!osmesa
->gl_visual
) {
899 /* Initialize device driver function table */
900 _mesa_init_driver_functions(&functions
);
901 /* override with our functions */
902 functions
.GetString
= get_string
;
903 functions
.UpdateState
= osmesa_update_state
;
904 functions
.GetBufferSize
= get_buffer_size
;
906 if (!_mesa_initialize_context(&osmesa
->mesa
,
908 sharelist
? &sharelist
->mesa
909 : (GLcontext
*) NULL
,
910 &functions
, (void *) osmesa
)) {
911 _mesa_destroy_visual( osmesa
->gl_visual
);
916 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
917 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
918 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
919 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
921 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
922 if (!osmesa
->gl_buffer
) {
923 _mesa_destroy_visual( osmesa
->gl_visual
);
924 _mesa_free_context_data( &osmesa
->mesa
);
929 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
,
930 new_osmesa_renderbuffer(format
));
931 _mesa_add_soft_renderbuffers(osmesa
->gl_buffer
,
932 GL_FALSE
, /* color */
933 osmesa
->gl_visual
->haveDepthBuffer
,
934 osmesa
->gl_visual
->haveStencilBuffer
,
935 osmesa
->gl_visual
->haveAccumBuffer
,
936 GL_FALSE
, /* alpha */
937 GL_FALSE
/* aux */ );
939 osmesa
->format
= format
;
940 osmesa
->buffer
= NULL
;
943 osmesa
->userRowLength
= 0;
944 osmesa
->rowlength
= 0;
945 osmesa
->yup
= GL_TRUE
;
946 osmesa
->rshift
= rshift
;
947 osmesa
->gshift
= gshift
;
948 osmesa
->bshift
= bshift
;
949 osmesa
->ashift
= ashift
;
955 /* Initialize the software rasterizer and helper modules. */
957 GLcontext
*ctx
= &osmesa
->mesa
;
959 struct swrast_device_driver
*swdd
;
962 if (!_swrast_CreateContext( ctx
) ||
963 !_ac_CreateContext( ctx
) ||
964 !_tnl_CreateContext( ctx
) ||
965 !_swsetup_CreateContext( ctx
)) {
966 _mesa_destroy_visual(osmesa
->gl_visual
);
967 _mesa_free_context_data(ctx
);
972 _swsetup_Wakeup( ctx
);
974 /* use default TCL pipeline */
975 tnl
= TNL_CONTEXT(ctx
);
976 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
978 swdd
= _swrast_GetDeviceDriverReference( ctx
);
979 swdd
->SetBuffer
= set_buffer
;
982 swdd
->WriteRGBASpan
= NULL
;
983 swdd
->WriteRGBSpan
= NULL
;
984 swdd
->WriteMonoRGBASpan
= NULL
;
985 swdd
->WriteRGBAPixels
= NULL
;
986 swdd
->WriteMonoRGBAPixels
= NULL
;
987 swdd
->ReadRGBASpan
= NULL
;
988 swdd
->ReadRGBAPixels
= NULL
;
990 /* Extend the software rasterizer with our optimized line and triangle
993 swrast
= SWRAST_CONTEXT( ctx
);
994 swrast
->choose_line
= osmesa_choose_line
;
995 swrast
->choose_triangle
= osmesa_choose_triangle
;
996 swrast
->invalidate_line
|= OSMESA_NEW_LINE
;
997 swrast
->invalidate_triangle
|= OSMESA_NEW_TRIANGLE
;
1005 * Destroy an Off-Screen Mesa rendering context.
1007 * Input: ctx - the context to destroy
1009 GLAPI
void GLAPIENTRY
1010 OSMesaDestroyContext( OSMesaContext ctx
)
1013 _swsetup_DestroyContext( &ctx
->mesa
);
1014 _tnl_DestroyContext( &ctx
->mesa
);
1015 _ac_DestroyContext( &ctx
->mesa
);
1016 _swrast_DestroyContext( &ctx
->mesa
);
1018 _mesa_destroy_visual( ctx
->gl_visual
);
1019 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
1020 _mesa_free_context_data( &ctx
->mesa
);
1027 * Recompute the values of the context's rowaddr array.
1030 compute_row_addresses( OSMesaContext ctx
)
1032 GLint bytesPerPixel
, bytesPerRow
, i
;
1033 GLubyte
*origin
= (GLubyte
*) ctx
->buffer
;
1035 if (ctx
->format
== OSMESA_COLOR_INDEX
) {
1037 bytesPerPixel
= 1 * sizeof(GLchan
);
1039 else if ((ctx
->format
== OSMESA_RGB
) || (ctx
->format
== OSMESA_BGR
)) {
1041 bytesPerPixel
= 3 * sizeof(GLchan
);
1043 else if (ctx
->format
== OSMESA_RGB_565
) {
1044 /* 5/6/5 RGB pixel in 16 bits */
1049 bytesPerPixel
= 4 * sizeof(GLchan
);
1052 bytesPerRow
= ctx
->rowlength
* bytesPerPixel
;
1055 /* Y=0 is bottom line of window */
1056 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1057 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
1061 /* Y=0 is top line of window */
1062 for (i
= 0; i
< MAX_HEIGHT
; i
++) {
1063 GLint j
= ctx
->height
- i
- 1;
1064 ctx
->rowaddr
[i
] = (GLchan
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
1071 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1072 * block of memory which the client provides. Its size must be at least
1073 * as large as width*height*sizeof(type). Its address should be a multiple
1074 * of 4 if using RGBA mode.
1076 * Image data is stored in the order of glDrawPixels: row-major order
1077 * with the lower-left image pixel stored in the first array position
1078 * (ie. bottom-to-top).
1080 * If the context's viewport hasn't been initialized yet, it will now be
1081 * initialized to (0,0,width,height).
1083 * Input: ctx - the rendering context
1084 * buffer - the image buffer memory
1085 * type - data type for pixel components
1086 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1087 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1088 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
1089 * with CHAN_BITS==32 then type must be GL_FLOAT.
1090 * width, height - size of image buffer in pixels, at least 1
1091 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
1092 * invalid buffer address, invalid type, width<1, height<1,
1093 * width>internal limit or height>internal limit.
1095 GLAPI GLboolean GLAPIENTRY
1096 OSMesaMakeCurrent( OSMesaContext ctx
, void *buffer
, GLenum type
,
1097 GLsizei width
, GLsizei height
)
1099 if (!ctx
|| !buffer
||
1100 width
< 1 || height
< 1 ||
1101 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1105 if (ctx
->format
== OSMESA_RGB_565
) {
1106 if (type
!= GL_UNSIGNED_SHORT_5_6_5
)
1109 else if (type
!= CHAN_TYPE
) {
1113 /* Need to set these before calling _mesa_make_current() since the first
1114 * time the context is bound, _mesa_make_current() will call our
1115 * get_buffer_size() function to initialize the viewport. These are the
1116 * values returned by get_buffer_size():
1118 ctx
->buffer
= buffer
;
1120 ctx
->height
= height
;
1122 osmesa_update_state( &ctx
->mesa
, 0 );
1123 _mesa_make_current( &ctx
->mesa
, ctx
->gl_buffer
, ctx
->gl_buffer
);
1125 if (ctx
->userRowLength
)
1126 ctx
->rowlength
= ctx
->userRowLength
;
1128 ctx
->rowlength
= width
;
1130 compute_row_addresses( ctx
);
1132 /* this will make ensure we recognize the new buffer size */
1133 _mesa_resize_framebuffer(&ctx
->mesa
, ctx
->gl_buffer
, width
, height
);
1135 _mesa_ResizeBuffersMESA();
1138 /* Added by Gerk Huisma: */
1139 _tnl_MakeCurrent( &ctx
->mesa
, ctx
->mesa
.DrawBuffer
,
1140 ctx
->mesa
.ReadBuffer
);
1147 GLAPI OSMesaContext GLAPIENTRY
1148 OSMesaGetCurrentContext( void )
1150 GLcontext
*ctx
= _mesa_get_current_context();
1152 return (OSMesaContext
) ctx
;
1159 GLAPI
void GLAPIENTRY
1160 OSMesaPixelStore( GLint pname
, GLint value
)
1162 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1165 case OSMESA_ROW_LENGTH
:
1167 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1168 "OSMesaPixelStore(value)" );
1171 osmesa
->userRowLength
= value
;
1172 osmesa
->rowlength
= value
? value
: osmesa
->width
;
1175 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1178 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1182 compute_row_addresses( osmesa
);
1186 GLAPI
void GLAPIENTRY
1187 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1189 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1193 *value
= osmesa
->width
;
1196 *value
= osmesa
->height
;
1199 *value
= osmesa
->format
;
1204 case OSMESA_ROW_LENGTH
:
1205 *value
= osmesa
->userRowLength
;
1208 *value
= osmesa
->yup
;
1210 case OSMESA_MAX_WIDTH
:
1213 case OSMESA_MAX_HEIGHT
:
1214 *value
= MAX_HEIGHT
;
1217 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1223 * Return the depth buffer associated with an OSMesa context.
1224 * Input: c - the OSMesa context
1225 * Output: width, height - size of buffer in pixels
1226 * bytesPerValue - bytes per depth value (2 or 4)
1227 * buffer - pointer to depth buffer values
1228 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1230 GLAPI GLboolean GLAPIENTRY
1231 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1232 GLint
*bytesPerValue
, void **buffer
)
1234 struct gl_renderbuffer
*rb
= NULL
;
1237 rb
= c
->gl_buffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1239 if (!rb
|| !rb
->Data
) {
1240 /*if ((!c->gl_buffer) || (!c->gl_buffer->DepthBuffer)) {*/
1248 *width
= c
->gl_buffer
->Width
;
1249 *height
= c
->gl_buffer
->Height
;
1250 if (c
->gl_visual
->depthBits
<= 16)
1251 *bytesPerValue
= sizeof(GLushort
);
1253 *bytesPerValue
= sizeof(GLuint
);
1260 * Return the color buffer associated with an OSMesa context.
1261 * Input: c - the OSMesa context
1262 * Output: width, height - size of buffer in pixels
1263 * format - the pixel format (OSMESA_FORMAT)
1264 * buffer - pointer to color buffer values
1265 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1267 GLAPI GLboolean GLAPIENTRY
1268 OSMesaGetColorBuffer( OSMesaContext c
, GLint
*width
,
1269 GLint
*height
, GLint
*format
, void **buffer
)
1280 *height
= c
->height
;
1281 *format
= c
->format
;
1282 *buffer
= c
->buffer
;
1288 struct name_function
1291 OSMESAproc Function
;
1294 static struct name_function functions
[] = {
1295 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1296 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1297 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1298 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1299 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1300 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1301 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1302 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1303 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1304 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1309 GLAPI OSMESAproc GLAPIENTRY
1310 OSMesaGetProcAddress( const char *funcName
)
1313 for (i
= 0; functions
[i
].Name
; i
++) {
1314 if (_mesa_strcmp(functions
[i
].Name
, funcName
) == 0)
1315 return functions
[i
].Function
;
1317 return _glapi_get_proc_address(funcName
);