blob: e106b0343ed764c49f6aed5da0ce500c5c59dd54 [file] [log] [blame]
Brian Paul86fc3702000-05-22 16:33:20 +00001/* $Id: context.c,v 1.66 2000/05/22 16:33:20 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"
Brian Paul4bdcfe52000-04-17 17:57:04 +000035#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000036#include "context.h"
37#include "cva.h"
38#include "depth.h"
39#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
jtgafb833d1999-08-19 00:55:39 +000090/**********************************************************************/
Brian Paul4d053dd2000-01-14 04:45:47 +000091/***** GL Visual allocation/destruction *****/
92/**********************************************************************/
93
94
95/*
96 * Allocate a new GLvisual object.
97 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
Brian Paul4d053dd2000-01-14 04:45:47 +000098 * dbFlag - double buffering?
99 * stereoFlag - stereo buffer?
Brian Pauled30dfa2000-03-03 17:47:39 +0000100 * depthBits - requested bits per depth buffer value
101 * Any value in [0, 32] is acceptable but the actual
102 * depth type will be GLushort or GLuint as needed.
103 * stencilBits - requested minimum bits per stencil buffer value
104 * accumBits - requested minimum bits per accum buffer component
105 * indexBits - number of bits per pixel if rgbFlag==GL_FALSE
106 * red/green/blue/alphaBits - number of bits per color component
107 * in frame buffer for RGB(A) mode.
108 * We always use 8 in core Mesa though.
Brian Paul4d053dd2000-01-14 04:45:47 +0000109 * Return: pointer to new GLvisual or NULL if requested parameters can't
110 * be met.
111 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000112GLvisual *
113_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000114 GLboolean dbFlag,
115 GLboolean stereoFlag,
116 GLint redBits,
117 GLint greenBits,
118 GLint blueBits,
119 GLint alphaBits,
120 GLint indexBits,
121 GLint depthBits,
122 GLint stencilBits,
123 GLint accumRedBits,
124 GLint accumGreenBits,
125 GLint accumBlueBits,
126 GLint accumAlphaBits,
127 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000128{
Brian Paul178a1c52000-04-22 01:05:00 +0000129 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
130 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000131 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000132 redBits, greenBits, blueBits, alphaBits,
133 indexBits, depthBits, stencilBits,
134 accumRedBits, accumGreenBits,
135 accumBlueBits, accumAlphaBits,
136 numSamples )) {
137 FREE(vis);
138 return NULL;
139 }
140 }
141 return vis;
142}
143
144
145/*
146 * Initialize the fields of the given GLvisual.
147 * Input: see _mesa_create_visual() above.
148 * Return: GL_TRUE = success
149 * GL_FALSE = failure.
150 */
151GLboolean
152_mesa_initialize_visual( GLvisual *vis,
153 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000154 GLboolean dbFlag,
155 GLboolean stereoFlag,
156 GLint redBits,
157 GLint greenBits,
158 GLint blueBits,
159 GLint alphaBits,
160 GLint indexBits,
161 GLint depthBits,
162 GLint stencilBits,
163 GLint accumRedBits,
164 GLint accumGreenBits,
165 GLint accumBlueBits,
166 GLint accumAlphaBits,
167 GLint numSamples )
168{
169 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000170
Brian Pauled30dfa2000-03-03 17:47:39 +0000171 /* This is to catch bad values from device drivers not updated for
172 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
173 * bad value now (a 1-bit depth buffer!?!).
174 */
175 assert(depthBits == 0 || depthBits > 1);
176
177 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000178 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000179 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000180 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000181 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000182 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000183 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000184 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000185 }
186 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000187 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000188 }
189 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000190 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000191 }
192 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000193 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000194 }
195
196 vis->RGBAflag = rgbFlag;
197 vis->DBflag = dbFlag;
198 vis->StereoFlag = stereoFlag;
199 vis->RedBits = redBits;
200 vis->GreenBits = greenBits;
201 vis->BlueBits = blueBits;
Brian Paule70c6232000-05-04 13:53:55 +0000202 vis->AlphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000203
Brian Paulb371e0d2000-03-31 01:05:51 +0000204 vis->IndexBits = indexBits;
205 vis->DepthBits = depthBits;
206 vis->AccumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
207 vis->AccumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
208 vis->AccumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
209 vis->AccumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
210 vis->StencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Paul4d053dd2000-01-14 04:45:47 +0000211
Brian Pauled30dfa2000-03-03 17:47:39 +0000212 if (depthBits == 0) {
213 /* Special case. Even if we don't have a depth buffer we need
214 * good values for DepthMax for Z vertex transformation purposes.
215 */
216 vis->DepthMax = 1;
217 vis->DepthMaxF = 1.0F;
218 }
Brian Paul3a94f5c2000-05-18 18:12:36 +0000219 else if (depthBits < 32) {
Brian Pauled30dfa2000-03-03 17:47:39 +0000220 vis->DepthMax = (1 << depthBits) - 1;
221 vis->DepthMaxF = (GLfloat) vis->DepthMax;
222 }
Brian Paul3a94f5c2000-05-18 18:12:36 +0000223 else {
224 /* Special case since shift values greater than or equal to the
225 * number of bits in the left hand expression's type are
226 * undefined.
227 */
228 vis->DepthMax = 0xffffffff;
229 vis->DepthMaxF = (GLfloat) vis->DepthMax;
230 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000231
Brian Paul178a1c52000-04-22 01:05:00 +0000232 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000233}
234
235
Brian Paulb371e0d2000-03-31 01:05:51 +0000236/* This function should no longer be used. Use _mesa_create_visual() instead */
Brian Paul178a1c52000-04-22 01:05:00 +0000237GLvisual *
238gl_create_visual( GLboolean rgbFlag,
239 GLboolean alphaFlag,
240 GLboolean dbFlag,
241 GLboolean stereoFlag,
242 GLint depthBits,
243 GLint stencilBits,
244 GLint accumBits,
245 GLint indexBits,
246 GLint redBits,
247 GLint greenBits,
248 GLint blueBits,
249 GLint alphaBits )
Brian Paulb371e0d2000-03-31 01:05:51 +0000250{
Brian Paule70c6232000-05-04 13:53:55 +0000251 (void) alphaFlag;
252 return _mesa_create_visual(rgbFlag, dbFlag, stereoFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000253 redBits, greenBits, blueBits, alphaBits,
254 indexBits, depthBits, stencilBits,
255 accumBits, accumBits, accumBits, accumBits, 0);
256}
Brian Paul4d053dd2000-01-14 04:45:47 +0000257
Brian Paulb371e0d2000-03-31 01:05:51 +0000258
259void
260_mesa_destroy_visual( GLvisual *vis )
261{
262 FREE(vis);
263}
264
265
266/* obsolete */
Brian Paul178a1c52000-04-22 01:05:00 +0000267void
268gl_destroy_visual( GLvisual *vis )
Brian Paul4d053dd2000-01-14 04:45:47 +0000269{
Brian Paulb371e0d2000-03-31 01:05:51 +0000270 _mesa_destroy_visual(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000271}
272
273
274
275/**********************************************************************/
276/***** GL Framebuffer allocation/destruction *****/
277/**********************************************************************/
278
279
280/*
281 * Create a new framebuffer. A GLframebuffer is a struct which
282 * encapsulates the depth, stencil and accum buffers and related
283 * parameters.
284 * Input: visual - a GLvisual pointer
285 * softwareDepth - create/use a software depth buffer?
286 * softwareStencil - create/use a software stencil buffer?
287 * softwareAccum - create/use a software accum buffer?
288 * softwareAlpha - create/use a software alpha buffer?
289
290 * Return: pointer to new GLframebuffer struct or NULL if error.
291 */
Brian Paul178a1c52000-04-22 01:05:00 +0000292GLframebuffer *
293gl_create_framebuffer( GLvisual *visual,
294 GLboolean softwareDepth,
295 GLboolean softwareStencil,
296 GLboolean softwareAccum,
297 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000298{
Brian Paul178a1c52000-04-22 01:05:00 +0000299 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
300 assert(visual);
301 if (buffer) {
302 _mesa_initialize_framebuffer(buffer, visual,
303 softwareDepth, softwareStencil,
304 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000305 }
Brian Paul178a1c52000-04-22 01:05:00 +0000306 return buffer;
307}
308
309
310/*
311 * Initialize a GLframebuffer object.
312 * Input: See gl_create_framebuffer() above.
313 */
314void
315_mesa_initialize_framebuffer( GLframebuffer *buffer,
316 GLvisual *visual,
317 GLboolean softwareDepth,
318 GLboolean softwareStencil,
319 GLboolean softwareAccum,
320 GLboolean softwareAlpha )
321{
322 assert(buffer);
323 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000324
325 /* sanity checks */
326 if (softwareDepth ) {
327 assert(visual->DepthBits > 0);
328 }
329 if (softwareStencil) {
330 assert(visual->StencilBits > 0);
331 }
332 if (softwareAccum) {
333 assert(visual->RGBAflag);
Brian Paulb371e0d2000-03-31 01:05:51 +0000334 assert(visual->AccumRedBits > 0);
335 assert(visual->AccumGreenBits > 0);
336 assert(visual->AccumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000337 }
338 if (softwareAlpha) {
339 assert(visual->RGBAflag);
340 assert(visual->AlphaBits > 0);
341 }
342
343 buffer->Visual = visual;
344 buffer->UseSoftwareDepthBuffer = softwareDepth;
345 buffer->UseSoftwareStencilBuffer = softwareStencil;
346 buffer->UseSoftwareAccumBuffer = softwareAccum;
347 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000348}
349
350
Brian Paul4d053dd2000-01-14 04:45:47 +0000351/*
352 * Free a framebuffer struct and its buffers.
353 */
Brian Paul178a1c52000-04-22 01:05:00 +0000354void
355gl_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000356{
357 if (buffer) {
Brian Paul650cb742000-03-17 15:31:52 +0000358 if (buffer->DepthBuffer) {
359 FREE( buffer->DepthBuffer );
Brian Paul4d053dd2000-01-14 04:45:47 +0000360 }
361 if (buffer->Accum) {
362 FREE( buffer->Accum );
363 }
364 if (buffer->Stencil) {
365 FREE( buffer->Stencil );
366 }
367 if (buffer->FrontLeftAlpha) {
368 FREE( buffer->FrontLeftAlpha );
369 }
370 if (buffer->BackLeftAlpha) {
371 FREE( buffer->BackLeftAlpha );
372 }
373 if (buffer->FrontRightAlpha) {
374 FREE( buffer->FrontRightAlpha );
375 }
376 if (buffer->BackRightAlpha) {
377 FREE( buffer->BackRightAlpha );
378 }
379 FREE(buffer);
380 }
381}
382
383
384
385/**********************************************************************/
jtgafb833d1999-08-19 00:55:39 +0000386/***** Context allocation, initialization, destroying *****/
387/**********************************************************************/
388
389
Brian Paul9560f052000-01-31 23:11:39 +0000390_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
391
392
jtgafb833d1999-08-19 00:55:39 +0000393/*
394 * This function just calls all the various one-time-init functions in Mesa.
395 */
Brian Paul178a1c52000-04-22 01:05:00 +0000396static void
397one_time_init( void )
jtgafb833d1999-08-19 00:55:39 +0000398{
399 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000400 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000401 if (!alreadyCalled) {
Brian Paul4d053dd2000-01-14 04:45:47 +0000402 /* do some implementation tests */
403 assert( sizeof(GLbyte) == 1 );
404 assert( sizeof(GLshort) >= 2 );
405 assert( sizeof(GLint) >= 4 );
406 assert( sizeof(GLubyte) == 1 );
407 assert( sizeof(GLushort) >= 2 );
408 assert( sizeof(GLuint) >= 4 );
409
jtgafb833d1999-08-19 00:55:39 +0000410 gl_init_clip();
411 gl_init_eval();
Brian Paul5829f0c2000-02-02 22:21:39 +0000412 _mesa_init_fog();
Brian Paul780c4e02000-03-22 23:20:12 +0000413 _mesa_init_math();
jtgafb833d1999-08-19 00:55:39 +0000414 gl_init_lists();
415 gl_init_shade();
416 gl_init_texture();
417 gl_init_transformation();
418 gl_init_translate();
419 gl_init_vbrender();
420 gl_init_vbxform();
Keith Whitwell38756791999-08-29 10:26:31 +0000421 gl_init_vertices();
Brian Paul68ee4bc2000-01-28 19:02:22 +0000422
423 if (getenv("MESA_DEBUG")) {
424 _glapi_noop_enable_warnings(GL_TRUE);
425 }
426 else {
427 _glapi_noop_enable_warnings(GL_FALSE);
428 }
429
jtgafb833d1999-08-19 00:55:39 +0000430#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
431 fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
432#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000433
434 alreadyCalled = GL_TRUE;
435 }
Brian Paul9560f052000-01-31 23:11:39 +0000436 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000437}
438
439
Brian Paul4d053dd2000-01-14 04:45:47 +0000440
jtgafb833d1999-08-19 00:55:39 +0000441/*
442 * Allocate and initialize a shared context state structure.
443 */
Brian Paul178a1c52000-04-22 01:05:00 +0000444static struct gl_shared_state *
445alloc_shared_state( void )
jtgafb833d1999-08-19 00:55:39 +0000446{
Brian Paul6e6d4c61999-10-09 20:17:07 +0000447 GLuint d;
jtgafb833d1999-08-19 00:55:39 +0000448 struct gl_shared_state *ss;
449 GLboolean outOfMemory;
450
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000451 ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000452 if (!ss)
453 return NULL;
454
Brian Paulbb797902000-01-24 16:19:54 +0000455 ss->DisplayList = _mesa_NewHashTable();
jtgafb833d1999-08-19 00:55:39 +0000456
Brian Paulbb797902000-01-24 16:19:54 +0000457 ss->TexObjects = _mesa_NewHashTable();
jtgafb833d1999-08-19 00:55:39 +0000458
459 /* Default Texture objects */
460 outOfMemory = GL_FALSE;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000461 for (d = 1 ; d <= 3 ; d++) {
462 ss->DefaultD[d] = gl_alloc_texture_object(ss, 0, d);
463 if (!ss->DefaultD[d]) {
464 outOfMemory = GL_TRUE;
465 break;
jtgafb833d1999-08-19 00:55:39 +0000466 }
Brian Paul6e6d4c61999-10-09 20:17:07 +0000467 ss->DefaultD[d]->RefCount++; /* don't free if not in use */
jtgafb833d1999-08-19 00:55:39 +0000468 }
469
470 if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
471 /* Ran out of memory at some point. Free everything and return NULL */
472 if (ss->DisplayList)
Brian Paulbb797902000-01-24 16:19:54 +0000473 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000474 if (ss->TexObjects)
Brian Paulbb797902000-01-24 16:19:54 +0000475 _mesa_DeleteHashTable(ss->TexObjects);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000476 if (ss->DefaultD[1])
477 gl_free_texture_object(ss, ss->DefaultD[1]);
478 if (ss->DefaultD[2])
479 gl_free_texture_object(ss, ss->DefaultD[2]);
480 if (ss->DefaultD[3])
481 gl_free_texture_object(ss, ss->DefaultD[3]);
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000482 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000483 return NULL;
484 }
485 else {
486 return ss;
487 }
488}
489
490
491/*
492 * Deallocate a shared state context and all children structures.
493 */
Brian Paul178a1c52000-04-22 01:05:00 +0000494static void
495free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000496{
497 /* Free display lists */
498 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000499 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000500 if (list) {
501 gl_destroy_list(ctx, list);
502 }
503 else {
504 break;
505 }
506 }
Brian Paulbb797902000-01-24 16:19:54 +0000507 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000508
509 /* Free texture objects */
510 while (ss->TexObjectList)
511 {
512 if (ctx->Driver.DeleteTexture)
513 (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
514 /* this function removes from linked list too! */
515 gl_free_texture_object(ss, ss->TexObjectList);
516 }
Brian Paulbb797902000-01-24 16:19:54 +0000517 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000518
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000519 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000520}
521
522
523
jtgafb833d1999-08-19 00:55:39 +0000524/*
525 * Initialize the nth light. Note that the defaults for light 0 are
526 * different than the other lights.
527 */
Brian Paul178a1c52000-04-22 01:05:00 +0000528static void
529init_light( struct gl_light *l, GLuint n )
jtgafb833d1999-08-19 00:55:39 +0000530{
531 make_empty_list( l );
532
533 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
534 if (n==0) {
535 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
536 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
537 }
538 else {
539 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
540 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
541 }
542 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
543 ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
544 l->SpotExponent = 0.0;
545 gl_compute_spot_exp_table( l );
546 l->SpotCutoff = 180.0;
547 l->CosCutoff = 0.0; /* KW: -ve values not admitted */
548 l->ConstantAttenuation = 1.0;
549 l->LinearAttenuation = 0.0;
550 l->QuadraticAttenuation = 0.0;
551 l->Enabled = GL_FALSE;
552}
553
554
555
Brian Paul178a1c52000-04-22 01:05:00 +0000556static void
557init_lightmodel( struct gl_lightmodel *lm )
jtgafb833d1999-08-19 00:55:39 +0000558{
559 ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
560 lm->LocalViewer = GL_FALSE;
561 lm->TwoSide = GL_FALSE;
562 lm->ColorControl = GL_SINGLE_COLOR;
563}
564
565
Brian Paul178a1c52000-04-22 01:05:00 +0000566static void
567init_material( struct gl_material *m )
jtgafb833d1999-08-19 00:55:39 +0000568{
569 ASSIGN_4V( m->Ambient, 0.2, 0.2, 0.2, 1.0 );
570 ASSIGN_4V( m->Diffuse, 0.8, 0.8, 0.8, 1.0 );
571 ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
572 ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
573 m->Shininess = 0.0;
574 m->AmbientIndex = 0;
575 m->DiffuseIndex = 1;
576 m->SpecularIndex = 1;
577}
578
579
580
Brian Paul178a1c52000-04-22 01:05:00 +0000581static void
582init_texture_unit( GLcontext *ctx, GLuint unit )
jtgafb833d1999-08-19 00:55:39 +0000583{
584 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
585
586 texUnit->EnvMode = GL_MODULATE;
587 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
588 texUnit->TexGenEnabled = 0;
589 texUnit->GenModeS = GL_EYE_LINEAR;
590 texUnit->GenModeT = GL_EYE_LINEAR;
591 texUnit->GenModeR = GL_EYE_LINEAR;
592 texUnit->GenModeQ = GL_EYE_LINEAR;
593 /* Yes, these plane coefficients are correct! */
594 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
595 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
596 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
597 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
598 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
599 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
600 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
601 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
602
Brian Paul6e6d4c61999-10-09 20:17:07 +0000603 texUnit->CurrentD[1] = ctx->Shared->DefaultD[1];
604 texUnit->CurrentD[2] = ctx->Shared->DefaultD[2];
605 texUnit->CurrentD[3] = ctx->Shared->DefaultD[3];
jtgafb833d1999-08-19 00:55:39 +0000606}
607
608
Brian Paul178a1c52000-04-22 01:05:00 +0000609static void
610init_fallback_arrays( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000611{
612 struct gl_client_array *cl;
613 GLuint i;
614
615 cl = &ctx->Fallback.Normal;
616 cl->Size = 3;
617 cl->Type = GL_FLOAT;
618 cl->Stride = 0;
619 cl->StrideB = 0;
620 cl->Ptr = (void *) ctx->Current.Normal;
621 cl->Enabled = 1;
622
623 cl = &ctx->Fallback.Color;
624 cl->Size = 4;
625 cl->Type = GL_UNSIGNED_BYTE;
626 cl->Stride = 0;
627 cl->StrideB = 0;
628 cl->Ptr = (void *) ctx->Current.ByteColor;
629 cl->Enabled = 1;
630
631 cl = &ctx->Fallback.Index;
632 cl->Size = 1;
633 cl->Type = GL_UNSIGNED_INT;
634 cl->Stride = 0;
635 cl->StrideB = 0;
636 cl->Ptr = (void *) &ctx->Current.Index;
637 cl->Enabled = 1;
638
639 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
640 cl = &ctx->Fallback.TexCoord[i];
641 cl->Size = 4;
642 cl->Type = GL_FLOAT;
643 cl->Stride = 0;
644 cl->StrideB = 0;
645 cl->Ptr = (void *) ctx->Current.Texcoord[i];
646 cl->Enabled = 1;
647 }
648
649 cl = &ctx->Fallback.EdgeFlag;
650 cl->Size = 1;
651 cl->Type = GL_UNSIGNED_BYTE;
652 cl->Stride = 0;
653 cl->StrideB = 0;
654 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
655 cl->Enabled = 1;
656}
657
Brian Paul4d053dd2000-01-14 04:45:47 +0000658
jtgafb833d1999-08-19 00:55:39 +0000659/* Initialize a 1-D evaluator map */
Brian Paul178a1c52000-04-22 01:05:00 +0000660static void
661init_1d_map( struct gl_1d_map *map, int n, const float *initial )
jtgafb833d1999-08-19 00:55:39 +0000662{
663 map->Order = 1;
664 map->u1 = 0.0;
665 map->u2 = 1.0;
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000666 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
jtgafb833d1999-08-19 00:55:39 +0000667 if (map->Points) {
668 GLint i;
669 for (i=0;i<n;i++)
670 map->Points[i] = initial[i];
671 }
jtgafb833d1999-08-19 00:55:39 +0000672}
673
674
675/* Initialize a 2-D evaluator map */
Brian Paul178a1c52000-04-22 01:05:00 +0000676static void
677init_2d_map( struct gl_2d_map *map, int n, const float *initial )
jtgafb833d1999-08-19 00:55:39 +0000678{
679 map->Uorder = 1;
680 map->Vorder = 1;
681 map->u1 = 0.0;
682 map->u2 = 1.0;
683 map->v1 = 0.0;
684 map->v2 = 1.0;
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000685 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
jtgafb833d1999-08-19 00:55:39 +0000686 if (map->Points) {
687 GLint i;
688 for (i=0;i<n;i++)
689 map->Points[i] = initial[i];
690 }
jtgafb833d1999-08-19 00:55:39 +0000691}
692
693
jtgafb833d1999-08-19 00:55:39 +0000694/*
Brian Paul4d053dd2000-01-14 04:45:47 +0000695 * Initialize the attribute groups in a GLcontext.
jtgafb833d1999-08-19 00:55:39 +0000696 */
Brian Paul178a1c52000-04-22 01:05:00 +0000697static void
698init_attrib_groups( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000699{
700 GLuint i, j;
701
Brian Paul4d053dd2000-01-14 04:45:47 +0000702 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +0000703
Brian Paul539cce52000-02-03 19:40:07 +0000704 /* Constants, may be overriden by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +0000705 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
706 ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
Brian Paul86fc3702000-05-22 16:33:20 +0000707 ctx->Const.MaxCubeTextureSize = ctx->Const.MaxTextureSize;
Brian Paul4d053dd2000-01-14 04:45:47 +0000708 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
709 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +0000710 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
711 ctx->Const.MinPointSize = MIN_POINT_SIZE;
712 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
713 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
714 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
715 ctx->Const.PointSizeGranularity = POINT_SIZE_GRANULARITY;
716 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
717 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
718 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
719 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
720 ctx->Const.LineWidthGranularity = LINE_WIDTH_GRANULARITY;
721 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
Brian Paul4bdcfe52000-04-17 17:57:04 +0000722 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +0000723 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
724 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
jtgafb833d1999-08-19 00:55:39 +0000725
Brian Paul4d053dd2000-01-14 04:45:47 +0000726 /* Modelview matrix */
727 gl_matrix_ctr( &ctx->ModelView );
728 gl_matrix_alloc_inv( &ctx->ModelView );
729
730 ctx->ModelViewStackDepth = 0;
Brian Paul7fc29c52000-03-06 17:03:03 +0000731 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
Brian Paul4d053dd2000-01-14 04:45:47 +0000732 gl_matrix_ctr( &ctx->ModelViewStack[i] );
733 gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
734 }
735
736 /* Projection matrix - need inv for user clipping in clip space*/
737 gl_matrix_ctr( &ctx->ProjectionMatrix );
738 gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
739
740 gl_matrix_ctr( &ctx->ModelProjectMatrix );
741 gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
742 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
743
744 ctx->ProjectionStackDepth = 0;
745 ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
746 ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
747
Brian Paul7fc29c52000-03-06 17:03:03 +0000748 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
Brian Paul4d053dd2000-01-14 04:45:47 +0000749 gl_matrix_ctr( &ctx->ProjectionStack[i] );
750 gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
751 }
752
753 /* Texture matrix */
754 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
755 gl_matrix_ctr( &ctx->TextureMatrix[i] );
756 ctx->TextureStackDepth[i] = 0;
Brian Paul7fc29c52000-03-06 17:03:03 +0000757 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
Brian Paul4d053dd2000-01-14 04:45:47 +0000758 ctx->TextureStack[i][j].inv = 0;
jtgafb833d1999-08-19 00:55:39 +0000759 }
Brian Paul4d053dd2000-01-14 04:45:47 +0000760 }
jtgafb833d1999-08-19 00:55:39 +0000761
Brian Paul250069d2000-04-08 18:57:45 +0000762 /* Color matrix */
763 gl_matrix_ctr(&ctx->ColorMatrix);
764 ctx->ColorStackDepth = 0;
765 for (j = 0; j < MAX_COLOR_STACK_DEPTH - 1; j++) {
766 gl_matrix_ctr(&ctx->ColorStack[j]);
767 }
768
Brian Paul4d053dd2000-01-14 04:45:47 +0000769 /* Accumulate buffer group */
770 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
jtgafb833d1999-08-19 00:55:39 +0000771
Brian Paul4d053dd2000-01-14 04:45:47 +0000772 /* Color buffer group */
773 ctx->Color.IndexMask = 0xffffffff;
774 ctx->Color.ColorMask[0] = 0xff;
775 ctx->Color.ColorMask[1] = 0xff;
776 ctx->Color.ColorMask[2] = 0xff;
777 ctx->Color.ColorMask[3] = 0xff;
778 ctx->Color.SWmasking = GL_FALSE;
779 ctx->Color.ClearIndex = 0;
780 ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
781 ctx->Color.DrawBuffer = GL_FRONT;
782 ctx->Color.AlphaEnabled = GL_FALSE;
783 ctx->Color.AlphaFunc = GL_ALWAYS;
784 ctx->Color.AlphaRef = 0;
785 ctx->Color.BlendEnabled = GL_FALSE;
786 ctx->Color.BlendSrcRGB = GL_ONE;
787 ctx->Color.BlendDstRGB = GL_ZERO;
788 ctx->Color.BlendSrcA = GL_ONE;
789 ctx->Color.BlendDstA = GL_ZERO;
790 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
791 ctx->Color.BlendFunc = NULL; /* this pointer set only when needed */
792 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
793 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
794 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
795 ctx->Color.SWLogicOpEnabled = GL_FALSE;
796 ctx->Color.LogicOp = GL_COPY;
797 ctx->Color.DitherFlag = GL_TRUE;
798 ctx->Color.MultiDrawBuffer = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000799
Brian Paul4d053dd2000-01-14 04:45:47 +0000800 /* Current group */
801 ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
802 ctx->Current.Index = 1;
803 for (i=0; i<MAX_TEXTURE_UNITS; i++)
804 ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
805 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
806 ctx->Current.RasterDistance = 0.0;
807 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
808 ctx->Current.RasterIndex = 1;
809 for (i=0; i<MAX_TEXTURE_UNITS; i++)
810 ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
811 ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
812 ctx->Current.RasterPosValid = GL_TRUE;
813 ctx->Current.EdgeFlag = GL_TRUE;
814 ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
815 ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
jtgafb833d1999-08-19 00:55:39 +0000816
Brian Paul4d053dd2000-01-14 04:45:47 +0000817 ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
818 VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
jtgafb833d1999-08-19 00:55:39 +0000819
Brian Paul4d053dd2000-01-14 04:45:47 +0000820 init_fallback_arrays( ctx );
jtgafb833d1999-08-19 00:55:39 +0000821
Brian Paul4d053dd2000-01-14 04:45:47 +0000822 /* Depth buffer group */
823 ctx->Depth.Test = GL_FALSE;
824 ctx->Depth.Clear = 1.0;
825 ctx->Depth.Func = GL_LESS;
826 ctx->Depth.Mask = GL_TRUE;
Brian Paul1b2ff692000-03-11 23:23:26 +0000827 ctx->Depth.OcclusionTest = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000828
Brian Paul4d053dd2000-01-14 04:45:47 +0000829 /* Evaluators group */
830 ctx->Eval.Map1Color4 = GL_FALSE;
831 ctx->Eval.Map1Index = GL_FALSE;
832 ctx->Eval.Map1Normal = GL_FALSE;
833 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
834 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
835 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
836 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
837 ctx->Eval.Map1Vertex3 = GL_FALSE;
838 ctx->Eval.Map1Vertex4 = GL_FALSE;
839 ctx->Eval.Map2Color4 = GL_FALSE;
840 ctx->Eval.Map2Index = GL_FALSE;
841 ctx->Eval.Map2Normal = GL_FALSE;
842 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
843 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
844 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
845 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
846 ctx->Eval.Map2Vertex3 = GL_FALSE;
847 ctx->Eval.Map2Vertex4 = GL_FALSE;
848 ctx->Eval.AutoNormal = GL_FALSE;
849 ctx->Eval.MapGrid1un = 1;
850 ctx->Eval.MapGrid1u1 = 0.0;
851 ctx->Eval.MapGrid1u2 = 1.0;
852 ctx->Eval.MapGrid2un = 1;
853 ctx->Eval.MapGrid2vn = 1;
854 ctx->Eval.MapGrid2u1 = 0.0;
855 ctx->Eval.MapGrid2u2 = 1.0;
856 ctx->Eval.MapGrid2v1 = 0.0;
857 ctx->Eval.MapGrid2v2 = 1.0;
jtgafb833d1999-08-19 00:55:39 +0000858
Brian Paul4d053dd2000-01-14 04:45:47 +0000859 /* Evaluator data */
860 {
861 static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
862 static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
863 static GLfloat index[1] = { 1.0 };
864 static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
865 static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
jtgafb833d1999-08-19 00:55:39 +0000866
Brian Paul4d053dd2000-01-14 04:45:47 +0000867 init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
868 init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
869 init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
870 init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
871 init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
872 init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
873 init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
874 init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
875 init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
jtgafb833d1999-08-19 00:55:39 +0000876
Brian Paul4d053dd2000-01-14 04:45:47 +0000877 init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
878 init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
879 init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
880 init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
881 init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
882 init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
883 init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
884 init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
885 init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
886 }
jtgafb833d1999-08-19 00:55:39 +0000887
Brian Paul4d053dd2000-01-14 04:45:47 +0000888 /* Fog group */
889 ctx->Fog.Enabled = GL_FALSE;
890 ctx->Fog.Mode = GL_EXP;
891 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
892 ctx->Fog.Index = 0.0;
893 ctx->Fog.Density = 1.0;
894 ctx->Fog.Start = 0.0;
895 ctx->Fog.End = 1.0;
jtgafb833d1999-08-19 00:55:39 +0000896
Brian Paul4d053dd2000-01-14 04:45:47 +0000897 /* Hint group */
898 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
899 ctx->Hint.PointSmooth = GL_DONT_CARE;
900 ctx->Hint.LineSmooth = GL_DONT_CARE;
901 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
902 ctx->Hint.Fog = GL_DONT_CARE;
jtgafb833d1999-08-19 00:55:39 +0000903
Brian Paul4d053dd2000-01-14 04:45:47 +0000904 ctx->Hint.AllowDrawWin = GL_TRUE;
Brian Paul8cce3142000-04-10 15:52:25 +0000905 ctx->Hint.AllowDrawFrg = GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000906 ctx->Hint.AllowDrawMem = GL_TRUE;
907 ctx->Hint.StrictLighting = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000908
Brian Paul0771d152000-04-07 00:19:41 +0000909 /* Histogram group */
910 ctx->Histogram.Width = 0;
911 ctx->Histogram.Format = GL_RGBA;
912 ctx->Histogram.Sink = GL_FALSE;
913 ctx->Histogram.RedSize = 0xffffffff;
914 ctx->Histogram.GreenSize = 0xffffffff;
915 ctx->Histogram.BlueSize = 0xffffffff;
916 ctx->Histogram.AlphaSize = 0xffffffff;
917 ctx->Histogram.LuminanceSize = 0xffffffff;
918 for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
919 ctx->Histogram.Count[i][0] = 0;
920 ctx->Histogram.Count[i][1] = 0;
921 ctx->Histogram.Count[i][2] = 0;
922 ctx->Histogram.Count[i][3] = 0;
923 }
924
925 /* Min/Max group */
926 ctx->MinMax.Format = GL_RGBA;
927 ctx->MinMax.Sink = GL_FALSE;
928 ctx->MinMax.Min[RCOMP] = 1000; ctx->MinMax.Max[RCOMP] = -1000;
929 ctx->MinMax.Min[GCOMP] = 1000; ctx->MinMax.Max[GCOMP] = -1000;
930 ctx->MinMax.Min[BCOMP] = 1000; ctx->MinMax.Max[BCOMP] = -1000;
931 ctx->MinMax.Min[ACOMP] = 1000; ctx->MinMax.Max[ACOMP] = -1000;
932
933
934
Brian Paul4d053dd2000-01-14 04:45:47 +0000935 /* Pipeline */
936 gl_pipeline_init( ctx );
937 gl_cva_init( ctx );
jtgafb833d1999-08-19 00:55:39 +0000938
Brian Paul4d053dd2000-01-14 04:45:47 +0000939 /* Extensions */
940 gl_extensions_ctr( ctx );
jtgafb833d1999-08-19 00:55:39 +0000941
Brian Paul4d053dd2000-01-14 04:45:47 +0000942 ctx->AllowVertexCull = CLIP_CULLED_BIT;
jtgafb833d1999-08-19 00:55:39 +0000943
Brian Paul4d053dd2000-01-14 04:45:47 +0000944 /* Lighting group */
945 for (i=0;i<MAX_LIGHTS;i++) {
946 init_light( &ctx->Light.Light[i], i );
947 }
948 make_empty_list( &ctx->Light.EnabledList );
jtgafb833d1999-08-19 00:55:39 +0000949
Brian Paul4d053dd2000-01-14 04:45:47 +0000950 init_lightmodel( &ctx->Light.Model );
951 init_material( &ctx->Light.Material[0] );
952 init_material( &ctx->Light.Material[1] );
953 ctx->Light.ShadeModel = GL_SMOOTH;
954 ctx->Light.Enabled = GL_FALSE;
955 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
956 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
957 ctx->Light.ColorMaterialBitmask
958 = gl_material_bitmask( ctx,
959 GL_FRONT_AND_BACK,
960 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000961
Brian Paul4d053dd2000-01-14 04:45:47 +0000962 ctx->Light.ColorMaterialEnabled = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000963
Brian Paul4d053dd2000-01-14 04:45:47 +0000964 /* Lighting miscellaneous */
965 ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
966 make_empty_list( ctx->ShineTabList );
967 for (i = 0 ; i < 10 ; i++) {
968 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
969 s->shininess = -1;
970 s->refcount = 0;
971 insert_at_tail( ctx->ShineTabList, s );
972 }
973 for (i = 0 ; i < 4 ; i++) {
974 ctx->ShineTable[i] = ctx->ShineTabList->prev;
975 ctx->ShineTable[i]->refcount++;
976 }
jtgafb833d1999-08-19 00:55:39 +0000977
jtgafb833d1999-08-19 00:55:39 +0000978
Brian Paul4d053dd2000-01-14 04:45:47 +0000979 /* Line group */
980 ctx->Line.SmoothFlag = GL_FALSE;
981 ctx->Line.StippleFlag = GL_FALSE;
982 ctx->Line.Width = 1.0;
983 ctx->Line.StipplePattern = 0xffff;
984 ctx->Line.StippleFactor = 1;
jtgafb833d1999-08-19 00:55:39 +0000985
Brian Paul4d053dd2000-01-14 04:45:47 +0000986 /* Display List group */
987 ctx->List.ListBase = 0;
jtgafb833d1999-08-19 00:55:39 +0000988
Brian Paul4d053dd2000-01-14 04:45:47 +0000989 /* Pixel group */
990 ctx->Pixel.RedBias = 0.0;
991 ctx->Pixel.RedScale = 1.0;
992 ctx->Pixel.GreenBias = 0.0;
993 ctx->Pixel.GreenScale = 1.0;
994 ctx->Pixel.BlueBias = 0.0;
995 ctx->Pixel.BlueScale = 1.0;
996 ctx->Pixel.AlphaBias = 0.0;
997 ctx->Pixel.AlphaScale = 1.0;
998 ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
999 ctx->Pixel.DepthBias = 0.0;
1000 ctx->Pixel.DepthScale = 1.0;
1001 ctx->Pixel.IndexOffset = 0;
1002 ctx->Pixel.IndexShift = 0;
1003 ctx->Pixel.ZoomX = 1.0;
1004 ctx->Pixel.ZoomY = 1.0;
1005 ctx->Pixel.MapColorFlag = GL_FALSE;
1006 ctx->Pixel.MapStencilFlag = GL_FALSE;
1007 ctx->Pixel.MapStoSsize = 1;
1008 ctx->Pixel.MapItoIsize = 1;
1009 ctx->Pixel.MapItoRsize = 1;
1010 ctx->Pixel.MapItoGsize = 1;
1011 ctx->Pixel.MapItoBsize = 1;
1012 ctx->Pixel.MapItoAsize = 1;
1013 ctx->Pixel.MapRtoRsize = 1;
1014 ctx->Pixel.MapGtoGsize = 1;
1015 ctx->Pixel.MapBtoBsize = 1;
1016 ctx->Pixel.MapAtoAsize = 1;
1017 ctx->Pixel.MapStoS[0] = 0;
1018 ctx->Pixel.MapItoI[0] = 0;
1019 ctx->Pixel.MapItoR[0] = 0.0;
1020 ctx->Pixel.MapItoG[0] = 0.0;
1021 ctx->Pixel.MapItoB[0] = 0.0;
1022 ctx->Pixel.MapItoA[0] = 0.0;
1023 ctx->Pixel.MapItoR8[0] = 0;
1024 ctx->Pixel.MapItoG8[0] = 0;
1025 ctx->Pixel.MapItoB8[0] = 0;
1026 ctx->Pixel.MapItoA8[0] = 0;
1027 ctx->Pixel.MapRtoR[0] = 0.0;
1028 ctx->Pixel.MapGtoG[0] = 0.0;
1029 ctx->Pixel.MapBtoB[0] = 0.0;
1030 ctx->Pixel.MapAtoA[0] = 0.0;
Brian Paul2b2e9252000-04-07 16:27:26 +00001031 ctx->Pixel.HistogramEnabled = GL_FALSE;
1032 ctx->Pixel.MinMaxEnabled = GL_FALSE;
1033 ctx->Pixel.PixelTextureEnabled = GL_FALSE;
1034 ctx->Pixel.FragmentRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
1035 ctx->Pixel.FragmentAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
Brian Paul82b02f02000-05-07 20:37:40 +00001036 ASSIGN_4V(ctx->Pixel.PostColorMatrixScale, 1.0, 1.0, 1.0, 1.0);
1037 ASSIGN_4V(ctx->Pixel.PostColorMatrixBias, 0.0, 0.0, 0.0, 0.0);
1038 ASSIGN_4V(ctx->Pixel.ColorTableScale, 1.0, 1.0, 1.0, 1.0);
1039 ASSIGN_4V(ctx->Pixel.ColorTableBias, 0.0, 0.0, 0.0, 0.0);
Brian Paul4fe34b22000-04-11 15:07:48 +00001040 ctx->Pixel.ColorTableEnabled = GL_FALSE;
1041 ctx->Pixel.PostConvolutionColorTableEnabled = GL_FALSE;
1042 ctx->Pixel.PostColorMatrixColorTableEnabled = GL_FALSE;
Brian Paul82b02f02000-05-07 20:37:40 +00001043 ctx->Pixel.Convolution1DEnabled = GL_FALSE;
1044 ctx->Pixel.Convolution2DEnabled = GL_FALSE;
1045 ctx->Pixel.Separable2DEnabled = GL_FALSE;
1046 for (i = 0; i < 3; i++) {
1047 ASSIGN_4V(ctx->Pixel.ConvolutionBorderColor[i], 0.0, 0.0, 0.0, 0.0);
1048 ctx->Pixel.ConvolutionBorderMode[i] = GL_REDUCE;
1049 ASSIGN_4V(ctx->Pixel.ConvolutionFilterScale[i], 1.0, 1.0, 1.0, 1.0);
1050 ASSIGN_4V(ctx->Pixel.ConvolutionFilterBias[i], 0.0, 0.0, 0.0, 0.0);
1051 }
1052 ASSIGN_4V(ctx->Pixel.PostConvolutionScale, 1.0, 1.0, 1.0, 1.0);
1053 ASSIGN_4V(ctx->Pixel.PostConvolutionBias, 0.0, 0.0, 0.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +00001054
Brian Paul4d053dd2000-01-14 04:45:47 +00001055 /* Point group */
1056 ctx->Point.SmoothFlag = GL_FALSE;
1057 ctx->Point.Size = 1.0;
1058 ctx->Point.Params[0] = 1.0;
1059 ctx->Point.Params[1] = 0.0;
1060 ctx->Point.Params[2] = 0.0;
1061 ctx->Point.Attenuated = GL_FALSE;
1062 ctx->Point.MinSize = 0.0;
1063 ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
1064 ctx->Point.Threshold = 1.0;
jtgafb833d1999-08-19 00:55:39 +00001065
Brian Paul4d053dd2000-01-14 04:45:47 +00001066 /* Polygon group */
1067 ctx->Polygon.CullFlag = GL_FALSE;
1068 ctx->Polygon.CullFaceMode = GL_BACK;
1069 ctx->Polygon.FrontFace = GL_CCW;
1070 ctx->Polygon.FrontBit = 0;
1071 ctx->Polygon.FrontMode = GL_FILL;
1072 ctx->Polygon.BackMode = GL_FILL;
1073 ctx->Polygon.Unfilled = GL_FALSE;
1074 ctx->Polygon.SmoothFlag = GL_FALSE;
1075 ctx->Polygon.StippleFlag = GL_FALSE;
1076 ctx->Polygon.OffsetFactor = 0.0F;
1077 ctx->Polygon.OffsetUnits = 0.0F;
1078 ctx->Polygon.OffsetPoint = GL_FALSE;
1079 ctx->Polygon.OffsetLine = GL_FALSE;
1080 ctx->Polygon.OffsetFill = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001081
Brian Paul4d053dd2000-01-14 04:45:47 +00001082 /* Polygon Stipple group */
1083 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
jtgafb833d1999-08-19 00:55:39 +00001084
Brian Paul4d053dd2000-01-14 04:45:47 +00001085 /* Scissor group */
1086 ctx->Scissor.Enabled = GL_FALSE;
1087 ctx->Scissor.X = 0;
1088 ctx->Scissor.Y = 0;
1089 ctx->Scissor.Width = 0;
1090 ctx->Scissor.Height = 0;
jtgafb833d1999-08-19 00:55:39 +00001091
Brian Paul4d053dd2000-01-14 04:45:47 +00001092 /* Stencil group */
1093 ctx->Stencil.Enabled = GL_FALSE;
1094 ctx->Stencil.Function = GL_ALWAYS;
1095 ctx->Stencil.FailFunc = GL_KEEP;
1096 ctx->Stencil.ZPassFunc = GL_KEEP;
1097 ctx->Stencil.ZFailFunc = GL_KEEP;
1098 ctx->Stencil.Ref = 0;
1099 ctx->Stencil.ValueMask = STENCIL_MAX;
1100 ctx->Stencil.Clear = 0;
1101 ctx->Stencil.WriteMask = STENCIL_MAX;
jtgafb833d1999-08-19 00:55:39 +00001102
Brian Paul4d053dd2000-01-14 04:45:47 +00001103 /* Texture group */
1104 ctx->Texture.CurrentUnit = 0; /* multitexture */
1105 ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
1106 ctx->Texture.Enabled = 0;
1107 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1108 init_texture_unit( ctx, i );
Brian Paul4bdcfe52000-04-17 17:57:04 +00001109 _mesa_init_colortable(&ctx->Texture.Palette);
jtgafb833d1999-08-19 00:55:39 +00001110
Brian Paul4d053dd2000-01-14 04:45:47 +00001111 /* Transformation group */
1112 ctx->Transform.MatrixMode = GL_MODELVIEW;
1113 ctx->Transform.Normalize = GL_FALSE;
1114 ctx->Transform.RescaleNormals = GL_FALSE;
1115 for (i=0;i<MAX_CLIP_PLANES;i++) {
1116 ctx->Transform.ClipEnabled[i] = GL_FALSE;
1117 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1118 }
1119 ctx->Transform.AnyClip = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001120
Brian Paul4d053dd2000-01-14 04:45:47 +00001121 /* Viewport group */
1122 ctx->Viewport.X = 0;
1123 ctx->Viewport.Y = 0;
1124 ctx->Viewport.Width = 0;
1125 ctx->Viewport.Height = 0;
1126 ctx->Viewport.Near = 0.0;
1127 ctx->Viewport.Far = 1.0;
1128 gl_matrix_ctr(&ctx->Viewport.WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001129
1130#define Sz 10
1131#define Tz 14
Brian Pauled30dfa2000-03-03 17:47:39 +00001132 ctx->Viewport.WindowMap.m[Sz] = 0.5 * ctx->Visual->DepthMaxF;
1133 ctx->Viewport.WindowMap.m[Tz] = 0.5 * ctx->Visual->DepthMaxF;
jtgafb833d1999-08-19 00:55:39 +00001134#undef Sz
1135#undef Tz
1136
Brian Paul4d053dd2000-01-14 04:45:47 +00001137 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1138 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
jtgafb833d1999-08-19 00:55:39 +00001139
Brian Paul4d053dd2000-01-14 04:45:47 +00001140 /* Vertex arrays */
1141 ctx->Array.Vertex.Size = 4;
1142 ctx->Array.Vertex.Type = GL_FLOAT;
1143 ctx->Array.Vertex.Stride = 0;
1144 ctx->Array.Vertex.StrideB = 0;
1145 ctx->Array.Vertex.Ptr = NULL;
1146 ctx->Array.Vertex.Enabled = GL_FALSE;
1147 ctx->Array.Normal.Type = GL_FLOAT;
1148 ctx->Array.Normal.Stride = 0;
1149 ctx->Array.Normal.StrideB = 0;
1150 ctx->Array.Normal.Ptr = NULL;
1151 ctx->Array.Normal.Enabled = GL_FALSE;
1152 ctx->Array.Color.Size = 4;
1153 ctx->Array.Color.Type = GL_FLOAT;
1154 ctx->Array.Color.Stride = 0;
1155 ctx->Array.Color.StrideB = 0;
1156 ctx->Array.Color.Ptr = NULL;
1157 ctx->Array.Color.Enabled = GL_FALSE;
1158 ctx->Array.Index.Type = GL_FLOAT;
1159 ctx->Array.Index.Stride = 0;
1160 ctx->Array.Index.StrideB = 0;
1161 ctx->Array.Index.Ptr = NULL;
1162 ctx->Array.Index.Enabled = GL_FALSE;
1163 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1164 ctx->Array.TexCoord[i].Size = 4;
1165 ctx->Array.TexCoord[i].Type = GL_FLOAT;
1166 ctx->Array.TexCoord[i].Stride = 0;
1167 ctx->Array.TexCoord[i].StrideB = 0;
1168 ctx->Array.TexCoord[i].Ptr = NULL;
1169 ctx->Array.TexCoord[i].Enabled = GL_FALSE;
1170 }
1171 ctx->Array.TexCoordInterleaveFactor = 1;
1172 ctx->Array.EdgeFlag.Stride = 0;
1173 ctx->Array.EdgeFlag.StrideB = 0;
1174 ctx->Array.EdgeFlag.Ptr = NULL;
1175 ctx->Array.EdgeFlag.Enabled = GL_FALSE;
1176 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1177
1178 /* Pixel transfer */
1179 ctx->Pack.Alignment = 4;
1180 ctx->Pack.RowLength = 0;
1181 ctx->Pack.ImageHeight = 0;
1182 ctx->Pack.SkipPixels = 0;
1183 ctx->Pack.SkipRows = 0;
1184 ctx->Pack.SkipImages = 0;
1185 ctx->Pack.SwapBytes = GL_FALSE;
1186 ctx->Pack.LsbFirst = GL_FALSE;
1187 ctx->Unpack.Alignment = 4;
1188 ctx->Unpack.RowLength = 0;
1189 ctx->Unpack.ImageHeight = 0;
1190 ctx->Unpack.SkipPixels = 0;
1191 ctx->Unpack.SkipRows = 0;
1192 ctx->Unpack.SkipImages = 0;
1193 ctx->Unpack.SwapBytes = GL_FALSE;
1194 ctx->Unpack.LsbFirst = GL_FALSE;
1195
1196 /* Feedback */
1197 ctx->Feedback.Type = GL_2D; /* TODO: verify */
1198 ctx->Feedback.Buffer = NULL;
1199 ctx->Feedback.BufferSize = 0;
1200 ctx->Feedback.Count = 0;
1201
1202 /* Selection/picking */
1203 ctx->Select.Buffer = NULL;
1204 ctx->Select.BufferSize = 0;
1205 ctx->Select.BufferCount = 0;
1206 ctx->Select.Hits = 0;
1207 ctx->Select.NameStackDepth = 0;
1208
1209 /* Optimized Accum buffer */
1210 ctx->IntegerAccumMode = GL_TRUE;
1211 ctx->IntegerAccumScaler = 0.0;
1212
1213 /* Renderer and client attribute stacks */
1214 ctx->AttribStackDepth = 0;
1215 ctx->ClientAttribStackDepth = 0;
1216
Brian Paul13811372000-04-12 00:27:37 +00001217 /* Display list */
1218 ctx->CallDepth = 0;
1219 ctx->ExecuteFlag = GL_TRUE;
1220 ctx->CompileFlag = GL_FALSE;
1221 ctx->CurrentListPtr = NULL;
1222 ctx->CurrentBlock = NULL;
1223 ctx->CurrentListNum = 0;
1224 ctx->CurrentPos = 0;
1225
1226 /* Color tables */
Brian Paul4bdcfe52000-04-17 17:57:04 +00001227 _mesa_init_colortable(&ctx->ColorTable);
1228 _mesa_init_colortable(&ctx->ProxyColorTable);
1229 _mesa_init_colortable(&ctx->PostConvolutionColorTable);
1230 _mesa_init_colortable(&ctx->ProxyPostConvolutionColorTable);
1231 _mesa_init_colortable(&ctx->PostColorMatrixColorTable);
1232 _mesa_init_colortable(&ctx->ProxyPostColorMatrixColorTable);
Brian Paul13811372000-04-12 00:27:37 +00001233
Brian Paul4d053dd2000-01-14 04:45:47 +00001234 /* Miscellaneous */
1235 ctx->NewState = NEW_ALL;
1236 ctx->RenderMode = GL_RENDER;
1237 ctx->StippleCounter = 0;
1238 ctx->NeedNormals = GL_FALSE;
1239 ctx->DoViewportMapping = GL_TRUE;
1240
1241 ctx->NeedEyeCoords = GL_FALSE;
1242 ctx->NeedEyeNormals = GL_FALSE;
1243 ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
1244
Brian Paul4d053dd2000-01-14 04:45:47 +00001245 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1246
1247 ctx->CatchSignals = GL_TRUE;
Brian Paul1b2ff692000-03-11 23:23:26 +00001248 ctx->OcclusionResult = GL_FALSE;
Brian Paul7e67fb42000-04-04 15:14:10 +00001249 ctx->OcclusionResultSaved = GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001250
1251 /* For debug/development only */
1252 ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
1253 ctx->FirstTimeCurrent = GL_TRUE;
1254
1255 /* Dither disable */
1256 ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
1257 if (ctx->NoDither) {
1258 if (getenv("MESA_DEBUG")) {
1259 fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
jtgafb833d1999-08-19 00:55:39 +00001260 }
Brian Paul4d053dd2000-01-14 04:45:47 +00001261 ctx->Color.DitherFlag = GL_FALSE;
Brian Paul00037781999-12-17 14:52:35 +00001262 }
1263}
1264
1265
1266
1267
jtgafb833d1999-08-19 00:55:39 +00001268/*
1269 * Allocate the proxy textures. If we run out of memory part way through
1270 * the allocations clean up and return GL_FALSE.
1271 * Return: GL_TRUE=success, GL_FALSE=failure
1272 */
Brian Paul178a1c52000-04-22 01:05:00 +00001273static GLboolean
1274alloc_proxy_textures( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001275{
1276 GLboolean out_of_memory;
1277 GLint i;
1278
1279 ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
1280 if (!ctx->Texture.Proxy1D) {
1281 return GL_FALSE;
1282 }
1283
1284 ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
1285 if (!ctx->Texture.Proxy2D) {
1286 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1287 return GL_FALSE;
1288 }
1289
1290 ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
1291 if (!ctx->Texture.Proxy3D) {
1292 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1293 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1294 return GL_FALSE;
1295 }
1296
1297 out_of_memory = GL_FALSE;
1298 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
Brian Paul021a5252000-03-27 17:54:17 +00001299 ctx->Texture.Proxy1D->Image[i] = _mesa_alloc_texture_image();
1300 ctx->Texture.Proxy2D->Image[i] = _mesa_alloc_texture_image();
1301 ctx->Texture.Proxy3D->Image[i] = _mesa_alloc_texture_image();
jtgafb833d1999-08-19 00:55:39 +00001302 if (!ctx->Texture.Proxy1D->Image[i]
1303 || !ctx->Texture.Proxy2D->Image[i]
1304 || !ctx->Texture.Proxy3D->Image[i]) {
1305 out_of_memory = GL_TRUE;
1306 }
1307 }
1308 if (out_of_memory) {
1309 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1310 if (ctx->Texture.Proxy1D->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +00001311 _mesa_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
jtgafb833d1999-08-19 00:55:39 +00001312 }
1313 if (ctx->Texture.Proxy2D->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +00001314 _mesa_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
jtgafb833d1999-08-19 00:55:39 +00001315 }
1316 if (ctx->Texture.Proxy3D->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +00001317 _mesa_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
jtgafb833d1999-08-19 00:55:39 +00001318 }
1319 }
1320 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1321 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1322 gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
1323 return GL_FALSE;
1324 }
1325 else {
1326 return GL_TRUE;
1327 }
1328}
1329
1330
1331
jtgafb833d1999-08-19 00:55:39 +00001332/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001333 * Initialize a GLcontext struct.
jtgafb833d1999-08-19 00:55:39 +00001334 */
Brian Paul178a1c52000-04-22 01:05:00 +00001335GLboolean
1336_mesa_initialize_context( GLcontext *ctx,
1337 GLvisual *visual,
1338 GLcontext *share_list,
1339 void *driver_ctx,
1340 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001341{
jtgafb833d1999-08-19 00:55:39 +00001342 (void) direct; /* not used */
1343
jtgafb833d1999-08-19 00:55:39 +00001344 /* misc one-time initializations */
1345 one_time_init();
1346
jtgafb833d1999-08-19 00:55:39 +00001347 ctx->DriverCtx = driver_ctx;
1348 ctx->Visual = visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001349 ctx->DrawBuffer = NULL;
1350 ctx->ReadBuffer = NULL;
jtgafb833d1999-08-19 00:55:39 +00001351
1352 ctx->VB = gl_vb_create_for_immediate( ctx );
1353 if (!ctx->VB) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001354 FREE( ctx );
Brian Paul4d053dd2000-01-14 04:45:47 +00001355 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001356 }
1357 ctx->input = ctx->VB->IM;
1358
1359 ctx->PB = gl_alloc_pb();
1360 if (!ctx->PB) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001361 FREE( ctx->VB );
1362 FREE( ctx );
Brian Paul4d053dd2000-01-14 04:45:47 +00001363 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001364 }
1365
1366 if (share_list) {
1367 /* share the group of display lists of another context */
1368 ctx->Shared = share_list->Shared;
1369 }
1370 else {
1371 /* allocate new group of display lists */
1372 ctx->Shared = alloc_shared_state();
1373 if (!ctx->Shared) {
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001374 FREE(ctx->VB);
1375 FREE(ctx->PB);
1376 FREE(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001377 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001378 }
1379 }
Brian Paul9560f052000-01-31 23:11:39 +00001380 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001381 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001382 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001383
Brian Paul4d053dd2000-01-14 04:45:47 +00001384 init_attrib_groups( ctx );
1385
jtgafb833d1999-08-19 00:55:39 +00001386 gl_reset_vb( ctx->VB );
1387 gl_reset_input( ctx );
1388
jtgafb833d1999-08-19 00:55:39 +00001389 if (visual->DBflag) {
1390 ctx->Color.DrawBuffer = GL_BACK;
1391 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
1392 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
1393 ctx->Pixel.ReadBuffer = GL_BACK;
1394 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
1395 }
1396 else {
1397 ctx->Color.DrawBuffer = GL_FRONT;
1398 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
1399 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
1400 ctx->Pixel.ReadBuffer = GL_FRONT;
1401 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
1402 }
1403
jtgafb833d1999-08-19 00:55:39 +00001404 if (!alloc_proxy_textures(ctx)) {
1405 free_shared_state(ctx, ctx->Shared);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001406 FREE(ctx->VB);
1407 FREE(ctx->PB);
1408 FREE(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001409 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001410 }
jtgafb833d1999-08-19 00:55:39 +00001411
Brian Paulfbd8f211999-11-11 01:22:25 +00001412 /* setup API dispatch tables */
Brian Paul959f8022000-03-19 01:10:11 +00001413 ctx->Exec = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1414 ctx->Save = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001415 if (!ctx->Exec || !ctx->Save) {
1416 free_shared_state(ctx, ctx->Shared);
1417 FREE(ctx->VB);
1418 FREE(ctx->PB);
1419 if (ctx->Exec)
1420 FREE(ctx->Exec);
1421 FREE(ctx);
1422 }
1423 _mesa_init_exec_table( ctx->Exec );
1424 _mesa_init_dlist_table( ctx->Save );
1425 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001426
Brian Paul4d053dd2000-01-14 04:45:47 +00001427 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001428}
1429
jtgafb833d1999-08-19 00:55:39 +00001430
1431
1432/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001433 * Allocate and initialize a GLcontext structure.
1434 * Input: visual - a GLvisual pointer
1435 * sharelist - another context to share display lists with or NULL
1436 * driver_ctx - pointer to device driver's context state struct
1437 * Return: pointer to a new gl_context struct or NULL if error.
1438 */
Brian Paul178a1c52000-04-22 01:05:00 +00001439GLcontext *
1440gl_create_context( GLvisual *visual,
1441 GLcontext *share_list,
1442 void *driver_ctx,
1443 GLboolean direct )
Brian Paul4d053dd2000-01-14 04:45:47 +00001444{
1445 GLcontext *ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
1446 if (!ctx) {
1447 return NULL;
1448 }
1449
Brian Paul178a1c52000-04-22 01:05:00 +00001450 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001451 return ctx;
1452 }
1453 else {
1454 FREE(ctx);
1455 return NULL;
1456 }
1457}
1458
1459
1460
1461/*
1462 * Free the data associated with the given context.
1463 * But don't free() the GLcontext struct itself!
1464 */
Brian Paul178a1c52000-04-22 01:05:00 +00001465void
1466gl_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001467{
Brian Paul4d053dd2000-01-14 04:45:47 +00001468 struct gl_shine_tab *s, *tmps;
Brian Paul7fc29c52000-03-06 17:03:03 +00001469 GLuint i, j;
Brian Paul4d053dd2000-01-14 04:45:47 +00001470
1471 /* if we're destroying the current context, unbind it first */
1472 if (ctx == gl_get_current_context()) {
1473 gl_make_current(NULL, NULL);
1474 }
1475
Brian Paul4d053dd2000-01-14 04:45:47 +00001476 gl_matrix_dtr( &ctx->ModelView );
Brian Paul7fc29c52000-03-06 17:03:03 +00001477 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001478 gl_matrix_dtr( &ctx->ModelViewStack[i] );
1479 }
1480 gl_matrix_dtr( &ctx->ProjectionMatrix );
Brian Paul7fc29c52000-03-06 17:03:03 +00001481 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001482 gl_matrix_dtr( &ctx->ProjectionStack[i] );
1483 }
Brian Paul7fc29c52000-03-06 17:03:03 +00001484 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1485 gl_matrix_dtr( &ctx->TextureMatrix[i] );
1486 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
1487 gl_matrix_dtr( &ctx->TextureStack[i][j] );
1488 }
1489 }
Brian Paul4d053dd2000-01-14 04:45:47 +00001490
1491 FREE( ctx->PB );
1492
Brian Paul4bdcfe52000-04-17 17:57:04 +00001493 if (ctx->input != ctx->VB->IM)
Brian Paul4d053dd2000-01-14 04:45:47 +00001494 gl_immediate_free( ctx->input );
1495
1496 gl_vb_free( ctx->VB );
1497
Brian Paul9560f052000-01-31 23:11:39 +00001498 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001499 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001500 assert(ctx->Shared->RefCount >= 0);
1501 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1502 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001503 /* free shared state */
1504 free_shared_state( ctx, ctx->Shared );
1505 }
1506
1507 foreach_s( s, tmps, ctx->ShineTabList ) {
1508 FREE( s );
1509 }
1510 FREE( ctx->ShineTabList );
1511
1512 /* Free proxy texture objects */
1513 gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
1514 gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
1515 gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
1516
1517 /* Free evaluator data */
1518 if (ctx->EvalMap.Map1Vertex3.Points)
1519 FREE( ctx->EvalMap.Map1Vertex3.Points );
1520 if (ctx->EvalMap.Map1Vertex4.Points)
1521 FREE( ctx->EvalMap.Map1Vertex4.Points );
1522 if (ctx->EvalMap.Map1Index.Points)
1523 FREE( ctx->EvalMap.Map1Index.Points );
1524 if (ctx->EvalMap.Map1Color4.Points)
1525 FREE( ctx->EvalMap.Map1Color4.Points );
1526 if (ctx->EvalMap.Map1Normal.Points)
1527 FREE( ctx->EvalMap.Map1Normal.Points );
1528 if (ctx->EvalMap.Map1Texture1.Points)
1529 FREE( ctx->EvalMap.Map1Texture1.Points );
1530 if (ctx->EvalMap.Map1Texture2.Points)
1531 FREE( ctx->EvalMap.Map1Texture2.Points );
1532 if (ctx->EvalMap.Map1Texture3.Points)
1533 FREE( ctx->EvalMap.Map1Texture3.Points );
1534 if (ctx->EvalMap.Map1Texture4.Points)
1535 FREE( ctx->EvalMap.Map1Texture4.Points );
1536
1537 if (ctx->EvalMap.Map2Vertex3.Points)
1538 FREE( ctx->EvalMap.Map2Vertex3.Points );
1539 if (ctx->EvalMap.Map2Vertex4.Points)
1540 FREE( ctx->EvalMap.Map2Vertex4.Points );
1541 if (ctx->EvalMap.Map2Index.Points)
1542 FREE( ctx->EvalMap.Map2Index.Points );
1543 if (ctx->EvalMap.Map2Color4.Points)
1544 FREE( ctx->EvalMap.Map2Color4.Points );
1545 if (ctx->EvalMap.Map2Normal.Points)
1546 FREE( ctx->EvalMap.Map2Normal.Points );
1547 if (ctx->EvalMap.Map2Texture1.Points)
1548 FREE( ctx->EvalMap.Map2Texture1.Points );
1549 if (ctx->EvalMap.Map2Texture2.Points)
1550 FREE( ctx->EvalMap.Map2Texture2.Points );
1551 if (ctx->EvalMap.Map2Texture3.Points)
1552 FREE( ctx->EvalMap.Map2Texture3.Points );
1553 if (ctx->EvalMap.Map2Texture4.Points)
1554 FREE( ctx->EvalMap.Map2Texture4.Points );
1555
Brian Paul4bdcfe52000-04-17 17:57:04 +00001556 _mesa_free_colortable_data( &ctx->ColorTable );
1557 _mesa_free_colortable_data( &ctx->PostConvolutionColorTable );
1558 _mesa_free_colortable_data( &ctx->PostColorMatrixColorTable );
1559 _mesa_free_colortable_data( &ctx->Texture.Palette );
1560
Brian Paul4d053dd2000-01-14 04:45:47 +00001561 /* Free cache of immediate buffers. */
1562 while (ctx->nr_im_queued-- > 0) {
1563 struct immediate * next = ctx->freed_im_queue->next;
1564 FREE( ctx->freed_im_queue );
1565 ctx->freed_im_queue = next;
1566 }
1567 gl_extensions_dtr(ctx);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001568
1569 FREE(ctx->Exec);
1570 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001571}
1572
1573
1574
1575/*
1576 * Destroy a GLcontext structure.
jtgafb833d1999-08-19 00:55:39 +00001577 */
Brian Paul178a1c52000-04-22 01:05:00 +00001578void
1579gl_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001580{
1581 if (ctx) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001582 gl_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001583 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001584 }
1585}
1586
1587
1588
1589/*
Brian Paul4d053dd2000-01-14 04:45:47 +00001590 * Called by the driver after both the context and driver are fully
1591 * initialized. Currently just reads the config file.
jtgafb833d1999-08-19 00:55:39 +00001592 */
Brian Paul178a1c52000-04-22 01:05:00 +00001593void
1594gl_context_initialize( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001595{
Brian Paul00037781999-12-17 14:52:35 +00001596 gl_read_config_file( ctx );
jtgafb833d1999-08-19 00:55:39 +00001597}
1598
1599
1600
1601/*
1602 * Copy attribute groups from one context to another.
1603 * Input: src - source context
1604 * dst - destination context
1605 * mask - bitwise OR of GL_*_BIT flags
1606 */
Brian Paul178a1c52000-04-22 01:05:00 +00001607void
1608gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001609{
1610 if (mask & GL_ACCUM_BUFFER_BIT) {
1611 MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
1612 }
1613 if (mask & GL_COLOR_BUFFER_BIT) {
1614 MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
1615 }
1616 if (mask & GL_CURRENT_BIT) {
1617 MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
1618 }
1619 if (mask & GL_DEPTH_BUFFER_BIT) {
1620 MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
1621 }
1622 if (mask & GL_ENABLE_BIT) {
1623 /* no op */
1624 }
1625 if (mask & GL_EVAL_BIT) {
1626 MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
1627 }
1628 if (mask & GL_FOG_BIT) {
1629 MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
1630 }
1631 if (mask & GL_HINT_BIT) {
1632 MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
1633 }
1634 if (mask & GL_LIGHTING_BIT) {
1635 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
Brian Paul00037781999-12-17 14:52:35 +00001636 /* gl_reinit_light_attrib( &dst->Light ); */
jtgafb833d1999-08-19 00:55:39 +00001637 }
1638 if (mask & GL_LINE_BIT) {
1639 MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
1640 }
1641 if (mask & GL_LIST_BIT) {
1642 MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
1643 }
1644 if (mask & GL_PIXEL_MODE_BIT) {
1645 MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
1646 }
1647 if (mask & GL_POINT_BIT) {
1648 MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
1649 }
1650 if (mask & GL_POLYGON_BIT) {
1651 MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
1652 }
1653 if (mask & GL_POLYGON_STIPPLE_BIT) {
1654 /* Use loop instead of MEMCPY due to problem with Portland Group's
1655 * C compiler. Reported by John Stone.
1656 */
1657 int i;
1658 for (i=0;i<32;i++) {
1659 dst->PolygonStipple[i] = src->PolygonStipple[i];
1660 }
1661 }
1662 if (mask & GL_SCISSOR_BIT) {
1663 MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
1664 }
1665 if (mask & GL_STENCIL_BUFFER_BIT) {
1666 MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
1667 }
1668 if (mask & GL_TEXTURE_BIT) {
1669 MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
1670 }
1671 if (mask & GL_TRANSFORM_BIT) {
1672 MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
1673 }
1674 if (mask & GL_VIEWPORT_BIT) {
1675 MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
1676 }
1677}
1678
1679
jtgafb833d1999-08-19 00:55:39 +00001680/*
Brian Paul00037781999-12-17 14:52:35 +00001681 * Set the current context, binding the given frame buffer to the context.
1682 */
1683void gl_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1684{
1685 gl_make_current2( newCtx, buffer, buffer );
1686}
1687
1688
1689/*
1690 * Bind the given context to the given draw-buffer and read-buffer
1691 * and make it the current context for this thread.
1692 */
1693void gl_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1694 GLframebuffer *readBuffer )
1695{
1696#if 0
Brian Paulf9b97d92000-01-28 20:17:42 +00001697 GLcontext *oldCtx = gl_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001698
1699 /* Flush the old context
1700 */
1701 if (oldCtx) {
1702 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx, "gl_make_current");
1703
1704 /* unbind frame buffers from context */
1705 if (oldCtx->DrawBuffer) {
1706 oldCtx->DrawBuffer = NULL;
1707 }
1708 if (oldCtx->ReadBuffer) {
1709 oldCtx->ReadBuffer = NULL;
1710 }
1711 }
1712#endif
1713
1714 /* We call this function periodically (just here for now) in
1715 * order to detect when multithreading has begun.
1716 */
1717 _glapi_check_multithread();
1718
Brian Paulf9b97d92000-01-28 20:17:42 +00001719 _glapi_set_context((void *) newCtx);
Brian Paul00037781999-12-17 14:52:35 +00001720 ASSERT(gl_get_current_context() == newCtx);
1721 if (newCtx) {
1722 SET_IMMEDIATE(newCtx, newCtx->input);
1723 _glapi_set_dispatch(newCtx->CurrentDispatch);
1724 }
1725 else {
1726 _glapi_set_dispatch(NULL); /* none current */
1727 }
1728
1729 if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
1730
1731 if (newCtx && drawBuffer && readBuffer) {
1732 /* TODO: check if newCtx and buffer's visual match??? */
1733 newCtx->DrawBuffer = drawBuffer;
1734 newCtx->ReadBuffer = readBuffer;
1735 newCtx->NewState = NEW_ALL; /* just to be safe */
1736 gl_update_state( newCtx );
1737 }
1738
1739 /* We can use this to help debug user's problems. Tell the to set
1740 * the MESA_INFO env variable before running their app. Then the
1741 * first time each context is made current we'll print some useful
1742 * information.
1743 */
1744 if (newCtx && newCtx->FirstTimeCurrent) {
1745 if (getenv("MESA_INFO")) {
1746 fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION));
1747 fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER));
1748 fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR));
1749 fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS));
Brian Paul09cb1481999-12-17 17:00:32 +00001750#if defined(THREADS)
1751 fprintf(stderr, "Mesa thread-safe: YES\n");
1752#else
1753 fprintf(stderr, "Mesa thread-safe: NO\n");
1754#endif
Brian Paul54287052000-01-17 20:00:15 +00001755#if defined(USE_X86_ASM)
1756 fprintf(stderr, "Mesa x86-optimized: YES\n");
1757#else
1758 fprintf(stderr, "Mesa x86-optimized: NO\n");
1759#endif
Brian Paul00037781999-12-17 14:52:35 +00001760 }
1761 newCtx->FirstTimeCurrent = GL_FALSE;
1762 }
1763}
1764
1765
1766
1767/*
1768 * Return current context handle for the calling thread.
1769 * This isn't the fastest way to get the current context.
1770 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1771 */
1772GLcontext *gl_get_current_context( void )
1773{
Brian Paulf9b97d92000-01-28 20:17:42 +00001774 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001775}
1776
1777
1778
1779/*
Brian Paulfbd8f211999-11-11 01:22:25 +00001780 * This should be called by device drivers just before they do a
1781 * swapbuffers. Any pending rendering commands will be executed.
jtgafb833d1999-08-19 00:55:39 +00001782 */
Brian Paulfbd8f211999-11-11 01:22:25 +00001783void
1784_mesa_swapbuffers(GLcontext *ctx)
jtgafb833d1999-08-19 00:55:39 +00001785{
Brian Paulfbd8f211999-11-11 01:22:25 +00001786 FLUSH_VB( ctx, "swap buffers" );
jtgafb833d1999-08-19 00:55:39 +00001787}
1788
1789
Brian Paul00037781999-12-17 14:52:35 +00001790
Brian Paulfbd8f211999-11-11 01:22:25 +00001791/*
1792 * Return pointer to this context's current API dispatch table.
1793 * It'll either be the immediate-mode execute dispatcher or the
1794 * display list compile dispatcher.
1795 */
1796struct _glapi_table *
1797_mesa_get_dispatch(GLcontext *ctx)
1798{
1799 return ctx->CurrentDispatch;
1800}
1801
1802
1803
jtgafb833d1999-08-19 00:55:39 +00001804/**********************************************************************/
1805/***** Miscellaneous functions *****/
1806/**********************************************************************/
1807
1808
1809/*
1810 * This function is called when the Mesa user has stumbled into a code
1811 * path which may not be implemented fully or correctly.
1812 */
1813void gl_problem( const GLcontext *ctx, const char *s )
1814{
1815 fprintf( stderr, "Mesa implementation error: %s\n", s );
1816 fprintf( stderr, "Report to mesa-bugs@mesa3d.org\n" );
1817 (void) ctx;
1818}
1819
1820
1821
1822/*
1823 * This is called to inform the user that he or she has tried to do
1824 * something illogical or if there's likely a bug in their program
1825 * (like enabled depth testing without a depth buffer).
1826 */
1827void gl_warning( const GLcontext *ctx, const char *s )
1828{
1829 GLboolean debug;
1830#ifdef DEBUG
1831 debug = GL_TRUE;
1832#else
1833 if (getenv("MESA_DEBUG")) {
1834 debug = GL_TRUE;
1835 }
1836 else {
1837 debug = GL_FALSE;
1838 }
1839#endif
1840 if (debug) {
1841 fprintf( stderr, "Mesa warning: %s\n", s );
1842 }
1843 (void) ctx;
1844}
1845
1846
1847
Brian Paulfa9df402000-02-02 19:16:46 +00001848/*
1849 * Compile an error into current display list.
1850 */
jtgafb833d1999-08-19 00:55:39 +00001851void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
1852{
1853 if (ctx->CompileFlag)
1854 gl_save_error( ctx, error, s );
1855
1856 if (ctx->ExecuteFlag)
1857 gl_error( ctx, error, s );
1858}
1859
1860
Brian Paulfa9df402000-02-02 19:16:46 +00001861
jtgafb833d1999-08-19 00:55:39 +00001862/*
1863 * This is Mesa's error handler. Normally, all that's done is the updating
1864 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1865 * environment variable "MESA_DEBUG" is defined then a real error message
1866 * is printed to stderr.
1867 * Input: error - the error value
1868 * s - a diagnostic string
1869 */
1870void gl_error( GLcontext *ctx, GLenum error, const char *s )
1871{
1872 GLboolean debug;
1873
1874#ifdef DEBUG
1875 debug = GL_TRUE;
1876#else
1877 if (getenv("MESA_DEBUG")) {
1878 debug = GL_TRUE;
1879 }
1880 else {
1881 debug = GL_FALSE;
1882 }
1883#endif
1884
1885 if (debug) {
1886 char errstr[1000];
1887
1888 switch (error) {
1889 case GL_NO_ERROR:
1890 strcpy( errstr, "GL_NO_ERROR" );
1891 break;
1892 case GL_INVALID_VALUE:
1893 strcpy( errstr, "GL_INVALID_VALUE" );
1894 break;
1895 case GL_INVALID_ENUM:
1896 strcpy( errstr, "GL_INVALID_ENUM" );
1897 break;
1898 case GL_INVALID_OPERATION:
1899 strcpy( errstr, "GL_INVALID_OPERATION" );
1900 break;
1901 case GL_STACK_OVERFLOW:
1902 strcpy( errstr, "GL_STACK_OVERFLOW" );
1903 break;
1904 case GL_STACK_UNDERFLOW:
1905 strcpy( errstr, "GL_STACK_UNDERFLOW" );
1906 break;
1907 case GL_OUT_OF_MEMORY:
1908 strcpy( errstr, "GL_OUT_OF_MEMORY" );
1909 break;
1910 default:
1911 strcpy( errstr, "unknown" );
1912 break;
1913 }
1914 fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
1915 }
1916
1917 if (ctx->ErrorValue==GL_NO_ERROR) {
1918 ctx->ErrorValue = error;
1919 }
1920
1921 /* Call device driver's error handler, if any. This is used on the Mac. */
1922 if (ctx->Driver.Error) {
1923 (*ctx->Driver.Error)( ctx );
1924 }
1925}
1926
1927
1928
Brian Paulfa9df402000-02-02 19:16:46 +00001929void
1930_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001931{
Brian Paulfa9df402000-02-02 19:16:46 +00001932 GET_CURRENT_CONTEXT(ctx);
1933 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFinish");
1934 if (ctx->Driver.Finish) {
1935 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001936 }
1937}
1938
1939
1940
Brian Paulfa9df402000-02-02 19:16:46 +00001941void
1942_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001943{
Brian Paulfa9df402000-02-02 19:16:46 +00001944 GET_CURRENT_CONTEXT(ctx);
1945 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFlush");
1946 if (ctx->Driver.Flush) {
1947 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001948 }
jtgafb833d1999-08-19 00:55:39 +00001949}