Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/osmesa.c

raw
1 /* $Id: osmesa.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24 * Off-Screen Mesa rendering / Rendering into client memory space
25 */
26
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include "GL/osmesa.h"
31 #include "context.h"
32 #include "depth.h"
33 #include "macros.h"
34 #include "matrix.h"
35 #include "types.h"
36 #include "vb.h"
37 #include "bresenhm.h"
38
39
40 struct osmesa_context {
41 GLcontext *gl_ctx; /* The core GL/Mesa context */
42 GLvisual *gl_visual; /* Describes the buffers */
43 GLframebuffer *gl_buffer; /* Depth, stencil, accum, etc buffers */
44 GLenum format; /* either GL_RGBA or GL_COLOR_INDEX */
45 void *buffer; /* the image buffer */
46 GLint width, height; /* size of image buffer */
47 GLuint pixel; /* current color index or RGBA pixel value */
48 GLuint clearpixel; /* pixel for clearing the color buffer */
49 GLint rowlength; /* number of pixels per row */
50 GLint rshift, gshift; /* bit shifts for RGBA formats */
51 GLint bshift, ashift;
52 GLint rind, gind, bind; /* index offsets for RGBA formats */
53 void *rowaddr[MAX_HEIGHT]; /* address of first pixel in each image row */
54 GLboolean yup; /* TRUE -> Y increases upward */
55 /* FALSE -> Y increases downward */
56 };
57
58
59
60 #ifdef THREADS
61 /* A context handle for each thread */
62 /* TODO: an array/table of contexts indexed by thread IDs */
63 #else
64 /* One current context for address space, all threads */
65 static OSMesaContext Current = NULL;
66 #endif
67
68
69
70 /* A forward declaration: */
71 static void osmesa_setup_DD_pointers( GLcontext *ctx );
72
73
74
75
76 /*
77 * Create an Off-Screen Mesa rendering context. The only attribute needed is
78 * an RGBA vs Color-Index mode flag.
79 *
80 * Input: format - either GL_RGBA or GL_COLOR_INDEX
81 * sharelist - specifies another OSMesaContext with which to share
82 * display lists. NULL indicates no sharing.
83 * Return: an OSMesaContext or 0 if error
84 */
85 OSMesaContext OSMesaCreateContext( GLenum format, OSMesaContext sharelist )
86 {
87 OSMesaContext osmesa;
88 GLfloat rscale, gscale, bscale, ascale;
89 GLint rshift, gshift, bshift, ashift;
90 GLint rind, gind, bind;
91 GLint index_bits;
92 GLboolean rgbmode;
93 GLboolean swalpha;
94 GLuint i4 = 1;
95 GLubyte *i1 = (GLubyte *) &i4;
96 GLint little_endian = *i1;
97
98 swalpha = GL_FALSE;
99 rind = gind = bind = 0;
100 if (format==OSMESA_COLOR_INDEX) {
101 rscale = gscale = bscale = ascale = 0.0;
102 index_bits = 8;
103 rshift = gshift = bshift = ashift = 0;
104 rgbmode = GL_FALSE;
105 }
106 else if (format==OSMESA_RGBA) {
107 rscale = gscale = bscale = ascale = 255.0;
108 index_bits = 0;
109 if (little_endian) {
110 rshift = 0;
111 gshift = 8;
112 bshift = 16;
113 ashift = 24;
114 }
115 else {
116 rshift = 24;
117 gshift = 16;
118 bshift = 8;
119 ashift = 0;
120 }
121 rgbmode = GL_TRUE;
122 }
123 else if (format==OSMESA_BGRA) {
124 rscale = gscale = bscale = ascale = 255.0;
125 index_bits = 0;
126 if (little_endian) {
127 ashift = 0;
128 rshift = 8;
129 gshift = 16;
130 bshift = 24;
131 }
132 else {
133 bshift = 24;
134 gshift = 16;
135 rshift = 8;
136 ashift = 0;
137 }
138 rgbmode = GL_TRUE;
139 }
140 else if (format==OSMESA_ARGB) {
141 rscale = gscale = bscale = ascale = 255.0;
142 index_bits = 0;
143 if (little_endian) {
144 bshift = 0;
145 gshift = 8;
146 rshift = 16;
147 ashift = 24;
148 }
149 else {
150 ashift = 24;
151 rshift = 16;
152 gshift = 8;
153 bshift = 0;
154 }
155 rgbmode = GL_TRUE;
156 }
157 else if (format==OSMESA_RGB) {
158 rscale = gscale = bscale = ascale = 255.0;
159 index_bits = 0;
160 bshift = 0;
161 gshift = 8;
162 rshift = 16;
163 ashift = 24;
164 bind = 2;
165 gind = 1;
166 rind = 0;
167 rgbmode = GL_TRUE;
168 swalpha = GL_TRUE;
169 }
170 else if (format==OSMESA_BGR) {
171 rscale = gscale = bscale = ascale = 255.0;
172 index_bits = 0;
173 bshift = 0;
174 gshift = 8;
175 rshift = 16;
176 ashift = 24;
177 bind = 0;
178 gind = 1;
179 rind = 2;
180 rgbmode = GL_TRUE;
181 swalpha = GL_TRUE;
182 }
183 else {
184 return NULL;
185 }
186
187
188 osmesa = (OSMesaContext) calloc( 1, sizeof(struct osmesa_context) );
189 if (osmesa) {
190 osmesa->gl_visual = gl_create_visual( rgbmode,
191 swalpha, /* software alpha */
192 GL_FALSE, /* db_flag */
193 DEPTH_BITS,
194 STENCIL_BITS,
195 ACCUM_BITS,
196 index_bits,
197 rscale, gscale, bscale, ascale );
198 if (!osmesa->gl_visual) {
199 return NULL;
200 }
201
202 osmesa->gl_ctx = gl_create_context( osmesa->gl_visual,
203 sharelist ? sharelist->gl_ctx : NULL,
204 (void *) osmesa );
205 if (!osmesa->gl_ctx) {
206 gl_destroy_visual( osmesa->gl_visual );
207 free(osmesa);
208 return NULL;
209 }
210 osmesa->gl_buffer = gl_create_framebuffer( osmesa->gl_visual );
211 if (!osmesa->gl_buffer) {
212 gl_destroy_visual( osmesa->gl_visual );
213 gl_destroy_context( osmesa->gl_ctx );
214 free(osmesa);
215 return NULL;
216 }
217 osmesa->format = format;
218 osmesa->buffer = NULL;
219 osmesa->width = 0;
220 osmesa->height = 0;
221 osmesa->pixel = 0;
222 osmesa->clearpixel = 0;
223 osmesa->rowlength = 0;
224 osmesa->yup = GL_TRUE;
225 osmesa->rshift = rshift;
226 osmesa->gshift = gshift;
227 osmesa->bshift = bshift;
228 osmesa->ashift = ashift;
229 osmesa->rind = rind;
230 osmesa->gind = gind;
231 osmesa->bind = bind;
232 }
233 return osmesa;
234 }
235
236
237
238 /*
239 * Destroy an Off-Screen Mesa rendering context.
240 *
241 * Input: ctx - the context to destroy
242 */
243 void OSMesaDestroyContext( OSMesaContext ctx )
244 {
245 if (ctx) {
246 gl_destroy_visual( ctx->gl_visual );
247 gl_destroy_framebuffer( ctx->gl_buffer );
248 gl_destroy_context( ctx->gl_ctx );
249 free( ctx );
250 }
251 }
252
253
254
255 /*
256 * Recompute the values of the context's rowaddr array.
257 */
258 static void compute_row_addresses( OSMesaContext ctx )
259 {
260 GLint i;
261
262 if (ctx->yup) {
263 /* Y=0 is bottom line of window */
264 if (ctx->format==OSMESA_COLOR_INDEX) {
265 /* 1-byte CI mode */
266 GLubyte *origin = (GLubyte *) ctx->buffer;
267 for (i=0;i<MAX_HEIGHT;i++) {
268 ctx->rowaddr[i] = origin + i * ctx->rowlength;
269 }
270 }
271 else {
272 if ((ctx->format==OSMESA_RGB) || (ctx->format==OSMESA_BGR)) {
273 /* 3-byte RGB mode */
274 GLubyte *origin = (GLubyte *) ctx->buffer;
275 for (i=0;i<MAX_HEIGHT;i++) {
276 ctx->rowaddr[i] = origin + (i * (ctx->rowlength*3));
277 }
278 } else {
279 /* 4-byte RGBA mode */
280 GLuint *origin = (GLuint *) ctx->buffer;
281 for (i=0;i<MAX_HEIGHT;i++) {
282 ctx->rowaddr[i] = origin + i * ctx->rowlength;
283 }
284 }
285 }
286 }
287 else {
288 /* Y=0 is top line of window */
289 if (ctx->format==OSMESA_COLOR_INDEX) {
290 /* 1-byte CI mode */
291 GLubyte *origin = (GLubyte *) ctx->buffer;
292 for (i=0;i<MAX_HEIGHT;i++) {
293 ctx->rowaddr[i] = origin + (ctx->height-i-1) * ctx->rowlength;
294 }
295 }
296 else {
297 if ((ctx->format==OSMESA_RGB) || (ctx->format==OSMESA_BGR)) {
298 /* 3-byte RGB mode */
299 GLubyte *origin = (GLubyte *) ctx->buffer;
300 for (i=0;i<MAX_HEIGHT;i++) {
301 ctx->rowaddr[i] = origin + ((ctx->height-i-1) * (ctx->rowlength*3));
302 }
303 } else {
304 /* 4-byte RGBA mode */
305 GLuint *origin = (GLuint *) ctx->buffer;
306 for (i=0;i<MAX_HEIGHT;i++) {
307 ctx->rowaddr[i] = origin + (ctx->height-i-1) * ctx->rowlength;
308 }
309 }
310 }
311 }
312 }
313
314
315 /*
316 * Bind an OSMesaContext to an image buffer. The image buffer is just a
317 * block of memory which the client provides. Its size must be at least
318 * as large as width*height*sizeof(type). Its address should be a multiple
319 * of 4 if using RGBA mode.
320 *
321 * Image data is stored in the order of glDrawPixels: row-major order
322 * with the lower-left image pixel stored in the first array position
323 * (ie. bottom-to-top).
324 *
325 * Since the only type initially supported is GL_UNSIGNED_BYTE, if the
326 * context is in RGBA mode, each pixel will be stored as a 4-byte RGBA
327 * value. If the context is in color indexed mode, each pixel will be
328 * stored as a 1-byte value.
329 *
330 * If the context's viewport hasn't been initialized yet, it will now be
331 * initialized to (0,0,width,height).
332 *
333 * Input: ctx - the rendering context
334 * buffer - the image buffer memory
335 * type - data type for pixel components, only GL_UNSIGNED_BYTE
336 * supported now
337 * width, height - size of image buffer in pixels, at least 1
338 * Return: GL_TRUE if success, GL_FALSE if error because of invalid ctx,
339 * invalid buffer address, type!=GL_UNSIGNED_BYTE, width<1, height<1,
340 * width>internal limit or height>internal limit.
341 */
342 GLboolean OSMesaMakeCurrent( OSMesaContext ctx, void *buffer, GLenum type,
343 GLsizei width, GLsizei height )
344 {
345 if (!ctx || !buffer || type!=GL_UNSIGNED_BYTE
346 || width<1 || height<1 || width>MAX_WIDTH || height>MAX_HEIGHT) {
347 return GL_FALSE;
348 }
349
350 gl_make_current( ctx->gl_ctx, ctx->gl_buffer );
351
352 ctx->buffer = buffer;
353 ctx->width = width;
354 ctx->height = height;
355 if (ctx->rowlength==0) {
356 ctx->rowlength = width;
357 }
358
359 osmesa_setup_DD_pointers( ctx->gl_ctx );
360
361 #ifdef THREADS
362 /* Set current context for the calling thread */
363 /* TODO */
364 #else
365 /* Set current context for the address space, all threads */
366 Current = ctx;
367 #endif
368
369 compute_row_addresses( ctx );
370
371 /* init viewport */
372 if (ctx->gl_ctx->Viewport.Width==0) {
373 /* initialize viewport and scissor box to buffer size */
374 gl_Viewport( ctx->gl_ctx, 0, 0, width, height );
375 ctx->gl_ctx->Scissor.Width = width;
376 ctx->gl_ctx->Scissor.Height = height;
377 }
378
379 return GL_TRUE;
380 }
381
382
383
384
385 OSMesaContext OSMesaGetCurrentContext( void )
386 {
387 #ifdef THREADS
388 /* Return current handle for the calling thread */
389 #else
390 /* Return current handle for the address space, all threads */
391 return Current;
392 #endif
393 }
394
395
396
397 void OSMesaPixelStore( GLint pname, GLint value )
398 {
399 OSMesaContext ctx = OSMesaGetCurrentContext();
400
401 switch (pname) {
402 case OSMESA_ROW_LENGTH:
403 if (value<0) {
404 gl_error( ctx->gl_ctx, GL_INVALID_VALUE,
405 "OSMesaPixelStore(value)" );
406 return;
407 }
408 ctx->rowlength = value;
409 break;
410 case OSMESA_Y_UP:
411 ctx->yup = value ? GL_TRUE : GL_FALSE;
412 break;
413 default:
414 gl_error( ctx->gl_ctx, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" );
415 return;
416 }
417
418 compute_row_addresses( ctx );
419 }
420
421
422 void OSMesaGetIntegerv( GLint pname, GLint *value )
423 {
424 OSMesaContext ctx = OSMesaGetCurrentContext();
425
426 switch (pname) {
427 case OSMESA_WIDTH:
428 *value = ctx->width;
429 return;
430 case OSMESA_HEIGHT:
431 *value = ctx->height;
432 return;
433 case OSMESA_FORMAT:
434 *value = ctx->format;
435 return;
436 case OSMESA_TYPE:
437 *value = GL_UNSIGNED_BYTE;
438 return;
439 case OSMESA_ROW_LENGTH:
440 *value = ctx->rowlength;
441 return;
442 case OSMESA_Y_UP:
443 *value = ctx->yup;
444 return;
445 default:
446 gl_error( ctx->gl_ctx, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)" );
447 return;
448 }
449 }
450
451
452
453 /**********************************************************************/
454 /*** Device Driver Functions ***/
455 /**********************************************************************/
456
457
458 /*
459 * Useful macros:
460 */
461 #define PACK_RGBA(R,G,B,A) ( ((R) << osmesa->rshift) \
462 | ((G) << osmesa->gshift) \
463 | ((B) << osmesa->bshift) \
464 | ((A) << osmesa->ashift) )
465
466 #define PACK_RGBA2(R,G,B,A) ( ((R) << rshift) \
467 | ((G) << gshift) \
468 | ((B) << bshift) \
469 | ((A) << ashift) )
470
471 #define UNPACK_RED(P) (((P) >> osmesa->rshift) & 0xff)
472 #define UNPACK_GREEN(P) (((P) >> osmesa->gshift) & 0xff)
473 #define UNPACK_BLUE(P) (((P) >> osmesa->bshift) & 0xff)
474 #define UNPACK_ALPHA(P) (((P) >> osmesa->ashift) & 0xff)
475
476 #define PIXELADDR1(X,Y) ((GLubyte *) osmesa->rowaddr[Y] + (X))
477 #define PIXELADDR3(X,Y) ((GLubyte *) osmesa->rowaddr[Y] + ((X)*3))
478 #define PIXELADDR4(X,Y) ((GLuint *) osmesa->rowaddr[Y] + (X))
479
480
481
482
483 static GLboolean set_buffer( GLcontext *ctx, GLenum mode )
484 {
485 if (mode==GL_FRONT) {
486 return GL_TRUE;
487 }
488 else {
489 return GL_FALSE;
490 }
491 }
492
493
494 static void clear_index( GLcontext *ctx, GLuint index )
495 {
496 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
497 osmesa->clearpixel = index;
498 }
499
500
501
502 static void clear_color( GLcontext *ctx,
503 GLubyte r, GLubyte g, GLubyte b, GLubyte a )
504 {
505 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
506 osmesa->clearpixel = PACK_RGBA( r, g, b, a );
507 }
508
509
510
511 static void clear( GLcontext *ctx,
512 GLboolean all, GLint x, GLint y, GLint width, GLint height )
513 {
514 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
515 if (osmesa->format==OSMESA_COLOR_INDEX) {
516 if (all) {
517 /* Clear whole CI buffer */
518 MEMSET(osmesa->buffer, osmesa->clearpixel, osmesa->rowlength*osmesa->height);
519 }
520 else {
521 /* Clear part of CI buffer */
522 GLuint i, j;
523 for (i=0;i<height;i++) {
524 GLubyte *ptr1 = PIXELADDR1( x, (y+i) );
525 for (j=0;j<width;j++) {
526 *ptr1++ = osmesa->clearpixel;
527 }
528 }
529 }
530 }
531 else if ((osmesa->format==OSMESA_RGB)||(osmesa->format==OSMESA_BGR)) {
532 GLubyte rval = UNPACK_RED(osmesa->clearpixel);
533 GLubyte gval = UNPACK_GREEN(osmesa->clearpixel);
534 GLubyte bval = UNPACK_BLUE(osmesa->clearpixel);
535 GLint rind = osmesa->rind;
536 GLint gind = osmesa->gind;
537 GLint bind = osmesa->bind;
538 if (all) {
539 GLuint i, n;
540 GLubyte *ptr3 = (GLubyte *) osmesa->buffer;
541 /* Clear whole RGB buffer */
542 n = osmesa->rowlength * osmesa->height;
543 for (i=0;i<n;i++) {
544 ptr3[rind] = rval;
545 ptr3[gind] = gval;
546 ptr3[bind] = bval;
547 ptr3 += 3;
548 }
549 }
550 else {
551 /* Clear part of RGB buffer */
552 GLuint i, j;
553 for (i=0;i<height;i++) {
554 GLubyte *ptr3 = PIXELADDR3( x, (y+i) );
555 for (j=0;j<width;j++) {
556 ptr3[rind] = rval;
557 ptr3[gind] = gval;
558 ptr3[bind] = bval;
559 ptr3 += 3;
560 }
561 }
562 }
563 }
564 else {
565 if (all) {
566 /* Clear whole RGBA buffer */
567 GLuint i, n, *ptr4;
568 n = osmesa->rowlength * osmesa->height;
569 ptr4 = (GLuint *) osmesa->buffer;
570 for (i=0;i<n;i++) {
571 *ptr4++ = osmesa->clearpixel;
572 }
573 }
574 else {
575 /* Clear part of RGBA buffer */
576 GLuint i, j;
577 for (i=0;i<height;i++) {
578 GLuint *ptr4 = PIXELADDR4( x, (y+i) );
579 for (j=0;j<width;j++) {
580 *ptr4++ = osmesa->clearpixel;
581 }
582 }
583 }
584 }
585 }
586
587
588
589 static void set_index( GLcontext *ctx, GLuint index )
590 {
591 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
592 osmesa->pixel = index;
593 }
594
595
596
597 static void set_color( GLcontext *ctx,
598 GLubyte r, GLubyte g, GLubyte b, GLubyte a )
599 {
600 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
601 osmesa->pixel = PACK_RGBA( r, g, b, a );
602 }
603
604
605
606 static void buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
607 {
608 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
609 *width = osmesa->width;
610 *height = osmesa->height;
611 }
612
613
614 /**********************************************************************/
615 /***** 4 byte RGB and 1 byte CI pixel support funcs *****/
616 /**********************************************************************/
617
618 static void write_color_span( GLcontext *ctx,
619 GLuint n, GLint x, GLint y,
620 const GLubyte red[], const GLubyte green[],
621 const GLubyte blue[], const GLubyte alpha[],
622 const GLubyte mask[] )
623 {
624 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
625 GLuint *ptr4 = PIXELADDR4( x, y );
626 GLuint i;
627 GLint rshift = osmesa->rshift;
628 GLint gshift = osmesa->gshift;
629 GLint bshift = osmesa->bshift;
630 GLint ashift = osmesa->ashift;
631 if (mask) {
632 for (i=0;i<n;i++,ptr4++) {
633 if (mask[i]) {
634 *ptr4 = PACK_RGBA2( red[i], green[i], blue[i], alpha[i] );
635 }
636 }
637 }
638 else {
639 for (i=0;i<n;i++,ptr4++) {
640 *ptr4 = PACK_RGBA2( red[i], green[i], blue[i], alpha[i] );
641 }
642 }
643 }
644
645
646
647 static void write_monocolor_span( GLcontext *ctx,
648 GLuint n, GLint x, GLint y,
649 const GLubyte mask[] )
650 {
651 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
652 GLuint *ptr4 = PIXELADDR4(x,y);
653 GLuint i;
654 for (i=0;i<n;i++,ptr4++) {
655 if (mask[i]) {
656 *ptr4 = osmesa->pixel;
657 }
658 }
659 }
660
661
662
663 static void write_color_pixels( GLcontext *ctx,
664 GLuint n, const GLint x[], const GLint y[],
665 const GLubyte red[], const GLubyte green[],
666 const GLubyte blue[], const GLubyte alpha[],
667 const GLubyte mask[] )
668 {
669 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
670 GLuint i;
671 GLint rshift = osmesa->rshift;
672 GLint gshift = osmesa->gshift;
673 GLint bshift = osmesa->bshift;
674 GLint ashift = osmesa->ashift;
675 for (i=0;i<n;i++) {
676 if (mask[i]) {
677 GLuint *ptr4 = PIXELADDR4(x[i],y[i]);
678 *ptr4 = PACK_RGBA2( red[i], green[i], blue[i], alpha[i] );
679 }
680 }
681 }
682
683
684
685 static void write_monocolor_pixels( GLcontext *ctx,
686 GLuint n, const GLint x[], const GLint y[],
687 const GLubyte mask[] )
688 {
689 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
690 GLuint i;
691 for (i=0;i<n;i++) {
692 if (mask[i]) {
693 GLuint *ptr4 = PIXELADDR4(x[i],y[i]);
694 *ptr4 = osmesa->pixel;
695 }
696 }
697 }
698
699
700
701 static void write_index_span( GLcontext *ctx,
702 GLuint n, GLint x, GLint y, const GLuint index[],
703 const GLubyte mask[] )
704 {
705 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
706 GLubyte *ptr1 = PIXELADDR1(x,y);
707 GLuint i;
708 for (i=0;i<n;i++,ptr1++) {
709 if (mask[i]) {
710 *ptr1 = (GLubyte) index[i];
711 }
712 }
713 }
714
715
716
717 static void write_monoindex_span( GLcontext *ctx,
718 GLuint n, GLint x, GLint y,
719 const GLubyte mask[] )
720 {
721 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
722 GLubyte *ptr1 = PIXELADDR1(x,y);
723 GLuint i;
724 for (i=0;i<n;i++,ptr1++) {
725 if (mask[i]) {
726 *ptr1 = (GLubyte) osmesa->pixel;
727 }
728 }
729 }
730
731
732
733 static void write_index_pixels( GLcontext *ctx,
734 GLuint n, const GLint x[], const GLint y[],
735 const GLuint index[], const GLubyte mask[] )
736 {
737 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
738 GLuint i;
739 for (i=0;i<n;i++) {
740 if (mask[i]) {
741 GLubyte *ptr1 = PIXELADDR1(x[i],y[i]);
742 *ptr1 = (GLubyte) index[i];
743 }
744 }
745 }
746
747
748
749 static void write_monoindex_pixels( GLcontext *ctx,
750 GLuint n, const GLint x[], const GLint y[],
751 const GLubyte mask[] )
752 {
753 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
754 GLuint i;
755 for (i=0;i<n;i++) {
756 if (mask[i]) {
757 GLubyte *ptr1 = PIXELADDR1(x[i],y[i]);
758 *ptr1 = (GLubyte) osmesa->pixel;
759 }
760 }
761 }
762
763
764
765 static void read_index_span( GLcontext *ctx,
766 GLuint n, GLint x, GLint y, GLuint index[] )
767 {
768 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
769 GLuint i;
770 GLubyte *ptr1 = PIXELADDR1(x,y);
771 for (i=0;i<n;i++,ptr1++) {
772 index[i] = (GLuint) *ptr1;
773 }
774 }
775
776
777 static void read_color_span( GLcontext *ctx,
778 GLuint n, GLint x, GLint y,
779 GLubyte red[], GLubyte green[],
780 GLubyte blue[], GLubyte alpha[] )
781 {
782 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
783 GLuint i;
784 GLuint *ptr4 = PIXELADDR4(x,y);
785 for (i=0;i<n;i++) {
786 GLuint pixel = *ptr4++;
787 red[i] = UNPACK_RED(pixel);
788 green[i] = UNPACK_GREEN(pixel);
789 blue[i] = UNPACK_BLUE(pixel);
790 alpha[i] = UNPACK_ALPHA(pixel);
791 }
792 }
793
794
795 static void read_index_pixels( GLcontext *ctx,
796 GLuint n, const GLint x[], const GLint y[],
797 GLuint index[], const GLubyte mask[] )
798 {
799 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
800 GLuint i;
801 for (i=0;i<n;i++) {
802 if (mask[i] ) {
803 GLubyte *ptr1 = PIXELADDR1(x[i],y[i]);
804 index[i] = (GLuint) *ptr1;
805 }
806 }
807 }
808
809
810 static void read_color_pixels( GLcontext *ctx,
811 GLuint n, const GLint x[], const GLint y[],
812 GLubyte red[], GLubyte green[],
813 GLubyte blue[], GLubyte alpha[],
814 const GLubyte mask[] )
815 {
816 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
817 GLuint i;
818 for (i=0;i<n;i++) {
819 if (mask[i]) {
820 GLuint *ptr4 = PIXELADDR4(x[i],y[i]);
821 GLuint pixel = *ptr4;
822 red[i] = UNPACK_RED(pixel);
823 green[i] = UNPACK_GREEN(pixel);
824 blue[i] = UNPACK_BLUE(pixel);
825 alpha[i] = UNPACK_ALPHA(pixel);
826 }
827 }
828 }
829
830 /**********************************************************************/
831 /***** 3 byte RGB pixel support funcs *****/
832 /**********************************************************************/
833
834 static void write_color_span3( GLcontext *ctx,
835 GLuint n, GLint x, GLint y,
836 const GLubyte red[], const GLubyte green[],
837 const GLubyte blue[], const GLubyte alpha[],
838 const GLubyte mask[] )
839 {
840 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
841 GLubyte *ptr3 = PIXELADDR3( x, y);
842 GLuint i;
843 GLint rind = osmesa->rind;
844 GLint gind = osmesa->gind;
845 GLint bind = osmesa->bind;
846 if (mask) {
847 for (i=0;i<n;i++,ptr3+=3) {
848 if (mask[i]) {
849 ptr3[rind] = red[i];
850 ptr3[gind] = green[i];
851 ptr3[bind] = blue[i];
852 }
853 }
854 }
855 else {
856 for (i=0;i<n;i++,ptr3+=3) {
857 ptr3[rind] = red[i];
858 ptr3[gind] = green[i];
859 ptr3[bind] = blue[i];
860 }
861 }
862 }
863
864 static void write_monocolor_span3( GLcontext *ctx,
865 GLuint n, GLint x, GLint y,
866 const GLubyte mask[] )
867 {
868 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
869
870 GLubyte rval = UNPACK_RED(osmesa->pixel);
871 GLubyte gval = UNPACK_GREEN(osmesa->pixel);
872 GLubyte bval = UNPACK_BLUE(osmesa->pixel);
873 GLint rind = osmesa->rind;
874 GLint gind = osmesa->gind;
875 GLint bind = osmesa->bind;
876
877
878 GLubyte *ptr3 = PIXELADDR3( x, y);
879 GLuint i;
880 for (i=0;i<n;i++,ptr3+=3) {
881 if (mask[i]) {
882 ptr3[rind] = rval;
883 ptr3[gind] = gval;
884 ptr3[bind] = bval;
885 }
886 }
887 }
888
889 static void write_color_pixels3( GLcontext *ctx,
890 GLuint n, const GLint x[], const GLint y[],
891 const GLubyte red[], const GLubyte green[],
892 const GLubyte blue[], const GLubyte alpha[],
893 const GLubyte mask[] )
894 {
895 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
896 GLuint i;
897 GLint rind = osmesa->rind;
898 GLint gind = osmesa->gind;
899 GLint bind = osmesa->bind;
900
901 for (i=0;i<n;i++) {
902 if (mask[i]) {
903 GLubyte *ptr3 = PIXELADDR3(x[i],y[i]);
904 ptr3[rind] = red[i];
905 ptr3[gind] = green[i];
906 ptr3[bind] = blue[i];
907 }
908 }
909 }
910
911 static void write_monocolor_pixels3( GLcontext *ctx,
912 GLuint n, const GLint x[], const GLint y[],
913 const GLubyte mask[] )
914 {
915 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
916 GLuint i;
917 GLint rind = osmesa->rind;
918 GLint gind = osmesa->gind;
919 GLint bind = osmesa->bind;
920 GLubyte rval = UNPACK_RED(osmesa->pixel);
921 GLubyte gval = UNPACK_GREEN(osmesa->pixel);
922 GLubyte bval = UNPACK_BLUE(osmesa->pixel);
923 for (i=0;i<n;i++) {
924 if (mask[i]) {
925 GLubyte *ptr3 = PIXELADDR3(x[i],y[i]);
926 ptr3[rind] = rval;
927 ptr3[gind] = gval;
928 ptr3[bind] = bval;
929 }
930 }
931 }
932
933 static void read_color_span3( GLcontext *ctx,
934 GLuint n, GLint x, GLint y,
935 GLubyte red[], GLubyte green[],
936 GLubyte blue[], GLubyte alpha[] )
937 {
938 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
939 GLuint i;
940 GLint rind = osmesa->rind;
941 GLint gind = osmesa->gind;
942 GLint bind = osmesa->bind;
943 GLubyte *ptr3 = PIXELADDR3( x, y);
944 for (i=0;i<n;i++,ptr3+=3) {
945 red[i] = ptr3[rind];
946 green[i] = ptr3[gind];
947 blue[i] = ptr3[bind];
948 alpha[i] = 0;
949 }
950 }
951
952 static void read_color_pixels3( GLcontext *ctx,
953 GLuint n, const GLint x[], const GLint y[],
954 GLubyte red[], GLubyte green[],
955 GLubyte blue[], GLubyte alpha[],
956 const GLubyte mask[] )
957 {
958 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
959 GLuint i;
960 GLint rind = osmesa->rind;
961 GLint gind = osmesa->gind;
962 GLint bind = osmesa->bind;
963 for (i=0;i<n;i++) {
964 if (mask[i]) {
965 GLubyte *ptr3 = PIXELADDR3(x[i],y[i]);
966 red[i] = ptr3[rind];
967 green[i] = ptr3[gind];
968 blue[i] = ptr3[bind];
969 alpha[i] = 0;
970 }
971 }
972 }
973
974
975 /**********************************************************************/
976 /***** Optimized line rendering *****/
977 /**********************************************************************/
978
979 /*
980 * Despite being clipped to the view volume, the line's window coordinates
981 * may just lie outside the window bounds. That is, if the legal window
982 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
983 * These quick and dirty macros take care of that possibility.
984 */
985
986 #define WINCLIP_X(X1,X2) \
987 { \
988 GLint w = ctx->Buffer->Width; \
989 if ((X1==w) | (X2==w)) { \
990 if ((X1==w) & (X2==w)) return;\
991 X1 -= (X1==w); X2 -= (X2==w);\
992 } \
993 }
994
995 #define WINCLIP_Y(Y1,Y2) \
996 { \
997 GLint h = ctx->Buffer->Height; \
998 if ((Y1==h) | (Y2==h)) { \
999 if ((Y1==h) & (Y2==h)) return;\
1000 Y1 -= (Y1==h); Y2 -= (Y2==h);\
1001 } \
1002 }
1003
1004 /*
1005 * Draw a flat-shaded, RGB line into an osmesa buffer.
1006 */
1007 static void flat_color_line( GLcontext *ctx,
1008 GLuint v0, GLuint v1, GLuint pv )
1009 {
1010 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1011 struct vertex_buffer *VB = ctx->VB;
1012 GLint x1 = (GLint) VB->Win[v0][0], y1 = (GLint) VB->Win[v0][1];
1013 GLint x2 = (GLint) VB->Win[v1][0], y2 = (GLint) VB->Win[v1][1];
1014 unsigned long pixel = PACK_RGBA( VB->Color[pv][0], VB->Color[pv][1],
1015 VB->Color[pv][2], 0 );
1016 WINCLIP_X(x1,x2);
1017 WINCLIP_Y(y1,y2);
1018
1019 #define BRESENHAM_PLOT(X,Y) \
1020 { GLuint *ptr4 = PIXELADDR4(X,Y); *ptr4 = pixel; }
1021
1022 BRESENHAM( x1, y1, x2, y2 );
1023
1024 #undef BRESENHAM_PLOT
1025 }
1026
1027 /*
1028 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
1029 */
1030 static void flat_color_z_line( GLcontext *ctx,
1031 GLuint v0, GLuint v1, GLuint pv )
1032 {
1033 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1034 struct vertex_buffer *VB = ctx->VB;
1035 GLint x1 = (GLint) VB->Win[v0][0], x2 = (GLint) VB->Win[v1][0];
1036 GLint y1 = (GLint) VB->Win[v0][1], y2 = (GLint) VB->Win[v1][1];
1037 GLint z1 = (GLint) (VB->Win[v0][2] + ctx->LineZoffset);
1038 GLint z2 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset);
1039 unsigned long pixel = PACK_RGBA( VB->Color[pv][0], VB->Color[pv][1],
1040 VB->Color[pv][2], 0 );
1041
1042 WINCLIP_X(x1,x2);
1043 WINCLIP_Y(y1,y2);
1044
1045 #define BRESENHAM_PLOT(X,Y,Z,ZBUF) \
1046 if (Z < *ZBUF) { \
1047 { GLuint *ptr4 = PIXELADDR4(X,Y); *ptr4 = pixel; } \
1048 *ZBUF = Z; \
1049 }
1050
1051 BRESENHAM_Z( ctx, x1, y1, z1, x2, y2, z2 );
1052
1053 #undef BRESENHAM_PLOT
1054 }
1055
1056 /*
1057 * Analyze context state to see if we can provide a fast line drawing
1058 * function, like those in lines.c. Otherwise, return NULL.
1059 */
1060 static line_func choose_line_function( GLcontext *ctx )
1061 {
1062 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1063
1064 if (ctx->Line.SmoothFlag) return NULL;
1065 if (ctx->Texture.Enabled) return NULL;
1066 if (ctx->Light.ShadeModel!=GL_FLAT) return NULL;
1067
1068 if (ctx->RasterMask==DEPTH_BIT
1069 && ctx->Depth.Func==GL_LESS
1070 && ctx->Depth.Mask==GL_TRUE
1071 && ctx->Line.Width==1.0F
1072 && ctx->Line.StippleFlag==GL_FALSE) {
1073 switch(osmesa->format) {
1074 case OSMESA_RGBA:
1075 case OSMESA_BGRA:
1076 case OSMESA_ARGB:
1077 return flat_color_z_line;
1078 break;
1079 default:
1080 return NULL;
1081 break;
1082 }
1083 }
1084 if (ctx->RasterMask==0
1085 && ctx->Line.Width==1.0F
1086 && ctx->Line.StippleFlag==GL_FALSE) {
1087 switch(osmesa->format) {
1088 case OSMESA_RGBA:
1089 case OSMESA_BGRA:
1090 case OSMESA_ARGB:
1091 return flat_color_line;
1092 break;
1093 default:
1094 return NULL;
1095 break;
1096 }
1097 }
1098 return NULL;
1099 }
1100
1101 /**********************************************************************/
1102 /***** Optimized triangle rendering *****/
1103 /**********************************************************************/
1104
1105
1106 /*
1107 * Smooth-shaded, z-less triangle, RGBA color.
1108 */
1109 static void smooth_color_z_triangle( GLcontext *ctx, GLuint v0, GLuint v1,
1110 GLuint v2, GLuint pv )
1111 {
1112 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1113 GLint rshift = osmesa->rshift;
1114 GLint gshift = osmesa->gshift;
1115 GLint bshift = osmesa->bshift;
1116 GLint ashift = osmesa->ashift;
1117 #define INTERP_Z 1
1118 #define INTERP_RGB 1
1119 #define INTERP_ALPHA 1
1120 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1121 { \
1122 GLint i, len = RIGHT-LEFT; \
1123 GLuint *img = PIXELADDR4(LEFT,Y); \
1124 for (i=0;i<len;i++,img++) { \
1125 GLdepth z = FixedToDepth(ffz); \
1126 if (z < zRow[i]) { \
1127 *img = PACK_RGBA2( FixedToInt(ffr), FixedToInt(ffg), \
1128 FixedToInt(ffb), FixedToInt(ffa) ); \
1129 zRow[i] = z; \
1130 } \
1131 ffr += fdrdx; ffg += fdgdx; ffb += fdbdx; ffa += fdadx;\
1132 ffz += fdzdx; \
1133 } \
1134 }
1135 #include "tritemp.h"
1136 }
1137
1138
1139
1140
1141 /*
1142 * Flat-shaded, z-less triangle, RGBA color.
1143 */
1144 static void flat_color_z_triangle( GLcontext *ctx, GLuint v0, GLuint v1,
1145 GLuint v2, GLuint pv )
1146 {
1147 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1148 #define INTERP_Z 1
1149 #define SETUP_CODE \
1150 GLubyte r = VB->Color[pv][0]; \
1151 GLubyte g = VB->Color[pv][1]; \
1152 GLubyte b = VB->Color[pv][2]; \
1153 GLubyte a = VB->Color[pv][3]; \
1154 GLuint pixel = PACK_RGBA(r,g,b,a);
1155
1156 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1157 { \
1158 GLint i, len = RIGHT-LEFT; \
1159 GLuint *img = PIXELADDR4(LEFT,Y); \
1160 for (i=0;i<len;i++,img++) { \
1161 GLdepth z = FixedToDepth(ffz); \
1162 if (z < zRow[i]) { \
1163 *img = pixel; \
1164 zRow[i] = z; \
1165 } \
1166 ffz += fdzdx; \
1167 } \
1168 }
1169 #include "tritemp.h"
1170 }
1171
1172
1173
1174 /*
1175 * Return pointer to an accelerated triangle function if possible.
1176 */
1177 static triangle_func choose_triangle_function( GLcontext *ctx )
1178 {
1179 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1180
1181 if ((osmesa->format==OSMESA_RGB)||(osmesa->format==OSMESA_BGR)) return NULL;
1182
1183 if (ctx->Polygon.SmoothFlag) return NULL;
1184 if (ctx->Polygon.StippleFlag) return NULL;
1185 if (ctx->Texture.Enabled) return NULL;
1186
1187 if (ctx->RasterMask==DEPTH_BIT
1188 && ctx->Depth.Func==GL_LESS
1189 && ctx->Depth.Mask==GL_TRUE
1190 && osmesa->format!=OSMESA_COLOR_INDEX) {
1191 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1192 return smooth_color_z_triangle;
1193 }
1194 else {
1195 return flat_color_z_triangle;
1196 }
1197 }
1198 return NULL;
1199 }
1200
1201
1202
1203 static void osmesa_setup_DD_pointers( GLcontext *ctx )
1204 {
1205 OSMesaContext osmesa = (OSMesaContext) ctx->DriverCtx;
1206
1207 ctx->Driver.UpdateState = osmesa_setup_DD_pointers;
1208
1209 ctx->Driver.SetBuffer = set_buffer;
1210 ctx->Driver.Color = set_color;
1211 ctx->Driver.Index = set_index;
1212 ctx->Driver.ClearIndex = clear_index;
1213 ctx->Driver.ClearColor = clear_color;
1214 ctx->Driver.Clear = clear;
1215
1216 ctx->Driver.GetBufferSize = buffer_size;
1217
1218 ctx->Driver.PointsFunc = NULL;
1219 ctx->Driver.LineFunc = choose_line_function( ctx );
1220 ctx->Driver.TriangleFunc = choose_triangle_function( ctx );
1221
1222 ctx->Driver.WriteColorSpan = write_color_span;
1223 ctx->Driver.WriteColorPixels = write_color_pixels;
1224 ctx->Driver.WriteIndexSpan = write_index_span;
1225 ctx->Driver.WriteMonocolorSpan = write_monocolor_span;
1226 ctx->Driver.WriteMonoindexSpan = write_monoindex_span;
1227 ctx->Driver.WriteIndexPixels = write_index_pixels;
1228 ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels;
1229 ctx->Driver.WriteMonoindexPixels = write_monoindex_pixels;
1230
1231 ctx->Driver.ReadColorSpan = read_color_span;
1232 ctx->Driver.ReadIndexSpan = read_index_span;
1233 ctx->Driver.ReadColorPixels = read_color_pixels;
1234 ctx->Driver.ReadIndexPixels = read_index_pixels;
1235
1236 if ((osmesa->format==OSMESA_RGB)||(osmesa->format==OSMESA_BGR)) {
1237 ctx->Driver.WriteColorSpan = write_color_span3;
1238 ctx->Driver.WriteColorPixels = write_color_pixels3;
1239 ctx->Driver.WriteMonocolorSpan = write_monocolor_span3;
1240 ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels3;
1241
1242 ctx->Driver.ReadColorSpan = read_color_span3;
1243 ctx->Driver.ReadColorPixels = read_color_pixels3;
1244 }
1245
1246 }