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