dynamically allocate color table data, uses less memory
[mesa.git] / src / mesa / main / context.c
1 /* $Id: context.c,v 1.60 2000/04/17 17:57:04 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 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 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "alphabuf.h"
34 #include "clip.h"
35 #include "colortab.h"
36 #include "context.h"
37 #include "cva.h"
38 #include "depth.h"
39 #include "dlist.h"
40 #include "eval.h"
41 #include "enums.h"
42 #include "extensions.h"
43 #include "fog.h"
44 #include "get.h"
45 #include "glapi.h"
46 #include "glapinoop.h"
47 #include "glthread.h"
48 #include "hash.h"
49 #include "light.h"
50 #include "macros.h"
51 #include "matrix.h"
52 #include "mem.h"
53 #include "mmath.h"
54 #include "pb.h"
55 #include "pipeline.h"
56 #include "shade.h"
57 #include "simple_list.h"
58 #include "stencil.h"
59 #include "stages.h"
60 #include "state.h"
61 #include "translate.h"
62 #include "teximage.h"
63 #include "texobj.h"
64 #include "texstate.h"
65 #include "texture.h"
66 #include "types.h"
67 #include "varray.h"
68 #include "vb.h"
69 #include "vbcull.h"
70 #include "vbrender.h"
71 #include "vbxform.h"
72 #include "vertices.h"
73 #include "xform.h"
74 #endif
75
76
77
78 /**********************************************************************/
79 /***** Context and Thread management *****/
80 /**********************************************************************/
81
82
83 #if !defined(THREADS)
84
85 struct immediate *_mesa_CurrentInput = NULL;
86
87 #endif
88
89
90
91
92 /**********************************************************************/
93 /***** Profiling functions *****/
94 /**********************************************************************/
95
96 #ifdef PROFILE
97
98 #include <sys/times.h>
99 #include <sys/param.h>
100
101
102 /*
103 * Return system time in seconds.
104 * NOTE: this implementation may not be very portable!
105 */
106 GLdouble gl_time( void )
107 {
108 static GLdouble prev_time = 0.0;
109 static GLdouble time;
110 struct tms tm;
111 clock_t clk;
112
113 clk = times(&tm);
114
115 #ifdef CLK_TCK
116 time = (double)clk / (double)CLK_TCK;
117 #else
118 time = (double)clk / (double)HZ;
119 #endif
120
121 if (time>prev_time) {
122 prev_time = time;
123 return time;
124 }
125 else {
126 return prev_time;
127 }
128 }
129
130 /*
131 * Reset the timing/profiling counters
132 */
133 static void init_timings( GLcontext *ctx )
134 {
135 ctx->BeginEndCount = 0;
136 ctx->BeginEndTime = 0.0;
137 ctx->VertexCount = 0;
138 ctx->VertexTime = 0.0;
139 ctx->PointCount = 0;
140 ctx->PointTime = 0.0;
141 ctx->LineCount = 0;
142 ctx->LineTime = 0.0;
143 ctx->PolygonCount = 0;
144 ctx->PolygonTime = 0.0;
145 ctx->ClearCount = 0;
146 ctx->ClearTime = 0.0;
147 ctx->SwapCount = 0;
148 ctx->SwapTime = 0.0;
149 }
150
151
152 /*
153 * Print the accumulated timing/profiling data.
154 */
155 static void print_timings( GLcontext *ctx )
156 {
157 GLdouble beginendrate;
158 GLdouble vertexrate;
159 GLdouble pointrate;
160 GLdouble linerate;
161 GLdouble polygonrate;
162 GLdouble overhead;
163 GLdouble clearrate;
164 GLdouble swaprate;
165 GLdouble avgvertices;
166
167 if (ctx->BeginEndTime>0.0) {
168 beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
169 }
170 else {
171 beginendrate = 0.0;
172 }
173 if (ctx->VertexTime>0.0) {
174 vertexrate = ctx->VertexCount / ctx->VertexTime;
175 }
176 else {
177 vertexrate = 0.0;
178 }
179 if (ctx->PointTime>0.0) {
180 pointrate = ctx->PointCount / ctx->PointTime;
181 }
182 else {
183 pointrate = 0.0;
184 }
185 if (ctx->LineTime>0.0) {
186 linerate = ctx->LineCount / ctx->LineTime;
187 }
188 else {
189 linerate = 0.0;
190 }
191 if (ctx->PolygonTime>0.0) {
192 polygonrate = ctx->PolygonCount / ctx->PolygonTime;
193 }
194 else {
195 polygonrate = 0.0;
196 }
197 if (ctx->ClearTime>0.0) {
198 clearrate = ctx->ClearCount / ctx->ClearTime;
199 }
200 else {
201 clearrate = 0.0;
202 }
203 if (ctx->SwapTime>0.0) {
204 swaprate = ctx->SwapCount / ctx->SwapTime;
205 }
206 else {
207 swaprate = 0.0;
208 }
209
210 if (ctx->BeginEndCount>0) {
211 avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
212 }
213 else {
214 avgvertices = 0.0;
215 }
216
217 overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
218 - ctx->LineTime - ctx->PolygonTime;
219
220
221 printf(" Count Time (s) Rate (/s) \n");
222 printf("--------------------------------------------------------\n");
223 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
224 ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
225 printf(" vertexes transformed %7d %8.3f %10.3f\n",
226 ctx->VertexCount, ctx->VertexTime, vertexrate );
227 printf(" points rasterized %7d %8.3f %10.3f\n",
228 ctx->PointCount, ctx->PointTime, pointrate );
229 printf(" lines rasterized %7d %8.3f %10.3f\n",
230 ctx->LineCount, ctx->LineTime, linerate );
231 printf(" polygons rasterized %7d %8.3f %10.3f\n",
232 ctx->PolygonCount, ctx->PolygonTime, polygonrate );
233 printf(" overhead %8.3f\n", overhead );
234 printf("glClear %7d %8.3f %10.3f\n",
235 ctx->ClearCount, ctx->ClearTime, clearrate );
236 printf("SwapBuffers %7d %8.3f %10.3f\n",
237 ctx->SwapCount, ctx->SwapTime, swaprate );
238 printf("\n");
239
240 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
241 }
242 #endif
243
244
245
246
247
248 /**********************************************************************/
249 /***** GL Visual allocation/destruction *****/
250 /**********************************************************************/
251
252
253 /*
254 * Allocate a new GLvisual object.
255 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
256 * alphaFlag - alloc software alpha buffers?
257 * dbFlag - double buffering?
258 * stereoFlag - stereo buffer?
259 * depthBits - requested bits per depth buffer value
260 * Any value in [0, 32] is acceptable but the actual
261 * depth type will be GLushort or GLuint as needed.
262 * stencilBits - requested minimum bits per stencil buffer value
263 * accumBits - requested minimum bits per accum buffer component
264 * indexBits - number of bits per pixel if rgbFlag==GL_FALSE
265 * red/green/blue/alphaBits - number of bits per color component
266 * in frame buffer for RGB(A) mode.
267 * We always use 8 in core Mesa though.
268 * Return: pointer to new GLvisual or NULL if requested parameters can't
269 * be met.
270 */
271 GLvisual *
272 _mesa_create_visual( GLboolean rgbFlag,
273 GLboolean alphaFlag,
274 GLboolean dbFlag,
275 GLboolean stereoFlag,
276 GLint redBits,
277 GLint greenBits,
278 GLint blueBits,
279 GLint alphaBits,
280 GLint indexBits,
281 GLint depthBits,
282 GLint stencilBits,
283 GLint accumRedBits,
284 GLint accumGreenBits,
285 GLint accumBlueBits,
286 GLint accumAlphaBits,
287 GLint numSamples )
288 {
289 GLvisual *vis;
290
291 /* This is to catch bad values from device drivers not updated for
292 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
293 * bad value now (a 1-bit depth buffer!?!).
294 */
295 assert(depthBits == 0 || depthBits > 1);
296
297 if (depthBits < 0 || depthBits > 32) {
298 return NULL;
299 }
300 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
301 return NULL;
302 }
303 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
304 return NULL;
305 }
306 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
307 return NULL;
308 }
309 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
310 return NULL;
311 }
312 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
313 return NULL;
314 }
315
316 vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
317 if (!vis) {
318 return NULL;
319 }
320
321 vis->RGBAflag = rgbFlag;
322 vis->DBflag = dbFlag;
323 vis->StereoFlag = stereoFlag;
324 vis->RedBits = redBits;
325 vis->GreenBits = greenBits;
326 vis->BlueBits = blueBits;
327 vis->AlphaBits = alphaFlag ? (8 * sizeof(GLubyte)) : alphaBits;
328
329 vis->IndexBits = indexBits;
330 vis->DepthBits = depthBits;
331 vis->AccumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
332 vis->AccumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
333 vis->AccumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
334 vis->AccumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
335 vis->StencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
336
337 vis->SoftwareAlpha = alphaFlag;
338
339 if (depthBits == 0) {
340 /* Special case. Even if we don't have a depth buffer we need
341 * good values for DepthMax for Z vertex transformation purposes.
342 */
343 vis->DepthMax = 1;
344 vis->DepthMaxF = 1.0F;
345 }
346 else {
347 vis->DepthMax = (1 << depthBits) - 1;
348 vis->DepthMaxF = (GLfloat) vis->DepthMax;
349 }
350
351 return vis;
352 }
353
354
355 /* This function should no longer be used. Use _mesa_create_visual() instead */
356 GLvisual *gl_create_visual( GLboolean rgbFlag,
357 GLboolean alphaFlag,
358 GLboolean dbFlag,
359 GLboolean stereoFlag,
360 GLint depthBits,
361 GLint stencilBits,
362 GLint accumBits,
363 GLint indexBits,
364 GLint redBits,
365 GLint greenBits,
366 GLint blueBits,
367 GLint alphaBits )
368 {
369 return _mesa_create_visual(rgbFlag, alphaFlag, dbFlag, stereoFlag,
370 redBits, greenBits, blueBits, alphaBits,
371 indexBits, depthBits, stencilBits,
372 accumBits, accumBits, accumBits, accumBits, 0);
373 }
374
375
376 void
377 _mesa_destroy_visual( GLvisual *vis )
378 {
379 FREE(vis);
380 }
381
382
383 /* obsolete */
384 void gl_destroy_visual( GLvisual *vis )
385 {
386 _mesa_destroy_visual(vis);
387 }
388
389
390
391 /**********************************************************************/
392 /***** GL Framebuffer allocation/destruction *****/
393 /**********************************************************************/
394
395
396 /*
397 * Create a new framebuffer. A GLframebuffer is a struct which
398 * encapsulates the depth, stencil and accum buffers and related
399 * parameters.
400 * Input: visual - a GLvisual pointer
401 * softwareDepth - create/use a software depth buffer?
402 * softwareStencil - create/use a software stencil buffer?
403 * softwareAccum - create/use a software accum buffer?
404 * softwareAlpha - create/use a software alpha buffer?
405
406 * Return: pointer to new GLframebuffer struct or NULL if error.
407 */
408 GLframebuffer *gl_create_framebuffer( GLvisual *visual,
409 GLboolean softwareDepth,
410 GLboolean softwareStencil,
411 GLboolean softwareAccum,
412 GLboolean softwareAlpha )
413 {
414 GLframebuffer *buffer;
415
416 buffer = CALLOC_STRUCT(gl_frame_buffer);
417 if (!buffer) {
418 return NULL;
419 }
420
421 /* sanity checks */
422 if (softwareDepth ) {
423 assert(visual->DepthBits > 0);
424 }
425 if (softwareStencil) {
426 assert(visual->StencilBits > 0);
427 }
428 if (softwareAccum) {
429 assert(visual->RGBAflag);
430 assert(visual->AccumRedBits > 0);
431 assert(visual->AccumGreenBits > 0);
432 assert(visual->AccumBlueBits > 0);
433 }
434 if (softwareAlpha) {
435 assert(visual->RGBAflag);
436 assert(visual->AlphaBits > 0);
437 }
438
439 buffer->Visual = visual;
440 buffer->UseSoftwareDepthBuffer = softwareDepth;
441 buffer->UseSoftwareStencilBuffer = softwareStencil;
442 buffer->UseSoftwareAccumBuffer = softwareAccum;
443 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
444
445 return buffer;
446 }
447
448
449
450 /*
451 * Free a framebuffer struct and its buffers.
452 */
453 void gl_destroy_framebuffer( GLframebuffer *buffer )
454 {
455 if (buffer) {
456 if (buffer->DepthBuffer) {
457 FREE( buffer->DepthBuffer );
458 }
459 if (buffer->Accum) {
460 FREE( buffer->Accum );
461 }
462 if (buffer->Stencil) {
463 FREE( buffer->Stencil );
464 }
465 if (buffer->FrontLeftAlpha) {
466 FREE( buffer->FrontLeftAlpha );
467 }
468 if (buffer->BackLeftAlpha) {
469 FREE( buffer->BackLeftAlpha );
470 }
471 if (buffer->FrontRightAlpha) {
472 FREE( buffer->FrontRightAlpha );
473 }
474 if (buffer->BackRightAlpha) {
475 FREE( buffer->BackRightAlpha );
476 }
477 FREE(buffer);
478 }
479 }
480
481
482
483 /**********************************************************************/
484 /***** Context allocation, initialization, destroying *****/
485 /**********************************************************************/
486
487
488 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
489
490
491 /*
492 * This function just calls all the various one-time-init functions in Mesa.
493 */
494 static void one_time_init( void )
495 {
496 static GLboolean alreadyCalled = GL_FALSE;
497 _glthread_LOCK_MUTEX(OneTimeLock);
498 if (!alreadyCalled) {
499 /* do some implementation tests */
500 assert( sizeof(GLbyte) == 1 );
501 assert( sizeof(GLshort) >= 2 );
502 assert( sizeof(GLint) >= 4 );
503 assert( sizeof(GLubyte) == 1 );
504 assert( sizeof(GLushort) >= 2 );
505 assert( sizeof(GLuint) >= 4 );
506
507 gl_init_clip();
508 gl_init_eval();
509 _mesa_init_fog();
510 _mesa_init_math();
511 gl_init_lists();
512 gl_init_shade();
513 gl_init_texture();
514 gl_init_transformation();
515 gl_init_translate();
516 gl_init_vbrender();
517 gl_init_vbxform();
518 gl_init_vertices();
519
520 if (getenv("MESA_DEBUG")) {
521 _glapi_noop_enable_warnings(GL_TRUE);
522 }
523 else {
524 _glapi_noop_enable_warnings(GL_FALSE);
525 }
526
527 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
528 fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
529 #endif
530
531 alreadyCalled = GL_TRUE;
532 }
533 _glthread_UNLOCK_MUTEX(OneTimeLock);
534 }
535
536
537
538 /*
539 * Allocate and initialize a shared context state structure.
540 */
541 static struct gl_shared_state *alloc_shared_state( void )
542 {
543 GLuint d;
544 struct gl_shared_state *ss;
545 GLboolean outOfMemory;
546
547 ss = CALLOC_STRUCT(gl_shared_state);
548 if (!ss)
549 return NULL;
550
551 ss->DisplayList = _mesa_NewHashTable();
552
553 ss->TexObjects = _mesa_NewHashTable();
554
555 /* Default Texture objects */
556 outOfMemory = GL_FALSE;
557 for (d = 1 ; d <= 3 ; d++) {
558 ss->DefaultD[d] = gl_alloc_texture_object(ss, 0, d);
559 if (!ss->DefaultD[d]) {
560 outOfMemory = GL_TRUE;
561 break;
562 }
563 ss->DefaultD[d]->RefCount++; /* don't free if not in use */
564 }
565
566 if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
567 /* Ran out of memory at some point. Free everything and return NULL */
568 if (ss->DisplayList)
569 _mesa_DeleteHashTable(ss->DisplayList);
570 if (ss->TexObjects)
571 _mesa_DeleteHashTable(ss->TexObjects);
572 if (ss->DefaultD[1])
573 gl_free_texture_object(ss, ss->DefaultD[1]);
574 if (ss->DefaultD[2])
575 gl_free_texture_object(ss, ss->DefaultD[2]);
576 if (ss->DefaultD[3])
577 gl_free_texture_object(ss, ss->DefaultD[3]);
578 FREE(ss);
579 return NULL;
580 }
581 else {
582 return ss;
583 }
584 }
585
586
587 /*
588 * Deallocate a shared state context and all children structures.
589 */
590 static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
591 {
592 /* Free display lists */
593 while (1) {
594 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
595 if (list) {
596 gl_destroy_list(ctx, list);
597 }
598 else {
599 break;
600 }
601 }
602 _mesa_DeleteHashTable(ss->DisplayList);
603
604 /* Free texture objects */
605 while (ss->TexObjectList)
606 {
607 if (ctx->Driver.DeleteTexture)
608 (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
609 /* this function removes from linked list too! */
610 gl_free_texture_object(ss, ss->TexObjectList);
611 }
612 _mesa_DeleteHashTable(ss->TexObjects);
613
614 FREE(ss);
615 }
616
617
618
619 /*
620 * Initialize the nth light. Note that the defaults for light 0 are
621 * different than the other lights.
622 */
623 static void init_light( struct gl_light *l, GLuint n )
624 {
625 make_empty_list( l );
626
627 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
628 if (n==0) {
629 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
630 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
631 }
632 else {
633 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
634 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
635 }
636 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
637 ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
638 l->SpotExponent = 0.0;
639 gl_compute_spot_exp_table( l );
640 l->SpotCutoff = 180.0;
641 l->CosCutoff = 0.0; /* KW: -ve values not admitted */
642 l->ConstantAttenuation = 1.0;
643 l->LinearAttenuation = 0.0;
644 l->QuadraticAttenuation = 0.0;
645 l->Enabled = GL_FALSE;
646 }
647
648
649
650 static void init_lightmodel( struct gl_lightmodel *lm )
651 {
652 ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
653 lm->LocalViewer = GL_FALSE;
654 lm->TwoSide = GL_FALSE;
655 lm->ColorControl = GL_SINGLE_COLOR;
656 }
657
658
659 static void init_material( struct gl_material *m )
660 {
661 ASSIGN_4V( m->Ambient, 0.2, 0.2, 0.2, 1.0 );
662 ASSIGN_4V( m->Diffuse, 0.8, 0.8, 0.8, 1.0 );
663 ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
664 ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
665 m->Shininess = 0.0;
666 m->AmbientIndex = 0;
667 m->DiffuseIndex = 1;
668 m->SpecularIndex = 1;
669 }
670
671
672
673 static void init_texture_unit( GLcontext *ctx, GLuint unit )
674 {
675 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
676
677 texUnit->EnvMode = GL_MODULATE;
678 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
679 texUnit->TexGenEnabled = 0;
680 texUnit->GenModeS = GL_EYE_LINEAR;
681 texUnit->GenModeT = GL_EYE_LINEAR;
682 texUnit->GenModeR = GL_EYE_LINEAR;
683 texUnit->GenModeQ = GL_EYE_LINEAR;
684 /* Yes, these plane coefficients are correct! */
685 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
686 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
687 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
688 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
689 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
690 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
691 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
692 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
693
694 texUnit->CurrentD[1] = ctx->Shared->DefaultD[1];
695 texUnit->CurrentD[2] = ctx->Shared->DefaultD[2];
696 texUnit->CurrentD[3] = ctx->Shared->DefaultD[3];
697 }
698
699
700 static void init_fallback_arrays( GLcontext *ctx )
701 {
702 struct gl_client_array *cl;
703 GLuint i;
704
705 cl = &ctx->Fallback.Normal;
706 cl->Size = 3;
707 cl->Type = GL_FLOAT;
708 cl->Stride = 0;
709 cl->StrideB = 0;
710 cl->Ptr = (void *) ctx->Current.Normal;
711 cl->Enabled = 1;
712
713 cl = &ctx->Fallback.Color;
714 cl->Size = 4;
715 cl->Type = GL_UNSIGNED_BYTE;
716 cl->Stride = 0;
717 cl->StrideB = 0;
718 cl->Ptr = (void *) ctx->Current.ByteColor;
719 cl->Enabled = 1;
720
721 cl = &ctx->Fallback.Index;
722 cl->Size = 1;
723 cl->Type = GL_UNSIGNED_INT;
724 cl->Stride = 0;
725 cl->StrideB = 0;
726 cl->Ptr = (void *) &ctx->Current.Index;
727 cl->Enabled = 1;
728
729 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
730 cl = &ctx->Fallback.TexCoord[i];
731 cl->Size = 4;
732 cl->Type = GL_FLOAT;
733 cl->Stride = 0;
734 cl->StrideB = 0;
735 cl->Ptr = (void *) ctx->Current.Texcoord[i];
736 cl->Enabled = 1;
737 }
738
739 cl = &ctx->Fallback.EdgeFlag;
740 cl->Size = 1;
741 cl->Type = GL_UNSIGNED_BYTE;
742 cl->Stride = 0;
743 cl->StrideB = 0;
744 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
745 cl->Enabled = 1;
746 }
747
748
749 /* Initialize a 1-D evaluator map */
750 static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
751 {
752 map->Order = 1;
753 map->u1 = 0.0;
754 map->u2 = 1.0;
755 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
756 if (map->Points) {
757 GLint i;
758 for (i=0;i<n;i++)
759 map->Points[i] = initial[i];
760 }
761 }
762
763
764 /* Initialize a 2-D evaluator map */
765 static void init_2d_map( struct gl_2d_map *map, int n, const float *initial )
766 {
767 map->Uorder = 1;
768 map->Vorder = 1;
769 map->u1 = 0.0;
770 map->u2 = 1.0;
771 map->v1 = 0.0;
772 map->v2 = 1.0;
773 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
774 if (map->Points) {
775 GLint i;
776 for (i=0;i<n;i++)
777 map->Points[i] = initial[i];
778 }
779 }
780
781
782 /*
783 * Initialize the attribute groups in a GLcontext.
784 */
785 static void init_attrib_groups( GLcontext *ctx )
786 {
787 GLuint i, j;
788
789 assert(ctx);
790
791 /* Constants, may be overriden by device drivers */
792 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
793 ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
794 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
795 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
796 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
797 ctx->Const.MinPointSize = MIN_POINT_SIZE;
798 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
799 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
800 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
801 ctx->Const.PointSizeGranularity = POINT_SIZE_GRANULARITY;
802 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
803 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
804 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
805 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
806 ctx->Const.LineWidthGranularity = LINE_WIDTH_GRANULARITY;
807 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
808 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
809
810 /* Modelview matrix */
811 gl_matrix_ctr( &ctx->ModelView );
812 gl_matrix_alloc_inv( &ctx->ModelView );
813
814 ctx->ModelViewStackDepth = 0;
815 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
816 gl_matrix_ctr( &ctx->ModelViewStack[i] );
817 gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
818 }
819
820 /* Projection matrix - need inv for user clipping in clip space*/
821 gl_matrix_ctr( &ctx->ProjectionMatrix );
822 gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
823
824 gl_matrix_ctr( &ctx->ModelProjectMatrix );
825 gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
826 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
827
828 ctx->ProjectionStackDepth = 0;
829 ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
830 ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
831
832 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
833 gl_matrix_ctr( &ctx->ProjectionStack[i] );
834 gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
835 }
836
837 /* Texture matrix */
838 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
839 gl_matrix_ctr( &ctx->TextureMatrix[i] );
840 ctx->TextureStackDepth[i] = 0;
841 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
842 ctx->TextureStack[i][j].inv = 0;
843 }
844 }
845
846 /* Color matrix */
847 gl_matrix_ctr(&ctx->ColorMatrix);
848 ctx->ColorStackDepth = 0;
849 for (j = 0; j < MAX_COLOR_STACK_DEPTH - 1; j++) {
850 gl_matrix_ctr(&ctx->ColorStack[j]);
851 }
852
853 /* Accumulate buffer group */
854 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
855
856 /* Color buffer group */
857 ctx->Color.IndexMask = 0xffffffff;
858 ctx->Color.ColorMask[0] = 0xff;
859 ctx->Color.ColorMask[1] = 0xff;
860 ctx->Color.ColorMask[2] = 0xff;
861 ctx->Color.ColorMask[3] = 0xff;
862 ctx->Color.SWmasking = GL_FALSE;
863 ctx->Color.ClearIndex = 0;
864 ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
865 ctx->Color.DrawBuffer = GL_FRONT;
866 ctx->Color.AlphaEnabled = GL_FALSE;
867 ctx->Color.AlphaFunc = GL_ALWAYS;
868 ctx->Color.AlphaRef = 0;
869 ctx->Color.BlendEnabled = GL_FALSE;
870 ctx->Color.BlendSrcRGB = GL_ONE;
871 ctx->Color.BlendDstRGB = GL_ZERO;
872 ctx->Color.BlendSrcA = GL_ONE;
873 ctx->Color.BlendDstA = GL_ZERO;
874 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
875 ctx->Color.BlendFunc = NULL; /* this pointer set only when needed */
876 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
877 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
878 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
879 ctx->Color.SWLogicOpEnabled = GL_FALSE;
880 ctx->Color.LogicOp = GL_COPY;
881 ctx->Color.DitherFlag = GL_TRUE;
882 ctx->Color.MultiDrawBuffer = GL_FALSE;
883
884 /* Current group */
885 ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
886 ctx->Current.Index = 1;
887 for (i=0; i<MAX_TEXTURE_UNITS; i++)
888 ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
889 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
890 ctx->Current.RasterDistance = 0.0;
891 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
892 ctx->Current.RasterIndex = 1;
893 for (i=0; i<MAX_TEXTURE_UNITS; i++)
894 ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
895 ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
896 ctx->Current.RasterPosValid = GL_TRUE;
897 ctx->Current.EdgeFlag = GL_TRUE;
898 ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
899 ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
900
901 ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
902 VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
903
904 init_fallback_arrays( ctx );
905
906 /* Depth buffer group */
907 ctx->Depth.Test = GL_FALSE;
908 ctx->Depth.Clear = 1.0;
909 ctx->Depth.Func = GL_LESS;
910 ctx->Depth.Mask = GL_TRUE;
911 ctx->Depth.OcclusionTest = GL_FALSE;
912
913 /* Evaluators group */
914 ctx->Eval.Map1Color4 = GL_FALSE;
915 ctx->Eval.Map1Index = GL_FALSE;
916 ctx->Eval.Map1Normal = GL_FALSE;
917 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
918 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
919 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
920 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
921 ctx->Eval.Map1Vertex3 = GL_FALSE;
922 ctx->Eval.Map1Vertex4 = GL_FALSE;
923 ctx->Eval.Map2Color4 = GL_FALSE;
924 ctx->Eval.Map2Index = GL_FALSE;
925 ctx->Eval.Map2Normal = GL_FALSE;
926 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
927 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
928 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
929 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
930 ctx->Eval.Map2Vertex3 = GL_FALSE;
931 ctx->Eval.Map2Vertex4 = GL_FALSE;
932 ctx->Eval.AutoNormal = GL_FALSE;
933 ctx->Eval.MapGrid1un = 1;
934 ctx->Eval.MapGrid1u1 = 0.0;
935 ctx->Eval.MapGrid1u2 = 1.0;
936 ctx->Eval.MapGrid2un = 1;
937 ctx->Eval.MapGrid2vn = 1;
938 ctx->Eval.MapGrid2u1 = 0.0;
939 ctx->Eval.MapGrid2u2 = 1.0;
940 ctx->Eval.MapGrid2v1 = 0.0;
941 ctx->Eval.MapGrid2v2 = 1.0;
942
943 /* Evaluator data */
944 {
945 static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
946 static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
947 static GLfloat index[1] = { 1.0 };
948 static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
949 static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
950
951 init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
952 init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
953 init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
954 init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
955 init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
956 init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
957 init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
958 init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
959 init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
960
961 init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
962 init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
963 init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
964 init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
965 init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
966 init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
967 init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
968 init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
969 init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
970 }
971
972 /* Fog group */
973 ctx->Fog.Enabled = GL_FALSE;
974 ctx->Fog.Mode = GL_EXP;
975 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
976 ctx->Fog.Index = 0.0;
977 ctx->Fog.Density = 1.0;
978 ctx->Fog.Start = 0.0;
979 ctx->Fog.End = 1.0;
980
981 /* Hint group */
982 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
983 ctx->Hint.PointSmooth = GL_DONT_CARE;
984 ctx->Hint.LineSmooth = GL_DONT_CARE;
985 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
986 ctx->Hint.Fog = GL_DONT_CARE;
987
988 ctx->Hint.AllowDrawWin = GL_TRUE;
989 ctx->Hint.AllowDrawFrg = GL_TRUE;
990 ctx->Hint.AllowDrawMem = GL_TRUE;
991 ctx->Hint.StrictLighting = GL_TRUE;
992
993 /* Histogram group */
994 ctx->Histogram.Width = 0;
995 ctx->Histogram.Format = GL_RGBA;
996 ctx->Histogram.Sink = GL_FALSE;
997 ctx->Histogram.RedSize = 0xffffffff;
998 ctx->Histogram.GreenSize = 0xffffffff;
999 ctx->Histogram.BlueSize = 0xffffffff;
1000 ctx->Histogram.AlphaSize = 0xffffffff;
1001 ctx->Histogram.LuminanceSize = 0xffffffff;
1002 for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
1003 ctx->Histogram.Count[i][0] = 0;
1004 ctx->Histogram.Count[i][1] = 0;
1005 ctx->Histogram.Count[i][2] = 0;
1006 ctx->Histogram.Count[i][3] = 0;
1007 }
1008
1009 /* Min/Max group */
1010 ctx->MinMax.Format = GL_RGBA;
1011 ctx->MinMax.Sink = GL_FALSE;
1012 ctx->MinMax.Min[RCOMP] = 1000; ctx->MinMax.Max[RCOMP] = -1000;
1013 ctx->MinMax.Min[GCOMP] = 1000; ctx->MinMax.Max[GCOMP] = -1000;
1014 ctx->MinMax.Min[BCOMP] = 1000; ctx->MinMax.Max[BCOMP] = -1000;
1015 ctx->MinMax.Min[ACOMP] = 1000; ctx->MinMax.Max[ACOMP] = -1000;
1016
1017
1018
1019 /* Pipeline */
1020 gl_pipeline_init( ctx );
1021 gl_cva_init( ctx );
1022
1023 /* Extensions */
1024 gl_extensions_ctr( ctx );
1025
1026 ctx->AllowVertexCull = CLIP_CULLED_BIT;
1027
1028 /* Lighting group */
1029 for (i=0;i<MAX_LIGHTS;i++) {
1030 init_light( &ctx->Light.Light[i], i );
1031 }
1032 make_empty_list( &ctx->Light.EnabledList );
1033
1034 init_lightmodel( &ctx->Light.Model );
1035 init_material( &ctx->Light.Material[0] );
1036 init_material( &ctx->Light.Material[1] );
1037 ctx->Light.ShadeModel = GL_SMOOTH;
1038 ctx->Light.Enabled = GL_FALSE;
1039 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
1040 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
1041 ctx->Light.ColorMaterialBitmask
1042 = gl_material_bitmask( ctx,
1043 GL_FRONT_AND_BACK,
1044 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
1045
1046 ctx->Light.ColorMaterialEnabled = GL_FALSE;
1047
1048 /* Lighting miscellaneous */
1049 ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
1050 make_empty_list( ctx->ShineTabList );
1051 for (i = 0 ; i < 10 ; i++) {
1052 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
1053 s->shininess = -1;
1054 s->refcount = 0;
1055 insert_at_tail( ctx->ShineTabList, s );
1056 }
1057 for (i = 0 ; i < 4 ; i++) {
1058 ctx->ShineTable[i] = ctx->ShineTabList->prev;
1059 ctx->ShineTable[i]->refcount++;
1060 }
1061
1062
1063 /* Line group */
1064 ctx->Line.SmoothFlag = GL_FALSE;
1065 ctx->Line.StippleFlag = GL_FALSE;
1066 ctx->Line.Width = 1.0;
1067 ctx->Line.StipplePattern = 0xffff;
1068 ctx->Line.StippleFactor = 1;
1069
1070 /* Display List group */
1071 ctx->List.ListBase = 0;
1072
1073 /* Pixel group */
1074 ctx->Pixel.RedBias = 0.0;
1075 ctx->Pixel.RedScale = 1.0;
1076 ctx->Pixel.GreenBias = 0.0;
1077 ctx->Pixel.GreenScale = 1.0;
1078 ctx->Pixel.BlueBias = 0.0;
1079 ctx->Pixel.BlueScale = 1.0;
1080 ctx->Pixel.AlphaBias = 0.0;
1081 ctx->Pixel.AlphaScale = 1.0;
1082 ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
1083 ctx->Pixel.DepthBias = 0.0;
1084 ctx->Pixel.DepthScale = 1.0;
1085 ctx->Pixel.IndexOffset = 0;
1086 ctx->Pixel.IndexShift = 0;
1087 ctx->Pixel.ZoomX = 1.0;
1088 ctx->Pixel.ZoomY = 1.0;
1089 ctx->Pixel.MapColorFlag = GL_FALSE;
1090 ctx->Pixel.MapStencilFlag = GL_FALSE;
1091 ctx->Pixel.MapStoSsize = 1;
1092 ctx->Pixel.MapItoIsize = 1;
1093 ctx->Pixel.MapItoRsize = 1;
1094 ctx->Pixel.MapItoGsize = 1;
1095 ctx->Pixel.MapItoBsize = 1;
1096 ctx->Pixel.MapItoAsize = 1;
1097 ctx->Pixel.MapRtoRsize = 1;
1098 ctx->Pixel.MapGtoGsize = 1;
1099 ctx->Pixel.MapBtoBsize = 1;
1100 ctx->Pixel.MapAtoAsize = 1;
1101 ctx->Pixel.MapStoS[0] = 0;
1102 ctx->Pixel.MapItoI[0] = 0;
1103 ctx->Pixel.MapItoR[0] = 0.0;
1104 ctx->Pixel.MapItoG[0] = 0.0;
1105 ctx->Pixel.MapItoB[0] = 0.0;
1106 ctx->Pixel.MapItoA[0] = 0.0;
1107 ctx->Pixel.MapItoR8[0] = 0;
1108 ctx->Pixel.MapItoG8[0] = 0;
1109 ctx->Pixel.MapItoB8[0] = 0;
1110 ctx->Pixel.MapItoA8[0] = 0;
1111 ctx->Pixel.MapRtoR[0] = 0.0;
1112 ctx->Pixel.MapGtoG[0] = 0.0;
1113 ctx->Pixel.MapBtoB[0] = 0.0;
1114 ctx->Pixel.MapAtoA[0] = 0.0;
1115 ctx->Pixel.HistogramEnabled = GL_FALSE;
1116 ctx->Pixel.MinMaxEnabled = GL_FALSE;
1117 ctx->Pixel.PixelTextureEnabled = GL_FALSE;
1118 ctx->Pixel.FragmentRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
1119 ctx->Pixel.FragmentAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
1120 ctx->Pixel.PostColorMatrixRedBias = 0.0;
1121 ctx->Pixel.PostColorMatrixRedScale = 1.0;
1122 ctx->Pixel.PostColorMatrixGreenBias = 0.0;
1123 ctx->Pixel.PostColorMatrixGreenScale = 1.0;
1124 ctx->Pixel.PostColorMatrixBlueBias = 0.0;
1125 ctx->Pixel.PostColorMatrixBlueScale = 1.0;
1126 ctx->Pixel.PostColorMatrixAlphaBias = 0.0;
1127 ctx->Pixel.PostColorMatrixAlphaScale = 1.0;
1128 ctx->Pixel.ColorTableScale[0] = 1.0F;
1129 ctx->Pixel.ColorTableScale[1] = 1.0F;
1130 ctx->Pixel.ColorTableScale[2] = 1.0F;
1131 ctx->Pixel.ColorTableScale[3] = 1.0F;
1132 ctx->Pixel.ColorTableBias[0] = 0.0F;
1133 ctx->Pixel.ColorTableBias[1] = 0.0F;
1134 ctx->Pixel.ColorTableBias[2] = 0.0F;
1135 ctx->Pixel.ColorTableBias[3] = 0.0F;
1136 ctx->Pixel.ColorTableEnabled = GL_FALSE;
1137 ctx->Pixel.PostConvolutionColorTableEnabled = GL_FALSE;
1138 ctx->Pixel.PostColorMatrixColorTableEnabled = GL_FALSE;
1139
1140 /* Point group */
1141 ctx->Point.SmoothFlag = GL_FALSE;
1142 ctx->Point.Size = 1.0;
1143 ctx->Point.Params[0] = 1.0;
1144 ctx->Point.Params[1] = 0.0;
1145 ctx->Point.Params[2] = 0.0;
1146 ctx->Point.Attenuated = GL_FALSE;
1147 ctx->Point.MinSize = 0.0;
1148 ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
1149 ctx->Point.Threshold = 1.0;
1150
1151 /* Polygon group */
1152 ctx->Polygon.CullFlag = GL_FALSE;
1153 ctx->Polygon.CullFaceMode = GL_BACK;
1154 ctx->Polygon.FrontFace = GL_CCW;
1155 ctx->Polygon.FrontBit = 0;
1156 ctx->Polygon.FrontMode = GL_FILL;
1157 ctx->Polygon.BackMode = GL_FILL;
1158 ctx->Polygon.Unfilled = GL_FALSE;
1159 ctx->Polygon.SmoothFlag = GL_FALSE;
1160 ctx->Polygon.StippleFlag = GL_FALSE;
1161 ctx->Polygon.OffsetFactor = 0.0F;
1162 ctx->Polygon.OffsetUnits = 0.0F;
1163 ctx->Polygon.OffsetPoint = GL_FALSE;
1164 ctx->Polygon.OffsetLine = GL_FALSE;
1165 ctx->Polygon.OffsetFill = GL_FALSE;
1166
1167 /* Polygon Stipple group */
1168 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
1169
1170 /* Scissor group */
1171 ctx->Scissor.Enabled = GL_FALSE;
1172 ctx->Scissor.X = 0;
1173 ctx->Scissor.Y = 0;
1174 ctx->Scissor.Width = 0;
1175 ctx->Scissor.Height = 0;
1176
1177 /* Stencil group */
1178 ctx->Stencil.Enabled = GL_FALSE;
1179 ctx->Stencil.Function = GL_ALWAYS;
1180 ctx->Stencil.FailFunc = GL_KEEP;
1181 ctx->Stencil.ZPassFunc = GL_KEEP;
1182 ctx->Stencil.ZFailFunc = GL_KEEP;
1183 ctx->Stencil.Ref = 0;
1184 ctx->Stencil.ValueMask = STENCIL_MAX;
1185 ctx->Stencil.Clear = 0;
1186 ctx->Stencil.WriteMask = STENCIL_MAX;
1187
1188 /* Texture group */
1189 ctx->Texture.CurrentUnit = 0; /* multitexture */
1190 ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
1191 ctx->Texture.Enabled = 0;
1192 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1193 init_texture_unit( ctx, i );
1194 _mesa_init_colortable(&ctx->Texture.Palette);
1195
1196 /* Transformation group */
1197 ctx->Transform.MatrixMode = GL_MODELVIEW;
1198 ctx->Transform.Normalize = GL_FALSE;
1199 ctx->Transform.RescaleNormals = GL_FALSE;
1200 for (i=0;i<MAX_CLIP_PLANES;i++) {
1201 ctx->Transform.ClipEnabled[i] = GL_FALSE;
1202 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1203 }
1204 ctx->Transform.AnyClip = GL_FALSE;
1205
1206 /* Viewport group */
1207 ctx->Viewport.X = 0;
1208 ctx->Viewport.Y = 0;
1209 ctx->Viewport.Width = 0;
1210 ctx->Viewport.Height = 0;
1211 ctx->Viewport.Near = 0.0;
1212 ctx->Viewport.Far = 1.0;
1213 gl_matrix_ctr(&ctx->Viewport.WindowMap);
1214
1215 #define Sz 10
1216 #define Tz 14
1217 ctx->Viewport.WindowMap.m[Sz] = 0.5 * ctx->Visual->DepthMaxF;
1218 ctx->Viewport.WindowMap.m[Tz] = 0.5 * ctx->Visual->DepthMaxF;
1219 #undef Sz
1220 #undef Tz
1221
1222 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1223 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
1224
1225 /* Vertex arrays */
1226 ctx->Array.Vertex.Size = 4;
1227 ctx->Array.Vertex.Type = GL_FLOAT;
1228 ctx->Array.Vertex.Stride = 0;
1229 ctx->Array.Vertex.StrideB = 0;
1230 ctx->Array.Vertex.Ptr = NULL;
1231 ctx->Array.Vertex.Enabled = GL_FALSE;
1232 ctx->Array.Normal.Type = GL_FLOAT;
1233 ctx->Array.Normal.Stride = 0;
1234 ctx->Array.Normal.StrideB = 0;
1235 ctx->Array.Normal.Ptr = NULL;
1236 ctx->Array.Normal.Enabled = GL_FALSE;
1237 ctx->Array.Color.Size = 4;
1238 ctx->Array.Color.Type = GL_FLOAT;
1239 ctx->Array.Color.Stride = 0;
1240 ctx->Array.Color.StrideB = 0;
1241 ctx->Array.Color.Ptr = NULL;
1242 ctx->Array.Color.Enabled = GL_FALSE;
1243 ctx->Array.Index.Type = GL_FLOAT;
1244 ctx->Array.Index.Stride = 0;
1245 ctx->Array.Index.StrideB = 0;
1246 ctx->Array.Index.Ptr = NULL;
1247 ctx->Array.Index.Enabled = GL_FALSE;
1248 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1249 ctx->Array.TexCoord[i].Size = 4;
1250 ctx->Array.TexCoord[i].Type = GL_FLOAT;
1251 ctx->Array.TexCoord[i].Stride = 0;
1252 ctx->Array.TexCoord[i].StrideB = 0;
1253 ctx->Array.TexCoord[i].Ptr = NULL;
1254 ctx->Array.TexCoord[i].Enabled = GL_FALSE;
1255 }
1256 ctx->Array.TexCoordInterleaveFactor = 1;
1257 ctx->Array.EdgeFlag.Stride = 0;
1258 ctx->Array.EdgeFlag.StrideB = 0;
1259 ctx->Array.EdgeFlag.Ptr = NULL;
1260 ctx->Array.EdgeFlag.Enabled = GL_FALSE;
1261 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1262
1263 /* Pixel transfer */
1264 ctx->Pack.Alignment = 4;
1265 ctx->Pack.RowLength = 0;
1266 ctx->Pack.ImageHeight = 0;
1267 ctx->Pack.SkipPixels = 0;
1268 ctx->Pack.SkipRows = 0;
1269 ctx->Pack.SkipImages = 0;
1270 ctx->Pack.SwapBytes = GL_FALSE;
1271 ctx->Pack.LsbFirst = GL_FALSE;
1272 ctx->Unpack.Alignment = 4;
1273 ctx->Unpack.RowLength = 0;
1274 ctx->Unpack.ImageHeight = 0;
1275 ctx->Unpack.SkipPixels = 0;
1276 ctx->Unpack.SkipRows = 0;
1277 ctx->Unpack.SkipImages = 0;
1278 ctx->Unpack.SwapBytes = GL_FALSE;
1279 ctx->Unpack.LsbFirst = GL_FALSE;
1280
1281 /* Feedback */
1282 ctx->Feedback.Type = GL_2D; /* TODO: verify */
1283 ctx->Feedback.Buffer = NULL;
1284 ctx->Feedback.BufferSize = 0;
1285 ctx->Feedback.Count = 0;
1286
1287 /* Selection/picking */
1288 ctx->Select.Buffer = NULL;
1289 ctx->Select.BufferSize = 0;
1290 ctx->Select.BufferCount = 0;
1291 ctx->Select.Hits = 0;
1292 ctx->Select.NameStackDepth = 0;
1293
1294 /* Optimized Accum buffer */
1295 ctx->IntegerAccumMode = GL_TRUE;
1296 ctx->IntegerAccumScaler = 0.0;
1297
1298 /* Renderer and client attribute stacks */
1299 ctx->AttribStackDepth = 0;
1300 ctx->ClientAttribStackDepth = 0;
1301
1302 /* Display list */
1303 ctx->CallDepth = 0;
1304 ctx->ExecuteFlag = GL_TRUE;
1305 ctx->CompileFlag = GL_FALSE;
1306 ctx->CurrentListPtr = NULL;
1307 ctx->CurrentBlock = NULL;
1308 ctx->CurrentListNum = 0;
1309 ctx->CurrentPos = 0;
1310
1311 /* Color tables */
1312 _mesa_init_colortable(&ctx->ColorTable);
1313 _mesa_init_colortable(&ctx->ProxyColorTable);
1314 _mesa_init_colortable(&ctx->PostConvolutionColorTable);
1315 _mesa_init_colortable(&ctx->ProxyPostConvolutionColorTable);
1316 _mesa_init_colortable(&ctx->PostColorMatrixColorTable);
1317 _mesa_init_colortable(&ctx->ProxyPostColorMatrixColorTable);
1318
1319 /* Miscellaneous */
1320 ctx->NewState = NEW_ALL;
1321 ctx->RenderMode = GL_RENDER;
1322 ctx->StippleCounter = 0;
1323 ctx->NeedNormals = GL_FALSE;
1324 ctx->DoViewportMapping = GL_TRUE;
1325
1326 ctx->NeedEyeCoords = GL_FALSE;
1327 ctx->NeedEyeNormals = GL_FALSE;
1328 ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
1329
1330 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1331
1332 ctx->CatchSignals = GL_TRUE;
1333 ctx->OcclusionResult = GL_FALSE;
1334 ctx->OcclusionResultSaved = GL_FALSE;
1335
1336 /* For debug/development only */
1337 ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
1338 ctx->FirstTimeCurrent = GL_TRUE;
1339
1340 /* Dither disable */
1341 ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
1342 if (ctx->NoDither) {
1343 if (getenv("MESA_DEBUG")) {
1344 fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
1345 }
1346 ctx->Color.DitherFlag = GL_FALSE;
1347 }
1348 }
1349
1350
1351
1352
1353 /*
1354 * Allocate the proxy textures. If we run out of memory part way through
1355 * the allocations clean up and return GL_FALSE.
1356 * Return: GL_TRUE=success, GL_FALSE=failure
1357 */
1358 static GLboolean alloc_proxy_textures( GLcontext *ctx )
1359 {
1360 GLboolean out_of_memory;
1361 GLint i;
1362
1363 ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
1364 if (!ctx->Texture.Proxy1D) {
1365 return GL_FALSE;
1366 }
1367
1368 ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
1369 if (!ctx->Texture.Proxy2D) {
1370 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1371 return GL_FALSE;
1372 }
1373
1374 ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
1375 if (!ctx->Texture.Proxy3D) {
1376 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1377 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1378 return GL_FALSE;
1379 }
1380
1381 out_of_memory = GL_FALSE;
1382 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1383 ctx->Texture.Proxy1D->Image[i] = _mesa_alloc_texture_image();
1384 ctx->Texture.Proxy2D->Image[i] = _mesa_alloc_texture_image();
1385 ctx->Texture.Proxy3D->Image[i] = _mesa_alloc_texture_image();
1386 if (!ctx->Texture.Proxy1D->Image[i]
1387 || !ctx->Texture.Proxy2D->Image[i]
1388 || !ctx->Texture.Proxy3D->Image[i]) {
1389 out_of_memory = GL_TRUE;
1390 }
1391 }
1392 if (out_of_memory) {
1393 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1394 if (ctx->Texture.Proxy1D->Image[i]) {
1395 _mesa_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
1396 }
1397 if (ctx->Texture.Proxy2D->Image[i]) {
1398 _mesa_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
1399 }
1400 if (ctx->Texture.Proxy3D->Image[i]) {
1401 _mesa_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
1402 }
1403 }
1404 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1405 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1406 gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
1407 return GL_FALSE;
1408 }
1409 else {
1410 return GL_TRUE;
1411 }
1412 }
1413
1414
1415
1416 /*
1417 * Initialize a GLcontext struct.
1418 */
1419 GLboolean gl_initialize_context_data( GLcontext *ctx,
1420 GLvisual *visual,
1421 GLcontext *share_list,
1422 void *driver_ctx,
1423 GLboolean direct )
1424 {
1425 (void) direct; /* not used */
1426
1427 /* misc one-time initializations */
1428 one_time_init();
1429
1430 ctx->DriverCtx = driver_ctx;
1431 ctx->Visual = visual;
1432 ctx->DrawBuffer = NULL;
1433 ctx->ReadBuffer = NULL;
1434
1435 ctx->VB = gl_vb_create_for_immediate( ctx );
1436 if (!ctx->VB) {
1437 FREE( ctx );
1438 return GL_FALSE;
1439 }
1440 ctx->input = ctx->VB->IM;
1441
1442 ctx->PB = gl_alloc_pb();
1443 if (!ctx->PB) {
1444 FREE( ctx->VB );
1445 FREE( ctx );
1446 return GL_FALSE;
1447 }
1448
1449 if (share_list) {
1450 /* share the group of display lists of another context */
1451 ctx->Shared = share_list->Shared;
1452 }
1453 else {
1454 /* allocate new group of display lists */
1455 ctx->Shared = alloc_shared_state();
1456 if (!ctx->Shared) {
1457 FREE(ctx->VB);
1458 FREE(ctx->PB);
1459 FREE(ctx);
1460 return GL_FALSE;
1461 }
1462 }
1463 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1464 ctx->Shared->RefCount++;
1465 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1466
1467 init_attrib_groups( ctx );
1468
1469 gl_reset_vb( ctx->VB );
1470 gl_reset_input( ctx );
1471
1472 if (visual->DBflag) {
1473 ctx->Color.DrawBuffer = GL_BACK;
1474 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
1475 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
1476 ctx->Pixel.ReadBuffer = GL_BACK;
1477 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
1478 }
1479 else {
1480 ctx->Color.DrawBuffer = GL_FRONT;
1481 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
1482 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
1483 ctx->Pixel.ReadBuffer = GL_FRONT;
1484 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
1485 }
1486
1487 #ifdef PROFILE
1488 init_timings( ctx );
1489 #endif
1490
1491 if (!alloc_proxy_textures(ctx)) {
1492 free_shared_state(ctx, ctx->Shared);
1493 FREE(ctx->VB);
1494 FREE(ctx->PB);
1495 FREE(ctx);
1496 return GL_FALSE;
1497 }
1498
1499 /* setup API dispatch tables */
1500 ctx->Exec = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1501 ctx->Save = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1502 if (!ctx->Exec || !ctx->Save) {
1503 free_shared_state(ctx, ctx->Shared);
1504 FREE(ctx->VB);
1505 FREE(ctx->PB);
1506 if (ctx->Exec)
1507 FREE(ctx->Exec);
1508 FREE(ctx);
1509 }
1510 _mesa_init_exec_table( ctx->Exec );
1511 _mesa_init_dlist_table( ctx->Save );
1512 ctx->CurrentDispatch = ctx->Exec;
1513
1514 return GL_TRUE;
1515 }
1516
1517
1518
1519 /*
1520 * Allocate and initialize a GLcontext structure.
1521 * Input: visual - a GLvisual pointer
1522 * sharelist - another context to share display lists with or NULL
1523 * driver_ctx - pointer to device driver's context state struct
1524 * Return: pointer to a new gl_context struct or NULL if error.
1525 */
1526 GLcontext *gl_create_context( GLvisual *visual,
1527 GLcontext *share_list,
1528 void *driver_ctx,
1529 GLboolean direct )
1530 {
1531 GLcontext *ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
1532 if (!ctx) {
1533 return NULL;
1534 }
1535
1536 if (gl_initialize_context_data(ctx, visual, share_list,
1537 driver_ctx, direct)) {
1538 return ctx;
1539 }
1540 else {
1541 FREE(ctx);
1542 return NULL;
1543 }
1544 }
1545
1546
1547
1548 /*
1549 * Free the data associated with the given context.
1550 * But don't free() the GLcontext struct itself!
1551 */
1552 void gl_free_context_data( GLcontext *ctx )
1553 {
1554 struct gl_shine_tab *s, *tmps;
1555 GLuint i, j;
1556
1557 /* if we're destroying the current context, unbind it first */
1558 if (ctx == gl_get_current_context()) {
1559 gl_make_current(NULL, NULL);
1560 }
1561
1562 #ifdef PROFILE
1563 if (getenv("MESA_PROFILE")) {
1564 print_timings( ctx );
1565 }
1566 #endif
1567
1568 gl_matrix_dtr( &ctx->ModelView );
1569 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
1570 gl_matrix_dtr( &ctx->ModelViewStack[i] );
1571 }
1572 gl_matrix_dtr( &ctx->ProjectionMatrix );
1573 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
1574 gl_matrix_dtr( &ctx->ProjectionStack[i] );
1575 }
1576 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1577 gl_matrix_dtr( &ctx->TextureMatrix[i] );
1578 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
1579 gl_matrix_dtr( &ctx->TextureStack[i][j] );
1580 }
1581 }
1582
1583 FREE( ctx->PB );
1584
1585 if (ctx->input != ctx->VB->IM)
1586 gl_immediate_free( ctx->input );
1587
1588 gl_vb_free( ctx->VB );
1589
1590 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1591 ctx->Shared->RefCount--;
1592 assert(ctx->Shared->RefCount >= 0);
1593 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1594 if (ctx->Shared->RefCount == 0) {
1595 /* free shared state */
1596 free_shared_state( ctx, ctx->Shared );
1597 }
1598
1599 foreach_s( s, tmps, ctx->ShineTabList ) {
1600 FREE( s );
1601 }
1602 FREE( ctx->ShineTabList );
1603
1604 /* Free proxy texture objects */
1605 gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
1606 gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
1607 gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
1608
1609 /* Free evaluator data */
1610 if (ctx->EvalMap.Map1Vertex3.Points)
1611 FREE( ctx->EvalMap.Map1Vertex3.Points );
1612 if (ctx->EvalMap.Map1Vertex4.Points)
1613 FREE( ctx->EvalMap.Map1Vertex4.Points );
1614 if (ctx->EvalMap.Map1Index.Points)
1615 FREE( ctx->EvalMap.Map1Index.Points );
1616 if (ctx->EvalMap.Map1Color4.Points)
1617 FREE( ctx->EvalMap.Map1Color4.Points );
1618 if (ctx->EvalMap.Map1Normal.Points)
1619 FREE( ctx->EvalMap.Map1Normal.Points );
1620 if (ctx->EvalMap.Map1Texture1.Points)
1621 FREE( ctx->EvalMap.Map1Texture1.Points );
1622 if (ctx->EvalMap.Map1Texture2.Points)
1623 FREE( ctx->EvalMap.Map1Texture2.Points );
1624 if (ctx->EvalMap.Map1Texture3.Points)
1625 FREE( ctx->EvalMap.Map1Texture3.Points );
1626 if (ctx->EvalMap.Map1Texture4.Points)
1627 FREE( ctx->EvalMap.Map1Texture4.Points );
1628
1629 if (ctx->EvalMap.Map2Vertex3.Points)
1630 FREE( ctx->EvalMap.Map2Vertex3.Points );
1631 if (ctx->EvalMap.Map2Vertex4.Points)
1632 FREE( ctx->EvalMap.Map2Vertex4.Points );
1633 if (ctx->EvalMap.Map2Index.Points)
1634 FREE( ctx->EvalMap.Map2Index.Points );
1635 if (ctx->EvalMap.Map2Color4.Points)
1636 FREE( ctx->EvalMap.Map2Color4.Points );
1637 if (ctx->EvalMap.Map2Normal.Points)
1638 FREE( ctx->EvalMap.Map2Normal.Points );
1639 if (ctx->EvalMap.Map2Texture1.Points)
1640 FREE( ctx->EvalMap.Map2Texture1.Points );
1641 if (ctx->EvalMap.Map2Texture2.Points)
1642 FREE( ctx->EvalMap.Map2Texture2.Points );
1643 if (ctx->EvalMap.Map2Texture3.Points)
1644 FREE( ctx->EvalMap.Map2Texture3.Points );
1645 if (ctx->EvalMap.Map2Texture4.Points)
1646 FREE( ctx->EvalMap.Map2Texture4.Points );
1647
1648 _mesa_free_colortable_data( &ctx->ColorTable );
1649 _mesa_free_colortable_data( &ctx->PostConvolutionColorTable );
1650 _mesa_free_colortable_data( &ctx->PostColorMatrixColorTable );
1651 _mesa_free_colortable_data( &ctx->Texture.Palette );
1652
1653 /* Free cache of immediate buffers. */
1654 while (ctx->nr_im_queued-- > 0) {
1655 struct immediate * next = ctx->freed_im_queue->next;
1656 FREE( ctx->freed_im_queue );
1657 ctx->freed_im_queue = next;
1658 }
1659 gl_extensions_dtr(ctx);
1660
1661 FREE(ctx->Exec);
1662 FREE(ctx->Save);
1663 }
1664
1665
1666
1667 /*
1668 * Destroy a GLcontext structure.
1669 */
1670 void gl_destroy_context( GLcontext *ctx )
1671 {
1672 if (ctx) {
1673 gl_free_context_data(ctx);
1674 FREE( (void *) ctx );
1675 }
1676 }
1677
1678
1679
1680 /*
1681 * Called by the driver after both the context and driver are fully
1682 * initialized. Currently just reads the config file.
1683 */
1684 void gl_context_initialize( GLcontext *ctx )
1685 {
1686 gl_read_config_file( ctx );
1687 }
1688
1689
1690
1691 /*
1692 * Copy attribute groups from one context to another.
1693 * Input: src - source context
1694 * dst - destination context
1695 * mask - bitwise OR of GL_*_BIT flags
1696 */
1697 void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1698 {
1699 if (mask & GL_ACCUM_BUFFER_BIT) {
1700 MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
1701 }
1702 if (mask & GL_COLOR_BUFFER_BIT) {
1703 MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
1704 }
1705 if (mask & GL_CURRENT_BIT) {
1706 MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
1707 }
1708 if (mask & GL_DEPTH_BUFFER_BIT) {
1709 MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
1710 }
1711 if (mask & GL_ENABLE_BIT) {
1712 /* no op */
1713 }
1714 if (mask & GL_EVAL_BIT) {
1715 MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
1716 }
1717 if (mask & GL_FOG_BIT) {
1718 MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
1719 }
1720 if (mask & GL_HINT_BIT) {
1721 MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
1722 }
1723 if (mask & GL_LIGHTING_BIT) {
1724 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
1725 /* gl_reinit_light_attrib( &dst->Light ); */
1726 }
1727 if (mask & GL_LINE_BIT) {
1728 MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
1729 }
1730 if (mask & GL_LIST_BIT) {
1731 MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
1732 }
1733 if (mask & GL_PIXEL_MODE_BIT) {
1734 MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
1735 }
1736 if (mask & GL_POINT_BIT) {
1737 MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
1738 }
1739 if (mask & GL_POLYGON_BIT) {
1740 MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
1741 }
1742 if (mask & GL_POLYGON_STIPPLE_BIT) {
1743 /* Use loop instead of MEMCPY due to problem with Portland Group's
1744 * C compiler. Reported by John Stone.
1745 */
1746 int i;
1747 for (i=0;i<32;i++) {
1748 dst->PolygonStipple[i] = src->PolygonStipple[i];
1749 }
1750 }
1751 if (mask & GL_SCISSOR_BIT) {
1752 MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
1753 }
1754 if (mask & GL_STENCIL_BUFFER_BIT) {
1755 MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
1756 }
1757 if (mask & GL_TEXTURE_BIT) {
1758 MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
1759 }
1760 if (mask & GL_TRANSFORM_BIT) {
1761 MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
1762 }
1763 if (mask & GL_VIEWPORT_BIT) {
1764 MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
1765 }
1766 }
1767
1768
1769 /*
1770 * Set the current context, binding the given frame buffer to the context.
1771 */
1772 void gl_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1773 {
1774 gl_make_current2( newCtx, buffer, buffer );
1775 }
1776
1777
1778 /*
1779 * Bind the given context to the given draw-buffer and read-buffer
1780 * and make it the current context for this thread.
1781 */
1782 void gl_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1783 GLframebuffer *readBuffer )
1784 {
1785 #if 0
1786 GLcontext *oldCtx = gl_get_context();
1787
1788 /* Flush the old context
1789 */
1790 if (oldCtx) {
1791 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx, "gl_make_current");
1792
1793 /* unbind frame buffers from context */
1794 if (oldCtx->DrawBuffer) {
1795 oldCtx->DrawBuffer = NULL;
1796 }
1797 if (oldCtx->ReadBuffer) {
1798 oldCtx->ReadBuffer = NULL;
1799 }
1800 }
1801 #endif
1802
1803 /* We call this function periodically (just here for now) in
1804 * order to detect when multithreading has begun.
1805 */
1806 _glapi_check_multithread();
1807
1808 _glapi_set_context((void *) newCtx);
1809 ASSERT(gl_get_current_context() == newCtx);
1810 if (newCtx) {
1811 SET_IMMEDIATE(newCtx, newCtx->input);
1812 _glapi_set_dispatch(newCtx->CurrentDispatch);
1813 }
1814 else {
1815 _glapi_set_dispatch(NULL); /* none current */
1816 }
1817
1818 if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
1819
1820 if (newCtx && drawBuffer && readBuffer) {
1821 /* TODO: check if newCtx and buffer's visual match??? */
1822 newCtx->DrawBuffer = drawBuffer;
1823 newCtx->ReadBuffer = readBuffer;
1824 newCtx->NewState = NEW_ALL; /* just to be safe */
1825 gl_update_state( newCtx );
1826 }
1827
1828 /* We can use this to help debug user's problems. Tell the to set
1829 * the MESA_INFO env variable before running their app. Then the
1830 * first time each context is made current we'll print some useful
1831 * information.
1832 */
1833 if (newCtx && newCtx->FirstTimeCurrent) {
1834 if (getenv("MESA_INFO")) {
1835 fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION));
1836 fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER));
1837 fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR));
1838 fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS));
1839 #if defined(THREADS)
1840 fprintf(stderr, "Mesa thread-safe: YES\n");
1841 #else
1842 fprintf(stderr, "Mesa thread-safe: NO\n");
1843 #endif
1844 #if defined(USE_X86_ASM)
1845 fprintf(stderr, "Mesa x86-optimized: YES\n");
1846 #else
1847 fprintf(stderr, "Mesa x86-optimized: NO\n");
1848 #endif
1849 }
1850 newCtx->FirstTimeCurrent = GL_FALSE;
1851 }
1852 }
1853
1854
1855
1856 /*
1857 * Return current context handle for the calling thread.
1858 * This isn't the fastest way to get the current context.
1859 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1860 */
1861 GLcontext *gl_get_current_context( void )
1862 {
1863 return (GLcontext *) _glapi_get_context();
1864 }
1865
1866
1867
1868 /*
1869 * This should be called by device drivers just before they do a
1870 * swapbuffers. Any pending rendering commands will be executed.
1871 */
1872 void
1873 _mesa_swapbuffers(GLcontext *ctx)
1874 {
1875 FLUSH_VB( ctx, "swap buffers" );
1876 }
1877
1878
1879
1880 /*
1881 * Return pointer to this context's current API dispatch table.
1882 * It'll either be the immediate-mode execute dispatcher or the
1883 * display list compile dispatcher.
1884 */
1885 struct _glapi_table *
1886 _mesa_get_dispatch(GLcontext *ctx)
1887 {
1888 return ctx->CurrentDispatch;
1889 }
1890
1891
1892
1893 /**********************************************************************/
1894 /***** Miscellaneous functions *****/
1895 /**********************************************************************/
1896
1897
1898 /*
1899 * This function is called when the Mesa user has stumbled into a code
1900 * path which may not be implemented fully or correctly.
1901 */
1902 void gl_problem( const GLcontext *ctx, const char *s )
1903 {
1904 fprintf( stderr, "Mesa implementation error: %s\n", s );
1905 fprintf( stderr, "Report to mesa-bugs@mesa3d.org\n" );
1906 (void) ctx;
1907 }
1908
1909
1910
1911 /*
1912 * This is called to inform the user that he or she has tried to do
1913 * something illogical or if there's likely a bug in their program
1914 * (like enabled depth testing without a depth buffer).
1915 */
1916 void gl_warning( const GLcontext *ctx, const char *s )
1917 {
1918 GLboolean debug;
1919 #ifdef DEBUG
1920 debug = GL_TRUE;
1921 #else
1922 if (getenv("MESA_DEBUG")) {
1923 debug = GL_TRUE;
1924 }
1925 else {
1926 debug = GL_FALSE;
1927 }
1928 #endif
1929 if (debug) {
1930 fprintf( stderr, "Mesa warning: %s\n", s );
1931 }
1932 (void) ctx;
1933 }
1934
1935
1936
1937 /*
1938 * Compile an error into current display list.
1939 */
1940 void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
1941 {
1942 if (ctx->CompileFlag)
1943 gl_save_error( ctx, error, s );
1944
1945 if (ctx->ExecuteFlag)
1946 gl_error( ctx, error, s );
1947 }
1948
1949
1950
1951 /*
1952 * This is Mesa's error handler. Normally, all that's done is the updating
1953 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1954 * environment variable "MESA_DEBUG" is defined then a real error message
1955 * is printed to stderr.
1956 * Input: error - the error value
1957 * s - a diagnostic string
1958 */
1959 void gl_error( GLcontext *ctx, GLenum error, const char *s )
1960 {
1961 GLboolean debug;
1962
1963 #ifdef DEBUG
1964 debug = GL_TRUE;
1965 #else
1966 if (getenv("MESA_DEBUG")) {
1967 debug = GL_TRUE;
1968 }
1969 else {
1970 debug = GL_FALSE;
1971 }
1972 #endif
1973
1974 if (debug) {
1975 char errstr[1000];
1976
1977 switch (error) {
1978 case GL_NO_ERROR:
1979 strcpy( errstr, "GL_NO_ERROR" );
1980 break;
1981 case GL_INVALID_VALUE:
1982 strcpy( errstr, "GL_INVALID_VALUE" );
1983 break;
1984 case GL_INVALID_ENUM:
1985 strcpy( errstr, "GL_INVALID_ENUM" );
1986 break;
1987 case GL_INVALID_OPERATION:
1988 strcpy( errstr, "GL_INVALID_OPERATION" );
1989 break;
1990 case GL_STACK_OVERFLOW:
1991 strcpy( errstr, "GL_STACK_OVERFLOW" );
1992 break;
1993 case GL_STACK_UNDERFLOW:
1994 strcpy( errstr, "GL_STACK_UNDERFLOW" );
1995 break;
1996 case GL_OUT_OF_MEMORY:
1997 strcpy( errstr, "GL_OUT_OF_MEMORY" );
1998 break;
1999 default:
2000 strcpy( errstr, "unknown" );
2001 break;
2002 }
2003 fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
2004 }
2005
2006 if (ctx->ErrorValue==GL_NO_ERROR) {
2007 ctx->ErrorValue = error;
2008 }
2009
2010 /* Call device driver's error handler, if any. This is used on the Mac. */
2011 if (ctx->Driver.Error) {
2012 (*ctx->Driver.Error)( ctx );
2013 }
2014 }
2015
2016
2017
2018 void
2019 _mesa_Finish( void )
2020 {
2021 GET_CURRENT_CONTEXT(ctx);
2022 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFinish");
2023 if (ctx->Driver.Finish) {
2024 (*ctx->Driver.Finish)( ctx );
2025 }
2026 }
2027
2028
2029
2030 void
2031 _mesa_Flush( void )
2032 {
2033 GET_CURRENT_CONTEXT(ctx);
2034 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFlush");
2035 if (ctx->Driver.Flush) {
2036 (*ctx->Driver.Flush)( ctx );
2037 }
2038 }