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