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