1 /* $Id: context.c,v 1.17 1999/10/30 08:22:45 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
28 /* $XFree86: xc/lib/GL/mesa/src/context.c,v 1.4 1999/04/04 00:20:21 dawes Exp $ */
31 * If multi-threading is enabled (-DTHREADS) then each thread has it's
32 * own rendering context. A thread obtains the pointer to its GLcontext
33 * with the gl_get_thread_context() function. Otherwise, the global
34 * pointer, CC, points to the current context used by all threads in
50 #include "GL/xf86glx.h"
62 #include "extensions.h"
77 #include "simple_list.h"
81 #include "translate.h"
96 #include "GL/xf86glx.h"
102 * Memory allocation functions. Called via the MALLOC, CALLOC and
103 * FREE macros when DEBUG symbol is defined.
104 * You might want to set breakpoints on these functions or plug in
105 * other memory allocation functions. The Mesa sources should only
106 * use the MALLOC and FREE macros (which could also be overriden).
108 * XXX these functions should probably go into a new glmemory.c file.
112 * Allocate memory (uninitialized)
114 void *gl_malloc(size_t bytes
)
116 return malloc(bytes
);
120 * Allocate memory and initialize to zero.
122 void *gl_calloc(size_t bytes
)
124 return calloc(1, bytes
);
130 void gl_free(void *ptr
)
136 /**********************************************************************/
137 /***** Context and Thread management *****/
138 /**********************************************************************/
143 #include "mthreads.h" /* Mesa platform independent threads interface */
145 static MesaTSD mesa_ctx_tsd
;
147 static void mesa_ctx_thread_init() {
148 MesaInitTSD(&mesa_ctx_tsd
);
151 GLcontext
*gl_get_thread_context( void ) {
152 return (GLcontext
*) MesaGetTSD(&mesa_ctx_tsd
);
155 static void set_thread_context( GLcontext
*ctx
) {
156 MesaSetTSD(&mesa_ctx_tsd
, ctx
, mesa_ctx_thread_init
);
162 /* One Current Context pointer for all threads in the address space */
163 GLcontext
*CC
= NULL
;
164 struct immediate
*CURRENT_INPUT
= NULL
;
171 /**********************************************************************/
172 /***** Profiling functions *****/
173 /**********************************************************************/
177 #include <sys/times.h>
178 #include <sys/param.h>
182 * Return system time in seconds.
183 * NOTE: this implementation may not be very portable!
185 GLdouble
gl_time( void )
187 static GLdouble prev_time
= 0.0;
188 static GLdouble time
;
195 time
= (double)clk
/ (double)CLK_TCK
;
197 time
= (double)clk
/ (double)HZ
;
200 if (time
>prev_time
) {
210 * Reset the timing/profiling counters
212 static void init_timings( GLcontext
*ctx
)
214 ctx
->BeginEndCount
= 0;
215 ctx
->BeginEndTime
= 0.0;
216 ctx
->VertexCount
= 0;
217 ctx
->VertexTime
= 0.0;
219 ctx
->PointTime
= 0.0;
222 ctx
->PolygonCount
= 0;
223 ctx
->PolygonTime
= 0.0;
225 ctx
->ClearTime
= 0.0;
232 * Print the accumulated timing/profiling data.
234 static void print_timings( GLcontext
*ctx
)
236 GLdouble beginendrate
;
240 GLdouble polygonrate
;
244 GLdouble avgvertices
;
246 if (ctx
->BeginEndTime
>0.0) {
247 beginendrate
= ctx
->BeginEndCount
/ ctx
->BeginEndTime
;
252 if (ctx
->VertexTime
>0.0) {
253 vertexrate
= ctx
->VertexCount
/ ctx
->VertexTime
;
258 if (ctx
->PointTime
>0.0) {
259 pointrate
= ctx
->PointCount
/ ctx
->PointTime
;
264 if (ctx
->LineTime
>0.0) {
265 linerate
= ctx
->LineCount
/ ctx
->LineTime
;
270 if (ctx
->PolygonTime
>0.0) {
271 polygonrate
= ctx
->PolygonCount
/ ctx
->PolygonTime
;
276 if (ctx
->ClearTime
>0.0) {
277 clearrate
= ctx
->ClearCount
/ ctx
->ClearTime
;
282 if (ctx
->SwapTime
>0.0) {
283 swaprate
= ctx
->SwapCount
/ ctx
->SwapTime
;
289 if (ctx
->BeginEndCount
>0) {
290 avgvertices
= (GLdouble
) ctx
->VertexCount
/ (GLdouble
) ctx
->BeginEndCount
;
296 overhead
= ctx
->BeginEndTime
- ctx
->VertexTime
- ctx
->PointTime
297 - ctx
->LineTime
- ctx
->PolygonTime
;
300 printf(" Count Time (s) Rate (/s) \n");
301 printf("--------------------------------------------------------\n");
302 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
303 ctx
->BeginEndCount
, ctx
->BeginEndTime
, beginendrate
);
304 printf(" vertexes transformed %7d %8.3f %10.3f\n",
305 ctx
->VertexCount
, ctx
->VertexTime
, vertexrate
);
306 printf(" points rasterized %7d %8.3f %10.3f\n",
307 ctx
->PointCount
, ctx
->PointTime
, pointrate
);
308 printf(" lines rasterized %7d %8.3f %10.3f\n",
309 ctx
->LineCount
, ctx
->LineTime
, linerate
);
310 printf(" polygons rasterized %7d %8.3f %10.3f\n",
311 ctx
->PolygonCount
, ctx
->PolygonTime
, polygonrate
);
312 printf(" overhead %8.3f\n", overhead
);
313 printf("glClear %7d %8.3f %10.3f\n",
314 ctx
->ClearCount
, ctx
->ClearTime
, clearrate
);
315 printf("SwapBuffers %7d %8.3f %10.3f\n",
316 ctx
->SwapCount
, ctx
->SwapTime
, swaprate
);
319 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices
);
327 /**********************************************************************/
328 /***** Context allocation, initialization, destroying *****/
329 /**********************************************************************/
333 * This function just calls all the various one-time-init functions in Mesa.
335 static void one_time_init( void )
337 static GLboolean alreadyCalled
= GL_FALSE
;
338 if (!alreadyCalled
) {
346 gl_init_transformation();
351 alreadyCalled
= GL_TRUE
;
353 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
354 fprintf(stderr
, "Mesa DEBUG build %s %s\n", __DATE__
, __TIME__
);
360 * Allocate and initialize a shared context state structure.
362 static struct gl_shared_state
*alloc_shared_state( void )
365 struct gl_shared_state
*ss
;
366 GLboolean outOfMemory
;
368 ss
= CALLOC_STRUCT(gl_shared_state
);
372 ss
->DisplayList
= NewHashTable();
374 ss
->TexObjects
= NewHashTable();
376 /* Default Texture objects */
377 outOfMemory
= GL_FALSE
;
378 for (d
= 1 ; d
<= 3 ; d
++) {
379 ss
->DefaultD
[d
] = gl_alloc_texture_object(ss
, 0, d
);
380 if (!ss
->DefaultD
[d
]) {
381 outOfMemory
= GL_TRUE
;
384 ss
->DefaultD
[d
]->RefCount
++; /* don't free if not in use */
387 if (!ss
->DisplayList
|| !ss
->TexObjects
|| outOfMemory
) {
388 /* Ran out of memory at some point. Free everything and return NULL */
390 DeleteHashTable(ss
->DisplayList
);
392 DeleteHashTable(ss
->TexObjects
);
394 gl_free_texture_object(ss
, ss
->DefaultD
[1]);
396 gl_free_texture_object(ss
, ss
->DefaultD
[2]);
398 gl_free_texture_object(ss
, ss
->DefaultD
[3]);
409 * Deallocate a shared state context and all children structures.
411 static void free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
413 /* Free display lists */
415 GLuint list
= HashFirstEntry(ss
->DisplayList
);
417 gl_destroy_list(ctx
, list
);
423 DeleteHashTable(ss
->DisplayList
);
425 /* Free texture objects */
426 while (ss
->TexObjectList
)
428 if (ctx
->Driver
.DeleteTexture
)
429 (*ctx
->Driver
.DeleteTexture
)( ctx
, ss
->TexObjectList
);
430 /* this function removes from linked list too! */
431 gl_free_texture_object(ss
, ss
->TexObjectList
);
433 DeleteHashTable(ss
->TexObjects
);
444 * Initialize the nth light. Note that the defaults for light 0 are
445 * different than the other lights.
447 static void init_light( struct gl_light
*l
, GLuint n
)
449 make_empty_list( l
);
451 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
453 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
454 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
457 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
458 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
460 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
461 ASSIGN_3V( l
->EyeDirection
, 0.0, 0.0, -1.0 );
462 l
->SpotExponent
= 0.0;
463 gl_compute_spot_exp_table( l
);
464 l
->SpotCutoff
= 180.0;
465 l
->CosCutoff
= 0.0; /* KW: -ve values not admitted */
466 l
->ConstantAttenuation
= 1.0;
467 l
->LinearAttenuation
= 0.0;
468 l
->QuadraticAttenuation
= 0.0;
469 l
->Enabled
= GL_FALSE
;
474 static void init_lightmodel( struct gl_lightmodel
*lm
)
476 ASSIGN_4V( lm
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
477 lm
->LocalViewer
= GL_FALSE
;
478 lm
->TwoSide
= GL_FALSE
;
479 lm
->ColorControl
= GL_SINGLE_COLOR
;
483 static void init_material( struct gl_material
*m
)
485 ASSIGN_4V( m
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
486 ASSIGN_4V( m
->Diffuse
, 0.8, 0.8, 0.8, 1.0 );
487 ASSIGN_4V( m
->Specular
, 0.0, 0.0, 0.0, 1.0 );
488 ASSIGN_4V( m
->Emission
, 0.0, 0.0, 0.0, 1.0 );
492 m
->SpecularIndex
= 1;
497 static void init_texture_unit( GLcontext
*ctx
, GLuint unit
)
499 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
501 texUnit
->EnvMode
= GL_MODULATE
;
502 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
503 texUnit
->TexGenEnabled
= 0;
504 texUnit
->GenModeS
= GL_EYE_LINEAR
;
505 texUnit
->GenModeT
= GL_EYE_LINEAR
;
506 texUnit
->GenModeR
= GL_EYE_LINEAR
;
507 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
508 /* Yes, these plane coefficients are correct! */
509 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
510 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
511 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
512 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
513 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
514 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
515 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
516 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
518 texUnit
->CurrentD
[1] = ctx
->Shared
->DefaultD
[1];
519 texUnit
->CurrentD
[2] = ctx
->Shared
->DefaultD
[2];
520 texUnit
->CurrentD
[3] = ctx
->Shared
->DefaultD
[3];
524 static void init_fallback_arrays( GLcontext
*ctx
)
526 struct gl_client_array
*cl
;
529 cl
= &ctx
->Fallback
.Normal
;
534 cl
->Ptr
= (void *) ctx
->Current
.Normal
;
537 cl
= &ctx
->Fallback
.Color
;
539 cl
->Type
= GL_UNSIGNED_BYTE
;
542 cl
->Ptr
= (void *) ctx
->Current
.ByteColor
;
545 cl
= &ctx
->Fallback
.Index
;
547 cl
->Type
= GL_UNSIGNED_INT
;
550 cl
->Ptr
= (void *) &ctx
->Current
.Index
;
553 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++) {
554 cl
= &ctx
->Fallback
.TexCoord
[i
];
559 cl
->Ptr
= (void *) ctx
->Current
.Texcoord
[i
];
563 cl
= &ctx
->Fallback
.EdgeFlag
;
565 cl
->Type
= GL_UNSIGNED_BYTE
;
568 cl
->Ptr
= (void *) &ctx
->Current
.EdgeFlag
;
572 /* Initialize a 1-D evaluator map */
573 static void init_1d_map( struct gl_1d_map
*map
, int n
, const float *initial
)
578 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
582 map
->Points
[i
] = initial
[i
];
584 map
->Retain
= GL_FALSE
;
588 /* Initialize a 2-D evaluator map */
589 static void init_2d_map( struct gl_2d_map
*map
, int n
, const float *initial
)
597 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
601 map
->Points
[i
] = initial
[i
];
603 map
->Retain
= GL_FALSE
;
609 * Initialize a gl_context structure to default values.
611 static void initialize_context( GLcontext
*ctx
)
616 /* Constants, may be overriden by device driver */
617 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
618 ctx
->Const
.MaxTextureSize
= 1 << (MAX_TEXTURE_LEVELS
- 1);
619 ctx
->Const
.MaxTextureUnits
= MAX_TEXTURE_UNITS
;
620 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
622 /* Modelview matrix */
623 gl_matrix_ctr( &ctx
->ModelView
);
624 gl_matrix_alloc_inv( &ctx
->ModelView
);
626 ctx
->ModelViewStackDepth
= 0;
627 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
628 gl_matrix_ctr( &ctx
->ModelViewStack
[i
] );
629 gl_matrix_alloc_inv( &ctx
->ModelViewStack
[i
] );
632 /* Projection matrix - need inv for user clipping in clip space*/
633 gl_matrix_ctr( &ctx
->ProjectionMatrix
);
634 gl_matrix_alloc_inv( &ctx
->ProjectionMatrix
);
636 gl_matrix_ctr( &ctx
->ModelProjectMatrix
);
637 gl_matrix_ctr( &ctx
->ModelProjectWinMatrix
);
638 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
640 ctx
->ProjectionStackDepth
= 0;
641 ctx
->NearFarStack
[0][0] = 1.0; /* These values seem weird by make */
642 ctx
->NearFarStack
[0][1] = 0.0; /* sense mathematically. */
644 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
645 gl_matrix_ctr( &ctx
->ProjectionStack
[i
] );
646 gl_matrix_alloc_inv( &ctx
->ProjectionStack
[i
] );
650 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++) {
651 gl_matrix_ctr( &ctx
->TextureMatrix
[i
] );
652 ctx
->TextureStackDepth
[i
] = 0;
653 for (j
= 0 ; j
< MAX_TEXTURE_STACK_DEPTH
; j
++) {
654 ctx
->TextureStack
[i
][j
].inv
= 0;
658 /* Accumulate buffer group */
659 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
661 /* Color buffer group */
662 ctx
->Color
.IndexMask
= 0xffffffff;
663 ctx
->Color
.ColorMask
[0] = 0xff;
664 ctx
->Color
.ColorMask
[1] = 0xff;
665 ctx
->Color
.ColorMask
[2] = 0xff;
666 ctx
->Color
.ColorMask
[3] = 0xff;
667 ctx
->Color
.SWmasking
= GL_FALSE
;
668 ctx
->Color
.ClearIndex
= 0;
669 ASSIGN_4V( ctx
->Color
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
670 ctx
->Color
.DrawBuffer
= GL_FRONT
;
671 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
672 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
673 ctx
->Color
.AlphaRef
= 0;
674 ctx
->Color
.BlendEnabled
= GL_FALSE
;
675 ctx
->Color
.BlendSrcRGB
= GL_ONE
;
676 ctx
->Color
.BlendDstRGB
= GL_ZERO
;
677 ctx
->Color
.BlendSrcA
= GL_ONE
;
678 ctx
->Color
.BlendDstA
= GL_ZERO
;
679 ctx
->Color
.BlendEquation
= GL_FUNC_ADD_EXT
;
680 ctx
->Color
.BlendFunc
= NULL
; /* this pointer set only when needed */
681 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
682 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
683 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
684 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
685 ctx
->Color
.LogicOp
= GL_COPY
;
686 ctx
->Color
.DitherFlag
= GL_TRUE
;
687 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
690 ASSIGN_4V( ctx
->Current
.ByteColor
, 255, 255, 255, 255);
691 ctx
->Current
.Index
= 1;
692 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
693 ASSIGN_4V( ctx
->Current
.Texcoord
[i
], 0.0, 0.0, 0.0, 1.0 );
694 ASSIGN_4V( ctx
->Current
.RasterPos
, 0.0, 0.0, 0.0, 1.0 );
695 ctx
->Current
.RasterDistance
= 0.0;
696 ASSIGN_4V( ctx
->Current
.RasterColor
, 1.0, 1.0, 1.0, 1.0 );
697 ctx
->Current
.RasterIndex
= 1;
698 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
699 ASSIGN_4V( ctx
->Current
.RasterMultiTexCoord
[i
], 0.0, 0.0, 0.0, 1.0 );
700 ctx
->Current
.RasterTexCoord
= ctx
->Current
.RasterMultiTexCoord
[0];
701 ctx
->Current
.RasterPosValid
= GL_TRUE
;
702 ctx
->Current
.EdgeFlag
= GL_TRUE
;
703 ASSIGN_3V( ctx
->Current
.Normal
, 0.0, 0.0, 1.0 );
704 ctx
->Current
.Primitive
= (GLenum
) (GL_POLYGON
+ 1);
706 ctx
->Current
.Flag
= (VERT_NORM
|VERT_INDEX
|VERT_RGBA
|VERT_EDGE
|
707 VERT_TEX0_1
|VERT_TEX1_1
|VERT_MATERIAL
);
709 init_fallback_arrays( ctx
);
711 /* Depth buffer group */
712 ctx
->Depth
.Test
= GL_FALSE
;
713 ctx
->Depth
.Clear
= 1.0;
714 ctx
->Depth
.Func
= GL_LESS
;
715 ctx
->Depth
.Mask
= GL_TRUE
;
717 /* Evaluators group */
718 ctx
->Eval
.Map1Color4
= GL_FALSE
;
719 ctx
->Eval
.Map1Index
= GL_FALSE
;
720 ctx
->Eval
.Map1Normal
= GL_FALSE
;
721 ctx
->Eval
.Map1TextureCoord1
= GL_FALSE
;
722 ctx
->Eval
.Map1TextureCoord2
= GL_FALSE
;
723 ctx
->Eval
.Map1TextureCoord3
= GL_FALSE
;
724 ctx
->Eval
.Map1TextureCoord4
= GL_FALSE
;
725 ctx
->Eval
.Map1Vertex3
= GL_FALSE
;
726 ctx
->Eval
.Map1Vertex4
= GL_FALSE
;
727 ctx
->Eval
.Map2Color4
= GL_FALSE
;
728 ctx
->Eval
.Map2Index
= GL_FALSE
;
729 ctx
->Eval
.Map2Normal
= GL_FALSE
;
730 ctx
->Eval
.Map2TextureCoord1
= GL_FALSE
;
731 ctx
->Eval
.Map2TextureCoord2
= GL_FALSE
;
732 ctx
->Eval
.Map2TextureCoord3
= GL_FALSE
;
733 ctx
->Eval
.Map2TextureCoord4
= GL_FALSE
;
734 ctx
->Eval
.Map2Vertex3
= GL_FALSE
;
735 ctx
->Eval
.Map2Vertex4
= GL_FALSE
;
736 ctx
->Eval
.AutoNormal
= GL_FALSE
;
737 ctx
->Eval
.MapGrid1un
= 1;
738 ctx
->Eval
.MapGrid1u1
= 0.0;
739 ctx
->Eval
.MapGrid1u2
= 1.0;
740 ctx
->Eval
.MapGrid2un
= 1;
741 ctx
->Eval
.MapGrid2vn
= 1;
742 ctx
->Eval
.MapGrid2u1
= 0.0;
743 ctx
->Eval
.MapGrid2u2
= 1.0;
744 ctx
->Eval
.MapGrid2v1
= 0.0;
745 ctx
->Eval
.MapGrid2v2
= 1.0;
749 static GLfloat vertex
[4] = { 0.0, 0.0, 0.0, 1.0 };
750 static GLfloat normal
[3] = { 0.0, 0.0, 1.0 };
751 static GLfloat index
[1] = { 1.0 };
752 static GLfloat color
[4] = { 1.0, 1.0, 1.0, 1.0 };
753 static GLfloat texcoord
[4] = { 0.0, 0.0, 0.0, 1.0 };
755 init_1d_map( &ctx
->EvalMap
.Map1Vertex3
, 3, vertex
);
756 init_1d_map( &ctx
->EvalMap
.Map1Vertex4
, 4, vertex
);
757 init_1d_map( &ctx
->EvalMap
.Map1Index
, 1, index
);
758 init_1d_map( &ctx
->EvalMap
.Map1Color4
, 4, color
);
759 init_1d_map( &ctx
->EvalMap
.Map1Normal
, 3, normal
);
760 init_1d_map( &ctx
->EvalMap
.Map1Texture1
, 1, texcoord
);
761 init_1d_map( &ctx
->EvalMap
.Map1Texture2
, 2, texcoord
);
762 init_1d_map( &ctx
->EvalMap
.Map1Texture3
, 3, texcoord
);
763 init_1d_map( &ctx
->EvalMap
.Map1Texture4
, 4, texcoord
);
765 init_2d_map( &ctx
->EvalMap
.Map2Vertex3
, 3, vertex
);
766 init_2d_map( &ctx
->EvalMap
.Map2Vertex4
, 4, vertex
);
767 init_2d_map( &ctx
->EvalMap
.Map2Index
, 1, index
);
768 init_2d_map( &ctx
->EvalMap
.Map2Color4
, 4, color
);
769 init_2d_map( &ctx
->EvalMap
.Map2Normal
, 3, normal
);
770 init_2d_map( &ctx
->EvalMap
.Map2Texture1
, 1, texcoord
);
771 init_2d_map( &ctx
->EvalMap
.Map2Texture2
, 2, texcoord
);
772 init_2d_map( &ctx
->EvalMap
.Map2Texture3
, 3, texcoord
);
773 init_2d_map( &ctx
->EvalMap
.Map2Texture4
, 4, texcoord
);
777 ctx
->Fog
.Enabled
= GL_FALSE
;
778 ctx
->Fog
.Mode
= GL_EXP
;
779 ASSIGN_4V( ctx
->Fog
.Color
, 0.0, 0.0, 0.0, 0.0 );
780 ctx
->Fog
.Index
= 0.0;
781 ctx
->Fog
.Density
= 1.0;
782 ctx
->Fog
.Start
= 0.0;
786 ctx
->Hint
.PerspectiveCorrection
= GL_DONT_CARE
;
787 ctx
->Hint
.PointSmooth
= GL_DONT_CARE
;
788 ctx
->Hint
.LineSmooth
= GL_DONT_CARE
;
789 ctx
->Hint
.PolygonSmooth
= GL_DONT_CARE
;
790 ctx
->Hint
.Fog
= GL_DONT_CARE
;
792 ctx
->Hint
.AllowDrawWin
= GL_TRUE
;
793 ctx
->Hint
.AllowDrawSpn
= GL_TRUE
;
794 ctx
->Hint
.AllowDrawMem
= GL_TRUE
;
795 ctx
->Hint
.StrictLighting
= GL_TRUE
;
798 gl_pipeline_init( ctx
);
802 gl_extensions_ctr( ctx
);
804 ctx
->AllowVertexCull
= CLIP_CULLED_BIT
;
807 for (i
=0;i
<MAX_LIGHTS
;i
++) {
808 init_light( &ctx
->Light
.Light
[i
], i
);
810 make_empty_list( &ctx
->Light
.EnabledList
);
812 init_lightmodel( &ctx
->Light
.Model
);
813 init_material( &ctx
->Light
.Material
[0] );
814 init_material( &ctx
->Light
.Material
[1] );
815 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
816 ctx
->Light
.Enabled
= GL_FALSE
;
817 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
818 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
819 ctx
->Light
.ColorMaterialBitmask
820 = gl_material_bitmask( ctx
,
822 GL_AMBIENT_AND_DIFFUSE
, ~0, 0 );
824 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
827 ctx
->Line
.SmoothFlag
= GL_FALSE
;
828 ctx
->Line
.StippleFlag
= GL_FALSE
;
829 ctx
->Line
.Width
= 1.0;
830 ctx
->Line
.StipplePattern
= 0xffff;
831 ctx
->Line
.StippleFactor
= 1;
833 /* Display List group */
834 ctx
->List
.ListBase
= 0;
837 ctx
->Pixel
.RedBias
= 0.0;
838 ctx
->Pixel
.RedScale
= 1.0;
839 ctx
->Pixel
.GreenBias
= 0.0;
840 ctx
->Pixel
.GreenScale
= 1.0;
841 ctx
->Pixel
.BlueBias
= 0.0;
842 ctx
->Pixel
.BlueScale
= 1.0;
843 ctx
->Pixel
.AlphaBias
= 0.0;
844 ctx
->Pixel
.AlphaScale
= 1.0;
845 ctx
->Pixel
.ScaleOrBiasRGBA
= GL_FALSE
;
846 ctx
->Pixel
.DepthBias
= 0.0;
847 ctx
->Pixel
.DepthScale
= 1.0;
848 ctx
->Pixel
.IndexOffset
= 0;
849 ctx
->Pixel
.IndexShift
= 0;
850 ctx
->Pixel
.ZoomX
= 1.0;
851 ctx
->Pixel
.ZoomY
= 1.0;
852 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
853 ctx
->Pixel
.MapStencilFlag
= GL_FALSE
;
854 ctx
->Pixel
.MapStoSsize
= 1;
855 ctx
->Pixel
.MapItoIsize
= 1;
856 ctx
->Pixel
.MapItoRsize
= 1;
857 ctx
->Pixel
.MapItoGsize
= 1;
858 ctx
->Pixel
.MapItoBsize
= 1;
859 ctx
->Pixel
.MapItoAsize
= 1;
860 ctx
->Pixel
.MapRtoRsize
= 1;
861 ctx
->Pixel
.MapGtoGsize
= 1;
862 ctx
->Pixel
.MapBtoBsize
= 1;
863 ctx
->Pixel
.MapAtoAsize
= 1;
864 ctx
->Pixel
.MapStoS
[0] = 0;
865 ctx
->Pixel
.MapItoI
[0] = 0;
866 ctx
->Pixel
.MapItoR
[0] = 0.0;
867 ctx
->Pixel
.MapItoG
[0] = 0.0;
868 ctx
->Pixel
.MapItoB
[0] = 0.0;
869 ctx
->Pixel
.MapItoA
[0] = 0.0;
870 ctx
->Pixel
.MapItoR8
[0] = 0;
871 ctx
->Pixel
.MapItoG8
[0] = 0;
872 ctx
->Pixel
.MapItoB8
[0] = 0;
873 ctx
->Pixel
.MapItoA8
[0] = 0;
874 ctx
->Pixel
.MapRtoR
[0] = 0.0;
875 ctx
->Pixel
.MapGtoG
[0] = 0.0;
876 ctx
->Pixel
.MapBtoB
[0] = 0.0;
877 ctx
->Pixel
.MapAtoA
[0] = 0.0;
880 ctx
->Point
.SmoothFlag
= GL_FALSE
;
881 ctx
->Point
.Size
= 1.0;
882 ctx
->Point
.Params
[0] = 1.0;
883 ctx
->Point
.Params
[1] = 0.0;
884 ctx
->Point
.Params
[2] = 0.0;
885 ctx
->Point
.Attenuated
= GL_FALSE
;
886 ctx
->Point
.MinSize
= 0.0;
887 ctx
->Point
.MaxSize
= (GLfloat
) MAX_POINT_SIZE
;
888 ctx
->Point
.Threshold
= 1.0;
891 ctx
->Polygon
.CullFlag
= GL_FALSE
;
892 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
893 ctx
->Polygon
.FrontFace
= GL_CCW
;
894 ctx
->Polygon
.FrontBit
= 0;
895 ctx
->Polygon
.FrontMode
= GL_FILL
;
896 ctx
->Polygon
.BackMode
= GL_FILL
;
897 ctx
->Polygon
.Unfilled
= GL_FALSE
;
898 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
899 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
900 ctx
->Polygon
.OffsetFactor
= 0.0F
;
901 ctx
->Polygon
.OffsetUnits
= 0.0F
;
902 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
903 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
904 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
906 /* Polygon Stipple group */
907 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );
910 ctx
->Scissor
.Enabled
= GL_FALSE
;
913 ctx
->Scissor
.Width
= 0;
914 ctx
->Scissor
.Height
= 0;
917 ctx
->Stencil
.Enabled
= GL_FALSE
;
918 ctx
->Stencil
.Function
= GL_ALWAYS
;
919 ctx
->Stencil
.FailFunc
= GL_KEEP
;
920 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
921 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
922 ctx
->Stencil
.Ref
= 0;
923 ctx
->Stencil
.ValueMask
= 0xff;
924 ctx
->Stencil
.Clear
= 0;
925 ctx
->Stencil
.WriteMask
= 0xff;
928 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
929 ctx
->Texture
.CurrentTransformUnit
= 0; /* multitexture */
930 ctx
->Texture
.Enabled
= 0;
932 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
933 init_texture_unit( ctx
, i
);
935 ctx
->Texture
.SharedPalette
= GL_FALSE
;
936 ctx
->Texture
.Palette
[0] = 255;
937 ctx
->Texture
.Palette
[1] = 255;
938 ctx
->Texture
.Palette
[2] = 255;
939 ctx
->Texture
.Palette
[3] = 255;
940 ctx
->Texture
.PaletteSize
= 1;
941 ctx
->Texture
.PaletteIntFormat
= GL_RGBA
;
942 ctx
->Texture
.PaletteFormat
= GL_RGBA
;
944 /* Transformation group */
945 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
946 ctx
->Transform
.Normalize
= GL_FALSE
;
947 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
948 for (i
=0;i
<MAX_CLIP_PLANES
;i
++) {
949 ctx
->Transform
.ClipEnabled
[i
] = GL_FALSE
;
950 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
952 ctx
->Transform
.AnyClip
= GL_FALSE
;
957 ctx
->Viewport
.Width
= 0;
958 ctx
->Viewport
.Height
= 0;
959 ctx
->Viewport
.Near
= 0.0;
960 ctx
->Viewport
.Far
= 1.0;
961 gl_matrix_ctr(&ctx
->Viewport
.WindowMap
);
965 ctx
->Viewport
.WindowMap
.m
[Sz
] = 0.5 * DEPTH_SCALE
;
966 ctx
->Viewport
.WindowMap
.m
[Tz
] = 0.5 * DEPTH_SCALE
;
970 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
971 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
974 ctx
->Array
.Vertex
.Size
= 4;
975 ctx
->Array
.Vertex
.Type
= GL_FLOAT
;
976 ctx
->Array
.Vertex
.Stride
= 0;
977 ctx
->Array
.Vertex
.StrideB
= 0;
978 ctx
->Array
.Vertex
.Ptr
= NULL
;
979 ctx
->Array
.Vertex
.Enabled
= GL_FALSE
;
980 ctx
->Array
.Normal
.Type
= GL_FLOAT
;
981 ctx
->Array
.Normal
.Stride
= 0;
982 ctx
->Array
.Normal
.StrideB
= 0;
983 ctx
->Array
.Normal
.Ptr
= NULL
;
984 ctx
->Array
.Normal
.Enabled
= GL_FALSE
;
985 ctx
->Array
.Color
.Size
= 4;
986 ctx
->Array
.Color
.Type
= GL_FLOAT
;
987 ctx
->Array
.Color
.Stride
= 0;
988 ctx
->Array
.Color
.StrideB
= 0;
989 ctx
->Array
.Color
.Ptr
= NULL
;
990 ctx
->Array
.Color
.Enabled
= GL_FALSE
;
991 ctx
->Array
.Index
.Type
= GL_FLOAT
;
992 ctx
->Array
.Index
.Stride
= 0;
993 ctx
->Array
.Index
.StrideB
= 0;
994 ctx
->Array
.Index
.Ptr
= NULL
;
995 ctx
->Array
.Index
.Enabled
= GL_FALSE
;
996 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
997 ctx
->Array
.TexCoord
[i
].Size
= 4;
998 ctx
->Array
.TexCoord
[i
].Type
= GL_FLOAT
;
999 ctx
->Array
.TexCoord
[i
].Stride
= 0;
1000 ctx
->Array
.TexCoord
[i
].StrideB
= 0;
1001 ctx
->Array
.TexCoord
[i
].Ptr
= NULL
;
1002 ctx
->Array
.TexCoord
[i
].Enabled
= GL_FALSE
;
1004 ctx
->Array
.TexCoordInterleaveFactor
= 1;
1005 ctx
->Array
.EdgeFlag
.Stride
= 0;
1006 ctx
->Array
.EdgeFlag
.StrideB
= 0;
1007 ctx
->Array
.EdgeFlag
.Ptr
= NULL
;
1008 ctx
->Array
.EdgeFlag
.Enabled
= GL_FALSE
;
1009 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
1011 /* Pixel transfer */
1012 ctx
->Pack
.Alignment
= 4;
1013 ctx
->Pack
.RowLength
= 0;
1014 ctx
->Pack
.ImageHeight
= 0;
1015 ctx
->Pack
.SkipPixels
= 0;
1016 ctx
->Pack
.SkipRows
= 0;
1017 ctx
->Pack
.SkipImages
= 0;
1018 ctx
->Pack
.SwapBytes
= GL_FALSE
;
1019 ctx
->Pack
.LsbFirst
= GL_FALSE
;
1020 ctx
->Unpack
.Alignment
= 4;
1021 ctx
->Unpack
.RowLength
= 0;
1022 ctx
->Unpack
.ImageHeight
= 0;
1023 ctx
->Unpack
.SkipPixels
= 0;
1024 ctx
->Unpack
.SkipRows
= 0;
1025 ctx
->Unpack
.SkipImages
= 0;
1026 ctx
->Unpack
.SwapBytes
= GL_FALSE
;
1027 ctx
->Unpack
.LsbFirst
= GL_FALSE
;
1030 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
1031 ctx
->Feedback
.Buffer
= NULL
;
1032 ctx
->Feedback
.BufferSize
= 0;
1033 ctx
->Feedback
.Count
= 0;
1035 /* Selection/picking */
1036 ctx
->Select
.Buffer
= NULL
;
1037 ctx
->Select
.BufferSize
= 0;
1038 ctx
->Select
.BufferCount
= 0;
1039 ctx
->Select
.Hits
= 0;
1040 ctx
->Select
.NameStackDepth
= 0;
1042 /* Optimized Accum buffer */
1043 ctx
->IntegerAccumMode
= GL_TRUE
;
1044 ctx
->IntegerAccumScaler
= 0.0;
1046 /* Renderer and client attribute stacks */
1047 ctx
->AttribStackDepth
= 0;
1048 ctx
->ClientAttribStackDepth
= 0;
1050 /*** Miscellaneous ***/
1051 ctx
->NewState
= NEW_ALL
;
1052 ctx
->RenderMode
= GL_RENDER
;
1053 ctx
->StippleCounter
= 0;
1054 ctx
->NeedNormals
= GL_FALSE
;
1055 ctx
->DoViewportMapping
= GL_TRUE
;
1057 ctx
->NeedEyeCoords
= GL_FALSE
;
1058 ctx
->NeedEyeNormals
= GL_FALSE
;
1059 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
1063 ctx
->ExecuteFlag
= GL_TRUE
;
1064 ctx
->CompileFlag
= GL_FALSE
;
1065 ctx
->CurrentListPtr
= NULL
;
1066 ctx
->CurrentBlock
= NULL
;
1067 ctx
->CurrentListNum
= 0;
1068 ctx
->CurrentPos
= 0;
1070 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1072 ctx
->CatchSignals
= GL_TRUE
;
1074 /* For debug/development only */
1075 ctx
->NoRaster
= getenv("MESA_NO_RASTER") ? GL_TRUE
: GL_FALSE
;
1077 /* Dither disable */
1078 ctx
->NoDither
= getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
1079 if (ctx
->NoDither
) {
1080 if (getenv("MESA_DEBUG")) {
1081 fprintf(stderr
, "MESA_NO_DITHER set - dithering disabled\n");
1083 ctx
->Color
.DitherFlag
= GL_FALSE
;
1091 * Allocate a new GLvisual object.
1092 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
1093 * alphaFlag - alloc software alpha buffers?
1094 * dbFlag - double buffering?
1095 * stereoFlag - stereo buffer?
1096 * depthFits - requested minimum bits per depth buffer value
1097 * stencilFits - requested minimum bits per stencil buffer value
1098 * accumFits - requested minimum bits per accum buffer component
1099 * indexFits - number of bits per pixel if rgbFlag==GL_FALSE
1100 * red/green/blue/alphaFits - number of bits per color component
1101 * in frame buffer for RGB(A) mode.
1102 * Return: pointer to new GLvisual or NULL if requested parameters can't
1105 GLvisual
*gl_create_visual( GLboolean rgbFlag
,
1106 GLboolean alphaFlag
,
1108 GLboolean stereoFlag
,
1120 if (depthBits
> (GLint
) (8*sizeof(GLdepth
))) {
1121 /* can't meet depth buffer requirements */
1124 if (stencilBits
> (GLint
) (8*sizeof(GLstencil
))) {
1125 /* can't meet stencil buffer requirements */
1128 if (accumBits
> (GLint
) (8*sizeof(GLaccum
))) {
1129 /* can't meet accum buffer requirements */
1133 vis
= (GLvisual
*) CALLOC( sizeof(GLvisual
) );
1138 vis
->RGBAflag
= rgbFlag
;
1139 vis
->DBflag
= dbFlag
;
1140 vis
->StereoFlag
= stereoFlag
;
1141 vis
->RedBits
= redBits
;
1142 vis
->GreenBits
= greenBits
;
1143 vis
->BlueBits
= blueBits
;
1144 vis
->AlphaBits
= alphaFlag
? 8*sizeof(GLubyte
) : alphaBits
;
1146 vis
->IndexBits
= indexBits
;
1147 vis
->DepthBits
= (depthBits
>0) ? 8*sizeof(GLdepth
) : 0;
1148 vis
->AccumBits
= (accumBits
>0) ? 8*sizeof(GLaccum
) : 0;
1149 vis
->StencilBits
= (stencilBits
>0) ? 8*sizeof(GLstencil
) : 0;
1151 vis
->SoftwareAlpha
= alphaFlag
;
1158 void gl_destroy_visual( GLvisual
*vis
)
1166 * Allocate the proxy textures. If we run out of memory part way through
1167 * the allocations clean up and return GL_FALSE.
1168 * Return: GL_TRUE=success, GL_FALSE=failure
1170 static GLboolean
alloc_proxy_textures( GLcontext
*ctx
)
1172 GLboolean out_of_memory
;
1175 ctx
->Texture
.Proxy1D
= gl_alloc_texture_object(NULL
, 0, 1);
1176 if (!ctx
->Texture
.Proxy1D
) {
1180 ctx
->Texture
.Proxy2D
= gl_alloc_texture_object(NULL
, 0, 2);
1181 if (!ctx
->Texture
.Proxy2D
) {
1182 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1186 ctx
->Texture
.Proxy3D
= gl_alloc_texture_object(NULL
, 0, 3);
1187 if (!ctx
->Texture
.Proxy3D
) {
1188 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1189 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1193 out_of_memory
= GL_FALSE
;
1194 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1195 ctx
->Texture
.Proxy1D
->Image
[i
] = gl_alloc_texture_image();
1196 ctx
->Texture
.Proxy2D
->Image
[i
] = gl_alloc_texture_image();
1197 ctx
->Texture
.Proxy3D
->Image
[i
] = gl_alloc_texture_image();
1198 if (!ctx
->Texture
.Proxy1D
->Image
[i
]
1199 || !ctx
->Texture
.Proxy2D
->Image
[i
]
1200 || !ctx
->Texture
.Proxy3D
->Image
[i
]) {
1201 out_of_memory
= GL_TRUE
;
1204 if (out_of_memory
) {
1205 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1206 if (ctx
->Texture
.Proxy1D
->Image
[i
]) {
1207 gl_free_texture_image(ctx
->Texture
.Proxy1D
->Image
[i
]);
1209 if (ctx
->Texture
.Proxy2D
->Image
[i
]) {
1210 gl_free_texture_image(ctx
->Texture
.Proxy2D
->Image
[i
]);
1212 if (ctx
->Texture
.Proxy3D
->Image
[i
]) {
1213 gl_free_texture_image(ctx
->Texture
.Proxy3D
->Image
[i
]);
1216 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1217 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1218 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy3D
);
1229 * Allocate and initialize a GLcontext structure.
1230 * Input: visual - a GLvisual pointer
1231 * sharelist - another context to share display lists with or NULL
1232 * driver_ctx - pointer to device driver's context state struct
1233 * Return: pointer to a new gl_context struct or NULL if error.
1235 GLcontext
*gl_create_context( GLvisual
*visual
,
1236 GLcontext
*share_list
,
1243 (void) direct
; /* not used */
1245 /* do some implementation tests */
1246 assert( sizeof(GLbyte
) == 1 );
1247 assert( sizeof(GLshort
) >= 2 );
1248 assert( sizeof(GLint
) >= 4 );
1249 assert( sizeof(GLubyte
) == 1 );
1250 assert( sizeof(GLushort
) >= 2 );
1251 assert( sizeof(GLuint
) >= 4 );
1253 /* misc one-time initializations */
1256 ctx
= (GLcontext
*) CALLOC( sizeof(GLcontext
) );
1261 ctx
->DriverCtx
= driver_ctx
;
1262 ctx
->Visual
= visual
;
1265 ctx
->VB
= gl_vb_create_for_immediate( ctx
);
1270 ctx
->input
= ctx
->VB
->IM
;
1272 ctx
->PB
= gl_alloc_pb();
1280 /* share the group of display lists of another context */
1281 ctx
->Shared
= share_list
->Shared
;
1284 /* allocate new group of display lists */
1285 ctx
->Shared
= alloc_shared_state();
1293 ctx
->Shared
->RefCount
++;
1295 initialize_context( ctx
);
1296 gl_reset_vb( ctx
->VB
);
1297 gl_reset_input( ctx
);
1300 ctx
->ShineTabList
= MALLOC_STRUCT( gl_shine_tab
);
1301 make_empty_list( ctx
->ShineTabList
);
1303 for (i
= 0 ; i
< 10 ; i
++) {
1304 struct gl_shine_tab
*s
= MALLOC_STRUCT( gl_shine_tab
);
1307 insert_at_tail( ctx
->ShineTabList
, s
);
1310 for (i
= 0 ; i
< 4 ; i
++) {
1311 ctx
->ShineTable
[i
] = ctx
->ShineTabList
->prev
;
1312 ctx
->ShineTable
[i
]->refcount
++;
1315 if (visual
->DBflag
) {
1316 ctx
->Color
.DrawBuffer
= GL_BACK
;
1317 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
1318 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
1319 ctx
->Pixel
.ReadBuffer
= GL_BACK
;
1320 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
1323 ctx
->Color
.DrawBuffer
= GL_FRONT
;
1324 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
1325 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
1326 ctx
->Pixel
.ReadBuffer
= GL_FRONT
;
1327 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
1331 /* Fill in some driver defaults now.
1333 ctx
->Driver
.AllocDepthBuffer
= gl_alloc_depth_buffer
;
1334 ctx
->Driver
.ReadDepthSpanFloat
= gl_read_depth_span_float
;
1335 ctx
->Driver
.ReadDepthSpanInt
= gl_read_depth_span_int
;
1340 init_timings( ctx
);
1343 #ifdef GL_VERSION_1_1
1344 if (!alloc_proxy_textures(ctx
)) {
1345 free_shared_state(ctx
, ctx
->Shared
);
1353 gl_init_api_function_pointers( ctx
);
1354 ctx
->API
= ctx
->Exec
; /* GL_EXECUTE is default */
1359 /* Just reads the config files...
1361 void gl_context_initialize( GLcontext
*ctx
)
1363 gl_read_config_file( ctx
);
1370 * Destroy a gl_context structure.
1372 void gl_destroy_context( GLcontext
*ctx
)
1377 struct gl_shine_tab
*s
, *tmps
;
1380 if (getenv("MESA_PROFILE")) {
1381 print_timings( ctx
);
1385 gl_matrix_dtr( &ctx
->ModelView
);
1386 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
1387 gl_matrix_dtr( &ctx
->ModelViewStack
[i
] );
1389 gl_matrix_dtr( &ctx
->ProjectionMatrix
);
1390 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
1391 gl_matrix_dtr( &ctx
->ProjectionStack
[i
] );
1396 if(ctx
->input
!= ctx
->VB
->IM
)
1397 gl_immediate_free( ctx
->input
);
1399 gl_vb_free( ctx
->VB
);
1401 ctx
->Shared
->RefCount
--;
1402 assert(ctx
->Shared
->RefCount
>=0);
1403 if (ctx
->Shared
->RefCount
==0) {
1404 /* free shared state */
1405 free_shared_state( ctx
, ctx
->Shared
);
1408 foreach_s( s
, tmps
, ctx
->ShineTabList
) {
1411 FREE( ctx
->ShineTabList
);
1413 /* Free proxy texture objects */
1414 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy1D
);
1415 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy2D
);
1416 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy3D
);
1418 /* Free evaluator data */
1419 if (ctx
->EvalMap
.Map1Vertex3
.Points
)
1420 FREE( ctx
->EvalMap
.Map1Vertex3
.Points
);
1421 if (ctx
->EvalMap
.Map1Vertex4
.Points
)
1422 FREE( ctx
->EvalMap
.Map1Vertex4
.Points
);
1423 if (ctx
->EvalMap
.Map1Index
.Points
)
1424 FREE( ctx
->EvalMap
.Map1Index
.Points
);
1425 if (ctx
->EvalMap
.Map1Color4
.Points
)
1426 FREE( ctx
->EvalMap
.Map1Color4
.Points
);
1427 if (ctx
->EvalMap
.Map1Normal
.Points
)
1428 FREE( ctx
->EvalMap
.Map1Normal
.Points
);
1429 if (ctx
->EvalMap
.Map1Texture1
.Points
)
1430 FREE( ctx
->EvalMap
.Map1Texture1
.Points
);
1431 if (ctx
->EvalMap
.Map1Texture2
.Points
)
1432 FREE( ctx
->EvalMap
.Map1Texture2
.Points
);
1433 if (ctx
->EvalMap
.Map1Texture3
.Points
)
1434 FREE( ctx
->EvalMap
.Map1Texture3
.Points
);
1435 if (ctx
->EvalMap
.Map1Texture4
.Points
)
1436 FREE( ctx
->EvalMap
.Map1Texture4
.Points
);
1438 if (ctx
->EvalMap
.Map2Vertex3
.Points
)
1439 FREE( ctx
->EvalMap
.Map2Vertex3
.Points
);
1440 if (ctx
->EvalMap
.Map2Vertex4
.Points
)
1441 FREE( ctx
->EvalMap
.Map2Vertex4
.Points
);
1442 if (ctx
->EvalMap
.Map2Index
.Points
)
1443 FREE( ctx
->EvalMap
.Map2Index
.Points
);
1444 if (ctx
->EvalMap
.Map2Color4
.Points
)
1445 FREE( ctx
->EvalMap
.Map2Color4
.Points
);
1446 if (ctx
->EvalMap
.Map2Normal
.Points
)
1447 FREE( ctx
->EvalMap
.Map2Normal
.Points
);
1448 if (ctx
->EvalMap
.Map2Texture1
.Points
)
1449 FREE( ctx
->EvalMap
.Map2Texture1
.Points
);
1450 if (ctx
->EvalMap
.Map2Texture2
.Points
)
1451 FREE( ctx
->EvalMap
.Map2Texture2
.Points
);
1452 if (ctx
->EvalMap
.Map2Texture3
.Points
)
1453 FREE( ctx
->EvalMap
.Map2Texture3
.Points
);
1454 if (ctx
->EvalMap
.Map2Texture4
.Points
)
1455 FREE( ctx
->EvalMap
.Map2Texture4
.Points
);
1457 /* Free cache of immediate buffers. */
1458 while (ctx
->nr_im_queued
-- > 0) {
1459 struct immediate
* next
= ctx
->freed_im_queue
->next
;
1460 FREE( ctx
->freed_im_queue
);
1461 ctx
->freed_im_queue
= next
;
1463 gl_extensions_dtr(ctx
);
1465 FREE( (void *) ctx
);
1470 CURRENT_INPUT
= NULL
;
1480 * Create a new framebuffer. A GLframebuffer is a struct which
1481 * encapsulates the depth, stencil and accum buffers and related
1483 * Input: visual - a GLvisual pointer
1484 * Return: pointer to new GLframebuffer struct or NULL if error.
1486 GLframebuffer
*gl_create_framebuffer( GLvisual
*visual
)
1488 GLframebuffer
*buffer
;
1490 buffer
= (GLframebuffer
*) CALLOC( sizeof(GLframebuffer
) );
1495 buffer
->Visual
= visual
;
1503 * Free a framebuffer struct and its buffers.
1505 void gl_destroy_framebuffer( GLframebuffer
*buffer
)
1508 if (buffer
->Depth
) {
1509 FREE( buffer
->Depth
);
1511 if (buffer
->Accum
) {
1512 FREE( buffer
->Accum
);
1514 if (buffer
->Stencil
) {
1515 FREE( buffer
->Stencil
);
1517 if (buffer
->FrontLeftAlpha
) {
1518 FREE( buffer
->FrontLeftAlpha
);
1520 if (buffer
->BackLeftAlpha
) {
1521 FREE( buffer
->BackLeftAlpha
);
1523 if (buffer
->FrontRightAlpha
) {
1524 FREE( buffer
->FrontRightAlpha
);
1526 if (buffer
->BackRightAlpha
) {
1527 FREE( buffer
->BackRightAlpha
);
1536 * Set the current context, binding the given frame buffer to the context.
1538 void gl_make_current( GLcontext
*ctx
, GLframebuffer
*buffer
)
1542 /* Flush the old context
1545 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(CC
, "gl_make_current");
1549 /* TODO: unbind old buffer from context? */
1550 set_thread_context( ctx
);
1552 if (CC
&& CC
->Buffer
) {
1553 /* unbind frame buffer from context */
1558 SET_IMMEDIATE(ctx
, ctx
->input
);
1562 if (MESA_VERBOSE
) fprintf(stderr
, "gl_make_current()\n");
1564 if (ctx
&& buffer
) {
1565 /* TODO: check if ctx and buffer's visual match??? */
1566 ctx
->Buffer
= buffer
; /* Bind the frame buffer to the context */
1567 ctx
->NewState
= NEW_ALL
; /* just to be safe */
1568 gl_update_state( ctx
);
1574 * Return current context handle.
1576 GLcontext
*gl_get_current_context( void )
1579 return gl_get_thread_context();
1588 * Copy attribute groups from one context to another.
1589 * Input: src - source context
1590 * dst - destination context
1591 * mask - bitwise OR of GL_*_BIT flags
1593 void gl_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1595 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1596 MEMCPY( &dst
->Accum
, &src
->Accum
, sizeof(struct gl_accum_attrib
) );
1598 if (mask
& GL_COLOR_BUFFER_BIT
) {
1599 MEMCPY( &dst
->Color
, &src
->Color
, sizeof(struct gl_colorbuffer_attrib
) );
1601 if (mask
& GL_CURRENT_BIT
) {
1602 MEMCPY( &dst
->Current
, &src
->Current
, sizeof(struct gl_current_attrib
) );
1604 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1605 MEMCPY( &dst
->Depth
, &src
->Depth
, sizeof(struct gl_depthbuffer_attrib
) );
1607 if (mask
& GL_ENABLE_BIT
) {
1610 if (mask
& GL_EVAL_BIT
) {
1611 MEMCPY( &dst
->Eval
, &src
->Eval
, sizeof(struct gl_eval_attrib
) );
1613 if (mask
& GL_FOG_BIT
) {
1614 MEMCPY( &dst
->Fog
, &src
->Fog
, sizeof(struct gl_fog_attrib
) );
1616 if (mask
& GL_HINT_BIT
) {
1617 MEMCPY( &dst
->Hint
, &src
->Hint
, sizeof(struct gl_hint_attrib
) );
1619 if (mask
& GL_LIGHTING_BIT
) {
1620 MEMCPY( &dst
->Light
, &src
->Light
, sizeof(struct gl_light_attrib
) );
1621 /* gl_reinit_light_attrib( &dst->Light ); */
1623 if (mask
& GL_LINE_BIT
) {
1624 MEMCPY( &dst
->Line
, &src
->Line
, sizeof(struct gl_line_attrib
) );
1626 if (mask
& GL_LIST_BIT
) {
1627 MEMCPY( &dst
->List
, &src
->List
, sizeof(struct gl_list_attrib
) );
1629 if (mask
& GL_PIXEL_MODE_BIT
) {
1630 MEMCPY( &dst
->Pixel
, &src
->Pixel
, sizeof(struct gl_pixel_attrib
) );
1632 if (mask
& GL_POINT_BIT
) {
1633 MEMCPY( &dst
->Point
, &src
->Point
, sizeof(struct gl_point_attrib
) );
1635 if (mask
& GL_POLYGON_BIT
) {
1636 MEMCPY( &dst
->Polygon
, &src
->Polygon
, sizeof(struct gl_polygon_attrib
) );
1638 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1639 /* Use loop instead of MEMCPY due to problem with Portland Group's
1640 * C compiler. Reported by John Stone.
1643 for (i
=0;i
<32;i
++) {
1644 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1647 if (mask
& GL_SCISSOR_BIT
) {
1648 MEMCPY( &dst
->Scissor
, &src
->Scissor
, sizeof(struct gl_scissor_attrib
) );
1650 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1651 MEMCPY( &dst
->Stencil
, &src
->Stencil
, sizeof(struct gl_stencil_attrib
) );
1653 if (mask
& GL_TEXTURE_BIT
) {
1654 MEMCPY( &dst
->Texture
, &src
->Texture
, sizeof(struct gl_texture_attrib
) );
1656 if (mask
& GL_TRANSFORM_BIT
) {
1657 MEMCPY( &dst
->Transform
, &src
->Transform
, sizeof(struct gl_transform_attrib
) );
1659 if (mask
& GL_VIEWPORT_BIT
) {
1660 MEMCPY( &dst
->Viewport
, &src
->Viewport
, sizeof(struct gl_viewport_attrib
) );
1667 * Someday a GLS library or OpenGL-like debugger may call this function
1668 * to register it's own set of API entry points.
1669 * Input: ctx - the context to set API pointers for
1670 * api - if NULL, restore original API pointers
1671 * else, set API function table to this table.
1673 void gl_set_api_table( GLcontext
*ctx
, const struct gl_api_table
*api
)
1676 MEMCPY( &ctx
->API
, api
, sizeof(struct gl_api_table
) );
1679 MEMCPY( &ctx
->API
, &ctx
->Exec
, sizeof(struct gl_api_table
) );
1686 /**********************************************************************/
1687 /***** Miscellaneous functions *****/
1688 /**********************************************************************/
1692 * This function is called when the Mesa user has stumbled into a code
1693 * path which may not be implemented fully or correctly.
1695 void gl_problem( const GLcontext
*ctx
, const char *s
)
1697 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
1698 fprintf( stderr
, "Report to mesa-bugs@mesa3d.org\n" );
1705 * This is called to inform the user that he or she has tried to do
1706 * something illogical or if there's likely a bug in their program
1707 * (like enabled depth testing without a depth buffer).
1709 void gl_warning( const GLcontext
*ctx
, const char *s
)
1715 if (getenv("MESA_DEBUG")) {
1723 fprintf( stderr
, "Mesa warning: %s\n", s
);
1730 void gl_compile_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1732 if (ctx
->CompileFlag
)
1733 gl_save_error( ctx
, error
, s
);
1735 if (ctx
->ExecuteFlag
)
1736 gl_error( ctx
, error
, s
);
1741 * This is Mesa's error handler. Normally, all that's done is the updating
1742 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1743 * environment variable "MESA_DEBUG" is defined then a real error message
1744 * is printed to stderr.
1745 * Input: error - the error value
1746 * s - a diagnostic string
1748 void gl_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1755 if (getenv("MESA_DEBUG")) {
1768 strcpy( errstr
, "GL_NO_ERROR" );
1770 case GL_INVALID_VALUE
:
1771 strcpy( errstr
, "GL_INVALID_VALUE" );
1773 case GL_INVALID_ENUM
:
1774 strcpy( errstr
, "GL_INVALID_ENUM" );
1776 case GL_INVALID_OPERATION
:
1777 strcpy( errstr
, "GL_INVALID_OPERATION" );
1779 case GL_STACK_OVERFLOW
:
1780 strcpy( errstr
, "GL_STACK_OVERFLOW" );
1782 case GL_STACK_UNDERFLOW
:
1783 strcpy( errstr
, "GL_STACK_UNDERFLOW" );
1785 case GL_OUT_OF_MEMORY
:
1786 strcpy( errstr
, "GL_OUT_OF_MEMORY" );
1789 strcpy( errstr
, "unknown" );
1792 fprintf( stderr
, "Mesa user error: %s in %s\n", errstr
, s
);
1795 if (ctx
->ErrorValue
==GL_NO_ERROR
) {
1796 ctx
->ErrorValue
= error
;
1799 /* Call device driver's error handler, if any. This is used on the Mac. */
1800 if (ctx
->Driver
.Error
) {
1801 (*ctx
->Driver
.Error
)( ctx
);
1808 * Execute a glGetError command
1810 GLenum
gl_GetError( GLcontext
*ctx
)
1812 GLenum e
= ctx
->ErrorValue
;
1814 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL( ctx
, "glGetError", (GLenum
) 0);
1816 if (MESA_VERBOSE
& VERBOSE_API
)
1817 fprintf(stderr
, "glGetError <-- %s\n", gl_lookup_enum_by_nr(e
));
1819 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1825 void gl_ResizeBuffersMESA( GLcontext
*ctx
)
1827 GLuint buf_width
, buf_height
;
1829 if (MESA_VERBOSE
& VERBOSE_API
)
1830 fprintf(stderr
, "glResizeBuffersMESA\n");
1832 /* ask device driver for size of output buffer */
1833 (*ctx
->Driver
.GetBufferSize
)( ctx
, &buf_width
, &buf_height
);
1835 /* see if size of device driver's color buffer (window) has changed */
1836 if (ctx
->Buffer
->Width
== (GLint
) buf_width
&&
1837 ctx
->Buffer
->Height
== (GLint
) buf_height
)
1840 ctx
->NewState
|= NEW_RASTER_OPS
; /* to update scissor / window bounds */
1842 /* save buffer size */
1843 ctx
->Buffer
->Width
= buf_width
;
1844 ctx
->Buffer
->Height
= buf_height
;
1846 /* Reallocate other buffers if needed. */
1847 if (ctx
->Visual
->DepthBits
>0) {
1848 /* reallocate depth buffer */
1849 (*ctx
->Driver
.AllocDepthBuffer
)( ctx
);
1851 if (ctx
->Visual
->StencilBits
>0) {
1852 /* reallocate stencil buffer */
1853 gl_alloc_stencil_buffer( ctx
);
1855 if (ctx
->Visual
->AccumBits
>0) {
1856 /* reallocate accum buffer */
1857 gl_alloc_accum_buffer( ctx
);
1859 if (ctx
->Visual
->SoftwareAlpha
) {
1860 gl_alloc_alpha_buffers( ctx
);
1867 /**********************************************************************/
1868 /***** State update logic *****/
1869 /**********************************************************************/
1873 * Since the device driver may or may not support pixel logic ops we
1874 * have to make some extensive tests to determine whether or not
1875 * software-implemented logic operations have to be used.
1877 static void update_pixel_logic( GLcontext
*ctx
)
1879 if (ctx
->Visual
->RGBAflag
) {
1880 /* RGBA mode blending w/ Logic Op */
1881 if (ctx
->Color
.ColorLogicOpEnabled
) {
1882 if (ctx
->Driver
.LogicOp
1883 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1884 /* Device driver can do logic, don't have to do it in software */
1885 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1888 /* Device driver can't do logic op so we do it in software */
1889 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1894 if (ctx
->Driver
.LogicOp
) {
1895 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1897 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1901 /* CI mode Logic Op */
1902 if (ctx
->Color
.IndexLogicOpEnabled
) {
1903 if (ctx
->Driver
.LogicOp
1904 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1905 /* Device driver can do logic, don't have to do it in software */
1906 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1909 /* Device driver can't do logic op so we do it in software */
1910 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1915 if (ctx
->Driver
.LogicOp
) {
1916 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1918 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1926 * Check if software implemented RGBA or Color Index masking is needed.
1928 static void update_pixel_masking( GLcontext
*ctx
)
1930 if (ctx
->Visual
->RGBAflag
) {
1931 GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1932 if (*colorMask
== 0xffffffff) {
1933 /* disable masking */
1934 if (ctx
->Driver
.ColorMask
) {
1935 (void) (*ctx
->Driver
.ColorMask
)( ctx
, GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1937 ctx
->Color
.SWmasking
= GL_FALSE
;
1940 /* Ask driver to do color masking, if it can't then
1943 GLboolean red
= ctx
->Color
.ColorMask
[RCOMP
] ? GL_TRUE
: GL_FALSE
;
1944 GLboolean green
= ctx
->Color
.ColorMask
[GCOMP
] ? GL_TRUE
: GL_FALSE
;
1945 GLboolean blue
= ctx
->Color
.ColorMask
[BCOMP
] ? GL_TRUE
: GL_FALSE
;
1946 GLboolean alpha
= ctx
->Color
.ColorMask
[ACOMP
] ? GL_TRUE
: GL_FALSE
;
1947 if (ctx
->Driver
.ColorMask
1948 && (*ctx
->Driver
.ColorMask
)( ctx
, red
, green
, blue
, alpha
)) {
1949 ctx
->Color
.SWmasking
= GL_FALSE
;
1952 ctx
->Color
.SWmasking
= GL_TRUE
;
1957 if (ctx
->Color
.IndexMask
==0xffffffff) {
1958 /* disable masking */
1959 if (ctx
->Driver
.IndexMask
) {
1960 (void) (*ctx
->Driver
.IndexMask
)( ctx
, 0xffffffff );
1962 ctx
->Color
.SWmasking
= GL_FALSE
;
1965 /* Ask driver to do index masking, if it can't then
1968 if (ctx
->Driver
.IndexMask
1969 && (*ctx
->Driver
.IndexMask
)( ctx
, ctx
->Color
.IndexMask
)) {
1970 ctx
->Color
.SWmasking
= GL_FALSE
;
1973 ctx
->Color
.SWmasking
= GL_TRUE
;
1980 static void update_fog_mode( GLcontext
*ctx
)
1982 int old_mode
= ctx
->FogMode
;
1984 if (ctx
->Fog
.Enabled
) {
1985 if (ctx
->Texture
.Enabled
)
1986 ctx
->FogMode
= FOG_FRAGMENT
;
1987 else if (ctx
->Hint
.Fog
== GL_NICEST
)
1988 ctx
->FogMode
= FOG_FRAGMENT
;
1990 ctx
->FogMode
= FOG_VERTEX
;
1992 if (ctx
->Driver
.GetParameteri
)
1993 if ((ctx
->Driver
.GetParameteri
)( ctx
, DD_HAVE_HARDWARE_FOG
))
1994 ctx
->FogMode
= FOG_FRAGMENT
;
1997 ctx
->FogMode
= FOG_NONE
;
2000 if (old_mode
!= ctx
->FogMode
)
2001 ctx
->NewState
|= NEW_FOG
;
2006 * Recompute the value of ctx->RasterMask, etc. according to
2007 * the current context.
2009 static void update_rasterflags( GLcontext
*ctx
)
2011 ctx
->RasterMask
= 0;
2013 if (ctx
->Color
.AlphaEnabled
) ctx
->RasterMask
|= ALPHATEST_BIT
;
2014 if (ctx
->Color
.BlendEnabled
) ctx
->RasterMask
|= BLEND_BIT
;
2015 if (ctx
->Depth
.Test
) ctx
->RasterMask
|= DEPTH_BIT
;
2016 if (ctx
->FogMode
==FOG_FRAGMENT
) ctx
->RasterMask
|= FOG_BIT
;
2017 if (ctx
->Color
.SWLogicOpEnabled
) ctx
->RasterMask
|= LOGIC_OP_BIT
;
2018 if (ctx
->Scissor
.Enabled
) ctx
->RasterMask
|= SCISSOR_BIT
;
2019 if (ctx
->Stencil
.Enabled
) ctx
->RasterMask
|= STENCIL_BIT
;
2020 if (ctx
->Color
.SWmasking
) ctx
->RasterMask
|= MASKING_BIT
;
2022 if (ctx
->Visual
->SoftwareAlpha
&& ctx
->Color
.ColorMask
[ACOMP
]
2023 && ctx
->Color
.DrawBuffer
!= GL_NONE
)
2024 ctx
->RasterMask
|= ALPHABUF_BIT
;
2026 if ( ctx
->Viewport
.X
<0
2027 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->Buffer
->Width
2028 || ctx
->Viewport
.Y
<0
2029 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->Buffer
->Height
) {
2030 ctx
->RasterMask
|= WINCLIP_BIT
;
2033 /* If we're not drawing to exactly one color buffer set the
2034 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
2035 * buffers or the RGBA or CI mask disables all writes.
2038 ctx
->TriangleCaps
&= ~DD_MULTIDRAW
;
2040 if (ctx
->Color
.MultiDrawBuffer
) {
2041 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2042 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2044 else if (ctx
->Color
.DrawBuffer
==GL_NONE
) {
2045 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2046 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2048 else if (ctx
->Visual
->RGBAflag
&& ctx
->Color
.ColorMask
==0) {
2049 /* all RGBA channels disabled */
2050 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2051 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2052 ctx
->Color
.DrawDestMask
= 0;
2054 else if (!ctx
->Visual
->RGBAflag
&& ctx
->Color
.IndexMask
==0) {
2055 /* all color index bits disabled */
2056 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2057 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2058 ctx
->Color
.DrawDestMask
= 0;
2063 void gl_print_state( const char *msg
, GLuint state
)
2066 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2069 (state
& NEW_LIGHTING
) ? "lighting, " : "",
2070 (state
& NEW_RASTER_OPS
) ? "raster-ops, " : "",
2071 (state
& NEW_TEXTURING
) ? "texturing, " : "",
2072 (state
& NEW_POLYGON
) ? "polygon, " : "",
2073 (state
& NEW_DRVSTATE0
) ? "driver-0, " : "",
2074 (state
& NEW_DRVSTATE1
) ? "driver-1, " : "",
2075 (state
& NEW_DRVSTATE2
) ? "driver-2, " : "",
2076 (state
& NEW_DRVSTATE3
) ? "driver-3, " : "",
2077 (state
& NEW_MODELVIEW
) ? "modelview, " : "",
2078 (state
& NEW_PROJECTION
) ? "projection, " : "",
2079 (state
& NEW_TEXTURE_MATRIX
) ? "texture-matrix, " : "",
2080 (state
& NEW_USER_CLIP
) ? "user-clip, " : "",
2081 (state
& NEW_TEXTURE_ENV
) ? "texture-env, " : "",
2082 (state
& NEW_CLIENT_STATE
) ? "client-state, " : "",
2083 (state
& NEW_FOG
) ? "fog, " : "",
2084 (state
& NEW_NORMAL_TRANSFORM
) ? "normal-transform, " : "",
2085 (state
& NEW_VIEWPORT
) ? "viewport, " : "",
2086 (state
& NEW_TEXTURE_ENABLE
) ? "texture-enable, " : "");
2089 void gl_print_enable_flags( const char *msg
, GLuint flags
)
2092 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s\n",
2095 (flags
& ENABLE_TEX0
) ? "tex-0, " : "",
2096 (flags
& ENABLE_TEX1
) ? "tex-1, " : "",
2097 (flags
& ENABLE_LIGHT
) ? "light, " : "",
2098 (flags
& ENABLE_FOG
) ? "fog, " : "",
2099 (flags
& ENABLE_USERCLIP
) ? "userclip, " : "",
2100 (flags
& ENABLE_TEXGEN0
) ? "tex-gen-0, " : "",
2101 (flags
& ENABLE_TEXGEN1
) ? "tex-gen-1, " : "",
2102 (flags
& ENABLE_TEXMAT0
) ? "tex-mat-0, " : "",
2103 (flags
& ENABLE_TEXMAT1
) ? "tex-mat-1, " : "",
2104 (flags
& ENABLE_NORMALIZE
) ? "normalize, " : "",
2105 (flags
& ENABLE_RESCALE
) ? "rescale, " : "");
2110 * If ctx->NewState is non-zero then this function MUST be called before
2111 * rendering any primitive. Basically, function pointers and miscellaneous
2112 * flags are updated to reflect the current state of the state machine.
2114 void gl_update_state( GLcontext
*ctx
)
2118 if (MESA_VERBOSE
& VERBOSE_STATE
)
2119 gl_print_state("", ctx
->NewState
);
2121 if (ctx
->NewState
& NEW_CLIENT_STATE
)
2122 gl_update_client_state( ctx
);
2124 if ((ctx
->NewState
& NEW_TEXTURE_ENABLE
) &&
2125 (ctx
->Enabled
& ENABLE_TEX_ANY
) != ctx
->Texture
.Enabled
)
2126 ctx
->NewState
|= NEW_TEXTURING
| NEW_RASTER_OPS
;
2128 if (ctx
->NewState
& NEW_TEXTURE_ENV
) {
2129 if (ctx
->Texture
.Unit
[0].EnvMode
== ctx
->Texture
.Unit
[0].LastEnvMode
&&
2130 ctx
->Texture
.Unit
[1].EnvMode
== ctx
->Texture
.Unit
[1].LastEnvMode
)
2131 ctx
->NewState
&= ~NEW_TEXTURE_ENV
;
2132 ctx
->Texture
.Unit
[0].LastEnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
2133 ctx
->Texture
.Unit
[1].LastEnvMode
= ctx
->Texture
.Unit
[1].EnvMode
;
2136 if ((ctx
->NewState
& ~(NEW_CLIENT_STATE
|NEW_TEXTURE_ENABLE
)) == 0) {
2138 if (MESA_VERBOSE
&VERBOSE_STATE
)
2139 fprintf(stderr
, "update_state: goto finished\n");
2144 if (ctx
->NewState
& NEW_TEXTURE_MATRIX
) {
2145 ctx
->Enabled
&= ~(ENABLE_TEXMAT0
|ENABLE_TEXMAT1
);
2147 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2148 if (ctx
->TextureMatrix
[i
].flags
& MAT_DIRTY_ALL_OVER
)
2150 gl_matrix_analyze( &ctx
->TextureMatrix
[i
] );
2151 ctx
->TextureMatrix
[i
].flags
&= ~MAT_DIRTY_DEPENDENTS
;
2153 if (ctx
->Texture
.Unit
[i
].Enabled
&&
2154 ctx
->TextureMatrix
[i
].type
!= MATRIX_IDENTITY
)
2155 ctx
->Enabled
|= ENABLE_TEXMAT0
<< i
;
2160 if (ctx
->NewState
& NEW_TEXTURING
) {
2161 ctx
->Texture
.NeedNormals
= GL_FALSE
;
2162 gl_update_dirty_texobjs(ctx
);
2163 ctx
->Enabled
&= ~(ENABLE_TEXGEN0
|ENABLE_TEXGEN1
);
2164 ctx
->Texture
.ReallyEnabled
= 0;
2166 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2167 if (ctx
->Texture
.Unit
[i
].Enabled
) {
2168 gl_update_texture_unit( ctx
, &ctx
->Texture
.Unit
[i
] );
2170 ctx
->Texture
.ReallyEnabled
|=
2171 ctx
->Texture
.Unit
[i
].ReallyEnabled
<<(i
*4);
2173 if (ctx
->Texture
.Unit
[i
].GenFlags
!= 0) {
2174 ctx
->Enabled
|= ENABLE_TEXGEN0
<< i
;
2176 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_NORMALS
)
2178 ctx
->Texture
.NeedNormals
= GL_TRUE
;
2179 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2182 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_EYE_COORD
)
2184 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2190 ctx
->Texture
.Enabled
= ctx
->Enabled
& ENABLE_TEX_ANY
;
2191 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2194 if (ctx
->NewState
& (NEW_RASTER_OPS
| NEW_LIGHTING
| NEW_FOG
)) {
2197 if (ctx
->NewState
& NEW_RASTER_OPS
) {
2198 update_pixel_logic(ctx
);
2199 update_pixel_masking(ctx
);
2200 update_fog_mode(ctx
);
2201 update_rasterflags(ctx
);
2202 if (ctx
->Driver
.Dither
) {
2203 (*ctx
->Driver
.Dither
)( ctx
, ctx
->Color
.DitherFlag
);
2206 /* Check if incoming colors can be modified during rasterization */
2207 if (ctx
->Fog
.Enabled
||
2208 ctx
->Texture
.Enabled
||
2209 ctx
->Color
.BlendEnabled
||
2210 ctx
->Color
.SWmasking
||
2211 ctx
->Color
.SWLogicOpEnabled
) {
2212 ctx
->MutablePixels
= GL_TRUE
;
2215 ctx
->MutablePixels
= GL_FALSE
;
2218 /* update scissor region */
2220 ctx
->Buffer
->Xmin
= 0;
2221 ctx
->Buffer
->Ymin
= 0;
2222 ctx
->Buffer
->Xmax
= ctx
->Buffer
->Width
-1;
2223 ctx
->Buffer
->Ymax
= ctx
->Buffer
->Height
-1;
2224 if (ctx
->Scissor
.Enabled
) {
2225 if (ctx
->Scissor
.X
> ctx
->Buffer
->Xmin
) {
2226 ctx
->Buffer
->Xmin
= ctx
->Scissor
.X
;
2228 if (ctx
->Scissor
.Y
> ctx
->Buffer
->Ymin
) {
2229 ctx
->Buffer
->Ymin
= ctx
->Scissor
.Y
;
2231 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1 < ctx
->Buffer
->Xmax
) {
2232 ctx
->Buffer
->Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1;
2234 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1 < ctx
->Buffer
->Ymax
) {
2235 ctx
->Buffer
->Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1;
2239 /* The driver isn't managing the depth buffer.
2241 if (ctx
->Driver
.AllocDepthBuffer
== gl_alloc_depth_buffer
)
2243 if (ctx
->Depth
.Mask
) {
2244 switch (ctx
->Depth
.Func
) {
2246 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_less
;
2247 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_less
;
2250 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_greater
;
2251 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_greater
;
2254 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2255 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2259 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2260 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2265 if (ctx
->NewState
& NEW_LIGHTING
) {
2266 ctx
->TriangleCaps
&= ~(DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2267 if (ctx
->Light
.Enabled
) {
2268 if (ctx
->Light
.Model
.TwoSide
)
2269 ctx
->TriangleCaps
|= (DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2270 gl_update_lighting(ctx
);
2275 if (ctx
->NewState
& (NEW_POLYGON
| NEW_LIGHTING
)) {
2277 ctx
->TriangleCaps
&= ~DD_TRI_CULL_FRONT_BACK
;
2279 if (ctx
->NewState
& NEW_POLYGON
) {
2280 /* Setup CullBits bitmask */
2281 if (ctx
->Polygon
.CullFlag
) {
2282 ctx
->backface_sign
= 1;
2283 switch(ctx
->Polygon
.CullFaceMode
) {
2285 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
2286 ctx
->backface_sign
= -1;
2287 ctx
->Polygon
.CullBits
= 1;
2290 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
2291 ctx
->backface_sign
= -1;
2292 ctx
->Polygon
.CullBits
= 2;
2295 case GL_FRONT_AND_BACK
:
2296 ctx
->backface_sign
= 0;
2297 ctx
->Polygon
.CullBits
= 0;
2298 ctx
->TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
2303 ctx
->Polygon
.CullBits
= 3;
2304 ctx
->backface_sign
= 0;
2307 /* Any Polygon offsets enabled? */
2308 ctx
->TriangleCaps
&= ~DD_TRI_OFFSET
;
2310 if (ctx
->Polygon
.OffsetPoint
||
2311 ctx
->Polygon
.OffsetLine
||
2312 ctx
->Polygon
.OffsetFill
)
2313 ctx
->TriangleCaps
|= DD_TRI_OFFSET
;
2315 /* reset Z offsets now */
2316 ctx
->PointZoffset
= 0.0;
2317 ctx
->LineZoffset
= 0.0;
2318 ctx
->PolygonZoffset
= 0.0;
2322 if (ctx
->NewState
& ~(NEW_CLIENT_STATE
|NEW_TEXTURE_ENABLE
|
2323 NEW_DRIVER_STATE
|NEW_USER_CLIP
|
2325 gl_update_clipmask(ctx
);
2327 if (ctx
->NewState
& (NEW_LIGHTING
|
2338 ctx
->IndirectTriangles
= ctx
->TriangleCaps
& ~ctx
->Driver
.TriangleCaps
;
2339 ctx
->IndirectTriangles
|= DD_SW_RASTERIZE
;
2341 if (MESA_VERBOSE
&VERBOSE_CULL
)
2342 gl_print_tri_caps("initial indirect tris", ctx
->IndirectTriangles
);
2344 ctx
->Driver
.PointsFunc
= NULL
;
2345 ctx
->Driver
.LineFunc
= NULL
;
2346 ctx
->Driver
.TriangleFunc
= NULL
;
2347 ctx
->Driver
.QuadFunc
= NULL
;
2348 ctx
->Driver
.RectFunc
= NULL
;
2349 ctx
->Driver
.RenderVBClippedTab
= NULL
;
2350 ctx
->Driver
.RenderVBCulledTab
= NULL
;
2351 ctx
->Driver
.RenderVBRawTab
= NULL
;
2354 * Here the driver sets up all the ctx->Driver function pointers to
2355 * it's specific, private functions.
2357 ctx
->Driver
.UpdateState(ctx
);
2359 if (MESA_VERBOSE
&VERBOSE_CULL
)
2360 gl_print_tri_caps("indirect tris", ctx
->IndirectTriangles
);
2363 * In case the driver didn't hook in an optimized point, line or
2364 * triangle function we'll now select "core/fallback" point, line
2365 * and triangle functions.
2367 if (ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) {
2368 gl_set_point_function(ctx
);
2369 gl_set_line_function(ctx
);
2370 gl_set_triangle_function(ctx
);
2371 gl_set_quad_function(ctx
);
2373 if ((ctx
->IndirectTriangles
&
2374 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
)) ==
2375 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
))
2376 ctx
->IndirectTriangles
&= ~DD_TRI_CULL
;
2379 if (MESA_VERBOSE
&VERBOSE_CULL
)
2380 gl_print_tri_caps("indirect tris 2", ctx
->IndirectTriangles
);
2382 gl_set_render_vb_function(ctx
);
2385 /* Should only be calc'd when !need_eye_coords and not culling.
2387 if (ctx
->NewState
& (NEW_MODELVIEW
|NEW_PROJECTION
)) {
2388 if (ctx
->NewState
& NEW_MODELVIEW
) {
2389 gl_matrix_analyze( &ctx
->ModelView
);
2390 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2393 if (ctx
->NewState
& NEW_PROJECTION
) {
2394 gl_matrix_analyze( &ctx
->ProjectionMatrix
);
2395 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2397 if (ctx
->Transform
.AnyClip
) {
2398 gl_update_userclip( ctx
);
2402 gl_calculate_model_project_matrix( ctx
);
2403 ctx
->ModelProjectWinMatrixUptodate
= 0;
2406 /* Figure out whether we can light in object space or not. If we
2407 * can, find the current positions of the lights in object space
2409 if ((ctx
->Enabled
& (ENABLE_POINT_ATTEN
| ENABLE_LIGHT
| ENABLE_FOG
|
2410 ENABLE_TEXGEN0
| ENABLE_TEXGEN1
)) &&
2411 (ctx
->NewState
& (NEW_LIGHTING
|
2419 GLboolean oldcoord
, oldnorm
;
2421 oldcoord
= ctx
->NeedEyeCoords
;
2422 oldnorm
= ctx
->NeedEyeNormals
;
2424 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2425 ctx
->NeedEyeCoords
= ((ctx
->Fog
.Enabled
&& ctx
->Hint
.Fog
!= GL_NICEST
) ||
2426 ctx
->Point
.Attenuated
);
2427 ctx
->NeedEyeNormals
= GL_FALSE
;
2429 if (ctx
->Light
.Enabled
) {
2430 if (ctx
->Light
.Flags
& LIGHT_POSITIONAL
) {
2431 /* Need length for attenuation */
2432 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_LENGTH_PRESERVING
))
2433 ctx
->NeedEyeCoords
= GL_TRUE
;
2434 } else if (ctx
->Light
.NeedVertices
) {
2435 /* Need angle for spot calculations */
2436 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_ANGLE_PRESERVING
))
2437 ctx
->NeedEyeCoords
= GL_TRUE
;
2439 ctx
->NeedEyeNormals
= ctx
->NeedEyeCoords
;
2441 if (ctx
->Texture
.Enabled
|| ctx
->RenderMode
==GL_FEEDBACK
) {
2442 if (ctx
->Texture
.NeedEyeCoords
) ctx
->NeedEyeCoords
= GL_TRUE
;
2443 if (ctx
->Texture
.NeedNormals
)
2444 ctx
->NeedNormals
= ctx
->NeedEyeNormals
= GL_TRUE
;
2447 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
2449 if (ctx
->NeedEyeCoords
)
2450 ctx
->vb_proj_matrix
= &ctx
->ProjectionMatrix
;
2452 if (ctx
->Light
.Enabled
) {
2453 gl_update_lighting_function(ctx
);
2455 if ( (ctx
->NewState
& NEW_LIGHTING
) ||
2456 ((ctx
->NewState
& (NEW_MODELVIEW
| NEW_PROJECTION
)) &&
2457 !ctx
->NeedEyeCoords
) ||
2458 oldcoord
!= ctx
->NeedEyeCoords
||
2459 oldnorm
!= ctx
->NeedEyeNormals
) {
2460 gl_compute_light_positions(ctx
);
2463 ctx
->rescale_factor
= 1.0F
;
2465 if (ctx
->ModelView
.flags
& (MAT_FLAG_UNIFORM_SCALE
|
2466 MAT_FLAG_GENERAL_SCALE
|
2467 MAT_FLAG_GENERAL_3D
|
2471 GLfloat
*m
= ctx
->ModelView
.inv
;
2472 GLfloat f
= m
[2]*m
[2] + m
[6]*m
[6] + m
[10]*m
[10];
2473 if (f
> 1e-12 && (f
-1)*(f
-1) > 1e-12)
2474 ctx
->rescale_factor
= 1.0/GL_SQRT(f
);
2478 gl_update_normal_transform( ctx
);
2482 gl_update_pipelines(ctx
);