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"
61 * OSMesa rendering context, derived from core Mesa struct gl_context.
65 struct gl_context mesa
; /*< Base class - this must be first */
66 struct gl_config
*gl_visual
; /*< Describes the buffers */
67 struct gl_renderbuffer
*rb
; /*< The user's colorbuffer */
68 struct gl_framebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
69 GLenum format
; /*< User-specified context format */
70 GLint userRowLength
; /*< user-specified number of pixels per row */
71 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
72 GLvoid
*rowaddr
[MAX_HEIGHT
]; /*< address of first pixel in each image row */
73 GLboolean yup
; /*< TRUE -> Y increases upward */
74 /*< FALSE -> Y increases downward */
78 static INLINE OSMesaContext
79 OSMESA_CONTEXT(struct gl_context
*ctx
)
81 /* Just cast, since we're using structure containment */
82 return (OSMesaContext
) ctx
;
86 /**********************************************************************/
87 /*** Private Device Driver Functions ***/
88 /**********************************************************************/
91 static const GLubyte
*
92 get_string( struct gl_context
*ctx
, GLenum name
)
98 return (const GLubyte
*) "Mesa OffScreen32";
100 return (const GLubyte
*) "Mesa OffScreen16";
102 return (const GLubyte
*) "Mesa OffScreen";
111 osmesa_update_state( struct gl_context
*ctx
, GLuint new_state
)
113 /* easy - just propogate */
114 _swrast_InvalidateState( ctx
, new_state
);
115 _swsetup_InvalidateState( ctx
, new_state
);
116 _tnl_InvalidateState( ctx
, new_state
);
117 _vbo_InvalidateState( ctx
, new_state
);
122 /**********************************************************************/
123 /***** Read/write spans/arrays of pixels *****/
124 /**********************************************************************/
127 #define NAME(PREFIX) PREFIX##_RGBA8
128 #define RB_TYPE GLubyte
130 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
131 #define INIT_PIXEL_PTR(P, X, Y) \
132 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
133 #define INC_PIXEL_PTR(P) P += 4
134 #define STORE_PIXEL(DST, X, Y, VALUE) \
135 DST[0] = VALUE[RCOMP]; \
136 DST[1] = VALUE[GCOMP]; \
137 DST[2] = VALUE[BCOMP]; \
138 DST[3] = VALUE[ACOMP]
139 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
140 DST[0] = VALUE[RCOMP]; \
141 DST[1] = VALUE[GCOMP]; \
142 DST[2] = VALUE[BCOMP]; \
144 #define FETCH_PIXEL(DST, SRC) \
145 DST[RCOMP] = SRC[0]; \
146 DST[GCOMP] = SRC[1]; \
147 DST[BCOMP] = SRC[2]; \
149 #include "swrast/s_spantemp.h"
152 #define NAME(PREFIX) PREFIX##_RGBA16
153 #define RB_TYPE GLushort
155 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
156 #define INIT_PIXEL_PTR(P, X, Y) \
157 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
158 #define INC_PIXEL_PTR(P) P += 4
159 #define STORE_PIXEL(DST, X, Y, VALUE) \
160 DST[0] = VALUE[RCOMP]; \
161 DST[1] = VALUE[GCOMP]; \
162 DST[2] = VALUE[BCOMP]; \
163 DST[3] = VALUE[ACOMP]
164 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
165 DST[0] = VALUE[RCOMP]; \
166 DST[1] = VALUE[GCOMP]; \
167 DST[2] = VALUE[BCOMP]; \
169 #define FETCH_PIXEL(DST, SRC) \
170 DST[RCOMP] = SRC[0]; \
171 DST[GCOMP] = SRC[1]; \
172 DST[BCOMP] = SRC[2]; \
174 #include "swrast/s_spantemp.h"
177 #define NAME(PREFIX) PREFIX##_RGBA32
178 #define RB_TYPE GLfloat
180 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
181 #define INIT_PIXEL_PTR(P, X, Y) \
182 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
183 #define INC_PIXEL_PTR(P) P += 4
184 #define STORE_PIXEL(DST, X, Y, VALUE) \
185 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
186 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
187 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
188 DST[3] = CLAMP((VALUE[ACOMP]), 0.0F, 1.0F)
189 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
190 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
191 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
192 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
194 #define FETCH_PIXEL(DST, SRC) \
195 DST[RCOMP] = SRC[0]; \
196 DST[GCOMP] = SRC[1]; \
197 DST[BCOMP] = SRC[2]; \
199 #include "swrast/s_spantemp.h"
203 #define NAME(PREFIX) PREFIX##_BGRA8
204 #define RB_TYPE GLubyte
206 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
207 #define INIT_PIXEL_PTR(P, X, Y) \
208 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
209 #define INC_PIXEL_PTR(P) P += 4
210 #define STORE_PIXEL(DST, X, Y, VALUE) \
211 DST[2] = VALUE[RCOMP]; \
212 DST[1] = VALUE[GCOMP]; \
213 DST[0] = VALUE[BCOMP]; \
214 DST[3] = VALUE[ACOMP]
215 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
216 DST[2] = VALUE[RCOMP]; \
217 DST[1] = VALUE[GCOMP]; \
218 DST[0] = VALUE[BCOMP]; \
220 #define FETCH_PIXEL(DST, SRC) \
221 DST[RCOMP] = SRC[2]; \
222 DST[GCOMP] = SRC[1]; \
223 DST[BCOMP] = SRC[0]; \
225 #include "swrast/s_spantemp.h"
228 #define NAME(PREFIX) PREFIX##_BGRA16
229 #define RB_TYPE GLushort
231 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
232 #define INIT_PIXEL_PTR(P, X, Y) \
233 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
234 #define INC_PIXEL_PTR(P) P += 4
235 #define STORE_PIXEL(DST, X, Y, VALUE) \
236 DST[2] = VALUE[RCOMP]; \
237 DST[1] = VALUE[GCOMP]; \
238 DST[0] = VALUE[BCOMP]; \
239 DST[3] = VALUE[ACOMP]
240 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
241 DST[2] = VALUE[RCOMP]; \
242 DST[1] = VALUE[GCOMP]; \
243 DST[0] = VALUE[BCOMP]; \
245 #define FETCH_PIXEL(DST, SRC) \
246 DST[RCOMP] = SRC[2]; \
247 DST[GCOMP] = SRC[1]; \
248 DST[BCOMP] = SRC[0]; \
250 #include "swrast/s_spantemp.h"
253 #define NAME(PREFIX) PREFIX##_BGRA32
254 #define RB_TYPE GLfloat
256 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
257 #define INIT_PIXEL_PTR(P, X, Y) \
258 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
259 #define INC_PIXEL_PTR(P) P += 4
260 #define STORE_PIXEL(DST, X, Y, VALUE) \
261 DST[2] = VALUE[RCOMP]; \
262 DST[1] = VALUE[GCOMP]; \
263 DST[0] = VALUE[BCOMP]; \
264 DST[3] = VALUE[ACOMP]
265 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
266 DST[2] = VALUE[RCOMP]; \
267 DST[1] = VALUE[GCOMP]; \
268 DST[0] = VALUE[BCOMP]; \
270 #define FETCH_PIXEL(DST, SRC) \
271 DST[RCOMP] = SRC[2]; \
272 DST[GCOMP] = SRC[1]; \
273 DST[BCOMP] = SRC[0]; \
275 #include "swrast/s_spantemp.h"
279 #define NAME(PREFIX) PREFIX##_ARGB8
280 #define RB_TYPE GLubyte
282 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
283 #define INIT_PIXEL_PTR(P, X, Y) \
284 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
285 #define INC_PIXEL_PTR(P) P += 4
286 #define STORE_PIXEL(DST, X, Y, VALUE) \
287 DST[1] = VALUE[RCOMP]; \
288 DST[2] = VALUE[GCOMP]; \
289 DST[3] = VALUE[BCOMP]; \
290 DST[0] = VALUE[ACOMP]
291 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
292 DST[1] = VALUE[RCOMP]; \
293 DST[2] = VALUE[GCOMP]; \
294 DST[3] = VALUE[BCOMP]; \
296 #define FETCH_PIXEL(DST, SRC) \
297 DST[RCOMP] = SRC[1]; \
298 DST[GCOMP] = SRC[2]; \
299 DST[BCOMP] = SRC[3]; \
301 #include "swrast/s_spantemp.h"
304 #define NAME(PREFIX) PREFIX##_ARGB16
305 #define RB_TYPE GLushort
307 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
308 #define INIT_PIXEL_PTR(P, X, Y) \
309 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
310 #define INC_PIXEL_PTR(P) P += 4
311 #define STORE_PIXEL(DST, X, Y, VALUE) \
312 DST[1] = VALUE[RCOMP]; \
313 DST[2] = VALUE[GCOMP]; \
314 DST[3] = VALUE[BCOMP]; \
315 DST[0] = VALUE[ACOMP]
316 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
317 DST[1] = VALUE[RCOMP]; \
318 DST[2] = VALUE[GCOMP]; \
319 DST[3] = VALUE[BCOMP]; \
321 #define FETCH_PIXEL(DST, SRC) \
322 DST[RCOMP] = SRC[1]; \
323 DST[GCOMP] = SRC[2]; \
324 DST[BCOMP] = SRC[3]; \
326 #include "swrast/s_spantemp.h"
329 #define NAME(PREFIX) PREFIX##_ARGB32
330 #define RB_TYPE GLfloat
332 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
333 #define INIT_PIXEL_PTR(P, X, Y) \
334 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
335 #define INC_PIXEL_PTR(P) P += 4
336 #define STORE_PIXEL(DST, X, Y, VALUE) \
337 DST[1] = VALUE[RCOMP]; \
338 DST[2] = VALUE[GCOMP]; \
339 DST[3] = VALUE[BCOMP]; \
340 DST[0] = VALUE[ACOMP]
341 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
342 DST[1] = VALUE[RCOMP]; \
343 DST[2] = VALUE[GCOMP]; \
344 DST[3] = VALUE[BCOMP]; \
346 #define FETCH_PIXEL(DST, SRC) \
347 DST[RCOMP] = SRC[1]; \
348 DST[GCOMP] = SRC[2]; \
349 DST[BCOMP] = SRC[3]; \
351 #include "swrast/s_spantemp.h"
355 #define NAME(PREFIX) PREFIX##_RGB8
356 #define RB_TYPE GLubyte
358 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
359 #define INIT_PIXEL_PTR(P, X, Y) \
360 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
361 #define INC_PIXEL_PTR(P) P += 3
362 #define STORE_PIXEL(DST, X, Y, VALUE) \
363 DST[0] = VALUE[RCOMP]; \
364 DST[1] = VALUE[GCOMP]; \
365 DST[2] = VALUE[BCOMP]
366 #define FETCH_PIXEL(DST, SRC) \
367 DST[RCOMP] = SRC[0]; \
368 DST[GCOMP] = SRC[1]; \
369 DST[BCOMP] = SRC[2]; \
371 #include "swrast/s_spantemp.h"
374 #define NAME(PREFIX) PREFIX##_RGB16
375 #define RB_TYPE GLushort
377 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
378 #define INIT_PIXEL_PTR(P, X, Y) \
379 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
380 #define INC_PIXEL_PTR(P) P += 3
381 #define STORE_PIXEL(DST, X, Y, VALUE) \
382 DST[0] = VALUE[RCOMP]; \
383 DST[1] = VALUE[GCOMP]; \
384 DST[2] = VALUE[BCOMP]
385 #define FETCH_PIXEL(DST, SRC) \
386 DST[RCOMP] = SRC[0]; \
387 DST[GCOMP] = SRC[1]; \
388 DST[BCOMP] = SRC[2]; \
390 #include "swrast/s_spantemp.h"
393 #define NAME(PREFIX) PREFIX##_RGB32
394 #define RB_TYPE GLfloat
396 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
397 #define INIT_PIXEL_PTR(P, X, Y) \
398 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
399 #define INC_PIXEL_PTR(P) P += 3
400 #define STORE_PIXEL(DST, X, Y, VALUE) \
401 DST[0] = VALUE[RCOMP]; \
402 DST[1] = VALUE[GCOMP]; \
403 DST[2] = VALUE[BCOMP]
404 #define FETCH_PIXEL(DST, SRC) \
405 DST[RCOMP] = SRC[0]; \
406 DST[GCOMP] = SRC[1]; \
407 DST[BCOMP] = SRC[2]; \
409 #include "swrast/s_spantemp.h"
413 #define NAME(PREFIX) PREFIX##_BGR8
414 #define RB_TYPE GLubyte
416 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
417 #define INIT_PIXEL_PTR(P, X, Y) \
418 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
419 #define INC_PIXEL_PTR(P) P += 3
420 #define STORE_PIXEL(DST, X, Y, VALUE) \
421 DST[2] = VALUE[RCOMP]; \
422 DST[1] = VALUE[GCOMP]; \
423 DST[0] = VALUE[BCOMP]
424 #define FETCH_PIXEL(DST, SRC) \
425 DST[RCOMP] = SRC[2]; \
426 DST[GCOMP] = SRC[1]; \
427 DST[BCOMP] = SRC[0]; \
429 #include "swrast/s_spantemp.h"
432 #define NAME(PREFIX) PREFIX##_BGR16
433 #define RB_TYPE GLushort
435 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
436 #define INIT_PIXEL_PTR(P, X, Y) \
437 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
438 #define INC_PIXEL_PTR(P) P += 3
439 #define STORE_PIXEL(DST, X, Y, VALUE) \
440 DST[2] = VALUE[RCOMP]; \
441 DST[1] = VALUE[GCOMP]; \
442 DST[0] = VALUE[BCOMP]
443 #define FETCH_PIXEL(DST, SRC) \
444 DST[RCOMP] = SRC[2]; \
445 DST[GCOMP] = SRC[1]; \
446 DST[BCOMP] = SRC[0]; \
448 #include "swrast/s_spantemp.h"
451 #define NAME(PREFIX) PREFIX##_BGR32
452 #define RB_TYPE GLfloat
454 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
455 #define INIT_PIXEL_PTR(P, X, Y) \
456 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
457 #define INC_PIXEL_PTR(P) P += 3
458 #define STORE_PIXEL(DST, X, Y, VALUE) \
459 DST[2] = VALUE[RCOMP]; \
460 DST[1] = VALUE[GCOMP]; \
461 DST[0] = VALUE[BCOMP]
462 #define FETCH_PIXEL(DST, SRC) \
463 DST[RCOMP] = SRC[2]; \
464 DST[GCOMP] = SRC[1]; \
465 DST[BCOMP] = SRC[0]; \
467 #include "swrast/s_spantemp.h"
470 /* 16-bit 5/6/5 RGB */
471 #define NAME(PREFIX) PREFIX##_RGB_565
472 #define RB_TYPE GLubyte
474 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
475 #define INIT_PIXEL_PTR(P, X, Y) \
476 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
477 #define INC_PIXEL_PTR(P) P += 1
478 #define STORE_PIXEL(DST, X, Y, VALUE) \
479 *DST = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
480 #define FETCH_PIXEL(DST, SRC) \
481 DST[RCOMP] = ( (((*SRC) >> 8) & 0xf8) | (((*SRC) >> 11) & 0x7) ); \
482 DST[GCOMP] = ( (((*SRC) >> 3) & 0xfc) | (((*SRC) >> 5) & 0x3) ); \
483 DST[BCOMP] = ( (((*SRC) << 3) & 0xf8) | (((*SRC) ) & 0x7) ); \
484 DST[ACOMP] = CHAN_MAX
485 #include "swrast/s_spantemp.h"
489 * Macros for optimized line/triangle rendering.
490 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
493 #define PACK_RGBA(DST, R, G, B, A) \
495 (DST)[osmesa->rInd] = R; \
496 (DST)[osmesa->gInd] = G; \
497 (DST)[osmesa->bInd] = B; \
498 (DST)[osmesa->aInd] = A; \
501 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
505 * Draw a flat-shaded, RGB line into an osmesa buffer.
507 #define NAME flat_rgba_line
510 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
511 const GLchan *color = vert1->color;
515 GLchan *p = PIXELADDR4(X, Y); \
516 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
519 #include "swrast/s_linetemp.h"
524 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
526 #define NAME flat_rgba_z_line
529 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
531 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
532 const GLchan *color = vert1->color;
537 GLchan *p = PIXELADDR4(X, Y); \
538 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
539 color[BCOMP], color[ACOMP]); \
544 #include "swrast/s_linetemp.h"
549 * Analyze context state to see if we can provide a fast line drawing
550 * function. Otherwise, return NULL.
552 static swrast_line_func
553 osmesa_choose_line_function( struct gl_context
*ctx
)
555 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
556 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
558 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
561 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
562 if (ctx
->Line
.SmoothFlag
) return NULL
;
563 if (ctx
->Texture
._EnabledUnits
) return NULL
;
564 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
565 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
566 if (ctx
->Line
.StippleFlag
) return NULL
;
567 if (ctx
->Line
.SmoothFlag
) return NULL
;
568 if (osmesa
->format
!= OSMESA_RGBA
&&
569 osmesa
->format
!= OSMESA_BGRA
&&
570 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
572 if (swrast
->_RasterMask
==DEPTH_BIT
573 && ctx
->Depth
.Func
==GL_LESS
574 && ctx
->Depth
.Mask
==GL_TRUE
575 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
576 return (swrast_line_func
) flat_rgba_z_line
;
579 if (swrast
->_RasterMask
== 0) {
580 return (swrast_line_func
) flat_rgba_line
;
583 return (swrast_line_func
) NULL
;
587 /**********************************************************************/
588 /***** Optimized triangle rendering *****/
589 /**********************************************************************/
593 * Smooth-shaded, z-less triangle, RGBA color.
595 #define NAME smooth_rgba_z_triangle
597 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
599 #define INTERP_ALPHA 1
601 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
602 #define RENDER_SPAN( span ) { \
604 GLchan *img = PIXELADDR4(span.x, span.y); \
605 for (i = 0; i < span.end; i++, img += 4) { \
606 const GLuint z = FixedToDepth(span.z); \
608 PACK_RGBA(img, FixedToChan(span.red), \
609 FixedToChan(span.green), FixedToChan(span.blue), \
610 FixedToChan(span.alpha)); \
613 span.red += span.redStep; \
614 span.green += span.greenStep; \
615 span.blue += span.blueStep; \
616 span.alpha += span.alphaStep; \
617 span.z += span.zStep; \
620 #include "swrast/s_tritemp.h"
625 * Flat-shaded, z-less triangle, RGBA color.
627 #define NAME flat_rgba_z_triangle
629 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
631 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
633 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
634 v2->color[2], v2->color[3]);
636 #define RENDER_SPAN( span ) { \
638 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
639 for (i = 0; i < span.end; i++) { \
640 const GLuint z = FixedToDepth(span.z); \
645 span.z += span.zStep; \
649 #include "swrast/s_tritemp.h"
654 * Return pointer to an optimized triangle function if possible.
656 static swrast_tri_func
657 osmesa_choose_triangle_function( struct gl_context
*ctx
)
659 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
660 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
662 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
663 return (swrast_tri_func
) NULL
;
665 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
666 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
667 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
668 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
669 if (osmesa
->format
!= OSMESA_RGBA
&&
670 osmesa
->format
!= OSMESA_BGRA
&&
671 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
672 if (ctx
->Polygon
.CullFlag
&&
673 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
674 return (swrast_tri_func
) NULL
;
676 if (swrast
->_RasterMask
== DEPTH_BIT
&&
677 ctx
->Depth
.Func
== GL_LESS
&&
678 ctx
->Depth
.Mask
== GL_TRUE
&&
679 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
680 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
681 return (swrast_tri_func
) smooth_rgba_z_triangle
;
684 return (swrast_tri_func
) flat_rgba_z_triangle
;
687 return (swrast_tri_func
) NULL
;
692 /* Override for the swrast triangle-selection function. Try to use one
693 * of our internal triangle functions, otherwise fall back to the
694 * standard swrast functions.
697 osmesa_choose_triangle( struct gl_context
*ctx
)
699 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
701 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
702 if (!swrast
->Triangle
)
703 _swrast_choose_triangle( ctx
);
707 osmesa_choose_line( struct gl_context
*ctx
)
709 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
711 swrast
->Line
= osmesa_choose_line_function( ctx
);
713 _swrast_choose_line( ctx
);
719 * Recompute the values of the context's rowaddr array.
722 compute_row_addresses( OSMesaContext osmesa
)
724 GLint bytesPerPixel
, bytesPerRow
, i
;
725 GLubyte
*origin
= (GLubyte
*) osmesa
->rb
->Data
;
726 GLint bpc
; /* bytes per channel */
727 GLint rowlength
; /* in pixels */
728 GLint height
= osmesa
->rb
->Height
;
730 if (osmesa
->userRowLength
)
731 rowlength
= osmesa
->userRowLength
;
733 rowlength
= osmesa
->rb
->Width
;
735 if (osmesa
->rb
->DataType
== GL_UNSIGNED_BYTE
)
737 else if (osmesa
->rb
->DataType
== GL_UNSIGNED_SHORT
)
739 else if (osmesa
->rb
->DataType
== GL_FLOAT
)
742 _mesa_problem(&osmesa
->mesa
,
743 "Unexpected datatype in osmesa::compute_row_addresses");
747 if ((osmesa
->format
== OSMESA_RGB
) || (osmesa
->format
== OSMESA_BGR
)) {
749 bytesPerPixel
= 3 * bpc
;
751 else if (osmesa
->format
== OSMESA_RGB_565
) {
752 /* 5/6/5 RGB pixel in 16 bits */
757 bytesPerPixel
= 4 * bpc
;
760 bytesPerRow
= rowlength
* bytesPerPixel
;
763 /* Y=0 is bottom line of window */
764 for (i
= 0; i
< height
; i
++) {
765 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
769 /* Y=0 is top line of window */
770 for (i
= 0; i
< height
; i
++) {
771 GLint j
= height
- i
- 1;
772 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
780 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
783 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
790 * Allocate renderbuffer storage. We don't actually allocate any storage
791 * since we're using a user-provided buffer.
792 * Just set up all the gl_renderbuffer methods.
795 osmesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
796 GLenum internalFormat
, GLuint width
, GLuint height
)
798 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
800 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
801 (void) internalFormat
;
803 if (osmesa
->format
== OSMESA_RGBA
) {
804 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
805 rb
->GetRow
= get_row_RGBA8
;
806 rb
->GetValues
= get_values_RGBA8
;
807 rb
->PutRow
= put_row_RGBA8
;
808 rb
->PutRowRGB
= put_row_rgb_RGBA8
;
809 rb
->PutMonoRow
= put_mono_row_RGBA8
;
810 rb
->PutValues
= put_values_RGBA8
;
811 rb
->PutMonoValues
= put_mono_values_RGBA8
;
813 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
814 rb
->GetRow
= get_row_RGBA16
;
815 rb
->GetValues
= get_values_RGBA16
;
816 rb
->PutRow
= put_row_RGBA16
;
817 rb
->PutRowRGB
= put_row_rgb_RGBA16
;
818 rb
->PutMonoRow
= put_mono_row_RGBA16
;
819 rb
->PutValues
= put_values_RGBA16
;
820 rb
->PutMonoValues
= put_mono_values_RGBA16
;
823 rb
->GetRow
= get_row_RGBA32
;
824 rb
->GetValues
= get_values_RGBA32
;
825 rb
->PutRow
= put_row_RGBA32
;
826 rb
->PutRowRGB
= put_row_rgb_RGBA32
;
827 rb
->PutMonoRow
= put_mono_row_RGBA32
;
828 rb
->PutValues
= put_values_RGBA32
;
829 rb
->PutMonoValues
= put_mono_values_RGBA32
;
832 else if (osmesa
->format
== OSMESA_BGRA
) {
833 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
834 rb
->GetRow
= get_row_BGRA8
;
835 rb
->GetValues
= get_values_BGRA8
;
836 rb
->PutRow
= put_row_BGRA8
;
837 rb
->PutRowRGB
= put_row_rgb_BGRA8
;
838 rb
->PutMonoRow
= put_mono_row_BGRA8
;
839 rb
->PutValues
= put_values_BGRA8
;
840 rb
->PutMonoValues
= put_mono_values_BGRA8
;
842 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
843 rb
->GetRow
= get_row_BGRA16
;
844 rb
->GetValues
= get_values_BGRA16
;
845 rb
->PutRow
= put_row_BGRA16
;
846 rb
->PutRowRGB
= put_row_rgb_BGRA16
;
847 rb
->PutMonoRow
= put_mono_row_BGRA16
;
848 rb
->PutValues
= put_values_BGRA16
;
849 rb
->PutMonoValues
= put_mono_values_BGRA16
;
852 rb
->GetRow
= get_row_BGRA32
;
853 rb
->GetValues
= get_values_BGRA32
;
854 rb
->PutRow
= put_row_BGRA32
;
855 rb
->PutRowRGB
= put_row_rgb_BGRA32
;
856 rb
->PutMonoRow
= put_mono_row_BGRA32
;
857 rb
->PutValues
= put_values_BGRA32
;
858 rb
->PutMonoValues
= put_mono_values_BGRA32
;
861 else if (osmesa
->format
== OSMESA_ARGB
) {
862 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
863 rb
->GetRow
= get_row_ARGB8
;
864 rb
->GetValues
= get_values_ARGB8
;
865 rb
->PutRow
= put_row_ARGB8
;
866 rb
->PutRowRGB
= put_row_rgb_ARGB8
;
867 rb
->PutMonoRow
= put_mono_row_ARGB8
;
868 rb
->PutValues
= put_values_ARGB8
;
869 rb
->PutMonoValues
= put_mono_values_ARGB8
;
871 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
872 rb
->GetRow
= get_row_ARGB16
;
873 rb
->GetValues
= get_values_ARGB16
;
874 rb
->PutRow
= put_row_ARGB16
;
875 rb
->PutRowRGB
= put_row_rgb_ARGB16
;
876 rb
->PutMonoRow
= put_mono_row_ARGB16
;
877 rb
->PutValues
= put_values_ARGB16
;
878 rb
->PutMonoValues
= put_mono_values_ARGB16
;
881 rb
->GetRow
= get_row_ARGB32
;
882 rb
->GetValues
= get_values_ARGB32
;
883 rb
->PutRow
= put_row_ARGB32
;
884 rb
->PutRowRGB
= put_row_rgb_ARGB32
;
885 rb
->PutMonoRow
= put_mono_row_ARGB32
;
886 rb
->PutValues
= put_values_ARGB32
;
887 rb
->PutMonoValues
= put_mono_values_ARGB32
;
890 else if (osmesa
->format
== OSMESA_RGB
) {
891 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
892 rb
->GetRow
= get_row_RGB8
;
893 rb
->GetValues
= get_values_RGB8
;
894 rb
->PutRow
= put_row_RGB8
;
895 rb
->PutRowRGB
= put_row_rgb_RGB8
;
896 rb
->PutMonoRow
= put_mono_row_RGB8
;
897 rb
->PutValues
= put_values_RGB8
;
898 rb
->PutMonoValues
= put_mono_values_RGB8
;
900 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
901 rb
->GetRow
= get_row_RGB16
;
902 rb
->GetValues
= get_values_RGB16
;
903 rb
->PutRow
= put_row_RGB16
;
904 rb
->PutRowRGB
= put_row_rgb_RGB16
;
905 rb
->PutMonoRow
= put_mono_row_RGB16
;
906 rb
->PutValues
= put_values_RGB16
;
907 rb
->PutMonoValues
= put_mono_values_RGB16
;
910 rb
->GetRow
= get_row_RGB32
;
911 rb
->GetValues
= get_values_RGB32
;
912 rb
->PutRow
= put_row_RGB32
;
913 rb
->PutRowRGB
= put_row_rgb_RGB32
;
914 rb
->PutMonoRow
= put_mono_row_RGB32
;
915 rb
->PutValues
= put_values_RGB32
;
916 rb
->PutMonoValues
= put_mono_values_RGB32
;
919 else if (osmesa
->format
== OSMESA_BGR
) {
920 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
921 rb
->GetRow
= get_row_BGR8
;
922 rb
->GetValues
= get_values_BGR8
;
923 rb
->PutRow
= put_row_BGR8
;
924 rb
->PutRowRGB
= put_row_rgb_BGR8
;
925 rb
->PutMonoRow
= put_mono_row_BGR8
;
926 rb
->PutValues
= put_values_BGR8
;
927 rb
->PutMonoValues
= put_mono_values_BGR8
;
929 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
930 rb
->GetRow
= get_row_BGR16
;
931 rb
->GetValues
= get_values_BGR16
;
932 rb
->PutRow
= put_row_BGR16
;
933 rb
->PutRowRGB
= put_row_rgb_BGR16
;
934 rb
->PutMonoRow
= put_mono_row_BGR16
;
935 rb
->PutValues
= put_values_BGR16
;
936 rb
->PutMonoValues
= put_mono_values_BGR16
;
939 rb
->GetRow
= get_row_BGR32
;
940 rb
->GetValues
= get_values_BGR32
;
941 rb
->PutRow
= put_row_BGR32
;
942 rb
->PutRowRGB
= put_row_rgb_BGR32
;
943 rb
->PutMonoRow
= put_mono_row_BGR32
;
944 rb
->PutValues
= put_values_BGR32
;
945 rb
->PutMonoValues
= put_mono_values_BGR32
;
948 else if (osmesa
->format
== OSMESA_RGB_565
) {
949 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
950 rb
->GetRow
= get_row_RGB_565
;
951 rb
->GetValues
= get_values_RGB_565
;
952 rb
->PutRow
= put_row_RGB_565
;
953 rb
->PutRowRGB
= put_row_rgb_RGB_565
;
954 rb
->PutMonoRow
= put_mono_row_RGB_565
;
955 rb
->PutValues
= put_values_RGB_565
;
956 rb
->PutMonoValues
= put_mono_values_RGB_565
;
959 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
965 compute_row_addresses( osmesa
);
972 * Allocate a new renderbuffer to describe the user-provided color buffer.
974 static struct gl_renderbuffer
*
975 new_osmesa_renderbuffer(struct gl_context
*ctx
, GLenum format
, GLenum type
)
977 const GLuint name
= 0;
978 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, name
);
981 rb
->Delete
= osmesa_delete_renderbuffer
;
982 rb
->AllocStorage
= osmesa_renderbuffer_storage
;
984 rb
->InternalFormat
= GL_RGBA
;
986 case GL_UNSIGNED_BYTE
:
987 rb
->Format
= MESA_FORMAT_RGBA8888
;
989 case GL_UNSIGNED_SHORT
:
990 rb
->Format
= MESA_FORMAT_RGBA_16
;
992 case GL_UNSIGNED_SHORT_5_6_5
:
993 rb
->Format
= MESA_FORMAT_RGB565
;
994 type
= GL_UNSIGNED_BYTE
;
997 rb
->Format
= MESA_FORMAT_RGBA_FLOAT32
;
1000 assert(0 && "Unexpected type in new_osmesa_renderbuffer()");
1001 rb
->Format
= MESA_FORMAT_RGBA8888
;
1003 rb
->_BaseFormat
= GL_RGBA
;
1004 rb
->DataType
= type
;
1010 /**********************************************************************/
1011 /***** Public Functions *****/
1012 /**********************************************************************/
1016 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1017 * an RGBA vs Color-Index mode flag.
1019 * Input: format - Must be GL_RGBA
1020 * sharelist - specifies another OSMesaContext with which to share
1021 * display lists. NULL indicates no sharing.
1022 * Return: an OSMesaContext or 0 if error
1024 GLAPI OSMesaContext GLAPIENTRY
1025 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
1027 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
1036 * Create context and specify size of ancillary buffers.
1038 GLAPI OSMesaContext GLAPIENTRY
1039 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
1040 GLint accumBits
, OSMesaContext sharelist
)
1042 OSMesaContext osmesa
;
1043 struct dd_function_table functions
;
1044 GLint rind
, gind
, bind
, aind
;
1045 GLint redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
1047 rind
= gind
= bind
= aind
= 0;
1048 if (format
==OSMESA_RGBA
) {
1049 redBits
= CHAN_BITS
;
1050 greenBits
= CHAN_BITS
;
1051 blueBits
= CHAN_BITS
;
1052 alphaBits
= CHAN_BITS
;
1058 else if (format
==OSMESA_BGRA
) {
1059 redBits
= CHAN_BITS
;
1060 greenBits
= CHAN_BITS
;
1061 blueBits
= CHAN_BITS
;
1062 alphaBits
= CHAN_BITS
;
1068 else if (format
==OSMESA_ARGB
) {
1069 redBits
= CHAN_BITS
;
1070 greenBits
= CHAN_BITS
;
1071 blueBits
= CHAN_BITS
;
1072 alphaBits
= CHAN_BITS
;
1078 else if (format
==OSMESA_RGB
) {
1079 redBits
= CHAN_BITS
;
1080 greenBits
= CHAN_BITS
;
1081 blueBits
= CHAN_BITS
;
1087 else if (format
==OSMESA_BGR
) {
1088 redBits
= CHAN_BITS
;
1089 greenBits
= CHAN_BITS
;
1090 blueBits
= CHAN_BITS
;
1096 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1097 else if (format
==OSMESA_RGB_565
) {
1102 rind
= 0; /* not used */
1111 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
1113 osmesa
->gl_visual
= _mesa_create_visual( GL_FALSE
, /* double buffer */
1114 GL_FALSE
, /* stereo */
1124 alphaBits
? accumBits
: 0,
1127 if (!osmesa
->gl_visual
) {
1132 /* Initialize device driver function table */
1133 _mesa_init_driver_functions(&functions
);
1134 /* override with our functions */
1135 functions
.GetString
= get_string
;
1136 functions
.UpdateState
= osmesa_update_state
;
1137 functions
.GetBufferSize
= NULL
;
1139 if (!_mesa_initialize_context(&osmesa
->mesa
,
1142 sharelist
? &sharelist
->mesa
1143 : (struct gl_context
*) NULL
,
1144 &functions
, (void *) osmesa
)) {
1145 _mesa_destroy_visual( osmesa
->gl_visual
);
1150 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
1151 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
1152 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
1153 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
1154 _mesa_enable_2_0_extensions(&(osmesa
->mesa
));
1155 _mesa_enable_2_1_extensions(&(osmesa
->mesa
));
1157 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
1158 if (!osmesa
->gl_buffer
) {
1159 _mesa_destroy_visual( osmesa
->gl_visual
);
1160 _mesa_free_context_data( &osmesa
->mesa
);
1165 /* Create depth/stencil/accum buffers. We'll create the color
1166 * buffer later in OSMesaMakeCurrent().
1168 _swrast_add_soft_renderbuffers(osmesa
->gl_buffer
,
1169 GL_FALSE
, /* color */
1170 osmesa
->gl_visual
->haveDepthBuffer
,
1171 osmesa
->gl_visual
->haveStencilBuffer
,
1172 osmesa
->gl_visual
->haveAccumBuffer
,
1173 GL_FALSE
, /* alpha */
1174 GL_FALSE
/* aux */ );
1176 osmesa
->format
= format
;
1177 osmesa
->userRowLength
= 0;
1178 osmesa
->yup
= GL_TRUE
;
1179 osmesa
->rInd
= rind
;
1180 osmesa
->gInd
= gind
;
1181 osmesa
->bInd
= bind
;
1182 osmesa
->aInd
= aind
;
1184 _mesa_meta_init(&osmesa
->mesa
);
1186 /* Initialize the software rasterizer and helper modules. */
1188 struct gl_context
*ctx
= &osmesa
->mesa
;
1192 if (!_swrast_CreateContext( ctx
) ||
1193 !_vbo_CreateContext( ctx
) ||
1194 !_tnl_CreateContext( ctx
) ||
1195 !_swsetup_CreateContext( ctx
)) {
1196 _mesa_destroy_visual(osmesa
->gl_visual
);
1197 _mesa_free_context_data(ctx
);
1202 _swsetup_Wakeup( ctx
);
1204 /* use default TCL pipeline */
1205 tnl
= TNL_CONTEXT(ctx
);
1206 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1208 /* Extend the software rasterizer with our optimized line and triangle
1209 * drawing functions.
1211 swrast
= SWRAST_CONTEXT( ctx
);
1212 swrast
->choose_line
= osmesa_choose_line
;
1213 swrast
->choose_triangle
= osmesa_choose_triangle
;
1221 * Destroy an Off-Screen Mesa rendering context.
1223 * \param osmesa the context to destroy
1225 GLAPI
void GLAPIENTRY
1226 OSMesaDestroyContext( OSMesaContext osmesa
)
1230 _mesa_reference_renderbuffer(&osmesa
->rb
, NULL
);
1232 _mesa_meta_free( &osmesa
->mesa
);
1234 _swsetup_DestroyContext( &osmesa
->mesa
);
1235 _tnl_DestroyContext( &osmesa
->mesa
);
1236 _vbo_DestroyContext( &osmesa
->mesa
);
1237 _swrast_DestroyContext( &osmesa
->mesa
);
1239 _mesa_destroy_visual( osmesa
->gl_visual
);
1240 _mesa_reference_framebuffer( &osmesa
->gl_buffer
, NULL
);
1242 _mesa_free_context_data( &osmesa
->mesa
);
1249 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1250 * block of memory which the client provides. Its size must be at least
1251 * as large as width*height*sizeof(type). Its address should be a multiple
1252 * of 4 if using RGBA mode.
1254 * Image data is stored in the order of glDrawPixels: row-major order
1255 * with the lower-left image pixel stored in the first array position
1256 * (ie. bottom-to-top).
1258 * If the context's viewport hasn't been initialized yet, it will now be
1259 * initialized to (0,0,width,height).
1261 * Input: osmesa - the rendering context
1262 * buffer - the image buffer memory
1263 * type - data type for pixel components
1264 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1265 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1266 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
1267 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
1268 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
1269 * width, height - size of image buffer in pixels, at least 1
1270 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
1271 * invalid buffer address, invalid type, width<1, height<1,
1272 * width>internal limit or height>internal limit.
1274 GLAPI GLboolean GLAPIENTRY
1275 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
1276 GLsizei width
, GLsizei height
)
1278 if (!osmesa
|| !buffer
||
1279 width
< 1 || height
< 1 ||
1280 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1284 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
1289 if (!(type
== GL_UNSIGNED_BYTE
||
1290 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
1291 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1292 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1297 osmesa_update_state( &osmesa
->mesa
, 0 );
1299 /* Call this periodically to detect when the user has begun using
1300 * GL rendering from multiple threads.
1302 _glapi_check_multithread();
1305 /* Create a front/left color buffer which wraps the user-provided buffer.
1306 * There is no back color buffer.
1307 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1308 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1309 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
1310 * that converts rendering from CHAN_BITS to the user-requested channel
1314 osmesa
->rb
= new_osmesa_renderbuffer(&osmesa
->mesa
, osmesa
->format
, type
);
1315 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1316 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1317 assert(osmesa
->rb
->RefCount
== 2);
1320 /* Set renderbuffer fields. Set width/height = 0 to force
1321 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1323 osmesa
->rb
->Data
= buffer
;
1324 osmesa
->rb
->Width
= osmesa
->rb
->Height
= 0;
1326 /* Set the framebuffer's size. This causes the
1327 * osmesa_renderbuffer_storage() function to get called.
1329 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1330 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1332 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1334 /* Remove renderbuffer attachment, then re-add. This installs the
1335 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1337 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1338 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1341 /* this updates the visual's red/green/blue/alphaBits fields */
1342 _mesa_update_framebuffer_visual(&osmesa
->mesa
, osmesa
->gl_buffer
);
1344 /* update the framebuffer size */
1345 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1352 GLAPI OSMesaContext GLAPIENTRY
1353 OSMesaGetCurrentContext( void )
1355 struct gl_context
*ctx
= _mesa_get_current_context();
1357 return (OSMesaContext
) ctx
;
1364 GLAPI
void GLAPIENTRY
1365 OSMesaPixelStore( GLint pname
, GLint value
)
1367 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1370 case OSMESA_ROW_LENGTH
:
1372 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1373 "OSMesaPixelStore(value)" );
1376 osmesa
->userRowLength
= value
;
1379 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1382 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1386 compute_row_addresses( osmesa
);
1390 GLAPI
void GLAPIENTRY
1391 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1393 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1397 if (osmesa
->gl_buffer
)
1398 *value
= osmesa
->gl_buffer
->Width
;
1403 if (osmesa
->gl_buffer
)
1404 *value
= osmesa
->gl_buffer
->Height
;
1409 *value
= osmesa
->format
;
1412 /* current color buffer's data type */
1414 *value
= osmesa
->rb
->DataType
;
1420 case OSMESA_ROW_LENGTH
:
1421 *value
= osmesa
->userRowLength
;
1424 *value
= osmesa
->yup
;
1426 case OSMESA_MAX_WIDTH
:
1429 case OSMESA_MAX_HEIGHT
:
1430 *value
= MAX_HEIGHT
;
1433 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1440 * Return the depth buffer associated with an OSMesa context.
1441 * Input: c - the OSMesa context
1442 * Output: width, height - size of buffer in pixels
1443 * bytesPerValue - bytes per depth value (2 or 4)
1444 * buffer - pointer to depth buffer values
1445 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1447 GLAPI GLboolean GLAPIENTRY
1448 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1449 GLint
*bytesPerValue
, void **buffer
)
1451 struct gl_renderbuffer
*rb
= NULL
;
1454 rb
= c
->gl_buffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1456 if (!rb
|| !rb
->Data
) {
1465 *height
= rb
->Height
;
1466 if (c
->gl_visual
->depthBits
<= 16)
1467 *bytesPerValue
= sizeof(GLushort
);
1469 *bytesPerValue
= sizeof(GLuint
);
1477 * Return the color buffer associated with an OSMesa context.
1478 * Input: c - the OSMesa context
1479 * Output: width, height - size of buffer in pixels
1480 * format - the pixel format (OSMESA_FORMAT)
1481 * buffer - pointer to color buffer values
1482 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1484 GLAPI GLboolean GLAPIENTRY
1485 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1486 GLint
*height
, GLint
*format
, void **buffer
)
1488 if (osmesa
->rb
&& osmesa
->rb
->Data
) {
1489 *width
= osmesa
->rb
->Width
;
1490 *height
= osmesa
->rb
->Height
;
1491 *format
= osmesa
->format
;
1492 *buffer
= osmesa
->rb
->Data
;
1505 struct name_function
1508 OSMESAproc Function
;
1511 static struct name_function functions
[] = {
1512 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1513 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1514 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1515 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1516 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1517 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1518 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1519 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1520 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1521 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1522 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1527 GLAPI OSMESAproc GLAPIENTRY
1528 OSMesaGetProcAddress( const char *funcName
)
1531 for (i
= 0; functions
[i
].Name
; i
++) {
1532 if (strcmp(functions
[i
].Name
, funcName
) == 0)
1533 return functions
[i
].Function
;
1535 return _glapi_get_proc_address(funcName
);
1539 GLAPI
void GLAPIENTRY
1540 OSMesaColorClamp(GLboolean enable
)
1542 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1544 if (enable
== GL_TRUE
) {
1545 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1548 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;
1554 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1555 * libglapi.a. We need to define them here.
1557 #ifdef GLX_INDIRECT_RENDERING
1559 #define GL_GLEXT_PROTOTYPES
1561 #include "glapi/glapi.h"
1562 #include "glapi/glapitable.h"
1564 #if defined(USE_MGL_NAMESPACE)
1565 #define NAME(func) mgl##func
1567 #define NAME(func) gl##func
1570 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1571 GET_DISPATCH()->FUNC ARGS
1573 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1574 return GET_DISPATCH()->FUNC ARGS
1576 /* skip normal ones */
1577 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1578 #include "glapi/glapitemp.h"
1580 #endif /* GLX_INDIRECT_RENDERING */