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