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