1 /* $Id: context.c,v 1.42 2000/02/03 19:40:07 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 * depthFits - requested minimum bits per depth buffer value
259 * stencilFits - requested minimum bits per stencil buffer value
260 * accumFits - requested minimum bits per accum buffer component
261 * indexFits - number of bits per pixel if rgbFlag==GL_FALSE
262 * red/green/blue/alphaFits - number of bits per color component
263 * in frame buffer for RGB(A) mode.
264 * Return: pointer to new GLvisual or NULL if requested parameters can't
267 GLvisual
*gl_create_visual( GLboolean rgbFlag
,
270 GLboolean stereoFlag
,
282 if (depthBits
> (GLint
) (8*sizeof(GLdepth
))) {
283 /* can't meet depth buffer requirements */
286 if (stencilBits
> (GLint
) (8*sizeof(GLstencil
))) {
287 /* can't meet stencil buffer requirements */
290 if (accumBits
> (GLint
) (8*sizeof(GLaccum
))) {
291 /* can't meet accum buffer requirements */
295 vis
= (GLvisual
*) CALLOC( sizeof(GLvisual
) );
300 vis
->RGBAflag
= rgbFlag
;
301 vis
->DBflag
= dbFlag
;
302 vis
->StereoFlag
= stereoFlag
;
303 vis
->RedBits
= redBits
;
304 vis
->GreenBits
= greenBits
;
305 vis
->BlueBits
= blueBits
;
306 vis
->AlphaBits
= alphaFlag
? 8*sizeof(GLubyte
) : alphaBits
;
308 vis
->IndexBits
= indexBits
;
309 vis
->DepthBits
= (depthBits
>0) ? 8*sizeof(GLdepth
) : 0;
310 vis
->AccumBits
= (accumBits
>0) ? 8*sizeof(GLaccum
) : 0;
311 vis
->StencilBits
= (stencilBits
>0) ? 8*sizeof(GLstencil
) : 0;
313 vis
->SoftwareAlpha
= alphaFlag
;
320 void gl_destroy_visual( GLvisual
*vis
)
327 /**********************************************************************/
328 /***** GL Framebuffer allocation/destruction *****/
329 /**********************************************************************/
333 * Create a new framebuffer. A GLframebuffer is a struct which
334 * encapsulates the depth, stencil and accum buffers and related
336 * Input: visual - a GLvisual pointer
337 * softwareDepth - create/use a software depth buffer?
338 * softwareStencil - create/use a software stencil buffer?
339 * softwareAccum - create/use a software accum buffer?
340 * softwareAlpha - create/use a software alpha buffer?
342 * Return: pointer to new GLframebuffer struct or NULL if error.
344 GLframebuffer
*gl_create_framebuffer( GLvisual
*visual
,
345 GLboolean softwareDepth
,
346 GLboolean softwareStencil
,
347 GLboolean softwareAccum
,
348 GLboolean softwareAlpha
)
350 GLframebuffer
*buffer
;
352 buffer
= CALLOC_STRUCT(gl_frame_buffer
);
358 if (softwareDepth
) {
359 assert(visual
->DepthBits
> 0);
361 if (softwareStencil
) {
362 assert(visual
->StencilBits
> 0);
365 assert(visual
->RGBAflag
);
366 assert(visual
->AccumBits
> 0);
369 assert(visual
->RGBAflag
);
370 assert(visual
->AlphaBits
> 0);
373 buffer
->Visual
= visual
;
374 buffer
->UseSoftwareDepthBuffer
= softwareDepth
;
375 buffer
->UseSoftwareStencilBuffer
= softwareStencil
;
376 buffer
->UseSoftwareAccumBuffer
= softwareAccum
;
377 buffer
->UseSoftwareAlphaBuffers
= softwareAlpha
;
385 * Free a framebuffer struct and its buffers.
387 void gl_destroy_framebuffer( GLframebuffer
*buffer
)
391 FREE( buffer
->Depth
);
394 FREE( buffer
->Accum
);
396 if (buffer
->Stencil
) {
397 FREE( buffer
->Stencil
);
399 if (buffer
->FrontLeftAlpha
) {
400 FREE( buffer
->FrontLeftAlpha
);
402 if (buffer
->BackLeftAlpha
) {
403 FREE( buffer
->BackLeftAlpha
);
405 if (buffer
->FrontRightAlpha
) {
406 FREE( buffer
->FrontRightAlpha
);
408 if (buffer
->BackRightAlpha
) {
409 FREE( buffer
->BackRightAlpha
);
417 /**********************************************************************/
418 /***** Context allocation, initialization, destroying *****/
419 /**********************************************************************/
422 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
426 * This function just calls all the various one-time-init functions in Mesa.
428 static void one_time_init( void )
430 static GLboolean alreadyCalled
= GL_FALSE
;
431 _glthread_LOCK_MUTEX(OneTimeLock
);
432 if (!alreadyCalled
) {
433 /* do some implementation tests */
434 assert( sizeof(GLbyte
) == 1 );
435 assert( sizeof(GLshort
) >= 2 );
436 assert( sizeof(GLint
) >= 4 );
437 assert( sizeof(GLubyte
) == 1 );
438 assert( sizeof(GLushort
) >= 2 );
439 assert( sizeof(GLuint
) >= 4 );
448 gl_init_transformation();
454 if (getenv("MESA_DEBUG")) {
455 _glapi_noop_enable_warnings(GL_TRUE
);
458 _glapi_noop_enable_warnings(GL_FALSE
);
461 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
462 fprintf(stderr
, "Mesa DEBUG build %s %s\n", __DATE__
, __TIME__
);
465 alreadyCalled
= GL_TRUE
;
467 _glthread_UNLOCK_MUTEX(OneTimeLock
);
473 * Allocate and initialize a shared context state structure.
475 static struct gl_shared_state
*alloc_shared_state( void )
478 struct gl_shared_state
*ss
;
479 GLboolean outOfMemory
;
481 ss
= CALLOC_STRUCT(gl_shared_state
);
485 ss
->DisplayList
= _mesa_NewHashTable();
487 ss
->TexObjects
= _mesa_NewHashTable();
489 /* Default Texture objects */
490 outOfMemory
= GL_FALSE
;
491 for (d
= 1 ; d
<= 3 ; d
++) {
492 ss
->DefaultD
[d
] = gl_alloc_texture_object(ss
, 0, d
);
493 if (!ss
->DefaultD
[d
]) {
494 outOfMemory
= GL_TRUE
;
497 ss
->DefaultD
[d
]->RefCount
++; /* don't free if not in use */
500 if (!ss
->DisplayList
|| !ss
->TexObjects
|| outOfMemory
) {
501 /* Ran out of memory at some point. Free everything and return NULL */
503 _mesa_DeleteHashTable(ss
->DisplayList
);
505 _mesa_DeleteHashTable(ss
->TexObjects
);
507 gl_free_texture_object(ss
, ss
->DefaultD
[1]);
509 gl_free_texture_object(ss
, ss
->DefaultD
[2]);
511 gl_free_texture_object(ss
, ss
->DefaultD
[3]);
522 * Deallocate a shared state context and all children structures.
524 static void free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
526 /* Free display lists */
528 GLuint list
= _mesa_HashFirstEntry(ss
->DisplayList
);
530 gl_destroy_list(ctx
, list
);
536 _mesa_DeleteHashTable(ss
->DisplayList
);
538 /* Free texture objects */
539 while (ss
->TexObjectList
)
541 if (ctx
->Driver
.DeleteTexture
)
542 (*ctx
->Driver
.DeleteTexture
)( ctx
, ss
->TexObjectList
);
543 /* this function removes from linked list too! */
544 gl_free_texture_object(ss
, ss
->TexObjectList
);
546 _mesa_DeleteHashTable(ss
->TexObjects
);
554 * Initialize the nth light. Note that the defaults for light 0 are
555 * different than the other lights.
557 static void init_light( struct gl_light
*l
, GLuint n
)
559 make_empty_list( l
);
561 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
563 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
564 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
567 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
568 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
570 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
571 ASSIGN_3V( l
->EyeDirection
, 0.0, 0.0, -1.0 );
572 l
->SpotExponent
= 0.0;
573 gl_compute_spot_exp_table( l
);
574 l
->SpotCutoff
= 180.0;
575 l
->CosCutoff
= 0.0; /* KW: -ve values not admitted */
576 l
->ConstantAttenuation
= 1.0;
577 l
->LinearAttenuation
= 0.0;
578 l
->QuadraticAttenuation
= 0.0;
579 l
->Enabled
= GL_FALSE
;
584 static void init_lightmodel( struct gl_lightmodel
*lm
)
586 ASSIGN_4V( lm
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
587 lm
->LocalViewer
= GL_FALSE
;
588 lm
->TwoSide
= GL_FALSE
;
589 lm
->ColorControl
= GL_SINGLE_COLOR
;
593 static void init_material( struct gl_material
*m
)
595 ASSIGN_4V( m
->Ambient
, 0.2, 0.2, 0.2, 1.0 );
596 ASSIGN_4V( m
->Diffuse
, 0.8, 0.8, 0.8, 1.0 );
597 ASSIGN_4V( m
->Specular
, 0.0, 0.0, 0.0, 1.0 );
598 ASSIGN_4V( m
->Emission
, 0.0, 0.0, 0.0, 1.0 );
602 m
->SpecularIndex
= 1;
607 static void init_texture_unit( GLcontext
*ctx
, GLuint unit
)
609 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
611 texUnit
->EnvMode
= GL_MODULATE
;
612 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
613 texUnit
->TexGenEnabled
= 0;
614 texUnit
->GenModeS
= GL_EYE_LINEAR
;
615 texUnit
->GenModeT
= GL_EYE_LINEAR
;
616 texUnit
->GenModeR
= GL_EYE_LINEAR
;
617 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
618 /* Yes, these plane coefficients are correct! */
619 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
620 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
621 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
622 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
623 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
624 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
625 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
626 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
628 texUnit
->CurrentD
[1] = ctx
->Shared
->DefaultD
[1];
629 texUnit
->CurrentD
[2] = ctx
->Shared
->DefaultD
[2];
630 texUnit
->CurrentD
[3] = ctx
->Shared
->DefaultD
[3];
634 static void init_fallback_arrays( GLcontext
*ctx
)
636 struct gl_client_array
*cl
;
639 cl
= &ctx
->Fallback
.Normal
;
644 cl
->Ptr
= (void *) ctx
->Current
.Normal
;
647 cl
= &ctx
->Fallback
.Color
;
649 cl
->Type
= GL_UNSIGNED_BYTE
;
652 cl
->Ptr
= (void *) ctx
->Current
.ByteColor
;
655 cl
= &ctx
->Fallback
.Index
;
657 cl
->Type
= GL_UNSIGNED_INT
;
660 cl
->Ptr
= (void *) &ctx
->Current
.Index
;
663 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++) {
664 cl
= &ctx
->Fallback
.TexCoord
[i
];
669 cl
->Ptr
= (void *) ctx
->Current
.Texcoord
[i
];
673 cl
= &ctx
->Fallback
.EdgeFlag
;
675 cl
->Type
= GL_UNSIGNED_BYTE
;
678 cl
->Ptr
= (void *) &ctx
->Current
.EdgeFlag
;
683 /* Initialize a 1-D evaluator map */
684 static void init_1d_map( struct gl_1d_map
*map
, int n
, const float *initial
)
689 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
693 map
->Points
[i
] = initial
[i
];
698 /* Initialize a 2-D evaluator map */
699 static void init_2d_map( struct gl_2d_map
*map
, int n
, const float *initial
)
707 map
->Points
= (GLfloat
*) MALLOC(n
* sizeof(GLfloat
));
711 map
->Points
[i
] = initial
[i
];
716 static void init_color_table( struct gl_color_table
*p
)
723 p
->IntFormat
= GL_RGBA
;
729 * Initialize the attribute groups in a GLcontext.
731 static void init_attrib_groups( GLcontext
*ctx
)
737 /* Constants, may be overriden by device drivers */
738 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
739 ctx
->Const
.MaxTextureSize
= 1 << (MAX_TEXTURE_LEVELS
- 1);
740 ctx
->Const
.MaxTextureUnits
= MAX_TEXTURE_UNITS
;
741 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
742 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
743 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
744 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
745 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
746 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
747 ctx
->Const
.PointSizeGranularity
= POINT_SIZE_GRANULARITY
;
748 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
749 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
750 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
751 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
752 ctx
->Const
.LineWidthGranularity
= LINE_WIDTH_GRANULARITY
;
753 ctx
->Const
.NumAuxBuffers
= NUM_AUX_BUFFERS
;
755 /* Modelview matrix */
756 gl_matrix_ctr( &ctx
->ModelView
);
757 gl_matrix_alloc_inv( &ctx
->ModelView
);
759 ctx
->ModelViewStackDepth
= 0;
760 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
761 gl_matrix_ctr( &ctx
->ModelViewStack
[i
] );
762 gl_matrix_alloc_inv( &ctx
->ModelViewStack
[i
] );
765 /* Projection matrix - need inv for user clipping in clip space*/
766 gl_matrix_ctr( &ctx
->ProjectionMatrix
);
767 gl_matrix_alloc_inv( &ctx
->ProjectionMatrix
);
769 gl_matrix_ctr( &ctx
->ModelProjectMatrix
);
770 gl_matrix_ctr( &ctx
->ModelProjectWinMatrix
);
771 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
773 ctx
->ProjectionStackDepth
= 0;
774 ctx
->NearFarStack
[0][0] = 1.0; /* These values seem weird by make */
775 ctx
->NearFarStack
[0][1] = 0.0; /* sense mathematically. */
777 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
778 gl_matrix_ctr( &ctx
->ProjectionStack
[i
] );
779 gl_matrix_alloc_inv( &ctx
->ProjectionStack
[i
] );
783 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++) {
784 gl_matrix_ctr( &ctx
->TextureMatrix
[i
] );
785 ctx
->TextureStackDepth
[i
] = 0;
786 for (j
= 0 ; j
< MAX_TEXTURE_STACK_DEPTH
; j
++) {
787 ctx
->TextureStack
[i
][j
].inv
= 0;
791 /* Accumulate buffer group */
792 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
794 /* Color buffer group */
795 ctx
->Color
.IndexMask
= 0xffffffff;
796 ctx
->Color
.ColorMask
[0] = 0xff;
797 ctx
->Color
.ColorMask
[1] = 0xff;
798 ctx
->Color
.ColorMask
[2] = 0xff;
799 ctx
->Color
.ColorMask
[3] = 0xff;
800 ctx
->Color
.SWmasking
= GL_FALSE
;
801 ctx
->Color
.ClearIndex
= 0;
802 ASSIGN_4V( ctx
->Color
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
803 ctx
->Color
.DrawBuffer
= GL_FRONT
;
804 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
805 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
806 ctx
->Color
.AlphaRef
= 0;
807 ctx
->Color
.BlendEnabled
= GL_FALSE
;
808 ctx
->Color
.BlendSrcRGB
= GL_ONE
;
809 ctx
->Color
.BlendDstRGB
= GL_ZERO
;
810 ctx
->Color
.BlendSrcA
= GL_ONE
;
811 ctx
->Color
.BlendDstA
= GL_ZERO
;
812 ctx
->Color
.BlendEquation
= GL_FUNC_ADD_EXT
;
813 ctx
->Color
.BlendFunc
= NULL
; /* this pointer set only when needed */
814 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
815 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
816 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
817 ctx
->Color
.SWLogicOpEnabled
= GL_FALSE
;
818 ctx
->Color
.LogicOp
= GL_COPY
;
819 ctx
->Color
.DitherFlag
= GL_TRUE
;
820 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
823 ASSIGN_4V( ctx
->Current
.ByteColor
, 255, 255, 255, 255);
824 ctx
->Current
.Index
= 1;
825 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
826 ASSIGN_4V( ctx
->Current
.Texcoord
[i
], 0.0, 0.0, 0.0, 1.0 );
827 ASSIGN_4V( ctx
->Current
.RasterPos
, 0.0, 0.0, 0.0, 1.0 );
828 ctx
->Current
.RasterDistance
= 0.0;
829 ASSIGN_4V( ctx
->Current
.RasterColor
, 1.0, 1.0, 1.0, 1.0 );
830 ctx
->Current
.RasterIndex
= 1;
831 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
832 ASSIGN_4V( ctx
->Current
.RasterMultiTexCoord
[i
], 0.0, 0.0, 0.0, 1.0 );
833 ctx
->Current
.RasterTexCoord
= ctx
->Current
.RasterMultiTexCoord
[0];
834 ctx
->Current
.RasterPosValid
= GL_TRUE
;
835 ctx
->Current
.EdgeFlag
= GL_TRUE
;
836 ASSIGN_3V( ctx
->Current
.Normal
, 0.0, 0.0, 1.0 );
837 ctx
->Current
.Primitive
= (GLenum
) (GL_POLYGON
+ 1);
839 ctx
->Current
.Flag
= (VERT_NORM
|VERT_INDEX
|VERT_RGBA
|VERT_EDGE
|
840 VERT_TEX0_1
|VERT_TEX1_1
|VERT_MATERIAL
);
842 init_fallback_arrays( ctx
);
844 /* Depth buffer group */
845 ctx
->Depth
.Test
= GL_FALSE
;
846 ctx
->Depth
.Clear
= 1.0;
847 ctx
->Depth
.Func
= GL_LESS
;
848 ctx
->Depth
.Mask
= GL_TRUE
;
850 /* Evaluators group */
851 ctx
->Eval
.Map1Color4
= GL_FALSE
;
852 ctx
->Eval
.Map1Index
= GL_FALSE
;
853 ctx
->Eval
.Map1Normal
= GL_FALSE
;
854 ctx
->Eval
.Map1TextureCoord1
= GL_FALSE
;
855 ctx
->Eval
.Map1TextureCoord2
= GL_FALSE
;
856 ctx
->Eval
.Map1TextureCoord3
= GL_FALSE
;
857 ctx
->Eval
.Map1TextureCoord4
= GL_FALSE
;
858 ctx
->Eval
.Map1Vertex3
= GL_FALSE
;
859 ctx
->Eval
.Map1Vertex4
= GL_FALSE
;
860 ctx
->Eval
.Map2Color4
= GL_FALSE
;
861 ctx
->Eval
.Map2Index
= GL_FALSE
;
862 ctx
->Eval
.Map2Normal
= GL_FALSE
;
863 ctx
->Eval
.Map2TextureCoord1
= GL_FALSE
;
864 ctx
->Eval
.Map2TextureCoord2
= GL_FALSE
;
865 ctx
->Eval
.Map2TextureCoord3
= GL_FALSE
;
866 ctx
->Eval
.Map2TextureCoord4
= GL_FALSE
;
867 ctx
->Eval
.Map2Vertex3
= GL_FALSE
;
868 ctx
->Eval
.Map2Vertex4
= GL_FALSE
;
869 ctx
->Eval
.AutoNormal
= GL_FALSE
;
870 ctx
->Eval
.MapGrid1un
= 1;
871 ctx
->Eval
.MapGrid1u1
= 0.0;
872 ctx
->Eval
.MapGrid1u2
= 1.0;
873 ctx
->Eval
.MapGrid2un
= 1;
874 ctx
->Eval
.MapGrid2vn
= 1;
875 ctx
->Eval
.MapGrid2u1
= 0.0;
876 ctx
->Eval
.MapGrid2u2
= 1.0;
877 ctx
->Eval
.MapGrid2v1
= 0.0;
878 ctx
->Eval
.MapGrid2v2
= 1.0;
882 static GLfloat vertex
[4] = { 0.0, 0.0, 0.0, 1.0 };
883 static GLfloat normal
[3] = { 0.0, 0.0, 1.0 };
884 static GLfloat index
[1] = { 1.0 };
885 static GLfloat color
[4] = { 1.0, 1.0, 1.0, 1.0 };
886 static GLfloat texcoord
[4] = { 0.0, 0.0, 0.0, 1.0 };
888 init_1d_map( &ctx
->EvalMap
.Map1Vertex3
, 3, vertex
);
889 init_1d_map( &ctx
->EvalMap
.Map1Vertex4
, 4, vertex
);
890 init_1d_map( &ctx
->EvalMap
.Map1Index
, 1, index
);
891 init_1d_map( &ctx
->EvalMap
.Map1Color4
, 4, color
);
892 init_1d_map( &ctx
->EvalMap
.Map1Normal
, 3, normal
);
893 init_1d_map( &ctx
->EvalMap
.Map1Texture1
, 1, texcoord
);
894 init_1d_map( &ctx
->EvalMap
.Map1Texture2
, 2, texcoord
);
895 init_1d_map( &ctx
->EvalMap
.Map1Texture3
, 3, texcoord
);
896 init_1d_map( &ctx
->EvalMap
.Map1Texture4
, 4, texcoord
);
898 init_2d_map( &ctx
->EvalMap
.Map2Vertex3
, 3, vertex
);
899 init_2d_map( &ctx
->EvalMap
.Map2Vertex4
, 4, vertex
);
900 init_2d_map( &ctx
->EvalMap
.Map2Index
, 1, index
);
901 init_2d_map( &ctx
->EvalMap
.Map2Color4
, 4, color
);
902 init_2d_map( &ctx
->EvalMap
.Map2Normal
, 3, normal
);
903 init_2d_map( &ctx
->EvalMap
.Map2Texture1
, 1, texcoord
);
904 init_2d_map( &ctx
->EvalMap
.Map2Texture2
, 2, texcoord
);
905 init_2d_map( &ctx
->EvalMap
.Map2Texture3
, 3, texcoord
);
906 init_2d_map( &ctx
->EvalMap
.Map2Texture4
, 4, texcoord
);
910 ctx
->Fog
.Enabled
= GL_FALSE
;
911 ctx
->Fog
.Mode
= GL_EXP
;
912 ASSIGN_4V( ctx
->Fog
.Color
, 0.0, 0.0, 0.0, 0.0 );
913 ctx
->Fog
.Index
= 0.0;
914 ctx
->Fog
.Density
= 1.0;
915 ctx
->Fog
.Start
= 0.0;
919 ctx
->Hint
.PerspectiveCorrection
= GL_DONT_CARE
;
920 ctx
->Hint
.PointSmooth
= GL_DONT_CARE
;
921 ctx
->Hint
.LineSmooth
= GL_DONT_CARE
;
922 ctx
->Hint
.PolygonSmooth
= GL_DONT_CARE
;
923 ctx
->Hint
.Fog
= GL_DONT_CARE
;
925 ctx
->Hint
.AllowDrawWin
= GL_TRUE
;
926 ctx
->Hint
.AllowDrawSpn
= GL_TRUE
;
927 ctx
->Hint
.AllowDrawMem
= GL_TRUE
;
928 ctx
->Hint
.StrictLighting
= GL_TRUE
;
931 gl_pipeline_init( ctx
);
935 gl_extensions_ctr( ctx
);
937 ctx
->AllowVertexCull
= CLIP_CULLED_BIT
;
940 for (i
=0;i
<MAX_LIGHTS
;i
++) {
941 init_light( &ctx
->Light
.Light
[i
], i
);
943 make_empty_list( &ctx
->Light
.EnabledList
);
945 init_lightmodel( &ctx
->Light
.Model
);
946 init_material( &ctx
->Light
.Material
[0] );
947 init_material( &ctx
->Light
.Material
[1] );
948 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
949 ctx
->Light
.Enabled
= GL_FALSE
;
950 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
951 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
952 ctx
->Light
.ColorMaterialBitmask
953 = gl_material_bitmask( ctx
,
955 GL_AMBIENT_AND_DIFFUSE
, ~0, 0 );
957 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
959 /* Lighting miscellaneous */
960 ctx
->ShineTabList
= MALLOC_STRUCT( gl_shine_tab
);
961 make_empty_list( ctx
->ShineTabList
);
962 for (i
= 0 ; i
< 10 ; i
++) {
963 struct gl_shine_tab
*s
= MALLOC_STRUCT( gl_shine_tab
);
966 insert_at_tail( ctx
->ShineTabList
, s
);
968 for (i
= 0 ; i
< 4 ; i
++) {
969 ctx
->ShineTable
[i
] = ctx
->ShineTabList
->prev
;
970 ctx
->ShineTable
[i
]->refcount
++;
975 ctx
->Line
.SmoothFlag
= GL_FALSE
;
976 ctx
->Line
.StippleFlag
= GL_FALSE
;
977 ctx
->Line
.Width
= 1.0;
978 ctx
->Line
.StipplePattern
= 0xffff;
979 ctx
->Line
.StippleFactor
= 1;
981 /* Display List group */
982 ctx
->List
.ListBase
= 0;
985 ctx
->Pixel
.RedBias
= 0.0;
986 ctx
->Pixel
.RedScale
= 1.0;
987 ctx
->Pixel
.GreenBias
= 0.0;
988 ctx
->Pixel
.GreenScale
= 1.0;
989 ctx
->Pixel
.BlueBias
= 0.0;
990 ctx
->Pixel
.BlueScale
= 1.0;
991 ctx
->Pixel
.AlphaBias
= 0.0;
992 ctx
->Pixel
.AlphaScale
= 1.0;
993 ctx
->Pixel
.ScaleOrBiasRGBA
= GL_FALSE
;
994 ctx
->Pixel
.DepthBias
= 0.0;
995 ctx
->Pixel
.DepthScale
= 1.0;
996 ctx
->Pixel
.IndexOffset
= 0;
997 ctx
->Pixel
.IndexShift
= 0;
998 ctx
->Pixel
.ZoomX
= 1.0;
999 ctx
->Pixel
.ZoomY
= 1.0;
1000 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
1001 ctx
->Pixel
.MapStencilFlag
= GL_FALSE
;
1002 ctx
->Pixel
.MapStoSsize
= 1;
1003 ctx
->Pixel
.MapItoIsize
= 1;
1004 ctx
->Pixel
.MapItoRsize
= 1;
1005 ctx
->Pixel
.MapItoGsize
= 1;
1006 ctx
->Pixel
.MapItoBsize
= 1;
1007 ctx
->Pixel
.MapItoAsize
= 1;
1008 ctx
->Pixel
.MapRtoRsize
= 1;
1009 ctx
->Pixel
.MapGtoGsize
= 1;
1010 ctx
->Pixel
.MapBtoBsize
= 1;
1011 ctx
->Pixel
.MapAtoAsize
= 1;
1012 ctx
->Pixel
.MapStoS
[0] = 0;
1013 ctx
->Pixel
.MapItoI
[0] = 0;
1014 ctx
->Pixel
.MapItoR
[0] = 0.0;
1015 ctx
->Pixel
.MapItoG
[0] = 0.0;
1016 ctx
->Pixel
.MapItoB
[0] = 0.0;
1017 ctx
->Pixel
.MapItoA
[0] = 0.0;
1018 ctx
->Pixel
.MapItoR8
[0] = 0;
1019 ctx
->Pixel
.MapItoG8
[0] = 0;
1020 ctx
->Pixel
.MapItoB8
[0] = 0;
1021 ctx
->Pixel
.MapItoA8
[0] = 0;
1022 ctx
->Pixel
.MapRtoR
[0] = 0.0;
1023 ctx
->Pixel
.MapGtoG
[0] = 0.0;
1024 ctx
->Pixel
.MapBtoB
[0] = 0.0;
1025 ctx
->Pixel
.MapAtoA
[0] = 0.0;
1028 ctx
->Point
.SmoothFlag
= GL_FALSE
;
1029 ctx
->Point
.Size
= 1.0;
1030 ctx
->Point
.Params
[0] = 1.0;
1031 ctx
->Point
.Params
[1] = 0.0;
1032 ctx
->Point
.Params
[2] = 0.0;
1033 ctx
->Point
.Attenuated
= GL_FALSE
;
1034 ctx
->Point
.MinSize
= 0.0;
1035 ctx
->Point
.MaxSize
= (GLfloat
) MAX_POINT_SIZE
;
1036 ctx
->Point
.Threshold
= 1.0;
1039 ctx
->Polygon
.CullFlag
= GL_FALSE
;
1040 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
1041 ctx
->Polygon
.FrontFace
= GL_CCW
;
1042 ctx
->Polygon
.FrontBit
= 0;
1043 ctx
->Polygon
.FrontMode
= GL_FILL
;
1044 ctx
->Polygon
.BackMode
= GL_FILL
;
1045 ctx
->Polygon
.Unfilled
= GL_FALSE
;
1046 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
1047 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
1048 ctx
->Polygon
.OffsetFactor
= 0.0F
;
1049 ctx
->Polygon
.OffsetUnits
= 0.0F
;
1050 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
1051 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
1052 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
1054 /* Polygon Stipple group */
1055 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );
1058 ctx
->Scissor
.Enabled
= GL_FALSE
;
1061 ctx
->Scissor
.Width
= 0;
1062 ctx
->Scissor
.Height
= 0;
1065 ctx
->Stencil
.Enabled
= GL_FALSE
;
1066 ctx
->Stencil
.Function
= GL_ALWAYS
;
1067 ctx
->Stencil
.FailFunc
= GL_KEEP
;
1068 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
1069 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
1070 ctx
->Stencil
.Ref
= 0;
1071 ctx
->Stencil
.ValueMask
= STENCIL_MAX
;
1072 ctx
->Stencil
.Clear
= 0;
1073 ctx
->Stencil
.WriteMask
= STENCIL_MAX
;
1076 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
1077 ctx
->Texture
.CurrentTransformUnit
= 0; /* multitexture */
1078 ctx
->Texture
.Enabled
= 0;
1079 for (i
=0; i
<MAX_TEXTURE_UNITS
; i
++)
1080 init_texture_unit( ctx
, i
);
1081 init_color_table(&ctx
->Texture
.Palette
);
1083 /* Transformation group */
1084 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
1085 ctx
->Transform
.Normalize
= GL_FALSE
;
1086 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
1087 for (i
=0;i
<MAX_CLIP_PLANES
;i
++) {
1088 ctx
->Transform
.ClipEnabled
[i
] = GL_FALSE
;
1089 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
1091 ctx
->Transform
.AnyClip
= GL_FALSE
;
1093 /* Viewport group */
1094 ctx
->Viewport
.X
= 0;
1095 ctx
->Viewport
.Y
= 0;
1096 ctx
->Viewport
.Width
= 0;
1097 ctx
->Viewport
.Height
= 0;
1098 ctx
->Viewport
.Near
= 0.0;
1099 ctx
->Viewport
.Far
= 1.0;
1100 gl_matrix_ctr(&ctx
->Viewport
.WindowMap
);
1104 ctx
->Viewport
.WindowMap
.m
[Sz
] = 0.5 * DEPTH_SCALE
;
1105 ctx
->Viewport
.WindowMap
.m
[Tz
] = 0.5 * DEPTH_SCALE
;
1109 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1110 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1113 ctx
->Array
.Vertex
.Size
= 4;
1114 ctx
->Array
.Vertex
.Type
= GL_FLOAT
;
1115 ctx
->Array
.Vertex
.Stride
= 0;
1116 ctx
->Array
.Vertex
.StrideB
= 0;
1117 ctx
->Array
.Vertex
.Ptr
= NULL
;
1118 ctx
->Array
.Vertex
.Enabled
= GL_FALSE
;
1119 ctx
->Array
.Normal
.Type
= GL_FLOAT
;
1120 ctx
->Array
.Normal
.Stride
= 0;
1121 ctx
->Array
.Normal
.StrideB
= 0;
1122 ctx
->Array
.Normal
.Ptr
= NULL
;
1123 ctx
->Array
.Normal
.Enabled
= GL_FALSE
;
1124 ctx
->Array
.Color
.Size
= 4;
1125 ctx
->Array
.Color
.Type
= GL_FLOAT
;
1126 ctx
->Array
.Color
.Stride
= 0;
1127 ctx
->Array
.Color
.StrideB
= 0;
1128 ctx
->Array
.Color
.Ptr
= NULL
;
1129 ctx
->Array
.Color
.Enabled
= GL_FALSE
;
1130 ctx
->Array
.Index
.Type
= GL_FLOAT
;
1131 ctx
->Array
.Index
.Stride
= 0;
1132 ctx
->Array
.Index
.StrideB
= 0;
1133 ctx
->Array
.Index
.Ptr
= NULL
;
1134 ctx
->Array
.Index
.Enabled
= GL_FALSE
;
1135 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1136 ctx
->Array
.TexCoord
[i
].Size
= 4;
1137 ctx
->Array
.TexCoord
[i
].Type
= GL_FLOAT
;
1138 ctx
->Array
.TexCoord
[i
].Stride
= 0;
1139 ctx
->Array
.TexCoord
[i
].StrideB
= 0;
1140 ctx
->Array
.TexCoord
[i
].Ptr
= NULL
;
1141 ctx
->Array
.TexCoord
[i
].Enabled
= GL_FALSE
;
1143 ctx
->Array
.TexCoordInterleaveFactor
= 1;
1144 ctx
->Array
.EdgeFlag
.Stride
= 0;
1145 ctx
->Array
.EdgeFlag
.StrideB
= 0;
1146 ctx
->Array
.EdgeFlag
.Ptr
= NULL
;
1147 ctx
->Array
.EdgeFlag
.Enabled
= GL_FALSE
;
1148 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
1150 /* Pixel transfer */
1151 ctx
->Pack
.Alignment
= 4;
1152 ctx
->Pack
.RowLength
= 0;
1153 ctx
->Pack
.ImageHeight
= 0;
1154 ctx
->Pack
.SkipPixels
= 0;
1155 ctx
->Pack
.SkipRows
= 0;
1156 ctx
->Pack
.SkipImages
= 0;
1157 ctx
->Pack
.SwapBytes
= GL_FALSE
;
1158 ctx
->Pack
.LsbFirst
= GL_FALSE
;
1159 ctx
->Unpack
.Alignment
= 4;
1160 ctx
->Unpack
.RowLength
= 0;
1161 ctx
->Unpack
.ImageHeight
= 0;
1162 ctx
->Unpack
.SkipPixels
= 0;
1163 ctx
->Unpack
.SkipRows
= 0;
1164 ctx
->Unpack
.SkipImages
= 0;
1165 ctx
->Unpack
.SwapBytes
= GL_FALSE
;
1166 ctx
->Unpack
.LsbFirst
= GL_FALSE
;
1169 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
1170 ctx
->Feedback
.Buffer
= NULL
;
1171 ctx
->Feedback
.BufferSize
= 0;
1172 ctx
->Feedback
.Count
= 0;
1174 /* Selection/picking */
1175 ctx
->Select
.Buffer
= NULL
;
1176 ctx
->Select
.BufferSize
= 0;
1177 ctx
->Select
.BufferCount
= 0;
1178 ctx
->Select
.Hits
= 0;
1179 ctx
->Select
.NameStackDepth
= 0;
1181 /* Optimized Accum buffer */
1182 ctx
->IntegerAccumMode
= GL_TRUE
;
1183 ctx
->IntegerAccumScaler
= 0.0;
1185 /* Renderer and client attribute stacks */
1186 ctx
->AttribStackDepth
= 0;
1187 ctx
->ClientAttribStackDepth
= 0;
1190 ctx
->NewState
= NEW_ALL
;
1191 ctx
->RenderMode
= GL_RENDER
;
1192 ctx
->StippleCounter
= 0;
1193 ctx
->NeedNormals
= GL_FALSE
;
1194 ctx
->DoViewportMapping
= GL_TRUE
;
1196 ctx
->NeedEyeCoords
= GL_FALSE
;
1197 ctx
->NeedEyeNormals
= GL_FALSE
;
1198 ctx
->vb_proj_matrix
= &ctx
->ModelProjectMatrix
;
1202 ctx
->ExecuteFlag
= GL_TRUE
;
1203 ctx
->CompileFlag
= GL_FALSE
;
1204 ctx
->CurrentListPtr
= NULL
;
1205 ctx
->CurrentBlock
= NULL
;
1206 ctx
->CurrentListNum
= 0;
1207 ctx
->CurrentPos
= 0;
1209 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1211 ctx
->CatchSignals
= GL_TRUE
;
1213 /* For debug/development only */
1214 ctx
->NoRaster
= getenv("MESA_NO_RASTER") ? GL_TRUE
: GL_FALSE
;
1215 ctx
->FirstTimeCurrent
= GL_TRUE
;
1217 /* Dither disable */
1218 ctx
->NoDither
= getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
1219 if (ctx
->NoDither
) {
1220 if (getenv("MESA_DEBUG")) {
1221 fprintf(stderr
, "MESA_NO_DITHER set - dithering disabled\n");
1223 ctx
->Color
.DitherFlag
= GL_FALSE
;
1231 * Allocate the proxy textures. If we run out of memory part way through
1232 * the allocations clean up and return GL_FALSE.
1233 * Return: GL_TRUE=success, GL_FALSE=failure
1235 static GLboolean
alloc_proxy_textures( GLcontext
*ctx
)
1237 GLboolean out_of_memory
;
1240 ctx
->Texture
.Proxy1D
= gl_alloc_texture_object(NULL
, 0, 1);
1241 if (!ctx
->Texture
.Proxy1D
) {
1245 ctx
->Texture
.Proxy2D
= gl_alloc_texture_object(NULL
, 0, 2);
1246 if (!ctx
->Texture
.Proxy2D
) {
1247 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1251 ctx
->Texture
.Proxy3D
= gl_alloc_texture_object(NULL
, 0, 3);
1252 if (!ctx
->Texture
.Proxy3D
) {
1253 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1254 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1258 out_of_memory
= GL_FALSE
;
1259 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1260 ctx
->Texture
.Proxy1D
->Image
[i
] = gl_alloc_texture_image();
1261 ctx
->Texture
.Proxy2D
->Image
[i
] = gl_alloc_texture_image();
1262 ctx
->Texture
.Proxy3D
->Image
[i
] = gl_alloc_texture_image();
1263 if (!ctx
->Texture
.Proxy1D
->Image
[i
]
1264 || !ctx
->Texture
.Proxy2D
->Image
[i
]
1265 || !ctx
->Texture
.Proxy3D
->Image
[i
]) {
1266 out_of_memory
= GL_TRUE
;
1269 if (out_of_memory
) {
1270 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
1271 if (ctx
->Texture
.Proxy1D
->Image
[i
]) {
1272 gl_free_texture_image(ctx
->Texture
.Proxy1D
->Image
[i
]);
1274 if (ctx
->Texture
.Proxy2D
->Image
[i
]) {
1275 gl_free_texture_image(ctx
->Texture
.Proxy2D
->Image
[i
]);
1277 if (ctx
->Texture
.Proxy3D
->Image
[i
]) {
1278 gl_free_texture_image(ctx
->Texture
.Proxy3D
->Image
[i
]);
1281 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy1D
);
1282 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy2D
);
1283 gl_free_texture_object(NULL
, ctx
->Texture
.Proxy3D
);
1294 * Initialize a GLcontext struct.
1296 GLboolean
gl_initialize_context_data( GLcontext
*ctx
,
1298 GLcontext
*share_list
,
1302 (void) direct
; /* not used */
1304 /* misc one-time initializations */
1307 ctx
->DriverCtx
= driver_ctx
;
1308 ctx
->Visual
= visual
;
1309 ctx
->DrawBuffer
= NULL
;
1310 ctx
->ReadBuffer
= NULL
;
1312 ctx
->VB
= gl_vb_create_for_immediate( ctx
);
1317 ctx
->input
= ctx
->VB
->IM
;
1319 ctx
->PB
= gl_alloc_pb();
1327 /* share the group of display lists of another context */
1328 ctx
->Shared
= share_list
->Shared
;
1331 /* allocate new group of display lists */
1332 ctx
->Shared
= alloc_shared_state();
1340 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1341 ctx
->Shared
->RefCount
++;
1342 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1344 init_attrib_groups( ctx
);
1346 gl_reset_vb( ctx
->VB
);
1347 gl_reset_input( ctx
);
1349 if (visual
->DBflag
) {
1350 ctx
->Color
.DrawBuffer
= GL_BACK
;
1351 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
1352 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
1353 ctx
->Pixel
.ReadBuffer
= GL_BACK
;
1354 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
1357 ctx
->Color
.DrawBuffer
= GL_FRONT
;
1358 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
1359 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
1360 ctx
->Pixel
.ReadBuffer
= GL_FRONT
;
1361 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
1365 init_timings( ctx
);
1368 if (!alloc_proxy_textures(ctx
)) {
1369 free_shared_state(ctx
, ctx
->Shared
);
1376 /* setup API dispatch tables */
1377 _mesa_init_exec_table( &ctx
->Exec
);
1378 _mesa_init_dlist_table( &ctx
->Save
);
1379 ctx
->CurrentDispatch
= &ctx
->Exec
;
1387 * Allocate and initialize a GLcontext structure.
1388 * Input: visual - a GLvisual pointer
1389 * sharelist - another context to share display lists with or NULL
1390 * driver_ctx - pointer to device driver's context state struct
1391 * Return: pointer to a new gl_context struct or NULL if error.
1393 GLcontext
*gl_create_context( GLvisual
*visual
,
1394 GLcontext
*share_list
,
1398 GLcontext
*ctx
= (GLcontext
*) CALLOC( sizeof(GLcontext
) );
1403 if (gl_initialize_context_data(ctx
, visual
, share_list
,
1404 driver_ctx
, direct
)) {
1416 * Free the data associated with the given context.
1417 * But don't free() the GLcontext struct itself!
1419 void gl_free_context_data( GLcontext
*ctx
)
1422 struct gl_shine_tab
*s
, *tmps
;
1424 /* if we're destroying the current context, unbind it first */
1425 if (ctx
== gl_get_current_context()) {
1426 gl_make_current(NULL
, NULL
);
1430 if (getenv("MESA_PROFILE")) {
1431 print_timings( ctx
);
1435 gl_matrix_dtr( &ctx
->ModelView
);
1436 for (i
= 0 ; i
< MAX_MODELVIEW_STACK_DEPTH
; i
++) {
1437 gl_matrix_dtr( &ctx
->ModelViewStack
[i
] );
1439 gl_matrix_dtr( &ctx
->ProjectionMatrix
);
1440 for (i
= 0 ; i
< MAX_PROJECTION_STACK_DEPTH
; i
++) {
1441 gl_matrix_dtr( &ctx
->ProjectionStack
[i
] );
1446 if(ctx
->input
!= ctx
->VB
->IM
)
1447 gl_immediate_free( ctx
->input
);
1449 gl_vb_free( ctx
->VB
);
1451 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1452 ctx
->Shared
->RefCount
--;
1453 assert(ctx
->Shared
->RefCount
>= 0);
1454 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1455 if (ctx
->Shared
->RefCount
== 0) {
1456 /* free shared state */
1457 free_shared_state( ctx
, ctx
->Shared
);
1460 foreach_s( s
, tmps
, ctx
->ShineTabList
) {
1463 FREE( ctx
->ShineTabList
);
1465 /* Free proxy texture objects */
1466 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy1D
);
1467 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy2D
);
1468 gl_free_texture_object( NULL
, ctx
->Texture
.Proxy3D
);
1470 /* Free evaluator data */
1471 if (ctx
->EvalMap
.Map1Vertex3
.Points
)
1472 FREE( ctx
->EvalMap
.Map1Vertex3
.Points
);
1473 if (ctx
->EvalMap
.Map1Vertex4
.Points
)
1474 FREE( ctx
->EvalMap
.Map1Vertex4
.Points
);
1475 if (ctx
->EvalMap
.Map1Index
.Points
)
1476 FREE( ctx
->EvalMap
.Map1Index
.Points
);
1477 if (ctx
->EvalMap
.Map1Color4
.Points
)
1478 FREE( ctx
->EvalMap
.Map1Color4
.Points
);
1479 if (ctx
->EvalMap
.Map1Normal
.Points
)
1480 FREE( ctx
->EvalMap
.Map1Normal
.Points
);
1481 if (ctx
->EvalMap
.Map1Texture1
.Points
)
1482 FREE( ctx
->EvalMap
.Map1Texture1
.Points
);
1483 if (ctx
->EvalMap
.Map1Texture2
.Points
)
1484 FREE( ctx
->EvalMap
.Map1Texture2
.Points
);
1485 if (ctx
->EvalMap
.Map1Texture3
.Points
)
1486 FREE( ctx
->EvalMap
.Map1Texture3
.Points
);
1487 if (ctx
->EvalMap
.Map1Texture4
.Points
)
1488 FREE( ctx
->EvalMap
.Map1Texture4
.Points
);
1490 if (ctx
->EvalMap
.Map2Vertex3
.Points
)
1491 FREE( ctx
->EvalMap
.Map2Vertex3
.Points
);
1492 if (ctx
->EvalMap
.Map2Vertex4
.Points
)
1493 FREE( ctx
->EvalMap
.Map2Vertex4
.Points
);
1494 if (ctx
->EvalMap
.Map2Index
.Points
)
1495 FREE( ctx
->EvalMap
.Map2Index
.Points
);
1496 if (ctx
->EvalMap
.Map2Color4
.Points
)
1497 FREE( ctx
->EvalMap
.Map2Color4
.Points
);
1498 if (ctx
->EvalMap
.Map2Normal
.Points
)
1499 FREE( ctx
->EvalMap
.Map2Normal
.Points
);
1500 if (ctx
->EvalMap
.Map2Texture1
.Points
)
1501 FREE( ctx
->EvalMap
.Map2Texture1
.Points
);
1502 if (ctx
->EvalMap
.Map2Texture2
.Points
)
1503 FREE( ctx
->EvalMap
.Map2Texture2
.Points
);
1504 if (ctx
->EvalMap
.Map2Texture3
.Points
)
1505 FREE( ctx
->EvalMap
.Map2Texture3
.Points
);
1506 if (ctx
->EvalMap
.Map2Texture4
.Points
)
1507 FREE( ctx
->EvalMap
.Map2Texture4
.Points
);
1509 /* Free cache of immediate buffers. */
1510 while (ctx
->nr_im_queued
-- > 0) {
1511 struct immediate
* next
= ctx
->freed_im_queue
->next
;
1512 FREE( ctx
->freed_im_queue
);
1513 ctx
->freed_im_queue
= next
;
1515 gl_extensions_dtr(ctx
);
1521 * Destroy a GLcontext structure.
1523 void gl_destroy_context( GLcontext
*ctx
)
1526 gl_free_context_data(ctx
);
1527 FREE( (void *) ctx
);
1534 * Called by the driver after both the context and driver are fully
1535 * initialized. Currently just reads the config file.
1537 void gl_context_initialize( GLcontext
*ctx
)
1539 gl_read_config_file( ctx
);
1545 * Copy attribute groups from one context to another.
1546 * Input: src - source context
1547 * dst - destination context
1548 * mask - bitwise OR of GL_*_BIT flags
1550 void gl_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1552 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1553 MEMCPY( &dst
->Accum
, &src
->Accum
, sizeof(struct gl_accum_attrib
) );
1555 if (mask
& GL_COLOR_BUFFER_BIT
) {
1556 MEMCPY( &dst
->Color
, &src
->Color
, sizeof(struct gl_colorbuffer_attrib
) );
1558 if (mask
& GL_CURRENT_BIT
) {
1559 MEMCPY( &dst
->Current
, &src
->Current
, sizeof(struct gl_current_attrib
) );
1561 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1562 MEMCPY( &dst
->Depth
, &src
->Depth
, sizeof(struct gl_depthbuffer_attrib
) );
1564 if (mask
& GL_ENABLE_BIT
) {
1567 if (mask
& GL_EVAL_BIT
) {
1568 MEMCPY( &dst
->Eval
, &src
->Eval
, sizeof(struct gl_eval_attrib
) );
1570 if (mask
& GL_FOG_BIT
) {
1571 MEMCPY( &dst
->Fog
, &src
->Fog
, sizeof(struct gl_fog_attrib
) );
1573 if (mask
& GL_HINT_BIT
) {
1574 MEMCPY( &dst
->Hint
, &src
->Hint
, sizeof(struct gl_hint_attrib
) );
1576 if (mask
& GL_LIGHTING_BIT
) {
1577 MEMCPY( &dst
->Light
, &src
->Light
, sizeof(struct gl_light_attrib
) );
1578 /* gl_reinit_light_attrib( &dst->Light ); */
1580 if (mask
& GL_LINE_BIT
) {
1581 MEMCPY( &dst
->Line
, &src
->Line
, sizeof(struct gl_line_attrib
) );
1583 if (mask
& GL_LIST_BIT
) {
1584 MEMCPY( &dst
->List
, &src
->List
, sizeof(struct gl_list_attrib
) );
1586 if (mask
& GL_PIXEL_MODE_BIT
) {
1587 MEMCPY( &dst
->Pixel
, &src
->Pixel
, sizeof(struct gl_pixel_attrib
) );
1589 if (mask
& GL_POINT_BIT
) {
1590 MEMCPY( &dst
->Point
, &src
->Point
, sizeof(struct gl_point_attrib
) );
1592 if (mask
& GL_POLYGON_BIT
) {
1593 MEMCPY( &dst
->Polygon
, &src
->Polygon
, sizeof(struct gl_polygon_attrib
) );
1595 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1596 /* Use loop instead of MEMCPY due to problem with Portland Group's
1597 * C compiler. Reported by John Stone.
1600 for (i
=0;i
<32;i
++) {
1601 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1604 if (mask
& GL_SCISSOR_BIT
) {
1605 MEMCPY( &dst
->Scissor
, &src
->Scissor
, sizeof(struct gl_scissor_attrib
) );
1607 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1608 MEMCPY( &dst
->Stencil
, &src
->Stencil
, sizeof(struct gl_stencil_attrib
) );
1610 if (mask
& GL_TEXTURE_BIT
) {
1611 MEMCPY( &dst
->Texture
, &src
->Texture
, sizeof(struct gl_texture_attrib
) );
1613 if (mask
& GL_TRANSFORM_BIT
) {
1614 MEMCPY( &dst
->Transform
, &src
->Transform
, sizeof(struct gl_transform_attrib
) );
1616 if (mask
& GL_VIEWPORT_BIT
) {
1617 MEMCPY( &dst
->Viewport
, &src
->Viewport
, sizeof(struct gl_viewport_attrib
) );
1623 * Set the current context, binding the given frame buffer to the context.
1625 void gl_make_current( GLcontext
*newCtx
, GLframebuffer
*buffer
)
1627 gl_make_current2( newCtx
, buffer
, buffer
);
1632 * Bind the given context to the given draw-buffer and read-buffer
1633 * and make it the current context for this thread.
1635 void gl_make_current2( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1636 GLframebuffer
*readBuffer
)
1639 GLcontext
*oldCtx
= gl_get_context();
1641 /* Flush the old context
1644 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx
, "gl_make_current");
1646 /* unbind frame buffers from context */
1647 if (oldCtx
->DrawBuffer
) {
1648 oldCtx
->DrawBuffer
= NULL
;
1650 if (oldCtx
->ReadBuffer
) {
1651 oldCtx
->ReadBuffer
= NULL
;
1656 /* We call this function periodically (just here for now) in
1657 * order to detect when multithreading has begun.
1659 _glapi_check_multithread();
1661 _glapi_set_context((void *) newCtx
);
1662 ASSERT(gl_get_current_context() == newCtx
);
1664 SET_IMMEDIATE(newCtx
, newCtx
->input
);
1665 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1668 _glapi_set_dispatch(NULL
); /* none current */
1671 if (MESA_VERBOSE
) fprintf(stderr
, "gl_make_current()\n");
1673 if (newCtx
&& drawBuffer
&& readBuffer
) {
1674 /* TODO: check if newCtx and buffer's visual match??? */
1675 newCtx
->DrawBuffer
= drawBuffer
;
1676 newCtx
->ReadBuffer
= readBuffer
;
1677 newCtx
->NewState
= NEW_ALL
; /* just to be safe */
1678 gl_update_state( newCtx
);
1681 /* We can use this to help debug user's problems. Tell the to set
1682 * the MESA_INFO env variable before running their app. Then the
1683 * first time each context is made current we'll print some useful
1686 if (newCtx
&& newCtx
->FirstTimeCurrent
) {
1687 if (getenv("MESA_INFO")) {
1688 fprintf(stderr
, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION
));
1689 fprintf(stderr
, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER
));
1690 fprintf(stderr
, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR
));
1691 fprintf(stderr
, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS
));
1692 #if defined(THREADS)
1693 fprintf(stderr
, "Mesa thread-safe: YES\n");
1695 fprintf(stderr
, "Mesa thread-safe: NO\n");
1697 #if defined(USE_X86_ASM)
1698 fprintf(stderr
, "Mesa x86-optimized: YES\n");
1700 fprintf(stderr
, "Mesa x86-optimized: NO\n");
1703 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1710 * Return current context handle for the calling thread.
1711 * This isn't the fastest way to get the current context.
1712 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1714 GLcontext
*gl_get_current_context( void )
1716 return (GLcontext
*) _glapi_get_context();
1722 * This should be called by device drivers just before they do a
1723 * swapbuffers. Any pending rendering commands will be executed.
1726 _mesa_swapbuffers(GLcontext
*ctx
)
1728 FLUSH_VB( ctx
, "swap buffers" );
1734 * Return pointer to this context's current API dispatch table.
1735 * It'll either be the immediate-mode execute dispatcher or the
1736 * display list compile dispatcher.
1738 struct _glapi_table
*
1739 _mesa_get_dispatch(GLcontext
*ctx
)
1741 return ctx
->CurrentDispatch
;
1746 /**********************************************************************/
1747 /***** Miscellaneous functions *****/
1748 /**********************************************************************/
1752 * This function is called when the Mesa user has stumbled into a code
1753 * path which may not be implemented fully or correctly.
1755 void gl_problem( const GLcontext
*ctx
, const char *s
)
1757 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
1758 fprintf( stderr
, "Report to mesa-bugs@mesa3d.org\n" );
1765 * This is called to inform the user that he or she has tried to do
1766 * something illogical or if there's likely a bug in their program
1767 * (like enabled depth testing without a depth buffer).
1769 void gl_warning( const GLcontext
*ctx
, const char *s
)
1775 if (getenv("MESA_DEBUG")) {
1783 fprintf( stderr
, "Mesa warning: %s\n", s
);
1791 * Compile an error into current display list.
1793 void gl_compile_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1795 if (ctx
->CompileFlag
)
1796 gl_save_error( ctx
, error
, s
);
1798 if (ctx
->ExecuteFlag
)
1799 gl_error( ctx
, error
, s
);
1805 * This is Mesa's error handler. Normally, all that's done is the updating
1806 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1807 * environment variable "MESA_DEBUG" is defined then a real error message
1808 * is printed to stderr.
1809 * Input: error - the error value
1810 * s - a diagnostic string
1812 void gl_error( GLcontext
*ctx
, GLenum error
, const char *s
)
1819 if (getenv("MESA_DEBUG")) {
1832 strcpy( errstr
, "GL_NO_ERROR" );
1834 case GL_INVALID_VALUE
:
1835 strcpy( errstr
, "GL_INVALID_VALUE" );
1837 case GL_INVALID_ENUM
:
1838 strcpy( errstr
, "GL_INVALID_ENUM" );
1840 case GL_INVALID_OPERATION
:
1841 strcpy( errstr
, "GL_INVALID_OPERATION" );
1843 case GL_STACK_OVERFLOW
:
1844 strcpy( errstr
, "GL_STACK_OVERFLOW" );
1846 case GL_STACK_UNDERFLOW
:
1847 strcpy( errstr
, "GL_STACK_UNDERFLOW" );
1849 case GL_OUT_OF_MEMORY
:
1850 strcpy( errstr
, "GL_OUT_OF_MEMORY" );
1853 strcpy( errstr
, "unknown" );
1856 fprintf( stderr
, "Mesa user error: %s in %s\n", errstr
, s
);
1859 if (ctx
->ErrorValue
==GL_NO_ERROR
) {
1860 ctx
->ErrorValue
= error
;
1863 /* Call device driver's error handler, if any. This is used on the Mac. */
1864 if (ctx
->Driver
.Error
) {
1865 (*ctx
->Driver
.Error
)( ctx
);
1872 _mesa_Finish( void )
1874 GET_CURRENT_CONTEXT(ctx
);
1875 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glFinish");
1876 if (ctx
->Driver
.Finish
) {
1877 (*ctx
->Driver
.Finish
)( ctx
);
1886 GET_CURRENT_CONTEXT(ctx
);
1887 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glFlush");
1888 if (ctx
->Driver
.Flush
) {
1889 (*ctx
->Driver
.Flush
)( ctx
);