1 /* $Id: context.c,v 1.48 2000/03/19 01:10:11 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
41 #include "extensions.h"
45 #include "glapinoop.h"
56 #include "simple_list.h"
60 #include "translate.h"
77 /**********************************************************************/
78 /***** Context and Thread management *****/
79 /**********************************************************************/
84 struct immediate
*_mesa_CurrentInput
= NULL
;
91 /**********************************************************************/
92 /***** Profiling functions *****/
93 /**********************************************************************/
97 #include <sys/times.h>
98 #include <sys/param.h>
102 * Return system time in seconds.
103 * NOTE: this implementation may not be very portable!
105 GLdouble
gl_time( void )
107 static GLdouble prev_time
= 0.0;
108 static GLdouble time
;
115 time
= (double)clk
/ (double)CLK_TCK
;
117 time
= (double)clk
/ (double)HZ
;
120 if (time
>prev_time
) {
130 * Reset the timing/profiling counters
132 static void init_timings( GLcontext
*ctx
)
134 ctx
->BeginEndCount
= 0;
135 ctx
->BeginEndTime
= 0.0;
136 ctx
->VertexCount
= 0;
137 ctx
->VertexTime
= 0.0;
139 ctx
->PointTime
= 0.0;
142 ctx
->PolygonCount
= 0;
143 ctx
->PolygonTime
= 0.0;
145 ctx
->ClearTime
= 0.0;
152 * Print the accumulated timing/profiling data.
154 static void print_timings( GLcontext
*ctx
)
156 GLdouble beginendrate
;
160 GLdouble polygonrate
;
164 GLdouble avgvertices
;
166 if (ctx
->BeginEndTime
>0.0) {
167 beginendrate
= ctx
->BeginEndCount
/ ctx
->BeginEndTime
;
172 if (ctx
->VertexTime
>0.0) {
173 vertexrate
= ctx
->VertexCount
/ ctx
->VertexTime
;
178 if (ctx
->PointTime
>0.0) {
179 pointrate
= ctx
->PointCount
/ ctx
->PointTime
;
184 if (ctx
->LineTime
>0.0) {
185 linerate
= ctx
->LineCount
/ ctx
->LineTime
;
190 if (ctx
->PolygonTime
>0.0) {
191 polygonrate
= ctx
->PolygonCount
/ ctx
->PolygonTime
;
196 if (ctx
->ClearTime
>0.0) {
197 clearrate
= ctx
->ClearCount
/ ctx
->ClearTime
;
202 if (ctx
->SwapTime
>0.0) {
203 swaprate
= ctx
->SwapCount
/ ctx
->SwapTime
;
209 if (ctx
->BeginEndCount
>0) {
210 avgvertices
= (GLdouble
) ctx
->VertexCount
/ (GLdouble
) ctx
->BeginEndCount
;
216 overhead
= ctx
->BeginEndTime
- ctx
->VertexTime
- ctx
->PointTime
217 - ctx
->LineTime
- ctx
->PolygonTime
;
220 printf(" Count Time (s) Rate (/s) \n");
221 printf("--------------------------------------------------------\n");
222 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
223 ctx
->BeginEndCount
, ctx
->BeginEndTime
, beginendrate
);
224 printf(" vertexes transformed %7d %8.3f %10.3f\n",
225 ctx
->VertexCount
, ctx
->VertexTime
, vertexrate
);
226 printf(" points rasterized %7d %8.3f %10.3f\n",
227 ctx
->PointCount
, ctx
->PointTime
, pointrate
);
228 printf(" lines rasterized %7d %8.3f %10.3f\n",
229 ctx
->LineCount
, ctx
->LineTime
, linerate
);
230 printf(" polygons rasterized %7d %8.3f %10.3f\n",
231 ctx
->PolygonCount
, ctx
->PolygonTime
, polygonrate
);
232 printf(" overhead %8.3f\n", overhead
);
233 printf("glClear %7d %8.3f %10.3f\n",
234 ctx
->ClearCount
, ctx
->ClearTime
, clearrate
);
235 printf("SwapBuffers %7d %8.3f %10.3f\n",
236 ctx
->SwapCount
, ctx
->SwapTime
, swaprate
);
239 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices
);
247 /**********************************************************************/
248 /***** GL Visual allocation/destruction *****/
249 /**********************************************************************/
253 * Allocate a new GLvisual object.
254 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
255 * alphaFlag - alloc software alpha buffers?
256 * dbFlag - double buffering?
257 * stereoFlag - stereo buffer?
258 * depthBits - requested bits per depth buffer value
259 * Any value in [0, 32] is acceptable but the actual
260 * depth type will be GLushort or GLuint as needed.
261 * stencilBits - requested minimum bits per stencil buffer value
262 * accumBits - requested minimum bits per accum buffer component
263 * indexBits - number of bits per pixel if rgbFlag==GL_FALSE
264 * red/green/blue/alphaBits - number of bits per color component
265 * in frame buffer for RGB(A) mode.
266 * We always use 8 in core Mesa though.
267 * Return: pointer to new GLvisual or NULL if requested parameters can't
270 GLvisual
*gl_create_visual( GLboolean rgbFlag
,
273 GLboolean stereoFlag
,
285 /* This is to catch bad values from device drivers not updated for
286 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
287 * bad value now (a 1-bit depth buffer!?!).
289 assert(depthBits
== 0 || depthBits
> 1);
290 printf("depthbits %d\n", depthBits
);
292 if (depthBits
< 0 || depthBits
> 32) {
295 if (stencilBits
< 0 || stencilBits
> (GLint
) (8 * sizeof(GLstencil
))) {
298 if (accumBits
< 0 || accumBits
> (GLint
) (8 * sizeof(GLaccum
))) {
302 vis
= (GLvisual
*) CALLOC( sizeof(GLvisual
) );
307 vis
->RGBAflag
= rgbFlag
;
308 vis
->DBflag
= dbFlag
;
309 vis
->StereoFlag
= stereoFlag
;
310 vis
->RedBits
= redBits
;
311 vis
->GreenBits
= greenBits
;
312 vis
->BlueBits
= blueBits
;
313 vis
->AlphaBits
= alphaFlag
? (8 * sizeof(GLubyte
)) : alphaBits
;
315 vis
->IndexBits
= indexBits
;
316 vis
->DepthBits
= depthBits
;
317 vis
->AccumBits
= (accumBits
> 0) ? (8 * sizeof(GLaccum
)) : 0;
318 vis
->StencilBits
= (stencilBits
> 0) ? (8 * sizeof(GLstencil
)) : 0;
320 vis
->SoftwareAlpha
= alphaFlag
;
322 if (depthBits
== 0) {
323 /* Special case. Even if we don't have a depth buffer we need
324 * good values for DepthMax for Z vertex transformation purposes.
327 vis
->DepthMaxF
= 1.0F
;
330 vis
->DepthMax
= (1 << depthBits
) - 1;
331 vis
->DepthMaxF
= (GLfloat
) vis
->DepthMax
;
339 void gl_destroy_visual( GLvisual
*vis
)
346 /**********************************************************************/
347 /***** GL Framebuffer allocation/destruction *****/
348 /**********************************************************************/
352 * Create a new framebuffer. A GLframebuffer is a struct which
353 * encapsulates the depth, stencil and accum buffers and related
355 * Input: visual - a GLvisual pointer
356 * softwareDepth - create/use a software depth buffer?
357 * softwareStencil - create/use a software stencil buffer?
358 * softwareAccum - create/use a software accum buffer?
359 * softwareAlpha - create/use a software alpha buffer?
361 * Return: pointer to new GLframebuffer struct or NULL if error.
363 GLframebuffer
*gl_create_framebuffer( GLvisual
*visual
,
364 GLboolean softwareDepth
,
365 GLboolean softwareStencil
,
366 GLboolean softwareAccum
,
367 GLboolean softwareAlpha
)
369 GLframebuffer
*buffer
;
371 buffer
= CALLOC_STRUCT(gl_frame_buffer
);
377 if (softwareDepth
) {
378 assert(visual
->DepthBits
> 0);
380 if (softwareStencil
) {
381 assert(visual
->StencilBits
> 0);
384 assert(visual
->RGBAflag
);
385 assert(visual
->AccumBits
> 0);
388 assert(visual
->RGBAflag
);
389 assert(visual
->AlphaBits
> 0);
392 buffer
->Visual
= visual
;
393 buffer
->UseSoftwareDepthBuffer
= softwareDepth
;
394 buffer
->UseSoftwareStencilBuffer
= softwareStencil
;
395 buffer
->UseSoftwareAccumBuffer
= softwareAccum
;
396 buffer
->UseSoftwareAlphaBuffers
= softwareAlpha
;
404 * Free a framebuffer struct and its buffers.
406 void gl_destroy_framebuffer( GLframebuffer
*buffer
)
409 if (buffer
->DepthBuffer
) {
410 FREE( buffer
->DepthBuffer
);
413 FREE( buffer
->Accum
);
415 if (buffer
->Stencil
) {
416 FREE( buffer
->Stencil
);
418 if (buffer
->FrontLeftAlpha
) {
419 FREE( buffer
->FrontLeftAlpha
);
421 if (buffer
->BackLeftAlpha
) {
422 FREE( buffer
->BackLeftAlpha
);
424 if (buffer
->FrontRightAlpha
) {
425 FREE( buffer
->FrontRightAlpha
);
427 if (buffer
->BackRightAlpha
) {
428 FREE( buffer
->BackRightAlpha
);
436 /**********************************************************************/
437 /***** Context allocation, initialization, destroying *****/
438 /**********************************************************************/
441 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
445 * This function just calls all the various one-time-init functions in Mesa.
447 static void one_time_init( void )
449 static GLboolean alreadyCalled
= GL_FALSE
;
450 _glthread_LOCK_MUTEX(OneTimeLock
);
451 if (!alreadyCalled
) {
452 /* do some implementation tests */
453 assert( sizeof(GLbyte
) == 1 );
454 assert( sizeof(GLshort
) >= 2 );
455 assert( sizeof(GLint
) >= 4 );
456 assert( sizeof(GLubyte
) == 1 );
457 assert( sizeof(GLushort
) >= 2 );
458 assert( sizeof(GLuint
) >= 4 );
467 gl_init_transformation();
473 if (getenv("MESA_DEBUG")) {
474 _glapi_noop_enable_warnings(GL_TRUE
);
477 _glapi_noop_enable_warnings(GL_FALSE
);
480 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
481 fprintf(stderr
, "Mesa DEBUG build %s %s\n", __DATE__
, __TIME__
);
484 alreadyCalled
= GL_TRUE
;
486 _glthread_UNLOCK_MUTEX(OneTimeLock
);
492 * Allocate and initialize a shared context state structure.
494 static struct gl_shared_state
*alloc_shared_state( void )
497 struct gl_shared_state
*ss
;
498 GLboolean outOfMemory
;
500 ss
= CALLOC_STRUCT(gl_shared_state
);
504 ss
->DisplayList
= _mesa_NewHashTable();
506 ss
->TexObjects
= _mesa_NewHashTable();
508 /* Default Texture objects */
509 outOfMemory
= GL_FALSE
;
510 for (d
= 1 ; d
<= 3 ; d
++) {
511 ss
->DefaultD
[d
] = gl_alloc_texture_object(ss
, 0, d
);
512 if (!ss
->DefaultD
[d
]) {
513 outOfMemory
= GL_TRUE
;
516 ss
->DefaultD
[d
]->RefCount
++; /* don't free if not in use */
519 if (!ss
->DisplayList
|| !ss
->TexObjects
|| outOfMemory
) {
520 /* Ran out of memory at some point. Free everything and return NULL */
522 _mesa_DeleteHashTable(ss
->DisplayList
);
524 _mesa_DeleteHashTable(ss
->TexObjects
);
526 gl_free_texture_object(ss
, ss
->DefaultD
[1]);
528 gl_free_texture_object(ss
, ss
->DefaultD
[2]);
530 gl_free_texture_object(ss
, ss
->DefaultD
[3]);
541 * Deallocate a shared state context and all children structures.
543 static void free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
545 /* Free display lists */
547 GLuint list
= _mesa_HashFirstEntry(ss
->DisplayList
);
549 gl_destroy_list(ctx
, list
);
555 _mesa_DeleteHashTable(ss
->DisplayList
);
557 /* Free texture objects */
558 while (ss
->TexObjectList
)
560 if (ctx
->Driver
.DeleteTexture
)
561 (*ctx
->Driver
.DeleteTexture
)( ctx
, ss
->TexObjectList
);
562 /* this function removes from linked list too! */
563 gl_free_texture_object(ss
, ss
->TexObjectList
);
565 _mesa_DeleteHashTable(ss
->TexObjects
);
573 * Initialize the nth light. Note that the defaults for light 0 are
574 * different than the other lights.
576 static void init_light( struct gl_light
*l
, GLuint n
)
578 make_empty_list( l
);
580 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
582 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
583 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
586 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
587 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
589 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
590 ASSIGN_3V( l
->EyeDirection
, 0.0, 0.0, -1.0 );
591 l
->SpotExponent
= 0.0;
592 gl_compute_spot_exp_table( l
);
593 l
->SpotCutoff
= 180.0;
594 l
->CosCutoff
= 0.0; /* KW: -ve values not admitted */
595 l
->ConstantAttenuation
= 1.0;
596 l
->LinearAttenuation
= 0.0;
597 l
->QuadraticAttenuation
= 0.0;
598 l
->Enabled
= GL_FALSE
;
603 static void init_lightmodel( struct gl_lightmodel
*lm
)
605 ASSIGN_4V( lm
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
606 lm
->LocalViewer
= GL_FALSE
;
607 lm
->TwoSide
= GL_FALSE
;
608 lm
->ColorControl
= GL_SINGLE_COLOR
;
612 static void init_material( struct gl_material
*m
)
614 ASSIGN_4V( m
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
615 ASSIGN_4V( m
->Diffuse
, 0.8, 0.8, 0.8, 1.0 );
616 ASSIGN_4V( m
->Specular
, 0.0, 0.0, 0.0, 1.0 );
617 ASSIGN_4V( m
->Emission
, 0.0, 0.0, 0.0, 1.0 );
621 m
->SpecularIndex
= 1;
626 static void init_texture_unit( GLcontext
*ctx
, GLuint unit
)
628 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
630 texUnit
->EnvMode
= GL_MODULATE
;
631 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
632 texUnit
->TexGenEnabled
= 0;
633 texUnit
->GenModeS
= GL_EYE_LINEAR
;
634 texUnit
->GenModeT
= GL_EYE_LINEAR
;
635 texUnit
->GenModeR
= GL_EYE_LINEAR
;
636 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
637 /* Yes, these plane coefficients are correct! */
638 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
639 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
640 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
641 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
642 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
643 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
644 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
645 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
647 texUnit
->CurrentD
[1] = ctx
->Shared
->DefaultD
[1];
648 texUnit
->CurrentD
[2] = ctx
->Shared
->DefaultD
[2];
649 texUnit
->CurrentD
[3] = ctx
->Shared
->DefaultD
[3];
653 static void init_fallback_arrays( GLcontext
*ctx
)
655 struct gl_client_array
*cl
;
658 cl
= &ctx
->Fallback
.Normal
;
663 cl
->Ptr
= (void *) ctx
->Current
.Normal
;
666 cl
= &ctx
->Fallback
.Color
;
668 cl
->Type
= GL_UNSIGNED_BYTE
;
671 cl
->Ptr
= (void *) ctx
->Current
.ByteColor
;
674 cl
= &ctx
->Fallback
.Index
;
676 cl
->Type
= GL_UNSIGNED_INT
;
679 cl
->Ptr
= (void *) &ctx
->Current
.Index
;
682 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++) {
683 cl
= &ctx
->Fallback
.TexCoord
[i
];
688 cl
->Ptr
= (void *) ctx
->Current
.Texcoord
[i
];
692 cl
= &ctx
->Fallback
.EdgeFlag
;
694 cl
->Type
= GL_UNSIGNED_BYTE
;
697 cl
->Ptr
= (void *) &ctx
->Current
.EdgeFlag
;
702 /* Initialize a 1-D evaluator map */
703 static void init_1d_map( struct gl_1d_map
*map
, int n
, const float *initial
)
708 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
712 map
->Points
[i
] = initial
[i
];
717 /* Initialize a 2-D evaluator map */
718 static void init_2d_map( struct gl_2d_map
*map
, int n
, const float *initial
)
726 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
730 map
->Points
[i
] = initial
[i
];
735 static void init_color_table( struct gl_color_table
*p
)
742 p
->IntFormat
= GL_RGBA
;
748 * Initialize the attribute groups in a GLcontext.
750 static void init_attrib_groups( GLcontext
*ctx
)
756 /* Constants, may be overriden by device drivers */
757 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
758 ctx
->Const
.MaxTextureSize
= 1 << (MAX_TEXTURE_LEVELS
- 1);
759 ctx
->Const
.MaxTextureUnits
= MAX_TEXTURE_UNITS
;
760 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
761 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
762 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
763 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
764 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
765 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
766 ctx
->Const
.PointSizeGranularity
= POINT_SIZE_GRANULARITY
;
767 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
768 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
769 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
770 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
771 ctx
->Const
.LineWidthGranularity
= LINE_WIDTH_GRANULARITY
;
772 ctx
->Const
.NumAuxBuffers
= NUM_AUX_BUFFERS
;
774 /* Modelview matrix */
775 gl_matrix_ctr( &ctx
->ModelView
);
776 gl_matrix_alloc_inv( &ctx
->ModelView
);
778 ctx
->ModelViewStackDepth
= 0;
779 for (i
= 0; i
< MAX_MODELVIEW_STACK_DEPTH
- 1; i
++) {
780 gl_matrix_ctr( &ctx
->ModelViewStack
[i
] );
781 gl_matrix_alloc_inv( &ctx
->ModelViewStack
[i
] );
784 /* Projection matrix - need inv for user clipping in clip space*/
785 gl_matrix_ctr( &ctx
->ProjectionMatrix
);
786 gl_matrix_alloc_inv( &ctx
->ProjectionMatrix
);
788 gl_matrix_ctr( &ctx
->ModelProjectMatrix
);
789 gl_matrix_ctr( &ctx
->ModelProjectWinMatrix
);
790 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
792 ctx
->ProjectionStackDepth
= 0;
793 ctx
->NearFarStack
[0][0] = 1.0; /* These values seem weird by make */
794 ctx
->NearFarStack
[0][1] = 0.0; /* sense mathematically. */
796 for (i
= 0; i
< MAX_PROJECTION_STACK_DEPTH
- 1; i
++) {
797 gl_matrix_ctr( &ctx
->ProjectionStack
[i
] );
798 gl_matrix_alloc_inv( &ctx
->ProjectionStack
[i
] );
802 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++) {
803 gl_matrix_ctr( &ctx
->TextureMatrix
[i
] );
804 ctx
->TextureStackDepth
[i
] = 0;
805 for (j
= 0; j
< MAX_TEXTURE_STACK_DEPTH
- 1; j
++) {
806 ctx
->TextureStack
[i
][j
].inv
= 0;
810 /* Accumulate buffer group */
811 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
813 /* Color buffer group */
814 ctx
->Color
.IndexMask
= 0xffffffff;
815 ctx
->Color
.ColorMask
[0] = 0xff;
816 ctx
->Color
.ColorMask
[1] = 0xff;
817 ctx
->Color
.ColorMask
[2] = 0xff;
818 ctx
->Color
.ColorMask
[3] = 0xff;
819 ctx
->Color
.SWmasking
= GL_FALSE
;
820 ctx
->Color
.ClearIndex
= 0;
821 ASSIGN_4V( ctx
->Color
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
822 ctx
->Color
.DrawBuffer
= GL_FRONT
;
823 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
824 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
825 ctx
->Color
.AlphaRef
= 0;
826 ctx
->Color
.BlendEnabled
= GL_FALSE
;
827 ctx
->Color
.BlendSrcRGB
= GL_ONE
;
828 ctx
->Color
.BlendDstRGB
= GL_ZERO
;
829 ctx
->Color
.BlendSrcA
= GL_ONE
;
830 ctx
->Color
.BlendDstA
= GL_ZERO
;
831 ctx
->Color
.BlendEquation
= GL_FUNC_ADD_EXT
;
832 ctx
->Color
.BlendFunc
= NULL
; /* this pointer set only when needed */
833 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
834 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
835 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
836 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
837 ctx
->Color
.LogicOp
= GL_COPY
;
838 ctx
->Color
.DitherFlag
= GL_TRUE
;
839 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
842 ASSIGN_4V( ctx
->Current
.ByteColor
, 255, 255, 255, 255);
843 ctx
->Current
.Index
= 1;
844 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
845 ASSIGN_4V( ctx
->Current
.Texcoord
[i
], 0.0, 0.0, 0.0, 1.0 );
846 ASSIGN_4V( ctx
->Current
.RasterPos
, 0.0, 0.0, 0.0, 1.0 );
847 ctx
->Current
.RasterDistance
= 0.0;
848 ASSIGN_4V( ctx
->Current
.RasterColor
, 1.0, 1.0, 1.0, 1.0 );
849 ctx
->Current
.RasterIndex
= 1;
850 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
851 ASSIGN_4V( ctx
->Current
.RasterMultiTexCoord
[i
], 0.0, 0.0, 0.0, 1.0 );
852 ctx
->Current
.RasterTexCoord
= ctx
->Current
.RasterMultiTexCoord
[0];
853 ctx
->Current
.RasterPosValid
= GL_TRUE
;
854 ctx
->Current
.EdgeFlag
= GL_TRUE
;
855 ASSIGN_3V( ctx
->Current
.Normal
, 0.0, 0.0, 1.0 );
856 ctx
->Current
.Primitive
= (GLenum
) (GL_POLYGON
+ 1);
858 ctx
->Current
.Flag
= (VERT_NORM
|VERT_INDEX
|VERT_RGBA
|VERT_EDGE
|
859 VERT_TEX0_1
|VERT_TEX1_1
|VERT_MATERIAL
);
861 init_fallback_arrays( ctx
);
863 /* Depth buffer group */
864 ctx
->Depth
.Test
= GL_FALSE
;
865 ctx
->Depth
.Clear
= 1.0;
866 ctx
->Depth
.Func
= GL_LESS
;
867 ctx
->Depth
.Mask
= GL_TRUE
;
868 ctx
->Depth
.OcclusionTest
= GL_FALSE
;
870 /* Evaluators group */
871 ctx
->Eval
.Map1Color4
= GL_FALSE
;
872 ctx
->Eval
.Map1Index
= GL_FALSE
;
873 ctx
->Eval
.Map1Normal
= GL_FALSE
;
874 ctx
->Eval
.Map1TextureCoord1
= GL_FALSE
;
875 ctx
->Eval
.Map1TextureCoord2
= GL_FALSE
;
876 ctx
->Eval
.Map1TextureCoord3
= GL_FALSE
;
877 ctx
->Eval
.Map1TextureCoord4
= GL_FALSE
;
878 ctx
->Eval
.Map1Vertex3
= GL_FALSE
;
879 ctx
->Eval
.Map1Vertex4
= GL_FALSE
;
880 ctx
->Eval
.Map2Color4
= GL_FALSE
;
881 ctx
->Eval
.Map2Index
= GL_FALSE
;
882 ctx
->Eval
.Map2Normal
= GL_FALSE
;
883 ctx
->Eval
.Map2TextureCoord1
= GL_FALSE
;
884 ctx
->Eval
.Map2TextureCoord2
= GL_FALSE
;
885 ctx
->Eval
.Map2TextureCoord3
= GL_FALSE
;
886 ctx
->Eval
.Map2TextureCoord4
= GL_FALSE
;
887 ctx
->Eval
.Map2Vertex3
= GL_FALSE
;
888 ctx
->Eval
.Map2Vertex4
= GL_FALSE
;
889 ctx
->Eval
.AutoNormal
= GL_FALSE
;
890 ctx
->Eval
.MapGrid1un
= 1;
891 ctx
->Eval
.MapGrid1u1
= 0.0;
892 ctx
->Eval
.MapGrid1u2
= 1.0;
893 ctx
->Eval
.MapGrid2un
= 1;
894 ctx
->Eval
.MapGrid2vn
= 1;
895 ctx
->Eval
.MapGrid2u1
= 0.0;
896 ctx
->Eval
.MapGrid2u2
= 1.0;
897 ctx
->Eval
.MapGrid2v1
= 0.0;
898 ctx
->Eval
.MapGrid2v2
= 1.0;
902 static GLfloat vertex
[4] = { 0.0, 0.0, 0.0, 1.0 };
903 static GLfloat normal
[3] = { 0.0, 0.0, 1.0 };
904 static GLfloat index
[1] = { 1.0 };
905 static GLfloat color
[4] = { 1.0, 1.0, 1.0, 1.0 };
906 static GLfloat texcoord
[4] = { 0.0, 0.0, 0.0, 1.0 };
908 init_1d_map( &ctx
->EvalMap
.Map1Vertex3
, 3, vertex
);
909 init_1d_map( &ctx
->EvalMap
.Map1Vertex4
, 4, vertex
);
910 init_1d_map( &ctx
->EvalMap
.Map1Index
, 1, index
);
911 init_1d_map( &ctx
->EvalMap
.Map1Color4
, 4, color
);
912 init_1d_map( &ctx
->EvalMap
.Map1Normal
, 3, normal
);
913 init_1d_map( &ctx
->EvalMap
.Map1Texture1
, 1, texcoord
);
914 init_1d_map( &ctx
->EvalMap
.Map1Texture2
, 2, texcoord
);
915 init_1d_map( &ctx
->EvalMap
.Map1Texture3
, 3, texcoord
);
916 init_1d_map( &ctx
->EvalMap
.Map1Texture4
, 4, texcoord
);
918 init_2d_map( &ctx
->EvalMap
.Map2Vertex3
, 3, vertex
);
919 init_2d_map( &ctx
->EvalMap
.Map2Vertex4
, 4, vertex
);
920 init_2d_map( &ctx
->EvalMap
.Map2Index
, 1, index
);
921 init_2d_map( &ctx
->EvalMap
.Map2Color4
, 4, color
);
922 init_2d_map( &ctx
->EvalMap
.Map2Normal
, 3, normal
);
923 init_2d_map( &ctx
->EvalMap
.Map2Texture1
, 1, texcoord
);
924 init_2d_map( &ctx
->EvalMap
.Map2Texture2
, 2, texcoord
);
925 init_2d_map( &ctx
->EvalMap
.Map2Texture3
, 3, texcoord
);
926 init_2d_map( &ctx
->EvalMap
.Map2Texture4
, 4, texcoord
);
930 ctx
->Fog
.Enabled
= GL_FALSE
;
931 ctx
->Fog
.Mode
= GL_EXP
;
932 ASSIGN_4V( ctx
->Fog
.Color
, 0.0, 0.0, 0.0, 0.0 );
933 ctx
->Fog
.Index
= 0.0;
934 ctx
->Fog
.Density
= 1.0;
935 ctx
->Fog
.Start
= 0.0;
939 ctx
->Hint
.PerspectiveCorrection
= GL_DONT_CARE
;
940 ctx
->Hint
.PointSmooth
= GL_DONT_CARE
;
941 ctx
->Hint
.LineSmooth
= GL_DONT_CARE
;
942 ctx
->Hint
.PolygonSmooth
= GL_DONT_CARE
;
943 ctx
->Hint
.Fog
= GL_DONT_CARE
;
945 ctx
->Hint
.AllowDrawWin
= GL_TRUE
;
946 ctx
->Hint
.AllowDrawSpn
= GL_TRUE
;
947 ctx
->Hint
.AllowDrawMem
= GL_TRUE
;
948 ctx
->Hint
.StrictLighting
= GL_TRUE
;
951 gl_pipeline_init( ctx
);
955 gl_extensions_ctr( ctx
);
957 ctx
->AllowVertexCull
= CLIP_CULLED_BIT
;
960 for (i
=0;i
<MAX_LIGHTS
;i
++) {
961 init_light( &ctx
->Light
.Light
[i
], i
);
963 make_empty_list( &ctx
->Light
.EnabledList
);
965 init_lightmodel( &ctx
->Light
.Model
);
966 init_material( &ctx
->Light
.Material
[0] );
967 init_material( &ctx
->Light
.Material
[1] );
968 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
969 ctx
->Light
.Enabled
= GL_FALSE
;
970 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
971 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
972 ctx
->Light
.ColorMaterialBitmask
973 = gl_material_bitmask( ctx
,
975 GL_AMBIENT_AND_DIFFUSE
, ~0, 0 );
977 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
979 /* Lighting miscellaneous */
980 ctx
->ShineTabList
= MALLOC_STRUCT( gl_shine_tab
);
981 make_empty_list( ctx
->ShineTabList
);
982 for (i
= 0 ; i
< 10 ; i
++) {
983 struct gl_shine_tab
*s
= MALLOC_STRUCT( gl_shine_tab
);
986 insert_at_tail( ctx
->ShineTabList
, s
);
988 for (i
= 0 ; i
< 4 ; i
++) {
989 ctx
->ShineTable
[i
] = ctx
->ShineTabList
->prev
;
990 ctx
->ShineTable
[i
]->refcount
++;
995 ctx
->Line
.SmoothFlag
= GL_FALSE
;
996 ctx
->Line
.StippleFlag
= GL_FALSE
;
997 ctx
->Line
.Width
= 1.0;
998 ctx
->Line
.StipplePattern
= 0xffff;
999 ctx
->Line
.StippleFactor
= 1;
1001 /* Display List group */
1002 ctx
->List
.ListBase
= 0;
1005 ctx
->Pixel
.RedBias
= 0.0;
1006 ctx
->Pixel
.RedScale
= 1.0;
1007 ctx
->Pixel
.GreenBias
= 0.0;
1008 ctx
->Pixel
.GreenScale
= 1.0;
1009 ctx
->Pixel
.BlueBias
= 0.0;
1010 ctx
->Pixel
.BlueScale
= 1.0;
1011 ctx
->Pixel
.AlphaBias
= 0.0;
1012 ctx
->Pixel
.AlphaScale
= 1.0;
1013 ctx
->Pixel
.ScaleOrBiasRGBA
= GL_FALSE
;
1014 ctx
->Pixel
.DepthBias
= 0.0;
1015 ctx
->Pixel
.DepthScale
= 1.0;
1016 ctx
->Pixel
.IndexOffset
= 0;
1017 ctx
->Pixel
.IndexShift
= 0;
1018 ctx
->Pixel
.ZoomX
= 1.0;
1019 ctx
->Pixel
.ZoomY
= 1.0;
1020 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
1021 ctx
->Pixel
.MapStencilFlag
= GL_FALSE
;
1022 ctx
->Pixel
.MapStoSsize
= 1;
1023 ctx
->Pixel
.MapItoIsize
= 1;
1024 ctx
->Pixel
.MapItoRsize
= 1;
1025 ctx
->Pixel
.MapItoGsize
= 1;
1026 ctx
->Pixel
.MapItoBsize
= 1;
1027 ctx
->Pixel
.MapItoAsize
= 1;
1028 ctx
->Pixel
.MapRtoRsize
= 1;
1029 ctx
->Pixel
.MapGtoGsize
= 1;
1030 ctx
->Pixel
.MapBtoBsize
= 1;
1031 ctx
->Pixel
.MapAtoAsize
= 1;
1032 ctx
->Pixel
.MapStoS
[0] = 0;
1033 ctx
->Pixel
.MapItoI
[0] = 0;
1034 ctx
->Pixel
.MapItoR
[0] = 0.0;
1035 ctx
->Pixel
.MapItoG
[0] = 0.0;
1036 ctx
->Pixel
.MapItoB
[0] = 0.0;
1037 ctx
->Pixel
.MapItoA
[0] = 0.0;
1038 ctx
->Pixel
.MapItoR8
[0] = 0;
1039 ctx
->Pixel
.MapItoG8
[0] = 0;
1040 ctx
->Pixel
.MapItoB8
[0] = 0;
1041 ctx
->Pixel
.MapItoA8
[0] = 0;
1042 ctx
->Pixel
.MapRtoR
[0] = 0.0;
1043 ctx
->Pixel
.MapGtoG
[0] = 0.0;
1044 ctx
->Pixel
.MapBtoB
[0] = 0.0;
1045 ctx
->Pixel
.MapAtoA
[0] = 0.0;
1048 ctx
->Point
.SmoothFlag
= GL_FALSE
;
1049 ctx
->Point
.Size
= 1.0;
1050 ctx
->Point
.Params
[0] = 1.0;
1051 ctx
->Point
.Params
[1] = 0.0;
1052 ctx
->Point
.Params
[2] = 0.0;
1053 ctx
->Point
.Attenuated
= GL_FALSE
;
1054 ctx
->Point
.MinSize
= 0.0;
1055 ctx
->Point
.MaxSize
= (GLfloat
) MAX_POINT_SIZE
;
1056 ctx
->Point
.Threshold
= 1.0;
1059 ctx
->Polygon
.CullFlag
= GL_FALSE
;
1060 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
1061 ctx
->Polygon
.FrontFace
= GL_CCW
;
1062 ctx
->Polygon
.FrontBit
= 0;
1063 ctx
->Polygon
.FrontMode
= GL_FILL
;
1064 ctx
->Polygon
.BackMode
= GL_FILL
;
1065 ctx
->Polygon
.Unfilled
= GL_FALSE
;
1066 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
1067 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
1068 ctx
->Polygon
.OffsetFactor
= 0.0F
;
1069 ctx
->Polygon
.OffsetUnits
= 0.0F
;
1070 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
1071 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
1072 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
1074 /* Polygon Stipple group */
1075 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );
1078 ctx
->Scissor
.Enabled
= GL_FALSE
;
1081 ctx
->Scissor
.Width
= 0;
1082 ctx
->Scissor
.Height
= 0;
1085 ctx
->Stencil
.Enabled
= GL_FALSE
;
1086 ctx
->Stencil
.Function
= GL_ALWAYS
;
1087 ctx
->Stencil
.FailFunc
= GL_KEEP
;
1088 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
1089 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
1090 ctx
->Stencil
.Ref
= 0;
1091 ctx
->Stencil
.ValueMask
= STENCIL_MAX
;
1092 ctx
->Stencil
.Clear
= 0;
1093 ctx
->Stencil
.WriteMask
= STENCIL_MAX
;
1096 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
1097 ctx
->Texture
.CurrentTransformUnit
= 0; /* multitexture */
1098 ctx
->Texture
.Enabled
= 0;
1099 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
1100 init_texture_unit( ctx
, i
);
1101 init_color_table(&ctx
->Texture
.Palette
);
1103 /* Transformation group */
1104 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
1105 ctx
->Transform
.Normalize
= GL_FALSE
;
1106 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
1107 for (i
=0;i
<MAX_CLIP_PLANES
;i
++) {
1108 ctx
->Transform
.ClipEnabled
[i
] = GL_FALSE
;
1109 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
1111 ctx
->Transform
.AnyClip
= GL_FALSE
;
1113 /* Viewport group */
1114 ctx
->Viewport
.X
= 0;
1115 ctx
->Viewport
.Y
= 0;
1116 ctx
->Viewport
.Width
= 0;
1117 ctx
->Viewport
.Height
= 0;
1118 ctx
->Viewport
.Near
= 0.0;
1119 ctx
->Viewport
.Far
= 1.0;
1120 gl_matrix_ctr(&ctx
->Viewport
.WindowMap
);
1124 ctx
->Viewport
.WindowMap
.m
[Sz
] = 0.5 * ctx
->Visual
->DepthMaxF
;
1125 ctx
->Viewport
.WindowMap
.m
[Tz
] = 0.5 * ctx
->Visual
->DepthMaxF
;
1129 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1130 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1133 ctx
->Array
.Vertex
.Size
= 4;
1134 ctx
->Array
.Vertex
.Type
= GL_FLOAT
;
1135 ctx
->Array
.Vertex
.Stride
= 0;
1136 ctx
->Array
.Vertex
.StrideB
= 0;
1137 ctx
->Array
.Vertex
.Ptr
= NULL
;
1138 ctx
->Array
.Vertex
.Enabled
= GL_FALSE
;
1139 ctx
->Array
.Normal
.Type
= GL_FLOAT
;
1140 ctx
->Array
.Normal
.Stride
= 0;
1141 ctx
->Array
.Normal
.StrideB
= 0;
1142 ctx
->Array
.Normal
.Ptr
= NULL
;
1143 ctx
->Array
.Normal
.Enabled
= GL_FALSE
;
1144 ctx
->Array
.Color
.Size
= 4;
1145 ctx
->Array
.Color
.Type
= GL_FLOAT
;
1146 ctx
->Array
.Color
.Stride
= 0;
1147 ctx
->Array
.Color
.StrideB
= 0;
1148 ctx
->Array
.Color
.Ptr
= NULL
;
1149 ctx
->Array
.Color
.Enabled
= GL_FALSE
;
1150 ctx
->Array
.Index
.Type
= GL_FLOAT
;
1151 ctx
->Array
.Index
.Stride
= 0;
1152 ctx
->Array
.Index
.StrideB
= 0;
1153 ctx
->Array
.Index
.Ptr
= NULL
;
1154 ctx
->Array
.Index
.Enabled
= GL_FALSE
;
1155 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1156 ctx
->Array
.TexCoord
[i
].Size
= 4;
1157 ctx
->Array
.TexCoord
[i
].Type
= GL_FLOAT
;
1158 ctx
->Array
.TexCoord
[i
].Stride
= 0;
1159 ctx
->Array
.TexCoord
[i
].StrideB
= 0;
1160 ctx
->Array
.TexCoord
[i
].Ptr
= NULL
;
1161 ctx
->Array
.TexCoord
[i
].Enabled
= GL_FALSE
;
1163 ctx
->Array
.TexCoordInterleaveFactor
= 1;
1164 ctx
->Array
.EdgeFlag
.Stride
= 0;
1165 ctx
->Array
.EdgeFlag
.StrideB
= 0;
1166 ctx
->Array
.EdgeFlag
.Ptr
= NULL
;
1167 ctx
->Array
.EdgeFlag
.Enabled
= GL_FALSE
;
1168 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
1170 /* Pixel transfer */
1171 ctx
->Pack
.Alignment
= 4;
1172 ctx
->Pack
.RowLength
= 0;
1173 ctx
->Pack
.ImageHeight
= 0;
1174 ctx
->Pack
.SkipPixels
= 0;
1175 ctx
->Pack
.SkipRows
= 0;
1176 ctx
->Pack
.SkipImages
= 0;
1177 ctx
->Pack
.SwapBytes
= GL_FALSE
;
1178 ctx
->Pack
.LsbFirst
= GL_FALSE
;
1179 ctx
->Unpack
.Alignment
= 4;
1180 ctx
->Unpack
.RowLength
= 0;
1181 ctx
->Unpack
.ImageHeight
= 0;
1182 ctx
->Unpack
.SkipPixels
= 0;
1183 ctx
->Unpack
.SkipRows
= 0;
1184 ctx
->Unpack
.SkipImages
= 0;
1185 ctx
->Unpack
.SwapBytes
= GL_FALSE
;
1186 ctx
->Unpack
.LsbFirst
= GL_FALSE
;
1189 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
1190 ctx
->Feedback
.Buffer
= NULL
;
1191 ctx
->Feedback
.BufferSize
= 0;
1192 ctx
->Feedback
.Count
= 0;
1194 /* Selection/picking */
1195 ctx
->Select
.Buffer
= NULL
;
1196 ctx
->Select
.BufferSize
= 0;
1197 ctx
->Select
.BufferCount
= 0;
1198 ctx
->Select
.Hits
= 0;
1199 ctx
->Select
.NameStackDepth
= 0;
1201 /* Optimized Accum buffer */
1202 ctx
->IntegerAccumMode
= GL_TRUE
;
1203 ctx
->IntegerAccumScaler
= 0.0;
1205 /* Renderer and client attribute stacks */
1206 ctx
->AttribStackDepth
= 0;
1207 ctx
->ClientAttribStackDepth
= 0;
1210 ctx
->NewState
= NEW_ALL
;
1211 ctx
->RenderMode
= GL_RENDER
;
1212 ctx
->StippleCounter
= 0;
1213 ctx
->NeedNormals
= GL_FALSE
;
1214 ctx
->DoViewportMapping
= GL_TRUE
;
1216 ctx
->NeedEyeCoords
= GL_FALSE
;
1217 ctx
->NeedEyeNormals
= GL_FALSE
;
1218 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
1222 ctx
->ExecuteFlag
= GL_TRUE
;
1223 ctx
->CompileFlag
= GL_FALSE
;
1224 ctx
->CurrentListPtr
= NULL
;
1225 ctx
->CurrentBlock
= NULL
;
1226 ctx
->CurrentListNum
= 0;
1227 ctx
->CurrentPos
= 0;
1229 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1231 ctx
->CatchSignals
= GL_TRUE
;
1232 ctx
->OcclusionResult
= GL_FALSE
;
1234 /* For debug/development only */
1235 ctx
->NoRaster
= getenv("MESA_NO_RASTER") ? GL_TRUE
: GL_FALSE
;
1236 ctx
->FirstTimeCurrent
= GL_TRUE
;
1238 /* Dither disable */
1239 ctx
->NoDither
= getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
1240 if (ctx
->NoDither
) {
1241 if (getenv("MESA_DEBUG")) {
1242 fprintf(stderr
, "MESA_NO_DITHER set - dithering disabled\n");
1244 ctx
->Color
.DitherFlag
= GL_FALSE
;
1252 * Allocate the proxy textures. If we run out of memory part way through
1253 * the allocations clean up and return GL_FALSE.
1254 * Return: GL_TRUE=success, GL_FALSE=failure
1256 static GLboolean
alloc_proxy_textures( GLcontext
*ctx
)
1258 GLboolean out_of_memory
;
1261 ctx
->Texture
.Proxy1D
= gl_alloc_texture_object(NULL
, 0, 1);
1262 if (!ctx
->Texture
.Proxy1D
) {
1266 ctx
->Texture
.Proxy2D
= gl_alloc_texture_object(NULL
, 0, 2);
1267 if (!ctx
->Texture
.Proxy2D
) {
1268 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1272 ctx
->Texture
.Proxy3D
= gl_alloc_texture_object(NULL
, 0, 3);
1273 if (!ctx
->Texture
.Proxy3D
) {
1274 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1275 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1279 out_of_memory
= GL_FALSE
;
1280 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1281 ctx
->Texture
.Proxy1D
->Image
[i
] = gl_alloc_texture_image();
1282 ctx
->Texture
.Proxy2D
->Image
[i
] = gl_alloc_texture_image();
1283 ctx
->Texture
.Proxy3D
->Image
[i
] = gl_alloc_texture_image();
1284 if (!ctx
->Texture
.Proxy1D
->Image
[i
]
1285 || !ctx
->Texture
.Proxy2D
->Image
[i
]
1286 || !ctx
->Texture
.Proxy3D
->Image
[i
]) {
1287 out_of_memory
= GL_TRUE
;
1290 if (out_of_memory
) {
1291 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1292 if (ctx
->Texture
.Proxy1D
->Image
[i
]) {
1293 gl_free_texture_image(ctx
->Texture
.Proxy1D
->Image
[i
]);
1295 if (ctx
->Texture
.Proxy2D
->Image
[i
]) {
1296 gl_free_texture_image(ctx
->Texture
.Proxy2D
->Image
[i
]);
1298 if (ctx
->Texture
.Proxy3D
->Image
[i
]) {
1299 gl_free_texture_image(ctx
->Texture
.Proxy3D
->Image
[i
]);
1302 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1303 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1304 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy3D
);
1315 * Initialize a GLcontext struct.
1317 GLboolean
gl_initialize_context_data( GLcontext
*ctx
,
1319 GLcontext
*share_list
,
1323 (void) direct
; /* not used */
1325 /* misc one-time initializations */
1328 ctx
->DriverCtx
= driver_ctx
;
1329 ctx
->Visual
= visual
;
1330 ctx
->DrawBuffer
= NULL
;
1331 ctx
->ReadBuffer
= NULL
;
1333 ctx
->VB
= gl_vb_create_for_immediate( ctx
);
1338 ctx
->input
= ctx
->VB
->IM
;
1340 ctx
->PB
= gl_alloc_pb();
1348 /* share the group of display lists of another context */
1349 ctx
->Shared
= share_list
->Shared
;
1352 /* allocate new group of display lists */
1353 ctx
->Shared
= alloc_shared_state();
1361 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1362 ctx
->Shared
->RefCount
++;
1363 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1365 init_attrib_groups( ctx
);
1367 gl_reset_vb( ctx
->VB
);
1368 gl_reset_input( ctx
);
1370 if (visual
->DBflag
) {
1371 ctx
->Color
.DrawBuffer
= GL_BACK
;
1372 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
1373 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
1374 ctx
->Pixel
.ReadBuffer
= GL_BACK
;
1375 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
1378 ctx
->Color
.DrawBuffer
= GL_FRONT
;
1379 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
1380 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
1381 ctx
->Pixel
.ReadBuffer
= GL_FRONT
;
1382 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
1386 init_timings( ctx
);
1389 if (!alloc_proxy_textures(ctx
)) {
1390 free_shared_state(ctx
, ctx
->Shared
);
1397 /* setup API dispatch tables */
1398 ctx
->Exec
= (struct _glapi_table
*) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1399 ctx
->Save
= (struct _glapi_table
*) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1400 if (!ctx
->Exec
|| !ctx
->Save
) {
1401 free_shared_state(ctx
, ctx
->Shared
);
1408 _mesa_init_exec_table( ctx
->Exec
);
1409 _mesa_init_dlist_table( ctx
->Save
);
1410 ctx
->CurrentDispatch
= ctx
->Exec
;
1418 * Allocate and initialize a GLcontext structure.
1419 * Input: visual - a GLvisual pointer
1420 * sharelist - another context to share display lists with or NULL
1421 * driver_ctx - pointer to device driver's context state struct
1422 * Return: pointer to a new gl_context struct or NULL if error.
1424 GLcontext
*gl_create_context( GLvisual
*visual
,
1425 GLcontext
*share_list
,
1429 GLcontext
*ctx
= (GLcontext
*) CALLOC( sizeof(GLcontext
) );
1434 if (gl_initialize_context_data(ctx
, visual
, share_list
,
1435 driver_ctx
, direct
)) {
1447 * Free the data associated with the given context.
1448 * But don't free() the GLcontext struct itself!
1450 void gl_free_context_data( GLcontext
*ctx
)
1452 struct gl_shine_tab
*s
, *tmps
;
1455 /* if we're destroying the current context, unbind it first */
1456 if (ctx
== gl_get_current_context()) {
1457 gl_make_current(NULL
, NULL
);
1461 if (getenv("MESA_PROFILE")) {
1462 print_timings( ctx
);
1466 gl_matrix_dtr( &ctx
->ModelView
);
1467 for (i
= 0; i
< MAX_MODELVIEW_STACK_DEPTH
- 1; i
++) {
1468 gl_matrix_dtr( &ctx
->ModelViewStack
[i
] );
1470 gl_matrix_dtr( &ctx
->ProjectionMatrix
);
1471 for (i
= 0; i
< MAX_PROJECTION_STACK_DEPTH
- 1; i
++) {
1472 gl_matrix_dtr( &ctx
->ProjectionStack
[i
] );
1474 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1475 gl_matrix_dtr( &ctx
->TextureMatrix
[i
] );
1476 for (j
= 0; j
< MAX_TEXTURE_STACK_DEPTH
- 1; j
++) {
1477 gl_matrix_dtr( &ctx
->TextureStack
[i
][j
] );
1483 if(ctx
->input
!= ctx
->VB
->IM
)
1484 gl_immediate_free( ctx
->input
);
1486 gl_vb_free( ctx
->VB
);
1488 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1489 ctx
->Shared
->RefCount
--;
1490 assert(ctx
->Shared
->RefCount
>= 0);
1491 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1492 if (ctx
->Shared
->RefCount
== 0) {
1493 /* free shared state */
1494 free_shared_state( ctx
, ctx
->Shared
);
1497 foreach_s( s
, tmps
, ctx
->ShineTabList
) {
1500 FREE( ctx
->ShineTabList
);
1502 /* Free proxy texture objects */
1503 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy1D
);
1504 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy2D
);
1505 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy3D
);
1507 /* Free evaluator data */
1508 if (ctx
->EvalMap
.Map1Vertex3
.Points
)
1509 FREE( ctx
->EvalMap
.Map1Vertex3
.Points
);
1510 if (ctx
->EvalMap
.Map1Vertex4
.Points
)
1511 FREE( ctx
->EvalMap
.Map1Vertex4
.Points
);
1512 if (ctx
->EvalMap
.Map1Index
.Points
)
1513 FREE( ctx
->EvalMap
.Map1Index
.Points
);
1514 if (ctx
->EvalMap
.Map1Color4
.Points
)
1515 FREE( ctx
->EvalMap
.Map1Color4
.Points
);
1516 if (ctx
->EvalMap
.Map1Normal
.Points
)
1517 FREE( ctx
->EvalMap
.Map1Normal
.Points
);
1518 if (ctx
->EvalMap
.Map1Texture1
.Points
)
1519 FREE( ctx
->EvalMap
.Map1Texture1
.Points
);
1520 if (ctx
->EvalMap
.Map1Texture2
.Points
)
1521 FREE( ctx
->EvalMap
.Map1Texture2
.Points
);
1522 if (ctx
->EvalMap
.Map1Texture3
.Points
)
1523 FREE( ctx
->EvalMap
.Map1Texture3
.Points
);
1524 if (ctx
->EvalMap
.Map1Texture4
.Points
)
1525 FREE( ctx
->EvalMap
.Map1Texture4
.Points
);
1527 if (ctx
->EvalMap
.Map2Vertex3
.Points
)
1528 FREE( ctx
->EvalMap
.Map2Vertex3
.Points
);
1529 if (ctx
->EvalMap
.Map2Vertex4
.Points
)
1530 FREE( ctx
->EvalMap
.Map2Vertex4
.Points
);
1531 if (ctx
->EvalMap
.Map2Index
.Points
)
1532 FREE( ctx
->EvalMap
.Map2Index
.Points
);
1533 if (ctx
->EvalMap
.Map2Color4
.Points
)
1534 FREE( ctx
->EvalMap
.Map2Color4
.Points
);
1535 if (ctx
->EvalMap
.Map2Normal
.Points
)
1536 FREE( ctx
->EvalMap
.Map2Normal
.Points
);
1537 if (ctx
->EvalMap
.Map2Texture1
.Points
)
1538 FREE( ctx
->EvalMap
.Map2Texture1
.Points
);
1539 if (ctx
->EvalMap
.Map2Texture2
.Points
)
1540 FREE( ctx
->EvalMap
.Map2Texture2
.Points
);
1541 if (ctx
->EvalMap
.Map2Texture3
.Points
)
1542 FREE( ctx
->EvalMap
.Map2Texture3
.Points
);
1543 if (ctx
->EvalMap
.Map2Texture4
.Points
)
1544 FREE( ctx
->EvalMap
.Map2Texture4
.Points
);
1546 /* Free cache of immediate buffers. */
1547 while (ctx
->nr_im_queued
-- > 0) {
1548 struct immediate
* next
= ctx
->freed_im_queue
->next
;
1549 FREE( ctx
->freed_im_queue
);
1550 ctx
->freed_im_queue
= next
;
1552 gl_extensions_dtr(ctx
);
1561 * Destroy a GLcontext structure.
1563 void gl_destroy_context( GLcontext
*ctx
)
1566 gl_free_context_data(ctx
);
1567 FREE( (void *) ctx
);
1574 * Called by the driver after both the context and driver are fully
1575 * initialized. Currently just reads the config file.
1577 void gl_context_initialize( GLcontext
*ctx
)
1579 gl_read_config_file( ctx
);
1585 * Copy attribute groups from one context to another.
1586 * Input: src - source context
1587 * dst - destination context
1588 * mask - bitwise OR of GL_*_BIT flags
1590 void gl_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1592 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1593 MEMCPY( &dst
->Accum
, &src
->Accum
, sizeof(struct gl_accum_attrib
) );
1595 if (mask
& GL_COLOR_BUFFER_BIT
) {
1596 MEMCPY( &dst
->Color
, &src
->Color
, sizeof(struct gl_colorbuffer_attrib
) );
1598 if (mask
& GL_CURRENT_BIT
) {
1599 MEMCPY( &dst
->Current
, &src
->Current
, sizeof(struct gl_current_attrib
) );
1601 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1602 MEMCPY( &dst
->Depth
, &src
->Depth
, sizeof(struct gl_depthbuffer_attrib
) );
1604 if (mask
& GL_ENABLE_BIT
) {
1607 if (mask
& GL_EVAL_BIT
) {
1608 MEMCPY( &dst
->Eval
, &src
->Eval
, sizeof(struct gl_eval_attrib
) );
1610 if (mask
& GL_FOG_BIT
) {
1611 MEMCPY( &dst
->Fog
, &src
->Fog
, sizeof(struct gl_fog_attrib
) );
1613 if (mask
& GL_HINT_BIT
) {
1614 MEMCPY( &dst
->Hint
, &src
->Hint
, sizeof(struct gl_hint_attrib
) );
1616 if (mask
& GL_LIGHTING_BIT
) {
1617 MEMCPY( &dst
->Light
, &src
->Light
, sizeof(struct gl_light_attrib
) );
1618 /* gl_reinit_light_attrib( &dst->Light ); */
1620 if (mask
& GL_LINE_BIT
) {
1621 MEMCPY( &dst
->Line
, &src
->Line
, sizeof(struct gl_line_attrib
) );
1623 if (mask
& GL_LIST_BIT
) {
1624 MEMCPY( &dst
->List
, &src
->List
, sizeof(struct gl_list_attrib
) );
1626 if (mask
& GL_PIXEL_MODE_BIT
) {
1627 MEMCPY( &dst
->Pixel
, &src
->Pixel
, sizeof(struct gl_pixel_attrib
) );
1629 if (mask
& GL_POINT_BIT
) {
1630 MEMCPY( &dst
->Point
, &src
->Point
, sizeof(struct gl_point_attrib
) );
1632 if (mask
& GL_POLYGON_BIT
) {
1633 MEMCPY( &dst
->Polygon
, &src
->Polygon
, sizeof(struct gl_polygon_attrib
) );
1635 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1636 /* Use loop instead of MEMCPY due to problem with Portland Group's
1637 * C compiler. Reported by John Stone.
1640 for (i
=0;i
<32;i
++) {
1641 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1644 if (mask
& GL_SCISSOR_BIT
) {
1645 MEMCPY( &dst
->Scissor
, &src
->Scissor
, sizeof(struct gl_scissor_attrib
) );
1647 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1648 MEMCPY( &dst
->Stencil
, &src
->Stencil
, sizeof(struct gl_stencil_attrib
) );
1650 if (mask
& GL_TEXTURE_BIT
) {
1651 MEMCPY( &dst
->Texture
, &src
->Texture
, sizeof(struct gl_texture_attrib
) );
1653 if (mask
& GL_TRANSFORM_BIT
) {
1654 MEMCPY( &dst
->Transform
, &src
->Transform
, sizeof(struct gl_transform_attrib
) );
1656 if (mask
& GL_VIEWPORT_BIT
) {
1657 MEMCPY( &dst
->Viewport
, &src
->Viewport
, sizeof(struct gl_viewport_attrib
) );
1663 * Set the current context, binding the given frame buffer to the context.
1665 void gl_make_current( GLcontext
*newCtx
, GLframebuffer
*buffer
)
1667 gl_make_current2( newCtx
, buffer
, buffer
);
1672 * Bind the given context to the given draw-buffer and read-buffer
1673 * and make it the current context for this thread.
1675 void gl_make_current2( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1676 GLframebuffer
*readBuffer
)
1679 GLcontext
*oldCtx
= gl_get_context();
1681 /* Flush the old context
1684 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx
, "gl_make_current");
1686 /* unbind frame buffers from context */
1687 if (oldCtx
->DrawBuffer
) {
1688 oldCtx
->DrawBuffer
= NULL
;
1690 if (oldCtx
->ReadBuffer
) {
1691 oldCtx
->ReadBuffer
= NULL
;
1696 /* We call this function periodically (just here for now) in
1697 * order to detect when multithreading has begun.
1699 _glapi_check_multithread();
1701 _glapi_set_context((void *) newCtx
);
1702 ASSERT(gl_get_current_context() == newCtx
);
1704 SET_IMMEDIATE(newCtx
, newCtx
->input
);
1705 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1708 _glapi_set_dispatch(NULL
); /* none current */
1711 if (MESA_VERBOSE
) fprintf(stderr
, "gl_make_current()\n");
1713 if (newCtx
&& drawBuffer
&& readBuffer
) {
1714 /* TODO: check if newCtx and buffer's visual match??? */
1715 newCtx
->DrawBuffer
= drawBuffer
;
1716 newCtx
->ReadBuffer
= readBuffer
;
1717 newCtx
->NewState
= NEW_ALL
; /* just to be safe */
1718 gl_update_state( newCtx
);
1721 /* We can use this to help debug user's problems. Tell the to set
1722 * the MESA_INFO env variable before running their app. Then the
1723 * first time each context is made current we'll print some useful
1726 if (newCtx
&& newCtx
->FirstTimeCurrent
) {
1727 if (getenv("MESA_INFO")) {
1728 fprintf(stderr
, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION
));
1729 fprintf(stderr
, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER
));
1730 fprintf(stderr
, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR
));
1731 fprintf(stderr
, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS
));
1732 #if defined(THREADS)
1733 fprintf(stderr
, "Mesa thread-safe: YES\n");
1735 fprintf(stderr
, "Mesa thread-safe: NO\n");
1737 #if defined(USE_X86_ASM)
1738 fprintf(stderr
, "Mesa x86-optimized: YES\n");
1740 fprintf(stderr
, "Mesa x86-optimized: NO\n");
1743 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1750 * Return current context handle for the calling thread.
1751 * This isn't the fastest way to get the current context.
1752 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1754 GLcontext
*gl_get_current_context( void )
1756 return (GLcontext
*) _glapi_get_context();
1762 * This should be called by device drivers just before they do a
1763 * swapbuffers. Any pending rendering commands will be executed.
1766 _mesa_swapbuffers(GLcontext
*ctx
)
1768 FLUSH_VB( ctx
, "swap buffers" );
1774 * Return pointer to this context's current API dispatch table.
1775 * It'll either be the immediate-mode execute dispatcher or the
1776 * display list compile dispatcher.
1778 struct _glapi_table
*
1779 _mesa_get_dispatch(GLcontext
*ctx
)
1781 return ctx
->CurrentDispatch
;
1786 /**********************************************************************/
1787 /***** Miscellaneous functions *****/
1788 /**********************************************************************/
1792 * This function is called when the Mesa user has stumbled into a code
1793 * path which may not be implemented fully or correctly.
1795 void gl_problem( const GLcontext
*ctx
, const char *s
)
1797 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
1798 fprintf( stderr
, "Report to mesa-bugs@mesa3d.org\n" );
1805 * This is called to inform the user that he or she has tried to do
1806 * something illogical or if there's likely a bug in their program
1807 * (like enabled depth testing without a depth buffer).
1809 void gl_warning( const GLcontext
*ctx
, const char *s
)
1815 if (getenv("MESA_DEBUG")) {
1823 fprintf( stderr
, "Mesa warning: %s\n", s
);
1831 * Compile an error into current display list.
1833 void gl_compile_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1835 if (ctx
->CompileFlag
)
1836 gl_save_error( ctx
, error
, s
);
1838 if (ctx
->ExecuteFlag
)
1839 gl_error( ctx
, error
, s
);
1845 * This is Mesa's error handler. Normally, all that's done is the updating
1846 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1847 * environment variable "MESA_DEBUG" is defined then a real error message
1848 * is printed to stderr.
1849 * Input: error - the error value
1850 * s - a diagnostic string
1852 void gl_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1859 if (getenv("MESA_DEBUG")) {
1872 strcpy( errstr
, "GL_NO_ERROR" );
1874 case GL_INVALID_VALUE
:
1875 strcpy( errstr
, "GL_INVALID_VALUE" );
1877 case GL_INVALID_ENUM
:
1878 strcpy( errstr
, "GL_INVALID_ENUM" );
1880 case GL_INVALID_OPERATION
:
1881 strcpy( errstr
, "GL_INVALID_OPERATION" );
1883 case GL_STACK_OVERFLOW
:
1884 strcpy( errstr
, "GL_STACK_OVERFLOW" );
1886 case GL_STACK_UNDERFLOW
:
1887 strcpy( errstr
, "GL_STACK_UNDERFLOW" );
1889 case GL_OUT_OF_MEMORY
:
1890 strcpy( errstr
, "GL_OUT_OF_MEMORY" );
1893 strcpy( errstr
, "unknown" );
1896 fprintf( stderr
, "Mesa user error: %s in %s\n", errstr
, s
);
1899 if (ctx
->ErrorValue
==GL_NO_ERROR
) {
1900 ctx
->ErrorValue
= error
;
1903 /* Call device driver's error handler, if any. This is used on the Mac. */
1904 if (ctx
->Driver
.Error
) {
1905 (*ctx
->Driver
.Error
)( ctx
);
1912 _mesa_Finish( void )
1914 GET_CURRENT_CONTEXT(ctx
);
1915 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glFinish");
1916 if (ctx
->Driver
.Finish
) {
1917 (*ctx
->Driver
.Finish
)( ctx
);
1926 GET_CURRENT_CONTEXT(ctx
);
1927 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glFlush");
1928 if (ctx
->Driver
.Flush
) {
1929 (*ctx
->Driver
.Flush
)( ctx
);