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