osmesa: fix renderbuffer format selection
[mesa.git] / src / mesa / drivers / osmesa / osmesa.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /*
27 * Off-Screen Mesa rendering / Rendering into client memory space
28 *
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.
33 */
34
35
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"
51 #include "tnl/tnl.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"
56 #include "vbo/vbo.h"
57
58
59 #define OSMESA_RENDERBUFFER_CLASS 0x053
60
61
62 /**
63 * OSMesa rendering context, derived from core Mesa struct gl_context.
64 */
65 struct osmesa_context
66 {
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 */
77 };
78
79
80 static INLINE OSMesaContext
81 OSMESA_CONTEXT(struct gl_context *ctx)
82 {
83 /* Just cast, since we're using structure containment */
84 return (OSMesaContext) ctx;
85 }
86
87
88 /**********************************************************************/
89 /*** Private Device Driver Functions ***/
90 /**********************************************************************/
91
92
93 static const GLubyte *
94 get_string( struct gl_context *ctx, GLenum name )
95 {
96 (void) ctx;
97 switch (name) {
98 case GL_RENDERER:
99 #if CHAN_BITS == 32
100 return (const GLubyte *) "Mesa OffScreen32";
101 #elif CHAN_BITS == 16
102 return (const GLubyte *) "Mesa OffScreen16";
103 #else
104 return (const GLubyte *) "Mesa OffScreen";
105 #endif
106 default:
107 return NULL;
108 }
109 }
110
111
112 static void
113 osmesa_update_state( struct gl_context *ctx, GLuint new_state )
114 {
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 );
120 }
121
122
123
124 /**********************************************************************/
125 /***** Read/write spans/arrays of pixels *****/
126 /**********************************************************************/
127
128 /* 8-bit RGBA */
129 #define NAME(PREFIX) PREFIX##_RGBA8
130 #define RB_TYPE GLubyte
131 #define SPAN_VARS \
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]; \
145 DST[3] = 255
146 #define FETCH_PIXEL(DST, SRC) \
147 DST[RCOMP] = SRC[0]; \
148 DST[GCOMP] = SRC[1]; \
149 DST[BCOMP] = SRC[2]; \
150 DST[ACOMP] = SRC[3]
151 #include "swrast/s_spantemp.h"
152
153 /* 16-bit RGBA */
154 #define NAME(PREFIX) PREFIX##_RGBA16
155 #define RB_TYPE GLushort
156 #define SPAN_VARS \
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]; \
170 DST[3] = 65535
171 #define FETCH_PIXEL(DST, SRC) \
172 DST[RCOMP] = SRC[0]; \
173 DST[GCOMP] = SRC[1]; \
174 DST[BCOMP] = SRC[2]; \
175 DST[ACOMP] = SRC[3]
176 #include "swrast/s_spantemp.h"
177
178 /* 32-bit RGBA */
179 #define NAME(PREFIX) PREFIX##_RGBA32
180 #define RB_TYPE GLfloat
181 #define SPAN_VARS \
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); \
195 DST[3] = 1.0F
196 #define FETCH_PIXEL(DST, SRC) \
197 DST[RCOMP] = SRC[0]; \
198 DST[GCOMP] = SRC[1]; \
199 DST[BCOMP] = SRC[2]; \
200 DST[ACOMP] = SRC[3]
201 #include "swrast/s_spantemp.h"
202
203
204 /* 8-bit BGRA */
205 #define NAME(PREFIX) PREFIX##_BGRA8
206 #define RB_TYPE GLubyte
207 #define SPAN_VARS \
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]; \
221 DST[3] = 255
222 #define FETCH_PIXEL(DST, SRC) \
223 DST[RCOMP] = SRC[2]; \
224 DST[GCOMP] = SRC[1]; \
225 DST[BCOMP] = SRC[0]; \
226 DST[ACOMP] = SRC[3]
227 #include "swrast/s_spantemp.h"
228
229 /* 16-bit BGRA */
230 #define NAME(PREFIX) PREFIX##_BGRA16
231 #define RB_TYPE GLushort
232 #define SPAN_VARS \
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]; \
246 DST[3] = 65535
247 #define FETCH_PIXEL(DST, SRC) \
248 DST[RCOMP] = SRC[2]; \
249 DST[GCOMP] = SRC[1]; \
250 DST[BCOMP] = SRC[0]; \
251 DST[ACOMP] = SRC[3]
252 #include "swrast/s_spantemp.h"
253
254 /* 32-bit BGRA */
255 #define NAME(PREFIX) PREFIX##_BGRA32
256 #define RB_TYPE GLfloat
257 #define SPAN_VARS \
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]; \
271 DST[3] = 1.0F
272 #define FETCH_PIXEL(DST, SRC) \
273 DST[RCOMP] = SRC[2]; \
274 DST[GCOMP] = SRC[1]; \
275 DST[BCOMP] = SRC[0]; \
276 DST[ACOMP] = SRC[3]
277 #include "swrast/s_spantemp.h"
278
279
280 /* 8-bit ARGB */
281 #define NAME(PREFIX) PREFIX##_ARGB8
282 #define RB_TYPE GLubyte
283 #define SPAN_VARS \
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]; \
297 DST[0] = 255
298 #define FETCH_PIXEL(DST, SRC) \
299 DST[RCOMP] = SRC[1]; \
300 DST[GCOMP] = SRC[2]; \
301 DST[BCOMP] = SRC[3]; \
302 DST[ACOMP] = SRC[0]
303 #include "swrast/s_spantemp.h"
304
305 /* 16-bit ARGB */
306 #define NAME(PREFIX) PREFIX##_ARGB16
307 #define RB_TYPE GLushort
308 #define SPAN_VARS \
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]; \
322 DST[0] = 65535
323 #define FETCH_PIXEL(DST, SRC) \
324 DST[RCOMP] = SRC[1]; \
325 DST[GCOMP] = SRC[2]; \
326 DST[BCOMP] = SRC[3]; \
327 DST[ACOMP] = SRC[0]
328 #include "swrast/s_spantemp.h"
329
330 /* 32-bit ARGB */
331 #define NAME(PREFIX) PREFIX##_ARGB32
332 #define RB_TYPE GLfloat
333 #define SPAN_VARS \
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]; \
347 DST[0] = 1.0F
348 #define FETCH_PIXEL(DST, SRC) \
349 DST[RCOMP] = SRC[1]; \
350 DST[GCOMP] = SRC[2]; \
351 DST[BCOMP] = SRC[3]; \
352 DST[ACOMP] = SRC[0]
353 #include "swrast/s_spantemp.h"
354
355
356 /* 8-bit RGB */
357 #define NAME(PREFIX) PREFIX##_RGB8
358 #define RB_TYPE GLubyte
359 #define SPAN_VARS \
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]; \
372 DST[ACOMP] = 255
373 #include "swrast/s_spantemp.h"
374
375 /* 16-bit RGB */
376 #define NAME(PREFIX) PREFIX##_RGB16
377 #define RB_TYPE GLushort
378 #define SPAN_VARS \
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]; \
391 DST[ACOMP] = 65535U
392 #include "swrast/s_spantemp.h"
393
394 /* 32-bit RGB */
395 #define NAME(PREFIX) PREFIX##_RGB32
396 #define RB_TYPE GLfloat
397 #define SPAN_VARS \
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]; \
410 DST[ACOMP] = 1.0F
411 #include "swrast/s_spantemp.h"
412
413
414 /* 8-bit BGR */
415 #define NAME(PREFIX) PREFIX##_BGR8
416 #define RB_TYPE GLubyte
417 #define SPAN_VARS \
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]; \
430 DST[ACOMP] = 255
431 #include "swrast/s_spantemp.h"
432
433 /* 16-bit BGR */
434 #define NAME(PREFIX) PREFIX##_BGR16
435 #define RB_TYPE GLushort
436 #define SPAN_VARS \
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]; \
449 DST[ACOMP] = 65535
450 #include "swrast/s_spantemp.h"
451
452 /* 32-bit BGR */
453 #define NAME(PREFIX) PREFIX##_BGR32
454 #define RB_TYPE GLfloat
455 #define SPAN_VARS \
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]; \
468 DST[ACOMP] = 1.0F
469 #include "swrast/s_spantemp.h"
470
471
472 /* 16-bit 5/6/5 RGB */
473 #define NAME(PREFIX) PREFIX##_RGB_565
474 #define RB_TYPE GLubyte
475 #define SPAN_VARS \
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"
488
489
490 /**
491 * Macros for optimized line/triangle rendering.
492 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
493 */
494
495 #define PACK_RGBA(DST, R, G, B, A) \
496 do { \
497 (DST)[osmesa->rInd] = R; \
498 (DST)[osmesa->gInd] = G; \
499 (DST)[osmesa->bInd] = B; \
500 (DST)[osmesa->aInd] = A; \
501 } while (0)
502
503 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
504
505
506 /**
507 * Draw a flat-shaded, RGB line into an osmesa buffer.
508 */
509 #define NAME flat_rgba_line
510 #define CLIP_HACK 1
511 #define SETUP_CODE \
512 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
513 const GLchan *color = vert1->color;
514
515 #define PLOT(X, Y) \
516 do { \
517 GLchan *p = PIXELADDR4(X, Y); \
518 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
519 } while (0)
520
521 #include "swrast/s_linetemp.h"
522
523
524
525 /**
526 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
527 */
528 #define NAME flat_rgba_z_line
529 #define CLIP_HACK 1
530 #define INTERP_Z 1
531 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
532 #define SETUP_CODE \
533 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
534 const GLchan *color = vert1->color;
535
536 #define PLOT(X, Y) \
537 do { \
538 if (Z < *zPtr) { \
539 GLchan *p = PIXELADDR4(X, Y); \
540 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
541 color[BCOMP], color[ACOMP]); \
542 *zPtr = Z; \
543 } \
544 } while (0)
545
546 #include "swrast/s_linetemp.h"
547
548
549
550 /**
551 * Analyze context state to see if we can provide a fast line drawing
552 * function. Otherwise, return NULL.
553 */
554 static swrast_line_func
555 osmesa_choose_line_function( struct gl_context *ctx )
556 {
557 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
558 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
559
560 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE)
561 return NULL;
562
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;
573
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;
579 }
580
581 if (swrast->_RasterMask == 0) {
582 return (swrast_line_func) flat_rgba_line;
583 }
584
585 return (swrast_line_func) NULL;
586 }
587
588
589 /**********************************************************************/
590 /***** Optimized triangle rendering *****/
591 /**********************************************************************/
592
593
594 /*
595 * Smooth-shaded, z-less triangle, RGBA color.
596 */
597 #define NAME smooth_rgba_z_triangle
598 #define INTERP_Z 1
599 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
600 #define INTERP_RGB 1
601 #define INTERP_ALPHA 1
602 #define SETUP_CODE \
603 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
604 #define RENDER_SPAN( span ) { \
605 GLuint i; \
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); \
609 if (z < zRow[i]) { \
610 PACK_RGBA(img, FixedToChan(span.red), \
611 FixedToChan(span.green), FixedToChan(span.blue), \
612 FixedToChan(span.alpha)); \
613 zRow[i] = z; \
614 } \
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; \
620 } \
621 }
622 #include "swrast/s_tritemp.h"
623
624
625
626 /*
627 * Flat-shaded, z-less triangle, RGBA color.
628 */
629 #define NAME flat_rgba_z_triangle
630 #define INTERP_Z 1
631 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
632 #define SETUP_CODE \
633 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
634 GLuint pixel; \
635 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
636 v2->color[2], v2->color[3]);
637
638 #define RENDER_SPAN( span ) { \
639 GLuint i; \
640 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
641 for (i = 0; i < span.end; i++) { \
642 const GLuint z = FixedToDepth(span.z); \
643 if (z < zRow[i]) { \
644 img[i] = pixel; \
645 zRow[i] = z; \
646 } \
647 span.z += span.zStep; \
648 } \
649 }
650
651 #include "swrast/s_tritemp.h"
652
653
654
655 /**
656 * Return pointer to an optimized triangle function if possible.
657 */
658 static swrast_tri_func
659 osmesa_choose_triangle_function( struct gl_context *ctx )
660 {
661 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
662 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
663
664 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE)
665 return (swrast_tri_func) NULL;
666
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;
677
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;
684 }
685 else {
686 return (swrast_tri_func) flat_rgba_z_triangle;
687 }
688 }
689 return (swrast_tri_func) NULL;
690 }
691
692
693
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.
697 */
698 static void
699 osmesa_choose_triangle( struct gl_context *ctx )
700 {
701 SWcontext *swrast = SWRAST_CONTEXT(ctx);
702
703 swrast->Triangle = osmesa_choose_triangle_function( ctx );
704 if (!swrast->Triangle)
705 _swrast_choose_triangle( ctx );
706 }
707
708 static void
709 osmesa_choose_line( struct gl_context *ctx )
710 {
711 SWcontext *swrast = SWRAST_CONTEXT(ctx);
712
713 swrast->Line = osmesa_choose_line_function( ctx );
714 if (!swrast->Line)
715 _swrast_choose_line( ctx );
716 }
717
718
719
720 /**
721 * Recompute the values of the context's rowaddr array.
722 */
723 static void
724 compute_row_addresses( OSMesaContext osmesa )
725 {
726 GLint bytesPerRow, i;
727 GLubyte *origin = (GLubyte *) osmesa->rb->Data;
728 GLint rowlength; /* in pixels */
729 GLint height = osmesa->rb->Height;
730
731 if (osmesa->userRowLength)
732 rowlength = osmesa->userRowLength;
733 else
734 rowlength = osmesa->rb->Width;
735
736 bytesPerRow = rowlength * _mesa_get_format_bytes(osmesa->rb->Format);
737
738 if (osmesa->yup) {
739 /* Y=0 is bottom line of window */
740 for (i = 0; i < height; i++) {
741 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + i * bytesPerRow);
742 }
743 }
744 else {
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);
749 }
750 }
751 }
752
753
754
755 /**
756 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
757 */
758 static void
759 osmesa_delete_renderbuffer(struct gl_renderbuffer *rb)
760 {
761 free(rb);
762 }
763
764
765 /**
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.
769 */
770 static GLboolean
771 osmesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
772 GLenum internalFormat, GLuint width, GLuint height)
773 {
774 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
775
776 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
777 (void) internalFormat;
778
779 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
780 * to use.
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
783 * XXX formats.
784 * XXX The 8-bit/channel formats should all be OK.
785 */
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;
790 else
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;
796 }
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;
803 }
804 else {
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;
810 }
811 }
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;
816 else
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;
822 }
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;
830 }
831 else {
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;
838 }
839 }
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;
844 else
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;
850 }
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;
858 }
859 else {
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;
866 }
867 }
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;
875 }
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;
883 }
884 else {
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;
891 }
892 }
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;
900 }
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;
908 }
909 else {
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;
916 }
917 }
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;
925 }
926 else {
927 _mesa_problem(ctx, "bad pixel format in osmesa renderbuffer_storage");
928 }
929
930 rb->Width = width;
931 rb->Height = height;
932
933 compute_row_addresses( osmesa );
934
935 return GL_TRUE;
936 }
937
938
939 /**
940 * Allocate a new renderbuffer to describe the user-provided color buffer.
941 */
942 static struct gl_renderbuffer *
943 new_osmesa_renderbuffer(struct gl_context *ctx, GLenum format, GLenum type)
944 {
945 const GLuint name = 0;
946 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
947 if (rb) {
948 rb->RefCount = 1;
949 rb->Delete = osmesa_delete_renderbuffer;
950 rb->AllocStorage = osmesa_renderbuffer_storage;
951 rb->ClassID = OSMESA_RENDERBUFFER_CLASS;
952
953 rb->InternalFormat = GL_RGBA;
954 rb->_BaseFormat = GL_RGBA;
955 rb->DataType = type;
956 }
957 return rb;
958 }
959
960
961
962 static void
963 osmesa_MapRenderbuffer(struct gl_context *ctx,
964 struct gl_renderbuffer *rb,
965 GLuint x, GLuint y, GLuint w, GLuint h,
966 GLbitfield mode,
967 GLubyte **mapOut, GLint *rowStrideOut)
968 {
969 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
970
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 */
975
976 if (osmesa->userRowLength)
977 rowStride = osmesa->userRowLength * bpp;
978 else
979 rowStride = rb->Width * bpp;
980
981 if (!osmesa->yup) {
982 /* Y=0 is top line of window */
983 y = rb->Height - y - 1;
984 *rowStrideOut = -rowStride;
985 }
986 else {
987 *rowStrideOut = rowStride;
988 }
989
990 *mapOut = (GLubyte *) rb->Data + y * rowStride + x * bpp;
991 }
992 else {
993 _swrast_map_soft_renderbuffer(ctx, rb, x, y, w, h, mode,
994 mapOut, rowStrideOut);
995 }
996 }
997
998
999 static void
1000 osmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
1001 {
1002 if (rb->ClassID == OSMESA_RENDERBUFFER_CLASS) {
1003 /* no-op */
1004 }
1005 else {
1006 _swrast_unmap_soft_renderbuffer(ctx, rb);
1007 }
1008 }
1009
1010
1011 /**********************************************************************/
1012 /***** Public Functions *****/
1013 /**********************************************************************/
1014
1015
1016 /**
1017 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1018 * an RGBA vs Color-Index mode flag.
1019 *
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
1024 */
1025 GLAPI OSMesaContext GLAPIENTRY
1026 OSMesaCreateContext( GLenum format, OSMesaContext sharelist )
1027 {
1028 return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS,
1029 8, 0, sharelist);
1030 }
1031
1032
1033
1034 /**
1035 * New in Mesa 3.5
1036 *
1037 * Create context and specify size of ancillary buffers.
1038 */
1039 GLAPI OSMesaContext GLAPIENTRY
1040 OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
1041 GLint accumBits, OSMesaContext sharelist )
1042 {
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;
1047
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;
1054 rind = 0;
1055 gind = 1;
1056 bind = 2;
1057 aind = 3;
1058 }
1059 else if (format==OSMESA_BGRA) {
1060 redBits = CHAN_BITS;
1061 greenBits = CHAN_BITS;
1062 blueBits = CHAN_BITS;
1063 alphaBits = CHAN_BITS;
1064 bind = 0;
1065 gind = 1;
1066 rind = 2;
1067 aind = 3;
1068 }
1069 else if (format==OSMESA_ARGB) {
1070 redBits = CHAN_BITS;
1071 greenBits = CHAN_BITS;
1072 blueBits = CHAN_BITS;
1073 alphaBits = CHAN_BITS;
1074 aind = 0;
1075 rind = 1;
1076 gind = 2;
1077 bind = 3;
1078 }
1079 else if (format==OSMESA_RGB) {
1080 redBits = CHAN_BITS;
1081 greenBits = CHAN_BITS;
1082 blueBits = CHAN_BITS;
1083 alphaBits = 0;
1084 rind = 0;
1085 gind = 1;
1086 bind = 2;
1087 }
1088 else if (format==OSMESA_BGR) {
1089 redBits = CHAN_BITS;
1090 greenBits = CHAN_BITS;
1091 blueBits = CHAN_BITS;
1092 alphaBits = 0;
1093 rind = 2;
1094 gind = 1;
1095 bind = 0;
1096 }
1097 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1098 else if (format==OSMESA_RGB_565) {
1099 redBits = 5;
1100 greenBits = 6;
1101 blueBits = 5;
1102 alphaBits = 0;
1103 rind = 0; /* not used */
1104 gind = 0;
1105 bind = 0;
1106 }
1107 #endif
1108 else {
1109 return NULL;
1110 }
1111
1112 osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context);
1113 if (osmesa) {
1114 osmesa->gl_visual = _mesa_create_visual( GL_FALSE, /* double buffer */
1115 GL_FALSE, /* stereo */
1116 redBits,
1117 greenBits,
1118 blueBits,
1119 alphaBits,
1120 depthBits,
1121 stencilBits,
1122 accumBits,
1123 accumBits,
1124 accumBits,
1125 alphaBits ? accumBits : 0,
1126 1 /* num samples */
1127 );
1128 if (!osmesa->gl_visual) {
1129 free(osmesa);
1130 return NULL;
1131 }
1132
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;
1139
1140 if (!_mesa_initialize_context(&osmesa->mesa,
1141 API_OPENGL,
1142 osmesa->gl_visual,
1143 sharelist ? &sharelist->mesa
1144 : (struct gl_context *) NULL,
1145 &functions, (void *) osmesa)) {
1146 _mesa_destroy_visual( osmesa->gl_visual );
1147 free(osmesa);
1148 return NULL;
1149 }
1150
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));
1157
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 );
1162 free(osmesa);
1163 return NULL;
1164 }
1165
1166 /* Create depth/stencil/accum buffers. We'll create the color
1167 * buffer later in OSMesaMakeCurrent().
1168 */
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 */ );
1176
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;
1184
1185 _mesa_meta_init(&osmesa->mesa);
1186
1187 /* Initialize the software rasterizer and helper modules. */
1188 {
1189 struct gl_context *ctx = &osmesa->mesa;
1190 SWcontext *swrast;
1191 TNLcontext *tnl;
1192
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);
1199 free(osmesa);
1200 return NULL;
1201 }
1202
1203 _swsetup_Wakeup( ctx );
1204
1205 /* use default TCL pipeline */
1206 tnl = TNL_CONTEXT(ctx);
1207 tnl->Driver.RunPipeline = _tnl_run_pipeline;
1208
1209 ctx->Driver.MapRenderbuffer = osmesa_MapRenderbuffer;
1210 ctx->Driver.UnmapRenderbuffer = osmesa_UnmapRenderbuffer;
1211
1212 /* Extend the software rasterizer with our optimized line and triangle
1213 * drawing functions.
1214 */
1215 swrast = SWRAST_CONTEXT( ctx );
1216 swrast->choose_line = osmesa_choose_line;
1217 swrast->choose_triangle = osmesa_choose_triangle;
1218 }
1219 }
1220 return osmesa;
1221 }
1222
1223
1224 /**
1225 * Destroy an Off-Screen Mesa rendering context.
1226 *
1227 * \param osmesa the context to destroy
1228 */
1229 GLAPI void GLAPIENTRY
1230 OSMesaDestroyContext( OSMesaContext osmesa )
1231 {
1232 if (osmesa) {
1233 if (osmesa->rb)
1234 _mesa_reference_renderbuffer(&osmesa->rb, NULL);
1235
1236 _mesa_meta_free( &osmesa->mesa );
1237
1238 _swsetup_DestroyContext( &osmesa->mesa );
1239 _tnl_DestroyContext( &osmesa->mesa );
1240 _vbo_DestroyContext( &osmesa->mesa );
1241 _swrast_DestroyContext( &osmesa->mesa );
1242
1243 _mesa_destroy_visual( osmesa->gl_visual );
1244 _mesa_reference_framebuffer( &osmesa->gl_buffer, NULL );
1245
1246 _mesa_free_context_data( &osmesa->mesa );
1247 free( osmesa );
1248 }
1249 }
1250
1251
1252 /**
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.
1257 *
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).
1261 *
1262 * If the context's viewport hasn't been initialized yet, it will now be
1263 * initialized to (0,0,width,height).
1264 *
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.
1277 */
1278 GLAPI GLboolean GLAPIENTRY
1279 OSMesaMakeCurrent( OSMesaContext osmesa, void *buffer, GLenum type,
1280 GLsizei width, GLsizei height )
1281 {
1282 if (!osmesa || !buffer ||
1283 width < 1 || height < 1 ||
1284 width > MAX_WIDTH || height > MAX_HEIGHT) {
1285 return GL_FALSE;
1286 }
1287
1288 if (osmesa->format == OSMESA_RGB_565 && type != GL_UNSIGNED_SHORT_5_6_5) {
1289 return GL_FALSE;
1290 }
1291
1292 #if 0
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? */
1297 return GL_FALSE;
1298 }
1299 #endif
1300
1301 osmesa_update_state( &osmesa->mesa, 0 );
1302
1303 /* Call this periodically to detect when the user has begun using
1304 * GL rendering from multiple threads.
1305 */
1306 _glapi_check_multithread();
1307
1308
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
1315 * size.
1316 */
1317 if (!osmesa->rb) {
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);
1322 }
1323
1324 /* Set renderbuffer fields. Set width/height = 0 to force
1325 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1326 */
1327 osmesa->rb->Data = buffer;
1328 osmesa->rb->Width = osmesa->rb->Height = 0;
1329
1330 /* Set the framebuffer's size. This causes the
1331 * osmesa_renderbuffer_storage() function to get called.
1332 */
1333 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
1334 osmesa->gl_buffer->Initialized = GL_TRUE; /* XXX TEMPORARY? */
1335
1336 _mesa_make_current( &osmesa->mesa, osmesa->gl_buffer, osmesa->gl_buffer );
1337
1338 /* Remove renderbuffer attachment, then re-add. This installs the
1339 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1340 */
1341 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT);
1342 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT, osmesa->rb);
1343
1344
1345 /* this updates the visual's red/green/blue/alphaBits fields */
1346 _mesa_update_framebuffer_visual(&osmesa->mesa, osmesa->gl_buffer);
1347
1348 /* update the framebuffer size */
1349 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
1350
1351 return GL_TRUE;
1352 }
1353
1354
1355
1356 GLAPI OSMesaContext GLAPIENTRY
1357 OSMesaGetCurrentContext( void )
1358 {
1359 struct gl_context *ctx = _mesa_get_current_context();
1360 if (ctx)
1361 return (OSMesaContext) ctx;
1362 else
1363 return NULL;
1364 }
1365
1366
1367
1368 GLAPI void GLAPIENTRY
1369 OSMesaPixelStore( GLint pname, GLint value )
1370 {
1371 OSMesaContext osmesa = OSMesaGetCurrentContext();
1372
1373 switch (pname) {
1374 case OSMESA_ROW_LENGTH:
1375 if (value<0) {
1376 _mesa_error( &osmesa->mesa, GL_INVALID_VALUE,
1377 "OSMesaPixelStore(value)" );
1378 return;
1379 }
1380 osmesa->userRowLength = value;
1381 break;
1382 case OSMESA_Y_UP:
1383 osmesa->yup = value ? GL_TRUE : GL_FALSE;
1384 break;
1385 default:
1386 _mesa_error( &osmesa->mesa, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" );
1387 return;
1388 }
1389
1390 compute_row_addresses( osmesa );
1391 }
1392
1393
1394 GLAPI void GLAPIENTRY
1395 OSMesaGetIntegerv( GLint pname, GLint *value )
1396 {
1397 OSMesaContext osmesa = OSMesaGetCurrentContext();
1398
1399 switch (pname) {
1400 case OSMESA_WIDTH:
1401 if (osmesa->gl_buffer)
1402 *value = osmesa->gl_buffer->Width;
1403 else
1404 *value = 0;
1405 return;
1406 case OSMESA_HEIGHT:
1407 if (osmesa->gl_buffer)
1408 *value = osmesa->gl_buffer->Height;
1409 else
1410 *value = 0;
1411 return;
1412 case OSMESA_FORMAT:
1413 *value = osmesa->format;
1414 return;
1415 case OSMESA_TYPE:
1416 /* current color buffer's data type */
1417 if (osmesa->rb) {
1418 *value = osmesa->rb->DataType;
1419 }
1420 else {
1421 *value = 0;
1422 }
1423 return;
1424 case OSMESA_ROW_LENGTH:
1425 *value = osmesa->userRowLength;
1426 return;
1427 case OSMESA_Y_UP:
1428 *value = osmesa->yup;
1429 return;
1430 case OSMESA_MAX_WIDTH:
1431 *value = MAX_WIDTH;
1432 return;
1433 case OSMESA_MAX_HEIGHT:
1434 *value = MAX_HEIGHT;
1435 return;
1436 default:
1437 _mesa_error(&osmesa->mesa, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)");
1438 return;
1439 }
1440 }
1441
1442
1443 /**
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.
1450 */
1451 GLAPI GLboolean GLAPIENTRY
1452 OSMesaGetDepthBuffer( OSMesaContext c, GLint *width, GLint *height,
1453 GLint *bytesPerValue, void **buffer )
1454 {
1455 struct gl_renderbuffer *rb = NULL;
1456
1457 if (c->gl_buffer)
1458 rb = c->gl_buffer->Attachment[BUFFER_DEPTH].Renderbuffer;
1459
1460 if (!rb || !rb->Data) {
1461 *width = 0;
1462 *height = 0;
1463 *bytesPerValue = 0;
1464 *buffer = 0;
1465 return GL_FALSE;
1466 }
1467 else {
1468 *width = rb->Width;
1469 *height = rb->Height;
1470 if (c->gl_visual->depthBits <= 16)
1471 *bytesPerValue = sizeof(GLushort);
1472 else
1473 *bytesPerValue = sizeof(GLuint);
1474 *buffer = rb->Data;
1475 return GL_TRUE;
1476 }
1477 }
1478
1479
1480 /**
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.
1487 */
1488 GLAPI GLboolean GLAPIENTRY
1489 OSMesaGetColorBuffer( OSMesaContext osmesa, GLint *width,
1490 GLint *height, GLint *format, void **buffer )
1491 {
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;
1497 return GL_TRUE;
1498 }
1499 else {
1500 *width = 0;
1501 *height = 0;
1502 *format = 0;
1503 *buffer = 0;
1504 return GL_FALSE;
1505 }
1506 }
1507
1508
1509 struct name_function
1510 {
1511 const char *Name;
1512 OSMESAproc Function;
1513 };
1514
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 },
1527 { NULL, NULL }
1528 };
1529
1530
1531 GLAPI OSMESAproc GLAPIENTRY
1532 OSMesaGetProcAddress( const char *funcName )
1533 {
1534 int i;
1535 for (i = 0; functions[i].Name; i++) {
1536 if (strcmp(functions[i].Name, funcName) == 0)
1537 return functions[i].Function;
1538 }
1539 return _glapi_get_proc_address(funcName);
1540 }
1541
1542
1543 GLAPI void GLAPIENTRY
1544 OSMesaColorClamp(GLboolean enable)
1545 {
1546 OSMesaContext osmesa = OSMesaGetCurrentContext();
1547
1548 if (enable == GL_TRUE) {
1549 osmesa->mesa.Color.ClampFragmentColor = GL_TRUE;
1550 }
1551 else {
1552 osmesa->mesa.Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
1553 }
1554 }
1555
1556
1557 /**
1558 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1559 * libglapi.a. We need to define them here.
1560 */
1561 #ifdef GLX_INDIRECT_RENDERING
1562
1563 #define GL_GLEXT_PROTOTYPES
1564 #include "GL/gl.h"
1565 #include "glapi/glapi.h"
1566 #include "glapi/glapitable.h"
1567
1568 #if defined(USE_MGL_NAMESPACE)
1569 #define NAME(func) mgl##func
1570 #else
1571 #define NAME(func) gl##func
1572 #endif
1573
1574 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1575 GET_DISPATCH()->FUNC ARGS
1576
1577 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1578 return GET_DISPATCH()->FUNC ARGS
1579
1580 /* skip normal ones */
1581 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1582 #include "glapi/glapitemp.h"
1583
1584 #endif /* GLX_INDIRECT_RENDERING */