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