blob: 68bfaf420410e0635ecc2414e308536e44aec891 [file] [log] [blame]
Brian Paulfa9df402000-02-02 19:16:46 +00001/* $Id: context.c,v 1.38 2000/02/02 19:16:46 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paulfbd8f211999-11-11 01:22:25 +00005 * Version: 3.3
jtgafb833d1999-08-19 00:55:39 +00006 *
Brian Paul54287052000-01-17 20:00:15 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +00008 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
jtgafb833d1999-08-19 00:55:39 +000028#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
jtgafb833d1999-08-19 00:55:39 +000032#include "accum.h"
33#include "alphabuf.h"
jtgafb833d1999-08-19 00:55:39 +000034#include "clip.h"
35#include "context.h"
36#include "cva.h"
37#include "depth.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000038#include "dispatch.h"
jtgafb833d1999-08-19 00:55:39 +000039#include "dlist.h"
40#include "eval.h"
41#include "enums.h"
Brian Paul585a68c1999-09-11 11:31:34 +000042#include "extensions.h"
jtgafb833d1999-08-19 00:55:39 +000043#include "fog.h"
Brian Paulb7a43041999-11-30 20:34:51 +000044#include "get.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000045#include "glapi.h"
Brian Paulf9b97d92000-01-28 20:17:42 +000046#include "glapinoop.h"
Brian Paul9560f052000-01-31 23:11:39 +000047#include "glthread.h"
jtgafb833d1999-08-19 00:55:39 +000048#include "hash.h"
49#include "light.h"
jtgafb833d1999-08-19 00:55:39 +000050#include "macros.h"
51#include "matrix.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000052#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000053#include "mmath.h"
54#include "pb.h"
55#include "pipeline.h"
jtgafb833d1999-08-19 00:55:39 +000056#include "shade.h"
57#include "simple_list.h"
58#include "stencil.h"
59#include "stages.h"
Brian Paulfa9df402000-02-02 19:16:46 +000060#include "state.h"
jtgafb833d1999-08-19 00:55:39 +000061#include "translate.h"
62#include "teximage.h"
63#include "texobj.h"
64#include "texstate.h"
65#include "texture.h"
66#include "types.h"
67#include "varray.h"
68#include "vb.h"
69#include "vbcull.h"
jtgafb833d1999-08-19 00:55:39 +000070#include "vbrender.h"
71#include "vbxform.h"
Keith Whitwell38756791999-08-29 10:26:31 +000072#include "vertices.h"
jtgafb833d1999-08-19 00:55:39 +000073#include "xform.h"
jtgafb833d1999-08-19 00:55:39 +000074#endif
75
76
77
78/**********************************************************************/
79/***** Context and Thread management *****/
80/**********************************************************************/
81
82
Brian Paul00037781999-12-17 14:52:35 +000083#if !defined(THREADS)
jtgafb833d1999-08-19 00:55:39 +000084
Brian Paul5666c632000-01-18 17:36:16 +000085struct immediate *_mesa_CurrentInput = NULL;
jtgafb833d1999-08-19 00:55:39 +000086
Brian Paul00037781999-12-17 14:52:35 +000087#endif
jtgafb833d1999-08-19 00:55:39 +000088
89
90
91
92/**********************************************************************/
93/***** Profiling functions *****/
94/**********************************************************************/
95
96#ifdef PROFILE
97
98#include <sys/times.h>
99#include <sys/param.h>
100
101
102/*
103 * Return system time in seconds.
104 * NOTE: this implementation may not be very portable!
105 */
106GLdouble gl_time( void )
107{
108 static GLdouble prev_time = 0.0;
109 static GLdouble time;
110 struct tms tm;
111 clock_t clk;
112
113 clk = times(&tm);
114
115#ifdef CLK_TCK
116 time = (double)clk / (double)CLK_TCK;
117#else
118 time = (double)clk / (double)HZ;
119#endif
120
121 if (time>prev_time) {
122 prev_time = time;
123 return time;
124 }
125 else {
126 return prev_time;
127 }
128}
129
130/*
131 * Reset the timing/profiling counters
132 */
133static void init_timings( GLcontext *ctx )
134{
135 ctx->BeginEndCount = 0;
136 ctx->BeginEndTime = 0.0;
137 ctx->VertexCount = 0;
138 ctx->VertexTime = 0.0;
139 ctx->PointCount = 0;
140 ctx->PointTime = 0.0;
141 ctx->LineCount = 0;
142 ctx->LineTime = 0.0;
143 ctx->PolygonCount = 0;
144 ctx->PolygonTime = 0.0;
145 ctx->ClearCount = 0;
146 ctx->ClearTime = 0.0;
147 ctx->SwapCount = 0;
148 ctx->SwapTime = 0.0;
149}
150
151
152/*
153 * Print the accumulated timing/profiling data.
154 */
155static void print_timings( GLcontext *ctx )
156{
157 GLdouble beginendrate;
158 GLdouble vertexrate;
159 GLdouble pointrate;
160 GLdouble linerate;
161 GLdouble polygonrate;
162 GLdouble overhead;
163 GLdouble clearrate;
164 GLdouble swaprate;
165 GLdouble avgvertices;
166
167 if (ctx->BeginEndTime>0.0) {
168 beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
169 }
170 else {
171 beginendrate = 0.0;
172 }
173 if (ctx->VertexTime>0.0) {
174 vertexrate = ctx->VertexCount / ctx->VertexTime;
175 }
176 else {
177 vertexrate = 0.0;
178 }
179 if (ctx->PointTime>0.0) {
180 pointrate = ctx->PointCount / ctx->PointTime;
181 }
182 else {
183 pointrate = 0.0;
184 }
185 if (ctx->LineTime>0.0) {
186 linerate = ctx->LineCount / ctx->LineTime;
187 }
188 else {
189 linerate = 0.0;
190 }
191 if (ctx->PolygonTime>0.0) {
192 polygonrate = ctx->PolygonCount / ctx->PolygonTime;
193 }
194 else {
195 polygonrate = 0.0;
196 }
197 if (ctx->ClearTime>0.0) {
198 clearrate = ctx->ClearCount / ctx->ClearTime;
199 }
200 else {
201 clearrate = 0.0;
202 }
203 if (ctx->SwapTime>0.0) {
204 swaprate = ctx->SwapCount / ctx->SwapTime;
205 }
206 else {
207 swaprate = 0.0;
208 }
209
210 if (ctx->BeginEndCount>0) {
211 avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
212 }
213 else {
214 avgvertices = 0.0;
215 }
216
217 overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
218 - ctx->LineTime - ctx->PolygonTime;
219
220
221 printf(" Count Time (s) Rate (/s) \n");
222 printf("--------------------------------------------------------\n");
223 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
224 ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
225 printf(" vertexes transformed %7d %8.3f %10.3f\n",
226 ctx->VertexCount, ctx->VertexTime, vertexrate );
227 printf(" points rasterized %7d %8.3f %10.3f\n",
228 ctx->PointCount, ctx->PointTime, pointrate );
229 printf(" lines rasterized %7d %8.3f %10.3f\n",
230 ctx->LineCount, ctx->LineTime, linerate );
231 printf(" polygons rasterized %7d %8.3f %10.3f\n",
232 ctx->PolygonCount, ctx->PolygonTime, polygonrate );
233 printf(" overhead %8.3f\n", overhead );
234 printf("glClear %7d %8.3f %10.3f\n",
235 ctx->ClearCount, ctx->ClearTime, clearrate );
236 printf("SwapBuffers %7d %8.3f %10.3f\n",
237 ctx->SwapCount, ctx->SwapTime, swaprate );
238 printf("\n");
239
240 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
241}
242#endif
243
244
245
246
247
248/**********************************************************************/
Brian Paul4d053dd2000-01-14 04:45:47 +0000249/***** GL Visual allocation/destruction *****/
250/**********************************************************************/
251
252
253/*
254 * Allocate a new GLvisual object.
255 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
256 * alphaFlag - alloc software alpha buffers?
257 * dbFlag - double buffering?
258 * stereoFlag - stereo buffer?
259 * depthFits - requested minimum bits per depth buffer value
260 * stencilFits - requested minimum bits per stencil buffer value
261 * accumFits - requested minimum bits per accum buffer component
262 * indexFits - number of bits per pixel if rgbFlag==GL_FALSE
263 * red/green/blue/alphaFits - number of bits per color component
264 * in frame buffer for RGB(A) mode.
265 * Return: pointer to new GLvisual or NULL if requested parameters can't
266 * be met.
267 */
268GLvisual *gl_create_visual( GLboolean rgbFlag,
269 GLboolean alphaFlag,
270 GLboolean dbFlag,
271 GLboolean stereoFlag,
272 GLint depthBits,
273 GLint stencilBits,
274 GLint accumBits,
275 GLint indexBits,
276 GLint redBits,
277 GLint greenBits,
278 GLint blueBits,
279 GLint alphaBits )
280{
281 GLvisual *vis;
282
283 if (depthBits > (GLint) (8*sizeof(GLdepth))) {
284 /* can't meet depth buffer requirements */
285 return NULL;
286 }
287 if (stencilBits > (GLint) (8*sizeof(GLstencil))) {
288 /* can't meet stencil buffer requirements */
289 return NULL;
290 }
291 if (accumBits > (GLint) (8*sizeof(GLaccum))) {
292 /* can't meet accum buffer requirements */
293 return NULL;
294 }
295
296 vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
297 if (!vis) {
298 return NULL;
299 }
300
301 vis->RGBAflag = rgbFlag;
302 vis->DBflag = dbFlag;
303 vis->StereoFlag = stereoFlag;
304 vis->RedBits = redBits;
305 vis->GreenBits = greenBits;
306 vis->BlueBits = blueBits;
307 vis->AlphaBits = alphaFlag ? 8*sizeof(GLubyte) : alphaBits;
308
309 vis->IndexBits = indexBits;
310 vis->DepthBits = (depthBits>0) ? 8*sizeof(GLdepth) : 0;
311 vis->AccumBits = (accumBits>0) ? 8*sizeof(GLaccum) : 0;
312 vis->StencilBits = (stencilBits>0) ? 8*sizeof(GLstencil) : 0;
313
314 vis->SoftwareAlpha = alphaFlag;
315
316 return vis;
317}
318
319
320
321void gl_destroy_visual( GLvisual *vis )
322{
323 FREE( vis );
324}
325
326
327
328/**********************************************************************/
329/***** GL Framebuffer allocation/destruction *****/
330/**********************************************************************/
331
332
333/*
334 * Create a new framebuffer. A GLframebuffer is a struct which
335 * encapsulates the depth, stencil and accum buffers and related
336 * parameters.
337 * Input: visual - a GLvisual pointer
338 * softwareDepth - create/use a software depth buffer?
339 * softwareStencil - create/use a software stencil buffer?
340 * softwareAccum - create/use a software accum buffer?
341 * softwareAlpha - create/use a software alpha buffer?
342
343 * Return: pointer to new GLframebuffer struct or NULL if error.
344 */
345GLframebuffer *gl_create_framebuffer( GLvisual *visual,
346 GLboolean softwareDepth,
347 GLboolean softwareStencil,
348 GLboolean softwareAccum,
349 GLboolean softwareAlpha )
350{
351 GLframebuffer *buffer;
352
353 buffer = CALLOC_STRUCT(gl_frame_buffer);
354 if (!buffer) {
355 return NULL;
356 }
357
358 /* sanity checks */
359 if (softwareDepth ) {
360 assert(visual->DepthBits > 0);
361 }
362 if (softwareStencil) {
363 assert(visual->StencilBits > 0);
364 }
365 if (softwareAccum) {
366 assert(visual->RGBAflag);
367 assert(visual->AccumBits > 0);
368 }
369 if (softwareAlpha) {
370 assert(visual->RGBAflag);
371 assert(visual->AlphaBits > 0);
372 }
373
374 buffer->Visual = visual;
375 buffer->UseSoftwareDepthBuffer = softwareDepth;
376 buffer->UseSoftwareStencilBuffer = softwareStencil;
377 buffer->UseSoftwareAccumBuffer = softwareAccum;
378 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
379
380 return buffer;
381}
382
383
384
385/*
386 * Free a framebuffer struct and its buffers.
387 */
388void gl_destroy_framebuffer( GLframebuffer *buffer )
389{
390 if (buffer) {
391 if (buffer->Depth) {
392 FREE( buffer->Depth );
393 }
394 if (buffer->Accum) {
395 FREE( buffer->Accum );
396 }
397 if (buffer->Stencil) {
398 FREE( buffer->Stencil );
399 }
400 if (buffer->FrontLeftAlpha) {
401 FREE( buffer->FrontLeftAlpha );
402 }
403 if (buffer->BackLeftAlpha) {
404 FREE( buffer->BackLeftAlpha );
405 }
406 if (buffer->FrontRightAlpha) {
407 FREE( buffer->FrontRightAlpha );
408 }
409 if (buffer->BackRightAlpha) {
410 FREE( buffer->BackRightAlpha );
411 }
412 FREE(buffer);
413 }
414}
415
416
417
418/**********************************************************************/
jtgafb833d1999-08-19 00:55:39 +0000419/***** Context allocation, initialization, destroying *****/
420/**********************************************************************/
421
422
Brian Paul9560f052000-01-31 23:11:39 +0000423_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
424
425
jtgafb833d1999-08-19 00:55:39 +0000426/*
427 * This function just calls all the various one-time-init functions in Mesa.
428 */
429static void one_time_init( void )
430{
431 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000432 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000433 if (!alreadyCalled) {
Brian Paul4d053dd2000-01-14 04:45:47 +0000434 /* do some implementation tests */
435 assert( sizeof(GLbyte) == 1 );
436 assert( sizeof(GLshort) >= 2 );
437 assert( sizeof(GLint) >= 4 );
438 assert( sizeof(GLubyte) == 1 );
439 assert( sizeof(GLushort) >= 2 );
440 assert( sizeof(GLuint) >= 4 );
441
jtgafb833d1999-08-19 00:55:39 +0000442 gl_init_clip();
443 gl_init_eval();
444 gl_init_fog();
445 gl_init_math();
446 gl_init_lists();
447 gl_init_shade();
448 gl_init_texture();
449 gl_init_transformation();
450 gl_init_translate();
451 gl_init_vbrender();
452 gl_init_vbxform();
Keith Whitwell38756791999-08-29 10:26:31 +0000453 gl_init_vertices();
Brian Paul68ee4bc2000-01-28 19:02:22 +0000454
455 if (getenv("MESA_DEBUG")) {
456 _glapi_noop_enable_warnings(GL_TRUE);
457 }
458 else {
459 _glapi_noop_enable_warnings(GL_FALSE);
460 }
461
jtgafb833d1999-08-19 00:55:39 +0000462#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
463 fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
464#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000465
466 alreadyCalled = GL_TRUE;
467 }
Brian Paul9560f052000-01-31 23:11:39 +0000468 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000469}
470
471
Brian Paul4d053dd2000-01-14 04:45:47 +0000472
jtgafb833d1999-08-19 00:55:39 +0000473/*
474 * Allocate and initialize a shared context state structure.
475 */
476static struct gl_shared_state *alloc_shared_state( void )
477{
Brian Paul6e6d4c61999-10-09 20:17:07 +0000478 GLuint d;
jtgafb833d1999-08-19 00:55:39 +0000479 struct gl_shared_state *ss;
480 GLboolean outOfMemory;
481
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000482 ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000483 if (!ss)
484 return NULL;
485
Brian Paulbb797902000-01-24 16:19:54 +0000486 ss->DisplayList = _mesa_NewHashTable();
jtgafb833d1999-08-19 00:55:39 +0000487
Brian Paulbb797902000-01-24 16:19:54 +0000488 ss->TexObjects = _mesa_NewHashTable();
jtgafb833d1999-08-19 00:55:39 +0000489
490 /* Default Texture objects */
491 outOfMemory = GL_FALSE;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000492 for (d = 1 ; d <= 3 ; d++) {
493 ss->DefaultD[d] = gl_alloc_texture_object(ss, 0, d);
494 if (!ss->DefaultD[d]) {
495 outOfMemory = GL_TRUE;
496 break;
jtgafb833d1999-08-19 00:55:39 +0000497 }
Brian Paul6e6d4c61999-10-09 20:17:07 +0000498 ss->DefaultD[d]->RefCount++; /* don't free if not in use */
jtgafb833d1999-08-19 00:55:39 +0000499 }
500
501 if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
502 /* Ran out of memory at some point. Free everything and return NULL */
503 if (ss->DisplayList)
Brian Paulbb797902000-01-24 16:19:54 +0000504 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000505 if (ss->TexObjects)
Brian Paulbb797902000-01-24 16:19:54 +0000506 _mesa_DeleteHashTable(ss->TexObjects);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000507 if (ss->DefaultD[1])
508 gl_free_texture_object(ss, ss->DefaultD[1]);
509 if (ss->DefaultD[2])
510 gl_free_texture_object(ss, ss->DefaultD[2]);
511 if (ss->DefaultD[3])
512 gl_free_texture_object(ss, ss->DefaultD[3]);
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000513 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000514 return NULL;
515 }
516 else {
517 return ss;
518 }
519}
520
521
522/*
523 * Deallocate a shared state context and all children structures.
524 */
525static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
526{
527 /* Free display lists */
528 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000529 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000530 if (list) {
531 gl_destroy_list(ctx, list);
532 }
533 else {
534 break;
535 }
536 }
Brian Paulbb797902000-01-24 16:19:54 +0000537 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000538
539 /* Free texture objects */
540 while (ss->TexObjectList)
541 {
542 if (ctx->Driver.DeleteTexture)
543 (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
544 /* this function removes from linked list too! */
545 gl_free_texture_object(ss, ss->TexObjectList);
546 }
Brian Paulbb797902000-01-24 16:19:54 +0000547 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000548
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000549 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000550}
551
552
553
jtgafb833d1999-08-19 00:55:39 +0000554/*
555 * Initialize the nth light. Note that the defaults for light 0 are
556 * different than the other lights.
557 */
558static void init_light( struct gl_light *l, GLuint n )
559{
560 make_empty_list( l );
561
562 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
563 if (n==0) {
564 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
565 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
566 }
567 else {
568 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
569 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
570 }
571 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
572 ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
573 l->SpotExponent = 0.0;
574 gl_compute_spot_exp_table( l );
575 l->SpotCutoff = 180.0;
576 l->CosCutoff = 0.0; /* KW: -ve values not admitted */
577 l->ConstantAttenuation = 1.0;
578 l->LinearAttenuation = 0.0;
579 l->QuadraticAttenuation = 0.0;
580 l->Enabled = GL_FALSE;
581}
582
583
584
585static void init_lightmodel( struct gl_lightmodel *lm )
586{
587 ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
588 lm->LocalViewer = GL_FALSE;
589 lm->TwoSide = GL_FALSE;
590 lm->ColorControl = GL_SINGLE_COLOR;
591}
592
593
594static void init_material( struct gl_material *m )
595{
596 ASSIGN_4V( m->Ambient, 0.2, 0.2, 0.2, 1.0 );
597 ASSIGN_4V( m->Diffuse, 0.8, 0.8, 0.8, 1.0 );
598 ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
599 ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
600 m->Shininess = 0.0;
601 m->AmbientIndex = 0;
602 m->DiffuseIndex = 1;
603 m->SpecularIndex = 1;
604}
605
606
607
608static void init_texture_unit( GLcontext *ctx, GLuint unit )
609{
610 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
611
612 texUnit->EnvMode = GL_MODULATE;
613 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
614 texUnit->TexGenEnabled = 0;
615 texUnit->GenModeS = GL_EYE_LINEAR;
616 texUnit->GenModeT = GL_EYE_LINEAR;
617 texUnit->GenModeR = GL_EYE_LINEAR;
618 texUnit->GenModeQ = GL_EYE_LINEAR;
619 /* Yes, these plane coefficients are correct! */
620 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
621 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
622 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
623 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
624 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
625 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
626 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
627 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
628
Brian Paul6e6d4c61999-10-09 20:17:07 +0000629 texUnit->CurrentD[1] = ctx->Shared->DefaultD[1];
630 texUnit->CurrentD[2] = ctx->Shared->DefaultD[2];
631 texUnit->CurrentD[3] = ctx->Shared->DefaultD[3];
jtgafb833d1999-08-19 00:55:39 +0000632}
633
634
635static void init_fallback_arrays( GLcontext *ctx )
636{
637 struct gl_client_array *cl;
638 GLuint i;
639
640 cl = &ctx->Fallback.Normal;
641 cl->Size = 3;
642 cl->Type = GL_FLOAT;
643 cl->Stride = 0;
644 cl->StrideB = 0;
645 cl->Ptr = (void *) ctx->Current.Normal;
646 cl->Enabled = 1;
647
648 cl = &ctx->Fallback.Color;
649 cl->Size = 4;
650 cl->Type = GL_UNSIGNED_BYTE;
651 cl->Stride = 0;
652 cl->StrideB = 0;
653 cl->Ptr = (void *) ctx->Current.ByteColor;
654 cl->Enabled = 1;
655
656 cl = &ctx->Fallback.Index;
657 cl->Size = 1;
658 cl->Type = GL_UNSIGNED_INT;
659 cl->Stride = 0;
660 cl->StrideB = 0;
661 cl->Ptr = (void *) &ctx->Current.Index;
662 cl->Enabled = 1;
663
664 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
665 cl = &ctx->Fallback.TexCoord[i];
666 cl->Size = 4;
667 cl->Type = GL_FLOAT;
668 cl->Stride = 0;
669 cl->StrideB = 0;
670 cl->Ptr = (void *) ctx->Current.Texcoord[i];
671 cl->Enabled = 1;
672 }
673
674 cl = &ctx->Fallback.EdgeFlag;
675 cl->Size = 1;
676 cl->Type = GL_UNSIGNED_BYTE;
677 cl->Stride = 0;
678 cl->StrideB = 0;
679 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
680 cl->Enabled = 1;
681}
682
Brian Paul4d053dd2000-01-14 04:45:47 +0000683
jtgafb833d1999-08-19 00:55:39 +0000684/* Initialize a 1-D evaluator map */
685static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
686{
687 map->Order = 1;
688 map->u1 = 0.0;
689 map->u2 = 1.0;
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000690 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
jtgafb833d1999-08-19 00:55:39 +0000691 if (map->Points) {
692 GLint i;
693 for (i=0;i<n;i++)
694 map->Points[i] = initial[i];
695 }
jtgafb833d1999-08-19 00:55:39 +0000696}
697
698
699/* Initialize a 2-D evaluator map */
700static void init_2d_map( struct gl_2d_map *map, int n, const float *initial )
701{
702 map->Uorder = 1;
703 map->Vorder = 1;
704 map->u1 = 0.0;
705 map->u2 = 1.0;
706 map->v1 = 0.0;
707 map->v2 = 1.0;
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000708 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
jtgafb833d1999-08-19 00:55:39 +0000709 if (map->Points) {
710 GLint i;
711 for (i=0;i<n;i++)
712 map->Points[i] = initial[i];
713 }
jtgafb833d1999-08-19 00:55:39 +0000714}
715
716
Brian Paulf0dee651999-11-19 22:51:29 +0000717static void init_color_table( struct gl_color_table *p )
Brian Paulfbd8f211999-11-11 01:22:25 +0000718{
719 p->Table[0] = 255;
720 p->Table[1] = 255;
721 p->Table[2] = 255;
722 p->Table[3] = 255;
723 p->Size = 1;
724 p->IntFormat = GL_RGBA;
725 p->Format = GL_RGBA;
726}
727
jtgafb833d1999-08-19 00:55:39 +0000728
729/*
Brian Paul4d053dd2000-01-14 04:45:47 +0000730 * Initialize the attribute groups in a GLcontext.
jtgafb833d1999-08-19 00:55:39 +0000731 */
Brian Paul4d053dd2000-01-14 04:45:47 +0000732static void init_attrib_groups( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000733{
734 GLuint i, j;
735
Brian Paul4d053dd2000-01-14 04:45:47 +0000736 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +0000737
Brian Paul4d053dd2000-01-14 04:45:47 +0000738 /* Constants, may be overriden by device driver */
739 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
740 ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
741 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
742 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
jtgafb833d1999-08-19 00:55:39 +0000743
Brian Paul4d053dd2000-01-14 04:45:47 +0000744 /* Modelview matrix */
745 gl_matrix_ctr( &ctx->ModelView );
746 gl_matrix_alloc_inv( &ctx->ModelView );
747
748 ctx->ModelViewStackDepth = 0;
749 for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
750 gl_matrix_ctr( &ctx->ModelViewStack[i] );
751 gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
752 }
753
754 /* Projection matrix - need inv for user clipping in clip space*/
755 gl_matrix_ctr( &ctx->ProjectionMatrix );
756 gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
757
758 gl_matrix_ctr( &ctx->ModelProjectMatrix );
759 gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
760 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
761
762 ctx->ProjectionStackDepth = 0;
763 ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
764 ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
765
766 for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
767 gl_matrix_ctr( &ctx->ProjectionStack[i] );
768 gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
769 }
770
771 /* Texture matrix */
772 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
773 gl_matrix_ctr( &ctx->TextureMatrix[i] );
774 ctx->TextureStackDepth[i] = 0;
775 for (j = 0 ; j < MAX_TEXTURE_STACK_DEPTH ; j++) {
776 ctx->TextureStack[i][j].inv = 0;
jtgafb833d1999-08-19 00:55:39 +0000777 }
Brian Paul4d053dd2000-01-14 04:45:47 +0000778 }
jtgafb833d1999-08-19 00:55:39 +0000779
Brian Paul4d053dd2000-01-14 04:45:47 +0000780 /* Accumulate buffer group */
781 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
jtgafb833d1999-08-19 00:55:39 +0000782
Brian Paul4d053dd2000-01-14 04:45:47 +0000783 /* Color buffer group */
784 ctx->Color.IndexMask = 0xffffffff;
785 ctx->Color.ColorMask[0] = 0xff;
786 ctx->Color.ColorMask[1] = 0xff;
787 ctx->Color.ColorMask[2] = 0xff;
788 ctx->Color.ColorMask[3] = 0xff;
789 ctx->Color.SWmasking = GL_FALSE;
790 ctx->Color.ClearIndex = 0;
791 ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
792 ctx->Color.DrawBuffer = GL_FRONT;
793 ctx->Color.AlphaEnabled = GL_FALSE;
794 ctx->Color.AlphaFunc = GL_ALWAYS;
795 ctx->Color.AlphaRef = 0;
796 ctx->Color.BlendEnabled = GL_FALSE;
797 ctx->Color.BlendSrcRGB = GL_ONE;
798 ctx->Color.BlendDstRGB = GL_ZERO;
799 ctx->Color.BlendSrcA = GL_ONE;
800 ctx->Color.BlendDstA = GL_ZERO;
801 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
802 ctx->Color.BlendFunc = NULL; /* this pointer set only when needed */
803 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
804 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
805 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
806 ctx->Color.SWLogicOpEnabled = GL_FALSE;
807 ctx->Color.LogicOp = GL_COPY;
808 ctx->Color.DitherFlag = GL_TRUE;
809 ctx->Color.MultiDrawBuffer = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000810
Brian Paul4d053dd2000-01-14 04:45:47 +0000811 /* Current group */
812 ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
813 ctx->Current.Index = 1;
814 for (i=0; i<MAX_TEXTURE_UNITS; i++)
815 ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
816 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
817 ctx->Current.RasterDistance = 0.0;
818 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
819 ctx->Current.RasterIndex = 1;
820 for (i=0; i<MAX_TEXTURE_UNITS; i++)
821 ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
822 ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
823 ctx->Current.RasterPosValid = GL_TRUE;
824 ctx->Current.EdgeFlag = GL_TRUE;
825 ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
826 ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
jtgafb833d1999-08-19 00:55:39 +0000827
Brian Paul4d053dd2000-01-14 04:45:47 +0000828 ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
829 VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
jtgafb833d1999-08-19 00:55:39 +0000830
Brian Paul4d053dd2000-01-14 04:45:47 +0000831 init_fallback_arrays( ctx );
jtgafb833d1999-08-19 00:55:39 +0000832
Brian Paul4d053dd2000-01-14 04:45:47 +0000833 /* Depth buffer group */
834 ctx->Depth.Test = GL_FALSE;
835 ctx->Depth.Clear = 1.0;
836 ctx->Depth.Func = GL_LESS;
837 ctx->Depth.Mask = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000838
Brian Paul4d053dd2000-01-14 04:45:47 +0000839 /* Evaluators group */
840 ctx->Eval.Map1Color4 = GL_FALSE;
841 ctx->Eval.Map1Index = GL_FALSE;
842 ctx->Eval.Map1Normal = GL_FALSE;
843 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
844 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
845 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
846 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
847 ctx->Eval.Map1Vertex3 = GL_FALSE;
848 ctx->Eval.Map1Vertex4 = GL_FALSE;
849 ctx->Eval.Map2Color4 = GL_FALSE;
850 ctx->Eval.Map2Index = GL_FALSE;
851 ctx->Eval.Map2Normal = GL_FALSE;
852 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
853 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
854 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
855 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
856 ctx->Eval.Map2Vertex3 = GL_FALSE;
857 ctx->Eval.Map2Vertex4 = GL_FALSE;
858 ctx->Eval.AutoNormal = GL_FALSE;
859 ctx->Eval.MapGrid1un = 1;
860 ctx->Eval.MapGrid1u1 = 0.0;
861 ctx->Eval.MapGrid1u2 = 1.0;
862 ctx->Eval.MapGrid2un = 1;
863 ctx->Eval.MapGrid2vn = 1;
864 ctx->Eval.MapGrid2u1 = 0.0;
865 ctx->Eval.MapGrid2u2 = 1.0;
866 ctx->Eval.MapGrid2v1 = 0.0;
867 ctx->Eval.MapGrid2v2 = 1.0;
jtgafb833d1999-08-19 00:55:39 +0000868
Brian Paul4d053dd2000-01-14 04:45:47 +0000869 /* Evaluator data */
870 {
871 static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
872 static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
873 static GLfloat index[1] = { 1.0 };
874 static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
875 static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
jtgafb833d1999-08-19 00:55:39 +0000876
Brian Paul4d053dd2000-01-14 04:45:47 +0000877 init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
878 init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
879 init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
880 init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
881 init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
882 init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
883 init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
884 init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
885 init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
jtgafb833d1999-08-19 00:55:39 +0000886
Brian Paul4d053dd2000-01-14 04:45:47 +0000887 init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
888 init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
889 init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
890 init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
891 init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
892 init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
893 init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
894 init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
895 init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
896 }
jtgafb833d1999-08-19 00:55:39 +0000897
Brian Paul4d053dd2000-01-14 04:45:47 +0000898 /* Fog group */
899 ctx->Fog.Enabled = GL_FALSE;
900 ctx->Fog.Mode = GL_EXP;
901 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
902 ctx->Fog.Index = 0.0;
903 ctx->Fog.Density = 1.0;
904 ctx->Fog.Start = 0.0;
905 ctx->Fog.End = 1.0;
jtgafb833d1999-08-19 00:55:39 +0000906
Brian Paul4d053dd2000-01-14 04:45:47 +0000907 /* Hint group */
908 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
909 ctx->Hint.PointSmooth = GL_DONT_CARE;
910 ctx->Hint.LineSmooth = GL_DONT_CARE;
911 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
912 ctx->Hint.Fog = GL_DONT_CARE;
jtgafb833d1999-08-19 00:55:39 +0000913
Brian Paul4d053dd2000-01-14 04:45:47 +0000914 ctx->Hint.AllowDrawWin = GL_TRUE;
915 ctx->Hint.AllowDrawSpn = GL_TRUE;
916 ctx->Hint.AllowDrawMem = GL_TRUE;
917 ctx->Hint.StrictLighting = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000918
Brian Paul4d053dd2000-01-14 04:45:47 +0000919 /* Pipeline */
920 gl_pipeline_init( ctx );
921 gl_cva_init( ctx );
jtgafb833d1999-08-19 00:55:39 +0000922
Brian Paul4d053dd2000-01-14 04:45:47 +0000923 /* Extensions */
924 gl_extensions_ctr( ctx );
jtgafb833d1999-08-19 00:55:39 +0000925
Brian Paul4d053dd2000-01-14 04:45:47 +0000926 ctx->AllowVertexCull = CLIP_CULLED_BIT;
jtgafb833d1999-08-19 00:55:39 +0000927
Brian Paul4d053dd2000-01-14 04:45:47 +0000928 /* Lighting group */
929 for (i=0;i<MAX_LIGHTS;i++) {
930 init_light( &ctx->Light.Light[i], i );
931 }
932 make_empty_list( &ctx->Light.EnabledList );
jtgafb833d1999-08-19 00:55:39 +0000933
Brian Paul4d053dd2000-01-14 04:45:47 +0000934 init_lightmodel( &ctx->Light.Model );
935 init_material( &ctx->Light.Material[0] );
936 init_material( &ctx->Light.Material[1] );
937 ctx->Light.ShadeModel = GL_SMOOTH;
938 ctx->Light.Enabled = GL_FALSE;
939 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
940 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
941 ctx->Light.ColorMaterialBitmask
942 = gl_material_bitmask( ctx,
943 GL_FRONT_AND_BACK,
944 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000945
Brian Paul4d053dd2000-01-14 04:45:47 +0000946 ctx->Light.ColorMaterialEnabled = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000947
Brian Paul4d053dd2000-01-14 04:45:47 +0000948 /* Lighting miscellaneous */
949 ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
950 make_empty_list( ctx->ShineTabList );
951 for (i = 0 ; i < 10 ; i++) {
952 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
953 s->shininess = -1;
954 s->refcount = 0;
955 insert_at_tail( ctx->ShineTabList, s );
956 }
957 for (i = 0 ; i < 4 ; i++) {
958 ctx->ShineTable[i] = ctx->ShineTabList->prev;
959 ctx->ShineTable[i]->refcount++;
960 }
jtgafb833d1999-08-19 00:55:39 +0000961
jtgafb833d1999-08-19 00:55:39 +0000962
Brian Paul4d053dd2000-01-14 04:45:47 +0000963 /* Line group */
964 ctx->Line.SmoothFlag = GL_FALSE;
965 ctx->Line.StippleFlag = GL_FALSE;
966 ctx->Line.Width = 1.0;
967 ctx->Line.StipplePattern = 0xffff;
968 ctx->Line.StippleFactor = 1;
jtgafb833d1999-08-19 00:55:39 +0000969
Brian Paul4d053dd2000-01-14 04:45:47 +0000970 /* Display List group */
971 ctx->List.ListBase = 0;
jtgafb833d1999-08-19 00:55:39 +0000972
Brian Paul4d053dd2000-01-14 04:45:47 +0000973 /* Pixel group */
974 ctx->Pixel.RedBias = 0.0;
975 ctx->Pixel.RedScale = 1.0;
976 ctx->Pixel.GreenBias = 0.0;
977 ctx->Pixel.GreenScale = 1.0;
978 ctx->Pixel.BlueBias = 0.0;
979 ctx->Pixel.BlueScale = 1.0;
980 ctx->Pixel.AlphaBias = 0.0;
981 ctx->Pixel.AlphaScale = 1.0;
982 ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
983 ctx->Pixel.DepthBias = 0.0;
984 ctx->Pixel.DepthScale = 1.0;
985 ctx->Pixel.IndexOffset = 0;
986 ctx->Pixel.IndexShift = 0;
987 ctx->Pixel.ZoomX = 1.0;
988 ctx->Pixel.ZoomY = 1.0;
989 ctx->Pixel.MapColorFlag = GL_FALSE;
990 ctx->Pixel.MapStencilFlag = GL_FALSE;
991 ctx->Pixel.MapStoSsize = 1;
992 ctx->Pixel.MapItoIsize = 1;
993 ctx->Pixel.MapItoRsize = 1;
994 ctx->Pixel.MapItoGsize = 1;
995 ctx->Pixel.MapItoBsize = 1;
996 ctx->Pixel.MapItoAsize = 1;
997 ctx->Pixel.MapRtoRsize = 1;
998 ctx->Pixel.MapGtoGsize = 1;
999 ctx->Pixel.MapBtoBsize = 1;
1000 ctx->Pixel.MapAtoAsize = 1;
1001 ctx->Pixel.MapStoS[0] = 0;
1002 ctx->Pixel.MapItoI[0] = 0;
1003 ctx->Pixel.MapItoR[0] = 0.0;
1004 ctx->Pixel.MapItoG[0] = 0.0;
1005 ctx->Pixel.MapItoB[0] = 0.0;
1006 ctx->Pixel.MapItoA[0] = 0.0;
1007 ctx->Pixel.MapItoR8[0] = 0;
1008 ctx->Pixel.MapItoG8[0] = 0;
1009 ctx->Pixel.MapItoB8[0] = 0;
1010 ctx->Pixel.MapItoA8[0] = 0;
1011 ctx->Pixel.MapRtoR[0] = 0.0;
1012 ctx->Pixel.MapGtoG[0] = 0.0;
1013 ctx->Pixel.MapBtoB[0] = 0.0;
1014 ctx->Pixel.MapAtoA[0] = 0.0;
jtgafb833d1999-08-19 00:55:39 +00001015
Brian Paul4d053dd2000-01-14 04:45:47 +00001016 /* Point group */
1017 ctx->Point.SmoothFlag = GL_FALSE;
1018 ctx->Point.Size = 1.0;
1019 ctx->Point.Params[0] = 1.0;
1020 ctx->Point.Params[1] = 0.0;
1021 ctx->Point.Params[2] = 0.0;
1022 ctx->Point.Attenuated = GL_FALSE;
1023 ctx->Point.MinSize = 0.0;
1024 ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
1025 ctx->Point.Threshold = 1.0;
jtgafb833d1999-08-19 00:55:39 +00001026
Brian Paul4d053dd2000-01-14 04:45:47 +00001027 /* Polygon group */
1028 ctx->Polygon.CullFlag = GL_FALSE;
1029 ctx->Polygon.CullFaceMode = GL_BACK;
1030 ctx->Polygon.FrontFace = GL_CCW;
1031 ctx->Polygon.FrontBit = 0;
1032 ctx->Polygon.FrontMode = GL_FILL;
1033 ctx->Polygon.BackMode = GL_FILL;
1034 ctx->Polygon.Unfilled = GL_FALSE;
1035 ctx->Polygon.SmoothFlag = GL_FALSE;
1036 ctx->Polygon.StippleFlag = GL_FALSE;
1037 ctx->Polygon.OffsetFactor = 0.0F;
1038 ctx->Polygon.OffsetUnits = 0.0F;
1039 ctx->Polygon.OffsetPoint = GL_FALSE;
1040 ctx->Polygon.OffsetLine = GL_FALSE;
1041 ctx->Polygon.OffsetFill = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001042
Brian Paul4d053dd2000-01-14 04:45:47 +00001043 /* Polygon Stipple group */
1044 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
jtgafb833d1999-08-19 00:55:39 +00001045
Brian Paul4d053dd2000-01-14 04:45:47 +00001046 /* Scissor group */
1047 ctx->Scissor.Enabled = GL_FALSE;
1048 ctx->Scissor.X = 0;
1049 ctx->Scissor.Y = 0;
1050 ctx->Scissor.Width = 0;
1051 ctx->Scissor.Height = 0;
jtgafb833d1999-08-19 00:55:39 +00001052
Brian Paul4d053dd2000-01-14 04:45:47 +00001053 /* Stencil group */
1054 ctx->Stencil.Enabled = GL_FALSE;
1055 ctx->Stencil.Function = GL_ALWAYS;
1056 ctx->Stencil.FailFunc = GL_KEEP;
1057 ctx->Stencil.ZPassFunc = GL_KEEP;
1058 ctx->Stencil.ZFailFunc = GL_KEEP;
1059 ctx->Stencil.Ref = 0;
1060 ctx->Stencil.ValueMask = STENCIL_MAX;
1061 ctx->Stencil.Clear = 0;
1062 ctx->Stencil.WriteMask = STENCIL_MAX;
jtgafb833d1999-08-19 00:55:39 +00001063
Brian Paul4d053dd2000-01-14 04:45:47 +00001064 /* Texture group */
1065 ctx->Texture.CurrentUnit = 0; /* multitexture */
1066 ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
1067 ctx->Texture.Enabled = 0;
1068 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1069 init_texture_unit( ctx, i );
1070 init_color_table(&ctx->Texture.Palette);
jtgafb833d1999-08-19 00:55:39 +00001071
Brian Paul4d053dd2000-01-14 04:45:47 +00001072 /* Transformation group */
1073 ctx->Transform.MatrixMode = GL_MODELVIEW;
1074 ctx->Transform.Normalize = GL_FALSE;
1075 ctx->Transform.RescaleNormals = GL_FALSE;
1076 for (i=0;i<MAX_CLIP_PLANES;i++) {
1077 ctx->Transform.ClipEnabled[i] = GL_FALSE;
1078 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1079 }
1080 ctx->Transform.AnyClip = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001081
Brian Paul4d053dd2000-01-14 04:45:47 +00001082 /* Viewport group */
1083 ctx->Viewport.X = 0;
1084 ctx->Viewport.Y = 0;
1085 ctx->Viewport.Width = 0;
1086 ctx->Viewport.Height = 0;
1087 ctx->Viewport.Near = 0.0;
1088 ctx->Viewport.Far = 1.0;
1089 gl_matrix_ctr(&ctx->Viewport.WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001090
1091#define Sz 10
1092#define Tz 14
Brian Paul4d053dd2000-01-14 04:45:47 +00001093 ctx->Viewport.WindowMap.m[Sz] = 0.5 * DEPTH_SCALE;
1094 ctx->Viewport.WindowMap.m[Tz] = 0.5 * DEPTH_SCALE;
jtgafb833d1999-08-19 00:55:39 +00001095#undef Sz
1096#undef Tz
1097
Brian Paul4d053dd2000-01-14 04:45:47 +00001098 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1099 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
jtgafb833d1999-08-19 00:55:39 +00001100
Brian Paul4d053dd2000-01-14 04:45:47 +00001101 /* Vertex arrays */
1102 ctx->Array.Vertex.Size = 4;
1103 ctx->Array.Vertex.Type = GL_FLOAT;
1104 ctx->Array.Vertex.Stride = 0;
1105 ctx->Array.Vertex.StrideB = 0;
1106 ctx->Array.Vertex.Ptr = NULL;
1107 ctx->Array.Vertex.Enabled = GL_FALSE;
1108 ctx->Array.Normal.Type = GL_FLOAT;
1109 ctx->Array.Normal.Stride = 0;
1110 ctx->Array.Normal.StrideB = 0;
1111 ctx->Array.Normal.Ptr = NULL;
1112 ctx->Array.Normal.Enabled = GL_FALSE;
1113 ctx->Array.Color.Size = 4;
1114 ctx->Array.Color.Type = GL_FLOAT;
1115 ctx->Array.Color.Stride = 0;
1116 ctx->Array.Color.StrideB = 0;
1117 ctx->Array.Color.Ptr = NULL;
1118 ctx->Array.Color.Enabled = GL_FALSE;
1119 ctx->Array.Index.Type = GL_FLOAT;
1120 ctx->Array.Index.Stride = 0;
1121 ctx->Array.Index.StrideB = 0;
1122 ctx->Array.Index.Ptr = NULL;
1123 ctx->Array.Index.Enabled = GL_FALSE;
1124 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1125 ctx->Array.TexCoord[i].Size = 4;
1126 ctx->Array.TexCoord[i].Type = GL_FLOAT;
1127 ctx->Array.TexCoord[i].Stride = 0;
1128 ctx->Array.TexCoord[i].StrideB = 0;
1129 ctx->Array.TexCoord[i].Ptr = NULL;
1130 ctx->Array.TexCoord[i].Enabled = GL_FALSE;
1131 }
1132 ctx->Array.TexCoordInterleaveFactor = 1;
1133 ctx->Array.EdgeFlag.Stride = 0;
1134 ctx->Array.EdgeFlag.StrideB = 0;
1135 ctx->Array.EdgeFlag.Ptr = NULL;
1136 ctx->Array.EdgeFlag.Enabled = GL_FALSE;
1137 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1138
1139 /* Pixel transfer */
1140 ctx->Pack.Alignment = 4;
1141 ctx->Pack.RowLength = 0;
1142 ctx->Pack.ImageHeight = 0;
1143 ctx->Pack.SkipPixels = 0;
1144 ctx->Pack.SkipRows = 0;
1145 ctx->Pack.SkipImages = 0;
1146 ctx->Pack.SwapBytes = GL_FALSE;
1147 ctx->Pack.LsbFirst = GL_FALSE;
1148 ctx->Unpack.Alignment = 4;
1149 ctx->Unpack.RowLength = 0;
1150 ctx->Unpack.ImageHeight = 0;
1151 ctx->Unpack.SkipPixels = 0;
1152 ctx->Unpack.SkipRows = 0;
1153 ctx->Unpack.SkipImages = 0;
1154 ctx->Unpack.SwapBytes = GL_FALSE;
1155 ctx->Unpack.LsbFirst = GL_FALSE;
1156
1157 /* Feedback */
1158 ctx->Feedback.Type = GL_2D; /* TODO: verify */
1159 ctx->Feedback.Buffer = NULL;
1160 ctx->Feedback.BufferSize = 0;
1161 ctx->Feedback.Count = 0;
1162
1163 /* Selection/picking */
1164 ctx->Select.Buffer = NULL;
1165 ctx->Select.BufferSize = 0;
1166 ctx->Select.BufferCount = 0;
1167 ctx->Select.Hits = 0;
1168 ctx->Select.NameStackDepth = 0;
1169
1170 /* Optimized Accum buffer */
1171 ctx->IntegerAccumMode = GL_TRUE;
1172 ctx->IntegerAccumScaler = 0.0;
1173
1174 /* Renderer and client attribute stacks */
1175 ctx->AttribStackDepth = 0;
1176 ctx->ClientAttribStackDepth = 0;
1177
1178 /* Miscellaneous */
1179 ctx->NewState = NEW_ALL;
1180 ctx->RenderMode = GL_RENDER;
1181 ctx->StippleCounter = 0;
1182 ctx->NeedNormals = GL_FALSE;
1183 ctx->DoViewportMapping = GL_TRUE;
1184
1185 ctx->NeedEyeCoords = GL_FALSE;
1186 ctx->NeedEyeNormals = GL_FALSE;
1187 ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
1188
1189 /* Display list */
1190 ctx->CallDepth = 0;
1191 ctx->ExecuteFlag = GL_TRUE;
1192 ctx->CompileFlag = GL_FALSE;
1193 ctx->CurrentListPtr = NULL;
1194 ctx->CurrentBlock = NULL;
1195 ctx->CurrentListNum = 0;
1196 ctx->CurrentPos = 0;
1197
1198 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1199
1200 ctx->CatchSignals = GL_TRUE;
1201
1202 /* For debug/development only */
1203 ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
1204 ctx->FirstTimeCurrent = GL_TRUE;
1205
1206 /* Dither disable */
1207 ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
1208 if (ctx->NoDither) {
1209 if (getenv("MESA_DEBUG")) {
1210 fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
jtgafb833d1999-08-19 00:55:39 +00001211 }
Brian Paul4d053dd2000-01-14 04:45:47 +00001212 ctx->Color.DitherFlag = GL_FALSE;
Brian Paul00037781999-12-17 14:52:35 +00001213 }
1214}
1215
1216
1217
1218
jtgafb833d1999-08-19 00:55:39 +00001219/*
1220 * Allocate the proxy textures. If we run out of memory part way through
1221 * the allocations clean up and return GL_FALSE.
1222 * Return: GL_TRUE=success, GL_FALSE=failure
1223 */
1224static GLboolean alloc_proxy_textures( GLcontext *ctx )
1225{
1226 GLboolean out_of_memory;
1227 GLint i;
1228
1229 ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
1230 if (!ctx->Texture.Proxy1D) {
1231 return GL_FALSE;
1232 }
1233
1234 ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
1235 if (!ctx->Texture.Proxy2D) {
1236 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1237 return GL_FALSE;
1238 }
1239
1240 ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
1241 if (!ctx->Texture.Proxy3D) {
1242 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1243 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1244 return GL_FALSE;
1245 }
1246
1247 out_of_memory = GL_FALSE;
1248 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1249 ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
1250 ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
1251 ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
1252 if (!ctx->Texture.Proxy1D->Image[i]
1253 || !ctx->Texture.Proxy2D->Image[i]
1254 || !ctx->Texture.Proxy3D->Image[i]) {
1255 out_of_memory = GL_TRUE;
1256 }
1257 }
1258 if (out_of_memory) {
1259 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1260 if (ctx->Texture.Proxy1D->Image[i]) {
1261 gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
1262 }
1263 if (ctx->Texture.Proxy2D->Image[i]) {
1264 gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
1265 }
1266 if (ctx->Texture.Proxy3D->Image[i]) {
1267 gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
1268 }
1269 }
1270 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1271 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1272 gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
1273 return GL_FALSE;
1274 }
1275 else {
1276 return GL_TRUE;
1277 }
1278}
1279
1280
1281
jtgafb833d1999-08-19 00:55:39 +00001282/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001283 * Initialize a GLcontext struct.
jtgafb833d1999-08-19 00:55:39 +00001284 */
Brian Paul4d053dd2000-01-14 04:45:47 +00001285GLboolean gl_initialize_context_data( GLcontext *ctx,
1286 GLvisual *visual,
1287 GLcontext *share_list,
1288 void *driver_ctx,
1289 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001290{
jtgafb833d1999-08-19 00:55:39 +00001291 (void) direct; /* not used */
1292
jtgafb833d1999-08-19 00:55:39 +00001293 /* misc one-time initializations */
1294 one_time_init();
1295
jtgafb833d1999-08-19 00:55:39 +00001296 ctx->DriverCtx = driver_ctx;
1297 ctx->Visual = visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001298 ctx->DrawBuffer = NULL;
1299 ctx->ReadBuffer = NULL;
jtgafb833d1999-08-19 00:55:39 +00001300
1301 ctx->VB = gl_vb_create_for_immediate( ctx );
1302 if (!ctx->VB) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001303 FREE( ctx );
Brian Paul4d053dd2000-01-14 04:45:47 +00001304 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001305 }
1306 ctx->input = ctx->VB->IM;
1307
1308 ctx->PB = gl_alloc_pb();
1309 if (!ctx->PB) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001310 FREE( ctx->VB );
1311 FREE( ctx );
Brian Paul4d053dd2000-01-14 04:45:47 +00001312 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001313 }
1314
1315 if (share_list) {
1316 /* share the group of display lists of another context */
1317 ctx->Shared = share_list->Shared;
1318 }
1319 else {
1320 /* allocate new group of display lists */
1321 ctx->Shared = alloc_shared_state();
1322 if (!ctx->Shared) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001323 FREE(ctx->VB);
1324 FREE(ctx->PB);
1325 FREE(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001326 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001327 }
1328 }
Brian Paul9560f052000-01-31 23:11:39 +00001329 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001330 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001331 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001332
Brian Paul4d053dd2000-01-14 04:45:47 +00001333 init_attrib_groups( ctx );
1334
jtgafb833d1999-08-19 00:55:39 +00001335 gl_reset_vb( ctx->VB );
1336 gl_reset_input( ctx );
1337
jtgafb833d1999-08-19 00:55:39 +00001338 if (visual->DBflag) {
1339 ctx->Color.DrawBuffer = GL_BACK;
1340 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
1341 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
1342 ctx->Pixel.ReadBuffer = GL_BACK;
1343 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
1344 }
1345 else {
1346 ctx->Color.DrawBuffer = GL_FRONT;
1347 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
1348 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
1349 ctx->Pixel.ReadBuffer = GL_FRONT;
1350 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
1351 }
1352
1353#ifdef PROFILE
1354 init_timings( ctx );
1355#endif
1356
jtgafb833d1999-08-19 00:55:39 +00001357 if (!alloc_proxy_textures(ctx)) {
1358 free_shared_state(ctx, ctx->Shared);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001359 FREE(ctx->VB);
1360 FREE(ctx->PB);
1361 FREE(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001362 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001363 }
jtgafb833d1999-08-19 00:55:39 +00001364
Brian Paulfbd8f211999-11-11 01:22:25 +00001365 /* setup API dispatch tables */
1366 _mesa_init_exec_table( &ctx->Exec );
1367 _mesa_init_dlist_table( &ctx->Save );
1368 ctx->CurrentDispatch = &ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001369
Brian Paul4d053dd2000-01-14 04:45:47 +00001370 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001371}
1372
jtgafb833d1999-08-19 00:55:39 +00001373
1374
1375/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001376 * Allocate and initialize a GLcontext structure.
1377 * Input: visual - a GLvisual pointer
1378 * sharelist - another context to share display lists with or NULL
1379 * driver_ctx - pointer to device driver's context state struct
1380 * Return: pointer to a new gl_context struct or NULL if error.
1381 */
1382GLcontext *gl_create_context( GLvisual *visual,
1383 GLcontext *share_list,
1384 void *driver_ctx,
1385 GLboolean direct )
1386{
1387 GLcontext *ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
1388 if (!ctx) {
1389 return NULL;
1390 }
1391
1392 if (gl_initialize_context_data(ctx, visual, share_list,
1393 driver_ctx, direct)) {
1394 return ctx;
1395 }
1396 else {
1397 FREE(ctx);
1398 return NULL;
1399 }
1400}
1401
1402
1403
1404/*
1405 * Free the data associated with the given context.
1406 * But don't free() the GLcontext struct itself!
1407 */
1408void gl_free_context_data( GLcontext *ctx )
1409{
1410 GLuint i;
1411 struct gl_shine_tab *s, *tmps;
1412
1413 /* if we're destroying the current context, unbind it first */
1414 if (ctx == gl_get_current_context()) {
1415 gl_make_current(NULL, NULL);
1416 }
1417
1418#ifdef PROFILE
1419 if (getenv("MESA_PROFILE")) {
1420 print_timings( ctx );
1421 }
1422#endif
1423
1424 gl_matrix_dtr( &ctx->ModelView );
1425 for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
1426 gl_matrix_dtr( &ctx->ModelViewStack[i] );
1427 }
1428 gl_matrix_dtr( &ctx->ProjectionMatrix );
1429 for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
1430 gl_matrix_dtr( &ctx->ProjectionStack[i] );
1431 }
1432
1433 FREE( ctx->PB );
1434
1435 if(ctx->input != ctx->VB->IM)
1436 gl_immediate_free( ctx->input );
1437
1438 gl_vb_free( ctx->VB );
1439
Brian Paul9560f052000-01-31 23:11:39 +00001440 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001441 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001442 assert(ctx->Shared->RefCount >= 0);
1443 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1444 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001445 /* free shared state */
1446 free_shared_state( ctx, ctx->Shared );
1447 }
1448
1449 foreach_s( s, tmps, ctx->ShineTabList ) {
1450 FREE( s );
1451 }
1452 FREE( ctx->ShineTabList );
1453
1454 /* Free proxy texture objects */
1455 gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
1456 gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
1457 gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
1458
1459 /* Free evaluator data */
1460 if (ctx->EvalMap.Map1Vertex3.Points)
1461 FREE( ctx->EvalMap.Map1Vertex3.Points );
1462 if (ctx->EvalMap.Map1Vertex4.Points)
1463 FREE( ctx->EvalMap.Map1Vertex4.Points );
1464 if (ctx->EvalMap.Map1Index.Points)
1465 FREE( ctx->EvalMap.Map1Index.Points );
1466 if (ctx->EvalMap.Map1Color4.Points)
1467 FREE( ctx->EvalMap.Map1Color4.Points );
1468 if (ctx->EvalMap.Map1Normal.Points)
1469 FREE( ctx->EvalMap.Map1Normal.Points );
1470 if (ctx->EvalMap.Map1Texture1.Points)
1471 FREE( ctx->EvalMap.Map1Texture1.Points );
1472 if (ctx->EvalMap.Map1Texture2.Points)
1473 FREE( ctx->EvalMap.Map1Texture2.Points );
1474 if (ctx->EvalMap.Map1Texture3.Points)
1475 FREE( ctx->EvalMap.Map1Texture3.Points );
1476 if (ctx->EvalMap.Map1Texture4.Points)
1477 FREE( ctx->EvalMap.Map1Texture4.Points );
1478
1479 if (ctx->EvalMap.Map2Vertex3.Points)
1480 FREE( ctx->EvalMap.Map2Vertex3.Points );
1481 if (ctx->EvalMap.Map2Vertex4.Points)
1482 FREE( ctx->EvalMap.Map2Vertex4.Points );
1483 if (ctx->EvalMap.Map2Index.Points)
1484 FREE( ctx->EvalMap.Map2Index.Points );
1485 if (ctx->EvalMap.Map2Color4.Points)
1486 FREE( ctx->EvalMap.Map2Color4.Points );
1487 if (ctx->EvalMap.Map2Normal.Points)
1488 FREE( ctx->EvalMap.Map2Normal.Points );
1489 if (ctx->EvalMap.Map2Texture1.Points)
1490 FREE( ctx->EvalMap.Map2Texture1.Points );
1491 if (ctx->EvalMap.Map2Texture2.Points)
1492 FREE( ctx->EvalMap.Map2Texture2.Points );
1493 if (ctx->EvalMap.Map2Texture3.Points)
1494 FREE( ctx->EvalMap.Map2Texture3.Points );
1495 if (ctx->EvalMap.Map2Texture4.Points)
1496 FREE( ctx->EvalMap.Map2Texture4.Points );
1497
1498 /* Free cache of immediate buffers. */
1499 while (ctx->nr_im_queued-- > 0) {
1500 struct immediate * next = ctx->freed_im_queue->next;
1501 FREE( ctx->freed_im_queue );
1502 ctx->freed_im_queue = next;
1503 }
1504 gl_extensions_dtr(ctx);
1505}
1506
1507
1508
1509/*
1510 * Destroy a GLcontext structure.
jtgafb833d1999-08-19 00:55:39 +00001511 */
1512void gl_destroy_context( GLcontext *ctx )
1513{
1514 if (ctx) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001515 gl_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001516 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001517 }
1518}
1519
1520
1521
1522/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001523 * Called by the driver after both the context and driver are fully
1524 * initialized. Currently just reads the config file.
jtgafb833d1999-08-19 00:55:39 +00001525 */
Brian Paul00037781999-12-17 14:52:35 +00001526void gl_context_initialize( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001527{
Brian Paul00037781999-12-17 14:52:35 +00001528 gl_read_config_file( ctx );
jtgafb833d1999-08-19 00:55:39 +00001529}
1530
1531
1532
1533/*
1534 * Copy attribute groups from one context to another.
1535 * Input: src - source context
1536 * dst - destination context
1537 * mask - bitwise OR of GL_*_BIT flags
1538 */
1539void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1540{
1541 if (mask & GL_ACCUM_BUFFER_BIT) {
1542 MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
1543 }
1544 if (mask & GL_COLOR_BUFFER_BIT) {
1545 MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
1546 }
1547 if (mask & GL_CURRENT_BIT) {
1548 MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
1549 }
1550 if (mask & GL_DEPTH_BUFFER_BIT) {
1551 MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
1552 }
1553 if (mask & GL_ENABLE_BIT) {
1554 /* no op */
1555 }
1556 if (mask & GL_EVAL_BIT) {
1557 MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
1558 }
1559 if (mask & GL_FOG_BIT) {
1560 MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
1561 }
1562 if (mask & GL_HINT_BIT) {
1563 MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
1564 }
1565 if (mask & GL_LIGHTING_BIT) {
1566 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
Brian Paul00037781999-12-17 14:52:35 +00001567 /* gl_reinit_light_attrib( &dst->Light ); */
jtgafb833d1999-08-19 00:55:39 +00001568 }
1569 if (mask & GL_LINE_BIT) {
1570 MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
1571 }
1572 if (mask & GL_LIST_BIT) {
1573 MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
1574 }
1575 if (mask & GL_PIXEL_MODE_BIT) {
1576 MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
1577 }
1578 if (mask & GL_POINT_BIT) {
1579 MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
1580 }
1581 if (mask & GL_POLYGON_BIT) {
1582 MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
1583 }
1584 if (mask & GL_POLYGON_STIPPLE_BIT) {
1585 /* Use loop instead of MEMCPY due to problem with Portland Group's
1586 * C compiler. Reported by John Stone.
1587 */
1588 int i;
1589 for (i=0;i<32;i++) {
1590 dst->PolygonStipple[i] = src->PolygonStipple[i];
1591 }
1592 }
1593 if (mask & GL_SCISSOR_BIT) {
1594 MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
1595 }
1596 if (mask & GL_STENCIL_BUFFER_BIT) {
1597 MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
1598 }
1599 if (mask & GL_TEXTURE_BIT) {
1600 MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
1601 }
1602 if (mask & GL_TRANSFORM_BIT) {
1603 MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
1604 }
1605 if (mask & GL_VIEWPORT_BIT) {
1606 MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
1607 }
1608}
1609
1610
jtgafb833d1999-08-19 00:55:39 +00001611/*
Brian Paul00037781999-12-17 14:52:35 +00001612 * Set the current context, binding the given frame buffer to the context.
1613 */
1614void gl_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1615{
1616 gl_make_current2( newCtx, buffer, buffer );
1617}
1618
1619
1620/*
1621 * Bind the given context to the given draw-buffer and read-buffer
1622 * and make it the current context for this thread.
1623 */
1624void gl_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1625 GLframebuffer *readBuffer )
1626{
1627#if 0
Brian Paulf9b97d92000-01-28 20:17:42 +00001628 GLcontext *oldCtx = gl_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001629
1630 /* Flush the old context
1631 */
1632 if (oldCtx) {
1633 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx, "gl_make_current");
1634
1635 /* unbind frame buffers from context */
1636 if (oldCtx->DrawBuffer) {
1637 oldCtx->DrawBuffer = NULL;
1638 }
1639 if (oldCtx->ReadBuffer) {
1640 oldCtx->ReadBuffer = NULL;
1641 }
1642 }
1643#endif
1644
1645 /* We call this function periodically (just here for now) in
1646 * order to detect when multithreading has begun.
1647 */
1648 _glapi_check_multithread();
1649
Brian Paulf9b97d92000-01-28 20:17:42 +00001650 _glapi_set_context((void *) newCtx);
Brian Paul00037781999-12-17 14:52:35 +00001651 ASSERT(gl_get_current_context() == newCtx);
1652 if (newCtx) {
1653 SET_IMMEDIATE(newCtx, newCtx->input);
1654 _glapi_set_dispatch(newCtx->CurrentDispatch);
1655 }
1656 else {
1657 _glapi_set_dispatch(NULL); /* none current */
1658 }
1659
1660 if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
1661
1662 if (newCtx && drawBuffer && readBuffer) {
1663 /* TODO: check if newCtx and buffer's visual match??? */
1664 newCtx->DrawBuffer = drawBuffer;
1665 newCtx->ReadBuffer = readBuffer;
1666 newCtx->NewState = NEW_ALL; /* just to be safe */
1667 gl_update_state( newCtx );
1668 }
1669
1670 /* We can use this to help debug user's problems. Tell the to set
1671 * the MESA_INFO env variable before running their app. Then the
1672 * first time each context is made current we'll print some useful
1673 * information.
1674 */
1675 if (newCtx && newCtx->FirstTimeCurrent) {
1676 if (getenv("MESA_INFO")) {
1677 fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION));
1678 fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER));
1679 fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR));
1680 fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS));
Brian Paul09cb1481999-12-17 17:00:32 +00001681#if defined(THREADS)
1682 fprintf(stderr, "Mesa thread-safe: YES\n");
1683#else
1684 fprintf(stderr, "Mesa thread-safe: NO\n");
1685#endif
Brian Paul54287052000-01-17 20:00:15 +00001686#if defined(USE_X86_ASM)
1687 fprintf(stderr, "Mesa x86-optimized: YES\n");
1688#else
1689 fprintf(stderr, "Mesa x86-optimized: NO\n");
1690#endif
Brian Paul00037781999-12-17 14:52:35 +00001691 }
1692 newCtx->FirstTimeCurrent = GL_FALSE;
1693 }
1694}
1695
1696
1697
1698/*
1699 * Return current context handle for the calling thread.
1700 * This isn't the fastest way to get the current context.
1701 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1702 */
1703GLcontext *gl_get_current_context( void )
1704{
Brian Paulf9b97d92000-01-28 20:17:42 +00001705 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001706}
1707
1708
1709
1710/*
Brian Paulfbd8f211999-11-11 01:22:25 +00001711 * This should be called by device drivers just before they do a
1712 * swapbuffers. Any pending rendering commands will be executed.
jtgafb833d1999-08-19 00:55:39 +00001713 */
Brian Paulfbd8f211999-11-11 01:22:25 +00001714void
1715_mesa_swapbuffers(GLcontext *ctx)
jtgafb833d1999-08-19 00:55:39 +00001716{
Brian Paulfbd8f211999-11-11 01:22:25 +00001717 FLUSH_VB( ctx, "swap buffers" );
jtgafb833d1999-08-19 00:55:39 +00001718}
1719
1720
Brian Paul00037781999-12-17 14:52:35 +00001721
Brian Paulfbd8f211999-11-11 01:22:25 +00001722/*
1723 * Return pointer to this context's current API dispatch table.
1724 * It'll either be the immediate-mode execute dispatcher or the
1725 * display list compile dispatcher.
1726 */
1727struct _glapi_table *
1728_mesa_get_dispatch(GLcontext *ctx)
1729{
1730 return ctx->CurrentDispatch;
1731}
1732
1733
1734
jtgafb833d1999-08-19 00:55:39 +00001735/**********************************************************************/
1736/***** Miscellaneous functions *****/
1737/**********************************************************************/
1738
1739
1740/*
1741 * This function is called when the Mesa user has stumbled into a code
1742 * path which may not be implemented fully or correctly.
1743 */
1744void gl_problem( const GLcontext *ctx, const char *s )
1745{
1746 fprintf( stderr, "Mesa implementation error: %s\n", s );
1747 fprintf( stderr, "Report to mesa-bugs@mesa3d.org\n" );
1748 (void) ctx;
1749}
1750
1751
1752
1753/*
1754 * This is called to inform the user that he or she has tried to do
1755 * something illogical or if there's likely a bug in their program
1756 * (like enabled depth testing without a depth buffer).
1757 */
1758void gl_warning( const GLcontext *ctx, const char *s )
1759{
1760 GLboolean debug;
1761#ifdef DEBUG
1762 debug = GL_TRUE;
1763#else
1764 if (getenv("MESA_DEBUG")) {
1765 debug = GL_TRUE;
1766 }
1767 else {
1768 debug = GL_FALSE;
1769 }
1770#endif
1771 if (debug) {
1772 fprintf( stderr, "Mesa warning: %s\n", s );
1773 }
1774 (void) ctx;
1775}
1776
1777
1778
Brian Paulfa9df402000-02-02 19:16:46 +00001779/*
1780 * Compile an error into current display list.
1781 */
jtgafb833d1999-08-19 00:55:39 +00001782void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
1783{
1784 if (ctx->CompileFlag)
1785 gl_save_error( ctx, error, s );
1786
1787 if (ctx->ExecuteFlag)
1788 gl_error( ctx, error, s );
1789}
1790
1791
Brian Paulfa9df402000-02-02 19:16:46 +00001792
jtgafb833d1999-08-19 00:55:39 +00001793/*
1794 * This is Mesa's error handler. Normally, all that's done is the updating
1795 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1796 * environment variable "MESA_DEBUG" is defined then a real error message
1797 * is printed to stderr.
1798 * Input: error - the error value
1799 * s - a diagnostic string
1800 */
1801void gl_error( GLcontext *ctx, GLenum error, const char *s )
1802{
1803 GLboolean debug;
1804
1805#ifdef DEBUG
1806 debug = GL_TRUE;
1807#else
1808 if (getenv("MESA_DEBUG")) {
1809 debug = GL_TRUE;
1810 }
1811 else {
1812 debug = GL_FALSE;
1813 }
1814#endif
1815
1816 if (debug) {
1817 char errstr[1000];
1818
1819 switch (error) {
1820 case GL_NO_ERROR:
1821 strcpy( errstr, "GL_NO_ERROR" );
1822 break;
1823 case GL_INVALID_VALUE:
1824 strcpy( errstr, "GL_INVALID_VALUE" );
1825 break;
1826 case GL_INVALID_ENUM:
1827 strcpy( errstr, "GL_INVALID_ENUM" );
1828 break;
1829 case GL_INVALID_OPERATION:
1830 strcpy( errstr, "GL_INVALID_OPERATION" );
1831 break;
1832 case GL_STACK_OVERFLOW:
1833 strcpy( errstr, "GL_STACK_OVERFLOW" );
1834 break;
1835 case GL_STACK_UNDERFLOW:
1836 strcpy( errstr, "GL_STACK_UNDERFLOW" );
1837 break;
1838 case GL_OUT_OF_MEMORY:
1839 strcpy( errstr, "GL_OUT_OF_MEMORY" );
1840 break;
1841 default:
1842 strcpy( errstr, "unknown" );
1843 break;
1844 }
1845 fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
1846 }
1847
1848 if (ctx->ErrorValue==GL_NO_ERROR) {
1849 ctx->ErrorValue = error;
1850 }
1851
1852 /* Call device driver's error handler, if any. This is used on the Mac. */
1853 if (ctx->Driver.Error) {
1854 (*ctx->Driver.Error)( ctx );
1855 }
1856}
1857
1858
1859
Brian Paulfa9df402000-02-02 19:16:46 +00001860void
1861_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001862{
Brian Paulfa9df402000-02-02 19:16:46 +00001863 GET_CURRENT_CONTEXT(ctx);
1864 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFinish");
1865 if (ctx->Driver.Finish) {
1866 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001867 }
1868}
1869
1870
1871
Brian Paulfa9df402000-02-02 19:16:46 +00001872void
1873_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001874{
Brian Paulfa9df402000-02-02 19:16:46 +00001875 GET_CURRENT_CONTEXT(ctx);
1876 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFlush");
1877 if (ctx->Driver.Flush) {
1878 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001879 }
jtgafb833d1999-08-19 00:55:39 +00001880}