Merge remote-tracking branch 'origin/master' into pipe-video
[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_triangle.h"
50 #include "tnl/tnl.h"
51 #include "tnl/t_context.h"
52 #include "tnl/t_pipeline.h"
53 #include "drivers/common/driverfuncs.h"
54 #include "drivers/common/meta.h"
55 #include "vbo/vbo.h"
56
57
58
59 /**
60 * OSMesa rendering context, derived from core Mesa struct gl_context.
61 */
62 struct osmesa_context
63 {
64 struct gl_context mesa; /*< Base class - this must be first */
65 struct gl_config *gl_visual; /*< Describes the buffers */
66 struct gl_renderbuffer *rb; /*< The user's colorbuffer */
67 struct gl_framebuffer *gl_buffer; /*< The framebuffer, containing user's rb */
68 GLenum format; /*< User-specified context format */
69 GLint userRowLength; /*< user-specified number of pixels per row */
70 GLint rInd, gInd, bInd, aInd;/*< index offsets for RGBA formats */
71 GLvoid *rowaddr[MAX_HEIGHT]; /*< address of first pixel in each image row */
72 GLboolean yup; /*< TRUE -> Y increases upward */
73 /*< FALSE -> Y increases downward */
74 };
75
76
77 static INLINE OSMesaContext
78 OSMESA_CONTEXT(struct gl_context *ctx)
79 {
80 /* Just cast, since we're using structure containment */
81 return (OSMesaContext) ctx;
82 }
83
84
85 /**********************************************************************/
86 /*** Private Device Driver Functions ***/
87 /**********************************************************************/
88
89
90 static const GLubyte *
91 get_string( struct gl_context *ctx, GLenum name )
92 {
93 (void) ctx;
94 switch (name) {
95 case GL_RENDERER:
96 #if CHAN_BITS == 32
97 return (const GLubyte *) "Mesa OffScreen32";
98 #elif CHAN_BITS == 16
99 return (const GLubyte *) "Mesa OffScreen16";
100 #else
101 return (const GLubyte *) "Mesa OffScreen";
102 #endif
103 default:
104 return NULL;
105 }
106 }
107
108
109 static void
110 osmesa_update_state( struct gl_context *ctx, GLuint new_state )
111 {
112 /* easy - just propogate */
113 _swrast_InvalidateState( ctx, new_state );
114 _swsetup_InvalidateState( ctx, new_state );
115 _tnl_InvalidateState( ctx, new_state );
116 _vbo_InvalidateState( ctx, new_state );
117 }
118
119
120
121 /**********************************************************************/
122 /***** Read/write spans/arrays of pixels *****/
123 /**********************************************************************/
124
125 /* 8-bit RGBA */
126 #define NAME(PREFIX) PREFIX##_RGBA8
127 #define RB_TYPE GLubyte
128 #define SPAN_VARS \
129 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
130 #define INIT_PIXEL_PTR(P, X, Y) \
131 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
132 #define INC_PIXEL_PTR(P) P += 4
133 #define STORE_PIXEL(DST, X, Y, VALUE) \
134 DST[0] = VALUE[RCOMP]; \
135 DST[1] = VALUE[GCOMP]; \
136 DST[2] = VALUE[BCOMP]; \
137 DST[3] = VALUE[ACOMP]
138 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
139 DST[0] = VALUE[RCOMP]; \
140 DST[1] = VALUE[GCOMP]; \
141 DST[2] = VALUE[BCOMP]; \
142 DST[3] = 255
143 #define FETCH_PIXEL(DST, SRC) \
144 DST[RCOMP] = SRC[0]; \
145 DST[GCOMP] = SRC[1]; \
146 DST[BCOMP] = SRC[2]; \
147 DST[ACOMP] = SRC[3]
148 #include "swrast/s_spantemp.h"
149
150 /* 16-bit RGBA */
151 #define NAME(PREFIX) PREFIX##_RGBA16
152 #define RB_TYPE GLushort
153 #define SPAN_VARS \
154 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
155 #define INIT_PIXEL_PTR(P, X, Y) \
156 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
157 #define INC_PIXEL_PTR(P) P += 4
158 #define STORE_PIXEL(DST, X, Y, VALUE) \
159 DST[0] = VALUE[RCOMP]; \
160 DST[1] = VALUE[GCOMP]; \
161 DST[2] = VALUE[BCOMP]; \
162 DST[3] = VALUE[ACOMP]
163 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
164 DST[0] = VALUE[RCOMP]; \
165 DST[1] = VALUE[GCOMP]; \
166 DST[2] = VALUE[BCOMP]; \
167 DST[3] = 65535
168 #define FETCH_PIXEL(DST, SRC) \
169 DST[RCOMP] = SRC[0]; \
170 DST[GCOMP] = SRC[1]; \
171 DST[BCOMP] = SRC[2]; \
172 DST[ACOMP] = SRC[3]
173 #include "swrast/s_spantemp.h"
174
175 /* 32-bit RGBA */
176 #define NAME(PREFIX) PREFIX##_RGBA32
177 #define RB_TYPE GLfloat
178 #define SPAN_VARS \
179 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
180 #define INIT_PIXEL_PTR(P, X, Y) \
181 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
182 #define INC_PIXEL_PTR(P) P += 4
183 #define STORE_PIXEL(DST, X, Y, VALUE) \
184 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
185 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
186 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
187 DST[3] = CLAMP((VALUE[ACOMP]), 0.0F, 1.0F)
188 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
189 DST[0] = MAX2((VALUE[RCOMP]), 0.0F); \
190 DST[1] = MAX2((VALUE[GCOMP]), 0.0F); \
191 DST[2] = MAX2((VALUE[BCOMP]), 0.0F); \
192 DST[3] = 1.0F
193 #define FETCH_PIXEL(DST, SRC) \
194 DST[RCOMP] = SRC[0]; \
195 DST[GCOMP] = SRC[1]; \
196 DST[BCOMP] = SRC[2]; \
197 DST[ACOMP] = SRC[3]
198 #include "swrast/s_spantemp.h"
199
200
201 /* 8-bit BGRA */
202 #define NAME(PREFIX) PREFIX##_BGRA8
203 #define RB_TYPE GLubyte
204 #define SPAN_VARS \
205 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
206 #define INIT_PIXEL_PTR(P, X, Y) \
207 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
208 #define INC_PIXEL_PTR(P) P += 4
209 #define STORE_PIXEL(DST, X, Y, VALUE) \
210 DST[2] = VALUE[RCOMP]; \
211 DST[1] = VALUE[GCOMP]; \
212 DST[0] = VALUE[BCOMP]; \
213 DST[3] = VALUE[ACOMP]
214 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
215 DST[2] = VALUE[RCOMP]; \
216 DST[1] = VALUE[GCOMP]; \
217 DST[0] = VALUE[BCOMP]; \
218 DST[3] = 255
219 #define FETCH_PIXEL(DST, SRC) \
220 DST[RCOMP] = SRC[2]; \
221 DST[GCOMP] = SRC[1]; \
222 DST[BCOMP] = SRC[0]; \
223 DST[ACOMP] = SRC[3]
224 #include "swrast/s_spantemp.h"
225
226 /* 16-bit BGRA */
227 #define NAME(PREFIX) PREFIX##_BGRA16
228 #define RB_TYPE GLushort
229 #define SPAN_VARS \
230 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
231 #define INIT_PIXEL_PTR(P, X, Y) \
232 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
233 #define INC_PIXEL_PTR(P) P += 4
234 #define STORE_PIXEL(DST, X, Y, VALUE) \
235 DST[2] = VALUE[RCOMP]; \
236 DST[1] = VALUE[GCOMP]; \
237 DST[0] = VALUE[BCOMP]; \
238 DST[3] = VALUE[ACOMP]
239 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
240 DST[2] = VALUE[RCOMP]; \
241 DST[1] = VALUE[GCOMP]; \
242 DST[0] = VALUE[BCOMP]; \
243 DST[3] = 65535
244 #define FETCH_PIXEL(DST, SRC) \
245 DST[RCOMP] = SRC[2]; \
246 DST[GCOMP] = SRC[1]; \
247 DST[BCOMP] = SRC[0]; \
248 DST[ACOMP] = SRC[3]
249 #include "swrast/s_spantemp.h"
250
251 /* 32-bit BGRA */
252 #define NAME(PREFIX) PREFIX##_BGRA32
253 #define RB_TYPE GLfloat
254 #define SPAN_VARS \
255 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
256 #define INIT_PIXEL_PTR(P, X, Y) \
257 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
258 #define INC_PIXEL_PTR(P) P += 4
259 #define STORE_PIXEL(DST, X, Y, VALUE) \
260 DST[2] = VALUE[RCOMP]; \
261 DST[1] = VALUE[GCOMP]; \
262 DST[0] = VALUE[BCOMP]; \
263 DST[3] = VALUE[ACOMP]
264 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
265 DST[2] = VALUE[RCOMP]; \
266 DST[1] = VALUE[GCOMP]; \
267 DST[0] = VALUE[BCOMP]; \
268 DST[3] = 1.0F
269 #define FETCH_PIXEL(DST, SRC) \
270 DST[RCOMP] = SRC[2]; \
271 DST[GCOMP] = SRC[1]; \
272 DST[BCOMP] = SRC[0]; \
273 DST[ACOMP] = SRC[3]
274 #include "swrast/s_spantemp.h"
275
276
277 /* 8-bit ARGB */
278 #define NAME(PREFIX) PREFIX##_ARGB8
279 #define RB_TYPE GLubyte
280 #define SPAN_VARS \
281 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
282 #define INIT_PIXEL_PTR(P, X, Y) \
283 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 4 * (X)
284 #define INC_PIXEL_PTR(P) P += 4
285 #define STORE_PIXEL(DST, X, Y, VALUE) \
286 DST[1] = VALUE[RCOMP]; \
287 DST[2] = VALUE[GCOMP]; \
288 DST[3] = VALUE[BCOMP]; \
289 DST[0] = VALUE[ACOMP]
290 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
291 DST[1] = VALUE[RCOMP]; \
292 DST[2] = VALUE[GCOMP]; \
293 DST[3] = VALUE[BCOMP]; \
294 DST[0] = 255
295 #define FETCH_PIXEL(DST, SRC) \
296 DST[RCOMP] = SRC[1]; \
297 DST[GCOMP] = SRC[2]; \
298 DST[BCOMP] = SRC[3]; \
299 DST[ACOMP] = SRC[0]
300 #include "swrast/s_spantemp.h"
301
302 /* 16-bit ARGB */
303 #define NAME(PREFIX) PREFIX##_ARGB16
304 #define RB_TYPE GLushort
305 #define SPAN_VARS \
306 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
307 #define INIT_PIXEL_PTR(P, X, Y) \
308 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 4 * (X)
309 #define INC_PIXEL_PTR(P) P += 4
310 #define STORE_PIXEL(DST, X, Y, VALUE) \
311 DST[1] = VALUE[RCOMP]; \
312 DST[2] = VALUE[GCOMP]; \
313 DST[3] = VALUE[BCOMP]; \
314 DST[0] = VALUE[ACOMP]
315 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
316 DST[1] = VALUE[RCOMP]; \
317 DST[2] = VALUE[GCOMP]; \
318 DST[3] = VALUE[BCOMP]; \
319 DST[0] = 65535
320 #define FETCH_PIXEL(DST, SRC) \
321 DST[RCOMP] = SRC[1]; \
322 DST[GCOMP] = SRC[2]; \
323 DST[BCOMP] = SRC[3]; \
324 DST[ACOMP] = SRC[0]
325 #include "swrast/s_spantemp.h"
326
327 /* 32-bit ARGB */
328 #define NAME(PREFIX) PREFIX##_ARGB32
329 #define RB_TYPE GLfloat
330 #define SPAN_VARS \
331 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
332 #define INIT_PIXEL_PTR(P, X, Y) \
333 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 4 * (X)
334 #define INC_PIXEL_PTR(P) P += 4
335 #define STORE_PIXEL(DST, X, Y, VALUE) \
336 DST[1] = VALUE[RCOMP]; \
337 DST[2] = VALUE[GCOMP]; \
338 DST[3] = VALUE[BCOMP]; \
339 DST[0] = VALUE[ACOMP]
340 #define STORE_PIXEL_RGB(DST, X, Y, VALUE) \
341 DST[1] = VALUE[RCOMP]; \
342 DST[2] = VALUE[GCOMP]; \
343 DST[3] = VALUE[BCOMP]; \
344 DST[0] = 1.0F
345 #define FETCH_PIXEL(DST, SRC) \
346 DST[RCOMP] = SRC[1]; \
347 DST[GCOMP] = SRC[2]; \
348 DST[BCOMP] = SRC[3]; \
349 DST[ACOMP] = SRC[0]
350 #include "swrast/s_spantemp.h"
351
352
353 /* 8-bit RGB */
354 #define NAME(PREFIX) PREFIX##_RGB8
355 #define RB_TYPE GLubyte
356 #define SPAN_VARS \
357 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
358 #define INIT_PIXEL_PTR(P, X, Y) \
359 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
360 #define INC_PIXEL_PTR(P) P += 3
361 #define STORE_PIXEL(DST, X, Y, VALUE) \
362 DST[0] = VALUE[RCOMP]; \
363 DST[1] = VALUE[GCOMP]; \
364 DST[2] = VALUE[BCOMP]
365 #define FETCH_PIXEL(DST, SRC) \
366 DST[RCOMP] = SRC[0]; \
367 DST[GCOMP] = SRC[1]; \
368 DST[BCOMP] = SRC[2]; \
369 DST[ACOMP] = 255
370 #include "swrast/s_spantemp.h"
371
372 /* 16-bit RGB */
373 #define NAME(PREFIX) PREFIX##_RGB16
374 #define RB_TYPE GLushort
375 #define SPAN_VARS \
376 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
377 #define INIT_PIXEL_PTR(P, X, Y) \
378 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
379 #define INC_PIXEL_PTR(P) P += 3
380 #define STORE_PIXEL(DST, X, Y, VALUE) \
381 DST[0] = VALUE[RCOMP]; \
382 DST[1] = VALUE[GCOMP]; \
383 DST[2] = VALUE[BCOMP]
384 #define FETCH_PIXEL(DST, SRC) \
385 DST[RCOMP] = SRC[0]; \
386 DST[GCOMP] = SRC[1]; \
387 DST[BCOMP] = SRC[2]; \
388 DST[ACOMP] = 65535U
389 #include "swrast/s_spantemp.h"
390
391 /* 32-bit RGB */
392 #define NAME(PREFIX) PREFIX##_RGB32
393 #define RB_TYPE GLfloat
394 #define SPAN_VARS \
395 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
396 #define INIT_PIXEL_PTR(P, X, Y) \
397 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
398 #define INC_PIXEL_PTR(P) P += 3
399 #define STORE_PIXEL(DST, X, Y, VALUE) \
400 DST[0] = VALUE[RCOMP]; \
401 DST[1] = VALUE[GCOMP]; \
402 DST[2] = VALUE[BCOMP]
403 #define FETCH_PIXEL(DST, SRC) \
404 DST[RCOMP] = SRC[0]; \
405 DST[GCOMP] = SRC[1]; \
406 DST[BCOMP] = SRC[2]; \
407 DST[ACOMP] = 1.0F
408 #include "swrast/s_spantemp.h"
409
410
411 /* 8-bit BGR */
412 #define NAME(PREFIX) PREFIX##_BGR8
413 #define RB_TYPE GLubyte
414 #define SPAN_VARS \
415 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
416 #define INIT_PIXEL_PTR(P, X, Y) \
417 GLubyte *P = (GLubyte *) osmesa->rowaddr[Y] + 3 * (X)
418 #define INC_PIXEL_PTR(P) P += 3
419 #define STORE_PIXEL(DST, X, Y, VALUE) \
420 DST[2] = VALUE[RCOMP]; \
421 DST[1] = VALUE[GCOMP]; \
422 DST[0] = VALUE[BCOMP]
423 #define FETCH_PIXEL(DST, SRC) \
424 DST[RCOMP] = SRC[2]; \
425 DST[GCOMP] = SRC[1]; \
426 DST[BCOMP] = SRC[0]; \
427 DST[ACOMP] = 255
428 #include "swrast/s_spantemp.h"
429
430 /* 16-bit BGR */
431 #define NAME(PREFIX) PREFIX##_BGR16
432 #define RB_TYPE GLushort
433 #define SPAN_VARS \
434 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
435 #define INIT_PIXEL_PTR(P, X, Y) \
436 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + 3 * (X)
437 #define INC_PIXEL_PTR(P) P += 3
438 #define STORE_PIXEL(DST, X, Y, VALUE) \
439 DST[2] = VALUE[RCOMP]; \
440 DST[1] = VALUE[GCOMP]; \
441 DST[0] = VALUE[BCOMP]
442 #define FETCH_PIXEL(DST, SRC) \
443 DST[RCOMP] = SRC[2]; \
444 DST[GCOMP] = SRC[1]; \
445 DST[BCOMP] = SRC[0]; \
446 DST[ACOMP] = 65535
447 #include "swrast/s_spantemp.h"
448
449 /* 32-bit BGR */
450 #define NAME(PREFIX) PREFIX##_BGR32
451 #define RB_TYPE GLfloat
452 #define SPAN_VARS \
453 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
454 #define INIT_PIXEL_PTR(P, X, Y) \
455 GLfloat *P = (GLfloat *) osmesa->rowaddr[Y] + 3 * (X)
456 #define INC_PIXEL_PTR(P) P += 3
457 #define STORE_PIXEL(DST, X, Y, VALUE) \
458 DST[2] = VALUE[RCOMP]; \
459 DST[1] = VALUE[GCOMP]; \
460 DST[0] = VALUE[BCOMP]
461 #define FETCH_PIXEL(DST, SRC) \
462 DST[RCOMP] = SRC[2]; \
463 DST[GCOMP] = SRC[1]; \
464 DST[BCOMP] = SRC[0]; \
465 DST[ACOMP] = 1.0F
466 #include "swrast/s_spantemp.h"
467
468
469 /* 16-bit 5/6/5 RGB */
470 #define NAME(PREFIX) PREFIX##_RGB_565
471 #define RB_TYPE GLubyte
472 #define SPAN_VARS \
473 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
474 #define INIT_PIXEL_PTR(P, X, Y) \
475 GLushort *P = (GLushort *) osmesa->rowaddr[Y] + (X)
476 #define INC_PIXEL_PTR(P) P += 1
477 #define STORE_PIXEL(DST, X, Y, VALUE) \
478 *DST = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
479 #define FETCH_PIXEL(DST, SRC) \
480 DST[RCOMP] = ( (((*SRC) >> 8) & 0xf8) | (((*SRC) >> 11) & 0x7) ); \
481 DST[GCOMP] = ( (((*SRC) >> 3) & 0xfc) | (((*SRC) >> 5) & 0x3) ); \
482 DST[BCOMP] = ( (((*SRC) << 3) & 0xf8) | (((*SRC) ) & 0x7) ); \
483 DST[ACOMP] = CHAN_MAX
484 #include "swrast/s_spantemp.h"
485
486
487 /**
488 * Macros for optimized line/triangle rendering.
489 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
490 */
491
492 #define PACK_RGBA(DST, R, G, B, A) \
493 do { \
494 (DST)[osmesa->rInd] = R; \
495 (DST)[osmesa->gInd] = G; \
496 (DST)[osmesa->bInd] = B; \
497 (DST)[osmesa->aInd] = A; \
498 } while (0)
499
500 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
501
502
503 /**
504 * Draw a flat-shaded, RGB line into an osmesa buffer.
505 */
506 #define NAME flat_rgba_line
507 #define CLIP_HACK 1
508 #define SETUP_CODE \
509 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
510 const GLchan *color = vert1->color;
511
512 #define PLOT(X, Y) \
513 do { \
514 GLchan *p = PIXELADDR4(X, Y); \
515 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
516 } while (0)
517
518 #include "swrast/s_linetemp.h"
519
520
521
522 /**
523 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
524 */
525 #define NAME flat_rgba_z_line
526 #define CLIP_HACK 1
527 #define INTERP_Z 1
528 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
529 #define SETUP_CODE \
530 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
531 const GLchan *color = vert1->color;
532
533 #define PLOT(X, Y) \
534 do { \
535 if (Z < *zPtr) { \
536 GLchan *p = PIXELADDR4(X, Y); \
537 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
538 color[BCOMP], color[ACOMP]); \
539 *zPtr = Z; \
540 } \
541 } while (0)
542
543 #include "swrast/s_linetemp.h"
544
545
546
547 /**
548 * Analyze context state to see if we can provide a fast line drawing
549 * function. Otherwise, return NULL.
550 */
551 static swrast_line_func
552 osmesa_choose_line_function( struct gl_context *ctx )
553 {
554 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
555 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
556
557 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE)
558 return NULL;
559
560 if (ctx->RenderMode != GL_RENDER) return NULL;
561 if (ctx->Line.SmoothFlag) return NULL;
562 if (ctx->Texture._EnabledUnits) return NULL;
563 if (ctx->Light.ShadeModel != GL_FLAT) return NULL;
564 if (ctx->Line.Width != 1.0F) return NULL;
565 if (ctx->Line.StippleFlag) return NULL;
566 if (ctx->Line.SmoothFlag) return NULL;
567 if (osmesa->format != OSMESA_RGBA &&
568 osmesa->format != OSMESA_BGRA &&
569 osmesa->format != OSMESA_ARGB) return NULL;
570
571 if (swrast->_RasterMask==DEPTH_BIT
572 && ctx->Depth.Func==GL_LESS
573 && ctx->Depth.Mask==GL_TRUE
574 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
575 return (swrast_line_func) flat_rgba_z_line;
576 }
577
578 if (swrast->_RasterMask == 0) {
579 return (swrast_line_func) flat_rgba_line;
580 }
581
582 return (swrast_line_func) NULL;
583 }
584
585
586 /**********************************************************************/
587 /***** Optimized triangle rendering *****/
588 /**********************************************************************/
589
590
591 /*
592 * Smooth-shaded, z-less triangle, RGBA color.
593 */
594 #define NAME smooth_rgba_z_triangle
595 #define INTERP_Z 1
596 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
597 #define INTERP_RGB 1
598 #define INTERP_ALPHA 1
599 #define SETUP_CODE \
600 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
601 #define RENDER_SPAN( span ) { \
602 GLuint i; \
603 GLchan *img = PIXELADDR4(span.x, span.y); \
604 for (i = 0; i < span.end; i++, img += 4) { \
605 const GLuint z = FixedToDepth(span.z); \
606 if (z < zRow[i]) { \
607 PACK_RGBA(img, FixedToChan(span.red), \
608 FixedToChan(span.green), FixedToChan(span.blue), \
609 FixedToChan(span.alpha)); \
610 zRow[i] = z; \
611 } \
612 span.red += span.redStep; \
613 span.green += span.greenStep; \
614 span.blue += span.blueStep; \
615 span.alpha += span.alphaStep; \
616 span.z += span.zStep; \
617 } \
618 }
619 #include "swrast/s_tritemp.h"
620
621
622
623 /*
624 * Flat-shaded, z-less triangle, RGBA color.
625 */
626 #define NAME flat_rgba_z_triangle
627 #define INTERP_Z 1
628 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
629 #define SETUP_CODE \
630 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
631 GLuint pixel; \
632 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
633 v2->color[2], v2->color[3]);
634
635 #define RENDER_SPAN( span ) { \
636 GLuint i; \
637 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
638 for (i = 0; i < span.end; i++) { \
639 const GLuint z = FixedToDepth(span.z); \
640 if (z < zRow[i]) { \
641 img[i] = pixel; \
642 zRow[i] = z; \
643 } \
644 span.z += span.zStep; \
645 } \
646 }
647
648 #include "swrast/s_tritemp.h"
649
650
651
652 /**
653 * Return pointer to an optimized triangle function if possible.
654 */
655 static swrast_tri_func
656 osmesa_choose_triangle_function( struct gl_context *ctx )
657 {
658 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
659 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
660
661 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE)
662 return (swrast_tri_func) NULL;
663
664 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
665 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
666 if (ctx->Polygon.StippleFlag) return (swrast_tri_func) NULL;
667 if (ctx->Texture._EnabledUnits) return (swrast_tri_func) NULL;
668 if (osmesa->format != OSMESA_RGBA &&
669 osmesa->format != OSMESA_BGRA &&
670 osmesa->format != OSMESA_ARGB) return (swrast_tri_func) NULL;
671 if (ctx->Polygon.CullFlag &&
672 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
673 return (swrast_tri_func) NULL;
674
675 if (swrast->_RasterMask == DEPTH_BIT &&
676 ctx->Depth.Func == GL_LESS &&
677 ctx->Depth.Mask == GL_TRUE &&
678 ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
679 if (ctx->Light.ShadeModel == GL_SMOOTH) {
680 return (swrast_tri_func) smooth_rgba_z_triangle;
681 }
682 else {
683 return (swrast_tri_func) flat_rgba_z_triangle;
684 }
685 }
686 return (swrast_tri_func) NULL;
687 }
688
689
690
691 /* Override for the swrast triangle-selection function. Try to use one
692 * of our internal triangle functions, otherwise fall back to the
693 * standard swrast functions.
694 */
695 static void
696 osmesa_choose_triangle( struct gl_context *ctx )
697 {
698 SWcontext *swrast = SWRAST_CONTEXT(ctx);
699
700 swrast->Triangle = osmesa_choose_triangle_function( ctx );
701 if (!swrast->Triangle)
702 _swrast_choose_triangle( ctx );
703 }
704
705 static void
706 osmesa_choose_line( struct gl_context *ctx )
707 {
708 SWcontext *swrast = SWRAST_CONTEXT(ctx);
709
710 swrast->Line = osmesa_choose_line_function( ctx );
711 if (!swrast->Line)
712 _swrast_choose_line( ctx );
713 }
714
715
716
717 /**
718 * Recompute the values of the context's rowaddr array.
719 */
720 static void
721 compute_row_addresses( OSMesaContext osmesa )
722 {
723 GLint bytesPerPixel, bytesPerRow, i;
724 GLubyte *origin = (GLubyte *) osmesa->rb->Data;
725 GLint bpc; /* bytes per channel */
726 GLint rowlength; /* in pixels */
727 GLint height = osmesa->rb->Height;
728
729 if (osmesa->userRowLength)
730 rowlength = osmesa->userRowLength;
731 else
732 rowlength = osmesa->rb->Width;
733
734 if (osmesa->rb->DataType == GL_UNSIGNED_BYTE)
735 bpc = 1;
736 else if (osmesa->rb->DataType == GL_UNSIGNED_SHORT)
737 bpc = 2;
738 else if (osmesa->rb->DataType == GL_FLOAT)
739 bpc = 4;
740 else {
741 _mesa_problem(&osmesa->mesa,
742 "Unexpected datatype in osmesa::compute_row_addresses");
743 return;
744 }
745
746 if ((osmesa->format == OSMESA_RGB) || (osmesa->format == OSMESA_BGR)) {
747 /* RGB mode */
748 bytesPerPixel = 3 * bpc;
749 }
750 else if (osmesa->format == OSMESA_RGB_565) {
751 /* 5/6/5 RGB pixel in 16 bits */
752 bytesPerPixel = 2;
753 }
754 else {
755 /* RGBA mode */
756 bytesPerPixel = 4 * bpc;
757 }
758
759 bytesPerRow = rowlength * bytesPerPixel;
760
761 if (osmesa->yup) {
762 /* Y=0 is bottom line of window */
763 for (i = 0; i < height; i++) {
764 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + i * bytesPerRow);
765 }
766 }
767 else {
768 /* Y=0 is top line of window */
769 for (i = 0; i < height; i++) {
770 GLint j = height - i - 1;
771 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + j * bytesPerRow);
772 }
773 }
774 }
775
776
777
778 /**
779 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data.
780 */
781 static void
782 osmesa_delete_renderbuffer(struct gl_renderbuffer *rb)
783 {
784 free(rb);
785 }
786
787
788 /**
789 * Allocate renderbuffer storage. We don't actually allocate any storage
790 * since we're using a user-provided buffer.
791 * Just set up all the gl_renderbuffer methods.
792 */
793 static GLboolean
794 osmesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
795 GLenum internalFormat, GLuint width, GLuint height)
796 {
797 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
798 GLint bpc; /* bits per channel */
799
800 if (rb->DataType == GL_UNSIGNED_BYTE)
801 bpc = 8;
802 else if (rb->DataType == GL_UNSIGNED_SHORT)
803 bpc = 16;
804 else
805 bpc = 32;
806
807 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
808 (void) internalFormat;
809
810 if (osmesa->format == OSMESA_RGBA) {
811 if (rb->DataType == GL_UNSIGNED_BYTE) {
812 rb->GetRow = get_row_RGBA8;
813 rb->GetValues = get_values_RGBA8;
814 rb->PutRow = put_row_RGBA8;
815 rb->PutRowRGB = put_row_rgb_RGBA8;
816 rb->PutMonoRow = put_mono_row_RGBA8;
817 rb->PutValues = put_values_RGBA8;
818 rb->PutMonoValues = put_mono_values_RGBA8;
819 }
820 else if (rb->DataType == GL_UNSIGNED_SHORT) {
821 rb->GetRow = get_row_RGBA16;
822 rb->GetValues = get_values_RGBA16;
823 rb->PutRow = put_row_RGBA16;
824 rb->PutRowRGB = put_row_rgb_RGBA16;
825 rb->PutMonoRow = put_mono_row_RGBA16;
826 rb->PutValues = put_values_RGBA16;
827 rb->PutMonoValues = put_mono_values_RGBA16;
828 }
829 else {
830 rb->GetRow = get_row_RGBA32;
831 rb->GetValues = get_values_RGBA32;
832 rb->PutRow = put_row_RGBA32;
833 rb->PutRowRGB = put_row_rgb_RGBA32;
834 rb->PutMonoRow = put_mono_row_RGBA32;
835 rb->PutValues = put_values_RGBA32;
836 rb->PutMonoValues = put_mono_values_RGBA32;
837 }
838 }
839 else if (osmesa->format == OSMESA_BGRA) {
840 if (rb->DataType == GL_UNSIGNED_BYTE) {
841 rb->GetRow = get_row_BGRA8;
842 rb->GetValues = get_values_BGRA8;
843 rb->PutRow = put_row_BGRA8;
844 rb->PutRowRGB = put_row_rgb_BGRA8;
845 rb->PutMonoRow = put_mono_row_BGRA8;
846 rb->PutValues = put_values_BGRA8;
847 rb->PutMonoValues = put_mono_values_BGRA8;
848 }
849 else if (rb->DataType == GL_UNSIGNED_SHORT) {
850 rb->GetRow = get_row_BGRA16;
851 rb->GetValues = get_values_BGRA16;
852 rb->PutRow = put_row_BGRA16;
853 rb->PutRowRGB = put_row_rgb_BGRA16;
854 rb->PutMonoRow = put_mono_row_BGRA16;
855 rb->PutValues = put_values_BGRA16;
856 rb->PutMonoValues = put_mono_values_BGRA16;
857 }
858 else {
859 rb->GetRow = get_row_BGRA32;
860 rb->GetValues = get_values_BGRA32;
861 rb->PutRow = put_row_BGRA32;
862 rb->PutRowRGB = put_row_rgb_BGRA32;
863 rb->PutMonoRow = put_mono_row_BGRA32;
864 rb->PutValues = put_values_BGRA32;
865 rb->PutMonoValues = put_mono_values_BGRA32;
866 }
867 }
868 else if (osmesa->format == OSMESA_ARGB) {
869 if (rb->DataType == GL_UNSIGNED_BYTE) {
870 rb->GetRow = get_row_ARGB8;
871 rb->GetValues = get_values_ARGB8;
872 rb->PutRow = put_row_ARGB8;
873 rb->PutRowRGB = put_row_rgb_ARGB8;
874 rb->PutMonoRow = put_mono_row_ARGB8;
875 rb->PutValues = put_values_ARGB8;
876 rb->PutMonoValues = put_mono_values_ARGB8;
877 }
878 else if (rb->DataType == GL_UNSIGNED_SHORT) {
879 rb->GetRow = get_row_ARGB16;
880 rb->GetValues = get_values_ARGB16;
881 rb->PutRow = put_row_ARGB16;
882 rb->PutRowRGB = put_row_rgb_ARGB16;
883 rb->PutMonoRow = put_mono_row_ARGB16;
884 rb->PutValues = put_values_ARGB16;
885 rb->PutMonoValues = put_mono_values_ARGB16;
886 }
887 else {
888 rb->GetRow = get_row_ARGB32;
889 rb->GetValues = get_values_ARGB32;
890 rb->PutRow = put_row_ARGB32;
891 rb->PutRowRGB = put_row_rgb_ARGB32;
892 rb->PutMonoRow = put_mono_row_ARGB32;
893 rb->PutValues = put_values_ARGB32;
894 rb->PutMonoValues = put_mono_values_ARGB32;
895 }
896 }
897 else if (osmesa->format == OSMESA_RGB) {
898 if (rb->DataType == GL_UNSIGNED_BYTE) {
899 rb->GetRow = get_row_RGB8;
900 rb->GetValues = get_values_RGB8;
901 rb->PutRow = put_row_RGB8;
902 rb->PutRowRGB = put_row_rgb_RGB8;
903 rb->PutMonoRow = put_mono_row_RGB8;
904 rb->PutValues = put_values_RGB8;
905 rb->PutMonoValues = put_mono_values_RGB8;
906 }
907 else if (rb->DataType == GL_UNSIGNED_SHORT) {
908 rb->GetRow = get_row_RGB16;
909 rb->GetValues = get_values_RGB16;
910 rb->PutRow = put_row_RGB16;
911 rb->PutRowRGB = put_row_rgb_RGB16;
912 rb->PutMonoRow = put_mono_row_RGB16;
913 rb->PutValues = put_values_RGB16;
914 rb->PutMonoValues = put_mono_values_RGB16;
915 }
916 else {
917 rb->GetRow = get_row_RGB32;
918 rb->GetValues = get_values_RGB32;
919 rb->PutRow = put_row_RGB32;
920 rb->PutRowRGB = put_row_rgb_RGB32;
921 rb->PutMonoRow = put_mono_row_RGB32;
922 rb->PutValues = put_values_RGB32;
923 rb->PutMonoValues = put_mono_values_RGB32;
924 }
925 }
926 else if (osmesa->format == OSMESA_BGR) {
927 if (rb->DataType == GL_UNSIGNED_BYTE) {
928 rb->GetRow = get_row_BGR8;
929 rb->GetValues = get_values_BGR8;
930 rb->PutRow = put_row_BGR8;
931 rb->PutRowRGB = put_row_rgb_BGR8;
932 rb->PutMonoRow = put_mono_row_BGR8;
933 rb->PutValues = put_values_BGR8;
934 rb->PutMonoValues = put_mono_values_BGR8;
935 }
936 else if (rb->DataType == GL_UNSIGNED_SHORT) {
937 rb->GetRow = get_row_BGR16;
938 rb->GetValues = get_values_BGR16;
939 rb->PutRow = put_row_BGR16;
940 rb->PutRowRGB = put_row_rgb_BGR16;
941 rb->PutMonoRow = put_mono_row_BGR16;
942 rb->PutValues = put_values_BGR16;
943 rb->PutMonoValues = put_mono_values_BGR16;
944 }
945 else {
946 rb->GetRow = get_row_BGR32;
947 rb->GetValues = get_values_BGR32;
948 rb->PutRow = put_row_BGR32;
949 rb->PutRowRGB = put_row_rgb_BGR32;
950 rb->PutMonoRow = put_mono_row_BGR32;
951 rb->PutValues = put_values_BGR32;
952 rb->PutMonoValues = put_mono_values_BGR32;
953 }
954 }
955 else if (osmesa->format == OSMESA_RGB_565) {
956 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
957 rb->GetRow = get_row_RGB_565;
958 rb->GetValues = get_values_RGB_565;
959 rb->PutRow = put_row_RGB_565;
960 rb->PutRowRGB = put_row_rgb_RGB_565;
961 rb->PutMonoRow = put_mono_row_RGB_565;
962 rb->PutValues = put_values_RGB_565;
963 rb->PutMonoValues = put_mono_values_RGB_565;
964 }
965 else {
966 _mesa_problem(ctx, "bad pixel format in osmesa renderbuffer_storage");
967 }
968
969 rb->Width = width;
970 rb->Height = height;
971
972 compute_row_addresses( osmesa );
973
974 return GL_TRUE;
975 }
976
977
978 /**
979 * Allocate a new renderbuffer to describe the user-provided color buffer.
980 */
981 static struct gl_renderbuffer *
982 new_osmesa_renderbuffer(struct gl_context *ctx, GLenum format, GLenum type)
983 {
984 const GLuint name = 0;
985 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
986 if (rb) {
987 rb->RefCount = 1;
988 rb->Delete = osmesa_delete_renderbuffer;
989 rb->AllocStorage = osmesa_renderbuffer_storage;
990
991 rb->InternalFormat = GL_RGBA;
992 switch (type) {
993 case GL_UNSIGNED_BYTE:
994 rb->Format = MESA_FORMAT_RGBA8888;
995 break;
996 case GL_UNSIGNED_SHORT:
997 rb->Format = MESA_FORMAT_RGBA_16;
998 break;
999 case GL_FLOAT:
1000 rb->Format = MESA_FORMAT_RGBA_FLOAT32;
1001 break;
1002 default:
1003 assert(0 && "Unexpected type in new_osmesa_renderbuffer()");
1004 rb->Format = MESA_FORMAT_RGBA8888;
1005 }
1006 rb->_BaseFormat = GL_RGBA;
1007 rb->DataType = type;
1008 }
1009 return rb;
1010 }
1011
1012
1013 /**********************************************************************/
1014 /***** Public Functions *****/
1015 /**********************************************************************/
1016
1017
1018 /**
1019 * Create an Off-Screen Mesa rendering context. The only attribute needed is
1020 * an RGBA vs Color-Index mode flag.
1021 *
1022 * Input: format - Must be GL_RGBA
1023 * sharelist - specifies another OSMesaContext with which to share
1024 * display lists. NULL indicates no sharing.
1025 * Return: an OSMesaContext or 0 if error
1026 */
1027 GLAPI OSMesaContext GLAPIENTRY
1028 OSMesaCreateContext( GLenum format, OSMesaContext sharelist )
1029 {
1030 return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS,
1031 8, 0, sharelist);
1032 }
1033
1034
1035
1036 /**
1037 * New in Mesa 3.5
1038 *
1039 * Create context and specify size of ancillary buffers.
1040 */
1041 GLAPI OSMesaContext GLAPIENTRY
1042 OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
1043 GLint accumBits, OSMesaContext sharelist )
1044 {
1045 OSMesaContext osmesa;
1046 struct dd_function_table functions;
1047 GLint rind, gind, bind, aind;
1048 GLint redBits = 0, greenBits = 0, blueBits = 0, alphaBits =0;
1049
1050 rind = gind = bind = aind = 0;
1051 if (format==OSMESA_RGBA) {
1052 redBits = CHAN_BITS;
1053 greenBits = CHAN_BITS;
1054 blueBits = CHAN_BITS;
1055 alphaBits = CHAN_BITS;
1056 rind = 0;
1057 gind = 1;
1058 bind = 2;
1059 aind = 3;
1060 }
1061 else if (format==OSMESA_BGRA) {
1062 redBits = CHAN_BITS;
1063 greenBits = CHAN_BITS;
1064 blueBits = CHAN_BITS;
1065 alphaBits = CHAN_BITS;
1066 bind = 0;
1067 gind = 1;
1068 rind = 2;
1069 aind = 3;
1070 }
1071 else if (format==OSMESA_ARGB) {
1072 redBits = CHAN_BITS;
1073 greenBits = CHAN_BITS;
1074 blueBits = CHAN_BITS;
1075 alphaBits = CHAN_BITS;
1076 aind = 0;
1077 rind = 1;
1078 gind = 2;
1079 bind = 3;
1080 }
1081 else if (format==OSMESA_RGB) {
1082 redBits = CHAN_BITS;
1083 greenBits = CHAN_BITS;
1084 blueBits = CHAN_BITS;
1085 alphaBits = 0;
1086 rind = 0;
1087 gind = 1;
1088 bind = 2;
1089 }
1090 else if (format==OSMESA_BGR) {
1091 redBits = CHAN_BITS;
1092 greenBits = CHAN_BITS;
1093 blueBits = CHAN_BITS;
1094 alphaBits = 0;
1095 rind = 2;
1096 gind = 1;
1097 bind = 0;
1098 }
1099 #if CHAN_TYPE == GL_UNSIGNED_BYTE
1100 else if (format==OSMESA_RGB_565) {
1101 redBits = 5;
1102 greenBits = 6;
1103 blueBits = 5;
1104 alphaBits = 0;
1105 rind = 0; /* not used */
1106 gind = 0;
1107 bind = 0;
1108 }
1109 #endif
1110 else {
1111 return NULL;
1112 }
1113
1114 osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context);
1115 if (osmesa) {
1116 osmesa->gl_visual = _mesa_create_visual( GL_FALSE, /* double buffer */
1117 GL_FALSE, /* stereo */
1118 redBits,
1119 greenBits,
1120 blueBits,
1121 alphaBits,
1122 depthBits,
1123 stencilBits,
1124 accumBits,
1125 accumBits,
1126 accumBits,
1127 alphaBits ? accumBits : 0,
1128 1 /* num samples */
1129 );
1130 if (!osmesa->gl_visual) {
1131 free(osmesa);
1132 return NULL;
1133 }
1134
1135 /* Initialize device driver function table */
1136 _mesa_init_driver_functions(&functions);
1137 /* override with our functions */
1138 functions.GetString = get_string;
1139 functions.UpdateState = osmesa_update_state;
1140 functions.GetBufferSize = NULL;
1141
1142 if (!_mesa_initialize_context(&osmesa->mesa,
1143 API_OPENGL,
1144 osmesa->gl_visual,
1145 sharelist ? &sharelist->mesa
1146 : (struct gl_context *) NULL,
1147 &functions, (void *) osmesa)) {
1148 _mesa_destroy_visual( osmesa->gl_visual );
1149 free(osmesa);
1150 return NULL;
1151 }
1152
1153 _mesa_enable_sw_extensions(&(osmesa->mesa));
1154 _mesa_enable_1_3_extensions(&(osmesa->mesa));
1155 _mesa_enable_1_4_extensions(&(osmesa->mesa));
1156 _mesa_enable_1_5_extensions(&(osmesa->mesa));
1157 _mesa_enable_2_0_extensions(&(osmesa->mesa));
1158 _mesa_enable_2_1_extensions(&(osmesa->mesa));
1159
1160 osmesa->gl_buffer = _mesa_create_framebuffer(osmesa->gl_visual);
1161 if (!osmesa->gl_buffer) {
1162 _mesa_destroy_visual( osmesa->gl_visual );
1163 _mesa_free_context_data( &osmesa->mesa );
1164 free(osmesa);
1165 return NULL;
1166 }
1167
1168 /* Create depth/stencil/accum buffers. We'll create the color
1169 * buffer later in OSMesaMakeCurrent().
1170 */
1171 _mesa_add_soft_renderbuffers(osmesa->gl_buffer,
1172 GL_FALSE, /* color */
1173 osmesa->gl_visual->haveDepthBuffer,
1174 osmesa->gl_visual->haveStencilBuffer,
1175 osmesa->gl_visual->haveAccumBuffer,
1176 GL_FALSE, /* alpha */
1177 GL_FALSE /* aux */ );
1178
1179 osmesa->format = format;
1180 osmesa->userRowLength = 0;
1181 osmesa->yup = GL_TRUE;
1182 osmesa->rInd = rind;
1183 osmesa->gInd = gind;
1184 osmesa->bInd = bind;
1185 osmesa->aInd = aind;
1186
1187 _mesa_meta_init(&osmesa->mesa);
1188
1189 /* Initialize the software rasterizer and helper modules. */
1190 {
1191 struct gl_context *ctx = &osmesa->mesa;
1192 SWcontext *swrast;
1193 TNLcontext *tnl;
1194
1195 if (!_swrast_CreateContext( ctx ) ||
1196 !_vbo_CreateContext( ctx ) ||
1197 !_tnl_CreateContext( ctx ) ||
1198 !_swsetup_CreateContext( ctx )) {
1199 _mesa_destroy_visual(osmesa->gl_visual);
1200 _mesa_free_context_data(ctx);
1201 free(osmesa);
1202 return NULL;
1203 }
1204
1205 _swsetup_Wakeup( ctx );
1206
1207 /* use default TCL pipeline */
1208 tnl = TNL_CONTEXT(ctx);
1209 tnl->Driver.RunPipeline = _tnl_run_pipeline;
1210
1211 /* Extend the software rasterizer with our optimized line and triangle
1212 * drawing functions.
1213 */
1214 swrast = SWRAST_CONTEXT( ctx );
1215 swrast->choose_line = osmesa_choose_line;
1216 swrast->choose_triangle = osmesa_choose_triangle;
1217 }
1218 }
1219 return osmesa;
1220 }
1221
1222
1223 /**
1224 * Destroy an Off-Screen Mesa rendering context.
1225 *
1226 * \param osmesa the context to destroy
1227 */
1228 GLAPI void GLAPIENTRY
1229 OSMesaDestroyContext( OSMesaContext osmesa )
1230 {
1231 if (osmesa) {
1232 if (osmesa->rb)
1233 _mesa_reference_renderbuffer(&osmesa->rb, NULL);
1234
1235 _mesa_meta_free( &osmesa->mesa );
1236
1237 _swsetup_DestroyContext( &osmesa->mesa );
1238 _tnl_DestroyContext( &osmesa->mesa );
1239 _vbo_DestroyContext( &osmesa->mesa );
1240 _swrast_DestroyContext( &osmesa->mesa );
1241
1242 _mesa_destroy_visual( osmesa->gl_visual );
1243 _mesa_reference_framebuffer( &osmesa->gl_buffer, NULL );
1244
1245 _mesa_free_context_data( &osmesa->mesa );
1246 free( osmesa );
1247 }
1248 }
1249
1250
1251 /**
1252 * Bind an OSMesaContext to an image buffer. The image buffer is just a
1253 * block of memory which the client provides. Its size must be at least
1254 * as large as width*height*sizeof(type). Its address should be a multiple
1255 * of 4 if using RGBA mode.
1256 *
1257 * Image data is stored in the order of glDrawPixels: row-major order
1258 * with the lower-left image pixel stored in the first array position
1259 * (ie. bottom-to-top).
1260 *
1261 * If the context's viewport hasn't been initialized yet, it will now be
1262 * initialized to (0,0,width,height).
1263 *
1264 * Input: osmesa - the rendering context
1265 * buffer - the image buffer memory
1266 * type - data type for pixel components
1267 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
1268 * are supported. But if Mesa's been compiled with CHAN_BITS==16
1269 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
1270 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
1271 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
1272 * width, height - size of image buffer in pixels, at least 1
1273 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
1274 * invalid buffer address, invalid type, width<1, height<1,
1275 * width>internal limit or height>internal limit.
1276 */
1277 GLAPI GLboolean GLAPIENTRY
1278 OSMesaMakeCurrent( OSMesaContext osmesa, void *buffer, GLenum type,
1279 GLsizei width, GLsizei height )
1280 {
1281 if (!osmesa || !buffer ||
1282 width < 1 || height < 1 ||
1283 width > MAX_WIDTH || height > MAX_HEIGHT) {
1284 return GL_FALSE;
1285 }
1286
1287 if (osmesa->format == OSMESA_RGB_565 && type != GL_UNSIGNED_SHORT_5_6_5) {
1288 return GL_FALSE;
1289 }
1290
1291 #if 0
1292 if (!(type == GL_UNSIGNED_BYTE ||
1293 (type == GL_UNSIGNED_SHORT && CHAN_BITS >= 16) ||
1294 (type == GL_FLOAT && CHAN_BITS == 32))) {
1295 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
1296 return GL_FALSE;
1297 }
1298 #endif
1299
1300 osmesa_update_state( &osmesa->mesa, 0 );
1301
1302 /* Call this periodically to detect when the user has begun using
1303 * GL rendering from multiple threads.
1304 */
1305 _glapi_check_multithread();
1306
1307
1308 /* Create a front/left color buffer which wraps the user-provided buffer.
1309 * There is no back color buffer.
1310 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
1311 * doesn't match what Mesa was compiled for (CHAN_BITS) the
1312 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
1313 * that converts rendering from CHAN_BITS to the user-requested channel
1314 * size.
1315 */
1316 if (!osmesa->rb) {
1317 osmesa->rb = new_osmesa_renderbuffer(&osmesa->mesa, osmesa->format, type);
1318 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT);
1319 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT, osmesa->rb);
1320 assert(osmesa->rb->RefCount == 2);
1321 }
1322
1323 /* Set renderbuffer fields. Set width/height = 0 to force
1324 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
1325 */
1326 osmesa->rb->Data = buffer;
1327 osmesa->rb->Width = osmesa->rb->Height = 0;
1328
1329 /* Set the framebuffer's size. This causes the
1330 * osmesa_renderbuffer_storage() function to get called.
1331 */
1332 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
1333 osmesa->gl_buffer->Initialized = GL_TRUE; /* XXX TEMPORARY? */
1334
1335 _mesa_make_current( &osmesa->mesa, osmesa->gl_buffer, osmesa->gl_buffer );
1336
1337 /* Remove renderbuffer attachment, then re-add. This installs the
1338 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
1339 */
1340 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT);
1341 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT, osmesa->rb);
1342
1343
1344 /* this updates the visual's red/green/blue/alphaBits fields */
1345 _mesa_update_framebuffer_visual(&osmesa->mesa, osmesa->gl_buffer);
1346
1347 /* update the framebuffer size */
1348 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
1349
1350 return GL_TRUE;
1351 }
1352
1353
1354
1355 GLAPI OSMesaContext GLAPIENTRY
1356 OSMesaGetCurrentContext( void )
1357 {
1358 struct gl_context *ctx = _mesa_get_current_context();
1359 if (ctx)
1360 return (OSMesaContext) ctx;
1361 else
1362 return NULL;
1363 }
1364
1365
1366
1367 GLAPI void GLAPIENTRY
1368 OSMesaPixelStore( GLint pname, GLint value )
1369 {
1370 OSMesaContext osmesa = OSMesaGetCurrentContext();
1371
1372 switch (pname) {
1373 case OSMESA_ROW_LENGTH:
1374 if (value<0) {
1375 _mesa_error( &osmesa->mesa, GL_INVALID_VALUE,
1376 "OSMesaPixelStore(value)" );
1377 return;
1378 }
1379 osmesa->userRowLength = value;
1380 break;
1381 case OSMESA_Y_UP:
1382 osmesa->yup = value ? GL_TRUE : GL_FALSE;
1383 break;
1384 default:
1385 _mesa_error( &osmesa->mesa, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" );
1386 return;
1387 }
1388
1389 compute_row_addresses( osmesa );
1390 }
1391
1392
1393 GLAPI void GLAPIENTRY
1394 OSMesaGetIntegerv( GLint pname, GLint *value )
1395 {
1396 OSMesaContext osmesa = OSMesaGetCurrentContext();
1397
1398 switch (pname) {
1399 case OSMESA_WIDTH:
1400 if (osmesa->gl_buffer)
1401 *value = osmesa->gl_buffer->Width;
1402 else
1403 *value = 0;
1404 return;
1405 case OSMESA_HEIGHT:
1406 if (osmesa->gl_buffer)
1407 *value = osmesa->gl_buffer->Height;
1408 else
1409 *value = 0;
1410 return;
1411 case OSMESA_FORMAT:
1412 *value = osmesa->format;
1413 return;
1414 case OSMESA_TYPE:
1415 /* current color buffer's data type */
1416 if (osmesa->rb) {
1417 *value = osmesa->rb->DataType;
1418 }
1419 else {
1420 *value = 0;
1421 }
1422 return;
1423 case OSMESA_ROW_LENGTH:
1424 *value = osmesa->userRowLength;
1425 return;
1426 case OSMESA_Y_UP:
1427 *value = osmesa->yup;
1428 return;
1429 case OSMESA_MAX_WIDTH:
1430 *value = MAX_WIDTH;
1431 return;
1432 case OSMESA_MAX_HEIGHT:
1433 *value = MAX_HEIGHT;
1434 return;
1435 default:
1436 _mesa_error(&osmesa->mesa, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)");
1437 return;
1438 }
1439 }
1440
1441
1442 /**
1443 * Return the depth buffer associated with an OSMesa context.
1444 * Input: c - the OSMesa context
1445 * Output: width, height - size of buffer in pixels
1446 * bytesPerValue - bytes per depth value (2 or 4)
1447 * buffer - pointer to depth buffer values
1448 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1449 */
1450 GLAPI GLboolean GLAPIENTRY
1451 OSMesaGetDepthBuffer( OSMesaContext c, GLint *width, GLint *height,
1452 GLint *bytesPerValue, void **buffer )
1453 {
1454 struct gl_renderbuffer *rb = NULL;
1455
1456 if (c->gl_buffer)
1457 rb = c->gl_buffer->Attachment[BUFFER_DEPTH].Renderbuffer;
1458
1459 if (!rb || !rb->Data) {
1460 *width = 0;
1461 *height = 0;
1462 *bytesPerValue = 0;
1463 *buffer = 0;
1464 return GL_FALSE;
1465 }
1466 else {
1467 *width = rb->Width;
1468 *height = rb->Height;
1469 if (c->gl_visual->depthBits <= 16)
1470 *bytesPerValue = sizeof(GLushort);
1471 else
1472 *bytesPerValue = sizeof(GLuint);
1473 *buffer = rb->Data;
1474 return GL_TRUE;
1475 }
1476 }
1477
1478
1479 /**
1480 * Return the color buffer associated with an OSMesa context.
1481 * Input: c - the OSMesa context
1482 * Output: width, height - size of buffer in pixels
1483 * format - the pixel format (OSMESA_FORMAT)
1484 * buffer - pointer to color buffer values
1485 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1486 */
1487 GLAPI GLboolean GLAPIENTRY
1488 OSMesaGetColorBuffer( OSMesaContext osmesa, GLint *width,
1489 GLint *height, GLint *format, void **buffer )
1490 {
1491 if (osmesa->rb && osmesa->rb->Data) {
1492 *width = osmesa->rb->Width;
1493 *height = osmesa->rb->Height;
1494 *format = osmesa->format;
1495 *buffer = osmesa->rb->Data;
1496 return GL_TRUE;
1497 }
1498 else {
1499 *width = 0;
1500 *height = 0;
1501 *format = 0;
1502 *buffer = 0;
1503 return GL_FALSE;
1504 }
1505 }
1506
1507
1508 struct name_function
1509 {
1510 const char *Name;
1511 OSMESAproc Function;
1512 };
1513
1514 static struct name_function functions[] = {
1515 { "OSMesaCreateContext", (OSMESAproc) OSMesaCreateContext },
1516 { "OSMesaCreateContextExt", (OSMESAproc) OSMesaCreateContextExt },
1517 { "OSMesaDestroyContext", (OSMESAproc) OSMesaDestroyContext },
1518 { "OSMesaMakeCurrent", (OSMESAproc) OSMesaMakeCurrent },
1519 { "OSMesaGetCurrentContext", (OSMESAproc) OSMesaGetCurrentContext },
1520 { "OSMesaPixelsStore", (OSMESAproc) OSMesaPixelStore },
1521 { "OSMesaGetIntegerv", (OSMESAproc) OSMesaGetIntegerv },
1522 { "OSMesaGetDepthBuffer", (OSMESAproc) OSMesaGetDepthBuffer },
1523 { "OSMesaGetColorBuffer", (OSMESAproc) OSMesaGetColorBuffer },
1524 { "OSMesaGetProcAddress", (OSMESAproc) OSMesaGetProcAddress },
1525 { "OSMesaColorClamp", (OSMESAproc) OSMesaColorClamp },
1526 { NULL, NULL }
1527 };
1528
1529
1530 GLAPI OSMESAproc GLAPIENTRY
1531 OSMesaGetProcAddress( const char *funcName )
1532 {
1533 int i;
1534 for (i = 0; functions[i].Name; i++) {
1535 if (strcmp(functions[i].Name, funcName) == 0)
1536 return functions[i].Function;
1537 }
1538 return _glapi_get_proc_address(funcName);
1539 }
1540
1541
1542 GLAPI void GLAPIENTRY
1543 OSMesaColorClamp(GLboolean enable)
1544 {
1545 OSMesaContext osmesa = OSMesaGetCurrentContext();
1546
1547 if (enable == GL_TRUE) {
1548 osmesa->mesa.Color.ClampFragmentColor = GL_TRUE;
1549 }
1550 else {
1551 osmesa->mesa.Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
1552 }
1553 }
1554
1555
1556 /**
1557 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1558 * libglapi.a. We need to define them here.
1559 */
1560 #ifdef GLX_INDIRECT_RENDERING
1561
1562 #define GL_GLEXT_PROTOTYPES
1563 #include "GL/gl.h"
1564 #include "glapi/glapi.h"
1565 #include "glapi/glapitable.h"
1566
1567 #if defined(USE_MGL_NAMESPACE)
1568 #define NAME(func) mgl##func
1569 #else
1570 #define NAME(func) gl##func
1571 #endif
1572
1573 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1574 GET_DISPATCH()->FUNC ARGS
1575
1576 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1577 return GET_DISPATCH()->FUNC ARGS
1578
1579 /* skip normal ones */
1580 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1581 #include "glapi/glapitemp.h"
1582
1583 #endif /* GLX_INDIRECT_RENDERING */