2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Off-Screen Mesa rendering / Rendering into client memory space
29 * Note on thread safety: this driver is thread safe. All
30 * functions are reentrant. The notion of current context is
31 * managed by the core _mesa_make_current() and _mesa_get_current_context()
32 * functions. Those functions are thread-safe.
36 #include "main/glheader.h"
37 #include "GL/osmesa.h"
38 #include "main/context.h"
39 #include "main/extensions.h"
40 #include "main/formats.h"
41 #include "main/framebuffer.h"
42 #include "main/imports.h"
43 #include "main/mtypes.h"
44 #include "main/renderbuffer.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "swrast/s_context.h"
48 #include "swrast/s_lines.h"
49 #include "swrast/s_renderbuffer.h"
50 #include "swrast/s_triangle.h"
52 #include "tnl/t_context.h"
53 #include "tnl/t_pipeline.h"
54 #include "drivers/common/driverfuncs.h"
55 #include "drivers/common/meta.h"
59 #define OSMESA_RENDERBUFFER_CLASS 0x053
63 * OSMesa rendering context, derived from core Mesa struct gl_context.
67 struct gl_context mesa
; /*< Base class - this must be first */
68 struct gl_config
*gl_visual
; /*< Describes the buffers */
69 struct gl_renderbuffer
*rb
; /*< The user's colorbuffer */
70 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
71 GLenum format
; /*< User-specified context format */
72 GLint userRowLength
; /*< user-specified number of pixels per row */
73 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
74 GLvoid
*rowaddr
[MAX_HEIGHT
]; /*< address of first pixel in each image row */
75 GLboolean yup
; /*< TRUE -> Y increases upward */
76 /*< FALSE -> Y increases downward */
80 static INLINE OSMesaContext
81 OSMESA_CONTEXT(struct gl_context
*ctx
)
83 /* Just cast, since we're using structure containment */
84 return (OSMesaContext
) ctx
;
88 /**********************************************************************/
89 /*** Private Device Driver Functions ***/
90 /**********************************************************************/
93 static const GLubyte
*
94 get_string( struct gl_context
*ctx
, GLenum name
)
100 return (const GLubyte
*) "Mesa OffScreen32";
101 #elif CHAN_BITS == 16
102 return (const GLubyte
*) "Mesa OffScreen16";
104 return (const GLubyte
*) "Mesa OffScreen";
113 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
115 /* easy - just propogate */
116 _swrast_InvalidateState( ctx
, new_state
);
117 _swsetup_InvalidateState( ctx
, new_state
);
118 _tnl_InvalidateState( ctx
, new_state
);
119 _vbo_InvalidateState( ctx
, new_state
);
124 /**********************************************************************/
125 /***** Read/write spans/arrays of pixels *****/
126 /**********************************************************************/
129 #define NAME(PREFIX) PREFIX##_RGBA8
130 #define RB_TYPE GLubyte
132 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
133 #define INIT_PIXEL_PTR(P, X, Y) \
134 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
135 #define INC_PIXEL_PTR(P) P += 4
136 #define STORE_PIXEL(DST, X, Y, VALUE) \
137 DST[0] = VALUE[RCOMP]; \
138 DST[1] = VALUE[GCOMP]; \
139 DST[2] = VALUE[BCOMP]; \
140 DST[3] = VALUE[ACOMP]
141 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
142 DST[0] = VALUE[RCOMP]; \
143 DST[1] = VALUE[GCOMP]; \
144 DST[2] = VALUE[BCOMP]; \
146 #define FETCH_PIXEL(DST, SRC) \
147 DST[RCOMP] = SRC[0]; \
148 DST[GCOMP] = SRC[1]; \
149 DST[BCOMP] = SRC[2]; \
151 #include "swrast/s_spantemp.h"
154 #define NAME(PREFIX) PREFIX##_RGBA16
155 #define RB_TYPE GLushort
157 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
158 #define INIT_PIXEL_PTR(P, X, Y) \
159 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
160 #define INC_PIXEL_PTR(P) P += 4
161 #define STORE_PIXEL(DST, X, Y, VALUE) \
162 DST[0] = VALUE[RCOMP]; \
163 DST[1] = VALUE[GCOMP]; \
164 DST[2] = VALUE[BCOMP]; \
165 DST[3] = VALUE[ACOMP]
166 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
167 DST[0] = VALUE[RCOMP]; \
168 DST[1] = VALUE[GCOMP]; \
169 DST[2] = VALUE[BCOMP]; \
171 #define FETCH_PIXEL(DST, SRC) \
172 DST[RCOMP] = SRC[0]; \
173 DST[GCOMP] = SRC[1]; \
174 DST[BCOMP] = SRC[2]; \
176 #include "swrast/s_spantemp.h"
179 #define NAME(PREFIX) PREFIX##_RGBA32
180 #define RB_TYPE GLfloat
182 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
183 #define INIT_PIXEL_PTR(P, X, Y) \
184 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
185 #define INC_PIXEL_PTR(P) P += 4
186 #define STORE_PIXEL(DST, X, Y, VALUE) \
187 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
188 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
189 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
190 DST[3] = CLAMP((VALUE[ACOMP]), 0.0F, 1.0F)
191 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
192 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
193 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
194 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
196 #define FETCH_PIXEL(DST, SRC) \
197 DST[RCOMP] = SRC[0]; \
198 DST[GCOMP] = SRC[1]; \
199 DST[BCOMP] = SRC[2]; \
201 #include "swrast/s_spantemp.h"
205 #define NAME(PREFIX) PREFIX##_BGRA8
206 #define RB_TYPE GLubyte
208 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
209 #define INIT_PIXEL_PTR(P, X, Y) \
210 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
211 #define INC_PIXEL_PTR(P) P += 4
212 #define STORE_PIXEL(DST, X, Y, VALUE) \
213 DST[2] = VALUE[RCOMP]; \
214 DST[1] = VALUE[GCOMP]; \
215 DST[0] = VALUE[BCOMP]; \
216 DST[3] = VALUE[ACOMP]
217 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
218 DST[2] = VALUE[RCOMP]; \
219 DST[1] = VALUE[GCOMP]; \
220 DST[0] = VALUE[BCOMP]; \
222 #define FETCH_PIXEL(DST, SRC) \
223 DST[RCOMP] = SRC[2]; \
224 DST[GCOMP] = SRC[1]; \
225 DST[BCOMP] = SRC[0]; \
227 #include "swrast/s_spantemp.h"
230 #define NAME(PREFIX) PREFIX##_BGRA16
231 #define RB_TYPE GLushort
233 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
234 #define INIT_PIXEL_PTR(P, X, Y) \
235 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
236 #define INC_PIXEL_PTR(P) P += 4
237 #define STORE_PIXEL(DST, X, Y, VALUE) \
238 DST[2] = VALUE[RCOMP]; \
239 DST[1] = VALUE[GCOMP]; \
240 DST[0] = VALUE[BCOMP]; \
241 DST[3] = VALUE[ACOMP]
242 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
243 DST[2] = VALUE[RCOMP]; \
244 DST[1] = VALUE[GCOMP]; \
245 DST[0] = VALUE[BCOMP]; \
247 #define FETCH_PIXEL(DST, SRC) \
248 DST[RCOMP] = SRC[2]; \
249 DST[GCOMP] = SRC[1]; \
250 DST[BCOMP] = SRC[0]; \
252 #include "swrast/s_spantemp.h"
255 #define NAME(PREFIX) PREFIX##_BGRA32
256 #define RB_TYPE GLfloat
258 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
259 #define INIT_PIXEL_PTR(P, X, Y) \
260 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
261 #define INC_PIXEL_PTR(P) P += 4
262 #define STORE_PIXEL(DST, X, Y, VALUE) \
263 DST[2] = VALUE[RCOMP]; \
264 DST[1] = VALUE[GCOMP]; \
265 DST[0] = VALUE[BCOMP]; \
266 DST[3] = VALUE[ACOMP]
267 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
268 DST[2] = VALUE[RCOMP]; \
269 DST[1] = VALUE[GCOMP]; \
270 DST[0] = VALUE[BCOMP]; \
272 #define FETCH_PIXEL(DST, SRC) \
273 DST[RCOMP] = SRC[2]; \
274 DST[GCOMP] = SRC[1]; \
275 DST[BCOMP] = SRC[0]; \
277 #include "swrast/s_spantemp.h"
281 #define NAME(PREFIX) PREFIX##_ARGB8
282 #define RB_TYPE GLubyte
284 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
285 #define INIT_PIXEL_PTR(P, X, Y) \
286 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
287 #define INC_PIXEL_PTR(P) P += 4
288 #define STORE_PIXEL(DST, X, Y, VALUE) \
289 DST[1] = VALUE[RCOMP]; \
290 DST[2] = VALUE[GCOMP]; \
291 DST[3] = VALUE[BCOMP]; \
292 DST[0] = VALUE[ACOMP]
293 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
294 DST[1] = VALUE[RCOMP]; \
295 DST[2] = VALUE[GCOMP]; \
296 DST[3] = VALUE[BCOMP]; \
298 #define FETCH_PIXEL(DST, SRC) \
299 DST[RCOMP] = SRC[1]; \
300 DST[GCOMP] = SRC[2]; \
301 DST[BCOMP] = SRC[3]; \
303 #include "swrast/s_spantemp.h"
306 #define NAME(PREFIX) PREFIX##_ARGB16
307 #define RB_TYPE GLushort
309 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
310 #define INIT_PIXEL_PTR(P, X, Y) \
311 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
312 #define INC_PIXEL_PTR(P) P += 4
313 #define STORE_PIXEL(DST, X, Y, VALUE) \
314 DST[1] = VALUE[RCOMP]; \
315 DST[2] = VALUE[GCOMP]; \
316 DST[3] = VALUE[BCOMP]; \
317 DST[0] = VALUE[ACOMP]
318 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
319 DST[1] = VALUE[RCOMP]; \
320 DST[2] = VALUE[GCOMP]; \
321 DST[3] = VALUE[BCOMP]; \
323 #define FETCH_PIXEL(DST, SRC) \
324 DST[RCOMP] = SRC[1]; \
325 DST[GCOMP] = SRC[2]; \
326 DST[BCOMP] = SRC[3]; \
328 #include "swrast/s_spantemp.h"
331 #define NAME(PREFIX) PREFIX##_ARGB32
332 #define RB_TYPE GLfloat
334 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
335 #define INIT_PIXEL_PTR(P, X, Y) \
336 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
337 #define INC_PIXEL_PTR(P) P += 4
338 #define STORE_PIXEL(DST, X, Y, VALUE) \
339 DST[1] = VALUE[RCOMP]; \
340 DST[2] = VALUE[GCOMP]; \
341 DST[3] = VALUE[BCOMP]; \
342 DST[0] = VALUE[ACOMP]
343 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
344 DST[1] = VALUE[RCOMP]; \
345 DST[2] = VALUE[GCOMP]; \
346 DST[3] = VALUE[BCOMP]; \
348 #define FETCH_PIXEL(DST, SRC) \
349 DST[RCOMP] = SRC[1]; \
350 DST[GCOMP] = SRC[2]; \
351 DST[BCOMP] = SRC[3]; \
353 #include "swrast/s_spantemp.h"
357 #define NAME(PREFIX) PREFIX##_RGB8
358 #define RB_TYPE GLubyte
360 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
361 #define INIT_PIXEL_PTR(P, X, Y) \
362 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
363 #define INC_PIXEL_PTR(P) P += 3
364 #define STORE_PIXEL(DST, X, Y, VALUE) \
365 DST[0] = VALUE[RCOMP]; \
366 DST[1] = VALUE[GCOMP]; \
367 DST[2] = VALUE[BCOMP]
368 #define FETCH_PIXEL(DST, SRC) \
369 DST[RCOMP] = SRC[0]; \
370 DST[GCOMP] = SRC[1]; \
371 DST[BCOMP] = SRC[2]; \
373 #include "swrast/s_spantemp.h"
376 #define NAME(PREFIX) PREFIX##_RGB16
377 #define RB_TYPE GLushort
379 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
380 #define INIT_PIXEL_PTR(P, X, Y) \
381 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
382 #define INC_PIXEL_PTR(P) P += 3
383 #define STORE_PIXEL(DST, X, Y, VALUE) \
384 DST[0] = VALUE[RCOMP]; \
385 DST[1] = VALUE[GCOMP]; \
386 DST[2] = VALUE[BCOMP]
387 #define FETCH_PIXEL(DST, SRC) \
388 DST[RCOMP] = SRC[0]; \
389 DST[GCOMP] = SRC[1]; \
390 DST[BCOMP] = SRC[2]; \
392 #include "swrast/s_spantemp.h"
395 #define NAME(PREFIX) PREFIX##_RGB32
396 #define RB_TYPE GLfloat
398 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
399 #define INIT_PIXEL_PTR(P, X, Y) \
400 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
401 #define INC_PIXEL_PTR(P) P += 3
402 #define STORE_PIXEL(DST, X, Y, VALUE) \
403 DST[0] = VALUE[RCOMP]; \
404 DST[1] = VALUE[GCOMP]; \
405 DST[2] = VALUE[BCOMP]
406 #define FETCH_PIXEL(DST, SRC) \
407 DST[RCOMP] = SRC[0]; \
408 DST[GCOMP] = SRC[1]; \
409 DST[BCOMP] = SRC[2]; \
411 #include "swrast/s_spantemp.h"
415 #define NAME(PREFIX) PREFIX##_BGR8
416 #define RB_TYPE GLubyte
418 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
419 #define INIT_PIXEL_PTR(P, X, Y) \
420 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
421 #define INC_PIXEL_PTR(P) P += 3
422 #define STORE_PIXEL(DST, X, Y, VALUE) \
423 DST[2] = VALUE[RCOMP]; \
424 DST[1] = VALUE[GCOMP]; \
425 DST[0] = VALUE[BCOMP]
426 #define FETCH_PIXEL(DST, SRC) \
427 DST[RCOMP] = SRC[2]; \
428 DST[GCOMP] = SRC[1]; \
429 DST[BCOMP] = SRC[0]; \
431 #include "swrast/s_spantemp.h"
434 #define NAME(PREFIX) PREFIX##_BGR16
435 #define RB_TYPE GLushort
437 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
438 #define INIT_PIXEL_PTR(P, X, Y) \
439 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
440 #define INC_PIXEL_PTR(P) P += 3
441 #define STORE_PIXEL(DST, X, Y, VALUE) \
442 DST[2] = VALUE[RCOMP]; \
443 DST[1] = VALUE[GCOMP]; \
444 DST[0] = VALUE[BCOMP]
445 #define FETCH_PIXEL(DST, SRC) \
446 DST[RCOMP] = SRC[2]; \
447 DST[GCOMP] = SRC[1]; \
448 DST[BCOMP] = SRC[0]; \
450 #include "swrast/s_spantemp.h"
453 #define NAME(PREFIX) PREFIX##_BGR32
454 #define RB_TYPE GLfloat
456 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
457 #define INIT_PIXEL_PTR(P, X, Y) \
458 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
459 #define INC_PIXEL_PTR(P) P += 3
460 #define STORE_PIXEL(DST, X, Y, VALUE) \
461 DST[2] = VALUE[RCOMP]; \
462 DST[1] = VALUE[GCOMP]; \
463 DST[0] = VALUE[BCOMP]
464 #define FETCH_PIXEL(DST, SRC) \
465 DST[RCOMP] = SRC[2]; \
466 DST[GCOMP] = SRC[1]; \
467 DST[BCOMP] = SRC[0]; \
469 #include "swrast/s_spantemp.h"
472 /* 16-bit 5/6/5 RGB */
473 #define NAME(PREFIX) PREFIX##_RGB_565
474 #define RB_TYPE GLubyte
476 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
477 #define INIT_PIXEL_PTR(P, X, Y) \
478 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
479 #define INC_PIXEL_PTR(P) P += 1
480 #define STORE_PIXEL(DST, X, Y, VALUE) \
481 *DST = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
482 #define FETCH_PIXEL(DST, SRC) \
483 DST[RCOMP] = ( (((*SRC) >> 8) & 0xf8) | (((*SRC) >> 11) & 0x7) ); \
484 DST[GCOMP] = ( (((*SRC) >> 3) & 0xfc) | (((*SRC) >> 5) & 0x3) ); \
485 DST[BCOMP] = ( (((*SRC) << 3) & 0xf8) | (((*SRC) ) & 0x7) ); \
486 DST[ACOMP] = CHAN_MAX
487 #include "swrast/s_spantemp.h"
491 * Macros for optimized line/triangle rendering.
492 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
495 #define PACK_RGBA(DST, R, G, B, A) \
497 (DST)[osmesa->rInd] = R; \
498 (DST)[osmesa->gInd] = G; \
499 (DST)[osmesa->bInd] = B; \
500 (DST)[osmesa->aInd] = A; \
503 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
507 * Draw a flat-shaded, RGB line into an osmesa buffer.
509 #define NAME flat_rgba_line
512 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
513 const GLchan *color = vert1->color;
517 GLchan *p = PIXELADDR4(X, Y); \
518 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
521 #include "swrast/s_linetemp.h"
526 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
528 #define NAME flat_rgba_z_line
531 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
533 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
534 const GLchan *color = vert1->color;
539 GLchan *p = PIXELADDR4(X, Y); \
540 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
541 color[BCOMP], color[ACOMP]); \
546 #include "swrast/s_linetemp.h"
551 * Analyze context state to see if we can provide a fast line drawing
552 * function. Otherwise, return NULL.
554 static swrast_line_func
555 osmesa_choose_line_function( struct gl_context
*ctx
)
557 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
558 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
560 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
563 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
564 if (ctx
->Line
.SmoothFlag
) return NULL
;
565 if (ctx
->Texture
._EnabledUnits
) return NULL
;
566 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
567 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
568 if (ctx
->Line
.StippleFlag
) return NULL
;
569 if (ctx
->Line
.SmoothFlag
) return NULL
;
570 if (osmesa
->format
!= OSMESA_RGBA
&&
571 osmesa
->format
!= OSMESA_BGRA
&&
572 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
574 if (swrast
->_RasterMask
==DEPTH_BIT
575 && ctx
->Depth
.Func
==GL_LESS
576 && ctx
->Depth
.Mask
==GL_TRUE
577 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
578 return (swrast_line_func
) flat_rgba_z_line
;
581 if (swrast
->_RasterMask
== 0) {
582 return (swrast_line_func
) flat_rgba_line
;
585 return (swrast_line_func
) NULL
;
589 /**********************************************************************/
590 /***** Optimized triangle rendering *****/
591 /**********************************************************************/
595 * Smooth-shaded, z-less triangle, RGBA color.
597 #define NAME smooth_rgba_z_triangle
599 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
601 #define INTERP_ALPHA 1
603 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
604 #define RENDER_SPAN( span ) { \
606 GLchan *img = PIXELADDR4(span.x, span.y); \
607 for (i = 0; i < span.end; i++, img += 4) { \
608 const GLuint z = FixedToDepth(span.z); \
610 PACK_RGBA(img, FixedToChan(span.red), \
611 FixedToChan(span.green), FixedToChan(span.blue), \
612 FixedToChan(span.alpha)); \
615 span.red += span.redStep; \
616 span.green += span.greenStep; \
617 span.blue += span.blueStep; \
618 span.alpha += span.alphaStep; \
619 span.z += span.zStep; \
622 #include "swrast/s_tritemp.h"
627 * Flat-shaded, z-less triangle, RGBA color.
629 #define NAME flat_rgba_z_triangle
631 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
633 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
635 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
636 v2->color[2], v2->color[3]);
638 #define RENDER_SPAN( span ) { \
640 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
641 for (i = 0; i < span.end; i++) { \
642 const GLuint z = FixedToDepth(span.z); \
647 span.z += span.zStep; \
651 #include "swrast/s_tritemp.h"
656 * Return pointer to an optimized triangle function if possible.
658 static swrast_tri_func
659 osmesa_choose_triangle_function( struct gl_context
*ctx
)
661 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
662 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
664 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
665 return (swrast_tri_func
) NULL
;
667 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
668 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
669 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
670 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
671 if (osmesa
->format
!= OSMESA_RGBA
&&
672 osmesa
->format
!= OSMESA_BGRA
&&
673 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
674 if (ctx
->Polygon
.CullFlag
&&
675 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
676 return (swrast_tri_func
) NULL
;
678 if (swrast
->_RasterMask
== DEPTH_BIT
&&
679 ctx
->Depth
.Func
== GL_LESS
&&
680 ctx
->Depth
.Mask
== GL_TRUE
&&
681 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
682 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
683 return (swrast_tri_func
) smooth_rgba_z_triangle
;
686 return (swrast_tri_func
) flat_rgba_z_triangle
;
689 return (swrast_tri_func
) NULL
;
694 /* Override for the swrast triangle-selection function. Try to use one
695 * of our internal triangle functions, otherwise fall back to the
696 * standard swrast functions.
699 osmesa_choose_triangle( struct gl_context
*ctx
)
701 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
703 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
704 if (!swrast
->Triangle
)
705 _swrast_choose_triangle( ctx
);
709 osmesa_choose_line( struct gl_context
*ctx
)
711 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
713 swrast
->Line
= osmesa_choose_line_function( ctx
);
715 _swrast_choose_line( ctx
);
721 * Recompute the values of the context's rowaddr array.
724 compute_row_addresses( OSMesaContext osmesa
)
726 GLint bytesPerPixel
, bytesPerRow
, i
;
727 GLubyte
*origin
= (GLubyte
*) osmesa
->rb
->Data
;
728 GLint bpc
; /* bytes per channel */
729 GLint rowlength
; /* in pixels */
730 GLint height
= osmesa
->rb
->Height
;
732 if (osmesa
->userRowLength
)
733 rowlength
= osmesa
->userRowLength
;
735 rowlength
= osmesa
->rb
->Width
;
737 if (osmesa
->rb
->DataType
== GL_UNSIGNED_BYTE
)
739 else if (osmesa
->rb
->DataType
== GL_UNSIGNED_SHORT
)
741 else if (osmesa
->rb
->DataType
== GL_FLOAT
)
744 _mesa_problem(&osmesa
->mesa
,
745 "Unexpected datatype in osmesa::compute_row_addresses");
749 if ((osmesa
->format
== OSMESA_RGB
) || (osmesa
->format
== OSMESA_BGR
)) {
751 bytesPerPixel
= 3 * bpc
;
753 else if (osmesa
->format
== OSMESA_RGB_565
) {
754 /* 5/6/5 RGB pixel in 16 bits */
759 bytesPerPixel
= 4 * bpc
;
762 bytesPerRow
= rowlength
* bytesPerPixel
;
765 /* Y=0 is bottom line of window */
766 for (i
= 0; i
< height
; i
++) {
767 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
771 /* Y=0 is top line of window */
772 for (i
= 0; i
< height
; i
++) {
773 GLint j
= height
- i
- 1;
774 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
782 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
785 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
792 * Allocate renderbuffer storage. We don't actually allocate any storage
793 * since we're using a user-provided buffer.
794 * Just set up all the gl_renderbuffer methods.
797 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
798 GLenum internalFormat
, GLuint width
, GLuint height
)
800 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
802 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
803 (void) internalFormat
;
805 if (osmesa
->format
== OSMESA_RGBA
) {
806 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
807 rb
->GetRow
= get_row_RGBA8
;
808 rb
->GetValues
= get_values_RGBA8
;
809 rb
->PutRow
= put_row_RGBA8
;
810 rb
->PutValues
= put_values_RGBA8
;
812 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
813 rb
->GetRow
= get_row_RGBA16
;
814 rb
->GetValues
= get_values_RGBA16
;
815 rb
->PutRow
= put_row_RGBA16
;
816 rb
->PutValues
= put_values_RGBA16
;
819 rb
->GetRow
= get_row_RGBA32
;
820 rb
->GetValues
= get_values_RGBA32
;
821 rb
->PutRow
= put_row_RGBA32
;
822 rb
->PutValues
= put_values_RGBA32
;
825 else if (osmesa
->format
== OSMESA_BGRA
) {
826 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
827 rb
->GetRow
= get_row_BGRA8
;
828 rb
->GetValues
= get_values_BGRA8
;
829 rb
->PutRow
= put_row_BGRA8
;
830 rb
->PutValues
= put_values_BGRA8
;
832 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
833 rb
->GetRow
= get_row_BGRA16
;
834 rb
->GetValues
= get_values_BGRA16
;
835 rb
->PutRow
= put_row_BGRA16
;
836 rb
->PutValues
= put_values_BGRA16
;
839 rb
->GetRow
= get_row_BGRA32
;
840 rb
->GetValues
= get_values_BGRA32
;
841 rb
->PutRow
= put_row_BGRA32
;
842 rb
->PutValues
= put_values_BGRA32
;
845 else if (osmesa
->format
== OSMESA_ARGB
) {
846 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
847 rb
->GetRow
= get_row_ARGB8
;
848 rb
->GetValues
= get_values_ARGB8
;
849 rb
->PutRow
= put_row_ARGB8
;
850 rb
->PutValues
= put_values_ARGB8
;
852 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
853 rb
->GetRow
= get_row_ARGB16
;
854 rb
->GetValues
= get_values_ARGB16
;
855 rb
->PutRow
= put_row_ARGB16
;
856 rb
->PutValues
= put_values_ARGB16
;
859 rb
->GetRow
= get_row_ARGB32
;
860 rb
->GetValues
= get_values_ARGB32
;
861 rb
->PutRow
= put_row_ARGB32
;
862 rb
->PutValues
= put_values_ARGB32
;
865 else if (osmesa
->format
== OSMESA_RGB
) {
866 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
867 rb
->GetRow
= get_row_RGB8
;
868 rb
->GetValues
= get_values_RGB8
;
869 rb
->PutRow
= put_row_RGB8
;
870 rb
->PutValues
= put_values_RGB8
;
872 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
873 rb
->GetRow
= get_row_RGB16
;
874 rb
->GetValues
= get_values_RGB16
;
875 rb
->PutRow
= put_row_RGB16
;
876 rb
->PutValues
= put_values_RGB16
;
879 rb
->GetRow
= get_row_RGB32
;
880 rb
->GetValues
= get_values_RGB32
;
881 rb
->PutRow
= put_row_RGB32
;
882 rb
->PutValues
= put_values_RGB32
;
885 else if (osmesa
->format
== OSMESA_BGR
) {
886 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
887 rb
->GetRow
= get_row_BGR8
;
888 rb
->GetValues
= get_values_BGR8
;
889 rb
->PutRow
= put_row_BGR8
;
890 rb
->PutValues
= put_values_BGR8
;
892 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
893 rb
->GetRow
= get_row_BGR16
;
894 rb
->GetValues
= get_values_BGR16
;
895 rb
->PutRow
= put_row_BGR16
;
896 rb
->PutValues
= put_values_BGR16
;
899 rb
->GetRow
= get_row_BGR32
;
900 rb
->GetValues
= get_values_BGR32
;
901 rb
->PutRow
= put_row_BGR32
;
902 rb
->PutValues
= put_values_BGR32
;
905 else if (osmesa
->format
== OSMESA_RGB_565
) {
906 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
907 rb
->GetRow
= get_row_RGB_565
;
908 rb
->GetValues
= get_values_RGB_565
;
909 rb
->PutRow
= put_row_RGB_565
;
910 rb
->PutValues
= put_values_RGB_565
;
913 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
919 compute_row_addresses( osmesa
);
926 * Allocate a new renderbuffer to describe the user-provided color buffer.
928 static struct gl_renderbuffer
*
929 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
931 const GLuint name
= 0;
932 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, name
);
935 rb
->Delete
= osmesa_delete_renderbuffer
;
936 rb
->AllocStorage
= osmesa_renderbuffer_storage
;
937 rb
->ClassID
= OSMESA_RENDERBUFFER_CLASS
;
939 rb
->InternalFormat
= GL_RGBA
;
941 case GL_UNSIGNED_BYTE
:
942 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
944 case GL_UNSIGNED_SHORT
:
945 rb
->Format
= MESA_FORMAT_RGBA_16
;
947 case GL_UNSIGNED_SHORT_5_6_5
:
948 rb
->Format
= MESA_FORMAT_RGB565
;
949 type
= GL_UNSIGNED_BYTE
;
952 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
955 assert(0 && "Unexpected type in new_osmesa_renderbuffer()");
956 rb
->Format
= MESA_FORMAT_RGBA8888
;
958 rb
->_BaseFormat
= GL_RGBA
;
967 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
968 struct gl_renderbuffer
*rb
,
969 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
971 GLubyte
**mapOut
, GLint
*rowStrideOut
)
973 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
975 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
976 /* this is an OSMesa renderbuffer which wraps user memory */
977 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
978 GLint rowStride
; /* in bytes */
980 if (osmesa
->userRowLength
)
981 rowStride
= osmesa
->userRowLength
* bpp
;
983 rowStride
= rb
->Width
* bpp
;
986 /* Y=0 is top line of window */
987 y
= rb
->Height
- y
- 1;
988 *rowStrideOut
= -rowStride
;
991 *rowStrideOut
= rowStride
;
994 *mapOut
= (GLubyte
*) rb
->Data
+ y
* rowStride
+ x
* bpp
;
997 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
998 mapOut
, rowStrideOut
);
1004 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
1006 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
1010 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
1015 /**********************************************************************/
1016 /***** Public Functions *****/
1017 /**********************************************************************/
1021 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1022 * an RGBA vs Color-Index mode flag.
1024 * Input: format - Must be GL_RGBA
1025 * sharelist - specifies another OSMesaContext with which to share
1026 * display lists. NULL indicates no sharing.
1027 * Return: an OSMesaContext or 0 if error
1029 GLAPI OSMesaContext GLAPIENTRY
1030 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
1032 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
1041 * Create context and specify size of ancillary buffers.
1043 GLAPI OSMesaContext GLAPIENTRY
1044 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
1045 GLint accumBits
, OSMesaContext sharelist
)
1047 OSMesaContext osmesa
;
1048 struct dd_function_table functions
;
1049 GLint rind
, gind
, bind
, aind
;
1050 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
1052 rind
= gind
= bind
= aind
= 0;
1053 if (format
==OSMESA_RGBA
) {
1054 redBits
= CHAN_BITS
;
1055 greenBits
= CHAN_BITS
;
1056 blueBits
= CHAN_BITS
;
1057 alphaBits
= CHAN_BITS
;
1063 else if (format
==OSMESA_BGRA
) {
1064 redBits
= CHAN_BITS
;
1065 greenBits
= CHAN_BITS
;
1066 blueBits
= CHAN_BITS
;
1067 alphaBits
= CHAN_BITS
;
1073 else if (format
==OSMESA_ARGB
) {
1074 redBits
= CHAN_BITS
;
1075 greenBits
= CHAN_BITS
;
1076 blueBits
= CHAN_BITS
;
1077 alphaBits
= CHAN_BITS
;
1083 else if (format
==OSMESA_RGB
) {
1084 redBits
= CHAN_BITS
;
1085 greenBits
= CHAN_BITS
;
1086 blueBits
= CHAN_BITS
;
1092 else if (format
==OSMESA_BGR
) {
1093 redBits
= CHAN_BITS
;
1094 greenBits
= CHAN_BITS
;
1095 blueBits
= CHAN_BITS
;
1101 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1102 else if (format
==OSMESA_RGB_565
) {
1107 rind
= 0; /* not used */
1116 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
1118 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
1119 GL_FALSE
, /* stereo */
1129 alphaBits
? accumBits
: 0,
1132 if (!osmesa
->gl_visual
) {
1137 /* Initialize device driver function table */
1138 _mesa_init_driver_functions(&functions
);
1139 /* override with our functions */
1140 functions
.GetString
= get_string
;
1141 functions
.UpdateState
= osmesa_update_state
;
1142 functions
.GetBufferSize
= NULL
;
1144 if (!_mesa_initialize_context(&osmesa
->mesa
,
1147 sharelist
? &sharelist
->mesa
1148 : (struct gl_context
*) NULL
,
1149 &functions
, (void *) osmesa
)) {
1150 _mesa_destroy_visual( osmesa
->gl_visual
);
1155 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
1156 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
1157 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
1158 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
1159 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
1160 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
1162 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
1163 if (!osmesa
->gl_buffer
) {
1164 _mesa_destroy_visual( osmesa
->gl_visual
);
1165 _mesa_free_context_data( &osmesa
->mesa
);
1170 /* Create depth/stencil/accum buffers. We'll create the color
1171 * buffer later in OSMesaMakeCurrent().
1173 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
1174 GL_FALSE
, /* color */
1175 osmesa
->gl_visual
->haveDepthBuffer
,
1176 osmesa
->gl_visual
->haveStencilBuffer
,
1177 osmesa
->gl_visual
->haveAccumBuffer
,
1178 GL_FALSE
, /* alpha */
1179 GL_FALSE
/* aux */ );
1181 osmesa
->format
= format
;
1182 osmesa
->userRowLength
= 0;
1183 osmesa
->yup
= GL_TRUE
;
1184 osmesa
->rInd
= rind
;
1185 osmesa
->gInd
= gind
;
1186 osmesa
->bInd
= bind
;
1187 osmesa
->aInd
= aind
;
1189 _mesa_meta_init(&osmesa
->mesa
);
1191 /* Initialize the software rasterizer and helper modules. */
1193 struct gl_context
*ctx
= &osmesa
->mesa
;
1197 if (!_swrast_CreateContext( ctx
) ||
1198 !_vbo_CreateContext( ctx
) ||
1199 !_tnl_CreateContext( ctx
) ||
1200 !_swsetup_CreateContext( ctx
)) {
1201 _mesa_destroy_visual(osmesa
->gl_visual
);
1202 _mesa_free_context_data(ctx
);
1207 _swsetup_Wakeup( ctx
);
1209 /* use default TCL pipeline */
1210 tnl
= TNL_CONTEXT(ctx
);
1211 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1213 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
1214 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
1216 /* Extend the software rasterizer with our optimized line and triangle
1217 * drawing functions.
1219 swrast
= SWRAST_CONTEXT( ctx
);
1220 swrast
->choose_line
= osmesa_choose_line
;
1221 swrast
->choose_triangle
= osmesa_choose_triangle
;
1229 * Destroy an Off-Screen Mesa rendering context.
1231 * \param osmesa the context to destroy
1233 GLAPI
void GLAPIENTRY
1234 OSMesaDestroyContext( OSMesaContext osmesa
)
1238 _mesa_reference_renderbuffer(&osmesa
->rb
, NULL
);
1240 _mesa_meta_free( &osmesa
->mesa
);
1242 _swsetup_DestroyContext( &osmesa
->mesa
);
1243 _tnl_DestroyContext( &osmesa
->mesa
);
1244 _vbo_DestroyContext( &osmesa
->mesa
);
1245 _swrast_DestroyContext( &osmesa
->mesa
);
1247 _mesa_destroy_visual( osmesa
->gl_visual
);
1248 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
1250 _mesa_free_context_data( &osmesa
->mesa
);
1257 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1258 * block of memory which the client provides. Its size must be at least
1259 * as large as width*height*sizeof(type). Its address should be a multiple
1260 * of 4 if using RGBA mode.
1262 * Image data is stored in the order of glDrawPixels: row-major order
1263 * with the lower-left image pixel stored in the first array position
1264 * (ie. bottom-to-top).
1266 * If the context's viewport hasn't been initialized yet, it will now be
1267 * initialized to (0,0,width,height).
1269 * Input: osmesa - the rendering context
1270 * buffer - the image buffer memory
1271 * type - data type for pixel components
1272 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1273 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1274 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
1275 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
1276 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
1277 * width, height - size of image buffer in pixels, at least 1
1278 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
1279 * invalid buffer address, invalid type, width<1, height<1,
1280 * width>internal limit or height>internal limit.
1282 GLAPI GLboolean GLAPIENTRY
1283 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
1284 GLsizei width
, GLsizei height
)
1286 if (!osmesa
|| !buffer
||
1287 width
< 1 || height
< 1 ||
1288 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1292 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
1297 if (!(type
== GL_UNSIGNED_BYTE
||
1298 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
1299 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1300 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1305 osmesa_update_state( &osmesa
->mesa
, 0 );
1307 /* Call this periodically to detect when the user has begun using
1308 * GL rendering from multiple threads.
1310 _glapi_check_multithread();
1313 /* Create a front/left color buffer which wraps the user-provided buffer.
1314 * There is no back color buffer.
1315 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1316 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1317 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
1318 * that converts rendering from CHAN_BITS to the user-requested channel
1322 osmesa
->rb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
1323 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1324 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1325 assert(osmesa
->rb
->RefCount
== 2);
1328 /* Set renderbuffer fields. Set width/height = 0 to force
1329 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1331 osmesa
->rb
->Data
= buffer
;
1332 osmesa
->rb
->Width
= osmesa
->rb
->Height
= 0;
1334 /* Set the framebuffer's size. This causes the
1335 * osmesa_renderbuffer_storage() function to get called.
1337 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1338 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1340 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1342 /* Remove renderbuffer attachment, then re-add. This installs the
1343 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1345 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1346 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1349 /* this updates the visual's red/green/blue/alphaBits fields */
1350 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
1352 /* update the framebuffer size */
1353 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1360 GLAPI OSMesaContext GLAPIENTRY
1361 OSMesaGetCurrentContext( void )
1363 struct gl_context
*ctx
= _mesa_get_current_context();
1365 return (OSMesaContext
) ctx
;
1372 GLAPI
void GLAPIENTRY
1373 OSMesaPixelStore( GLint pname
, GLint value
)
1375 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1378 case OSMESA_ROW_LENGTH
:
1380 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1381 "OSMesaPixelStore(value)" );
1384 osmesa
->userRowLength
= value
;
1387 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1390 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1394 compute_row_addresses( osmesa
);
1398 GLAPI
void GLAPIENTRY
1399 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1401 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1405 if (osmesa
->gl_buffer
)
1406 *value
= osmesa
->gl_buffer
->Width
;
1411 if (osmesa
->gl_buffer
)
1412 *value
= osmesa
->gl_buffer
->Height
;
1417 *value
= osmesa
->format
;
1420 /* current color buffer's data type */
1422 *value
= osmesa
->rb
->DataType
;
1428 case OSMESA_ROW_LENGTH
:
1429 *value
= osmesa
->userRowLength
;
1432 *value
= osmesa
->yup
;
1434 case OSMESA_MAX_WIDTH
:
1437 case OSMESA_MAX_HEIGHT
:
1438 *value
= MAX_HEIGHT
;
1441 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1448 * Return the depth buffer associated with an OSMesa context.
1449 * Input: c - the OSMesa context
1450 * Output: width, height - size of buffer in pixels
1451 * bytesPerValue - bytes per depth value (2 or 4)
1452 * buffer - pointer to depth buffer values
1453 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1455 GLAPI GLboolean GLAPIENTRY
1456 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1457 GLint
*bytesPerValue
, void **buffer
)
1459 struct gl_renderbuffer
*rb
= NULL
;
1462 rb
= c
->gl_buffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1464 if (!rb
|| !rb
->Data
) {
1473 *height
= rb
->Height
;
1474 if (c
->gl_visual
->depthBits
<= 16)
1475 *bytesPerValue
= sizeof(GLushort
);
1477 *bytesPerValue
= sizeof(GLuint
);
1485 * Return the color buffer associated with an OSMesa context.
1486 * Input: c - the OSMesa context
1487 * Output: width, height - size of buffer in pixels
1488 * format - the pixel format (OSMESA_FORMAT)
1489 * buffer - pointer to color buffer values
1490 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1492 GLAPI GLboolean GLAPIENTRY
1493 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1494 GLint
*height
, GLint
*format
, void **buffer
)
1496 if (osmesa
->rb
&& osmesa
->rb
->Data
) {
1497 *width
= osmesa
->rb
->Width
;
1498 *height
= osmesa
->rb
->Height
;
1499 *format
= osmesa
->format
;
1500 *buffer
= osmesa
->rb
->Data
;
1513 struct name_function
1516 OSMESAproc Function
;
1519 static struct name_function functions
[] = {
1520 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1521 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1522 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1523 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1524 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1525 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1526 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1527 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1528 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1529 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1530 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1535 GLAPI OSMESAproc GLAPIENTRY
1536 OSMesaGetProcAddress( const char *funcName
)
1539 for (i
= 0; functions
[i
].Name
; i
++) {
1540 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1541 return functions
[i
].Function
;
1543 return _glapi_get_proc_address(funcName
);
1547 GLAPI
void GLAPIENTRY
1548 OSMesaColorClamp(GLboolean enable
)
1550 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1552 if (enable
== GL_TRUE
) {
1553 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1556 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1562 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1563 * libglapi.a. We need to define them here.
1565 #ifdef GLX_INDIRECT_RENDERING
1567 #define GL_GLEXT_PROTOTYPES
1569 #include "glapi/glapi.h"
1570 #include "glapi/glapitable.h"
1572 #if defined(USE_MGL_NAMESPACE)
1573 #define NAME(func) mgl##func
1575 #define NAME(func) gl##func
1578 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1579 GET_DISPATCH()->FUNC ARGS
1581 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1582 return GET_DISPATCH()->FUNC ARGS
1584 /* skip normal ones */
1585 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1586 #include "glapi/glapitemp.h"
1588 #endif /* GLX_INDIRECT_RENDERING */