1 /* $Id: context.c,v 1.26 1999/12/04 21:23:17 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.
29 * If multi-threading is enabled (-DTHREADS) then each thread has it's
30 * own rendering context. A thread obtains the pointer to its GLcontext
31 * with the gl_get_thread_context() function. Otherwise, the global
32 * pointer, CC, points to the current context used by all threads in
51 #include "extensions.h"
68 #include "simple_list.h"
72 #include "translate.h"
90 /**********************************************************************/
91 /***** Context and Thread management *****/
92 /**********************************************************************/
98 static MesaTSD mesa_ctx_tsd
;
99 static void mesa_ctx_thread_init() {
100 MesaInitTSD(&mesa_ctx_tsd
);
105 /* One Current Context pointer for all threads in the address space */
106 GLcontext
*_mesa_current_context
= NULL
;
107 struct immediate
*CURRENT_INPUT
= NULL
;
114 /**********************************************************************/
115 /***** Profiling functions *****/
116 /**********************************************************************/
120 #include <sys/times.h>
121 #include <sys/param.h>
125 * Return system time in seconds.
126 * NOTE: this implementation may not be very portable!
128 GLdouble
gl_time( void )
130 static GLdouble prev_time
= 0.0;
131 static GLdouble time
;
138 time
= (double)clk
/ (double)CLK_TCK
;
140 time
= (double)clk
/ (double)HZ
;
143 if (time
>prev_time
) {
153 * Reset the timing/profiling counters
155 static void init_timings( GLcontext
*ctx
)
157 ctx
->BeginEndCount
= 0;
158 ctx
->BeginEndTime
= 0.0;
159 ctx
->VertexCount
= 0;
160 ctx
->VertexTime
= 0.0;
162 ctx
->PointTime
= 0.0;
165 ctx
->PolygonCount
= 0;
166 ctx
->PolygonTime
= 0.0;
168 ctx
->ClearTime
= 0.0;
175 * Print the accumulated timing/profiling data.
177 static void print_timings( GLcontext
*ctx
)
179 GLdouble beginendrate
;
183 GLdouble polygonrate
;
187 GLdouble avgvertices
;
189 if (ctx
->BeginEndTime
>0.0) {
190 beginendrate
= ctx
->BeginEndCount
/ ctx
->BeginEndTime
;
195 if (ctx
->VertexTime
>0.0) {
196 vertexrate
= ctx
->VertexCount
/ ctx
->VertexTime
;
201 if (ctx
->PointTime
>0.0) {
202 pointrate
= ctx
->PointCount
/ ctx
->PointTime
;
207 if (ctx
->LineTime
>0.0) {
208 linerate
= ctx
->LineCount
/ ctx
->LineTime
;
213 if (ctx
->PolygonTime
>0.0) {
214 polygonrate
= ctx
->PolygonCount
/ ctx
->PolygonTime
;
219 if (ctx
->ClearTime
>0.0) {
220 clearrate
= ctx
->ClearCount
/ ctx
->ClearTime
;
225 if (ctx
->SwapTime
>0.0) {
226 swaprate
= ctx
->SwapCount
/ ctx
->SwapTime
;
232 if (ctx
->BeginEndCount
>0) {
233 avgvertices
= (GLdouble
) ctx
->VertexCount
/ (GLdouble
) ctx
->BeginEndCount
;
239 overhead
= ctx
->BeginEndTime
- ctx
->VertexTime
- ctx
->PointTime
240 - ctx
->LineTime
- ctx
->PolygonTime
;
243 printf(" Count Time (s) Rate (/s) \n");
244 printf("--------------------------------------------------------\n");
245 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
246 ctx
->BeginEndCount
, ctx
->BeginEndTime
, beginendrate
);
247 printf(" vertexes transformed %7d %8.3f %10.3f\n",
248 ctx
->VertexCount
, ctx
->VertexTime
, vertexrate
);
249 printf(" points rasterized %7d %8.3f %10.3f\n",
250 ctx
->PointCount
, ctx
->PointTime
, pointrate
);
251 printf(" lines rasterized %7d %8.3f %10.3f\n",
252 ctx
->LineCount
, ctx
->LineTime
, linerate
);
253 printf(" polygons rasterized %7d %8.3f %10.3f\n",
254 ctx
->PolygonCount
, ctx
->PolygonTime
, polygonrate
);
255 printf(" overhead %8.3f\n", overhead
);
256 printf("glClear %7d %8.3f %10.3f\n",
257 ctx
->ClearCount
, ctx
->ClearTime
, clearrate
);
258 printf("SwapBuffers %7d %8.3f %10.3f\n",
259 ctx
->SwapCount
, ctx
->SwapTime
, swaprate
);
262 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices
);
270 /**********************************************************************/
271 /***** Context allocation, initialization, destroying *****/
272 /**********************************************************************/
276 * This function just calls all the various one-time-init functions in Mesa.
278 static void one_time_init( void )
280 static GLboolean alreadyCalled
= GL_FALSE
;
281 if (!alreadyCalled
) {
289 gl_init_transformation();
294 alreadyCalled
= GL_TRUE
;
296 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
297 fprintf(stderr
, "Mesa DEBUG build %s %s\n", __DATE__
, __TIME__
);
303 * Allocate and initialize a shared context state structure.
305 static struct gl_shared_state
*alloc_shared_state( void )
308 struct gl_shared_state
*ss
;
309 GLboolean outOfMemory
;
311 ss
= CALLOC_STRUCT(gl_shared_state
);
315 ss
->DisplayList
= NewHashTable();
317 ss
->TexObjects
= NewHashTable();
319 /* Default Texture objects */
320 outOfMemory
= GL_FALSE
;
321 for (d
= 1 ; d
<= 3 ; d
++) {
322 ss
->DefaultD
[d
] = gl_alloc_texture_object(ss
, 0, d
);
323 if (!ss
->DefaultD
[d
]) {
324 outOfMemory
= GL_TRUE
;
327 ss
->DefaultD
[d
]->RefCount
++; /* don't free if not in use */
330 if (!ss
->DisplayList
|| !ss
->TexObjects
|| outOfMemory
) {
331 /* Ran out of memory at some point. Free everything and return NULL */
333 DeleteHashTable(ss
->DisplayList
);
335 DeleteHashTable(ss
->TexObjects
);
337 gl_free_texture_object(ss
, ss
->DefaultD
[1]);
339 gl_free_texture_object(ss
, ss
->DefaultD
[2]);
341 gl_free_texture_object(ss
, ss
->DefaultD
[3]);
352 * Deallocate a shared state context and all children structures.
354 static void free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
356 /* Free display lists */
358 GLuint list
= HashFirstEntry(ss
->DisplayList
);
360 gl_destroy_list(ctx
, list
);
366 DeleteHashTable(ss
->DisplayList
);
368 /* Free texture objects */
369 while (ss
->TexObjectList
)
371 if (ctx
->Driver
.DeleteTexture
)
372 (*ctx
->Driver
.DeleteTexture
)( ctx
, ss
->TexObjectList
);
373 /* this function removes from linked list too! */
374 gl_free_texture_object(ss
, ss
->TexObjectList
);
376 DeleteHashTable(ss
->TexObjects
);
387 * Initialize the nth light. Note that the defaults for light 0 are
388 * different than the other lights.
390 static void init_light( struct gl_light
*l
, GLuint n
)
392 make_empty_list( l
);
394 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
396 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
397 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
400 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
401 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
403 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
404 ASSIGN_3V( l
->EyeDirection
, 0.0, 0.0, -1.0 );
405 l
->SpotExponent
= 0.0;
406 gl_compute_spot_exp_table( l
);
407 l
->SpotCutoff
= 180.0;
408 l
->CosCutoff
= 0.0; /* KW: -ve values not admitted */
409 l
->ConstantAttenuation
= 1.0;
410 l
->LinearAttenuation
= 0.0;
411 l
->QuadraticAttenuation
= 0.0;
412 l
->Enabled
= GL_FALSE
;
417 static void init_lightmodel( struct gl_lightmodel
*lm
)
419 ASSIGN_4V( lm
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
420 lm
->LocalViewer
= GL_FALSE
;
421 lm
->TwoSide
= GL_FALSE
;
422 lm
->ColorControl
= GL_SINGLE_COLOR
;
426 static void init_material( struct gl_material
*m
)
428 ASSIGN_4V( m
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
429 ASSIGN_4V( m
->Diffuse
, 0.8, 0.8, 0.8, 1.0 );
430 ASSIGN_4V( m
->Specular
, 0.0, 0.0, 0.0, 1.0 );
431 ASSIGN_4V( m
->Emission
, 0.0, 0.0, 0.0, 1.0 );
435 m
->SpecularIndex
= 1;
440 static void init_texture_unit( GLcontext
*ctx
, GLuint unit
)
442 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
444 texUnit
->EnvMode
= GL_MODULATE
;
445 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
446 texUnit
->TexGenEnabled
= 0;
447 texUnit
->GenModeS
= GL_EYE_LINEAR
;
448 texUnit
->GenModeT
= GL_EYE_LINEAR
;
449 texUnit
->GenModeR
= GL_EYE_LINEAR
;
450 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
451 /* Yes, these plane coefficients are correct! */
452 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
453 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
454 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
455 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
456 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
457 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
458 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
459 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
461 texUnit
->CurrentD
[1] = ctx
->Shared
->DefaultD
[1];
462 texUnit
->CurrentD
[2] = ctx
->Shared
->DefaultD
[2];
463 texUnit
->CurrentD
[3] = ctx
->Shared
->DefaultD
[3];
467 static void init_fallback_arrays( GLcontext
*ctx
)
469 struct gl_client_array
*cl
;
472 cl
= &ctx
->Fallback
.Normal
;
477 cl
->Ptr
= (void *) ctx
->Current
.Normal
;
480 cl
= &ctx
->Fallback
.Color
;
482 cl
->Type
= GL_UNSIGNED_BYTE
;
485 cl
->Ptr
= (void *) ctx
->Current
.ByteColor
;
488 cl
= &ctx
->Fallback
.Index
;
490 cl
->Type
= GL_UNSIGNED_INT
;
493 cl
->Ptr
= (void *) &ctx
->Current
.Index
;
496 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++) {
497 cl
= &ctx
->Fallback
.TexCoord
[i
];
502 cl
->Ptr
= (void *) ctx
->Current
.Texcoord
[i
];
506 cl
= &ctx
->Fallback
.EdgeFlag
;
508 cl
->Type
= GL_UNSIGNED_BYTE
;
511 cl
->Ptr
= (void *) &ctx
->Current
.EdgeFlag
;
515 /* Initialize a 1-D evaluator map */
516 static void init_1d_map( struct gl_1d_map
*map
, int n
, const float *initial
)
521 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
525 map
->Points
[i
] = initial
[i
];
530 /* Initialize a 2-D evaluator map */
531 static void init_2d_map( struct gl_2d_map
*map
, int n
, const float *initial
)
539 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
543 map
->Points
[i
] = initial
[i
];
548 static void init_color_table( struct gl_color_table
*p
)
555 p
->IntFormat
= GL_RGBA
;
561 * Initialize a gl_context structure to default values.
563 static void initialize_context( GLcontext
*ctx
)
568 /* Constants, may be overriden by device driver */
569 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
570 ctx
->Const
.MaxTextureSize
= 1 << (MAX_TEXTURE_LEVELS
- 1);
571 ctx
->Const
.MaxTextureUnits
= MAX_TEXTURE_UNITS
;
572 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
574 /* Modelview matrix */
575 gl_matrix_ctr( &ctx
->ModelView
);
576 gl_matrix_alloc_inv( &ctx
->ModelView
);
578 ctx
->ModelViewStackDepth
= 0;
579 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
580 gl_matrix_ctr( &ctx
->ModelViewStack
[i
] );
581 gl_matrix_alloc_inv( &ctx
->ModelViewStack
[i
] );
584 /* Projection matrix - need inv for user clipping in clip space*/
585 gl_matrix_ctr( &ctx
->ProjectionMatrix
);
586 gl_matrix_alloc_inv( &ctx
->ProjectionMatrix
);
588 gl_matrix_ctr( &ctx
->ModelProjectMatrix
);
589 gl_matrix_ctr( &ctx
->ModelProjectWinMatrix
);
590 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
592 ctx
->ProjectionStackDepth
= 0;
593 ctx
->NearFarStack
[0][0] = 1.0; /* These values seem weird by make */
594 ctx
->NearFarStack
[0][1] = 0.0; /* sense mathematically. */
596 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
597 gl_matrix_ctr( &ctx
->ProjectionStack
[i
] );
598 gl_matrix_alloc_inv( &ctx
->ProjectionStack
[i
] );
602 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++) {
603 gl_matrix_ctr( &ctx
->TextureMatrix
[i
] );
604 ctx
->TextureStackDepth
[i
] = 0;
605 for (j
= 0 ; j
< MAX_TEXTURE_STACK_DEPTH
; j
++) {
606 ctx
->TextureStack
[i
][j
].inv
= 0;
610 /* Accumulate buffer group */
611 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
613 /* Color buffer group */
614 ctx
->Color
.IndexMask
= 0xffffffff;
615 ctx
->Color
.ColorMask
[0] = 0xff;
616 ctx
->Color
.ColorMask
[1] = 0xff;
617 ctx
->Color
.ColorMask
[2] = 0xff;
618 ctx
->Color
.ColorMask
[3] = 0xff;
619 ctx
->Color
.SWmasking
= GL_FALSE
;
620 ctx
->Color
.ClearIndex
= 0;
621 ASSIGN_4V( ctx
->Color
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
622 ctx
->Color
.DrawBuffer
= GL_FRONT
;
623 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
624 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
625 ctx
->Color
.AlphaRef
= 0;
626 ctx
->Color
.BlendEnabled
= GL_FALSE
;
627 ctx
->Color
.BlendSrcRGB
= GL_ONE
;
628 ctx
->Color
.BlendDstRGB
= GL_ZERO
;
629 ctx
->Color
.BlendSrcA
= GL_ONE
;
630 ctx
->Color
.BlendDstA
= GL_ZERO
;
631 ctx
->Color
.BlendEquation
= GL_FUNC_ADD_EXT
;
632 ctx
->Color
.BlendFunc
= NULL
; /* this pointer set only when needed */
633 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
634 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
635 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
636 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
637 ctx
->Color
.LogicOp
= GL_COPY
;
638 ctx
->Color
.DitherFlag
= GL_TRUE
;
639 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
642 ASSIGN_4V( ctx
->Current
.ByteColor
, 255, 255, 255, 255);
643 ctx
->Current
.Index
= 1;
644 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
645 ASSIGN_4V( ctx
->Current
.Texcoord
[i
], 0.0, 0.0, 0.0, 1.0 );
646 ASSIGN_4V( ctx
->Current
.RasterPos
, 0.0, 0.0, 0.0, 1.0 );
647 ctx
->Current
.RasterDistance
= 0.0;
648 ASSIGN_4V( ctx
->Current
.RasterColor
, 1.0, 1.0, 1.0, 1.0 );
649 ctx
->Current
.RasterIndex
= 1;
650 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
651 ASSIGN_4V( ctx
->Current
.RasterMultiTexCoord
[i
], 0.0, 0.0, 0.0, 1.0 );
652 ctx
->Current
.RasterTexCoord
= ctx
->Current
.RasterMultiTexCoord
[0];
653 ctx
->Current
.RasterPosValid
= GL_TRUE
;
654 ctx
->Current
.EdgeFlag
= GL_TRUE
;
655 ASSIGN_3V( ctx
->Current
.Normal
, 0.0, 0.0, 1.0 );
656 ctx
->Current
.Primitive
= (GLenum
) (GL_POLYGON
+ 1);
658 ctx
->Current
.Flag
= (VERT_NORM
|VERT_INDEX
|VERT_RGBA
|VERT_EDGE
|
659 VERT_TEX0_1
|VERT_TEX1_1
|VERT_MATERIAL
);
661 init_fallback_arrays( ctx
);
663 /* Depth buffer group */
664 ctx
->Depth
.Test
= GL_FALSE
;
665 ctx
->Depth
.Clear
= 1.0;
666 ctx
->Depth
.Func
= GL_LESS
;
667 ctx
->Depth
.Mask
= GL_TRUE
;
669 /* Evaluators group */
670 ctx
->Eval
.Map1Color4
= GL_FALSE
;
671 ctx
->Eval
.Map1Index
= GL_FALSE
;
672 ctx
->Eval
.Map1Normal
= GL_FALSE
;
673 ctx
->Eval
.Map1TextureCoord1
= GL_FALSE
;
674 ctx
->Eval
.Map1TextureCoord2
= GL_FALSE
;
675 ctx
->Eval
.Map1TextureCoord3
= GL_FALSE
;
676 ctx
->Eval
.Map1TextureCoord4
= GL_FALSE
;
677 ctx
->Eval
.Map1Vertex3
= GL_FALSE
;
678 ctx
->Eval
.Map1Vertex4
= GL_FALSE
;
679 ctx
->Eval
.Map2Color4
= GL_FALSE
;
680 ctx
->Eval
.Map2Index
= GL_FALSE
;
681 ctx
->Eval
.Map2Normal
= GL_FALSE
;
682 ctx
->Eval
.Map2TextureCoord1
= GL_FALSE
;
683 ctx
->Eval
.Map2TextureCoord2
= GL_FALSE
;
684 ctx
->Eval
.Map2TextureCoord3
= GL_FALSE
;
685 ctx
->Eval
.Map2TextureCoord4
= GL_FALSE
;
686 ctx
->Eval
.Map2Vertex3
= GL_FALSE
;
687 ctx
->Eval
.Map2Vertex4
= GL_FALSE
;
688 ctx
->Eval
.AutoNormal
= GL_FALSE
;
689 ctx
->Eval
.MapGrid1un
= 1;
690 ctx
->Eval
.MapGrid1u1
= 0.0;
691 ctx
->Eval
.MapGrid1u2
= 1.0;
692 ctx
->Eval
.MapGrid2un
= 1;
693 ctx
->Eval
.MapGrid2vn
= 1;
694 ctx
->Eval
.MapGrid2u1
= 0.0;
695 ctx
->Eval
.MapGrid2u2
= 1.0;
696 ctx
->Eval
.MapGrid2v1
= 0.0;
697 ctx
->Eval
.MapGrid2v2
= 1.0;
701 static GLfloat vertex
[4] = { 0.0, 0.0, 0.0, 1.0 };
702 static GLfloat normal
[3] = { 0.0, 0.0, 1.0 };
703 static GLfloat index
[1] = { 1.0 };
704 static GLfloat color
[4] = { 1.0, 1.0, 1.0, 1.0 };
705 static GLfloat texcoord
[4] = { 0.0, 0.0, 0.0, 1.0 };
707 init_1d_map( &ctx
->EvalMap
.Map1Vertex3
, 3, vertex
);
708 init_1d_map( &ctx
->EvalMap
.Map1Vertex4
, 4, vertex
);
709 init_1d_map( &ctx
->EvalMap
.Map1Index
, 1, index
);
710 init_1d_map( &ctx
->EvalMap
.Map1Color4
, 4, color
);
711 init_1d_map( &ctx
->EvalMap
.Map1Normal
, 3, normal
);
712 init_1d_map( &ctx
->EvalMap
.Map1Texture1
, 1, texcoord
);
713 init_1d_map( &ctx
->EvalMap
.Map1Texture2
, 2, texcoord
);
714 init_1d_map( &ctx
->EvalMap
.Map1Texture3
, 3, texcoord
);
715 init_1d_map( &ctx
->EvalMap
.Map1Texture4
, 4, texcoord
);
717 init_2d_map( &ctx
->EvalMap
.Map2Vertex3
, 3, vertex
);
718 init_2d_map( &ctx
->EvalMap
.Map2Vertex4
, 4, vertex
);
719 init_2d_map( &ctx
->EvalMap
.Map2Index
, 1, index
);
720 init_2d_map( &ctx
->EvalMap
.Map2Color4
, 4, color
);
721 init_2d_map( &ctx
->EvalMap
.Map2Normal
, 3, normal
);
722 init_2d_map( &ctx
->EvalMap
.Map2Texture1
, 1, texcoord
);
723 init_2d_map( &ctx
->EvalMap
.Map2Texture2
, 2, texcoord
);
724 init_2d_map( &ctx
->EvalMap
.Map2Texture3
, 3, texcoord
);
725 init_2d_map( &ctx
->EvalMap
.Map2Texture4
, 4, texcoord
);
729 ctx
->Fog
.Enabled
= GL_FALSE
;
730 ctx
->Fog
.Mode
= GL_EXP
;
731 ASSIGN_4V( ctx
->Fog
.Color
, 0.0, 0.0, 0.0, 0.0 );
732 ctx
->Fog
.Index
= 0.0;
733 ctx
->Fog
.Density
= 1.0;
734 ctx
->Fog
.Start
= 0.0;
738 ctx
->Hint
.PerspectiveCorrection
= GL_DONT_CARE
;
739 ctx
->Hint
.PointSmooth
= GL_DONT_CARE
;
740 ctx
->Hint
.LineSmooth
= GL_DONT_CARE
;
741 ctx
->Hint
.PolygonSmooth
= GL_DONT_CARE
;
742 ctx
->Hint
.Fog
= GL_DONT_CARE
;
744 ctx
->Hint
.AllowDrawWin
= GL_TRUE
;
745 ctx
->Hint
.AllowDrawSpn
= GL_TRUE
;
746 ctx
->Hint
.AllowDrawMem
= GL_TRUE
;
747 ctx
->Hint
.StrictLighting
= GL_TRUE
;
750 gl_pipeline_init( ctx
);
754 gl_extensions_ctr( ctx
);
756 ctx
->AllowVertexCull
= CLIP_CULLED_BIT
;
759 for (i
=0;i
<MAX_LIGHTS
;i
++) {
760 init_light( &ctx
->Light
.Light
[i
], i
);
762 make_empty_list( &ctx
->Light
.EnabledList
);
764 init_lightmodel( &ctx
->Light
.Model
);
765 init_material( &ctx
->Light
.Material
[0] );
766 init_material( &ctx
->Light
.Material
[1] );
767 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
768 ctx
->Light
.Enabled
= GL_FALSE
;
769 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
770 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
771 ctx
->Light
.ColorMaterialBitmask
772 = gl_material_bitmask( ctx
,
774 GL_AMBIENT_AND_DIFFUSE
, ~0, 0 );
776 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
779 ctx
->Line
.SmoothFlag
= GL_FALSE
;
780 ctx
->Line
.StippleFlag
= GL_FALSE
;
781 ctx
->Line
.Width
= 1.0;
782 ctx
->Line
.StipplePattern
= 0xffff;
783 ctx
->Line
.StippleFactor
= 1;
785 /* Display List group */
786 ctx
->List
.ListBase
= 0;
789 ctx
->Pixel
.RedBias
= 0.0;
790 ctx
->Pixel
.RedScale
= 1.0;
791 ctx
->Pixel
.GreenBias
= 0.0;
792 ctx
->Pixel
.GreenScale
= 1.0;
793 ctx
->Pixel
.BlueBias
= 0.0;
794 ctx
->Pixel
.BlueScale
= 1.0;
795 ctx
->Pixel
.AlphaBias
= 0.0;
796 ctx
->Pixel
.AlphaScale
= 1.0;
797 ctx
->Pixel
.ScaleOrBiasRGBA
= GL_FALSE
;
798 ctx
->Pixel
.DepthBias
= 0.0;
799 ctx
->Pixel
.DepthScale
= 1.0;
800 ctx
->Pixel
.IndexOffset
= 0;
801 ctx
->Pixel
.IndexShift
= 0;
802 ctx
->Pixel
.ZoomX
= 1.0;
803 ctx
->Pixel
.ZoomY
= 1.0;
804 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
805 ctx
->Pixel
.MapStencilFlag
= GL_FALSE
;
806 ctx
->Pixel
.MapStoSsize
= 1;
807 ctx
->Pixel
.MapItoIsize
= 1;
808 ctx
->Pixel
.MapItoRsize
= 1;
809 ctx
->Pixel
.MapItoGsize
= 1;
810 ctx
->Pixel
.MapItoBsize
= 1;
811 ctx
->Pixel
.MapItoAsize
= 1;
812 ctx
->Pixel
.MapRtoRsize
= 1;
813 ctx
->Pixel
.MapGtoGsize
= 1;
814 ctx
->Pixel
.MapBtoBsize
= 1;
815 ctx
->Pixel
.MapAtoAsize
= 1;
816 ctx
->Pixel
.MapStoS
[0] = 0;
817 ctx
->Pixel
.MapItoI
[0] = 0;
818 ctx
->Pixel
.MapItoR
[0] = 0.0;
819 ctx
->Pixel
.MapItoG
[0] = 0.0;
820 ctx
->Pixel
.MapItoB
[0] = 0.0;
821 ctx
->Pixel
.MapItoA
[0] = 0.0;
822 ctx
->Pixel
.MapItoR8
[0] = 0;
823 ctx
->Pixel
.MapItoG8
[0] = 0;
824 ctx
->Pixel
.MapItoB8
[0] = 0;
825 ctx
->Pixel
.MapItoA8
[0] = 0;
826 ctx
->Pixel
.MapRtoR
[0] = 0.0;
827 ctx
->Pixel
.MapGtoG
[0] = 0.0;
828 ctx
->Pixel
.MapBtoB
[0] = 0.0;
829 ctx
->Pixel
.MapAtoA
[0] = 0.0;
832 ctx
->Point
.SmoothFlag
= GL_FALSE
;
833 ctx
->Point
.Size
= 1.0;
834 ctx
->Point
.Params
[0] = 1.0;
835 ctx
->Point
.Params
[1] = 0.0;
836 ctx
->Point
.Params
[2] = 0.0;
837 ctx
->Point
.Attenuated
= GL_FALSE
;
838 ctx
->Point
.MinSize
= 0.0;
839 ctx
->Point
.MaxSize
= (GLfloat
) MAX_POINT_SIZE
;
840 ctx
->Point
.Threshold
= 1.0;
843 ctx
->Polygon
.CullFlag
= GL_FALSE
;
844 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
845 ctx
->Polygon
.FrontFace
= GL_CCW
;
846 ctx
->Polygon
.FrontBit
= 0;
847 ctx
->Polygon
.FrontMode
= GL_FILL
;
848 ctx
->Polygon
.BackMode
= GL_FILL
;
849 ctx
->Polygon
.Unfilled
= GL_FALSE
;
850 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
851 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
852 ctx
->Polygon
.OffsetFactor
= 0.0F
;
853 ctx
->Polygon
.OffsetUnits
= 0.0F
;
854 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
855 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
856 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
858 /* Polygon Stipple group */
859 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );
862 ctx
->Scissor
.Enabled
= GL_FALSE
;
865 ctx
->Scissor
.Width
= 0;
866 ctx
->Scissor
.Height
= 0;
869 ctx
->Stencil
.Enabled
= GL_FALSE
;
870 ctx
->Stencil
.Function
= GL_ALWAYS
;
871 ctx
->Stencil
.FailFunc
= GL_KEEP
;
872 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
873 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
874 ctx
->Stencil
.Ref
= 0;
875 ctx
->Stencil
.ValueMask
= STENCIL_MAX
;
876 ctx
->Stencil
.Clear
= 0;
877 ctx
->Stencil
.WriteMask
= STENCIL_MAX
;
880 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
881 ctx
->Texture
.CurrentTransformUnit
= 0; /* multitexture */
882 ctx
->Texture
.Enabled
= 0;
884 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
885 init_texture_unit( ctx
, i
);
887 init_color_table(&ctx
->Texture
.Palette
);
889 /* Transformation group */
890 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
891 ctx
->Transform
.Normalize
= GL_FALSE
;
892 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
893 for (i
=0;i
<MAX_CLIP_PLANES
;i
++) {
894 ctx
->Transform
.ClipEnabled
[i
] = GL_FALSE
;
895 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
897 ctx
->Transform
.AnyClip
= GL_FALSE
;
902 ctx
->Viewport
.Width
= 0;
903 ctx
->Viewport
.Height
= 0;
904 ctx
->Viewport
.Near
= 0.0;
905 ctx
->Viewport
.Far
= 1.0;
906 gl_matrix_ctr(&ctx
->Viewport
.WindowMap
);
910 ctx
->Viewport
.WindowMap
.m
[Sz
] = 0.5 * DEPTH_SCALE
;
911 ctx
->Viewport
.WindowMap
.m
[Tz
] = 0.5 * DEPTH_SCALE
;
915 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
916 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
919 ctx
->Array
.Vertex
.Size
= 4;
920 ctx
->Array
.Vertex
.Type
= GL_FLOAT
;
921 ctx
->Array
.Vertex
.Stride
= 0;
922 ctx
->Array
.Vertex
.StrideB
= 0;
923 ctx
->Array
.Vertex
.Ptr
= NULL
;
924 ctx
->Array
.Vertex
.Enabled
= GL_FALSE
;
925 ctx
->Array
.Normal
.Type
= GL_FLOAT
;
926 ctx
->Array
.Normal
.Stride
= 0;
927 ctx
->Array
.Normal
.StrideB
= 0;
928 ctx
->Array
.Normal
.Ptr
= NULL
;
929 ctx
->Array
.Normal
.Enabled
= GL_FALSE
;
930 ctx
->Array
.Color
.Size
= 4;
931 ctx
->Array
.Color
.Type
= GL_FLOAT
;
932 ctx
->Array
.Color
.Stride
= 0;
933 ctx
->Array
.Color
.StrideB
= 0;
934 ctx
->Array
.Color
.Ptr
= NULL
;
935 ctx
->Array
.Color
.Enabled
= GL_FALSE
;
936 ctx
->Array
.Index
.Type
= GL_FLOAT
;
937 ctx
->Array
.Index
.Stride
= 0;
938 ctx
->Array
.Index
.StrideB
= 0;
939 ctx
->Array
.Index
.Ptr
= NULL
;
940 ctx
->Array
.Index
.Enabled
= GL_FALSE
;
941 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
942 ctx
->Array
.TexCoord
[i
].Size
= 4;
943 ctx
->Array
.TexCoord
[i
].Type
= GL_FLOAT
;
944 ctx
->Array
.TexCoord
[i
].Stride
= 0;
945 ctx
->Array
.TexCoord
[i
].StrideB
= 0;
946 ctx
->Array
.TexCoord
[i
].Ptr
= NULL
;
947 ctx
->Array
.TexCoord
[i
].Enabled
= GL_FALSE
;
949 ctx
->Array
.TexCoordInterleaveFactor
= 1;
950 ctx
->Array
.EdgeFlag
.Stride
= 0;
951 ctx
->Array
.EdgeFlag
.StrideB
= 0;
952 ctx
->Array
.EdgeFlag
.Ptr
= NULL
;
953 ctx
->Array
.EdgeFlag
.Enabled
= GL_FALSE
;
954 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
957 ctx
->Pack
.Alignment
= 4;
958 ctx
->Pack
.RowLength
= 0;
959 ctx
->Pack
.ImageHeight
= 0;
960 ctx
->Pack
.SkipPixels
= 0;
961 ctx
->Pack
.SkipRows
= 0;
962 ctx
->Pack
.SkipImages
= 0;
963 ctx
->Pack
.SwapBytes
= GL_FALSE
;
964 ctx
->Pack
.LsbFirst
= GL_FALSE
;
965 ctx
->Unpack
.Alignment
= 4;
966 ctx
->Unpack
.RowLength
= 0;
967 ctx
->Unpack
.ImageHeight
= 0;
968 ctx
->Unpack
.SkipPixels
= 0;
969 ctx
->Unpack
.SkipRows
= 0;
970 ctx
->Unpack
.SkipImages
= 0;
971 ctx
->Unpack
.SwapBytes
= GL_FALSE
;
972 ctx
->Unpack
.LsbFirst
= GL_FALSE
;
975 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
976 ctx
->Feedback
.Buffer
= NULL
;
977 ctx
->Feedback
.BufferSize
= 0;
978 ctx
->Feedback
.Count
= 0;
980 /* Selection/picking */
981 ctx
->Select
.Buffer
= NULL
;
982 ctx
->Select
.BufferSize
= 0;
983 ctx
->Select
.BufferCount
= 0;
984 ctx
->Select
.Hits
= 0;
985 ctx
->Select
.NameStackDepth
= 0;
987 /* Optimized Accum buffer */
988 ctx
->IntegerAccumMode
= GL_TRUE
;
989 ctx
->IntegerAccumScaler
= 0.0;
991 /* Renderer and client attribute stacks */
992 ctx
->AttribStackDepth
= 0;
993 ctx
->ClientAttribStackDepth
= 0;
995 /*** Miscellaneous ***/
996 ctx
->NewState
= NEW_ALL
;
997 ctx
->RenderMode
= GL_RENDER
;
998 ctx
->StippleCounter
= 0;
999 ctx
->NeedNormals
= GL_FALSE
;
1000 ctx
->DoViewportMapping
= GL_TRUE
;
1002 ctx
->NeedEyeCoords
= GL_FALSE
;
1003 ctx
->NeedEyeNormals
= GL_FALSE
;
1004 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
1008 ctx
->ExecuteFlag
= GL_TRUE
;
1009 ctx
->CompileFlag
= GL_FALSE
;
1010 ctx
->CurrentListPtr
= NULL
;
1011 ctx
->CurrentBlock
= NULL
;
1012 ctx
->CurrentListNum
= 0;
1013 ctx
->CurrentPos
= 0;
1015 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1017 ctx
->CatchSignals
= GL_TRUE
;
1019 /* For debug/development only */
1020 ctx
->NoRaster
= getenv("MESA_NO_RASTER") ? GL_TRUE
: GL_FALSE
;
1021 ctx
->FirstTimeCurrent
= GL_TRUE
;
1023 /* Dither disable */
1024 ctx
->NoDither
= getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
1025 if (ctx
->NoDither
) {
1026 if (getenv("MESA_DEBUG")) {
1027 fprintf(stderr
, "MESA_NO_DITHER set - dithering disabled\n");
1029 ctx
->Color
.DitherFlag
= GL_FALSE
;
1037 * Allocate a new GLvisual object.
1038 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
1039 * alphaFlag - alloc software alpha buffers?
1040 * dbFlag - double buffering?
1041 * stereoFlag - stereo buffer?
1042 * depthFits - requested minimum bits per depth buffer value
1043 * stencilFits - requested minimum bits per stencil buffer value
1044 * accumFits - requested minimum bits per accum buffer component
1045 * indexFits - number of bits per pixel if rgbFlag==GL_FALSE
1046 * red/green/blue/alphaFits - number of bits per color component
1047 * in frame buffer for RGB(A) mode.
1048 * Return: pointer to new GLvisual or NULL if requested parameters can't
1051 GLvisual
*gl_create_visual( GLboolean rgbFlag
,
1052 GLboolean alphaFlag
,
1054 GLboolean stereoFlag
,
1066 if (depthBits
> (GLint
) (8*sizeof(GLdepth
))) {
1067 /* can't meet depth buffer requirements */
1070 if (stencilBits
> (GLint
) (8*sizeof(GLstencil
))) {
1071 /* can't meet stencil buffer requirements */
1074 if (accumBits
> (GLint
) (8*sizeof(GLaccum
))) {
1075 /* can't meet accum buffer requirements */
1079 vis
= (GLvisual
*) CALLOC( sizeof(GLvisual
) );
1084 vis
->RGBAflag
= rgbFlag
;
1085 vis
->DBflag
= dbFlag
;
1086 vis
->StereoFlag
= stereoFlag
;
1087 vis
->RedBits
= redBits
;
1088 vis
->GreenBits
= greenBits
;
1089 vis
->BlueBits
= blueBits
;
1090 vis
->AlphaBits
= alphaFlag
? 8*sizeof(GLubyte
) : alphaBits
;
1092 vis
->IndexBits
= indexBits
;
1093 vis
->DepthBits
= (depthBits
>0) ? 8*sizeof(GLdepth
) : 0;
1094 vis
->AccumBits
= (accumBits
>0) ? 8*sizeof(GLaccum
) : 0;
1095 vis
->StencilBits
= (stencilBits
>0) ? 8*sizeof(GLstencil
) : 0;
1097 vis
->SoftwareAlpha
= alphaFlag
;
1104 void gl_destroy_visual( GLvisual
*vis
)
1112 * Allocate the proxy textures. If we run out of memory part way through
1113 * the allocations clean up and return GL_FALSE.
1114 * Return: GL_TRUE=success, GL_FALSE=failure
1116 static GLboolean
alloc_proxy_textures( GLcontext
*ctx
)
1118 GLboolean out_of_memory
;
1121 ctx
->Texture
.Proxy1D
= gl_alloc_texture_object(NULL
, 0, 1);
1122 if (!ctx
->Texture
.Proxy1D
) {
1126 ctx
->Texture
.Proxy2D
= gl_alloc_texture_object(NULL
, 0, 2);
1127 if (!ctx
->Texture
.Proxy2D
) {
1128 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1132 ctx
->Texture
.Proxy3D
= gl_alloc_texture_object(NULL
, 0, 3);
1133 if (!ctx
->Texture
.Proxy3D
) {
1134 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1135 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1139 out_of_memory
= GL_FALSE
;
1140 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1141 ctx
->Texture
.Proxy1D
->Image
[i
] = gl_alloc_texture_image();
1142 ctx
->Texture
.Proxy2D
->Image
[i
] = gl_alloc_texture_image();
1143 ctx
->Texture
.Proxy3D
->Image
[i
] = gl_alloc_texture_image();
1144 if (!ctx
->Texture
.Proxy1D
->Image
[i
]
1145 || !ctx
->Texture
.Proxy2D
->Image
[i
]
1146 || !ctx
->Texture
.Proxy3D
->Image
[i
]) {
1147 out_of_memory
= GL_TRUE
;
1150 if (out_of_memory
) {
1151 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1152 if (ctx
->Texture
.Proxy1D
->Image
[i
]) {
1153 gl_free_texture_image(ctx
->Texture
.Proxy1D
->Image
[i
]);
1155 if (ctx
->Texture
.Proxy2D
->Image
[i
]) {
1156 gl_free_texture_image(ctx
->Texture
.Proxy2D
->Image
[i
]);
1158 if (ctx
->Texture
.Proxy3D
->Image
[i
]) {
1159 gl_free_texture_image(ctx
->Texture
.Proxy3D
->Image
[i
]);
1162 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1163 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1164 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy3D
);
1175 * Allocate and initialize a GLcontext structure.
1176 * Input: visual - a GLvisual pointer
1177 * sharelist - another context to share display lists with or NULL
1178 * driver_ctx - pointer to device driver's context state struct
1179 * Return: pointer to a new gl_context struct or NULL if error.
1181 GLcontext
*gl_create_context( GLvisual
*visual
,
1182 GLcontext
*share_list
,
1189 (void) direct
; /* not used */
1191 /* do some implementation tests */
1192 assert( sizeof(GLbyte
) == 1 );
1193 assert( sizeof(GLshort
) >= 2 );
1194 assert( sizeof(GLint
) >= 4 );
1195 assert( sizeof(GLubyte
) == 1 );
1196 assert( sizeof(GLushort
) >= 2 );
1197 assert( sizeof(GLuint
) >= 4 );
1199 /* misc one-time initializations */
1202 ctx
= (GLcontext
*) CALLOC( sizeof(GLcontext
) );
1207 ctx
->DriverCtx
= driver_ctx
;
1208 ctx
->Visual
= visual
;
1209 ctx
->DrawBuffer
= NULL
;
1210 ctx
->ReadBuffer
= NULL
;
1212 ctx
->VB
= gl_vb_create_for_immediate( ctx
);
1217 ctx
->input
= ctx
->VB
->IM
;
1219 ctx
->PB
= gl_alloc_pb();
1227 /* share the group of display lists of another context */
1228 ctx
->Shared
= share_list
->Shared
;
1231 /* allocate new group of display lists */
1232 ctx
->Shared
= alloc_shared_state();
1240 ctx
->Shared
->RefCount
++;
1242 initialize_context( ctx
);
1243 gl_reset_vb( ctx
->VB
);
1244 gl_reset_input( ctx
);
1247 ctx
->ShineTabList
= MALLOC_STRUCT( gl_shine_tab
);
1248 make_empty_list( ctx
->ShineTabList
);
1250 for (i
= 0 ; i
< 10 ; i
++) {
1251 struct gl_shine_tab
*s
= MALLOC_STRUCT( gl_shine_tab
);
1254 insert_at_tail( ctx
->ShineTabList
, s
);
1257 for (i
= 0 ; i
< 4 ; i
++) {
1258 ctx
->ShineTable
[i
] = ctx
->ShineTabList
->prev
;
1259 ctx
->ShineTable
[i
]->refcount
++;
1262 if (visual
->DBflag
) {
1263 ctx
->Color
.DrawBuffer
= GL_BACK
;
1264 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
1265 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
1266 ctx
->Pixel
.ReadBuffer
= GL_BACK
;
1267 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
1270 ctx
->Color
.DrawBuffer
= GL_FRONT
;
1271 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
1272 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
1273 ctx
->Pixel
.ReadBuffer
= GL_FRONT
;
1274 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
1278 /* Fill in some driver defaults now.
1280 ctx
->Driver
.AllocDepthBuffer
= gl_alloc_depth_buffer
;
1281 ctx
->Driver
.ReadDepthSpanFloat
= gl_read_depth_span_float
;
1282 ctx
->Driver
.ReadDepthSpanInt
= gl_read_depth_span_int
;
1286 init_timings( ctx
);
1289 if (!alloc_proxy_textures(ctx
)) {
1290 free_shared_state(ctx
, ctx
->Shared
);
1297 /* setup API dispatch tables */
1298 _mesa_init_exec_table( &ctx
->Exec
);
1299 _mesa_init_dlist_table( &ctx
->Save
);
1300 ctx
->CurrentDispatch
= &ctx
->Exec
;
1305 /* Just reads the config files...
1307 void gl_context_initialize( GLcontext
*ctx
)
1309 gl_read_config_file( ctx
);
1316 * Destroy a gl_context structure.
1318 void gl_destroy_context( GLcontext
*ctx
)
1323 struct gl_shine_tab
*s
, *tmps
;
1326 if (getenv("MESA_PROFILE")) {
1327 print_timings( ctx
);
1331 gl_matrix_dtr( &ctx
->ModelView
);
1332 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
1333 gl_matrix_dtr( &ctx
->ModelViewStack
[i
] );
1335 gl_matrix_dtr( &ctx
->ProjectionMatrix
);
1336 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
1337 gl_matrix_dtr( &ctx
->ProjectionStack
[i
] );
1342 if(ctx
->input
!= ctx
->VB
->IM
)
1343 gl_immediate_free( ctx
->input
);
1345 gl_vb_free( ctx
->VB
);
1347 ctx
->Shared
->RefCount
--;
1348 assert(ctx
->Shared
->RefCount
>=0);
1349 if (ctx
->Shared
->RefCount
==0) {
1350 /* free shared state */
1351 free_shared_state( ctx
, ctx
->Shared
);
1354 foreach_s( s
, tmps
, ctx
->ShineTabList
) {
1357 FREE( ctx
->ShineTabList
);
1359 /* Free proxy texture objects */
1360 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy1D
);
1361 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy2D
);
1362 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy3D
);
1364 /* Free evaluator data */
1365 if (ctx
->EvalMap
.Map1Vertex3
.Points
)
1366 FREE( ctx
->EvalMap
.Map1Vertex3
.Points
);
1367 if (ctx
->EvalMap
.Map1Vertex4
.Points
)
1368 FREE( ctx
->EvalMap
.Map1Vertex4
.Points
);
1369 if (ctx
->EvalMap
.Map1Index
.Points
)
1370 FREE( ctx
->EvalMap
.Map1Index
.Points
);
1371 if (ctx
->EvalMap
.Map1Color4
.Points
)
1372 FREE( ctx
->EvalMap
.Map1Color4
.Points
);
1373 if (ctx
->EvalMap
.Map1Normal
.Points
)
1374 FREE( ctx
->EvalMap
.Map1Normal
.Points
);
1375 if (ctx
->EvalMap
.Map1Texture1
.Points
)
1376 FREE( ctx
->EvalMap
.Map1Texture1
.Points
);
1377 if (ctx
->EvalMap
.Map1Texture2
.Points
)
1378 FREE( ctx
->EvalMap
.Map1Texture2
.Points
);
1379 if (ctx
->EvalMap
.Map1Texture3
.Points
)
1380 FREE( ctx
->EvalMap
.Map1Texture3
.Points
);
1381 if (ctx
->EvalMap
.Map1Texture4
.Points
)
1382 FREE( ctx
->EvalMap
.Map1Texture4
.Points
);
1384 if (ctx
->EvalMap
.Map2Vertex3
.Points
)
1385 FREE( ctx
->EvalMap
.Map2Vertex3
.Points
);
1386 if (ctx
->EvalMap
.Map2Vertex4
.Points
)
1387 FREE( ctx
->EvalMap
.Map2Vertex4
.Points
);
1388 if (ctx
->EvalMap
.Map2Index
.Points
)
1389 FREE( ctx
->EvalMap
.Map2Index
.Points
);
1390 if (ctx
->EvalMap
.Map2Color4
.Points
)
1391 FREE( ctx
->EvalMap
.Map2Color4
.Points
);
1392 if (ctx
->EvalMap
.Map2Normal
.Points
)
1393 FREE( ctx
->EvalMap
.Map2Normal
.Points
);
1394 if (ctx
->EvalMap
.Map2Texture1
.Points
)
1395 FREE( ctx
->EvalMap
.Map2Texture1
.Points
);
1396 if (ctx
->EvalMap
.Map2Texture2
.Points
)
1397 FREE( ctx
->EvalMap
.Map2Texture2
.Points
);
1398 if (ctx
->EvalMap
.Map2Texture3
.Points
)
1399 FREE( ctx
->EvalMap
.Map2Texture3
.Points
);
1400 if (ctx
->EvalMap
.Map2Texture4
.Points
)
1401 FREE( ctx
->EvalMap
.Map2Texture4
.Points
);
1403 /* Free cache of immediate buffers. */
1404 while (ctx
->nr_im_queued
-- > 0) {
1405 struct immediate
* next
= ctx
->freed_im_queue
->next
;
1406 FREE( ctx
->freed_im_queue
);
1407 ctx
->freed_im_queue
= next
;
1409 gl_extensions_dtr(ctx
);
1411 FREE( (void *) ctx
);
1414 if (ctx
== _mesa_current_context
) {
1415 _mesa_current_context
= NULL
;
1416 CURRENT_INPUT
= NULL
;
1426 * Create a new framebuffer. A GLframebuffer is a struct which
1427 * encapsulates the depth, stencil and accum buffers and related
1429 * Input: visual - a GLvisual pointer
1430 * Return: pointer to new GLframebuffer struct or NULL if error.
1432 GLframebuffer
*gl_create_framebuffer( GLvisual
*visual
)
1434 GLframebuffer
*buffer
;
1436 buffer
= (GLframebuffer
*) CALLOC( sizeof(GLframebuffer
) );
1441 buffer
->Visual
= visual
;
1449 * Free a framebuffer struct and its buffers.
1451 void gl_destroy_framebuffer( GLframebuffer
*buffer
)
1454 if (buffer
->Depth
) {
1455 FREE( buffer
->Depth
);
1457 if (buffer
->Accum
) {
1458 FREE( buffer
->Accum
);
1460 if (buffer
->Stencil
) {
1461 FREE( buffer
->Stencil
);
1463 if (buffer
->FrontLeftAlpha
) {
1464 FREE( buffer
->FrontLeftAlpha
);
1466 if (buffer
->BackLeftAlpha
) {
1467 FREE( buffer
->BackLeftAlpha
);
1469 if (buffer
->FrontRightAlpha
) {
1470 FREE( buffer
->FrontRightAlpha
);
1472 if (buffer
->BackRightAlpha
) {
1473 FREE( buffer
->BackRightAlpha
);
1482 * Set the current context, binding the given frame buffer to the context.
1484 void gl_make_current( GLcontext
*newCtx
, GLframebuffer
*buffer
)
1486 gl_make_current2( newCtx
, buffer
, buffer
);
1491 * Bind the given context to the given draw-buffer and read-buffer
1492 * and make it the current context for this thread.
1494 void gl_make_current2( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1495 GLframebuffer
*readBuffer
)
1497 GET_CURRENT_CONTEXT(oldCtx
);
1499 /* Flush the old context
1502 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx
, "gl_make_current");
1505 /* unbind frame buffers from context */
1506 if (oldCtx
&& oldCtx
->DrawBuffer
) {
1507 oldCtx
->DrawBuffer
= NULL
;
1509 if (oldCtx
&& oldCtx
->ReadBuffer
) {
1510 oldCtx
->ReadBuffer
= NULL
;
1514 /* TODO: unbind old buffer from context? */
1515 MesaSetTSD(&mesa_ctx_tsd
, (void *) newCtx
, mesa_ctx_thread_init
);
1517 _mesa_current_context
= newCtx
;
1520 SET_IMMEDIATE(newCtx
, newCtx
->input
);
1524 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1526 _glapi_set_dispatch(NULL
); /* none current */
1528 if (MESA_VERBOSE
) fprintf(stderr
, "gl_make_current()\n");
1530 if (newCtx
&& drawBuffer
&& readBuffer
) {
1531 /* TODO: check if newCtx and buffer's visual match??? */
1532 newCtx
->DrawBuffer
= drawBuffer
;
1533 newCtx
->ReadBuffer
= readBuffer
;
1534 newCtx
->NewState
= NEW_ALL
; /* just to be safe */
1535 gl_update_state( newCtx
);
1538 /* We can use this to help debug user's problems. Tell the to set
1539 * the MESA_INFO env variable before running their app. Then the
1540 * first time each context is made current we'll print some useful
1543 if (newCtx
&& newCtx
->FirstTimeCurrent
) {
1544 if (getenv("MESA_INFO")) {
1545 fprintf(stderr
, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION
));
1546 fprintf(stderr
, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER
));
1547 fprintf(stderr
, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR
));
1548 fprintf(stderr
, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS
));
1550 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1557 * Return current context handle for the calling thread.
1559 GLcontext
*gl_get_current_context( void )
1562 return (GLcontext
*) MesaGetTSD(&mesa_ctx_tsd
);
1564 return _mesa_current_context
;
1571 * Copy attribute groups from one context to another.
1572 * Input: src - source context
1573 * dst - destination context
1574 * mask - bitwise OR of GL_*_BIT flags
1576 void gl_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1578 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1579 MEMCPY( &dst
->Accum
, &src
->Accum
, sizeof(struct gl_accum_attrib
) );
1581 if (mask
& GL_COLOR_BUFFER_BIT
) {
1582 MEMCPY( &dst
->Color
, &src
->Color
, sizeof(struct gl_colorbuffer_attrib
) );
1584 if (mask
& GL_CURRENT_BIT
) {
1585 MEMCPY( &dst
->Current
, &src
->Current
, sizeof(struct gl_current_attrib
) );
1587 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1588 MEMCPY( &dst
->Depth
, &src
->Depth
, sizeof(struct gl_depthbuffer_attrib
) );
1590 if (mask
& GL_ENABLE_BIT
) {
1593 if (mask
& GL_EVAL_BIT
) {
1594 MEMCPY( &dst
->Eval
, &src
->Eval
, sizeof(struct gl_eval_attrib
) );
1596 if (mask
& GL_FOG_BIT
) {
1597 MEMCPY( &dst
->Fog
, &src
->Fog
, sizeof(struct gl_fog_attrib
) );
1599 if (mask
& GL_HINT_BIT
) {
1600 MEMCPY( &dst
->Hint
, &src
->Hint
, sizeof(struct gl_hint_attrib
) );
1602 if (mask
& GL_LIGHTING_BIT
) {
1603 MEMCPY( &dst
->Light
, &src
->Light
, sizeof(struct gl_light_attrib
) );
1604 /* gl_reinit_light_attrib( &dst->Light ); */
1606 if (mask
& GL_LINE_BIT
) {
1607 MEMCPY( &dst
->Line
, &src
->Line
, sizeof(struct gl_line_attrib
) );
1609 if (mask
& GL_LIST_BIT
) {
1610 MEMCPY( &dst
->List
, &src
->List
, sizeof(struct gl_list_attrib
) );
1612 if (mask
& GL_PIXEL_MODE_BIT
) {
1613 MEMCPY( &dst
->Pixel
, &src
->Pixel
, sizeof(struct gl_pixel_attrib
) );
1615 if (mask
& GL_POINT_BIT
) {
1616 MEMCPY( &dst
->Point
, &src
->Point
, sizeof(struct gl_point_attrib
) );
1618 if (mask
& GL_POLYGON_BIT
) {
1619 MEMCPY( &dst
->Polygon
, &src
->Polygon
, sizeof(struct gl_polygon_attrib
) );
1621 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1622 /* Use loop instead of MEMCPY due to problem with Portland Group's
1623 * C compiler. Reported by John Stone.
1626 for (i
=0;i
<32;i
++) {
1627 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1630 if (mask
& GL_SCISSOR_BIT
) {
1631 MEMCPY( &dst
->Scissor
, &src
->Scissor
, sizeof(struct gl_scissor_attrib
) );
1633 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1634 MEMCPY( &dst
->Stencil
, &src
->Stencil
, sizeof(struct gl_stencil_attrib
) );
1636 if (mask
& GL_TEXTURE_BIT
) {
1637 MEMCPY( &dst
->Texture
, &src
->Texture
, sizeof(struct gl_texture_attrib
) );
1639 if (mask
& GL_TRANSFORM_BIT
) {
1640 MEMCPY( &dst
->Transform
, &src
->Transform
, sizeof(struct gl_transform_attrib
) );
1642 if (mask
& GL_VIEWPORT_BIT
) {
1643 MEMCPY( &dst
->Viewport
, &src
->Viewport
, sizeof(struct gl_viewport_attrib
) );
1649 * This should be called by device drivers just before they do a
1650 * swapbuffers. Any pending rendering commands will be executed.
1653 _mesa_swapbuffers(GLcontext
*ctx
)
1655 FLUSH_VB( ctx
, "swap buffers" );
1660 * Return pointer to this context's current API dispatch table.
1661 * It'll either be the immediate-mode execute dispatcher or the
1662 * display list compile dispatcher.
1664 struct _glapi_table
*
1665 _mesa_get_dispatch(GLcontext
*ctx
)
1667 return ctx
->CurrentDispatch
;
1673 _mesa_ResizeBuffersMESA( void )
1675 GET_CURRENT_CONTEXT(ctx
);
1677 GLuint buf_width
, buf_height
;
1679 if (MESA_VERBOSE
& VERBOSE_API
)
1680 fprintf(stderr
, "glResizeBuffersMESA\n");
1682 /* ask device driver for size of output buffer */
1683 (*ctx
->Driver
.GetBufferSize
)( ctx
, &buf_width
, &buf_height
);
1685 /* see if size of device driver's color buffer (window) has changed */
1686 if (ctx
->DrawBuffer
->Width
== (GLint
) buf_width
&&
1687 ctx
->DrawBuffer
->Height
== (GLint
) buf_height
)
1690 ctx
->NewState
|= NEW_RASTER_OPS
; /* to update scissor / window bounds */
1692 /* save buffer size */
1693 ctx
->DrawBuffer
->Width
= buf_width
;
1694 ctx
->DrawBuffer
->Height
= buf_height
;
1696 /* Reallocate other buffers if needed. */
1697 if (ctx
->Visual
->DepthBits
>0) {
1698 /* reallocate depth buffer */
1699 (*ctx
->Driver
.AllocDepthBuffer
)( ctx
);
1701 if (ctx
->Visual
->StencilBits
>0) {
1702 /* reallocate stencil buffer */
1703 gl_alloc_stencil_buffer( ctx
);
1705 if (ctx
->Visual
->AccumBits
>0) {
1706 /* reallocate accum buffer */
1707 gl_alloc_accum_buffer( ctx
);
1709 if (ctx
->Visual
->SoftwareAlpha
) {
1710 gl_alloc_alpha_buffers( ctx
);
1716 /**********************************************************************/
1717 /***** Miscellaneous functions *****/
1718 /**********************************************************************/
1722 * This function is called when the Mesa user has stumbled into a code
1723 * path which may not be implemented fully or correctly.
1725 void gl_problem( const GLcontext
*ctx
, const char *s
)
1727 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
1728 fprintf( stderr
, "Report to mesa-bugs@mesa3d.org\n" );
1735 * This is called to inform the user that he or she has tried to do
1736 * something illogical or if there's likely a bug in their program
1737 * (like enabled depth testing without a depth buffer).
1739 void gl_warning( const GLcontext
*ctx
, const char *s
)
1745 if (getenv("MESA_DEBUG")) {
1753 fprintf( stderr
, "Mesa warning: %s\n", s
);
1760 void gl_compile_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1762 if (ctx
->CompileFlag
)
1763 gl_save_error( ctx
, error
, s
);
1765 if (ctx
->ExecuteFlag
)
1766 gl_error( ctx
, error
, s
);
1771 * This is Mesa's error handler. Normally, all that's done is the updating
1772 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1773 * environment variable "MESA_DEBUG" is defined then a real error message
1774 * is printed to stderr.
1775 * Input: error - the error value
1776 * s - a diagnostic string
1778 void gl_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1785 if (getenv("MESA_DEBUG")) {
1798 strcpy( errstr
, "GL_NO_ERROR" );
1800 case GL_INVALID_VALUE
:
1801 strcpy( errstr
, "GL_INVALID_VALUE" );
1803 case GL_INVALID_ENUM
:
1804 strcpy( errstr
, "GL_INVALID_ENUM" );
1806 case GL_INVALID_OPERATION
:
1807 strcpy( errstr
, "GL_INVALID_OPERATION" );
1809 case GL_STACK_OVERFLOW
:
1810 strcpy( errstr
, "GL_STACK_OVERFLOW" );
1812 case GL_STACK_UNDERFLOW
:
1813 strcpy( errstr
, "GL_STACK_UNDERFLOW" );
1815 case GL_OUT_OF_MEMORY
:
1816 strcpy( errstr
, "GL_OUT_OF_MEMORY" );
1819 strcpy( errstr
, "unknown" );
1822 fprintf( stderr
, "Mesa user error: %s in %s\n", errstr
, s
);
1825 if (ctx
->ErrorValue
==GL_NO_ERROR
) {
1826 ctx
->ErrorValue
= error
;
1829 /* Call device driver's error handler, if any. This is used on the Mac. */
1830 if (ctx
->Driver
.Error
) {
1831 (*ctx
->Driver
.Error
)( ctx
);
1837 /**********************************************************************/
1838 /***** State update logic *****/
1839 /**********************************************************************/
1843 * Since the device driver may or may not support pixel logic ops we
1844 * have to make some extensive tests to determine whether or not
1845 * software-implemented logic operations have to be used.
1847 static void update_pixel_logic( GLcontext
*ctx
)
1849 if (ctx
->Visual
->RGBAflag
) {
1850 /* RGBA mode blending w/ Logic Op */
1851 if (ctx
->Color
.ColorLogicOpEnabled
) {
1852 if (ctx
->Driver
.LogicOp
1853 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1854 /* Device driver can do logic, don't have to do it in software */
1855 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1858 /* Device driver can't do logic op so we do it in software */
1859 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1864 if (ctx
->Driver
.LogicOp
) {
1865 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1867 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1871 /* CI mode Logic Op */
1872 if (ctx
->Color
.IndexLogicOpEnabled
) {
1873 if (ctx
->Driver
.LogicOp
1874 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1875 /* Device driver can do logic, don't have to do it in software */
1876 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1879 /* Device driver can't do logic op so we do it in software */
1880 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1885 if (ctx
->Driver
.LogicOp
) {
1886 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1888 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1896 * Check if software implemented RGBA or Color Index masking is needed.
1898 static void update_pixel_masking( GLcontext
*ctx
)
1900 if (ctx
->Visual
->RGBAflag
) {
1901 GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1902 if (*colorMask
== 0xffffffff) {
1903 /* disable masking */
1904 if (ctx
->Driver
.ColorMask
) {
1905 (void) (*ctx
->Driver
.ColorMask
)( ctx
, GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1907 ctx
->Color
.SWmasking
= GL_FALSE
;
1910 /* Ask driver to do color masking, if it can't then
1913 GLboolean red
= ctx
->Color
.ColorMask
[RCOMP
] ? GL_TRUE
: GL_FALSE
;
1914 GLboolean green
= ctx
->Color
.ColorMask
[GCOMP
] ? GL_TRUE
: GL_FALSE
;
1915 GLboolean blue
= ctx
->Color
.ColorMask
[BCOMP
] ? GL_TRUE
: GL_FALSE
;
1916 GLboolean alpha
= ctx
->Color
.ColorMask
[ACOMP
] ? GL_TRUE
: GL_FALSE
;
1917 if (ctx
->Driver
.ColorMask
1918 && (*ctx
->Driver
.ColorMask
)( ctx
, red
, green
, blue
, alpha
)) {
1919 ctx
->Color
.SWmasking
= GL_FALSE
;
1922 ctx
->Color
.SWmasking
= GL_TRUE
;
1927 if (ctx
->Color
.IndexMask
==0xffffffff) {
1928 /* disable masking */
1929 if (ctx
->Driver
.IndexMask
) {
1930 (void) (*ctx
->Driver
.IndexMask
)( ctx
, 0xffffffff );
1932 ctx
->Color
.SWmasking
= GL_FALSE
;
1935 /* Ask driver to do index masking, if it can't then
1938 if (ctx
->Driver
.IndexMask
1939 && (*ctx
->Driver
.IndexMask
)( ctx
, ctx
->Color
.IndexMask
)) {
1940 ctx
->Color
.SWmasking
= GL_FALSE
;
1943 ctx
->Color
.SWmasking
= GL_TRUE
;
1950 static void update_fog_mode( GLcontext
*ctx
)
1952 int old_mode
= ctx
->FogMode
;
1954 if (ctx
->Fog
.Enabled
) {
1955 if (ctx
->Texture
.Enabled
)
1956 ctx
->FogMode
= FOG_FRAGMENT
;
1957 else if (ctx
->Hint
.Fog
== GL_NICEST
)
1958 ctx
->FogMode
= FOG_FRAGMENT
;
1960 ctx
->FogMode
= FOG_VERTEX
;
1962 if (ctx
->Driver
.GetParameteri
)
1963 if ((ctx
->Driver
.GetParameteri
)( ctx
, DD_HAVE_HARDWARE_FOG
))
1964 ctx
->FogMode
= FOG_FRAGMENT
;
1967 ctx
->FogMode
= FOG_NONE
;
1970 if (old_mode
!= ctx
->FogMode
)
1971 ctx
->NewState
|= NEW_FOG
;
1976 * Recompute the value of ctx->RasterMask, etc. according to
1977 * the current context.
1979 static void update_rasterflags( GLcontext
*ctx
)
1981 ctx
->RasterMask
= 0;
1983 if (ctx
->Color
.AlphaEnabled
) ctx
->RasterMask
|= ALPHATEST_BIT
;
1984 if (ctx
->Color
.BlendEnabled
) ctx
->RasterMask
|= BLEND_BIT
;
1985 if (ctx
->Depth
.Test
) ctx
->RasterMask
|= DEPTH_BIT
;
1986 if (ctx
->FogMode
==FOG_FRAGMENT
) ctx
->RasterMask
|= FOG_BIT
;
1987 if (ctx
->Color
.SWLogicOpEnabled
) ctx
->RasterMask
|= LOGIC_OP_BIT
;
1988 if (ctx
->Scissor
.Enabled
) ctx
->RasterMask
|= SCISSOR_BIT
;
1989 if (ctx
->Stencil
.Enabled
) ctx
->RasterMask
|= STENCIL_BIT
;
1990 if (ctx
->Color
.SWmasking
) ctx
->RasterMask
|= MASKING_BIT
;
1992 if (ctx
->Visual
->SoftwareAlpha
&& ctx
->Color
.ColorMask
[ACOMP
]
1993 && ctx
->Color
.DrawBuffer
!= GL_NONE
)
1994 ctx
->RasterMask
|= ALPHABUF_BIT
;
1996 if ( ctx
->Viewport
.X
<0
1997 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->DrawBuffer
->Width
1998 || ctx
->Viewport
.Y
<0
1999 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->DrawBuffer
->Height
) {
2000 ctx
->RasterMask
|= WINCLIP_BIT
;
2003 /* If we're not drawing to exactly one color buffer set the
2004 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
2005 * buffers or the RGBA or CI mask disables all writes.
2008 ctx
->TriangleCaps
&= ~DD_MULTIDRAW
;
2010 if (ctx
->Color
.MultiDrawBuffer
) {
2011 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2012 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2014 else if (ctx
->Color
.DrawBuffer
==GL_NONE
) {
2015 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2016 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2018 else if (ctx
->Visual
->RGBAflag
&& ctx
->Color
.ColorMask
==0) {
2019 /* all RGBA channels disabled */
2020 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2021 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2022 ctx
->Color
.DrawDestMask
= 0;
2024 else if (!ctx
->Visual
->RGBAflag
&& ctx
->Color
.IndexMask
==0) {
2025 /* all color index bits disabled */
2026 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
2027 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
2028 ctx
->Color
.DrawDestMask
= 0;
2033 void gl_print_state( const char *msg
, GLuint state
)
2036 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2039 (state
& NEW_LIGHTING
) ? "lighting, " : "",
2040 (state
& NEW_RASTER_OPS
) ? "raster-ops, " : "",
2041 (state
& NEW_TEXTURING
) ? "texturing, " : "",
2042 (state
& NEW_POLYGON
) ? "polygon, " : "",
2043 (state
& NEW_DRVSTATE0
) ? "driver-0, " : "",
2044 (state
& NEW_DRVSTATE1
) ? "driver-1, " : "",
2045 (state
& NEW_DRVSTATE2
) ? "driver-2, " : "",
2046 (state
& NEW_DRVSTATE3
) ? "driver-3, " : "",
2047 (state
& NEW_MODELVIEW
) ? "modelview, " : "",
2048 (state
& NEW_PROJECTION
) ? "projection, " : "",
2049 (state
& NEW_TEXTURE_MATRIX
) ? "texture-matrix, " : "",
2050 (state
& NEW_USER_CLIP
) ? "user-clip, " : "",
2051 (state
& NEW_TEXTURE_ENV
) ? "texture-env, " : "",
2052 (state
& NEW_CLIENT_STATE
) ? "client-state, " : "",
2053 (state
& NEW_FOG
) ? "fog, " : "",
2054 (state
& NEW_NORMAL_TRANSFORM
) ? "normal-transform, " : "",
2055 (state
& NEW_VIEWPORT
) ? "viewport, " : "",
2056 (state
& NEW_TEXTURE_ENABLE
) ? "texture-enable, " : "");
2059 void gl_print_enable_flags( const char *msg
, GLuint flags
)
2062 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s\n",
2065 (flags
& ENABLE_TEX0
) ? "tex-0, " : "",
2066 (flags
& ENABLE_TEX1
) ? "tex-1, " : "",
2067 (flags
& ENABLE_LIGHT
) ? "light, " : "",
2068 (flags
& ENABLE_FOG
) ? "fog, " : "",
2069 (flags
& ENABLE_USERCLIP
) ? "userclip, " : "",
2070 (flags
& ENABLE_TEXGEN0
) ? "tex-gen-0, " : "",
2071 (flags
& ENABLE_TEXGEN1
) ? "tex-gen-1, " : "",
2072 (flags
& ENABLE_TEXMAT0
) ? "tex-mat-0, " : "",
2073 (flags
& ENABLE_TEXMAT1
) ? "tex-mat-1, " : "",
2074 (flags
& ENABLE_NORMALIZE
) ? "normalize, " : "",
2075 (flags
& ENABLE_RESCALE
) ? "rescale, " : "");
2080 * If ctx->NewState is non-zero then this function MUST be called before
2081 * rendering any primitive. Basically, function pointers and miscellaneous
2082 * flags are updated to reflect the current state of the state machine.
2084 void gl_update_state( GLcontext
*ctx
)
2088 if (MESA_VERBOSE
& VERBOSE_STATE
)
2089 gl_print_state("", ctx
->NewState
);
2091 if (ctx
->NewState
& NEW_CLIENT_STATE
)
2092 gl_update_client_state( ctx
);
2094 if ((ctx
->NewState
& NEW_TEXTURE_ENABLE
) &&
2095 (ctx
->Enabled
& ENABLE_TEX_ANY
) != ctx
->Texture
.Enabled
)
2096 ctx
->NewState
|= NEW_TEXTURING
| NEW_RASTER_OPS
;
2098 if (ctx
->NewState
& NEW_TEXTURE_ENV
) {
2099 if (ctx
->Texture
.Unit
[0].EnvMode
== ctx
->Texture
.Unit
[0].LastEnvMode
&&
2100 ctx
->Texture
.Unit
[1].EnvMode
== ctx
->Texture
.Unit
[1].LastEnvMode
)
2101 ctx
->NewState
&= ~NEW_TEXTURE_ENV
;
2102 ctx
->Texture
.Unit
[0].LastEnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
2103 ctx
->Texture
.Unit
[1].LastEnvMode
= ctx
->Texture
.Unit
[1].EnvMode
;
2106 if (ctx
->NewState
& NEW_TEXTURE_MATRIX
) {
2107 ctx
->Enabled
&= ~(ENABLE_TEXMAT0
|ENABLE_TEXMAT1
);
2109 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2110 if (ctx
->TextureMatrix
[i
].flags
& MAT_DIRTY_ALL_OVER
)
2112 gl_matrix_analyze( &ctx
->TextureMatrix
[i
] );
2113 ctx
->TextureMatrix
[i
].flags
&= ~MAT_DIRTY_DEPENDENTS
;
2115 if (ctx
->Texture
.Unit
[i
].Enabled
&&
2116 ctx
->TextureMatrix
[i
].type
!= MATRIX_IDENTITY
)
2117 ctx
->Enabled
|= ENABLE_TEXMAT0
<< i
;
2122 if (ctx
->NewState
& (NEW_TEXTURING
| NEW_TEXTURE_ENABLE
)) {
2123 ctx
->Texture
.NeedNormals
= GL_FALSE
;
2124 gl_update_dirty_texobjs(ctx
);
2125 ctx
->Enabled
&= ~(ENABLE_TEXGEN0
|ENABLE_TEXGEN1
);
2126 ctx
->Texture
.ReallyEnabled
= 0;
2128 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2129 if (ctx
->Texture
.Unit
[i
].Enabled
) {
2130 gl_update_texture_unit( ctx
, &ctx
->Texture
.Unit
[i
] );
2132 ctx
->Texture
.ReallyEnabled
|=
2133 ctx
->Texture
.Unit
[i
].ReallyEnabled
<<(i
*4);
2135 if (ctx
->Texture
.Unit
[i
].GenFlags
!= 0) {
2136 ctx
->Enabled
|= ENABLE_TEXGEN0
<< i
;
2138 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_NORMALS
)
2140 ctx
->Texture
.NeedNormals
= GL_TRUE
;
2141 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2144 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_EYE_COORD
)
2146 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2152 ctx
->Texture
.Enabled
= ctx
->Enabled
& ENABLE_TEX_ANY
;
2153 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2156 if (ctx
->NewState
& (NEW_RASTER_OPS
| NEW_LIGHTING
| NEW_FOG
)) {
2159 if (ctx
->NewState
& NEW_RASTER_OPS
) {
2160 update_pixel_logic(ctx
);
2161 update_pixel_masking(ctx
);
2162 update_fog_mode(ctx
);
2163 update_rasterflags(ctx
);
2164 if (ctx
->Driver
.Dither
) {
2165 (*ctx
->Driver
.Dither
)( ctx
, ctx
->Color
.DitherFlag
);
2168 /* Check if incoming colors can be modified during rasterization */
2169 if (ctx
->Fog
.Enabled
||
2170 ctx
->Texture
.Enabled
||
2171 ctx
->Color
.BlendEnabled
||
2172 ctx
->Color
.SWmasking
||
2173 ctx
->Color
.SWLogicOpEnabled
) {
2174 ctx
->MutablePixels
= GL_TRUE
;
2177 ctx
->MutablePixels
= GL_FALSE
;
2180 /* update scissor region */
2182 ctx
->DrawBuffer
->Xmin
= 0;
2183 ctx
->DrawBuffer
->Ymin
= 0;
2184 ctx
->DrawBuffer
->Xmax
= ctx
->DrawBuffer
->Width
-1;
2185 ctx
->DrawBuffer
->Ymax
= ctx
->DrawBuffer
->Height
-1;
2186 if (ctx
->Scissor
.Enabled
) {
2187 if (ctx
->Scissor
.X
> ctx
->DrawBuffer
->Xmin
) {
2188 ctx
->DrawBuffer
->Xmin
= ctx
->Scissor
.X
;
2190 if (ctx
->Scissor
.Y
> ctx
->DrawBuffer
->Ymin
) {
2191 ctx
->DrawBuffer
->Ymin
= ctx
->Scissor
.Y
;
2193 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1 < ctx
->DrawBuffer
->Xmax
) {
2194 ctx
->DrawBuffer
->Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1;
2196 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1 < ctx
->DrawBuffer
->Ymax
) {
2197 ctx
->DrawBuffer
->Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1;
2201 /* The driver isn't managing the depth buffer.
2203 if (ctx
->Driver
.AllocDepthBuffer
== gl_alloc_depth_buffer
)
2205 if (ctx
->Depth
.Mask
) {
2206 switch (ctx
->Depth
.Func
) {
2208 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_less
;
2209 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_less
;
2212 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_greater
;
2213 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_greater
;
2216 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2217 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2221 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2222 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2227 if (ctx
->NewState
& NEW_LIGHTING
) {
2228 ctx
->TriangleCaps
&= ~(DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2229 if (ctx
->Light
.Enabled
) {
2230 if (ctx
->Light
.Model
.TwoSide
)
2231 ctx
->TriangleCaps
|= (DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2232 gl_update_lighting(ctx
);
2237 if (ctx
->NewState
& (NEW_POLYGON
| NEW_LIGHTING
)) {
2239 ctx
->TriangleCaps
&= ~DD_TRI_CULL_FRONT_BACK
;
2241 if (ctx
->NewState
& NEW_POLYGON
) {
2242 /* Setup CullBits bitmask */
2243 if (ctx
->Polygon
.CullFlag
) {
2244 ctx
->backface_sign
= 1;
2245 switch(ctx
->Polygon
.CullFaceMode
) {
2247 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
2248 ctx
->backface_sign
= -1;
2249 ctx
->Polygon
.CullBits
= 1;
2252 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
2253 ctx
->backface_sign
= -1;
2254 ctx
->Polygon
.CullBits
= 2;
2257 case GL_FRONT_AND_BACK
:
2258 ctx
->backface_sign
= 0;
2259 ctx
->Polygon
.CullBits
= 0;
2260 ctx
->TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
2265 ctx
->Polygon
.CullBits
= 3;
2266 ctx
->backface_sign
= 0;
2269 /* Any Polygon offsets enabled? */
2270 ctx
->TriangleCaps
&= ~DD_TRI_OFFSET
;
2272 if (ctx
->Polygon
.OffsetPoint
||
2273 ctx
->Polygon
.OffsetLine
||
2274 ctx
->Polygon
.OffsetFill
)
2275 ctx
->TriangleCaps
|= DD_TRI_OFFSET
;
2277 /* reset Z offsets now */
2278 ctx
->PointZoffset
= 0.0;
2279 ctx
->LineZoffset
= 0.0;
2280 ctx
->PolygonZoffset
= 0.0;
2284 if (ctx
->NewState
& ~(NEW_CLIENT_STATE
|
2285 NEW_DRIVER_STATE
|NEW_USER_CLIP
|
2287 gl_update_clipmask(ctx
);
2289 if (ctx
->NewState
& (NEW_LIGHTING
|
2301 ctx
->IndirectTriangles
= ctx
->TriangleCaps
& ~ctx
->Driver
.TriangleCaps
;
2302 ctx
->IndirectTriangles
|= DD_SW_RASTERIZE
;
2304 if (MESA_VERBOSE
&VERBOSE_CULL
)
2305 gl_print_tri_caps("initial indirect tris", ctx
->IndirectTriangles
);
2307 ctx
->Driver
.PointsFunc
= NULL
;
2308 ctx
->Driver
.LineFunc
= NULL
;
2309 ctx
->Driver
.TriangleFunc
= NULL
;
2310 ctx
->Driver
.QuadFunc
= NULL
;
2311 ctx
->Driver
.RectFunc
= NULL
;
2312 ctx
->Driver
.RenderVBClippedTab
= NULL
;
2313 ctx
->Driver
.RenderVBCulledTab
= NULL
;
2314 ctx
->Driver
.RenderVBRawTab
= NULL
;
2317 * Here the driver sets up all the ctx->Driver function pointers to
2318 * it's specific, private functions.
2320 ctx
->Driver
.UpdateState(ctx
);
2322 if (MESA_VERBOSE
&VERBOSE_CULL
)
2323 gl_print_tri_caps("indirect tris", ctx
->IndirectTriangles
);
2326 * In case the driver didn't hook in an optimized point, line or
2327 * triangle function we'll now select "core/fallback" point, line
2328 * and triangle functions.
2330 if (ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) {
2331 gl_set_point_function(ctx
);
2332 gl_set_line_function(ctx
);
2333 gl_set_triangle_function(ctx
);
2334 gl_set_quad_function(ctx
);
2336 if ((ctx
->IndirectTriangles
&
2337 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
)) ==
2338 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
))
2339 ctx
->IndirectTriangles
&= ~DD_TRI_CULL
;
2342 if (MESA_VERBOSE
&VERBOSE_CULL
)
2343 gl_print_tri_caps("indirect tris 2", ctx
->IndirectTriangles
);
2345 gl_set_render_vb_function(ctx
);
2348 /* Should only be calc'd when !need_eye_coords and not culling.
2350 if (ctx
->NewState
& (NEW_MODELVIEW
|NEW_PROJECTION
)) {
2351 if (ctx
->NewState
& NEW_MODELVIEW
) {
2352 gl_matrix_analyze( &ctx
->ModelView
);
2353 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2356 if (ctx
->NewState
& NEW_PROJECTION
) {
2357 gl_matrix_analyze( &ctx
->ProjectionMatrix
);
2358 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2360 if (ctx
->Transform
.AnyClip
) {
2361 gl_update_userclip( ctx
);
2365 gl_calculate_model_project_matrix( ctx
);
2366 ctx
->ModelProjectWinMatrixUptodate
= 0;
2369 /* Figure out whether we can light in object space or not. If we
2370 * can, find the current positions of the lights in object space
2372 if ((ctx
->Enabled
& (ENABLE_POINT_ATTEN
| ENABLE_LIGHT
| ENABLE_FOG
|
2373 ENABLE_TEXGEN0
| ENABLE_TEXGEN1
)) &&
2374 (ctx
->NewState
& (NEW_LIGHTING
|
2382 GLboolean oldcoord
, oldnorm
;
2384 oldcoord
= ctx
->NeedEyeCoords
;
2385 oldnorm
= ctx
->NeedEyeNormals
;
2387 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2388 ctx
->NeedEyeCoords
= ((ctx
->Fog
.Enabled
&& ctx
->Hint
.Fog
!= GL_NICEST
) ||
2389 ctx
->Point
.Attenuated
);
2390 ctx
->NeedEyeNormals
= GL_FALSE
;
2392 if (ctx
->Light
.Enabled
) {
2393 if (ctx
->Light
.Flags
& LIGHT_POSITIONAL
) {
2394 /* Need length for attenuation */
2395 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_LENGTH_PRESERVING
))
2396 ctx
->NeedEyeCoords
= GL_TRUE
;
2397 } else if (ctx
->Light
.NeedVertices
) {
2398 /* Need angle for spot calculations */
2399 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_ANGLE_PRESERVING
))
2400 ctx
->NeedEyeCoords
= GL_TRUE
;
2402 ctx
->NeedEyeNormals
= ctx
->NeedEyeCoords
;
2404 if (ctx
->Texture
.Enabled
|| ctx
->RenderMode
==GL_FEEDBACK
) {
2405 if (ctx
->Texture
.NeedEyeCoords
) ctx
->NeedEyeCoords
= GL_TRUE
;
2406 if (ctx
->Texture
.NeedNormals
)
2407 ctx
->NeedNormals
= ctx
->NeedEyeNormals
= GL_TRUE
;
2410 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
2412 if (ctx
->NeedEyeCoords
)
2413 ctx
->vb_proj_matrix
= &ctx
->ProjectionMatrix
;
2415 if (ctx
->Light
.Enabled
) {
2416 gl_update_lighting_function(ctx
);
2418 if ( (ctx
->NewState
& NEW_LIGHTING
) ||
2419 ((ctx
->NewState
& (NEW_MODELVIEW
| NEW_PROJECTION
)) &&
2420 !ctx
->NeedEyeCoords
) ||
2421 oldcoord
!= ctx
->NeedEyeCoords
||
2422 oldnorm
!= ctx
->NeedEyeNormals
) {
2423 gl_compute_light_positions(ctx
);
2426 ctx
->rescale_factor
= 1.0F
;
2428 if (ctx
->ModelView
.flags
& (MAT_FLAG_UNIFORM_SCALE
|
2429 MAT_FLAG_GENERAL_SCALE
|
2430 MAT_FLAG_GENERAL_3D
|
2434 GLfloat
*m
= ctx
->ModelView
.inv
;
2435 GLfloat f
= m
[2]*m
[2] + m
[6]*m
[6] + m
[10]*m
[10];
2436 if (f
> 1e-12 && (f
-1)*(f
-1) > 1e-12)
2437 ctx
->rescale_factor
= 1.0/GL_SQRT(f
);
2441 gl_update_normal_transform( ctx
);
2444 gl_update_pipelines(ctx
);