use _glapi_get_context() instead of GET_CURRENT_CONTEXT(ctx); in buffer_size()
[mesa.git] / src / mesa / drivers / osmesa / osmesa.c
1 /* $Id: osmesa.c,v 1.76 2002/03/29 17:20:48 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * Off-Screen Mesa rendering / Rendering into client memory space
30 *
31 * Note on thread safety: this driver is thread safe. All
32 * functions are reentrant. The notion of current context is
33 * managed by the core _mesa_make_current() and _mesa_get_current_context()
34 * functions. Those functions are thread-safe.
35 */
36
37
38 #include "glheader.h"
39 #include "GL/osmesa.h"
40 #include "context.h"
41 #include "colormac.h"
42 #include "depth.h"
43 #include "extensions.h"
44 #include "macros.h"
45 #include "matrix.h"
46 #include "mem.h"
47 #include "mmath.h"
48 #include "mtypes.h"
49 #include "texformat.h"
50 #include "texstore.h"
51 #include "array_cache/acache.h"
52 #include "swrast/swrast.h"
53 #include "swrast_setup/swrast_setup.h"
54 #include "swrast/s_context.h"
55 #include "swrast/s_depth.h"
56 #include "swrast/s_lines.h"
57 #include "swrast/s_triangle.h"
58 #include "tnl/tnl.h"
59 #include "tnl/t_context.h"
60 #include "tnl/t_pipeline.h"
61
62
63
64 /*
65 * This is the OS/Mesa context struct.
66 * Notice how it includes a GLcontext. By doing this we're mimicking
67 * C++ inheritance/derivation.
68 * Later, we can cast a GLcontext pointer into an OSMesaContext pointer
69 * or vice versa.
70 */
71 struct osmesa_context {
72 GLcontext gl_ctx; /* The core GL/Mesa context */
73 GLvisual *gl_visual; /* Describes the buffers */
74 GLframebuffer *gl_buffer; /* Depth, stencil, accum, etc buffers */
75 GLenum format; /* either GL_RGBA or GL_COLOR_INDEX */
76 void *buffer; /* the image buffer */
77 GLint width, height; /* size of image buffer */
78 GLint rowlength; /* number of pixels per row */
79 GLint userRowLength; /* user-specified number of pixels per row */
80 GLint rshift, gshift; /* bit shifts for RGBA formats */
81 GLint bshift, ashift;
82 GLint rInd, gInd, bInd, aInd;/* index offsets for RGBA formats */
83 GLchan *rowaddr[MAX_HEIGHT]; /* address of first pixel in each image row */
84 GLboolean yup; /* TRUE -> Y increases upward */
85 /* FALSE -> Y increases downward */
86 };
87
88
89
90 /* A forward declaration: */
91 static void osmesa_update_state( GLcontext *ctx, GLuint newstate );
92 static void osmesa_register_swrast_functions( GLcontext *ctx );
93
94
95
96 #define OSMESA_CONTEXT(ctx) ((OSMesaContext) (ctx->DriverCtx))
97
98
99
100 /**********************************************************************/
101 /***** Public Functions *****/
102 /**********************************************************************/
103
104
105 /*
106 * Create an Off-Screen Mesa rendering context. The only attribute needed is
107 * an RGBA vs Color-Index mode flag.
108 *
109 * Input: format - either GL_RGBA or GL_COLOR_INDEX
110 * sharelist - specifies another OSMesaContext with which to share
111 * display lists. NULL indicates no sharing.
112 * Return: an OSMesaContext or 0 if error
113 */
114 GLAPI OSMesaContext GLAPIENTRY
115 OSMesaCreateContext( GLenum format, OSMesaContext sharelist )
116 {
117 return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS,
118 8, 16, sharelist);
119 }
120
121
122
123 /*
124 * New in Mesa 3.5
125 *
126 * Create context and specify size of ancillary buffers.
127 */
128 GLAPI OSMesaContext GLAPIENTRY
129 OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
130 GLint accumBits, OSMesaContext sharelist )
131 {
132 OSMesaContext osmesa;
133 GLint rshift, gshift, bshift, ashift;
134 GLint rind, gind, bind, aind;
135 GLint indexBits = 0, redBits = 0, greenBits = 0, blueBits = 0, alphaBits =0;
136 GLboolean rgbmode;
137 const GLuint i4 = 1;
138 const GLubyte *i1 = (GLubyte *) &i4;
139 const GLint little_endian = *i1;
140
141 rind = gind = bind = aind = 0;
142 if (format==OSMESA_COLOR_INDEX) {
143 indexBits = 8;
144 rshift = gshift = bshift = ashift = 0;
145 rgbmode = GL_FALSE;
146 }
147 else if (format==OSMESA_RGBA) {
148 indexBits = 0;
149 redBits = CHAN_BITS;
150 greenBits = CHAN_BITS;
151 blueBits = CHAN_BITS;
152 alphaBits = CHAN_BITS;
153 rind = 0;
154 gind = 1;
155 bind = 2;
156 aind = 3;
157 if (little_endian) {
158 rshift = 0;
159 gshift = 8;
160 bshift = 16;
161 ashift = 24;
162 }
163 else {
164 rshift = 24;
165 gshift = 16;
166 bshift = 8;
167 ashift = 0;
168 }
169 rgbmode = GL_TRUE;
170 }
171 else if (format==OSMESA_BGRA) {
172 indexBits = 0;
173 redBits = CHAN_BITS;
174 greenBits = CHAN_BITS;
175 blueBits = CHAN_BITS;
176 alphaBits = CHAN_BITS;
177 bind = 0;
178 gind = 1;
179 rind = 2;
180 aind = 3;
181 if (little_endian) {
182 bshift = 0;
183 gshift = 8;
184 rshift = 16;
185 ashift = 24;
186 }
187 else {
188 bshift = 24;
189 gshift = 16;
190 rshift = 8;
191 ashift = 0;
192 }
193 rgbmode = GL_TRUE;
194 }
195 else if (format==OSMESA_ARGB) {
196 indexBits = 0;
197 redBits = CHAN_BITS;
198 greenBits = CHAN_BITS;
199 blueBits = CHAN_BITS;
200 alphaBits = CHAN_BITS;
201 aind = 0;
202 rind = 1;
203 gind = 2;
204 bind = 3;
205 if (little_endian) {
206 ashift = 0;
207 rshift = 8;
208 gshift = 16;
209 bshift = 24;
210 }
211 else {
212 ashift = 24;
213 rshift = 16;
214 gshift = 8;
215 bshift = 0;
216 }
217 rgbmode = GL_TRUE;
218 }
219 else if (format==OSMESA_RGB) {
220 indexBits = 0;
221 redBits = CHAN_BITS;
222 greenBits = CHAN_BITS;
223 blueBits = CHAN_BITS;
224 alphaBits = 0;
225 bshift = 0;
226 gshift = 8;
227 rshift = 16;
228 ashift = 24;
229 rind = 0;
230 gind = 1;
231 bind = 2;
232 rgbmode = GL_TRUE;
233 }
234 else if (format==OSMESA_BGR) {
235 indexBits = 0;
236 redBits = CHAN_BITS;
237 greenBits = CHAN_BITS;
238 blueBits = CHAN_BITS;
239 alphaBits = 0;
240 bshift = 0;
241 gshift = 8;
242 rshift = 16;
243 ashift = 24;
244 rind = 2;
245 gind = 1;
246 bind = 0;
247 rgbmode = GL_TRUE;
248 }
249 else if (format==OSMESA_RGB_565) {
250 indexBits = 0;
251 redBits = 5;
252 greenBits = 6;
253 blueBits = 5;
254 alphaBits = 0;
255 rshift = 11;
256 gshift = 5;
257 bshift = 0;
258 ashift = 0;
259 rind = 0; /* not used */
260 gind = 0;
261 bind = 0;
262 rgbmode = GL_TRUE;
263 }
264 else {
265 return NULL;
266 }
267
268
269 osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context);
270 if (osmesa) {
271 osmesa->gl_visual = _mesa_create_visual( rgbmode,
272 GL_FALSE, /* double buffer */
273 GL_FALSE, /* stereo */
274 redBits,
275 greenBits,
276 blueBits,
277 alphaBits,
278 indexBits,
279 depthBits,
280 stencilBits,
281 accumBits,
282 accumBits,
283 accumBits,
284 alphaBits ? accumBits : 0,
285 1 /* num samples */
286 );
287 if (!osmesa->gl_visual) {
288 FREE(osmesa);
289 return NULL;
290 }
291
292 if (!_mesa_initialize_context(&osmesa->gl_ctx,
293 osmesa->gl_visual,
294 sharelist ? &sharelist->gl_ctx
295 : (GLcontext *) NULL,
296 (void *) osmesa, GL_TRUE )) {
297 _mesa_destroy_visual( osmesa->gl_visual );
298 FREE(osmesa);
299 return NULL;
300 }
301
302 _mesa_enable_sw_extensions(&(osmesa->gl_ctx));
303 _mesa_enable_1_3_extensions(&(osmesa->gl_ctx));
304
305 osmesa->gl_buffer = _mesa_create_framebuffer( osmesa->gl_visual,
306 (GLboolean) ( osmesa->gl_visual->depthBits > 0 ),
307 (GLboolean) ( osmesa->gl_visual->stencilBits > 0 ),
308 (GLboolean) ( osmesa->gl_visual->accumRedBits > 0 ),
309 GL_FALSE /* s/w alpha */ );
310
311 if (!osmesa->gl_buffer) {
312 _mesa_destroy_visual( osmesa->gl_visual );
313 _mesa_free_context_data( &osmesa->gl_ctx );
314 FREE(osmesa);
315 return NULL;
316 }
317 osmesa->format = format;
318 osmesa->buffer = NULL;
319 osmesa->width = 0;
320 osmesa->height = 0;
321 osmesa->userRowLength = 0;
322 osmesa->rowlength = 0;
323 osmesa->yup = GL_TRUE;
324 osmesa->rshift = rshift;
325 osmesa->gshift = gshift;
326 osmesa->bshift = bshift;
327 osmesa->ashift = ashift;
328 osmesa->rInd = rind;
329 osmesa->gInd = gind;
330 osmesa->bInd = bind;
331 osmesa->aInd = aind;
332
333
334 /* Initialize the software rasterizer and helper modules.
335 */
336 {
337 GLcontext *ctx = &osmesa->gl_ctx;
338
339 _swrast_CreateContext( ctx );
340 _ac_CreateContext( ctx );
341 _tnl_CreateContext( ctx );
342 _swsetup_CreateContext( ctx );
343
344 _swsetup_Wakeup( ctx );
345 osmesa_register_swrast_functions( ctx );
346 }
347 }
348 return osmesa;
349 }
350
351
352
353
354 /*
355 * Destroy an Off-Screen Mesa rendering context.
356 *
357 * Input: ctx - the context to destroy
358 */
359 GLAPI void GLAPIENTRY OSMesaDestroyContext( OSMesaContext ctx )
360 {
361 if (ctx) {
362 _swsetup_DestroyContext( &ctx->gl_ctx );
363 _tnl_DestroyContext( &ctx->gl_ctx );
364 _ac_DestroyContext( &ctx->gl_ctx );
365 _swrast_DestroyContext( &ctx->gl_ctx );
366
367 _mesa_destroy_visual( ctx->gl_visual );
368 _mesa_destroy_framebuffer( ctx->gl_buffer );
369 _mesa_free_context_data( &ctx->gl_ctx );
370 FREE( ctx );
371 }
372 }
373
374
375
376 /*
377 * Recompute the values of the context's rowaddr array.
378 */
379 static void compute_row_addresses( OSMesaContext ctx )
380 {
381 GLint bytesPerPixel, bytesPerRow, i;
382 GLubyte *origin = (GLubyte *) ctx->buffer;
383
384 if (ctx->format == OSMESA_COLOR_INDEX) {
385 /* CI mode */
386 bytesPerPixel = 1 * sizeof(GLchan);
387 }
388 else if ((ctx->format == OSMESA_RGB) || (ctx->format == OSMESA_BGR)) {
389 /* RGB mode */
390 bytesPerPixel = 3 * sizeof(GLchan);
391 }
392 else if (ctx->format == OSMESA_RGB_565) {
393 /* 5/6/5 RGB pixel in 16 bits */
394 bytesPerPixel = 2;
395 }
396 else {
397 /* RGBA mode */
398 bytesPerPixel = 4 * sizeof(GLchan);
399 }
400
401 bytesPerRow = ctx->rowlength * bytesPerPixel;
402
403 if (ctx->yup) {
404 /* Y=0 is bottom line of window */
405 for (i = 0; i < MAX_HEIGHT; i++) {
406 ctx->rowaddr[i] = (GLchan *) ((GLubyte *) origin + i * bytesPerRow);
407 }
408 }
409 else {
410 /* Y=0 is top line of window */
411 for (i = 0; i < MAX_HEIGHT; i++) {
412 GLint j = ctx->height - i - 1;
413 ctx->rowaddr[i] = (GLchan *) ((GLubyte *) origin + j * bytesPerRow);
414 }
415 }
416 }
417
418
419 /*
420 * Bind an OSMesaContext to an image buffer. The image buffer is just a
421 * block of memory which the client provides. Its size must be at least
422 * as large as width*height*sizeof(type). Its address should be a multiple
423 * of 4 if using RGBA mode.
424 *
425 * Image data is stored in the order of glDrawPixels: row-major order
426 * with the lower-left image pixel stored in the first array position
427 * (ie. bottom-to-top).
428 *
429 * If the context's viewport hasn't been initialized yet, it will now be
430 * initialized to (0,0,width,height).
431 *
432 * Input: ctx - the rendering context
433 * buffer - the image buffer memory
434 * type - data type for pixel components
435 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
436 * are supported. But if Mesa's been compiled with CHAN_BITS==16
437 * then type must be GL_UNSIGNED_SHORT. And if Mesa's been build
438 * with CHAN_BITS==32 then type must be GL_FLOAT.
439 * width, height - size of image buffer in pixels, at least 1
440 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
441 * invalid buffer address, invalid type, width<1, height<1,
442 * width>internal limit or height>internal limit.
443 */
444 GLAPI GLboolean GLAPIENTRY
445 OSMesaMakeCurrent( OSMesaContext ctx, void *buffer, GLenum type,
446 GLsizei width, GLsizei height )
447 {
448 if (!ctx || !buffer ||
449 width < 1 || height < 1 ||
450 width > MAX_WIDTH || height > MAX_HEIGHT) {
451 return GL_FALSE;
452 }
453
454 if (ctx->format == OSMESA_RGB_565) {
455 if (type != GL_UNSIGNED_SHORT_5_6_5)
456 return GL_FALSE;
457 }
458 else if (type != CHAN_TYPE) {
459 return GL_FALSE;
460 }
461
462 osmesa_update_state( &ctx->gl_ctx, 0 );
463 _mesa_make_current( &ctx->gl_ctx, ctx->gl_buffer );
464
465 ctx->buffer = buffer;
466 ctx->width = width;
467 ctx->height = height;
468 if (ctx->userRowLength)
469 ctx->rowlength = ctx->userRowLength;
470 else
471 ctx->rowlength = width;
472
473 compute_row_addresses( ctx );
474
475 /* init viewport */
476 if (ctx->gl_ctx.Viewport.Width==0) {
477 /* initialize viewport and scissor box to buffer size */
478 _mesa_Viewport( 0, 0, width, height );
479 ctx->gl_ctx.Scissor.Width = width;
480 ctx->gl_ctx.Scissor.Height = height;
481 }
482
483 return GL_TRUE;
484 }
485
486
487
488 GLAPI OSMesaContext GLAPIENTRY OSMesaGetCurrentContext( void )
489 {
490 GLcontext *ctx = _mesa_get_current_context();
491 if (ctx)
492 return (OSMesaContext) ctx;
493 else
494 return NULL;
495 }
496
497
498
499 GLAPI void GLAPIENTRY OSMesaPixelStore( GLint pname, GLint value )
500 {
501 OSMesaContext ctx = OSMesaGetCurrentContext();
502
503 switch (pname) {
504 case OSMESA_ROW_LENGTH:
505 if (value<0) {
506 _mesa_error( &ctx->gl_ctx, GL_INVALID_VALUE,
507 "OSMesaPixelStore(value)" );
508 return;
509 }
510 ctx->userRowLength = value;
511 ctx->rowlength = value;
512 break;
513 case OSMESA_Y_UP:
514 ctx->yup = value ? GL_TRUE : GL_FALSE;
515 break;
516 default:
517 _mesa_error( &ctx->gl_ctx, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" );
518 return;
519 }
520
521 compute_row_addresses( ctx );
522 }
523
524
525 GLAPI void GLAPIENTRY OSMesaGetIntegerv( GLint pname, GLint *value )
526 {
527 OSMesaContext ctx = OSMesaGetCurrentContext();
528
529 switch (pname) {
530 case OSMESA_WIDTH:
531 *value = ctx->width;
532 return;
533 case OSMESA_HEIGHT:
534 *value = ctx->height;
535 return;
536 case OSMESA_FORMAT:
537 *value = ctx->format;
538 return;
539 case OSMESA_TYPE:
540 *value = CHAN_TYPE;
541 return;
542 case OSMESA_ROW_LENGTH:
543 *value = ctx->rowlength;
544 return;
545 case OSMESA_Y_UP:
546 *value = ctx->yup;
547 return;
548 case OSMESA_MAX_WIDTH:
549 *value = MAX_WIDTH;
550 return;
551 case OSMESA_MAX_HEIGHT:
552 *value = MAX_HEIGHT;
553 return;
554 default:
555 _mesa_error(&ctx->gl_ctx, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)");
556 return;
557 }
558 }
559
560 /*
561 * Return the depth buffer associated with an OSMesa context.
562 * Input: c - the OSMesa context
563 * Output: width, height - size of buffer in pixels
564 * bytesPerValue - bytes per depth value (2 or 4)
565 * buffer - pointer to depth buffer values
566 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
567 */
568 GLAPI GLboolean GLAPIENTRY
569 OSMesaGetDepthBuffer( OSMesaContext c, GLint *width, GLint *height,
570 GLint *bytesPerValue, void **buffer )
571 {
572 if ((!c->gl_buffer) || (!c->gl_buffer->DepthBuffer)) {
573 *width = 0;
574 *height = 0;
575 *bytesPerValue = 0;
576 *buffer = 0;
577 return GL_FALSE;
578 }
579 else {
580 *width = c->gl_buffer->Width;
581 *height = c->gl_buffer->Height;
582 if (c->gl_visual->depthBits <= 16)
583 *bytesPerValue = sizeof(GLushort);
584 else
585 *bytesPerValue = sizeof(GLuint);
586 *buffer = c->gl_buffer->DepthBuffer;
587 return GL_TRUE;
588 }
589 }
590
591 /*
592 * Return the color buffer associated with an OSMesa context.
593 * Input: c - the OSMesa context
594 * Output: width, height - size of buffer in pixels
595 * format - the pixel format (OSMESA_FORMAT)
596 * buffer - pointer to color buffer values
597 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
598 */
599 GLAPI GLboolean GLAPIENTRY
600 OSMesaGetColorBuffer( OSMesaContext c, GLint *width,
601 GLint *height, GLint *format, void **buffer )
602 {
603 if (!c->buffer) {
604 *width = 0;
605 *height = 0;
606 *format = 0;
607 *buffer = 0;
608 return GL_FALSE;
609 }
610 else {
611 *width = c->width;
612 *height = c->height;
613 *format = c->format;
614 *buffer = c->buffer;
615 return GL_TRUE;
616 }
617 }
618
619 /**********************************************************************/
620 /*** Device Driver Functions ***/
621 /**********************************************************************/
622
623
624 /*
625 * Useful macros:
626 */
627
628 #define PACK_RGBA(DST, R, G, B, A) \
629 do { \
630 (DST)[osmesa->rInd] = R; \
631 (DST)[osmesa->gInd] = G; \
632 (DST)[osmesa->bInd] = B; \
633 (DST)[osmesa->aInd] = A; \
634 } while (0)
635
636 #define PACK_RGB(DST, R, G, B) \
637 do { \
638 (DST)[0] = R; \
639 (DST)[1] = G; \
640 (DST)[2] = B; \
641 } while (0)
642
643 #define PACK_BGR(DST, R, G, B) \
644 do { \
645 (DST)[0] = B; \
646 (DST)[1] = G; \
647 (DST)[2] = R; \
648 } while (0)
649
650 #define PACK_RGB_565(DST, R, G, B) \
651 do { \
652 (DST) = (((int) (R) << 8) & 0xf800) | (((int) (G) << 3) & 0x7e0) | ((int) (B) >> 3);\
653 } while (0)
654
655
656 #define UNPACK_RED(P) ( (P)[osmesa->rInd] )
657 #define UNPACK_GREEN(P) ( (P)[osmesa->gInd] )
658 #define UNPACK_BLUE(P) ( (P)[osmesa->bInd] )
659 #define UNPACK_ALPHA(P) ( (P)[osmesa->aInd] )
660
661
662 #define PIXELADDR1(X,Y) (osmesa->rowaddr[Y] + (X))
663 #define PIXELADDR2(X,Y) (osmesa->rowaddr[Y] + 2 * (X))
664 #define PIXELADDR3(X,Y) (osmesa->rowaddr[Y] + 3 * (X))
665 #define PIXELADDR4(X,Y) (osmesa->rowaddr[Y] + 4 * (X))
666
667
668
669 static GLboolean set_draw_buffer( GLcontext *ctx, GLenum mode )
670 {
671 (void) ctx;
672 if (mode==GL_FRONT_LEFT) {
673 return GL_TRUE;
674 }
675 else {
676 return GL_FALSE;
677 }
678 }
679
680
681 static void set_read_buffer( GLcontext *ctx, GLframebuffer *buffer, GLenum mode )
682 {
683 /* separate read buffer not supported */
684 ASSERT(buffer == ctx->DrawBuffer);
685 ASSERT(mode == GL_FRONT_LEFT);
686 }
687
688
689 static void clear( GLcontext *ctx, GLbitfield mask, GLboolean all,
690 GLint x, GLint y, GLint width, GLint height )
691 {
692 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
693 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
694
695 /* sanity check - we only have a front-left buffer */
696 ASSERT((mask & (DD_FRONT_RIGHT_BIT | DD_BACK_LEFT_BIT | DD_BACK_RIGHT_BIT)) == 0);
697 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
698 if (mask & DD_FRONT_LEFT_BIT) {
699 if (osmesa->format == OSMESA_COLOR_INDEX) {
700 if (all) {
701 /* Clear whole CI buffer */
702 #if CHAN_TYPE == GL_UNSIGNED_BYTE
703 MEMSET(osmesa->buffer, ctx->Color.ClearIndex,
704 osmesa->rowlength * osmesa->height);
705 #else
706 const GLint n = osmesa->rowlength * osmesa->height;
707 GLchan *buffer = (GLchan *) osmesa->buffer;
708 GLint i;
709 for (i = 0; i < n; i ++) {
710 buffer[i] = ctx->Color.ClearIndex;
711 }
712 #endif
713 }
714 else {
715 /* Clear part of CI buffer */
716 const GLchan clearIndex = (GLchan) ctx->Color.ClearIndex;
717 GLint i, j;
718 for (i = 0; i < height; i++) {
719 GLchan *ptr1 = PIXELADDR1(x, (y + i));
720 for (j = 0; j < width; j++) {
721 *ptr1++ = clearIndex;
722 }
723 }
724 }
725 }
726 else if (osmesa->format == OSMESA_RGB) {
727 const GLchan r = ctx->Color.ClearColor[0];
728 const GLchan g = ctx->Color.ClearColor[1];
729 const GLchan b = ctx->Color.ClearColor[2];
730 if (all) {
731 /* Clear whole RGB buffer */
732 GLuint n = osmesa->rowlength * osmesa->height;
733 GLchan *ptr3 = (GLchan *) osmesa->buffer;
734 GLuint i;
735 for (i = 0; i < n; i++) {
736 PACK_RGB(ptr3, r, g, b);
737 ptr3 += 3;
738 }
739 }
740 else {
741 /* Clear part of RGB buffer */
742 GLint i, j;
743 for (i = 0; i < height; i++) {
744 GLchan *ptr3 = PIXELADDR3(x, (y + i));
745 for (j = 0; j < width; j++) {
746 PACK_RGB(ptr3, r, g, b);
747 ptr3 += 3;
748 }
749 }
750 }
751 }
752 else if (osmesa->format == OSMESA_BGR) {
753 const GLchan r = ctx->Color.ClearColor[0];
754 const GLchan g = ctx->Color.ClearColor[1];
755 const GLchan b = ctx->Color.ClearColor[2];
756 if (all) {
757 /* Clear whole RGB buffer */
758 const GLint n = osmesa->rowlength * osmesa->height;
759 GLchan *ptr3 = (GLchan *) osmesa->buffer;
760 GLint i;
761 for (i = 0; i < n; i++) {
762 PACK_BGR(ptr3, r, g, b);
763 ptr3 += 3;
764 }
765 }
766 else {
767 /* Clear part of RGB buffer */
768 GLint i, j;
769 for (i = 0; i < height; i++) {
770 GLchan *ptr3 = PIXELADDR3(x, (y + i));
771 for (j = 0; j < width; j++) {
772 PACK_BGR(ptr3, r, g, b);
773 ptr3 += 3;
774 }
775 }
776 }
777 }
778 else if (osmesa->format == OSMESA_RGB_565) {
779 const GLchan r = ctx->Color.ClearColor[0];
780 const GLchan g = ctx->Color.ClearColor[1];
781 const GLchan b = ctx->Color.ClearColor[2];
782 GLushort clearPixel;
783 PACK_RGB_565(clearPixel, r, g, b);
784 if (all) {
785 /* Clear whole RGB buffer */
786 const GLuint n = osmesa->rowlength * osmesa->height;
787 GLushort *ptr2 = (GLushort *) osmesa->buffer;
788 GLuint i;
789 for (i = 0; i < n; i++) {
790 *ptr2 = clearPixel;
791 ptr2++;
792 }
793 }
794 else {
795 /* clear scissored region */
796 GLint i, j;
797 for (i = 0; i < height; i++) {
798 GLushort *ptr2 = (GLushort *) PIXELADDR2(x, (y + i));
799 for (j = 0; j < width; j++) {
800 *ptr2 = clearPixel;
801 ptr2++;
802 }
803 }
804 }
805 }
806 else {
807 #if CHAN_TYPE == GL_UNSIGNED_BYTE
808 /* 4-byte pixel value */
809 GLuint clearPixel;
810 GLchan *clr = (GLchan *) &clearPixel;
811 clr[osmesa->rInd] = ctx->Color.ClearColor[0];
812 clr[osmesa->gInd] = ctx->Color.ClearColor[1];
813 clr[osmesa->bInd] = ctx->Color.ClearColor[2];
814 clr[osmesa->aInd] = ctx->Color.ClearColor[3];
815 if (all) {
816 /* Clear whole RGBA buffer */
817 const GLuint n = osmesa->rowlength * osmesa->height;
818 GLuint *ptr4 = (GLuint *) osmesa->buffer;
819 GLuint i;
820 if (clearPixel) {
821 for (i = 0; i < n; i++) {
822 *ptr4++ = clearPixel;
823 }
824 }
825 else {
826 BZERO(ptr4, n * sizeof(GLuint));
827 }
828 }
829 else {
830 /* Clear part of RGBA buffer */
831 GLint i, j;
832 for (i = 0; i < height; i++) {
833 GLuint *ptr4 = (GLuint *) PIXELADDR4(x, (y + i));
834 for (j = 0; j < width; j++) {
835 *ptr4++ = clearPixel;
836 }
837 }
838 }
839 #else
840 const GLchan r = ctx->Color.ClearColor[0];
841 const GLchan g = ctx->Color.ClearColor[1];
842 const GLchan b = ctx->Color.ClearColor[2];
843 const GLchan a = ctx->Color.ClearColor[3];
844 if (all) {
845 /* Clear whole RGBA buffer */
846 const GLuint n = osmesa->rowlength * osmesa->height;
847 GLchan *p = (GLchan *) osmesa->buffer;
848 GLuint i;
849 for (i = 0; i < n; i++) {
850 PACK_RGBA(p, r, g, b, a);
851 p += 4;
852 }
853 }
854 else {
855 /* Clear part of RGBA buffer */
856 GLint i, j;
857 for (i = 0; i < height; i++) {
858 GLchan *p = PIXELADDR4(x, (y + i));
859 for (j = 0; j < width; j++) {
860 PACK_RGBA(p, r, g, b, a);
861 p += 4;
862 }
863 }
864 }
865
866 #endif
867 }
868 mask &= ~DD_FRONT_LEFT_BIT;
869 }
870 }
871
872 if (mask)
873 _swrast_Clear( ctx, mask, all, x, y, width, height );
874 }
875
876
877
878 static void buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
879 {
880 /* don't use GET_CURRENT_CONTEXT(ctx) here - it's a problem on Windows */
881 GLcontext *ctx = (GLcontext *) _glapi_get_context();
882 (void) buffer;
883 if (ctx) {
884 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
885 *width = osmesa->width;
886 *height = osmesa->height;
887 }
888 }
889
890
891 /**********************************************************************/
892 /***** Read/write spans/arrays of RGBA pixels *****/
893 /**********************************************************************/
894
895 /* Write RGBA pixels to an RGBA (or permuted) buffer. */
896 static void
897 write_rgba_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
898 CONST GLchan rgba[][4], const GLubyte mask[] )
899 {
900 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
901 GLchan *p = PIXELADDR4(x, y);
902 GLuint i;
903 if (mask) {
904 for (i = 0; i < n; i++, p += 4) {
905 if (mask[i]) {
906 PACK_RGBA(p, rgba[i][RCOMP], rgba[i][GCOMP],
907 rgba[i][BCOMP], rgba[i][ACOMP]);
908 }
909 }
910 }
911 else {
912 for (i = 0; i < n; i++, p += 4) {
913 PACK_RGBA(p, rgba[i][RCOMP], rgba[i][GCOMP],
914 rgba[i][BCOMP], rgba[i][ACOMP]);
915 }
916 }
917 }
918
919
920 /* Write RGBA pixels to an RGBA buffer. This is the fastest span-writer. */
921 static void
922 write_rgba_span_rgba( const GLcontext *ctx, GLuint n, GLint x, GLint y,
923 CONST GLchan rgba[][4], const GLubyte mask[] )
924 {
925 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
926 GLuint *ptr4 = (GLuint *) PIXELADDR4(x, y);
927 const GLuint *rgba4 = (const GLuint *) rgba;
928 GLuint i;
929 ASSERT(CHAN_TYPE == GL_UNSIGNED_BYTE);
930 if (mask) {
931 for (i = 0; i < n; i++) {
932 if (mask[i]) {
933 ptr4[i] = rgba4[i];
934 }
935 }
936 }
937 else {
938 MEMCPY( ptr4, rgba4, n * 4 );
939 }
940 }
941
942
943 /* Write RGB pixels to an RGBA (or permuted) buffer. */
944 static void
945 write_rgb_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
946 CONST GLchan rgb[][3], const GLubyte mask[] )
947 {
948 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
949 GLchan *p = PIXELADDR4(x, y);
950 GLuint i;
951 if (mask) {
952 for (i = 0; i < n; i++, p+=4) {
953 if (mask[i]) {
954 PACK_RGBA(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP], CHAN_MAX);
955 }
956 }
957 }
958 else {
959 for (i = 0; i < n; i++, p+=4) {
960 PACK_RGBA(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP], CHAN_MAX);
961 }
962 }
963 }
964
965
966
967 static void
968 write_monocolor_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
969 const GLchan color[4], const GLubyte mask[] )
970 {
971 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
972 GLchan *p = PIXELADDR4(x, y);
973 GLuint i;
974 for (i = 0; i < n; i++, p += 4) {
975 if (mask[i]) {
976 PACK_RGBA(p, color[RCOMP], color[GCOMP], color[BCOMP], color[ACOMP]);
977 }
978 }
979 }
980
981
982
983 static void
984 write_rgba_pixels( const GLcontext *ctx, GLuint n,
985 const GLint x[], const GLint y[],
986 CONST GLchan rgba[][4], const GLubyte mask[] )
987 {
988 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
989 GLuint i;
990 for (i = 0; i < n; i++) {
991 if (mask[i]) {
992 GLchan *p = PIXELADDR4(x[i], y[i]);
993 PACK_RGBA(p, rgba[i][RCOMP], rgba[i][GCOMP],
994 rgba[i][BCOMP], rgba[i][ACOMP]);
995 }
996 }
997 }
998
999
1000
1001 static void
1002 write_monocolor_pixels( const GLcontext *ctx, GLuint n,
1003 const GLint x[], const GLint y[],
1004 const GLchan color[4], const GLubyte mask[] )
1005 {
1006 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1007 GLuint i;
1008 for (i = 0; i < n; i++) {
1009 if (mask[i]) {
1010 GLchan *p = PIXELADDR4(x[i], y[i]);
1011 PACK_RGBA(p, color[RCOMP], color[GCOMP], color[BCOMP], color[ACOMP]);
1012 }
1013 }
1014 }
1015
1016
1017 static void
1018 read_rgba_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1019 GLchan rgba[][4] )
1020 {
1021 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1022 GLuint i;
1023 GLchan *p = PIXELADDR4(x, y);
1024 for (i = 0; i < n; i++, p += 4) {
1025 rgba[i][RCOMP] = UNPACK_RED(p);
1026 rgba[i][GCOMP] = UNPACK_GREEN(p);
1027 rgba[i][BCOMP] = UNPACK_BLUE(p);
1028 rgba[i][ACOMP] = UNPACK_ALPHA(p);
1029 }
1030 }
1031
1032
1033 /* Read RGBA pixels from an RGBA buffer */
1034 static void
1035 read_rgba_span_rgba( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1036 GLchan rgba[][4] )
1037 {
1038 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1039 GLuint *ptr4 = (GLuint *) PIXELADDR4(x, y);
1040 MEMCPY( rgba, ptr4, n * 4 * sizeof(GLchan) );
1041 }
1042
1043
1044 static void
1045 read_rgba_pixels( const GLcontext *ctx,
1046 GLuint n, const GLint x[], const GLint y[],
1047 GLchan rgba[][4], const GLubyte mask[] )
1048 {
1049 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1050 GLuint i;
1051 for (i = 0; i < n; i++) {
1052 if (mask[i]) {
1053 const GLchan *p = PIXELADDR4(x[i], y[i]);
1054 rgba[i][RCOMP] = UNPACK_RED(p);
1055 rgba[i][GCOMP] = UNPACK_GREEN(p);
1056 rgba[i][BCOMP] = UNPACK_BLUE(p);
1057 rgba[i][ACOMP] = UNPACK_ALPHA(p);
1058 }
1059 }
1060 }
1061
1062 /**********************************************************************/
1063 /***** 3 byte RGB pixel support funcs *****/
1064 /**********************************************************************/
1065
1066 /* Write RGBA pixels to an RGB buffer. */
1067 static void
1068 write_rgba_span_RGB( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1069 CONST GLchan rgba[][4], const GLubyte mask[] )
1070 {
1071 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1072 GLchan *p = PIXELADDR3(x, y);
1073 GLuint i;
1074 if (mask) {
1075 for (i = 0; i < n; i++, p += 3) {
1076 if (mask[i]) {
1077 PACK_RGB(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1078 }
1079 }
1080 }
1081 else {
1082 for (i = 0; i < n; i++, p += 3) {
1083 PACK_RGB(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1084 }
1085 }
1086 }
1087
1088 /* Write RGBA pixels to an BGR buffer. */
1089 static void
1090 write_rgba_span_BGR( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1091 CONST GLchan rgba[][4], const GLubyte mask[] )
1092 {
1093 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1094 GLchan *p = PIXELADDR3(x, y);
1095 GLuint i;
1096 if (mask) {
1097 for (i = 0; i < n; i++, p += 3) {
1098 if (mask[i]) {
1099 PACK_BGR(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1100 }
1101 }
1102 }
1103 else {
1104 for (i = 0; i < n; i++, p += 3) {
1105 PACK_BGR(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1106 }
1107 }
1108 }
1109
1110 /* Write RGB pixels to an RGB buffer. */
1111 static void
1112 write_rgb_span_RGB( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1113 CONST GLchan rgb[][3], const GLubyte mask[] )
1114 {
1115 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1116 GLchan *p = PIXELADDR3(x, y);
1117 GLuint i;
1118 if (mask) {
1119 for (i = 0; i < n; i++, p += 3) {
1120 if (mask[i]) {
1121 PACK_RGB(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1122 }
1123 }
1124 }
1125 else {
1126 for (i = 0; i < n; i++, p += 3) {
1127 PACK_RGB(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1128 }
1129 }
1130 }
1131
1132 /* Write RGB pixels to an BGR buffer. */
1133 static void
1134 write_rgb_span_BGR( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1135 CONST GLchan rgb[][3], const GLubyte mask[] )
1136 {
1137 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1138 GLchan *p = PIXELADDR3(x, y);
1139 GLuint i;
1140 if (mask) {
1141 for (i = 0; i < n; i++, p += 3) {
1142 if (mask[i]) {
1143 PACK_BGR(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1144 }
1145 }
1146 }
1147 else {
1148 for (i = 0; i < n; i++, p += 3) {
1149 PACK_BGR(p, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1150 }
1151 }
1152 }
1153
1154
1155 static void
1156 write_monocolor_span_RGB( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1157 const GLchan color[4], const GLubyte mask[] )
1158 {
1159 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1160 GLchan *p = PIXELADDR3(x, y);
1161 GLuint i;
1162 for (i = 0; i < n; i++, p += 3) {
1163 if (mask[i]) {
1164 PACK_RGB(p, color[RCOMP], color[GCOMP], color[BCOMP]);
1165 }
1166 }
1167 }
1168
1169 static void
1170 write_monocolor_span_BGR( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1171 const GLchan color[4], const GLubyte mask[] )
1172 {
1173 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1174 GLchan *p = PIXELADDR3(x, y);
1175 GLuint i;
1176 for (i = 0; i < n; i++, p += 3) {
1177 if (mask[i]) {
1178 PACK_BGR(p, color[RCOMP], color[GCOMP], color[BCOMP]);
1179 }
1180 }
1181 }
1182
1183 static void
1184 write_rgba_pixels_RGB( const GLcontext *ctx, GLuint n,
1185 const GLint x[], const GLint y[],
1186 CONST GLchan rgba[][4], const GLubyte mask[] )
1187 {
1188 const OSMesaContext osmesa = (const OSMesaContext) ctx;
1189 GLuint i;
1190 for (i = 0; i < n; i++) {
1191 if (mask[i]) {
1192 GLchan *p = PIXELADDR3(x[i], y[i]);
1193 PACK_RGB(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1194 }
1195 }
1196 }
1197
1198 static void
1199 write_rgba_pixels_BGR( const GLcontext *ctx, GLuint n,
1200 const GLint x[], const GLint y[],
1201 CONST GLchan rgba[][4], const GLubyte mask[] )
1202 {
1203 const OSMesaContext osmesa = (const OSMesaContext) ctx;
1204 GLuint i;
1205 for (i = 0; i < n; i++) {
1206 if (mask[i]) {
1207 GLchan *p = PIXELADDR3(x[i], y[i]);
1208 PACK_BGR(p, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1209 }
1210 }
1211 }
1212
1213 static void
1214 write_monocolor_pixels_RGB( const GLcontext *ctx,
1215 GLuint n, const GLint x[], const GLint y[],
1216 const GLchan color[4], const GLubyte mask[] )
1217 {
1218 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1219 GLuint i;
1220 for (i = 0; i < n; i++) {
1221 if (mask[i]) {
1222 GLchan *p = PIXELADDR3(x[i], y[i]);
1223 PACK_RGB(p, color[RCOMP], color[GCOMP], color[BCOMP]);
1224 }
1225 }
1226 }
1227
1228 static void
1229 write_monocolor_pixels_BGR( const GLcontext *ctx,
1230 GLuint n, const GLint x[], const GLint y[],
1231 const GLchan color[4], const GLubyte mask[] )
1232 {
1233 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1234 GLuint i;
1235 for (i = 0; i < n; i++) {
1236 if (mask[i]) {
1237 GLchan *p = PIXELADDR3(x[i], y[i]);
1238 PACK_BGR(p, color[RCOMP], color[GCOMP], color[BCOMP]);
1239 }
1240 }
1241 }
1242
1243 static void
1244 read_rgba_span3( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1245 GLchan rgba[][4] )
1246 {
1247 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1248 GLuint i;
1249 const GLchan *p = PIXELADDR3(x, y);
1250 for (i = 0; i < n; i++, p += 3) {
1251 rgba[i][RCOMP] = UNPACK_RED(p);
1252 rgba[i][GCOMP] = UNPACK_GREEN(p);
1253 rgba[i][BCOMP] = UNPACK_BLUE(p);
1254 rgba[i][ACOMP] = CHAN_MAX;
1255 }
1256 }
1257
1258 static void
1259 read_rgba_pixels3( const GLcontext *ctx,
1260 GLuint n, const GLint x[], const GLint y[],
1261 GLchan rgba[][4], const GLubyte mask[] )
1262 {
1263 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1264 GLuint i;
1265 for (i = 0; i < n; i++) {
1266 if (mask[i]) {
1267 const GLchan *p = PIXELADDR3(x[i], y[i]);
1268 rgba[i][RCOMP] = UNPACK_RED(p);
1269 rgba[i][GCOMP] = UNPACK_GREEN(p);
1270 rgba[i][BCOMP] = UNPACK_BLUE(p);
1271 rgba[i][ACOMP] = CHAN_MAX;
1272 }
1273 }
1274 }
1275
1276
1277 /**********************************************************************/
1278 /***** 2 byte RGB pixel support funcs *****/
1279 /**********************************************************************/
1280
1281 /* Write RGBA pixels to an RGB_565 buffer. */
1282 static void
1283 write_rgba_span2( const GLcontext *ctx,
1284 GLuint n, GLint x, GLint y,
1285 CONST GLchan rgba[][4], const GLubyte mask[] )
1286 {
1287 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1288 GLushort *ptr2 = (GLushort *) PIXELADDR2(x, y);
1289 GLuint i;
1290 if (mask) {
1291 for (i = 0; i < n; i++, ptr2++) {
1292 if (mask[i]) {
1293 PACK_RGB_565(*ptr2, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1294 }
1295 }
1296 }
1297 else {
1298 for (i = 0; i < n; i++, ptr2++) {
1299 PACK_RGB_565(*ptr2, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1300 }
1301 }
1302 }
1303
1304
1305 /* Write RGB pixels to an RGB_565 buffer. */
1306 static void
1307 write_rgb_span2( const GLcontext *ctx,
1308 GLuint n, GLint x, GLint y,
1309 CONST GLchan rgb[][3], const GLubyte mask[] )
1310 {
1311 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1312 GLushort *ptr2 = (GLushort *) PIXELADDR2(x, y);
1313 GLuint i;
1314 if (mask) {
1315 for (i = 0; i < n; i++, ptr2++) {
1316 if (mask[i]) {
1317 PACK_RGB_565(*ptr2, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1318 }
1319 }
1320 }
1321 else {
1322 for (i = 0; i < n; i++, ptr2++) {
1323 PACK_RGB_565(*ptr2, rgb[i][RCOMP], rgb[i][GCOMP], rgb[i][BCOMP]);
1324 }
1325 }
1326 }
1327
1328
1329 static void
1330 write_monocolor_span2( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1331 const GLchan color[4], const GLubyte mask[] )
1332 {
1333 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1334 GLushort pixel;
1335 GLushort *ptr2 = (GLushort *) PIXELADDR2(x, y);
1336 GLuint i;
1337 PACK_RGB_565(pixel, color[RCOMP], color[GCOMP], color[BCOMP]);
1338 for (i = 0; i < n; i++, ptr2++) {
1339 if (mask[i]) {
1340 *ptr2 = pixel;
1341 }
1342 }
1343 }
1344
1345
1346 static void
1347 write_rgba_pixels2( const GLcontext *ctx,
1348 GLuint n, const GLint x[], const GLint y[],
1349 CONST GLchan rgba[][4], const GLubyte mask[] )
1350 {
1351 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1352 GLuint i;
1353 for (i = 0; i < n; i++) {
1354 if (mask[i]) {
1355 GLushort *ptr2 = (GLushort *) PIXELADDR2(x[i],y[i]);
1356 PACK_RGB_565(*ptr2, rgba[i][RCOMP], rgba[i][GCOMP], rgba[i][BCOMP]);
1357 }
1358 }
1359 }
1360
1361 static void
1362 write_monocolor_pixels2( const GLcontext *ctx,
1363 GLuint n, const GLint x[], const GLint y[],
1364 const GLchan color[4], const GLubyte mask[] )
1365 {
1366 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1367 GLuint i;
1368 GLushort pixel;
1369 PACK_RGB_565(pixel, color[RCOMP], color[GCOMP], color[BCOMP]);
1370 for (i = 0; i < n; i++) {
1371 if (mask[i]) {
1372 GLushort *ptr2 = (GLushort *) PIXELADDR2(x[i],y[i]);
1373 *ptr2 = pixel;
1374 }
1375 }
1376 }
1377
1378 static void
1379 read_rgba_span2( const GLcontext *ctx,
1380 GLuint n, GLint x, GLint y,
1381 GLchan rgba[][4] )
1382 {
1383 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1384 GLuint i;
1385 const GLushort *ptr2 = (const GLushort *) PIXELADDR2(x, y);
1386 for (i = 0; i < n; i++, ptr2++) {
1387 /* This should be fixed to get the low bits right */
1388 rgba[i][RCOMP] = (*ptr2 >> 8) & 0xFe;
1389 rgba[i][GCOMP] = (*ptr2 >> 3) & 0xFc;
1390 rgba[i][BCOMP] = (*ptr2 << 3) & 0xFe;
1391 rgba[i][ACOMP] = 0;
1392 }
1393 }
1394
1395 static void
1396 read_rgba_pixels2( const GLcontext *ctx,
1397 GLuint n, const GLint x[], const GLint y[],
1398 GLchan rgba[][4], const GLubyte mask[] )
1399 {
1400 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1401 GLuint i;
1402 for (i = 0; i < n; i++) {
1403 if (mask[i]) {
1404 /* This should be fixed to get the low bits right */
1405 const GLushort *ptr2 = (const GLushort *) PIXELADDR2(x[i],y[i]);
1406 rgba[i][RCOMP] = (*ptr2 >> 8) & 0xFE;
1407 rgba[i][GCOMP] = (*ptr2 >> 3) & 0xFC;
1408 rgba[i][BCOMP] = (*ptr2 << 3) & 0xFE;
1409 rgba[i][ACOMP] = 0;
1410 }
1411 }
1412 }
1413
1414
1415
1416 /**********************************************************************/
1417 /***** Read/write spans/arrays of CI pixels *****/
1418 /**********************************************************************/
1419
1420 /* Write 32-bit color index to buffer */
1421 static void
1422 write_index32_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1423 const GLuint index[], const GLubyte mask[] )
1424 {
1425 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1426 GLchan *ptr1 = PIXELADDR1(x, y);
1427 GLuint i;
1428 if (mask) {
1429 for (i=0;i<n;i++,ptr1++) {
1430 if (mask[i]) {
1431 *ptr1 = (GLchan) index[i];
1432 }
1433 }
1434 }
1435 else {
1436 for (i=0;i<n;i++,ptr1++) {
1437 *ptr1 = (GLchan) index[i];
1438 }
1439 }
1440 }
1441
1442
1443 /* Write 8-bit color index to buffer */
1444 static void
1445 write_index8_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1446 const GLubyte index[], const GLubyte mask[] )
1447 {
1448 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1449 GLchan *ptr1 = PIXELADDR1(x, y);
1450 GLuint i;
1451 if (mask) {
1452 for (i=0;i<n;i++,ptr1++) {
1453 if (mask[i]) {
1454 *ptr1 = (GLchan) index[i];
1455 }
1456 }
1457 }
1458 else {
1459 MEMCPY(ptr1, index, n * sizeof(GLchan));
1460 }
1461 }
1462
1463
1464 static void
1465 write_monoindex_span( const GLcontext *ctx, GLuint n, GLint x, GLint y,
1466 GLuint colorIndex, const GLubyte mask[] )
1467 {
1468 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1469 GLchan *ptr1 = PIXELADDR1(x, y);
1470 GLuint i;
1471 for (i=0;i<n;i++,ptr1++) {
1472 if (mask[i]) {
1473 *ptr1 = (GLchan) colorIndex;
1474 }
1475 }
1476 }
1477
1478
1479 static void
1480 write_index_pixels( const GLcontext *ctx,
1481 GLuint n, const GLint x[], const GLint y[],
1482 const GLuint index[], const GLubyte mask[] )
1483 {
1484 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1485 GLuint i;
1486 for (i=0;i<n;i++) {
1487 if (mask[i]) {
1488 GLchan *ptr1 = PIXELADDR1(x[i], y[i]);
1489 *ptr1 = (GLchan) index[i];
1490 }
1491 }
1492 }
1493
1494
1495 static void
1496 write_monoindex_pixels( const GLcontext *ctx,
1497 GLuint n, const GLint x[], const GLint y[],
1498 GLuint colorIndex, const GLubyte mask[] )
1499 {
1500 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1501 GLuint i;
1502 for (i=0;i<n;i++) {
1503 if (mask[i]) {
1504 GLchan *ptr1 = PIXELADDR1(x[i], y[i]);
1505 *ptr1 = (GLchan) colorIndex;
1506 }
1507 }
1508 }
1509
1510
1511 static void
1512 read_index_span( const GLcontext *ctx,
1513 GLuint n, GLint x, GLint y, GLuint index[] )
1514 {
1515 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1516 GLuint i;
1517 const GLchan *ptr1 = (const GLchan *) PIXELADDR1(x, y);
1518 for (i=0;i<n;i++,ptr1++) {
1519 index[i] = (GLuint) *ptr1;
1520 }
1521 }
1522
1523
1524 static void
1525 read_index_pixels( const GLcontext *ctx,
1526 GLuint n, const GLint x[], const GLint y[],
1527 GLuint index[], const GLubyte mask[] )
1528 {
1529 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1530 GLuint i;
1531 for (i=0;i<n;i++) {
1532 if (mask[i] ) {
1533 const GLchan *ptr1 = PIXELADDR1(x[i], y[i]);
1534 index[i] = (GLuint) *ptr1;
1535 }
1536 }
1537 }
1538
1539
1540
1541 /**********************************************************************/
1542 /***** Optimized line rendering *****/
1543 /**********************************************************************/
1544
1545
1546 /*
1547 * Draw a flat-shaded, RGB line into an osmesa buffer.
1548 */
1549 static void
1550 flat_rgba_line( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 )
1551 {
1552 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1553 const GLchan *color = vert1->color;
1554
1555 #define INTERP_XY 1
1556 #define CLIP_HACK 1
1557 #define PLOT(X, Y) \
1558 do { \
1559 GLchan *p = PIXELADDR4(X, Y); \
1560 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
1561 } while (0)
1562
1563 #ifdef WIN32
1564 #include "..\swrast\s_linetemp.h"
1565 #else
1566 #include "swrast/s_linetemp.h"
1567 #endif
1568 }
1569
1570
1571 /*
1572 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1573 */
1574 static void
1575 flat_rgba_z_line(GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1)
1576 {
1577 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1578 const GLchan *color = vert1->color;
1579
1580 #define INTERP_XY 1
1581 #define INTERP_Z 1
1582 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1583 #define CLIP_HACK 1
1584 #define PLOT(X, Y) \
1585 do { \
1586 if (Z < *zPtr) { \
1587 GLchan *p = PIXELADDR4(X, Y); \
1588 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
1589 color[BCOMP], color[ACOMP]); \
1590 *zPtr = Z; \
1591 } \
1592 } while (0)
1593
1594
1595 #ifdef WIN32
1596 #include "..\swrast\s_linetemp.h"
1597 #else
1598 #include "swrast/s_linetemp.h"
1599 #endif
1600 }
1601
1602
1603 /*
1604 * Draw a flat-shaded, alpha-blended, RGB line into an osmesa buffer.
1605 * XXX update for GLchan
1606 */
1607 static void
1608 flat_blend_rgba_line( GLcontext *ctx,
1609 const SWvertex *vert0, const SWvertex *vert1 )
1610 {
1611 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1612 const GLint rshift = osmesa->rshift;
1613 const GLint gshift = osmesa->gshift;
1614 const GLint bshift = osmesa->bshift;
1615 const GLint avalue = vert0->color[3];
1616 const GLint msavalue = CHAN_MAX - avalue;
1617 const GLint rvalue = vert1->color[0]*avalue;
1618 const GLint gvalue = vert1->color[1]*avalue;
1619 const GLint bvalue = vert1->color[2]*avalue;
1620
1621 #define INTERP_XY 1
1622 #define CLIP_HACK 1
1623 #define PLOT(X,Y) \
1624 { GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1625 GLuint pixel = 0; \
1626 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift);\
1627 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift);\
1628 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift);\
1629 *ptr4 = pixel; \
1630 }
1631
1632 #if 0 /* XXX use this in the future */
1633 #define PLOT(X,Y) \
1634 { \
1635 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1636 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1637 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1638 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1639 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1640 }
1641 #endif
1642
1643 #ifdef WIN32
1644 #include "..\swrast\s_linetemp.h"
1645 #else
1646 #include "swrast/s_linetemp.h"
1647 #endif
1648 }
1649
1650
1651 /*
1652 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1653 * But don't write to Z buffer.
1654 * XXX update for GLchan
1655 */
1656 static void
1657 flat_blend_rgba_z_line( GLcontext *ctx,
1658 const SWvertex *vert0, const SWvertex *vert1 )
1659 {
1660 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1661 const GLint rshift = osmesa->rshift;
1662 const GLint gshift = osmesa->gshift;
1663 const GLint bshift = osmesa->bshift;
1664 const GLint avalue = vert0->color[3];
1665 const GLint msavalue = 256 - avalue;
1666 const GLint rvalue = vert1->color[0]*avalue;
1667 const GLint gvalue = vert1->color[1]*avalue;
1668 const GLint bvalue = vert1->color[2]*avalue;
1669
1670 #define INTERP_XY 1
1671 #define INTERP_Z 1
1672 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1673 #define CLIP_HACK 1
1674 #define PLOT(X,Y) \
1675 if (Z < *zPtr) { \
1676 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1677 GLuint pixel = 0; \
1678 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1679 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1680 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1681 *ptr4 = pixel; \
1682 }
1683
1684 #if 0 /* XXX use this in the future */
1685 #define PLOT(X,Y) \
1686 if (Z < *zPtr) { \
1687 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1688 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1689 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1690 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1691 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1692 }
1693 #endif
1694
1695 #ifdef WIN32
1696 #include "..\swrast\s_linetemp.h"
1697 #else
1698 #include "swrast/s_linetemp.h"
1699 #endif
1700 }
1701
1702
1703 /*
1704 * Draw a flat-shaded, Z-less, alpha-blended, RGB line into an osmesa buffer.
1705 * XXX update for GLchan
1706 */
1707 static void
1708 flat_blend_rgba_z_line_write( GLcontext *ctx,
1709 const SWvertex *vert0, const SWvertex *vert1 )
1710 {
1711 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1712 const GLint rshift = osmesa->rshift;
1713 const GLint gshift = osmesa->gshift;
1714 const GLint bshift = osmesa->bshift;
1715 const GLint avalue = vert0->color[3];
1716 const GLint msavalue = 256 - avalue;
1717 const GLint rvalue = vert1->color[0]*avalue;
1718 const GLint gvalue = vert1->color[1]*avalue;
1719 const GLint bvalue = vert1->color[2]*avalue;
1720
1721 #define INTERP_XY 1
1722 #define INTERP_Z 1
1723 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1724 #define CLIP_HACK 1
1725 #define PLOT(X,Y) \
1726 if (Z < *zPtr) { \
1727 GLuint *ptr4 = (GLuint *) PIXELADDR4(X, Y); \
1728 GLuint pixel = 0; \
1729 pixel |=((((((*ptr4) >> rshift) & 0xff)*msavalue+rvalue)>>8) << rshift); \
1730 pixel |=((((((*ptr4) >> gshift) & 0xff)*msavalue+gvalue)>>8) << gshift); \
1731 pixel |=((((((*ptr4) >> bshift) & 0xff)*msavalue+bvalue)>>8) << bshift); \
1732 *ptr4 = pixel; \
1733 *zPtr = Z; \
1734 }
1735
1736 #if 0 /* XXX use this in the future */
1737 #define PLOT(X,Y) \
1738 if (Z < *zPtr) { \
1739 GLchan *pixel = (GLchan *) PIXELADDR4(X, Y); \
1740 pixel[rInd] = (pixel[rInd] * msavalue + rvalue) >> CHAN_BITS; \
1741 pixel[gInd] = (pixel[gInd] * msavalue + gvalue) >> CHAN_BITS; \
1742 pixel[bInd] = (pixel[bInd] * msavalue + bvalue) >> CHAN_BITS; \
1743 pixel[aInd] = (pixel[aInd] * msavalue + avalue) >> CHAN_BITS; \
1744 *zPtr = Z; \
1745 }
1746 #endif
1747
1748 #ifdef WIN32
1749 #include "..\swrast\s_linetemp.h"
1750 #else
1751 #include "swrast/s_linetemp.h"
1752 #endif
1753 }
1754
1755
1756 /*
1757 * Analyze context state to see if we can provide a fast line drawing
1758 * function, like those in lines.c. Otherwise, return NULL.
1759 */
1760 static swrast_line_func
1761 osmesa_choose_line_function( GLcontext *ctx )
1762 {
1763 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1764 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
1765
1766 if (CHAN_BITS != 8) return NULL;
1767 if (ctx->RenderMode != GL_RENDER) return NULL;
1768 if (ctx->Line.SmoothFlag) return NULL;
1769 if (ctx->Texture._ReallyEnabled) return NULL;
1770 if (ctx->Light.ShadeModel != GL_FLAT) return NULL;
1771 if (ctx->Line.Width != 1.0F) return NULL;
1772 if (ctx->Line.StippleFlag) return NULL;
1773 if (ctx->Line.SmoothFlag) return NULL;
1774 if (osmesa->format != OSMESA_RGBA &&
1775 osmesa->format != OSMESA_BGRA &&
1776 osmesa->format != OSMESA_ARGB) return NULL;
1777
1778 if (swrast->_RasterMask==DEPTH_BIT
1779 && ctx->Depth.Func==GL_LESS
1780 && ctx->Depth.Mask==GL_TRUE
1781 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
1782 return (swrast_line_func) flat_rgba_z_line;
1783 }
1784
1785 if (swrast->_RasterMask == 0) {
1786 return (swrast_line_func) flat_rgba_line;
1787 }
1788
1789 if (swrast->_RasterMask==(DEPTH_BIT|BLEND_BIT)
1790 && ctx->Depth.Func==GL_LESS
1791 && ctx->Depth.Mask==GL_TRUE
1792 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
1793 && ctx->Color.BlendSrcRGB==GL_SRC_ALPHA
1794 && ctx->Color.BlendDstRGB==GL_ONE_MINUS_SRC_ALPHA
1795 && ctx->Color.BlendSrcA==GL_SRC_ALPHA
1796 && ctx->Color.BlendDstA==GL_ONE_MINUS_SRC_ALPHA
1797 && ctx->Color.BlendEquation==GL_FUNC_ADD_EXT) {
1798 return (swrast_line_func) flat_blend_rgba_z_line_write;
1799 }
1800
1801 if (swrast->_RasterMask==(DEPTH_BIT|BLEND_BIT)
1802 && ctx->Depth.Func==GL_LESS
1803 && ctx->Depth.Mask==GL_FALSE
1804 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
1805 && ctx->Color.BlendSrcRGB==GL_SRC_ALPHA
1806 && ctx->Color.BlendDstRGB==GL_ONE_MINUS_SRC_ALPHA
1807 && ctx->Color.BlendSrcA==GL_SRC_ALPHA
1808 && ctx->Color.BlendDstA==GL_ONE_MINUS_SRC_ALPHA
1809 && ctx->Color.BlendEquation==GL_FUNC_ADD_EXT) {
1810 return (swrast_line_func) flat_blend_rgba_z_line;
1811 }
1812
1813 if (swrast->_RasterMask==BLEND_BIT
1814 && ctx->Color.BlendSrcRGB==GL_SRC_ALPHA
1815 && ctx->Color.BlendDstRGB==GL_ONE_MINUS_SRC_ALPHA
1816 && ctx->Color.BlendSrcA==GL_SRC_ALPHA
1817 && ctx->Color.BlendDstA==GL_ONE_MINUS_SRC_ALPHA
1818 && ctx->Color.BlendEquation==GL_FUNC_ADD_EXT) {
1819 return (swrast_line_func) flat_blend_rgba_line;
1820 }
1821
1822 return (swrast_line_func) NULL;
1823 }
1824
1825
1826 /**********************************************************************/
1827 /***** Optimized triangle rendering *****/
1828 /**********************************************************************/
1829
1830
1831 /*
1832 * Smooth-shaded, z-less triangle, RGBA color.
1833 */
1834 static void smooth_rgba_z_triangle( GLcontext *ctx,
1835 const SWvertex *v0,
1836 const SWvertex *v1,
1837 const SWvertex *v2 )
1838 {
1839 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1840
1841 #define INTERP_Z 1
1842 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1843 #define INTERP_RGB 1
1844 #define INTERP_ALPHA 1
1845 #define RENDER_SPAN( span ) \
1846 GLuint i; \
1847 GLchan *img = PIXELADDR4(span.x, span.y); \
1848 for (i = 0; i < span.end; i++, img += 4) { \
1849 const GLdepth z = FixedToDepth(span.z); \
1850 if (z < zRow[i]) { \
1851 PACK_RGBA(img, FixedToChan(span.red), \
1852 FixedToChan(span.green), FixedToChan(span.blue), \
1853 FixedToChan(span.alpha)); \
1854 zRow[i] = z; \
1855 } \
1856 span.red += span.redStep; \
1857 span.green += span.greenStep; \
1858 span.blue += span.blueStep; \
1859 span.alpha += span.alphaStep; \
1860 span.z += span.zStep; \
1861 }
1862
1863 #ifdef WIN32
1864 #include "..\swrast\s_tritemp.h"
1865 #else
1866 #include "swrast/s_tritemp.h"
1867 #endif
1868 }
1869
1870
1871
1872
1873 /*
1874 * Flat-shaded, z-less triangle, RGBA color.
1875 */
1876 static void flat_rgba_z_triangle( GLcontext *ctx,
1877 const SWvertex *v0,
1878 const SWvertex *v1,
1879 const SWvertex *v2 )
1880 {
1881 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1882 #define INTERP_Z 1
1883 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1884 #define SETUP_CODE \
1885 GLuint pixel; \
1886 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
1887 v2->color[2], v2->color[3]);
1888
1889 #define RENDER_SPAN( span ) \
1890 GLuint i; \
1891 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
1892 for (i = 0; i < span.end; i++) { \
1893 const GLdepth z = FixedToDepth(span.z); \
1894 if (z < zRow[i]) { \
1895 img[i] = pixel; \
1896 zRow[i] = z; \
1897 } \
1898 span.z += span.zStep; \
1899 }
1900
1901 #ifdef WIN32
1902 #include "..\swrast\s_tritemp.h"
1903 #else
1904 #include "swrast/s_tritemp.h"
1905 #endif
1906 }
1907
1908
1909
1910 /*
1911 * Return pointer to an accelerated triangle function if possible.
1912 */
1913 static swrast_tri_func
1914 osmesa_choose_triangle_function( GLcontext *ctx )
1915 {
1916 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
1917 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
1918
1919 if (CHAN_BITS != 8) return (swrast_tri_func) NULL;
1920 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
1921 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
1922 if (ctx->Polygon.StippleFlag) return (swrast_tri_func) NULL;
1923 if (ctx->Texture._ReallyEnabled) return (swrast_tri_func) NULL;
1924 if (osmesa->format != OSMESA_RGBA &&
1925 osmesa->format != OSMESA_BGRA &&
1926 osmesa->format != OSMESA_ARGB) return (swrast_tri_func) NULL;
1927
1928 if (swrast->_RasterMask == DEPTH_BIT &&
1929 ctx->Depth.Func == GL_LESS &&
1930 ctx->Depth.Mask == GL_TRUE &&
1931 ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
1932 if (ctx->Light.ShadeModel == GL_SMOOTH) {
1933 return (swrast_tri_func) smooth_rgba_z_triangle;
1934 }
1935 else {
1936 return (swrast_tri_func) flat_rgba_z_triangle;
1937 }
1938 }
1939 return (swrast_tri_func) NULL;
1940 }
1941
1942
1943
1944 /* Override for the swrast triangle-selection function. Try to use one
1945 * of our internal triangle functions, otherwise fall back to the
1946 * standard swrast functions.
1947 */
1948 static void osmesa_choose_triangle( GLcontext *ctx )
1949 {
1950 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1951
1952 swrast->Triangle = osmesa_choose_triangle_function( ctx );
1953 if (!swrast->Triangle)
1954 _swrast_choose_triangle( ctx );
1955 }
1956
1957 static void osmesa_choose_line( GLcontext *ctx )
1958 {
1959 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1960
1961 swrast->Line = osmesa_choose_line_function( ctx );
1962 if (!swrast->Line)
1963 _swrast_choose_line( ctx );
1964 }
1965
1966
1967 #define OSMESA_NEW_LINE (_NEW_LINE | \
1968 _NEW_TEXTURE | \
1969 _NEW_LIGHT | \
1970 _NEW_DEPTH | \
1971 _NEW_RENDERMODE | \
1972 _SWRAST_NEW_RASTERMASK)
1973
1974 #define OSMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1975 _NEW_TEXTURE | \
1976 _NEW_LIGHT | \
1977 _NEW_DEPTH | \
1978 _NEW_RENDERMODE | \
1979 _SWRAST_NEW_RASTERMASK)
1980
1981
1982 /* Extend the software rasterizer with our line and triangle
1983 * functions.
1984 */
1985 static void osmesa_register_swrast_functions( GLcontext *ctx )
1986 {
1987 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1988
1989 swrast->choose_line = osmesa_choose_line;
1990 swrast->choose_triangle = osmesa_choose_triangle;
1991
1992 swrast->invalidate_line |= OSMESA_NEW_LINE;
1993 swrast->invalidate_triangle |= OSMESA_NEW_TRIANGLE;
1994 }
1995
1996
1997 static const GLubyte *get_string( GLcontext *ctx, GLenum name )
1998 {
1999 (void) ctx;
2000 switch (name) {
2001 case GL_RENDERER:
2002 #if CHAN_BITS == 32
2003 return (const GLubyte *) "Mesa OffScreen32";
2004 #elif CHAN_BITS == 16
2005 return (const GLubyte *) "Mesa OffScreen16";
2006 #else
2007 return (const GLubyte *) "Mesa OffScreen";
2008 #endif
2009 default:
2010 return NULL;
2011 }
2012 }
2013
2014
2015 static void osmesa_update_state( GLcontext *ctx, GLuint new_state )
2016 {
2017 OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
2018 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference( ctx );
2019 TNLcontext *tnl = TNL_CONTEXT(ctx);
2020
2021 ASSERT((void *) osmesa == (void *) ctx->DriverCtx);
2022
2023 /*
2024 * XXX these function pointers could be initialized just once during
2025 * context creation since they don't depend on any state changes.
2026 */
2027
2028 ctx->Driver.GetString = get_string;
2029 ctx->Driver.UpdateState = osmesa_update_state;
2030 ctx->Driver.SetDrawBuffer = set_draw_buffer;
2031 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
2032 ctx->Driver.GetBufferSize = buffer_size;
2033
2034 ctx->Driver.Accum = _swrast_Accum;
2035 ctx->Driver.Bitmap = _swrast_Bitmap;
2036 ctx->Driver.Clear = clear;
2037 ctx->Driver.CopyPixels = _swrast_CopyPixels;
2038 ctx->Driver.DrawPixels = _swrast_DrawPixels;
2039 ctx->Driver.ReadPixels = _swrast_ReadPixels;
2040
2041 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
2042 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
2043 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
2044 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
2045 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
2046 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
2047 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
2048 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
2049
2050 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
2051 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
2052 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
2053 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
2054 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
2055 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
2056 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
2057 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
2058 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
2059
2060 ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
2061 ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
2062 ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
2063
2064 /* RGB(A) span/pixel functions */
2065 if (osmesa->format == OSMESA_RGB) {
2066 swdd->WriteRGBASpan = write_rgba_span_RGB;
2067 swdd->WriteRGBSpan = write_rgb_span_RGB;
2068 swdd->WriteMonoRGBASpan = write_monocolor_span_RGB;
2069 swdd->WriteRGBAPixels = write_rgba_pixels_RGB;
2070 swdd->WriteMonoRGBAPixels = write_monocolor_pixels_RGB;
2071 swdd->ReadRGBASpan = read_rgba_span3;
2072 swdd->ReadRGBAPixels = read_rgba_pixels3;
2073 }
2074 else if (osmesa->format == OSMESA_BGR) {
2075 swdd->WriteRGBASpan = write_rgba_span_BGR;
2076 swdd->WriteRGBSpan = write_rgb_span_BGR;
2077 swdd->WriteMonoRGBASpan = write_monocolor_span_BGR;
2078 swdd->WriteRGBAPixels = write_rgba_pixels_BGR;
2079 swdd->WriteMonoRGBAPixels = write_monocolor_pixels_BGR;
2080 swdd->ReadRGBASpan = read_rgba_span3;
2081 swdd->ReadRGBAPixels = read_rgba_pixels3;
2082 }
2083 else if (osmesa->format == OSMESA_RGB_565) {
2084 swdd->WriteRGBASpan = write_rgba_span2;
2085 swdd->WriteRGBSpan = write_rgb_span2;
2086 swdd->WriteMonoRGBASpan = write_monocolor_span2;
2087 swdd->WriteRGBAPixels = write_rgba_pixels2;
2088 swdd->WriteMonoRGBAPixels = write_monocolor_pixels2;
2089 swdd->ReadRGBASpan = read_rgba_span2;
2090 swdd->ReadRGBAPixels = read_rgba_pixels2;
2091 }
2092 else {
2093 /* 4 GLchan / pixel in frame buffer */
2094 swdd->WriteRGBSpan = write_rgb_span;
2095 swdd->WriteRGBAPixels = write_rgba_pixels;
2096 swdd->WriteMonoRGBASpan = write_monocolor_span;
2097 swdd->WriteMonoRGBAPixels = write_monocolor_pixels;
2098 if (osmesa->format == OSMESA_RGBA &&
2099 CHAN_TYPE == GL_UNSIGNED_BYTE &&
2100 RCOMP==0 && GCOMP==1 && BCOMP==2 && ACOMP==3) {
2101 /* special, fast case */
2102 swdd->WriteRGBASpan = write_rgba_span_rgba;
2103 swdd->ReadRGBASpan = read_rgba_span_rgba;
2104 }
2105 else {
2106 swdd->WriteRGBASpan = write_rgba_span;
2107 swdd->ReadRGBASpan = read_rgba_span;
2108 }
2109 swdd->ReadRGBAPixels = read_rgba_pixels;
2110 }
2111
2112 /* CI span/pixel functions */
2113 swdd->WriteCI32Span = write_index32_span;
2114 swdd->WriteCI8Span = write_index8_span;
2115 swdd->WriteMonoCISpan = write_monoindex_span;
2116 swdd->WriteCI32Pixels = write_index_pixels;
2117 swdd->WriteMonoCIPixels = write_monoindex_pixels;
2118 swdd->ReadCI32Span = read_index_span;
2119 swdd->ReadCI32Pixels = read_index_pixels;
2120
2121 swdd->SetReadBuffer = set_read_buffer;
2122
2123 tnl->Driver.RunPipeline = _tnl_run_pipeline;
2124
2125 _swrast_InvalidateState( ctx, new_state );
2126 _swsetup_InvalidateState( ctx, new_state );
2127 _ac_InvalidateState( ctx, new_state );
2128 _tnl_InvalidateState( ctx, new_state );
2129 }