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/framebuffer.h"
41 #include "main/imports.h"
42 #include "main/mtypes.h"
43 #include "main/renderbuffer.h"
44 #include "swrast/swrast.h"
45 #include "swrast_setup/swrast_setup.h"
46 #include "swrast/s_context.h"
47 #include "swrast/s_lines.h"
48 #include "swrast/s_triangle.h"
50 #include "tnl/t_context.h"
51 #include "tnl/t_pipeline.h"
52 #include "drivers/common/driverfuncs.h"
58 * OSMesa rendering context, derived from core Mesa GLcontext.
62 GLcontext mesa
; /*< Base class - this must be first */
63 GLvisual
*gl_visual
; /*< Describes the buffers */
64 struct gl_renderbuffer
*rb
; /*< The user's colorbuffer */
65 GLframebuffer
*gl_buffer
; /*< The framebuffer, containing user's rb */
66 GLenum format
; /*< User-specified context format */
67 GLint userRowLength
; /*< user-specified number of pixels per row */
68 GLint rInd
, gInd
, bInd
, aInd
;/*< index offsets for RGBA formats */
69 GLvoid
*rowaddr
[MAX_HEIGHT
]; /*< address of first pixel in each image row */
70 GLboolean yup
; /*< TRUE -> Y increases upward */
71 /*< FALSE -> Y increases downward */
75 static INLINE OSMesaContext
76 OSMESA_CONTEXT(GLcontext
*ctx
)
78 /* Just cast, since we're using structure containment */
79 return (OSMesaContext
) ctx
;
83 /**********************************************************************/
84 /*** Private Device Driver Functions ***/
85 /**********************************************************************/
88 static const GLubyte
*
89 get_string( GLcontext
*ctx
, GLenum name
)
95 return (const GLubyte
*) "Mesa OffScreen32";
97 return (const GLubyte
*) "Mesa OffScreen16";
99 return (const GLubyte
*) "Mesa OffScreen";
108 osmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
110 /* easy - just propogate */
111 _swrast_InvalidateState( ctx
, new_state
);
112 _swsetup_InvalidateState( ctx
, new_state
);
113 _tnl_InvalidateState( ctx
, new_state
);
114 _vbo_InvalidateState( ctx
, new_state
);
119 /**********************************************************************/
120 /***** Read/write spans/arrays of pixels *****/
121 /**********************************************************************/
124 #define NAME(PREFIX) PREFIX##_RGBA8
125 #define RB_TYPE GLubyte
127 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
128 #define INIT_PIXEL_PTR(P, X, Y) \
129 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
130 #define INC_PIXEL_PTR(P) P += 4
131 #define STORE_PIXEL(DST, X, Y, VALUE) \
132 DST[0] = VALUE[RCOMP]; \
133 DST[1] = VALUE[GCOMP]; \
134 DST[2] = VALUE[BCOMP]; \
135 DST[3] = VALUE[ACOMP]
136 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
137 DST[0] = VALUE[RCOMP]; \
138 DST[1] = VALUE[GCOMP]; \
139 DST[2] = VALUE[BCOMP]; \
141 #define FETCH_PIXEL(DST, SRC) \
142 DST[RCOMP] = SRC[0]; \
143 DST[GCOMP] = SRC[1]; \
144 DST[BCOMP] = SRC[2]; \
146 #include "swrast/s_spantemp.h"
149 #define NAME(PREFIX) PREFIX##_RGBA16
150 #define RB_TYPE GLushort
152 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
153 #define INIT_PIXEL_PTR(P, X, Y) \
154 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
155 #define INC_PIXEL_PTR(P) P += 4
156 #define STORE_PIXEL(DST, X, Y, VALUE) \
157 DST[0] = VALUE[RCOMP]; \
158 DST[1] = VALUE[GCOMP]; \
159 DST[2] = VALUE[BCOMP]; \
160 DST[3] = VALUE[ACOMP]
161 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
162 DST[0] = VALUE[RCOMP]; \
163 DST[1] = VALUE[GCOMP]; \
164 DST[2] = VALUE[BCOMP]; \
166 #define FETCH_PIXEL(DST, SRC) \
167 DST[RCOMP] = SRC[0]; \
168 DST[GCOMP] = SRC[1]; \
169 DST[BCOMP] = SRC[2]; \
171 #include "swrast/s_spantemp.h"
174 #define NAME(PREFIX) PREFIX##_RGBA32
175 #define RB_TYPE GLfloat
177 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
178 #define INIT_PIXEL_PTR(P, X, Y) \
179 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
180 #define INC_PIXEL_PTR(P) P += 4
181 #define STORE_PIXEL(DST, X, Y, VALUE) \
182 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
183 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
184 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
185 DST[3] = CLAMP((VALUE[ACOMP]), 0.0F, 1.0F)
186 #define STORE_PIXEL_RGB(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); \
191 #define FETCH_PIXEL(DST, SRC) \
192 DST[RCOMP] = SRC[0]; \
193 DST[GCOMP] = SRC[1]; \
194 DST[BCOMP] = SRC[2]; \
196 #include "swrast/s_spantemp.h"
200 #define NAME(PREFIX) PREFIX##_BGRA8
201 #define RB_TYPE GLubyte
203 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
204 #define INIT_PIXEL_PTR(P, X, Y) \
205 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
206 #define INC_PIXEL_PTR(P) P += 4
207 #define STORE_PIXEL(DST, X, Y, VALUE) \
208 DST[2] = VALUE[RCOMP]; \
209 DST[1] = VALUE[GCOMP]; \
210 DST[0] = VALUE[BCOMP]; \
211 DST[3] = VALUE[ACOMP]
212 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
213 DST[2] = VALUE[RCOMP]; \
214 DST[1] = VALUE[GCOMP]; \
215 DST[0] = VALUE[BCOMP]; \
217 #define FETCH_PIXEL(DST, SRC) \
218 DST[RCOMP] = SRC[2]; \
219 DST[GCOMP] = SRC[1]; \
220 DST[BCOMP] = SRC[0]; \
222 #include "swrast/s_spantemp.h"
225 #define NAME(PREFIX) PREFIX##_BGRA16
226 #define RB_TYPE GLushort
228 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
229 #define INIT_PIXEL_PTR(P, X, Y) \
230 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
231 #define INC_PIXEL_PTR(P) P += 4
232 #define STORE_PIXEL(DST, X, Y, VALUE) \
233 DST[2] = VALUE[RCOMP]; \
234 DST[1] = VALUE[GCOMP]; \
235 DST[0] = VALUE[BCOMP]; \
236 DST[3] = VALUE[ACOMP]
237 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
238 DST[2] = VALUE[RCOMP]; \
239 DST[1] = VALUE[GCOMP]; \
240 DST[0] = VALUE[BCOMP]; \
242 #define FETCH_PIXEL(DST, SRC) \
243 DST[RCOMP] = SRC[2]; \
244 DST[GCOMP] = SRC[1]; \
245 DST[BCOMP] = SRC[0]; \
247 #include "swrast/s_spantemp.h"
250 #define NAME(PREFIX) PREFIX##_BGRA32
251 #define RB_TYPE GLfloat
253 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
254 #define INIT_PIXEL_PTR(P, X, Y) \
255 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
256 #define INC_PIXEL_PTR(P) P += 4
257 #define STORE_PIXEL(DST, X, Y, VALUE) \
258 DST[2] = VALUE[RCOMP]; \
259 DST[1] = VALUE[GCOMP]; \
260 DST[0] = VALUE[BCOMP]; \
261 DST[3] = VALUE[ACOMP]
262 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
263 DST[2] = VALUE[RCOMP]; \
264 DST[1] = VALUE[GCOMP]; \
265 DST[0] = VALUE[BCOMP]; \
267 #define FETCH_PIXEL(DST, SRC) \
268 DST[RCOMP] = SRC[2]; \
269 DST[GCOMP] = SRC[1]; \
270 DST[BCOMP] = SRC[0]; \
272 #include "swrast/s_spantemp.h"
276 #define NAME(PREFIX) PREFIX##_ARGB8
277 #define RB_TYPE GLubyte
279 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
280 #define INIT_PIXEL_PTR(P, X, Y) \
281 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
282 #define INC_PIXEL_PTR(P) P += 4
283 #define STORE_PIXEL(DST, X, Y, VALUE) \
284 DST[1] = VALUE[RCOMP]; \
285 DST[2] = VALUE[GCOMP]; \
286 DST[3] = VALUE[BCOMP]; \
287 DST[0] = VALUE[ACOMP]
288 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
289 DST[1] = VALUE[RCOMP]; \
290 DST[2] = VALUE[GCOMP]; \
291 DST[3] = VALUE[BCOMP]; \
293 #define FETCH_PIXEL(DST, SRC) \
294 DST[RCOMP] = SRC[1]; \
295 DST[GCOMP] = SRC[2]; \
296 DST[BCOMP] = SRC[3]; \
298 #include "swrast/s_spantemp.h"
301 #define NAME(PREFIX) PREFIX##_ARGB16
302 #define RB_TYPE GLushort
304 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
305 #define INIT_PIXEL_PTR(P, X, Y) \
306 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
307 #define INC_PIXEL_PTR(P) P += 4
308 #define STORE_PIXEL(DST, X, Y, VALUE) \
309 DST[1] = VALUE[RCOMP]; \
310 DST[2] = VALUE[GCOMP]; \
311 DST[3] = VALUE[BCOMP]; \
312 DST[0] = VALUE[ACOMP]
313 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
314 DST[1] = VALUE[RCOMP]; \
315 DST[2] = VALUE[GCOMP]; \
316 DST[3] = VALUE[BCOMP]; \
318 #define FETCH_PIXEL(DST, SRC) \
319 DST[RCOMP] = SRC[1]; \
320 DST[GCOMP] = SRC[2]; \
321 DST[BCOMP] = SRC[3]; \
323 #include "swrast/s_spantemp.h"
326 #define NAME(PREFIX) PREFIX##_ARGB32
327 #define RB_TYPE GLfloat
329 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
330 #define INIT_PIXEL_PTR(P, X, Y) \
331 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
332 #define INC_PIXEL_PTR(P) P += 4
333 #define STORE_PIXEL(DST, X, Y, VALUE) \
334 DST[1] = VALUE[RCOMP]; \
335 DST[2] = VALUE[GCOMP]; \
336 DST[3] = VALUE[BCOMP]; \
337 DST[0] = VALUE[ACOMP]
338 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
339 DST[1] = VALUE[RCOMP]; \
340 DST[2] = VALUE[GCOMP]; \
341 DST[3] = VALUE[BCOMP]; \
343 #define FETCH_PIXEL(DST, SRC) \
344 DST[RCOMP] = SRC[1]; \
345 DST[GCOMP] = SRC[2]; \
346 DST[BCOMP] = SRC[3]; \
348 #include "swrast/s_spantemp.h"
352 #define NAME(PREFIX) PREFIX##_RGB8
353 #define RB_TYPE GLubyte
355 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
356 #define INIT_PIXEL_PTR(P, X, Y) \
357 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
358 #define INC_PIXEL_PTR(P) P += 3
359 #define STORE_PIXEL(DST, X, Y, VALUE) \
360 DST[0] = VALUE[RCOMP]; \
361 DST[1] = VALUE[GCOMP]; \
362 DST[2] = VALUE[BCOMP]
363 #define FETCH_PIXEL(DST, SRC) \
364 DST[RCOMP] = SRC[0]; \
365 DST[GCOMP] = SRC[1]; \
366 DST[BCOMP] = SRC[2]; \
368 #include "swrast/s_spantemp.h"
371 #define NAME(PREFIX) PREFIX##_RGB16
372 #define RB_TYPE GLushort
374 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
375 #define INIT_PIXEL_PTR(P, X, Y) \
376 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
377 #define INC_PIXEL_PTR(P) P += 3
378 #define STORE_PIXEL(DST, X, Y, VALUE) \
379 DST[0] = VALUE[RCOMP]; \
380 DST[1] = VALUE[GCOMP]; \
381 DST[2] = VALUE[BCOMP]
382 #define FETCH_PIXEL(DST, SRC) \
383 DST[RCOMP] = SRC[0]; \
384 DST[GCOMP] = SRC[1]; \
385 DST[BCOMP] = SRC[2]; \
387 #include "swrast/s_spantemp.h"
390 #define NAME(PREFIX) PREFIX##_RGB32
391 #define RB_TYPE GLfloat
393 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
394 #define INIT_PIXEL_PTR(P, X, Y) \
395 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
396 #define INC_PIXEL_PTR(P) P += 3
397 #define STORE_PIXEL(DST, X, Y, VALUE) \
398 DST[0] = VALUE[RCOMP]; \
399 DST[1] = VALUE[GCOMP]; \
400 DST[2] = VALUE[BCOMP]
401 #define FETCH_PIXEL(DST, SRC) \
402 DST[RCOMP] = SRC[0]; \
403 DST[GCOMP] = SRC[1]; \
404 DST[BCOMP] = SRC[2]; \
406 #include "swrast/s_spantemp.h"
410 #define NAME(PREFIX) PREFIX##_BGR8
411 #define RB_TYPE GLubyte
413 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
414 #define INIT_PIXEL_PTR(P, X, Y) \
415 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
416 #define INC_PIXEL_PTR(P) P += 3
417 #define STORE_PIXEL(DST, X, Y, VALUE) \
418 DST[2] = VALUE[RCOMP]; \
419 DST[1] = VALUE[GCOMP]; \
420 DST[0] = VALUE[BCOMP]
421 #define FETCH_PIXEL(DST, SRC) \
422 DST[RCOMP] = SRC[2]; \
423 DST[GCOMP] = SRC[1]; \
424 DST[BCOMP] = SRC[0]; \
426 #include "swrast/s_spantemp.h"
429 #define NAME(PREFIX) PREFIX##_BGR16
430 #define RB_TYPE GLushort
432 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
433 #define INIT_PIXEL_PTR(P, X, Y) \
434 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
435 #define INC_PIXEL_PTR(P) P += 3
436 #define STORE_PIXEL(DST, X, Y, VALUE) \
437 DST[2] = VALUE[RCOMP]; \
438 DST[1] = VALUE[GCOMP]; \
439 DST[0] = VALUE[BCOMP]
440 #define FETCH_PIXEL(DST, SRC) \
441 DST[RCOMP] = SRC[2]; \
442 DST[GCOMP] = SRC[1]; \
443 DST[BCOMP] = SRC[0]; \
445 #include "swrast/s_spantemp.h"
448 #define NAME(PREFIX) PREFIX##_BGR32
449 #define RB_TYPE GLfloat
451 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
452 #define INIT_PIXEL_PTR(P, X, Y) \
453 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
454 #define INC_PIXEL_PTR(P) P += 3
455 #define STORE_PIXEL(DST, X, Y, VALUE) \
456 DST[2] = VALUE[RCOMP]; \
457 DST[1] = VALUE[GCOMP]; \
458 DST[0] = VALUE[BCOMP]
459 #define FETCH_PIXEL(DST, SRC) \
460 DST[RCOMP] = SRC[2]; \
461 DST[GCOMP] = SRC[1]; \
462 DST[BCOMP] = SRC[0]; \
464 #include "swrast/s_spantemp.h"
467 /* 16-bit 5/6/5 RGB */
468 #define NAME(PREFIX) PREFIX##_RGB_565
469 #define RB_TYPE GLubyte
471 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
472 #define INIT_PIXEL_PTR(P, X, Y) \
473 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
474 #define INC_PIXEL_PTR(P) P += 1
475 #define STORE_PIXEL(DST, X, Y, VALUE) \
476 *DST = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
477 #define FETCH_PIXEL(DST, SRC) \
478 DST[RCOMP] = ( (((*SRC) >> 8) & 0xf8) | (((*SRC) >> 11) & 0x7) ); \
479 DST[GCOMP] = ( (((*SRC) >> 3) & 0xfc) | (((*SRC) >> 5) & 0x3) ); \
480 DST[BCOMP] = ( (((*SRC) << 3) & 0xf8) | (((*SRC) ) & 0x7) ); \
481 DST[ACOMP] = CHAN_MAX
482 #include "swrast/s_spantemp.h"
486 #define NAME(PREFIX) PREFIX##_CI
488 #define RB_TYPE GLubyte
490 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
491 #define INIT_PIXEL_PTR(P, X, Y) \
492 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + (X)
493 #define INC_PIXEL_PTR(P) P += 1
494 #define STORE_PIXEL(DST, X, Y, VALUE) \
496 #define FETCH_PIXEL(DST, SRC) \
498 #include "swrast/s_spantemp.h"
504 * Macros for optimized line/triangle rendering.
505 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
508 #define PACK_RGBA(DST, R, G, B, A) \
510 (DST)[osmesa->rInd] = R; \
511 (DST)[osmesa->gInd] = G; \
512 (DST)[osmesa->bInd] = B; \
513 (DST)[osmesa->aInd] = A; \
516 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
520 * Draw a flat-shaded, RGB line into an osmesa buffer.
522 #define NAME flat_rgba_line
525 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
526 const GLchan *color = vert1->color;
530 GLchan *p = PIXELADDR4(X, Y); \
531 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
535 #include "..\swrast\s_linetemp.h"
537 #include "swrast/s_linetemp.h"
543 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
545 #define NAME flat_rgba_z_line
548 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
550 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
551 const GLchan *color = vert1->color;
556 GLchan *p = PIXELADDR4(X, Y); \
557 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
558 color[BCOMP], color[ACOMP]); \
564 #include "..\swrast\s_linetemp.h"
566 #include "swrast/s_linetemp.h"
572 * Analyze context state to see if we can provide a fast line drawing
573 * function. Otherwise, return NULL.
575 static swrast_line_func
576 osmesa_choose_line_function( GLcontext
*ctx
)
578 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
579 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
581 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
584 if (ctx
->RenderMode
!= GL_RENDER
) return NULL
;
585 if (ctx
->Line
.SmoothFlag
) return NULL
;
586 if (ctx
->Texture
._EnabledUnits
) return NULL
;
587 if (ctx
->Light
.ShadeModel
!= GL_FLAT
) return NULL
;
588 if (ctx
->Line
.Width
!= 1.0F
) return NULL
;
589 if (ctx
->Line
.StippleFlag
) return NULL
;
590 if (ctx
->Line
.SmoothFlag
) return NULL
;
591 if (osmesa
->format
!= OSMESA_RGBA
&&
592 osmesa
->format
!= OSMESA_BGRA
&&
593 osmesa
->format
!= OSMESA_ARGB
) return NULL
;
595 if (swrast
->_RasterMask
==DEPTH_BIT
596 && ctx
->Depth
.Func
==GL_LESS
597 && ctx
->Depth
.Mask
==GL_TRUE
598 && ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
599 return (swrast_line_func
) flat_rgba_z_line
;
602 if (swrast
->_RasterMask
== 0) {
603 return (swrast_line_func
) flat_rgba_line
;
606 return (swrast_line_func
) NULL
;
610 /**********************************************************************/
611 /***** Optimized triangle rendering *****/
612 /**********************************************************************/
616 * Smooth-shaded, z-less triangle, RGBA color.
618 #define NAME smooth_rgba_z_triangle
620 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
622 #define INTERP_ALPHA 1
624 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
625 #define RENDER_SPAN( span ) { \
627 GLchan *img = PIXELADDR4(span.x, span.y); \
628 for (i = 0; i < span.end; i++, img += 4) { \
629 const GLuint z = FixedToDepth(span.z); \
631 PACK_RGBA(img, FixedToChan(span.red), \
632 FixedToChan(span.green), FixedToChan(span.blue), \
633 FixedToChan(span.alpha)); \
636 span.red += span.redStep; \
637 span.green += span.greenStep; \
638 span.blue += span.blueStep; \
639 span.alpha += span.alphaStep; \
640 span.z += span.zStep; \
644 #include "..\swrast\s_tritemp.h"
646 #include "swrast/s_tritemp.h"
652 * Flat-shaded, z-less triangle, RGBA color.
654 #define NAME flat_rgba_z_triangle
656 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
658 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
660 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
661 v2->color[2], v2->color[3]);
663 #define RENDER_SPAN( span ) { \
665 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
666 for (i = 0; i < span.end; i++) { \
667 const GLuint z = FixedToDepth(span.z); \
672 span.z += span.zStep; \
676 #include "..\swrast\s_tritemp.h"
678 #include "swrast/s_tritemp.h"
684 * Return pointer to an optimized triangle function if possible.
686 static swrast_tri_func
687 osmesa_choose_triangle_function( GLcontext
*ctx
)
689 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
690 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
692 if (osmesa
->rb
->DataType
!= GL_UNSIGNED_BYTE
)
693 return (swrast_tri_func
) NULL
;
695 if (ctx
->RenderMode
!= GL_RENDER
) return (swrast_tri_func
) NULL
;
696 if (ctx
->Polygon
.SmoothFlag
) return (swrast_tri_func
) NULL
;
697 if (ctx
->Polygon
.StippleFlag
) return (swrast_tri_func
) NULL
;
698 if (ctx
->Texture
._EnabledUnits
) return (swrast_tri_func
) NULL
;
699 if (osmesa
->format
!= OSMESA_RGBA
&&
700 osmesa
->format
!= OSMESA_BGRA
&&
701 osmesa
->format
!= OSMESA_ARGB
) return (swrast_tri_func
) NULL
;
702 if (ctx
->Polygon
.CullFlag
&&
703 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
704 return (swrast_tri_func
) NULL
;
706 if (swrast
->_RasterMask
== DEPTH_BIT
&&
707 ctx
->Depth
.Func
== GL_LESS
&&
708 ctx
->Depth
.Mask
== GL_TRUE
&&
709 ctx
->Visual
.depthBits
== DEFAULT_SOFTWARE_DEPTH_BITS
) {
710 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
711 return (swrast_tri_func
) smooth_rgba_z_triangle
;
714 return (swrast_tri_func
) flat_rgba_z_triangle
;
717 return (swrast_tri_func
) NULL
;
722 /* Override for the swrast triangle-selection function. Try to use one
723 * of our internal triangle functions, otherwise fall back to the
724 * standard swrast functions.
727 osmesa_choose_triangle( GLcontext
*ctx
)
729 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
731 swrast
->Triangle
= osmesa_choose_triangle_function( ctx
);
732 if (!swrast
->Triangle
)
733 _swrast_choose_triangle( ctx
);
737 osmesa_choose_line( GLcontext
*ctx
)
739 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
741 swrast
->Line
= osmesa_choose_line_function( ctx
);
743 _swrast_choose_line( ctx
);
749 * Recompute the values of the context's rowaddr array.
752 compute_row_addresses( OSMesaContext osmesa
)
754 GLint bytesPerPixel
, bytesPerRow
, i
;
755 GLubyte
*origin
= (GLubyte
*) osmesa
->rb
->Data
;
756 GLint bpc
; /* bytes per channel */
757 GLint rowlength
; /* in pixels */
758 GLint height
= osmesa
->rb
->Height
;
760 if (osmesa
->userRowLength
)
761 rowlength
= osmesa
->userRowLength
;
763 rowlength
= osmesa
->rb
->Width
;
765 if (osmesa
->rb
->DataType
== GL_UNSIGNED_BYTE
)
767 else if (osmesa
->rb
->DataType
== GL_UNSIGNED_SHORT
)
769 else if (osmesa
->rb
->DataType
== GL_FLOAT
)
772 _mesa_problem(&osmesa
->mesa
,
773 "Unexpected datatype in osmesa::compute_row_addresses");
777 if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
779 bytesPerPixel
= 1 * sizeof(GLubyte
);
781 else if ((osmesa
->format
== OSMESA_RGB
) || (osmesa
->format
== OSMESA_BGR
)) {
783 bytesPerPixel
= 3 * bpc
;
785 else if (osmesa
->format
== OSMESA_RGB_565
) {
786 /* 5/6/5 RGB pixel in 16 bits */
791 bytesPerPixel
= 4 * bpc
;
794 bytesPerRow
= rowlength
* bytesPerPixel
;
797 /* Y=0 is bottom line of window */
798 for (i
= 0; i
< height
; i
++) {
799 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ i
* bytesPerRow
);
803 /* Y=0 is top line of window */
804 for (i
= 0; i
< height
; i
++) {
805 GLint j
= height
- i
- 1;
806 osmesa
->rowaddr
[i
] = (GLvoid
*) ((GLubyte
*) origin
+ j
* bytesPerRow
);
814 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
817 osmesa_delete_renderbuffer(struct gl_renderbuffer
*rb
)
824 * Allocate renderbuffer storage. We don't actually allocate any storage
825 * since we're using a user-provided buffer.
826 * Just set up all the gl_renderbuffer methods.
829 osmesa_renderbuffer_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
830 GLenum internalFormat
, GLuint width
, GLuint height
)
832 const OSMesaContext osmesa
= OSMESA_CONTEXT(ctx
);
833 GLint bpc
; /* bits per channel */
835 if (rb
->DataType
== GL_UNSIGNED_BYTE
)
837 else if (rb
->DataType
== GL_UNSIGNED_SHORT
)
847 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
848 (void) internalFormat
;
850 if (osmesa
->format
== OSMESA_RGBA
) {
851 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
852 rb
->GetRow
= get_row_RGBA8
;
853 rb
->GetValues
= get_values_RGBA8
;
854 rb
->PutRow
= put_row_RGBA8
;
855 rb
->PutRowRGB
= put_row_rgb_RGBA8
;
856 rb
->PutMonoRow
= put_mono_row_RGBA8
;
857 rb
->PutValues
= put_values_RGBA8
;
858 rb
->PutMonoValues
= put_mono_values_RGBA8
;
860 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
861 rb
->GetRow
= get_row_RGBA16
;
862 rb
->GetValues
= get_values_RGBA16
;
863 rb
->PutRow
= put_row_RGBA16
;
864 rb
->PutRowRGB
= put_row_rgb_RGBA16
;
865 rb
->PutMonoRow
= put_mono_row_RGBA16
;
866 rb
->PutValues
= put_values_RGBA16
;
867 rb
->PutMonoValues
= put_mono_values_RGBA16
;
870 rb
->GetRow
= get_row_RGBA32
;
871 rb
->GetValues
= get_values_RGBA32
;
872 rb
->PutRow
= put_row_RGBA32
;
873 rb
->PutRowRGB
= put_row_rgb_RGBA32
;
874 rb
->PutMonoRow
= put_mono_row_RGBA32
;
875 rb
->PutValues
= put_values_RGBA32
;
876 rb
->PutMonoValues
= put_mono_values_RGBA32
;
878 rb
->RedBits
= rb
->GreenBits
= rb
->BlueBits
= rb
->AlphaBits
= bpc
;
880 else if (osmesa
->format
== OSMESA_BGRA
) {
881 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
882 rb
->GetRow
= get_row_BGRA8
;
883 rb
->GetValues
= get_values_BGRA8
;
884 rb
->PutRow
= put_row_BGRA8
;
885 rb
->PutRowRGB
= put_row_rgb_BGRA8
;
886 rb
->PutMonoRow
= put_mono_row_BGRA8
;
887 rb
->PutValues
= put_values_BGRA8
;
888 rb
->PutMonoValues
= put_mono_values_BGRA8
;
890 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
891 rb
->GetRow
= get_row_BGRA16
;
892 rb
->GetValues
= get_values_BGRA16
;
893 rb
->PutRow
= put_row_BGRA16
;
894 rb
->PutRowRGB
= put_row_rgb_BGRA16
;
895 rb
->PutMonoRow
= put_mono_row_BGRA16
;
896 rb
->PutValues
= put_values_BGRA16
;
897 rb
->PutMonoValues
= put_mono_values_BGRA16
;
900 rb
->GetRow
= get_row_BGRA32
;
901 rb
->GetValues
= get_values_BGRA32
;
902 rb
->PutRow
= put_row_BGRA32
;
903 rb
->PutRowRGB
= put_row_rgb_BGRA32
;
904 rb
->PutMonoRow
= put_mono_row_BGRA32
;
905 rb
->PutValues
= put_values_BGRA32
;
906 rb
->PutMonoValues
= put_mono_values_BGRA32
;
908 rb
->RedBits
= rb
->GreenBits
= rb
->BlueBits
= rb
->AlphaBits
= bpc
;
910 else if (osmesa
->format
== OSMESA_ARGB
) {
911 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
912 rb
->GetRow
= get_row_ARGB8
;
913 rb
->GetValues
= get_values_ARGB8
;
914 rb
->PutRow
= put_row_ARGB8
;
915 rb
->PutRowRGB
= put_row_rgb_ARGB8
;
916 rb
->PutMonoRow
= put_mono_row_ARGB8
;
917 rb
->PutValues
= put_values_ARGB8
;
918 rb
->PutMonoValues
= put_mono_values_ARGB8
;
920 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
921 rb
->GetRow
= get_row_ARGB16
;
922 rb
->GetValues
= get_values_ARGB16
;
923 rb
->PutRow
= put_row_ARGB16
;
924 rb
->PutRowRGB
= put_row_rgb_ARGB16
;
925 rb
->PutMonoRow
= put_mono_row_ARGB16
;
926 rb
->PutValues
= put_values_ARGB16
;
927 rb
->PutMonoValues
= put_mono_values_ARGB16
;
930 rb
->GetRow
= get_row_ARGB32
;
931 rb
->GetValues
= get_values_ARGB32
;
932 rb
->PutRow
= put_row_ARGB32
;
933 rb
->PutRowRGB
= put_row_rgb_ARGB32
;
934 rb
->PutMonoRow
= put_mono_row_ARGB32
;
935 rb
->PutValues
= put_values_ARGB32
;
936 rb
->PutMonoValues
= put_mono_values_ARGB32
;
938 rb
->RedBits
= rb
->GreenBits
= rb
->BlueBits
= rb
->AlphaBits
= bpc
;
940 else if (osmesa
->format
== OSMESA_RGB
) {
941 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
942 rb
->GetRow
= get_row_RGB8
;
943 rb
->GetValues
= get_values_RGB8
;
944 rb
->PutRow
= put_row_RGB8
;
945 rb
->PutRowRGB
= put_row_rgb_RGB8
;
946 rb
->PutMonoRow
= put_mono_row_RGB8
;
947 rb
->PutValues
= put_values_RGB8
;
948 rb
->PutMonoValues
= put_mono_values_RGB8
;
950 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
951 rb
->GetRow
= get_row_RGB16
;
952 rb
->GetValues
= get_values_RGB16
;
953 rb
->PutRow
= put_row_RGB16
;
954 rb
->PutRowRGB
= put_row_rgb_RGB16
;
955 rb
->PutMonoRow
= put_mono_row_RGB16
;
956 rb
->PutValues
= put_values_RGB16
;
957 rb
->PutMonoValues
= put_mono_values_RGB16
;
960 rb
->GetRow
= get_row_RGB32
;
961 rb
->GetValues
= get_values_RGB32
;
962 rb
->PutRow
= put_row_RGB32
;
963 rb
->PutRowRGB
= put_row_rgb_RGB32
;
964 rb
->PutMonoRow
= put_mono_row_RGB32
;
965 rb
->PutValues
= put_values_RGB32
;
966 rb
->PutMonoValues
= put_mono_values_RGB32
;
968 rb
->RedBits
= rb
->GreenBits
= rb
->BlueBits
= bpc
;
970 else if (osmesa
->format
== OSMESA_BGR
) {
971 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
972 rb
->GetRow
= get_row_BGR8
;
973 rb
->GetValues
= get_values_BGR8
;
974 rb
->PutRow
= put_row_BGR8
;
975 rb
->PutRowRGB
= put_row_rgb_BGR8
;
976 rb
->PutMonoRow
= put_mono_row_BGR8
;
977 rb
->PutValues
= put_values_BGR8
;
978 rb
->PutMonoValues
= put_mono_values_BGR8
;
980 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
981 rb
->GetRow
= get_row_BGR16
;
982 rb
->GetValues
= get_values_BGR16
;
983 rb
->PutRow
= put_row_BGR16
;
984 rb
->PutRowRGB
= put_row_rgb_BGR16
;
985 rb
->PutMonoRow
= put_mono_row_BGR16
;
986 rb
->PutValues
= put_values_BGR16
;
987 rb
->PutMonoValues
= put_mono_values_BGR16
;
990 rb
->GetRow
= get_row_BGR32
;
991 rb
->GetValues
= get_values_BGR32
;
992 rb
->PutRow
= put_row_BGR32
;
993 rb
->PutRowRGB
= put_row_rgb_BGR32
;
994 rb
->PutMonoRow
= put_mono_row_BGR32
;
995 rb
->PutValues
= put_values_BGR32
;
996 rb
->PutMonoValues
= put_mono_values_BGR32
;
998 rb
->RedBits
= rb
->GreenBits
= rb
->BlueBits
= bpc
;
1000 else if (osmesa
->format
== OSMESA_RGB_565
) {
1001 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
1002 rb
->GetRow
= get_row_RGB_565
;
1003 rb
->GetValues
= get_values_RGB_565
;
1004 rb
->PutRow
= put_row_RGB_565
;
1005 rb
->PutRowRGB
= put_row_rgb_RGB_565
;
1006 rb
->PutMonoRow
= put_mono_row_RGB_565
;
1007 rb
->PutValues
= put_values_RGB_565
;
1008 rb
->PutMonoValues
= put_mono_values_RGB_565
;
1013 else if (osmesa
->format
== OSMESA_COLOR_INDEX
) {
1014 rb
->GetRow
= get_row_CI
;
1015 rb
->GetValues
= get_values_CI
;
1016 rb
->PutRow
= put_row_CI
;
1017 rb
->PutMonoRow
= put_mono_row_CI
;
1018 rb
->PutValues
= put_values_CI
;
1019 rb
->PutMonoValues
= put_mono_values_CI
;
1023 _mesa_problem(ctx
, "bad pixel format in osmesa renderbuffer_storage");
1027 rb
->Height
= height
;
1029 compute_row_addresses( osmesa
);
1036 * Allocate a new renderbuffer to describe the user-provided color buffer.
1038 static struct gl_renderbuffer
*
1039 new_osmesa_renderbuffer(GLcontext
*ctx
, GLenum format
, GLenum type
)
1041 const GLuint name
= 0;
1042 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, name
);
1045 rb
->Delete
= osmesa_delete_renderbuffer
;
1046 rb
->AllocStorage
= osmesa_renderbuffer_storage
;
1048 if (format
== OSMESA_COLOR_INDEX
) {
1049 rb
->InternalFormat
= GL_COLOR_INDEX
;
1050 rb
->_ActualFormat
= GL_COLOR_INDEX8_EXT
;
1051 rb
->_BaseFormat
= GL_COLOR_INDEX
;
1052 rb
->DataType
= GL_UNSIGNED_BYTE
;
1055 rb
->InternalFormat
= GL_RGBA
;
1056 rb
->_ActualFormat
= GL_RGBA
;
1057 rb
->_BaseFormat
= GL_RGBA
;
1058 rb
->DataType
= type
;
1065 /**********************************************************************/
1066 /***** Public Functions *****/
1067 /**********************************************************************/
1071 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1072 * an RGBA vs Color-Index mode flag.
1074 * Input: format - either GL_RGBA or GL_COLOR_INDEX
1075 * sharelist - specifies another OSMesaContext with which to share
1076 * display lists. NULL indicates no sharing.
1077 * Return: an OSMesaContext or 0 if error
1079 GLAPI OSMesaContext GLAPIENTRY
1080 OSMesaCreateContext( GLenum format
, OSMesaContext sharelist
)
1082 const GLint accumBits
= (format
== OSMESA_COLOR_INDEX
) ? 0 : 16;
1083 return OSMesaCreateContextExt(format
, DEFAULT_SOFTWARE_DEPTH_BITS
,
1084 8, accumBits
, sharelist
);
1092 * Create context and specify size of ancillary buffers.
1094 GLAPI OSMesaContext GLAPIENTRY
1095 OSMesaCreateContextExt( GLenum format
, GLint depthBits
, GLint stencilBits
,
1096 GLint accumBits
, OSMesaContext sharelist
)
1098 OSMesaContext osmesa
;
1099 struct dd_function_table functions
;
1100 GLint rind
, gind
, bind
, aind
;
1101 GLint indexBits
= 0, redBits
= 0, greenBits
= 0, blueBits
= 0, alphaBits
=0;
1103 GLenum type
= CHAN_TYPE
;
1105 rind
= gind
= bind
= aind
= 0;
1106 if (format
==OSMESA_COLOR_INDEX
) {
1110 else if (format
==OSMESA_RGBA
) {
1112 redBits
= CHAN_BITS
;
1113 greenBits
= CHAN_BITS
;
1114 blueBits
= CHAN_BITS
;
1115 alphaBits
= CHAN_BITS
;
1122 else if (format
==OSMESA_BGRA
) {
1124 redBits
= CHAN_BITS
;
1125 greenBits
= CHAN_BITS
;
1126 blueBits
= CHAN_BITS
;
1127 alphaBits
= CHAN_BITS
;
1134 else if (format
==OSMESA_ARGB
) {
1136 redBits
= CHAN_BITS
;
1137 greenBits
= CHAN_BITS
;
1138 blueBits
= CHAN_BITS
;
1139 alphaBits
= CHAN_BITS
;
1146 else if (format
==OSMESA_RGB
) {
1148 redBits
= CHAN_BITS
;
1149 greenBits
= CHAN_BITS
;
1150 blueBits
= CHAN_BITS
;
1157 else if (format
==OSMESA_BGR
) {
1159 redBits
= CHAN_BITS
;
1160 greenBits
= CHAN_BITS
;
1161 blueBits
= CHAN_BITS
;
1168 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1169 else if (format
==OSMESA_RGB_565
) {
1175 rind
= 0; /* not used */
1185 osmesa
= (OSMesaContext
) CALLOC_STRUCT(osmesa_context
);
1187 osmesa
->gl_visual
= _mesa_create_visual( rgbmode
,
1188 GL_FALSE
, /* double buffer */
1189 GL_FALSE
, /* stereo */
1200 alphaBits
? accumBits
: 0,
1203 if (!osmesa
->gl_visual
) {
1208 /* Initialize device driver function table */
1209 _mesa_init_driver_functions(&functions
);
1210 /* override with our functions */
1211 functions
.GetString
= get_string
;
1212 functions
.UpdateState
= osmesa_update_state
;
1213 functions
.GetBufferSize
= NULL
;
1215 if (!_mesa_initialize_context(&osmesa
->mesa
,
1217 sharelist
? &sharelist
->mesa
1218 : (GLcontext
*) NULL
,
1219 &functions
, (void *) osmesa
)) {
1220 _mesa_destroy_visual( osmesa
->gl_visual
);
1225 _mesa_enable_sw_extensions(&(osmesa
->mesa
));
1226 _mesa_enable_1_3_extensions(&(osmesa
->mesa
));
1227 _mesa_enable_1_4_extensions(&(osmesa
->mesa
));
1228 _mesa_enable_1_5_extensions(&(osmesa
->mesa
));
1230 osmesa
->gl_buffer
= _mesa_create_framebuffer(osmesa
->gl_visual
);
1231 if (!osmesa
->gl_buffer
) {
1232 _mesa_destroy_visual( osmesa
->gl_visual
);
1233 _mesa_free_context_data( &osmesa
->mesa
);
1238 /* create front color buffer in user-provided memory (no back buffer) */
1239 osmesa
->rb
= new_osmesa_renderbuffer(&osmesa
->mesa
, format
, type
);
1240 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1241 assert(osmesa
->rb
->RefCount
== 2);
1243 _mesa_add_soft_renderbuffers(osmesa
->gl_buffer
,
1244 GL_FALSE
, /* color */
1245 osmesa
->gl_visual
->haveDepthBuffer
,
1246 osmesa
->gl_visual
->haveStencilBuffer
,
1247 osmesa
->gl_visual
->haveAccumBuffer
,
1248 GL_FALSE
, /* alpha */
1249 GL_FALSE
/* aux */ );
1251 osmesa
->format
= format
;
1252 osmesa
->userRowLength
= 0;
1253 osmesa
->yup
= GL_TRUE
;
1254 osmesa
->rInd
= rind
;
1255 osmesa
->gInd
= gind
;
1256 osmesa
->bInd
= bind
;
1257 osmesa
->aInd
= aind
;
1259 /* Initialize the software rasterizer and helper modules. */
1261 GLcontext
*ctx
= &osmesa
->mesa
;
1265 if (!_swrast_CreateContext( ctx
) ||
1266 !_vbo_CreateContext( ctx
) ||
1267 !_tnl_CreateContext( ctx
) ||
1268 !_swsetup_CreateContext( ctx
)) {
1269 _mesa_destroy_visual(osmesa
->gl_visual
);
1270 _mesa_free_context_data(ctx
);
1275 _swsetup_Wakeup( ctx
);
1277 /* use default TCL pipeline */
1278 tnl
= TNL_CONTEXT(ctx
);
1279 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1281 /* Extend the software rasterizer with our optimized line and triangle
1282 * drawing functions.
1284 swrast
= SWRAST_CONTEXT( ctx
);
1285 swrast
->choose_line
= osmesa_choose_line
;
1286 swrast
->choose_triangle
= osmesa_choose_triangle
;
1294 * Destroy an Off-Screen Mesa rendering context.
1296 * \param osmesa the context to destroy
1298 GLAPI
void GLAPIENTRY
1299 OSMesaDestroyContext( OSMesaContext osmesa
)
1303 _mesa_reference_renderbuffer(&osmesa
->rb
, NULL
);
1305 _swsetup_DestroyContext( &osmesa
->mesa
);
1306 _tnl_DestroyContext( &osmesa
->mesa
);
1307 _vbo_DestroyContext( &osmesa
->mesa
);
1308 _swrast_DestroyContext( &osmesa
->mesa
);
1310 _mesa_destroy_visual( osmesa
->gl_visual
);
1311 _mesa_unreference_framebuffer( &osmesa
->gl_buffer
);
1313 _mesa_free_context_data( &osmesa
->mesa
);
1314 _mesa_free( osmesa
);
1320 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1321 * block of memory which the client provides. Its size must be at least
1322 * as large as width*height*sizeof(type). Its address should be a multiple
1323 * of 4 if using RGBA mode.
1325 * Image data is stored in the order of glDrawPixels: row-major order
1326 * with the lower-left image pixel stored in the first array position
1327 * (ie. bottom-to-top).
1329 * If the context's viewport hasn't been initialized yet, it will now be
1330 * initialized to (0,0,width,height).
1332 * Input: osmesa - the rendering context
1333 * buffer - the image buffer memory
1334 * type - data type for pixel components
1335 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1336 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1337 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
1338 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
1339 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
1340 * width, height - size of image buffer in pixels, at least 1
1341 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
1342 * invalid buffer address, invalid type, width<1, height<1,
1343 * width>internal limit or height>internal limit.
1345 GLAPI GLboolean GLAPIENTRY
1346 OSMesaMakeCurrent( OSMesaContext osmesa
, void *buffer
, GLenum type
,
1347 GLsizei width
, GLsizei height
)
1349 if (!osmesa
|| !buffer
||
1350 width
< 1 || height
< 1 ||
1351 width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
1355 if (osmesa
->format
== OSMESA_RGB_565
&& type
!= GL_UNSIGNED_SHORT_5_6_5
) {
1360 if (!(type
== GL_UNSIGNED_BYTE
||
1361 (type
== GL_UNSIGNED_SHORT
&& CHAN_BITS
>= 16) ||
1362 (type
== GL_FLOAT
&& CHAN_BITS
== 32))) {
1363 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1368 osmesa_update_state( &osmesa
->mesa
, 0 );
1370 /* Call this periodically to detect when the user has begun using
1371 * GL rendering from multiple threads.
1373 _glapi_check_multithread();
1375 /* Set renderbuffer fields. Set width/height = 0 to force
1376 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1378 osmesa
->rb
->Data
= buffer
;
1379 osmesa
->rb
->DataType
= type
;
1380 osmesa
->rb
->Width
= osmesa
->rb
->Height
= 0;
1382 /* Set the framebuffer's size. This causes the
1383 * osmesa_renderbuffer_storage() function to get called.
1385 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1386 osmesa
->gl_buffer
->Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1388 _mesa_make_current( &osmesa
->mesa
, osmesa
->gl_buffer
, osmesa
->gl_buffer
);
1390 /* Remove renderbuffer attachment, then re-add. This installs the
1391 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1393 _mesa_remove_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
);
1394 _mesa_add_renderbuffer(osmesa
->gl_buffer
, BUFFER_FRONT_LEFT
, osmesa
->rb
);
1397 /* this updates the visual's red/green/blue/alphaBits fields */
1398 _mesa_update_framebuffer_visual(osmesa
->gl_buffer
);
1400 /* update the framebuffer size */
1401 _mesa_resize_framebuffer(&osmesa
->mesa
, osmesa
->gl_buffer
, width
, height
);
1408 GLAPI OSMesaContext GLAPIENTRY
1409 OSMesaGetCurrentContext( void )
1411 GLcontext
*ctx
= _mesa_get_current_context();
1413 return (OSMesaContext
) ctx
;
1420 GLAPI
void GLAPIENTRY
1421 OSMesaPixelStore( GLint pname
, GLint value
)
1423 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1426 case OSMESA_ROW_LENGTH
:
1428 _mesa_error( &osmesa
->mesa
, GL_INVALID_VALUE
,
1429 "OSMesaPixelStore(value)" );
1432 osmesa
->userRowLength
= value
;
1435 osmesa
->yup
= value
? GL_TRUE
: GL_FALSE
;
1438 _mesa_error( &osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaPixelStore(pname)" );
1442 compute_row_addresses( osmesa
);
1446 GLAPI
void GLAPIENTRY
1447 OSMesaGetIntegerv( GLint pname
, GLint
*value
)
1449 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1453 if (osmesa
->gl_buffer
)
1454 *value
= osmesa
->gl_buffer
->Width
;
1459 if (osmesa
->gl_buffer
)
1460 *value
= osmesa
->gl_buffer
->Height
;
1465 *value
= osmesa
->format
;
1468 /* current color buffer's data type */
1470 *value
= osmesa
->rb
->DataType
;
1476 case OSMESA_ROW_LENGTH
:
1477 *value
= osmesa
->userRowLength
;
1480 *value
= osmesa
->yup
;
1482 case OSMESA_MAX_WIDTH
:
1485 case OSMESA_MAX_HEIGHT
:
1486 *value
= MAX_HEIGHT
;
1489 _mesa_error(&osmesa
->mesa
, GL_INVALID_ENUM
, "OSMesaGetIntergerv(pname)");
1496 * Return the depth buffer associated with an OSMesa context.
1497 * Input: c - the OSMesa context
1498 * Output: width, height - size of buffer in pixels
1499 * bytesPerValue - bytes per depth value (2 or 4)
1500 * buffer - pointer to depth buffer values
1501 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1503 GLAPI GLboolean GLAPIENTRY
1504 OSMesaGetDepthBuffer( OSMesaContext c
, GLint
*width
, GLint
*height
,
1505 GLint
*bytesPerValue
, void **buffer
)
1507 struct gl_renderbuffer
*rb
= NULL
;
1510 rb
= c
->gl_buffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1512 if (!rb
|| !rb
->Data
) {
1521 *height
= rb
->Height
;
1522 if (c
->gl_visual
->depthBits
<= 16)
1523 *bytesPerValue
= sizeof(GLushort
);
1525 *bytesPerValue
= sizeof(GLuint
);
1533 * Return the color buffer associated with an OSMesa context.
1534 * Input: c - the OSMesa context
1535 * Output: width, height - size of buffer in pixels
1536 * format - the pixel format (OSMESA_FORMAT)
1537 * buffer - pointer to color buffer values
1538 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1540 GLAPI GLboolean GLAPIENTRY
1541 OSMesaGetColorBuffer( OSMesaContext osmesa
, GLint
*width
,
1542 GLint
*height
, GLint
*format
, void **buffer
)
1544 if (osmesa
->rb
&& osmesa
->rb
->Data
) {
1545 *width
= osmesa
->rb
->Width
;
1546 *height
= osmesa
->rb
->Height
;
1547 *format
= osmesa
->format
;
1548 *buffer
= osmesa
->rb
->Data
;
1561 struct name_function
1564 OSMESAproc Function
;
1567 static struct name_function functions
[] = {
1568 { "OSMesaCreateContext", (OSMESAproc
) OSMesaCreateContext
},
1569 { "OSMesaCreateContextExt", (OSMESAproc
) OSMesaCreateContextExt
},
1570 { "OSMesaDestroyContext", (OSMESAproc
) OSMesaDestroyContext
},
1571 { "OSMesaMakeCurrent", (OSMESAproc
) OSMesaMakeCurrent
},
1572 { "OSMesaGetCurrentContext", (OSMESAproc
) OSMesaGetCurrentContext
},
1573 { "OSMesaPixelsStore", (OSMESAproc
) OSMesaPixelStore
},
1574 { "OSMesaGetIntegerv", (OSMESAproc
) OSMesaGetIntegerv
},
1575 { "OSMesaGetDepthBuffer", (OSMESAproc
) OSMesaGetDepthBuffer
},
1576 { "OSMesaGetColorBuffer", (OSMESAproc
) OSMesaGetColorBuffer
},
1577 { "OSMesaGetProcAddress", (OSMESAproc
) OSMesaGetProcAddress
},
1578 { "OSMesaColorClamp", (OSMESAproc
) OSMesaColorClamp
},
1583 GLAPI OSMESAproc GLAPIENTRY
1584 OSMesaGetProcAddress( const char *funcName
)
1587 for (i
= 0; functions
[i
].Name
; i
++) {
1588 if (_mesa_strcmp(functions
[i
].Name
, funcName
) == 0)
1589 return functions
[i
].Function
;
1591 return _glapi_get_proc_address(funcName
);
1595 GLAPI
void GLAPIENTRY
1596 OSMesaColorClamp(GLboolean enable
)
1598 OSMesaContext osmesa
= OSMesaGetCurrentContext();
1600 if (enable
== GL_TRUE
) {
1601 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_TRUE
;
1604 osmesa
->mesa
.Color
.ClampFragmentColor
= GL_FIXED_ONLY_ARB
;