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