1 /* $Id: context.c,v 1.22 1999/11/19 22:51:29 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"
67 #include "simple_list.h"
71 #include "translate.h"
89 /**********************************************************************/
90 /***** Context and Thread management *****/
91 /**********************************************************************/
97 static MesaTSD mesa_ctx_tsd
;
98 static void mesa_ctx_thread_init() {
99 MesaInitTSD(&mesa_ctx_tsd
);
104 /* One Current Context pointer for all threads in the address space */
105 GLcontext
*_mesa_current_context
= NULL
;
106 struct immediate
*CURRENT_INPUT
= NULL
;
113 /**********************************************************************/
114 /***** Profiling functions *****/
115 /**********************************************************************/
119 #include <sys/times.h>
120 #include <sys/param.h>
124 * Return system time in seconds.
125 * NOTE: this implementation may not be very portable!
127 GLdouble
gl_time( void )
129 static GLdouble prev_time
= 0.0;
130 static GLdouble time
;
137 time
= (double)clk
/ (double)CLK_TCK
;
139 time
= (double)clk
/ (double)HZ
;
142 if (time
>prev_time
) {
152 * Reset the timing/profiling counters
154 static void init_timings( GLcontext
*ctx
)
156 ctx
->BeginEndCount
= 0;
157 ctx
->BeginEndTime
= 0.0;
158 ctx
->VertexCount
= 0;
159 ctx
->VertexTime
= 0.0;
161 ctx
->PointTime
= 0.0;
164 ctx
->PolygonCount
= 0;
165 ctx
->PolygonTime
= 0.0;
167 ctx
->ClearTime
= 0.0;
174 * Print the accumulated timing/profiling data.
176 static void print_timings( GLcontext
*ctx
)
178 GLdouble beginendrate
;
182 GLdouble polygonrate
;
186 GLdouble avgvertices
;
188 if (ctx
->BeginEndTime
>0.0) {
189 beginendrate
= ctx
->BeginEndCount
/ ctx
->BeginEndTime
;
194 if (ctx
->VertexTime
>0.0) {
195 vertexrate
= ctx
->VertexCount
/ ctx
->VertexTime
;
200 if (ctx
->PointTime
>0.0) {
201 pointrate
= ctx
->PointCount
/ ctx
->PointTime
;
206 if (ctx
->LineTime
>0.0) {
207 linerate
= ctx
->LineCount
/ ctx
->LineTime
;
212 if (ctx
->PolygonTime
>0.0) {
213 polygonrate
= ctx
->PolygonCount
/ ctx
->PolygonTime
;
218 if (ctx
->ClearTime
>0.0) {
219 clearrate
= ctx
->ClearCount
/ ctx
->ClearTime
;
224 if (ctx
->SwapTime
>0.0) {
225 swaprate
= ctx
->SwapCount
/ ctx
->SwapTime
;
231 if (ctx
->BeginEndCount
>0) {
232 avgvertices
= (GLdouble
) ctx
->VertexCount
/ (GLdouble
) ctx
->BeginEndCount
;
238 overhead
= ctx
->BeginEndTime
- ctx
->VertexTime
- ctx
->PointTime
239 - ctx
->LineTime
- ctx
->PolygonTime
;
242 printf(" Count Time (s) Rate (/s) \n");
243 printf("--------------------------------------------------------\n");
244 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
245 ctx
->BeginEndCount
, ctx
->BeginEndTime
, beginendrate
);
246 printf(" vertexes transformed %7d %8.3f %10.3f\n",
247 ctx
->VertexCount
, ctx
->VertexTime
, vertexrate
);
248 printf(" points rasterized %7d %8.3f %10.3f\n",
249 ctx
->PointCount
, ctx
->PointTime
, pointrate
);
250 printf(" lines rasterized %7d %8.3f %10.3f\n",
251 ctx
->LineCount
, ctx
->LineTime
, linerate
);
252 printf(" polygons rasterized %7d %8.3f %10.3f\n",
253 ctx
->PolygonCount
, ctx
->PolygonTime
, polygonrate
);
254 printf(" overhead %8.3f\n", overhead
);
255 printf("glClear %7d %8.3f %10.3f\n",
256 ctx
->ClearCount
, ctx
->ClearTime
, clearrate
);
257 printf("SwapBuffers %7d %8.3f %10.3f\n",
258 ctx
->SwapCount
, ctx
->SwapTime
, swaprate
);
261 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices
);
269 /**********************************************************************/
270 /***** Context allocation, initialization, destroying *****/
271 /**********************************************************************/
275 * This function just calls all the various one-time-init functions in Mesa.
277 static void one_time_init( void )
279 static GLboolean alreadyCalled
= GL_FALSE
;
280 if (!alreadyCalled
) {
288 gl_init_transformation();
293 alreadyCalled
= GL_TRUE
;
295 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
296 fprintf(stderr
, "Mesa DEBUG build %s %s\n", __DATE__
, __TIME__
);
302 * Allocate and initialize a shared context state structure.
304 static struct gl_shared_state
*alloc_shared_state( void )
307 struct gl_shared_state
*ss
;
308 GLboolean outOfMemory
;
310 ss
= CALLOC_STRUCT(gl_shared_state
);
314 ss
->DisplayList
= NewHashTable();
316 ss
->TexObjects
= NewHashTable();
318 /* Default Texture objects */
319 outOfMemory
= GL_FALSE
;
320 for (d
= 1 ; d
<= 3 ; d
++) {
321 ss
->DefaultD
[d
] = gl_alloc_texture_object(ss
, 0, d
);
322 if (!ss
->DefaultD
[d
]) {
323 outOfMemory
= GL_TRUE
;
326 ss
->DefaultD
[d
]->RefCount
++; /* don't free if not in use */
329 if (!ss
->DisplayList
|| !ss
->TexObjects
|| outOfMemory
) {
330 /* Ran out of memory at some point. Free everything and return NULL */
332 DeleteHashTable(ss
->DisplayList
);
334 DeleteHashTable(ss
->TexObjects
);
336 gl_free_texture_object(ss
, ss
->DefaultD
[1]);
338 gl_free_texture_object(ss
, ss
->DefaultD
[2]);
340 gl_free_texture_object(ss
, ss
->DefaultD
[3]);
351 * Deallocate a shared state context and all children structures.
353 static void free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
355 /* Free display lists */
357 GLuint list
= HashFirstEntry(ss
->DisplayList
);
359 gl_destroy_list(ctx
, list
);
365 DeleteHashTable(ss
->DisplayList
);
367 /* Free texture objects */
368 while (ss
->TexObjectList
)
370 if (ctx
->Driver
.DeleteTexture
)
371 (*ctx
->Driver
.DeleteTexture
)( ctx
, ss
->TexObjectList
);
372 /* this function removes from linked list too! */
373 gl_free_texture_object(ss
, ss
->TexObjectList
);
375 DeleteHashTable(ss
->TexObjects
);
386 * Initialize the nth light. Note that the defaults for light 0 are
387 * different than the other lights.
389 static void init_light( struct gl_light
*l
, GLuint n
)
391 make_empty_list( l
);
393 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
395 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
396 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
399 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
400 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
402 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
403 ASSIGN_3V( l
->EyeDirection
, 0.0, 0.0, -1.0 );
404 l
->SpotExponent
= 0.0;
405 gl_compute_spot_exp_table( l
);
406 l
->SpotCutoff
= 180.0;
407 l
->CosCutoff
= 0.0; /* KW: -ve values not admitted */
408 l
->ConstantAttenuation
= 1.0;
409 l
->LinearAttenuation
= 0.0;
410 l
->QuadraticAttenuation
= 0.0;
411 l
->Enabled
= GL_FALSE
;
416 static void init_lightmodel( struct gl_lightmodel
*lm
)
418 ASSIGN_4V( lm
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
419 lm
->LocalViewer
= GL_FALSE
;
420 lm
->TwoSide
= GL_FALSE
;
421 lm
->ColorControl
= GL_SINGLE_COLOR
;
425 static void init_material( struct gl_material
*m
)
427 ASSIGN_4V( m
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
428 ASSIGN_4V( m
->Diffuse
, 0.8, 0.8, 0.8, 1.0 );
429 ASSIGN_4V( m
->Specular
, 0.0, 0.0, 0.0, 1.0 );
430 ASSIGN_4V( m
->Emission
, 0.0, 0.0, 0.0, 1.0 );
434 m
->SpecularIndex
= 1;
439 static void init_texture_unit( GLcontext
*ctx
, GLuint unit
)
441 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
443 texUnit
->EnvMode
= GL_MODULATE
;
444 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
445 texUnit
->TexGenEnabled
= 0;
446 texUnit
->GenModeS
= GL_EYE_LINEAR
;
447 texUnit
->GenModeT
= GL_EYE_LINEAR
;
448 texUnit
->GenModeR
= GL_EYE_LINEAR
;
449 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
450 /* Yes, these plane coefficients are correct! */
451 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
452 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
453 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
454 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
455 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
456 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
457 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
458 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
460 texUnit
->CurrentD
[1] = ctx
->Shared
->DefaultD
[1];
461 texUnit
->CurrentD
[2] = ctx
->Shared
->DefaultD
[2];
462 texUnit
->CurrentD
[3] = ctx
->Shared
->DefaultD
[3];
466 static void init_fallback_arrays( GLcontext
*ctx
)
468 struct gl_client_array
*cl
;
471 cl
= &ctx
->Fallback
.Normal
;
476 cl
->Ptr
= (void *) ctx
->Current
.Normal
;
479 cl
= &ctx
->Fallback
.Color
;
481 cl
->Type
= GL_UNSIGNED_BYTE
;
484 cl
->Ptr
= (void *) ctx
->Current
.ByteColor
;
487 cl
= &ctx
->Fallback
.Index
;
489 cl
->Type
= GL_UNSIGNED_INT
;
492 cl
->Ptr
= (void *) &ctx
->Current
.Index
;
495 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++) {
496 cl
= &ctx
->Fallback
.TexCoord
[i
];
501 cl
->Ptr
= (void *) ctx
->Current
.Texcoord
[i
];
505 cl
= &ctx
->Fallback
.EdgeFlag
;
507 cl
->Type
= GL_UNSIGNED_BYTE
;
510 cl
->Ptr
= (void *) &ctx
->Current
.EdgeFlag
;
514 /* Initialize a 1-D evaluator map */
515 static void init_1d_map( struct gl_1d_map
*map
, int n
, const float *initial
)
520 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
524 map
->Points
[i
] = initial
[i
];
529 /* Initialize a 2-D evaluator map */
530 static void init_2d_map( struct gl_2d_map
*map
, int n
, const float *initial
)
538 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
542 map
->Points
[i
] = initial
[i
];
547 static void init_color_table( struct gl_color_table
*p
)
554 p
->IntFormat
= GL_RGBA
;
560 * Initialize a gl_context structure to default values.
562 static void initialize_context( GLcontext
*ctx
)
567 /* Constants, may be overriden by device driver */
568 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
569 ctx
->Const
.MaxTextureSize
= 1 << (MAX_TEXTURE_LEVELS
- 1);
570 ctx
->Const
.MaxTextureUnits
= MAX_TEXTURE_UNITS
;
571 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
573 /* Modelview matrix */
574 gl_matrix_ctr( &ctx
->ModelView
);
575 gl_matrix_alloc_inv( &ctx
->ModelView
);
577 ctx
->ModelViewStackDepth
= 0;
578 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
579 gl_matrix_ctr( &ctx
->ModelViewStack
[i
] );
580 gl_matrix_alloc_inv( &ctx
->ModelViewStack
[i
] );
583 /* Projection matrix - need inv for user clipping in clip space*/
584 gl_matrix_ctr( &ctx
->ProjectionMatrix
);
585 gl_matrix_alloc_inv( &ctx
->ProjectionMatrix
);
587 gl_matrix_ctr( &ctx
->ModelProjectMatrix
);
588 gl_matrix_ctr( &ctx
->ModelProjectWinMatrix
);
589 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
591 ctx
->ProjectionStackDepth
= 0;
592 ctx
->NearFarStack
[0][0] = 1.0; /* These values seem weird by make */
593 ctx
->NearFarStack
[0][1] = 0.0; /* sense mathematically. */
595 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
596 gl_matrix_ctr( &ctx
->ProjectionStack
[i
] );
597 gl_matrix_alloc_inv( &ctx
->ProjectionStack
[i
] );
601 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++) {
602 gl_matrix_ctr( &ctx
->TextureMatrix
[i
] );
603 ctx
->TextureStackDepth
[i
] = 0;
604 for (j
= 0 ; j
< MAX_TEXTURE_STACK_DEPTH
; j
++) {
605 ctx
->TextureStack
[i
][j
].inv
= 0;
609 /* Accumulate buffer group */
610 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
612 /* Color buffer group */
613 ctx
->Color
.IndexMask
= 0xffffffff;
614 ctx
->Color
.ColorMask
[0] = 0xff;
615 ctx
->Color
.ColorMask
[1] = 0xff;
616 ctx
->Color
.ColorMask
[2] = 0xff;
617 ctx
->Color
.ColorMask
[3] = 0xff;
618 ctx
->Color
.SWmasking
= GL_FALSE
;
619 ctx
->Color
.ClearIndex
= 0;
620 ASSIGN_4V( ctx
->Color
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
621 ctx
->Color
.DrawBuffer
= GL_FRONT
;
622 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
623 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
624 ctx
->Color
.AlphaRef
= 0;
625 ctx
->Color
.BlendEnabled
= GL_FALSE
;
626 ctx
->Color
.BlendSrcRGB
= GL_ONE
;
627 ctx
->Color
.BlendDstRGB
= GL_ZERO
;
628 ctx
->Color
.BlendSrcA
= GL_ONE
;
629 ctx
->Color
.BlendDstA
= GL_ZERO
;
630 ctx
->Color
.BlendEquation
= GL_FUNC_ADD_EXT
;
631 ctx
->Color
.BlendFunc
= NULL
; /* this pointer set only when needed */
632 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
633 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
634 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
635 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
636 ctx
->Color
.LogicOp
= GL_COPY
;
637 ctx
->Color
.DitherFlag
= GL_TRUE
;
638 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
641 ASSIGN_4V( ctx
->Current
.ByteColor
, 255, 255, 255, 255);
642 ctx
->Current
.Index
= 1;
643 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
644 ASSIGN_4V( ctx
->Current
.Texcoord
[i
], 0.0, 0.0, 0.0, 1.0 );
645 ASSIGN_4V( ctx
->Current
.RasterPos
, 0.0, 0.0, 0.0, 1.0 );
646 ctx
->Current
.RasterDistance
= 0.0;
647 ASSIGN_4V( ctx
->Current
.RasterColor
, 1.0, 1.0, 1.0, 1.0 );
648 ctx
->Current
.RasterIndex
= 1;
649 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
650 ASSIGN_4V( ctx
->Current
.RasterMultiTexCoord
[i
], 0.0, 0.0, 0.0, 1.0 );
651 ctx
->Current
.RasterTexCoord
= ctx
->Current
.RasterMultiTexCoord
[0];
652 ctx
->Current
.RasterPosValid
= GL_TRUE
;
653 ctx
->Current
.EdgeFlag
= GL_TRUE
;
654 ASSIGN_3V( ctx
->Current
.Normal
, 0.0, 0.0, 1.0 );
655 ctx
->Current
.Primitive
= (GLenum
) (GL_POLYGON
+ 1);
657 ctx
->Current
.Flag
= (VERT_NORM
|VERT_INDEX
|VERT_RGBA
|VERT_EDGE
|
658 VERT_TEX0_1
|VERT_TEX1_1
|VERT_MATERIAL
);
660 init_fallback_arrays( ctx
);
662 /* Depth buffer group */
663 ctx
->Depth
.Test
= GL_FALSE
;
664 ctx
->Depth
.Clear
= 1.0;
665 ctx
->Depth
.Func
= GL_LESS
;
666 ctx
->Depth
.Mask
= GL_TRUE
;
668 /* Evaluators group */
669 ctx
->Eval
.Map1Color4
= GL_FALSE
;
670 ctx
->Eval
.Map1Index
= GL_FALSE
;
671 ctx
->Eval
.Map1Normal
= GL_FALSE
;
672 ctx
->Eval
.Map1TextureCoord1
= GL_FALSE
;
673 ctx
->Eval
.Map1TextureCoord2
= GL_FALSE
;
674 ctx
->Eval
.Map1TextureCoord3
= GL_FALSE
;
675 ctx
->Eval
.Map1TextureCoord4
= GL_FALSE
;
676 ctx
->Eval
.Map1Vertex3
= GL_FALSE
;
677 ctx
->Eval
.Map1Vertex4
= GL_FALSE
;
678 ctx
->Eval
.Map2Color4
= GL_FALSE
;
679 ctx
->Eval
.Map2Index
= GL_FALSE
;
680 ctx
->Eval
.Map2Normal
= GL_FALSE
;
681 ctx
->Eval
.Map2TextureCoord1
= GL_FALSE
;
682 ctx
->Eval
.Map2TextureCoord2
= GL_FALSE
;
683 ctx
->Eval
.Map2TextureCoord3
= GL_FALSE
;
684 ctx
->Eval
.Map2TextureCoord4
= GL_FALSE
;
685 ctx
->Eval
.Map2Vertex3
= GL_FALSE
;
686 ctx
->Eval
.Map2Vertex4
= GL_FALSE
;
687 ctx
->Eval
.AutoNormal
= GL_FALSE
;
688 ctx
->Eval
.MapGrid1un
= 1;
689 ctx
->Eval
.MapGrid1u1
= 0.0;
690 ctx
->Eval
.MapGrid1u2
= 1.0;
691 ctx
->Eval
.MapGrid2un
= 1;
692 ctx
->Eval
.MapGrid2vn
= 1;
693 ctx
->Eval
.MapGrid2u1
= 0.0;
694 ctx
->Eval
.MapGrid2u2
= 1.0;
695 ctx
->Eval
.MapGrid2v1
= 0.0;
696 ctx
->Eval
.MapGrid2v2
= 1.0;
700 static GLfloat vertex
[4] = { 0.0, 0.0, 0.0, 1.0 };
701 static GLfloat normal
[3] = { 0.0, 0.0, 1.0 };
702 static GLfloat index
[1] = { 1.0 };
703 static GLfloat color
[4] = { 1.0, 1.0, 1.0, 1.0 };
704 static GLfloat texcoord
[4] = { 0.0, 0.0, 0.0, 1.0 };
706 init_1d_map( &ctx
->EvalMap
.Map1Vertex3
, 3, vertex
);
707 init_1d_map( &ctx
->EvalMap
.Map1Vertex4
, 4, vertex
);
708 init_1d_map( &ctx
->EvalMap
.Map1Index
, 1, index
);
709 init_1d_map( &ctx
->EvalMap
.Map1Color4
, 4, color
);
710 init_1d_map( &ctx
->EvalMap
.Map1Normal
, 3, normal
);
711 init_1d_map( &ctx
->EvalMap
.Map1Texture1
, 1, texcoord
);
712 init_1d_map( &ctx
->EvalMap
.Map1Texture2
, 2, texcoord
);
713 init_1d_map( &ctx
->EvalMap
.Map1Texture3
, 3, texcoord
);
714 init_1d_map( &ctx
->EvalMap
.Map1Texture4
, 4, texcoord
);
716 init_2d_map( &ctx
->EvalMap
.Map2Vertex3
, 3, vertex
);
717 init_2d_map( &ctx
->EvalMap
.Map2Vertex4
, 4, vertex
);
718 init_2d_map( &ctx
->EvalMap
.Map2Index
, 1, index
);
719 init_2d_map( &ctx
->EvalMap
.Map2Color4
, 4, color
);
720 init_2d_map( &ctx
->EvalMap
.Map2Normal
, 3, normal
);
721 init_2d_map( &ctx
->EvalMap
.Map2Texture1
, 1, texcoord
);
722 init_2d_map( &ctx
->EvalMap
.Map2Texture2
, 2, texcoord
);
723 init_2d_map( &ctx
->EvalMap
.Map2Texture3
, 3, texcoord
);
724 init_2d_map( &ctx
->EvalMap
.Map2Texture4
, 4, texcoord
);
728 ctx
->Fog
.Enabled
= GL_FALSE
;
729 ctx
->Fog
.Mode
= GL_EXP
;
730 ASSIGN_4V( ctx
->Fog
.Color
, 0.0, 0.0, 0.0, 0.0 );
731 ctx
->Fog
.Index
= 0.0;
732 ctx
->Fog
.Density
= 1.0;
733 ctx
->Fog
.Start
= 0.0;
737 ctx
->Hint
.PerspectiveCorrection
= GL_DONT_CARE
;
738 ctx
->Hint
.PointSmooth
= GL_DONT_CARE
;
739 ctx
->Hint
.LineSmooth
= GL_DONT_CARE
;
740 ctx
->Hint
.PolygonSmooth
= GL_DONT_CARE
;
741 ctx
->Hint
.Fog
= GL_DONT_CARE
;
743 ctx
->Hint
.AllowDrawWin
= GL_TRUE
;
744 ctx
->Hint
.AllowDrawSpn
= GL_TRUE
;
745 ctx
->Hint
.AllowDrawMem
= GL_TRUE
;
746 ctx
->Hint
.StrictLighting
= GL_TRUE
;
749 gl_pipeline_init( ctx
);
753 gl_extensions_ctr( ctx
);
755 ctx
->AllowVertexCull
= CLIP_CULLED_BIT
;
758 for (i
=0;i
<MAX_LIGHTS
;i
++) {
759 init_light( &ctx
->Light
.Light
[i
], i
);
761 make_empty_list( &ctx
->Light
.EnabledList
);
763 init_lightmodel( &ctx
->Light
.Model
);
764 init_material( &ctx
->Light
.Material
[0] );
765 init_material( &ctx
->Light
.Material
[1] );
766 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
767 ctx
->Light
.Enabled
= GL_FALSE
;
768 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
769 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
770 ctx
->Light
.ColorMaterialBitmask
771 = gl_material_bitmask( ctx
,
773 GL_AMBIENT_AND_DIFFUSE
, ~0, 0 );
775 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
778 ctx
->Line
.SmoothFlag
= GL_FALSE
;
779 ctx
->Line
.StippleFlag
= GL_FALSE
;
780 ctx
->Line
.Width
= 1.0;
781 ctx
->Line
.StipplePattern
= 0xffff;
782 ctx
->Line
.StippleFactor
= 1;
784 /* Display List group */
785 ctx
->List
.ListBase
= 0;
788 ctx
->Pixel
.RedBias
= 0.0;
789 ctx
->Pixel
.RedScale
= 1.0;
790 ctx
->Pixel
.GreenBias
= 0.0;
791 ctx
->Pixel
.GreenScale
= 1.0;
792 ctx
->Pixel
.BlueBias
= 0.0;
793 ctx
->Pixel
.BlueScale
= 1.0;
794 ctx
->Pixel
.AlphaBias
= 0.0;
795 ctx
->Pixel
.AlphaScale
= 1.0;
796 ctx
->Pixel
.ScaleOrBiasRGBA
= GL_FALSE
;
797 ctx
->Pixel
.DepthBias
= 0.0;
798 ctx
->Pixel
.DepthScale
= 1.0;
799 ctx
->Pixel
.IndexOffset
= 0;
800 ctx
->Pixel
.IndexShift
= 0;
801 ctx
->Pixel
.ZoomX
= 1.0;
802 ctx
->Pixel
.ZoomY
= 1.0;
803 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
804 ctx
->Pixel
.MapStencilFlag
= GL_FALSE
;
805 ctx
->Pixel
.MapStoSsize
= 1;
806 ctx
->Pixel
.MapItoIsize
= 1;
807 ctx
->Pixel
.MapItoRsize
= 1;
808 ctx
->Pixel
.MapItoGsize
= 1;
809 ctx
->Pixel
.MapItoBsize
= 1;
810 ctx
->Pixel
.MapItoAsize
= 1;
811 ctx
->Pixel
.MapRtoRsize
= 1;
812 ctx
->Pixel
.MapGtoGsize
= 1;
813 ctx
->Pixel
.MapBtoBsize
= 1;
814 ctx
->Pixel
.MapAtoAsize
= 1;
815 ctx
->Pixel
.MapStoS
[0] = 0;
816 ctx
->Pixel
.MapItoI
[0] = 0;
817 ctx
->Pixel
.MapItoR
[0] = 0.0;
818 ctx
->Pixel
.MapItoG
[0] = 0.0;
819 ctx
->Pixel
.MapItoB
[0] = 0.0;
820 ctx
->Pixel
.MapItoA
[0] = 0.0;
821 ctx
->Pixel
.MapItoR8
[0] = 0;
822 ctx
->Pixel
.MapItoG8
[0] = 0;
823 ctx
->Pixel
.MapItoB8
[0] = 0;
824 ctx
->Pixel
.MapItoA8
[0] = 0;
825 ctx
->Pixel
.MapRtoR
[0] = 0.0;
826 ctx
->Pixel
.MapGtoG
[0] = 0.0;
827 ctx
->Pixel
.MapBtoB
[0] = 0.0;
828 ctx
->Pixel
.MapAtoA
[0] = 0.0;
831 ctx
->Point
.SmoothFlag
= GL_FALSE
;
832 ctx
->Point
.Size
= 1.0;
833 ctx
->Point
.Params
[0] = 1.0;
834 ctx
->Point
.Params
[1] = 0.0;
835 ctx
->Point
.Params
[2] = 0.0;
836 ctx
->Point
.Attenuated
= GL_FALSE
;
837 ctx
->Point
.MinSize
= 0.0;
838 ctx
->Point
.MaxSize
= (GLfloat
) MAX_POINT_SIZE
;
839 ctx
->Point
.Threshold
= 1.0;
842 ctx
->Polygon
.CullFlag
= GL_FALSE
;
843 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
844 ctx
->Polygon
.FrontFace
= GL_CCW
;
845 ctx
->Polygon
.FrontBit
= 0;
846 ctx
->Polygon
.FrontMode
= GL_FILL
;
847 ctx
->Polygon
.BackMode
= GL_FILL
;
848 ctx
->Polygon
.Unfilled
= GL_FALSE
;
849 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
850 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
851 ctx
->Polygon
.OffsetFactor
= 0.0F
;
852 ctx
->Polygon
.OffsetUnits
= 0.0F
;
853 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
854 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
855 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
857 /* Polygon Stipple group */
858 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );
861 ctx
->Scissor
.Enabled
= GL_FALSE
;
864 ctx
->Scissor
.Width
= 0;
865 ctx
->Scissor
.Height
= 0;
868 ctx
->Stencil
.Enabled
= GL_FALSE
;
869 ctx
->Stencil
.Function
= GL_ALWAYS
;
870 ctx
->Stencil
.FailFunc
= GL_KEEP
;
871 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
872 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
873 ctx
->Stencil
.Ref
= 0;
874 ctx
->Stencil
.ValueMask
= 0xff;
875 ctx
->Stencil
.Clear
= 0;
876 ctx
->Stencil
.WriteMask
= 0xff;
879 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
880 ctx
->Texture
.CurrentTransformUnit
= 0; /* multitexture */
881 ctx
->Texture
.Enabled
= 0;
883 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
884 init_texture_unit( ctx
, i
);
886 init_color_table(&ctx
->Texture
.Palette
);
888 /* Transformation group */
889 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
890 ctx
->Transform
.Normalize
= GL_FALSE
;
891 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
892 for (i
=0;i
<MAX_CLIP_PLANES
;i
++) {
893 ctx
->Transform
.ClipEnabled
[i
] = GL_FALSE
;
894 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
896 ctx
->Transform
.AnyClip
= GL_FALSE
;
901 ctx
->Viewport
.Width
= 0;
902 ctx
->Viewport
.Height
= 0;
903 ctx
->Viewport
.Near
= 0.0;
904 ctx
->Viewport
.Far
= 1.0;
905 gl_matrix_ctr(&ctx
->Viewport
.WindowMap
);
909 ctx
->Viewport
.WindowMap
.m
[Sz
] = 0.5 * DEPTH_SCALE
;
910 ctx
->Viewport
.WindowMap
.m
[Tz
] = 0.5 * DEPTH_SCALE
;
914 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
915 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
918 ctx
->Array
.Vertex
.Size
= 4;
919 ctx
->Array
.Vertex
.Type
= GL_FLOAT
;
920 ctx
->Array
.Vertex
.Stride
= 0;
921 ctx
->Array
.Vertex
.StrideB
= 0;
922 ctx
->Array
.Vertex
.Ptr
= NULL
;
923 ctx
->Array
.Vertex
.Enabled
= GL_FALSE
;
924 ctx
->Array
.Normal
.Type
= GL_FLOAT
;
925 ctx
->Array
.Normal
.Stride
= 0;
926 ctx
->Array
.Normal
.StrideB
= 0;
927 ctx
->Array
.Normal
.Ptr
= NULL
;
928 ctx
->Array
.Normal
.Enabled
= GL_FALSE
;
929 ctx
->Array
.Color
.Size
= 4;
930 ctx
->Array
.Color
.Type
= GL_FLOAT
;
931 ctx
->Array
.Color
.Stride
= 0;
932 ctx
->Array
.Color
.StrideB
= 0;
933 ctx
->Array
.Color
.Ptr
= NULL
;
934 ctx
->Array
.Color
.Enabled
= GL_FALSE
;
935 ctx
->Array
.Index
.Type
= GL_FLOAT
;
936 ctx
->Array
.Index
.Stride
= 0;
937 ctx
->Array
.Index
.StrideB
= 0;
938 ctx
->Array
.Index
.Ptr
= NULL
;
939 ctx
->Array
.Index
.Enabled
= GL_FALSE
;
940 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
941 ctx
->Array
.TexCoord
[i
].Size
= 4;
942 ctx
->Array
.TexCoord
[i
].Type
= GL_FLOAT
;
943 ctx
->Array
.TexCoord
[i
].Stride
= 0;
944 ctx
->Array
.TexCoord
[i
].StrideB
= 0;
945 ctx
->Array
.TexCoord
[i
].Ptr
= NULL
;
946 ctx
->Array
.TexCoord
[i
].Enabled
= GL_FALSE
;
948 ctx
->Array
.TexCoordInterleaveFactor
= 1;
949 ctx
->Array
.EdgeFlag
.Stride
= 0;
950 ctx
->Array
.EdgeFlag
.StrideB
= 0;
951 ctx
->Array
.EdgeFlag
.Ptr
= NULL
;
952 ctx
->Array
.EdgeFlag
.Enabled
= GL_FALSE
;
953 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
956 ctx
->Pack
.Alignment
= 4;
957 ctx
->Pack
.RowLength
= 0;
958 ctx
->Pack
.ImageHeight
= 0;
959 ctx
->Pack
.SkipPixels
= 0;
960 ctx
->Pack
.SkipRows
= 0;
961 ctx
->Pack
.SkipImages
= 0;
962 ctx
->Pack
.SwapBytes
= GL_FALSE
;
963 ctx
->Pack
.LsbFirst
= GL_FALSE
;
964 ctx
->Unpack
.Alignment
= 4;
965 ctx
->Unpack
.RowLength
= 0;
966 ctx
->Unpack
.ImageHeight
= 0;
967 ctx
->Unpack
.SkipPixels
= 0;
968 ctx
->Unpack
.SkipRows
= 0;
969 ctx
->Unpack
.SkipImages
= 0;
970 ctx
->Unpack
.SwapBytes
= GL_FALSE
;
971 ctx
->Unpack
.LsbFirst
= GL_FALSE
;
974 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
975 ctx
->Feedback
.Buffer
= NULL
;
976 ctx
->Feedback
.BufferSize
= 0;
977 ctx
->Feedback
.Count
= 0;
979 /* Selection/picking */
980 ctx
->Select
.Buffer
= NULL
;
981 ctx
->Select
.BufferSize
= 0;
982 ctx
->Select
.BufferCount
= 0;
983 ctx
->Select
.Hits
= 0;
984 ctx
->Select
.NameStackDepth
= 0;
986 /* Optimized Accum buffer */
987 ctx
->IntegerAccumMode
= GL_TRUE
;
988 ctx
->IntegerAccumScaler
= 0.0;
990 /* Renderer and client attribute stacks */
991 ctx
->AttribStackDepth
= 0;
992 ctx
->ClientAttribStackDepth
= 0;
994 /*** Miscellaneous ***/
995 ctx
->NewState
= NEW_ALL
;
996 ctx
->RenderMode
= GL_RENDER
;
997 ctx
->StippleCounter
= 0;
998 ctx
->NeedNormals
= GL_FALSE
;
999 ctx
->DoViewportMapping
= GL_TRUE
;
1001 ctx
->NeedEyeCoords
= GL_FALSE
;
1002 ctx
->NeedEyeNormals
= GL_FALSE
;
1003 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
1007 ctx
->ExecuteFlag
= GL_TRUE
;
1008 ctx
->CompileFlag
= GL_FALSE
;
1009 ctx
->CurrentListPtr
= NULL
;
1010 ctx
->CurrentBlock
= NULL
;
1011 ctx
->CurrentListNum
= 0;
1012 ctx
->CurrentPos
= 0;
1014 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1016 ctx
->CatchSignals
= GL_TRUE
;
1018 /* For debug/development only */
1019 ctx
->NoRaster
= getenv("MESA_NO_RASTER") ? GL_TRUE
: GL_FALSE
;
1021 /* Dither disable */
1022 ctx
->NoDither
= getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
1023 if (ctx
->NoDither
) {
1024 if (getenv("MESA_DEBUG")) {
1025 fprintf(stderr
, "MESA_NO_DITHER set - dithering disabled\n");
1027 ctx
->Color
.DitherFlag
= GL_FALSE
;
1035 * Allocate a new GLvisual object.
1036 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
1037 * alphaFlag - alloc software alpha buffers?
1038 * dbFlag - double buffering?
1039 * stereoFlag - stereo buffer?
1040 * depthFits - requested minimum bits per depth buffer value
1041 * stencilFits - requested minimum bits per stencil buffer value
1042 * accumFits - requested minimum bits per accum buffer component
1043 * indexFits - number of bits per pixel if rgbFlag==GL_FALSE
1044 * red/green/blue/alphaFits - number of bits per color component
1045 * in frame buffer for RGB(A) mode.
1046 * Return: pointer to new GLvisual or NULL if requested parameters can't
1049 GLvisual
*gl_create_visual( GLboolean rgbFlag
,
1050 GLboolean alphaFlag
,
1052 GLboolean stereoFlag
,
1064 if (depthBits
> (GLint
) (8*sizeof(GLdepth
))) {
1065 /* can't meet depth buffer requirements */
1068 if (stencilBits
> (GLint
) (8*sizeof(GLstencil
))) {
1069 /* can't meet stencil buffer requirements */
1072 if (accumBits
> (GLint
) (8*sizeof(GLaccum
))) {
1073 /* can't meet accum buffer requirements */
1077 vis
= (GLvisual
*) CALLOC( sizeof(GLvisual
) );
1082 vis
->RGBAflag
= rgbFlag
;
1083 vis
->DBflag
= dbFlag
;
1084 vis
->StereoFlag
= stereoFlag
;
1085 vis
->RedBits
= redBits
;
1086 vis
->GreenBits
= greenBits
;
1087 vis
->BlueBits
= blueBits
;
1088 vis
->AlphaBits
= alphaFlag
? 8*sizeof(GLubyte
) : alphaBits
;
1090 vis
->IndexBits
= indexBits
;
1091 vis
->DepthBits
= (depthBits
>0) ? 8*sizeof(GLdepth
) : 0;
1092 vis
->AccumBits
= (accumBits
>0) ? 8*sizeof(GLaccum
) : 0;
1093 vis
->StencilBits
= (stencilBits
>0) ? 8*sizeof(GLstencil
) : 0;
1095 vis
->SoftwareAlpha
= alphaFlag
;
1102 void gl_destroy_visual( GLvisual
*vis
)
1110 * Allocate the proxy textures. If we run out of memory part way through
1111 * the allocations clean up and return GL_FALSE.
1112 * Return: GL_TRUE=success, GL_FALSE=failure
1114 static GLboolean
alloc_proxy_textures( GLcontext
*ctx
)
1116 GLboolean out_of_memory
;
1119 ctx
->Texture
.Proxy1D
= gl_alloc_texture_object(NULL
, 0, 1);
1120 if (!ctx
->Texture
.Proxy1D
) {
1124 ctx
->Texture
.Proxy2D
= gl_alloc_texture_object(NULL
, 0, 2);
1125 if (!ctx
->Texture
.Proxy2D
) {
1126 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1130 ctx
->Texture
.Proxy3D
= gl_alloc_texture_object(NULL
, 0, 3);
1131 if (!ctx
->Texture
.Proxy3D
) {
1132 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1133 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1137 out_of_memory
= GL_FALSE
;
1138 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1139 ctx
->Texture
.Proxy1D
->Image
[i
] = gl_alloc_texture_image();
1140 ctx
->Texture
.Proxy2D
->Image
[i
] = gl_alloc_texture_image();
1141 ctx
->Texture
.Proxy3D
->Image
[i
] = gl_alloc_texture_image();
1142 if (!ctx
->Texture
.Proxy1D
->Image
[i
]
1143 || !ctx
->Texture
.Proxy2D
->Image
[i
]
1144 || !ctx
->Texture
.Proxy3D
->Image
[i
]) {
1145 out_of_memory
= GL_TRUE
;
1148 if (out_of_memory
) {
1149 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1150 if (ctx
->Texture
.Proxy1D
->Image
[i
]) {
1151 gl_free_texture_image(ctx
->Texture
.Proxy1D
->Image
[i
]);
1153 if (ctx
->Texture
.Proxy2D
->Image
[i
]) {
1154 gl_free_texture_image(ctx
->Texture
.Proxy2D
->Image
[i
]);
1156 if (ctx
->Texture
.Proxy3D
->Image
[i
]) {
1157 gl_free_texture_image(ctx
->Texture
.Proxy3D
->Image
[i
]);
1160 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1161 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1162 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy3D
);
1173 * Allocate and initialize a GLcontext structure.
1174 * Input: visual - a GLvisual pointer
1175 * sharelist - another context to share display lists with or NULL
1176 * driver_ctx - pointer to device driver's context state struct
1177 * Return: pointer to a new gl_context struct or NULL if error.
1179 GLcontext
*gl_create_context( GLvisual
*visual
,
1180 GLcontext
*share_list
,
1187 (void) direct
; /* not used */
1189 /* do some implementation tests */
1190 assert( sizeof(GLbyte
) == 1 );
1191 assert( sizeof(GLshort
) >= 2 );
1192 assert( sizeof(GLint
) >= 4 );
1193 assert( sizeof(GLubyte
) == 1 );
1194 assert( sizeof(GLushort
) >= 2 );
1195 assert( sizeof(GLuint
) >= 4 );
1197 /* misc one-time initializations */
1200 ctx
= (GLcontext
*) CALLOC( sizeof(GLcontext
) );
1205 ctx
->DriverCtx
= driver_ctx
;
1206 ctx
->Visual
= visual
;
1209 ctx
->VB
= gl_vb_create_for_immediate( ctx
);
1214 ctx
->input
= ctx
->VB
->IM
;
1216 ctx
->PB
= gl_alloc_pb();
1224 /* share the group of display lists of another context */
1225 ctx
->Shared
= share_list
->Shared
;
1228 /* allocate new group of display lists */
1229 ctx
->Shared
= alloc_shared_state();
1237 ctx
->Shared
->RefCount
++;
1239 initialize_context( ctx
);
1240 gl_reset_vb( ctx
->VB
);
1241 gl_reset_input( ctx
);
1244 ctx
->ShineTabList
= MALLOC_STRUCT( gl_shine_tab
);
1245 make_empty_list( ctx
->ShineTabList
);
1247 for (i
= 0 ; i
< 10 ; i
++) {
1248 struct gl_shine_tab
*s
= MALLOC_STRUCT( gl_shine_tab
);
1251 insert_at_tail( ctx
->ShineTabList
, s
);
1254 for (i
= 0 ; i
< 4 ; i
++) {
1255 ctx
->ShineTable
[i
] = ctx
->ShineTabList
->prev
;
1256 ctx
->ShineTable
[i
]->refcount
++;
1259 if (visual
->DBflag
) {
1260 ctx
->Color
.DrawBuffer
= GL_BACK
;
1261 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
1262 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
1263 ctx
->Pixel
.ReadBuffer
= GL_BACK
;
1264 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
1267 ctx
->Color
.DrawBuffer
= GL_FRONT
;
1268 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
1269 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
1270 ctx
->Pixel
.ReadBuffer
= GL_FRONT
;
1271 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
1275 /* Fill in some driver defaults now.
1277 ctx
->Driver
.AllocDepthBuffer
= gl_alloc_depth_buffer
;
1278 ctx
->Driver
.ReadDepthSpanFloat
= gl_read_depth_span_float
;
1279 ctx
->Driver
.ReadDepthSpanInt
= gl_read_depth_span_int
;
1282 init_timings( ctx
);
1285 if (!alloc_proxy_textures(ctx
)) {
1286 free_shared_state(ctx
, ctx
->Shared
);
1293 /* setup API dispatch tables */
1294 _mesa_init_exec_table( &ctx
->Exec
);
1295 _mesa_init_dlist_table( &ctx
->Save
);
1296 ctx
->CurrentDispatch
= &ctx
->Exec
;
1301 /* Just reads the config files...
1303 void gl_context_initialize( GLcontext
*ctx
)
1305 gl_read_config_file( ctx
);
1312 * Destroy a gl_context structure.
1314 void gl_destroy_context( GLcontext
*ctx
)
1319 struct gl_shine_tab
*s
, *tmps
;
1322 if (getenv("MESA_PROFILE")) {
1323 print_timings( ctx
);
1327 gl_matrix_dtr( &ctx
->ModelView
);
1328 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
1329 gl_matrix_dtr( &ctx
->ModelViewStack
[i
] );
1331 gl_matrix_dtr( &ctx
->ProjectionMatrix
);
1332 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
1333 gl_matrix_dtr( &ctx
->ProjectionStack
[i
] );
1338 if(ctx
->input
!= ctx
->VB
->IM
)
1339 gl_immediate_free( ctx
->input
);
1341 gl_vb_free( ctx
->VB
);
1343 ctx
->Shared
->RefCount
--;
1344 assert(ctx
->Shared
->RefCount
>=0);
1345 if (ctx
->Shared
->RefCount
==0) {
1346 /* free shared state */
1347 free_shared_state( ctx
, ctx
->Shared
);
1350 foreach_s( s
, tmps
, ctx
->ShineTabList
) {
1353 FREE( ctx
->ShineTabList
);
1355 /* Free proxy texture objects */
1356 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy1D
);
1357 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy2D
);
1358 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy3D
);
1360 /* Free evaluator data */
1361 if (ctx
->EvalMap
.Map1Vertex3
.Points
)
1362 FREE( ctx
->EvalMap
.Map1Vertex3
.Points
);
1363 if (ctx
->EvalMap
.Map1Vertex4
.Points
)
1364 FREE( ctx
->EvalMap
.Map1Vertex4
.Points
);
1365 if (ctx
->EvalMap
.Map1Index
.Points
)
1366 FREE( ctx
->EvalMap
.Map1Index
.Points
);
1367 if (ctx
->EvalMap
.Map1Color4
.Points
)
1368 FREE( ctx
->EvalMap
.Map1Color4
.Points
);
1369 if (ctx
->EvalMap
.Map1Normal
.Points
)
1370 FREE( ctx
->EvalMap
.Map1Normal
.Points
);
1371 if (ctx
->EvalMap
.Map1Texture1
.Points
)
1372 FREE( ctx
->EvalMap
.Map1Texture1
.Points
);
1373 if (ctx
->EvalMap
.Map1Texture2
.Points
)
1374 FREE( ctx
->EvalMap
.Map1Texture2
.Points
);
1375 if (ctx
->EvalMap
.Map1Texture3
.Points
)
1376 FREE( ctx
->EvalMap
.Map1Texture3
.Points
);
1377 if (ctx
->EvalMap
.Map1Texture4
.Points
)
1378 FREE( ctx
->EvalMap
.Map1Texture4
.Points
);
1380 if (ctx
->EvalMap
.Map2Vertex3
.Points
)
1381 FREE( ctx
->EvalMap
.Map2Vertex3
.Points
);
1382 if (ctx
->EvalMap
.Map2Vertex4
.Points
)
1383 FREE( ctx
->EvalMap
.Map2Vertex4
.Points
);
1384 if (ctx
->EvalMap
.Map2Index
.Points
)
1385 FREE( ctx
->EvalMap
.Map2Index
.Points
);
1386 if (ctx
->EvalMap
.Map2Color4
.Points
)
1387 FREE( ctx
->EvalMap
.Map2Color4
.Points
);
1388 if (ctx
->EvalMap
.Map2Normal
.Points
)
1389 FREE( ctx
->EvalMap
.Map2Normal
.Points
);
1390 if (ctx
->EvalMap
.Map2Texture1
.Points
)
1391 FREE( ctx
->EvalMap
.Map2Texture1
.Points
);
1392 if (ctx
->EvalMap
.Map2Texture2
.Points
)
1393 FREE( ctx
->EvalMap
.Map2Texture2
.Points
);
1394 if (ctx
->EvalMap
.Map2Texture3
.Points
)
1395 FREE( ctx
->EvalMap
.Map2Texture3
.Points
);
1396 if (ctx
->EvalMap
.Map2Texture4
.Points
)
1397 FREE( ctx
->EvalMap
.Map2Texture4
.Points
);
1399 /* Free cache of immediate buffers. */
1400 while (ctx
->nr_im_queued
-- > 0) {
1401 struct immediate
* next
= ctx
->freed_im_queue
->next
;
1402 FREE( ctx
->freed_im_queue
);
1403 ctx
->freed_im_queue
= next
;
1405 gl_extensions_dtr(ctx
);
1407 FREE( (void *) ctx
);
1410 if (ctx
== _mesa_current_context
) {
1411 _mesa_current_context
= NULL
;
1412 CURRENT_INPUT
= NULL
;
1422 * Create a new framebuffer. A GLframebuffer is a struct which
1423 * encapsulates the depth, stencil and accum buffers and related
1425 * Input: visual - a GLvisual pointer
1426 * Return: pointer to new GLframebuffer struct or NULL if error.
1428 GLframebuffer
*gl_create_framebuffer( GLvisual
*visual
)
1430 GLframebuffer
*buffer
;
1432 buffer
= (GLframebuffer
*) CALLOC( sizeof(GLframebuffer
) );
1437 buffer
->Visual
= visual
;
1445 * Free a framebuffer struct and its buffers.
1447 void gl_destroy_framebuffer( GLframebuffer
*buffer
)
1450 if (buffer
->Depth
) {
1451 FREE( buffer
->Depth
);
1453 if (buffer
->Accum
) {
1454 FREE( buffer
->Accum
);
1456 if (buffer
->Stencil
) {
1457 FREE( buffer
->Stencil
);
1459 if (buffer
->FrontLeftAlpha
) {
1460 FREE( buffer
->FrontLeftAlpha
);
1462 if (buffer
->BackLeftAlpha
) {
1463 FREE( buffer
->BackLeftAlpha
);
1465 if (buffer
->FrontRightAlpha
) {
1466 FREE( buffer
->FrontRightAlpha
);
1468 if (buffer
->BackRightAlpha
) {
1469 FREE( buffer
->BackRightAlpha
);
1478 * Set the current context, binding the given frame buffer to the context.
1480 void gl_make_current( GLcontext
*newCtx
, GLframebuffer
*buffer
)
1482 GET_CURRENT_CONTEXT(oldCtx
);
1484 /* Flush the old context
1487 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx
, "gl_make_current");
1490 if (oldCtx
&& oldCtx
->Buffer
) {
1491 /* unbind frame buffer from context */
1492 oldCtx
->Buffer
= NULL
;
1496 /* TODO: unbind old buffer from context? */
1497 MesaSetTSD(&mesa_ctx_tsd
, (void *) newCtx
, mesa_ctx_thread_init
);
1499 _mesa_current_context
= newCtx
;
1502 SET_IMMEDIATE(newCtx
, newCtx
->input
);
1506 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1508 _glapi_set_dispatch(NULL
); /* none current */
1510 if (MESA_VERBOSE
) fprintf(stderr
, "gl_make_current()\n");
1512 if (newCtx
&& buffer
) {
1513 /* TODO: check if newCtx and buffer's visual match??? */
1514 newCtx
->Buffer
= buffer
; /* Bind the frame buffer to the context */
1515 newCtx
->NewState
= NEW_ALL
; /* just to be safe */
1516 gl_update_state( newCtx
);
1522 * Return current context handle for the calling thread.
1524 GLcontext
*gl_get_current_context( void )
1527 return (GLcontext
*) MesaGetTSD(&mesa_ctx_tsd
);
1529 return _mesa_current_context
;
1536 * Copy attribute groups from one context to another.
1537 * Input: src - source context
1538 * dst - destination context
1539 * mask - bitwise OR of GL_*_BIT flags
1541 void gl_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1543 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1544 MEMCPY( &dst
->Accum
, &src
->Accum
, sizeof(struct gl_accum_attrib
) );
1546 if (mask
& GL_COLOR_BUFFER_BIT
) {
1547 MEMCPY( &dst
->Color
, &src
->Color
, sizeof(struct gl_colorbuffer_attrib
) );
1549 if (mask
& GL_CURRENT_BIT
) {
1550 MEMCPY( &dst
->Current
, &src
->Current
, sizeof(struct gl_current_attrib
) );
1552 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1553 MEMCPY( &dst
->Depth
, &src
->Depth
, sizeof(struct gl_depthbuffer_attrib
) );
1555 if (mask
& GL_ENABLE_BIT
) {
1558 if (mask
& GL_EVAL_BIT
) {
1559 MEMCPY( &dst
->Eval
, &src
->Eval
, sizeof(struct gl_eval_attrib
) );
1561 if (mask
& GL_FOG_BIT
) {
1562 MEMCPY( &dst
->Fog
, &src
->Fog
, sizeof(struct gl_fog_attrib
) );
1564 if (mask
& GL_HINT_BIT
) {
1565 MEMCPY( &dst
->Hint
, &src
->Hint
, sizeof(struct gl_hint_attrib
) );
1567 if (mask
& GL_LIGHTING_BIT
) {
1568 MEMCPY( &dst
->Light
, &src
->Light
, sizeof(struct gl_light_attrib
) );
1569 /* gl_reinit_light_attrib( &dst->Light ); */
1571 if (mask
& GL_LINE_BIT
) {
1572 MEMCPY( &dst
->Line
, &src
->Line
, sizeof(struct gl_line_attrib
) );
1574 if (mask
& GL_LIST_BIT
) {
1575 MEMCPY( &dst
->List
, &src
->List
, sizeof(struct gl_list_attrib
) );
1577 if (mask
& GL_PIXEL_MODE_BIT
) {
1578 MEMCPY( &dst
->Pixel
, &src
->Pixel
, sizeof(struct gl_pixel_attrib
) );
1580 if (mask
& GL_POINT_BIT
) {
1581 MEMCPY( &dst
->Point
, &src
->Point
, sizeof(struct gl_point_attrib
) );
1583 if (mask
& GL_POLYGON_BIT
) {
1584 MEMCPY( &dst
->Polygon
, &src
->Polygon
, sizeof(struct gl_polygon_attrib
) );
1586 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1587 /* Use loop instead of MEMCPY due to problem with Portland Group's
1588 * C compiler. Reported by John Stone.
1591 for (i
=0;i
<32;i
++) {
1592 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1595 if (mask
& GL_SCISSOR_BIT
) {
1596 MEMCPY( &dst
->Scissor
, &src
->Scissor
, sizeof(struct gl_scissor_attrib
) );
1598 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1599 MEMCPY( &dst
->Stencil
, &src
->Stencil
, sizeof(struct gl_stencil_attrib
) );
1601 if (mask
& GL_TEXTURE_BIT
) {
1602 MEMCPY( &dst
->Texture
, &src
->Texture
, sizeof(struct gl_texture_attrib
) );
1604 if (mask
& GL_TRANSFORM_BIT
) {
1605 MEMCPY( &dst
->Transform
, &src
->Transform
, sizeof(struct gl_transform_attrib
) );
1607 if (mask
& GL_VIEWPORT_BIT
) {
1608 MEMCPY( &dst
->Viewport
, &src
->Viewport
, sizeof(struct gl_viewport_attrib
) );
1614 * This should be called by device drivers just before they do a
1615 * swapbuffers. Any pending rendering commands will be executed.
1618 _mesa_swapbuffers(GLcontext
*ctx
)
1620 FLUSH_VB( ctx
, "swap buffers" );
1625 * Return pointer to this context's current API dispatch table.
1626 * It'll either be the immediate-mode execute dispatcher or the
1627 * display list compile dispatcher.
1629 struct _glapi_table
*
1630 _mesa_get_dispatch(GLcontext
*ctx
)
1632 return ctx
->CurrentDispatch
;
1638 _mesa_ResizeBuffersMESA( void )
1640 GET_CURRENT_CONTEXT(ctx
);
1642 GLuint buf_width
, buf_height
;
1644 if (MESA_VERBOSE
& VERBOSE_API
)
1645 fprintf(stderr
, "glResizeBuffersMESA\n");
1647 /* ask device driver for size of output buffer */
1648 (*ctx
->Driver
.GetBufferSize
)( ctx
, &buf_width
, &buf_height
);
1650 /* see if size of device driver's color buffer (window) has changed */
1651 if (ctx
->Buffer
->Width
== (GLint
) buf_width
&&
1652 ctx
->Buffer
->Height
== (GLint
) buf_height
)
1655 ctx
->NewState
|= NEW_RASTER_OPS
; /* to update scissor / window bounds */
1657 /* save buffer size */
1658 ctx
->Buffer
->Width
= buf_width
;
1659 ctx
->Buffer
->Height
= buf_height
;
1661 /* Reallocate other buffers if needed. */
1662 if (ctx
->Visual
->DepthBits
>0) {
1663 /* reallocate depth buffer */
1664 (*ctx
->Driver
.AllocDepthBuffer
)( ctx
);
1666 if (ctx
->Visual
->StencilBits
>0) {
1667 /* reallocate stencil buffer */
1668 gl_alloc_stencil_buffer( ctx
);
1670 if (ctx
->Visual
->AccumBits
>0) {
1671 /* reallocate accum buffer */
1672 gl_alloc_accum_buffer( ctx
);
1674 if (ctx
->Visual
->SoftwareAlpha
) {
1675 gl_alloc_alpha_buffers( ctx
);
1681 /**********************************************************************/
1682 /***** Miscellaneous functions *****/
1683 /**********************************************************************/
1687 * This function is called when the Mesa user has stumbled into a code
1688 * path which may not be implemented fully or correctly.
1690 void gl_problem( const GLcontext
*ctx
, const char *s
)
1692 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
1693 fprintf( stderr
, "Report to mesa-bugs@mesa3d.org\n" );
1700 * This is called to inform the user that he or she has tried to do
1701 * something illogical or if there's likely a bug in their program
1702 * (like enabled depth testing without a depth buffer).
1704 void gl_warning( const GLcontext
*ctx
, const char *s
)
1710 if (getenv("MESA_DEBUG")) {
1718 fprintf( stderr
, "Mesa warning: %s\n", s
);
1725 void gl_compile_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1727 if (ctx
->CompileFlag
)
1728 gl_save_error( ctx
, error
, s
);
1730 if (ctx
->ExecuteFlag
)
1731 gl_error( ctx
, error
, s
);
1736 * This is Mesa's error handler. Normally, all that's done is the updating
1737 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1738 * environment variable "MESA_DEBUG" is defined then a real error message
1739 * is printed to stderr.
1740 * Input: error - the error value
1741 * s - a diagnostic string
1743 void gl_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1750 if (getenv("MESA_DEBUG")) {
1763 strcpy( errstr
, "GL_NO_ERROR" );
1765 case GL_INVALID_VALUE
:
1766 strcpy( errstr
, "GL_INVALID_VALUE" );
1768 case GL_INVALID_ENUM
:
1769 strcpy( errstr
, "GL_INVALID_ENUM" );
1771 case GL_INVALID_OPERATION
:
1772 strcpy( errstr
, "GL_INVALID_OPERATION" );
1774 case GL_STACK_OVERFLOW
:
1775 strcpy( errstr
, "GL_STACK_OVERFLOW" );
1777 case GL_STACK_UNDERFLOW
:
1778 strcpy( errstr
, "GL_STACK_UNDERFLOW" );
1780 case GL_OUT_OF_MEMORY
:
1781 strcpy( errstr
, "GL_OUT_OF_MEMORY" );
1784 strcpy( errstr
, "unknown" );
1787 fprintf( stderr
, "Mesa user error: %s in %s\n", errstr
, s
);
1790 if (ctx
->ErrorValue
==GL_NO_ERROR
) {
1791 ctx
->ErrorValue
= error
;
1794 /* Call device driver's error handler, if any. This is used on the Mac. */
1795 if (ctx
->Driver
.Error
) {
1796 (*ctx
->Driver
.Error
)( ctx
);
1802 /**********************************************************************/
1803 /***** State update logic *****/
1804 /**********************************************************************/
1808 * Since the device driver may or may not support pixel logic ops we
1809 * have to make some extensive tests to determine whether or not
1810 * software-implemented logic operations have to be used.
1812 static void update_pixel_logic( GLcontext
*ctx
)
1814 if (ctx
->Visual
->RGBAflag
) {
1815 /* RGBA mode blending w/ Logic Op */
1816 if (ctx
->Color
.ColorLogicOpEnabled
) {
1817 if (ctx
->Driver
.LogicOp
1818 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1819 /* Device driver can do logic, don't have to do it in software */
1820 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1823 /* Device driver can't do logic op so we do it in software */
1824 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1829 if (ctx
->Driver
.LogicOp
) {
1830 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1832 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1836 /* CI mode Logic Op */
1837 if (ctx
->Color
.IndexLogicOpEnabled
) {
1838 if (ctx
->Driver
.LogicOp
1839 && (*ctx
->Driver
.LogicOp
)( ctx
, ctx
->Color
.LogicOp
)) {
1840 /* Device driver can do logic, don't have to do it in software */
1841 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1844 /* Device driver can't do logic op so we do it in software */
1845 ctx
->Color
.SWLogicOpEnabled
= GL_TRUE
;
1850 if (ctx
->Driver
.LogicOp
) {
1851 (void) (*ctx
->Driver
.LogicOp
)( ctx
, GL_COPY
);
1853 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
1861 * Check if software implemented RGBA or Color Index masking is needed.
1863 static void update_pixel_masking( GLcontext
*ctx
)
1865 if (ctx
->Visual
->RGBAflag
) {
1866 GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1867 if (*colorMask
== 0xffffffff) {
1868 /* disable masking */
1869 if (ctx
->Driver
.ColorMask
) {
1870 (void) (*ctx
->Driver
.ColorMask
)( ctx
, GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1872 ctx
->Color
.SWmasking
= GL_FALSE
;
1875 /* Ask driver to do color masking, if it can't then
1878 GLboolean red
= ctx
->Color
.ColorMask
[RCOMP
] ? GL_TRUE
: GL_FALSE
;
1879 GLboolean green
= ctx
->Color
.ColorMask
[GCOMP
] ? GL_TRUE
: GL_FALSE
;
1880 GLboolean blue
= ctx
->Color
.ColorMask
[BCOMP
] ? GL_TRUE
: GL_FALSE
;
1881 GLboolean alpha
= ctx
->Color
.ColorMask
[ACOMP
] ? GL_TRUE
: GL_FALSE
;
1882 if (ctx
->Driver
.ColorMask
1883 && (*ctx
->Driver
.ColorMask
)( ctx
, red
, green
, blue
, alpha
)) {
1884 ctx
->Color
.SWmasking
= GL_FALSE
;
1887 ctx
->Color
.SWmasking
= GL_TRUE
;
1892 if (ctx
->Color
.IndexMask
==0xffffffff) {
1893 /* disable masking */
1894 if (ctx
->Driver
.IndexMask
) {
1895 (void) (*ctx
->Driver
.IndexMask
)( ctx
, 0xffffffff );
1897 ctx
->Color
.SWmasking
= GL_FALSE
;
1900 /* Ask driver to do index masking, if it can't then
1903 if (ctx
->Driver
.IndexMask
1904 && (*ctx
->Driver
.IndexMask
)( ctx
, ctx
->Color
.IndexMask
)) {
1905 ctx
->Color
.SWmasking
= GL_FALSE
;
1908 ctx
->Color
.SWmasking
= GL_TRUE
;
1915 static void update_fog_mode( GLcontext
*ctx
)
1917 int old_mode
= ctx
->FogMode
;
1919 if (ctx
->Fog
.Enabled
) {
1920 if (ctx
->Texture
.Enabled
)
1921 ctx
->FogMode
= FOG_FRAGMENT
;
1922 else if (ctx
->Hint
.Fog
== GL_NICEST
)
1923 ctx
->FogMode
= FOG_FRAGMENT
;
1925 ctx
->FogMode
= FOG_VERTEX
;
1927 if (ctx
->Driver
.GetParameteri
)
1928 if ((ctx
->Driver
.GetParameteri
)( ctx
, DD_HAVE_HARDWARE_FOG
))
1929 ctx
->FogMode
= FOG_FRAGMENT
;
1932 ctx
->FogMode
= FOG_NONE
;
1935 if (old_mode
!= ctx
->FogMode
)
1936 ctx
->NewState
|= NEW_FOG
;
1941 * Recompute the value of ctx->RasterMask, etc. according to
1942 * the current context.
1944 static void update_rasterflags( GLcontext
*ctx
)
1946 ctx
->RasterMask
= 0;
1948 if (ctx
->Color
.AlphaEnabled
) ctx
->RasterMask
|= ALPHATEST_BIT
;
1949 if (ctx
->Color
.BlendEnabled
) ctx
->RasterMask
|= BLEND_BIT
;
1950 if (ctx
->Depth
.Test
) ctx
->RasterMask
|= DEPTH_BIT
;
1951 if (ctx
->FogMode
==FOG_FRAGMENT
) ctx
->RasterMask
|= FOG_BIT
;
1952 if (ctx
->Color
.SWLogicOpEnabled
) ctx
->RasterMask
|= LOGIC_OP_BIT
;
1953 if (ctx
->Scissor
.Enabled
) ctx
->RasterMask
|= SCISSOR_BIT
;
1954 if (ctx
->Stencil
.Enabled
) ctx
->RasterMask
|= STENCIL_BIT
;
1955 if (ctx
->Color
.SWmasking
) ctx
->RasterMask
|= MASKING_BIT
;
1957 if (ctx
->Visual
->SoftwareAlpha
&& ctx
->Color
.ColorMask
[ACOMP
]
1958 && ctx
->Color
.DrawBuffer
!= GL_NONE
)
1959 ctx
->RasterMask
|= ALPHABUF_BIT
;
1961 if ( ctx
->Viewport
.X
<0
1962 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->Buffer
->Width
1963 || ctx
->Viewport
.Y
<0
1964 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->Buffer
->Height
) {
1965 ctx
->RasterMask
|= WINCLIP_BIT
;
1968 /* If we're not drawing to exactly one color buffer set the
1969 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
1970 * buffers or the RGBA or CI mask disables all writes.
1973 ctx
->TriangleCaps
&= ~DD_MULTIDRAW
;
1975 if (ctx
->Color
.MultiDrawBuffer
) {
1976 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
1977 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
1979 else if (ctx
->Color
.DrawBuffer
==GL_NONE
) {
1980 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
1981 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
1983 else if (ctx
->Visual
->RGBAflag
&& ctx
->Color
.ColorMask
==0) {
1984 /* all RGBA channels disabled */
1985 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
1986 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
1987 ctx
->Color
.DrawDestMask
= 0;
1989 else if (!ctx
->Visual
->RGBAflag
&& ctx
->Color
.IndexMask
==0) {
1990 /* all color index bits disabled */
1991 ctx
->RasterMask
|= MULTI_DRAW_BIT
;
1992 ctx
->TriangleCaps
|= DD_MULTIDRAW
;
1993 ctx
->Color
.DrawDestMask
= 0;
1998 void gl_print_state( const char *msg
, GLuint state
)
2001 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2004 (state
& NEW_LIGHTING
) ? "lighting, " : "",
2005 (state
& NEW_RASTER_OPS
) ? "raster-ops, " : "",
2006 (state
& NEW_TEXTURING
) ? "texturing, " : "",
2007 (state
& NEW_POLYGON
) ? "polygon, " : "",
2008 (state
& NEW_DRVSTATE0
) ? "driver-0, " : "",
2009 (state
& NEW_DRVSTATE1
) ? "driver-1, " : "",
2010 (state
& NEW_DRVSTATE2
) ? "driver-2, " : "",
2011 (state
& NEW_DRVSTATE3
) ? "driver-3, " : "",
2012 (state
& NEW_MODELVIEW
) ? "modelview, " : "",
2013 (state
& NEW_PROJECTION
) ? "projection, " : "",
2014 (state
& NEW_TEXTURE_MATRIX
) ? "texture-matrix, " : "",
2015 (state
& NEW_USER_CLIP
) ? "user-clip, " : "",
2016 (state
& NEW_TEXTURE_ENV
) ? "texture-env, " : "",
2017 (state
& NEW_CLIENT_STATE
) ? "client-state, " : "",
2018 (state
& NEW_FOG
) ? "fog, " : "",
2019 (state
& NEW_NORMAL_TRANSFORM
) ? "normal-transform, " : "",
2020 (state
& NEW_VIEWPORT
) ? "viewport, " : "",
2021 (state
& NEW_TEXTURE_ENABLE
) ? "texture-enable, " : "");
2024 void gl_print_enable_flags( const char *msg
, GLuint flags
)
2027 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s\n",
2030 (flags
& ENABLE_TEX0
) ? "tex-0, " : "",
2031 (flags
& ENABLE_TEX1
) ? "tex-1, " : "",
2032 (flags
& ENABLE_LIGHT
) ? "light, " : "",
2033 (flags
& ENABLE_FOG
) ? "fog, " : "",
2034 (flags
& ENABLE_USERCLIP
) ? "userclip, " : "",
2035 (flags
& ENABLE_TEXGEN0
) ? "tex-gen-0, " : "",
2036 (flags
& ENABLE_TEXGEN1
) ? "tex-gen-1, " : "",
2037 (flags
& ENABLE_TEXMAT0
) ? "tex-mat-0, " : "",
2038 (flags
& ENABLE_TEXMAT1
) ? "tex-mat-1, " : "",
2039 (flags
& ENABLE_NORMALIZE
) ? "normalize, " : "",
2040 (flags
& ENABLE_RESCALE
) ? "rescale, " : "");
2045 * If ctx->NewState is non-zero then this function MUST be called before
2046 * rendering any primitive. Basically, function pointers and miscellaneous
2047 * flags are updated to reflect the current state of the state machine.
2049 void gl_update_state( GLcontext
*ctx
)
2053 if (MESA_VERBOSE
& VERBOSE_STATE
)
2054 gl_print_state("", ctx
->NewState
);
2056 if (ctx
->NewState
& NEW_CLIENT_STATE
)
2057 gl_update_client_state( ctx
);
2059 if ((ctx
->NewState
& NEW_TEXTURE_ENABLE
) &&
2060 (ctx
->Enabled
& ENABLE_TEX_ANY
) != ctx
->Texture
.Enabled
)
2061 ctx
->NewState
|= NEW_TEXTURING
| NEW_RASTER_OPS
;
2063 if (ctx
->NewState
& NEW_TEXTURE_ENV
) {
2064 if (ctx
->Texture
.Unit
[0].EnvMode
== ctx
->Texture
.Unit
[0].LastEnvMode
&&
2065 ctx
->Texture
.Unit
[1].EnvMode
== ctx
->Texture
.Unit
[1].LastEnvMode
)
2066 ctx
->NewState
&= ~NEW_TEXTURE_ENV
;
2067 ctx
->Texture
.Unit
[0].LastEnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
2068 ctx
->Texture
.Unit
[1].LastEnvMode
= ctx
->Texture
.Unit
[1].EnvMode
;
2071 if (ctx
->NewState
& NEW_TEXTURE_MATRIX
) {
2072 ctx
->Enabled
&= ~(ENABLE_TEXMAT0
|ENABLE_TEXMAT1
);
2074 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2075 if (ctx
->TextureMatrix
[i
].flags
& MAT_DIRTY_ALL_OVER
)
2077 gl_matrix_analyze( &ctx
->TextureMatrix
[i
] );
2078 ctx
->TextureMatrix
[i
].flags
&= ~MAT_DIRTY_DEPENDENTS
;
2080 if (ctx
->Texture
.Unit
[i
].Enabled
&&
2081 ctx
->TextureMatrix
[i
].type
!= MATRIX_IDENTITY
)
2082 ctx
->Enabled
|= ENABLE_TEXMAT0
<< i
;
2087 if (ctx
->NewState
& (NEW_TEXTURING
| NEW_TEXTURE_ENABLE
)) {
2088 ctx
->Texture
.NeedNormals
= GL_FALSE
;
2089 gl_update_dirty_texobjs(ctx
);
2090 ctx
->Enabled
&= ~(ENABLE_TEXGEN0
|ENABLE_TEXGEN1
);
2091 ctx
->Texture
.ReallyEnabled
= 0;
2093 for (i
=0; i
< MAX_TEXTURE_UNITS
; i
++) {
2094 if (ctx
->Texture
.Unit
[i
].Enabled
) {
2095 gl_update_texture_unit( ctx
, &ctx
->Texture
.Unit
[i
] );
2097 ctx
->Texture
.ReallyEnabled
|=
2098 ctx
->Texture
.Unit
[i
].ReallyEnabled
<<(i
*4);
2100 if (ctx
->Texture
.Unit
[i
].GenFlags
!= 0) {
2101 ctx
->Enabled
|= ENABLE_TEXGEN0
<< i
;
2103 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_NORMALS
)
2105 ctx
->Texture
.NeedNormals
= GL_TRUE
;
2106 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2109 if (ctx
->Texture
.Unit
[i
].GenFlags
& TEXGEN_NEED_EYE_COORD
)
2111 ctx
->Texture
.NeedEyeCoords
= GL_TRUE
;
2117 ctx
->Texture
.Enabled
= ctx
->Enabled
& ENABLE_TEX_ANY
;
2118 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2121 if (ctx
->NewState
& (NEW_RASTER_OPS
| NEW_LIGHTING
| NEW_FOG
)) {
2124 if (ctx
->NewState
& NEW_RASTER_OPS
) {
2125 update_pixel_logic(ctx
);
2126 update_pixel_masking(ctx
);
2127 update_fog_mode(ctx
);
2128 update_rasterflags(ctx
);
2129 if (ctx
->Driver
.Dither
) {
2130 (*ctx
->Driver
.Dither
)( ctx
, ctx
->Color
.DitherFlag
);
2133 /* Check if incoming colors can be modified during rasterization */
2134 if (ctx
->Fog
.Enabled
||
2135 ctx
->Texture
.Enabled
||
2136 ctx
->Color
.BlendEnabled
||
2137 ctx
->Color
.SWmasking
||
2138 ctx
->Color
.SWLogicOpEnabled
) {
2139 ctx
->MutablePixels
= GL_TRUE
;
2142 ctx
->MutablePixels
= GL_FALSE
;
2145 /* update scissor region */
2147 ctx
->Buffer
->Xmin
= 0;
2148 ctx
->Buffer
->Ymin
= 0;
2149 ctx
->Buffer
->Xmax
= ctx
->Buffer
->Width
-1;
2150 ctx
->Buffer
->Ymax
= ctx
->Buffer
->Height
-1;
2151 if (ctx
->Scissor
.Enabled
) {
2152 if (ctx
->Scissor
.X
> ctx
->Buffer
->Xmin
) {
2153 ctx
->Buffer
->Xmin
= ctx
->Scissor
.X
;
2155 if (ctx
->Scissor
.Y
> ctx
->Buffer
->Ymin
) {
2156 ctx
->Buffer
->Ymin
= ctx
->Scissor
.Y
;
2158 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1 < ctx
->Buffer
->Xmax
) {
2159 ctx
->Buffer
->Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
- 1;
2161 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1 < ctx
->Buffer
->Ymax
) {
2162 ctx
->Buffer
->Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
- 1;
2166 /* The driver isn't managing the depth buffer.
2168 if (ctx
->Driver
.AllocDepthBuffer
== gl_alloc_depth_buffer
)
2170 if (ctx
->Depth
.Mask
) {
2171 switch (ctx
->Depth
.Func
) {
2173 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_less
;
2174 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_less
;
2177 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_greater
;
2178 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_greater
;
2181 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2182 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2186 ctx
->Driver
.DepthTestSpan
= gl_depth_test_span_generic
;
2187 ctx
->Driver
.DepthTestPixels
= gl_depth_test_pixels_generic
;
2192 if (ctx
->NewState
& NEW_LIGHTING
) {
2193 ctx
->TriangleCaps
&= ~(DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2194 if (ctx
->Light
.Enabled
) {
2195 if (ctx
->Light
.Model
.TwoSide
)
2196 ctx
->TriangleCaps
|= (DD_TRI_LIGHT_TWOSIDE
|DD_LIGHTING_CULL
);
2197 gl_update_lighting(ctx
);
2202 if (ctx
->NewState
& (NEW_POLYGON
| NEW_LIGHTING
)) {
2204 ctx
->TriangleCaps
&= ~DD_TRI_CULL_FRONT_BACK
;
2206 if (ctx
->NewState
& NEW_POLYGON
) {
2207 /* Setup CullBits bitmask */
2208 if (ctx
->Polygon
.CullFlag
) {
2209 ctx
->backface_sign
= 1;
2210 switch(ctx
->Polygon
.CullFaceMode
) {
2212 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
2213 ctx
->backface_sign
= -1;
2214 ctx
->Polygon
.CullBits
= 1;
2217 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
2218 ctx
->backface_sign
= -1;
2219 ctx
->Polygon
.CullBits
= 2;
2222 case GL_FRONT_AND_BACK
:
2223 ctx
->backface_sign
= 0;
2224 ctx
->Polygon
.CullBits
= 0;
2225 ctx
->TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
2230 ctx
->Polygon
.CullBits
= 3;
2231 ctx
->backface_sign
= 0;
2234 /* Any Polygon offsets enabled? */
2235 ctx
->TriangleCaps
&= ~DD_TRI_OFFSET
;
2237 if (ctx
->Polygon
.OffsetPoint
||
2238 ctx
->Polygon
.OffsetLine
||
2239 ctx
->Polygon
.OffsetFill
)
2240 ctx
->TriangleCaps
|= DD_TRI_OFFSET
;
2242 /* reset Z offsets now */
2243 ctx
->PointZoffset
= 0.0;
2244 ctx
->LineZoffset
= 0.0;
2245 ctx
->PolygonZoffset
= 0.0;
2249 if (ctx
->NewState
& ~(NEW_CLIENT_STATE
|
2250 NEW_DRIVER_STATE
|NEW_USER_CLIP
|
2252 gl_update_clipmask(ctx
);
2254 if (ctx
->NewState
& (NEW_LIGHTING
|
2266 ctx
->IndirectTriangles
= ctx
->TriangleCaps
& ~ctx
->Driver
.TriangleCaps
;
2267 ctx
->IndirectTriangles
|= DD_SW_RASTERIZE
;
2269 if (MESA_VERBOSE
&VERBOSE_CULL
)
2270 gl_print_tri_caps("initial indirect tris", ctx
->IndirectTriangles
);
2272 ctx
->Driver
.PointsFunc
= NULL
;
2273 ctx
->Driver
.LineFunc
= NULL
;
2274 ctx
->Driver
.TriangleFunc
= NULL
;
2275 ctx
->Driver
.QuadFunc
= NULL
;
2276 ctx
->Driver
.RectFunc
= NULL
;
2277 ctx
->Driver
.RenderVBClippedTab
= NULL
;
2278 ctx
->Driver
.RenderVBCulledTab
= NULL
;
2279 ctx
->Driver
.RenderVBRawTab
= NULL
;
2282 * Here the driver sets up all the ctx->Driver function pointers to
2283 * it's specific, private functions.
2285 ctx
->Driver
.UpdateState(ctx
);
2287 if (MESA_VERBOSE
&VERBOSE_CULL
)
2288 gl_print_tri_caps("indirect tris", ctx
->IndirectTriangles
);
2291 * In case the driver didn't hook in an optimized point, line or
2292 * triangle function we'll now select "core/fallback" point, line
2293 * and triangle functions.
2295 if (ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) {
2296 gl_set_point_function(ctx
);
2297 gl_set_line_function(ctx
);
2298 gl_set_triangle_function(ctx
);
2299 gl_set_quad_function(ctx
);
2301 if ((ctx
->IndirectTriangles
&
2302 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
)) ==
2303 (DD_TRI_SW_RASTERIZE
|DD_QUAD_SW_RASTERIZE
|DD_TRI_CULL
))
2304 ctx
->IndirectTriangles
&= ~DD_TRI_CULL
;
2307 if (MESA_VERBOSE
&VERBOSE_CULL
)
2308 gl_print_tri_caps("indirect tris 2", ctx
->IndirectTriangles
);
2310 gl_set_render_vb_function(ctx
);
2313 /* Should only be calc'd when !need_eye_coords and not culling.
2315 if (ctx
->NewState
& (NEW_MODELVIEW
|NEW_PROJECTION
)) {
2316 if (ctx
->NewState
& NEW_MODELVIEW
) {
2317 gl_matrix_analyze( &ctx
->ModelView
);
2318 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2321 if (ctx
->NewState
& NEW_PROJECTION
) {
2322 gl_matrix_analyze( &ctx
->ProjectionMatrix
);
2323 ctx
->ProjectionMatrix
.flags
&= ~MAT_DIRTY_DEPENDENTS
;
2325 if (ctx
->Transform
.AnyClip
) {
2326 gl_update_userclip( ctx
);
2330 gl_calculate_model_project_matrix( ctx
);
2331 ctx
->ModelProjectWinMatrixUptodate
= 0;
2334 /* Figure out whether we can light in object space or not. If we
2335 * can, find the current positions of the lights in object space
2337 if ((ctx
->Enabled
& (ENABLE_POINT_ATTEN
| ENABLE_LIGHT
| ENABLE_FOG
|
2338 ENABLE_TEXGEN0
| ENABLE_TEXGEN1
)) &&
2339 (ctx
->NewState
& (NEW_LIGHTING
|
2347 GLboolean oldcoord
, oldnorm
;
2349 oldcoord
= ctx
->NeedEyeCoords
;
2350 oldnorm
= ctx
->NeedEyeNormals
;
2352 ctx
->NeedNormals
= (ctx
->Light
.Enabled
|| ctx
->Texture
.NeedNormals
);
2353 ctx
->NeedEyeCoords
= ((ctx
->Fog
.Enabled
&& ctx
->Hint
.Fog
!= GL_NICEST
) ||
2354 ctx
->Point
.Attenuated
);
2355 ctx
->NeedEyeNormals
= GL_FALSE
;
2357 if (ctx
->Light
.Enabled
) {
2358 if (ctx
->Light
.Flags
& LIGHT_POSITIONAL
) {
2359 /* Need length for attenuation */
2360 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_LENGTH_PRESERVING
))
2361 ctx
->NeedEyeCoords
= GL_TRUE
;
2362 } else if (ctx
->Light
.NeedVertices
) {
2363 /* Need angle for spot calculations */
2364 if (!TEST_MAT_FLAGS( &ctx
->ModelView
, MAT_FLAGS_ANGLE_PRESERVING
))
2365 ctx
->NeedEyeCoords
= GL_TRUE
;
2367 ctx
->NeedEyeNormals
= ctx
->NeedEyeCoords
;
2369 if (ctx
->Texture
.Enabled
|| ctx
->RenderMode
==GL_FEEDBACK
) {
2370 if (ctx
->Texture
.NeedEyeCoords
) ctx
->NeedEyeCoords
= GL_TRUE
;
2371 if (ctx
->Texture
.NeedNormals
)
2372 ctx
->NeedNormals
= ctx
->NeedEyeNormals
= GL_TRUE
;
2375 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
2377 if (ctx
->NeedEyeCoords
)
2378 ctx
->vb_proj_matrix
= &ctx
->ProjectionMatrix
;
2380 if (ctx
->Light
.Enabled
) {
2381 gl_update_lighting_function(ctx
);
2383 if ( (ctx
->NewState
& NEW_LIGHTING
) ||
2384 ((ctx
->NewState
& (NEW_MODELVIEW
| NEW_PROJECTION
)) &&
2385 !ctx
->NeedEyeCoords
) ||
2386 oldcoord
!= ctx
->NeedEyeCoords
||
2387 oldnorm
!= ctx
->NeedEyeNormals
) {
2388 gl_compute_light_positions(ctx
);
2391 ctx
->rescale_factor
= 1.0F
;
2393 if (ctx
->ModelView
.flags
& (MAT_FLAG_UNIFORM_SCALE
|
2394 MAT_FLAG_GENERAL_SCALE
|
2395 MAT_FLAG_GENERAL_3D
|
2399 GLfloat
*m
= ctx
->ModelView
.inv
;
2400 GLfloat f
= m
[2]*m
[2] + m
[6]*m
[6] + m
[10]*m
[10];
2401 if (f
> 1e-12 && (f
-1)*(f
-1) > 1e-12)
2402 ctx
->rescale_factor
= 1.0/GL_SQRT(f
);
2406 gl_update_normal_transform( ctx
);
2409 gl_update_pipelines(ctx
);