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