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 bytesPerRow
, i
;
727 GLubyte
*origin
= (GLubyte
*) osmesa
->rb
->Data
;
728 GLint rowlength
; /* in pixels */
729 GLint height
= osmesa
->rb
->Height
;
731 if (osmesa
->userRowLength
)
732 rowlength
= osmesa
->userRowLength
;
734 rowlength
= osmesa
->rb
->Width
;
736 bytesPerRow
= rowlength
* _mesa_get_format_bytes(osmesa
->rb
->Format
);
739 /* Y=0 is bottom line of window */
740 for (i
= 0; i
< height
; i
++) {
741 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
745 /* Y=0 is top line of window */
746 for (i
= 0; i
< height
; i
++) {
747 GLint j
= height
- i
- 1;
748 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
756 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
759 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
766 * Allocate renderbuffer storage. We don't actually allocate any storage
767 * since we're using a user-provided buffer.
768 * Just set up all the gl_renderbuffer methods.
771 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
772 GLenum internalFormat
, GLuint width
, GLuint height
)
774 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
776 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
777 (void) internalFormat
;
779 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
781 * XXX There aren't Mesa formats for all the possible combinations here!
782 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
784 * XXX The 8-bit/channel formats should all be OK.
786 if (osmesa
->format
== OSMESA_RGBA
) {
787 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
788 if (_mesa_little_endian())
789 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
791 rb
->Format
= MESA_FORMAT_RGBA8888
;
792 rb
->GetRow
= get_row_RGBA8
;
793 rb
->GetValues
= get_values_RGBA8
;
794 rb
->PutRow
= put_row_RGBA8
;
795 rb
->PutValues
= put_values_RGBA8
;
797 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
798 rb
->Format
= MESA_FORMAT_RGBA_16
;
799 rb
->GetRow
= get_row_RGBA16
;
800 rb
->GetValues
= get_values_RGBA16
;
801 rb
->PutRow
= put_row_RGBA16
;
802 rb
->PutValues
= put_values_RGBA16
;
805 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
806 rb
->GetRow
= get_row_RGBA32
;
807 rb
->GetValues
= get_values_RGBA32
;
808 rb
->PutRow
= put_row_RGBA32
;
809 rb
->PutValues
= put_values_RGBA32
;
812 else if (osmesa
->format
== OSMESA_BGRA
) {
813 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
814 if (_mesa_little_endian())
815 rb
->Format
= MESA_FORMAT_ARGB8888
;
817 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
818 rb
->GetRow
= get_row_BGRA8
;
819 rb
->GetValues
= get_values_BGRA8
;
820 rb
->PutRow
= put_row_BGRA8
;
821 rb
->PutValues
= put_values_BGRA8
;
823 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
824 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLushort");
825 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
826 rb
->GetRow
= get_row_BGRA16
;
827 rb
->GetValues
= get_values_BGRA16
;
828 rb
->PutRow
= put_row_BGRA16
;
829 rb
->PutValues
= put_values_BGRA16
;
832 _mesa_warning(ctx
, "Unsupported OSMesa format BGRA/GLfloat");
833 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
834 rb
->GetRow
= get_row_BGRA32
;
835 rb
->GetValues
= get_values_BGRA32
;
836 rb
->PutRow
= put_row_BGRA32
;
837 rb
->PutValues
= put_values_BGRA32
;
840 else if (osmesa
->format
== OSMESA_ARGB
) {
841 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
842 if (_mesa_little_endian())
843 rb
->Format
= MESA_FORMAT_ARGB8888_REV
;
845 rb
->Format
= MESA_FORMAT_ARGB8888
;
846 rb
->GetRow
= get_row_ARGB8
;
847 rb
->GetValues
= get_values_ARGB8
;
848 rb
->PutRow
= put_row_ARGB8
;
849 rb
->PutValues
= put_values_ARGB8
;
851 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
852 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLushort");
853 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
854 rb
->GetRow
= get_row_ARGB16
;
855 rb
->GetValues
= get_values_ARGB16
;
856 rb
->PutRow
= put_row_ARGB16
;
857 rb
->PutValues
= put_values_ARGB16
;
860 _mesa_warning(ctx
, "Unsupported OSMesa format ARGB/GLfloat");
861 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
862 rb
->GetRow
= get_row_ARGB32
;
863 rb
->GetValues
= get_values_ARGB32
;
864 rb
->PutRow
= put_row_ARGB32
;
865 rb
->PutValues
= put_values_ARGB32
;
868 else if (osmesa
->format
== OSMESA_RGB
) {
869 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
870 rb
->Format
= MESA_FORMAT_RGB888
;
871 rb
->GetRow
= get_row_RGB8
;
872 rb
->GetValues
= get_values_RGB8
;
873 rb
->PutRow
= put_row_RGB8
;
874 rb
->PutValues
= put_values_RGB8
;
876 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
877 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLushort");
878 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
879 rb
->GetRow
= get_row_RGB16
;
880 rb
->GetValues
= get_values_RGB16
;
881 rb
->PutRow
= put_row_RGB16
;
882 rb
->PutValues
= put_values_RGB16
;
885 _mesa_warning(ctx
, "Unsupported OSMesa format RGB/GLfloat");
886 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
887 rb
->GetRow
= get_row_RGB32
;
888 rb
->GetValues
= get_values_RGB32
;
889 rb
->PutRow
= put_row_RGB32
;
890 rb
->PutValues
= put_values_RGB32
;
893 else if (osmesa
->format
== OSMESA_BGR
) {
894 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
895 rb
->Format
= MESA_FORMAT_BGR888
;
896 rb
->GetRow
= get_row_BGR8
;
897 rb
->GetValues
= get_values_BGR8
;
898 rb
->PutRow
= put_row_BGR8
;
899 rb
->PutValues
= put_values_BGR8
;
901 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
902 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLushort");
903 rb
->Format
= MESA_FORMAT_RGBA_16
; /* not exactly right */
904 rb
->GetRow
= get_row_BGR16
;
905 rb
->GetValues
= get_values_BGR16
;
906 rb
->PutRow
= put_row_BGR16
;
907 rb
->PutValues
= put_values_BGR16
;
910 _mesa_warning(ctx
, "Unsupported OSMesa format BGR/GLfloat");
911 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
; /* not exactly right */
912 rb
->GetRow
= get_row_BGR32
;
913 rb
->GetValues
= get_values_BGR32
;
914 rb
->PutRow
= put_row_BGR32
;
915 rb
->PutValues
= put_values_BGR32
;
918 else if (osmesa
->format
== OSMESA_RGB_565
) {
919 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
920 rb
->Format
= MESA_FORMAT_RGB565
;
921 rb
->GetRow
= get_row_RGB_565
;
922 rb
->GetValues
= get_values_RGB_565
;
923 rb
->PutRow
= put_row_RGB_565
;
924 rb
->PutValues
= put_values_RGB_565
;
927 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
933 compute_row_addresses( osmesa
);
940 * Allocate a new renderbuffer to describe the user-provided color buffer.
942 static struct gl_renderbuffer
*
943 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
945 const GLuint name
= 0;
946 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, name
);
949 rb
->Delete
= osmesa_delete_renderbuffer
;
950 rb
->AllocStorage
= osmesa_renderbuffer_storage
;
951 rb
->ClassID
= OSMESA_RENDERBUFFER_CLASS
;
953 rb
->InternalFormat
= GL_RGBA
;
954 rb
->_BaseFormat
= GL_RGBA
;
963 osmesa_MapRenderbuffer(struct gl_context
*ctx
,
964 struct gl_renderbuffer
*rb
,
965 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
967 GLubyte
**mapOut
, GLint
*rowStrideOut
)
969 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
971 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
972 /* this is an OSMesa renderbuffer which wraps user memory */
973 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
974 GLint rowStride
; /* in bytes */
976 if (osmesa
->userRowLength
)
977 rowStride
= osmesa
->userRowLength
* bpp
;
979 rowStride
= rb
->Width
* bpp
;
982 /* Y=0 is top line of window */
983 y
= rb
->Height
- y
- 1;
984 *rowStrideOut
= -rowStride
;
987 *rowStrideOut
= rowStride
;
990 *mapOut
= (GLubyte
*) rb
->Data
+ y
* rowStride
+ x
* bpp
;
993 _swrast_map_soft_renderbuffer(ctx
, rb
, x
, y
, w
, h
, mode
,
994 mapOut
, rowStrideOut
);
1000 osmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
1002 if (rb
->ClassID
== OSMESA_RENDERBUFFER_CLASS
) {
1006 _swrast_unmap_soft_renderbuffer(ctx
, rb
);
1011 /**********************************************************************/
1012 /***** Public Functions *****/
1013 /**********************************************************************/
1017 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1018 * an RGBA vs Color-Index mode flag.
1020 * Input: format - Must be GL_RGBA
1021 * sharelist - specifies another OSMesaContext with which to share
1022 * display lists. NULL indicates no sharing.
1023 * Return: an OSMesaContext or 0 if error
1025 GLAPI OSMesaContext GLAPIENTRY
1026 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
1028 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
1037 * Create context and specify size of ancillary buffers.
1039 GLAPI OSMesaContext GLAPIENTRY
1040 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
1041 GLint accumBits
, OSMesaContext sharelist
)
1043 OSMesaContext osmesa
;
1044 struct dd_function_table functions
;
1045 GLint rind
, gind
, bind
, aind
;
1046 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
1048 rind
= gind
= bind
= aind
= 0;
1049 if (format
==OSMESA_RGBA
) {
1050 redBits
= CHAN_BITS
;
1051 greenBits
= CHAN_BITS
;
1052 blueBits
= CHAN_BITS
;
1053 alphaBits
= CHAN_BITS
;
1059 else if (format
==OSMESA_BGRA
) {
1060 redBits
= CHAN_BITS
;
1061 greenBits
= CHAN_BITS
;
1062 blueBits
= CHAN_BITS
;
1063 alphaBits
= CHAN_BITS
;
1069 else if (format
==OSMESA_ARGB
) {
1070 redBits
= CHAN_BITS
;
1071 greenBits
= CHAN_BITS
;
1072 blueBits
= CHAN_BITS
;
1073 alphaBits
= CHAN_BITS
;
1079 else if (format
==OSMESA_RGB
) {
1080 redBits
= CHAN_BITS
;
1081 greenBits
= CHAN_BITS
;
1082 blueBits
= CHAN_BITS
;
1088 else if (format
==OSMESA_BGR
) {
1089 redBits
= CHAN_BITS
;
1090 greenBits
= CHAN_BITS
;
1091 blueBits
= CHAN_BITS
;
1097 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1098 else if (format
==OSMESA_RGB_565
) {
1103 rind
= 0; /* not used */
1112 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
1114 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
1115 GL_FALSE
, /* stereo */
1125 alphaBits
? accumBits
: 0,
1128 if (!osmesa
->gl_visual
) {
1133 /* Initialize device driver function table */
1134 _mesa_init_driver_functions(&functions
);
1135 /* override with our functions */
1136 functions
.GetString
= get_string
;
1137 functions
.UpdateState
= osmesa_update_state
;
1138 functions
.GetBufferSize
= NULL
;
1140 if (!_mesa_initialize_context(&osmesa
->mesa
,
1143 sharelist
? &sharelist
->mesa
1144 : (struct gl_context
*) NULL
,
1145 &functions
, (void *) osmesa
)) {
1146 _mesa_destroy_visual( osmesa
->gl_visual
);
1151 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
1152 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
1153 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
1154 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
1155 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
1156 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
1158 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
1159 if (!osmesa
->gl_buffer
) {
1160 _mesa_destroy_visual( osmesa
->gl_visual
);
1161 _mesa_free_context_data( &osmesa
->mesa
);
1166 /* Create depth/stencil/accum buffers. We'll create the color
1167 * buffer later in OSMesaMakeCurrent().
1169 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
1170 GL_FALSE
, /* color */
1171 osmesa
->gl_visual
->haveDepthBuffer
,
1172 osmesa
->gl_visual
->haveStencilBuffer
,
1173 osmesa
->gl_visual
->haveAccumBuffer
,
1174 GL_FALSE
, /* alpha */
1175 GL_FALSE
/* aux */ );
1177 osmesa
->format
= format
;
1178 osmesa
->userRowLength
= 0;
1179 osmesa
->yup
= GL_TRUE
;
1180 osmesa
->rInd
= rind
;
1181 osmesa
->gInd
= gind
;
1182 osmesa
->bInd
= bind
;
1183 osmesa
->aInd
= aind
;
1185 _mesa_meta_init(&osmesa
->mesa
);
1187 /* Initialize the software rasterizer and helper modules. */
1189 struct gl_context
*ctx
= &osmesa
->mesa
;
1193 if (!_swrast_CreateContext( ctx
) ||
1194 !_vbo_CreateContext( ctx
) ||
1195 !_tnl_CreateContext( ctx
) ||
1196 !_swsetup_CreateContext( ctx
)) {
1197 _mesa_destroy_visual(osmesa
->gl_visual
);
1198 _mesa_free_context_data(ctx
);
1203 _swsetup_Wakeup( ctx
);
1205 /* use default TCL pipeline */
1206 tnl
= TNL_CONTEXT(ctx
);
1207 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1209 ctx
->Driver
.MapRenderbuffer
= osmesa_MapRenderbuffer
;
1210 ctx
->Driver
.UnmapRenderbuffer
= osmesa_UnmapRenderbuffer
;
1212 /* Extend the software rasterizer with our optimized line and triangle
1213 * drawing functions.
1215 swrast
= SWRAST_CONTEXT( ctx
);
1216 swrast
->choose_line
= osmesa_choose_line
;
1217 swrast
->choose_triangle
= osmesa_choose_triangle
;
1225 * Destroy an Off-Screen Mesa rendering context.
1227 * \param osmesa the context to destroy
1229 GLAPI
void GLAPIENTRY
1230 OSMesaDestroyContext( OSMesaContext osmesa
)
1234 _mesa_reference_renderbuffer(&osmesa
->rb
, NULL
);
1236 _mesa_meta_free( &osmesa
->mesa
);
1238 _swsetup_DestroyContext( &osmesa
->mesa
);
1239 _tnl_DestroyContext( &osmesa
->mesa
);
1240 _vbo_DestroyContext( &osmesa
->mesa
);
1241 _swrast_DestroyContext( &osmesa
->mesa
);
1243 _mesa_destroy_visual( osmesa
->gl_visual
);
1244 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
1246 _mesa_free_context_data( &osmesa
->mesa
);
1253 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1254 * block of memory which the client provides. Its size must be at least
1255 * as large as width*height*sizeof(type). Its address should be a multiple
1256 * of 4 if using RGBA mode.
1258 * Image data is stored in the order of glDrawPixels: row-major order
1259 * with the lower-left image pixel stored in the first array position
1260 * (ie. bottom-to-top).
1262 * If the context's viewport hasn't been initialized yet, it will now be
1263 * initialized to (0,0,width,height).
1265 * Input: osmesa - the rendering context
1266 * buffer - the image buffer memory
1267 * type - data type for pixel components
1268 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1269 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1270 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
1271 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
1272 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
1273 * width, height - size of image buffer in pixels, at least 1
1274 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
1275 * invalid buffer address, invalid type, width<1, height<1,
1276 * width>internal limit or height>internal limit.
1278 GLAPI GLboolean GLAPIENTRY
1279 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
1280 GLsizei width
, GLsizei height
)
1282 if (!osmesa
|| !buffer
||
1283 width
< 1 || height
< 1 ||
1284 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1288 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
1293 if (!(type
== GL_UNSIGNED_BYTE
||
1294 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
1295 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1296 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1301 osmesa_update_state( &osmesa
->mesa
, 0 );
1303 /* Call this periodically to detect when the user has begun using
1304 * GL rendering from multiple threads.
1306 _glapi_check_multithread();
1309 /* Create a front/left color buffer which wraps the user-provided buffer.
1310 * There is no back color buffer.
1311 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1312 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1313 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
1314 * that converts rendering from CHAN_BITS to the user-requested channel
1318 osmesa
->rb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
1319 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1320 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1321 assert(osmesa
->rb
->RefCount
== 2);
1324 /* Set renderbuffer fields. Set width/height = 0 to force
1325 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1327 osmesa
->rb
->Data
= buffer
;
1328 osmesa
->rb
->Width
= osmesa
->rb
->Height
= 0;
1330 /* Set the framebuffer's size. This causes the
1331 * osmesa_renderbuffer_storage() function to get called.
1333 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1334 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1336 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1338 /* Remove renderbuffer attachment, then re-add. This installs the
1339 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1341 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1342 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1345 /* this updates the visual's red/green/blue/alphaBits fields */
1346 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
1348 /* update the framebuffer size */
1349 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1356 GLAPI OSMesaContext GLAPIENTRY
1357 OSMesaGetCurrentContext( void )
1359 struct gl_context
*ctx
= _mesa_get_current_context();
1361 return (OSMesaContext
) ctx
;
1368 GLAPI
void GLAPIENTRY
1369 OSMesaPixelStore( GLint pname
, GLint value
)
1371 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1374 case OSMESA_ROW_LENGTH
:
1376 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1377 "OSMesaPixelStore(value)" );
1380 osmesa
->userRowLength
= value
;
1383 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1386 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1390 compute_row_addresses( osmesa
);
1394 GLAPI
void GLAPIENTRY
1395 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1397 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1401 if (osmesa
->gl_buffer
)
1402 *value
= osmesa
->gl_buffer
->Width
;
1407 if (osmesa
->gl_buffer
)
1408 *value
= osmesa
->gl_buffer
->Height
;
1413 *value
= osmesa
->format
;
1416 /* current color buffer's data type */
1418 *value
= osmesa
->rb
->DataType
;
1424 case OSMESA_ROW_LENGTH
:
1425 *value
= osmesa
->userRowLength
;
1428 *value
= osmesa
->yup
;
1430 case OSMESA_MAX_WIDTH
:
1433 case OSMESA_MAX_HEIGHT
:
1434 *value
= MAX_HEIGHT
;
1437 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1444 * Return the depth buffer associated with an OSMesa context.
1445 * Input: c - the OSMesa context
1446 * Output: width, height - size of buffer in pixels
1447 * bytesPerValue - bytes per depth value (2 or 4)
1448 * buffer - pointer to depth buffer values
1449 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1451 GLAPI GLboolean GLAPIENTRY
1452 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1453 GLint
*bytesPerValue
, void **buffer
)
1455 struct gl_renderbuffer
*rb
= NULL
;
1458 rb
= c
->gl_buffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1460 if (!rb
|| !rb
->Data
) {
1469 *height
= rb
->Height
;
1470 if (c
->gl_visual
->depthBits
<= 16)
1471 *bytesPerValue
= sizeof(GLushort
);
1473 *bytesPerValue
= sizeof(GLuint
);
1481 * Return the color buffer associated with an OSMesa context.
1482 * Input: c - the OSMesa context
1483 * Output: width, height - size of buffer in pixels
1484 * format - the pixel format (OSMESA_FORMAT)
1485 * buffer - pointer to color buffer values
1486 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1488 GLAPI GLboolean GLAPIENTRY
1489 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1490 GLint
*height
, GLint
*format
, void **buffer
)
1492 if (osmesa
->rb
&& osmesa
->rb
->Data
) {
1493 *width
= osmesa
->rb
->Width
;
1494 *height
= osmesa
->rb
->Height
;
1495 *format
= osmesa
->format
;
1496 *buffer
= osmesa
->rb
->Data
;
1509 struct name_function
1512 OSMESAproc Function
;
1515 static struct name_function functions
[] = {
1516 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1517 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1518 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1519 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1520 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1521 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1522 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1523 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1524 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1525 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1526 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1531 GLAPI OSMESAproc GLAPIENTRY
1532 OSMesaGetProcAddress( const char *funcName
)
1535 for (i
= 0; functions
[i
].Name
; i
++) {
1536 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1537 return functions
[i
].Function
;
1539 return _glapi_get_proc_address(funcName
);
1543 GLAPI
void GLAPIENTRY
1544 OSMesaColorClamp(GLboolean enable
)
1546 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1548 if (enable
== GL_TRUE
) {
1549 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1552 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1558 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1559 * libglapi.a. We need to define them here.
1561 #ifdef GLX_INDIRECT_RENDERING
1563 #define GL_GLEXT_PROTOTYPES
1565 #include "glapi/glapi.h"
1566 #include "glapi/glapitable.h"
1568 #if defined(USE_MGL_NAMESPACE)
1569 #define NAME(func) mgl##func
1571 #define NAME(func) gl##func
1574 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1575 GET_DISPATCH()->FUNC ARGS
1577 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1578 return GET_DISPATCH()->FUNC ARGS
1580 /* skip normal ones */
1581 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1582 #include "glapi/glapitemp.h"
1584 #endif /* GLX_INDIRECT_RENDERING */