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